IR: Split Metadata from Value
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Tue, 9 Dec 2014 18:38:53 +0000 (18:38 +0000)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Tue, 9 Dec 2014 18:38:53 +0000 (18:38 +0000)
commitdad20b2ae2544708d6a33abdb9bddd0a329f50e0
treefdb7968b3e5073b9ca72d4ca118217a955dd6752
parentdb7b69e3a634c5fdff0eceeee2a41ee49c3270a2
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
88 files changed:
bindings/go/llvm/DIBuilderBindings.cpp
include/llvm-c/Core.h
include/llvm/CodeGen/LexicalScopes.h
include/llvm/CodeGen/MachineInstr.h
include/llvm/CodeGen/MachineModuleInfo.h
include/llvm/CodeGen/SelectionDAGNodes.h
include/llvm/IR/DIBuilder.h
include/llvm/IR/DebugInfo.h
include/llvm/IR/DebugLoc.h
include/llvm/IR/IntrinsicInst.h
include/llvm/IR/MDBuilder.h
include/llvm/IR/Metadata.def [new file with mode: 0644]
include/llvm/IR/Metadata.h
include/llvm/IR/MetadataTracking.h [new file with mode: 0644]
include/llvm/IR/Module.h
include/llvm/IR/TrackingMDRef.h [new file with mode: 0644]
include/llvm/IR/TypeFinder.h
include/llvm/IR/Value.h
include/llvm/IR/ValueMap.h
include/llvm/Transforms/Utils/ValueMapper.h
lib/Analysis/BranchProbabilityInfo.cpp
lib/Analysis/ScalarEvolution.cpp
lib/Analysis/TypeBasedAliasAnalysis.cpp
lib/Analysis/ValueTracking.cpp
lib/AsmParser/LLParser.cpp
lib/AsmParser/LLParser.h
lib/Bitcode/Reader/BitcodeReader.cpp
lib/Bitcode/Reader/BitcodeReader.h
lib/Bitcode/Writer/BitcodeWriter.cpp
lib/Bitcode/Writer/ValueEnumerator.cpp
lib/Bitcode/Writer/ValueEnumerator.h
lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
lib/CodeGen/AsmPrinter/DwarfUnit.cpp
lib/CodeGen/CodeGenPrepare.cpp
lib/CodeGen/MachineInstr.cpp
lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
lib/CodeGen/StackColoring.cpp
lib/CodeGen/TargetLoweringObjectFileImpl.cpp
lib/IR/AsmWriter.cpp
lib/IR/AutoUpgrade.cpp
lib/IR/CMakeLists.txt
lib/IR/Core.cpp
lib/IR/DIBuilder.cpp
lib/IR/DebugInfo.cpp
lib/IR/DebugLoc.cpp
lib/IR/DiagnosticInfo.cpp
lib/IR/Instructions.cpp
lib/IR/IntrinsicInst.cpp
lib/IR/LLVMContextImpl.cpp
lib/IR/LLVMContextImpl.h
lib/IR/MDBuilder.cpp
lib/IR/Metadata.cpp
lib/IR/MetadataTracking.cpp [new file with mode: 0644]
lib/IR/Module.cpp
lib/IR/TypeFinder.cpp
lib/IR/Value.cpp
lib/IR/ValueSymbolTable.cpp
lib/IR/Verifier.cpp
lib/LTO/LTOModule.cpp
lib/Linker/LinkModules.cpp
lib/Target/ARM/ARMAsmPrinter.cpp
lib/Target/NVPTX/NVPTXGenericToNVVM.cpp
lib/Target/NVPTX/NVPTXUtilities.cpp
lib/Transforms/IPO/StripSymbols.cpp
lib/Transforms/InstCombine/InstCombineCalls.cpp
lib/Transforms/Instrumentation/AddressSanitizer.cpp
lib/Transforms/Instrumentation/SanitizerCoverage.cpp
lib/Transforms/ObjCARC/ObjCARCOpts.cpp
lib/Transforms/Scalar/LoopUnrollPass.cpp
lib/Transforms/Scalar/SROA.cpp
lib/Transforms/Scalar/ScalarReplAggregates.cpp
lib/Transforms/Utils/AddDiscriminators.cpp
lib/Transforms/Utils/CloneFunction.cpp
lib/Transforms/Utils/InlineFunction.cpp
lib/Transforms/Utils/Local.cpp
lib/Transforms/Utils/LoopUnrollRuntime.cpp
lib/Transforms/Utils/PromoteMemoryToRegister.cpp
lib/Transforms/Utils/SimplifyCFG.cpp
lib/Transforms/Utils/ValueMapper.cpp
lib/Transforms/Vectorize/LoopVectorize.cpp
test/Assembler/functionlocal-metadata-attachments.ll
test/Assembler/functionlocal-metadata-complex-3.ll
test/Feature/metadata.ll
test/Linker/Inputs/unique-fwd-decl-order.ll [new file with mode: 0644]
test/Linker/unique-fwd-decl-order.ll [new file with mode: 0644]
test/Transforms/GlobalOpt/metadata.ll
unittests/IR/MDBuilderTest.cpp
unittests/IR/MetadataTest.cpp