J. Charles N. M.
2014-May-05 14:43 UTC
[LLVMdev] Terminator found in the middle of a basic block error
Hello, I am currently building a compilator for an extended CIL to LLVM with the OCaml's LLVM API. For all the branch statements, I need to create some basic block to represente the LLVM branchement. But after compilation, I got a module error See message error below : Terminator found in the middle of a basic block! label %lbl3 Broken module found, compilation aborted! Aborted (core dumped) ------- And here is the C source code follows by its compilation in LLVM IR representation. C Source: int f(void) { int c; c = 170; if (c % 7 > 5) c = 7; return c; } int main(void) { int tmp; tmp = f(); return tmp; } LLVM module : ; ModuleID = 'program' define i32 @f() { entry: %c = alloca i32 store i32 170, i32* %c %fclv2 = load i32* %c %fclv3 = srem i32 %fclv2, 7 %fclv4 = icmp sgt i32 %fclv3, 5 br i1 %fclv4, label %lbl1, label %lbl2 lbl1: ; preds = %entry store i32 7, i32* %c br label %lbl3 lbl2: ; preds = %entry br label %lbl3 lbl3: ; preds = %lbl2, %lbl1 store i32 7, i32* %c %fclv5 = load i32* %c ret i32 %fclv5 } Until this optimized form does not work! define i32 @f() { entry: %c = alloca i32 store i32 170, i32* %c %fclv2 = load i32* %c %fclv3 = srem i32 %fclv2, 7 %fclv4 = icmp sgt i32 %fclv3, 5 br i1 %fclv4, label %lbl1, label %lbl3 lbl1: ; preds = %entry store i32 7, i32* %c br label %lbl3 lbl3: ; preds = %entry, %lbl1 store i32 7, i32* %c %fclv5 = load i32* %c ret i32 %fclv5 } I cannot understand why my module is corrupted. Reference manuel says "[...] 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). The terminator instructions are: ‘ret‘, ‘br‘, ‘switch‘, ‘indirectbr‘, ‘invoke‘, ‘resume‘, and ‘unreachable‘." So whats happened here ? Thanks for your helps. --- | J. Charles -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140505/83a67d25/attachment.html>
J. Charles N. M.
2014-May-05 22:27 UTC
[LLVMdev] Terminator found in the middle of a basic block error
Problem resolved, sound like a mistake in my source code. Just forgot to manage my builders. --- | J. Charles 2014-05-05 16:43 GMT+02:00 J. Charles N. M. <jcharles.nmbiada at gmail.com>:> Hello, > I am currently building a compilator for an extended CIL to LLVM with the > OCaml's LLVM API. > > For all the branch statements, I need to create some basic block to > represente the LLVM branchement. > But after compilation, I got a module error > > See message error below : > > Terminator found in the middle of a basic block! > label %lbl3 > Broken module found, compilation aborted! > Aborted (core dumped) > > ------- > And here is the C source code follows by its compilation in LLVM IR > representation. > > C Source: > int f(void) > { > int c; > c = 170; > if (c % 7 > 5) c = 7; > return c; > } > > int main(void) > { > int tmp; > tmp = f(); > return tmp; > } > > LLVM module : > ; ModuleID = 'program' > > define i32 @f() { > entry: > %c = alloca i32 > store i32 170, i32* %c > %fclv2 = load i32* %c > %fclv3 = srem i32 %fclv2, 7 > %fclv4 = icmp sgt i32 %fclv3, 5 > br i1 %fclv4, label %lbl1, label %lbl2 > > lbl1: ; preds = %entry > store i32 7, i32* %c > br label %lbl3 > > lbl2: ; preds = %entry > br label %lbl3 > > lbl3: ; preds = %lbl2, %lbl1 > store i32 7, i32* %c > %fclv5 = load i32* %c > ret i32 %fclv5 > } > > Until this optimized form does not work! > > define i32 @f() { > entry: > %c = alloca i32 > store i32 170, i32* %c > %fclv2 = load i32* %c > %fclv3 = srem i32 %fclv2, 7 > %fclv4 = icmp sgt i32 %fclv3, 5 > br i1 %fclv4, label %lbl1, label %lbl3 > > lbl1: ; preds = %entry > store i32 7, i32* %c > br label %lbl3 > > lbl3: ; preds = %entry, %lbl1 > store i32 7, i32* %c > %fclv5 = load i32* %c > ret i32 %fclv5 > } > > I cannot understand why my module is corrupted. > Reference manuel says > "[...] 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). > The terminator instructions are: ‘ret‘, ‘br‘, ‘switch‘, ‘indirectbr‘, > ‘invoke‘, ‘resume‘, and ‘unreachable‘." > > So whats happened here ? > Thanks for your helps. > --- > | J. Charles > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140506/570f34d8/attachment.html>