comparison gen/arrays.cpp @ 1514:7a528017b4c6

Don't initialize arrays of (arrays of...) void-initialized typedefs.
author Frits van Bommel <fvbommel wxs.nl>
date Wed, 24 Jun 2009 17:14:50 +0200
parents 0c8d6d345001
children 391fdb36f2aa
comparison
equal deleted inserted replaced
1513:8a5570ddad25 1514:7a528017b4c6
396 LLConstant* values[2] = { dim, ptr }; 396 LLConstant* values[2] = { dim, ptr };
397 return llvm::ConstantStruct::get(values, 2); 397 return llvm::ConstantStruct::get(values, 2);
398 } 398 }
399 399
400 ////////////////////////////////////////////////////////////////////////////////////////// 400 //////////////////////////////////////////////////////////////////////////////////////////
401 static bool isInitialized(Type* et) {
402 // Strip array types from element type
403 Type* bt = et->toBasetype();
404 while (bt->ty == Tsarray || bt->ty == Tarray) {
405 et = bt->nextOf();
406 bt = et->toBasetype();
407 }
408 // If it's a typedef with "= void" initializer then don't initialize.
409 if (et->ty == Ttypedef) {
410 Logger::println("Typedef: %s", et->toChars());
411 TypedefDeclaration* tdd = ((TypeTypedef*)et)->sym;
412 if (tdd && tdd->init && tdd->init->isVoidInitializer())
413 return false;
414 }
415 // Otherwise, it's always initialized.
416 return true;
417 }
418
419 //////////////////////////////////////////////////////////////////////////////////////////
401 DSliceValue* DtoNewDynArray(Loc& loc, Type* arrayType, DValue* dim, bool defaultInit) 420 DSliceValue* DtoNewDynArray(Loc& loc, Type* arrayType, DValue* dim, bool defaultInit)
402 { 421 {
403 Logger::println("DtoNewDynArray : %s", arrayType->toChars()); 422 Logger::println("DtoNewDynArray : %s", arrayType->toChars());
404 LOG_SCOPE; 423 LOG_SCOPE;
405 424
409 // dim arg 428 // dim arg
410 assert(DtoType(dim->getType()) == DtoSize_t()); 429 assert(DtoType(dim->getType()) == DtoSize_t());
411 LLValue* arrayLen = dim->getRVal(); 430 LLValue* arrayLen = dim->getRVal();
412 431
413 // get runtime function 432 // get runtime function
414 bool zeroInit = arrayType->toBasetype()->nextOf()->isZeroInit(); 433 Type* eltType = arrayType->toBasetype()->nextOf();
434 if (defaultInit && !isInitialized(eltType))
435 defaultInit = false;
436 bool zeroInit = eltType->isZeroInit();
415 const char* fnname = defaultInit ? (zeroInit ? "_d_newarrayT" : "_d_newarrayiT") : "_d_newarrayvT"; 437 const char* fnname = defaultInit ? (zeroInit ? "_d_newarrayT" : "_d_newarrayiT") : "_d_newarrayvT";
416 LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, fnname); 438 LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, fnname);
417 439
418 // call allocator 440 // call allocator
419 LLValue* newptr = gIR->CreateCallOrInvoke2(fn, arrayTypeInfo, arrayLen, ".gc_mem").getInstruction(); 441 LLValue* newptr = gIR->CreateCallOrInvoke2(fn, arrayTypeInfo, arrayLen, ".gc_mem").getInstruction();
443 for (size_t i=0; i<ndims; ++i) 465 for (size_t i=0; i<ndims; ++i)
444 vtype = vtype->nextOf(); 466 vtype = vtype->nextOf();
445 467
446 // get runtime function 468 // get runtime function
447 bool zeroInit = vtype->isZeroInit(); 469 bool zeroInit = vtype->isZeroInit();
470 if (defaultInit && !isInitialized(vtype))
471 defaultInit = false;
448 const char* fnname = defaultInit ? (zeroInit ? "_d_newarraymT" : "_d_newarraymiT") : "_d_newarraymvT"; 472 const char* fnname = defaultInit ? (zeroInit ? "_d_newarraymT" : "_d_newarraymiT") : "_d_newarraymvT";
449 LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, fnname); 473 LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, fnname);
450 474
451 // build dims 475 // build dims
452 LLValue* dimsArg = DtoArrayAlloca(Type::tsize_t, ndims, ".newdims"); 476 LLValue* dimsArg = DtoArrayAlloca(Type::tsize_t, ndims, ".newdims");