Re: Inlining functions with loops

Michael Meissner <>
Thu, 30 Nov 1995 23:11:21 GMT

          From comp.compilers

Related articles
Inlining functions with loops (Michael Rice) (1995-11-29)
Re: Inlining functions with loops (1995-11-30)
Re: Inlining functions with loops (Michael Meissner) (1995-11-30)
Re: Inlining functions with loops (1995-11-30)
Re: Inlining functions with loops (1995-11-30)
Re: Inlining functions with loops (1995-12-01)
Re: Inlining functions with loops (1995-12-01)
Inlining functions with loops (Dave Lloyd) (1995-12-01)
Re: Inlining functions with loops serrano@ardeche.IRO.UMontreal.CA (1995-12-01)
[6 later articles]
| List of all articles for this month |

Newsgroups: comp.compilers
From: Michael Meissner <>
Keywords: optimize, C++, GCC
Organization: Compilers Central
References: 95-11-241
Date: Thu, 30 Nov 1995 23:11:21 GMT

| All C++ compilers that I am aware of will not inline a function if it
| contains any type of loop. Is anyone aware of ANY C++ compiler that
| will do this? Is anyone aware of a compiler for any language which is
| able to do this?

Gcc/g++ does this. For example, consider the following example:

inline void
zero(char *p, int cnt)
while (cnt-- > 0)
*p++ = cnt;

char buffer[100];

zero(buffer, sizeof(buffer));

Gcc 2.7.2 under Linux on a Pentium generates the following code with
-O2 optimization:

.file ""
.version "01.01"
.align 16
.globl foo__Fv
.type foo__Fv,@function
pushl %ebp
movl %esp,%ebp
movl $buffer,%ecx
movl $99,%edx
.align 4
movb %dl,%al
movb %al,(%ecx)
incl %ecx
movl %edx,%eax
decl %edx
testl %eax,%eax
jg .L11
movl %ebp,%esp
popl %ebp
.size foo__Fv,.Lfe1-foo__Fv
.globl buffer
.type buffer,@object
.size buffer,100
.zero 100
.ident "GCC: (GNU) 2.7.2"

It generates the same code for C if you use "static inline" when
declaring zero as opposed to plain "inline".

| I believe the basic problem is the inability to convert such a function
| to a suitable expression tree. That is, loops are syntactically statements
| with no equivalent expression-like construct.
| For example: if(c1) e1 else e2 can be converted to c1 ? e1 : e2
| e1; e2; e3 can be converted to e1,e2,e3
| What other languages allow inlining functions, and what constructs
| make the function un-inlinable in these languages?

The C end of GCC does inlining too (note, inline functions are
external in scope in the C end of things). Not optimizing, taking the
address of an inline function, recursive call, etc. are common reasons
why GCC doesn't inline some functions. If you want to know what
things don't get inlined, use the -Winline option, and the compiler
will tell you. I routinely compile the PowerPC simulator I work with
with most functions inlined.

Michael Meissner, Cygnus Support (East Coast)
Suite 105, 48 Grove Street, Somerville, MA 02144, USA, 617-629-3016 (office), 617-629-3010 (fax)

Post a followup to this message

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