You're getting a very clear error, though. According to the LLVM IR language reference:
There must be no non-phi instructions between the start of a basic block and the PHI instructions: i.e. PHI instructions must be first in a basic block.
You have a phi
in L1
which violates this.
Why does it have %_L1
as one of its sources? There are no jumps to %_L1
anywhere else. I think you should first understand how phi
works, possibly by compiling small pieces of C code into LLVM IR with Clang and see what gets generated.
Put simply, a phi
is needed to have consistency in SSA form while being able to assign one of several values into the same register. Make sure you read about SSA - it explains Phi node as well. And additional good resource is the LLVM tutorial which you should go through. In particular, part 5 covers Phis. As suggested above, running small pieces of C through Clang is a great way to understand how things work. This is in no way "hacky" - it's the scientific method! You read the theory, think hard about it, form hypotheses about how things work and then verify those hypotheses by running Clang and seeing what it generates for real-life control flow.