Thread overview
dmd: if still there
Sep 19, 2014
Chris
Sep 19, 2014
Daniel Murphy
Sep 19, 2014
Chris
Sep 21, 2014
Rainer Schuetze
Sep 21, 2014
Vladimir Panteleev
Re: if still there
Sep 22, 2014
Daniel Murphy
Sep 22, 2014
Rainer Schuetze
September 19, 2014
Out of curiosity. dmd still produces the if statement, although it ain't gonna happen. Same is true of "DoIt.yes". I know, it's an unlikely and marginal example.

[code]
import std.stdio;

enum DoIt {
  yes,
  no
}

void main() {
  doit(DoIt.no);
}

void doit(DoIt flag) {
  if (flag == DoIt.yes) {
    writeln("I'm doing it!");
  }
  else {
    writeln("No, I won't");
  }
}

[asm]

_Dmain:
		push	RBP
		mov	RBP,RSP
		mov	EDI,1
		call	  _D7dump_if4doitFE7dump_if4DoItZv@PC32
		xor	EAX,EAX
		pop	RBP
		ret
		0f1f
		add	[RAX],R8B
.text._Dmain	ends
.text._D7dump_if4doitFE7dump_if4DoItZv	segment
	assume	CS:.text._D7dump_if4doitFE7dump_if4DoItZv
_D7dump_if4doitFE7dump_if4DoItZv:
		push	RBP
		mov	RBP,RSP
		sub	RSP,010h
		mov	-8[RBP],EDI
		cmp	dword ptr -8[RBP],0
		jne	L29
		mov	RDX,FLAT:.rodata[00h][RIP]
		mov	RDI,FLAT:.rodata[00h][RIP]
		mov	RSI,RDX
		call	  _D3std5stdio16__T7writelnTAyaZ7writelnFAyaZv@PC32
		jmp short	L3F
L29:		mov	RDX,FLAT:.rodata[00h][RIP]
		mov	RDI,FLAT:.rodata[00h][RIP]
		mov	RSI,RDX
		call	  _D3std5stdio16__T7writelnTAyaZ7writelnFAyaZv@PC32
L3F:		leave
		ret
		0f1f
		add	byte ptr [RAX],0
		add	[RAX],AL
.text._D7dump_if4doitFE7dump_if4DoItZv	ends

[asm dmd (2.066) -release -boundscheck=off -O -inline]

_Dmain:
		push	RBP
		mov	RBP,RSP
		mov	RDX,FLAT:.rodata[00h][RIP]
		mov	RDI,FLAT:.rodata[00h][RIP]
		mov	RSI,RDX
		call	  _D3std5stdio16__T7writelnTAyaZ7writelnFAyaZv@PC32
		xor	EAX,EAX
		pop	RBP
		ret
		nop
.text._Dmain	ends
.text._D7dump_if4doitFE7dump_if4DoItZv	segment
	assume	CS:.text._D7dump_if4doitFE7dump_if4DoItZv
_D7dump_if4doitFE7dump_if4DoItZv:
		push	RBP
		mov	RBP,RSP
		sub	RSP,010h
		mov	-8[RBP],EDI
		cmp	dword ptr -8[RBP],0
		jne	L29
		mov	RDX,FLAT:.rodata[00h][RIP]
		mov	RDI,FLAT:.rodata[00h][RIP]
		mov	RSI,RDX
		call	  _D3std5stdio16__T7writelnTAyaZ7writelnFAyaZv@PC32
		jmp short	L3F
L29:		mov	RDX,FLAT:.rodata[00h][RIP]
		mov	RDI,FLAT:.rodata[00h][RIP]
		mov	RSI,RDX
		call	  _D3std5stdio16__T7writelnTAyaZ7writelnFAyaZv@PC32
L3F:		mov	RSP,RBP
		pop	RBP
		ret
		0f1f
		add	0[RCX],SPL
.text._D7dump_if4doitFE7dump_if4DoItZv	ends
September 19, 2014
"Chris"  wrote in message news:kcsnboocxeykhknjljfl@forum.dlang.org... 

> Out of curiosity. dmd still produces the if statement, although it ain't gonna happen. Same is true of "DoIt.yes". I know, it's an unlikely and marginal example.

