LLVM label function as const and removing duplicate calls

I have an external (C) function that I call in my LLVM IR. IR gets JITed and everything works fine, but the generated code is performance sensitive, and if possible, I want to remove duplicate calls to my external function. The function has no side effects. Is there a FunctionPass that eliminates redundant function calls? Is there something I have to do to mark the function as the absence of side effects?

Thank!

+5
source share
1 answer

According to http://llvm.org/docs/LangRef.html#function-attributes you can specify readonly or readnone attributes for a function:

declare i32 @fn(i32 %i);
declare i32 @readonly_fn(i32 %i) readonly;
declare i32 @readnone_fn(i32 %i) readnone;

readonly , , readnone , (, sin() )

, , ( ). readnone .

llvm readonly readnone EarlyCSE pass ( ), :

define i32 @test_no_readonly()
{
  %1 = call i32 @fn(i32 0)
  %2 = call i32 @fn(i32 0)
  %add = add i32 %1, %2
  ret i32 %add
}
define i32 @test_readonly()
{
  %1 = call i32 @readonly_fn(i32 0)
  %2 = call i32 @readonly_fn(i32 0)
  %add = add i32 %1, %2
  ret i32 %add
}
define i32 @test_readnone()
{
  %1 = call i32 @readnone_fn(i32 0)
  %2 = call i32 @readnone_fn(i32 0)
  %add = add i32 %1, %2
  ret i32 %add
}

opt -early-cse -S readonly_fn.ll > readonly_fn_opt.ll readonly readnone,

define i32 @test_no_readonly() {
  %1 = call i32 @fn(i32 0)
  %2 = call i32 @fn(i32 0)
  %add = add i32 %1, %2
  ret i32 %add
}

define i32 @test_readonly() {
  %1 = call i32 @readonly_fn(i32 0)
  %add = add i32 %1, %1
  ret i32 %add
}

define i32 @test_readnone() {
  %1 = call i32 @readnone_fn(i32 0)
  %add = add i32 %1, %1
  ret i32 %add
}

readonly_fn readnone_fn , , redundand calls.

-functionattrs

+1

All Articles