# Address of Label and Indirect Branches in LLVM IR

DevFeed: [Address of Label and Indirect Branches in LLVM IR](<https://devfeed.tech/articles/address-of-label-and-indirect-branches-in-llvm-ir-42841.md>)

Original publisher: [Read original article](<https://blog.llvm.org/2010/01/address-of-label-and-indirect-branches.html>)

Author: Chris Lattner

Published: 2010-01-03T00:11:00Z

Content type: article

Language: en

Sources: [LLVM Project](<https://devfeed.tech/sources/the-llvm-project-blog.md>)

Topics: [LLVM](<https://devfeed.tech/topics/llvm.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>), [gcc](<https://devfeed.tech/topics/gcc.md>), [C](<https://devfeed.tech/topics/c.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [c](<https://devfeed.tech/tags/c.md>), [code](<https://devfeed.tech/tags/code.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [gcc](<https://devfeed.tech/tags/gcc.md>), [llvm](<https://devfeed.tech/tags/llvm.md>), [new-in-llvm-2-7](<https://devfeed.tech/tags/new-in-llvm-2-7.md>), [optimization](<https://devfeed.tech/tags/optimization.md>)

## AI overview

This post explains LLVM IR support introduced in LLVM 2.7 for taking the address of a label and jumping to it later. It compares the feature with GCC's "Label as Values" extension and describes how earlier LLVM versions lowered indirect gotos using integer IDs and switch instructions.

## Source excerpt

The GCC Compiler supports a useful "Label as Values" extension, which allows code to take the address of a label and then later do an unconditional branch to an address specified as a void*. This extension is particularly useful for building efficient interpreters. LLVM has long supported this extension by lowering it to a "correct" but extremely inefficient form. New in LLVM 2.7 is IR support for taking the address of a label and jumping to it later, which allows implementing this extension much more efficiently. This post describes this new LLVM IR feature and how it works. In this discussion, I limit the scope to only considering "local" jumps. We don't talk about non-local jumps in the GCC extension sense: jumps from a nested function to an outer one. The Address of Label Extension Before I dive into the new feature, I'll describe the GCC C extension along with how LLVM 2.6 and earlier used to compile it. Consider this code (from PR3120): static int fn(Opcodes opcodes) { static const void *codetable[] = { &&RETURN, &&INCREMENT, &&DECREMENT, &&DOUBLE, &&SWAPWORD }; int result = 0; goto *codetable[*(opcodes++)]; RETURN: return result; INCREMENT: result++; goto *codetable[*(opcodes++)]; DECREMENT: result--; goto *codetable[*(opcodes++)]; DOUBLE: result <<= 1; goto *codetable[*(opcodes++)]; SWAPWORD: result = (result << 16) | (result >> 16); goto *codetable[*(opcodes++)]; } As you can see, the code initializes the 'codetable' array with the addresses of 5 labels, and later jumps through a computed pointer. An interesting aspect about this extension is that the only thing you are allowed to do with an address taken label is to jump to it. While it is widely misused for other things (e.g. the BUG macro in the linux kernel, which prints the address of a label on error), you are really really only allowed jump to it. You are not allowed to use it in an inline asm, inspect the value, or do anything else. LLVM 2.6 and Earlier In LLVM 2.6 and earlier, LLVM took full advant