Search code examples
llvmllvm-ir

how to backpatch basic blocks in LLVM


I'm writing c compiler, and don't know how to implement goto statement in my one pass compiler with llvm, for example:

int main() {
  goto label;
label:
  return 0 
}

since the parser is one pass, when I analyzed goto label;, I cannot get the llvm::BasicBlock which represent label since it does not exist yet, so I if I create a br to implement this goto statement, the parameter of BasicBlock is unknown.

So how can I use llvm to backpatch a basic block?


Solution

  • What you need to do is keep a map from label names to basic blocks, and create entries whenever you see either a label, and it doesn't matter whether the first mention is use or definition.