mirror of
https://github.com/RPCS3/llvm.git
synced 2025-02-22 03:31:06 +00:00
Cleanup ConstantExpr handling:
* Correctly delete TypeHandles in AsmParser. In addition to not leaking memory, this prevents a bug that could have occurred when a type got resolved that the constexpr was using * Check for errors in the AsmParser instead of hitting assertion failures deep in the code * Simplify the interface to the ConstantExpr class, removing unneccesary parameters to the ::get* methods. * Rename the 'getelementptr' version of ConstantExpr::get to ConstantExpr::getGetElementPtr git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@3161 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
e8e4605021
commit
c188eeb08c
@ -8,17 +8,14 @@
|
|||||||
#include "ParserInternals.h"
|
#include "ParserInternals.h"
|
||||||
#include "llvm/SymbolTable.h"
|
#include "llvm/SymbolTable.h"
|
||||||
#include "llvm/Module.h"
|
#include "llvm/Module.h"
|
||||||
#include "llvm/GlobalVariable.h"
|
|
||||||
#include "llvm/iTerminators.h"
|
#include "llvm/iTerminators.h"
|
||||||
#include "llvm/iMemory.h"
|
#include "llvm/iMemory.h"
|
||||||
#include "llvm/iPHINode.h"
|
#include "llvm/iPHINode.h"
|
||||||
#include "llvm/Argument.h"
|
|
||||||
#include "Support/STLExtras.h"
|
#include "Support/STLExtras.h"
|
||||||
#include "Support/DepthFirstIterator.h"
|
#include "Support/DepthFirstIterator.h"
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <utility> // Get definition of pair class
|
#include <utility>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
|
||||||
using std::list;
|
using std::list;
|
||||||
using std::vector;
|
using std::vector;
|
||||||
using std::pair;
|
using std::pair;
|
||||||
@ -972,33 +969,53 @@ ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
|
|||||||
|
|
||||||
// FIXME: ConstExpr::get never return null! Do checking here in the parser.
|
// FIXME: ConstExpr::get never return null! Do checking here in the parser.
|
||||||
ConstExpr: Types CAST ConstVal {
|
ConstExpr: Types CAST ConstVal {
|
||||||
$$ = ConstantExpr::get($2, $3, $1->get());
|
$$ = ConstantExpr::get(Instruction::Cast, $3, $1->get());
|
||||||
if ($$ == 0) ThrowException("constant expression builder returned null!");
|
if ($$ == 0) ThrowException("constant expression builder returned null!");
|
||||||
|
delete $1;
|
||||||
}
|
}
|
||||||
| Types GETELEMENTPTR '(' ConstVal IndexList ')' {
|
| Types GETELEMENTPTR '(' ConstVal IndexList ')' {
|
||||||
|
if (!isa<PointerType>($4->getType()))
|
||||||
|
ThrowException("GetElementPtr requires a pointer operand!");
|
||||||
|
|
||||||
|
const Type *IdxTy =
|
||||||
|
GetElementPtrInst::getIndexedType($4->getType(), *$5, true);
|
||||||
|
if (!IdxTy)
|
||||||
|
ThrowException("Index list invalid for constant getelementptr!");
|
||||||
|
if (PointerType::get(IdxTy) != $1->get())
|
||||||
|
ThrowException("Declared type of constant getelementptr is incorrect!");
|
||||||
|
|
||||||
vector<Constant*> IdxVec;
|
vector<Constant*> IdxVec;
|
||||||
for (unsigned i = 0, e = $5->size(); i != e; ++i)
|
for (unsigned i = 0, e = $5->size(); i != e; ++i)
|
||||||
if (Constant *C = dyn_cast<Constant>((*$5)[i]))
|
if (Constant *C = dyn_cast<Constant>((*$5)[i]))
|
||||||
IdxVec.push_back(C);
|
IdxVec.push_back(C);
|
||||||
else
|
else
|
||||||
ThrowException("Arguments to getelementptr must be constants!");
|
ThrowException("Indices to constant getelementptr must be constants!");
|
||||||
|
|
||||||
delete $5;
|
delete $5;
|
||||||
|
|
||||||
$$ = ConstantExpr::get($2, $4, IdxVec, $1->get());
|
$$ = ConstantExpr::getGetElementPtr($4, IdxVec);
|
||||||
if ($$ == 0) ThrowException("constant expression builder returned null!");
|
delete $1;
|
||||||
}
|
}
|
||||||
| Types UnaryOps ConstVal {
|
| Types UnaryOps ConstVal {
|
||||||
$$ = ConstantExpr::get($2, $3, $1->get());
|
$$ = ConstantExpr::get($2, $3, $1->get());
|
||||||
if ($$ == 0) ThrowException("constant expression builder returned null!");
|
delete $1;
|
||||||
}
|
}
|
||||||
| Types BinaryOps ConstVal ',' ConstVal {
|
| Types BinaryOps ConstVal ',' ConstVal {
|
||||||
$$ = ConstantExpr::get($2, $3, $5, $1->get());
|
if ($3->getType() != $5->getType())
|
||||||
if ($$ == 0) ThrowException("constant expression builder returned null!");
|
ThrowException("Binary operator types must match!");
|
||||||
|
if ($1->get() != $3->getType())
|
||||||
|
ThrowException("Return type of binary constant must match arguments!");
|
||||||
|
$$ = ConstantExpr::get($2, $3, $5);
|
||||||
|
delete $1;
|
||||||
}
|
}
|
||||||
| Types ShiftOps ConstVal ',' ConstVal {
|
| Types ShiftOps ConstVal ',' ConstVal {
|
||||||
$$ = ConstantExpr::get($2, $3, $5, $1->get());
|
if ($1->get() != $3->getType())
|
||||||
if ($$ == 0) ThrowException("constant expression builder returned null!");
|
ThrowException("Return type of shift constant must match argument!");
|
||||||
|
if ($5->getType() != Type::UByteTy)
|
||||||
|
ThrowException("Shift count for shift constant must be unsigned byte!");
|
||||||
|
|
||||||
|
$$ = ConstantExpr::get($2, $3, $5);
|
||||||
|
delete $1;
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@ -100,7 +100,8 @@ bool BytecodeWriter::outputConstant(const Constant *CPV) {
|
|||||||
// We must check for a ConstantExpr before switching by type because
|
// We must check for a ConstantExpr before switching by type because
|
||||||
// a ConstantExpr can be of any type, and has no explicit value.
|
// a ConstantExpr can be of any type, and has no explicit value.
|
||||||
//
|
//
|
||||||
if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CPV)) {
|
if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
|
||||||
|
// FIXME: Encoding of constant exprs could be much more compact!
|
||||||
assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
|
assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
|
||||||
output_vbr(CE->getNumOperands(), Out); // flags as an expr
|
output_vbr(CE->getNumOperands(), Out); // flags as an expr
|
||||||
output_vbr(CE->getOpcode(), Out); // flags as an expr
|
output_vbr(CE->getOpcode(), Out); // flags as an expr
|
||||||
@ -113,9 +114,9 @@ bool BytecodeWriter::outputConstant(const Constant *CPV) {
|
|||||||
output_vbr((unsigned)Slot, Out);
|
output_vbr((unsigned)Slot, Out);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
output_vbr((unsigned)0, Out); // flag as not a ConstantExpr
|
output_vbr((unsigned)0, Out); // flag as not a ConstantExpr
|
||||||
|
}
|
||||||
|
|
||||||
switch (CPV->getType()->getPrimitiveID()) {
|
switch (CPV->getType()->getPrimitiveID()) {
|
||||||
case Type::BoolTyID: // Boolean Types
|
case Type::BoolTyID: // Boolean Types
|
||||||
|
@ -333,7 +333,7 @@ static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName,
|
|||||||
Out << "<pointer reference without context info>";
|
Out << "<pointer reference without context info>";
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (const ConstantExpr *CE=dyn_cast<ConstantExpr>(CV)) {
|
} else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
|
||||||
Out << CE->getOpcodeName();
|
Out << CE->getOpcodeName();
|
||||||
|
|
||||||
bool isGEP = CE->getOpcode() == Instruction::GetElementPtr;
|
bool isGEP = CE->getOpcode() == Instruction::GetElementPtr;
|
||||||
@ -343,7 +343,7 @@ static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName,
|
|||||||
printTypeInt(Out, (*OI)->getType(), TypeTable);
|
printTypeInt(Out, (*OI)->getType(), TypeTable);
|
||||||
WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Table);
|
WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Table);
|
||||||
if (OI+1 != CE->op_end())
|
if (OI+1 != CE->op_end())
|
||||||
Out << ", "; // ((isGEP && OI == CE->op_begin())? " " : ", ");
|
Out << ", ";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isGEP)
|
if (isGEP)
|
||||||
|
@ -138,21 +138,20 @@ ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
|
|||||||
Operands.push_back(Use(GV, this));
|
Operands.push_back(Use(GV, this));
|
||||||
}
|
}
|
||||||
|
|
||||||
ConstantExpr::ConstantExpr(unsigned opCode, Constant *C, const Type *Ty)
|
ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
|
||||||
: Constant(Ty), iType(opCode) {
|
: Constant(Ty), iType(Opcode) {
|
||||||
Operands.push_back(Use(C, this));
|
Operands.push_back(Use(C, this));
|
||||||
}
|
}
|
||||||
|
|
||||||
ConstantExpr::ConstantExpr(unsigned opCode, Constant* C1,
|
ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
|
||||||
Constant* C2, const Type *Ty)
|
: Constant(C1->getType()), iType(Opcode) {
|
||||||
: Constant(Ty), iType(opCode) {
|
|
||||||
Operands.push_back(Use(C1, this));
|
Operands.push_back(Use(C1, this));
|
||||||
Operands.push_back(Use(C2, this));
|
Operands.push_back(Use(C2, this));
|
||||||
}
|
}
|
||||||
|
|
||||||
ConstantExpr::ConstantExpr(unsigned opCode, Constant* C,
|
ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
|
||||||
const std::vector<Constant*> &IdxList, const Type *Ty)
|
const Type *DestTy)
|
||||||
: Constant(Ty), iType(opCode) {
|
: Constant(DestTy), iType(Instruction::GetElementPtr) {
|
||||||
Operands.reserve(1+IdxList.size());
|
Operands.reserve(1+IdxList.size());
|
||||||
Operands.push_back(Use(C, this));
|
Operands.push_back(Use(C, this));
|
||||||
for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
|
for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
|
||||||
@ -386,7 +385,6 @@ ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//---- ConstantExpr::get() implementations...
|
//---- ConstantExpr::get() implementations...
|
||||||
// Return NULL on invalid expressions.
|
|
||||||
//
|
//
|
||||||
typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
|
typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
|
||||||
static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
|
static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
|
||||||
@ -415,60 +413,51 @@ ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C, const Type *Ty) {
|
|||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
|
ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
|
||||||
const Type *Ty) {
|
|
||||||
|
|
||||||
// Look up the constant in the table first to ensure uniqueness
|
// Look up the constant in the table first to ensure uniqueness
|
||||||
vector<Constant*> argVec(1, C1); argVec.push_back(C2);
|
vector<Constant*> argVec(1, C1); argVec.push_back(C2);
|
||||||
const ExprMapKeyType &Key = make_pair(Opcode, argVec);
|
const ExprMapKeyType &Key = make_pair(Opcode, argVec);
|
||||||
ConstantExpr *Result = ExprConstants.get(Ty, Key);
|
ConstantExpr *Result = ExprConstants.get(C1->getType(), Key);
|
||||||
if (Result) return Result;
|
if (Result) return Result;
|
||||||
|
|
||||||
// Its not in the table so create a new one and put it in the table.
|
// Its not in the table so create a new one and put it in the table.
|
||||||
// Check the operands for consistency first
|
// Check the operands for consistency first
|
||||||
assert((Opcode >= Instruction::FirstBinaryOp &&
|
assert((Opcode >= Instruction::FirstBinaryOp &&
|
||||||
Opcode < Instruction::NumBinaryOps) &&
|
Opcode < Instruction::NumBinaryOps) &&
|
||||||
"Invalid opcode in binary constant expression");
|
"Invalid opcode in binary constant expression");
|
||||||
|
|
||||||
assert(Ty == C1->getType() && Ty == C2->getType() &&
|
assert(C1->getType() == C2->getType() &&
|
||||||
"Operand types in binary constant expression should match result");
|
"Operand types in binary constant expression should match");
|
||||||
|
|
||||||
Result = new ConstantExpr(Opcode, C1, C2, Ty);
|
Result = new ConstantExpr(Opcode, C1, C2);
|
||||||
ExprConstants.add(Ty, Key, Result);
|
ExprConstants.add(C1->getType(), Key, Result);
|
||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C,
|
ConstantExpr *ConstantExpr::getGetElementPtr(Constant *C,
|
||||||
const std::vector<Constant*> &IdxList,
|
const std::vector<Constant*> &IdxList) {
|
||||||
const Type *Ty) {
|
const Type *Ty = C->getType();
|
||||||
|
|
||||||
// Look up the constant in the table first to ensure uniqueness
|
// Look up the constant in the table first to ensure uniqueness
|
||||||
vector<Constant*> argVec(1, C);
|
vector<Constant*> argVec(1, C);
|
||||||
argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
|
argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
|
||||||
|
|
||||||
const ExprMapKeyType &Key = make_pair(Opcode, argVec);
|
const ExprMapKeyType &Key = make_pair(Instruction::GetElementPtr, argVec);
|
||||||
ConstantExpr *Result = ExprConstants.get(Ty, Key);
|
ConstantExpr *Result = ExprConstants.get(Ty, Key);
|
||||||
if (Result) return Result;
|
if (Result) return Result;
|
||||||
|
|
||||||
// Its not in the table so create a new one and put it in the table.
|
// Its not in the table so create a new one and put it in the table.
|
||||||
// Check the operands for consistency first
|
// Check the operands for consistency first
|
||||||
// Must be a getElementPtr. Check for valid getElementPtr expression.
|
|
||||||
//
|
//
|
||||||
assert(Opcode == Instruction::GetElementPtr &&
|
|
||||||
"Operator other than GetElementPtr used with an index list");
|
|
||||||
|
|
||||||
assert(isa<PointerType>(Ty) &&
|
assert(isa<PointerType>(Ty) &&
|
||||||
"Non-pointer type for constant GelElementPtr expression");
|
"Non-pointer type for constant GelElementPtr expression");
|
||||||
|
|
||||||
|
// Check that the indices list is valid...
|
||||||
std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
|
std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
|
||||||
const Type *fldType = GetElementPtrInst::getIndexedType(C->getType(),
|
const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, true);
|
||||||
ValIdxList, true);
|
assert(DestTy && "Invalid index list for constant GelElementPtr expression");
|
||||||
assert(fldType && "Invalid index list for constant GelElementPtr expression");
|
|
||||||
|
|
||||||
assert(cast<PointerType>(Ty)->getElementType() == fldType &&
|
|
||||||
"Type for constant GelElementPtr expression doesn't match field type");
|
|
||||||
|
|
||||||
Result = new ConstantExpr(Opcode, C, IdxList, Ty);
|
Result = new ConstantExpr(C, IdxList, PointerType::get(DestTy));
|
||||||
ExprConstants.add(Ty, Key, Result);
|
ExprConstants.add(Ty, Key, Result);
|
||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
@ -480,8 +469,8 @@ void ConstantExpr::destroyConstant() {
|
|||||||
destroyConstantImpl();
|
destroyConstantImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *ConstantExpr::getOpcodeName(unsigned Opcode) {
|
const char *ConstantExpr::getOpcodeName() const {
|
||||||
return Instruction::getOpcodeName(Opcode);
|
return Instruction::getOpcodeName(getOpcode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user