www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.ldc - if parameter never used, is ldc smart enough to remove it ?

reply Newbie2019 <newbie2019 gmail.com> writes:
for example:

void  enforce(bool val, string file = __FILE__, size_t line = 
__LINE__){
      if( !val ) {
          debug printf("%s, from line %d\n", file.ptr, line);
        abort();
     }
}

__gshared int base = 10;
int somefn(int val,  string file = __FILE__, size_t line = 
__LINE__) {
       enforce(val > 0, file, line);
      return val + base  ;
}

void main(){
      int i = somefn(1);
}

when build for release, will the parameter reduce into 1 for 
function  somefn && enforce ?

and how to verify this ?
Jul 12 2019
parent John Burton <john.burton jbmail.com> writes:
On Saturday, 13 July 2019 at 06:33:35 UTC, Newbie2019 wrote:
 for example:

 void  enforce(bool val, string file = __FILE__, size_t line = 
 __LINE__){
      if( !val ) {
          debug printf("%s, from line %d\n", file.ptr, line);
        abort();
     }
 }

 __gshared int base = 10;
 int somefn(int val,  string file = __FILE__, size_t line = 
 __LINE__) {
       enforce(val > 0, file, line);
      return val + base  ;
 }

 void main(){
      int i = somefn(1);
 }

 when build for release, will the parameter reduce into 1 for 
 function  somefn && enforce ?
Well I put it into http://d.godbolt.org and it seems to be smart enough to remove all the code from main entirely. https://d.godbolt.org/z/o-pKBL Literally compiling your main to :- _Dmain: xor eax, eax ret If I add a fake function for the value passed in so that it can't just determine at compile time that main needs to do nothing I get this :- https://d.godbolt.org/z/ypgBK2 It has inlined the functions and removed anything to do with the parameters that are not used.
Jul 16 2019