<div dir="ltr"><div><div><div><div><div><div><div><div><div>Hello,<br></div>I am currently building a compilator for an extended CIL to LLVM with the OCaml's LLVM API.<br><br></div>For all the branch statements, I need to create some basic block to represente the LLVM branchement.<br>
</div>But after compilation, I got a module error<br><br></div>See message error below :<br><br>Terminator found in the middle of a basic block!<br>label %lbl3<br>Broken module found, compilation aborted!<br>Aborted (core dumped)<br>
<br>-------<br></div>And here is the C source code follows by its compilation in LLVM IR representation.<br><br></div>C Source:<br>int f(void)<br>{<br> int c;<br> c = 170;<br> if (c % 7 > 5) c = 7;<br> return c;<br>
}<br><br>int main(void)<br>{<br> int tmp;<br> tmp = f();<br> return tmp;<br>}<br><br></div>LLVM module :<br>; ModuleID = 'program'<br><br>define i32 @f() {<br>entry:<br> %c = alloca i32<br> store i32 170, i32* %c<br>
%fclv2 = load i32* %c<br> %fclv3 = srem i32 %fclv2, 7<br> %fclv4 = icmp sgt i32 %fclv3, 5<br> br i1 %fclv4, label %lbl1, label %lbl2<br><br>lbl1: ; preds = %entry<br> store i32 7, i32* %c<br>
br label %lbl3<br><br>lbl2: ; preds = %entry<br> br label %lbl3<br><br>lbl3: ; preds = %lbl2, %lbl1<br> store i32 7, i32* %c<br>
%fclv5 = load i32* %c<br>
ret i32 %fclv5<br>}<br><br></div>Until this optimized form does not work!<br><br>define i32 @f() {<br>entry:<br> %c = alloca i32<br> store i32 170, i32* %c<br> %fclv2 = load i32* %c<br> %fclv3 = srem i32 %fclv2, 7<br>
%fclv4 = icmp sgt i32 %fclv3, 5<br> br i1 %fclv4, label %lbl1, label %lbl3<br><br>lbl1: ; preds = %entry<br> store i32 7, i32* %c<br> br label %lbl3<br><br>lbl3: ; preds = %entry, %lbl1<br>
store i32 7, i32* %c<br> %fclv5 = load i32* %c<br> ret i32 %fclv5<br>}<br><br></div>I cannot understand why my module is corrupted. <br>Reference manuel says <br>"[...] every basic block in a program ends with a “Terminator” instruction, which indicates which block should be executed after the current block is finished. These terminator instructions typically yield a ‘void‘ value: they produce control flow, not values (the one exception being the ‘invoke‘ instruction).<br>
The terminator instructions are: ‘ret‘, ‘br‘, ‘switch‘, ‘indirectbr‘, ‘invoke‘, ‘resume‘, and ‘unreachable‘."<p>So whats happened here ?<br></p><div><div><div><div><div><div><div><div><div><div>Thanks for your helps.<br clear="all">
</div><div><div><div><div><div dir="ltr"><div style="font-family:arial;font-size:small"><div dir="ltr"><div> ---</div><div>| J. Charles </div><br></div></div></div></div>
</div></div></div></div></div></div></div></div></div></div></div></div></div>