IR: Split Metadata from Value

Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532.  Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.

I have a follow-up patch prepared for `clang`.  If this breaks other
sub-projects, I apologize in advance :(.  Help me compile it on Darwin
I'll try to fix it.  FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.

This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.

Here's a quick guide for updating your code:

  - `Metadata` is the root of a class hierarchy with three main classes:
    `MDNode`, `MDString`, and `ValueAsMetadata`.  It is distinct from
    the `Value` class hierarchy.  It is typeless -- i.e., instances do
    *not* have a `Type`.

  - `MDNode`'s operands are all `Metadata *` (instead of `Value *`).

  - `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
    replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.

    If you're referring solely to resolved `MDNode`s -- post graph
    construction -- just use `MDNode*`.

  - `MDNode` (and the rest of `Metadata`) have only limited support for
    `replaceAllUsesWith()`.

    As long as an `MDNode` is pointing at a forward declaration -- the
    result of `MDNode::getTemporary()` -- it maintains a side map of its
    uses and can RAUW itself.  Once the forward declarations are fully
    resolved RAUW support is dropped on the ground.  This means that
    uniquing collisions on changing operands cause nodes to become
    "distinct".  (This already happened fairly commonly, whenever an
    operand went to null.)

    If you're constructing complex (non self-reference) `MDNode` cycles,
    you need to call `MDNode::resolveCycles()` on each node (or on a
    top-level node that somehow references all of the nodes).  Also,
    don't do that.  Metadata cycles (and the RAUW machinery needed to
    construct them) are expensive.

  - An `MDNode` can only refer to a `Constant` through a bridge called
    `ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).

    As a side effect, accessing an operand of an `MDNode` that is known
    to be, e.g., `ConstantInt`, takes three steps: first, cast from
    `Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
    third, cast down to `ConstantInt`.

    The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
    metadata schema owners transition away from using `Constant`s when
    the type isn't important (and they don't care about referring to
    `GlobalValue`s).

    In the meantime, I've added transitional API to the `mdconst`
    namespace that matches semantics with the old code, in order to
    avoid adding the error-prone three-step equivalent to every call
    site.  If your old code was:

        MDNode *N = foo();
        bar(isa             <ConstantInt>(N->getOperand(0)));
        baz(cast            <ConstantInt>(N->getOperand(1)));
        bak(cast_or_null    <ConstantInt>(N->getOperand(2)));
        bat(dyn_cast        <ConstantInt>(N->getOperand(3)));
        bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));

    you can trivially match its semantics with:

        MDNode *N = foo();
        bar(mdconst::hasa               <ConstantInt>(N->getOperand(0)));
        baz(mdconst::extract            <ConstantInt>(N->getOperand(1)));
        bak(mdconst::extract_or_null    <ConstantInt>(N->getOperand(2)));
        bat(mdconst::dyn_extract        <ConstantInt>(N->getOperand(3)));
        bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));

    and when you transition your metadata schema to `MDInt`:

        MDNode *N = foo();
        bar(isa             <MDInt>(N->getOperand(0)));
        baz(cast            <MDInt>(N->getOperand(1)));
        bak(cast_or_null    <MDInt>(N->getOperand(2)));
        bat(dyn_cast        <MDInt>(N->getOperand(3)));
        bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));

  - A `CallInst` -- specifically, intrinsic instructions -- can refer to
    metadata through a bridge called `MetadataAsValue`.  This is a
    subclass of `Value` where `getType()->isMetadataTy()`.

    `MetadataAsValue` is the *only* class that can legally refer to a
    `LocalAsMetadata`, which is a bridged form of non-`Constant` values
    like `Argument` and `Instruction`.  It can also refer to any other
    `Metadata` subclass.

(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)

llvm-svn: 223802
This commit is contained in:
Duncan P. N. Exon Smith 2014-12-09 18:38:53 +00:00
parent b39e22bdc5
commit 5bf8fef580
88 changed files with 3370 additions and 1970 deletions

View File

@ -18,12 +18,33 @@
using namespace llvm; using namespace llvm;
static Metadata *unwrapMetadata(LLVMValueRef VRef) {
Value *V = unwrap(VRef);
if (!V)
return nullptr;
if (auto *MD = dyn_cast<MetadataAsValue>(V))
return MD->getMetadata();
return ValueAsMetadata::get(V);
}
static SmallVector<Metadata *, 8> unwrapMetadataArray(LLVMValueRef *Data,
size_t Length) {
SmallVector<Metadata *, 8> Elements;
for (size_t I = 0; I != Length; ++I)
Elements.push_back(unwrapMetadata(Data[I]));
return Elements;
}
namespace { namespace {
template <typename T> T unwrapDI(LLVMValueRef v) { template <typename T> T unwrapDI(LLVMValueRef v) {
return v ? T(unwrap<MDNode>(v)) : T(); return T(cast_or_null<MDNode>(unwrapMetadata(v)));
} }
} }
static LLVMValueRef wrapDI(DIDescriptor N) {
return wrap(MetadataAsValue::get(N->getContext(), N));
}
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef) DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef)
LLVMDIBuilderRef LLVMNewDIBuilder(LLVMModuleRef mref) { LLVMDIBuilderRef LLVMNewDIBuilder(LLVMModuleRef mref) {
@ -47,14 +68,14 @@ LLVMValueRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef Dref,
DIBuilder *D = unwrap(Dref); DIBuilder *D = unwrap(Dref);
DICompileUnit CU = D->createCompileUnit(Lang, File, Dir, Producer, Optimized, DICompileUnit CU = D->createCompileUnit(Lang, File, Dir, Producer, Optimized,
Flags, RuntimeVersion); Flags, RuntimeVersion);
return wrap(CU); return wrapDI(CU);
} }
LLVMValueRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref, const char *File, LLVMValueRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref, const char *File,
const char *Dir) { const char *Dir) {
DIBuilder *D = unwrap(Dref); DIBuilder *D = unwrap(Dref);
DIFile F = D->createFile(File, Dir); DIFile F = D->createFile(File, Dir);
return wrap(F); return wrapDI(F);
} }
LLVMValueRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref, LLVMValueRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref,
@ -64,7 +85,7 @@ LLVMValueRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref,
DIBuilder *D = unwrap(Dref); DIBuilder *D = unwrap(Dref);
DILexicalBlock LB = D->createLexicalBlock( DILexicalBlock LB = D->createLexicalBlock(
unwrapDI<DIDescriptor>(Scope), unwrapDI<DIFile>(File), Line, Column); unwrapDI<DIDescriptor>(Scope), unwrapDI<DIFile>(File), Line, Column);
return wrap(LB); return wrapDI(LB);
} }
LLVMValueRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref, LLVMValueRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref,
@ -74,7 +95,7 @@ LLVMValueRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref,
DIBuilder *D = unwrap(Dref); DIBuilder *D = unwrap(Dref);
DILexicalBlockFile LBF = D->createLexicalBlockFile( DILexicalBlockFile LBF = D->createLexicalBlockFile(
unwrapDI<DIDescriptor>(Scope), unwrapDI<DIFile>(File), Discriminator); unwrapDI<DIDescriptor>(Scope), unwrapDI<DIFile>(File), Discriminator);
return wrap(LBF); return wrapDI(LBF);
} }
LLVMValueRef LLVMDIBuilderCreateFunction( LLVMValueRef LLVMDIBuilderCreateFunction(
@ -87,7 +108,7 @@ LLVMValueRef LLVMDIBuilderCreateFunction(
unwrapDI<DIDescriptor>(Scope), Name, LinkageName, unwrapDI<DIFile>(File), unwrapDI<DIDescriptor>(Scope), Name, LinkageName, unwrapDI<DIFile>(File),
Line, unwrapDI<DICompositeType>(CompositeType), IsLocalToUnit, Line, unwrapDI<DICompositeType>(CompositeType), IsLocalToUnit,
IsDefinition, ScopeLine, Flags, IsOptimized, unwrap<Function>(Func)); IsDefinition, ScopeLine, Flags, IsOptimized, unwrap<Function>(Func));
return wrap(SP); return wrapDI(SP);
} }
LLVMValueRef LLVMDIBuilderCreateLocalVariable( LLVMValueRef LLVMDIBuilderCreateLocalVariable(
@ -98,7 +119,7 @@ LLVMValueRef LLVMDIBuilderCreateLocalVariable(
DIVariable V = D->createLocalVariable( DIVariable V = D->createLocalVariable(
Tag, unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line, Tag, unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
unwrapDI<DIType>(Ty), AlwaysPreserve, Flags, ArgNo); unwrapDI<DIType>(Ty), AlwaysPreserve, Flags, ArgNo);
return wrap(V); return wrapDI(V);
} }
LLVMValueRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref, LLVMValueRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref,
@ -107,7 +128,7 @@ LLVMValueRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref,
unsigned Encoding) { unsigned Encoding) {
DIBuilder *D = unwrap(Dref); DIBuilder *D = unwrap(Dref);
DIBasicType T = D->createBasicType(Name, SizeInBits, AlignInBits, Encoding); DIBasicType T = D->createBasicType(Name, SizeInBits, AlignInBits, Encoding);
return wrap(T); return wrapDI(T);
} }
LLVMValueRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref, LLVMValueRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
@ -118,7 +139,7 @@ LLVMValueRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
DIBuilder *D = unwrap(Dref); DIBuilder *D = unwrap(Dref);
DIDerivedType T = D->createPointerType(unwrapDI<DIType>(PointeeType), DIDerivedType T = D->createPointerType(unwrapDI<DIType>(PointeeType),
SizeInBits, AlignInBits, Name); SizeInBits, AlignInBits, Name);
return wrap(T); return wrapDI(T);
} }
LLVMValueRef LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref, LLVMValueRef LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref,
@ -127,7 +148,7 @@ LLVMValueRef LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref,
DIBuilder *D = unwrap(Dref); DIBuilder *D = unwrap(Dref);
DICompositeType CT = D->createSubroutineType( DICompositeType CT = D->createSubroutineType(
unwrapDI<DIFile>(File), unwrapDI<DITypeArray>(ParameterTypes)); unwrapDI<DIFile>(File), unwrapDI<DITypeArray>(ParameterTypes));
return wrap(CT); return wrapDI(CT);
} }
LLVMValueRef LLVMDIBuilderCreateStructType( LLVMValueRef LLVMDIBuilderCreateStructType(
@ -139,7 +160,7 @@ LLVMValueRef LLVMDIBuilderCreateStructType(
unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line, unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
SizeInBits, AlignInBits, Flags, unwrapDI<DIType>(DerivedFrom), SizeInBits, AlignInBits, Flags, unwrapDI<DIType>(DerivedFrom),
unwrapDI<DIArray>(ElementTypes)); unwrapDI<DIArray>(ElementTypes));
return wrap(CT); return wrapDI(CT);
} }
LLVMValueRef LLVMDIBuilderCreateMemberType( LLVMValueRef LLVMDIBuilderCreateMemberType(
@ -150,7 +171,7 @@ LLVMValueRef LLVMDIBuilderCreateMemberType(
DIDerivedType DT = D->createMemberType( DIDerivedType DT = D->createMemberType(
unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line, unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
SizeInBits, AlignInBits, OffsetInBits, Flags, unwrapDI<DIType>(Ty)); SizeInBits, AlignInBits, OffsetInBits, Flags, unwrapDI<DIType>(Ty));
return wrap(DT); return wrapDI(DT);
} }
LLVMValueRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref, LLVMValueRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,
@ -162,7 +183,7 @@ LLVMValueRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,
DICompositeType CT = DICompositeType CT =
D->createArrayType(SizeInBits, AlignInBits, unwrapDI<DIType>(ElementType), D->createArrayType(SizeInBits, AlignInBits, unwrapDI<DIType>(ElementType),
unwrapDI<DIArray>(Subscripts)); unwrapDI<DIArray>(Subscripts));
return wrap(CT); return wrapDI(CT);
} }
LLVMValueRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref, LLVMValueRef Ty, LLVMValueRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref, LLVMValueRef Ty,
@ -172,40 +193,36 @@ LLVMValueRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref, LLVMValueRef Ty,
DIDerivedType DT = DIDerivedType DT =
D->createTypedef(unwrapDI<DIType>(Ty), Name, unwrapDI<DIFile>(File), Line, D->createTypedef(unwrapDI<DIType>(Ty), Name, unwrapDI<DIFile>(File), Line,
unwrapDI<DIDescriptor>(Context)); unwrapDI<DIDescriptor>(Context));
return wrap(DT); return wrapDI(DT);
} }
LLVMValueRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref, int64_t Lo, LLVMValueRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref, int64_t Lo,
int64_t Count) { int64_t Count) {
DIBuilder *D = unwrap(Dref); DIBuilder *D = unwrap(Dref);
DISubrange S = D->getOrCreateSubrange(Lo, Count); DISubrange S = D->getOrCreateSubrange(Lo, Count);
return wrap(S); return wrapDI(S);
} }
LLVMValueRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref, LLVMValueRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
LLVMValueRef *Data, size_t Length) { LLVMValueRef *Data, size_t Length) {
DIBuilder *D = unwrap(Dref); DIBuilder *D = unwrap(Dref);
Value **DataValue = unwrap(Data); DIArray A = D->getOrCreateArray(unwrapMetadataArray(Data, Length));
ArrayRef<Value *> Elements(DataValue, Length); return wrapDI(A);
DIArray A = D->getOrCreateArray(Elements);
return wrap(A);
} }
LLVMValueRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref, LLVMValueRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
LLVMValueRef *Data, LLVMValueRef *Data,
size_t Length) { size_t Length) {
DIBuilder *D = unwrap(Dref); DIBuilder *D = unwrap(Dref);
Value **DataValue = unwrap(Data); DITypeArray A = D->getOrCreateTypeArray(unwrapMetadataArray(Data, Length));
ArrayRef<Value *> Elements(DataValue, Length); return wrapDI(A);
DITypeArray A = D->getOrCreateTypeArray(Elements);
return wrap(A);
} }
LLVMValueRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref, int64_t *Addr, LLVMValueRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref, int64_t *Addr,
size_t Length) { size_t Length) {
DIBuilder *D = unwrap(Dref); DIBuilder *D = unwrap(Dref);
DIExpression Expr = D->createExpression(ArrayRef<int64_t>(Addr, Length)); DIExpression Expr = D->createExpression(ArrayRef<int64_t>(Addr, Length));
return wrap(Expr); return wrapDI(Expr);
} }
LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref, LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,

View File

@ -1157,8 +1157,6 @@ LLVMTypeRef LLVMX86MMXType(void);
macro(Argument) \ macro(Argument) \
macro(BasicBlock) \ macro(BasicBlock) \
macro(InlineAsm) \ macro(InlineAsm) \
macro(MDNode) \
macro(MDString) \
macro(User) \ macro(User) \
macro(Constant) \ macro(Constant) \
macro(BlockAddress) \ macro(BlockAddress) \
@ -1307,6 +1305,9 @@ LLVMBool LLVMIsUndef(LLVMValueRef Val);
LLVMValueRef LLVMIsA##name(LLVMValueRef Val); LLVMValueRef LLVMIsA##name(LLVMValueRef Val);
LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DECLARE_VALUE_CAST) LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DECLARE_VALUE_CAST)
LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val);
LLVMValueRef LLVMIsAMDString(LLVMValueRef Val);
/** /**
* @} * @}
*/ */

View File

@ -48,6 +48,8 @@ public:
LexicalScope(LexicalScope *P, const MDNode *D, const MDNode *I, bool A) LexicalScope(LexicalScope *P, const MDNode *D, const MDNode *I, bool A)
: Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(A), : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(A),
LastInsn(nullptr), FirstInsn(nullptr), DFSIn(0), DFSOut(0) { LastInsn(nullptr), FirstInsn(nullptr), DFSIn(0), DFSOut(0) {
assert((!D || D->isResolved()) && "Expected resolved node");
assert((!I || I->isResolved()) && "Expected resolved node");
if (Parent) if (Parent)
Parent->addChild(this); Parent->addChild(this);
} }
@ -116,8 +118,8 @@ public:
private: private:
LexicalScope *Parent; // Parent to this scope. LexicalScope *Parent; // Parent to this scope.
AssertingVH<const MDNode> Desc; // Debug info descriptor. const MDNode *Desc; // Debug info descriptor.
AssertingVH<const MDNode> InlinedAtLocation; // Location at which this const MDNode *InlinedAtLocation; // Location at which this
// scope is inlined. // scope is inlined.
bool AbstractScope; // Abstract Scope bool AbstractScope; // Abstract Scope
SmallVector<LexicalScope *, 4> Children; // Scopes defined in scope. SmallVector<LexicalScope *, 4> Children; // Scopes defined in scope.

View File

@ -1139,7 +1139,10 @@ public:
/// setDebugLoc - Replace current source information with new such. /// setDebugLoc - Replace current source information with new such.
/// Avoid using this, the constructor argument is preferable. /// Avoid using this, the constructor argument is preferable.
/// ///
void setDebugLoc(const DebugLoc dl) { debugLoc = dl; } void setDebugLoc(const DebugLoc dl) {
debugLoc = dl;
assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
}
/// RemoveOperand - Erase an operand from an instruction, leaving it with one /// RemoveOperand - Erase an operand from an instruction, leaving it with one
/// fewer operand than it started with. /// fewer operand than it started with.

View File

@ -165,10 +165,13 @@ public:
static char ID; // Pass identification, replacement for typeid static char ID; // Pass identification, replacement for typeid
struct VariableDbgInfo { struct VariableDbgInfo {
TrackingVH<MDNode> Var; TrackingMDNodeRef Var;
TrackingVH<MDNode> Expr; TrackingMDNodeRef Expr;
unsigned Slot; unsigned Slot;
DebugLoc Loc; DebugLoc Loc;
VariableDbgInfo(MDNode *Var, MDNode *Expr, unsigned Slot, DebugLoc Loc)
: Var(Var), Expr(Expr), Slot(Slot), Loc(Loc) {}
}; };
typedef SmallVector<VariableDbgInfo, 4> VariableDbgInfoMapTy; typedef SmallVector<VariableDbgInfo, 4> VariableDbgInfoMapTy;
VariableDbgInfoMapTy VariableDbgInfos; VariableDbgInfoMapTy VariableDbgInfos;
@ -393,8 +396,7 @@ public:
/// information of a variable. /// information of a variable.
void setVariableDbgInfo(MDNode *Var, MDNode *Expr, unsigned Slot, void setVariableDbgInfo(MDNode *Var, MDNode *Expr, unsigned Slot,
DebugLoc Loc) { DebugLoc Loc) {
VariableDbgInfo Info = {Var, Expr, Slot, Loc}; VariableDbgInfos.emplace_back(Var, Expr, Slot, Loc);
VariableDbgInfos.push_back(std::move(Info));
} }
VariableDbgInfoMapTy &getVariableDbgInfo() { return VariableDbgInfos; } VariableDbgInfoMapTy &getVariableDbgInfo() { return VariableDbgInfos; }

View File

@ -762,6 +762,7 @@ protected:
ValueList(VTs.VTs), UseList(nullptr), ValueList(VTs.VTs), UseList(nullptr),
NumOperands(Ops.size()), NumValues(VTs.NumVTs), NumOperands(Ops.size()), NumValues(VTs.NumVTs),
debugLoc(dl), IROrder(Order) { debugLoc(dl), IROrder(Order) {
assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
assert(NumOperands == Ops.size() && assert(NumOperands == Ops.size() &&
"NumOperands wasn't wide enough for its operands!"); "NumOperands wasn't wide enough for its operands!");
assert(NumValues == VTs.NumVTs && assert(NumValues == VTs.NumVTs &&
@ -780,6 +781,7 @@ protected:
SubclassData(0), NodeId(-1), OperandList(nullptr), ValueList(VTs.VTs), SubclassData(0), NodeId(-1), OperandList(nullptr), ValueList(VTs.VTs),
UseList(nullptr), NumOperands(0), NumValues(VTs.NumVTs), debugLoc(dl), UseList(nullptr), NumOperands(0), NumValues(VTs.NumVTs), debugLoc(dl),
IROrder(Order) { IROrder(Order) {
assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
assert(NumValues == VTs.NumVTs && assert(NumValues == VTs.NumVTs &&
"NumValues wasn't wide enough for its operands!"); "NumValues wasn't wide enough for its operands!");
} }

View File

@ -18,6 +18,7 @@
#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include "llvm/IR/DebugInfo.h" #include "llvm/IR/DebugInfo.h"
#include "llvm/IR/TrackingMDRef.h"
#include "llvm/IR/ValueHandle.h" #include "llvm/IR/ValueHandle.h"
#include "llvm/Support/DataTypes.h" #include "llvm/Support/DataTypes.h"
@ -65,22 +66,34 @@ namespace llvm {
Function *DeclareFn; // llvm.dbg.declare Function *DeclareFn; // llvm.dbg.declare
Function *ValueFn; // llvm.dbg.value Function *ValueFn; // llvm.dbg.value
SmallVector<Value *, 4> AllEnumTypes; SmallVector<Metadata *, 4> AllEnumTypes;
/// Use TrackingVH to collect RetainTypes, since they can be updated /// Track the RetainTypes, since they can be updated later on.
/// later on. SmallVector<TrackingMDNodeRef, 4> AllRetainTypes;
SmallVector<TrackingVH<MDNode>, 4> AllRetainTypes; SmallVector<Metadata *, 4> AllSubprograms;
SmallVector<Value *, 4> AllSubprograms; SmallVector<Metadata *, 4> AllGVs;
SmallVector<Value *, 4> AllGVs; SmallVector<TrackingMDNodeRef, 4> AllImportedModules;
SmallVector<TrackingVH<MDNode>, 4> AllImportedModules;
/// \brief Track nodes that may be unresolved.
SmallVector<TrackingMDNodeRef, 4> UnresolvedNodes;
bool AllowUnresolvedNodes;
/// Each subprogram's preserved local variables. /// Each subprogram's preserved local variables.
DenseMap<MDNode *, std::vector<TrackingVH<MDNode>>> PreservedVariables; DenseMap<MDNode *, std::vector<TrackingMDNodeRef>> PreservedVariables;
DIBuilder(const DIBuilder &) LLVM_DELETED_FUNCTION; DIBuilder(const DIBuilder &) LLVM_DELETED_FUNCTION;
void operator=(const DIBuilder &) LLVM_DELETED_FUNCTION; void operator=(const DIBuilder &) LLVM_DELETED_FUNCTION;
/// \brief Create a temporary.
///
/// Create an \a MDNodeFwdDecl and track it in \a UnresolvedNodes.
void trackIfUnresolved(MDNode *N);
public: public:
explicit DIBuilder(Module &M); /// \brief Construct a builder for a module.
///
/// If \c AllowUnresolved, collect unresolved nodes attached to the module
/// in order to resolve cycles during \a finalize().
explicit DIBuilder(Module &M, bool AllowUnresolved = true);
enum DebugEmissionKind { FullDebug=1, LineTablesOnly }; enum DebugEmissionKind { FullDebug=1, LineTablesOnly };
/// finalize - Construct any deferred debug info descriptors. /// finalize - Construct any deferred debug info descriptors.
@ -437,10 +450,10 @@ namespace llvm {
DIBasicType createUnspecifiedParameter(); DIBasicType createUnspecifiedParameter();
/// getOrCreateArray - Get a DIArray, create one if required. /// getOrCreateArray - Get a DIArray, create one if required.
DIArray getOrCreateArray(ArrayRef<Value *> Elements); DIArray getOrCreateArray(ArrayRef<Metadata *> Elements);
/// getOrCreateTypeArray - Get a DITypeArray, create one if required. /// getOrCreateTypeArray - Get a DITypeArray, create one if required.
DITypeArray getOrCreateTypeArray(ArrayRef<Value *> Elements); DITypeArray getOrCreateTypeArray(ArrayRef<Metadata *> Elements);
/// getOrCreateSubrange - Create a descriptor for a value range. This /// getOrCreateSubrange - Create a descriptor for a value range. This
/// implicitly uniques the values returned. /// implicitly uniques the values returned.

View File

@ -168,8 +168,9 @@ public:
bool Verify() const; bool Verify() const;
operator MDNode *() const { return const_cast<MDNode *>(DbgNode); } MDNode *get() const { return const_cast<MDNode *>(DbgNode); }
MDNode *operator->() const { return const_cast<MDNode *>(DbgNode); } operator MDNode *() const { return get(); }
MDNode *operator->() const { return get(); }
// An explicit operator bool so that we can do testing of DI values // An explicit operator bool so that we can do testing of DI values
// easily. // easily.
@ -740,7 +741,7 @@ public:
DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); } DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
DITypeRef getType() const { return getFieldAs<DITypeRef>(2); } DITypeRef getType() const { return getFieldAs<DITypeRef>(2); }
Value *getValue() const; Metadata *getValue() const;
StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); } StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); }
StringRef getDirectory() const { StringRef getDirectory() const {
return getFieldAs<DIFile>(4).getDirectory(); return getFieldAs<DIFile>(4).getDirectory();

View File

@ -16,50 +16,40 @@
#define LLVM_IR_DEBUGLOC_H #define LLVM_IR_DEBUGLOC_H
#include "llvm/Support/DataTypes.h" #include "llvm/Support/DataTypes.h"
#include "llvm/IR/TrackingMDRef.h"
namespace llvm { namespace llvm {
template <typename T> struct DenseMapInfo;
class MDNode;
class LLVMContext; class LLVMContext;
class raw_ostream; class raw_ostream;
class MDNode;
/// DebugLoc - Debug location id. This is carried by Instruction, SDNode, /// DebugLoc - Debug location id. This is carried by Instruction, SDNode,
/// and MachineInstr to compactly encode file/line/scope information for an /// and MachineInstr to compactly encode file/line/scope information for an
/// operation. /// operation.
class DebugLoc { class DebugLoc {
friend struct DenseMapInfo<DebugLoc>; TrackingMDNodeRef Loc;
/// getEmptyKey() - A private constructor that returns an unknown that is
/// not equal to the tombstone key or DebugLoc().
static DebugLoc getEmptyKey() {
DebugLoc DL;
DL.LineCol = 1;
return DL;
}
/// getTombstoneKey() - A private constructor that returns an unknown that
/// is not equal to the empty key or DebugLoc().
static DebugLoc getTombstoneKey() {
DebugLoc DL;
DL.LineCol = 2;
return DL;
}
/// LineCol - This 32-bit value encodes the line and column number for the
/// location, encoded as 24-bits for line and 8 bits for col. A value of 0
/// for either means unknown.
uint32_t LineCol;
/// ScopeIdx - This is an opaque ID# for Scope/InlinedAt information,
/// decoded by LLVMContext. 0 is unknown.
int ScopeIdx;
public: public:
DebugLoc() : LineCol(0), ScopeIdx(0) {} // Defaults to unknown. DebugLoc() {}
DebugLoc(DebugLoc &&X) : Loc(std::move(X.Loc)) {}
DebugLoc(const DebugLoc &X) : Loc(X.Loc) {}
DebugLoc &operator=(DebugLoc &&X) {
Loc = std::move(X.Loc);
return *this;
}
DebugLoc &operator=(const DebugLoc &X) {
Loc = X.Loc;
return *this;
}
/// \brief Check whether this has a trivial destructor.
bool hasTrivialDestructor() const { return Loc.hasTrivialDestructor(); }
/// get - Get a new DebugLoc that corresponds to the specified line/col /// get - Get a new DebugLoc that corresponds to the specified line/col
/// scope/inline location. /// scope/inline location.
static DebugLoc get(unsigned Line, unsigned Col, static DebugLoc get(unsigned Line, unsigned Col, MDNode *Scope,
MDNode *Scope, MDNode *InlinedAt = nullptr); MDNode *InlinedAt = nullptr);
/// getFromDILocation - Translate the DILocation quad into a DebugLoc. /// getFromDILocation - Translate the DILocation quad into a DebugLoc.
static DebugLoc getFromDILocation(MDNode *N); static DebugLoc getFromDILocation(MDNode *N);
@ -68,56 +58,54 @@ namespace llvm {
static DebugLoc getFromDILexicalBlock(MDNode *N); static DebugLoc getFromDILexicalBlock(MDNode *N);
/// isUnknown - Return true if this is an unknown location. /// isUnknown - Return true if this is an unknown location.
bool isUnknown() const { return ScopeIdx == 0; } bool isUnknown() const { return !Loc; }
unsigned getLine() const { unsigned getLine() const;
return (LineCol << 8) >> 8; // Mask out column. unsigned getCol() const;
}
unsigned getCol() const {
return LineCol >> 24;
}
/// getScope - This returns the scope pointer for this DebugLoc, or null if /// getScope - This returns the scope pointer for this DebugLoc, or null if
/// invalid. /// invalid.
MDNode *getScope(const LLVMContext &Ctx) const; MDNode *getScope() const;
MDNode *getScope(const LLVMContext &) const { return getScope(); }
/// getInlinedAt - This returns the InlinedAt pointer for this DebugLoc, or /// getInlinedAt - This returns the InlinedAt pointer for this DebugLoc, or
/// null if invalid or not present. /// null if invalid or not present.
MDNode *getInlinedAt(const LLVMContext &Ctx) const; MDNode *getInlinedAt() const;
MDNode *getInlinedAt(const LLVMContext &) const { return getInlinedAt(); }
/// getScopeAndInlinedAt - Return both the Scope and the InlinedAt values. /// getScopeAndInlinedAt - Return both the Scope and the InlinedAt values.
void getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA) const;
void getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA, void getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA,
const LLVMContext &Ctx) const; const LLVMContext &) const {
return getScopeAndInlinedAt(Scope, IA);
}
/// getScopeNode - Get MDNode for DebugLoc's scope, or null if invalid. /// getScopeNode - Get MDNode for DebugLoc's scope, or null if invalid.
MDNode *getScopeNode(const LLVMContext &Ctx) const; MDNode *getScopeNode() const;
MDNode *getScopeNode(const LLVMContext &) const { return getScopeNode(); }
// getFnDebugLoc - Walk up the scope chain of given debug loc and find line // getFnDebugLoc - Walk up the scope chain of given debug loc and find line
// number info for the function. // number info for the function.
DebugLoc getFnDebugLoc(const LLVMContext &Ctx) const; DebugLoc getFnDebugLoc() const;
DebugLoc getFnDebugLoc(const LLVMContext &) const {
return getFnDebugLoc();
}
/// getAsMDNode - This method converts the compressed DebugLoc node into a /// getAsMDNode - This method converts the compressed DebugLoc node into a
/// DILocation compatible MDNode. /// DILocation compatible MDNode.
MDNode *getAsMDNode(const LLVMContext &Ctx) const; MDNode *getAsMDNode() const;
MDNode *getAsMDNode(LLVMContext &) const { return getAsMDNode(); }
bool operator==(const DebugLoc &DL) const { bool operator==(const DebugLoc &DL) const { return Loc == DL.Loc; }
return LineCol == DL.LineCol && ScopeIdx == DL.ScopeIdx;
}
bool operator!=(const DebugLoc &DL) const { return !(*this == DL); } bool operator!=(const DebugLoc &DL) const { return !(*this == DL); }
void dump(const LLVMContext &Ctx) const; void dump() const;
void dump(const LLVMContext &) const { dump(); }
/// \brief prints source location /path/to/file.exe:line:col @[inlined at] /// \brief prints source location /path/to/file.exe:line:col @[inlined at]
void print(const LLVMContext &Ctx, raw_ostream &OS) const; void print(raw_ostream &OS) const;
void print(const LLVMContext &, raw_ostream &OS) const { print(OS); }
}; };
template <>
struct DenseMapInfo<DebugLoc> {
static DebugLoc getEmptyKey() { return DebugLoc::getEmptyKey(); }
static DebugLoc getTombstoneKey() { return DebugLoc::getTombstoneKey(); }
static unsigned getHashValue(const DebugLoc &Key);
static bool isEqual(DebugLoc LHS, DebugLoc RHS) { return LHS == RHS; }
};
} // end namespace llvm } // end namespace llvm
#endif /* LLVM_SUPPORT_DEBUGLOC_H */ #endif /* LLVM_SUPPORT_DEBUGLOC_H */

View File

@ -28,6 +28,7 @@
#include "llvm/IR/Function.h" #include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h" #include "llvm/IR/Instructions.h"
#include "llvm/IR/Intrinsics.h" #include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Metadata.h"
namespace llvm { namespace llvm {
/// IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic /// IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic
@ -81,8 +82,14 @@ namespace llvm {
class DbgDeclareInst : public DbgInfoIntrinsic { class DbgDeclareInst : public DbgInfoIntrinsic {
public: public:
Value *getAddress() const; Value *getAddress() const;
MDNode *getVariable() const { return cast<MDNode>(getArgOperand(1)); } MDNode *getVariable() const {
MDNode *getExpression() const { return cast<MDNode>(getArgOperand(2)); } return cast<MDNode>(
cast<MetadataAsValue>(getArgOperand(1))->getMetadata());
}
MDNode *getExpression() const {
return cast<MDNode>(
cast<MetadataAsValue>(getArgOperand(2))->getMetadata());
}
// Methods for support type inquiry through isa, cast, and dyn_cast: // Methods for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const IntrinsicInst *I) { static inline bool classof(const IntrinsicInst *I) {
@ -103,8 +110,14 @@ namespace llvm {
return cast<ConstantInt>( return cast<ConstantInt>(
const_cast<Value*>(getArgOperand(1)))->getZExtValue(); const_cast<Value*>(getArgOperand(1)))->getZExtValue();
} }
MDNode *getVariable() const { return cast<MDNode>(getArgOperand(2)); } MDNode *getVariable() const {
MDNode *getExpression() const { return cast<MDNode>(getArgOperand(3)); } return cast<MDNode>(
cast<MetadataAsValue>(getArgOperand(2))->getMetadata());
}
MDNode *getExpression() const {
return cast<MDNode>(
cast<MetadataAsValue>(getArgOperand(3))->getMetadata());
}
// Methods for support type inquiry through isa, cast, and dyn_cast: // Methods for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const IntrinsicInst *I) { static inline bool classof(const IntrinsicInst *I) {

View File

@ -24,6 +24,8 @@ namespace llvm {
class APInt; class APInt;
template <typename T> class ArrayRef; template <typename T> class ArrayRef;
class LLVMContext; class LLVMContext;
class Constant;
class ConstantAsMetadata;
class MDNode; class MDNode;
class MDString; class MDString;
@ -36,6 +38,9 @@ public:
/// \brief Return the given string as metadata. /// \brief Return the given string as metadata.
MDString *createString(StringRef Str); MDString *createString(StringRef Str);
/// \brief Return the given constant as metadata.
ConstantAsMetadata *createConstant(Constant *C);
//===------------------------------------------------------------------===// //===------------------------------------------------------------------===//
// FPMath metadata. // FPMath metadata.
//===------------------------------------------------------------------===// //===------------------------------------------------------------------===//

View File

@ -0,0 +1,44 @@
//===- llvm/Metadata.def - Metadata definitions -----------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Macros for running through all types of metadata.
//
//===----------------------------------------------------------------------===//
#if !(defined HANDLE_METADATA || defined HANDLE_METADATA_LEAF || \
defined HANDLE_METADATA_BRANCH)
#error "Missing macro definition of HANDLE_METADATA*"
#endif
// Handler for all types of metadata.
#ifndef HANDLE_METADATA
#define HANDLE_METADATA(CLASS)
#endif
// Handler for leaf nodes in the class hierarchy.
#ifndef HANDLE_METADATA_LEAF
#define HANDLE_METADATA_LEAF(CLASS) HANDLE_METADATA(CLASS)
#endif
// Handler for non-leaf nodes in the class hierarchy.
#ifndef HANDLE_METADATA_BRANCH
#define HANDLE_METADATA_BRANCH(CLASS) HANDLE_METADATA(CLASS)
#endif
HANDLE_METADATA_LEAF(MDString)
HANDLE_METADATA_BRANCH(ValueAsMetadata)
HANDLE_METADATA_LEAF(ConstantAsMetadata)
HANDLE_METADATA_LEAF(LocalAsMetadata)
HANDLE_METADATA_BRANCH(MDNode)
HANDLE_METADATA_LEAF(MDNodeFwdDecl)
HANDLE_METADATA_LEAF(GenericMDNode)
#undef HANDLE_METADATA
#undef HANDLE_METADATA_LEAF
#undef HANDLE_METADATA_BRANCH

View File

@ -18,11 +18,13 @@
#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/ilist_node.h" #include "llvm/ADT/ilist_node.h"
#include "llvm/ADT/iterator_range.h" #include "llvm/ADT/iterator_range.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/MetadataTracking.h"
#include "llvm/IR/Value.h" #include "llvm/IR/Value.h"
#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorHandling.h"
#include <type_traits>
namespace llvm { namespace llvm {
class LLVMContext; class LLVMContext;
@ -38,20 +40,388 @@ enum LLVMConstants : uint32_t {
/// \brief Root of the metadata hierarchy. /// \brief Root of the metadata hierarchy.
/// ///
/// This is a root class for typeless data in the IR. /// This is a root class for typeless data in the IR.
/// class Metadata {
/// TODO: Detach from the Value hierarchy. friend class ReplaceableMetadataImpl;
class Metadata : public Value {
/// \brief RTTI.
const unsigned char SubclassID;
protected: protected:
Metadata(LLVMContext &Context, unsigned ID); /// \brief Storage flag for non-uniqued, otherwise unowned, metadata.
bool IsDistinctInContext : 1;
// TODO: expose remaining bits to subclasses.
unsigned short SubclassData16;
unsigned SubclassData32;
public: public:
enum MetadataKind {
GenericMDNodeKind,
MDNodeFwdDeclKind,
ConstantAsMetadataKind,
LocalAsMetadataKind,
MDStringKind
};
protected:
Metadata(unsigned ID)
: SubclassID(ID), IsDistinctInContext(false), SubclassData16(0),
SubclassData32(0) {}
~Metadata() {}
/// \brief Store this in a big non-uniqued untyped bucket.
bool isStoredDistinctInContext() const { return IsDistinctInContext; }
/// \brief Default handling of a changed operand, which asserts.
///
/// If subclasses pass themselves in as owners to a tracking node reference,
/// they must provide an implementation of this method.
void handleChangedOperand(void *, Metadata *) {
llvm_unreachable("Unimplemented in Metadata subclass");
}
public:
unsigned getMetadataID() const { return SubclassID; }
/// \brief User-friendly dump.
void dump() const;
void print(raw_ostream &OS) const;
void printAsOperand(raw_ostream &OS, bool PrintType = true,
const Module *M = nullptr) const;
};
#define HANDLE_METADATA(CLASS) class CLASS;
#include "llvm/IR/Metadata.def"
inline raw_ostream &operator<<(raw_ostream &OS, const Metadata &MD) {
MD.print(OS);
return OS;
}
/// \brief Metadata wrapper in the Value hierarchy.
///
/// A member of the \a Value hierarchy to represent a reference to metadata.
/// This allows, e.g., instrinsics to have metadata as operands.
///
/// Notably, this is the only thing in either hierarchy that is allowed to
/// reference \a LocalAsMetadata.
class MetadataAsValue : public Value {
friend class ReplaceableMetadataImpl;
friend class LLVMContextImpl;
Metadata *MD;
MetadataAsValue(Type *Ty, Metadata *MD);
~MetadataAsValue();
public:
static MetadataAsValue *get(LLVMContext &Context, Metadata *MD);
static MetadataAsValue *getIfExists(LLVMContext &Context, Metadata *MD);
Metadata *getMetadata() const { return MD; }
static bool classof(const Value *V) { static bool classof(const Value *V) {
return V->getValueID() == GenericMDNodeVal || return V->getValueID() == MetadataAsValueVal;
V->getValueID() == MDNodeFwdDeclVal || }
V->getValueID() == MDStringVal;
private:
void handleChangedMetadata(Metadata *MD);
void track();
void untrack();
};
/// \brief Shared implementation of use-lists for replaceable metadata.
///
/// Most metadata cannot be RAUW'ed. This is a shared implementation of
/// use-lists and associated API for the two that support it (\a ValueAsMetadata
/// and \a TempMDNode).
class ReplaceableMetadataImpl {
friend class MetadataTracking;
public:
typedef MetadataTracking::OwnerTy OwnerTy;
private:
SmallDenseMap<void *, OwnerTy, 4> UseMap;
public:
~ReplaceableMetadataImpl() {
assert(UseMap.empty() && "Cannot destroy in-use replaceable metadata");
}
/// \brief Replace all uses of this with MD.
///
/// Replace all uses of this with \c MD, which is allowed to be null.
void replaceAllUsesWith(Metadata *MD);
/// \brief Resolve all uses of this.
///
/// Resolve all uses of this, turning off RAUW permanently. If \c
/// ResolveUsers, call \a GenericMDNode::resolve() on any users whose last
/// operand is resolved.
void resolveAllUses(bool ResolveUsers = true);
private:
void addRef(void *Ref, OwnerTy Owner);
void dropRef(void *Ref);
void moveRef(void *Ref, void *New, const Metadata &MD);
static ReplaceableMetadataImpl *get(Metadata &MD);
};
/// \brief Value wrapper in the Metadata hierarchy.
///
/// This is a custom value handle that allows other metadata to refer to
/// classes in the Value hierarchy.
///
/// Because of full uniquing support, each value is only wrapped by a single \a
/// ValueAsMetadata object, so the lookup maps are far more efficient than
/// those using ValueHandleBase.
class ValueAsMetadata : public Metadata, ReplaceableMetadataImpl {
friend class ReplaceableMetadataImpl;
friend class LLVMContextImpl;
Value *V;
protected:
ValueAsMetadata(LLVMContext &Context, unsigned ID, Value *V)
: Metadata(ID), V(V) {
assert(V && "Expected valid value");
}
~ValueAsMetadata() {}
public:
static ValueAsMetadata *get(Value *V);
static ConstantAsMetadata *getConstant(Value *C) {
return cast<ConstantAsMetadata>(get(C));
}
static LocalAsMetadata *getLocal(Value *Local) {
return cast<LocalAsMetadata>(get(Local));
}
static ValueAsMetadata *getIfExists(Value *V);
static ConstantAsMetadata *getConstantIfExists(Value *C) {
return cast_or_null<ConstantAsMetadata>(getIfExists(C));
}
static LocalAsMetadata *getLocalIfExists(Value *Local) {
return cast_or_null<LocalAsMetadata>(getIfExists(Local));
}
Value *getValue() const { return V; }
Type *getType() const { return V->getType(); }
LLVMContext &getContext() const { return V->getContext(); }
static void handleDeletion(Value *V);
static void handleRAUW(Value *From, Value *To);
protected:
/// \brief Handle collisions after \a Value::replaceAllUsesWith().
///
/// RAUW isn't supported directly for \a ValueAsMetadata, but if the wrapped
/// \a Value gets RAUW'ed and the target already exists, this is used to
/// merge the two metadata nodes.
void replaceAllUsesWith(Metadata *MD) {
ReplaceableMetadataImpl::replaceAllUsesWith(MD);
}
public:
static bool classof(const Metadata *MD) {
return MD->getMetadataID() == LocalAsMetadataKind ||
MD->getMetadataID() == ConstantAsMetadataKind;
} }
}; };
class ConstantAsMetadata : public ValueAsMetadata {
friend class ValueAsMetadata;
ConstantAsMetadata(LLVMContext &Context, Constant *C)
: ValueAsMetadata(Context, ConstantAsMetadataKind, C) {}
public:
static ConstantAsMetadata *get(Constant *C) {
return ValueAsMetadata::getConstant(C);
}
static ConstantAsMetadata *getIfExists(Constant *C) {
return ValueAsMetadata::getConstantIfExists(C);
}
Constant *getValue() const {
return cast<Constant>(ValueAsMetadata::getValue());
}
static bool classof(const Metadata *MD) {
return MD->getMetadataID() == ConstantAsMetadataKind;
}
};
class LocalAsMetadata : public ValueAsMetadata {
friend class ValueAsMetadata;
LocalAsMetadata(LLVMContext &Context, Value *Local)
: ValueAsMetadata(Context, LocalAsMetadataKind, Local) {
assert(!isa<Constant>(Local) && "Expected local value");
}
public:
static LocalAsMetadata *get(Value *Local) {
return ValueAsMetadata::getLocal(Local);
}
static LocalAsMetadata *getIfExists(Value *Local) {
return ValueAsMetadata::getLocalIfExists(Local);
}
static bool classof(const Metadata *MD) {
return MD->getMetadataID() == LocalAsMetadataKind;
}
};
/// \brief Transitional API for extracting constants from Metadata.
///
/// This namespace contains transitional functions for metadata that points to
/// \a Constants.
///
/// In prehistory -- when metadata was a subclass of \a Value -- \a MDNode
/// operands could refer to any \a Value. There's was a lot of code like this:
///
/// \code
/// MDNode *N = ...;
/// auto *CI = dyn_cast<ConstantInt>(N->getOperand(2));
/// \endcode
///
/// Now that \a Value and \a Metadata are in separate hierarchies, maintaining
/// the semantics for \a isa(), \a cast(), \a dyn_cast() (etc.) requires three
/// steps: cast in the \a Metadata hierarchy, extraction of the \a Value, and
/// cast in the \a Value hierarchy. Besides creating boiler-plate, this
/// requires subtle control flow changes.
///
/// The end-goal is to create a new type of metadata, called (e.g.) \a MDInt,
/// so that metadata can refer to numbers without traversing a bridge to the \a
/// Value hierarchy. In this final state, the code above would look like this:
///
/// \code
/// MDNode *N = ...;
/// auto *MI = dyn_cast<MDInt>(N->getOperand(2));
/// \endcode
///
/// The API in this namespace supports the transition. \a MDInt doesn't exist
/// yet, and even once it does, changing each metadata schema to use it is its
/// own mini-project. In the meantime this API prevents us from introducing
/// complex and bug-prone control flow that will disappear in the end. In
/// particular, the above code looks like this:
///
/// \code
/// MDNode *N = ...;
/// auto *CI = mdconst::dyn_extract<ConstantInt>(N->getOperand(2));
/// \endcode
///
/// The full set of provided functions includes:
///
/// mdconst::hasa <=> isa
/// mdconst::extract <=> cast
/// mdconst::extract_or_null <=> cast_or_null
/// mdconst::dyn_extract <=> dyn_cast
/// mdconst::dyn_extract_or_null <=> dyn_cast_or_null
///
/// The target of the cast must be a subclass of \a Constant.
namespace mdconst {
namespace detail {
template <class T> T &make();
template <class T, class Result> struct HasDereference {
typedef char Yes[1];
typedef char No[2];
template <size_t N> struct SFINAE {};
template <class U, class V>
static Yes &hasDereference(SFINAE<sizeof(static_cast<V>(*make<U>()))> * = 0);
template <class U, class V> static No &hasDereference(...);
static const bool value =
sizeof(hasDereference<T, Result>(nullptr)) == sizeof(Yes);
};
template <class V, class M> struct IsValidPointer {
static const bool value = std::is_base_of<Constant, V>::value &&
HasDereference<M, const Metadata &>::value;
};
template <class V, class M> struct IsValidReference {
static const bool value = std::is_base_of<Constant, V>::value &&
std::is_convertible<M, const Metadata &>::value;
};
} // end namespace detail
/// \brief Check whether Metadata has a Value.
///
/// As an analogue to \a isa(), check whether \c MD has an \a Value inside of
/// type \c X.
template <class X, class Y>
inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, bool>::type
hasa(Y &&MD) {
assert(MD && "Null pointer sent into hasa");
if (auto *V = dyn_cast<ConstantAsMetadata>(MD))
return isa<X>(V->getValue());
return false;
}
template <class X, class Y>
inline
typename std::enable_if<detail::IsValidReference<X, Y &>::value, bool>::type
hasa(Y &MD) {
return hasa(&MD);
}
/// \brief Extract a Value from Metadata.
///
/// As an analogue to \a cast(), extract the \a Value subclass \c X from \c MD.
template <class X, class Y>
inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
extract(Y &&MD) {
return cast<X>(cast<ConstantAsMetadata>(MD)->getValue());
}
template <class X, class Y>
inline
typename std::enable_if<detail::IsValidReference<X, Y &>::value, X *>::type
extract(Y &MD) {
return extract(&MD);
}
/// \brief Extract a Value from Metadata, allowing null.
///
/// As an analogue to \a cast_or_null(), extract the \a Value subclass \c X
/// from \c MD, allowing \c MD to be null.
template <class X, class Y>
inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
extract_or_null(Y &&MD) {
if (auto *V = cast_or_null<ConstantAsMetadata>(MD))
return cast<X>(V->getValue());
return nullptr;
}
/// \brief Extract a Value from Metadata, if any.
///
/// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X
/// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a
/// Value it does contain is of the wrong subclass.
template <class X, class Y>
inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
dyn_extract(Y &&MD) {
if (auto *V = dyn_cast<ConstantAsMetadata>(MD))
return dyn_cast<X>(V->getValue());
return nullptr;
}
/// \brief Extract a Value from Metadata, if any, allowing null.
///
/// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X
/// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a
/// Value it does contain is of the wrong subclass, allowing \c MD to be null.
template <class X, class Y>
inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
dyn_extract_or_null(Y &&MD) {
if (auto *V = dyn_cast_or_null<ConstantAsMetadata>(MD))
return dyn_cast<X>(V->getValue());
return nullptr;
}
} // end namespace mdconst
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// \brief A single uniqued string. /// \brief A single uniqued string.
/// ///
@ -60,15 +430,13 @@ public:
class MDString : public Metadata { class MDString : public Metadata {
friend class StringMapEntry<MDString>; friend class StringMapEntry<MDString>;
virtual void anchor();
MDString(const MDString &) LLVM_DELETED_FUNCTION; MDString(const MDString &) LLVM_DELETED_FUNCTION;
MDString &operator=(MDString &&) LLVM_DELETED_FUNCTION;
MDString &operator=(const MDString &) LLVM_DELETED_FUNCTION;
StringMapEntry<MDString> *Entry; StringMapEntry<MDString> *Entry;
explicit MDString(LLVMContext &Context) MDString() : Metadata(MDStringKind), Entry(nullptr) {}
: Metadata(Context, Value::MDStringVal), Entry(nullptr) {} MDString(MDString &&) : Metadata(MDStringKind) {}
/// \brief Shadow Value::getName() to prevent its use.
StringRef getName() const LLVM_DELETED_FUNCTION;
public: public:
static MDString *get(LLVMContext &Context, StringRef Str); static MDString *get(LLVMContext &Context, StringRef Str);
@ -89,8 +457,8 @@ public:
iterator end() const { return getString().end(); } iterator end() const { return getString().end(); }
/// \brief Methods for support type inquiry through isa, cast, and dyn_cast. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
static bool classof(const Value *V) { static bool classof(const Metadata *MD) {
return V->getValueID() == MDStringVal; return MD->getMetadataID() == MDStringKind;
} }
}; };
@ -138,18 +506,80 @@ struct DenseMapInfo<AAMDNodes> {
} }
}; };
class MDNodeOperand; /// \brief Tracking metadata reference owned by Metadata.
///
/// Similar to \a TrackingMDRef, but it's expected to be owned by an instance
/// of \a Metadata, which has the option of registering itself for callbacks to
/// re-unique itself.
///
/// In particular, this is used by \a MDNode.
class MDOperand {
MDOperand(MDOperand &&) LLVM_DELETED_FUNCTION;
MDOperand(const MDOperand &) LLVM_DELETED_FUNCTION;
MDOperand &operator=(MDOperand &&) LLVM_DELETED_FUNCTION;
MDOperand &operator=(const MDOperand &) LLVM_DELETED_FUNCTION;
Metadata *MD;
public:
MDOperand() : MD(nullptr) {}
~MDOperand() { untrack(); }
LLVM_EXPLICIT operator bool() const { return get(); }
Metadata *get() const { return MD; }
operator Metadata *() const { return get(); }
Metadata *operator->() const { return get(); }
Metadata &operator*() const { return *get(); }
void reset() {
untrack();
MD = nullptr;
}
void reset(Metadata *MD, Metadata *Owner) {
untrack();
this->MD = MD;
track(Owner);
}
private:
void track(Metadata *Owner) {
if (MD) {
if (Owner)
MetadataTracking::track(this, *MD, *Owner);
else
MetadataTracking::track(MD);
}
}
void untrack() {
assert(static_cast<void *>(this) == &MD && "Expected same address");
if (MD)
MetadataTracking::untrack(MD);
}
};
template <> struct simplify_type<MDOperand> {
typedef Metadata *SimpleType;
static SimpleType getSimplifiedValue(MDOperand &MD) { return MD.get(); }
};
template <> struct simplify_type<const MDOperand> {
typedef Metadata *SimpleType;
static SimpleType getSimplifiedValue(const MDOperand &MD) { return MD.get(); }
};
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// \brief Tuple of metadata. /// \brief Tuple of metadata.
class MDNode : public Metadata { class MDNode : public Metadata {
MDNode(const MDNode &) LLVM_DELETED_FUNCTION; MDNode(const MDNode &) LLVM_DELETED_FUNCTION;
void operator=(const MDNode &) LLVM_DELETED_FUNCTION; void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
friend class MDNodeOperand;
friend class LLVMContextImpl;
void *operator new(size_t) LLVM_DELETED_FUNCTION; void *operator new(size_t) LLVM_DELETED_FUNCTION;
LLVMContext &Context;
unsigned NumOperands;
protected: protected:
unsigned MDNodeSubclassData;
void *operator new(size_t Size, unsigned NumOps); void *operator new(size_t Size, unsigned NumOps);
/// \brief Required by std, but never called. /// \brief Required by std, but never called.
@ -165,83 +595,83 @@ protected:
llvm_unreachable("Constructor throws?"); llvm_unreachable("Constructor throws?");
} }
/// \brief Subclass data enums. MDNode(LLVMContext &Context, unsigned ID, ArrayRef<Metadata *> MDs);
enum { ~MDNode() { dropAllReferences(); }
/// FunctionLocalBit - This bit is set if this MDNode is function local.
/// This is true when it (potentially transitively) contains a reference to
/// something in a function, like an argument, basicblock, or instruction.
FunctionLocalBit = 1 << 0,
/// NotUniquedBit - This is set on MDNodes that are not uniqued because they void dropAllReferences();
/// have a null operand. void storeDistinctInContext();
NotUniquedBit = 1 << 1
};
/// \brief FunctionLocal enums. static MDNode *getMDNode(LLVMContext &C, ArrayRef<Metadata *> MDs,
enum FunctionLocalness { bool Insert = true);
FL_Unknown = -1,
FL_No = 0,
FL_Yes = 1
};
/// \brief Replace each instance of the given operand with a new value. MDOperand *mutable_begin() { return mutable_end() - NumOperands; }
void replaceOperand(MDNodeOperand *Op, Value *NewVal); MDOperand *mutable_end() { return reinterpret_cast<MDOperand *>(this); }
MDNode(LLVMContext &C, unsigned ID, ArrayRef<Value *> Vals,
bool isFunctionLocal);
~MDNode() {}
static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
FunctionLocalness FL, bool Insert = true);
public: public:
static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals); static MDNode *get(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
/// \brief Construct MDNode with an explicit function-localness. return getMDNode(Context, MDs, true);
/// }
/// Don't analyze Vals; trust isFunctionLocal.
static MDNode *getWhenValsUnresolved(LLVMContext &Context, static MDNode *getWhenValsUnresolved(LLVMContext &Context,
ArrayRef<Value*> Vals, ArrayRef<Metadata *> MDs) {
bool isFunctionLocal); // TODO: Remove this.
return get(Context, MDs);
}
static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals); static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
return getMDNode(Context, MDs, false);
}
/// \brief Return a temporary MDNode /// \brief Return a temporary MDNode
/// ///
/// For use in constructing cyclic MDNode structures. A temporary MDNode is /// For use in constructing cyclic MDNode structures. A temporary MDNode is
/// not uniqued, may be RAUW'd, and must be manually deleted with /// not uniqued, may be RAUW'd, and must be manually deleted with
/// deleteTemporary. /// deleteTemporary.
static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals); static MDNodeFwdDecl *getTemporary(LLVMContext &Context,
ArrayRef<Metadata *> MDs);
/// \brief Deallocate a node created by getTemporary. /// \brief Deallocate a node created by getTemporary.
/// ///
/// The node must not have any users. /// The node must not have any users.
static void deleteTemporary(MDNode *N); static void deleteTemporary(MDNode *N);
/// \brief Replace a specific operand. LLVMContext &getContext() const { return Context; }
void replaceOperandWith(unsigned i, Value *NewVal);
/// \brief Return specified operand. /// \brief Replace a specific operand.
Value *getOperand(unsigned i) const LLVM_READONLY; void replaceOperandWith(unsigned I, Metadata *New);
/// \brief Check if node is fully resolved.
bool isResolved() const;
protected:
/// \brief Set an operand.
///
/// Sets the operand directly, without worrying about uniquing.
void setOperand(unsigned I, Metadata *New);
public:
typedef const MDOperand *op_iterator;
typedef iterator_range<op_iterator> op_range;
op_iterator op_begin() const {
return const_cast<MDNode *>(this)->mutable_begin();
}
op_iterator op_end() const {
return const_cast<MDNode *>(this)->mutable_end();
}
op_range operands() const { return op_range(op_begin(), op_end()); }
const MDOperand &getOperand(unsigned I) const {
assert(I < NumOperands && "Out of range");
return op_begin()[I];
}
/// \brief Return number of MDNode operands. /// \brief Return number of MDNode operands.
unsigned getNumOperands() const { return NumOperands; } unsigned getNumOperands() const { return NumOperands; }
/// \brief Return whether MDNode is local to a function.
bool isFunctionLocal() const {
return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
}
/// \brief Return the first function-local operand's function.
///
/// If this metadata is function-local and recursively has a function-local
/// operand, return the first such operand's parent function. Otherwise,
/// return null. getFunction() should not be used for performance- critical
/// code because it recursively visits all the MDNode's operands.
const Function *getFunction() const;
/// \brief Methods for support type inquiry through isa, cast, and dyn_cast: /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
static bool classof(const Value *V) { static bool classof(const Metadata *MD) {
return V->getValueID() == GenericMDNodeVal || return MD->getMetadataID() == GenericMDNodeKind ||
V->getValueID() == MDNodeFwdDeclVal; MD->getMetadataID() == MDNodeFwdDeclKind;
} }
/// \brief Check whether MDNode is a vtable access. /// \brief Check whether MDNode is a vtable access.
@ -254,18 +684,6 @@ public:
static AAMDNodes getMostGenericAA(const AAMDNodes &A, const AAMDNodes &B); static AAMDNodes getMostGenericAA(const AAMDNodes &A, const AAMDNodes &B);
static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B); static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
static MDNode *getMostGenericRange(MDNode *A, MDNode *B); static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
protected:
bool isNotUniqued() const {
return (getSubclassDataFromValue() & NotUniquedBit) != 0;
}
void setIsNotUniqued();
// Shadow Value::setValueSubclassData with a private forwarding method so that
// any future subclasses cannot accidentally use it.
void setValueSubclassData(unsigned short D) {
Value::setValueSubclassData(D);
}
}; };
/// \brief Generic metadata node. /// \brief Generic metadata node.
@ -279,24 +697,59 @@ protected:
/// TODO: Make uniquing opt-out (status: mandatory, sometimes dropped). /// TODO: Make uniquing opt-out (status: mandatory, sometimes dropped).
/// TODO: Drop support for RAUW. /// TODO: Drop support for RAUW.
class GenericMDNode : public MDNode { class GenericMDNode : public MDNode {
friend class Metadata;
friend class MDNode; friend class MDNode;
friend class LLVMContextImpl; friend class LLVMContextImpl;
friend class ReplaceableMetadataImpl;
unsigned Hash; /// \brief Support RAUW as long as one of its arguments is replaceable.
///
/// If an operand is an \a MDNodeFwdDecl (or a replaceable \a GenericMDNode),
/// support RAUW to support uniquing as forward declarations are resolved.
/// As soon as operands have been resolved, drop support.
///
/// FIXME: Save memory by storing this in a pointer union with the
/// LLVMContext, and adding an LLVMContext reference to RMI.
std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses;
GenericMDNode(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal) GenericMDNode(LLVMContext &C, ArrayRef<Metadata *> Vals);
: MDNode(C, GenericMDNodeVal, Vals, isFunctionLocal), Hash(0) {}
~GenericMDNode(); ~GenericMDNode();
void dropAllReferences(); void setHash(unsigned Hash) { MDNodeSubclassData = Hash; }
public: public:
/// \brief Get the hash, if any. /// \brief Get the hash, if any.
unsigned getHash() const { return Hash; } unsigned getHash() const { return MDNodeSubclassData; }
static bool classof(const Value *V) { static bool classof(const Metadata *MD) {
return V->getValueID() == GenericMDNodeVal; return MD->getMetadataID() == GenericMDNodeKind;
} }
/// \brief Check whether any operands are forward declarations.
///
/// Returns \c true as long as any operands (or their operands, etc.) are \a
/// MDNodeFwdDecl.
///
/// As forward declarations are resolved, their containers should get
/// resolved automatically. However, if this (or one of its operands) is
/// involved in a cycle, \a resolveCycles() needs to be called explicitly.
bool isResolved() const { return !ReplaceableUses; }
/// \brief Resolve cycles.
///
/// Once all forward declarations have been resolved, force cycles to be
/// resolved.
///
/// \pre No operands (or operands' operands, etc.) are \a MDNodeFwdDecl.
void resolveCycles();
private:
void handleChangedOperand(void *Ref, Metadata *New);
bool hasUnresolvedOperands() const { return SubclassData32; }
void incrementUnresolvedOperands() { ++SubclassData32; }
void decrementUnresolvedOperands() { --SubclassData32; }
void resolve();
}; };
/// \brief Forward declaration of metadata. /// \brief Forward declaration of metadata.
@ -304,17 +757,21 @@ public:
/// Forward declaration of metadata, in the form of a metadata node. Unlike \a /// Forward declaration of metadata, in the form of a metadata node. Unlike \a
/// GenericMDNode, this class has support for RAUW and is suitable for forward /// GenericMDNode, this class has support for RAUW and is suitable for forward
/// references. /// references.
class MDNodeFwdDecl : public MDNode { class MDNodeFwdDecl : public MDNode, ReplaceableMetadataImpl {
friend class Metadata;
friend class MDNode; friend class MDNode;
friend class ReplaceableMetadataImpl;
MDNodeFwdDecl(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal) MDNodeFwdDecl(LLVMContext &C, ArrayRef<Metadata *> Vals)
: MDNode(C, MDNodeFwdDeclVal, Vals, isFunctionLocal) {} : MDNode(C, MDNodeFwdDeclKind, Vals) {}
~MDNodeFwdDecl() {} ~MDNodeFwdDecl() {}
public: public:
static bool classof(const Value *V) { static bool classof(const Metadata *MD) {
return V->getValueID() == MDNodeFwdDeclVal; return MD->getMetadataID() == MDNodeFwdDeclKind;
} }
using ReplaceableMetadataImpl::replaceAllUsesWith;
}; };
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -333,7 +790,7 @@ class NamedMDNode : public ilist_node<NamedMDNode> {
std::string Name; std::string Name;
Module *Parent; Module *Parent;
void *Operands; // SmallVector<TrackingVH<MDNode>, 4> void *Operands; // SmallVector<TrackingMDRef, 4>
void setParent(Module *M) { Parent = M; } void setParent(Module *M) { Parent = M; }

View File

@ -0,0 +1,99 @@
//===- llvm/IR/MetadataTracking.h - Metadata tracking ---------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Low-level functions to enable tracking of metadata that could RAUW.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_IR_METADATATRACKING_H
#define LLVM_IR_METADATATRACKING_H
#include "llvm/ADT/PointerUnion.h"
#include "llvm/Support/Casting.h"
#include <type_traits>
namespace llvm {
class Metadata;
class MetadataAsValue;
/// \brief API for tracking metadata references through RAUW and deletion.
///
/// Shared API for updating \a Metadata pointers in subclasses that support
/// RAUW.
///
/// This API is not meant to be used directly. See \a TrackingMDRef for a
/// user-friendly tracking reference.
class MetadataTracking {
public:
/// \brief Track the reference to metadata.
///
/// Register \c MD with \c *MD, if the subclass supports tracking. If \c *MD
/// gets RAUW'ed, \c MD will be updated to the new address. If \c *MD gets
/// deleted, \c MD will be set to \c nullptr.
///
/// If tracking isn't supported, \c *MD will not change.
///
/// \return true iff tracking is supported by \c MD.
static bool track(Metadata *&MD) {
return track(&MD, *MD, static_cast<Metadata *>(nullptr));
}
/// \brief Track the reference to metadata for \a Metadata.
///
/// As \a track(Metadata*&), but with support for calling back to \c Owner to
/// tell it that its operand changed. This could trigger \c Owner being
/// re-uniqued.
static bool track(void *Ref, Metadata &MD, Metadata &Owner) {
return track(Ref, MD, &Owner);
}
/// \brief Track the reference to metadata for \a MetadataAsValue.
///
/// As \a track(Metadata*&), but with support for calling back to \c Owner to
/// tell it that its operand changed. This could trigger \c Owner being
/// re-uniqued.
static bool track(void *Ref, Metadata &MD, MetadataAsValue &Owner) {
return track(Ref, MD, &Owner);
}
/// \brief Stop tracking a reference to metadata.
///
/// Stops \c *MD from tracking \c MD.
static void untrack(Metadata *&MD) { untrack(&MD, *MD); }
static void untrack(void *Ref, Metadata &MD);
/// \brief Move tracking from one reference to another.
///
/// Semantically equivalent to \c untrack(MD) followed by \c track(New),
/// except that ownership callbacks are maintained.
///
/// Note: it is an error if \c *MD does not equal \c New.
///
/// \return true iff tracking is supported by \c MD.
static bool retrack(Metadata *&MD, Metadata *&New) {
return retrack(&MD, *MD, &New);
}
static bool retrack(void *Ref, Metadata &MD, void *New);
/// \brief Check whether metadata is replaceable.
static bool isReplaceable(const Metadata &MD);
typedef PointerUnion<MetadataAsValue *, Metadata *> OwnerTy;
private:
/// \brief Track a reference to metadata for an owner.
///
/// Generalized version of tracking.
static bool track(void *Ref, Metadata &MD, OwnerTy Owner);
};
} // end namespace llvm
#endif

View File

@ -188,16 +188,16 @@ public:
ModFlagBehaviorLastVal = AppendUnique ModFlagBehaviorLastVal = AppendUnique
}; };
/// Checks if Value represents a valid ModFlagBehavior, and stores the /// Checks if Metadata represents a valid ModFlagBehavior, and stores the
/// converted result in MFB. /// converted result in MFB.
static bool isValidModFlagBehavior(Value *V, ModFlagBehavior &MFB); static bool isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB);
struct ModuleFlagEntry { struct ModuleFlagEntry {
ModFlagBehavior Behavior; ModFlagBehavior Behavior;
MDString *Key; MDString *Key;
Value *Val; Metadata *Val;
ModuleFlagEntry(ModFlagBehavior B, MDString *K, Value *V) ModuleFlagEntry(ModFlagBehavior B, MDString *K, Metadata *V)
: Behavior(B), Key(K), Val(V) {} : Behavior(B), Key(K), Val(V) {}
}; };
/// @} /// @}
@ -442,7 +442,7 @@ public:
/// Return the corresponding value if Key appears in module flags, otherwise /// Return the corresponding value if Key appears in module flags, otherwise
/// return null. /// return null.
Value *getModuleFlag(StringRef Key) const; Metadata *getModuleFlag(StringRef Key) const;
/// Returns the NamedMDNode in the module that represents module-level flags. /// Returns the NamedMDNode in the module that represents module-level flags.
/// This method returns null if there are no module-level flags. /// This method returns null if there are no module-level flags.
@ -455,7 +455,8 @@ public:
/// Add a module-level flag to the module-level flags metadata. It will create /// Add a module-level flag to the module-level flags metadata. It will create
/// the module-level flags named metadata if it doesn't already exist. /// the module-level flags named metadata if it doesn't already exist.
void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, Value *Val); void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, Metadata *Val);
void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, Constant *Val);
void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, uint32_t Val); void addModuleFlag(ModFlagBehavior Behavior, StringRef Key, uint32_t Val);
void addModuleFlag(MDNode *Node); void addModuleFlag(MDNode *Node);

View File

@ -0,0 +1,166 @@
//===- llvm/IR/TrackingMDRef.h - Tracking Metadata references -------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// References to metadata that track RAUW.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_IR_TRACKINGMDREF_H
#define LLVM_IR_TRACKINGMDREF_H
#include "llvm/IR/MetadataTracking.h"
#include "llvm/Support/Casting.h"
namespace llvm {
class Metadata;
class MDNode;
class ValueAsMetadata;
/// \brief Tracking metadata reference.
///
/// This class behaves like \a TrackingVH, but for metadata.
class TrackingMDRef {
Metadata *MD;
public:
TrackingMDRef() : MD(nullptr) {}
explicit TrackingMDRef(Metadata *MD) : MD(MD) { track(); }
TrackingMDRef(TrackingMDRef &&X) : MD(X.MD) { retrack(X); }
TrackingMDRef(const TrackingMDRef &X) : MD(X.MD) { track(); }
TrackingMDRef &operator=(TrackingMDRef &&X) {
if (&X == this)
return *this;
untrack();
MD = X.MD;
retrack(X);
return *this;
}
TrackingMDRef &operator=(const TrackingMDRef &X) {
if (&X == this)
return *this;
untrack();
MD = X.MD;
track();
return *this;
}
~TrackingMDRef() { untrack(); }
LLVM_EXPLICIT operator bool() const { return get(); }
Metadata *get() const { return MD; }
operator Metadata *() const { return get(); }
Metadata *operator->() const { return get(); }
Metadata &operator*() const { return *get(); }
void reset() {
untrack();
MD = nullptr;
}
void reset(Metadata *MD) {
untrack();
this->MD = MD;
track();
}
/// \brief Check whether this has a trivial destructor.
///
/// If \c MD isn't replaceable, the destructor will be a no-op.
bool hasTrivialDestructor() const {
return !MD || !MetadataTracking::isReplaceable(*MD);
}
private:
void track() {
if (MD)
MetadataTracking::track(MD);
}
void untrack() {
if (MD)
MetadataTracking::untrack(MD);
}
void retrack(TrackingMDRef &X) {
assert(MD == X.MD && "Expected values to match");
if (X.MD) {
MetadataTracking::retrack(X.MD, MD);
X.MD = nullptr;
}
}
};
/// \brief Typed tracking ref.
///
/// Track refererences of a particular type. It's useful to use this for \a
/// MDNode and \a ValueAsMetadata.
template <class T> class TypedTrackingMDRef {
TrackingMDRef Ref;
public:
TypedTrackingMDRef() {}
explicit TypedTrackingMDRef(T *MD) : Ref(static_cast<Metadata *>(MD)) {}
TypedTrackingMDRef(TypedTrackingMDRef &&X) : Ref(std::move(X.Ref)) {}
TypedTrackingMDRef(const TypedTrackingMDRef &X) : Ref(X.Ref) {}
TypedTrackingMDRef &operator=(TypedTrackingMDRef &&X) {
Ref = std::move(X.Ref);
return *this;
}
TypedTrackingMDRef &operator=(const TypedTrackingMDRef &X) {
Ref = X.Ref;
return *this;
}
LLVM_EXPLICIT operator bool() const { return get(); }
T *get() const { return (T *)Ref.get(); }
operator T *() const { return get(); }
T *operator->() const { return get(); }
T &operator*() const { return *get(); }
void reset() { Ref.reset(); }
void reset(T *MD) { Ref.reset(static_cast<Metadata *>(MD)); }
/// \brief Check whether this has a trivial destructor.
bool hasTrivialDestructor() const { return Ref.hasTrivialDestructor(); }
};
typedef TypedTrackingMDRef<MDNode> TrackingMDNodeRef;
typedef TypedTrackingMDRef<ValueAsMetadata> TrackingValueAsMetadataRef;
// Expose the underlying metadata to casting.
template <> struct simplify_type<TrackingMDRef> {
typedef Metadata *SimpleType;
static SimpleType getSimplifiedValue(TrackingMDRef &MD) { return MD.get(); }
};
template <> struct simplify_type<const TrackingMDRef> {
typedef Metadata *SimpleType;
static SimpleType getSimplifiedValue(const TrackingMDRef &MD) {
return MD.get();
}
};
template <class T> struct simplify_type<TypedTrackingMDRef<T>> {
typedef T *SimpleType;
static SimpleType getSimplifiedValue(TypedTrackingMDRef<T> &MD) {
return MD.get();
}
};
template <class T> struct simplify_type<const TypedTrackingMDRef<T>> {
typedef T *SimpleType;
static SimpleType getSimplifiedValue(const TypedTrackingMDRef<T> &MD) {
return MD.get();
}
};
} // end namespace llvm
#endif

View File

@ -31,6 +31,7 @@ class TypeFinder {
// To avoid walking constant expressions multiple times and other IR // To avoid walking constant expressions multiple times and other IR
// objects, we keep several helper maps. // objects, we keep several helper maps.
DenseSet<const Value*> VisitedConstants; DenseSet<const Value*> VisitedConstants;
DenseSet<const MDNode *> VisitedMetadata;
DenseSet<Type*> VisitedTypes; DenseSet<Type*> VisitedTypes;
std::vector<StructType*> StructTypes; std::vector<StructType*> StructTypes;

View File

@ -37,7 +37,6 @@ class GlobalVariable;
class InlineAsm; class InlineAsm;
class Instruction; class Instruction;
class LLVMContext; class LLVMContext;
class MDNode;
class Module; class Module;
class StringRef; class StringRef;
class Twine; class Twine;
@ -70,9 +69,9 @@ class Value {
Type *VTy; Type *VTy;
Use *UseList; Use *UseList;
friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name. friend class ValueAsMetadata; // Allow access to NameAndIsUsedByMD.
friend class ValueHandleBase; friend class ValueHandleBase;
ValueName *Name; PointerIntPair<ValueName *, 1> NameAndIsUsedByMD;
const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast) const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this? unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
@ -226,10 +225,14 @@ public:
LLVMContext &getContext() const; LLVMContext &getContext() const;
// \brief All values can potentially be named. // \brief All values can potentially be named.
bool hasName() const { return Name != nullptr; } bool hasName() const { return getValueName() != nullptr; }
ValueName *getValueName() const { return Name; } ValueName *getValueName() const { return NameAndIsUsedByMD.getPointer(); }
void setValueName(ValueName *VN) { Name = VN; } void setValueName(ValueName *VN) { NameAndIsUsedByMD.setPointer(VN); }
private:
void destroyValueName();
public:
/// \brief Return a constant reference to the value's name. /// \brief Return a constant reference to the value's name.
/// ///
/// This is cheap and guaranteed to return the same reference as long as the /// This is cheap and guaranteed to return the same reference as long as the
@ -352,9 +355,7 @@ public:
ConstantStructVal, // This is an instance of ConstantStruct ConstantStructVal, // This is an instance of ConstantStruct
ConstantVectorVal, // This is an instance of ConstantVector ConstantVectorVal, // This is an instance of ConstantVector
ConstantPointerNullVal, // This is an instance of ConstantPointerNull ConstantPointerNullVal, // This is an instance of ConstantPointerNull
GenericMDNodeVal, // This is an instance of GenericMDNode MetadataAsValueVal, // This is an instance of MetadataAsValue
MDNodeFwdDeclVal, // This is an instance of MDNodeFwdDecl
MDStringVal, // This is an instance of MDString
InlineAsmVal, // This is an instance of InlineAsm InlineAsmVal, // This is an instance of InlineAsm
InstructionVal, // This is an instance of Instruction InstructionVal, // This is an instance of Instruction
// Enum values starting at InstructionVal are used for Instructions; // Enum values starting at InstructionVal are used for Instructions;
@ -404,6 +405,9 @@ public:
/// \brief Return true if there is a value handle associated with this value. /// \brief Return true if there is a value handle associated with this value.
bool hasValueHandle() const { return HasValueHandle; } bool hasValueHandle() const { return HasValueHandle; }
/// \brief Return true if there is metadata referencing this value.
bool isUsedByMetadata() const { return NameAndIsUsedByMD.getInt(); }
/// \brief Strip off pointer casts, all-zero GEPs, and aliases. /// \brief Strip off pointer casts, all-zero GEPs, and aliases.
/// ///
/// Returns the original uncasted value. If this is called on a non-pointer /// Returns the original uncasted value. If this is called on a non-pointer
@ -687,13 +691,6 @@ template <> struct isa_impl<GlobalObject, Value> {
} }
}; };
template <> struct isa_impl<MDNode, Value> {
static inline bool doit(const Value &Val) {
return Val.getValueID() == Value::GenericMDNodeVal ||
Val.getValueID() == Value::MDNodeFwdDeclVal;
}
};
// Value* is only 4-byte aligned. // Value* is only 4-byte aligned.
template<> template<>
class PointerLikeTypeTraits<Value*> { class PointerLikeTypeTraits<Value*> {

View File

@ -27,6 +27,7 @@
#define LLVM_IR_VALUEMAP_H #define LLVM_IR_VALUEMAP_H
#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMap.h"
#include "llvm/IR/TrackingMDRef.h"
#include "llvm/IR/ValueHandle.h" #include "llvm/IR/ValueHandle.h"
#include "llvm/Support/Mutex.h" #include "llvm/Support/Mutex.h"
#include "llvm/Support/UniqueLock.h" #include "llvm/Support/UniqueLock.h"
@ -79,8 +80,10 @@ class ValueMap {
friend class ValueMapCallbackVH<KeyT, ValueT, Config>; friend class ValueMapCallbackVH<KeyT, ValueT, Config>;
typedef ValueMapCallbackVH<KeyT, ValueT, Config> ValueMapCVH; typedef ValueMapCallbackVH<KeyT, ValueT, Config> ValueMapCVH;
typedef DenseMap<ValueMapCVH, ValueT, DenseMapInfo<ValueMapCVH> > MapT; typedef DenseMap<ValueMapCVH, ValueT, DenseMapInfo<ValueMapCVH> > MapT;
typedef DenseMap<const Metadata *, TrackingMDRef> MDMapT;
typedef typename Config::ExtraData ExtraData; typedef typename Config::ExtraData ExtraData;
MapT Map; MapT Map;
std::unique_ptr<MDMapT> MDMap;
ExtraData Data; ExtraData Data;
ValueMap(const ValueMap&) LLVM_DELETED_FUNCTION; ValueMap(const ValueMap&) LLVM_DELETED_FUNCTION;
ValueMap& operator=(const ValueMap&) LLVM_DELETED_FUNCTION; ValueMap& operator=(const ValueMap&) LLVM_DELETED_FUNCTION;
@ -91,12 +94,19 @@ public:
typedef unsigned size_type; typedef unsigned size_type;
explicit ValueMap(unsigned NumInitBuckets = 64) explicit ValueMap(unsigned NumInitBuckets = 64)
: Map(NumInitBuckets), Data() {} : Map(NumInitBuckets), Data() {}
explicit ValueMap(const ExtraData &Data, unsigned NumInitBuckets = 64) explicit ValueMap(const ExtraData &Data, unsigned NumInitBuckets = 64)
: Map(NumInitBuckets), Data(Data) {} : Map(NumInitBuckets), Data(Data) {}
~ValueMap() {} ~ValueMap() {}
bool hasMD() const { return MDMap; }
MDMapT &MD() {
if (!MDMap)
MDMap.reset(new MDMapT);
return *MDMap;
}
typedef ValueMapIterator<MapT, KeyT> iterator; typedef ValueMapIterator<MapT, KeyT> iterator;
typedef ValueMapConstIterator<MapT, KeyT> const_iterator; typedef ValueMapConstIterator<MapT, KeyT> const_iterator;
inline iterator begin() { return iterator(Map.begin()); } inline iterator begin() { return iterator(Map.begin()); }
@ -110,7 +120,10 @@ public:
/// Grow the map so that it has at least Size buckets. Does not shrink /// Grow the map so that it has at least Size buckets. Does not shrink
void resize(size_t Size) { Map.resize(Size); } void resize(size_t Size) { Map.resize(Size); }
void clear() { Map.clear(); } void clear() {
Map.clear();
MDMap.reset();
}
/// Return 1 if the specified key is in the map, 0 otherwise. /// Return 1 if the specified key is in the map, 0 otherwise.
size_type count(const KeyT &Val) const { size_type count(const KeyT &Val) const {

View File

@ -71,20 +71,23 @@ namespace llvm {
ValueMapTypeRemapper *TypeMapper = nullptr, ValueMapTypeRemapper *TypeMapper = nullptr,
ValueMaterializer *Materializer = nullptr); ValueMaterializer *Materializer = nullptr);
Metadata *MapValue(const Metadata *MD, ValueToValueMapTy &VM,
RemapFlags Flags = RF_None,
ValueMapTypeRemapper *TypeMapper = nullptr,
ValueMaterializer *Materializer = nullptr);
/// MapValue - provide versions that preserve type safety for MDNodes.
MDNode *MapValue(const MDNode *MD, ValueToValueMapTy &VM,
RemapFlags Flags = RF_None,
ValueMapTypeRemapper *TypeMapper = nullptr,
ValueMaterializer *Materializer = nullptr);
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, void RemapInstruction(Instruction *I, ValueToValueMapTy &VM,
RemapFlags Flags = RF_None, RemapFlags Flags = RF_None,
ValueMapTypeRemapper *TypeMapper = nullptr, ValueMapTypeRemapper *TypeMapper = nullptr,
ValueMaterializer *Materializer = nullptr); ValueMaterializer *Materializer = nullptr);
/// MapValue - provide versions that preserve type safety for MDNode and /// MapValue - provide versions that preserve type safety for Constants.
/// Constants.
inline MDNode *MapValue(const MDNode *V, ValueToValueMapTy &VM,
RemapFlags Flags = RF_None,
ValueMapTypeRemapper *TypeMapper = nullptr,
ValueMaterializer *Materializer = nullptr) {
return cast<MDNode>(MapValue((const Value*)V, VM, Flags, TypeMapper,
Materializer));
}
inline Constant *MapValue(const Constant *V, ValueToValueMapTy &VM, inline Constant *MapValue(const Constant *V, ValueToValueMapTy &VM,
RemapFlags Flags = RF_None, RemapFlags Flags = RF_None,
ValueMapTypeRemapper *TypeMapper = nullptr, ValueMapTypeRemapper *TypeMapper = nullptr,

View File

@ -196,7 +196,8 @@ bool BranchProbabilityInfo::calcMetadataWeights(BasicBlock *BB) {
SmallVector<uint32_t, 2> Weights; SmallVector<uint32_t, 2> Weights;
Weights.reserve(TI->getNumSuccessors()); Weights.reserve(TI->getNumSuccessors());
for (unsigned i = 1, e = WeightsNode->getNumOperands(); i != e; ++i) { for (unsigned i = 1, e = WeightsNode->getNumOperands(); i != e; ++i) {
ConstantInt *Weight = dyn_cast<ConstantInt>(WeightsNode->getOperand(i)); ConstantInt *Weight =
mdconst::dyn_extract<ConstantInt>(WeightsNode->getOperand(i));
if (!Weight) if (!Weight)
return false; return false;
Weights.push_back( Weights.push_back(

View File

@ -3750,8 +3750,10 @@ static Optional<ConstantRange> GetRangeFromMetadata(Value *V) {
assert(NumRanges >= 1); assert(NumRanges >= 1);
for (unsigned i = 0; i < NumRanges; ++i) { for (unsigned i = 0; i < NumRanges; ++i) {
ConstantInt *Lower = cast<ConstantInt>(MD->getOperand(2*i + 0)); ConstantInt *Lower =
ConstantInt *Upper = cast<ConstantInt>(MD->getOperand(2*i + 1)); mdconst::extract<ConstantInt>(MD->getOperand(2 * i + 0));
ConstantInt *Upper =
mdconst::extract<ConstantInt>(MD->getOperand(2 * i + 1));
ConstantRange Range(Lower->getValue(), Upper->getValue()); ConstantRange Range(Lower->getValue(), Upper->getValue());
TotalRange = TotalRange.unionWith(Range); TotalRange = TotalRange.unionWith(Range);
} }

View File

@ -167,7 +167,7 @@ namespace {
bool TypeIsImmutable() const { bool TypeIsImmutable() const {
if (Node->getNumOperands() < 3) if (Node->getNumOperands() < 3)
return false; return false;
ConstantInt *CI = dyn_cast<ConstantInt>(Node->getOperand(2)); ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Node->getOperand(2));
if (!CI) if (!CI)
return false; return false;
return CI->getValue()[0]; return CI->getValue()[0];
@ -194,7 +194,7 @@ namespace {
return dyn_cast_or_null<MDNode>(Node->getOperand(1)); return dyn_cast_or_null<MDNode>(Node->getOperand(1));
} }
uint64_t getOffset() const { uint64_t getOffset() const {
return cast<ConstantInt>(Node->getOperand(2))->getZExtValue(); return mdconst::extract<ConstantInt>(Node->getOperand(2))->getZExtValue();
} }
/// TypeIsImmutable - Test if this TBAAStructTagNode represents a type for /// TypeIsImmutable - Test if this TBAAStructTagNode represents a type for
/// objects which are not modified (by any means) in the context where this /// objects which are not modified (by any means) in the context where this
@ -202,7 +202,7 @@ namespace {
bool TypeIsImmutable() const { bool TypeIsImmutable() const {
if (Node->getNumOperands() < 4) if (Node->getNumOperands() < 4)
return false; return false;
ConstantInt *CI = dyn_cast<ConstantInt>(Node->getOperand(3)); ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Node->getOperand(3));
if (!CI) if (!CI)
return false; return false;
return CI->getValue()[0]; return CI->getValue()[0];
@ -233,8 +233,10 @@ namespace {
// Fast path for a scalar type node and a struct type node with a single // Fast path for a scalar type node and a struct type node with a single
// field. // field.
if (Node->getNumOperands() <= 3) { if (Node->getNumOperands() <= 3) {
uint64_t Cur = Node->getNumOperands() == 2 ? 0 : uint64_t Cur = Node->getNumOperands() == 2
cast<ConstantInt>(Node->getOperand(2))->getZExtValue(); ? 0
: mdconst::extract<ConstantInt>(Node->getOperand(2))
->getZExtValue();
Offset -= Cur; Offset -= Cur;
MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(1)); MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(1));
if (!P) if (!P)
@ -246,8 +248,8 @@ namespace {
// the current offset is bigger than the given offset. // the current offset is bigger than the given offset.
unsigned TheIdx = 0; unsigned TheIdx = 0;
for (unsigned Idx = 1; Idx < Node->getNumOperands(); Idx += 2) { for (unsigned Idx = 1; Idx < Node->getNumOperands(); Idx += 2) {
uint64_t Cur = cast<ConstantInt>(Node->getOperand(Idx + 1))-> uint64_t Cur = mdconst::extract<ConstantInt>(Node->getOperand(Idx + 1))
getZExtValue(); ->getZExtValue();
if (Cur > Offset) { if (Cur > Offset) {
assert(Idx >= 3 && assert(Idx >= 3 &&
"TBAAStructTypeNode::getParent should have an offset match!"); "TBAAStructTypeNode::getParent should have an offset match!");
@ -258,8 +260,8 @@ namespace {
// Move along the last field. // Move along the last field.
if (TheIdx == 0) if (TheIdx == 0)
TheIdx = Node->getNumOperands() - 2; TheIdx = Node->getNumOperands() - 2;
uint64_t Cur = cast<ConstantInt>(Node->getOperand(TheIdx + 1))-> uint64_t Cur = mdconst::extract<ConstantInt>(Node->getOperand(TheIdx + 1))
getZExtValue(); ->getZExtValue();
Offset -= Cur; Offset -= Cur;
MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(TheIdx)); MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(TheIdx));
if (!P) if (!P)
@ -608,7 +610,8 @@ MDNode *MDNode::getMostGenericTBAA(MDNode *A, MDNode *B) {
return nullptr; return nullptr;
// We need to convert from a type node to a tag node. // We need to convert from a type node to a tag node.
Type *Int64 = IntegerType::get(A->getContext(), 64); Type *Int64 = IntegerType::get(A->getContext(), 64);
Value *Ops[3] = { Ret, Ret, ConstantInt::get(Int64, 0) }; Metadata *Ops[3] = {Ret, Ret,
ConstantAsMetadata::get(ConstantInt::get(Int64, 0))};
return MDNode::get(A->getContext(), Ops); return MDNode::get(A->getContext(), Ops);
} }

View File

@ -312,8 +312,10 @@ void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
// Use the high end of the ranges to find leading zeros. // Use the high end of the ranges to find leading zeros.
unsigned MinLeadingZeros = BitWidth; unsigned MinLeadingZeros = BitWidth;
for (unsigned i = 0; i < NumRanges; ++i) { for (unsigned i = 0; i < NumRanges; ++i) {
ConstantInt *Lower = cast<ConstantInt>(Ranges.getOperand(2*i + 0)); ConstantInt *Lower =
ConstantInt *Upper = cast<ConstantInt>(Ranges.getOperand(2*i + 1)); mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
ConstantInt *Upper =
mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
ConstantRange Range(Lower->getValue(), Upper->getValue()); ConstantRange Range(Lower->getValue(), Upper->getValue());
if (Range.isWrappedSet()) if (Range.isWrappedSet())
MinLeadingZeros = 0; // -1 has no zeros MinLeadingZeros = 0; // -1 has no zeros
@ -1504,8 +1506,10 @@ static bool rangeMetadataExcludesValue(MDNode* Ranges,
const unsigned NumRanges = Ranges->getNumOperands() / 2; const unsigned NumRanges = Ranges->getNumOperands() / 2;
assert(NumRanges >= 1); assert(NumRanges >= 1);
for (unsigned i = 0; i < NumRanges; ++i) { for (unsigned i = 0; i < NumRanges; ++i) {
ConstantInt *Lower = cast<ConstantInt>(Ranges->getOperand(2*i + 0)); ConstantInt *Lower =
ConstantInt *Upper = cast<ConstantInt>(Ranges->getOperand(2*i + 1)); mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
ConstantInt *Upper =
mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
ConstantRange Range(Lower->getValue(), Upper->getValue()); ConstantRange Range(Lower->getValue(), Upper->getValue());
if (Range.contains(Value)) if (Range.contains(Value))
return false; return false;

View File

@ -62,8 +62,6 @@ bool LLParser::ValidateEndOfModule() {
NumberedMetadata[SlotNo] == nullptr) NumberedMetadata[SlotNo] == nullptr)
return Error(MDList[i].Loc, "use of undefined metadata '!" + return Error(MDList[i].Loc, "use of undefined metadata '!" +
Twine(SlotNo) + "'"); Twine(SlotNo) + "'");
assert(!NumberedMetadata[SlotNo]->isFunctionLocal() &&
"Unexpected function-local metadata");
Inst->setMetadata(MDList[i].MDKind, NumberedMetadata[SlotNo]); Inst->setMetadata(MDList[i].MDKind, NumberedMetadata[SlotNo]);
} }
} }
@ -169,6 +167,10 @@ bool LLParser::ValidateEndOfModule() {
"use of undefined metadata '!" + "use of undefined metadata '!" +
Twine(ForwardRefMDNodes.begin()->first) + "'"); Twine(ForwardRefMDNodes.begin()->first) + "'");
// Resolve metadata cycles.
for (auto &N : NumberedMetadata)
if (auto *G = cast_or_null<GenericMDNode>(N))
G->resolveCycles();
// Look for intrinsic functions and CallInst that need to be upgraded // Look for intrinsic functions and CallInst that need to be upgraded
for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ) for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
@ -561,12 +563,12 @@ bool LLParser::ParseMDNodeID(MDNode *&Result) {
if (Result) return false; if (Result) return false;
// Otherwise, create MDNode forward reference. // Otherwise, create MDNode forward reference.
MDNode *FwdNode = MDNode::getTemporary(Context, None); MDNodeFwdDecl *FwdNode = MDNode::getTemporary(Context, None);
ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc()); ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
if (NumberedMetadata.size() <= MID) if (NumberedMetadata.size() <= MID)
NumberedMetadata.resize(MID+1); NumberedMetadata.resize(MID+1);
NumberedMetadata[MID] = FwdNode; NumberedMetadata[MID].reset(FwdNode);
Result = FwdNode; Result = FwdNode;
return false; return false;
} }
@ -609,23 +611,18 @@ bool LLParser::ParseStandaloneMetadata() {
LocTy TyLoc; LocTy TyLoc;
Type *Ty = nullptr; Type *Ty = nullptr;
SmallVector<Value *, 16> Elts; MDNode *Init;
if (ParseUInt32(MetadataID) || if (ParseUInt32(MetadataID) ||
ParseToken(lltok::equal, "expected '=' here") || ParseToken(lltok::equal, "expected '=' here") ||
ParseType(Ty, TyLoc) || ParseType(Ty, TyLoc) ||
ParseToken(lltok::exclaim, "Expected '!' here") || ParseToken(lltok::exclaim, "Expected '!' here") ||
ParseToken(lltok::lbrace, "Expected '{' here") || ParseMDNode(Init))
ParseMDNodeVector(Elts, nullptr) ||
ParseToken(lltok::rbrace, "expected end of metadata node"))
return true; return true;
MDNode *Init = MDNode::get(Context, Elts);
// See if this was forward referenced, if so, handle it. // See if this was forward referenced, if so, handle it.
std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator auto FI = ForwardRefMDNodes.find(MetadataID);
FI = ForwardRefMDNodes.find(MetadataID);
if (FI != ForwardRefMDNodes.end()) { if (FI != ForwardRefMDNodes.end()) {
MDNode *Temp = FI->second.first; auto *Temp = FI->second.first;
Temp->replaceAllUsesWith(Init); Temp->replaceAllUsesWith(Init);
MDNode::deleteTemporary(Temp); MDNode::deleteTemporary(Temp);
ForwardRefMDNodes.erase(FI); ForwardRefMDNodes.erase(FI);
@ -637,7 +634,7 @@ bool LLParser::ParseStandaloneMetadata() {
if (NumberedMetadata[MetadataID] != nullptr) if (NumberedMetadata[MetadataID] != nullptr)
return TokError("Metadata id is already used"); return TokError("Metadata id is already used");
NumberedMetadata[MetadataID] = Init; NumberedMetadata[MetadataID].reset(Init);
} }
return false; return false;
@ -1527,18 +1524,15 @@ bool LLParser::ParseInstructionMetadata(Instruction *Inst,
if (ParseToken(lltok::exclaim, "expected '!' here")) if (ParseToken(lltok::exclaim, "expected '!' here"))
return true; return true;
// This code is similar to that of ParseMetadataValue, however it needs to // This code is similar to that of ParseMetadata, however it needs to
// have special-case code for a forward reference; see the comments on // have special-case code for a forward reference; see the comments on
// ForwardRefInstMetadata for details. Also, MDStrings are not supported // ForwardRefInstMetadata for details. Also, MDStrings are not supported
// at the top level here. // at the top level here.
if (Lex.getKind() == lltok::lbrace) { if (Lex.getKind() == lltok::lbrace) {
ValID ID; MDNode *N;
if (ParseMetadataListValue(ID, PFS)) if (ParseMDNode(N))
return true; return true;
assert(ID.Kind == ValID::t_MDNode); Inst->setMetadata(MDK, N);
if (ID.MDNodeVal->isFunctionLocal())
return Error(Loc, "unexpected function-local metadata");
Inst->setMetadata(MDK, ID.MDNodeVal);
} else { } else {
unsigned NodeID = 0; unsigned NodeID = 0;
if (ParseMDNodeID(Node, NodeID)) if (ParseMDNodeID(Node, NodeID))
@ -2395,7 +2389,7 @@ bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
ID.Kind = ValID::t_LocalName; ID.Kind = ValID::t_LocalName;
break; break;
case lltok::exclaim: // !42, !{...}, or !"foo" case lltok::exclaim: // !42, !{...}, or !"foo"
return ParseMetadataValue(ID, PFS); return ParseMetadataAsValue(ID, PFS);
case lltok::APSInt: case lltok::APSInt:
ID.APSIntVal = Lex.getAPSIntVal(); ID.APSIntVal = Lex.getAPSIntVal();
ID.Kind = ValID::t_APSInt; ID.Kind = ValID::t_APSInt;
@ -2935,45 +2929,69 @@ bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts) {
return false; return false;
} }
bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) { bool LLParser::ParseMDNode(MDNode *&MD) {
assert(Lex.getKind() == lltok::lbrace); SmallVector<Metadata *, 16> Elts;
Lex.Lex(); if (ParseMDNodeVector(Elts, nullptr))
SmallVector<Value*, 16> Elts;
if (ParseMDNodeVector(Elts, PFS) ||
ParseToken(lltok::rbrace, "expected end of metadata node"))
return true; return true;
ID.MDNodeVal = MDNode::get(Context, Elts); MD = MDNode::get(Context, Elts);
ID.Kind = ValID::t_MDNode;
return false; return false;
} }
/// ParseMetadataValue bool LLParser::ParseMDNodeOrLocal(Metadata *&MD, PerFunctionState *PFS) {
SmallVector<Metadata *, 16> Elts;
if (ParseMDNodeVector(Elts, PFS))
return true;
// Check for function-local metadata masquerading as an MDNode.
if (PFS && Elts.size() == 1 && Elts[0] && isa<LocalAsMetadata>(Elts[0])) {
MD = Elts[0];
return false;
}
MD = MDNode::get(Context, Elts);
return false;
}
bool LLParser::ParseMetadataAsValue(ValID &ID, PerFunctionState *PFS) {
Metadata *MD;
if (ParseMetadata(MD, PFS))
return true;
ID.Kind = ValID::t_Metadata;
ID.MetadataVal = MetadataAsValue::get(Context, MD);
return false;
}
/// ParseMetadata
/// ::= !42 /// ::= !42
/// ::= !{...} /// ::= !{...}
/// ::= !"string" /// ::= !"string"
bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) { bool LLParser::ParseMetadata(Metadata *&MD, PerFunctionState *PFS) {
assert(Lex.getKind() == lltok::exclaim); assert(Lex.getKind() == lltok::exclaim);
Lex.Lex(); Lex.Lex();
// MDNode: // MDNode:
// !{ ... } // !{ ... }
if (Lex.getKind() == lltok::lbrace) if (Lex.getKind() == lltok::lbrace)
return ParseMetadataListValue(ID, PFS); return ParseMDNodeOrLocal(MD, PFS);
// Standalone metadata reference // Standalone metadata reference
// !42 // !42
if (Lex.getKind() == lltok::APSInt) { if (Lex.getKind() == lltok::APSInt) {
if (ParseMDNodeID(ID.MDNodeVal)) return true; MDNode *N;
ID.Kind = ValID::t_MDNode; if (ParseMDNodeID(N))
return true;
MD = N;
return false; return false;
} }
// MDString: // MDString:
// ::= '!' STRINGCONSTANT // ::= '!' STRINGCONSTANT
if (ParseMDString(ID.MDStringVal)) return true; MDString *S;
ID.Kind = ValID::t_MDString; if (ParseMDString(S))
return true;
MD = S;
return false; return false;
} }
@ -3006,15 +3024,10 @@ bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
(ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2))); (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2)));
return false; return false;
} }
case ValID::t_MDNode: case ValID::t_Metadata:
if (!Ty->isMetadataTy()) if (!Ty->isMetadataTy())
return Error(ID.Loc, "metadata value must have metadata type"); return Error(ID.Loc, "metadata value must have metadata type");
V = ID.MDNodeVal; V = ID.MetadataVal;
return false;
case ValID::t_MDString:
if (!Ty->isMetadataTy())
return Error(ID.Loc, "metadata value must have metadata type");
V = ID.MDStringVal;
return false; return false;
case ValID::t_GlobalName: case ValID::t_GlobalName:
V = GetGlobalVal(ID.StrVal, Ty, ID.Loc); V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
@ -4668,13 +4681,16 @@ int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// ParseMDNodeVector /// ParseMDNodeVector
/// ::= Element (',' Element)* /// ::= { Element (',' Element)* }
/// Element /// Element
/// ::= 'null' | TypeAndValue /// ::= 'null' | TypeAndValue
bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts, bool LLParser::ParseMDNodeVector(SmallVectorImpl<Metadata *> &Elts,
PerFunctionState *PFS) { PerFunctionState *PFS) {
assert(Lex.getKind() == lltok::lbrace);
Lex.Lex();
// Check for an empty list. // Check for an empty list.
if (Lex.getKind() == lltok::rbrace) if (EatIfPresent(lltok::rbrace))
return false; return false;
bool IsLocal = false; bool IsLocal = false;
@ -4688,13 +4704,26 @@ bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
continue; continue;
} }
Value *V = nullptr; Type *Ty = nullptr;
if (ParseTypeAndValue(V, PFS)) return true; if (ParseType(Ty))
Elts.push_back(V); return true;
if (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal()) if (Ty->isMetadataTy()) {
return TokError("unexpected nested function-local metadata"); // No function-local metadata here.
if (!V->getType()->isMetadataTy() && !isa<Constant>(V)) { Metadata *MD = nullptr;
if (ParseMetadata(MD, nullptr))
return true;
Elts.push_back(MD);
continue;
}
Value *V = nullptr;
if (ParseValue(Ty, V, PFS))
return true;
assert(V && "Expected valid value");
Elts.push_back(ValueAsMetadata::get(V));
if (isa<LocalAsMetadata>(Elts.back())) {
assert(PFS && "Unexpected function-local metadata without PFS"); assert(PFS && "Unexpected function-local metadata without PFS");
if (Elts.size() > 1) if (Elts.size() > 1)
return TokError("unexpected function-local metadata"); return TokError("unexpected function-local metadata");
@ -4702,7 +4731,7 @@ bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
} }
} while (EatIfPresent(lltok::comma)); } while (EatIfPresent(lltok::comma));
return false; return ParseToken(lltok::rbrace, "expected end of metadata node");
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//

View File

@ -52,8 +52,7 @@ namespace llvm {
t_EmptyArray, // No value: [] t_EmptyArray, // No value: []
t_Constant, // Value in ConstantVal. t_Constant, // Value in ConstantVal.
t_InlineAsm, // Value in StrVal/StrVal2/UIntVal. t_InlineAsm, // Value in StrVal/StrVal2/UIntVal.
t_MDNode, // Value in MDNodeVal. t_Metadata, // Value in MetadataVal.
t_MDString, // Value in MDStringVal.
t_ConstantStruct, // Value in ConstantStructElts. t_ConstantStruct, // Value in ConstantStructElts.
t_PackedConstantStruct // Value in ConstantStructElts. t_PackedConstantStruct // Value in ConstantStructElts.
} Kind; } Kind;
@ -64,8 +63,7 @@ namespace llvm {
APSInt APSIntVal; APSInt APSIntVal;
APFloat APFloatVal; APFloat APFloatVal;
Constant *ConstantVal; Constant *ConstantVal;
MDNode *MDNodeVal; MetadataAsValue *MetadataVal;
MDString *MDStringVal;
Constant **ConstantStructElts; Constant **ConstantStructElts;
ValID() : Kind(t_LocalID), APFloatVal(0.0) {} ValID() : Kind(t_LocalID), APFloatVal(0.0) {}
@ -115,8 +113,8 @@ namespace llvm {
StringMap<std::pair<Type*, LocTy> > NamedTypes; StringMap<std::pair<Type*, LocTy> > NamedTypes;
std::vector<std::pair<Type*, LocTy> > NumberedTypes; std::vector<std::pair<Type*, LocTy> > NumberedTypes;
std::vector<TrackingVH<MDNode> > NumberedMetadata; std::vector<TrackingMDNodeRef> NumberedMetadata;
std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes; std::map<unsigned, std::pair<MDNodeFwdDecl *, LocTy>> ForwardRefMDNodes;
// Global Value reference information. // Global Value reference information.
std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals; std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
@ -382,9 +380,12 @@ namespace llvm {
bool ParseGlobalTypeAndValue(Constant *&V); bool ParseGlobalTypeAndValue(Constant *&V);
bool ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts); bool ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts);
bool parseOptionalComdat(Comdat *&C); bool parseOptionalComdat(Comdat *&C);
bool ParseMetadataListValue(ValID &ID, PerFunctionState *PFS); bool ParseMetadataAsValue(ValID &ID, PerFunctionState *PFS);
bool ParseMetadataValue(ValID &ID, PerFunctionState *PFS); bool ParseMetadata(Metadata *&MD, PerFunctionState *PFS);
bool ParseMDNodeVector(SmallVectorImpl<Value*> &, PerFunctionState *PFS); bool ParseMDNode(MDNode *&MD);
bool ParseMDNodeOrLocal(Metadata *&MD, PerFunctionState *PFS);
bool ParseMDNodeVector(SmallVectorImpl<Metadata *> &,
PerFunctionState *PFS);
bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS); bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS);
// Function Parsing. // Function Parsing.

View File

@ -438,43 +438,58 @@ void BitcodeReaderValueList::ResolveConstantForwardRefs() {
} }
} }
void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) { void BitcodeReaderMDValueList::AssignValue(Metadata *MD, unsigned Idx) {
if (Idx == size()) { if (Idx == size()) {
push_back(V); push_back(MD);
return; return;
} }
if (Idx >= size()) if (Idx >= size())
resize(Idx+1); resize(Idx+1);
WeakVH &OldV = MDValuePtrs[Idx]; TrackingMDRef &OldMD = MDValuePtrs[Idx];
if (!OldV) { if (!OldMD) {
OldV = V; OldMD.reset(MD);
return; return;
} }
// If there was a forward reference to this value, replace it. // If there was a forward reference to this value, replace it.
MDNode *PrevVal = cast<MDNode>(OldV); MDNodeFwdDecl *PrevMD = cast<MDNodeFwdDecl>(OldMD.get());
OldV->replaceAllUsesWith(V); PrevMD->replaceAllUsesWith(MD);
MDNode::deleteTemporary(PrevVal); MDNode::deleteTemporary(PrevMD);
// Deleting PrevVal sets Idx value in MDValuePtrs to null. Set new --NumFwdRefs;
// value for Idx.
MDValuePtrs[Idx] = V;
} }
Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) { Metadata *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
if (Idx >= size()) if (Idx >= size())
resize(Idx + 1); resize(Idx + 1);
if (Value *V = MDValuePtrs[Idx]) { if (Metadata *MD = MDValuePtrs[Idx])
assert(V->getType()->isMetadataTy() && "Type mismatch in value table!"); return MD;
return V;
}
// Create and return a placeholder, which will later be RAUW'd. // Create and return a placeholder, which will later be RAUW'd.
Value *V = MDNode::getTemporary(Context, None); AnyFwdRefs = true;
MDValuePtrs[Idx] = V; ++NumFwdRefs;
return V; Metadata *MD = MDNode::getTemporary(Context, None);
MDValuePtrs[Idx].reset(MD);
return MD;
}
void BitcodeReaderMDValueList::tryToResolveCycles() {
if (!AnyFwdRefs)
// Nothing to do.
return;
if (NumFwdRefs)
// Still forward references... can't resolve cycles.
return;
// Resolve any cycles.
for (auto &MD : MDValuePtrs) {
assert(!(MD && isa<MDNodeFwdDecl>(MD)) && "Unexpected forward reference");
if (auto *G = dyn_cast_or_null<GenericMDNode>(MD))
G->resolveCycles();
}
} }
Type *BitcodeReader::getTypeByID(unsigned ID) { Type *BitcodeReader::getTypeByID(unsigned ID) {
@ -1066,6 +1081,7 @@ std::error_code BitcodeReader::ParseMetadata() {
case BitstreamEntry::Error: case BitstreamEntry::Error:
return Error(BitcodeError::MalformedBlock); return Error(BitcodeError::MalformedBlock);
case BitstreamEntry::EndBlock: case BitstreamEntry::EndBlock:
MDValueList.tryToResolveCycles();
return std::error_code(); return std::error_code();
case BitstreamEntry::Record: case BitstreamEntry::Record:
// The interesting case. // The interesting case.
@ -1100,13 +1116,13 @@ std::error_code BitcodeReader::ParseMetadata() {
break; break;
} }
case bitc::METADATA_FN_NODE: { case bitc::METADATA_FN_NODE: {
// This is a function-local node. // This is a LocalAsMetadata record, the only type of function-local
// metadata.
if (Record.size() % 2 == 1) if (Record.size() % 2 == 1)
return Error(BitcodeError::InvalidRecord); return Error(BitcodeError::InvalidRecord);
// If this isn't a single-operand node that directly references // If this isn't a LocalAsMetadata record, we're dropping it. This used
// non-metadata, we're dropping it. This used to be legal, but there's // to be legal, but there's no upgrade path.
// no upgrade path.
auto dropRecord = [&] { auto dropRecord = [&] {
MDValueList.AssignValue(MDNode::get(Context, None), NextMDValueNo++); MDValueList.AssignValue(MDNode::get(Context, None), NextMDValueNo++);
}; };
@ -1121,10 +1137,9 @@ std::error_code BitcodeReader::ParseMetadata() {
break; break;
} }
Value *Elts[] = {ValueList.getValueFwdRef(Record[1], Ty)}; MDValueList.AssignValue(
Value *V = MDNode::getWhenValsUnresolved(Context, Elts, LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
/*IsFunctionLocal*/ true); NextMDValueNo++);
MDValueList.AssignValue(V, NextMDValueNo++);
break; break;
} }
case bitc::METADATA_NODE: { case bitc::METADATA_NODE: {
@ -1132,28 +1147,30 @@ std::error_code BitcodeReader::ParseMetadata() {
return Error(BitcodeError::InvalidRecord); return Error(BitcodeError::InvalidRecord);
unsigned Size = Record.size(); unsigned Size = Record.size();
SmallVector<Value*, 8> Elts; SmallVector<Metadata *, 8> Elts;
for (unsigned i = 0; i != Size; i += 2) { for (unsigned i = 0; i != Size; i += 2) {
Type *Ty = getTypeByID(Record[i]); Type *Ty = getTypeByID(Record[i]);
if (!Ty) if (!Ty)
return Error(BitcodeError::InvalidRecord); return Error(BitcodeError::InvalidRecord);
if (Ty->isMetadataTy()) if (Ty->isMetadataTy())
Elts.push_back(MDValueList.getValueFwdRef(Record[i+1])); Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
else if (!Ty->isVoidTy()) else if (!Ty->isVoidTy()) {
Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty)); auto *MD =
else ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty));
assert(isa<ConstantAsMetadata>(MD) &&
"Expected non-function-local metadata");
Elts.push_back(MD);
} else
Elts.push_back(nullptr); Elts.push_back(nullptr);
} }
Value *V = MDNode::getWhenValsUnresolved(Context, Elts, MDValueList.AssignValue(MDNode::get(Context, Elts), NextMDValueNo++);
/*IsFunctionLocal*/ false);
MDValueList.AssignValue(V, NextMDValueNo++);
break; break;
} }
case bitc::METADATA_STRING: { case bitc::METADATA_STRING: {
std::string String(Record.begin(), Record.end()); std::string String(Record.begin(), Record.end());
llvm::UpgradeMDStringConstant(String); llvm::UpgradeMDStringConstant(String);
Value *V = MDString::get(Context, String); Metadata *MD = MDString::get(Context, String);
MDValueList.AssignValue(V, NextMDValueNo++); MDValueList.AssignValue(MD, NextMDValueNo++);
break; break;
} }
case bitc::METADATA_KIND: { case bitc::METADATA_KIND: {
@ -2359,12 +2376,12 @@ std::error_code BitcodeReader::ParseMetadataAttachment() {
MDKindMap.find(Kind); MDKindMap.find(Kind);
if (I == MDKindMap.end()) if (I == MDKindMap.end())
return Error(BitcodeError::InvalidID); return Error(BitcodeError::InvalidID);
MDNode *Node = cast<MDNode>(MDValueList.getValueFwdRef(Record[i+1])); Metadata *Node = MDValueList.getValueFwdRef(Record[i + 1]);
if (Node->isFunctionLocal()) if (isa<LocalAsMetadata>(Node))
// Drop the attachment. This used to be legal, but there's no // Drop the attachment. This used to be legal, but there's no
// upgrade path. // upgrade path.
break; break;
Inst->setMetadata(I->second, Node); Inst->setMetadata(I->second, cast<MDNode>(Node));
if (I->second == LLVMContext::MD_tbaa) if (I->second == LLVMContext::MD_tbaa)
InstsWithTBAATag.push_back(Inst); InstsWithTBAATag.push_back(Inst);
} }

View File

@ -19,7 +19,9 @@
#include "llvm/Bitcode/LLVMBitCodes.h" #include "llvm/Bitcode/LLVMBitCodes.h"
#include "llvm/IR/Attributes.h" #include "llvm/IR/Attributes.h"
#include "llvm/IR/GVMaterializer.h" #include "llvm/IR/GVMaterializer.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/OperandTraits.h" #include "llvm/IR/OperandTraits.h"
#include "llvm/IR/TrackingMDRef.h"
#include "llvm/IR/Type.h" #include "llvm/IR/Type.h"
#include "llvm/IR/ValueHandle.h" #include "llvm/IR/ValueHandle.h"
#include <deque> #include <deque>
@ -95,22 +97,25 @@ public:
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
class BitcodeReaderMDValueList { class BitcodeReaderMDValueList {
std::vector<WeakVH> MDValuePtrs; unsigned NumFwdRefs;
bool AnyFwdRefs;
std::vector<TrackingMDRef> MDValuePtrs;
LLVMContext &Context; LLVMContext &Context;
public: public:
BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {} BitcodeReaderMDValueList(LLVMContext &C)
: NumFwdRefs(0), AnyFwdRefs(false), Context(C) {}
// vector compatibility methods // vector compatibility methods
unsigned size() const { return MDValuePtrs.size(); } unsigned size() const { return MDValuePtrs.size(); }
void resize(unsigned N) { MDValuePtrs.resize(N); } void resize(unsigned N) { MDValuePtrs.resize(N); }
void push_back(Value *V) { MDValuePtrs.push_back(V); } void push_back(Metadata *MD) { MDValuePtrs.emplace_back(MD); }
void clear() { MDValuePtrs.clear(); } void clear() { MDValuePtrs.clear(); }
Value *back() const { return MDValuePtrs.back(); } Metadata *back() const { return MDValuePtrs.back(); }
void pop_back() { MDValuePtrs.pop_back(); } void pop_back() { MDValuePtrs.pop_back(); }
bool empty() const { return MDValuePtrs.empty(); } bool empty() const { return MDValuePtrs.empty(); }
Value *operator[](unsigned i) const { Metadata *operator[](unsigned i) const {
assert(i < MDValuePtrs.size()); assert(i < MDValuePtrs.size());
return MDValuePtrs[i]; return MDValuePtrs[i];
} }
@ -120,8 +125,9 @@ public:
MDValuePtrs.resize(N); MDValuePtrs.resize(N);
} }
Value *getValueFwdRef(unsigned Idx); Metadata *getValueFwdRef(unsigned Idx);
void AssignValue(Value *V, unsigned Idx); void AssignValue(Metadata *MD, unsigned Idx);
void tryToResolveCycles();
}; };
class BitcodeReader : public GVMaterializer { class BitcodeReader : public GVMaterializer {
@ -248,9 +254,12 @@ private:
Type *getTypeByID(unsigned ID); Type *getTypeByID(unsigned ID);
Value *getFnValueByID(unsigned ID, Type *Ty) { Value *getFnValueByID(unsigned ID, Type *Ty) {
if (Ty && Ty->isMetadataTy()) if (Ty && Ty->isMetadataTy())
return MDValueList.getValueFwdRef(ID); return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID));
return ValueList.getValueFwdRef(ID, Ty); return ValueList.getValueFwdRef(ID, Ty);
} }
Metadata *getFnMetadataByID(unsigned ID) {
return MDValueList.getValueFwdRef(ID);
}
BasicBlock *getBasicBlock(unsigned ID) const { BasicBlock *getBasicBlock(unsigned ID) const {
if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
return FunctionBBs[ID]; return FunctionBBs[ID];

View File

@ -737,44 +737,79 @@ static uint64_t GetOptimizationFlags(const Value *V) {
return Flags; return Flags;
} }
static void WriteValueAsMetadataImpl(const ValueAsMetadata *MD,
const ValueEnumerator &VE,
BitstreamWriter &Stream,
SmallVectorImpl<uint64_t> &Record,
unsigned Code) {
// Mimic an MDNode with a value as one operand.
Value *V = MD->getValue();
Record.push_back(VE.getTypeID(V->getType()));
Record.push_back(VE.getValueID(V));
Stream.EmitRecord(Code, Record, 0);
Record.clear();
}
static void WriteLocalAsMetadata(const LocalAsMetadata *MD,
const ValueEnumerator &VE,
BitstreamWriter &Stream,
SmallVectorImpl<uint64_t> &Record) {
WriteValueAsMetadataImpl(MD, VE, Stream, Record, bitc::METADATA_FN_NODE);
}
static void WriteConstantAsMetadata(const ConstantAsMetadata *MD,
const ValueEnumerator &VE,
BitstreamWriter &Stream,
SmallVectorImpl<uint64_t> &Record) {
WriteValueAsMetadataImpl(MD, VE, Stream, Record, bitc::METADATA_NODE);
}
static void WriteMDNode(const MDNode *N, static void WriteMDNode(const MDNode *N,
const ValueEnumerator &VE, const ValueEnumerator &VE,
BitstreamWriter &Stream, BitstreamWriter &Stream,
SmallVectorImpl<uint64_t> &Record) { SmallVectorImpl<uint64_t> &Record) {
for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
if (N->getOperand(i)) { Metadata *MD = N->getOperand(i);
Record.push_back(VE.getTypeID(N->getOperand(i)->getType())); if (!MD) {
Record.push_back(VE.getValueID(N->getOperand(i)));
} else {
Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext()))); Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext())));
Record.push_back(0); Record.push_back(0);
continue;
} }
if (auto *V = dyn_cast<ConstantAsMetadata>(MD)) {
Record.push_back(VE.getTypeID(V->getValue()->getType()));
Record.push_back(VE.getValueID(V->getValue()));
continue;
}
assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata");
Record.push_back(VE.getTypeID(Type::getMetadataTy(N->getContext())));
Record.push_back(VE.getMetadataID(MD));
} }
unsigned MDCode = N->isFunctionLocal() ? bitc::METADATA_FN_NODE : Stream.EmitRecord(bitc::METADATA_NODE, Record, 0);
bitc::METADATA_NODE;
Stream.EmitRecord(MDCode, Record, 0);
Record.clear(); Record.clear();
} }
static void WriteModuleMetadata(const Module *M, static void WriteModuleMetadata(const Module *M,
const ValueEnumerator &VE, const ValueEnumerator &VE,
BitstreamWriter &Stream) { BitstreamWriter &Stream) {
const auto &Vals = VE.getMDValues(); const auto &MDs = VE.getMDs();
bool StartedMetadataBlock = false; bool StartedMetadataBlock = false;
unsigned MDSAbbrev = 0; unsigned MDSAbbrev = 0;
SmallVector<uint64_t, 64> Record; SmallVector<uint64_t, 64> Record;
for (unsigned i = 0, e = Vals.size(); i != e; ++i) { for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
if (const MDNode *N = dyn_cast<MDNode>(MDs[i])) {
if (const MDNode *N = dyn_cast<MDNode>(Vals[i])) { if (!StartedMetadataBlock) {
if (!N->isFunctionLocal() || !N->getFunction()) { Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
if (!StartedMetadataBlock) { StartedMetadataBlock = true;
Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
StartedMetadataBlock = true;
}
WriteMDNode(N, VE, Stream, Record);
} }
} else if (const MDString *MDS = dyn_cast<MDString>(Vals[i])) { WriteMDNode(N, VE, Stream, Record);
if (!StartedMetadataBlock) { } else if (const auto *MDC = dyn_cast<ConstantAsMetadata>(MDs[i])) {
if (!StartedMetadataBlock) {
Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
StartedMetadataBlock = true;
}
WriteConstantAsMetadata(MDC, VE, Stream, Record);
} else if (const MDString *MDS = dyn_cast<MDString>(MDs[i])) {
if (!StartedMetadataBlock) {
Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
// Abbrev for METADATA_STRING. // Abbrev for METADATA_STRING.
@ -813,7 +848,7 @@ static void WriteModuleMetadata(const Module *M,
// Write named metadata operands. // Write named metadata operands.
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
Record.push_back(VE.getValueID(NMD->getOperand(i))); Record.push_back(VE.getMetadataID(NMD->getOperand(i)));
Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0); Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
Record.clear(); Record.clear();
} }
@ -827,16 +862,16 @@ static void WriteFunctionLocalMetadata(const Function &F,
BitstreamWriter &Stream) { BitstreamWriter &Stream) {
bool StartedMetadataBlock = false; bool StartedMetadataBlock = false;
SmallVector<uint64_t, 64> Record; SmallVector<uint64_t, 64> Record;
const SmallVectorImpl<const MDNode *> &Vals = VE.getFunctionLocalMDValues(); const SmallVectorImpl<const LocalAsMetadata *> &MDs =
for (unsigned i = 0, e = Vals.size(); i != e; ++i) VE.getFunctionLocalMDs();
if (const MDNode *N = Vals[i]) for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
if (N->isFunctionLocal() && N->getFunction() == &F) { assert(MDs[i] && "Expected valid function-local metadata");
if (!StartedMetadataBlock) { if (!StartedMetadataBlock) {
Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
StartedMetadataBlock = true; StartedMetadataBlock = true;
} }
WriteMDNode(N, VE, Stream, Record); WriteLocalAsMetadata(MDs[i], VE, Stream, Record);
} }
if (StartedMetadataBlock) if (StartedMetadataBlock)
Stream.ExitBlock(); Stream.ExitBlock();
@ -866,7 +901,7 @@ static void WriteMetadataAttachment(const Function &F,
for (unsigned i = 0, e = MDs.size(); i != e; ++i) { for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
Record.push_back(MDs[i].first); Record.push_back(MDs[i].first);
Record.push_back(VE.getValueID(MDs[i].second)); Record.push_back(VE.getMetadataID(MDs[i].second));
} }
Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0); Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
Record.clear(); Record.clear();
@ -1686,11 +1721,12 @@ static void WriteFunction(const Function &F, ValueEnumerator &VE,
} else { } else {
MDNode *Scope, *IA; MDNode *Scope, *IA;
DL.getScopeAndInlinedAt(Scope, IA, I->getContext()); DL.getScopeAndInlinedAt(Scope, IA, I->getContext());
assert(Scope && "Expected valid scope");
Vals.push_back(DL.getLine()); Vals.push_back(DL.getLine());
Vals.push_back(DL.getCol()); Vals.push_back(DL.getCol());
Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0); Vals.push_back(Scope ? VE.getMetadataID(Scope) + 1 : 0);
Vals.push_back(IA ? VE.getValueID(IA)+1 : 0); Vals.push_back(IA ? VE.getMetadataID(IA) + 1 : 0);
Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals); Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
Vals.clear(); Vals.clear();

View File

@ -326,6 +326,12 @@ ValueEnumerator::ValueEnumerator(const Module &M) {
if (I->hasPrologueData()) if (I->hasPrologueData())
EnumerateValue(I->getPrologueData()); EnumerateValue(I->getPrologueData());
// Enumerate the metadata type.
//
// TODO: Move this to ValueEnumerator::EnumerateOperandType() once bitcode
// only encodes the metadata type when it's used as a value.
EnumerateType(Type::getMetadataTy(M.getContext()));
// Insert constants and metadata that are named at module level into the slot // Insert constants and metadata that are named at module level into the slot
// pool so that the module symbol table can refer to them... // pool so that the module symbol table can refer to them...
EnumerateValueSymbolTable(M.getValueSymbolTable()); EnumerateValueSymbolTable(M.getValueSymbolTable());
@ -341,11 +347,17 @@ ValueEnumerator::ValueEnumerator(const Module &M) {
for (const BasicBlock &BB : F) for (const BasicBlock &BB : F)
for (const Instruction &I : BB) { for (const Instruction &I : BB) {
for (const Use &Op : I.operands()) { for (const Use &Op : I.operands()) {
if (MDNode *MD = dyn_cast<MDNode>(&Op)) auto *MD = dyn_cast<MetadataAsValue>(&Op);
if (MD->isFunctionLocal() && MD->getFunction()) if (!MD) {
// These will get enumerated during function-incorporation. EnumerateOperandType(Op);
continue; continue;
EnumerateOperandType(Op); }
// Local metadata is enumerated during function-incorporation.
if (isa<LocalAsMetadata>(MD->getMetadata()))
continue;
EnumerateMetadata(MD->getMetadata());
} }
EnumerateType(I.getType()); EnumerateType(I.getType());
if (const CallInst *CI = dyn_cast<CallInst>(&I)) if (const CallInst *CI = dyn_cast<CallInst>(&I))
@ -389,17 +401,20 @@ void ValueEnumerator::setInstructionID(const Instruction *I) {
} }
unsigned ValueEnumerator::getValueID(const Value *V) const { unsigned ValueEnumerator::getValueID(const Value *V) const {
if (isa<MDNode>(V) || isa<MDString>(V)) { if (auto *MD = dyn_cast<MetadataAsValue>(V))
ValueMapType::const_iterator I = MDValueMap.find(V); return getMetadataID(MD->getMetadata());
assert(I != MDValueMap.end() && "Value not in slotcalculator!");
return I->second-1;
}
ValueMapType::const_iterator I = ValueMap.find(V); ValueMapType::const_iterator I = ValueMap.find(V);
assert(I != ValueMap.end() && "Value not in slotcalculator!"); assert(I != ValueMap.end() && "Value not in slotcalculator!");
return I->second-1; return I->second-1;
} }
unsigned ValueEnumerator::getMetadataID(const Metadata *MD) const {
auto I = MDValueMap.find(MD);
assert(I != MDValueMap.end() && "Metadata not in slotcalculator!");
return I->second - 1;
}
void ValueEnumerator::dump() const { void ValueEnumerator::dump() const {
print(dbgs(), ValueMap, "Default"); print(dbgs(), ValueMap, "Default");
dbgs() << '\n'; dbgs() << '\n';
@ -436,6 +451,18 @@ void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,
} }
} }
void ValueEnumerator::print(raw_ostream &OS, const MetadataMapType &Map,
const char *Name) const {
OS << "Map Name: " << Name << "\n";
OS << "Size: " << Map.size() << "\n";
for (auto I = Map.begin(), E = Map.end(); I != E; ++I) {
const Metadata *MD = I->first;
OS << "Metadata: slot = " << I->second << "\n";
MD->dump();
}
}
/// OptimizeConstants - Reorder constant pool for denser encoding. /// OptimizeConstants - Reorder constant pool for denser encoding.
void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) { void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
if (CstStart == CstEnd || CstStart+1 == CstEnd) return; if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
@ -493,25 +520,24 @@ void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
/// and types referenced by the given MDNode. /// and types referenced by the given MDNode.
void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) { void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
if (Value *V = N->getOperand(i)) { Metadata *MD = N->getOperand(i);
if (isa<MDNode>(V) || isa<MDString>(V)) if (!MD) {
EnumerateMetadata(V);
else if (!isa<Instruction>(V) && !isa<Argument>(V))
EnumerateValue(V);
} else
EnumerateType(Type::getVoidTy(N->getContext())); EnumerateType(Type::getVoidTy(N->getContext()));
continue;
}
assert(!isa<LocalAsMetadata>(MD) && "MDNodes cannot be function-local");
if (auto *C = dyn_cast<ConstantAsMetadata>(MD)) {
EnumerateValue(C->getValue());
continue;
}
EnumerateMetadata(MD);
} }
} }
void ValueEnumerator::EnumerateMetadata(const Value *MD) { void ValueEnumerator::EnumerateMetadata(const Metadata *MD) {
assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind"); assert(
(isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) &&
// Skip function-local nodes themselves, but walk their operands. "Invalid metadata kind");
const MDNode *N = dyn_cast<MDNode>(MD);
if (N && N->isFunctionLocal() && N->getFunction()) {
EnumerateMDNodeOperands(N);
return;
}
// Insert a dummy ID to block the co-recursive call to // Insert a dummy ID to block the co-recursive call to
// EnumerateMDNodeOperands() from re-visiting MD in a cyclic graph. // EnumerateMDNodeOperands() from re-visiting MD in a cyclic graph.
@ -520,55 +546,39 @@ void ValueEnumerator::EnumerateMetadata(const Value *MD) {
if (!MDValueMap.insert(std::make_pair(MD, 0)).second) if (!MDValueMap.insert(std::make_pair(MD, 0)).second)
return; return;
// Enumerate the type of this value.
EnumerateType(MD->getType());
// Visit operands first to minimize RAUW. // Visit operands first to minimize RAUW.
if (N) if (auto *N = dyn_cast<MDNode>(MD))
EnumerateMDNodeOperands(N); EnumerateMDNodeOperands(N);
else if (auto *C = dyn_cast<ConstantAsMetadata>(MD))
EnumerateValue(C->getValue());
// Replace the dummy ID inserted above with the correct one. MDValueMap may // Replace the dummy ID inserted above with the correct one. MDValueMap may
// have changed by inserting operands, so we need a fresh lookup here. // have changed by inserting operands, so we need a fresh lookup here.
MDValues.push_back(MD); MDs.push_back(MD);
MDValueMap[MD] = MDValues.size(); MDValueMap[MD] = MDs.size();
} }
/// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata /// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
/// information reachable from the given MDNode. /// information reachable from the metadata.
void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) { void ValueEnumerator::EnumerateFunctionLocalMetadata(
assert(N->isFunctionLocal() && N->getFunction() && const LocalAsMetadata *Local) {
"EnumerateFunctionLocalMetadata called on non-function-local mdnode!");
// Enumerate the type of this value.
EnumerateType(N->getType());
// Check to see if it's already in! // Check to see if it's already in!
unsigned &MDValueID = MDValueMap[N]; unsigned &MDValueID = MDValueMap[Local];
if (MDValueID) if (MDValueID)
return; return;
MDValues.push_back(N); MDs.push_back(Local);
MDValueID = MDValues.size(); MDValueID = MDs.size();
// To incoroporate function-local information visit all function-local EnumerateValue(Local->getValue());
// MDNodes and all function-local values they reference.
for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
if (Value *V = N->getOperand(i)) {
if (MDNode *O = dyn_cast<MDNode>(V)) {
if (O->isFunctionLocal() && O->getFunction())
EnumerateFunctionLocalMetadata(O);
} else if (isa<Instruction>(V) || isa<Argument>(V))
EnumerateValue(V);
}
// Also, collect all function-local MDNodes for easy access. // Also, collect all function-local metadata for easy access.
FunctionLocalMDs.push_back(N); FunctionLocalMDs.push_back(Local);
} }
void ValueEnumerator::EnumerateValue(const Value *V) { void ValueEnumerator::EnumerateValue(const Value *V) {
assert(!V->getType()->isVoidTy() && "Can't insert void values!"); assert(!V->getType()->isVoidTy() && "Can't insert void values!");
assert(!isa<MDNode>(V) && !isa<MDString>(V) && assert(!isa<MetadataAsValue>(V) && "EnumerateValue doesn't handle Metadata!");
"EnumerateValue doesn't handle Metadata!");
// Check to see if it's already in! // Check to see if it's already in!
unsigned &ValueID = ValueMap[V]; unsigned &ValueID = ValueMap[V];
@ -657,30 +667,35 @@ void ValueEnumerator::EnumerateType(Type *Ty) {
void ValueEnumerator::EnumerateOperandType(const Value *V) { void ValueEnumerator::EnumerateOperandType(const Value *V) {
EnumerateType(V->getType()); EnumerateType(V->getType());
if (const Constant *C = dyn_cast<Constant>(V)) { if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
// If this constant is already enumerated, ignore it, we know its type must assert(!isa<LocalAsMetadata>(MD->getMetadata()) &&
// be enumerated. "Function-local metadata should be left for later");
if (ValueMap.count(V)) return;
// This constant may have operands, make sure to enumerate the types in EnumerateMetadata(MD->getMetadata());
// them. return;
for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) { }
const Value *Op = C->getOperand(i);
// Don't enumerate basic blocks here, this happens as operands to const Constant *C = dyn_cast<Constant>(V);
// blockaddress. if (!C)
if (isa<BasicBlock>(Op)) continue; return;
EnumerateOperandType(Op); // If this constant is already enumerated, ignore it, we know its type must
} // be enumerated.
if (ValueMap.count(C))
return;
if (const MDNode *N = dyn_cast<MDNode>(V)) { // This constant may have operands, make sure to enumerate the types in
for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) // them.
if (Value *Elem = N->getOperand(i)) for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
EnumerateOperandType(Elem); const Value *Op = C->getOperand(i);
}
} else if (isa<MDString>(V) || isa<MDNode>(V)) // Don't enumerate basic blocks here, this happens as operands to
EnumerateMetadata(V); // blockaddress.
if (isa<BasicBlock>(Op))
continue;
EnumerateOperandType(Op);
}
} }
void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) { void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) {
@ -708,7 +723,7 @@ void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) {
void ValueEnumerator::incorporateFunction(const Function &F) { void ValueEnumerator::incorporateFunction(const Function &F) {
InstructionCount = 0; InstructionCount = 0;
NumModuleValues = Values.size(); NumModuleValues = Values.size();
NumModuleMDValues = MDValues.size(); NumModuleMDs = MDs.size();
// Adding function arguments to the value table. // Adding function arguments to the value table.
for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
@ -739,24 +754,16 @@ void ValueEnumerator::incorporateFunction(const Function &F) {
FirstInstID = Values.size(); FirstInstID = Values.size();
SmallVector<MDNode *, 8> FnLocalMDVector; SmallVector<LocalAsMetadata *, 8> FnLocalMDVector;
// Add all of the instructions. // Add all of the instructions.
for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) { for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
OI != E; ++OI) { OI != E; ++OI) {
if (MDNode *MD = dyn_cast<MDNode>(*OI)) if (auto *MD = dyn_cast<MetadataAsValue>(&*OI))
if (MD->isFunctionLocal() && MD->getFunction()) if (auto *Local = dyn_cast<LocalAsMetadata>(MD->getMetadata()))
// Enumerate metadata after the instructions they might refer to. // Enumerate metadata after the instructions they might refer to.
FnLocalMDVector.push_back(MD); FnLocalMDVector.push_back(Local);
}
SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
I->getAllMetadataOtherThanDebugLoc(MDs);
for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
MDNode *N = MDs[i].second;
if (N->isFunctionLocal() && N->getFunction())
FnLocalMDVector.push_back(N);
} }
if (!I->getType()->isVoidTy()) if (!I->getType()->isVoidTy())
@ -773,13 +780,13 @@ void ValueEnumerator::purgeFunction() {
/// Remove purged values from the ValueMap. /// Remove purged values from the ValueMap.
for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i) for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
ValueMap.erase(Values[i].first); ValueMap.erase(Values[i].first);
for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i) for (unsigned i = NumModuleMDs, e = MDs.size(); i != e; ++i)
MDValueMap.erase(MDValues[i]); MDValueMap.erase(MDs[i]);
for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i) for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
ValueMap.erase(BasicBlocks[i]); ValueMap.erase(BasicBlocks[i]);
Values.resize(NumModuleValues); Values.resize(NumModuleValues);
MDValues.resize(NumModuleMDValues); MDs.resize(NumModuleMDs);
BasicBlocks.clear(); BasicBlocks.clear();
FunctionLocalMDs.clear(); FunctionLocalMDs.clear();
} }

View File

@ -30,6 +30,8 @@ class BasicBlock;
class Comdat; class Comdat;
class Function; class Function;
class Module; class Module;
class Metadata;
class LocalAsMetadata;
class MDNode; class MDNode;
class NamedMDNode; class NamedMDNode;
class AttributeSet; class AttributeSet;
@ -58,9 +60,10 @@ private:
typedef UniqueVector<const Comdat *> ComdatSetType; typedef UniqueVector<const Comdat *> ComdatSetType;
ComdatSetType Comdats; ComdatSetType Comdats;
std::vector<const Value *> MDValues; std::vector<const Metadata *> MDs;
SmallVector<const MDNode *, 8> FunctionLocalMDs; SmallVector<const LocalAsMetadata *, 8> FunctionLocalMDs;
ValueMapType MDValueMap; typedef DenseMap<const Metadata *, unsigned> MetadataMapType;
MetadataMapType MDValueMap;
typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType; typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType;
AttributeGroupMapType AttributeGroupMap; AttributeGroupMapType AttributeGroupMap;
@ -88,7 +91,7 @@ private:
/// When a function is incorporated, this is the size of the MDValues list /// When a function is incorporated, this is the size of the MDValues list
/// before incorporation. /// before incorporation.
unsigned NumModuleMDValues; unsigned NumModuleMDs;
unsigned FirstFuncConstantID; unsigned FirstFuncConstantID;
unsigned FirstInstID; unsigned FirstInstID;
@ -100,8 +103,11 @@ public:
void dump() const; void dump() const;
void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const; void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
void print(raw_ostream &OS, const MetadataMapType &Map,
const char *Name) const;
unsigned getValueID(const Value *V) const; unsigned getValueID(const Value *V) const;
unsigned getMetadataID(const Metadata *V) const;
unsigned getTypeID(Type *T) const { unsigned getTypeID(Type *T) const {
TypeMapType::const_iterator I = TypeMap.find(T); TypeMapType::const_iterator I = TypeMap.find(T);
@ -134,8 +140,8 @@ public:
} }
const ValueList &getValues() const { return Values; } const ValueList &getValues() const { return Values; }
const std::vector<const Value *> &getMDValues() const { return MDValues; } const std::vector<const Metadata *> &getMDs() const { return MDs; }
const SmallVectorImpl<const MDNode *> &getFunctionLocalMDValues() const { const SmallVectorImpl<const LocalAsMetadata *> &getFunctionLocalMDs() const {
return FunctionLocalMDs; return FunctionLocalMDs;
} }
const TypeList &getTypes() const { return Types; } const TypeList &getTypes() const { return Types; }
@ -167,8 +173,8 @@ private:
void OptimizeConstants(unsigned CstStart, unsigned CstEnd); void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
void EnumerateMDNodeOperands(const MDNode *N); void EnumerateMDNodeOperands(const MDNode *N);
void EnumerateMetadata(const Value *MD); void EnumerateMetadata(const Metadata *MD);
void EnumerateFunctionLocalMetadata(const MDNode *N); void EnumerateFunctionLocalMetadata(const LocalAsMetadata *Local);
void EnumerateNamedMDNode(const NamedMDNode *NMD); void EnumerateNamedMDNode(const NamedMDNode *NMD);
void EnumerateValue(const Value *V); void EnumerateValue(const Value *V);
void EnumerateType(Type *T); void EnumerateType(Type *T);

View File

@ -64,7 +64,7 @@ static void srcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
if (LocInfo->getNumOperands() != 0) if (LocInfo->getNumOperands() != 0)
if (const ConstantInt *CI = if (const ConstantInt *CI =
dyn_cast<ConstantInt>(LocInfo->getOperand(ErrorLine))) mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
LocCookie = CI->getZExtValue(); LocCookie = CI->getZExtValue();
} }
@ -467,7 +467,8 @@ void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
if (MI->getOperand(i-1).isMetadata() && if (MI->getOperand(i-1).isMetadata() &&
(LocMD = MI->getOperand(i-1).getMetadata()) && (LocMD = MI->getOperand(i-1).getMetadata()) &&
LocMD->getNumOperands() != 0) { LocMD->getNumOperands() != 0) {
if (const ConstantInt *CI = dyn_cast<ConstantInt>(LocMD->getOperand(0))) { if (const ConstantInt *CI =
mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
LocCookie = CI->getZExtValue(); LocCookie = CI->getZExtValue();
break; break;
} }

View File

@ -1192,10 +1192,10 @@ DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
addType(ParamDIE, resolve(VP.getType())); addType(ParamDIE, resolve(VP.getType()));
if (!VP.getName().empty()) if (!VP.getName().empty())
addString(ParamDIE, dwarf::DW_AT_name, VP.getName()); addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
if (Value *Val = VP.getValue()) { if (Metadata *Val = VP.getValue()) {
if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) if (ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Val))
addConstantValue(ParamDIE, CI, resolve(VP.getType())); addConstantValue(ParamDIE, CI, resolve(VP.getType()));
else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) { else if (GlobalValue *GV = mdconst::dyn_extract<GlobalValue>(Val)) {
// For declaration non-type template parameters (such as global values and // For declaration non-type template parameters (such as global values and
// functions) // functions)
DIELoc *Loc = new (DIEValueAllocator) DIELoc(); DIELoc *Loc = new (DIEValueAllocator) DIELoc();

View File

@ -3810,8 +3810,10 @@ static bool extractBranchMetadata(BranchInst *BI,
if (!ProfileData || ProfileData->getNumOperands() != 3) if (!ProfileData || ProfileData->getNumOperands() != 3)
return false; return false;
const auto *CITrue = dyn_cast<ConstantInt>(ProfileData->getOperand(1)); const auto *CITrue =
const auto *CIFalse = dyn_cast<ConstantInt>(ProfileData->getOperand(2)); mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1));
const auto *CIFalse =
mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2));
if (!CITrue || !CIFalse) if (!CITrue || !CIFalse)
return false; return false;

View File

@ -397,7 +397,7 @@ void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
break; break;
case MachineOperand::MO_Metadata: case MachineOperand::MO_Metadata:
OS << '<'; OS << '<';
getMetadata()->printAsOperand(OS, /*PrintType=*/false); getMetadata()->printAsOperand(OS);
OS << '>'; OS << '>';
break; break;
case MachineOperand::MO_MCSymbol: case MachineOperand::MO_MCSymbol:
@ -537,7 +537,7 @@ raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) {
if (const MDNode *TBAAInfo = MMO.getAAInfo().TBAA) { if (const MDNode *TBAAInfo = MMO.getAAInfo().TBAA) {
OS << "(tbaa="; OS << "(tbaa=";
if (TBAAInfo->getNumOperands() > 0) if (TBAAInfo->getNumOperands() > 0)
TBAAInfo->getOperand(0)->printAsOperand(OS, /*PrintType=*/false); TBAAInfo->getOperand(0)->printAsOperand(OS);
else else
OS << "<unknown>"; OS << "<unknown>";
OS << ")"; OS << ")";
@ -548,7 +548,7 @@ raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) {
OS << "(alias.scope="; OS << "(alias.scope=";
if (ScopeInfo->getNumOperands() > 0) if (ScopeInfo->getNumOperands() > 0)
for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) { for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) {
ScopeInfo->getOperand(i)->printAsOperand(OS, /*PrintType=*/false); ScopeInfo->getOperand(i)->printAsOperand(OS);
if (i != ie-1) if (i != ie-1)
OS << ","; OS << ",";
} }
@ -562,7 +562,7 @@ raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) {
OS << "(noalias="; OS << "(noalias=";
if (NoAliasInfo->getNumOperands() > 0) if (NoAliasInfo->getNumOperands() > 0)
for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) { for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) {
NoAliasInfo->getOperand(i)->printAsOperand(OS, /*PrintType=*/false); NoAliasInfo->getOperand(i)->printAsOperand(OS);
if (i != ie-1) if (i != ie-1)
OS << ","; OS << ",";
} }
@ -599,6 +599,8 @@ MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid,
: MCID(&tid), Parent(nullptr), Operands(nullptr), NumOperands(0), : MCID(&tid), Parent(nullptr), Operands(nullptr), NumOperands(0),
Flags(0), AsmPrinterFlags(0), Flags(0), AsmPrinterFlags(0),
NumMemRefs(0), MemRefs(nullptr), debugLoc(dl) { NumMemRefs(0), MemRefs(nullptr), debugLoc(dl) {
assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
// Reserve space for the expected number of operands. // Reserve space for the expected number of operands.
if (unsigned NumOps = MCID->getNumOperands() + if (unsigned NumOps = MCID->getNumOperands() +
MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) { MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) {
@ -617,6 +619,8 @@ MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Flags(0), AsmPrinterFlags(0), Flags(0), AsmPrinterFlags(0),
NumMemRefs(MI.NumMemRefs), MemRefs(MI.MemRefs), NumMemRefs(MI.NumMemRefs), MemRefs(MI.MemRefs),
debugLoc(MI.getDebugLoc()) { debugLoc(MI.getDebugLoc()) {
assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
CapOperands = OperandCapacity::get(MI.getNumOperands()); CapOperands = OperandCapacity::get(MI.getNumOperands());
Operands = MF.allocateOperandArray(CapOperands); Operands = MF.allocateOperandArray(CapOperands);
@ -1960,7 +1964,8 @@ void MachineInstr::emitError(StringRef Msg) const {
if (getOperand(i-1).isMetadata() && if (getOperand(i-1).isMetadata() &&
(LocMD = getOperand(i-1).getMetadata()) && (LocMD = getOperand(i-1).getMetadata()) &&
LocMD->getNumOperands() != 0) { LocMD->getNumOperands() != 0) {
if (const ConstantInt *CI = dyn_cast<ConstantInt>(LocMD->getOperand(0))) { if (const ConstantInt *CI =
mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
LocCookie = CI->getZExtValue(); LocCookie = CI->getZExtValue();
break; break;
} }

View File

@ -4714,7 +4714,8 @@ SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
return nullptr; return nullptr;
case Intrinsic::read_register: { case Intrinsic::read_register: {
Value *Reg = I.getArgOperand(0); Value *Reg = I.getArgOperand(0);
SDValue RegName = DAG.getMDNode(cast<MDNode>(Reg)); SDValue RegName =
DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
EVT VT = TLI.getValueType(I.getType()); EVT VT = TLI.getValueType(I.getType());
setValue(&I, DAG.getNode(ISD::READ_REGISTER, sdl, VT, RegName)); setValue(&I, DAG.getNode(ISD::READ_REGISTER, sdl, VT, RegName));
return nullptr; return nullptr;
@ -4723,7 +4724,8 @@ SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Value *Reg = I.getArgOperand(0); Value *Reg = I.getArgOperand(0);
Value *RegValue = I.getArgOperand(1); Value *RegValue = I.getArgOperand(1);
SDValue Chain = getValue(RegValue).getOperand(0); SDValue Chain = getValue(RegValue).getOperand(0);
SDValue RegName = DAG.getMDNode(cast<MDNode>(Reg)); SDValue RegName =
DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
DAG.setRoot(DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other, Chain, DAG.setRoot(DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other, Chain,
RegName, getValue(RegValue))); RegName, getValue(RegValue)));
return nullptr; return nullptr;

View File

@ -463,7 +463,8 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
if (!VI.Var) if (!VI.Var)
continue; continue;
if (SlotRemap.count(VI.Slot)) { if (SlotRemap.count(VI.Slot)) {
DEBUG(dbgs()<<"Remapping debug info for ["<<VI.Var->getName()<<"].\n"); DEBUG(dbgs() << "Remapping debug info for ["
<< DIVariable(VI.Var).getName() << "].\n");
VI.Slot = SlotRemap[VI.Slot]; VI.Slot = SlotRemap[VI.Slot];
FixedDbg++; FixedDbg++;
} }

View File

@ -464,15 +464,15 @@ emitModuleFlags(MCStreamer &Streamer,
continue; continue;
StringRef Key = MFE.Key->getString(); StringRef Key = MFE.Key->getString();
Value *Val = MFE.Val; Metadata *Val = MFE.Val;
if (Key == "Objective-C Image Info Version") { if (Key == "Objective-C Image Info Version") {
VersionVal = cast<ConstantInt>(Val)->getZExtValue(); VersionVal = mdconst::extract<ConstantInt>(Val)->getZExtValue();
} else if (Key == "Objective-C Garbage Collection" || } else if (Key == "Objective-C Garbage Collection" ||
Key == "Objective-C GC Only" || Key == "Objective-C GC Only" ||
Key == "Objective-C Is Simulated" || Key == "Objective-C Is Simulated" ||
Key == "Objective-C Image Swift Version") { Key == "Objective-C Image Swift Version") {
ImageInfoFlags |= cast<ConstantInt>(Val)->getZExtValue(); ImageInfoFlags |= mdconst::extract<ConstantInt>(Val)->getZExtValue();
} else if (Key == "Objective-C Image Info Section") { } else if (Key == "Objective-C Image Info Section") {
SectionVal = cast<MDString>(Val)->getString(); SectionVal = cast<MDString>(Val)->getString();
} else if (Key == "Linker Options") { } else if (Key == "Linker Options") {
@ -966,7 +966,7 @@ emitModuleFlags(MCStreamer &Streamer,
i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) { i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
const Module::ModuleFlagEntry &MFE = *i; const Module::ModuleFlagEntry &MFE = *i;
StringRef Key = MFE.Key->getString(); StringRef Key = MFE.Key->getString();
Value *Val = MFE.Val; Metadata *Val = MFE.Val;
if (Key == "Linker Options") { if (Key == "Linker Options") {
LinkerOptions = cast<MDNode>(Val); LinkerOptions = cast<MDNode>(Val);
break; break;

View File

@ -634,13 +634,6 @@ static SlotTracker *createSlotTracker(const Value *V) {
if (const Function *Func = dyn_cast<Function>(V)) if (const Function *Func = dyn_cast<Function>(V))
return new SlotTracker(Func); return new SlotTracker(Func);
if (const MDNode *MD = dyn_cast<MDNode>(V)) {
if (!MD->isFunctionLocal())
return new SlotTracker(MD->getFunction());
return new SlotTracker((Function *)nullptr);
}
return nullptr; return nullptr;
} }
@ -653,16 +646,14 @@ static SlotTracker *createSlotTracker(const Value *V) {
// Module level constructor. Causes the contents of the Module (sans functions) // Module level constructor. Causes the contents of the Module (sans functions)
// to be added to the slot table. // to be added to the slot table.
SlotTracker::SlotTracker(const Module *M) SlotTracker::SlotTracker(const Module *M)
: TheModule(M), TheFunction(nullptr), FunctionProcessed(false), : TheModule(M), TheFunction(nullptr), FunctionProcessed(false), mNext(0),
mNext(0), fNext(0), mdnNext(0), asNext(0) { fNext(0), mdnNext(0), asNext(0) {}
}
// Function level constructor. Causes the contents of the Module and the one // Function level constructor. Causes the contents of the Module and the one
// function provided to be added to the slot table. // function provided to be added to the slot table.
SlotTracker::SlotTracker(const Function *F) SlotTracker::SlotTracker(const Function *F)
: TheModule(F ? F->getParent() : nullptr), TheFunction(F), : TheModule(F ? F->getParent() : nullptr), TheFunction(F),
FunctionProcessed(false), mNext(0), fNext(0), mdnNext(0), asNext(0) { FunctionProcessed(false), mNext(0), fNext(0), mdnNext(0), asNext(0) {}
}
inline void SlotTracker::initialize() { inline void SlotTracker::initialize() {
if (TheModule) { if (TheModule) {
@ -744,8 +735,9 @@ void SlotTracker::processFunction() {
if (Function *F = CI->getCalledFunction()) if (Function *F = CI->getCalledFunction())
if (F->isIntrinsic()) if (F->isIntrinsic())
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i))) if (auto *V = dyn_cast_or_null<MetadataAsValue>(I->getOperand(i)))
CreateMetadataSlot(N); if (MDNode *N = dyn_cast<MDNode>(V->getMetadata()))
CreateMetadataSlot(N);
// Add all the call attributes to the table. // Add all the call attributes to the table.
AttributeSet Attrs = CI->getAttributes().getFnAttributes(); AttributeSet Attrs = CI->getAttributes().getFnAttributes();
@ -856,16 +848,10 @@ void SlotTracker::CreateFunctionSlot(const Value *V) {
void SlotTracker::CreateMetadataSlot(const MDNode *N) { void SlotTracker::CreateMetadataSlot(const MDNode *N) {
assert(N && "Can't insert a null Value into SlotTracker!"); assert(N && "Can't insert a null Value into SlotTracker!");
// Don't insert if N is a function-local metadata, these are always printed unsigned DestSlot = mdnNext;
// inline. if (!mdnMap.insert(std::make_pair(N, DestSlot)).second)
if (!N->isFunctionLocal()) { return;
mdn_iterator I = mdnMap.find(N); ++mdnNext;
if (I != mdnMap.end())
return;
unsigned DestSlot = mdnNext++;
mdnMap[N] = DestSlot;
}
// Recursively add any MDNodes referenced by operands. // Recursively add any MDNodes referenced by operands.
for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
@ -894,6 +880,11 @@ static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
SlotTracker *Machine, SlotTracker *Machine,
const Module *Context); const Module *Context);
static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
TypePrinting *TypePrinter,
SlotTracker *Machine, const Module *Context,
bool FromValue = false);
static const char *getPredicateText(unsigned predicate) { static const char *getPredicateText(unsigned predicate) {
const char * pred = "unknown"; const char * pred = "unknown";
switch (predicate) { switch (predicate) {
@ -1264,14 +1255,17 @@ static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
const Module *Context) { const Module *Context) {
Out << "!{"; Out << "!{";
for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) { for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
const Value *V = Node->getOperand(mi); const Metadata *MD = Node->getOperand(mi);
if (!V) if (!MD)
Out << "null"; Out << "null";
else { else if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) {
Value *V = MDV->getValue();
TypePrinter->print(V->getType(), Out); TypePrinter->print(V->getType(), Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, Node->getOperand(mi), WriteAsOperandInternal(Out, V, TypePrinter, Machine, Context);
TypePrinter, Machine, Context); } else {
Out << "metadata ";
WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context);
} }
if (mi + 1 != me) if (mi + 1 != me)
Out << ", "; Out << ", ";
@ -1315,31 +1309,9 @@ static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
return; return;
} }
if (const MDNode *N = dyn_cast<MDNode>(V)) { if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
if (N->isFunctionLocal()) { WriteAsOperandInternal(Out, MD->getMetadata(), TypePrinter, Machine,
// Print metadata inline, not via slot reference number. Context, /* FromValue */ true);
WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine, Context);
return;
}
if (!Machine) {
if (N->isFunctionLocal())
Machine = new SlotTracker(N->getFunction());
else
Machine = new SlotTracker(Context);
}
int Slot = Machine->getMetadataSlot(N);
if (Slot == -1)
Out << "<badref>";
else
Out << '!' << Slot;
return;
}
if (const MDString *MDS = dyn_cast<MDString>(V)) {
Out << "!\"";
PrintEscapedString(MDS->getString(), Out);
Out << '"';
return; return;
} }
@ -1382,6 +1354,42 @@ static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Out << "<badref>"; Out << "<badref>";
} }
static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
TypePrinting *TypePrinter,
SlotTracker *Machine, const Module *Context,
bool FromValue) {
if (const MDNode *N = dyn_cast<MDNode>(MD)) {
if (!Machine)
Machine = new SlotTracker(Context);
int Slot = Machine->getMetadataSlot(N);
if (Slot == -1)
Out << "<badref>";
else
Out << '!' << Slot;
return;
}
if (const MDString *MDS = dyn_cast<MDString>(MD)) {
Out << "!\"";
PrintEscapedString(MDS->getString(), Out);
Out << '"';
return;
}
auto *V = cast<ValueAsMetadata>(MD);
assert(TypePrinter && "TypePrinter required for metadata values");
assert((FromValue || !isa<LocalAsMetadata>(V)) &&
"Unexpected function-local metadata outside of value argument");
if (FromValue)
Out << "!{";
TypePrinter->print(V->getValue()->getType(), Out);
Out << ' ';
WriteAsOperandInternal(Out, V->getValue(), TypePrinter, Machine, Context);
if (FromValue)
Out << "}";
}
void AssemblyWriter::init() { void AssemblyWriter::init() {
if (!TheModule) if (!TheModule)
return; return;
@ -2351,7 +2359,7 @@ static void WriteMDNodeComment(const MDNode *Node,
if (Node->getNumOperands() < 1) if (Node->getNumOperands() < 1)
return; return;
Value *Op = Node->getOperand(0); Metadata *Op = Node->getOperand(0);
if (!Op || !isa<MDString>(Op)) if (!Op || !isa<MDString>(Op))
return; return;
@ -2522,18 +2530,14 @@ void Value::print(raw_ostream &ROS) const {
W.printFunction(F); W.printFunction(F);
else else
W.printAlias(cast<GlobalAlias>(GV)); W.printAlias(cast<GlobalAlias>(GV));
} else if (const MDNode *N = dyn_cast<MDNode>(this)) { } else if (const MetadataAsValue *V = dyn_cast<MetadataAsValue>(this)) {
const Function *F = N->getFunction(); V->getMetadata()->print(ROS);
SlotTracker SlotTable(F);
AssemblyWriter W(OS, SlotTable, F ? F->getParent() : nullptr, nullptr);
W.printMDNodeBody(N);
} else if (const Constant *C = dyn_cast<Constant>(this)) { } else if (const Constant *C = dyn_cast<Constant>(this)) {
TypePrinting TypePrinter; TypePrinting TypePrinter;
TypePrinter.print(C->getType(), OS); TypePrinter.print(C->getType(), OS);
OS << ' '; OS << ' ';
WriteConstantInternal(OS, C, TypePrinter, nullptr, nullptr); WriteConstantInternal(OS, C, TypePrinter, nullptr, nullptr);
} else if (isa<InlineAsm>(this) || isa<MDString>(this) || } else if (isa<InlineAsm>(this) || isa<Argument>(this)) {
isa<Argument>(this)) {
this->printAsOperand(OS); this->printAsOperand(OS);
} else { } else {
llvm_unreachable("Unknown value to print out!"); llvm_unreachable("Unknown value to print out!");
@ -2543,9 +2547,8 @@ void Value::print(raw_ostream &ROS) const {
void Value::printAsOperand(raw_ostream &O, bool PrintType, const Module *M) const { void Value::printAsOperand(raw_ostream &O, bool PrintType, const Module *M) const {
// Fast path: Don't construct and populate a TypePrinting object if we // Fast path: Don't construct and populate a TypePrinting object if we
// won't be needing any types printed. // won't be needing any types printed.
if (!PrintType && if (!PrintType && ((!isa<Constant>(this) && !isa<MetadataAsValue>(this)) ||
((!isa<Constant>(this) && !isa<MDNode>(this)) || hasName() || isa<GlobalValue>(this))) {
hasName() || isa<GlobalValue>(this))) {
WriteAsOperandInternal(O, this, nullptr, nullptr, M); WriteAsOperandInternal(O, this, nullptr, nullptr, M);
return; return;
} }
@ -2564,6 +2567,35 @@ void Value::printAsOperand(raw_ostream &O, bool PrintType, const Module *M) cons
WriteAsOperandInternal(O, this, &TypePrinter, nullptr, M); WriteAsOperandInternal(O, this, &TypePrinter, nullptr, M);
} }
void Metadata::print(raw_ostream &ROS) const {
formatted_raw_ostream OS(ROS);
if (auto *N = dyn_cast<MDNode>(this)) {
OS << "metadata ";
SlotTracker SlotTable(static_cast<Function *>(nullptr));
AssemblyWriter W(OS, SlotTable, nullptr, nullptr);
W.printMDNodeBody(N);
return;
}
printAsOperand(OS);
}
void Metadata::printAsOperand(raw_ostream &ROS, bool PrintType,
const Module *M) const {
formatted_raw_ostream OS(ROS);
if (PrintType)
OS << "metadata ";
std::unique_ptr<TypePrinting> TypePrinter;
if (PrintType) {
TypePrinter.reset(new TypePrinting);
if (M)
TypePrinter->incorporateTypes(*M);
}
WriteAsOperandInternal(OS, this, TypePrinter.get(), nullptr, M,
/* FromValue */ true);
}
// Value::dump - allow easy printing of Values from the debugger. // Value::dump - allow easy printing of Values from the debugger.
void Value::dump() const { print(dbgs()); dbgs() << '\n'; } void Value::dump() const { print(dbgs()); dbgs() << '\n'; }
@ -2578,3 +2610,8 @@ void Comdat::dump() const { print(dbgs()); }
// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger. // NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
void NamedMDNode::dump() const { print(dbgs()); } void NamedMDNode::dump() const { print(dbgs()); }
void Metadata::dump() const {
print(dbgs());
dbgs() << '\n';
}

View File

@ -260,14 +260,15 @@ static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
return dyn_cast_or_null<MDNode>(DbgNode->getOperand(Elt)); return dyn_cast_or_null<MDNode>(DbgNode->getOperand(Elt));
} }
static DIExpression getExpression(Value *VarOperand, Function *F) { static MetadataAsValue *getExpression(Value *VarOperand, Function *F) {
// Old-style DIVariables have an optional expression as the 8th element. // Old-style DIVariables have an optional expression as the 8th element.
DIExpression Expr(getNodeField(cast<MDNode>(VarOperand), 8)); DIExpression Expr(getNodeField(
cast<MDNode>(cast<MetadataAsValue>(VarOperand)->getMetadata()), 8));
if (!Expr) { if (!Expr) {
DIBuilder DIB(*F->getParent()); DIBuilder DIB(*F->getParent(), /*AllowUnresolved*/ false);
Expr = DIB.createExpression(); Expr = DIB.createExpression();
} }
return Expr; return MetadataAsValue::get(F->getContext(), Expr);
} }
// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
@ -306,8 +307,9 @@ void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Builder.SetInsertPoint(CI->getParent(), CI); Builder.SetInsertPoint(CI->getParent(), CI);
Module *M = F->getParent(); Module *M = F->getParent();
SmallVector<Value *, 1> Elts; SmallVector<Metadata *, 1> Elts;
Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1)); Elts.push_back(
ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
MDNode *Node = MDNode::get(C, Elts); MDNode *Node = MDNode::get(C, Elts);
Value *Arg0 = CI->getArgOperand(0); Value *Arg0 = CI->getArgOperand(0);
@ -578,22 +580,18 @@ void llvm::UpgradeInstWithTBAATag(Instruction *I) {
return; return;
if (MD->getNumOperands() == 3) { if (MD->getNumOperands() == 3) {
Value *Elts[] = { Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)};
MD->getOperand(0),
MD->getOperand(1)
};
MDNode *ScalarType = MDNode::get(I->getContext(), Elts); MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
// Create a MDNode <ScalarType, ScalarType, offset 0, const> // Create a MDNode <ScalarType, ScalarType, offset 0, const>
Value *Elts2[] = { Metadata *Elts2[] = {ScalarType, ScalarType,
ScalarType, ScalarType, ConstantAsMetadata::get(Constant::getNullValue(
Constant::getNullValue(Type::getInt64Ty(I->getContext())), Type::getInt64Ty(I->getContext()))),
MD->getOperand(2) MD->getOperand(2)};
};
I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2)); I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
} else { } else {
// Create a MDNode <MD, MD, offset 0> // Create a MDNode <MD, MD, offset 0>
Value *Elts[] = {MD, MD, Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue(
Constant::getNullValue(Type::getInt64Ty(I->getContext()))}; Type::getInt64Ty(I->getContext())))};
I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts)); I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
} }
} }

View File

@ -32,6 +32,7 @@ add_llvm_library(LLVMCore
MDBuilder.cpp MDBuilder.cpp
Mangler.cpp Mangler.cpp
Metadata.cpp Metadata.cpp
MetadataTracking.cpp
Module.cpp Module.cpp
Pass.cpp Pass.cpp
PassManager.cpp PassManager.cpp

View File

@ -556,12 +556,17 @@ int LLVMHasMetadata(LLVMValueRef Inst) {
} }
LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) { LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID)); auto *I = unwrap<Instruction>(Inst);
assert(I && "Expected instruction");
if (auto *MD = I->getMetadata(KindID))
return wrap(MetadataAsValue::get(I->getContext(), MD));
return nullptr;
} }
void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) { void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
unwrap<Instruction>(Inst) MDNode *N =
->setMetadata(KindID, MD ? unwrap<MDNode>(MD) : nullptr); MD ? cast<MDNode>(unwrap<MetadataAsValue>(MD)->getMetadata()) : nullptr;
unwrap<Instruction>(Inst)->setMetadata(KindID, N);
} }
/*--.. Conversion functions ................................................--*/ /*--.. Conversion functions ................................................--*/
@ -573,6 +578,21 @@ void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST) LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) {
if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
if (isa<MDNode>(MD->getMetadata()) ||
isa<ValueAsMetadata>(MD->getMetadata()))
return Val;
return nullptr;
}
LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) {
if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
if (isa<MDString>(MD->getMetadata()))
return Val;
return nullptr;
}
/*--.. Operations on Uses ..................................................--*/ /*--.. Operations on Uses ..................................................--*/
LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) { LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
Value *V = unwrap(Val); Value *V = unwrap(Val);
@ -598,10 +618,28 @@ LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
} }
/*--.. Operations on Users .................................................--*/ /*--.. Operations on Users .................................................--*/
static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N,
unsigned Index) {
Metadata *Op = N->getOperand(Index);
if (!Op)
return nullptr;
if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
return wrap(C->getValue());
return wrap(MetadataAsValue::get(Context, Op));
}
LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) { LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
Value *V = unwrap(Val); Value *V = unwrap(Val);
if (MDNode *MD = dyn_cast<MDNode>(V)) if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
return wrap(MD->getOperand(Index)); if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
assert(Index == 0 && "Function-local metadata can only have one operand");
return wrap(L->getValue());
}
return getMDNodeOperandImpl(V->getContext(),
cast<MDNode>(MD->getMetadata()), Index);
}
return wrap(cast<User>(V)->getOperand(Index)); return wrap(cast<User>(V)->getOperand(Index));
} }
@ -616,8 +654,9 @@ void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
int LLVMGetNumOperands(LLVMValueRef Val) { int LLVMGetNumOperands(LLVMValueRef Val) {
Value *V = unwrap(Val); Value *V = unwrap(Val);
if (MDNode *MD = dyn_cast<MDNode>(V)) if (isa<MetadataAsValue>(V))
return MD->getNumOperands(); return LLVMGetMDNodeNumOperands(Val);
return cast<User>(V)->getNumOperands(); return cast<User>(V)->getNumOperands();
} }
@ -658,7 +697,9 @@ LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str, LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
unsigned SLen) { unsigned SLen) {
return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen))); LLVMContext &Context = *unwrap(C);
return wrap(MetadataAsValue::get(
Context, MDString::get(Context, StringRef(Str, SLen))));
} }
LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) { LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
@ -667,8 +708,29 @@ LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals, LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
unsigned Count) { unsigned Count) {
return wrap(MDNode::get(*unwrap(C), LLVMContext &Context = *unwrap(C);
makeArrayRef(unwrap<Value>(Vals, Count), Count))); SmallVector<Metadata *, 8> MDs;
for (auto *OV : makeArrayRef(Vals, Count)) {
Value *V = unwrap(OV);
Metadata *MD;
if (!V)
MD = nullptr;
else if (auto *C = dyn_cast<Constant>(V))
MD = ConstantAsMetadata::get(C);
else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
MD = MDV->getMetadata();
assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
"outside of direct argument to call");
} else {
// This is function-local metadata. Pretend to make an MDNode.
assert(Count == 1 &&
"Expected only one operand to function-local metadata");
return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V)));
}
MDs.push_back(MD);
}
return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs)));
} }
LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) { LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
@ -676,25 +738,35 @@ LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
} }
const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) { const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
if (const MDString *S = dyn_cast<MDString>(unwrap(V))) { if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
*Len = S->getString().size(); if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
return S->getString().data(); *Len = S->getString().size();
} return S->getString().data();
}
*Len = 0; *Len = 0;
return nullptr; return nullptr;
} }
unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
{ {
return cast<MDNode>(unwrap(V))->getNumOperands(); auto *MD = cast<MetadataAsValue>(unwrap(V));
if (isa<ValueAsMetadata>(MD->getMetadata()))
return 1;
return cast<MDNode>(MD->getMetadata())->getNumOperands();
} }
void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
{ {
const MDNode *N = cast<MDNode>(unwrap(V)); auto *MD = cast<MetadataAsValue>(unwrap(V));
if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
*Dest = wrap(MDV->getValue());
return;
}
const auto *N = cast<MDNode>(MD->getMetadata());
const unsigned numOperands = N->getNumOperands(); const unsigned numOperands = N->getNumOperands();
LLVMContext &Context = unwrap(V)->getContext();
for (unsigned i = 0; i < numOperands; i++) for (unsigned i = 0; i < numOperands; i++)
Dest[i] = wrap(N->getOperand(i)); Dest[i] = getMDNodeOperandImpl(Context, N, i);
} }
unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name) unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
@ -710,8 +782,9 @@ void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRe
NamedMDNode *N = unwrap(M)->getNamedMetadata(name); NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
if (!N) if (!N)
return; return;
LLVMContext &Context = unwrap(M)->getContext();
for (unsigned i=0;i<N->getNumOperands();i++) for (unsigned i=0;i<N->getNumOperands();i++)
Dest[i] = wrap(N->getOperand(i)); Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
} }
void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name, void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
@ -720,9 +793,9 @@ void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name); NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
if (!N) if (!N)
return; return;
MDNode *Op = Val ? unwrap<MDNode>(Val) : nullptr; if (!Val)
if (Op) return;
N->addOperand(Op); N->addOperand(cast<MDNode>(unwrap<MetadataAsValue>(Val)->getMetadata()));
} }
/*--.. Operations on scalar constants ......................................--*/ /*--.. Operations on scalar constants ......................................--*/
@ -2092,13 +2165,16 @@ void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
/*--.. Metadata builders ...................................................--*/ /*--.. Metadata builders ...................................................--*/
void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) { void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
MDNode *Loc = L ? unwrap<MDNode>(L) : nullptr; MDNode *Loc =
L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc)); unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
} }
LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) { LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
return wrap(unwrap(Builder)->getCurrentDebugLocation() LLVMContext &Context = unwrap(Builder)->getContext();
.getAsMDNode(unwrap(Builder)->getContext())); return wrap(MetadataAsValue::get(
Context,
unwrap(Builder)->getCurrentDebugLocation().getAsMDNode(Context)));
} }
void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) { void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {

File diff suppressed because it is too large Load Diff

View File

@ -52,7 +52,7 @@ bool DIDescriptor::Verify() const {
DIImportedEntity(DbgNode).Verify() || DIExpression(DbgNode).Verify()); DIImportedEntity(DbgNode).Verify() || DIExpression(DbgNode).Verify());
} }
static Value *getField(const MDNode *DbgNode, unsigned Elt) { static Metadata *getField(const MDNode *DbgNode, unsigned Elt) {
if (!DbgNode || Elt >= DbgNode->getNumOperands()) if (!DbgNode || Elt >= DbgNode->getNumOperands())
return nullptr; return nullptr;
return DbgNode->getOperand(Elt); return DbgNode->getOperand(Elt);
@ -73,25 +73,17 @@ StringRef DIDescriptor::getStringField(unsigned Elt) const {
} }
uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const { uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
if (!DbgNode) if (auto *C = getConstantField(Elt))
return 0; if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
if (Elt < DbgNode->getNumOperands())
if (ConstantInt *CI =
dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
return CI->getZExtValue(); return CI->getZExtValue();
return 0; return 0;
} }
int64_t DIDescriptor::getInt64Field(unsigned Elt) const { int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
if (!DbgNode) if (auto *C = getConstantField(Elt))
return 0; if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
return CI->getZExtValue();
if (Elt < DbgNode->getNumOperands())
if (ConstantInt *CI =
dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
return CI->getSExtValue();
return 0; return 0;
} }
@ -102,12 +94,7 @@ DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
} }
GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const { GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
if (!DbgNode) return dyn_cast_or_null<GlobalVariable>(getConstantField(Elt));
return nullptr;
if (Elt < DbgNode->getNumOperands())
return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
return nullptr;
} }
Constant *DIDescriptor::getConstantField(unsigned Elt) const { Constant *DIDescriptor::getConstantField(unsigned Elt) const {
@ -115,17 +102,14 @@ Constant *DIDescriptor::getConstantField(unsigned Elt) const {
return nullptr; return nullptr;
if (Elt < DbgNode->getNumOperands()) if (Elt < DbgNode->getNumOperands())
return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt)); if (auto *C =
dyn_cast_or_null<ConstantAsMetadata>(DbgNode->getOperand(Elt)))
return C->getValue();
return nullptr; return nullptr;
} }
Function *DIDescriptor::getFunctionField(unsigned Elt) const { Function *DIDescriptor::getFunctionField(unsigned Elt) const {
if (!DbgNode) return dyn_cast_or_null<Function>(getConstantField(Elt));
return nullptr;
if (Elt < DbgNode->getNumOperands())
return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
return nullptr;
} }
void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) { void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
@ -134,7 +118,7 @@ void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
if (Elt < DbgNode->getNumOperands()) { if (Elt < DbgNode->getNumOperands()) {
MDNode *Node = const_cast<MDNode *>(DbgNode); MDNode *Node = const_cast<MDNode *>(DbgNode);
Node->replaceOperandWith(Elt, F); Node->replaceOperandWith(Elt, F ? ConstantAsMetadata::get(F) : nullptr);
} }
} }
@ -347,27 +331,23 @@ void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) {
// itself. // itself.
const MDNode *DN = D; const MDNode *DN = D;
if (DbgNode == DN) { if (DbgNode == DN) {
SmallVector<Value*, 10> Ops(DbgNode->getNumOperands()); SmallVector<Metadata *, 10> Ops(DbgNode->getNumOperands());
for (size_t i = 0; i != Ops.size(); ++i) for (size_t i = 0; i != Ops.size(); ++i)
Ops[i] = DbgNode->getOperand(i); Ops[i] = DbgNode->getOperand(i);
DN = MDNode::get(VMContext, Ops); DN = MDNode::get(VMContext, Ops);
} }
MDNode *Node = const_cast<MDNode *>(DbgNode); auto *Node = cast<MDNodeFwdDecl>(const_cast<MDNode *>(DbgNode));
const Value *V = cast_or_null<Value>(DN); Node->replaceAllUsesWith(const_cast<MDNode *>(DN));
Node->replaceAllUsesWith(const_cast<Value *>(V));
MDNode::deleteTemporary(Node); MDNode::deleteTemporary(Node);
DbgNode = DN; DbgNode = DN;
} }
void DIDescriptor::replaceAllUsesWith(MDNode *D) { void DIDescriptor::replaceAllUsesWith(MDNode *D) {
assert(DbgNode && "Trying to replace an unverified type!"); assert(DbgNode && "Trying to replace an unverified type!");
assert(DbgNode != D && "This replacement should always happen"); assert(DbgNode != D && "This replacement should always happen");
MDNode *Node = const_cast<MDNode *>(DbgNode); auto *Node = cast<MDNodeFwdDecl>(const_cast<MDNode *>(DbgNode));
const MDNode *DN = D; Node->replaceAllUsesWith(D);
const Value *V = cast_or_null<Value>(DN);
Node->replaceAllUsesWith(const_cast<Value *>(V));
MDNode::deleteTemporary(Node); MDNode::deleteTemporary(Node);
} }
@ -398,7 +378,7 @@ bool DIObjCProperty::Verify() const {
static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) { static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
// FIXME: This function should return true, if the field is null or the field // FIXME: This function should return true, if the field is null or the field
// is indeed a MDNode: return !Fld || isa<MDNode>(Fld). // is indeed a MDNode: return !Fld || isa<MDNode>(Fld).
Value *Fld = getField(DbgNode, Elt); Metadata *Fld = getField(DbgNode, Elt);
if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty()) if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty())
return false; return false;
return true; return true;
@ -406,7 +386,7 @@ static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
/// \brief Check if a field at position Elt of a MDNode is a MDString. /// \brief Check if a field at position Elt of a MDNode is a MDString.
static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) { static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
Value *Fld = getField(DbgNode, Elt); Metadata *Fld = getField(DbgNode, Elt);
return !Fld || isa<MDString>(Fld); return !Fld || isa<MDString>(Fld);
} }
@ -533,7 +513,6 @@ bool DISubprogram::Verify() const {
// If a DISubprogram has an llvm::Function*, then scope chains from all // If a DISubprogram has an llvm::Function*, then scope chains from all
// instructions within the function should lead to this DISubprogram. // instructions within the function should lead to this DISubprogram.
if (auto *F = getFunction()) { if (auto *F = getFunction()) {
LLVMContext &Ctxt = F->getContext();
for (auto &BB : *F) { for (auto &BB : *F) {
for (auto &I : BB) { for (auto &I : BB) {
DebugLoc DL = I.getDebugLoc(); DebugLoc DL = I.getDebugLoc();
@ -543,15 +522,15 @@ bool DISubprogram::Verify() const {
MDNode *Scope = nullptr; MDNode *Scope = nullptr;
MDNode *IA = nullptr; MDNode *IA = nullptr;
// walk the inlined-at scopes // walk the inlined-at scopes
while (DL.getScopeAndInlinedAt(Scope, IA, F->getContext()), IA) while ((IA = DL.getInlinedAt()))
DL = DebugLoc::getFromDILocation(IA); DL = DebugLoc::getFromDILocation(IA);
DL.getScopeAndInlinedAt(Scope, IA, Ctxt); DL.getScopeAndInlinedAt(Scope, IA);
assert(!IA); assert(!IA);
while (!DIDescriptor(Scope).isSubprogram()) { while (!DIDescriptor(Scope).isSubprogram()) {
DILexicalBlockFile D(Scope); DILexicalBlockFile D(Scope);
Scope = D.isLexicalBlockFile() Scope = D.isLexicalBlockFile()
? D.getScope() ? D.getScope()
: DebugLoc::getFromDILexicalBlock(Scope).getScope(Ctxt); : DebugLoc::getFromDILexicalBlock(Scope).getScope();
} }
if (!DISubprogram(Scope).describes(F)) if (!DISubprogram(Scope).describes(F))
return false; return false;
@ -678,7 +657,7 @@ MDString *DICompositeType::getIdentifier() const {
static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) { static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
for (unsigned i = 0; i != LHS->getNumOperands(); ++i) { for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
// Skip the 'empty' list (that's a single i32 0, rather than truly empty). // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
if (i == 0 && isa<ConstantInt>(LHS->getOperand(i))) if (i == 0 && mdconst::hasa<ConstantInt>(LHS->getOperand(i)))
continue; continue;
const MDNode *E = cast<MDNode>(LHS->getOperand(i)); const MDNode *E = cast<MDNode>(LHS->getOperand(i));
bool found = false; bool found = false;
@ -690,7 +669,7 @@ static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
#endif #endif
void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) { void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
TrackingVH<MDNode> N(*this); TrackingMDNodeRef N(*this);
if (Elements) { if (Elements) {
#ifndef NDEBUG #ifndef NDEBUG
// Check that the new list of members contains all the old members as well. // Check that the new list of members contains all the old members as well.
@ -714,7 +693,7 @@ DIScopeRef DIScope::getRef() const {
} }
void DICompositeType::setContainingType(DICompositeType ContainingType) { void DICompositeType::setContainingType(DICompositeType ContainingType) {
TrackingVH<MDNode> N(*this); TrackingMDNodeRef N(*this);
N->replaceOperandWith(5, ContainingType.getRef()); N->replaceOperandWith(5, ContainingType.getRef());
DbgNode = N; DbgNode = N;
} }
@ -748,8 +727,8 @@ DIArray DISubprogram::getVariables() const {
return DIArray(getNodeField(DbgNode, 8)); return DIArray(getNodeField(DbgNode, 8));
} }
Value *DITemplateValueParameter::getValue() const { Metadata *DITemplateValueParameter::getValue() const {
return getField(DbgNode, 3); return DbgNode->getOperand(3);
} }
DIScopeRef DIScope::getContext() const { DIScopeRef DIScope::getContext() const {
@ -851,7 +830,7 @@ void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
DILocation DILocation::copyWithNewScope(LLVMContext &Ctx, DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
DILexicalBlockFile NewScope) { DILexicalBlockFile NewScope) {
SmallVector<Value *, 10> Elts; SmallVector<Metadata *, 10> Elts;
assert(Verify()); assert(Verify());
for (unsigned I = 0; I < DbgNode->getNumOperands(); ++I) { for (unsigned I = 0; I < DbgNode->getNumOperands(); ++I) {
if (I != 2) if (I != 2)
@ -875,7 +854,7 @@ DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
return cleanseInlinedVariable(DV, VMContext); return cleanseInlinedVariable(DV, VMContext);
// Insert inlined scope. // Insert inlined scope.
SmallVector<Value *, 8> Elts; SmallVector<Metadata *, 8> Elts;
for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I) for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
Elts.push_back(DV->getOperand(I)); Elts.push_back(DV->getOperand(I));
Elts.push_back(InlinedScope); Elts.push_back(InlinedScope);
@ -891,7 +870,7 @@ DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
return DIVariable(DV); return DIVariable(DV);
// Remove inlined scope. // Remove inlined scope.
SmallVector<Value *, 8> Elts; SmallVector<Metadata *, 8> Elts;
for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I) for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
Elts.push_back(DV->getOperand(I)); Elts.push_back(DV->getOperand(I));
@ -923,7 +902,7 @@ DISubprogram llvm::getDISubprogram(const Function *F) {
if (Inst == BB.end()) if (Inst == BB.end())
continue; continue;
DebugLoc DLoc = Inst->getDebugLoc(); DebugLoc DLoc = Inst->getDebugLoc();
const MDNode *Scope = DLoc.getScopeNode(F->getParent()->getContext()); const MDNode *Scope = DLoc.getScopeNode();
DISubprogram Subprogram = getDISubprogram(Scope); DISubprogram Subprogram = getDISubprogram(Scope);
return Subprogram.describes(F) ? Subprogram : DISubprogram(); return Subprogram.describes(F) ? Subprogram : DISubprogram();
} }
@ -1533,10 +1512,10 @@ bool llvm::StripDebugInfo(Module &M) {
} }
unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
Value *Val = M.getModuleFlag("Debug Info Version"); if (auto *Val = mdconst::extract_or_null<ConstantInt>(
if (!Val) M.getModuleFlag("Debug Info Version")))
return 0; return Val->getZExtValue();
return cast<ConstantInt>(Val)->getZExtValue(); return 0;
} }
llvm::DenseMap<const llvm::Function *, llvm::DISubprogram> llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>

View File

@ -17,67 +17,29 @@ using namespace llvm;
// DebugLoc Implementation // DebugLoc Implementation
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
MDNode *DebugLoc::getScope(const LLVMContext &Ctx) const { unsigned DebugLoc::getLine() const { return DILocation(Loc).getLineNumber(); }
if (ScopeIdx == 0) return nullptr; unsigned DebugLoc::getCol() const { return DILocation(Loc).getColumnNumber(); }
if (ScopeIdx > 0) {
// Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
// position specified.
assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() &&
"Invalid ScopeIdx!");
return Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
}
// Otherwise, the index is in the ScopeInlinedAtRecords array.
assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
"Invalid ScopeIdx");
return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
}
MDNode *DebugLoc::getInlinedAt(const LLVMContext &Ctx) const { MDNode *DebugLoc::getScope() const { return DILocation(Loc).getScope(); }
// Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
// position specified. Zero is invalid. MDNode *DebugLoc::getInlinedAt() const {
if (ScopeIdx >= 0) return nullptr; return DILocation(Loc).getOrigLocation();
// Otherwise, the index is in the ScopeInlinedAtRecords array.
assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
"Invalid ScopeIdx");
return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
} }
/// Return both the Scope and the InlinedAt values. /// Return both the Scope and the InlinedAt values.
void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA, void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA) const {
const LLVMContext &Ctx) const { Scope = getScope();
if (ScopeIdx == 0) { IA = getInlinedAt();
Scope = IA = nullptr;
return;
}
if (ScopeIdx > 0) {
// Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
// position specified.
assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() &&
"Invalid ScopeIdx!");
Scope = Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
IA = nullptr;
return;
}
// Otherwise, the index is in the ScopeInlinedAtRecords array.
assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
"Invalid ScopeIdx");
Scope = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
IA = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
} }
MDNode *DebugLoc::getScopeNode(const LLVMContext &Ctx) const { MDNode *DebugLoc::getScopeNode() const {
if (MDNode *InlinedAt = getInlinedAt(Ctx)) if (MDNode *InlinedAt = getInlinedAt())
return DebugLoc::getFromDILocation(InlinedAt).getScopeNode(Ctx); return DebugLoc::getFromDILocation(InlinedAt).getScopeNode();
return getScope(Ctx); return getScope();
} }
DebugLoc DebugLoc::getFnDebugLoc(const LLVMContext &Ctx) const { DebugLoc DebugLoc::getFnDebugLoc() const {
const MDNode *Scope = getScopeNode(Ctx); const MDNode *Scope = getScopeNode();
DISubprogram SP = getDISubprogram(Scope); DISubprogram SP = getDISubprogram(Scope);
if (SP.isSubprogram()) if (SP.isSubprogram())
return DebugLoc::get(SP.getScopeLineNumber(), 0, SP); return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
@ -87,53 +49,32 @@ DebugLoc DebugLoc::getFnDebugLoc(const LLVMContext &Ctx) const {
DebugLoc DebugLoc::get(unsigned Line, unsigned Col, DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
MDNode *Scope, MDNode *InlinedAt) { MDNode *Scope, MDNode *InlinedAt) {
DebugLoc Result;
// If no scope is available, this is an unknown location. // If no scope is available, this is an unknown location.
if (!Scope) return Result; if (!Scope)
return DebugLoc();
// Saturate line and col to "unknown". // Saturate line and col to "unknown".
// FIXME: Allow 16-bits for columns.
if (Col > 255) Col = 0; if (Col > 255) Col = 0;
if (Line >= (1 << 24)) Line = 0; if (Line >= (1 << 24)) Line = 0;
Result.LineCol = Line | (Col << 24);
LLVMContext &Ctx = Scope->getContext();
// If there is no inlined-at location, use the ScopeRecords array.
if (!InlinedAt)
Result.ScopeIdx = Ctx.pImpl->getOrAddScopeRecordIdxEntry(Scope, 0);
else
Result.ScopeIdx = Ctx.pImpl->getOrAddScopeInlinedAtIdxEntry(Scope,
InlinedAt, 0);
return Result; LLVMContext &Context = Scope->getContext();
Type *Int32 = Type::getInt32Ty(Context);
Metadata *Elts[] = {ConstantAsMetadata::get(ConstantInt::get(Int32, Line)),
ConstantAsMetadata::get(ConstantInt::get(Int32, Col)),
Scope, InlinedAt};
return getFromDILocation(MDNode::get(Context, Elts));
} }
/// getAsMDNode - This method converts the compressed DebugLoc node into a /// getAsMDNode - This method converts the compressed DebugLoc node into a
/// DILocation-compatible MDNode. /// DILocation-compatible MDNode.
MDNode *DebugLoc::getAsMDNode(const LLVMContext &Ctx) const { MDNode *DebugLoc::getAsMDNode() const { return Loc; }
if (isUnknown()) return nullptr;
MDNode *Scope, *IA;
getScopeAndInlinedAt(Scope, IA, Ctx);
assert(Scope && "If scope is null, this should be isUnknown()");
LLVMContext &Ctx2 = Scope->getContext();
Type *Int32 = Type::getInt32Ty(Ctx2);
Value *Elts[] = {
ConstantInt::get(Int32, getLine()), ConstantInt::get(Int32, getCol()),
Scope, IA
};
return MDNode::get(Ctx2, Elts);
}
/// getFromDILocation - Translate the DILocation quad into a DebugLoc. /// getFromDILocation - Translate the DILocation quad into a DebugLoc.
DebugLoc DebugLoc::getFromDILocation(MDNode *N) { DebugLoc DebugLoc::getFromDILocation(MDNode *N) {
DILocation Loc(N); DebugLoc Loc;
MDNode *Scope = Loc.getScope(); Loc.Loc.reset(N);
if (!Scope) return DebugLoc(); return Loc;
return get(Loc.getLineNumber(), Loc.getColumnNumber(), Scope,
Loc.getOrigLocation());
} }
/// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc. /// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
@ -145,26 +86,26 @@ DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) {
nullptr); nullptr);
} }
void DebugLoc::dump(const LLVMContext &Ctx) const { void DebugLoc::dump() const {
#ifndef NDEBUG #ifndef NDEBUG
if (!isUnknown()) { if (!isUnknown()) {
dbgs() << getLine(); dbgs() << getLine();
if (getCol() != 0) if (getCol() != 0)
dbgs() << ',' << getCol(); dbgs() << ',' << getCol();
DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx)); DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt());
if (!InlinedAtDL.isUnknown()) { if (!InlinedAtDL.isUnknown()) {
dbgs() << " @ "; dbgs() << " @ ";
InlinedAtDL.dump(Ctx); InlinedAtDL.dump();
} else } else
dbgs() << "\n"; dbgs() << "\n";
} }
#endif #endif
} }
void DebugLoc::print(const LLVMContext &Ctx, raw_ostream &OS) const { void DebugLoc::print(raw_ostream &OS) const {
if (!isUnknown()) { if (!isUnknown()) {
// Print source line info. // Print source line info.
DIScope Scope(getScope(Ctx)); DIScope Scope(getScope());
assert((!Scope || Scope.isScope()) && assert((!Scope || Scope.isScope()) &&
"Scope of a DebugLoc should be null or a DIScope."); "Scope of a DebugLoc should be null or a DIScope.");
if (Scope) if (Scope)
@ -174,179 +115,11 @@ void DebugLoc::print(const LLVMContext &Ctx, raw_ostream &OS) const {
OS << ':' << getLine(); OS << ':' << getLine();
if (getCol() != 0) if (getCol() != 0)
OS << ':' << getCol(); OS << ':' << getCol();
DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx)); DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt());
if (!InlinedAtDL.isUnknown()) { if (!InlinedAtDL.isUnknown()) {
OS << " @[ "; OS << " @[ ";
InlinedAtDL.print(Ctx, OS); InlinedAtDL.print(OS);
OS << " ]"; OS << " ]";
} }
} }
} }
//===----------------------------------------------------------------------===//
// DenseMap specialization
//===----------------------------------------------------------------------===//
unsigned DenseMapInfo<DebugLoc>::getHashValue(const DebugLoc &Key) {
return static_cast<unsigned>(hash_combine(Key.LineCol, Key.ScopeIdx));
}
//===----------------------------------------------------------------------===//
// LLVMContextImpl Implementation
//===----------------------------------------------------------------------===//
int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode *Scope,
int ExistingIdx) {
// If we already have an entry for this scope, return it.
int &Idx = ScopeRecordIdx[Scope];
if (Idx) return Idx;
// If we don't have an entry, but ExistingIdx is specified, use it.
if (ExistingIdx)
return Idx = ExistingIdx;
// Otherwise add a new entry.
// Start out ScopeRecords with a minimal reasonable size to avoid
// excessive reallocation starting out.
if (ScopeRecords.empty())
ScopeRecords.reserve(128);
// Index is biased by 1 for index.
Idx = ScopeRecords.size()+1;
ScopeRecords.push_back(DebugRecVH(Scope, this, Idx));
return Idx;
}
int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,
int ExistingIdx) {
// If we already have an entry, return it.
int &Idx = ScopeInlinedAtIdx[std::make_pair(Scope, IA)];
if (Idx) return Idx;
// If we don't have an entry, but ExistingIdx is specified, use it.
if (ExistingIdx)
return Idx = ExistingIdx;
// Start out ScopeInlinedAtRecords with a minimal reasonable size to avoid
// excessive reallocation starting out.
if (ScopeInlinedAtRecords.empty())
ScopeInlinedAtRecords.reserve(128);
// Index is biased by 1 and negated.
Idx = -ScopeInlinedAtRecords.size()-1;
ScopeInlinedAtRecords.push_back(std::make_pair(DebugRecVH(Scope, this, Idx),
DebugRecVH(IA, this, Idx)));
return Idx;
}
//===----------------------------------------------------------------------===//
// DebugRecVH Implementation
//===----------------------------------------------------------------------===//
/// deleted - The MDNode this is pointing to got deleted, so this pointer needs
/// to drop to null and we need remove our entry from the DenseMap.
void DebugRecVH::deleted() {
// If this is a non-canonical reference, just drop the value to null, we know
// it doesn't have a map entry.
if (Idx == 0) {
setValPtr(nullptr);
return;
}
MDNode *Cur = get();
// If the index is positive, it is an entry in ScopeRecords.
if (Idx > 0) {
assert(Ctx->ScopeRecordIdx[Cur] == Idx && "Mapping out of date!");
Ctx->ScopeRecordIdx.erase(Cur);
// Reset this VH to null and we're done.
setValPtr(nullptr);
Idx = 0;
return;
}
// Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
// is the scope or the inlined-at record entry.
assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
assert((this == &Entry.first || this == &Entry.second) &&
"Mapping out of date!");
MDNode *OldScope = Entry.first.get();
MDNode *OldInlinedAt = Entry.second.get();
assert(OldScope && OldInlinedAt &&
"Entry should be non-canonical if either val dropped to null");
// Otherwise, we do have an entry in it, nuke it and we're done.
assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
"Mapping out of date");
Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
// Reset this VH to null. Drop both 'Idx' values to null to indicate that
// we're in non-canonical form now.
setValPtr(nullptr);
Entry.first.Idx = Entry.second.Idx = 0;
}
void DebugRecVH::allUsesReplacedWith(Value *NewVa) {
// If being replaced with a non-mdnode value (e.g. undef) handle this as if
// the mdnode got deleted.
MDNode *NewVal = dyn_cast<MDNode>(NewVa);
if (!NewVal) return deleted();
// If this is a non-canonical reference, just change it, we know it already
// doesn't have a map entry.
if (Idx == 0) {
setValPtr(NewVa);
return;
}
MDNode *OldVal = get();
assert(OldVal != NewVa && "Node replaced with self?");
// If the index is positive, it is an entry in ScopeRecords.
if (Idx > 0) {
assert(Ctx->ScopeRecordIdx[OldVal] == Idx && "Mapping out of date!");
Ctx->ScopeRecordIdx.erase(OldVal);
setValPtr(NewVal);
int NewEntry = Ctx->getOrAddScopeRecordIdxEntry(NewVal, Idx);
// If NewVal already has an entry, this becomes a non-canonical reference,
// just drop Idx to 0 to signify this.
if (NewEntry != Idx)
Idx = 0;
return;
}
// Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
// is the scope or the inlined-at record entry.
assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
assert((this == &Entry.first || this == &Entry.second) &&
"Mapping out of date!");
MDNode *OldScope = Entry.first.get();
MDNode *OldInlinedAt = Entry.second.get();
assert(OldScope && OldInlinedAt &&
"Entry should be non-canonical if either val dropped to null");
// Otherwise, we do have an entry in it, nuke it and we're done.
assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
"Mapping out of date");
Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
// Reset this VH to the new value.
setValPtr(NewVal);
int NewIdx = Ctx->getOrAddScopeInlinedAtIdxEntry(Entry.first.get(),
Entry.second.get(), Idx);
// If NewVal already has an entry, this becomes a non-canonical reference,
// just drop Idx to 0 to signify this.
if (NewIdx != Idx) {
std::pair<DebugRecVH, DebugRecVH> &Entry=Ctx->ScopeInlinedAtRecords[-Idx-1];
Entry.first.Idx = Entry.second.Idx = 0;
}
}

View File

@ -98,7 +98,8 @@ DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(const Instruction &I,
Instr(&I) { Instr(&I) {
if (const MDNode *SrcLoc = I.getMetadata("srcloc")) { if (const MDNode *SrcLoc = I.getMetadata("srcloc")) {
if (SrcLoc->getNumOperands() != 0) if (SrcLoc->getNumOperands() != 0)
if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0))) if (const auto *CI =
mdconst::dyn_extract<ConstantInt>(SrcLoc->getOperand(0)))
LocCookie = CI->getZExtValue(); LocCookie = CI->getZExtValue();
} }
} }

View File

@ -796,11 +796,8 @@ void BranchInst::swapSuccessors() {
return; return;
// The first operand is the name. Fetch them backwards and build a new one. // The first operand is the name. Fetch them backwards and build a new one.
Value *Ops[] = { Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
ProfileData->getOperand(0), ProfileData->getOperand(1)};
ProfileData->getOperand(2),
ProfileData->getOperand(1)
};
setMetadata(LLVMContext::MD_prof, setMetadata(LLVMContext::MD_prof,
MDNode::get(ProfileData->getContext(), Ops)); MDNode::get(ProfileData->getContext(), Ops));
} }
@ -2076,7 +2073,7 @@ float FPMathOperator::getFPAccuracy() const {
cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath); cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
if (!MD) if (!MD)
return 0.0; return 0.0;
ConstantFP *Accuracy = cast<ConstantFP>(MD->getOperand(0)); ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
return Accuracy->getValueAPF().convertToFloat(); return Accuracy->getValueAPF().convertToFloat();
} }

View File

@ -49,15 +49,25 @@ Value *DbgInfoIntrinsic::StripCast(Value *C) {
return dyn_cast<GlobalVariable>(C); return dyn_cast<GlobalVariable>(C);
} }
static Value *getValueImpl(Value *Op) {
auto *MD = cast<MetadataAsValue>(Op)->getMetadata();
if (auto *V = dyn_cast<ValueAsMetadata>(MD))
return V->getValue();
// When the value goes to null, it gets replaced by an empty MDNode.
assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode");
return nullptr;
}
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// DbgDeclareInst - This represents the llvm.dbg.declare instruction. /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
/// ///
Value *DbgDeclareInst::getAddress() const { Value *DbgDeclareInst::getAddress() const {
if (MDNode* MD = cast_or_null<MDNode>(getArgOperand(0))) if (!getArgOperand(0))
return MD->getOperand(0);
else
return nullptr; return nullptr;
return getValueImpl(getArgOperand(0));
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -65,9 +75,7 @@ Value *DbgDeclareInst::getAddress() const {
/// ///
const Value *DbgValueInst::getValue() const { const Value *DbgValueInst::getValue() const {
return cast<MDNode>(getArgOperand(0))->getOperand(0); return const_cast<DbgValueInst *>(this)->getValue();
} }
Value *DbgValueInst::getValue() { Value *DbgValueInst::getValue() { return getValueImpl(getArgOperand(0)); }
return cast<MDNode>(getArgOperand(0))->getOperand(0);
}

View File

@ -120,6 +120,21 @@ LLVMContextImpl::~LLVMContextImpl() {
delete &*Elem; delete &*Elem;
} }
// Destroy MetadataAsValues.
{
SmallVector<MetadataAsValue *, 8> MDVs;
MDVs.reserve(MetadataAsValues.size());
for (auto &Pair : MetadataAsValues)
MDVs.push_back(Pair.second);
MetadataAsValues.clear();
for (auto *V : MDVs)
delete V;
}
// Destroy ValuesAsMetadata.
for (auto &Pair : ValuesAsMetadata)
delete Pair.second;
// Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet
// and the NonUniquedMDNodes sets, so copy the values out first. // and the NonUniquedMDNodes sets, so copy the values out first.
SmallVector<GenericMDNode *, 8> MDNodes; SmallVector<GenericMDNode *, 8> MDNodes;

View File

@ -172,29 +172,29 @@ struct FunctionTypeKeyInfo {
/// the operands. /// the operands.
struct GenericMDNodeInfo { struct GenericMDNodeInfo {
struct KeyTy { struct KeyTy {
ArrayRef<Value *> Ops; ArrayRef<Metadata *> RawOps;
ArrayRef<MDOperand> Ops;
unsigned Hash; unsigned Hash;
KeyTy(ArrayRef<Value *> Ops) KeyTy(ArrayRef<Metadata *> Ops)
: Ops(Ops), Hash(hash_combine_range(Ops.begin(), Ops.end())) {} : RawOps(Ops), Hash(hash_combine_range(Ops.begin(), Ops.end())) {}
KeyTy(GenericMDNode *N, SmallVectorImpl<Value *> &Storage) { KeyTy(GenericMDNode *N)
Storage.resize(N->getNumOperands()); : Ops(N->op_begin(), N->op_end()), Hash(N->getHash()) {}
for (unsigned I = 0, E = N->getNumOperands(); I != E; ++I)
Storage[I] = N->getOperand(I);
Ops = Storage;
Hash = hash_combine_range(Ops.begin(), Ops.end());
}
bool operator==(const GenericMDNode *RHS) const { bool operator==(const GenericMDNode *RHS) const {
if (RHS == getEmptyKey() || RHS == getTombstoneKey()) if (RHS == getEmptyKey() || RHS == getTombstoneKey())
return false; return false;
if (Hash != RHS->getHash() || Ops.size() != RHS->getNumOperands()) if (Hash != RHS->getHash())
return false; return false;
for (unsigned I = 0, E = Ops.size(); I != E; ++I) assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
if (Ops[I] != RHS->getOperand(I)) return RawOps.empty() ? compareOps(Ops, RHS) : compareOps(RawOps, RHS);
return false; }
return true; template <class T>
static bool compareOps(ArrayRef<T> Ops, const GenericMDNode *RHS) {
if (Ops.size() != RHS->getNumOperands())
return false;
return std::equal(Ops.begin(), Ops.end(), RHS->op_begin());
} }
}; };
static inline GenericMDNode *getEmptyKey() { static inline GenericMDNode *getEmptyKey() {
@ -215,29 +215,6 @@ struct GenericMDNodeInfo {
} }
}; };
/// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
/// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp.
class DebugRecVH : public CallbackVH {
/// Ctx - This is the LLVM Context being referenced.
LLVMContextImpl *Ctx;
/// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
/// this reference lives in. If this is zero, then it represents a
/// non-canonical entry that has no DenseMap value. This can happen due to
/// RAUW.
int Idx;
public:
DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
: CallbackVH(n), Ctx(ctx), Idx(idx) {}
MDNode *get() const {
return cast_or_null<MDNode>(getValPtr());
}
void deleted() override;
void allUsesReplacedWith(Value *VNew) override;
};
class LLVMContextImpl { class LLVMContextImpl {
public: public:
/// OwnedModules - The set of modules instantiated in this context, and which /// OwnedModules - The set of modules instantiated in this context, and which
@ -265,6 +242,8 @@ public:
FoldingSet<AttributeSetNode> AttrsSetNodes; FoldingSet<AttributeSetNode> AttrsSetNodes;
StringMap<MDString> MDStringCache; StringMap<MDString> MDStringCache;
DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
DenseSet<GenericMDNode *, GenericMDNodeInfo> MDNodeSet; DenseSet<GenericMDNode *, GenericMDNodeInfo> MDNodeSet;
@ -301,7 +280,8 @@ public:
ConstantInt *TheFalseVal; ConstantInt *TheFalseVal;
LeakDetectorImpl<Value> LLVMObjects; LeakDetectorImpl<Value> LLVMObjects;
LeakDetectorImpl<Metadata> LLVMMDObjects;
// Basic type instances. // Basic type instances.
Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy; Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy; Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
@ -335,32 +315,14 @@ public:
/// CustomMDKindNames - Map to hold the metadata string to ID mapping. /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
StringMap<unsigned> CustomMDKindNames; StringMap<unsigned> CustomMDKindNames;
typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy; typedef std::pair<unsigned, TrackingMDNodeRef> MDPairTy;
typedef SmallVector<MDPairTy, 2> MDMapTy; typedef SmallVector<MDPairTy, 2> MDMapTy;
/// MetadataStore - Collection of per-instruction metadata used in this /// MetadataStore - Collection of per-instruction metadata used in this
/// context. /// context.
DenseMap<const Instruction *, MDMapTy> MetadataStore; DenseMap<const Instruction *, MDMapTy> MetadataStore;
/// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
/// entry with no "inlined at" element.
DenseMap<MDNode*, int> ScopeRecordIdx;
/// ScopeRecords - These are the actual mdnodes (in a value handle) for an
/// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if
/// the MDNode is RAUW'd.
std::vector<DebugRecVH> ScopeRecords;
/// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
/// scope/inlined-at pair.
DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
/// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
/// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up
/// to date.
std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
/// DiscriminatorTable - This table maps file:line locations to an /// DiscriminatorTable - This table maps file:line locations to an
/// integer representing the next DWARF path discriminator to assign to /// integer representing the next DWARF path discriminator to assign to
/// instructions in different blocks at the same location. /// instructions in different blocks at the same location.

View File

@ -21,11 +21,16 @@ MDString *MDBuilder::createString(StringRef Str) {
return MDString::get(Context, Str); return MDString::get(Context, Str);
} }
ConstantAsMetadata *MDBuilder::createConstant(Constant *C) {
return ConstantAsMetadata::get(C);
}
MDNode *MDBuilder::createFPMath(float Accuracy) { MDNode *MDBuilder::createFPMath(float Accuracy) {
if (Accuracy == 0.0) if (Accuracy == 0.0)
return nullptr; return nullptr;
assert(Accuracy > 0.0 && "Invalid fpmath accuracy!"); assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy); auto *Op =
createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy));
return MDNode::get(Context, Op); return MDNode::get(Context, Op);
} }
@ -38,12 +43,12 @@ MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) { MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
assert(Weights.size() >= 2 && "Need at least two branch weights!"); assert(Weights.size() >= 2 && "Need at least two branch weights!");
SmallVector<Value *, 4> Vals(Weights.size() + 1); SmallVector<Metadata *, 4> Vals(Weights.size() + 1);
Vals[0] = createString("branch_weights"); Vals[0] = createString("branch_weights");
Type *Int32Ty = Type::getInt32Ty(Context); Type *Int32Ty = Type::getInt32Ty(Context);
for (unsigned i = 0, e = Weights.size(); i != e; ++i) for (unsigned i = 0, e = Weights.size(); i != e; ++i)
Vals[i + 1] = ConstantInt::get(Int32Ty, Weights[i]); Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));
return MDNode::get(Context, Vals); return MDNode::get(Context, Vals);
} }
@ -56,7 +61,8 @@ MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
// Return the range [Lo, Hi). // Return the range [Lo, Hi).
Type *Ty = IntegerType::get(Context, Lo.getBitWidth()); Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
Value *Range[2] = {ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi)}; Metadata *Range[2] = {createConstant(ConstantInt::get(Ty, Lo)),
createConstant(ConstantInt::get(Ty, Hi))};
return MDNode::get(Context, Range); return MDNode::get(Context, Range);
} }
@ -64,7 +70,7 @@ MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
// To ensure uniqueness the root node is self-referential. // To ensure uniqueness the root node is self-referential.
MDNode *Dummy = MDNode::getTemporary(Context, None); MDNode *Dummy = MDNode::getTemporary(Context, None);
SmallVector<Value *, 3> Args(1, Dummy); SmallVector<Metadata *, 3> Args(1, Dummy);
if (Extra) if (Extra)
Args.push_back(Extra); Args.push_back(Extra);
if (!Name.empty()) if (!Name.empty())
@ -92,10 +98,10 @@ MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
bool isConstant) { bool isConstant) {
if (isConstant) { if (isConstant) {
Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1); Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
Value *Ops[3] = {createString(Name), Parent, Flags}; Metadata *Ops[3] = {createString(Name), Parent, createConstant(Flags)};
return MDNode::get(Context, Ops); return MDNode::get(Context, Ops);
} else { } else {
Value *Ops[2] = {createString(Name), Parent}; Metadata *Ops[2] = {createString(Name), Parent};
return MDNode::get(Context, Ops); return MDNode::get(Context, Ops);
} }
} }
@ -105,18 +111,18 @@ MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
} }
MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) { MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
Value *Ops[2] = { createString(Name), Domain }; Metadata *Ops[2] = {createString(Name), Domain};
return MDNode::get(Context, Ops); return MDNode::get(Context, Ops);
} }
/// \brief Return metadata for a tbaa.struct node with the given /// \brief Return metadata for a tbaa.struct node with the given
/// struct field descriptions. /// struct field descriptions.
MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) { MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
SmallVector<Value *, 4> Vals(Fields.size() * 3); SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
Type *Int64 = Type::getInt64Ty(Context); Type *Int64 = Type::getInt64Ty(Context);
for (unsigned i = 0, e = Fields.size(); i != e; ++i) { for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
Vals[i * 3 + 0] = ConstantInt::get(Int64, Fields[i].Offset); Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
Vals[i * 3 + 1] = ConstantInt::get(Int64, Fields[i].Size); Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
Vals[i * 3 + 2] = Fields[i].TBAA; Vals[i * 3 + 2] = Fields[i].TBAA;
} }
return MDNode::get(Context, Vals); return MDNode::get(Context, Vals);
@ -126,12 +132,12 @@ MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
/// with the given name, a list of pairs (offset, field type in the type DAG). /// with the given name, a list of pairs (offset, field type in the type DAG).
MDNode *MDBuilder::createTBAAStructTypeNode( MDNode *MDBuilder::createTBAAStructTypeNode(
StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) { StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
SmallVector<Value *, 4> Ops(Fields.size() * 2 + 1); SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
Type *Int64 = Type::getInt64Ty(Context); Type *Int64 = Type::getInt64Ty(Context);
Ops[0] = createString(Name); Ops[0] = createString(Name);
for (unsigned i = 0, e = Fields.size(); i != e; ++i) { for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
Ops[i * 2 + 1] = Fields[i].first; Ops[i * 2 + 1] = Fields[i].first;
Ops[i * 2 + 2] = ConstantInt::get(Int64, Fields[i].second); Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
} }
return MDNode::get(Context, Ops); return MDNode::get(Context, Ops);
} }
@ -141,7 +147,7 @@ MDNode *MDBuilder::createTBAAStructTypeNode(
MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
uint64_t Offset) { uint64_t Offset) {
ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset); ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
Value *Ops[3] = {createString(Name), Parent, Off}; Metadata *Ops[3] = {createString(Name), Parent, createConstant(Off)};
return MDNode::get(Context, Ops); return MDNode::get(Context, Ops);
} }
@ -150,6 +156,7 @@ MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType, MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
uint64_t Offset) { uint64_t Offset) {
Type *Int64 = Type::getInt64Ty(Context); Type *Int64 = Type::getInt64Ty(Context);
Value *Ops[3] = {BaseType, AccessType, ConstantInt::get(Int64, Offset)}; Metadata *Ops[3] = {BaseType, AccessType,
createConstant(ConstantInt::get(Int64, Offset))};
return MDNode::get(Context, Ops); return MDNode::get(Context, Ops);
} }

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,58 @@
//===- MetadataTracking.cpp - Implement metadata tracking -----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements Metadata tracking.
//
//===----------------------------------------------------------------------===//
#include "llvm/IR/MetadataTracking.h"
#include "llvm/IR/Metadata.h"
using namespace llvm;
ReplaceableMetadataImpl *ReplaceableMetadataImpl::get(Metadata &MD) {
if (auto *N = dyn_cast<MDNode>(&MD)) {
if (auto *G = dyn_cast<GenericMDNode>(N))
return G->ReplaceableUses.get();
return cast<MDNodeFwdDecl>(N);
}
return dyn_cast<ValueAsMetadata>(&MD);
}
bool MetadataTracking::track(void *Ref, Metadata &MD, OwnerTy Owner) {
assert(Ref && "Expected live reference");
assert((Owner || *static_cast<Metadata **>(Ref) == &MD) &&
"Reference without owner must be direct");
if (auto *R = ReplaceableMetadataImpl::get(MD)) {
R->addRef(Ref, Owner);
return true;
}
return false;
}
void MetadataTracking::untrack(void *Ref, Metadata &MD) {
assert(Ref && "Expected live reference");
if (auto *R = ReplaceableMetadataImpl::get(MD))
R->dropRef(Ref);
}
bool MetadataTracking::retrack(void *Ref, Metadata &MD, void *New) {
assert(Ref && "Expected live reference");
assert(New && "Expected live reference");
assert(Ref != New && "Expected change");
if (auto *R = ReplaceableMetadataImpl::get(MD)) {
R->moveRef(Ref, New, MD);
return true;
}
return false;
}
bool MetadataTracking::isReplaceable(const Metadata &MD) {
return ReplaceableMetadataImpl::get(const_cast<Metadata &>(MD));
}

View File

@ -260,8 +260,8 @@ void Module::eraseNamedMetadata(NamedMDNode *NMD) {
NamedMDList.erase(NMD); NamedMDList.erase(NMD);
} }
bool Module::isValidModFlagBehavior(Value *V, ModFlagBehavior &MFB) { bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) {
if (ConstantInt *Behavior = dyn_cast<ConstantInt>(V)) { if (ConstantInt *Behavior = mdconst::dyn_extract<ConstantInt>(MD)) {
uint64_t Val = Behavior->getLimitedValue(); uint64_t Val = Behavior->getLimitedValue();
if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) { if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) {
MFB = static_cast<ModFlagBehavior>(Val); MFB = static_cast<ModFlagBehavior>(Val);
@ -285,7 +285,7 @@ getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
// Check the operands of the MDNode before accessing the operands. // Check the operands of the MDNode before accessing the operands.
// The verifier will actually catch these failures. // The verifier will actually catch these failures.
MDString *Key = cast<MDString>(Flag->getOperand(1)); MDString *Key = cast<MDString>(Flag->getOperand(1));
Value *Val = Flag->getOperand(2); Metadata *Val = Flag->getOperand(2);
Flags.push_back(ModuleFlagEntry(MFB, Key, Val)); Flags.push_back(ModuleFlagEntry(MFB, Key, Val));
} }
} }
@ -293,7 +293,7 @@ getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
/// Return the corresponding value if Key appears in module flags, otherwise /// Return the corresponding value if Key appears in module flags, otherwise
/// return null. /// return null.
Value *Module::getModuleFlag(StringRef Key) const { Metadata *Module::getModuleFlag(StringRef Key) const {
SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags; SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
getModuleFlagsMetadata(ModuleFlags); getModuleFlagsMetadata(ModuleFlags);
for (const ModuleFlagEntry &MFE : ModuleFlags) { for (const ModuleFlagEntry &MFE : ModuleFlags) {
@ -321,13 +321,17 @@ NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
/// metadata. It will create the module-level flags named metadata if it doesn't /// metadata. It will create the module-level flags named metadata if it doesn't
/// already exist. /// already exist.
void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
Value *Val) { Metadata *Val) {
Type *Int32Ty = Type::getInt32Ty(Context); Type *Int32Ty = Type::getInt32Ty(Context);
Value *Ops[3] = { Metadata *Ops[3] = {
ConstantInt::get(Int32Ty, Behavior), MDString::get(Context, Key), Val ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)),
}; MDString::get(Context, Key), Val};
getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops)); getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
} }
void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
Constant *Val) {
addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
}
void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
uint32_t Val) { uint32_t Val) {
Type *Int32Ty = Type::getInt32Ty(Context); Type *Int32Ty = Type::getInt32Ty(Context);
@ -336,7 +340,7 @@ void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
void Module::addModuleFlag(MDNode *Node) { void Module::addModuleFlag(MDNode *Node) {
assert(Node->getNumOperands() == 3 && assert(Node->getNumOperands() == 3 &&
"Invalid number of operands for module flag!"); "Invalid number of operands for module flag!");
assert(isa<ConstantInt>(Node->getOperand(0)) && assert(mdconst::hasa<ConstantInt>(Node->getOperand(0)) &&
isa<MDString>(Node->getOperand(1)) && isa<MDString>(Node->getOperand(1)) &&
"Invalid operand types for module flag!"); "Invalid operand types for module flag!");
getOrInsertModuleFlagsMetadata()->addOperand(Node); getOrInsertModuleFlagsMetadata()->addOperand(Node);
@ -459,10 +463,10 @@ void Module::dropAllReferences() {
} }
unsigned Module::getDwarfVersion() const { unsigned Module::getDwarfVersion() const {
Value *Val = getModuleFlag("Dwarf Version"); auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version"));
if (!Val) if (!Val)
return dwarf::DWARF_VERSION; return dwarf::DWARF_VERSION;
return cast<ConstantInt>(Val)->getZExtValue(); return cast<ConstantInt>(Val->getValue())->getZExtValue();
} }
Comdat *Module::getOrInsertComdat(StringRef Name) { Comdat *Module::getOrInsertComdat(StringRef Name) {
@ -472,12 +476,13 @@ Comdat *Module::getOrInsertComdat(StringRef Name) {
} }
PICLevel::Level Module::getPICLevel() const { PICLevel::Level Module::getPICLevel() const {
Value *Val = getModuleFlag("PIC Level"); auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level"));
if (Val == NULL) if (Val == NULL)
return PICLevel::Default; return PICLevel::Default;
return static_cast<PICLevel::Level>(cast<ConstantInt>(Val)->getZExtValue()); return static_cast<PICLevel::Level>(
cast<ConstantInt>(Val->getValue())->getZExtValue());
} }
void Module::setPICLevel(PICLevel::Level PL) { void Module::setPICLevel(PICLevel::Level PL) {

View File

@ -125,8 +125,13 @@ void TypeFinder::incorporateType(Type *Ty) {
/// other ways. GlobalValues, basic blocks, instructions, and inst operands are /// other ways. GlobalValues, basic blocks, instructions, and inst operands are
/// all explicitly enumerated. /// all explicitly enumerated.
void TypeFinder::incorporateValue(const Value *V) { void TypeFinder::incorporateValue(const Value *V) {
if (const MDNode *M = dyn_cast<MDNode>(V)) if (const auto *M = dyn_cast<MetadataAsValue>(V)) {
return incorporateMDNode(M); if (const auto *N = dyn_cast<MDNode>(M->getMetadata()))
return incorporateMDNode(N);
if (const auto *MDV = dyn_cast<ValueAsMetadata>(M->getMetadata()))
return incorporateValue(MDV->getValue());
return;
}
if (!isa<Constant>(V) || isa<GlobalValue>(V)) return; if (!isa<Constant>(V) || isa<GlobalValue>(V)) return;
@ -152,11 +157,21 @@ void TypeFinder::incorporateValue(const Value *V) {
/// find types hiding within. /// find types hiding within.
void TypeFinder::incorporateMDNode(const MDNode *V) { void TypeFinder::incorporateMDNode(const MDNode *V) {
// Already visited? // Already visited?
if (!VisitedConstants.insert(V).second) if (!VisitedMetadata.insert(V).second)
return; return;
// Look in operands for types. // Look in operands for types.
for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i) for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i) {
if (Value *Op = V->getOperand(i)) Metadata *Op = V->getOperand(i);
incorporateValue(Op); if (!Op)
continue;
if (auto *N = dyn_cast<MDNode>(Op)) {
incorporateMDNode(N);
continue;
}
if (auto *C = dyn_cast<ConstantAsMetadata>(Op)) {
incorporateValue(C->getValue());
continue;
}
}
} }

View File

@ -44,9 +44,8 @@ static inline Type *checkType(Type *Ty) {
} }
Value::Value(Type *ty, unsigned scid) Value::Value(Type *ty, unsigned scid)
: VTy(checkType(ty)), UseList(nullptr), Name(nullptr), SubclassID(scid), : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), HasValueHandle(0),
HasValueHandle(0), SubclassOptionalData(0), SubclassData(0), SubclassOptionalData(0), SubclassData(0), NumOperands(0) {
NumOperands(0) {
// FIXME: Why isn't this in the subclass gunk?? // FIXME: Why isn't this in the subclass gunk??
// Note, we cannot call isa<CallInst> before the CallInst has been // Note, we cannot call isa<CallInst> before the CallInst has been
// constructed. // constructed.
@ -63,6 +62,8 @@ Value::~Value() {
// Notify all ValueHandles (if present) that this value is going away. // Notify all ValueHandles (if present) that this value is going away.
if (HasValueHandle) if (HasValueHandle)
ValueHandleBase::ValueIsDeleted(this); ValueHandleBase::ValueIsDeleted(this);
if (isUsedByMetadata())
ValueAsMetadata::handleDeletion(this);
#ifndef NDEBUG // Only in -g mode... #ifndef NDEBUG // Only in -g mode...
// Check to make sure that there are no uses of this value that are still // Check to make sure that there are no uses of this value that are still
@ -82,13 +83,19 @@ Value::~Value() {
// If this value is named, destroy the name. This should not be in a symtab // If this value is named, destroy the name. This should not be in a symtab
// at this point. // at this point.
if (Name && SubclassID != MDStringVal) destroyValueName();
Name->Destroy();
// There should be no uses of this object anymore, remove it. // There should be no uses of this object anymore, remove it.
LeakDetector::removeGarbageObject(this); LeakDetector::removeGarbageObject(this);
} }
void Value::destroyValueName() {
ValueName *Name = getValueName();
if (Name)
Name->Destroy();
setValueName(nullptr);
}
bool Value::hasNUses(unsigned N) const { bool Value::hasNUses(unsigned N) const {
const_use_iterator UI = use_begin(), E = use_end(); const_use_iterator UI = use_begin(), E = use_end();
@ -146,9 +153,7 @@ static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
} else if (Argument *A = dyn_cast<Argument>(V)) { } else if (Argument *A = dyn_cast<Argument>(V)) {
if (Function *P = A->getParent()) if (Function *P = A->getParent())
ST = &P->getValueSymbolTable(); ST = &P->getValueSymbolTable();
} else if (isa<MDString>(V)) } else {
return true;
else {
assert(isa<Constant>(V) && "Unknown value type!"); assert(isa<Constant>(V) && "Unknown value type!");
return true; // no name is setable for this. return true; // no name is setable for this.
} }
@ -159,14 +164,12 @@ StringRef Value::getName() const {
// Make sure the empty string is still a C string. For historical reasons, // Make sure the empty string is still a C string. For historical reasons,
// some clients want to call .data() on the result and expect it to be null // some clients want to call .data() on the result and expect it to be null
// terminated. // terminated.
if (!Name) return StringRef("", 0); if (!getValueName())
return Name->getKey(); return StringRef("", 0);
return getValueName()->getKey();
} }
void Value::setName(const Twine &NewName) { void Value::setName(const Twine &NewName) {
assert(SubclassID != MDStringVal &&
"Cannot set the name of MDString with this method!");
// Fast path for common IRBuilder case of setName("") when there is no name. // Fast path for common IRBuilder case of setName("") when there is no name.
if (NewName.isTriviallyEmpty() && !hasName()) if (NewName.isTriviallyEmpty() && !hasName())
return; return;
@ -193,20 +196,17 @@ void Value::setName(const Twine &NewName) {
if (!ST) { // No symbol table to update? Just do the change. if (!ST) { // No symbol table to update? Just do the change.
if (NameRef.empty()) { if (NameRef.empty()) {
// Free the name for this value. // Free the name for this value.
Name->Destroy(); destroyValueName();
Name = nullptr;
return; return;
} }
if (Name)
Name->Destroy();
// NOTE: Could optimize for the case the name is shrinking to not deallocate // NOTE: Could optimize for the case the name is shrinking to not deallocate
// then reallocated. // then reallocated.
destroyValueName();
// Create the new name. // Create the new name.
Name = ValueName::Create(NameRef); setValueName(ValueName::Create(NameRef));
Name->setValue(this); getValueName()->setValue(this);
return; return;
} }
@ -214,21 +214,18 @@ void Value::setName(const Twine &NewName) {
// then reallocated. // then reallocated.
if (hasName()) { if (hasName()) {
// Remove old name. // Remove old name.
ST->removeValueName(Name); ST->removeValueName(getValueName());
Name->Destroy(); destroyValueName();
Name = nullptr;
if (NameRef.empty()) if (NameRef.empty())
return; return;
} }
// Name is changing to something new. // Name is changing to something new.
Name = ST->createValueName(NameRef, this); setValueName(ST->createValueName(NameRef, this));
} }
void Value::takeName(Value *V) { void Value::takeName(Value *V) {
assert(SubclassID != MDStringVal && "Cannot take the name of an MDString!");
ValueSymbolTable *ST = nullptr; ValueSymbolTable *ST = nullptr;
// If this value has a name, drop it. // If this value has a name, drop it.
if (hasName()) { if (hasName()) {
@ -242,9 +239,8 @@ void Value::takeName(Value *V) {
// Remove old name. // Remove old name.
if (ST) if (ST)
ST->removeValueName(Name); ST->removeValueName(getValueName());
Name->Destroy(); destroyValueName();
Name = nullptr;
} }
// Now we know that this has no name. // Now we know that this has no name.
@ -270,9 +266,9 @@ void Value::takeName(Value *V) {
// This works even if both values have no symtab yet. // This works even if both values have no symtab yet.
if (ST == VST) { if (ST == VST) {
// Take the name! // Take the name!
Name = V->Name; setValueName(V->getValueName());
V->Name = nullptr; V->setValueName(nullptr);
Name->setValue(this); getValueName()->setValue(this);
return; return;
} }
@ -280,10 +276,10 @@ void Value::takeName(Value *V) {
// then reinsert it into ST. // then reinsert it into ST.
if (VST) if (VST)
VST->removeValueName(V->Name); VST->removeValueName(V->getValueName());
Name = V->Name; setValueName(V->getValueName());
V->Name = nullptr; V->setValueName(nullptr);
Name->setValue(this); getValueName()->setValue(this);
if (ST) if (ST)
ST->reinsertValue(this); ST->reinsertValue(this);
@ -334,6 +330,8 @@ void Value::replaceAllUsesWith(Value *New) {
// Notify all ValueHandles (if present) that this value is going away. // Notify all ValueHandles (if present) that this value is going away.
if (HasValueHandle) if (HasValueHandle)
ValueHandleBase::ValueIsRAUWd(this, New); ValueHandleBase::ValueIsRAUWd(this, New);
if (isUsedByMetadata())
ValueAsMetadata::handleRAUW(this, New);
while (!use_empty()) { while (!use_empty()) {
Use &U = *UseList; Use &U = *UseList;

View File

@ -38,8 +38,8 @@ void ValueSymbolTable::reinsertValue(Value* V) {
assert(V->hasName() && "Can't insert nameless Value into symbol table"); assert(V->hasName() && "Can't insert nameless Value into symbol table");
// Try inserting the name, assuming it won't conflict. // Try inserting the name, assuming it won't conflict.
if (vmap.insert(V->Name)) { if (vmap.insert(V->getValueName())) {
//DEBUG(dbgs() << " Inserted value: " << V->Name << ": " << *V << "\n"); //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n");
return; return;
} }
@ -47,8 +47,8 @@ void ValueSymbolTable::reinsertValue(Value* V) {
SmallString<256> UniqueName(V->getName().begin(), V->getName().end()); SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
// The name is too already used, just free it so we can allocate a new name. // The name is too already used, just free it so we can allocate a new name.
V->Name->Destroy(); V->getValueName()->Destroy();
unsigned BaseSize = UniqueName.size(); unsigned BaseSize = UniqueName.size();
while (1) { while (1) {
// Trim any suffix off and append the next number. // Trim any suffix off and append the next number.
@ -59,7 +59,7 @@ void ValueSymbolTable::reinsertValue(Value* V) {
auto IterBool = vmap.insert(std::make_pair(UniqueName, V)); auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
if (IterBool.second) { if (IterBool.second) {
// Newly inserted name. Success! // Newly inserted name. Success!
V->Name = &*IterBool.first; V->setValueName(&*IterBool.first);
//DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n"); //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n");
return; return;
} }

View File

@ -102,6 +102,13 @@ struct VerifierSupport {
} }
} }
void WriteMetadata(const Metadata *MD) {
if (!MD)
return;
MD->printAsOperand(OS, true, M);
OS << '\n';
}
void WriteType(Type *T) { void WriteType(Type *T) {
if (!T) if (!T)
return; return;
@ -128,6 +135,24 @@ struct VerifierSupport {
Broken = true; Broken = true;
} }
void CheckFailed(const Twine &Message, const Metadata *V1, const Metadata *V2,
const Metadata *V3 = nullptr, const Metadata *V4 = nullptr) {
OS << Message.str() << "\n";
WriteMetadata(V1);
WriteMetadata(V2);
WriteMetadata(V3);
WriteMetadata(V4);
Broken = true;
}
void CheckFailed(const Twine &Message, const Metadata *V1,
const Value *V2 = nullptr) {
OS << Message.str() << "\n";
WriteMetadata(V1);
WriteValue(V2);
Broken = true;
}
void CheckFailed(const Twine &Message, const Value *V1, Type *T2, void CheckFailed(const Twine &Message, const Value *V1, Type *T2,
const Value *V3 = nullptr) { const Value *V3 = nullptr) {
OS << Message.str() << "\n"; OS << Message.str() << "\n";
@ -167,7 +192,7 @@ class Verifier : public InstVisitor<Verifier>, VerifierSupport {
SmallPtrSet<Instruction *, 16> InstsInThisBlock; SmallPtrSet<Instruction *, 16> InstsInThisBlock;
/// \brief Keep track of the metadata nodes that have been checked already. /// \brief Keep track of the metadata nodes that have been checked already.
SmallPtrSet<MDNode *, 32> MDNodes; SmallPtrSet<Metadata *, 32> MDNodes;
/// \brief The personality function referenced by the LandingPadInsts. /// \brief The personality function referenced by the LandingPadInsts.
/// All LandingPadInsts within the same function must use the same /// All LandingPadInsts within the same function must use the same
@ -261,7 +286,9 @@ private:
void visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias *> &Visited, void visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias *> &Visited,
const GlobalAlias &A, const Constant &C); const GlobalAlias &A, const Constant &C);
void visitNamedMDNode(const NamedMDNode &NMD); void visitNamedMDNode(const NamedMDNode &NMD);
void visitMDNode(MDNode &MD, Function *F); void visitMDNode(MDNode &MD);
void visitMetadataAsValue(MetadataAsValue &MD, Function *F);
void visitValueAsMetadata(ValueAsMetadata &MD, Function *F);
void visitComdat(const Comdat &C); void visitComdat(const Comdat &C);
void visitModuleIdents(const Module &M); void visitModuleIdents(const Module &M);
void visitModuleFlags(const Module &M); void visitModuleFlags(const Module &M);
@ -560,46 +587,77 @@ void Verifier::visitNamedMDNode(const NamedMDNode &NMD) {
if (!MD) if (!MD)
continue; continue;
Assert1(!MD->isFunctionLocal(), visitMDNode(*MD);
"Named metadata operand cannot be function local!", MD);
visitMDNode(*MD, nullptr);
} }
} }
void Verifier::visitMDNode(MDNode &MD, Function *F) { void Verifier::visitMDNode(MDNode &MD) {
// Only visit each node once. Metadata can be mutually recursive, so this // Only visit each node once. Metadata can be mutually recursive, so this
// avoids infinite recursion here, as well as being an optimization. // avoids infinite recursion here, as well as being an optimization.
if (!MDNodes.insert(&MD).second) if (!MDNodes.insert(&MD).second)
return; return;
for (unsigned i = 0, e = MD.getNumOperands(); i != e; ++i) { for (unsigned i = 0, e = MD.getNumOperands(); i != e; ++i) {
Value *Op = MD.getOperand(i); Metadata *Op = MD.getOperand(i);
if (!Op) if (!Op)
continue; continue;
if (isa<Constant>(Op) || isa<MDString>(Op)) Assert2(!isa<LocalAsMetadata>(Op), "Invalid operand for global metadata!",
continue; &MD, Op);
if (MDNode *N = dyn_cast<MDNode>(Op)) { if (auto *N = dyn_cast<MDNode>(Op)) {
Assert2(MD.isFunctionLocal() || !N->isFunctionLocal(), visitMDNode(*N);
"Global metadata operand cannot be function local!", &MD, N); continue;
visitMDNode(*N, F); }
if (auto *V = dyn_cast<ValueAsMetadata>(Op)) {
visitValueAsMetadata(*V, nullptr);
continue; continue;
} }
Assert2(MD.isFunctionLocal(), "Invalid operand for global metadata!", &MD, Op);
// If this was an instruction, bb, or argument, verify that it is in the
// function that we expect.
Function *ActualF = nullptr;
if (Instruction *I = dyn_cast<Instruction>(Op))
ActualF = I->getParent()->getParent();
else if (BasicBlock *BB = dyn_cast<BasicBlock>(Op))
ActualF = BB->getParent();
else if (Argument *A = dyn_cast<Argument>(Op))
ActualF = A->getParent();
assert(ActualF && "Unimplemented function local metadata case!");
Assert2(ActualF == F, "function-local metadata used in wrong function",
&MD, Op);
} }
// Check these last, so we diagnose problems in operands first.
Assert1(!isa<MDNodeFwdDecl>(MD), "Expected no forward declarations!", &MD);
Assert1(MD.isResolved(), "All nodes should be resolved!", &MD);
}
void Verifier::visitValueAsMetadata(ValueAsMetadata &MD, Function *F) {
Assert1(MD.getValue(), "Expected valid value", &MD);
Assert2(!MD.getValue()->getType()->isMetadataTy(),
"Unexpected metadata round-trip through values", &MD, MD.getValue());
auto *L = dyn_cast<LocalAsMetadata>(&MD);
if (!L)
return;
Assert1(F, "function-local metadata used outside a function", L);
// If this was an instruction, bb, or argument, verify that it is in the
// function that we expect.
Function *ActualF = nullptr;
if (Instruction *I = dyn_cast<Instruction>(L->getValue())) {
Assert2(I->getParent(), "function-local metadata not in basic block", L, I);
ActualF = I->getParent()->getParent();
} else if (BasicBlock *BB = dyn_cast<BasicBlock>(L->getValue()))
ActualF = BB->getParent();
else if (Argument *A = dyn_cast<Argument>(L->getValue()))
ActualF = A->getParent();
assert(ActualF && "Unimplemented function local metadata case!");
Assert1(ActualF == F, "function-local metadata used in wrong function", L);
}
void Verifier::visitMetadataAsValue(MetadataAsValue &MDV, Function *F) {
Metadata *MD = MDV.getMetadata();
if (auto *N = dyn_cast<MDNode>(MD)) {
visitMDNode(*N);
return;
}
// Only visit each node once. Metadata can be mutually recursive, so this
// avoids infinite recursion here, as well as being an optimization.
if (!MDNodes.insert(MD).second)
return;
if (auto *V = dyn_cast<ValueAsMetadata>(MD))
visitValueAsMetadata(*V, F);
} }
void Verifier::visitComdat(const Comdat &C) { void Verifier::visitComdat(const Comdat &C) {
@ -650,7 +708,7 @@ void Verifier::visitModuleFlags(const Module &M) {
for (unsigned I = 0, E = Requirements.size(); I != E; ++I) { for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
const MDNode *Requirement = Requirements[I]; const MDNode *Requirement = Requirements[I];
const MDString *Flag = cast<MDString>(Requirement->getOperand(0)); const MDString *Flag = cast<MDString>(Requirement->getOperand(0));
const Value *ReqValue = Requirement->getOperand(1); const Metadata *ReqValue = Requirement->getOperand(1);
const MDNode *Op = SeenIDs.lookup(Flag); const MDNode *Op = SeenIDs.lookup(Flag);
if (!Op) { if (!Op) {
@ -679,7 +737,7 @@ Verifier::visitModuleFlag(const MDNode *Op,
Module::ModFlagBehavior MFB; Module::ModFlagBehavior MFB;
if (!Module::isValidModFlagBehavior(Op->getOperand(0), MFB)) { if (!Module::isValidModFlagBehavior(Op->getOperand(0), MFB)) {
Assert1( Assert1(
dyn_cast<ConstantInt>(Op->getOperand(0)), mdconst::dyn_extract<ConstantInt>(Op->getOperand(0)),
"invalid behavior operand in module flag (expected constant integer)", "invalid behavior operand in module flag (expected constant integer)",
Op->getOperand(0)); Op->getOperand(0));
Assert1(false, Assert1(false,
@ -1907,9 +1965,11 @@ void Verifier::visitRangeMetadata(Instruction& I,
ConstantRange LastRange(1); // Dummy initial value ConstantRange LastRange(1); // Dummy initial value
for (unsigned i = 0; i < NumRanges; ++i) { for (unsigned i = 0; i < NumRanges; ++i) {
ConstantInt *Low = dyn_cast<ConstantInt>(Range->getOperand(2*i)); ConstantInt *Low =
mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i));
Assert1(Low, "The lower limit must be an integer!", Low); Assert1(Low, "The lower limit must be an integer!", Low);
ConstantInt *High = dyn_cast<ConstantInt>(Range->getOperand(2*i + 1)); ConstantInt *High =
mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i + 1));
Assert1(High, "The upper limit must be an integer!", High); Assert1(High, "The upper limit must be an integer!", High);
Assert1(High->getType() == Low->getType() && Assert1(High->getType() == Low->getType() &&
High->getType() == Ty, "Range types must match instruction type!", High->getType() == Ty, "Range types must match instruction type!",
@ -1932,9 +1992,9 @@ void Verifier::visitRangeMetadata(Instruction& I,
} }
if (NumRanges > 2) { if (NumRanges > 2) {
APInt FirstLow = APInt FirstLow =
dyn_cast<ConstantInt>(Range->getOperand(0))->getValue(); mdconst::dyn_extract<ConstantInt>(Range->getOperand(0))->getValue();
APInt FirstHigh = APInt FirstHigh =
dyn_cast<ConstantInt>(Range->getOperand(1))->getValue(); mdconst::dyn_extract<ConstantInt>(Range->getOperand(1))->getValue();
ConstantRange FirstRange(FirstLow, FirstHigh); ConstantRange FirstRange(FirstLow, FirstHigh);
Assert1(FirstRange.intersectWith(LastRange).isEmptySet(), Assert1(FirstRange.intersectWith(LastRange).isEmptySet(),
"Intervals are overlapping", Range); "Intervals are overlapping", Range);
@ -2278,8 +2338,8 @@ void Verifier::visitInstruction(Instruction &I) {
Assert1(I.getType()->isFPOrFPVectorTy(), Assert1(I.getType()->isFPOrFPVectorTy(),
"fpmath requires a floating point result!", &I); "fpmath requires a floating point result!", &I);
Assert1(MD->getNumOperands() == 1, "fpmath takes one operand!", &I); Assert1(MD->getNumOperands() == 1, "fpmath takes one operand!", &I);
Value *Op0 = MD->getOperand(0); if (ConstantFP *CFP0 =
if (ConstantFP *CFP0 = dyn_cast_or_null<ConstantFP>(Op0)) { mdconst::dyn_extract_or_null<ConstantFP>(MD->getOperand(0))) {
APFloat Accuracy = CFP0->getValueAPF(); APFloat Accuracy = CFP0->getValueAPF();
Assert1(Accuracy.isFiniteNonZero() && !Accuracy.isNegative(), Assert1(Accuracy.isFiniteNonZero() && !Accuracy.isNegative(),
"fpmath accuracy not a positive number!", &I); "fpmath accuracy not a positive number!", &I);
@ -2496,8 +2556,8 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
// If the intrinsic takes MDNode arguments, verify that they are either global // If the intrinsic takes MDNode arguments, verify that they are either global
// or are local to *this* function. // or are local to *this* function.
for (unsigned i = 0, e = CI.getNumArgOperands(); i != e; ++i) for (unsigned i = 0, e = CI.getNumArgOperands(); i != e; ++i)
if (MDNode *MD = dyn_cast<MDNode>(CI.getArgOperand(i))) if (auto *MD = dyn_cast<MetadataAsValue>(CI.getArgOperand(i)))
visitMDNode(*MD, CI.getParent()->getParent()); visitMetadataAsValue(*MD, CI.getParent()->getParent());
switch (ID) { switch (ID) {
default: default:
@ -2509,11 +2569,8 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
"constant int", &CI); "constant int", &CI);
break; break;
case Intrinsic::dbg_declare: { // llvm.dbg.declare case Intrinsic::dbg_declare: { // llvm.dbg.declare
Assert1(CI.getArgOperand(0) && isa<MDNode>(CI.getArgOperand(0)), Assert1(CI.getArgOperand(0) && isa<MetadataAsValue>(CI.getArgOperand(0)),
"invalid llvm.dbg.declare intrinsic call 1", &CI); "invalid llvm.dbg.declare intrinsic call 1", &CI);
MDNode *MD = cast<MDNode>(CI.getArgOperand(0));
Assert1(MD->getNumOperands() == 1,
"invalid llvm.dbg.declare intrinsic call 2", &CI);
} break; } break;
case Intrinsic::memcpy: case Intrinsic::memcpy:
case Intrinsic::memmove: case Intrinsic::memmove:

View File

@ -604,7 +604,7 @@ bool LTOModule::parseSymbols(std::string &errMsg) {
/// parseMetadata - Parse metadata from the module /// parseMetadata - Parse metadata from the module
void LTOModule::parseMetadata() { void LTOModule::parseMetadata() {
// Linker Options // Linker Options
if (Value *Val = getModule().getModuleFlag("Linker Options")) { if (Metadata *Val = getModule().getModuleFlag("Linker Options")) {
MDNode *LinkerOptions = cast<MDNode>(Val); MDNode *LinkerOptions = cast<MDNode>(Val);
for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) { for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i)); MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));

View File

@ -1265,7 +1265,7 @@ bool ModuleLinker::linkModuleFlagsMetadata() {
SmallSetVector<MDNode*, 16> Requirements; SmallSetVector<MDNode*, 16> Requirements;
for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) { for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
MDNode *Op = DstModFlags->getOperand(I); MDNode *Op = DstModFlags->getOperand(I);
ConstantInt *Behavior = cast<ConstantInt>(Op->getOperand(0)); ConstantInt *Behavior = mdconst::extract<ConstantInt>(Op->getOperand(0));
MDString *ID = cast<MDString>(Op->getOperand(1)); MDString *ID = cast<MDString>(Op->getOperand(1));
if (Behavior->getZExtValue() == Module::Require) { if (Behavior->getZExtValue() == Module::Require) {
@ -1280,7 +1280,8 @@ bool ModuleLinker::linkModuleFlagsMetadata() {
bool HasErr = false; bool HasErr = false;
for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) { for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
MDNode *SrcOp = SrcModFlags->getOperand(I); MDNode *SrcOp = SrcModFlags->getOperand(I);
ConstantInt *SrcBehavior = cast<ConstantInt>(SrcOp->getOperand(0)); ConstantInt *SrcBehavior =
mdconst::extract<ConstantInt>(SrcOp->getOperand(0));
MDString *ID = cast<MDString>(SrcOp->getOperand(1)); MDString *ID = cast<MDString>(SrcOp->getOperand(1));
MDNode *DstOp = Flags.lookup(ID); MDNode *DstOp = Flags.lookup(ID);
unsigned SrcBehaviorValue = SrcBehavior->getZExtValue(); unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
@ -1303,7 +1304,8 @@ bool ModuleLinker::linkModuleFlagsMetadata() {
} }
// Otherwise, perform a merge. // Otherwise, perform a merge.
ConstantInt *DstBehavior = cast<ConstantInt>(DstOp->getOperand(0)); ConstantInt *DstBehavior =
mdconst::extract<ConstantInt>(DstOp->getOperand(0));
unsigned DstBehaviorValue = DstBehavior->getZExtValue(); unsigned DstBehaviorValue = DstBehavior->getZExtValue();
// If either flag has override behavior, handle it first. // If either flag has override behavior, handle it first.
@ -1317,7 +1319,7 @@ bool ModuleLinker::linkModuleFlagsMetadata() {
continue; continue;
} else if (SrcBehaviorValue == Module::Override) { } else if (SrcBehaviorValue == Module::Override) {
// Update the destination flag to that of the source. // Update the destination flag to that of the source.
DstOp->replaceOperandWith(0, SrcBehavior); DstOp->replaceOperandWith(0, ConstantAsMetadata::get(SrcBehavior));
DstOp->replaceOperandWith(2, SrcOp->getOperand(2)); DstOp->replaceOperandWith(2, SrcOp->getOperand(2));
continue; continue;
} }
@ -1352,29 +1354,26 @@ bool ModuleLinker::linkModuleFlagsMetadata() {
case Module::Append: { case Module::Append: {
MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2)); MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2)); MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
unsigned NumOps = DstValue->getNumOperands() + SrcValue->getNumOperands(); SmallVector<Metadata *, 8> MDs;
Value **VP, **Values = VP = new Value*[NumOps]; MDs.reserve(DstValue->getNumOperands() + SrcValue->getNumOperands());
for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i, ++VP) for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i)
*VP = DstValue->getOperand(i); MDs.push_back(DstValue->getOperand(i));
for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i, ++VP) for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i)
*VP = SrcValue->getOperand(i); MDs.push_back(SrcValue->getOperand(i));
DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(), DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(), MDs));
ArrayRef<Value*>(Values,
NumOps)));
delete[] Values;
break; break;
} }
case Module::AppendUnique: { case Module::AppendUnique: {
SmallSetVector<Value*, 16> Elts; SmallSetVector<Metadata *, 16> Elts;
MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2)); MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2)); MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i) for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i)
Elts.insert(DstValue->getOperand(i)); Elts.insert(DstValue->getOperand(i));
for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i) for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i)
Elts.insert(SrcValue->getOperand(i)); Elts.insert(SrcValue->getOperand(i));
DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(), DstOp->replaceOperandWith(
ArrayRef<Value*>(Elts.begin(), 2, MDNode::get(DstM->getContext(),
Elts.end()))); makeArrayRef(Elts.begin(), Elts.end())));
break; break;
} }
} }
@ -1384,7 +1383,7 @@ bool ModuleLinker::linkModuleFlagsMetadata() {
for (unsigned I = 0, E = Requirements.size(); I != E; ++I) { for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
MDNode *Requirement = Requirements[I]; MDNode *Requirement = Requirements[I];
MDString *Flag = cast<MDString>(Requirement->getOperand(0)); MDString *Flag = cast<MDString>(Requirement->getOperand(0));
Value *ReqValue = Requirement->getOperand(1); Metadata *ReqValue = Requirement->getOperand(1);
MDNode *Op = Flags[Flag]; MDNode *Op = Flags[Flag];
if (!Op || Op->getOperand(2) != ReqValue) { if (!Op || Op->getOperand(2) != ReqValue) {

View File

@ -798,7 +798,7 @@ void ARMAsmPrinter::emitAttributes() {
if (const Module *SourceModule = MMI->getModule()) { if (const Module *SourceModule = MMI->getModule()) {
// ABI_PCS_wchar_t to indicate wchar_t width // ABI_PCS_wchar_t to indicate wchar_t width
// FIXME: There is no way to emit value 0 (wchar_t prohibited). // FIXME: There is no way to emit value 0 (wchar_t prohibited).
if (auto WCharWidthValue = cast_or_null<ConstantInt>( if (auto WCharWidthValue = mdconst::extract_or_null<ConstantInt>(
SourceModule->getModuleFlag("wchar_size"))) { SourceModule->getModuleFlag("wchar_size"))) {
int WCharWidth = WCharWidthValue->getZExtValue(); int WCharWidth = WCharWidthValue->getZExtValue();
assert((WCharWidth == 2 || WCharWidth == 4) && assert((WCharWidth == 2 || WCharWidth == 4) &&
@ -809,7 +809,7 @@ void ARMAsmPrinter::emitAttributes() {
// ABI_enum_size to indicate enum width // ABI_enum_size to indicate enum width
// FIXME: There is no way to emit value 0 (enums prohibited) or value 3 // FIXME: There is no way to emit value 0 (enums prohibited) or value 3
// (all enums contain a value needing 32 bits to encode). // (all enums contain a value needing 32 bits to encode).
if (auto EnumWidthValue = cast_or_null<ConstantInt>( if (auto EnumWidthValue = mdconst::extract_or_null<ConstantInt>(
SourceModule->getModuleFlag("min_enum_size"))) { SourceModule->getModuleFlag("min_enum_size"))) {
int EnumWidth = EnumWidthValue->getZExtValue(); int EnumWidth = EnumWidthValue->getZExtValue();
assert((EnumWidth == 1 || EnumWidth == 4) && assert((EnumWidth == 1 || EnumWidth == 4) &&

View File

@ -394,31 +394,34 @@ void GenericToNVVM::remapNamedMDNode(Module *M, NamedMDNode *N) {
MDNode *GenericToNVVM::remapMDNode(Module *M, MDNode *N) { MDNode *GenericToNVVM::remapMDNode(Module *M, MDNode *N) {
bool OperandChanged = false; bool OperandChanged = false;
SmallVector<Value *, 8> NewOperands; SmallVector<Metadata *, 8> NewOperands;
unsigned NumOperands = N->getNumOperands(); unsigned NumOperands = N->getNumOperands();
// Check if any operand is or contains a global variable in GVMap, and thus // Check if any operand is or contains a global variable in GVMap, and thus
// converted to another value. // converted to another value.
for (unsigned i = 0; i < NumOperands; ++i) { for (unsigned i = 0; i < NumOperands; ++i) {
Value *Operand = N->getOperand(i); Metadata *Operand = N->getOperand(i);
Value *NewOperand = Operand; Metadata *NewOperand = Operand;
if (Operand) { if (Operand) {
if (isa<GlobalVariable>(Operand)) { if (auto *N = dyn_cast<MDNode>(Operand)) {
GVMapTy::iterator I = GVMap.find(cast<GlobalVariable>(Operand)); NewOperand = remapMDNode(M, N);
if (I != GVMap.end()) { } else if (auto *C = dyn_cast<ConstantAsMetadata>(Operand)) {
NewOperand = I->second; if (auto *G = dyn_cast<GlobalVariable>(C->getValue())) {
if (++i < NumOperands) { GVMapTy::iterator I = GVMap.find(G);
NewOperands.push_back(NewOperand); if (I != GVMap.end()) {
// Address space of the global variable follows the global variable NewOperand = ConstantAsMetadata::get(I->second);
// in the global variable debug info (see createGlobalVariable in if (++i < NumOperands) {
// lib/Analysis/DIBuilder.cpp). NewOperands.push_back(NewOperand);
NewOperand = // Address space of the global variable follows the global
ConstantInt::get(Type::getInt32Ty(M->getContext()), // variable
I->second->getType()->getAddressSpace()); // in the global variable debug info (see createGlobalVariable in
// lib/Analysis/DIBuilder.cpp).
NewOperand = ConstantAsMetadata::get(
ConstantInt::get(Type::getInt32Ty(M->getContext()),
I->second->getType()->getAddressSpace()));
}
} }
} }
} else if (isa<MDNode>(Operand)) {
NewOperand = remapMDNode(M, cast<MDNode>(Operand));
} }
} }
OperandChanged |= Operand != NewOperand; OperandChanged |= Operand != NewOperand;

View File

@ -52,7 +52,7 @@ static void cacheAnnotationFromMD(const MDNode *md, key_val_pair_t &retval) {
assert(prop && "Annotation property not a string"); assert(prop && "Annotation property not a string");
// value // value
ConstantInt *Val = dyn_cast<ConstantInt>(md->getOperand(i + 1)); ConstantInt *Val = mdconst::dyn_extract<ConstantInt>(md->getOperand(i + 1));
assert(Val && "Value operand not a constant int"); assert(Val && "Value operand not a constant int");
std::string keyname = prop->getString().str(); std::string keyname = prop->getString().str();
@ -75,7 +75,8 @@ static void cacheAnnotationFromMD(const Module *m, const GlobalValue *gv) {
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
const MDNode *elem = NMD->getOperand(i); const MDNode *elem = NMD->getOperand(i);
Value *entity = elem->getOperand(0); GlobalValue *entity =
mdconst::dyn_extract_or_null<GlobalValue>(elem->getOperand(0));
// entity may be null due to DCE // entity may be null due to DCE
if (!entity) if (!entity)
continue; continue;
@ -322,7 +323,7 @@ bool llvm::getAlign(const CallInst &I, unsigned index, unsigned &align) {
if (MDNode *alignNode = I.getMetadata("callalign")) { if (MDNode *alignNode = I.getMetadata("callalign")) {
for (int i = 0, n = alignNode->getNumOperands(); i < n; i++) { for (int i = 0, n = alignNode->getNumOperands(); i < n; i++) {
if (const ConstantInt *CI = if (const ConstantInt *CI =
dyn_cast<ConstantInt>(alignNode->getOperand(i))) { mdconst::dyn_extract<ConstantInt>(alignNode->getOperand(i))) {
unsigned v = CI->getZExtValue(); unsigned v = CI->getZExtValue();
if ((v >> 16) == index) { if ((v >> 16) == index) {
align = v & 0xFFFF; align = v & 0xFFFF;

View File

@ -301,8 +301,8 @@ bool StripDeadDebugInfo::runOnModule(Module &M) {
// For each compile unit, find the live set of global variables/functions and // For each compile unit, find the live set of global variables/functions and
// replace the current list of potentially dead global variables/functions // replace the current list of potentially dead global variables/functions
// with the live list. // with the live list.
SmallVector<Value *, 64> LiveGlobalVariables; SmallVector<Metadata *, 64> LiveGlobalVariables;
SmallVector<Value *, 64> LiveSubprograms; SmallVector<Metadata *, 64> LiveSubprograms;
DenseSet<const MDNode *> VisitedSet; DenseSet<const MDNode *> VisitedSet;
for (DICompileUnit DIC : F.compile_units()) { for (DICompileUnit DIC : F.compile_units()) {

View File

@ -119,15 +119,14 @@ Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) {
// If the memcpy has metadata describing the members, see if we can // If the memcpy has metadata describing the members, see if we can
// get the TBAA tag describing our copy. // get the TBAA tag describing our copy.
if (MDNode *M = MI->getMetadata(LLVMContext::MD_tbaa_struct)) { if (MDNode *M = MI->getMetadata(LLVMContext::MD_tbaa_struct)) {
if (M->getNumOperands() == 3 && if (M->getNumOperands() == 3 && M->getOperand(0) &&
M->getOperand(0) && mdconst::hasa<ConstantInt>(M->getOperand(0)) &&
isa<ConstantInt>(M->getOperand(0)) && mdconst::extract<ConstantInt>(M->getOperand(0))->isNullValue() &&
cast<ConstantInt>(M->getOperand(0))->isNullValue() &&
M->getOperand(1) && M->getOperand(1) &&
isa<ConstantInt>(M->getOperand(1)) && mdconst::hasa<ConstantInt>(M->getOperand(1)) &&
cast<ConstantInt>(M->getOperand(1))->getValue() == Size && mdconst::extract<ConstantInt>(M->getOperand(1))->getValue() ==
M->getOperand(2) && Size &&
isa<MDNode>(M->getOperand(2))) M->getOperand(2) && isa<MDNode>(M->getOperand(2)))
CopyMD = cast<MDNode>(M->getOperand(2)); CopyMD = cast<MDNode>(M->getOperand(2));
} }
} }
@ -1129,7 +1128,7 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
cast<Constant>(RHS)->isNullValue()) { cast<Constant>(RHS)->isNullValue()) {
LoadInst* LI = cast<LoadInst>(LHS); LoadInst* LI = cast<LoadInst>(LHS);
if (isValidAssumeForContext(II, LI, DL, DT)) { if (isValidAssumeForContext(II, LI, DL, DT)) {
MDNode* MD = MDNode::get(II->getContext(), ArrayRef<Value*>()); MDNode *MD = MDNode::get(II->getContext(), None);
LI->setMetadata(LLVMContext::MD_nonnull, MD); LI->setMetadata(LLVMContext::MD_nonnull, MD);
return EraseInstFromFunction(*II); return EraseInstFromFunction(*II);
} }

View File

@ -220,8 +220,10 @@ struct LocationMetadata {
assert(MDN->getNumOperands() == 3); assert(MDN->getNumOperands() == 3);
MDString *MDFilename = cast<MDString>(MDN->getOperand(0)); MDString *MDFilename = cast<MDString>(MDN->getOperand(0));
Filename = MDFilename->getString(); Filename = MDFilename->getString();
LineNo = cast<ConstantInt>(MDN->getOperand(1))->getLimitedValue(); LineNo =
ColumnNo = cast<ConstantInt>(MDN->getOperand(2))->getLimitedValue(); mdconst::extract<ConstantInt>(MDN->getOperand(1))->getLimitedValue();
ColumnNo =
mdconst::extract<ConstantInt>(MDN->getOperand(2))->getLimitedValue();
} }
}; };
@ -249,23 +251,22 @@ class GlobalsMetadata {
for (auto MDN : Globals->operands()) { for (auto MDN : Globals->operands()) {
// Metadata node contains the global and the fields of "Entry". // Metadata node contains the global and the fields of "Entry".
assert(MDN->getNumOperands() == 5); assert(MDN->getNumOperands() == 5);
Value *V = MDN->getOperand(0); auto *GV = mdconst::extract_or_null<GlobalVariable>(MDN->getOperand(0));
// The optimizer may optimize away a global entirely. // The optimizer may optimize away a global entirely.
if (!V) if (!GV)
continue; continue;
GlobalVariable *GV = cast<GlobalVariable>(V);
// We can already have an entry for GV if it was merged with another // We can already have an entry for GV if it was merged with another
// global. // global.
Entry &E = Entries[GV]; Entry &E = Entries[GV];
if (Value *Loc = MDN->getOperand(1)) if (auto *Loc = cast_or_null<MDNode>(MDN->getOperand(1)))
E.SourceLoc.parse(cast<MDNode>(Loc)); E.SourceLoc.parse(Loc);
if (Value *Name = MDN->getOperand(2)) { if (auto *Name = cast_or_null<MDString>(MDN->getOperand(2)))
MDString *MDName = cast<MDString>(Name); E.Name = Name->getString();
E.Name = MDName->getString(); ConstantInt *IsDynInit =
} mdconst::extract<ConstantInt>(MDN->getOperand(3));
ConstantInt *IsDynInit = cast<ConstantInt>(MDN->getOperand(3));
E.IsDynInit |= IsDynInit->isOne(); E.IsDynInit |= IsDynInit->isOne();
ConstantInt *IsBlacklisted = cast<ConstantInt>(MDN->getOperand(4)); ConstantInt *IsBlacklisted =
mdconst::extract<ConstantInt>(MDN->getOperand(4));
E.IsBlacklisted |= IsBlacklisted->isOne(); E.IsBlacklisted |= IsBlacklisted->isOne();
} }
} }
@ -496,9 +497,9 @@ struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
AllocaForValueMapTy AllocaForValue; AllocaForValueMapTy AllocaForValue;
FunctionStackPoisoner(Function &F, AddressSanitizer &ASan) FunctionStackPoisoner(Function &F, AddressSanitizer &ASan)
: F(F), ASan(ASan), DIB(*F.getParent()), C(ASan.C), : F(F), ASan(ASan), DIB(*F.getParent(), /*AllowUnresolved*/ false),
IntptrTy(ASan.IntptrTy), IntptrPtrTy(PointerType::get(IntptrTy, 0)), C(ASan.C), IntptrTy(ASan.IntptrTy),
Mapping(ASan.Mapping), IntptrPtrTy(PointerType::get(IntptrTy, 0)), Mapping(ASan.Mapping),
StackAlignment(1 << Mapping.Scale) {} StackAlignment(1 << Mapping.Scale) {}
bool runOnFunction() { bool runOnFunction() {

View File

@ -275,7 +275,7 @@ void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F,
Load->setAtomic(Monotonic); Load->setAtomic(Monotonic);
Load->setAlignment(1); Load->setAlignment(1);
Load->setMetadata(F.getParent()->getMDKindID("nosanitize"), Load->setMetadata(F.getParent()->getMDKindID("nosanitize"),
MDNode::get(*C, ArrayRef<llvm::Value *>())); MDNode::get(*C, None));
Value *Cmp = IRB.CreateICmpEQ(Constant::getNullValue(Int8Ty), Load); Value *Cmp = IRB.CreateICmpEQ(Constant::getNullValue(Int8Ty), Load);
Instruction *Ins = SplitBlockAndInsertIfThen( Instruction *Ins = SplitBlockAndInsertIfThen(
Cmp, IP, false, MDBuilder(*C).createBranchWeights(1, 100000)); Cmp, IP, false, MDBuilder(*C).createBranchWeights(1, 100000));

View File

@ -880,11 +880,9 @@ static void AppendMDNodeToInstForPtr(unsigned NodeId,
Sequence OldSeq, Sequence OldSeq,
Sequence NewSeq) { Sequence NewSeq) {
MDNode *Node = nullptr; MDNode *Node = nullptr;
Value *tmp[3] = {PtrSourceMDNodeID, Metadata *tmp[3] = {PtrSourceMDNodeID,
SequenceToMDString(Inst->getContext(), SequenceToMDString(Inst->getContext(), OldSeq),
OldSeq), SequenceToMDString(Inst->getContext(), NewSeq)};
SequenceToMDString(Inst->getContext(),
NewSeq)};
Node = MDNode::get(Inst->getContext(), tmp); Node = MDNode::get(Inst->getContext(), tmp);
Inst->setMetadata(NodeId, Node); Inst->setMetadata(NodeId, Node);

View File

@ -272,7 +272,8 @@ static unsigned UnrollCountPragmaValue(const Loop *L) {
if (MD) { if (MD) {
assert(MD->getNumOperands() == 2 && assert(MD->getNumOperands() == 2 &&
"Unroll count hint metadata should have two operands."); "Unroll count hint metadata should have two operands.");
unsigned Count = cast<ConstantInt>(MD->getOperand(1))->getZExtValue(); unsigned Count =
mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue();
assert(Count >= 1 && "Unroll count must be positive."); assert(Count >= 1 && "Unroll count must be positive.");
return Count; return Count;
} }
@ -288,9 +289,9 @@ static void SetLoopAlreadyUnrolled(Loop *L) {
if (!LoopID) return; if (!LoopID) return;
// First remove any existing loop unrolling metadata. // First remove any existing loop unrolling metadata.
SmallVector<Value *, 4> Vals; SmallVector<Metadata *, 4> MDs;
// Reserve first location for self reference to the LoopID metadata node. // Reserve first location for self reference to the LoopID metadata node.
Vals.push_back(nullptr); MDs.push_back(nullptr);
for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) { for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
bool IsUnrollMetadata = false; bool IsUnrollMetadata = false;
MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i)); MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
@ -298,17 +299,18 @@ static void SetLoopAlreadyUnrolled(Loop *L) {
const MDString *S = dyn_cast<MDString>(MD->getOperand(0)); const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll."); IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll.");
} }
if (!IsUnrollMetadata) Vals.push_back(LoopID->getOperand(i)); if (!IsUnrollMetadata)
MDs.push_back(LoopID->getOperand(i));
} }
// Add unroll(disable) metadata to disable future unrolling. // Add unroll(disable) metadata to disable future unrolling.
LLVMContext &Context = L->getHeader()->getContext(); LLVMContext &Context = L->getHeader()->getContext();
SmallVector<Value *, 1> DisableOperands; SmallVector<Metadata *, 1> DisableOperands;
DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable")); DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable"));
MDNode *DisableNode = MDNode::get(Context, DisableOperands); MDNode *DisableNode = MDNode::get(Context, DisableOperands);
Vals.push_back(DisableNode); MDs.push_back(DisableNode);
MDNode *NewLoopID = MDNode::get(Context, Vals); MDNode *NewLoopID = MDNode::get(Context, MDs);
// Set operand 0 to refer to the loop id itself. // Set operand 0 to refer to the loop id itself.
NewLoopID->replaceOperandWith(0, NewLoopID); NewLoopID->replaceOperandWith(0, NewLoopID);
L->setLoopID(NewLoopID); L->setLoopID(NewLoopID);

View File

@ -807,12 +807,14 @@ public:
void run(const SmallVectorImpl<Instruction*> &Insts) { void run(const SmallVectorImpl<Instruction*> &Insts) {
// Retain the debug information attached to the alloca for use when // Retain the debug information attached to the alloca for use when
// rewriting loads and stores. // rewriting loads and stores.
if (MDNode *DebugNode = MDNode::getIfExists(AI.getContext(), &AI)) { if (auto *L = LocalAsMetadata::getIfExists(&AI)) {
for (User *U : DebugNode->users()) if (auto *DebugNode = MetadataAsValue::getIfExists(AI.getContext(), L)) {
if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U)) for (User *U : DebugNode->users())
DDIs.push_back(DDI); if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U))
else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(U)) DDIs.push_back(DDI);
DVIs.push_back(DVI); else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(U))
DVIs.push_back(DVI);
}
} }
LoadAndStorePromoter::run(Insts); LoadAndStorePromoter::run(Insts);
@ -3614,7 +3616,7 @@ bool SROA::promoteAllocas(Function &F) {
DEBUG(dbgs() << "Promoting allocas with SSAUpdater...\n"); DEBUG(dbgs() << "Promoting allocas with SSAUpdater...\n");
SSAUpdater SSA; SSAUpdater SSA;
DIBuilder DIB(*F.getParent()); DIBuilder DIB(*F.getParent(), /*AllowUnresolved*/ false);
SmallVector<Instruction *, 64> Insts; SmallVector<Instruction *, 64> Insts;
// We need a worklist to walk the uses of each alloca. // We need a worklist to walk the uses of each alloca.

View File

@ -1068,12 +1068,14 @@ public:
void run(AllocaInst *AI, const SmallVectorImpl<Instruction*> &Insts) { void run(AllocaInst *AI, const SmallVectorImpl<Instruction*> &Insts) {
// Remember which alloca we're promoting (for isInstInList). // Remember which alloca we're promoting (for isInstInList).
this->AI = AI; this->AI = AI;
if (MDNode *DebugNode = MDNode::getIfExists(AI->getContext(), AI)) { if (auto *L = LocalAsMetadata::getIfExists(AI)) {
for (User *U : DebugNode->users()) if (auto *DebugNode = MetadataAsValue::getIfExists(AI->getContext(), L)) {
if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U)) for (User *U : DebugNode->users())
DDIs.push_back(DDI); if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U))
else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(U)) DDIs.push_back(DDI);
DVIs.push_back(DVI); else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(U))
DVIs.push_back(DVI);
}
} }
LoadAndStorePromoter::run(Insts); LoadAndStorePromoter::run(Insts);
@ -1420,7 +1422,7 @@ bool SROA::performPromotion(Function &F) {
AssumptionTracker *AT = &getAnalysis<AssumptionTracker>(); AssumptionTracker *AT = &getAnalysis<AssumptionTracker>();
BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
DIBuilder DIB(*F.getParent()); DIBuilder DIB(*F.getParent(), /*AllowUnresolved*/ false);
bool Changed = false; bool Changed = false;
SmallVector<Instruction*, 64> Insts; SmallVector<Instruction*, 64> Insts;
while (1) { while (1) {

View File

@ -167,7 +167,7 @@ bool AddDiscriminators::runOnFunction(Function &F) {
bool Changed = false; bool Changed = false;
Module *M = F.getParent(); Module *M = F.getParent();
LLVMContext &Ctx = M->getContext(); LLVMContext &Ctx = M->getContext();
DIBuilder Builder(*M); DIBuilder Builder(*M, /*AllowUnresolved*/ false);
// Traverse all the blocks looking for instructions in different // Traverse all the blocks looking for instructions in different
// blocks that are at the same file:line location. // blocks that are at the same file:line location.

View File

@ -164,8 +164,8 @@ static MDNode* FindSubprogram(const Function *F, DebugInfoFinder &Finder) {
// Add an operand to an existing MDNode. The new operand will be added at the // Add an operand to an existing MDNode. The new operand will be added at the
// back of the operand list. // back of the operand list.
static void AddOperand(DICompileUnit CU, DIArray SPs, Value *NewSP) { static void AddOperand(DICompileUnit CU, DIArray SPs, Metadata *NewSP) {
SmallVector<Value *, 16> NewSPs; SmallVector<Metadata *, 16> NewSPs;
NewSPs.reserve(SPs->getNumOperands() + 1); NewSPs.reserve(SPs->getNumOperands() + 1);
for (unsigned I = 0, E = SPs->getNumOperands(); I != E; ++I) for (unsigned I = 0, E = SPs->getNumOperands(); I != E; ++I)
NewSPs.push_back(SPs->getOperand(I)); NewSPs.push_back(SPs->getOperand(I));

View File

@ -308,7 +308,7 @@ static void CloneAliasScopeMetadata(CallSite CS, ValueToValueMapTy &VMap) {
// Walk the existing metadata, adding the complete (perhaps cyclic) chain to // Walk the existing metadata, adding the complete (perhaps cyclic) chain to
// the set. // the set.
SmallVector<const Value *, 16> Queue(MD.begin(), MD.end()); SmallVector<const Metadata *, 16> Queue(MD.begin(), MD.end());
while (!Queue.empty()) { while (!Queue.empty()) {
const MDNode *M = cast<MDNode>(Queue.pop_back_val()); const MDNode *M = cast<MDNode>(Queue.pop_back_val());
for (unsigned i = 0, ie = M->getNumOperands(); i != ie; ++i) for (unsigned i = 0, ie = M->getNumOperands(); i != ie; ++i)
@ -320,12 +320,12 @@ static void CloneAliasScopeMetadata(CallSite CS, ValueToValueMapTy &VMap) {
// Now we have a complete set of all metadata in the chains used to specify // Now we have a complete set of all metadata in the chains used to specify
// the noalias scopes and the lists of those scopes. // the noalias scopes and the lists of those scopes.
SmallVector<MDNode *, 16> DummyNodes; SmallVector<MDNode *, 16> DummyNodes;
DenseMap<const MDNode *, TrackingVH<MDNode> > MDMap; DenseMap<const MDNode *, TrackingMDNodeRef> MDMap;
for (SetVector<const MDNode *>::iterator I = MD.begin(), IE = MD.end(); for (SetVector<const MDNode *>::iterator I = MD.begin(), IE = MD.end();
I != IE; ++I) { I != IE; ++I) {
MDNode *Dummy = MDNode::getTemporary(CalledFunc->getContext(), None); MDNode *Dummy = MDNode::getTemporary(CalledFunc->getContext(), None);
DummyNodes.push_back(Dummy); DummyNodes.push_back(Dummy);
MDMap[*I] = Dummy; MDMap[*I].reset(Dummy);
} }
// Create new metadata nodes to replace the dummy nodes, replacing old // Create new metadata nodes to replace the dummy nodes, replacing old
@ -333,17 +333,17 @@ static void CloneAliasScopeMetadata(CallSite CS, ValueToValueMapTy &VMap) {
// node. // node.
for (SetVector<const MDNode *>::iterator I = MD.begin(), IE = MD.end(); for (SetVector<const MDNode *>::iterator I = MD.begin(), IE = MD.end();
I != IE; ++I) { I != IE; ++I) {
SmallVector<Value *, 4> NewOps; SmallVector<Metadata *, 4> NewOps;
for (unsigned i = 0, ie = (*I)->getNumOperands(); i != ie; ++i) { for (unsigned i = 0, ie = (*I)->getNumOperands(); i != ie; ++i) {
const Value *V = (*I)->getOperand(i); const Metadata *V = (*I)->getOperand(i);
if (const MDNode *M = dyn_cast<MDNode>(V)) if (const MDNode *M = dyn_cast<MDNode>(V))
NewOps.push_back(MDMap[M]); NewOps.push_back(MDMap[M]);
else else
NewOps.push_back(const_cast<Value *>(V)); NewOps.push_back(const_cast<Metadata *>(V));
} }
MDNode *NewM = MDNode::get(CalledFunc->getContext(), NewOps), MDNode *NewM = MDNode::get(CalledFunc->getContext(), NewOps);
*TempM = MDMap[*I]; MDNodeFwdDecl *TempM = cast<MDNodeFwdDecl>(MDMap[*I]);
TempM->replaceAllUsesWith(NewM); TempM->replaceAllUsesWith(NewM);
} }
@ -516,7 +516,7 @@ static void AddAliasScopeMetadata(CallSite CS, ValueToValueMapTy &VMap,
// need to go through several PHIs to see it, and thus could be // need to go through several PHIs to see it, and thus could be
// repeated in the Objects list. // repeated in the Objects list.
SmallPtrSet<const Value *, 4> ObjSet; SmallPtrSet<const Value *, 4> ObjSet;
SmallVector<Value *, 4> Scopes, NoAliases; SmallVector<Metadata *, 4> Scopes, NoAliases;
SmallSetVector<const Argument *, 4> NAPtrArgs; SmallSetVector<const Argument *, 4> NAPtrArgs;
for (unsigned i = 0, ie = PtrArgs.size(); i != ie; ++i) { for (unsigned i = 0, ie = PtrArgs.size(); i != ie; ++i) {
@ -869,8 +869,9 @@ static void fixupLineNumbers(Function *Fn, Function::iterator FI,
if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(BI)) { if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(BI)) {
LLVMContext &Ctx = BI->getContext(); LLVMContext &Ctx = BI->getContext();
MDNode *InlinedAt = BI->getDebugLoc().getInlinedAt(Ctx); MDNode *InlinedAt = BI->getDebugLoc().getInlinedAt(Ctx);
DVI->setOperand(2, createInlinedVariable(DVI->getVariable(), DVI->setOperand(2, MetadataAsValue::get(
InlinedAt, Ctx)); Ctx, createInlinedVariable(DVI->getVariable(),
InlinedAt, Ctx)));
} }
} }
} }

View File

@ -137,7 +137,8 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions,
SmallVector<uint32_t, 8> Weights; SmallVector<uint32_t, 8> Weights;
for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e; for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e;
++MD_i) { ++MD_i) {
ConstantInt* CI = dyn_cast<ConstantInt>(MD->getOperand(MD_i)); ConstantInt *CI =
mdconst::dyn_extract<ConstantInt>(MD->getOperand(MD_i));
assert(CI); assert(CI);
Weights.push_back(CI->getValue().getZExtValue()); Weights.push_back(CI->getValue().getZExtValue());
} }
@ -208,8 +209,10 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions,
SI->getDefaultDest()); SI->getDefaultDest());
MDNode *MD = SI->getMetadata(LLVMContext::MD_prof); MDNode *MD = SI->getMetadata(LLVMContext::MD_prof);
if (MD && MD->getNumOperands() == 3) { if (MD && MD->getNumOperands() == 3) {
ConstantInt *SICase = dyn_cast<ConstantInt>(MD->getOperand(2)); ConstantInt *SICase =
ConstantInt *SIDef = dyn_cast<ConstantInt>(MD->getOperand(1)); mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
ConstantInt *SIDef =
mdconst::dyn_extract<ConstantInt>(MD->getOperand(1));
assert(SICase && SIDef); assert(SICase && SIDef);
// The TrueWeight should be the weight for the single case of SI. // The TrueWeight should be the weight for the single case of SI.
NewBr->setMetadata(LLVMContext::MD_prof, NewBr->setMetadata(LLVMContext::MD_prof,
@ -1048,7 +1051,7 @@ static bool isArray(AllocaInst *AI) {
/// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
/// of llvm.dbg.value intrinsics. /// of llvm.dbg.value intrinsics.
bool llvm::LowerDbgDeclare(Function &F) { bool llvm::LowerDbgDeclare(Function &F) {
DIBuilder DIB(*F.getParent()); DIBuilder DIB(*F.getParent(), /*AllowUnresolved*/ false);
SmallVector<DbgDeclareInst *, 4> Dbgs; SmallVector<DbgDeclareInst *, 4> Dbgs;
for (auto &FI : F) for (auto &FI : F)
for (BasicBlock::iterator BI : FI) for (BasicBlock::iterator BI : FI)
@ -1091,10 +1094,11 @@ bool llvm::LowerDbgDeclare(Function &F) {
/// FindAllocaDbgDeclare - Finds the llvm.dbg.declare intrinsic describing the /// FindAllocaDbgDeclare - Finds the llvm.dbg.declare intrinsic describing the
/// alloca 'V', if any. /// alloca 'V', if any.
DbgDeclareInst *llvm::FindAllocaDbgDeclare(Value *V) { DbgDeclareInst *llvm::FindAllocaDbgDeclare(Value *V) {
if (MDNode *DebugNode = MDNode::getIfExists(V->getContext(), V)) if (auto *L = LocalAsMetadata::getIfExists(V))
for (User *U : DebugNode->users()) if (auto *MDV = MetadataAsValue::getIfExists(V->getContext(), L))
if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U)) for (User *U : MDV->users())
return DDI; if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U))
return DDI;
return nullptr; return nullptr;
} }

View File

@ -217,9 +217,9 @@ static void CloneLoopBlocks(Loop *L, Value *NewIter, const bool UnrollProlog,
} }
if (NewLoop) { if (NewLoop) {
// Add unroll disable metadata to disable future unrolling for this loop. // Add unroll disable metadata to disable future unrolling for this loop.
SmallVector<Value *, 4> Vals; SmallVector<Metadata *, 4> MDs;
// Reserve first location for self reference to the LoopID metadata node. // Reserve first location for self reference to the LoopID metadata node.
Vals.push_back(nullptr); MDs.push_back(nullptr);
MDNode *LoopID = NewLoop->getLoopID(); MDNode *LoopID = NewLoop->getLoopID();
if (LoopID) { if (LoopID) {
// First remove any existing loop unrolling metadata. // First remove any existing loop unrolling metadata.
@ -230,17 +230,18 @@ static void CloneLoopBlocks(Loop *L, Value *NewIter, const bool UnrollProlog,
const MDString *S = dyn_cast<MDString>(MD->getOperand(0)); const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll."); IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll.");
} }
if (!IsUnrollMetadata) Vals.push_back(LoopID->getOperand(i)); if (!IsUnrollMetadata)
MDs.push_back(LoopID->getOperand(i));
} }
} }
LLVMContext &Context = NewLoop->getHeader()->getContext(); LLVMContext &Context = NewLoop->getHeader()->getContext();
SmallVector<Value *, 1> DisableOperands; SmallVector<Metadata *, 1> DisableOperands;
DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable")); DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable"));
MDNode *DisableNode = MDNode::get(Context, DisableOperands); MDNode *DisableNode = MDNode::get(Context, DisableOperands);
Vals.push_back(DisableNode); MDs.push_back(DisableNode);
MDNode *NewLoopID = MDNode::get(Context, Vals); MDNode *NewLoopID = MDNode::get(Context, MDs);
// Set operand 0 to refer to the loop id itself. // Set operand 0 to refer to the loop id itself.
NewLoopID->replaceOperandWith(0, NewLoopID); NewLoopID->replaceOperandWith(0, NewLoopID);
NewLoop->setLoopID(NewLoopID); NewLoop->setLoopID(NewLoopID);

View File

@ -284,7 +284,8 @@ public:
PromoteMem2Reg(ArrayRef<AllocaInst *> Allocas, DominatorTree &DT, PromoteMem2Reg(ArrayRef<AllocaInst *> Allocas, DominatorTree &DT,
AliasSetTracker *AST, AssumptionTracker *AT) AliasSetTracker *AST, AssumptionTracker *AT)
: Allocas(Allocas.begin(), Allocas.end()), DT(DT), : Allocas(Allocas.begin(), Allocas.end()), DT(DT),
DIB(*DT.getRoot()->getParent()->getParent()), AST(AST), AT(AT) {} DIB(*DT.getRoot()->getParent()->getParent(), /*AllowUnresolved*/ false),
AST(AST), AT(AT) {}
void run(); void run();
@ -415,7 +416,8 @@ static bool rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info,
// Record debuginfo for the store and remove the declaration's // Record debuginfo for the store and remove the declaration's
// debuginfo. // debuginfo.
if (DbgDeclareInst *DDI = Info.DbgDeclare) { if (DbgDeclareInst *DDI = Info.DbgDeclare) {
DIBuilder DIB(*AI->getParent()->getParent()->getParent()); DIBuilder DIB(*AI->getParent()->getParent()->getParent(),
/*AllowUnresolved*/ false);
ConvertDebugDeclareToDebugValue(DDI, Info.OnlyStore, DIB); ConvertDebugDeclareToDebugValue(DDI, Info.OnlyStore, DIB);
DDI->eraseFromParent(); DDI->eraseFromParent();
LBI.deleteValue(DDI); LBI.deleteValue(DDI);
@ -498,7 +500,8 @@ static void promoteSingleBlockAlloca(AllocaInst *AI, const AllocaInfo &Info,
StoreInst *SI = cast<StoreInst>(AI->user_back()); StoreInst *SI = cast<StoreInst>(AI->user_back());
// Record debuginfo for the store before removing it. // Record debuginfo for the store before removing it.
if (DbgDeclareInst *DDI = Info.DbgDeclare) { if (DbgDeclareInst *DDI = Info.DbgDeclare) {
DIBuilder DIB(*AI->getParent()->getParent()->getParent()); DIBuilder DIB(*AI->getParent()->getParent()->getParent(),
/*AllowUnresolved*/ false);
ConvertDebugDeclareToDebugValue(DDI, SI, DIB); ConvertDebugDeclareToDebugValue(DDI, SI, DIB);
} }
SI->eraseFromParent(); SI->eraseFromParent();

View File

@ -713,8 +713,7 @@ SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI,
if (HasWeight) if (HasWeight)
for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e; for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e;
++MD_i) { ++MD_i) {
ConstantInt* CI = dyn_cast<ConstantInt>(MD->getOperand(MD_i)); ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(MD_i));
assert(CI);
Weights.push_back(CI->getValue().getZExtValue()); Weights.push_back(CI->getValue().getZExtValue());
} }
for (SwitchInst::CaseIt i = SI->case_end(), e = SI->case_begin(); i != e;) { for (SwitchInst::CaseIt i = SI->case_end(), e = SI->case_begin(); i != e;) {
@ -819,7 +818,7 @@ static void GetBranchWeights(TerminatorInst *TI,
MDNode *MD = TI->getMetadata(LLVMContext::MD_prof); MDNode *MD = TI->getMetadata(LLVMContext::MD_prof);
assert(MD); assert(MD);
for (unsigned i = 1, e = MD->getNumOperands(); i < e; ++i) { for (unsigned i = 1, e = MD->getNumOperands(); i < e; ++i) {
ConstantInt *CI = cast<ConstantInt>(MD->getOperand(i)); ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(i));
Weights.push_back(CI->getValue().getZExtValue()); Weights.push_back(CI->getValue().getZExtValue());
} }
@ -2037,8 +2036,10 @@ static bool ExtractBranchMetadata(BranchInst *BI,
"Looking for probabilities on unconditional branch?"); "Looking for probabilities on unconditional branch?");
MDNode *ProfileData = BI->getMetadata(LLVMContext::MD_prof); MDNode *ProfileData = BI->getMetadata(LLVMContext::MD_prof);
if (!ProfileData || ProfileData->getNumOperands() != 3) return false; if (!ProfileData || ProfileData->getNumOperands() != 3) return false;
ConstantInt *CITrue = dyn_cast<ConstantInt>(ProfileData->getOperand(1)); ConstantInt *CITrue =
ConstantInt *CIFalse = dyn_cast<ConstantInt>(ProfileData->getOperand(2)); mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1));
ConstantInt *CIFalse =
mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2));
if (!CITrue || !CIFalse) return false; if (!CITrue || !CIFalse) return false;
ProbTrue = CITrue->getValue().getZExtValue(); ProbTrue = CITrue->getValue().getZExtValue();
ProbFalse = CIFalse->getValue().getZExtValue(); ProbFalse = CIFalse->getValue().getZExtValue();

View File

@ -40,7 +40,7 @@ Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
// Global values do not need to be seeded into the VM if they // Global values do not need to be seeded into the VM if they
// are using the identity mapping. // are using the identity mapping.
if (isa<GlobalValue>(V) || isa<MDString>(V)) if (isa<GlobalValue>(V))
return VM[V] = const_cast<Value*>(V); return VM[V] = const_cast<Value*>(V);
if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
@ -56,57 +56,24 @@ Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
return VM[V] = const_cast<Value*>(V); return VM[V] = const_cast<Value*>(V);
} }
if (const MDNode *MD = dyn_cast<MDNode>(V)) { if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) {
const Metadata *MD = MDV->getMetadata();
// If this is a module-level metadata and we know that nothing at the module // If this is a module-level metadata and we know that nothing at the module
// level is changing, then use an identity mapping. // level is changing, then use an identity mapping.
if (!MD->isFunctionLocal() && (Flags & RF_NoModuleLevelChanges)) if (!isa<LocalAsMetadata>(MD) && (Flags & RF_NoModuleLevelChanges))
return VM[V] = const_cast<Value*>(V); return VM[V] = const_cast<Value *>(V);
// Create a dummy node in case we have a metadata cycle.
MDNode *Dummy = MDNode::getTemporary(V->getContext(), None);
VM[V] = Dummy;
// Check all operands to see if any need to be remapped.
for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
Value *OP = MD->getOperand(i);
if (!OP) continue;
Value *Mapped_OP = MapValue(OP, VM, Flags, TypeMapper, Materializer);
// Use identity map if Mapped_Op is null and we can ignore missing
// entries.
if (Mapped_OP == OP ||
(Mapped_OP == nullptr && (Flags & RF_IgnoreMissingEntries)))
continue;
// Ok, at least one operand needs remapping. auto *MappedMD = MapValue(MD, VM, Flags, TypeMapper, Materializer);
SmallVector<Value*, 4> Elts; if (MD == MappedMD || (!MappedMD && (Flags & RF_IgnoreMissingEntries)))
Elts.reserve(MD->getNumOperands()); return VM[V] = const_cast<Value *>(V);
for (i = 0; i != e; ++i) {
Value *Op = MD->getOperand(i);
if (!Op)
Elts.push_back(nullptr);
else {
Value *Mapped_Op = MapValue(Op, VM, Flags, TypeMapper, Materializer);
// Use identity map if Mapped_Op is null and we can ignore missing
// entries.
if (Mapped_Op == nullptr && (Flags & RF_IgnoreMissingEntries))
Mapped_Op = Op;
Elts.push_back(Mapped_Op);
}
}
MDNode *NewMD = MDNode::get(V->getContext(), Elts);
Dummy->replaceAllUsesWith(NewMD);
VM[V] = NewMD;
MDNode::deleteTemporary(Dummy);
return NewMD;
}
VM[V] = const_cast<Value*>(V); // FIXME: This assert crashes during bootstrap, but I think it should be
MDNode::deleteTemporary(Dummy); // correct. For now, just match behaviour from before the metadata/value
// split.
// No operands needed remapping. Use an identity mapping. //
return const_cast<Value*>(V); // assert(MappedMD && "Referenced metadata value not in value map");
return VM[V] = MetadataAsValue::get(V->getContext(), MappedMD);
} }
// Okay, this either must be a constant (which may or may not be mappable) or // Okay, this either must be a constant (which may or may not be mappable) or
@ -177,6 +144,120 @@ Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy)); return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
} }
static Metadata *map(ValueToValueMapTy &VM, const Metadata *Key,
Metadata *Val) {
VM.MD()[Key].reset(Val);
return Val;
}
static Metadata *mapToSelf(ValueToValueMapTy &VM, const Metadata *MD) {
return map(VM, MD, const_cast<Metadata *>(MD));
}
static Metadata *MapValueImpl(const Metadata *MD, ValueToValueMapTy &VM,
RemapFlags Flags,
ValueMapTypeRemapper *TypeMapper,
ValueMaterializer *Materializer) {
// If the value already exists in the map, use it.
if (Metadata *NewMD = VM.MD().lookup(MD).get())
return NewMD;
if (isa<MDString>(MD))
return mapToSelf(VM, MD);
if (isa<ConstantAsMetadata>(MD))
if ((Flags & RF_NoModuleLevelChanges))
return mapToSelf(VM, MD);
if (const auto *VMD = dyn_cast<ValueAsMetadata>(MD)) {
Value *MappedV =
MapValue(VMD->getValue(), VM, Flags, TypeMapper, Materializer);
if (VMD->getValue() == MappedV ||
(!MappedV && (Flags & RF_IgnoreMissingEntries)))
return mapToSelf(VM, MD);
// FIXME: This assert crashes during bootstrap, but I think it should be
// correct. For now, just match behaviour from before the metadata/value
// split.
//
// assert(MappedV && "Referenced metadata not in value map!");
if (MappedV)
return map(VM, MD, ValueAsMetadata::get(MappedV));
return nullptr;
}
const MDNode *Node = cast<MDNode>(MD);
assert(Node->isResolved() && "Unexpected unresolved node");
auto getMappedOp = [&](Metadata *Op) -> Metadata *{
if (!Op)
return nullptr;
if (Metadata *MappedOp =
MapValueImpl(Op, VM, Flags, TypeMapper, Materializer))
return MappedOp;
// Use identity map if MappedOp is null and we can ignore missing entries.
if (Flags & RF_IgnoreMissingEntries)
return Op;
// FIXME: This assert crashes during bootstrap, but I think it should be
// correct. For now, just match behaviour from before the metadata/value
// split.
//
// llvm_unreachable("Referenced metadata not in value map!");
return nullptr;
};
// If this is a module-level metadata and we know that nothing at the
// module level is changing, then use an identity mapping.
if (Flags & RF_NoModuleLevelChanges)
return mapToSelf(VM, MD);
// Create a dummy node in case we have a metadata cycle.
MDNodeFwdDecl *Dummy = MDNode::getTemporary(Node->getContext(), None);
map(VM, Node, Dummy);
// Check all operands to see if any need to be remapped.
for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) {
Metadata *Op = Node->getOperand(I);
Metadata *MappedOp = getMappedOp(Op);
if (Op == MappedOp)
continue;
// Ok, at least one operand needs remapping.
SmallVector<Metadata *, 4> Elts;
Elts.reserve(Node->getNumOperands());
for (I = 0; I != E; ++I)
Elts.push_back(getMappedOp(Node->getOperand(I)));
MDNode *NewMD = MDNode::get(Node->getContext(), Elts);
Dummy->replaceAllUsesWith(NewMD);
MDNode::deleteTemporary(Dummy);
return map(VM, Node, NewMD);
}
// No operands needed remapping. Use an identity mapping.
mapToSelf(VM, MD);
MDNode::deleteTemporary(Dummy);
return const_cast<Metadata *>(MD);
}
Metadata *llvm::MapValue(const Metadata *MD, ValueToValueMapTy &VM,
RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
ValueMaterializer *Materializer) {
Metadata *NewMD = MapValueImpl(MD, VM, Flags, TypeMapper, Materializer);
if (NewMD && NewMD != MD)
if (auto *G = dyn_cast<GenericMDNode>(NewMD))
G->resolveCycles();
return NewMD;
}
MDNode *llvm::MapValue(const MDNode *MD, ValueToValueMapTy &VM,
RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
ValueMaterializer *Materializer) {
return cast<MDNode>(MapValue(static_cast<const Metadata *>(MD), VM, Flags,
TypeMapper, Materializer));
}
/// RemapInstruction - Convert the instruction operands from referencing the /// RemapInstruction - Convert the instruction operands from referencing the
/// current values into those specified by VMap. /// current values into those specified by VMap.
/// ///

View File

@ -1097,7 +1097,7 @@ private:
for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) { for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
const MDString *S = nullptr; const MDString *S = nullptr;
SmallVector<Value*, 4> Args; SmallVector<Metadata *, 4> Args;
// The expected hint is either a MDString or a MDNode with the first // The expected hint is either a MDString or a MDNode with the first
// operand a MDString. // operand a MDString.
@ -1123,12 +1123,12 @@ private:
} }
/// Checks string hint with one operand and set value if valid. /// Checks string hint with one operand and set value if valid.
void setHint(StringRef Name, Value *Arg) { void setHint(StringRef Name, Metadata *Arg) {
if (!Name.startswith(Prefix())) if (!Name.startswith(Prefix()))
return; return;
Name = Name.substr(Prefix().size(), StringRef::npos); Name = Name.substr(Prefix().size(), StringRef::npos);
const ConstantInt *C = dyn_cast<ConstantInt>(Arg); const ConstantInt *C = mdconst::dyn_extract<ConstantInt>(Arg);
if (!C) return; if (!C) return;
unsigned Val = C->getZExtValue(); unsigned Val = C->getZExtValue();
@ -1147,9 +1147,10 @@ private:
/// Create a new hint from name / value pair. /// Create a new hint from name / value pair.
MDNode *createHintMetadata(StringRef Name, unsigned V) const { MDNode *createHintMetadata(StringRef Name, unsigned V) const {
LLVMContext &Context = TheLoop->getHeader()->getContext(); LLVMContext &Context = TheLoop->getHeader()->getContext();
Value *Vals[] = {MDString::get(Context, Name), Metadata *MDs[] = {MDString::get(Context, Name),
ConstantInt::get(Type::getInt32Ty(Context), V)}; ConstantAsMetadata::get(
return MDNode::get(Context, Vals); ConstantInt::get(Type::getInt32Ty(Context), V))};
return MDNode::get(Context, MDs);
} }
/// Matches metadata with hint name. /// Matches metadata with hint name.
@ -1170,7 +1171,7 @@ private:
return; return;
// Reserve the first element to LoopID (see below). // Reserve the first element to LoopID (see below).
SmallVector<Value*, 4> Vals(1); SmallVector<Metadata *, 4> MDs(1);
// If the loop already has metadata, then ignore the existing operands. // If the loop already has metadata, then ignore the existing operands.
MDNode *LoopID = TheLoop->getLoopID(); MDNode *LoopID = TheLoop->getLoopID();
if (LoopID) { if (LoopID) {
@ -1178,18 +1179,17 @@ private:
MDNode *Node = cast<MDNode>(LoopID->getOperand(i)); MDNode *Node = cast<MDNode>(LoopID->getOperand(i));
// If node in update list, ignore old value. // If node in update list, ignore old value.
if (!matchesHintMetadataName(Node, HintTypes)) if (!matchesHintMetadataName(Node, HintTypes))
Vals.push_back(Node); MDs.push_back(Node);
} }
} }
// Now, add the missing hints. // Now, add the missing hints.
for (auto H : HintTypes) for (auto H : HintTypes)
Vals.push_back( MDs.push_back(createHintMetadata(Twine(Prefix(), H.Name).str(), H.Value));
createHintMetadata(Twine(Prefix(), H.Name).str(), H.Value));
// Replace current metadata node with new one. // Replace current metadata node with new one.
LLVMContext &Context = TheLoop->getHeader()->getContext(); LLVMContext &Context = TheLoop->getHeader()->getContext();
MDNode *NewLoopID = MDNode::get(Context, Vals); MDNode *NewLoopID = MDNode::get(Context, MDs);
// Set operand 0 to refer to the loop id itself. // Set operand 0 to refer to the loop id itself.
NewLoopID->replaceOperandWith(0, NewLoopID); NewLoopID->replaceOperandWith(0, NewLoopID);

View File

@ -2,6 +2,6 @@
define void @foo(i32 %v) { define void @foo(i32 %v) {
entry: entry:
; CHECK: <stdin>:[[@LINE+1]]:{{[0-9]+}}: error: unexpected function-local metadata ; CHECK: <stdin>:[[@LINE+1]]:{{[0-9]+}}: error: invalid use of function-local name
ret void, !foo !{i32 %v} ret void, !foo !{i32 %v}
} }

View File

@ -2,7 +2,7 @@
define void @foo(i32 %v) { define void @foo(i32 %v) {
entry: entry:
; CHECK: <stdin>:[[@LINE+1]]:{{[0-9]+}}: error: unexpected nested function-local metadata ; CHECK: <stdin>:[[@LINE+1]]:{{[0-9]+}}: error: invalid use of function-local name
call void @llvm.bar(metadata !{metadata !{i32 %v}}) call void @llvm.bar(metadata !{metadata !{i32 %v}})
ret void ret void
} }

View File

@ -2,7 +2,7 @@
; PR7105 ; PR7105
define void @foo(i32 %x) { define void @foo(i32 %x) {
call void @llvm.zonk(metadata !1, i64 0, metadata !1) call void @llvm.zonk(metadata !{i32 %x}, i64 0, metadata !1)
store i32 0, i32* null, !whatever !0, !whatever_else !{}, !more !{metadata !"hello"} store i32 0, i32* null, !whatever !0, !whatever_else !{}, !more !{metadata !"hello"}
store i32 0, i32* null, !whatever !{metadata !"hello", metadata !1, metadata !{}, metadata !2} store i32 0, i32* null, !whatever !{metadata !"hello", metadata !1, metadata !{}, metadata !2}
ret void ret void

View File

@ -0,0 +1,6 @@
!named = !{!0}
; These nodes are intentionally in the opposite order from the test-driver.
; However, they are numbered the same for the reader's convenience.
!1 = metadata !{}
!0 = metadata !{metadata !1}

View File

@ -0,0 +1,20 @@
; RUN: llvm-link %s %S/Inputs/unique-fwd-decl-order.ll -S -o - | FileCheck %s
; RUN: llvm-link %S/Inputs/unique-fwd-decl-order.ll %s -S -o - | FileCheck %s
; This test exercises MDNode hashing. For the nodes to be correctly uniqued,
; the hash of a to-be-created MDNode has to match the hash of an
; operand-just-changed MDNode (with the same operands).
;
; Note that these two assembly files number the nodes identically, even though
; the nodes are in a different order. This is for the reader's convenience.
; CHECK: !named = !{!0, !0}
!named = !{!0}
; CHECK: !0 = metadata !{metadata !1}
!0 = metadata !{metadata !1}
; CHECK: !1 = metadata !{}
!1 = metadata !{}
; CHECK-NOT: !2

View File

@ -13,15 +13,20 @@ define i32 @main(i32 %argc, i8** %argv) {
} }
define void @foo(i32 %x) { define void @foo(i32 %x) {
call void @llvm.foo(metadata !{i8*** @G}) ; Note: these arguments look like MDNodes, but they're really syntactic sugar
; CHECK: call void @llvm.foo(metadata !0) ; for 'MetadataAsValue::get(ValueAsMetadata::get(Value*))'. When @G drops to
; null, the ValueAsMetadata instance gets replaced by metadata !{}, or
; MDNode::get({}).
call void @llvm.foo(metadata !{i8*** @G}, metadata !{i32 %x})
; CHECK: call void @llvm.foo(metadata ![[EMPTY:[0-9]+]], metadata !{i32 %x})
ret void ret void
} }
declare void @llvm.foo(metadata) nounwind readnone declare void @llvm.foo(metadata, metadata) nounwind readnone
!named = !{!0} !named = !{!0}
; CHECK: !named = !{!0} ; CHECK: !named = !{![[NULL:[0-9]+]]}
!0 = metadata !{i8*** @G} !0 = metadata !{i8*** @G}
; CHECK: !0 = metadata !{null} ; CHECK-DAG: ![[NULL]] = metadata !{null}
; CHECK-DAG: ![[EMPTY]] = metadata !{}

View File

@ -36,10 +36,10 @@ TEST_F(MDBuilderTest, createFPMath) {
EXPECT_EQ(MD0, (MDNode *)nullptr); EXPECT_EQ(MD0, (MDNode *)nullptr);
EXPECT_NE(MD1, (MDNode *)nullptr); EXPECT_NE(MD1, (MDNode *)nullptr);
EXPECT_EQ(MD1->getNumOperands(), 1U); EXPECT_EQ(MD1->getNumOperands(), 1U);
Value *Op = MD1->getOperand(0); Metadata *Op = MD1->getOperand(0);
EXPECT_TRUE(isa<ConstantFP>(Op)); EXPECT_TRUE(mdconst::hasa<ConstantFP>(Op));
EXPECT_TRUE(Op->getType()->isFloatingPointTy()); ConstantFP *Val = mdconst::extract<ConstantFP>(Op);
ConstantFP *Val = cast<ConstantFP>(Op); EXPECT_TRUE(Val->getType()->isFloatingPointTy());
EXPECT_TRUE(Val->isExactlyValue(1.0)); EXPECT_TRUE(Val->isExactlyValue(1.0));
} }
TEST_F(MDBuilderTest, createRangeMetadata) { TEST_F(MDBuilderTest, createRangeMetadata) {
@ -50,10 +50,10 @@ TEST_F(MDBuilderTest, createRangeMetadata) {
EXPECT_EQ(R0, (MDNode *)nullptr); EXPECT_EQ(R0, (MDNode *)nullptr);
EXPECT_NE(R1, (MDNode *)nullptr); EXPECT_NE(R1, (MDNode *)nullptr);
EXPECT_EQ(R1->getNumOperands(), 2U); EXPECT_EQ(R1->getNumOperands(), 2U);
EXPECT_TRUE(isa<ConstantInt>(R1->getOperand(0))); EXPECT_TRUE(mdconst::hasa<ConstantInt>(R1->getOperand(0)));
EXPECT_TRUE(isa<ConstantInt>(R1->getOperand(1))); EXPECT_TRUE(mdconst::hasa<ConstantInt>(R1->getOperand(1)));
ConstantInt *C0 = cast<ConstantInt>(R1->getOperand(0)); ConstantInt *C0 = mdconst::extract<ConstantInt>(R1->getOperand(0));
ConstantInt *C1 = cast<ConstantInt>(R1->getOperand(1)); ConstantInt *C1 = mdconst::extract<ConstantInt>(R1->getOperand(1));
EXPECT_EQ(C0->getValue(), A); EXPECT_EQ(C0->getValue(), A);
EXPECT_EQ(C1->getValue(), B); EXPECT_EQ(C1->getValue(), B);
} }
@ -101,7 +101,8 @@ TEST_F(MDBuilderTest, createTBAANode) {
EXPECT_EQ(N0->getOperand(1), R); EXPECT_EQ(N0->getOperand(1), R);
EXPECT_EQ(N1->getOperand(1), R); EXPECT_EQ(N1->getOperand(1), R);
EXPECT_EQ(N2->getOperand(1), R); EXPECT_EQ(N2->getOperand(1), R);
EXPECT_TRUE(isa<ConstantInt>(N2->getOperand(2))); EXPECT_TRUE(mdconst::hasa<ConstantInt>(N2->getOperand(2)));
EXPECT_EQ(cast<ConstantInt>(N2->getOperand(2))->getZExtValue(), 1U); EXPECT_EQ(mdconst::extract<ConstantInt>(N2->getOperand(2))->getZExtValue(),
1U);
} }
} }

View File

@ -13,7 +13,6 @@
#include "llvm/IR/LLVMContext.h" #include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h" #include "llvm/IR/Module.h"
#include "llvm/IR/Type.h" #include "llvm/IR/Type.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
using namespace llvm; using namespace llvm;
@ -80,17 +79,18 @@ TEST_F(MDNodeTest, Simple) {
MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
MDString *s2 = MDString::get(Context, StringRef(&y[0], 3)); MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0)); ConstantAsMetadata *CI = ConstantAsMetadata::get(
ConstantInt::get(getGlobalContext(), APInt(8, 0)));
std::vector<Value *> V; std::vector<Metadata *> V;
V.push_back(s1); V.push_back(s1);
V.push_back(CI); V.push_back(CI);
V.push_back(s2); V.push_back(s2);
MDNode *n1 = MDNode::get(Context, V); MDNode *n1 = MDNode::get(Context, V);
Value *const c1 = n1; Metadata *const c1 = n1;
MDNode *n2 = MDNode::get(Context, c1); MDNode *n2 = MDNode::get(Context, c1);
Value *const c2 = n2; Metadata *const c2 = n2;
MDNode *n3 = MDNode::get(Context, V); MDNode *n3 = MDNode::get(Context, V);
MDNode *n4 = MDNode::getIfExists(Context, V); MDNode *n4 = MDNode::getIfExists(Context, V);
MDNode *n5 = MDNode::getIfExists(Context, c1); MDNode *n5 = MDNode::getIfExists(Context, c1);
@ -99,7 +99,7 @@ TEST_F(MDNodeTest, Simple) {
EXPECT_EQ(n1, n3); EXPECT_EQ(n1, n3);
EXPECT_EQ(n4, n1); EXPECT_EQ(n4, n1);
EXPECT_EQ(n5, n2); EXPECT_EQ(n5, n2);
EXPECT_EQ(n6, (Value*)nullptr); EXPECT_EQ(n6, (Metadata *)nullptr);
EXPECT_EQ(3u, n1->getNumOperands()); EXPECT_EQ(3u, n1->getNumOperands());
EXPECT_EQ(s1, n1->getOperand(0)); EXPECT_EQ(s1, n1->getOperand(0));
@ -114,9 +114,9 @@ TEST_F(MDNodeTest, Delete) {
Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1); Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext())); Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
Value *const V = I; Metadata *const V = LocalAsMetadata::get(I);
MDNode *n = MDNode::get(Context, V); MDNode *n = MDNode::get(Context, V);
WeakVH wvh = n; TrackingMDRef wvh(n);
EXPECT_EQ(n, wvh); EXPECT_EQ(n, wvh);
@ -128,7 +128,7 @@ TEST_F(MDNodeTest, SelfReference) {
// !1 = metadata !{metadata !0} // !1 = metadata !{metadata !0}
{ {
MDNode *Temp = MDNode::getTemporary(Context, None); MDNode *Temp = MDNode::getTemporary(Context, None);
Value *Args[] = {Temp}; Metadata *Args[] = {Temp};
MDNode *Self = MDNode::get(Context, Args); MDNode *Self = MDNode::get(Context, Args);
Self->replaceOperandWith(0, Self); Self->replaceOperandWith(0, Self);
MDNode::deleteTemporary(Temp); MDNode::deleteTemporary(Temp);
@ -147,7 +147,7 @@ TEST_F(MDNodeTest, SelfReference) {
// !1 = metadata !{metadata !0, metadata !{}} // !1 = metadata !{metadata !0, metadata !{}}
{ {
MDNode *Temp = MDNode::getTemporary(Context, None); MDNode *Temp = MDNode::getTemporary(Context, None);
Value *Args[] = {Temp, MDNode::get(Context, None)}; Metadata *Args[] = {Temp, MDNode::get(Context, None)};
MDNode *Self = MDNode::get(Context, Args); MDNode *Self = MDNode::get(Context, Args);
Self->replaceOperandWith(0, Self); Self->replaceOperandWith(0, Self);
MDNode::deleteTemporary(Temp); MDNode::deleteTemporary(Temp);
@ -163,13 +163,59 @@ TEST_F(MDNodeTest, SelfReference) {
} }
} }
typedef MetadataTest MetadataAsValueTest;
TEST_F(MetadataAsValueTest, MDNode) {
MDNode *N = MDNode::get(Context, None);
auto *V = MetadataAsValue::get(Context, N);
EXPECT_TRUE(V->getType()->isMetadataTy());
EXPECT_EQ(N, V->getMetadata());
auto *V2 = MetadataAsValue::get(Context, N);
EXPECT_EQ(V, V2);
}
TEST_F(MetadataAsValueTest, MDNodeMDNode) {
MDNode *N = MDNode::get(Context, None);
Metadata *Ops[] = {N};
MDNode *N2 = MDNode::get(Context, Ops);
auto *V = MetadataAsValue::get(Context, N2);
EXPECT_TRUE(V->getType()->isMetadataTy());
EXPECT_EQ(N2, V->getMetadata());
auto *V2 = MetadataAsValue::get(Context, N2);
EXPECT_EQ(V, V2);
auto *V3 = MetadataAsValue::get(Context, N);
EXPECT_TRUE(V3->getType()->isMetadataTy());
EXPECT_NE(V, V3);
EXPECT_EQ(N, V3->getMetadata());
}
TEST_F(MetadataAsValueTest, MDNodeConstant) {
auto *C = ConstantInt::getTrue(Context);
auto *MD = ConstantAsMetadata::get(C);
Metadata *Ops[] = {MD};
auto *N = MDNode::get(Context, Ops);
auto *V = MetadataAsValue::get(Context, MD);
EXPECT_TRUE(V->getType()->isMetadataTy());
EXPECT_EQ(MD, V->getMetadata());
auto *V2 = MetadataAsValue::get(Context, N);
EXPECT_EQ(MD, V2->getMetadata());
EXPECT_EQ(V, V2);
}
TEST(NamedMDNodeTest, Search) { TEST(NamedMDNodeTest, Search) {
LLVMContext Context; LLVMContext Context;
Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1); ConstantAsMetadata *C =
Constant *C2 = ConstantInt::get(Type::getInt32Ty(Context), 2); ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
ConstantAsMetadata *C2 =
ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Value *const V = C; Metadata *const V = C;
Value *const V2 = C2; Metadata *const V2 = C2;
MDNode *n = MDNode::get(Context, V); MDNode *n = MDNode::get(Context, V);
MDNode *n2 = MDNode::get(Context, V2); MDNode *n2 = MDNode::get(Context, V2);