Dead Code Elimination using C Compiler

mjc@dsbc.icl.co.uk (Mike Clarke)
Thu, 7 Nov 91 15:21:58 GMT

          From comp.compilers

Related articles
Dead Code Elimination using C Compiler mjc@dsbc.icl.co.uk (1991-11-07)
Re: Dead Code Elimination preston@dawn.cs.rice.edu (1991-11-07)
Re: Re: Dead Code Elimination beck@cs.cornell.edu (1991-11-10)
| List of all articles for this month |
Newsgroups: comp.compilers
From: mjc@dsbc.icl.co.uk (Mike Clarke)
Keywords: optimize, C
Organization: ICL Computers Limited, UK
Date: Thu, 7 Nov 91 15:21:58 GMT

There have been numerous items covering dead code elimination, but I have
yet to see what may amount to a fundamental understanding of what may or
may not be considered to be acceptable/achievable.


If a function has no side effects, should that function collapse into a
'no-op'. Where exactly should the line be drawn? I am a newcomer to this
newgroup and apologise if this topic has been done to death before. If so
then references would be useful.


Take the following example function:


                                ------ code starts -------


#include <stdio.h>


int testdead()
{
char ba[100],ot[100],xa[100],xb[100];
int i,j,k;
strcpy(ba,"simulation program running..............");
strcpy(ot,"string to be copied anywhere anytime....");


k=strlen(ba);


for (j=0; j < 2500 ; j++)
for (i= 0; i < k ; i++)
                {
                                xa[i] = ba[i]; xb[i] = ba[i];
                                xa[i] = ot[i]; xb[i] = ot[i];
                                xa[i] = ba[i]; xb[i] = ba[i];
                                xa[i] = ot[i]; xb[i] = ot[i];
                }
return (1);
}


                                ------ code ends -------


I have put this routine through various compilers across a mixture of
machine architectures, and in the main it would appear that most produce
assembler/object that accurately reflects the C source. One came close to
trying to turn into 'no-op', but in doing so left an interesting code
structure. I submit the resultant code/object as a simple C source to
give the idea of what was optimised out.


                                ------ resultant pseudo-code starts -------


#include <stdio.h>


int testdead()
{
char ba[100];
int i,j,k;
strcpy(ba,"simulation program running..............");


k=strlen(ba);


for (j=0; j < 2500 ; j++)
for (i= 0; i < k ; i++)
                {
/* empty loop */
                }
return (1);
}


                                ------ resultant pseudo-code ends -------


I would like to hear views from all those Compiler gurus out there,
please mail me and I will sift and post a summary.
--
Mike Clarke email: mjc@dsbc.icl.co.uk
ICL Computers Limited, UK
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.