Hi, I got a question about optimizing function pointers for direct function calls in C.
Consider the following scenario: one of the fields of a structure is a function pointer, and all its assignments come from the same function. Can all its uses be replaced by direct calls to this function? So the later passes can do more optimizations. Here is the example: int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } struct Foo { int (*add)(int, int); }; int main() { struct Foo foo[5] = malloc(sizeof(struct Foo) * 5); for (int i = 0; i < 5; i++) { foo[i].add = add; } int sum = 0; for (int i = 0; i < 5; i++) { sum += foo[i].add(1, 2); } return 0; } Can I replace the code above to the code below? int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } struct Foo { int (*add)(int, int); }; int main() { struct Foo foo[5] = malloc(sizeof(struct Foo) * 5); for (int i = 0; i < 5; i++) { foo[i].add = add; } int sum = 0; for (int i = 0; i < 5; i++) { sum += add(1,2); } return 0; } My idea is to determine whether the assignment of the field is the same function, and if so, perform the replacement. Of course this is not a reasonable optimization, I just want to know if there are security issues in doing so, and if I want to do it in the IPA stage, is it possible? Thanks Hanke Zhang