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.)

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Duncan P. N. Exon Smith
2014-12-09 18:38:53 +00:00
parent db7b69e3a6
commit dad20b2ae2
88 changed files with 3370 additions and 1970 deletions

View File

@ -326,6 +326,12 @@ ValueEnumerator::ValueEnumerator(const Module &M) {
if (I->hasPrologueData())
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
// pool so that the module symbol table can refer to them...
EnumerateValueSymbolTable(M.getValueSymbolTable());
@ -341,11 +347,17 @@ ValueEnumerator::ValueEnumerator(const Module &M) {
for (const BasicBlock &BB : F)
for (const Instruction &I : BB) {
for (const Use &Op : I.operands()) {
if (MDNode *MD = dyn_cast<MDNode>(&Op))
if (MD->isFunctionLocal() && MD->getFunction())
// These will get enumerated during function-incorporation.
continue;
EnumerateOperandType(Op);
auto *MD = dyn_cast<MetadataAsValue>(&Op);
if (!MD) {
EnumerateOperandType(Op);
continue;
}
// Local metadata is enumerated during function-incorporation.
if (isa<LocalAsMetadata>(MD->getMetadata()))
continue;
EnumerateMetadata(MD->getMetadata());
}
EnumerateType(I.getType());
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 {
if (isa<MDNode>(V) || isa<MDString>(V)) {
ValueMapType::const_iterator I = MDValueMap.find(V);
assert(I != MDValueMap.end() && "Value not in slotcalculator!");
return I->second-1;
}
if (auto *MD = dyn_cast<MetadataAsValue>(V))
return getMetadataID(MD->getMetadata());
ValueMapType::const_iterator I = ValueMap.find(V);
assert(I != ValueMap.end() && "Value not in slotcalculator!");
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 {
print(dbgs(), ValueMap, "Default");
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.
void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
@ -493,25 +520,24 @@ void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
/// and types referenced by the given MDNode.
void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
if (Value *V = N->getOperand(i)) {
if (isa<MDNode>(V) || isa<MDString>(V))
EnumerateMetadata(V);
else if (!isa<Instruction>(V) && !isa<Argument>(V))
EnumerateValue(V);
} else
Metadata *MD = N->getOperand(i);
if (!MD) {
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) {
assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind");
// Skip function-local nodes themselves, but walk their operands.
const MDNode *N = dyn_cast<MDNode>(MD);
if (N && N->isFunctionLocal() && N->getFunction()) {
EnumerateMDNodeOperands(N);
return;
}
void ValueEnumerator::EnumerateMetadata(const Metadata *MD) {
assert(
(isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) &&
"Invalid metadata kind");
// Insert a dummy ID to block the co-recursive call to
// 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)
return;
// Enumerate the type of this value.
EnumerateType(MD->getType());
// Visit operands first to minimize RAUW.
if (N)
if (auto *N = dyn_cast<MDNode>(MD))
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
// have changed by inserting operands, so we need a fresh lookup here.
MDValues.push_back(MD);
MDValueMap[MD] = MDValues.size();
MDs.push_back(MD);
MDValueMap[MD] = MDs.size();
}
/// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
/// information reachable from the given MDNode.
void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) {
assert(N->isFunctionLocal() && N->getFunction() &&
"EnumerateFunctionLocalMetadata called on non-function-local mdnode!");
// Enumerate the type of this value.
EnumerateType(N->getType());
/// information reachable from the metadata.
void ValueEnumerator::EnumerateFunctionLocalMetadata(
const LocalAsMetadata *Local) {
// Check to see if it's already in!
unsigned &MDValueID = MDValueMap[N];
unsigned &MDValueID = MDValueMap[Local];
if (MDValueID)
return;
MDValues.push_back(N);
MDValueID = MDValues.size();
MDs.push_back(Local);
MDValueID = MDs.size();
// To incoroporate function-local information visit all function-local
// 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);
}
EnumerateValue(Local->getValue());
// Also, collect all function-local MDNodes for easy access.
FunctionLocalMDs.push_back(N);
// Also, collect all function-local metadata for easy access.
FunctionLocalMDs.push_back(Local);
}
void ValueEnumerator::EnumerateValue(const Value *V) {
assert(!V->getType()->isVoidTy() && "Can't insert void values!");
assert(!isa<MDNode>(V) && !isa<MDString>(V) &&
"EnumerateValue doesn't handle Metadata!");
assert(!isa<MetadataAsValue>(V) && "EnumerateValue doesn't handle Metadata!");
// Check to see if it's already in!
unsigned &ValueID = ValueMap[V];
@ -657,30 +667,35 @@ void ValueEnumerator::EnumerateType(Type *Ty) {
void ValueEnumerator::EnumerateOperandType(const Value *V) {
EnumerateType(V->getType());
if (const Constant *C = dyn_cast<Constant>(V)) {
// If this constant is already enumerated, ignore it, we know its type must
// be enumerated.
if (ValueMap.count(V)) return;
if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
assert(!isa<LocalAsMetadata>(MD->getMetadata()) &&
"Function-local metadata should be left for later");
// This constant may have operands, make sure to enumerate the types in
// them.
for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
const Value *Op = C->getOperand(i);
EnumerateMetadata(MD->getMetadata());
return;
}
// Don't enumerate basic blocks here, this happens as operands to
// blockaddress.
if (isa<BasicBlock>(Op)) continue;
const Constant *C = dyn_cast<Constant>(V);
if (!C)
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)) {
for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
if (Value *Elem = N->getOperand(i))
EnumerateOperandType(Elem);
}
} else if (isa<MDString>(V) || isa<MDNode>(V))
EnumerateMetadata(V);
// This constant may have operands, make sure to enumerate the types in
// them.
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
// blockaddress.
if (isa<BasicBlock>(Op))
continue;
EnumerateOperandType(Op);
}
}
void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) {
@ -708,7 +723,7 @@ void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) {
void ValueEnumerator::incorporateFunction(const Function &F) {
InstructionCount = 0;
NumModuleValues = Values.size();
NumModuleMDValues = MDValues.size();
NumModuleMDs = MDs.size();
// Adding function arguments to the value table.
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();
SmallVector<MDNode *, 8> FnLocalMDVector;
SmallVector<LocalAsMetadata *, 8> FnLocalMDVector;
// Add all of the instructions.
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 (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
OI != E; ++OI) {
if (MDNode *MD = dyn_cast<MDNode>(*OI))
if (MD->isFunctionLocal() && MD->getFunction())
if (auto *MD = dyn_cast<MetadataAsValue>(&*OI))
if (auto *Local = dyn_cast<LocalAsMetadata>(MD->getMetadata()))
// Enumerate metadata after the instructions they might refer to.
FnLocalMDVector.push_back(MD);
}
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);
FnLocalMDVector.push_back(Local);
}
if (!I->getType()->isVoidTy())
@ -773,13 +780,13 @@ void ValueEnumerator::purgeFunction() {
/// Remove purged values from the ValueMap.
for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
ValueMap.erase(Values[i].first);
for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i)
MDValueMap.erase(MDValues[i]);
for (unsigned i = NumModuleMDs, e = MDs.size(); i != e; ++i)
MDValueMap.erase(MDs[i]);
for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
ValueMap.erase(BasicBlocks[i]);
Values.resize(NumModuleValues);
MDValues.resize(NumModuleMDValues);
MDs.resize(NumModuleMDs);
BasicBlocks.clear();
FunctionLocalMDs.clear();
}