Skip to content

FR: Take branches that break loops out of them.

Don’t know if and how easily this is possible, but as can be seen from !360 (merged), code like

	while CondA do
	begin
		DoA1;
		if CondB then
		begin
			DoB;
			break {/ exit};
		end;
		DoA2;
	end;
	DoC;

can perform better if compiled as

	while CondA do
	begin
		DoA1;
		if CondB then goto B;
		DoA2;
	end;
	goto LoopExit;
B:
	DoB;
	goto LoopExit {/ FunctionExit};
LoopExit:
	DoC;

Because a branch that breaks or exits is obviously executed at most once per loop, and the rest of the time stands in the way of normal loop iterations that must jump over it.

Demo: loop.pas.

My results:

Has101or102_V1: 369 ns/call
Has101or102_V2: 192 ns/call

GCC does it, too!

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information