2001-06-07 04:29:01 +08:00
|
|
|
//===-- Value.cpp - Implement the Value class -----------------------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
2009-04-01 06:11:05 +08:00
|
|
|
// This file implements the Value, ValueHandle, and User classes.
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Value.h"
|
2009-08-19 02:28:58 +08:00
|
|
|
#include "LLVMContextImpl.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2014-07-18 23:51:28 +08:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constant.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
2014-07-10 13:27:53 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-19 01:24:10 +08:00
|
|
|
#include "llvm/IR/DerivedUser.h"
|
2014-03-04 18:40:04 +08:00
|
|
|
#include "llvm/IR/GetElementPtrTypeIterator.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/InstrTypes.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2015-02-10 05:08:03 +08:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Operator.h"
|
2015-02-10 05:08:03 +08:00
|
|
|
#include "llvm/IR/Statepoint.h"
|
2014-03-04 19:17:44 +08:00
|
|
|
#include "llvm/IR/ValueHandle.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/ValueSymbolTable.h"
|
2006-11-17 16:03:48 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-09 02:01:40 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-04-01 06:11:05 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2015-03-24 03:32:43 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2001-06-07 04:29:01 +08:00
|
|
|
#include <algorithm>
|
2016-02-03 02:20:45 +08:00
|
|
|
|
2003-11-22 04:23:48 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Value Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
2011-07-18 12:54:35 +08:00
|
|
|
static inline Type *checkType(Type *Ty) {
|
2001-12-13 08:41:27 +08:00
|
|
|
assert(Ty && "Value defined with a null type: Error!");
|
2014-06-10 07:32:20 +08:00
|
|
|
return Ty;
|
2001-12-13 08:41:27 +08:00
|
|
|
}
|
|
|
|
|
2011-07-18 12:54:35 +08:00
|
|
|
Value::Value(Type *ty, unsigned scid)
|
2015-06-02 06:24:01 +08:00
|
|
|
: VTy(checkType(ty)), UseList(nullptr), SubclassID(scid),
|
|
|
|
HasValueHandle(0), SubclassOptionalData(0), SubclassData(0),
|
2015-06-13 01:48:10 +08:00
|
|
|
NumUserOperands(0), IsUsedByMD(false), HasName(false) {
|
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.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
// FIXME: Why isn't this in the subclass gunk??
|
2012-12-20 12:11:02 +08: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.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) &&
|
|
|
|
"invalid CallInst type!");
|
2012-12-20 12:11:02 +08: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.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
assert((VTy->isFirstClassType() || VTy->isVoidTy()) &&
|
2004-07-07 01:44:17 +08:00
|
|
|
"Cannot create non-first-class values except for constants!");
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-19 01:24:10 +08:00
|
|
|
static_assert(sizeof(Value) == 2 * sizeof(void *) + 2 * sizeof(unsigned),
|
2016-02-27 02:08:59 +08:00
|
|
|
"Value too big");
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
2007-12-10 10:14:30 +08:00
|
|
|
Value::~Value() {
|
2009-04-01 06:11:05 +08: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.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
if (isUsedByMetadata())
|
|
|
|
ValueAsMetadata::handleDeletion(this);
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
#ifndef NDEBUG // Only in -g mode...
|
2001-06-11 23:04:40 +08: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-11 07:55:38 +08:00
|
|
|
// reference and something is wrong. This code is here to print out where
|
|
|
|
// the value is still being referenced.
|
2001-06-11 23:04:40 +08:00
|
|
|
//
|
2007-12-10 10:14:30 +08:00
|
|
|
if (!use_empty()) {
|
2011-11-16 00:27:03 +08:00
|
|
|
dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n";
|
2015-03-11 07:55:38 +08:00
|
|
|
for (auto *U : users())
|
|
|
|
dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n";
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
#endif
|
2007-12-10 10:14:30 +08:00
|
|
|
assert(use_empty() && "Uses remain when a value is destroyed!");
|
2009-08-05 07:07:12 +08: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.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
destroyValueName();
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-19 01:24:10 +08:00
|
|
|
void Value::deleteValue() {
|
|
|
|
switch (getValueID()) {
|
|
|
|
#define HANDLE_VALUE(Name) \
|
|
|
|
case Value::Name##Val: \
|
|
|
|
delete static_cast<Name *>(this); \
|
|
|
|
break;
|
|
|
|
#define HANDLE_MEMORY_VALUE(Name) \
|
|
|
|
case Value::Name##Val: \
|
|
|
|
static_cast<DerivedUser *>(this)->DeleteValue( \
|
|
|
|
static_cast<DerivedUser *>(this)); \
|
|
|
|
break;
|
|
|
|
#define HANDLE_INSTRUCTION(Name) /* nothing */
|
|
|
|
#include "llvm/IR/Value.def"
|
|
|
|
|
|
|
|
#define HANDLE_INST(N, OPC, CLASS) \
|
|
|
|
case Value::InstructionVal + Instruction::OPC: \
|
|
|
|
delete static_cast<CLASS *>(this); \
|
|
|
|
break;
|
|
|
|
#define HANDLE_USER_INST(N, OPC, CLASS)
|
|
|
|
#include "llvm/IR/Instruction.def"
|
|
|
|
|
|
|
|
default:
|
|
|
|
llvm_unreachable("attempting to delete unknown value kind");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
void Value::destroyValueName() {
|
|
|
|
ValueName *Name = getValueName();
|
|
|
|
if (Name)
|
|
|
|
Name->Destroy();
|
|
|
|
setValueName(nullptr);
|
|
|
|
}
|
|
|
|
|
2005-02-01 09:24:21 +08:00
|
|
|
bool Value::hasNUses(unsigned N) const {
|
2010-03-26 07:06:16 +08:00
|
|
|
const_use_iterator UI = use_begin(), E = use_end();
|
2005-02-01 09:24:21 +08:00
|
|
|
|
|
|
|
for (; N; --N, ++UI)
|
|
|
|
if (UI == E) return false; // Too few.
|
|
|
|
return UI == E;
|
|
|
|
}
|
|
|
|
|
2005-02-24 00:51:11 +08:00
|
|
|
bool Value::hasNUsesOrMore(unsigned N) const {
|
2010-03-26 07:06:16 +08:00
|
|
|
const_use_iterator UI = use_begin(), E = use_end();
|
2005-02-24 00:51:11 +08:00
|
|
|
|
|
|
|
for (; N; --N, ++UI)
|
|
|
|
if (UI == E) return false; // Too few.
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-26 06:42:01 +08:00
|
|
|
bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
|
2013-05-15 07:45:56 +08: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 11:16:01 +08:00
|
|
|
const_user_iterator UI = user_begin(), UE = user_end();
|
2013-05-15 07:45:56 +08:00
|
|
|
for (; BI != BE && UI != UE; ++BI, ++UI) {
|
|
|
|
// Scan basic block: Check if this Value is used by the instruction at BI.
|
2016-08-12 11:55:06 +08:00
|
|
|
if (is_contained(BI->operands(), this))
|
2011-12-06 01:23:27 +08:00
|
|
|
return true;
|
2013-05-15 07:45:56 +08:00
|
|
|
// Scan use list: Check if the use at UI is in BB.
|
2016-08-12 11:55:06 +08:00
|
|
|
const auto *User = dyn_cast<Instruction>(*UI);
|
2008-06-13 05:15:59 +08:00
|
|
|
if (User && User->getParent() == BB)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-02-01 09:24:21 +08:00
|
|
|
unsigned Value::getNumUses() const {
|
|
|
|
return (unsigned)std::distance(use_begin(), use_end());
|
|
|
|
}
|
|
|
|
|
2007-02-11 08:37:27 +08:00
|
|
|
static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
|
2014-04-09 14:08:46 +08:00
|
|
|
ST = nullptr;
|
2007-02-11 08:37:27 +08:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V)) {
|
2005-03-06 03:51:50 +08:00
|
|
|
if (BasicBlock *P = I->getParent())
|
|
|
|
if (Function *PP = P->getParent())
|
2016-09-17 14:00:02 +08:00
|
|
|
ST = PP->getValueSymbolTable();
|
2007-02-11 08:37:27 +08:00
|
|
|
} else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
|
2009-09-20 12:03:34 +08:00
|
|
|
if (Function *P = BB->getParent())
|
2016-09-17 14:00:02 +08:00
|
|
|
ST = P->getValueSymbolTable();
|
2007-02-11 08:37:27 +08:00
|
|
|
} else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
|
2009-09-20 12:03:34 +08:00
|
|
|
if (Module *P = GV->getParent())
|
2007-02-06 04:47:22 +08:00
|
|
|
ST = &P->getValueSymbolTable();
|
2007-02-11 08:37:27 +08:00
|
|
|
} else if (Argument *A = dyn_cast<Argument>(V)) {
|
2009-09-20 12:03:34 +08:00
|
|
|
if (Function *P = A->getParent())
|
2016-09-17 14:00:02 +08: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.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
} else {
|
2007-02-11 08:37:27 +08:00
|
|
|
assert(isa<Constant>(V) && "Unknown value type!");
|
|
|
|
return true; // no name is setable for this.
|
2005-03-06 03:51:50 +08:00
|
|
|
}
|
2007-02-11 08:37:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-02 06:24:01 +08:00
|
|
|
ValueName *Value::getValueName() const {
|
|
|
|
if (!HasName) return nullptr;
|
|
|
|
|
|
|
|
LLVMContext &Ctx = getContext();
|
|
|
|
auto I = Ctx.pImpl->ValueNames.find(this);
|
|
|
|
assert(I != Ctx.pImpl->ValueNames.end() &&
|
|
|
|
"No name entry found!");
|
|
|
|
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Value::setValueName(ValueName *VN) {
|
|
|
|
LLVMContext &Ctx = getContext();
|
|
|
|
|
|
|
|
assert(HasName == Ctx.pImpl->ValueNames.count(this) &&
|
|
|
|
"HasName bit out of sync!");
|
|
|
|
|
|
|
|
if (!VN) {
|
|
|
|
if (HasName)
|
|
|
|
Ctx.pImpl->ValueNames.erase(this);
|
|
|
|
HasName = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
HasName = true;
|
|
|
|
Ctx.pImpl->ValueNames[this] = VN;
|
|
|
|
}
|
|
|
|
|
2009-07-26 08:51:56 +08:00
|
|
|
StringRef Value::getName() const {
|
2009-07-26 17:22:02 +08: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.
|
2015-06-02 06:24:01 +08:00
|
|
|
if (!hasName())
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
return StringRef("", 0);
|
|
|
|
return getValueName()->getKey();
|
2007-08-10 23:34:35 +08:00
|
|
|
}
|
|
|
|
|
2015-05-19 08:24:26 +08:00
|
|
|
void Value::setNameImpl(const Twine &NewName) {
|
Add a flag to the LLVMContext to disable name for Value other than GlobalValue
Summary:
This is intended to be a performance flag, on the same level as clang
cc1 option "--disable-free". LLVM will never initialize it by default,
it will be up to the client creating the LLVMContext to request this
behavior. Clang will do it by default in Release build (just like
--disable-free).
"opt" and "llc" can opt-in using -disable-named-value command line
option.
When performing LTO on llvm-tblgen, the initial merging of IR peaks
at 92MB without this patch, and 86MB after this patch,setNameImpl()
drops from 6.5MB to 0.5MB.
The total link time goes from ~29.5s to ~27.8s.
Compared to a compile-time flag (like the IRBuilder one), it performs
very close. I profiled on SROA and obtain these results:
420ms with IRBuilder that preserve name
372ms with IRBuilder that strip name
375ms with IRBuilder that preserve name, and a runtime flag to strip
Reviewers: chandlerc, dexonsmith, bogner
Subscribers: joker.eph, llvm-commits
Differential Revision: http://reviews.llvm.org/D17946
From: Mehdi Amini <mehdi.amini@apple.com>
llvm-svn: 263086
2016-03-10 09:28:54 +08:00
|
|
|
// Fast-path: LLVMContext can be set to strip out non-GlobalValue names
|
2016-04-02 11:46:17 +08:00
|
|
|
if (getContext().shouldDiscardValueNames() && !isa<GlobalValue>(this))
|
Add a flag to the LLVMContext to disable name for Value other than GlobalValue
Summary:
This is intended to be a performance flag, on the same level as clang
cc1 option "--disable-free". LLVM will never initialize it by default,
it will be up to the client creating the LLVMContext to request this
behavior. Clang will do it by default in Release build (just like
--disable-free).
"opt" and "llc" can opt-in using -disable-named-value command line
option.
When performing LTO on llvm-tblgen, the initial merging of IR peaks
at 92MB without this patch, and 86MB after this patch,setNameImpl()
drops from 6.5MB to 0.5MB.
The total link time goes from ~29.5s to ~27.8s.
Compared to a compile-time flag (like the IRBuilder one), it performs
very close. I profiled on SROA and obtain these results:
420ms with IRBuilder that preserve name
372ms with IRBuilder that strip name
375ms with IRBuilder that preserve name, and a runtime flag to strip
Reviewers: chandlerc, dexonsmith, bogner
Subscribers: joker.eph, llvm-commits
Differential Revision: http://reviews.llvm.org/D17946
From: Mehdi Amini <mehdi.amini@apple.com>
llvm-svn: 263086
2016-03-10 09:28:54 +08:00
|
|
|
return;
|
|
|
|
|
2009-08-20 07:37:23 +08:00
|
|
|
// Fast path for common IRBuilder case of setName("") when there is no name.
|
|
|
|
if (NewName.isTriviallyEmpty() && !hasName())
|
|
|
|
return;
|
|
|
|
|
2009-08-19 13:08:06 +08:00
|
|
|
SmallString<256> NameData;
|
2010-01-13 20:45:23 +08:00
|
|
|
StringRef NameRef = NewName.toStringRef(NameData);
|
2013-11-20 05:12:39 +08:00
|
|
|
assert(NameRef.find_first_of(0) == StringRef::npos &&
|
|
|
|
"Null bytes are not allowed in names");
|
2007-02-13 02:52:59 +08:00
|
|
|
|
2009-07-26 08:42:33 +08:00
|
|
|
// Name isn't changing?
|
2010-01-13 20:45:23 +08:00
|
|
|
if (getName() == NameRef)
|
2009-07-26 08:42:33 +08:00
|
|
|
return;
|
|
|
|
|
2010-01-05 21:12:22 +08:00
|
|
|
assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-11 08:37:27 +08: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-06 03:51:50 +08:00
|
|
|
|
2007-02-12 13:18:08 +08:00
|
|
|
if (!ST) { // No symbol table to update? Just do the change.
|
2010-01-13 20:45:23 +08:00
|
|
|
if (NameRef.empty()) {
|
2007-02-12 13:18:08 +08: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.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
destroyValueName();
|
2007-02-13 02:52:59 +08:00
|
|
|
return;
|
2005-03-06 10:14:28 +08:00
|
|
|
}
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-13 02:52:59 +08: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.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
destroyValueName();
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-13 02:52:59 +08: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.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
setValueName(ValueName::Create(NameRef));
|
|
|
|
getValueName()->setValue(this);
|
2007-02-12 13:18:08 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-12 13:18:08 +08: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.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
ST->removeValueName(getValueName());
|
|
|
|
destroyValueName();
|
2007-02-12 13:18:08 +08:00
|
|
|
|
2010-01-13 20:45:23 +08:00
|
|
|
if (NameRef.empty())
|
2007-02-13 02:52:59 +08:00
|
|
|
return;
|
2005-03-06 10:14:28 +08:00
|
|
|
}
|
2007-02-12 13:18:08 +08: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.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
setValueName(ST->createValueName(NameRef, this));
|
2005-03-06 03:51:50 +08:00
|
|
|
}
|
|
|
|
|
2015-05-19 08:24:26 +08:00
|
|
|
void Value::setName(const Twine &NewName) {
|
|
|
|
setNameImpl(NewName);
|
|
|
|
if (Function *F = dyn_cast<Function>(this))
|
|
|
|
F->recalculateIntrinsicID();
|
|
|
|
}
|
|
|
|
|
2007-02-11 08:37:27 +08:00
|
|
|
void Value::takeName(Value *V) {
|
2014-04-09 14:08:46 +08:00
|
|
|
ValueSymbolTable *ST = nullptr;
|
2007-02-16 04:01:43 +08: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 08:34:27 +08:00
|
|
|
if (V->hasName()) V->setName("");
|
2007-02-16 04:01:43 +08:00
|
|
|
return; // Cannot set a name on this value (e.g. constant).
|
|
|
|
}
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-16 04:01:43 +08: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.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
ST->removeValueName(getValueName());
|
|
|
|
destroyValueName();
|
2009-09-20 12:03:34 +08:00
|
|
|
}
|
|
|
|
|
2007-02-16 04:01:43 +08:00
|
|
|
// Now we know that this has no name.
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-16 04:01:43 +08:00
|
|
|
// If V has no name either, we're done.
|
|
|
|
if (!V->hasName()) return;
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-16 04:01:43 +08:00
|
|
|
// Get this's symtab if we didn't before.
|
|
|
|
if (!ST) {
|
|
|
|
if (getSymTab(this, ST)) {
|
|
|
|
// Clear V's name.
|
2009-07-26 08:34:27 +08:00
|
|
|
V->setName("");
|
2007-02-16 04:01:43 +08:00
|
|
|
return; // Cannot set a name on this value (e.g. constant).
|
|
|
|
}
|
|
|
|
}
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-16 04:01:43 +08:00
|
|
|
// Get V's ST, this should always succed, because V has a name.
|
|
|
|
ValueSymbolTable *VST;
|
|
|
|
bool Failure = getSymTab(V, VST);
|
2010-12-23 08:58:24 +08:00
|
|
|
assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure;
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-16 04:01:43 +08: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.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
setValueName(V->getValueName());
|
|
|
|
V->setValueName(nullptr);
|
|
|
|
getValueName()->setValue(this);
|
2007-02-11 09:04:09 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-16 04:01:43 +08:00
|
|
|
// Otherwise, things are slightly more complex. Remove V's name from VST and
|
|
|
|
// then reinsert it into ST.
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-16 04:01:43 +08: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.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
VST->removeValueName(V->getValueName());
|
|
|
|
setValueName(V->getValueName());
|
|
|
|
V->setValueName(nullptr);
|
|
|
|
getValueName()->setValue(this);
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2007-02-16 04:01:43 +08:00
|
|
|
if (ST)
|
|
|
|
ST->reinsertValue(this);
|
2007-02-11 08:37:27 +08:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:26:18 +08:00
|
|
|
void Value::assertModuleIsMaterializedImpl() const {
|
2016-02-04 05:13:23 +08:00
|
|
|
#ifndef NDEBUG
|
2016-01-16 03:00:20 +08:00
|
|
|
const GlobalValue *GV = dyn_cast<GlobalValue>(this);
|
|
|
|
if (!GV)
|
|
|
|
return;
|
|
|
|
const Module *M = GV->getParent();
|
|
|
|
if (!M)
|
|
|
|
return;
|
|
|
|
assert(M->isMaterialized());
|
2016-02-04 05:13:23 +08:00
|
|
|
#endif
|
2016-01-16 03:00:20 +08:00
|
|
|
}
|
|
|
|
|
2016-02-04 05:13:23 +08:00
|
|
|
#ifndef NDEBUG
|
2014-08-21 13:55:13 +08:00
|
|
|
static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr,
|
2014-05-13 09:23:21 +08:00
|
|
|
Constant *C) {
|
2014-11-19 15:49:26 +08:00
|
|
|
if (!Cache.insert(Expr).second)
|
2014-05-13 09:23:21 +08: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);
|
|
|
|
}
|
2016-02-03 02:20:45 +08:00
|
|
|
#endif // NDEBUG
|
2007-02-11 08:37:27 +08:00
|
|
|
|
2016-12-08 05:47:32 +08:00
|
|
|
void Value::doRAUW(Value *New, bool NoMetadata) {
|
2011-07-15 14:18:52 +08:00
|
|
|
assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
|
2014-05-13 09:23:21 +08:00
|
|
|
assert(!contains(New, this) &&
|
|
|
|
"this->replaceAllUsesWith(expr(this)) is NOT valid!");
|
2011-07-15 14:18:52 +08:00
|
|
|
assert(New->getType() == getType() &&
|
|
|
|
"replaceAllUses of value with new value of different type!");
|
|
|
|
|
2009-04-01 06:11:05 +08:00
|
|
|
// Notify all ValueHandles (if present) that this value is going away.
|
|
|
|
if (HasValueHandle)
|
|
|
|
ValueHandleBase::ValueIsRAUWd(this, New);
|
2016-12-08 05:47:32 +08:00
|
|
|
if (!NoMetadata && isUsedByMetadata())
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
llvm-svn: 223802
2014-12-10 02:38:53 +08:00
|
|
|
ValueAsMetadata::handleRAUW(this, New);
|
2013-03-02 02:48:54 +08:00
|
|
|
|
2005-02-01 09:24:21 +08:00
|
|
|
while (!use_empty()) {
|
2008-09-19 23:13:20 +08:00
|
|
|
Use &U = *UseList;
|
2003-08-29 13:09:37 +08:00
|
|
|
// Must handle Constants specially, we cannot call replaceUsesOfWith on a
|
2007-08-21 08:21:07 +08:00
|
|
|
// constant because they are uniqued.
|
2014-05-17 03:35:39 +08:00
|
|
|
if (auto *C = dyn_cast<Constant>(U.getUser())) {
|
2007-08-21 08:21:07 +08:00
|
|
|
if (!isa<GlobalValue>(C)) {
|
2016-02-11 06:47:15 +08:00
|
|
|
C->handleOperandChange(this, New);
|
2007-08-21 08:21:07 +08:00
|
|
|
continue;
|
|
|
|
}
|
2003-08-29 13:09:37 +08:00
|
|
|
}
|
2013-03-02 02:48:54 +08:00
|
|
|
|
2007-08-21 08:21:07 +08:00
|
|
|
U.set(New);
|
2003-08-29 13:09:37 +08:00
|
|
|
}
|
2013-03-02 02:48:54 +08:00
|
|
|
|
2011-06-23 17:09:15 +08:00
|
|
|
if (BasicBlock *BB = dyn_cast<BasicBlock>(this))
|
|
|
|
BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
|
2003-08-29 13:09:37 +08:00
|
|
|
}
|
|
|
|
|
2016-12-08 05:47:32 +08:00
|
|
|
void Value::replaceAllUsesWith(Value *New) {
|
|
|
|
doRAUW(New, false /* NoMetadata */);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Value::replaceNonMetadataUsesWith(Value *New) {
|
|
|
|
doRAUW(New, true /* NoMetadata */);
|
|
|
|
}
|
|
|
|
|
2014-11-22 07:36:44 +08: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 08:09:25 +08:00
|
|
|
void Value::replaceUsesExceptBlockAddr(Value *New) {
|
|
|
|
use_iterator UI = use_begin(), E = use_end();
|
|
|
|
for (; UI != E;) {
|
|
|
|
Use &U = *UI;
|
|
|
|
++UI;
|
|
|
|
|
|
|
|
if (isa<BlockAddress>(U.getUser()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Must handle Constants specially, we cannot call replaceUsesOfWith on a
|
|
|
|
// constant because they are uniqued.
|
|
|
|
if (auto *C = dyn_cast<Constant>(U.getUser())) {
|
|
|
|
if (!isa<GlobalValue>(C)) {
|
|
|
|
C->handleOperandChange(this, New);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U.set(New);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-10 16:39:09 +08:00
|
|
|
namespace {
|
|
|
|
// Various metrics for how much to strip off of pointers.
|
|
|
|
enum PointerStripKind {
|
|
|
|
PSK_ZeroIndices,
|
2013-05-06 09:48:55 +08:00
|
|
|
PSK_ZeroIndicesAndAliases,
|
Handle invariant.group.barrier in BasicAA
Summary:
llvm.invariant.group.barrier returns pointer that mustalias
pointer it takes. It can't be marked with `returned` attribute,
because it would be remove easily. The other reason is that
only Alias Analysis can know about this, because if any other
pass would know it, then the result would be replaced with it's
argument, which would be invalid.
We can think about returned pointer as something that mustalias, but
it doesn't have to be bitwise the same as the argument.
Reviewers: dberlin, chandlerc, hfinkel, sanjoy
Subscribers: reames, nlewycky, rsmith, anna, amharc
Differential Revision: https://reviews.llvm.org/D31585
llvm-svn: 301227
2017-04-25 03:37:17 +08:00
|
|
|
PSK_ZeroIndicesAndAliasesAndBarriers,
|
2012-03-15 07:19:53 +08:00
|
|
|
PSK_InBoundsConstantIndices,
|
|
|
|
PSK_InBounds
|
2012-03-10 16:39:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <PointerStripKind StripKind>
|
2017-03-27 13:47:03 +08:00
|
|
|
static const Value *stripPointerCastsAndOffsets(const Value *V) {
|
2012-03-10 16:39:09 +08:00
|
|
|
if (!V->getType()->isPointerTy())
|
|
|
|
return V;
|
2010-06-29 05:16:52 +08: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.
|
2017-03-27 13:47:03 +08:00
|
|
|
SmallPtrSet<const Value *, 4> Visited;
|
2010-06-29 05:16:52 +08:00
|
|
|
|
|
|
|
Visited.insert(V);
|
2008-12-30 05:06:19 +08:00
|
|
|
do {
|
2017-03-27 13:47:03 +08:00
|
|
|
if (auto *GEP = dyn_cast<GEPOperator>(V)) {
|
2012-03-10 16:39:09 +08:00
|
|
|
switch (StripKind) {
|
2013-05-06 09:48:55 +08:00
|
|
|
case PSK_ZeroIndicesAndAliases:
|
Handle invariant.group.barrier in BasicAA
Summary:
llvm.invariant.group.barrier returns pointer that mustalias
pointer it takes. It can't be marked with `returned` attribute,
because it would be remove easily. The other reason is that
only Alias Analysis can know about this, because if any other
pass would know it, then the result would be replaced with it's
argument, which would be invalid.
We can think about returned pointer as something that mustalias, but
it doesn't have to be bitwise the same as the argument.
Reviewers: dberlin, chandlerc, hfinkel, sanjoy
Subscribers: reames, nlewycky, rsmith, anna, amharc
Differential Revision: https://reviews.llvm.org/D31585
llvm-svn: 301227
2017-04-25 03:37:17 +08:00
|
|
|
case PSK_ZeroIndicesAndAliasesAndBarriers:
|
2012-03-10 16:39:09 +08:00
|
|
|
case PSK_ZeroIndices:
|
|
|
|
if (!GEP->hasAllZeroIndices())
|
|
|
|
return V;
|
|
|
|
break;
|
2012-03-15 07:19:53 +08:00
|
|
|
case PSK_InBoundsConstantIndices:
|
2012-03-10 16:39:09 +08:00
|
|
|
if (!GEP->hasAllConstantIndices())
|
|
|
|
return V;
|
2016-08-17 13:10:15 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2012-03-10 16:39:09 +08:00
|
|
|
case PSK_InBounds:
|
|
|
|
if (!GEP->isInBounds())
|
|
|
|
return V;
|
|
|
|
break;
|
|
|
|
}
|
2009-07-18 07:55:56 +08:00
|
|
|
V = GEP->getPointerOperand();
|
2013-11-15 09:34:59 +08:00
|
|
|
} else if (Operator::getOpcode(V) == Instruction::BitCast ||
|
|
|
|
Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
|
2009-07-18 07:55:56 +08:00
|
|
|
V = cast<Operator>(V)->getOperand(0);
|
2017-03-27 13:47:03 +08:00
|
|
|
} else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
|
Don't IPO over functions that can be de-refined
Summary:
Fixes PR26774.
If you're aware of the issue, feel free to skip the "Motivation"
section and jump directly to "This patch".
Motivation:
I define "refinement" as discarding behaviors from a program that the
optimizer has license to discard. So transforming:
```
void f(unsigned x) {
unsigned t = 5 / x;
(void)t;
}
```
to
```
void f(unsigned x) { }
```
is refinement, since the behavior went from "if x == 0 then undefined
else nothing" to "nothing" (the optimizer has license to discard
undefined behavior).
Refinement is a fundamental aspect of many mid-level optimizations done
by LLVM. For instance, transforming `x == (x + 1)` to `false` also
involves refinement since the expression's value went from "if x is
`undef` then { `true` or `false` } else { `false` }" to "`false`" (by
definition, the optimizer has license to fold `undef` to any non-`undef`
value).
Unfortunately, refinement implies that the optimizer cannot assume
that the implementation of a function it can see has all of the
behavior an unoptimized or a differently optimized version of the same
function can have. This is a problem for functions with comdat
linkage, where a function can be replaced by an unoptimized or a
differently optimized version of the same source level function.
For instance, FunctionAttrs cannot assume a comdat function is
actually `readnone` even if it does not have any loads or stores in
it; since there may have been loads and stores in the "original
function" that were refined out in the currently visible variant, and
at the link step the linker may in fact choose an implementation with
a load or a store. As an example, consider a function that does two
atomic loads from the same memory location, and writes to memory only
if the two values are not equal. The optimizer is allowed to refine
this function by first CSE'ing the two loads, and the folding the
comparision to always report that the two values are equal. Such a
refined variant will look like it is `readonly`. However, the
unoptimized version of the function can still write to memory (since
the two loads //can// result in different values), and selecting the
unoptimized version at link time will retroactively invalidate
transforms we may have done under the assumption that the function
does not write to memory.
Note: this is not just a problem with atomics or with linking
differently optimized object files. See PR26774 for more realistic
examples that involved neither.
This patch:
This change introduces a new set of linkage types, predicated as
`GlobalValue::mayBeDerefined` that returns true if the linkage type
allows a function to be replaced by a differently optimized variant at
link time. It then changes a set of IPO passes to bail out if they see
such a function.
Reviewers: chandlerc, hfinkel, dexonsmith, joker.eph, rnk
Subscribers: mcrosier, llvm-commits
Differential Revision: http://reviews.llvm.org/D18634
llvm-svn: 265762
2016-04-08 08:48:30 +08:00
|
|
|
if (StripKind == PSK_ZeroIndices || GA->isInterposable())
|
2009-08-28 01:55:13 +08:00
|
|
|
return V;
|
|
|
|
V = GA->getAliasee();
|
2008-12-30 05:06:19 +08:00
|
|
|
} else {
|
Handle invariant.group.barrier in BasicAA
Summary:
llvm.invariant.group.barrier returns pointer that mustalias
pointer it takes. It can't be marked with `returned` attribute,
because it would be remove easily. The other reason is that
only Alias Analysis can know about this, because if any other
pass would know it, then the result would be replaced with it's
argument, which would be invalid.
We can think about returned pointer as something that mustalias, but
it doesn't have to be bitwise the same as the argument.
Reviewers: dberlin, chandlerc, hfinkel, sanjoy
Subscribers: reames, nlewycky, rsmith, anna, amharc
Differential Revision: https://reviews.llvm.org/D31585
llvm-svn: 301227
2017-04-25 03:37:17 +08:00
|
|
|
if (auto CS = ImmutableCallSite(V)) {
|
2017-03-27 13:47:03 +08:00
|
|
|
if (const Value *RV = CS.getReturnedArgOperand()) {
|
2016-07-11 09:32:20 +08:00
|
|
|
V = RV;
|
|
|
|
continue;
|
|
|
|
}
|
Handle invariant.group.barrier in BasicAA
Summary:
llvm.invariant.group.barrier returns pointer that mustalias
pointer it takes. It can't be marked with `returned` attribute,
because it would be remove easily. The other reason is that
only Alias Analysis can know about this, because if any other
pass would know it, then the result would be replaced with it's
argument, which would be invalid.
We can think about returned pointer as something that mustalias, but
it doesn't have to be bitwise the same as the argument.
Reviewers: dberlin, chandlerc, hfinkel, sanjoy
Subscribers: reames, nlewycky, rsmith, anna, amharc
Differential Revision: https://reviews.llvm.org/D31585
llvm-svn: 301227
2017-04-25 03:37:17 +08:00
|
|
|
// The result of invariant.group.barrier must alias it's argument,
|
|
|
|
// but it can't be marked with returned attribute, that's why it needs
|
|
|
|
// special case.
|
|
|
|
if (StripKind == PSK_ZeroIndicesAndAliasesAndBarriers &&
|
|
|
|
CS.getIntrinsicID() == Intrinsic::invariant_group_barrier) {
|
|
|
|
V = CS.getArgOperand(0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2008-12-30 05:06:19 +08:00
|
|
|
return V;
|
2008-05-08 06:54:15 +08:00
|
|
|
}
|
2010-02-16 19:11:14 +08:00
|
|
|
assert(V->getType()->isPointerTy() && "Unexpected operand type!");
|
2014-11-19 15:49:26 +08:00
|
|
|
} while (Visited.insert(V).second);
|
2010-06-29 05:16:52 +08:00
|
|
|
|
|
|
|
return V;
|
2008-05-08 06:54:15 +08:00
|
|
|
}
|
2016-02-03 02:20:45 +08:00
|
|
|
} // end anonymous namespace
|
2012-03-10 16:39:09 +08:00
|
|
|
|
2017-03-27 13:47:03 +08:00
|
|
|
const Value *Value::stripPointerCasts() const {
|
2013-05-06 09:48:55 +08:00
|
|
|
return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this);
|
|
|
|
}
|
|
|
|
|
2017-03-27 13:47:03 +08:00
|
|
|
const Value *Value::stripPointerCastsNoFollowAliases() const {
|
2012-03-10 16:39:09 +08:00
|
|
|
return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);
|
|
|
|
}
|
|
|
|
|
2017-03-27 13:47:03 +08:00
|
|
|
const Value *Value::stripInBoundsConstantOffsets() const {
|
2012-03-15 07:19:53 +08:00
|
|
|
return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);
|
Handle invariant.group.barrier in BasicAA
Summary:
llvm.invariant.group.barrier returns pointer that mustalias
pointer it takes. It can't be marked with `returned` attribute,
because it would be remove easily. The other reason is that
only Alias Analysis can know about this, because if any other
pass would know it, then the result would be replaced with it's
argument, which would be invalid.
We can think about returned pointer as something that mustalias, but
it doesn't have to be bitwise the same as the argument.
Reviewers: dberlin, chandlerc, hfinkel, sanjoy
Subscribers: reames, nlewycky, rsmith, anna, amharc
Differential Revision: https://reviews.llvm.org/D31585
llvm-svn: 301227
2017-04-25 03:37:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Value *Value::stripPointerCastsAndBarriers() const {
|
|
|
|
return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliasesAndBarriers>(
|
|
|
|
this);
|
2012-03-10 16:39:09 +08:00
|
|
|
}
|
|
|
|
|
2017-03-27 13:47:03 +08:00
|
|
|
const Value *
|
|
|
|
Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
|
|
|
|
APInt &Offset) const {
|
2013-08-22 19:25:11 +08:00
|
|
|
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.
|
2017-03-27 13:47:03 +08:00
|
|
|
SmallPtrSet<const Value *, 4> Visited;
|
2013-08-22 19:25:11 +08:00
|
|
|
Visited.insert(this);
|
2017-03-27 13:47:03 +08:00
|
|
|
const Value *V = this;
|
2013-08-22 19:25:11 +08:00
|
|
|
do {
|
2017-03-27 13:47:03 +08:00
|
|
|
if (auto *GEP = dyn_cast<GEPOperator>(V)) {
|
2013-08-22 19:25:11 +08:00
|
|
|
if (!GEP->isInBounds())
|
|
|
|
return V;
|
2013-08-25 18:46:39 +08:00
|
|
|
APInt GEPOffset(Offset);
|
|
|
|
if (!GEP->accumulateConstantOffset(DL, GEPOffset))
|
2013-08-22 19:25:11 +08:00
|
|
|
return V;
|
2013-08-25 18:46:39 +08:00
|
|
|
Offset = GEPOffset;
|
2013-08-22 19:25:11 +08:00
|
|
|
V = GEP->getPointerOperand();
|
2015-09-24 03:48:43 +08:00
|
|
|
} else if (Operator::getOpcode(V) == Instruction::BitCast) {
|
2013-08-22 19:25:11 +08:00
|
|
|
V = cast<Operator>(V)->getOperand(0);
|
2017-03-27 13:47:03 +08:00
|
|
|
} else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
|
2013-08-22 19:25:11 +08:00
|
|
|
V = GA->getAliasee();
|
|
|
|
} else {
|
2017-03-27 13:47:03 +08:00
|
|
|
if (auto CS = ImmutableCallSite(V))
|
|
|
|
if (const Value *RV = CS.getReturnedArgOperand()) {
|
2016-07-11 09:32:20 +08:00
|
|
|
V = RV;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-08-22 19:25:11 +08:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
assert(V->getType()->isPointerTy() && "Unexpected operand type!");
|
2014-11-19 15:49:26 +08:00
|
|
|
} while (Visited.insert(V).second);
|
2013-08-22 19:25:11 +08:00
|
|
|
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2017-03-27 13:47:03 +08:00
|
|
|
const Value *Value::stripInBoundsOffsets() const {
|
2012-03-10 16:39:09 +08:00
|
|
|
return stripPointerCastsAndOffsets<PSK_InBounds>(this);
|
|
|
|
}
|
2008-05-08 06:54:15 +08:00
|
|
|
|
2016-06-02 08:52:48 +08:00
|
|
|
unsigned Value::getPointerDereferenceableBytes(const DataLayout &DL,
|
|
|
|
bool &CanBeNull) const {
|
2016-04-27 20:51:01 +08:00
|
|
|
assert(getType()->isPointerTy() && "must be pointer");
|
|
|
|
|
|
|
|
unsigned DerefBytes = 0;
|
|
|
|
CanBeNull = false;
|
|
|
|
if (const Argument *A = dyn_cast<Argument>(this)) {
|
|
|
|
DerefBytes = A->getDereferenceableBytes();
|
2016-06-02 08:52:48 +08:00
|
|
|
if (DerefBytes == 0 && A->hasByValAttr() && A->getType()->isSized()) {
|
|
|
|
DerefBytes = DL.getTypeStoreSize(A->getType());
|
|
|
|
CanBeNull = false;
|
|
|
|
}
|
2016-04-27 20:51:01 +08:00
|
|
|
if (DerefBytes == 0) {
|
|
|
|
DerefBytes = A->getDereferenceableOrNullBytes();
|
|
|
|
CanBeNull = true;
|
|
|
|
}
|
|
|
|
} else if (auto CS = ImmutableCallSite(this)) {
|
2017-04-29 02:37:16 +08:00
|
|
|
DerefBytes = CS.getDereferenceableBytes(AttributeList::ReturnIndex);
|
2016-04-27 20:51:01 +08:00
|
|
|
if (DerefBytes == 0) {
|
2017-04-29 02:37:16 +08:00
|
|
|
DerefBytes = CS.getDereferenceableOrNullBytes(AttributeList::ReturnIndex);
|
2016-04-27 20:51:01 +08:00
|
|
|
CanBeNull = true;
|
|
|
|
}
|
|
|
|
} else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {
|
|
|
|
if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) {
|
|
|
|
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
|
|
|
|
DerefBytes = CI->getLimitedValue();
|
|
|
|
}
|
|
|
|
if (DerefBytes == 0) {
|
|
|
|
if (MDNode *MD =
|
|
|
|
LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
|
|
|
|
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
|
|
|
|
DerefBytes = CI->getLimitedValue();
|
|
|
|
}
|
|
|
|
CanBeNull = true;
|
|
|
|
}
|
2016-06-02 08:52:48 +08:00
|
|
|
} else if (auto *AI = dyn_cast<AllocaInst>(this)) {
|
|
|
|
if (AI->getAllocatedType()->isSized()) {
|
|
|
|
DerefBytes = DL.getTypeStoreSize(AI->getAllocatedType());
|
|
|
|
CanBeNull = false;
|
|
|
|
}
|
|
|
|
} else if (auto *GV = dyn_cast<GlobalVariable>(this)) {
|
|
|
|
if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) {
|
|
|
|
// TODO: Don't outright reject hasExternalWeakLinkage but set the
|
|
|
|
// CanBeNull flag.
|
|
|
|
DerefBytes = DL.getTypeStoreSize(GV->getValueType());
|
|
|
|
CanBeNull = false;
|
|
|
|
}
|
2016-04-27 20:51:01 +08:00
|
|
|
}
|
|
|
|
return DerefBytes;
|
|
|
|
}
|
|
|
|
|
2016-02-24 20:25:10 +08:00
|
|
|
unsigned Value::getPointerAlignment(const DataLayout &DL) const {
|
|
|
|
assert(getType()->isPointerTy() && "must be pointer");
|
|
|
|
|
|
|
|
unsigned Align = 0;
|
|
|
|
if (auto *GO = dyn_cast<GlobalObject>(this)) {
|
|
|
|
Align = GO->getAlignment();
|
|
|
|
if (Align == 0) {
|
|
|
|
if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
|
|
|
|
Type *ObjectType = GVar->getValueType();
|
|
|
|
if (ObjectType->isSized()) {
|
|
|
|
// If the object is defined in the current Module, we'll be giving
|
|
|
|
// it the preferred alignment. Otherwise, we have to assume that it
|
|
|
|
// may only have the minimum ABI alignment.
|
|
|
|
if (GVar->isStrongDefinitionForLinker())
|
|
|
|
Align = DL.getPreferredAlignment(GVar);
|
|
|
|
else
|
|
|
|
Align = DL.getABITypeAlignment(ObjectType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (const Argument *A = dyn_cast<Argument>(this)) {
|
|
|
|
Align = A->getParamAlignment();
|
|
|
|
|
|
|
|
if (!Align && A->hasStructRetAttr()) {
|
|
|
|
// An sret parameter has at least the ABI alignment of the return type.
|
|
|
|
Type *EltTy = cast<PointerType>(A->getType())->getElementType();
|
|
|
|
if (EltTy->isSized())
|
|
|
|
Align = DL.getABITypeAlignment(EltTy);
|
|
|
|
}
|
2016-04-27 18:42:29 +08:00
|
|
|
} else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) {
|
2016-02-24 20:25:10 +08:00
|
|
|
Align = AI->getAlignment();
|
2016-04-27 18:42:29 +08:00
|
|
|
if (Align == 0) {
|
|
|
|
Type *AllocatedType = AI->getAllocatedType();
|
|
|
|
if (AllocatedType->isSized())
|
|
|
|
Align = DL.getPrefTypeAlignment(AllocatedType);
|
|
|
|
}
|
|
|
|
} else if (auto CS = ImmutableCallSite(this))
|
2017-04-29 04:34:27 +08:00
|
|
|
Align = CS.getAttributes().getRetAlignment();
|
2016-02-24 20:25:10 +08:00
|
|
|
else if (const LoadInst *LI = dyn_cast<LoadInst>(this))
|
|
|
|
if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) {
|
|
|
|
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
|
|
|
|
Align = CI->getLimitedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Align;
|
|
|
|
}
|
|
|
|
|
2017-03-27 13:47:03 +08:00
|
|
|
const Value *Value::DoPHITranslation(const BasicBlock *CurBB,
|
|
|
|
const BasicBlock *PredBB) const {
|
|
|
|
auto *PN = dyn_cast<PHINode>(this);
|
2008-12-02 15:16:45 +08:00
|
|
|
if (PN && PN->getParent() == CurBB)
|
|
|
|
return PN->getIncomingValueForBlock(PredBB);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2009-07-22 08:24:57 +08:00
|
|
|
LLVMContext &Value::getContext() const { return VTy->getContext(); }
|
|
|
|
|
2014-08-02 07:28:49 +08: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);
|
|
|
|
}
|
|
|
|
|
2016-09-11 02:14:54 +08:00
|
|
|
bool Value::isSwiftError() const {
|
|
|
|
auto *Arg = dyn_cast<Argument>(this);
|
|
|
|
if (Arg)
|
|
|
|
return Arg->hasSwiftErrorAttr();
|
|
|
|
auto *Alloca = dyn_cast<AllocaInst>(this);
|
|
|
|
if (!Alloca)
|
|
|
|
return false;
|
|
|
|
return Alloca->isSwiftError();
|
|
|
|
}
|
|
|
|
|
2009-04-01 06:11:05 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ValueHandleBase Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
|
|
|
|
assert(List && "Handle list is null?");
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-04-01 06:11:05 +08:00
|
|
|
// Splice ourselves into the list.
|
|
|
|
Next = *List;
|
|
|
|
*List = this;
|
|
|
|
setPrevPtr(List);
|
|
|
|
if (Next) {
|
|
|
|
Next->setPrevPtr(&Next);
|
2017-04-27 14:02:18 +08:00
|
|
|
assert(getValPtr() == Next->getValPtr() && "Added to wrong list?");
|
2009-04-01 06:11:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-13 01:43:32 +08: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-04-01 06:11:05 +08:00
|
|
|
void ValueHandleBase::AddToUseList() {
|
2017-04-27 14:02:18 +08:00
|
|
|
assert(getValPtr() && "Null pointer doesn't have a use list!");
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2017-04-27 14:02:18 +08:00
|
|
|
LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2017-04-27 14:02:18 +08:00
|
|
|
if (getValPtr()->HasValueHandle) {
|
2009-04-01 06:11:05 +08:00
|
|
|
// If this value already has a ValueHandle, then it must be in the
|
|
|
|
// ValueHandles map already.
|
2017-04-27 14:02:18 +08:00
|
|
|
ValueHandleBase *&Entry = pImpl->ValueHandles[getValPtr()];
|
2014-04-15 14:32:26 +08:00
|
|
|
assert(Entry && "Value doesn't have any handles?");
|
2009-06-18 01:36:57 +08:00
|
|
|
AddToExistingUseList(&Entry);
|
|
|
|
return;
|
2009-04-01 06:11:05 +08:00
|
|
|
}
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-04-01 06:11:05 +08: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-19 02:28:58 +08:00
|
|
|
DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
|
2009-04-01 06:11:05 +08:00
|
|
|
const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2017-04-27 14:02:18 +08:00
|
|
|
ValueHandleBase *&Entry = Handles[getValPtr()];
|
2014-04-15 14:32:26 +08:00
|
|
|
assert(!Entry && "Value really did already have handles?");
|
2009-04-01 06:11:05 +08:00
|
|
|
AddToExistingUseList(&Entry);
|
2017-04-27 14:02:18 +08:00
|
|
|
getValPtr()->HasValueHandle = true;
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-04-01 06:11:05 +08:00
|
|
|
// If reallocation didn't happen or if this was the first insertion, don't
|
|
|
|
// walk the table.
|
2009-09-20 12:03:34 +08:00
|
|
|
if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
|
2009-06-18 01:36:57 +08:00
|
|
|
Handles.size() == 1) {
|
2009-04-01 06:11:05 +08:00
|
|
|
return;
|
2009-06-18 01:36:57 +08:00
|
|
|
}
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-04-01 06:11:05 +08:00
|
|
|
// Okay, reallocation did happen. Fix the Prev Pointers.
|
2009-08-19 02:28:58 +08:00
|
|
|
for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
|
|
|
|
E = Handles.end(); I != E; ++I) {
|
2017-04-27 14:02:18 +08:00
|
|
|
assert(I->second && I->first == I->second->getValPtr() &&
|
2012-04-08 18:16:43 +08:00
|
|
|
"List invariant broken!");
|
2009-04-01 06:11:05 +08:00
|
|
|
I->second->setPrevPtr(&I->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ValueHandleBase::RemoveFromUseList() {
|
2017-04-27 14:02:18 +08:00
|
|
|
assert(getValPtr() && getValPtr()->HasValueHandle &&
|
2012-04-08 18:16:43 +08:00
|
|
|
"Pointer doesn't have a use list!");
|
2009-04-01 06:11:05 +08:00
|
|
|
|
|
|
|
// Unlink this from its use list.
|
|
|
|
ValueHandleBase **PrevPtr = getPrevPtr();
|
|
|
|
assert(*PrevPtr == this && "List invariant broken");
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-04-01 06:11:05 +08:00
|
|
|
*PrevPtr = Next;
|
|
|
|
if (Next) {
|
|
|
|
assert(Next->getPrevPtr() == &Next && "List invariant broken");
|
|
|
|
Next->setPrevPtr(PrevPtr);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-04-01 06:11:05 +08: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.
|
2017-04-27 14:02:18 +08:00
|
|
|
LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
|
2009-08-19 02:28:58 +08:00
|
|
|
DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
|
2009-04-01 06:11:05 +08:00
|
|
|
if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
|
2017-04-27 14:02:18 +08:00
|
|
|
Handles.erase(getValPtr());
|
|
|
|
getValPtr()->HasValueHandle = false;
|
2009-04-01 06:11:05 +08: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-19 02:28:58 +08:00
|
|
|
LLVMContextImpl *pImpl = V->getContext().pImpl;
|
|
|
|
ValueHandleBase *Entry = pImpl->ValueHandles[V];
|
2009-04-01 06:11:05 +08:00
|
|
|
assert(Entry && "Value bit set but no entries exist");
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2010-07-24 20:09:22 +08: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 14:53:14 +08:00
|
|
|
// be processed and the checking code will mete out righteous punishment if
|
2010-07-24 20:09:22 +08: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-13 01:43:32 +08: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 12:03:34 +08:00
|
|
|
|
2009-10-13 01:43:32 +08:00
|
|
|
switch (Entry->getKind()) {
|
2009-04-01 06:11:05 +08:00
|
|
|
case Assert:
|
2009-10-13 01:43:32 +08:00
|
|
|
break;
|
2017-05-02 01:07:54 +08:00
|
|
|
case Weak:
|
2017-05-02 01:07:49 +08:00
|
|
|
case WeakTracking:
|
2017-05-02 01:07:54 +08:00
|
|
|
// WeakTracking and Weak just go to null, which unlinks them
|
|
|
|
// from the list.
|
2014-04-09 14:08:46 +08:00
|
|
|
Entry->operator=(nullptr);
|
2009-04-01 06:11:05 +08:00
|
|
|
break;
|
|
|
|
case Callback:
|
2009-05-03 05:10:48 +08:00
|
|
|
// Forward to the subclass's implementation.
|
2009-10-13 01:43:32 +08:00
|
|
|
static_cast<CallbackVH*>(Entry)->deleted();
|
2009-05-03 05:10:48 +08:00
|
|
|
break;
|
2009-04-01 06:11:05 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-10-13 01:43:32 +08:00
|
|
|
// All callbacks, weak references, and assertingVHs should be dropped by now.
|
|
|
|
if (V->HasValueHandle) {
|
|
|
|
#ifndef NDEBUG // Only in +Asserts mode...
|
2011-11-16 00:27:03 +08:00
|
|
|
dbgs() << "While deleting: " << *V->getType() << " %" << V->getName()
|
2009-10-13 01:43:32 +08: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-04-01 06:11:05 +08: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 12:08:42 +08:00
|
|
|
assert(Old->getType() == New->getType() &&
|
|
|
|
"replaceAllUses of value with new value of different type!");
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-04-01 06:11:05 +08:00
|
|
|
// Get the linked list base, which is guaranteed to exist since the
|
|
|
|
// HasValueHandle flag is set.
|
2009-08-19 02:28:58 +08:00
|
|
|
LLVMContextImpl *pImpl = Old->getContext().pImpl;
|
|
|
|
ValueHandleBase *Entry = pImpl->ValueHandles[Old];
|
|
|
|
|
2009-04-01 06:11:05 +08:00
|
|
|
assert(Entry && "Value bit set but no entries exist");
|
2009-09-20 12:03:34 +08:00
|
|
|
|
2009-10-13 01:43:32 +08: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 12:03:34 +08:00
|
|
|
|
2009-10-13 01:43:32 +08:00
|
|
|
switch (Entry->getKind()) {
|
2009-04-01 06:11:05 +08:00
|
|
|
case Assert:
|
2017-05-02 01:07:54 +08:00
|
|
|
case Weak:
|
|
|
|
// Asserting and Weak handles do not follow RAUW implicitly.
|
2009-04-01 06:11:05 +08:00
|
|
|
break;
|
2017-05-02 01:07:49 +08:00
|
|
|
case WeakTracking:
|
2017-04-27 00:37:05 +08:00
|
|
|
// Weak goes to the new value, which will unlink it from Old's list.
|
2009-10-13 01:43:32 +08:00
|
|
|
Entry->operator=(New);
|
2009-04-01 06:11:05 +08:00
|
|
|
break;
|
|
|
|
case Callback:
|
2009-05-03 05:10:48 +08:00
|
|
|
// Forward to the subclass's implementation.
|
2009-10-13 01:43:32 +08:00
|
|
|
static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
|
2009-05-03 05:10:48 +08:00
|
|
|
break;
|
2009-04-01 06:11:05 +08:00
|
|
|
}
|
|
|
|
}
|
2010-07-27 14:53:14 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2017-05-02 00:28:58 +08:00
|
|
|
// If any new weak value handles were added while processing the
|
2010-07-27 14:53:14 +08:00
|
|
|
// list, then complain about it now.
|
|
|
|
if (Old->HasValueHandle)
|
|
|
|
for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
|
|
|
|
switch (Entry->getKind()) {
|
2017-05-02 01:07:49 +08:00
|
|
|
case WeakTracking:
|
2010-07-27 14:53:14 +08:00
|
|
|
dbgs() << "After RAUW from " << *Old->getType() << " %"
|
2011-11-16 00:27:03 +08:00
|
|
|
<< Old->getName() << " to " << *New->getType() << " %"
|
|
|
|
<< New->getName() << "\n";
|
2017-05-02 00:28:58 +08:00
|
|
|
llvm_unreachable(
|
2017-05-02 01:07:49 +08:00
|
|
|
"A weak tracking value handle still pointed to the old value!\n");
|
2010-07-27 14:53:14 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2009-04-01 06:11:05 +08:00
|
|
|
}
|
|
|
|
|
2013-11-19 08:57:56 +08:00
|
|
|
// Pin the vtable to this file.
|
|
|
|
void CallbackVH::anchor() {}
|