[LLVMdev] Loop elimination with floating point counter.

Jan Rehders wurstgebaeck at googlemail.com
Thu Jan 8 15:46:16 PST 2009


Martin,

> Isn't "simplifying" the loop index to an integer also dependent on
> precision issues? The following loop is infinite:
>
> for (float i=0.0; i<...; i+=1.0) {}
>
> where ... is a large "integral" float literal, e.g. 2^40.

it is. But luckily at least llvm-gcc appears to be smart enough to  
check against the end
value. I've just tested the following snippet (compiled using llvm-gcc  
-c -O3 --emit-llvm)

void foo() {
     float f;
     for( f = 0.0; f < 100.0f; f += 1.0f ) {
     }
}
=>
define void @foo() nounwind readnone {
entry:
	ret void
}

void foo() {
     float f;
     for( f = 0.0; f < 101.0f; f += 1.0f ) {
     }
}
=>
define void @foo() nounwind readnone {
entry:
	br label %bb

bb:		; preds = %bb, %entry
	%f.0.reg2mem.0 = phi float [ 0.000000e+00, %entry ], [ %0, %bb ]		;  
<float> [#uses=1]
	%0 = add float %f.0.reg2mem.0, 1.000000e+00		; <float> [#uses=2]
	%1 = fcmp olt float %0, 1.010000e+02		; <i1> [#uses=1]
	br i1 %1, label %bb, label %return

return:		; preds = %bb
	ret void
}

So everything is fine here

Jan




More information about the llvm-dev mailing list