Simplify aggregate initilizer implementation. Use the CodeGenModule::EmitConstantExpr method when

possible.
Fix mediabench/mpeg2/mpeg2dec test.

llvm-svn: 47336
This commit is contained in:
Lauro Ramos Venancio 2008-02-19 19:27:31 +00:00
parent 9fd2531b5e
commit e2162c6549
2 changed files with 15 additions and 43 deletions

View File

@ -85,8 +85,6 @@ public:
private: private:
llvm::Constant *GetConstantInit(InitListExpr *E,
const llvm::ArrayType *AType);
void EmitNonConstInit(Expr *E, llvm::Value *Dest, const llvm::Type *DestType); void EmitNonConstInit(Expr *E, llvm::Value *Dest, const llvm::Type *DestType);
}; };
} // end anonymous namespace. } // end anonymous namespace.
@ -236,35 +234,6 @@ void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
CGF.EmitBlock(ContBlock); CGF.EmitBlock(ContBlock);
} }
llvm::Constant *AggExprEmitter::GetConstantInit(InitListExpr *E,
const llvm::ArrayType *AType) {
std::vector<llvm::Constant*> ArrayElts;
unsigned NumInitElements = E->getNumInits();
const llvm::Type *ElementType = AType->getElementType();
unsigned i;
for (i = 0; i != NumInitElements; ++i) {
if (InitListExpr *InitList = dyn_cast<InitListExpr>(E->getInit(i))) {
assert(isa<llvm::ArrayType>(ElementType) && "Invalid initilizer");
llvm::Constant *C =
GetConstantInit(InitList, cast<llvm::ArrayType>(ElementType));
if (!C) return NULL;
ArrayElts.push_back(C);
} else if (llvm::Constant *C =
dyn_cast<llvm::Constant>(CGF.EmitScalarExpr(E->getInit(i))))
ArrayElts.push_back(C);
else
return NULL;
}
// Remaining default initializers
unsigned NumArrayElements = AType->getNumElements();
for (/*Do not initialize i*/; i < NumArrayElements; ++i)
ArrayElts.push_back(llvm::Constant::getNullValue(ElementType));
return llvm::ConstantArray::get(AType, ArrayElts);
}
void AggExprEmitter::EmitNonConstInit(Expr *E, llvm::Value *DestPtr, void AggExprEmitter::EmitNonConstInit(Expr *E, llvm::Value *DestPtr,
const llvm::Type *DestType) { const llvm::Type *DestType) {
@ -309,17 +278,8 @@ void AggExprEmitter::EmitNonConstInit(Expr *E, llvm::Value *DestPtr,
void AggExprEmitter::VisitInitListExpr(InitListExpr *E) { void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
if (!E->getType()->isArrayType()) { if (E->isConstantExpr(CGF.CGM.getContext(), NULL)) {
CGF.WarnUnsupported(E, "aggregate init-list expression"); llvm::Constant *V = CGF.CGM.EmitConstantExpr(E);
return;
}
const llvm::PointerType *APType = cast<llvm::PointerType>(DestPtr->getType());
const llvm::ArrayType *AType =
cast<llvm::ArrayType>(APType->getElementType());
llvm::Constant *V = GetConstantInit(E, AType);
if (V) {
// Create global value to hold this array. // Create global value to hold this array.
V = new llvm::GlobalVariable(V->getType(), true, V = new llvm::GlobalVariable(V->getType(), true,
llvm::GlobalValue::InternalLinkage, llvm::GlobalValue::InternalLinkage,
@ -328,8 +288,19 @@ void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
EmitAggregateCopy(DestPtr, V , E->getType()); EmitAggregateCopy(DestPtr, V , E->getType());
return; return;
} else } else {
if (!E->getType()->isArrayType()) {
CGF.WarnUnsupported(E, "aggregate init-list expression");
return;
}
const llvm::PointerType *APType =
cast<llvm::PointerType>(DestPtr->getType());
const llvm::ArrayType *AType =
cast<llvm::ArrayType>(APType->getElementType());
EmitNonConstInit(E, DestPtr, AType); EmitNonConstInit(E, DestPtr, AType);
}
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//

View File

@ -11,4 +11,5 @@ void f2() {
int *c[2] = { &a[1][1], &b[2][2] }; int *c[2] = { &a[1][1], &b[2][2] };
int *d[2][2] = { {&a[1][1], &b[2][2]}, {&a[0][0], &b[1][1]} }; int *d[2][2] = { {&a[1][1], &b[2][2]}, {&a[0][0], &b[1][1]} };
int *e[3][3] = { {&a[1][1], &b[2][2]}, {&a[0][0], &b[1][1]} }; int *e[3][3] = { {&a[1][1], &b[2][2]}, {&a[0][0], &b[1][1]} };
char ext[3][3] = {".Y",".U",".V"};
} }