Respect TargetLibraryInfo CustomNames in getAllocationData

Shea Levy shea at shealevy.com
Sat Feb 8 19:55:38 PST 2014


Hi all,

Currently, getAllocationData in MemoryBuiltins.cpp (a helper used for
functions like isAllocLikeFn) uses TargetLibraryInfo::getLibFunc on the
passed in function name to determine if it corresponds to a
LibFunc::Func. Unfortunately, getLibFunc looks up Funcs by StandardName
(e.g. "malloc" -> LibFunc::Func::malloc) and doesn't respect any
overridden names set by TargetLibraryInfo::setAvailableWithName. This is
perhaps a bug in getLibFunc (if it isn't, getLibFunc should probably be
static), but since existing code may depend on this behavior the
attached patch instead modifies getAllocationData to compare the name
associated with each of the Funcs in AllocationFnData with the passed-in
function names, and if it matches (which will by the way never be true
if the Func is not enabled for that TLI) then that is the Func used for
the rest of the function.

My test case for this was compiling some IR that had a superfluous call
to GC_malloc (return value unused) using a PassManagerBuilder at -O3. By
default, the call wasn't optimized away (as expected), but when I set
the builder's LibraryInfo to one with Func::malloc set to "GC_malloc",
it was optimized away only with this patch applied.

Cheers,
Shea Levy

P.S. I am not currently subscribed to the list, so please CC me in
replies (though I'll check the list archives periodically for replies as
well)
-------------- next part --------------
Index: lib/Analysis/MemoryBuiltins.cpp
===================================================================
--- lib/Analysis/MemoryBuiltins.cpp	(revision 201032)
+++ lib/Analysis/MemoryBuiltins.cpp	(working copy)
@@ -100,16 +100,13 @@
   if (!Callee)
     return 0;
 
-  // Make sure that the function is available.
+  if (!TLI)
+    return 0;
   StringRef FnName = Callee->getName();
-  LibFunc::Func TLIFn;
-  if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
-    return 0;
-
   unsigned i = 0;
   bool found = false;
   for ( ; i < array_lengthof(AllocationFnData); ++i) {
-    if (AllocationFnData[i].Func == TLIFn) {
+    if (TLI->getName(AllocationFnData[i].Func) == FnName) {
       found = true;
       break;
     }


More information about the llvm-commits mailing list