[LLVMdev] Redefining function

Duncan Sands baldrick at free.fr
Sat Jan 30 05:54:23 PST 2010


Hi Conrado,

> I couldn't find the solution to my problem (if it has one) in the 
> mailing list or the source code. The problem is: how can I redefine a 
> function that's been called already by some other function?

why do you want to do this?

> Suppose I have 3 files, all compiled to bytecode through llvm-gcc (I 
> think it could be clang instead).
> 
> File1.c:
> void do_print() { print(); }
> 
> File2.c:
> void print() { printf("File2.c\n"); }
> 
> File3.c:
> void print() { printf("File3.c\n"); }

The solution in C is to give the version in File2 a weak linkage type,
for example using gcc's "weak" attribute.  You then link all three files
together, and the weak print in File2 will be magically replaced with the
non-weak print in File3.

>   //swap the definition of the function "print" from the one in File2.c 
> to File3.c
>   swap (file1, file2, file3);

If all the functions are in the same module, then you can use
FunctionA->replaceAllUsesWith(FunctionB) if they have the same
type.

Ciao,

Duncan.



More information about the llvm-dev mailing list