Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tell LLVM optimizer contents of variables

I'm writing a compiler using LLVM as backend and have a lot of reference counting. When I borrow an object, I increment the object's reference counter. When I release an object, I decrement the reference counter, and free the object if it goes to zero. However, if I only do a small piece of code, like this one:

++obj->ref;
global_variable_A = obj->a;
if (--obj->ref == 0)
    free_object(obj);

LLVM optimizes this to (in IR but this is the equal code in C):

global_variable_A = obj->a;
if (obj->ref == 0)
    free_object(obj);

But since I know that a reference counter is always positive before the first statement, it could be optimized to only

global_variable_A = obj->a;

My question: is there any way to tell the LLVM optimizer that a register or some memory, at a the time of reading it, is known to contain non-zero data?

An other equal question would be if I can tell the optimizer that a pointer is non-null, that would also be great.

like image 896
Emil Avatar asked Nov 14 '22 09:11

Emil


1 Answers

You could write a custom FunctionPass that would replace the variable with a true value, then it should be optimised by DCE or SimplifyCFG. http://llvm.org/docs/WritingAnLLVMPass.html

like image 142
joey Avatar answered Dec 21 '22 19:12

joey