@@ -236,10 +236,13 @@ class AllocToken {
236236 bool instrumentFunction (Function &F);
237237
238238private:
239- // / Returns true for !isAllocationFn() functions that are also eligible for
240- // / instrumentation.
241- static bool isInstrumentableLibFunc (LibFunc Func, const Value *V,
242- const TargetLibraryInfo *TLI);
239+ // / Returns the LibFunc (or NotLibFunc) if this call should be instrumented.
240+ std::optional<LibFunc>
241+ shouldInstrumentCall (const CallBase &CB, const TargetLibraryInfo &TLI) const ;
242+
243+ // / Returns true for functions that are eligible for instrumentation.
244+ static bool isInstrumentableLibFunc (LibFunc Func, const CallBase &CB,
245+ const TargetLibraryInfo &TLI);
243246
244247 // / Returns true for isAllocationFn() functions that we should ignore.
245248 static bool ignoreInstrumentableLibFunc (LibFunc Func);
@@ -291,21 +294,8 @@ bool AllocToken::instrumentFunction(Function &F) {
291294 auto *CB = dyn_cast<CallBase>(&I);
292295 if (!CB)
293296 continue ;
294- const Function *Callee = CB->getCalledFunction ();
295- if (!Callee)
296- continue ;
297- // Ignore nobuiltin of the CallBase, so that we can cover nobuiltin libcalls
298- // if requested via isInstrumentableLibFunc(). Note that isAllocationFn() is
299- // returning false for nobuiltin calls.
300- LibFunc Func;
301- if (TLI.getLibFunc (*Callee, Func)) {
302- if (ignoreInstrumentableLibFunc (Func))
303- continue ;
304- if (isInstrumentableLibFunc (Func, CB, &TLI))
305- AllocCalls.emplace_back (CB, Func);
306- } else if (Options.Extended && getAllocTokenMetadata (*CB)) {
307- AllocCalls.emplace_back (CB, NotLibFunc);
308- }
297+ if (std::optional<LibFunc> Func = shouldInstrumentCall (*CB, TLI))
298+ AllocCalls.emplace_back (CB, Func.value ());
309299 }
310300
311301 bool Modified = false ;
@@ -317,18 +307,46 @@ bool AllocToken::instrumentFunction(Function &F) {
317307 return Modified;
318308}
319309
320- bool AllocToken::isInstrumentableLibFunc (LibFunc Func, const Value *V,
321- const TargetLibraryInfo *TLI) {
322- if (isAllocationFn (V, TLI))
310+ std::optional<LibFunc>
311+ AllocToken::shouldInstrumentCall (const CallBase &CB,
312+ const TargetLibraryInfo &TLI) const {
313+ const Function *Callee = CB.getCalledFunction ();
314+ if (!Callee)
315+ return std::nullopt ;
316+
317+ // Ignore nobuiltin of the CallBase, so that we can cover nobuiltin libcalls
318+ // if requested via isInstrumentableLibFunc(). Note that isAllocationFn() is
319+ // returning false for nobuiltin calls.
320+ LibFunc Func;
321+ if (TLI.getLibFunc (*Callee, Func)) {
322+ if (isInstrumentableLibFunc (Func, CB, TLI))
323+ return Func;
324+ } else if (Options.Extended && getAllocTokenMetadata (CB)) {
325+ return NotLibFunc;
326+ }
327+
328+ return std::nullopt ;
329+ }
330+
331+ bool AllocToken::isInstrumentableLibFunc (LibFunc Func, const CallBase &CB,
332+ const TargetLibraryInfo &TLI) {
333+ if (ignoreInstrumentableLibFunc (Func))
334+ return false ;
335+
336+ if (isAllocationFn (&CB, &TLI))
323337 return true ;
324338
325339 switch (Func) {
340+ // These libfuncs don't return normal pointers, and are therefore not handled
341+ // by isAllocationFn().
326342 case LibFunc_posix_memalign:
327343 case LibFunc_size_returning_new:
328344 case LibFunc_size_returning_new_hot_cold:
329345 case LibFunc_size_returning_new_aligned:
330346 case LibFunc_size_returning_new_aligned_hot_cold:
331347 return true ;
348+
349+ // See comment above ClCoverReplaceableNew.
332350 case LibFunc_Znwj:
333351 case LibFunc_ZnwjRKSt9nothrow_t:
334352 case LibFunc_ZnwjSt11align_val_t:
@@ -354,6 +372,7 @@ bool AllocToken::isInstrumentableLibFunc(LibFunc Func, const Value *V,
354372 case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t:
355373 case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t:
356374 return ClCoverReplaceableNew;
375+
357376 default :
358377 return false ;
359378 }
@@ -390,7 +409,7 @@ bool AllocToken::replaceAllocationCall(CallBase *CB, LibFunc Func,
390409 IRBuilder<> IRB (CB);
391410 // Original args.
392411 SmallVector<Value *, 4 > NewArgs{CB->args ()};
393- // Add token ID.
412+ // Add token ID, truncated to IntPtrTy width .
394413 NewArgs.push_back (ConstantInt::get (IntPtrTy, TokenID));
395414 assert (TokenAlloc.getFunctionType ()->getNumParams () == NewArgs.size ());
396415
@@ -420,7 +439,7 @@ FunctionCallee AllocToken::getTokenAllocFunction(const CallBase &CB,
420439 if (OriginalFunc != NotLibFunc) {
421440 Key = std::make_pair (OriginalFunc, Options.FastABI ? TokenID : 0 );
422441 auto It = TokenAllocFunctions.find (*Key);
423- if (LLVM_LIKELY ( It != TokenAllocFunctions.end () ))
442+ if (It != TokenAllocFunctions.end ())
424443 return It->second ;
425444 }
426445
@@ -459,7 +478,7 @@ PreservedAnalyses AllocTokenPass::run(Module &M, ModuleAnalysisManager &MAM) {
459478 bool Modified = false ;
460479
461480 for (Function &F : M) {
462- if (LLVM_LIKELY ( F.empty () ))
481+ if (F.empty ())
463482 continue ; // declaration
464483 Modified |= Pass.instrumentFunction (F);
465484 }
0 commit comments