mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-08-25 00:29:20 +00:00
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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223189 91177308-0d34-0410-b5e6-96231b3b80d8
163 lines
5.2 KiB
C++
163 lines
5.2 KiB
C++
//===-- TypeFinder.cpp - Implement the TypeFinder class -------------------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file implements the TypeFinder class for the IR library.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/IR/TypeFinder.h"
|
|
#include "llvm/ADT/SmallVector.h"
|
|
#include "llvm/IR/BasicBlock.h"
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
#include "llvm/IR/Function.h"
|
|
#include "llvm/IR/Metadata.h"
|
|
#include "llvm/IR/Module.h"
|
|
using namespace llvm;
|
|
|
|
void TypeFinder::run(const Module &M, bool onlyNamed) {
|
|
OnlyNamed = onlyNamed;
|
|
|
|
// Get types from global variables.
|
|
for (Module::const_global_iterator I = M.global_begin(),
|
|
E = M.global_end(); I != E; ++I) {
|
|
incorporateType(I->getType());
|
|
if (I->hasInitializer())
|
|
incorporateValue(I->getInitializer());
|
|
}
|
|
|
|
// Get types from aliases.
|
|
for (Module::const_alias_iterator I = M.alias_begin(),
|
|
E = M.alias_end(); I != E; ++I) {
|
|
incorporateType(I->getType());
|
|
if (const Value *Aliasee = I->getAliasee())
|
|
incorporateValue(Aliasee);
|
|
}
|
|
|
|
// Get types from functions.
|
|
SmallVector<std::pair<unsigned, MDNode *>, 4> MDForInst;
|
|
for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
|
|
incorporateType(FI->getType());
|
|
|
|
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)
|
|
incorporateValue(AI);
|
|
|
|
for (Function::const_iterator BB = FI->begin(), E = FI->end();
|
|
BB != E;++BB)
|
|
for (BasicBlock::const_iterator II = BB->begin(),
|
|
E = BB->end(); II != E; ++II) {
|
|
const Instruction &I = *II;
|
|
|
|
// Incorporate the type of the instruction.
|
|
incorporateType(I.getType());
|
|
|
|
// Incorporate non-instruction operand types. (We are incorporating all
|
|
// instructions with this loop.)
|
|
for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
|
|
OI != OE; ++OI)
|
|
if (!isa<Instruction>(OI))
|
|
incorporateValue(*OI);
|
|
|
|
// Incorporate types hiding in metadata.
|
|
I.getAllMetadataOtherThanDebugLoc(MDForInst);
|
|
for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
|
|
incorporateMDNode(MDForInst[i].second);
|
|
|
|
MDForInst.clear();
|
|
}
|
|
}
|
|
|
|
for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
|
|
E = M.named_metadata_end(); I != E; ++I) {
|
|
const NamedMDNode *NMD = I;
|
|
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
|
|
incorporateMDNode(NMD->getOperand(i));
|
|
}
|
|
}
|
|
|
|
void TypeFinder::clear() {
|
|
VisitedConstants.clear();
|
|
VisitedTypes.clear();
|
|
StructTypes.clear();
|
|
}
|
|
|
|
/// incorporateType - This method adds the type to the list of used structures
|
|
/// if it's not in there already.
|
|
void TypeFinder::incorporateType(Type *Ty) {
|
|
// Check to see if we've already visited this type.
|
|
if (!VisitedTypes.insert(Ty).second)
|
|
return;
|
|
|
|
SmallVector<Type *, 4> TypeWorklist;
|
|
TypeWorklist.push_back(Ty);
|
|
do {
|
|
Ty = TypeWorklist.pop_back_val();
|
|
|
|
// If this is a structure or opaque type, add a name for the type.
|
|
if (StructType *STy = dyn_cast<StructType>(Ty))
|
|
if (!OnlyNamed || STy->hasName())
|
|
StructTypes.push_back(STy);
|
|
|
|
// Add all unvisited subtypes to worklist for processing
|
|
for (Type::subtype_reverse_iterator I = Ty->subtype_rbegin(),
|
|
E = Ty->subtype_rend();
|
|
I != E; ++I)
|
|
if (VisitedTypes.insert(*I).second)
|
|
TypeWorklist.push_back(*I);
|
|
} while (!TypeWorklist.empty());
|
|
}
|
|
|
|
/// incorporateValue - This method is used to walk operand lists finding types
|
|
/// hiding in constant expressions and other operands that won't be walked in
|
|
/// other ways. GlobalValues, basic blocks, instructions, and inst operands are
|
|
/// all explicitly enumerated.
|
|
void TypeFinder::incorporateValue(const Value *V) {
|
|
if (const MDNode *M = dyn_cast<MDNode>(V))
|
|
return incorporateMDNode(M);
|
|
|
|
if (!isa<Constant>(V) || isa<GlobalValue>(V)) return;
|
|
|
|
// Already visited?
|
|
if (!VisitedConstants.insert(V).second)
|
|
return;
|
|
|
|
// Check this type.
|
|
incorporateType(V->getType());
|
|
|
|
// If this is an instruction, we incorporate it separately.
|
|
if (isa<Instruction>(V))
|
|
return;
|
|
|
|
// Look in operands for types.
|
|
const User *U = cast<User>(V);
|
|
for (Constant::const_op_iterator I = U->op_begin(),
|
|
E = U->op_end(); I != E;++I)
|
|
incorporateValue(*I);
|
|
}
|
|
|
|
/// incorporateMDNode - This method is used to walk the operands of an MDNode to
|
|
/// find types hiding within.
|
|
void TypeFinder::incorporateMDNode(const MDNode *V) {
|
|
// Already visited?
|
|
if (!VisitedConstants.insert(V).second)
|
|
return;
|
|
|
|
// Look in operands for types.
|
|
for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i)
|
|
if (Value *Op = V->getOperand(i))
|
|
incorporateValue(Op);
|
|
}
|