Optimization disappears in the easier case

Iain McClatchie iainmcc@ix.netcom.com
Fri Apr 24 16:23:00 GMT 1998


I've furthur reduced the problem, and found a somewhat surprising
result.

In the following test case, I have two loops, which I've juxtaposed
with the resulting code from egcs-1.0.2.  The second is inside a
templatized function, and shows a nice tail-branch optimization.  The
first is inside a non-templatized function, and does NOT have the
optimization.  Aside from the context, the two functions are
identical.

Can someone please take a look?  This really looks like a pretty
simple bug.

-Iain McClatchie

class ent {
public:
};

template <class base>
class list : public base {
public:
  const char    *str;
  list          *next;
};

static inline
int
match( list<ent> *listp, list<ent> &key )
{
  return( listp->str == key.str );
}

ent *                                           lookup1:                 
lookup1( list<ent> *listp, list<ent> &key )	        pushl %ebp       
{						        movl %esp,%ebp   
  for( ; listp; listp = listp->next ) {		        movl 8(%ebp),%edx
    if( match( listp, key ))			        movl 12(%ebp),%ec
      return listp;				        .align 4         
  }						.L4:                     
  return listp;					        testl %edx,%edx  
}						        je .L5           
						        movl 4(%ecx),%eax
						        cmpl %eax,4(%edx)
						        je .L5           
						        movl 8(%edx),%edx
						        jmp .L4          
						        .align 4         
						.L5:                     
						        movl %edx,%eax   
						        movl %ebp,%esp   
						        popl %ebp        
						        ret              

template <class base>				lookup2:                  
base *						        pushl %ebp        
lookup2( list<base> *listp, list<base> &key )	        movl %esp,%ebp    
{						        movl 8(%ebp),%edx 
  for( ; listp; listp = listp->next ) {		        testl %edx,%edx   
    if( match( listp, key ))			        je .L17           
      return listp;				        movl 12(%ebp),%eax
  }						        movl 4(%eax),%eax 
  return listp;					        .align 4          
}						.L19:                     
						        cmpl %eax,4(%edx) 
template class list<ent>;			        je .L17           
						        movl 8(%edx),%edx 
ent *						        testl %edx,%edx   
baz()						        jne .L19          
{						.L17:                     
  list<ent>    key;				        movl %edx,%eax    
  list<ent>   *l = 0;				        movl %ebp,%esp    
  return lookup2( l, key );			        popl %ebp         
}						        ret



More information about the Gcc mailing list