[LLVMdev] Void vs int

Bill Wendling wendling at apple.com
Fri Jan 1 04:18:34 PST 2010


On Dec 30, 2009, at 11:37 PM, Jon Harrop wrote:

> Is it more efficient to return void rather than the int 0, e.g. does it reduce 
> register pressure?
> 
If you don't use the value after the call, then it will produce the same LLVM code for both functions from llvm-gcc and clang:

$ cat t.c
int foo();

void bar() {
  foo();
}

void baz();

void qux() {
  baz();
}

$ llvm-gcc -o - -S t.c -mllvm -disable-llvm-optzns -emit-llvm

define void @bar() nounwind ssp {
entry:
  %0 = call i32 (...)* @foo() nounwind            ; <i32> [#uses=0]
  br label %return

return:                                           ; preds = %entry
  ret void
}

declare i32 @foo(...)

define void @qux() nounwind ssp {
entry:
  call void (...)* @baz() nounwind
  br label %return

return:                                           ; preds = %entry
  ret void
}

$ clang -o - -S t.c -emit-llvm

define void @bar() nounwind ssp {
entry:
  %call = call i32 (...)* @foo()                  ; <i32> [#uses=0]
  ret void
}

declare i32 @foo(...)

define void @qux() nounwind ssp {
entry:
  call void (...)* @baz()
  ret void
}

So there should be no advantage in this situation. However, if you use the "int 0" value, then it will produce different code for the two functions. And then the register allocator will have to get involved.

Caveat: This was tested on a Mac.

-bw





More information about the llvm-dev mailing list