[LLVMdev] More detailed example...

Nick Lewycky nicholas at mxc.ca
Fri Jan 5 16:57:07 PST 2007


Sarah Thompson wrote:
> Further to that, I thought an example might be useful. In the following 
> code, n should end up with a value that varies nondeterministically with 
> the scheduling decisions made by the underlying run time system -- my 
> model checker, for example, should in theory be able to enumerate all 
> possible values. Anyway, if you look at the compiler output (see below), 
> the volatile global variable, n, has vanished, with the printf output 
> only ever taking the (constant) result 0.

How are you compiling this? I get the following sort of output:

void %inc(int* %p) {
entry:
        %tmp = volatile load int* %p            ; <int> [#uses=1]
        %tmp1 = add int %tmp, 1         ; <int> [#uses=1]
        volatile store int %tmp1, int* %p
        tail call void (...)* %mcp_yield( )
        %tmp.1 = volatile load int* %p          ; <int> [#uses=1]
        %tmp1.1 = add int %tmp.1, 1             ; <int> [#uses=1]
        volatile store int %tmp1.1, int* %p
        tail call void (...)* %mcp_yield( )
        %tmp.2 = volatile load int* %p          ; <int> [#uses=1]
        %tmp1.2 = add int %tmp.2, 1             ; <int> [#uses=1]
        volatile store int %tmp1.2, int* %p
        tail call void (...)* %mcp_yield( )
        ret void
}

I ran this with "llvm-gcc -c mcp.c -o mcp.bc". In the example you gave,
 it is valid for LLVM to optimize away the volatile memory accesses if
it were doing link-time optimizations; it could prove that "int n"'s
value never changed the observable behaviour of the program and elided it.

Nick Lewycky



More information about the llvm-dev mailing list