Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

inlining and "dead" code

已查看 0 次
跳至第一个未读帖子

Jens Troeger

未读,
2002年6月24日 20:12:112002/6/24
收件人

hello, two short questions

1) is there any way to force gcc to inline a function/method, when i declare
it as "inline"? seemingly it's up to gcc to decide if the function is to
be inlined or not...

2) i reference code without having control flow, that reaches this code:

int codesize() {
return ((unsigned int) &&Lend) - ((unsigned int) &&Lstart);

Lstart:
__asm__ __volatile__ (" ... ");
Lend:
(void)0;
}

now some versions of gcc (the RH gcc 2.96, to be precise) removes the code
because it is dead. arguable... its not reachable, but referenced. so is
it still dead? anyway, my workaround is ugly

bool phony = false;
int codesize() {
if (phony) goto Lstart;
else return ((unsigned int) &&Lend) - ((unsigned int) &&Lstart);

Lstart:
__asm__ __volatile__ (" ... ");
Lend:
(void)0;
}

is there any other way to prevent the code from being removed? i thought
this is the purpose of __volatile__ but obviously not.

cheers,
jens
--
Jens Tröger Ph: +61 7 3864-9342
Programming Languages & Systems Research Center
School of Computing Science j.tr...@qut.edu.au
QUT, Brisbane, Australia http://www.fit.qut.edu.au/~troger

Paul Pluzhnikov

未读,
2002年6月25日 00:10:522002/6/25
收件人
Jens Troeger <j.tr...@qut.edu.au> writes:

> 1) is there any way to force gcc to inline a function/method, when i declare
> it as "inline"? seemingly it's up to gcc to decide if the function is to
> be inlined or not...

I believe that is true for all C++ compilers -- 'inline' is just like 'register'.
If you want it *guaranteed* inlined, #define it ...

> 2) i reference code without having control flow, that reaches this code:
>
> int codesize() {
> return ((unsigned int) &&Lend) - ((unsigned int) &&Lstart);
>
> Lstart:
> __asm__ __volatile__ (" ... ");
> Lend:
> (void)0;
> }
>
> now some versions of gcc (the RH gcc 2.96, to be precise) removes the code
> because it is dead. arguable... its not reachable, but referenced.

It's not even referenced, just the distance is computed ...

> it still dead? anyway, my workaround is ugly
>

I've tried compiling your workaround with 'gcc-3.0 -O6', and
while it doesn't optimize away the asm, it *doesn't* compute
correct size either:

...
movl $.L5, %eax
subl $.L4, %eax
popl %ebp
ret
.L4:
.L5:
movl $ 99,%eax ## this is what I put into __asm__

I suggest simply coding the whole thing in Assembly.

Cheers,
--
In order to understand recursion you must first understand recursion.

Tauno Voipio

未读,
2002年6月25日 10:19:122002/6/25
收件人

"Jens Troeger" <j.tr...@qut.edu.au> wrote in message
news:3D17B55B...@qut.edu.au...

>
> hello, two short questions
>
> 1) is there any way to force gcc to inline a function/method, when i
declare
> it as "inline"? seemingly it's up to gcc to decide if the function is
to
> be inlined or not...
>
> 2) i reference code without having control flow, that reaches this code:
>
> int codesize() {
> return ((unsigned int) &&Lend) - ((unsigned int) &&Lstart);
>
> Lstart:
> __asm__ __volatile__ (" ... ");
> Lend:
> (void)0;
> }
>

I do it with a little more assembly (this is for an ARM):

-----

static int codesize(void)
{
unsigned int retval;

asm (
"mov %0,=Lend-Lstart\n\t" /* get length */
"b skip_it\n" /* skip the code piece */

"Lstart:\t"
" ... your code here ...\n"
"Lend:\n\t"

"skip_it:\n"
: "=r" (retval)
);

return retval;
}

-----

HTH

Tauno Voipio
tauno voipio @ iki fi


Jens Troeger

未读,
2002年6月25日 19:26:202002/6/25
收件人
> > 1) is there any way to force gcc to inline a function/method, when i declare
> > it as "inline"? seemingly it's up to gcc to decide if the function is to
> > be inlined or not...
>
> I believe that is true for all C++ compilers -- 'inline' is just like 'register'.
> If you want it *guaranteed* inlined, #define it ...

what do you mean with #define it?



> > 2) i reference code without having control flow, that reaches this code:
> >
> > int codesize() {
> > return ((unsigned int) &&Lend) - ((unsigned int) &&Lstart);
> >
> > Lstart:
> > __asm__ __volatile__ (" ... ");
> > Lend:
> > (void)0;
> > }
> >
> > now some versions of gcc (the RH gcc 2.96, to be precise) removes the code
> > because it is dead. arguable... its not reachable, but referenced.
>
> It's not even referenced, just the distance is computed ...

i reference two labels, isn't this "referenced"?

> > it still dead? anyway, my workaround is ugly
> >
>
> I've tried compiling your workaround with 'gcc-3.0 -O6', and
> while it doesn't optimize away the asm, it *doesn't* compute
> correct size either:
>
> ...
> movl $.L5, %eax
> subl $.L4, %eax
> popl %ebp
> ret
> .L4:
> .L5:
> movl $ 99,%eax ## this is what I put into __asm__
>
> I suggest simply coding the whole thing in Assembly.

whops!? even though the code is reachable AND it is declared as volatile,
gcc removes it! that is weird, or a bug. have you tried gcc3.1?

Jens Troeger

未读,
2002年6月25日 19:27:382002/6/25
收件人

> > 2) i reference code without having control flow, that reaches this code:
> >
> > int codesize() {
> > return ((unsigned int) &&Lend) - ((unsigned int) &&Lstart);
> >
> > Lstart:
> > __asm__ __volatile__ (" ... ");
> > Lend:
> > (void)0;
> > }
> >
>
> I do it with a little more assembly (this is for an ARM):
>
> -----
>
> static int codesize(void)
> {
> unsigned int retval;
>
> asm (
> "mov %0,=Lend-Lstart\n\t" /* get length */
> "b skip_it\n" /* skip the code piece */
>
> "Lstart:\t"
> " ... your code here ...\n"
> "Lend:\n\t"
>
> "skip_it:\n"
> : "=r" (retval)
> );
>
> return retval;
> }

hm, since i use asm anyway i might take that approach. although i'd like to
limit the amount of asm to an absolute minimum...

Paul Pluzhnikov

未读,
2002年6月26日 01:05:112002/6/26
收件人
Jens Troeger <j.tr...@qut.edu.au> writes:

> > I believe that is true for all C++ compilers -- 'inline' is just like 'register'.
> > If you want it *guaranteed* inlined, #define it ...
>
> what do you mean with #define it?

I mean instead of this:

inline int min(int a, int b) { return a < b ? a : b; }

write this:

#define MIN(a, b) (a) < (b) ? (a) : (b)

This MIN is *guaranteed* to be inlined ;-)

> > It's not even referenced, just the distance is computed ...
>
> i reference two labels, isn't this "referenced"?

Computing label address is a gcc-extension to begin with.

You can't say something is *broken* if there is no
standard by which you can judge the implementation, can you?

0 个新帖子