Mercurial > projects > ldc
diff gen/passes/GarbageCollect2Stack.cpp @ 1317:4099548c80e0
Allocate objects on the stack if they (a) don't have a destructor, and
(b) don't override the delete operator (on top of the regular conditions for
stack allocation that also apply to arrays, structs, etc.).
The "no destructor" clause is not strictly necessary, but calling them at the
right time would be tricky to say the least; it would involve, among other
things, "manually" inserting a try-finally block around anything that might
throw exceptions not caught in the current function.
Note: objects with custom new operators are automatically ignored because they
don't use the regular allocation runtime call, so there's no need to pay special
attention to them.
author | Frits van Bommel <fvbommel wxs.nl> |
---|---|
date | Sat, 09 May 2009 00:50:15 +0200 |
parents | 8c65217be813 |
children | c21a6654cce2 |
line wrap: on
line diff
--- a/gen/passes/GarbageCollect2Stack.cpp Fri May 08 16:00:44 2009 +0200 +++ b/gen/passes/GarbageCollect2Stack.cpp Sat May 09 00:50:15 2009 +0200 @@ -180,6 +180,53 @@ return alloca; } }; + + // FunctionInfo for _d_allocclass + class AllocClassFI : public FunctionInfo { + public: + virtual bool analyze(CallSite CS, const Analysis& A) { + // This call contains no TypeInfo parameter, so don't call the + // base class implementation here... + if (CS.arg_size() != 1) + return false; + Value* arg = CS.getArgument(0)->stripPointerCasts(); + GlobalVariable* ClassInfo = dyn_cast<GlobalVariable>(arg); + if (!ClassInfo) + return false; + + std::string metaname = CD_PREFIX; + metaname.append(ClassInfo->getNameStart(), ClassInfo->getNameEnd()); + + GlobalVariable* global = A.M.getGlobalVariable(metaname); + if (!global || !global->hasInitializer()) + return false; + + MDNode* node = dyn_cast<MDNode>(global->getInitializer()); + if (!node || node->getNumOperands() != CD_NumFields) + return false; + + // Inserting destructor calls is not implemented yet, so classes + // with destructors are ignored for now. + Constant* hasDestructor = dyn_cast<Constant>(node->getOperand(CD_Finalize)); + // We can't stack-allocate if the class has a custom deallocator + // (Custom allocators don't get turned into this runtime call, so + // those can be ignored) + Constant* hasCustomDelete = dyn_cast<Constant>(node->getOperand(CD_CustomDelete)); + if (hasDestructor == NULL || hasCustomDelete == NULL) + return false; + + if (ConstantExpr::getOr(hasDestructor, hasCustomDelete) + != ConstantInt::getFalse()) + return false; + + Ty = node->getOperand(CD_BodyType)->getType(); + return true; + } + + // The default promote() should be fine. + + AllocClassFI() : FunctionInfo(-1, true) {} + }; } @@ -197,6 +244,7 @@ FunctionInfo AllocMemoryT; ArrayFI NewArrayVT; ArrayFI NewArrayT; + AllocClassFI AllocClass; public: static char ID; // Pass identification @@ -233,6 +281,7 @@ KnownFunctions["_d_allocmemoryT"] = &AllocMemoryT; KnownFunctions["_d_newarrayvT"] = &NewArrayVT; KnownFunctions["_d_newarrayT"] = &NewArrayT; + KnownFunctions["_d_allocclass"] = &AllocClass; } static void RemoveCall(Instruction* Inst) {