LLVM IR Branchs

Hello,
I would know how can I make a branch in llvm ir without a label of destination?
For example:
br i1 %1, label %bb, NO LABEL HERE

Thanks!

Rafael Baldiati Parizi wrote:

Hello,
I would know how can I make a branch in llvm ir without a label of
destination?
For example:
br i1 %1, label %bb, *NO LABEL HERE*

Unlike an assembly "jmp" instruction, there is no fall-through behaviour in LLVM IR. Your choices for a branch are an unconditional branch which specifies one target block, or a conditional branch which specifies a target for true and a target for false.

See http://llvm.org/docs/LangRef.html#i_br for details.

Nick

What are you trying to do?

-Eli

I have two values and I want see if them are equals. Then, I would a instruction like
if Value1 != Value2, error
without create a new block, continuing the execution in the same block, forcing the LLI to execute this.

2011/9/5 Eli Friedman <eli.friedman@gmail.com>

This is necessary because I am working with the addition of instruction in the code llvm for C programs, after generating the intermediate language and for that, I would put this deviation without changing block.

2011/9/5 Rafael Baldiati Parizi <parizi.computacao@gmail.com>

I have two values and I want see if them are equals. Then, I would a
instruction like
if Value1 != Value2, error
without create a new block, continuing the execution in the same block,
forcing the LLI to execute this.

You can't do that; a br instruction must be at the end of a block.

Splitting a block is trivial, though: the C++ API provides SplitBlock,
and if you're just manipulating the text, you can insert a br to a
label immediately followed by that label anywhere in any function
without changing the semantics of the program.

-Eli

Err, strictly speaking, not anywhere... you can't split a block before
a PHI node or a landingpad. But otherwise, there are no restrictions.

-Eli

Rafael Baldiati Parizi wrote:

I have two values and I want see if them are equals. Then, I would a
instruction like
if Value1 != Value2, error
without create a new block, continuing the execution in the same block,
forcing the LLI to execute this.

You can not. The definition of a "basic block" is a sequence of instructions with a single entry and a single exit. You can not insert multiple exits within a basic block.

I realize that this restriction feels arbitrary, but there's a vast amount of research and practical experience in compiler design built around the notion of basic blocks making up a control flow graph.

Nick

Yes, that’s how I’m doing now … create a new block where the execution flow will continue after the shift, following the basics of basic blocks. But, I want to find a way to reduce the number of block of my code.

2011/9/5 Nick Lewycky <nicholas@mxc.ca>

Yes, that’s how I’m doing now … create a new block where the execution flow will continue after the shift, following the basics of basic blocks. But, I want to find a way to reduce the number of block of my code.

Why do you want to reduce the number of LLVM IR blocks? Just because LLVM IR does not have fall-through branches does not mean that the generated code will not have them. As mentioned before, this is just a level of abstraction used in the LLVM IR.

Ok! I do understand.
Thanks by help!

2011/9/5 Justin Holewinski <justin.holewinski@gmail.com>

Hello,
I created a new block for execution to continue, but I got the case in which a statement defined in a block is used in another, not dominate all uses.
For example:

entry:

%n_addr = alloca i32, align 4
%retval = alloca i32
%“alloca point” = bitcast i32 0 to i32
store i32 %n, i32* %n_addr
%0 = call i32 (i8*, …)* @printf(i8* noalias getelementptr inbounds ([20 x i8]* @.str, i32 0, i32 0)) nounwind
%1 = load i32* %n_addr, align 4
%2 = and i32 %1, 1

%3 = icmp eq i32 %2, 0

br label %new_block

new_block:
%4 = call i32 (i8*, …)* @printf(i8* noalias getelementptr inbounds ([10 x i8]* @.str1, i32 0, i32 0), i32 %4) nounwind

How to solve this?

2011/9/5 Rafael Baldiati Parizi <parizi.computacao@gmail.com>

Sorry, %3 instead of %4.

2011/9/14 Rafael Baldiati Parizi <parizi.computacao@gmail.com>

Hi Rafael,

Sorry, %3 instead of %4.

2011/9/14 Rafael Baldiati Parizi <parizi.computacao@gmail.com
<mailto:parizi.computacao@gmail.com>>

    Hello,
    I created a new block for execution to continue, but I got the case in which
    a statement defined in a block is used in another, not dominate all uses.
    For example:

    entry:
        %n_addr = alloca i32, align 4
        %retval = alloca i32
        %"alloca point" = bitcast i32 0 to i32
       store i32 %n, i32* %n_addr
        %0 = call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds
    ([20 x i8]* @.str, i32 0, i32 0)) nounwind
        %1 = load i32* %n_addr, align 4
        %2 = and i32 %1, 1
    * %3* = icmp eq i32 %2, 0
        .....
        br label %new_block

    new_block:
      %4 = call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds ([10
    x i8]* @.str1, i32 0, i32 0), i32 *%3*) nounwind

since the entry block dominates all others, there is no problem here. I assume
the real code is more complicated, or the message wasn't about %3.

Ciao, Duncan.