[LLVMdev] Issues with the llvm.stackrestore intrinsic

Patrik Hägglund patrik.h.hagglund at ericsson.com
Wed Feb 1 03:11:06 PST 2012


Hi,

I have two problems regarding the llvm.stackrestore intrinsic. I'm
running on 3.0, but a quick test on trunk also showed the same behavior.

First problem:
---------------
I have code like:

   tmp1 = call llvm.stacksave()
   tmp2 = alloca
   [do some stuff with tmp2]
   call llvm.stackrestore(tmp1)

   [some other stuff]

   tmp3 = call llvm.stacksave()
   tmp4 = alloca
   [do some stuff with tmp4]
   call llvm.stackrestore(tmp3)

Then some transformation rewrites this to

   tmp1 = call llvm.stacksave()
   tmp2 = alloca
   [do some stuff with tmp2]
   call llvm.stackrestore(tmp1)

   [some other stuff]

   tmp3 = call llvm.stacksave()
   tmp4 = tmp2      <----- Ops!!!
   [do some stuff with tmp4]
   call llvm.stackrestore(tmp3)

Unfortunately the tmp2 pointer isn't valid after the first stackrestore, 
since the memory it's pointing at has in fact been deallocated by the 
intrinsic, so the uses of it through the variable tmp4 are wrong.

Maybe some dependencies between alloca and the stackrestore instrinsic
are missing or how should this work?

In Intrinsics.td it says

// Note: we treat stacksave/stackrestore as writemem because we don't
otherwise
// model their dependencies on allocas.
def int_stacksave     : Intrinsic<[llvm_ptr_ty]>,
                          GCCBuiltin<"__builtin_stack_save">;
def int_stackrestore  : Intrinsic<[], [llvm_ptr_ty]>,
                          GCCBuiltin<"__builtin_stack_restore">;

Does "GCCBuiltin" imply "writemem", or how does the comment and the code 
correspond?


Second problem:
---------------
It seems that calls to stackrestore are removed if they are close to a
ret instruction. I suppose the idea is that the function epilogue will
restore the stack pointer anyway, so the llvm.stackrestore can be safely 
removed.

However, in my target, the stack restoration in the epilogue is
currently done by adding the used stacksize to the stackpointer, so I do 
indeed need the call to the stackrestore intrinsic to be left.

Am I breaking some design rule by reading the stackpointer in the
epilogue or how is this supposed to work?



More information about the llvm-dev mailing list