Query on optimizing away function calls.

Hi all.

Consider the following code:

Hi,

Consider the following code:

------------
define void @func() {
   %a = alloca i32
   store i32 42, i32* %a
   call void @func2(i32* %a) nounwind
   ret void
}

define void @func2(i32* %a) nounwind {
   store i32 43, i32* %a
   ret void
}
------------

It is possible to optimize this to:

------------
define void @func() {
       ret void
}

define void @func2(i32* %a) nounwind {
       store i32 43, i32* %a
       ret void
}
------------

which is just what I want. However, if @func2 is implemented in an external C
library, is this not possible? Specifically, while optimizing:

------------
define void @func() {
   %a = alloca i32
   store i32 42, i32* %a
   call void @func2(i32* %a) nounwind
   ret void
}

declare void @func2(i32* %a) nounwind
------------

is there some way to specify that @func2 only modifies values accessible via %a,
so that if all those values are ultimately discarded then the call to @func2
itself can be discarded?

no, there is currently no way. There was some discussion of adding
attributes for this, but I think the conclusion was that there were
too many hairy details for not enough gain.

The "readonly" function attribute looks to be too strict for this.

It is possible to put "readonly" on the call, rather than on func2
itself. So if your front-end somehow knows that this use of func2
has no real effect it could mark the call this way.

Or am I missing something obvious?

Nope.

Ciao,

Duncan.

Duncan Sands wrote:

The "readonly" function attribute looks to be too strict for this.

It is possible to put "readonly" on the call, rather than on func2
itself. So if your front-end somehow knows that this use of func2
has no real effect it could mark the call this way.

Unfortunately, it can only know that if it knows that %a won't be read from after the call...