From eac742b9f71823b3b17a3872d0cd77d0611ced19 Mon Sep 17 00:00:00 2001 From: David Nadlinger Date: Thu, 27 Dec 2012 23:22:46 +0100 Subject: [PATCH] Don't create IrType twice for aggregate arrays with forward references. GitHub: Fixes #257. --- ir/irtype.cpp | 39 ++++++++++++++++----------------------- ir/irtype.h | 4 +--- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/ir/irtype.cpp b/ir/irtype.cpp index 1ac9073f..78ebf4b0 100644 --- a/ir/irtype.cpp +++ b/ir/irtype.cpp @@ -207,8 +207,8 @@ llvm::Type * IrTypeSArray::sarray2llvm(Type * t) ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// -IrTypeArray::IrTypeArray(Type * dt) -: IrType(dt, array2llvm(dt)) +IrTypeArray::IrTypeArray(Type* dt, LLType* lt) +: IrType(dt, lt) { } @@ -216,30 +216,23 @@ IrTypeArray::IrTypeArray(Type * dt) IrTypeArray* IrTypeArray::get(Type* dt) { - IrTypeArray* t = new IrTypeArray(dt); - dt->irtype = t; - return t; -} + assert(!dt->irtype); + assert(dt->ty == Tarray && "not dynamic array type"); -////////////////////////////////////////////////////////////////////////////// + LLType* elemType = DtoTypeNotVoid(dt->nextOf()); -llvm::Type * IrTypeArray::array2llvm(Type * t) -{ - assert(t->ty == Tarray && "not dynamic array type"); + // Could have already built the type as part of a struct forward reference, + // just as for pointers. + if (!dt->irtype) + { + llvm::SmallVector types; + types.push_back(DtoSize_t()); + types.push_back(llvm::PointerType::get(elemType, 0)); + LLType* at = llvm::StructType::get(llvm::getGlobalContext(), types/*, t->toChars()*/); + dt->irtype = new IrTypeArray(dt, at); + } - // get .ptr type - LLType* elemType = DtoType(t->nextOf()); - if (elemType == llvm::Type::getVoidTy(llvm::getGlobalContext())) - elemType = llvm::Type::getInt8Ty(llvm::getGlobalContext()); - elemType = llvm::PointerType::get(elemType, 0); - - // create struct type - llvm::SmallVector types; - types.push_back(DtoSize_t()); - types.push_back(elemType); - LLType* at = llvm::StructType::get(llvm::getGlobalContext(), types/*, t->toChars()*/); - - return at; + return dt->irtype->isArray(); } ////////////////////////////////////////////////////////////////////////////// diff --git a/ir/irtype.h b/ir/irtype.h index 21a43d23..a85a760b 100644 --- a/ir/irtype.h +++ b/ir/irtype.h @@ -161,9 +161,7 @@ public: protected: /// - IrTypeArray(Type* dt); - /// - static llvm::Type* array2llvm(Type* t); + IrTypeArray(Type* dt, LLType *lt); }; //////////////////////////////////////////////////////////////////////////////