reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
; RUN: opt < %s -mem2reg -S | FileCheck %s

; This tests that mem2reg preserves the !nonnull metadata on loads
; from allocas that get optimized out.

; Check the case where the alloca in question has a single store.
define float* @single_store(float** %arg) {
; CHECK-LABEL: define float* @single_store
; CHECK: %arg.load = load float*, float** %arg, align 8
; CHECK: [[ASSUME:%(.*)]] = icmp ne float* %arg.load, null
; CHECK: call void @llvm.assume(i1 {{.*}}[[ASSUME]])
; CHECK: ret float* %arg.load
entry:
  %buf = alloca float*
  %arg.load = load float*, float** %arg, align 8
  store float* %arg.load, float** %buf, align 8
  %buf.load = load float*, float **%buf, !nonnull !0
  ret float* %buf.load
}

; Check the case where the alloca in question has more than one
; store but still within one basic block.
define float* @single_block(float** %arg) {
; CHECK-LABEL: define float* @single_block
; CHECK: %arg.load = load float*, float** %arg, align 8
; CHECK: [[ASSUME:%(.*)]] = icmp ne float* %arg.load, null
; CHECK: call void @llvm.assume(i1 {{.*}}[[ASSUME]])
; CHECK: ret float* %arg.load
entry:
  %buf = alloca float*
  %arg.load = load float*, float** %arg, align 8
  store float* null, float** %buf, align 8
  store float* %arg.load, float** %buf, align 8
  %buf.load = load float*, float **%buf, !nonnull !0
  ret float* %buf.load
}

; Check the case where the alloca in question has more than one
; store and also reads ands writes in multiple blocks.
define float* @multi_block(float** %arg) {
; CHECK-LABEL: define float* @multi_block
; CHECK-LABEL: entry:
; CHECK: %arg.load = load float*, float** %arg, align 8
; CHECK: br label %next
; CHECK-LABEL: next:
; CHECK: [[ASSUME:%(.*)]] = icmp ne float* %arg.load, null
; CHECK: call void @llvm.assume(i1 {{.*}}[[ASSUME]])
; CHECK: ret float* %arg.load
entry:
  %buf = alloca float*
  %arg.load = load float*, float** %arg, align 8
  store float* null, float** %buf, align 8
  br label %next
next:
  store float* %arg.load, float** %buf, align 8
  %buf.load = load float*, float** %buf, !nonnull !0
  ret float* %buf.load
}

; Check that we don't add an assume if it's not
; necessary i.e. the value is already implied to be nonnull
define float* @no_assume(float** %arg) {
; CHECK-LABEL: define float* @no_assume
; CHECK-LABEL: entry:
; CHECK: %arg.load = load float*, float** %arg, align 8
; CHECK: %cn = icmp ne float* %arg.load, null
; CHECK: br i1 %cn, label %next, label %fin
; CHECK-LABEL: next:
; CHECK-NOT: call void @llvm.assume
; CHECK: ret float* %arg.load
; CHECK-LABEL: fin:
; CHECK: ret float* null
entry:
  %buf = alloca float*
  %arg.load = load float*, float** %arg, align 8
  %cn = icmp ne float* %arg.load, null
  br i1 %cn, label %next, label %fin
next:
; At this point the above nonnull check ensures that
; the value %arg.load is nonnull in this block and thus
; we need not add the assume.
  store float* %arg.load, float** %buf, align 8
  %buf.load = load float*, float** %buf, !nonnull !0
  ret float* %buf.load
fin:
  ret float* null
}

!0 = !{}