Indirect branching to BasicBlock


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",
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];

  return 0;

  return 0;

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


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