2017-09-08 07:28:24 +08:00
|
|
|
//===- ValueList.cpp - Internal BitcodeReader implementation --------------===//
|
2016-12-13 03:34:26 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-12-13 03:34:26 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ValueList.h"
|
2017-09-08 07:28:24 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/IR/Argument.h"
|
|
|
|
#include "llvm/IR/Constant.h"
|
2016-12-13 03:34:26 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2017-09-08 07:28:24 +08:00
|
|
|
#include "llvm/IR/GlobalValue.h"
|
|
|
|
#include "llvm/IR/Instruction.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/IR/User.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
|
|
|
#include "llvm/IR/ValueHandle.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <limits>
|
|
|
|
#include <utility>
|
2016-12-13 03:34:26 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace llvm {
|
2017-09-08 07:28:24 +08:00
|
|
|
|
2016-12-13 03:34:26 +08:00
|
|
|
namespace {
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// A class for maintaining the slot number definition
|
2016-12-13 03:34:26 +08:00
|
|
|
/// as a placeholder for the actual definition for forward constants defs.
|
|
|
|
class ConstantPlaceHolder : public ConstantExpr {
|
|
|
|
public:
|
|
|
|
explicit ConstantPlaceHolder(Type *Ty, LLVMContext &Context)
|
|
|
|
: ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
|
|
|
|
Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
|
|
|
|
}
|
|
|
|
|
2017-09-08 07:28:24 +08:00
|
|
|
ConstantPlaceHolder &operator=(const ConstantPlaceHolder &) = delete;
|
|
|
|
|
|
|
|
// allocate space for exactly one operand
|
|
|
|
void *operator new(size_t s) { return User::operator new(s, 1); }
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Methods to support type inquiry through isa, cast, and dyn_cast.
|
2016-12-13 03:34:26 +08:00
|
|
|
static bool classof(const Value *V) {
|
|
|
|
return isa<ConstantExpr>(V) &&
|
|
|
|
cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Provide fast operand accessors
|
|
|
|
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
// FIXME: can we inherit this from ConstantExpr?
|
|
|
|
template <>
|
|
|
|
struct OperandTraits<ConstantPlaceHolder>
|
|
|
|
: public FixedNumOperandTraits<ConstantPlaceHolder, 1> {};
|
|
|
|
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value)
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
2019-06-27 22:46:51 +08:00
|
|
|
void BitcodeReaderValueList::assignValue(Value *V, unsigned Idx, Type *FullTy) {
|
2016-12-13 03:34:26 +08:00
|
|
|
if (Idx == size()) {
|
2019-06-27 22:46:51 +08:00
|
|
|
push_back(V, FullTy);
|
2016-12-13 03:34:26 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Idx >= size())
|
|
|
|
resize(Idx + 1);
|
|
|
|
|
2019-06-27 22:46:51 +08:00
|
|
|
assert(FullTypes[Idx] == nullptr || FullTypes[Idx] == FullTy);
|
|
|
|
FullTypes[Idx] = FullTy;
|
|
|
|
|
2017-05-02 01:07:49 +08:00
|
|
|
WeakTrackingVH &OldV = ValuePtrs[Idx];
|
2016-12-13 03:34:26 +08:00
|
|
|
if (!OldV) {
|
|
|
|
OldV = V;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle constants and non-constants (e.g. instrs) differently for
|
|
|
|
// efficiency.
|
|
|
|
if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
|
|
|
|
ResolveConstants.push_back(std::make_pair(PHC, Idx));
|
|
|
|
OldV = V;
|
|
|
|
} else {
|
|
|
|
// If there was a forward reference to this value, replace it.
|
|
|
|
Value *PrevVal = OldV;
|
|
|
|
OldV->replaceAllUsesWith(V);
|
[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
|
|
|
PrevVal->deleteValue();
|
2016-12-13 03:34:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx, Type *Ty) {
|
2019-07-14 20:35:50 +08:00
|
|
|
// Bail out for a clearly invalid value.
|
|
|
|
if (Idx >= RefsUpperBound)
|
|
|
|
return nullptr;
|
|
|
|
|
2016-12-13 03:34:26 +08:00
|
|
|
if (Idx >= size())
|
|
|
|
resize(Idx + 1);
|
|
|
|
|
|
|
|
if (Value *V = ValuePtrs[Idx]) {
|
|
|
|
if (Ty != V->getType())
|
|
|
|
report_fatal_error("Type mismatch in constant table!");
|
|
|
|
return cast<Constant>(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and return a placeholder, which will later be RAUW'd.
|
|
|
|
Constant *C = new ConstantPlaceHolder(Ty, Context);
|
|
|
|
ValuePtrs[Idx] = C;
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2019-06-27 22:46:51 +08:00
|
|
|
Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty,
|
|
|
|
Type **FullTy) {
|
2019-07-14 20:35:50 +08:00
|
|
|
// Bail out for a clearly invalid value.
|
|
|
|
if (Idx >= RefsUpperBound)
|
2016-12-13 03:34:26 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (Idx >= size())
|
|
|
|
resize(Idx + 1);
|
|
|
|
|
|
|
|
if (Value *V = ValuePtrs[Idx]) {
|
|
|
|
// If the types don't match, it's invalid.
|
|
|
|
if (Ty && Ty != V->getType())
|
|
|
|
return nullptr;
|
2019-06-27 22:46:51 +08:00
|
|
|
if (FullTy)
|
|
|
|
*FullTy = FullTypes[Idx];
|
2016-12-13 03:34:26 +08:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No type specified, must be invalid reference.
|
|
|
|
if (!Ty)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Create and return a placeholder, which will later be RAUW'd.
|
|
|
|
Value *V = new Argument(Ty);
|
|
|
|
ValuePtrs[Idx] = V;
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Once all constants are read, this method bulk resolves any forward
|
|
|
|
/// references. The idea behind this is that we sometimes get constants (such
|
|
|
|
/// as large arrays) which reference *many* forward ref constants. Replacing
|
|
|
|
/// each of these causes a lot of thrashing when building/reuniquing the
|
|
|
|
/// constant. Instead of doing this, we look at all the uses and rewrite all
|
|
|
|
/// the place holders at once for any constant that uses a placeholder.
|
|
|
|
void BitcodeReaderValueList::resolveConstantForwardRefs() {
|
|
|
|
// Sort the values by-pointer so that they are efficient to look up with a
|
|
|
|
// binary search.
|
llvm::sort(C.begin(), C.end(), ...) -> llvm::sort(C, ...)
Summary: The convenience wrapper in STLExtras is available since rL342102.
Reviewers: dblaikie, javed.absar, JDevlieghere, andreadb
Subscribers: MatzeB, sanjoy, arsenm, dschuff, mehdi_amini, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, eraman, aheejin, kbarton, JDevlieghere, javed.absar, gbedwell, jrtc27, mgrang, atanasyan, steven_wu, george.burgess.iv, dexonsmith, kristina, jsji, llvm-commits
Differential Revision: https://reviews.llvm.org/D52573
llvm-svn: 343163
2018-09-27 10:13:45 +08:00
|
|
|
llvm::sort(ResolveConstants);
|
2016-12-13 03:34:26 +08:00
|
|
|
|
|
|
|
SmallVector<Constant *, 64> NewOps;
|
|
|
|
|
|
|
|
while (!ResolveConstants.empty()) {
|
|
|
|
Value *RealVal = operator[](ResolveConstants.back().second);
|
|
|
|
Constant *Placeholder = ResolveConstants.back().first;
|
|
|
|
ResolveConstants.pop_back();
|
|
|
|
|
|
|
|
// Loop over all users of the placeholder, updating them to reference the
|
|
|
|
// new value. If they reference more than one placeholder, update them all
|
|
|
|
// at once.
|
|
|
|
while (!Placeholder->use_empty()) {
|
|
|
|
auto UI = Placeholder->user_begin();
|
|
|
|
User *U = *UI;
|
|
|
|
|
|
|
|
// If the using object isn't uniqued, just update the operands. This
|
|
|
|
// handles instructions and initializers for global variables.
|
|
|
|
if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
|
|
|
|
UI.getUse().set(RealVal);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we have a constant that uses the placeholder. Replace that
|
|
|
|
// constant with a new constant that has *all* placeholder uses updated.
|
|
|
|
Constant *UserC = cast<Constant>(U);
|
|
|
|
for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end(); I != E;
|
|
|
|
++I) {
|
|
|
|
Value *NewOp;
|
|
|
|
if (!isa<ConstantPlaceHolder>(*I)) {
|
|
|
|
// Not a placeholder reference.
|
|
|
|
NewOp = *I;
|
|
|
|
} else if (*I == Placeholder) {
|
|
|
|
// Common case is that it just references this one placeholder.
|
|
|
|
NewOp = RealVal;
|
|
|
|
} else {
|
|
|
|
// Otherwise, look up the placeholder in ResolveConstants.
|
2019-04-12 10:02:06 +08:00
|
|
|
ResolveConstantsTy::iterator It = llvm::lower_bound(
|
|
|
|
ResolveConstants,
|
2016-12-13 03:34:26 +08:00
|
|
|
std::pair<Constant *, unsigned>(cast<Constant>(*I), 0));
|
|
|
|
assert(It != ResolveConstants.end() && It->first == *I);
|
|
|
|
NewOp = operator[](It->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
NewOps.push_back(cast<Constant>(NewOp));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the new constant.
|
|
|
|
Constant *NewC;
|
|
|
|
if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
|
|
|
|
NewC = ConstantArray::get(UserCA->getType(), NewOps);
|
|
|
|
} else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
|
|
|
|
NewC = ConstantStruct::get(UserCS->getType(), NewOps);
|
|
|
|
} else if (isa<ConstantVector>(UserC)) {
|
|
|
|
NewC = ConstantVector::get(NewOps);
|
|
|
|
} else {
|
|
|
|
assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
|
|
|
|
NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
|
|
|
|
}
|
|
|
|
|
|
|
|
UserC->replaceAllUsesWith(NewC);
|
|
|
|
UserC->destroyConstant();
|
|
|
|
NewOps.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update all ValueHandles, they should be the only users at this point.
|
|
|
|
Placeholder->replaceAllUsesWith(RealVal);
|
[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
|
|
|
Placeholder->deleteValue();
|
2016-12-13 03:34:26 +08:00
|
|
|
}
|
|
|
|
}
|