mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-09-26 09:18:56 +00:00
recommit bcreader, handling packed structs correctly. Apparently
people want fast *and* correct. Sheesh. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@55102 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -133,6 +133,15 @@ namespace {
|
|||||||
: ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
|
: ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
|
||||||
Op<0>() = UndefValue::get(Type::Int32Ty);
|
Op<0>() = UndefValue::get(Type::Int32Ty);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
|
||||||
|
static inline bool classof(const ConstantPlaceHolder *) { return true; }
|
||||||
|
static bool classof(const Value *V) {
|
||||||
|
return isa<ConstantExpr>(V) &&
|
||||||
|
cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Provide fast operand accessors
|
/// Provide fast operand accessors
|
||||||
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
|
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
|
||||||
};
|
};
|
||||||
@@ -205,6 +214,86 @@ Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, const Type *Ty) {
|
|||||||
return V;
|
return V;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// ResolveConstantForwardRefs - 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.
|
||||||
|
std::sort(ResolveConstants.begin(), ResolveConstants.end());
|
||||||
|
|
||||||
|
SmallVector<Constant*, 64> NewOps;
|
||||||
|
|
||||||
|
while (!ResolveConstants.empty()) {
|
||||||
|
Value *RealVal = getOperand(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()) {
|
||||||
|
User *U = Placeholder->use_back();
|
||||||
|
// 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)) {
|
||||||
|
U->replaceUsesOfWith(Placeholder, 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.
|
||||||
|
ResolveConstantsTy::iterator It =
|
||||||
|
std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
|
||||||
|
std::pair<Constant*, unsigned>(cast<Constant>(*I),
|
||||||
|
0));
|
||||||
|
assert(It != ResolveConstants.end() && It->first == *I);
|
||||||
|
NewOp = this->getOperand(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[0], NewOps.size());
|
||||||
|
} else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
|
||||||
|
NewC = ConstantStruct::get(&NewOps[0], NewOps.size(),
|
||||||
|
UserCS->getType()->isPacked());
|
||||||
|
} else if (isa<ConstantVector>(UserC)) {
|
||||||
|
NewC = ConstantVector::get(&NewOps[0], NewOps.size());
|
||||||
|
} else {
|
||||||
|
// Must be a constant expression.
|
||||||
|
NewC = cast<ConstantExpr>(UserC)->getWithOperands(&NewOps[0],
|
||||||
|
NewOps.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
UserC->replaceAllUsesWith(NewC);
|
||||||
|
UserC->destroyConstant();
|
||||||
|
NewOps.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
delete Placeholder;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const Type *BitcodeReader::getTypeByID(unsigned ID, bool isTypeTable) {
|
const Type *BitcodeReader::getTypeByID(unsigned ID, bool isTypeTable) {
|
||||||
// If the TypeID is in range, return it.
|
// If the TypeID is in range, return it.
|
||||||
@@ -602,14 +691,8 @@ bool BitcodeReader::ParseConstants() {
|
|||||||
unsigned NextCstNo = ValueList.size();
|
unsigned NextCstNo = ValueList.size();
|
||||||
while (1) {
|
while (1) {
|
||||||
unsigned Code = Stream.ReadCode();
|
unsigned Code = Stream.ReadCode();
|
||||||
if (Code == bitc::END_BLOCK) {
|
if (Code == bitc::END_BLOCK)
|
||||||
if (NextCstNo != ValueList.size())
|
break;
|
||||||
return Error("Invalid constant reference!");
|
|
||||||
|
|
||||||
if (Stream.ReadBlockEnd())
|
|
||||||
return Error("Error at end of constants block");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Code == bitc::ENTER_SUBBLOCK) {
|
if (Code == bitc::ENTER_SUBBLOCK) {
|
||||||
// No known subblocks, always skip them.
|
// No known subblocks, always skip them.
|
||||||
@@ -852,6 +935,17 @@ bool BitcodeReader::ParseConstants() {
|
|||||||
ValueList.AssignValue(V, NextCstNo);
|
ValueList.AssignValue(V, NextCstNo);
|
||||||
++NextCstNo;
|
++NextCstNo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (NextCstNo != ValueList.size())
|
||||||
|
return Error("Invalid constant reference!");
|
||||||
|
|
||||||
|
if (Stream.ReadBlockEnd())
|
||||||
|
return Error("Error at end of constants block");
|
||||||
|
|
||||||
|
// Once all the constants have been read, go through and resolve forward
|
||||||
|
// references.
|
||||||
|
ValueList.ResolveConstantForwardRefs();
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// RememberAndSkipFunctionBody - When we see the block for a function body,
|
/// RememberAndSkipFunctionBody - When we see the block for a function body,
|
||||||
|
@@ -32,9 +32,22 @@ namespace llvm {
|
|||||||
|
|
||||||
class BitcodeReaderValueList : public User {
|
class BitcodeReaderValueList : public User {
|
||||||
unsigned Capacity;
|
unsigned Capacity;
|
||||||
|
|
||||||
|
/// ResolveConstants - As we resolve forward-referenced constants, we add
|
||||||
|
/// information about them to this vector. This allows us to resolve them in
|
||||||
|
/// bulk instead of resolving each reference at a time. See the code in
|
||||||
|
/// ResolveConstantForwardRefs for more information about this.
|
||||||
|
///
|
||||||
|
/// The key of this vector is the placeholder constant, the value is the slot
|
||||||
|
/// number that holds the resolved value.
|
||||||
|
typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
|
||||||
|
ResolveConstantsTy ResolveConstants;
|
||||||
public:
|
public:
|
||||||
BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0)
|
BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0)
|
||||||
, Capacity(0) {}
|
, Capacity(0) {}
|
||||||
|
~BitcodeReaderValueList() {
|
||||||
|
assert(ResolveConstants.empty() && "Constants not resolved?");
|
||||||
|
}
|
||||||
|
|
||||||
/// Provide fast operand accessors
|
/// Provide fast operand accessors
|
||||||
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
|
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
|
||||||
@@ -50,6 +63,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
|
assert(ResolveConstants.empty() && "Constants not resolved?");
|
||||||
if (OperandList) dropHungoffUses(OperandList);
|
if (OperandList) dropHungoffUses(OperandList);
|
||||||
Capacity = 0;
|
Capacity = 0;
|
||||||
}
|
}
|
||||||
@@ -73,15 +87,26 @@ public:
|
|||||||
if (Idx == size()) {
|
if (Idx == size()) {
|
||||||
push_back(V);
|
push_back(V);
|
||||||
} else if (Value *OldV = getOperand(Idx)) {
|
} else if (Value *OldV = getOperand(Idx)) {
|
||||||
// If there was a forward reference to this value, replace it.
|
// Handle constants and non-constants (e.g. instrs) differently for
|
||||||
setOperand(Idx, V);
|
// efficiency.
|
||||||
OldV->replaceAllUsesWith(V);
|
if (Constant *PHC = dyn_cast<Constant>(OldV)) {
|
||||||
delete OldV;
|
ResolveConstants.push_back(std::make_pair(PHC, Idx));
|
||||||
|
setOperand(Idx, V);
|
||||||
|
} else {
|
||||||
|
// If there was a forward reference to this value, replace it.
|
||||||
|
setOperand(Idx, V);
|
||||||
|
OldV->replaceAllUsesWith(V);
|
||||||
|
delete OldV;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
initVal(Idx, V);
|
initVal(Idx, V);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// ResolveConstantForwardRefs - Once all constants are read, this method bulk
|
||||||
|
/// resolves any forward references.
|
||||||
|
void ResolveConstantForwardRefs();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void initVal(unsigned Idx, Value *V) {
|
void initVal(unsigned Idx, Value *V) {
|
||||||
if (Idx >= size()) {
|
if (Idx >= size()) {
|
||||||
@@ -94,7 +119,8 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct OperandTraits<BitcodeReaderValueList> : HungoffOperandTraits</*16 FIXME*/> {
|
struct OperandTraits<BitcodeReaderValueList>
|
||||||
|
: HungoffOperandTraits</*16 FIXME*/> {
|
||||||
};
|
};
|
||||||
|
|
||||||
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BitcodeReaderValueList, Value)
|
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BitcodeReaderValueList, Value)
|
||||||
|
Reference in New Issue
Block a user