# HG changeset patch # User Frits van Bommel # Date 1241258330 -7200 # Node ID 23b23b74e3264b784fa0456d2d4ec8fc7530fd05 # Parent 91d9386d4a5ab1804d756e510b488cf47b1d30e2 Remove calls to some runtime functions if their results are unused diff -r 91d9386d4a5a -r 23b23b74e326 gen/passes/SimplifyDRuntimeCalls.cpp --- a/gen/passes/SimplifyDRuntimeCalls.cpp Sat May 02 11:58:50 2009 +0200 +++ b/gen/passes/SimplifyDRuntimeCalls.cpp Sat May 02 11:58:50 2009 +0200 @@ -145,6 +145,15 @@ } }; +/// DeleteUnusedOpt - remove libcall if the return value is unused. +struct VISIBILITY_HIDDEN DeleteUnusedOpt : public LibCallOptimization { + virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { + if (CI->use_empty()) + return CI; + return 0; + } +}; + // TODO: More optimizations! :) } // end anonymous namespace. @@ -163,6 +172,9 @@ ArraySetLengthOpt ArraySetLength; ArrayCastLenOpt ArrayCastLen; + // GC allocations + DeleteUnusedOpt DeleteUnused; + public: static char ID; // Pass identification SimplifyDRuntimeCalls() : FunctionPass(&ID) {} @@ -188,9 +200,28 @@ /// Optimizations - Populate the Optimizations map with all the optimizations /// we know. void SimplifyDRuntimeCalls::InitOptimizations() { + // Some array-related optimizations Optimizations["_d_arraysetlengthT"] = &ArraySetLength; Optimizations["_d_arraysetlengthiT"] = &ArraySetLength; Optimizations["_d_array_cast_len"] = &ArrayCastLen; + + /* Delete calls to runtime functions which aren't needed if their result is + * unused. That comes down to functions that don't do anything but + * GC-allocate and initialize some memory. + * We don't need to do this for functions which are marked 'readnone' or + * 'readonly', since LLVM doesn't need our help figuring out when those can + * be deleted. + * (We can't mark allocating calls as readonly/readnone because they don't + * return the same pointer every time when called with the same arguments) + */ + Optimizations["_d_allocmemoryT"] = &DeleteUnused; + Optimizations["_d_newarrayT"] = &DeleteUnused; + Optimizations["_d_newarrayiT"] = &DeleteUnused; + Optimizations["_d_newarrayvT"] = &DeleteUnused; + Optimizations["_d_newarraymT"] = &DeleteUnused; + Optimizations["_d_newarraymiT"] = &DeleteUnused; + Optimizations["_d_newarraymvT"] = &DeleteUnused; + Optimizations["_d_allocclass"] = &DeleteUnused; }