2001-06-06 20:29:01 +00:00
|
|
|
//===-- Value.cpp - Implement the Value class -----------------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2009-03-31 22:11:05 +00:00
|
|
|
// This file implements the Value, ValueHandle, and User classes.
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Value.h"
|
2009-08-18 18:28:58 +00:00
|
|
|
#include "LLVMContextImpl.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2014-07-18 15:51:28 +00:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constant.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
2014-07-10 05:27:53 +00:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2014-03-04 10:40:04 +00:00
|
|
|
#include "llvm/IR/GetElementPtrTypeIterator.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/InstrTypes.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2015-02-09 21:08:03 +00:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Operator.h"
|
2015-02-09 21:08:03 +00:00
|
|
|
#include "llvm/IR/Statepoint.h"
|
2014-03-04 11:17:44 +00:00
|
|
|
#include "llvm/IR/ValueHandle.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/ValueSymbolTable.h"
|
2006-11-17 08:03:48 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-08 18:01:40 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-03-31 22:11:05 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2015-03-23 19:32:43 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include <algorithm>
|
2003-11-21 20:23:48 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Value Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
static inline Type *checkType(Type *Ty) {
|
2001-12-13 00:41:27 +00:00
|
|
|
assert(Ty && "Value defined with a null type: Error!");
|
2014-06-09 23:32:20 +00:00
|
|
|
return Ty;
|
2001-12-13 00:41:27 +00:00
|
|
|
}
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Value::Value(Type *ty, unsigned scid)
|
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
|
|
|
: VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), HasValueHandle(0),
|
|
|
|
SubclassOptionalData(0), SubclassData(0), NumOperands(0) {
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
// FIXME: Why isn't this in the subclass gunk??
|
2012-12-20 04:11:02 +00:00
|
|
|
// Note, we cannot call isa<CallInst> before the CallInst has been
|
|
|
|
// constructed.
|
|
|
|
if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke)
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) &&
|
|
|
|
"invalid CallInst type!");
|
2012-12-20 04:11:02 +00:00
|
|
|
else if (SubclassID != BasicBlockVal &&
|
|
|
|
(SubclassID < ConstantFirstVal || SubclassID > ConstantLastVal))
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
assert((VTy->isFirstClassType() || VTy->isVoidTy()) &&
|
2004-07-06 17:44:17 +00:00
|
|
|
"Cannot create non-first-class values except for constants!");
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2007-12-10 02:14:30 +00:00
|
|
|
Value::~Value() {
|
2009-03-31 22:11:05 +00:00
|
|
|
// Notify all ValueHandles (if present) that this value is going away.
|
|
|
|
if (HasValueHandle)
|
|
|
|
ValueHandleBase::ValueIsDeleted(this);
|
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
|
|
|
if (isUsedByMetadata())
|
|
|
|
ValueAsMetadata::handleDeletion(this);
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
#ifndef NDEBUG // Only in -g mode...
|
2001-06-11 15:04:40 +00:00
|
|
|
// Check to make sure that there are no uses of this value that are still
|
|
|
|
// around when the value is destroyed. If there are, then we have a dangling
|
2015-03-10 23:55:38 +00:00
|
|
|
// reference and something is wrong. This code is here to print out where
|
|
|
|
// the value is still being referenced.
|
2001-06-11 15:04:40 +00:00
|
|
|
//
|
2007-12-10 02:14:30 +00:00
|
|
|
if (!use_empty()) {
|
2011-11-15 16:27:03 +00:00
|
|
|
dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n";
|
2015-03-10 23:55:38 +00:00
|
|
|
for (auto *U : users())
|
|
|
|
dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
#endif
|
2007-12-10 02:14:30 +00:00
|
|
|
assert(use_empty() && "Uses remain when a value is destroyed!");
|
2009-08-04 23:07:12 +00:00
|
|
|
|
|
|
|
// If this value is named, destroy the name. This should not be in a symtab
|
|
|
|
// at this point.
|
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
|
|
|
destroyValueName();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
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 Value::destroyValueName() {
|
|
|
|
ValueName *Name = getValueName();
|
|
|
|
if (Name)
|
|
|
|
Name->Destroy();
|
|
|
|
setValueName(nullptr);
|
|
|
|
}
|
|
|
|
|
2005-02-01 01:24:21 +00:00
|
|
|
bool Value::hasNUses(unsigned N) const {
|
2010-03-25 23:06:16 +00:00
|
|
|
const_use_iterator UI = use_begin(), E = use_end();
|
2005-02-01 01:24:21 +00:00
|
|
|
|
|
|
|
for (; N; --N, ++UI)
|
|
|
|
if (UI == E) return false; // Too few.
|
|
|
|
return UI == E;
|
|
|
|
}
|
|
|
|
|
2005-02-23 16:51:11 +00:00
|
|
|
bool Value::hasNUsesOrMore(unsigned N) const {
|
2010-03-25 23:06:16 +00:00
|
|
|
const_use_iterator UI = use_begin(), E = use_end();
|
2005-02-23 16:51:11 +00:00
|
|
|
|
|
|
|
for (; N; --N, ++UI)
|
|
|
|
if (UI == E) return false; // Too few.
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-25 22:42:01 +00:00
|
|
|
bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
|
2013-05-14 23:45:56 +00:00
|
|
|
// This can be computed either by scanning the instructions in BB, or by
|
|
|
|
// scanning the use list of this Value. Both lists can be very long, but
|
|
|
|
// usually one is quite short.
|
|
|
|
//
|
|
|
|
// Scan both lists simultaneously until one is exhausted. This limits the
|
|
|
|
// search to the shorter list.
|
|
|
|
BasicBlock::const_iterator BI = BB->begin(), BE = BB->end();
|
2014-03-09 03:16:01 +00:00
|
|
|
const_user_iterator UI = user_begin(), UE = user_end();
|
2013-05-14 23:45:56 +00:00
|
|
|
for (; BI != BE && UI != UE; ++BI, ++UI) {
|
|
|
|
// Scan basic block: Check if this Value is used by the instruction at BI.
|
|
|
|
if (std::find(BI->op_begin(), BI->op_end(), this) != BI->op_end())
|
2011-12-05 17:23:27 +00:00
|
|
|
return true;
|
2013-05-14 23:45:56 +00:00
|
|
|
// Scan use list: Check if the use at UI is in BB.
|
|
|
|
const Instruction *User = dyn_cast<Instruction>(*UI);
|
2008-06-12 21:15:59 +00:00
|
|
|
if (User && User->getParent() == BB)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-02-01 01:24:21 +00:00
|
|
|
unsigned Value::getNumUses() const {
|
|
|
|
return (unsigned)std::distance(use_begin(), use_end());
|
|
|
|
}
|
|
|
|
|
2007-02-11 00:37:27 +00:00
|
|
|
static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
|
2014-04-09 06:08:46 +00:00
|
|
|
ST = nullptr;
|
2007-02-11 00:37:27 +00:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V)) {
|
2005-03-05 19:51:50 +00:00
|
|
|
if (BasicBlock *P = I->getParent())
|
|
|
|
if (Function *PP = P->getParent())
|
2007-01-06 07:24:44 +00:00
|
|
|
ST = &PP->getValueSymbolTable();
|
2007-02-11 00:37:27 +00:00
|
|
|
} else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
|
2009-09-20 04:03:34 +00:00
|
|
|
if (Function *P = BB->getParent())
|
2007-02-05 20:47:22 +00:00
|
|
|
ST = &P->getValueSymbolTable();
|
2007-02-11 00:37:27 +00:00
|
|
|
} else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
|
2009-09-20 04:03:34 +00:00
|
|
|
if (Module *P = GV->getParent())
|
2007-02-05 20:47:22 +00:00
|
|
|
ST = &P->getValueSymbolTable();
|
2007-02-11 00:37:27 +00:00
|
|
|
} else if (Argument *A = dyn_cast<Argument>(V)) {
|
2009-09-20 04:03:34 +00:00
|
|
|
if (Function *P = A->getParent())
|
2007-02-05 20:47:22 +00:00
|
|
|
ST = &P->getValueSymbolTable();
|
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
|
|
|
} else {
|
2007-02-11 00:37:27 +00:00
|
|
|
assert(isa<Constant>(V) && "Unknown value type!");
|
|
|
|
return true; // no name is setable for this.
|
2005-03-05 19:51:50 +00:00
|
|
|
}
|
2007-02-11 00:37:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-26 00:51:56 +00:00
|
|
|
StringRef Value::getName() const {
|
2009-07-26 09:22:02 +00:00
|
|
|
// 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
|
|
|
|
// terminated.
|
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
|
|
|
if (!getValueName())
|
|
|
|
return StringRef("", 0);
|
|
|
|
return getValueName()->getKey();
|
2007-08-10 15:34:35 +00:00
|
|
|
}
|
|
|
|
|
2009-07-26 00:34:27 +00:00
|
|
|
void Value::setName(const Twine &NewName) {
|
2009-08-19 23:37:23 +00:00
|
|
|
// Fast path for common IRBuilder case of setName("") when there is no name.
|
|
|
|
if (NewName.isTriviallyEmpty() && !hasName())
|
|
|
|
return;
|
|
|
|
|
2009-08-19 05:08:06 +00:00
|
|
|
SmallString<256> NameData;
|
2010-01-13 12:45:23 +00:00
|
|
|
StringRef NameRef = NewName.toStringRef(NameData);
|
2013-11-19 21:12:39 +00:00
|
|
|
assert(NameRef.find_first_of(0) == StringRef::npos &&
|
|
|
|
"Null bytes are not allowed in names");
|
2007-02-12 18:52:59 +00:00
|
|
|
|
2009-07-26 00:42:33 +00:00
|
|
|
// Name isn't changing?
|
2010-01-13 12:45:23 +00:00
|
|
|
if (getName() == NameRef)
|
2009-07-26 00:42:33 +00:00
|
|
|
return;
|
|
|
|
|
2010-01-05 13:12:22 +00:00
|
|
|
assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2007-02-11 00:37:27 +00:00
|
|
|
// Get the symbol table to update for this object.
|
|
|
|
ValueSymbolTable *ST;
|
|
|
|
if (getSymTab(this, ST))
|
|
|
|
return; // Cannot set a name on this value (e.g. constant).
|
2005-03-05 19:51:50 +00:00
|
|
|
|
2013-03-01 18:48:54 +00:00
|
|
|
if (Function *F = dyn_cast<Function>(this))
|
|
|
|
getContext().pImpl->IntrinsicIDCache.erase(F);
|
|
|
|
|
2007-02-12 05:18:08 +00:00
|
|
|
if (!ST) { // No symbol table to update? Just do the change.
|
2010-01-13 12:45:23 +00:00
|
|
|
if (NameRef.empty()) {
|
2007-02-12 05:18:08 +00:00
|
|
|
// Free the name for this value.
|
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
|
|
|
destroyValueName();
|
2007-02-12 18:52:59 +00:00
|
|
|
return;
|
2005-03-06 02:14:28 +00:00
|
|
|
}
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2007-02-12 18:52:59 +00:00
|
|
|
// NOTE: Could optimize for the case the name is shrinking to not deallocate
|
|
|
|
// then reallocated.
|
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
|
|
|
destroyValueName();
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2007-02-12 18:52:59 +00:00
|
|
|
// Create the new name.
|
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
|
|
|
setValueName(ValueName::Create(NameRef));
|
|
|
|
getValueName()->setValue(this);
|
2007-02-12 05:18:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2007-02-12 05:18:08 +00:00
|
|
|
// NOTE: Could optimize for the case the name is shrinking to not deallocate
|
|
|
|
// then reallocated.
|
|
|
|
if (hasName()) {
|
|
|
|
// Remove old name.
|
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
|
|
|
ST->removeValueName(getValueName());
|
|
|
|
destroyValueName();
|
2007-02-12 05:18:08 +00:00
|
|
|
|
2010-01-13 12:45:23 +00:00
|
|
|
if (NameRef.empty())
|
2007-02-12 18:52:59 +00:00
|
|
|
return;
|
2005-03-06 02:14:28 +00:00
|
|
|
}
|
2007-02-12 05:18:08 +00:00
|
|
|
|
|
|
|
// Name is changing to something new.
|
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
|
|
|
setValueName(ST->createValueName(NameRef, this));
|
2005-03-05 19:51:50 +00:00
|
|
|
}
|
|
|
|
|
2007-02-11 00:37:27 +00:00
|
|
|
void Value::takeName(Value *V) {
|
2014-04-09 06:08:46 +00:00
|
|
|
ValueSymbolTable *ST = nullptr;
|
2007-02-15 20:01:43 +00:00
|
|
|
// If this value has a name, drop it.
|
|
|
|
if (hasName()) {
|
|
|
|
// Get the symtab this is in.
|
|
|
|
if (getSymTab(this, ST)) {
|
|
|
|
// We can't set a name on this value, but we need to clear V's name if
|
|
|
|
// it has one.
|
2009-07-26 00:34:27 +00:00
|
|
|
if (V->hasName()) V->setName("");
|
2007-02-15 20:01:43 +00:00
|
|
|
return; // Cannot set a name on this value (e.g. constant).
|
|
|
|
}
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2007-02-15 20:01:43 +00:00
|
|
|
// Remove old name.
|
|
|
|
if (ST)
|
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
|
|
|
ST->removeValueName(getValueName());
|
|
|
|
destroyValueName();
|
2009-09-20 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2007-02-15 20:01:43 +00:00
|
|
|
// Now we know that this has no name.
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2007-02-15 20:01:43 +00:00
|
|
|
// If V has no name either, we're done.
|
|
|
|
if (!V->hasName()) return;
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2007-02-15 20:01:43 +00:00
|
|
|
// Get this's symtab if we didn't before.
|
|
|
|
if (!ST) {
|
|
|
|
if (getSymTab(this, ST)) {
|
|
|
|
// Clear V's name.
|
2009-07-26 00:34:27 +00:00
|
|
|
V->setName("");
|
2007-02-15 20:01:43 +00:00
|
|
|
return; // Cannot set a name on this value (e.g. constant).
|
|
|
|
}
|
|
|
|
}
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2007-02-15 20:01:43 +00:00
|
|
|
// Get V's ST, this should always succed, because V has a name.
|
|
|
|
ValueSymbolTable *VST;
|
|
|
|
bool Failure = getSymTab(V, VST);
|
2010-12-23 00:58:24 +00:00
|
|
|
assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure;
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2007-02-15 20:01:43 +00:00
|
|
|
// If these values are both in the same symtab, we can do this very fast.
|
|
|
|
// This works even if both values have no symtab yet.
|
|
|
|
if (ST == VST) {
|
|
|
|
// Take the name!
|
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
|
|
|
setValueName(V->getValueName());
|
|
|
|
V->setValueName(nullptr);
|
|
|
|
getValueName()->setValue(this);
|
2007-02-11 01:04:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2007-02-15 20:01:43 +00:00
|
|
|
// Otherwise, things are slightly more complex. Remove V's name from VST and
|
|
|
|
// then reinsert it into ST.
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2007-02-15 20:01:43 +00:00
|
|
|
if (VST)
|
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
|
|
|
VST->removeValueName(V->getValueName());
|
|
|
|
setValueName(V->getValueName());
|
|
|
|
V->setValueName(nullptr);
|
|
|
|
getValueName()->setValue(this);
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2007-02-15 20:01:43 +00:00
|
|
|
if (ST)
|
|
|
|
ST->reinsertValue(this);
|
2007-02-11 00:37:27 +00:00
|
|
|
}
|
|
|
|
|
2014-05-13 01:23:21 +00:00
|
|
|
#ifndef NDEBUG
|
2014-08-21 05:55:13 +00:00
|
|
|
static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr,
|
2014-05-13 01:23:21 +00:00
|
|
|
Constant *C) {
|
2014-11-19 07:49:26 +00:00
|
|
|
if (!Cache.insert(Expr).second)
|
2014-05-13 01:23:21 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for (auto &O : Expr->operands()) {
|
|
|
|
if (O == C)
|
|
|
|
return true;
|
|
|
|
auto *CE = dyn_cast<ConstantExpr>(O);
|
|
|
|
if (!CE)
|
|
|
|
continue;
|
|
|
|
if (contains(Cache, CE, C))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool contains(Value *Expr, Value *V) {
|
|
|
|
if (Expr == V)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto *C = dyn_cast<Constant>(V);
|
|
|
|
if (!C)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto *CE = dyn_cast<ConstantExpr>(Expr);
|
|
|
|
if (!CE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SmallPtrSet<ConstantExpr *, 4> Cache;
|
|
|
|
return contains(Cache, CE, C);
|
|
|
|
}
|
|
|
|
#endif
|
2007-02-11 00:37:27 +00:00
|
|
|
|
2011-07-15 06:18:52 +00:00
|
|
|
void Value::replaceAllUsesWith(Value *New) {
|
|
|
|
assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
|
2014-05-13 01:23:21 +00:00
|
|
|
assert(!contains(New, this) &&
|
|
|
|
"this->replaceAllUsesWith(expr(this)) is NOT valid!");
|
2011-07-15 06:18:52 +00:00
|
|
|
assert(New->getType() == getType() &&
|
|
|
|
"replaceAllUses of value with new value of different type!");
|
|
|
|
|
2009-03-31 22:11:05 +00:00
|
|
|
// Notify all ValueHandles (if present) that this value is going away.
|
|
|
|
if (HasValueHandle)
|
|
|
|
ValueHandleBase::ValueIsRAUWd(this, New);
|
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
|
|
|
if (isUsedByMetadata())
|
|
|
|
ValueAsMetadata::handleRAUW(this, New);
|
2013-03-01 18:48:54 +00:00
|
|
|
|
2005-02-01 01:24:21 +00:00
|
|
|
while (!use_empty()) {
|
2008-09-19 15:13:20 +00:00
|
|
|
Use &U = *UseList;
|
2003-08-29 05:09:37 +00:00
|
|
|
// Must handle Constants specially, we cannot call replaceUsesOfWith on a
|
2007-08-21 00:21:07 +00:00
|
|
|
// constant because they are uniqued.
|
2014-05-16 19:35:39 +00:00
|
|
|
if (auto *C = dyn_cast<Constant>(U.getUser())) {
|
2007-08-21 00:21:07 +00:00
|
|
|
if (!isa<GlobalValue>(C)) {
|
2005-10-04 18:13:04 +00:00
|
|
|
C->replaceUsesOfWithOnConstant(this, New, &U);
|
2007-08-21 00:21:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2003-08-29 05:09:37 +00:00
|
|
|
}
|
2013-03-01 18:48:54 +00:00
|
|
|
|
2007-08-21 00:21:07 +00:00
|
|
|
U.set(New);
|
2003-08-29 05:09:37 +00:00
|
|
|
}
|
2013-03-01 18:48:54 +00:00
|
|
|
|
2011-06-23 09:09:15 +00:00
|
|
|
if (BasicBlock *BB = dyn_cast<BasicBlock>(this))
|
|
|
|
BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
|
2003-08-29 05:09:37 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 23:36:44 +00:00
|
|
|
// Like replaceAllUsesWith except it does not handle constants or basic blocks.
|
|
|
|
// This routine leaves uses within BB.
|
|
|
|
void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) {
|
|
|
|
assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!");
|
|
|
|
assert(!contains(New, this) &&
|
|
|
|
"this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!");
|
|
|
|
assert(New->getType() == getType() &&
|
|
|
|
"replaceUses of value with new value of different type!");
|
|
|
|
assert(BB && "Basic block that may contain a use of 'New' must be defined\n");
|
|
|
|
|
|
|
|
use_iterator UI = use_begin(), E = use_end();
|
|
|
|
for (; UI != E;) {
|
|
|
|
Use &U = *UI;
|
|
|
|
++UI;
|
|
|
|
auto *Usr = dyn_cast<Instruction>(U.getUser());
|
|
|
|
if (Usr && Usr->getParent() == BB)
|
|
|
|
continue;
|
|
|
|
U.set(New);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-10 08:39:09 +00:00
|
|
|
namespace {
|
|
|
|
// Various metrics for how much to strip off of pointers.
|
|
|
|
enum PointerStripKind {
|
|
|
|
PSK_ZeroIndices,
|
2013-05-06 01:48:55 +00:00
|
|
|
PSK_ZeroIndicesAndAliases,
|
2012-03-14 23:19:53 +00:00
|
|
|
PSK_InBoundsConstantIndices,
|
|
|
|
PSK_InBounds
|
2012-03-10 08:39:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <PointerStripKind StripKind>
|
|
|
|
static Value *stripPointerCastsAndOffsets(Value *V) {
|
|
|
|
if (!V->getType()->isPointerTy())
|
|
|
|
return V;
|
2010-06-28 21:16:52 +00:00
|
|
|
|
|
|
|
// Even though we don't look through PHI nodes, we could be called on an
|
|
|
|
// instruction in an unreachable block, which may be on a cycle.
|
|
|
|
SmallPtrSet<Value *, 4> Visited;
|
|
|
|
|
|
|
|
Visited.insert(V);
|
2008-12-29 21:06:19 +00:00
|
|
|
do {
|
2009-07-17 23:55:56 +00:00
|
|
|
if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
|
2012-03-10 08:39:09 +00:00
|
|
|
switch (StripKind) {
|
2013-05-06 01:48:55 +00:00
|
|
|
case PSK_ZeroIndicesAndAliases:
|
2012-03-10 08:39:09 +00:00
|
|
|
case PSK_ZeroIndices:
|
|
|
|
if (!GEP->hasAllZeroIndices())
|
|
|
|
return V;
|
|
|
|
break;
|
2012-03-14 23:19:53 +00:00
|
|
|
case PSK_InBoundsConstantIndices:
|
2012-03-10 08:39:09 +00:00
|
|
|
if (!GEP->hasAllConstantIndices())
|
|
|
|
return V;
|
2012-03-14 23:19:53 +00:00
|
|
|
// fallthrough
|
2012-03-10 08:39:09 +00:00
|
|
|
case PSK_InBounds:
|
|
|
|
if (!GEP->isInBounds())
|
|
|
|
return V;
|
|
|
|
break;
|
|
|
|
}
|
2009-07-17 23:55:56 +00:00
|
|
|
V = GEP->getPointerOperand();
|
2013-11-15 01:34:59 +00:00
|
|
|
} else if (Operator::getOpcode(V) == Instruction::BitCast ||
|
|
|
|
Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
|
2009-07-17 23:55:56 +00:00
|
|
|
V = cast<Operator>(V)->getOperand(0);
|
2009-08-27 17:55:13 +00:00
|
|
|
} else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
|
2013-05-06 01:48:55 +00:00
|
|
|
if (StripKind == PSK_ZeroIndices || GA->mayBeOverridden())
|
2009-08-27 17:55:13 +00:00
|
|
|
return V;
|
|
|
|
V = GA->getAliasee();
|
2008-12-29 21:06:19 +00:00
|
|
|
} else {
|
|
|
|
return V;
|
2008-05-07 22:54:15 +00:00
|
|
|
}
|
2010-02-16 11:11:14 +00:00
|
|
|
assert(V->getType()->isPointerTy() && "Unexpected operand type!");
|
2014-11-19 07:49:26 +00:00
|
|
|
} while (Visited.insert(V).second);
|
2010-06-28 21:16:52 +00:00
|
|
|
|
|
|
|
return V;
|
2008-05-07 22:54:15 +00:00
|
|
|
}
|
2012-03-10 08:39:09 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Value *Value::stripPointerCasts() {
|
2013-05-06 01:48:55 +00:00
|
|
|
return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *Value::stripPointerCastsNoFollowAliases() {
|
2012-03-10 08:39:09 +00:00
|
|
|
return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);
|
|
|
|
}
|
|
|
|
|
2012-03-14 23:19:53 +00:00
|
|
|
Value *Value::stripInBoundsConstantOffsets() {
|
|
|
|
return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);
|
2012-03-10 08:39:09 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 11:25:11 +00:00
|
|
|
Value *Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
|
|
|
|
APInt &Offset) {
|
|
|
|
if (!getType()->isPointerTy())
|
|
|
|
return this;
|
|
|
|
|
|
|
|
assert(Offset.getBitWidth() == DL.getPointerSizeInBits(cast<PointerType>(
|
|
|
|
getType())->getAddressSpace()) &&
|
|
|
|
"The offset must have exactly as many bits as our pointer.");
|
|
|
|
|
|
|
|
// Even though we don't look through PHI nodes, we could be called on an
|
|
|
|
// instruction in an unreachable block, which may be on a cycle.
|
|
|
|
SmallPtrSet<Value *, 4> Visited;
|
|
|
|
Visited.insert(this);
|
|
|
|
Value *V = this;
|
|
|
|
do {
|
|
|
|
if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
|
|
|
|
if (!GEP->isInBounds())
|
|
|
|
return V;
|
2013-08-25 10:46:39 +00:00
|
|
|
APInt GEPOffset(Offset);
|
|
|
|
if (!GEP->accumulateConstantOffset(DL, GEPOffset))
|
2013-08-22 11:25:11 +00:00
|
|
|
return V;
|
2013-08-25 10:46:39 +00:00
|
|
|
Offset = GEPOffset;
|
2013-08-22 11:25:11 +00:00
|
|
|
V = GEP->getPointerOperand();
|
2014-07-19 03:32:02 +00:00
|
|
|
} else if (Operator::getOpcode(V) == Instruction::BitCast ||
|
|
|
|
Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
|
2013-08-22 11:25:11 +00:00
|
|
|
V = cast<Operator>(V)->getOperand(0);
|
|
|
|
} else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
|
|
|
|
V = GA->getAliasee();
|
|
|
|
} else {
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
assert(V->getType()->isPointerTy() && "Unexpected operand type!");
|
2014-11-19 07:49:26 +00:00
|
|
|
} while (Visited.insert(V).second);
|
2013-08-22 11:25:11 +00:00
|
|
|
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2012-03-10 08:39:09 +00:00
|
|
|
Value *Value::stripInBoundsOffsets() {
|
|
|
|
return stripPointerCastsAndOffsets<PSK_InBounds>(this);
|
|
|
|
}
|
2008-05-07 22:54:15 +00:00
|
|
|
|
2009-09-20 04:03:34 +00:00
|
|
|
Value *Value::DoPHITranslation(const BasicBlock *CurBB,
|
2008-12-02 07:16:45 +00:00
|
|
|
const BasicBlock *PredBB) {
|
|
|
|
PHINode *PN = dyn_cast<PHINode>(this);
|
|
|
|
if (PN && PN->getParent() == CurBB)
|
|
|
|
return PN->getIncomingValueForBlock(PredBB);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2009-07-22 00:24:57 +00:00
|
|
|
LLVMContext &Value::getContext() const { return VTy->getContext(); }
|
|
|
|
|
2014-08-01 23:28:49 +00:00
|
|
|
void Value::reverseUseList() {
|
|
|
|
if (!UseList || !UseList->Next)
|
|
|
|
// No need to reverse 0 or 1 uses.
|
|
|
|
return;
|
|
|
|
|
|
|
|
Use *Head = UseList;
|
|
|
|
Use *Current = UseList->Next;
|
|
|
|
Head->Next = nullptr;
|
|
|
|
while (Current) {
|
|
|
|
Use *Next = Current->Next;
|
|
|
|
Current->Next = Head;
|
|
|
|
Head->setPrev(&Current->Next);
|
|
|
|
Head = Current;
|
|
|
|
Current = Next;
|
|
|
|
}
|
|
|
|
UseList = Head;
|
|
|
|
Head->setPrev(&UseList);
|
|
|
|
}
|
|
|
|
|
2009-03-31 22:11:05 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ValueHandleBase Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
|
|
|
|
assert(List && "Handle list is null?");
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2009-03-31 22:11:05 +00:00
|
|
|
// Splice ourselves into the list.
|
|
|
|
Next = *List;
|
|
|
|
*List = this;
|
|
|
|
setPrevPtr(List);
|
|
|
|
if (Next) {
|
|
|
|
Next->setPrevPtr(&Next);
|
2015-01-09 00:48:47 +00:00
|
|
|
assert(V == Next->V && "Added to wrong list?");
|
2009-03-31 22:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-12 17:43:32 +00:00
|
|
|
void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
|
|
|
|
assert(List && "Must insert after existing node");
|
|
|
|
|
|
|
|
Next = List->Next;
|
|
|
|
setPrevPtr(&List->Next);
|
|
|
|
List->Next = this;
|
|
|
|
if (Next)
|
|
|
|
Next->setPrevPtr(&Next);
|
|
|
|
}
|
|
|
|
|
2009-03-31 22:11:05 +00:00
|
|
|
void ValueHandleBase::AddToUseList() {
|
2015-01-09 00:48:47 +00:00
|
|
|
assert(V && "Null pointer doesn't have a use list!");
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2015-01-09 00:48:47 +00:00
|
|
|
LLVMContextImpl *pImpl = V->getContext().pImpl;
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2015-01-09 00:48:47 +00:00
|
|
|
if (V->HasValueHandle) {
|
2009-03-31 22:11:05 +00:00
|
|
|
// If this value already has a ValueHandle, then it must be in the
|
|
|
|
// ValueHandles map already.
|
2015-01-09 00:48:47 +00:00
|
|
|
ValueHandleBase *&Entry = pImpl->ValueHandles[V];
|
2014-04-15 06:32:26 +00:00
|
|
|
assert(Entry && "Value doesn't have any handles?");
|
2009-06-17 17:36:57 +00:00
|
|
|
AddToExistingUseList(&Entry);
|
|
|
|
return;
|
2009-03-31 22:11:05 +00:00
|
|
|
}
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2009-03-31 22:11:05 +00:00
|
|
|
// Ok, it doesn't have any handles yet, so we must insert it into the
|
|
|
|
// DenseMap. However, doing this insertion could cause the DenseMap to
|
|
|
|
// reallocate itself, which would invalidate all of the PrevP pointers that
|
|
|
|
// point into the old table. Handle this by checking for reallocation and
|
|
|
|
// updating the stale pointers only if needed.
|
2009-08-18 18:28:58 +00:00
|
|
|
DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
|
2009-03-31 22:11:05 +00:00
|
|
|
const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2015-01-09 00:48:47 +00:00
|
|
|
ValueHandleBase *&Entry = Handles[V];
|
2014-04-15 06:32:26 +00:00
|
|
|
assert(!Entry && "Value really did already have handles?");
|
2009-03-31 22:11:05 +00:00
|
|
|
AddToExistingUseList(&Entry);
|
2015-01-09 00:48:47 +00:00
|
|
|
V->HasValueHandle = true;
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2009-03-31 22:11:05 +00:00
|
|
|
// If reallocation didn't happen or if this was the first insertion, don't
|
|
|
|
// walk the table.
|
2009-09-20 04:03:34 +00:00
|
|
|
if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
|
2009-06-17 17:36:57 +00:00
|
|
|
Handles.size() == 1) {
|
2009-03-31 22:11:05 +00:00
|
|
|
return;
|
2009-06-17 17:36:57 +00:00
|
|
|
}
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2009-03-31 22:11:05 +00:00
|
|
|
// Okay, reallocation did happen. Fix the Prev Pointers.
|
2009-08-18 18:28:58 +00:00
|
|
|
for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
|
|
|
|
E = Handles.end(); I != E; ++I) {
|
2015-01-09 00:48:47 +00:00
|
|
|
assert(I->second && I->first == I->second->V &&
|
2012-04-08 10:16:43 +00:00
|
|
|
"List invariant broken!");
|
2009-03-31 22:11:05 +00:00
|
|
|
I->second->setPrevPtr(&I->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ValueHandleBase::RemoveFromUseList() {
|
2015-01-09 00:48:47 +00:00
|
|
|
assert(V && V->HasValueHandle &&
|
2012-04-08 10:16:43 +00:00
|
|
|
"Pointer doesn't have a use list!");
|
2009-03-31 22:11:05 +00:00
|
|
|
|
|
|
|
// Unlink this from its use list.
|
|
|
|
ValueHandleBase **PrevPtr = getPrevPtr();
|
|
|
|
assert(*PrevPtr == this && "List invariant broken");
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2009-03-31 22:11:05 +00:00
|
|
|
*PrevPtr = Next;
|
|
|
|
if (Next) {
|
|
|
|
assert(Next->getPrevPtr() == &Next && "List invariant broken");
|
|
|
|
Next->setPrevPtr(PrevPtr);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2009-03-31 22:11:05 +00:00
|
|
|
// If the Next pointer was null, then it is possible that this was the last
|
|
|
|
// ValueHandle watching VP. If so, delete its entry from the ValueHandles
|
|
|
|
// map.
|
2015-01-09 00:48:47 +00:00
|
|
|
LLVMContextImpl *pImpl = V->getContext().pImpl;
|
2009-08-18 18:28:58 +00:00
|
|
|
DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
|
2009-03-31 22:11:05 +00:00
|
|
|
if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
|
2015-01-09 00:48:47 +00:00
|
|
|
Handles.erase(V);
|
|
|
|
V->HasValueHandle = false;
|
2009-03-31 22:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ValueHandleBase::ValueIsDeleted(Value *V) {
|
|
|
|
assert(V->HasValueHandle && "Should only be called if ValueHandles present");
|
|
|
|
|
|
|
|
// Get the linked list base, which is guaranteed to exist since the
|
|
|
|
// HasValueHandle flag is set.
|
2009-08-18 18:28:58 +00:00
|
|
|
LLVMContextImpl *pImpl = V->getContext().pImpl;
|
|
|
|
ValueHandleBase *Entry = pImpl->ValueHandles[V];
|
2009-03-31 22:11:05 +00:00
|
|
|
assert(Entry && "Value bit set but no entries exist");
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2010-07-24 12:09:22 +00:00
|
|
|
// We use a local ValueHandleBase as an iterator so that ValueHandles can add
|
|
|
|
// and remove themselves from the list without breaking our iteration. This
|
|
|
|
// is not really an AssertingVH; we just have to give ValueHandleBase a kind.
|
|
|
|
// Note that we deliberately do not the support the case when dropping a value
|
|
|
|
// handle results in a new value handle being permanently added to the list
|
|
|
|
// (as might occur in theory for CallbackVH's): the new value handle will not
|
2010-07-27 06:53:14 +00:00
|
|
|
// be processed and the checking code will mete out righteous punishment if
|
2010-07-24 12:09:22 +00:00
|
|
|
// the handle is still present once we have finished processing all the other
|
|
|
|
// value handles (it is fine to momentarily add then remove a value handle).
|
2009-10-12 17:43:32 +00:00
|
|
|
for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
|
|
|
|
Iterator.RemoveFromUseList();
|
|
|
|
Iterator.AddToExistingUseListAfter(Entry);
|
|
|
|
assert(Entry->Next == &Iterator && "Loop invariant broken.");
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2009-10-12 17:43:32 +00:00
|
|
|
switch (Entry->getKind()) {
|
2009-03-31 22:11:05 +00:00
|
|
|
case Assert:
|
2009-10-12 17:43:32 +00:00
|
|
|
break;
|
2009-09-22 02:02:33 +00:00
|
|
|
case Tracking:
|
|
|
|
// Mark that this value has been deleted by setting it to an invalid Value
|
|
|
|
// pointer.
|
2009-10-12 17:43:32 +00:00
|
|
|
Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey());
|
2009-09-22 02:02:33 +00:00
|
|
|
break;
|
2009-03-31 22:11:05 +00:00
|
|
|
case Weak:
|
|
|
|
// Weak just goes to null, which will unlink it from the list.
|
2014-04-09 06:08:46 +00:00
|
|
|
Entry->operator=(nullptr);
|
2009-03-31 22:11:05 +00:00
|
|
|
break;
|
|
|
|
case Callback:
|
2009-05-02 21:10:48 +00:00
|
|
|
// Forward to the subclass's implementation.
|
2009-10-12 17:43:32 +00:00
|
|
|
static_cast<CallbackVH*>(Entry)->deleted();
|
2009-05-02 21:10:48 +00:00
|
|
|
break;
|
2009-03-31 22:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2009-10-12 17:43:32 +00:00
|
|
|
// All callbacks, weak references, and assertingVHs should be dropped by now.
|
|
|
|
if (V->HasValueHandle) {
|
|
|
|
#ifndef NDEBUG // Only in +Asserts mode...
|
2011-11-15 16:27:03 +00:00
|
|
|
dbgs() << "While deleting: " << *V->getType() << " %" << V->getName()
|
2009-10-12 17:43:32 +00:00
|
|
|
<< "\n";
|
|
|
|
if (pImpl->ValueHandles[V]->getKind() == Assert)
|
|
|
|
llvm_unreachable("An asserting value handle still pointed to this"
|
|
|
|
" value!");
|
|
|
|
|
|
|
|
#endif
|
|
|
|
llvm_unreachable("All references to V were not removed?");
|
|
|
|
}
|
2009-03-31 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
|
|
|
|
assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
|
|
|
|
assert(Old != New && "Changing value into itself!");
|
2014-10-23 04:08:42 +00:00
|
|
|
assert(Old->getType() == New->getType() &&
|
|
|
|
"replaceAllUses of value with new value of different type!");
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2009-03-31 22:11:05 +00:00
|
|
|
// Get the linked list base, which is guaranteed to exist since the
|
|
|
|
// HasValueHandle flag is set.
|
2009-08-18 18:28:58 +00:00
|
|
|
LLVMContextImpl *pImpl = Old->getContext().pImpl;
|
|
|
|
ValueHandleBase *Entry = pImpl->ValueHandles[Old];
|
|
|
|
|
2009-03-31 22:11:05 +00:00
|
|
|
assert(Entry && "Value bit set but no entries exist");
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2009-10-12 17:43:32 +00:00
|
|
|
// We use a local ValueHandleBase as an iterator so that
|
|
|
|
// ValueHandles can add and remove themselves from the list without
|
|
|
|
// breaking our iteration. This is not really an AssertingVH; we
|
|
|
|
// just have to give ValueHandleBase some kind.
|
|
|
|
for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
|
|
|
|
Iterator.RemoveFromUseList();
|
|
|
|
Iterator.AddToExistingUseListAfter(Entry);
|
|
|
|
assert(Entry->Next == &Iterator && "Loop invariant broken.");
|
2009-09-20 04:03:34 +00:00
|
|
|
|
2009-10-12 17:43:32 +00:00
|
|
|
switch (Entry->getKind()) {
|
2009-03-31 22:11:05 +00:00
|
|
|
case Assert:
|
|
|
|
// Asserting handle does not follow RAUW implicitly.
|
|
|
|
break;
|
2009-09-22 02:02:33 +00:00
|
|
|
case Tracking:
|
|
|
|
// Tracking goes to new value like a WeakVH. Note that this may make it
|
|
|
|
// something incompatible with its templated type. We don't want to have a
|
|
|
|
// virtual (or inline) interface to handle this though, so instead we make
|
2009-09-22 10:30:34 +00:00
|
|
|
// the TrackingVH accessors guarantee that a client never sees this value.
|
2009-09-22 02:02:33 +00:00
|
|
|
|
|
|
|
// FALLTHROUGH
|
2009-03-31 22:11:05 +00:00
|
|
|
case Weak:
|
|
|
|
// Weak goes to the new value, which will unlink it from Old's list.
|
2009-10-12 17:43:32 +00:00
|
|
|
Entry->operator=(New);
|
2009-03-31 22:11:05 +00:00
|
|
|
break;
|
|
|
|
case Callback:
|
2009-05-02 21:10:48 +00:00
|
|
|
// Forward to the subclass's implementation.
|
2009-10-12 17:43:32 +00:00
|
|
|
static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
|
2009-05-02 21:10:48 +00:00
|
|
|
break;
|
2009-03-31 22:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-27 06:53:14 +00:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// If any new tracking or weak value handles were added while processing the
|
|
|
|
// list, then complain about it now.
|
|
|
|
if (Old->HasValueHandle)
|
|
|
|
for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
|
|
|
|
switch (Entry->getKind()) {
|
|
|
|
case Tracking:
|
|
|
|
case Weak:
|
|
|
|
dbgs() << "After RAUW from " << *Old->getType() << " %"
|
2011-11-15 16:27:03 +00:00
|
|
|
<< Old->getName() << " to " << *New->getType() << " %"
|
|
|
|
<< New->getName() << "\n";
|
2010-07-27 06:53:14 +00:00
|
|
|
llvm_unreachable("A tracking or weak value handle still pointed to the"
|
|
|
|
" old value!\n");
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2009-03-31 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
2013-11-19 00:57:56 +00:00
|
|
|
// Pin the vtable to this file.
|
|
|
|
void CallbackVH::anchor() {}
|