open-watcom / open-watcom-v2

Open Watcom V2.0 - Source code repository, Wiki, Latest Binary build, Archived builds including all installers for download.
Other
983 stars 160 forks source link

different asm output with different dummy statements #799

Open sezero opened 2 years ago

sezero commented 2 years ago

I wouldn't have imagine this happening, here is the issue. This source: https://github.com/libxmp/libxmp/blob/master/src/loaders/vorbis.c All I did was changing CHECK() and temp_free() macros from ((void) 0) to do {} while(0). For convenience, here are the preprocessed sources:

Before: 1a_c.txt After : 1b_c.txt Diff : src-diff.txt

Build command is: (same with 1b.c) wcc386 -zq -bt=os2 -bm -fp5 -fpi87 -mf -oeatxh -w4 -ei -zp8 -wcd=303 -5s -bd -Fo=1a.obj 1a.c ... which gave a 38406 bytes 1a.obj and 38398 bytes 1b.obj

Here are the asm outputs generated with wdis -a: Before: 1a_asm.txt After : 1b_asm.txt Diff : asm-diff.txt

As a result, the os/2 dll built with the changes is 296 bytes smaller than the one built without.

Any insights? Am I missing something?

FWIW, the ow build is Version 2.0 beta Jan 15 2022 17:51:44 (32-bit) i.e. the CI build from commit 3593d2ffc8ded484172fe6d4d5e411149963e2c3

jmalak commented 2 years ago

It is not simple question, because you use optimization and optimization is complex process, not fully knowned for me. As first try to use for comparison code generated with -od (disable optimization). I am not sure but it is mainly difference that you use in first case dummy statement ((void)0) and in second case loop statement. Loop optimization is different then dummy statement optimization and it could be a reason for code difference, because both versions are optimized different way. I don't know but it can be also afected by issue #614 , that some code optimized out is not removed from generated code (it stays here as dead code. OW compiler generate unoptimized intermediate code (as with -od option) and this intermediate code is translated to machine code. There are multiple optimizers (pre and post). which works on intermediate code and also on machine code.

sezero commented 2 years ago

Tried with -od, the asm diff a lot larger: most of it looks like label name changes, but there are different orderings too, e.g. see around line 180 of this asm diff: asm-od-diff.txt (I can give you the asm outputs as they are too, if you want.)

jmalak commented 2 years ago

For better understanding use -od -d1 compiler options and for disassembly add -s option.

sezero commented 2 years ago

OK, here goes: Before: asm-1a.txt After : asm-1b.txt Diff : asm-diff.txt

jmalak commented 2 years ago

It looks like OW compiler is less eficient if do {} while(0); is used some additional jumps are added and probably has impact to final optimized code. I suppose generated code is working in both cases only size is different. It could be caused by #614 problem that some optimized out parts stay in generated code and therefore size is higher even if it should not. I will link this issue to issue #614 for next fixing.

sezero commented 2 years ago

I suppose generated code is working in both cases only size is different.

Yes, it really seems to be working. I just wasn't expecting any asm difference.

It could be caused by #614 problem that some optimized out parts stay in generated code and therefore size is higher even if it should not. I will link this issue to issue #614 for next fixing.

OK.