freyone
freyone

Reputation: 349

how to generate the LLVM IR when nest basicblock in LLVM

There is a function basicblock A,another block in the inside of block A ,how to generate the LLVM IR.For example:

int fun()
{/*block A*/
  int i=0;
  {/*block B*/
    int i=1
    printf("i in block B is %d\n",i);
  }
  printf("i in block A is %d\n",i);
 }

Upvotes: 0

Views: 578

Answers (1)

sepp2k
sepp2k

Reputation: 370465

Your blocks A and B aren't basic blocks, they're just blocks. C (or whichever language this is) does not have a concept of basic blocks - LLVM does.

Basic blocks in LLVM do not have to (and often don't) correspond to blocks in the source language. Basically a basic block is just a unit of code, such that you never jump into or out of the middle of it. You only jump to the beginning of the block and only jump from its end.

Blocks in source languages can serve many purposes. Sometimes they're used as part of control flow statements - sometimes they're not. And sometimes you can have control flow without blocks. For example, in many languages, loops and if statements can be used with a single statement body that is not a compound statement (e.g. if (condition) return; - no block here, but still control flow). Likewise switch statements generally don't have a block for each case and then there's of course goto.

So when there's control flow without blocks, the generated program will contain more basic blocks than the source program contained blocks. And in the opposite case where there are blocks without control flow, the generated program will contain less basic blocks.

In your example, the function fun contains no control flow other than the implicit return at the end of the function. Therefore you should only generate a single basic block for it.

Upvotes: 4

Related Questions