Reputation: 347
I am trying to write a simple LLVM pass with the following goal:
call
instructions.As an example, consider that I have the following sample program:
#include <stdio.h>
#include <stdlib.h>
int times_two(int val);
int main(int argc, char** argv) {
int arg_1 = atoi(argv[1]);
char test_str[] = "Test String";
int res = times_two(arg_1);
printf("%d", res);
return 0;
}
int times_two(int val) {
// ----> INSERT SOME EXTERNAL FUNCTION HERE <----
return val * 2;
}
Here is part of my LLVM instrumentation:
/* Begin instrumentation
--------------------- */
for (auto &F : M)
{
for (auto &B : F)
{
for (auto &I : B)
{
IRBuilder<> builder(&I);
// Now we have reached a 'call' instruction
if (auto *CallInstr = dyn_cast<CallInst>(&I))
{
// Cast into Function pointer
Function *called_func = CallInstr->getCalledFunction();
errs() << called_func->getName() << "\n";
// loop into the function, getFirstInstruction, do stuff and break.
for (auto &bb_in_func : *called_func)
{
// Set an Insert point at the first
// line of the external function
BasicBlock::iterator insert_point = bb_in_func.getFirstInsertionPt();
builder.SetInsertPoint(&bb_in_func, insert_point);
// Make an external call
builder.CreateCall(extern1);
break;
}
}
}
}
}
However, as I loop through all the functions in a Module, this list of functions also seems to comprise of the built-in functions. In the above case, I get the following:
atoi
llvm.memcpy.p0i8.p0i8.i64
times_two
printf
How do I ignore these built-in functions and only consider times_two?
Upvotes: 1
Views: 1304
Reputation: 347
I think I have figured it out. We want to use getLibFunc()
. This example does something very similar.
In my case, I had to update the LLVM instrumentation as follows:
/* Include this:
#include "llvm/Analysis/TargetLibraryInfo.h"
#include <bits/stdc++.h>
*/
bool Example::runOnModule(Module &M)
{
const TargetLibraryInfo *TLI;
LibFunc inbuilt_func;
std::set<StringRef> builtins;
/* Gather all built-in functions
--------------------- */
for (auto &F : M)
{
if (TLI->getLibFunc(F, inbuilt_func))
builtins.insert(F.getFunction().getName());
}
/* Begin instrumentation
--------------------- */
for (auto &F : M)
{
for (auto &B : F)
{
for (auto &I : B)
{
IRBuilder<> builder(&I);
// Now we have reached a 'call' instruction
if (auto *CallInstr = dyn_cast<CallInst>(&I))
{
// Cast into Function pointer
Function *called_func = CallInstr->getCalledFunction();
StringRef func_name = called_func->getName();
// This line checks to see if the function is not a builtin-function
if (builtins.count(func_name)==0)
{
// loop into the function, getFirstInstruction, do stuff and break.
for (auto &bb_in_func : *called_func)
{
// Set an Insert point at the first
// line of the external function
BasicBlock::iterator insert_point = bb_in_func.getFirstInsertionPt();
builder.SetInsertPoint(&bb_in_func, insert_point);
// Make an external call
builder.CreateCall(extern1);
break;
}
}
}
}
}
}
}
Upvotes: 3