Book Image

LLVM Cookbook

Book Image

LLVM Cookbook

Overview of this book

Table of Contents (16 chapters)
LLVM Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Sibling call optimisation


In this recipe, we will see how sibling call optimization works in LLVM. Sibling call optimization can be looked at as an optimized tail call, the only constraint being that the functions should share a similar function signature, that is, matching return types and matching function arguments.

Getting ready

Write a test case for sibling call optimization, making sure that the caller and callee have the same calling conventions (in either C or fastcc), and that the call in the tail position is a tail call:

$ cat sibcall.ll
declare i32 @bar(i32, i32)

define i32 @foo(i32 %a, i32 %b, i32 %c) {
  entry:
    %0 = tail call i32 @bar(i32 %a, i32 %b)
  ret i32 %0
}

How to do it…

  1. Run the llc tool to generate the assembly:

    $ llc sibcall.ll
    
  2. View the generated assembly using the cat command:

    $ cat sibcall.s
        .text
        .file    "sibcall.ll"
        .globl    foo
        .align    16, 0x90
        .type    foo,@function
    foo:                                    # @foo
        .cfi_startproc
    ...