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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With