verifyFunction fails validation with Terminator found in the middle of a basic block

Hi,

I’m generating IR for a factorial function that looks like below. When I call the verifyFunction, it fails validation with the error message Terminator found in the middle of a basic block. label %then

The resulting IR still works fine but I’m not sure if the code generation should be done in a better way for the if/then block below.

Thanks in advance,
Vijay

define double @fact(double %n) {
entry:
%n1 = alloca double, align 8
store double %n, double* %n1, align 8
%n2 = load double, double* %n1, align 8
%cmptmp = fcmp ule double %n2, 1.000000e+00
%booltmp = uitofp i1 %cmptmp to double
%ifcond = fcmp one double %booltmp, 0.000000e+00
br i1 %ifcond, label %then, label %ifcont

then: ; preds = %entry
ret double 1.000000e+00
br label %ifcont

ifcont: ; preds = %then, %entry
%n3 = load double, double* %n1, align 8
%n4 = load double, double* %n1, align 8
%subtmp = fsub double %n4, 1.000000e+00
%calltmp = call double @fact(double %subtmp)
%multmp = fmul double %n3, %calltmp
ret double %multmp
}
Validating function…
Terminator found in the middle of a basic block!
label %then
Validation failed

Hi,

I’m generating IR for a factorial function that looks like below. When I call the verifyFunction, it fails validation with the error message Terminator found in the middle of a basic block. label %then

Because there shouldn’t be a terminator instruction (in this case the return instruction) in the middle of a basic block. I see no problem with the verifier’s decision here.
I think the %then block should only contain the return instruction. The br label %ifcont instruction is redundant because the control flow will not proceed anyway beyond the return instruction once %then block is taken.

-Min