2014-03-04 11:26:31 +00:00
|
|
|
//===- ValueMap.h - Safe map from Values to data ----------------*- C++ -*-===//
|
2009-10-22 22:11:22 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-10-23 20:54:00 +00:00
|
|
|
// This file defines the ValueMap class. ValueMap maps Value* or any subclass
|
|
|
|
// to an arbitrary other type. It provides the DenseMap interface but updates
|
|
|
|
// itself to remain safe when keys are RAUWed or deleted. By default, when a
|
|
|
|
// key is RAUWed from V1 to V2, the old mapping V1->target is removed, and a new
|
|
|
|
// mapping V2->target is added. If V2 already existed, its old target is
|
|
|
|
// overwritten. When a key is deleted, its mapping is removed.
|
|
|
|
//
|
|
|
|
// You can override a ValueMap's Config parameter to control exactly what
|
|
|
|
// happens on RAUW and destruction and to get called back on each event. It's
|
|
|
|
// legal to call back into the ValueMap from a Config's callbacks. Config
|
|
|
|
// parameters should inherit from ValueMapConfig<KeyT> to get default
|
|
|
|
// implementations of all the methods ValueMap uses. See ValueMapConfig for
|
|
|
|
// documentation of the functions you can override.
|
2009-10-22 22:11:22 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-03-04 11:26:31 +00:00
|
|
|
#ifndef LLVM_IR_VALUEMAP_H
|
|
|
|
#define LLVM_IR_VALUEMAP_H
|
2009-10-22 22:11:22 +00:00
|
|
|
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
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
2014-12-09 18:38:53 +00:00
|
|
|
#include "llvm/IR/TrackingMDRef.h"
|
2014-03-04 11:17:44 +00:00
|
|
|
#include "llvm/IR/ValueHandle.h"
|
2014-06-20 21:07:14 +00:00
|
|
|
#include "llvm/Support/Mutex.h"
|
2014-08-23 23:07:14 +00:00
|
|
|
#include "llvm/Support/UniqueLock.h"
|
2009-10-22 22:11:22 +00:00
|
|
|
#include "llvm/Support/type_traits.h"
|
|
|
|
#include <iterator>
|
2014-12-09 18:59:09 +00:00
|
|
|
#include <memory>
|
2009-10-22 22:11:22 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2012-04-06 10:43:44 +00:00
|
|
|
template<typename KeyT, typename ValueT, typename Config>
|
2009-10-22 22:11:22 +00:00
|
|
|
class ValueMapCallbackVH;
|
|
|
|
|
|
|
|
template<typename DenseMapT, typename KeyT>
|
|
|
|
class ValueMapIterator;
|
|
|
|
template<typename DenseMapT, typename KeyT>
|
|
|
|
class ValueMapConstIterator;
|
|
|
|
|
2009-10-23 20:54:00 +00:00
|
|
|
/// This class defines the default behavior for configurable aspects of
|
|
|
|
/// ValueMap<>. User Configs should inherit from this class to be as compatible
|
|
|
|
/// as possible with future versions of ValueMap.
|
2014-06-20 21:07:14 +00:00
|
|
|
template<typename KeyT, typename MutexT = sys::Mutex>
|
2009-10-22 22:11:22 +00:00
|
|
|
struct ValueMapConfig {
|
2014-06-17 00:17:38 +00:00
|
|
|
typedef MutexT mutex_type;
|
|
|
|
|
2009-10-22 22:11:22 +00:00
|
|
|
/// If FollowRAUW is true, the ValueMap will update mappings on RAUW. If it's
|
|
|
|
/// false, the ValueMap will leave the original mapping in place.
|
|
|
|
enum { FollowRAUW = true };
|
|
|
|
|
|
|
|
// All methods will be called with a first argument of type ExtraData. The
|
|
|
|
// default implementations in this class take a templated first argument so
|
|
|
|
// that users' subclasses can use any type they want without having to
|
|
|
|
// override all the defaults.
|
|
|
|
struct ExtraData {};
|
|
|
|
|
|
|
|
template<typename ExtraDataT>
|
2010-06-24 20:14:34 +00:00
|
|
|
static void onRAUW(const ExtraDataT & /*Data*/, KeyT /*Old*/, KeyT /*New*/) {}
|
2009-10-22 22:11:22 +00:00
|
|
|
template<typename ExtraDataT>
|
2010-06-24 20:14:34 +00:00
|
|
|
static void onDelete(const ExtraDataT &/*Data*/, KeyT /*Old*/) {}
|
2009-10-22 22:11:22 +00:00
|
|
|
|
|
|
|
/// Returns a mutex that should be acquired around any changes to the map.
|
|
|
|
/// This is only acquired from the CallbackVH (and held around calls to onRAUW
|
2009-10-23 20:54:00 +00:00
|
|
|
/// and onDelete) and not inside other ValueMap methods. NULL means that no
|
2009-10-22 22:11:22 +00:00
|
|
|
/// mutex is necessary.
|
|
|
|
template<typename ExtraDataT>
|
2014-06-17 00:17:38 +00:00
|
|
|
static mutex_type *getMutex(const ExtraDataT &/*Data*/) { return nullptr; }
|
2009-10-22 22:11:22 +00:00
|
|
|
};
|
|
|
|
|
2009-10-23 20:54:00 +00:00
|
|
|
/// See the file comment.
|
2012-04-06 10:43:44 +00:00
|
|
|
template<typename KeyT, typename ValueT, typename Config =ValueMapConfig<KeyT> >
|
2009-10-22 22:11:22 +00:00
|
|
|
class ValueMap {
|
2012-04-06 10:43:44 +00:00
|
|
|
friend class ValueMapCallbackVH<KeyT, ValueT, Config>;
|
|
|
|
typedef ValueMapCallbackVH<KeyT, ValueT, Config> ValueMapCVH;
|
|
|
|
typedef DenseMap<ValueMapCVH, ValueT, DenseMapInfo<ValueMapCVH> > MapT;
|
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
2014-12-09 18:38:53 +00:00
|
|
|
typedef DenseMap<const Metadata *, TrackingMDRef> MDMapT;
|
2009-10-22 22:11:22 +00:00
|
|
|
typedef typename Config::ExtraData ExtraData;
|
|
|
|
MapT Map;
|
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
2014-12-09 18:38:53 +00:00
|
|
|
std::unique_ptr<MDMapT> MDMap;
|
2009-10-22 22:11:22 +00:00
|
|
|
ExtraData Data;
|
2015-02-15 22:54:22 +00:00
|
|
|
ValueMap(const ValueMap&) = delete;
|
|
|
|
ValueMap& operator=(const ValueMap&) = delete;
|
2009-10-22 22:11:22 +00:00
|
|
|
public:
|
|
|
|
typedef KeyT key_type;
|
|
|
|
typedef ValueT mapped_type;
|
|
|
|
typedef std::pair<KeyT, ValueT> value_type;
|
2014-06-20 12:20:56 +00:00
|
|
|
typedef unsigned size_type;
|
2009-10-22 22:11:22 +00:00
|
|
|
|
|
|
|
explicit ValueMap(unsigned NumInitBuckets = 64)
|
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
2014-12-09 18:38:53 +00:00
|
|
|
: Map(NumInitBuckets), Data() {}
|
2009-10-22 22:11:22 +00:00
|
|
|
explicit ValueMap(const ExtraData &Data, unsigned NumInitBuckets = 64)
|
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
2014-12-09 18:38:53 +00:00
|
|
|
: Map(NumInitBuckets), Data(Data) {}
|
2009-10-22 22:11:22 +00:00
|
|
|
|
|
|
|
~ValueMap() {}
|
|
|
|
|
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
2014-12-09 18:38:53 +00:00
|
|
|
bool hasMD() const { return MDMap; }
|
|
|
|
MDMapT &MD() {
|
|
|
|
if (!MDMap)
|
|
|
|
MDMap.reset(new MDMapT);
|
|
|
|
return *MDMap;
|
|
|
|
}
|
|
|
|
|
2009-10-22 22:11:22 +00:00
|
|
|
typedef ValueMapIterator<MapT, KeyT> iterator;
|
|
|
|
typedef ValueMapConstIterator<MapT, KeyT> const_iterator;
|
|
|
|
inline iterator begin() { return iterator(Map.begin()); }
|
|
|
|
inline iterator end() { return iterator(Map.end()); }
|
|
|
|
inline const_iterator begin() const { return const_iterator(Map.begin()); }
|
|
|
|
inline const_iterator end() const { return const_iterator(Map.end()); }
|
|
|
|
|
|
|
|
bool empty() const { return Map.empty(); }
|
2014-06-20 10:26:56 +00:00
|
|
|
size_type size() const { return Map.size(); }
|
2009-10-22 22:11:22 +00:00
|
|
|
|
|
|
|
/// Grow the map so that it has at least Size buckets. Does not shrink
|
|
|
|
void resize(size_t Size) { Map.resize(Size); }
|
|
|
|
|
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
2014-12-09 18:38:53 +00:00
|
|
|
void clear() {
|
|
|
|
Map.clear();
|
|
|
|
MDMap.reset();
|
|
|
|
}
|
2009-10-22 22:11:22 +00:00
|
|
|
|
2014-08-23 22:49:22 +00:00
|
|
|
/// Return 1 if the specified key is in the map, 0 otherwise.
|
2014-06-20 10:26:56 +00:00
|
|
|
size_type count(const KeyT &Val) const {
|
|
|
|
return Map.find_as(Val) == Map.end() ? 0 : 1;
|
2009-10-22 22:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iterator find(const KeyT &Val) {
|
2012-05-19 19:15:32 +00:00
|
|
|
return iterator(Map.find_as(Val));
|
2009-10-22 22:11:22 +00:00
|
|
|
}
|
|
|
|
const_iterator find(const KeyT &Val) const {
|
2012-05-19 19:15:32 +00:00
|
|
|
return const_iterator(Map.find_as(Val));
|
2009-10-22 22:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// lookup - Return the entry for the specified key, or a default
|
|
|
|
/// constructed value if no such entry exists.
|
|
|
|
ValueT lookup(const KeyT &Val) const {
|
2012-05-19 19:15:32 +00:00
|
|
|
typename MapT::const_iterator I = Map.find_as(Val);
|
|
|
|
return I != Map.end() ? I->second : ValueT();
|
2009-10-22 22:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Inserts key,value pair into the map if the key isn't already in the map.
|
|
|
|
// If the key is already in the map, it returns false and doesn't update the
|
|
|
|
// value.
|
|
|
|
std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
|
|
|
|
std::pair<typename MapT::iterator, bool> map_result=
|
|
|
|
Map.insert(std::make_pair(Wrap(KV.first), KV.second));
|
|
|
|
return std::make_pair(iterator(map_result.first), map_result.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// insert - Range insertion of pairs.
|
|
|
|
template<typename InputIt>
|
|
|
|
void insert(InputIt I, InputIt E) {
|
|
|
|
for (; I != E; ++I)
|
|
|
|
insert(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool erase(const KeyT &Val) {
|
2012-05-19 19:15:32 +00:00
|
|
|
typename MapT::iterator I = Map.find_as(Val);
|
|
|
|
if (I == Map.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Map.erase(I);
|
|
|
|
return true;
|
2009-10-22 22:11:22 +00:00
|
|
|
}
|
2010-09-01 14:00:35 +00:00
|
|
|
void erase(iterator I) {
|
2009-10-22 22:11:22 +00:00
|
|
|
return Map.erase(I.base());
|
|
|
|
}
|
|
|
|
|
|
|
|
value_type& FindAndConstruct(const KeyT &Key) {
|
|
|
|
return Map.FindAndConstruct(Wrap(Key));
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueT &operator[](const KeyT &Key) {
|
|
|
|
return Map[Wrap(Key)];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// isPointerIntoBucketsArray - Return true if the specified pointer points
|
|
|
|
/// somewhere into the ValueMap's array of buckets (i.e. either to a key or
|
|
|
|
/// value in the ValueMap).
|
|
|
|
bool isPointerIntoBucketsArray(const void *Ptr) const {
|
|
|
|
return Map.isPointerIntoBucketsArray(Ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getPointerIntoBucketsArray() - Return an opaque pointer into the buckets
|
|
|
|
/// array. In conjunction with the previous method, this can be used to
|
|
|
|
/// determine whether an insertion caused the ValueMap to reallocate.
|
|
|
|
const void *getPointerIntoBucketsArray() const {
|
|
|
|
return Map.getPointerIntoBucketsArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2009-10-23 20:54:00 +00:00
|
|
|
// Takes a key being looked up in the map and wraps it into a
|
|
|
|
// ValueMapCallbackVH, the actual key type of the map. We use a helper
|
|
|
|
// function because ValueMapCVH is constructed with a second parameter.
|
2009-10-22 22:11:22 +00:00
|
|
|
ValueMapCVH Wrap(KeyT key) const {
|
|
|
|
// The only way the resulting CallbackVH could try to modify *this (making
|
|
|
|
// the const_cast incorrect) is if it gets inserted into the map. But then
|
|
|
|
// this function must have been called from a non-const method, making the
|
|
|
|
// const_cast ok.
|
|
|
|
return ValueMapCVH(key, const_cast<ValueMap*>(this));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-10-23 20:54:00 +00:00
|
|
|
// This CallbackVH updates its ValueMap when the contained Value changes,
|
|
|
|
// according to the user's preferences expressed through the Config object.
|
2012-04-06 10:43:44 +00:00
|
|
|
template<typename KeyT, typename ValueT, typename Config>
|
2009-10-22 22:11:22 +00:00
|
|
|
class ValueMapCallbackVH : public CallbackVH {
|
2012-04-06 10:43:44 +00:00
|
|
|
friend class ValueMap<KeyT, ValueT, Config>;
|
2009-10-27 02:17:51 +00:00
|
|
|
friend struct DenseMapInfo<ValueMapCallbackVH>;
|
2012-04-06 10:43:44 +00:00
|
|
|
typedef ValueMap<KeyT, ValueT, Config> ValueMapT;
|
2014-03-07 14:42:25 +00:00
|
|
|
typedef typename std::remove_pointer<KeyT>::type KeySansPointerT;
|
2009-10-22 22:11:22 +00:00
|
|
|
|
|
|
|
ValueMapT *Map;
|
|
|
|
|
|
|
|
ValueMapCallbackVH(KeyT Key, ValueMapT *Map)
|
|
|
|
: CallbackVH(const_cast<Value*>(static_cast<const Value*>(Key))),
|
|
|
|
Map(Map) {}
|
|
|
|
|
2015-01-09 23:17:25 +00:00
|
|
|
// Private constructor used to create empty/tombstone DenseMap keys.
|
|
|
|
ValueMapCallbackVH(Value *V) : CallbackVH(V), Map(nullptr) {}
|
|
|
|
|
2009-10-22 22:11:22 +00:00
|
|
|
public:
|
|
|
|
KeyT Unwrap() const { return cast_or_null<KeySansPointerT>(getValPtr()); }
|
|
|
|
|
2014-03-05 07:52:44 +00:00
|
|
|
void deleted() override {
|
2009-10-22 22:11:22 +00:00
|
|
|
// Make a copy that won't get changed even when *this is destroyed.
|
|
|
|
ValueMapCallbackVH Copy(*this);
|
2014-06-17 00:17:38 +00:00
|
|
|
typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data);
|
2014-08-23 23:07:14 +00:00
|
|
|
unique_lock<typename Config::mutex_type> Guard;
|
2009-10-22 22:11:22 +00:00
|
|
|
if (M)
|
2014-08-23 23:07:14 +00:00
|
|
|
Guard = unique_lock<typename Config::mutex_type>(*M);
|
2009-10-23 20:54:00 +00:00
|
|
|
Config::onDelete(Copy.Map->Data, Copy.Unwrap()); // May destroy *this.
|
2009-10-22 22:11:22 +00:00
|
|
|
Copy.Map->Map.erase(Copy); // Definitely destroys *this.
|
|
|
|
}
|
2014-03-05 07:52:44 +00:00
|
|
|
void allUsesReplacedWith(Value *new_key) override {
|
2009-10-22 22:11:22 +00:00
|
|
|
assert(isa<KeySansPointerT>(new_key) &&
|
|
|
|
"Invalid RAUW on key of ValueMap<>");
|
|
|
|
// Make a copy that won't get changed even when *this is destroyed.
|
|
|
|
ValueMapCallbackVH Copy(*this);
|
2014-06-17 00:17:38 +00:00
|
|
|
typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data);
|
2014-08-23 23:07:14 +00:00
|
|
|
unique_lock<typename Config::mutex_type> Guard;
|
2009-10-22 22:11:22 +00:00
|
|
|
if (M)
|
2014-08-23 23:07:14 +00:00
|
|
|
Guard = unique_lock<typename Config::mutex_type>(*M);
|
2009-10-22 22:11:22 +00:00
|
|
|
|
|
|
|
KeyT typed_new_key = cast<KeySansPointerT>(new_key);
|
|
|
|
// Can destroy *this:
|
|
|
|
Config::onRAUW(Copy.Map->Data, Copy.Unwrap(), typed_new_key);
|
|
|
|
if (Config::FollowRAUW) {
|
|
|
|
typename ValueMapT::MapT::iterator I = Copy.Map->Map.find(Copy);
|
|
|
|
// I could == Copy.Map->Map.end() if the onRAUW callback already
|
|
|
|
// removed the old mapping.
|
|
|
|
if (I != Copy.Map->Map.end()) {
|
|
|
|
ValueT Target(I->second);
|
|
|
|
Copy.Map->Map.erase(I); // Definitely destroys *this.
|
|
|
|
Copy.Map->insert(std::make_pair(typed_new_key, Target));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-04-06 10:43:44 +00:00
|
|
|
template<typename KeyT, typename ValueT, typename Config>
|
|
|
|
struct DenseMapInfo<ValueMapCallbackVH<KeyT, ValueT, Config> > {
|
|
|
|
typedef ValueMapCallbackVH<KeyT, ValueT, Config> VH;
|
2009-10-22 22:11:22 +00:00
|
|
|
|
|
|
|
static inline VH getEmptyKey() {
|
2015-01-09 23:17:25 +00:00
|
|
|
return VH(DenseMapInfo<Value *>::getEmptyKey());
|
2009-10-22 22:11:22 +00:00
|
|
|
}
|
|
|
|
static inline VH getTombstoneKey() {
|
2015-01-09 23:17:25 +00:00
|
|
|
return VH(DenseMapInfo<Value *>::getTombstoneKey());
|
2009-10-22 22:11:22 +00:00
|
|
|
}
|
|
|
|
static unsigned getHashValue(const VH &Val) {
|
2015-01-09 23:17:25 +00:00
|
|
|
return DenseMapInfo<KeyT>::getHashValue(Val.Unwrap());
|
2009-10-22 22:11:22 +00:00
|
|
|
}
|
2012-05-19 19:15:32 +00:00
|
|
|
static unsigned getHashValue(const KeyT &Val) {
|
2015-01-09 23:17:25 +00:00
|
|
|
return DenseMapInfo<KeyT>::getHashValue(Val);
|
2012-05-19 19:15:32 +00:00
|
|
|
}
|
2009-10-22 22:11:22 +00:00
|
|
|
static bool isEqual(const VH &LHS, const VH &RHS) {
|
|
|
|
return LHS == RHS;
|
|
|
|
}
|
2012-05-19 19:15:32 +00:00
|
|
|
static bool isEqual(const KeyT &LHS, const VH &RHS) {
|
2012-05-19 19:32:11 +00:00
|
|
|
return LHS == RHS.getValPtr();
|
2012-05-19 19:15:32 +00:00
|
|
|
}
|
2009-10-22 22:11:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<typename DenseMapT, typename KeyT>
|
|
|
|
class ValueMapIterator :
|
|
|
|
public std::iterator<std::forward_iterator_tag,
|
|
|
|
std::pair<KeyT, typename DenseMapT::mapped_type>,
|
|
|
|
ptrdiff_t> {
|
|
|
|
typedef typename DenseMapT::iterator BaseT;
|
|
|
|
typedef typename DenseMapT::mapped_type ValueT;
|
|
|
|
BaseT I;
|
|
|
|
public:
|
|
|
|
ValueMapIterator() : I() {}
|
|
|
|
|
|
|
|
ValueMapIterator(BaseT I) : I(I) {}
|
|
|
|
|
|
|
|
BaseT base() const { return I; }
|
|
|
|
|
|
|
|
struct ValueTypeProxy {
|
|
|
|
const KeyT first;
|
|
|
|
ValueT& second;
|
2009-10-23 20:54:00 +00:00
|
|
|
ValueTypeProxy *operator->() { return this; }
|
2009-10-22 22:11:22 +00:00
|
|
|
operator std::pair<KeyT, ValueT>() const {
|
|
|
|
return std::make_pair(first, second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ValueTypeProxy operator*() const {
|
|
|
|
ValueTypeProxy Result = {I->first.Unwrap(), I->second};
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueTypeProxy operator->() const {
|
|
|
|
return operator*();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const ValueMapIterator &RHS) const {
|
|
|
|
return I == RHS.I;
|
|
|
|
}
|
|
|
|
bool operator!=(const ValueMapIterator &RHS) const {
|
|
|
|
return I != RHS.I;
|
|
|
|
}
|
|
|
|
|
2009-10-23 20:54:00 +00:00
|
|
|
inline ValueMapIterator& operator++() { // Preincrement
|
2009-10-22 22:11:22 +00:00
|
|
|
++I;
|
|
|
|
return *this;
|
|
|
|
}
|
2009-10-23 20:54:00 +00:00
|
|
|
ValueMapIterator operator++(int) { // Postincrement
|
2009-10-22 22:11:22 +00:00
|
|
|
ValueMapIterator tmp = *this; ++*this; return tmp;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename DenseMapT, typename KeyT>
|
|
|
|
class ValueMapConstIterator :
|
|
|
|
public std::iterator<std::forward_iterator_tag,
|
|
|
|
std::pair<KeyT, typename DenseMapT::mapped_type>,
|
|
|
|
ptrdiff_t> {
|
|
|
|
typedef typename DenseMapT::const_iterator BaseT;
|
|
|
|
typedef typename DenseMapT::mapped_type ValueT;
|
|
|
|
BaseT I;
|
|
|
|
public:
|
|
|
|
ValueMapConstIterator() : I() {}
|
|
|
|
ValueMapConstIterator(BaseT I) : I(I) {}
|
|
|
|
ValueMapConstIterator(ValueMapIterator<DenseMapT, KeyT> Other)
|
|
|
|
: I(Other.base()) {}
|
|
|
|
|
|
|
|
BaseT base() const { return I; }
|
|
|
|
|
|
|
|
struct ValueTypeProxy {
|
|
|
|
const KeyT first;
|
|
|
|
const ValueT& second;
|
2009-10-23 20:54:00 +00:00
|
|
|
ValueTypeProxy *operator->() { return this; }
|
2009-10-22 22:11:22 +00:00
|
|
|
operator std::pair<KeyT, ValueT>() const {
|
|
|
|
return std::make_pair(first, second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ValueTypeProxy operator*() const {
|
|
|
|
ValueTypeProxy Result = {I->first.Unwrap(), I->second};
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueTypeProxy operator->() const {
|
|
|
|
return operator*();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const ValueMapConstIterator &RHS) const {
|
|
|
|
return I == RHS.I;
|
|
|
|
}
|
|
|
|
bool operator!=(const ValueMapConstIterator &RHS) const {
|
|
|
|
return I != RHS.I;
|
|
|
|
}
|
|
|
|
|
2009-10-23 20:54:00 +00:00
|
|
|
inline ValueMapConstIterator& operator++() { // Preincrement
|
2009-10-22 22:11:22 +00:00
|
|
|
++I;
|
|
|
|
return *this;
|
|
|
|
}
|
2009-10-23 20:54:00 +00:00
|
|
|
ValueMapConstIterator operator++(int) { // Postincrement
|
2009-10-22 22:11:22 +00:00
|
|
|
ValueMapConstIterator tmp = *this; ++*this; return tmp;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|