Re: Optimizing empty functions in C

"Saumya K. Debray" <debray@CS.Arizona.EDU>
14 Jun 2002 15:26:03 -0400

          From comp.compilers

Related articles
Optimizing empty functions in C wasowski@data.pl (Andrzej Wasowski) (2002-06-13)
Re: Optimizing empty functions in C snicol@apk.net (Scott Nicol) (2002-06-14)
Re: Optimizing empty functions in C joachim_d@gmx.de (Joachim Durchholz) (2002-06-14)
Re: Optimizing empty functions in C vbdis@aol.com (VBDis) (2002-06-14)
Re: Optimizing empty functions in C debray@CS.Arizona.EDU (Saumya K. Debray) (2002-06-14)
Re: Optimizing empty functions in C cdg@nullstone.com (Christopher Glaeser) (2002-06-17)
Re: Optimizing empty functions in C wasowski@data.pl (Andrzej Wasowski) (2002-06-20)
Re: Optimizing empty functions in C wasowski@data.pl (Andrzej Wasowski) (2002-06-20)
Re: Optimizing empty functions in C wasowski@data.pl (Andrzej Wasowski) (2002-06-20)
Re: Optimizing empty functions in C mlacey@microsoft.com (Mark Lacey \[MSFT\]) (2002-06-20)
Re: Optimizing empty functions in C dnovillo@redhat.com (Diego Novillo) (2002-06-28)
[5 later articles]
| List of all articles for this month |

From: "Saumya K. Debray" <debray@CS.Arizona.EDU>
Newsgroups: comp.compilers
Date: 14 Jun 2002 15:26:03 -0400
Organization: University of Arizona CS Department, Tucson AZ
References: 02-06-025
Keywords: C, optimize
Posted-Date: 14 Jun 2002 15:26:03 EDT

Andrzej Wasowski <wasowski@data.pl> wrote:
>A special case of this problem is most interesting for me: if I put many
>empty functions of the same time (let's say void f(void)) in my code -
>will I get many empty functions generated, just one of them, or
>perhaps none (because the compiler can eliminate the call alread)?


An anecdote about optimizing away calls to empty functions: a few years
ago I was experimenting with link-time optimization of Scheme programs
on a Scheme system [Bigloo v1.8] that used the Boehm-Weiser conservative
garbage collector [version 4.7]. The garbage collector contained code
of the form


    [file: os_dep.c]
          GC_find_limit()
          {
                static volatile char *result;
                ...
                GC_setup_temporary_fault_handler();
                ...
                for(;;) {
                    if (...) result += MIN_PAGE_SIZE;
                    else result -= MIN_PAGE_SIZE;
                    GC_noop(*result);
                }
                ...
          }
    --------------------
    [file: mark.c]
          void GC_noop()
          {
              /* do nothing */
          }


In this code, an apparently nonterminating for loop repeatedly changes
the value of the pointer variable "result" until it becomes an illegal
address. At this point, dereferencing it prior to the call to GC_noop()
generates an exception, which is fielded by a handler set up prior to the
for loop by the call to GC_setup_temporary_fault_handler(): this allows
control to leave the for loop. When processing (the machine code
resulting from) this code, our optimizer inlined the call to GC_noop(),
then eliminated the dereference operation "*result" that evaluated the
argument after inferring that it was unnecessary since it was not used.
This, of course, got rid of the exception raised by dereferencing an
illegal address, and produced a nonterminating program.
--
Saumya Debray
Dept. of Computer Science, University of Arizona, Tucson
debray@cs.arizona.edu
http://www.cs.arizona.edu/people/debray
[Isn't that a compiler bug? The compiler's not allowed to optimize away
volatile references. -John]



Post a followup to this message

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