llvm - delete a function from a file

1.6k Views Asked by At

I have a file in which the main function calls three test functions, namely, test 1, test 2 and test 3. I want to make a pass that deletes each two of the functions one by one and returns the file that's calling only one of the three test functions. Right now, my pass is a ModulePass with this body:

 for(Module::iterator F= M.begin(), E = M.end(); F != E; ++F) // iterating over functions in a module
    {
            std::string function_name = F->getName();   
            if((std::find(calledFunctions.begin(), calledFunctions.end(), function_name) != calledFunctions.end())
                    && function_name != current_function ) 
            {
                F->replaceAllUsesWith(UndefValue::get(F->getType()));
                F->removeFromParent(); 
            }
    }

where called functions contain the functions you want gone (say, test 2 and test 3) and current_function is the one you want in the main function (test 1 in this case)

Can someone please tell me if my approach is right? As of now, I'm getting a segmentation fault.

1

There are 1 best solutions below

0
Zhang HanSheng On

You need to delete the invoke/call Instructions referencing your deleted function instead of replacing them with UndefinedValue. This could be achieved from iterating the function's Def-Use Chain and delete Values accordingly