[LLVMdev] Does nounwind have semantics?

Duncan Sands baldrick at free.fr
Sun Jul 21 23:55:44 PDT 2013

Hi Andrew,

On 22/07/13 02:56, Andrew Trick wrote:
> Does 'nounwind' have semantics that inform optimization passes? It seems to in some cases, but not consistently. For example...
> int32_t foo(int32_t* ptr) {
>    int i = 0;
>    int result;
>    do {
>      bar(ptr);
>      result = *ptr;
>      bar(ptr);
>    } while (i++ < *ptr);
>    return result;
> }
> Say we have a front end that declares bar as...
> declare void @bar(i32*) readonly;
> So 'bar' is 'readonly' and 'may-unwind'.
> When LICM tries to hoist the load it interprets the 'may-unwind' as "MayThrow" in LICM-language and bails. However, when it tries to sink the call itself it sees the 'readonly', assumes no side effects and sinks it below the loads. Hmm...

is your worry here about the following case?
  - the load will trap if executed
  - bar throws an exception
Thus with the original code the trap will not occur, because an exception will
be thrown first, while if you move the first bar call below the load then the
tap will occur.

> There doesn't appear to be a way to declare a function that is guaranteed not to write to memory in a way that affects the caller, but may have another well-defined side effect like aborting the program.

I'm pretty sure that exiting the program is considered to write memory, so bar
can't do that itself.

Ciao, Duncan.

  This is interesting, because that is the way runtime checks for safe languages 
would like to be defined. I'm perfectly happy telling front ends to generate 
control flow for well-defined traps, since I like lots of basic blocks in my IR. 
But I'm still curious how others deal with this.
> -Andy
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

More information about the llvm-dev mailing list