loongson-community / gcc

GCC source tree for Loongson
GNU General Public License v2.0
17 stars 1 forks source link

[tree-ssa-strlen] missing strlen optimizations after string truncation by assigning NUL #10

Open xiangzhai opened 6 years ago

xiangzhai commented 6 years ago

Hi fellows,

It is not duplicate of #8

As PR71304 mentioned: Testcase:

$ cat t.c
int f (void)
{
  char s[] = "12345";
  __builtin_strcpy (s, "1");
  return __builtin_strlen (s);
}

int g (void)
{
  char s[] = "12345";
  s [1] = '\0';
  return __builtin_strlen (s);
}

GCC 5.5 is able to reproduce the issue:

$ /opt/loongson-gnu-5.5/bin/mips64-linux-gnu-gcc -O2 -S -Wall -Wextra -fdump-tree-optimized=/dev/stdout -o/dev/stdout t.c
        .file   1 "t.c"
        .section .mdebug.abi64
        .previous
        .nan    legacy
        .module fp=64
        .module oddspreg
        .abicalls

;; Function f (f, funcdef_no=0, decl_uid=1486, cgraph_uid=0, symbol_order=0)

f ()
{
  <bb 2>:
  return 1;

}

        .text
        .align  2
        .align  3
        .globl  f
        .set    nomips16
        .set    nomicromips
        .ent    f
        .type   f, @function
f:
        .frame  $sp,0,$31               # vars= 0, regs= 0/0, args= 0, gp= 0
        .mask   0x00000000,0
        .fmask  0x00000000,0
        .set    noreorder
        .set    nomacro
        j       $31
        li      $2,1                    # 0x1

        .set    macro
        .set    reorder
        .end    f
        .size   f, .-f

;; Function g (g, funcdef_no=1, decl_uid=1490, cgraph_uid=1, symbol_order=1)

g ()
{
  char s[6];
  long unsigned int _4;
  int _5;

  <bb 2>:
  s = "12345";
  s[1] = 0;
  _4 = __builtin_strlen (&s);
  _5 = (int) _4;
  s ={v} {CLOBBER};
  return _5;

}

        .align  2
        .align  3
        .globl  g
        .set    nomips16
        .set    nomicromips
        .ent    g
        .type   g, @function
g:
        .frame  $sp,32,$31              # vars= 16, regs= 2/0, args= 0, gp= 0
        .mask   0x90000000,-8
        .fmask  0x00000000,0
        .set    noreorder
        .set    nomacro
        daddiu  $sp,$sp,-32
        sd      $28,16($sp)
        lui     $28,%hi(%neg(%gp_rel(g)))
        daddu   $28,$28,$25
        daddiu  $28,$28,%lo(%neg(%gp_rel(g)))
        ld      $25,%call16(strlen)($28)
        li      $2,825360384                    # 0x31320000
        move    $4,$sp
        addiu   $2,$2,13108
        sw      $2,0($sp)
        sd      $31,24($sp)
        li      $2,13568                        # 0x3500
        sh      $2,4($sp)
        .reloc  1f,R_MIPS_JALR,strlen
1:      jalr    $25
        sb      $0,1($sp)

        ld      $31,24($sp)
        ld      $28,16($sp)
        sll     $2,$2,0
        j       $31
        daddiu  $sp,$sp,32

        .set    macro
        .set    reorder
        .end    g
        .size   g, .-g
        .ident  "GCC: (GNU) 5.5.0 20171010 (LLVM China GCC 5.5-2018.01)"

But GCC 8.x works:

$ /opt/mips-gnu-git/bin/mips64-linux-gnu-gcc -O2 -S -Wall -Wextra -fdump-tree-optimized=/dev/stdout -o/dev/stdout t.c           
        .file   1 "t.c"
        .section .mdebug.abi64
        .previous
        .nan    legacy
        .module fp=64
        .module oddspreg
        .abicalls
        .text

;; Function f (f, funcdef_no=0, decl_uid=1582, cgraph_uid=0, symbol_order=0)

f ()
{
  <bb 2> [local count: 1073741825]:
  return 1;

}

        .align  2
        .align  3
        .globl  f
        .set    nomips16
        .set    nomicromips
        .ent    f
        .type   f, @function
f:
        .frame  $sp,0,$31               # vars= 0, regs= 0/0, args= 0, gp= 0
        .mask   0x00000000,0
        .fmask  0x00000000,0
        .set    noreorder
        .set    nomacro
        jr      $31
        li      $2,1                    # 0x1

        .set    macro
        .set    reorder
        .end    f
        .size   f, .-f

;; Function g (g, funcdef_no=1, decl_uid=1586, cgraph_uid=1, symbol_order=1)

g ()
{
  <bb 2> [local count: 1073741825]:
  return 1;

}

        .align  2
        .align  3
        .globl  g
        .set    nomips16
        .set    nomicromips
        .ent    g
        .type   g, @function
g:
        .frame  $sp,0,$31               # vars= 0, regs= 0/0, args= 0, gp= 0
        .mask   0x00000000,0
        .fmask  0x00000000,0
        .set    noreorder
        .set    nomacro
        jr      $31
        li      $2,1                    # 0x1

        .set    macro
        .set    reorder
        .end    g
        .size   g, .-g
        .ident  "GCC: (GNU) 8.0.1 20180123 (experimental)"

Regards, Leslie Zhai