> -----Original Message-----
> From: Karol Lewandowski [mailto:k.lewando...@samsung.com]
> Subject: Re: [PATCH v6 4/4] libusbg: Add inline to
> gadget_write_string().
> 
> On 01/23/2014 11:13 AM, Krzysztof Opasiak wrote:
> >> -----Original Message-----
> >> From: David Laight [mailto:david.lai...@aculab.com]>
> >> From: Of Stanislaw Wadas
> >>> Add inline to gadget_write_string().
> >> ...
> >>> -static void usbg_write_string(char *path, char *name, char
> >> *file, char *buf)
> >>> +static inline void usbg_write_string(char *path, char *name,
> >> char *file, char *buf)
> >>>  {
> >>>   usbg_write_buf(path, name, file, buf);
> >>>  }
> >>
> >> Why?
> >> The compiler is very unlikely not to inline it anyway.
> >>
> >> If it doesn't inline it, then specifying 'inline' probably
> >> won't make any difference either.
> >>
> >
> > I agree that new version of gcc can do this without explicit
> inline, but
> > please think about some legacy systems and compilers. Older
> versions of
> > gcc or other compilers may not inline this function if there is
> no
> > keyword. We should write portable code and don't depend on any
> version
> > of compiler. So to do it good we should pass as much information
> to
> > compiler as possible.
> 
> Given different optimization flags (-Os vs -O3) compiler can
> generate
> completely different code.  Both will be "optimal" but by
> completely
> different criteria.
> 
> We should not try to outsmart compiler, it's usually way smarter
> than
> us.

As far as I know , compiler is not smart but only has a good heuristic.
In my opinion this heuristic is based also on inline keyword. Let's take
an example 2 simple functions:

void func(int *a, int b)
{
        int i =1;
        for(i=1; i < b; ++a);
}

static inline void func2(int* a, int b)
{
         func(a, b);
}

Then in main we have a set of 7 call to this functions. Using my gcc
4.6.3 with -01 flag I get different asm if I use inline keyword or not.
When using inline keyword I got func2 optimized out but if I don't use
inline keyword func2 is still present in code and called each time. Both
source and asm results you may found below this message.

I hope that you see now that using or not using inline may influent the
result of the compilation, so specifying inline makes a difference.

--
BR's
Krzysztof Opasiak
Samsung R&D Institute Poland
Samsung Electronics


--- 
test.c:
      
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void func(int *a, int b)
{
        int i =1;
        a++;
        for(i=1; i < b; ++a);
}

static inline void func2(int* a, int b)
{
         func(a, b);
}

int main(int argc, char** argv)
{

        int a =(int) (long)argv[0];
        int b =(int) (long)argv[1];

        func2(&a, b);
        func2(&a, b);
        func2(&a, b);
        func2(&a, b);
        func2(&a, b);
        func2(&a, b);
        func2(&a, b);

          a++;
        return a;
}      
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
gcc -S test.c -O1 without func2 inline      
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        .file   "test.c"
        .text
        .globl  func
        .type   func, @function
func:
.LFB0:
        .cfi_startproc
        cmpl    $1, %esi
        jle     .L4
.L3:
        jmp     .L3
.L4:
        rep
        ret
        .cfi_endproc
.LFE0:
        .size   func, .-func
        .type   func2, @function
func2:
.LFB1:
        .cfi_startproc
        call    func
        rep
        ret
        .cfi_endproc
.LFE1:
        .size   func2, .-func2
        .globl  main
        .type   main, @function
main:
.LFB2:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        pushq   %rbx
        .cfi_def_cfa_offset 24
        .cfi_offset 3, -24
        subq    $16, %rsp
        .cfi_def_cfa_offset 40
        movq    %rsi, %rbx
        movq    8(%rsi), %rbp
        movl    %ebp, %esi
        leaq    12(%rsp), %rdi
        call    func2
        movl    %ebp, %esi
        leaq    12(%rsp), %rdi
        call    func2
        movl    %ebp, %esi
        leaq    12(%rsp), %rdi
        call    func2
        movl    %ebp, %esi
        leaq    12(%rsp), %rdi
        call    func2
        movl    %ebp, %esi
        leaq    12(%rsp), %rdi
        call    func2
        movl    %ebp, %esi
        leaq    12(%rsp), %rdi
        call    func2
        movl    %ebp, %esi
        leaq    12(%rsp), %rdi
        call    func2
        movq    (%rbx), %rax
        addl    $1, %eax
        addq    $16, %rsp
        .cfi_def_cfa_offset 24
        popq    %rbx
        .cfi_def_cfa_offset 16
        popq    %rbp
        .cfi_def_cfa_offset 8
        ret
        .cfi_endproc
.LFE2:
        .size   main, .-main
        .ident  "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
        .section        .note.GNU-stack,"",@progbit
      
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
gcc -S test.c -O1 with func2 inline
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

        .file   "test.c"
        .text
        .globl  func
        .type   func, @function
func:
.LFB0:
        .cfi_startproc
        cmpl    $1, %esi
        jle     .L4
.L3:
        jmp     .L3
.L4:
        rep
        ret
        .cfi_endproc
.LFE0:
        .size   func, .-func
        .globl  main
        .type   main, @function
main:
.LFB2:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        pushq   %rbx
        .cfi_def_cfa_offset 24
        .cfi_offset 3, -24
        subq    $16, %rsp
        .cfi_def_cfa_offset 40
        movq    %rsi, %rbx
        movq    8(%rsi), %rbp
        movl    %ebp, %esi
        leaq    12(%rsp), %rdi
        call    func
        movl    %ebp, %esi
        leaq    12(%rsp), %rdi
        call    func
        movl    %ebp, %esi
        leaq    12(%rsp), %rdi
        call    func
        movl    %ebp, %esi
        leaq    12(%rsp), %rdi
        call    func
        movl    %ebp, %esi
        leaq    12(%rsp), %rdi
        call    func
        movl    %ebp, %esi
        leaq    12(%rsp), %rdi
        call    func
        movl    %ebp, %esi
        leaq    12(%rsp), %rdi
        call    func
        movq    (%rbx), %rax
        addl    $1, %eax
        addq    $16, %rsp
        .cfi_def_cfa_offset 24
        popq    %rbx
        .cfi_def_cfa_offset 16
        popq    %rbp
        .cfi_def_cfa_offset 8
        ret
        .cfi_endproc
.LFE2:
        .size   main, .-main
        .ident  "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
        .section        .note.GNU-stack,"",@progbits
      
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to