[llvm-bugs] [Bug 51747] New: Missed hoisting opportunity

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Sep 4 03:28:31 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=51747

            Bug ID: 51747
           Summary: Missed hoisting opportunity
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: david.bolvansky at gmail.com
                CC: llvm-bugs at lists.llvm.org

#include <stdio.h>      /* fprintf, open, fdopen, fread, _fileno, stdin, stdout
*/
#include <stdlib.h>     /* malloc, free */
#include <string.h>     /* strcmp, strlen */

static const char*
extractFilename(const char* path, char separator)
{
    const char* search = strrchr(path, separator);
    if (search == NULL) return path;
    return search+1;
}

char*
FIO_createFilename_fromOutDir(const char* path, const char* outDirName, const
size_t suffixLen)
{
    const char* filenameStart;
    char separator;
    char* result;

    separator = '/';


    filenameStart = extractFilename(path, separator);


    result = (char*) calloc(1, strlen(outDirName) + 1 + strlen(filenameStart) +
suffixLen + 1);
    if (!result) {
       ; // EXM_THROW(30, "zstd: FIO_createFilename_fromOutDir: %s",
strerror(errno));
    }

    memcpy(result, outDirName, strlen(outDirName));
    if (outDirName[strlen(outDirName)-1] == separator) {
        memcpy(result + strlen(outDirName), filenameStart,
strlen(filenameStart));
    } else {
        memcpy(result + strlen(outDirName), &separator, 1);
        memcpy(result + strlen(outDirName) + 1, filenameStart,
strlen(filenameStart));
    }

    return result;
}


define dso_local noalias i8* @_Z29FIO_createFilename_fromOutDirPKcS0_m(i8*
readonly %0, i8* nocapture readonly %1, i64 %2) local_unnamed_addr #0 {
  %4 = tail call i8* @strrchr(i8* noundef nonnull dereferenceable(1) %0, i32
47) #4
  %5 = icmp eq i8* %4, null
  %6 = getelementptr inbounds i8, i8* %4, i64 1
  %7 = select i1 %5, i8* %0, i8* %6
  %8 = tail call i64 @strlen(i8* noundef nonnull dereferenceable(1) %1) #4
  %9 = tail call i64 @strlen(i8* noundef nonnull dereferenceable(1) %7) #4
  %10 = add i64 %2, 2
  %11 = add i64 %10, %8
  %12 = add i64 %11, %9
  %13 = tail call noalias align 16 i8* @calloc(i64 1, i64 %12) #5
  tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 16 %13, i8* align 1 %1,
i64 %8, i1 false)
  %14 = add i64 %8, -1
  %15 = getelementptr inbounds i8, i8* %1, i64 %14
  %16 = load i8, i8* %15, align 1, !tbaa !3
  %17 = icmp eq i8 %16, 47
  %18 = getelementptr inbounds i8, i8* %13, i64 %8
  br i1 %17, label %19, label %21

19:                                               ; preds = %3
  %20 = tail call i64 @strlen(i8* noundef nonnull dereferenceable(1) %7) #4
  tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %18, i8* align 1 %7,
i64 %20, i1 false)
  br label %24

21:                                               ; preds = %3
  store i8 47, i8* %18, align 1
  %22 = getelementptr inbounds i8, i8* %18, i64 1
  %23 = tail call i64 @strlen(i8* noundef nonnull dereferenceable(1) %7) #4
  tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align 1 %22, i8* align
1 %7, i64 %23, i1 false)
  br label %24

24:                                               ; preds = %21, %19
  ret i8* %13
}



Issue: hoist strlen (%20, %23) before branch? GVN Hoist does not help. Seems
like LLVM is worried about "store i8 47, i8* %18, align 1".


With:
19:                                               ; preds = %3
  %20 = tail call i64 @strlen(i8* noundef nonnull dereferenceable(1) %7) #4
  tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %18, i8* align 1 %7,
i64 %20, i1 false)
  br label %24

21:                                               ; preds = %3
  %22 = tail call i64 @strlen(i8* noundef nonnull dereferenceable(1) %7) #4
  %23 = getelementptr inbounds i8, i8* %18, i64 1
  tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align 1 %23, i8* align
1 %7, i64 %22, i1 false)
  br label %24

24:                                               ; preds = %21, %19
  ret i8* %13
}

Hoisted fine.


But if gep and strlen is reordered (also in motivating testcase IR):
19:                                               ; preds = %3
  %20 = tail call i64 @strlen(i8* noundef nonnull dereferenceable(1) %7) #4
  tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %18, i8* align 1 %7,
i64 %20, i1 false)
  br label %24

21:                                               ; preds = %3
  %22 = getelementptr inbounds i8, i8* %18, i64 1         
  %23 = tail call i64 @strlen(i8* noundef nonnull dereferenceable(1) %7) #4
  tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align 1 %22, i8* align
1 %7, i64 %23, i1 false)
  br label %24

24:                                               ; preds = %21, %19
  ret i8* %13
}

Hoisting failed.

%22

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210904/da6bf83f/attachment.html>


More information about the llvm-bugs mailing list