summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorPeter Collingbourne <peter@pcc.me.uk>2014-12-03 02:08:38 +0000
committerPeter Collingbourne <peter@pcc.me.uk>2014-12-03 02:08:38 +0000
commit51d2de7b9edfb8e5aad0c533c249b16989a9af41 (patch)
treebeb252c6d4c847126fcb191cc5e2a79a5e97e227 /llvm/lib
parent4c71cc1dcb57d1beb5acbd5fdb0cba7e8c246a48 (diff)
downloadbcm5719-llvm-51d2de7b9edfb8e5aad0c533c249b16989a9af41.tar.gz
bcm5719-llvm-51d2de7b9edfb8e5aad0c533c249b16989a9af41.zip
Prologue support
Patch by Ben Gamari! This redefines the `prefix` attribute introduced previously and introduces a `prologue` attribute. There are a two primary usecases that these attributes aim to serve, 1. Function prologue sigils 2. Function hot-patching: Enable the user to insert `nop` operations at the beginning of the function which can later be safely replaced with a call to some instrumentation facility 3. Runtime metadata: Allow a compiler to insert data for use by the runtime during execution. GHC is one example of a compiler that needs this functionality for its tables-next-to-code functionality. Previously `prefix` served cases (1) and (2) quite well by allowing the user to introduce arbitrary data at the entrypoint but before the function body. Case (3), however, was poorly handled by this approach as it required that prefix data was valid executable code. Here we redefine the notion of prefix data to instead be data which occurs immediately before the function entrypoint (i.e. the symbol address). Since prefix data now occurs before the function entrypoint, there is no need for the data to be valid code. The previous notion of prefix data now goes under the name "prologue data" to emphasize its duality with the function epilogue. The intention here is to handle cases (1) and (2) with prologue data and case (3) with prefix data. References ---------- This idea arose out of discussions[1] with Reid Kleckner in response to a proposal to introduce the notion of symbol offsets to enable handling of case (3). [1] http://lists.cs.uiuc.edu/pipermail/llvmdev/2014-May/073235.html Test Plan: testsuite Differential Revision: http://reviews.llvm.org/D6454 llvm-svn: 223189
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/AsmParser/LLLexer.cpp1
-rw-r--r--llvm/lib/AsmParser/LLParser.cpp8
-rw-r--r--llvm/lib/AsmParser/LLToken.h1
-rw-r--r--llvm/lib/Bitcode/Reader/BitcodeReader.cpp22
-rw-r--r--llvm/lib/Bitcode/Reader/BitcodeReader.h1
-rw-r--r--llvm/lib/Bitcode/Writer/BitcodeWriter.cpp9
-rw-r--r--llvm/lib/Bitcode/Writer/ValueEnumerator.cpp16
-rw-r--r--llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp10
-rw-r--r--llvm/lib/IR/AsmWriter.cpp10
-rw-r--r--llvm/lib/IR/Function.cpp42
-rw-r--r--llvm/lib/IR/LLVMContextImpl.h6
-rw-r--r--llvm/lib/IR/TypeFinder.cpp3
-rw-r--r--llvm/lib/Linker/LinkModules.cpp11
-rw-r--r--llvm/lib/Transforms/IPO/GlobalDCE.cpp3
14 files changed, 124 insertions, 19 deletions
diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp
index 07c13b7ecab..614a283241e 100644
--- a/llvm/lib/AsmParser/LLLexer.cpp
+++ b/llvm/lib/AsmParser/LLLexer.cpp
@@ -573,6 +573,7 @@ lltok::Kind LLLexer::LexIdentifier() {
KEYWORD(inteldialect);
KEYWORD(gc);
KEYWORD(prefix);
+ KEYWORD(prologue);
KEYWORD(ccc);
KEYWORD(fastcc);
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 4a4cc95975d..3e56c8dc051 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -3122,7 +3122,7 @@ bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
/// FunctionHeader
/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
-/// OptionalAlign OptGC OptionalPrefix
+/// OptionalAlign OptGC OptionalPrefix OptionalPrologue
bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
// Parse the linkage.
LocTy LinkageLoc = Lex.getLoc();
@@ -3203,6 +3203,7 @@ bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
bool UnnamedAddr;
LocTy UnnamedAddrLoc;
Constant *Prefix = nullptr;
+ Constant *Prologue = nullptr;
Comdat *C;
if (ParseArgumentList(ArgList, isVarArg) ||
@@ -3217,7 +3218,9 @@ bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
(EatIfPresent(lltok::kw_gc) &&
ParseStringConstant(GC)) ||
(EatIfPresent(lltok::kw_prefix) &&
- ParseGlobalTypeAndValue(Prefix)))
+ ParseGlobalTypeAndValue(Prefix)) ||
+ (EatIfPresent(lltok::kw_prologue) &&
+ ParseGlobalTypeAndValue(Prologue)))
return true;
if (FuncAttrs.contains(Attribute::Builtin))
@@ -3318,6 +3321,7 @@ bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
Fn->setComdat(C);
if (!GC.empty()) Fn->setGC(GC.c_str());
Fn->setPrefixData(Prefix);
+ Fn->setPrologueData(Prologue);
ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
// Add all of the arguments we parsed to the function.
diff --git a/llvm/lib/AsmParser/LLToken.h b/llvm/lib/AsmParser/LLToken.h
index d8583bd2d40..a2111ca96e9 100644
--- a/llvm/lib/AsmParser/LLToken.h
+++ b/llvm/lib/AsmParser/LLToken.h
@@ -83,6 +83,7 @@ namespace lltok {
kw_inteldialect,
kw_gc,
kw_prefix,
+ kw_prologue,
kw_c,
kw_cc, kw_ccc, kw_fastcc, kw_coldcc,
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 7aadb43b3dd..5dab73991de 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -1152,10 +1152,12 @@ std::error_code BitcodeReader::ResolveGlobalAndAliasInits() {
std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist;
+ std::vector<std::pair<Function*, unsigned> > FunctionPrologueWorklist;
GlobalInitWorklist.swap(GlobalInits);
AliasInitWorklist.swap(AliasInits);
FunctionPrefixWorklist.swap(FunctionPrefixes);
+ FunctionPrologueWorklist.swap(FunctionPrologues);
while (!GlobalInitWorklist.empty()) {
unsigned ValID = GlobalInitWorklist.back().second;
@@ -1197,6 +1199,19 @@ std::error_code BitcodeReader::ResolveGlobalAndAliasInits() {
FunctionPrefixWorklist.pop_back();
}
+ while (!FunctionPrologueWorklist.empty()) {
+ unsigned ValID = FunctionPrologueWorklist.back().second;
+ if (ValID >= ValueList.size()) {
+ FunctionPrologues.push_back(FunctionPrologueWorklist.back());
+ } else {
+ if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
+ FunctionPrologueWorklist.back().first->setPrologueData(C);
+ else
+ return Error(BitcodeError::ExpectedConstant);
+ }
+ FunctionPrologueWorklist.pop_back();
+ }
+
return std::error_code();
}
@@ -2011,7 +2026,7 @@ std::error_code BitcodeReader::ParseModule(bool Resume) {
}
// FUNCTION: [type, callingconv, isproto, linkage, paramattr,
// alignment, section, visibility, gc, unnamed_addr,
- // dllstorageclass]
+ // prologuedata, dllstorageclass, comdat, prefixdata]
case bitc::MODULE_CODE_FUNCTION: {
if (Record.size() < 8)
return Error(BitcodeError::InvalidRecord);
@@ -2053,7 +2068,7 @@ std::error_code BitcodeReader::ParseModule(bool Resume) {
UnnamedAddr = Record[9];
Func->setUnnamedAddr(UnnamedAddr);
if (Record.size() > 10 && Record[10] != 0)
- FunctionPrefixes.push_back(std::make_pair(Func, Record[10]-1));
+ FunctionPrologues.push_back(std::make_pair(Func, Record[10]-1));
if (Record.size() > 11)
Func->setDLLStorageClass(GetDecodedDLLStorageClass(Record[11]));
@@ -2066,6 +2081,9 @@ std::error_code BitcodeReader::ParseModule(bool Resume) {
Func->setComdat(ComdatList[ComdatID - 1]);
}
+ if (Record.size() > 13 && Record[13] != 0)
+ FunctionPrefixes.push_back(std::make_pair(Func, Record[13]-1));
+
ValueList.push_back(Func);
// If this is a function with a body, remember the prototype we are
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.h b/llvm/lib/Bitcode/Reader/BitcodeReader.h
index 047fef8fbae..070300ac3e0 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.h
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.h
@@ -143,6 +143,7 @@ class BitcodeReader : public GVMaterializer {
std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
std::vector<std::pair<Function*, unsigned> > FunctionPrefixes;
+ std::vector<std::pair<Function*, unsigned> > FunctionPrologues;
SmallVector<Instruction*, 64> InstsWithTBAATag;
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index 6cfc357b3e2..2e6701156ca 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -670,7 +670,8 @@ static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
// Emit the function proto information.
for (const Function &F : *M) {
// FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
- // section, visibility, gc, unnamed_addr, prefix]
+ // section, visibility, gc, unnamed_addr, prologuedata,
+ // dllstorageclass, comdat, prefixdata]
Vals.push_back(VE.getTypeID(F.getType()));
Vals.push_back(F.getCallingConv());
Vals.push_back(F.isDeclaration());
@@ -681,10 +682,12 @@ static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
Vals.push_back(getEncodedVisibility(F));
Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
Vals.push_back(F.hasUnnamedAddr());
- Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
- : 0);
+ Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
+ : 0);
Vals.push_back(getEncodedDLLStorageClass(F));
Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
+ Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
+ : 0);
unsigned AbbrevToUse = 0;
Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
diff --git a/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp b/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
index 6971d3aacd3..22b7f52387a 100644
--- a/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
+++ b/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
@@ -85,10 +85,14 @@ static OrderMap orderModule(const Module &M) {
for (const GlobalAlias &A : M.aliases())
if (!isa<GlobalValue>(A.getAliasee()))
orderValue(A.getAliasee(), OM);
- for (const Function &F : M)
+ for (const Function &F : M) {
if (F.hasPrefixData())
if (!isa<GlobalValue>(F.getPrefixData()))
orderValue(F.getPrefixData(), OM);
+ if (F.hasPrologueData())
+ if (!isa<GlobalValue>(F.getPrologueData()))
+ orderValue(F.getPrologueData(), OM);
+ }
OM.LastGlobalConstantID = OM.size();
// Initializers of GlobalValues are processed in
@@ -264,9 +268,12 @@ static UseListOrderStack predictUseListOrder(const Module &M) {
predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);
for (const GlobalAlias &A : M.aliases())
predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
- for (const Function &F : M)
+ for (const Function &F : M) {
if (F.hasPrefixData())
predictValueUseListOrder(F.getPrefixData(), nullptr, OM, Stack);
+ if (F.hasPrologueData())
+ predictValueUseListOrder(F.getPrologueData(), nullptr, OM, Stack);
+ }
return Stack;
}
@@ -314,6 +321,11 @@ ValueEnumerator::ValueEnumerator(const Module &M) {
if (I->hasPrefixData())
EnumerateValue(I->getPrefixData());
+ // Enumerate the prologue data constants.
+ for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
+ if (I->hasPrologueData())
+ EnumerateValue(I->getPrologueData());
+
// Insert constants and metadata that are named at module level into the slot
// pool so that the module symbol table can refer to them...
EnumerateValueSymbolTable(M.getValueSymbolTable());
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 8a327134f27..727bdbda31d 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -508,6 +508,10 @@ void AsmPrinter::EmitFunctionHeader() {
OutStreamer.GetCommentOS() << '\n';
}
+ // Emit the prefix data.
+ if (F->hasPrefixData())
+ EmitGlobalConstant(F->getPrefixData());
+
// Emit the CurrentFnSym. This is a virtual function to allow targets to
// do their wild and crazy things as required.
EmitFunctionEntryLabel();
@@ -528,9 +532,9 @@ void AsmPrinter::EmitFunctionHeader() {
HI.Handler->beginFunction(MF);
}
- // Emit the prefix data.
- if (F->hasPrefixData())
- EmitGlobalConstant(F->getPrefixData());
+ // Emit the prologue data.
+ if (F->hasPrologueData())
+ EmitGlobalConstant(F->getPrologueData());
}
/// EmitFunctionEntryLabel - Emit the label that is the entrypoint for the
diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index e461187a0c6..4af03465f41 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -101,6 +101,11 @@ static OrderMap orderModule(const Module *M) {
if (F.hasPrefixData())
if (!isa<GlobalValue>(F.getPrefixData()))
orderValue(F.getPrefixData(), OM);
+
+ if (F.hasPrologueData())
+ if (!isa<GlobalValue>(F.getPrologueData()))
+ orderValue(F.getPrologueData(), OM);
+
orderValue(&F, OM);
if (F.isDeclaration())
@@ -1902,6 +1907,11 @@ void AssemblyWriter::printFunction(const Function *F) {
Out << " prefix ";
writeOperand(F->getPrefixData(), true);
}
+ if (F->hasPrologueData()) {
+ Out << " prologue ";
+ writeOperand(F->getPrologueData(), true);
+ }
+
if (F->isDeclaration()) {
Out << '\n';
} else {
diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp
index b53f6f314ee..c7d3d149e0c 100644
--- a/llvm/lib/IR/Function.cpp
+++ b/llvm/lib/IR/Function.cpp
@@ -298,7 +298,7 @@ void Function::BuildLazyArguments() const {
// Clear the lazy arguments bit.
unsigned SDC = getSubclassDataFromValue();
- const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
+ const_cast<Function*>(this)->setValueSubclassData(SDC &= ~(1<<0));
}
size_t Function::arg_size() const {
@@ -335,8 +335,9 @@ void Function::dropAllReferences() {
while (!BasicBlocks.empty())
BasicBlocks.begin()->eraseFromParent();
- // Prefix data is stored in a side table.
+ // Prefix and prologue data are stored in a side table.
setPrefixData(nullptr);
+ setPrologueData(nullptr);
}
void Function::addAttribute(unsigned i, Attribute::AttrKind attr) {
@@ -416,6 +417,10 @@ void Function::copyAttributesFrom(const GlobalValue *Src) {
setPrefixData(SrcF->getPrefixData());
else
setPrefixData(nullptr);
+ if (SrcF->hasPrologueData())
+ setPrologueData(SrcF->getPrologueData());
+ else
+ setPrologueData(nullptr);
}
/// getIntrinsicID - This method returns the ID number of the specified
@@ -880,11 +885,40 @@ void Function::setPrefixData(Constant *PrefixData) {
PDHolder->setOperand(0, PrefixData);
else
PDHolder = ReturnInst::Create(getContext(), PrefixData);
- SCData |= 2;
+ SCData |= (1<<1);
} else {
delete PDHolder;
PDMap.erase(this);
- SCData &= ~2;
+ SCData &= ~(1<<1);
}
setValueSubclassData(SCData);
}
+
+Constant *Function::getPrologueData() const {
+ assert(hasPrologueData());
+ const LLVMContextImpl::PrologueDataMapTy &SOMap =
+ getContext().pImpl->PrologueDataMap;
+ assert(SOMap.find(this) != SOMap.end());
+ return cast<Constant>(SOMap.find(this)->second->getReturnValue());
+}
+
+void Function::setPrologueData(Constant *PrologueData) {
+ if (!PrologueData && !hasPrologueData())
+ return;
+
+ unsigned PDData = getSubclassDataFromValue();
+ LLVMContextImpl::PrologueDataMapTy &PDMap = getContext().pImpl->PrologueDataMap;
+ ReturnInst *&PDHolder = PDMap[this];
+ if (PrologueData) {
+ if (PDHolder)
+ PDHolder->setOperand(0, PrologueData);
+ else
+ PDHolder = ReturnInst::Create(getContext(), PrologueData);
+ PDData |= (1<<2);
+ } else {
+ delete PDHolder;
+ PDMap.erase(this);
+ PDData &= ~(1<<2);
+ }
+ setValueSubclassData(PDData);
+}
diff --git a/llvm/lib/IR/LLVMContextImpl.h b/llvm/lib/IR/LLVMContextImpl.h
index 7a298cfecf3..5fd8683ccaf 100644
--- a/llvm/lib/IR/LLVMContextImpl.h
+++ b/llvm/lib/IR/LLVMContextImpl.h
@@ -401,6 +401,12 @@ public:
typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
PrefixDataMapTy PrefixDataMap;
+ /// \brief Mapping from a function to its prologue data, which is stored as
+ /// the operand of an unparented ReturnInst so that the prologue data has a
+ /// Use.
+ typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy;
+ PrologueDataMapTy PrologueDataMap;
+
int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
diff --git a/llvm/lib/IR/TypeFinder.cpp b/llvm/lib/IR/TypeFinder.cpp
index 6796075bfc2..7e92818db5e 100644
--- a/llvm/lib/IR/TypeFinder.cpp
+++ b/llvm/lib/IR/TypeFinder.cpp
@@ -47,6 +47,9 @@ void TypeFinder::run(const Module &M, bool onlyNamed) {
if (FI->hasPrefixData())
incorporateValue(FI->getPrefixData());
+ if (FI->hasPrologueData())
+ incorporateValue(FI->getPrologueData());
+
// First incorporate the arguments.
for (Function::const_arg_iterator AI = FI->arg_begin(),
AE = FI->arg_end(); AI != AE; ++AI)
diff --git a/llvm/lib/Linker/LinkModules.cpp b/llvm/lib/Linker/LinkModules.cpp
index 6c025d685f3..ebb790201fd 100644
--- a/llvm/lib/Linker/LinkModules.cpp
+++ b/llvm/lib/Linker/LinkModules.cpp
@@ -1505,11 +1505,16 @@ bool ModuleLinker::run() {
if (DoNotLinkFromSource.count(SF)) continue;
Function *DF = cast<Function>(ValueMap[SF]);
- if (SF->hasPrefixData()) {
- // Link in the prefix data.
+
+ // Link in the prefix data.
+ if (SF->hasPrefixData())
DF->setPrefixData(MapValue(
SF->getPrefixData(), ValueMap, RF_None, &TypeMap, &ValMaterializer));
- }
+
+ // Link in the prologue data.
+ if (SF->hasPrologueData())
+ DF->setPrologueData(MapValue(
+ SF->getPrologueData(), ValueMap, RF_None, &TypeMap, &ValMaterializer));
// Materialize if needed.
if (std::error_code EC = SF->materialize())
diff --git a/llvm/lib/Transforms/IPO/GlobalDCE.cpp b/llvm/lib/Transforms/IPO/GlobalDCE.cpp
index 705e9294934..0c844fe7065 100644
--- a/llvm/lib/Transforms/IPO/GlobalDCE.cpp
+++ b/llvm/lib/Transforms/IPO/GlobalDCE.cpp
@@ -219,6 +219,9 @@ void GlobalDCE::GlobalIsNeeded(GlobalValue *G) {
if (F->hasPrefixData())
MarkUsedGlobalsAsNeeded(F->getPrefixData());
+ if (F->hasPrologueData())
+ MarkUsedGlobalsAsNeeded(F->getPrologueData());
+
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
for (User::op_iterator U = I->op_begin(), E = I->op_end(); U != E; ++U)
OpenPOWER on IntegriCloud