No it doesn't, here's main:

> _Dmain:
> push RBP
> mov RBP,RSP
> mov EDI,1
> call   _D7dump_if4doitFE7dump_if4DoItZv@PC32
> xor EAX,EAX
> pop RBP
> ret
> 0f1f
> add [RAX],R8B
> .text._Dmain ends

See, no branches or comparisons.
September 19, 2014
On Friday, 19 September 2014 at 15:30:25 UTC, Daniel Murphy wrote:
> "Chris"  wrote in message news:kcsnboocxeykhknjljfl@forum.dlang.org...
>
>> Out of curiosity. dmd still produces the if statement, although it ain't gonna happen. Same is true of "DoIt.yes". I know, it's an unlikely and marginal example.
>
> No it doesn't, here's main:
>
>> _Dmain:
>> push RBP
>> mov RBP,RSP
>> mov EDI,1
>> call   _D7dump_if4doitFE7dump_if4DoItZv@PC32
>> xor EAX,EAX
>> pop RBP
>> ret
>> 0f1f
>> add [RAX],R8B
>> .text._Dmain ends
>
> See, no branches or comparisons.

All right, that escaped me. I'm not exactly asm-literate. Mea maxima culpa! But good to know that you can rely on the compiler. It's not that unusual actually. Often I have things like that in my programs. I make the program flexible for extensions in the future but until that happens it lives happily with something like DoIt.no (usually in the signature flag = DoIt.no), i.e. DoIt.yes is never used throughout the program. The if statement should be kept alive in a library though, cos there's no way you can foresee what users gonna do.
September 21, 2014

On 19.09.2014 17:30, Daniel Murphy wrote:
> "Chris"  wrote in message news:kcsnboocxeykhknjljfl@forum.dlang.org...
>> Out of curiosity. dmd still produces the if statement, although it
>> ain't gonna happen. Same is true of "DoIt.yes". I know, it's an
>> unlikely and marginal example.
>
> No it doesn't, here's main:
>
>> _Dmain:
>> push RBP
>> mov RBP,RSP
>> mov EDI,1
>> call   _D7dump_if4doitFE7dump_if4DoItZv@PC32
>> xor EAX,EAX
>> pop RBP
>> ret
>> 0f1f
>> add [RAX],R8B
>> .text._Dmain ends
>
> See, no branches or comparisons.

The branch is still in the doIt function:

> _D7dump_if4doitFE7dump_if4DoItZv:
>          push    RBP
>          mov    RBP,RSP
>          sub    RSP,010h
>          mov    -8[RBP],EDI
>          cmp    dword ptr -8[RBP],0
>          jne    L29

dmd didn't do any inlining at all. It is very restrained with inlining, you'll get much better results with GDC or LDC.
September 21, 2014
On Sunday, 21 September 2014 at 13:28:59 UTC, Rainer Schuetze wrote:
> dmd didn't do any inlining at all. It is very restrained with inlining, you'll get much better results with GDC or LDC.

I believe the function *was* inlined, however the function was still compiled separately and included in the object file.
September 22, 2014
"Rainer Schuetze"  wrote in message news:lvmjqr$h13$1@digitalmars.com...

> The branch is still in the doIt function:

Yes.

> dmd didn't do any inlining at all. It is very restrained with inlining, you'll get much better results with GDC or LDC.

Check again, it inlined doIt into main.  It still writes out doIt even though it's now unreferenced, but that doesn't have much to do with the inliner. 

September 22, 2014

On 22.09.2014 15:24, Daniel Murphy wrote:
> "Rainer Schuetze"  wrote in message news:lvmjqr$h13$1@digitalmars.com...
>
>> The branch is still in the doIt function:
>
> Yes.
>
>> dmd didn't do any inlining at all. It is very restrained with
>> inlining, you'll get much better results with GDC or LDC.
>
> Check again, it inlined doIt into main.  It still writes out doIt even
> though it's now unreferenced, but that doesn't have much to do with the
> inliner.

Yeah, my bad. I must have been confused by your quote of the non-inlining main.

Sorry to dmd for blaming it's inliner, again ;-)