[llvm] Propagate nonnull attribute through LLVM IR (mono/mono#13697)
authorAlexander Kyte <alexmkyte@gmail.com>
Wed, 24 Apr 2019 14:53:47 +0000 (10:53 -0400)
committerAlexander Köplinger <alex.koeplinger@outlook.com>
Wed, 24 Apr 2019 14:53:47 +0000 (16:53 +0200)
commit734f3388a339c7ad110dd6da33d4b3da0c0bdebf
tree6e6909169e689a9e93c45303b5322e33c423dcc5
parentaaf988d64dea9ac96899a22925abc75c09bc16bf
[llvm] Propagate nonnull attribute through LLVM IR (mono/mono#13697)

## Summary

This change allows LLVM to identify unnecessary null checks. We mark GOT accesses (including those made by LDSTR) and new object calls as nonnull.

Since LLVM won't propagate this, we propagate from definition to usage, across casts, and from usage to definition (conditionally).

This enables it to remove a significant portion of some benchmarks.

In real-world code, we can expect to see this remove the unnecessary null checks made by private methods.

## Example

### C#:

Note that the constant strings are trivially non-null. This PR spots and propagates that.

```

   static void ThrowIfNull(string s)
    {
        if (s == null)
            ThrowArgumentNullException();
    }

    static void ThrowArgumentNullException()
    {
        throw new ArgumentNullException();
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static int Bench(string a, string b, string c, string d)
    {
        ThrowIfNull(a);
        ThrowIfNull(b);
        ThrowIfNull(c);
        ThrowIfNull(d);

        return a.Length + b.Length + c.Length + d.Length;
    }

    [Benchmark(Description = nameof(NoThrowInline))]
    public int Test() => Bench("a", "bc", "def", "ghij");

```

### Before:

```

define hidden monocc i32 @NoThrowInline_MainClass_Bench_string_string_string_string(i64*  %arg_a, i64* %arg_b, i64* %arg_c, i64*  %arg_d) mono/mono#6 gc "mono" {
BB0:
  br label %INIT_BB1

INIT_BB1:                                         ; preds = %BB0
  br label %INITED_BB2

INITED_BB2:                                       ; preds = %INIT_BB1
  br label %BB3

BB3:                                              ; preds = %INITED_BB2
  br label %BB2

BB2:                                              ; preds = %BB3
  notail call monocc void @NoThrowInline_MainClass_ThrowIfNull_string(i64* %arg_a)
  notail call monocc void @NoThrowInline_MainClass_ThrowIfNull_string(i64* %arg_b)
  notail call monocc void @NoThrowInline_MainClass_ThrowIfNull_string(i64* %arg_c)
  notail call monocc void @NoThrowInline_MainClass_ThrowIfNull_string(i64* %arg_d)
  %0 = bitcast i64* %arg_a to i32*
  %1 = getelementptr i32, i32* %0, i32 4
  %t50 = load volatile i32, i32* %1
  %2 = bitcast i64* %arg_b to i32*
  %3 = getelementptr i32, i32* %2, i32 4
  %t52 = load volatile i32, i32* %3
  %t53 = add i32 %t50, %t52
  %4 = bitcast i64* %arg_c to i32*
  %5 = getelementptr i32, i32* %4, i32 4
  %t55 = load volatile i32, i32* %5
  %t56 = add i32 %t53, %t55
  %6 = bitcast i64* %arg_d to i32*
  %7 = getelementptr i32, i32* %6, i32 4
  %t58 = load volatile i32, i32* %7
  %t60 = add i32 %t56, %t58
  br label %BB1

BB1:                                              ; preds = %BB2
  ret i32 %t60
}

```

### After:

Note: safepoint in below code is added by backend, not part of this change

```

define hidden monocc i32 @NoThrowInline_MainClass_Bench_string_string_string_string(i64* nonnull %arg_a, i64* nonnull %arg_b, i64* nonnull %arg_c, i64* nonnull %arg_d) mono/mono#6 gc "mono" {
BB0:
  %0 = getelementptr i64, i64* %arg_a, i64 2
  %1 = bitcast i64* %0 to i32*
  %t50 = load volatile i32, i32* %1, align 4
  %2 = getelementptr i64, i64* %arg_b, i64 2
  %3 = bitcast i64* %2 to i32*
  %t52 = load volatile i32, i32* %3, align 4
  %t53 = add i32 %t52, %t50
  %4 = getelementptr i64, i64* %arg_c, i64 2
  %5 = bitcast i64* %4 to i32*
  %t55 = load volatile i32, i32* %5, align 4
  %t56 = add i32 %t53, %t55
  %6 = getelementptr i64, i64* %arg_d, i64 2
  %7 = bitcast i64* %6 to i32*
  %t58 = load volatile i32, i32* %7, align 4
  %t60 = add i32 %t56, %t58
  %8 = load i64*, i64** getelementptr inbounds ([37 x i64*], [37 x i64*]* @mono_aot_NoThrowInline_llvm_got, i64 0, i64 7), align 8
  %9 = load i64, i64* %8, align 4
  %10 = icmp eq i64 %9, 0
  br i1 %10, label %gc.safepoint_poll.exit, label %gc.safepoint_poll.poll.i

gc.safepoint_poll.poll.i:                         ; preds = %BB0
  %11 = load void ()*, void ()** bitcast (i64** getelementptr inbounds ([37 x i64*], [37 x i64*]* @mono_aot_NoThrowInline_llvm_got, i64 0, i64 25) to void ()**), align 8
  call void %11() mono/mono#8
  br label %gc.safepoint_poll.exit

gc.safepoint_poll.exit:                           ; preds = %BB0, %gc.safepoint_poll.poll.i
  ret i32 %t60
}

```

## Dependencies

This depends on https://github.com/mono/linker/pull/528.

Commit migrated from https://github.com/mono/mono/commit/0d9e13f983ffa82e8e2360072dc67a4ee3989324
src/mono/mono/mini/aot-compiler.c
src/mono/mono/mini/aot-compiler.h
src/mono/mono/mini/mini-llvm-cpp.cpp
src/mono/mono/mini/mini-llvm-cpp.h
src/mono/mono/mini/mini-llvm.c