2012-08-03 00:30:35 +00:00
|
|
|
//===-- TypeFinder.cpp - Implement the TypeFinder class -------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2013-01-02 09:10:48 +00:00
|
|
|
// This file implements the TypeFinder class for the IR library.
|
2012-08-03 00:30:35 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-07 15:43:51 +00:00
|
|
|
#include "llvm/IR/TypeFinder.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Metadata.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2012-08-03 00:30:35 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
void TypeFinder::run(const Module &M, bool onlyNamed) {
|
|
|
|
OnlyNamed = onlyNamed;
|
|
|
|
|
|
|
|
// Get types from global variables.
|
|
|
|
for (Module::const_global_iterator I = M.global_begin(),
|
|
|
|
E = M.global_end(); I != E; ++I) {
|
|
|
|
incorporateType(I->getType());
|
|
|
|
if (I->hasInitializer())
|
|
|
|
incorporateValue(I->getInitializer());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get types from aliases.
|
|
|
|
for (Module::const_alias_iterator I = M.alias_begin(),
|
|
|
|
E = M.alias_end(); I != E; ++I) {
|
|
|
|
incorporateType(I->getType());
|
|
|
|
if (const Value *Aliasee = I->getAliasee())
|
|
|
|
incorporateValue(Aliasee);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get types from functions.
|
2014-11-11 21:30:22 +00:00
|
|
|
SmallVector<std::pair<unsigned, MDNode *>, 4> MDForInst;
|
2012-08-03 00:30:35 +00:00
|
|
|
for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
|
|
|
|
incorporateType(FI->getType());
|
|
|
|
|
2013-09-16 01:08:15 +00:00
|
|
|
if (FI->hasPrefixData())
|
|
|
|
incorporateValue(FI->getPrefixData());
|
|
|
|
|
2014-12-03 02:08:38 +00:00
|
|
|
if (FI->hasPrologueData())
|
|
|
|
incorporateValue(FI->getPrologueData());
|
|
|
|
|
2015-06-17 20:52:32 +00:00
|
|
|
if (FI->hasPersonalityFn())
|
|
|
|
incorporateValue(FI->getPersonalityFn());
|
|
|
|
|
2012-08-03 00:30:35 +00:00
|
|
|
// First incorporate the arguments.
|
|
|
|
for (Function::const_arg_iterator AI = FI->arg_begin(),
|
|
|
|
AE = FI->arg_end(); AI != AE; ++AI)
|
|
|
|
incorporateValue(AI);
|
|
|
|
|
|
|
|
for (Function::const_iterator BB = FI->begin(), E = FI->end();
|
|
|
|
BB != E;++BB)
|
|
|
|
for (BasicBlock::const_iterator II = BB->begin(),
|
|
|
|
E = BB->end(); II != E; ++II) {
|
|
|
|
const Instruction &I = *II;
|
|
|
|
|
|
|
|
// Incorporate the type of the instruction.
|
|
|
|
incorporateType(I.getType());
|
|
|
|
|
|
|
|
// Incorporate non-instruction operand types. (We are incorporating all
|
|
|
|
// instructions with this loop.)
|
|
|
|
for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
|
|
|
|
OI != OE; ++OI)
|
2015-03-10 23:55:38 +00:00
|
|
|
if (*OI && !isa<Instruction>(OI))
|
2012-08-03 00:30:35 +00:00
|
|
|
incorporateValue(*OI);
|
|
|
|
|
|
|
|
// Incorporate types hiding in metadata.
|
|
|
|
I.getAllMetadataOtherThanDebugLoc(MDForInst);
|
|
|
|
for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
|
2014-11-11 21:30:22 +00:00
|
|
|
incorporateMDNode(MDForInst[i].second);
|
2012-08-03 00:30:35 +00:00
|
|
|
|
|
|
|
MDForInst.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
|
|
|
|
E = M.named_metadata_end(); I != E; ++I) {
|
|
|
|
const NamedMDNode *NMD = I;
|
|
|
|
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
|
2014-11-11 21:30:22 +00:00
|
|
|
incorporateMDNode(NMD->getOperand(i));
|
2012-08-03 00:30:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TypeFinder::clear() {
|
|
|
|
VisitedConstants.clear();
|
|
|
|
VisitedTypes.clear();
|
|
|
|
StructTypes.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// incorporateType - This method adds the type to the list of used structures
|
|
|
|
/// if it's not in there already.
|
|
|
|
void TypeFinder::incorporateType(Type *Ty) {
|
2013-10-16 04:10:06 +00:00
|
|
|
// Check to see if we've already visited this type.
|
2012-08-03 00:30:35 +00:00
|
|
|
if (!VisitedTypes.insert(Ty).second)
|
|
|
|
return;
|
|
|
|
|
2013-10-16 04:10:06 +00:00
|
|
|
SmallVector<Type *, 4> TypeWorklist;
|
|
|
|
TypeWorklist.push_back(Ty);
|
|
|
|
do {
|
|
|
|
Ty = TypeWorklist.pop_back_val();
|
|
|
|
|
|
|
|
// If this is a structure or opaque type, add a name for the type.
|
|
|
|
if (StructType *STy = dyn_cast<StructType>(Ty))
|
|
|
|
if (!OnlyNamed || STy->hasName())
|
|
|
|
StructTypes.push_back(STy);
|
|
|
|
|
|
|
|
// Add all unvisited subtypes to worklist for processing
|
|
|
|
for (Type::subtype_reverse_iterator I = Ty->subtype_rbegin(),
|
|
|
|
E = Ty->subtype_rend();
|
|
|
|
I != E; ++I)
|
|
|
|
if (VisitedTypes.insert(*I).second)
|
|
|
|
TypeWorklist.push_back(*I);
|
|
|
|
} while (!TypeWorklist.empty());
|
2012-08-03 00:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// incorporateValue - This method is used to walk operand lists finding types
|
|
|
|
/// hiding in constant expressions and other operands that won't be walked in
|
|
|
|
/// other ways. GlobalValues, basic blocks, instructions, and inst operands are
|
|
|
|
/// all explicitly enumerated.
|
|
|
|
void TypeFinder::incorporateValue(const Value *V) {
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-09 18:38:53 +00:00
|
|
|
if (const auto *M = dyn_cast<MetadataAsValue>(V)) {
|
|
|
|
if (const auto *N = dyn_cast<MDNode>(M->getMetadata()))
|
|
|
|
return incorporateMDNode(N);
|
|
|
|
if (const auto *MDV = dyn_cast<ValueAsMetadata>(M->getMetadata()))
|
|
|
|
return incorporateValue(MDV->getValue());
|
|
|
|
return;
|
|
|
|
}
|
2012-08-03 00:30:35 +00:00
|
|
|
|
|
|
|
if (!isa<Constant>(V) || isa<GlobalValue>(V)) return;
|
|
|
|
|
|
|
|
// Already visited?
|
|
|
|
if (!VisitedConstants.insert(V).second)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check this type.
|
|
|
|
incorporateType(V->getType());
|
|
|
|
|
|
|
|
// If this is an instruction, we incorporate it separately.
|
|
|
|
if (isa<Instruction>(V))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Look in operands for types.
|
|
|
|
const User *U = cast<User>(V);
|
|
|
|
for (Constant::const_op_iterator I = U->op_begin(),
|
|
|
|
E = U->op_end(); I != E;++I)
|
|
|
|
incorporateValue(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// incorporateMDNode - This method is used to walk the operands of an MDNode to
|
|
|
|
/// find types hiding within.
|
|
|
|
void TypeFinder::incorporateMDNode(const MDNode *V) {
|
|
|
|
// Already visited?
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-09 18:38:53 +00:00
|
|
|
if (!VisitedMetadata.insert(V).second)
|
2012-08-03 00:30:35 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Look in operands for types.
|
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532. Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.
I have a follow-up patch prepared for `clang`. If this breaks other
sub-projects, I apologize in advance :(. Help me compile it on Darwin
I'll try to fix it. FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.
This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.
Here's a quick guide for updating your code:
- `Metadata` is the root of a class hierarchy with three main classes:
`MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from
the `Value` class hierarchy. It is typeless -- i.e., instances do
*not* have a `Type`.
- `MDNode`'s operands are all `Metadata *` (instead of `Value *`).
- `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.
If you're referring solely to resolved `MDNode`s -- post graph
construction -- just use `MDNode*`.
- `MDNode` (and the rest of `Metadata`) have only limited support for
`replaceAllUsesWith()`.
As long as an `MDNode` is pointing at a forward declaration -- the
result of `MDNode::getTemporary()` -- it maintains a side map of its
uses and can RAUW itself. Once the forward declarations are fully
resolved RAUW support is dropped on the ground. This means that
uniquing collisions on changing operands cause nodes to become
"distinct". (This already happened fairly commonly, whenever an
operand went to null.)
If you're constructing complex (non self-reference) `MDNode` cycles,
you need to call `MDNode::resolveCycles()` on each node (or on a
top-level node that somehow references all of the nodes). Also,
don't do that. Metadata cycles (and the RAUW machinery needed to
construct them) are expensive.
- An `MDNode` can only refer to a `Constant` through a bridge called
`ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).
As a side effect, accessing an operand of an `MDNode` that is known
to be, e.g., `ConstantInt`, takes three steps: first, cast from
`Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
third, cast down to `ConstantInt`.
The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
metadata schema owners transition away from using `Constant`s when
the type isn't important (and they don't care about referring to
`GlobalValue`s).
In the meantime, I've added transitional API to the `mdconst`
namespace that matches semantics with the old code, in order to
avoid adding the error-prone three-step equivalent to every call
site. If your old code was:
MDNode *N = foo();
bar(isa <ConstantInt>(N->getOperand(0)));
baz(cast <ConstantInt>(N->getOperand(1)));
bak(cast_or_null <ConstantInt>(N->getOperand(2)));
bat(dyn_cast <ConstantInt>(N->getOperand(3)));
bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));
you can trivially match its semantics with:
MDNode *N = foo();
bar(mdconst::hasa <ConstantInt>(N->getOperand(0)));
baz(mdconst::extract <ConstantInt>(N->getOperand(1)));
bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2)));
bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3)));
bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));
and when you transition your metadata schema to `MDInt`:
MDNode *N = foo();
bar(isa <MDInt>(N->getOperand(0)));
baz(cast <MDInt>(N->getOperand(1)));
bak(cast_or_null <MDInt>(N->getOperand(2)));
bat(dyn_cast <MDInt>(N->getOperand(3)));
bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));
- A `CallInst` -- specifically, intrinsic instructions -- can refer to
metadata through a bridge called `MetadataAsValue`. This is a
subclass of `Value` where `getType()->isMetadataTy()`.
`MetadataAsValue` is the *only* class that can legally refer to a
`LocalAsMetadata`, which is a bridged form of non-`Constant` values
like `Argument` and `Instruction`. It can also refer to any other
`Metadata` subclass.
(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-09 18:38:53 +00:00
|
|
|
for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i) {
|
|
|
|
Metadata *Op = V->getOperand(i);
|
|
|
|
if (!Op)
|
|
|
|
continue;
|
|
|
|
if (auto *N = dyn_cast<MDNode>(Op)) {
|
|
|
|
incorporateMDNode(N);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (auto *C = dyn_cast<ConstantAsMetadata>(Op)) {
|
|
|
|
incorporateValue(C->getValue());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-08-03 00:30:35 +00:00
|
|
|
}
|