[cfe-users] Can I do a conditional jump without variables using phi?

Levo DeLellis via cfe-users cfe-users at lists.llvm.org
Fri May 17 13:16:25 PDT 2019


Lets take this C code. Obviously, it will always print out normal value

int main() {
    for(int i=0; i<2; i++) {
        if(i>4)
            put("High value")
        else
            put("Normal value")
    }
    return 0;
}

Lets say I want the first time it runs this loop to go through the normal
control flow. I got some simple code when compiling with  -S -emit-llvm .
Lets say after it loops I'd like to go to the if statement that puts high
value instead of the start of the for loop.

I was thinking this phi statement would work. The bottom of the for loop is
label %14, 0% is the obviously the first label/control block, In the phi
statement I use %6 (start of for body) and %9 (the block with puts high)

%a = phi label [%6, %0], [%9, %14]

Then I replace the %6 with %a

br i1 %5, label %6, label %17 ;17 is after the foor loop, the return 0 line

I get the error "error: expected a basic block". I'm assuming it's because
%a isn't a fixed value.

What my real goal is in the middle of the for loop I'd like to re-use the
loop increment and conditional and continue on. Some conditions can be
quite long (a&&b && (c||d) && e>f && more) so it seems like generating the
condition at every point would cause a lot of code and be slow until I hit
it with an optimizer. What are my options? Can I do this without changing
it to a large switch statement?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20190517/0cb9d215/attachment.html>


More information about the cfe-users mailing list