Indirect branching to BasicBlock

Hello,

I'm currently writing a function pass for LLVM and face the following
problem. I want to store the address of a BasicBlock (or the label, as
LLVM refers to it) in a local variable (AllocaInst). Later on I intend
to indirectly branch to this address by "dereferencing" the variable.

Is this possible? I tried creating an instance of AllocaInst but I'm not
quite sure which type to use. Using block->getType() results in a
segmentation fault (Cannot allocate unsized type %myVar = alloca label).

Code might help:

AllocaInst *myVar = new AllocaInst(blockToStore.getType(), "myVar",
curBlock);
new StoreInst(&blockToStore, myVar);

I guess I should use an integer type to store the address but then LLVM
complains about type mismatch as I don not know how to cast the block's
label to an integer. I am not sure where to look this up, might be I
missed an important part in the docs.

Kind regards

Check out the generated code for:

Check out the generated code for:

--
#include <stdio.h>

int main(int argc, char **argv) {
  static void *array[] = { &&foo, &&bar };

  goto *array[argc % 2];

foo:
  puts("foo");
  return 0;

bar:
  puts("bar");
  return 0;
}
--

Also, take a look at the class BlockAddress in llvm/IR/Constants.h.
I think those might help you out.

H.

Thank you! I totally forgot about labels-as-values.