Compiler projects using llvm
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes=newgvn -enable-phi-of-ops=true -S -o - %s | FileCheck %s

define void @hoisted-load(i8* %p, i8* %q) {
; CHECK-LABEL: @hoisted-load(
; CHECK-NEXT:  bb56:
; CHECK-NEXT:    [[N60:%.*]] = load i8, i8* [[P:%.*]], align 1
; CHECK-NEXT:    br label [[BB57:%.*]]
; CHECK:       bb57:
; CHECK-NEXT:    [[PHIOFOPS:%.*]] = phi i1 [ true, [[BB56:%.*]] ], [ [[N62:%.*]], [[BB229:%.*]] ]
; CHECK-NEXT:    [[N59:%.*]] = phi i1 [ false, [[BB229]] ], [ true, [[BB56]] ]
; CHECK-NEXT:    [[IDX:%.*]] = phi i8 [ 0, [[BB56]] ], [ [[INC:%.*]], [[BB229]] ]
; CHECK-NEXT:    [[N62]] = icmp ne i8 [[N60]], 2
; CHECK-NEXT:    br i1 [[PHIOFOPS]], label [[BB229]], label [[BB237:%.*]]
; CHECK:       bb229:
; CHECK-NEXT:    [[INC]] = add i8 [[IDX]], 1
; CHECK-NEXT:    store i8 [[INC]], i8* [[Q:%.*]], align 1
; CHECK-NEXT:    br label [[BB57]]
; CHECK:       bb237:
; CHECK-NEXT:    ret void
;
bb56:
  %n60 = load i8, i8* %p
  br label %bb57

bb57:
  %n59 = phi i1 [ false, %bb229 ], [ true, %bb56 ]
  %idx = phi i8 [0, %bb56], [%inc, %bb229]
  %n62 = icmp ne i8 %n60, 2
  %n63 = or i1 %n59, %n62
  br i1 %n63, label %bb229, label %bb237

bb229:
  %inc = add i8 %idx, 1
  store i8 %inc, i8* %q
  br label %bb57

bb237:
  ret void
}

define void @store-in-loop(i8* %p, i8* %q) {
; CHECK-LABEL: @store-in-loop(
; CHECK-NEXT:  bb56:
; CHECK-NEXT:    br label [[BB57:%.*]]
; CHECK:       bb57:
; CHECK-NEXT:    [[N59:%.*]] = phi i1 [ false, [[BB229:%.*]] ], [ true, [[BB56:%.*]] ]
; CHECK-NEXT:    [[IDX:%.*]] = phi i8 [ 0, [[BB56]] ], [ [[INC:%.*]], [[BB229]] ]
; CHECK-NEXT:    [[N60:%.*]] = load i8, i8* [[P:%.*]], align 1
; CHECK-NEXT:    [[N62:%.*]] = icmp ne i8 [[N60]], 2
; CHECK-NEXT:    [[N63:%.*]] = or i1 [[N59]], [[N62]]
; CHECK-NEXT:    br i1 [[N63]], label [[BB229]], label [[BB237:%.*]]
; CHECK:       bb229:
; CHECK-NEXT:    [[INC]] = add i8 [[IDX]], 1
; CHECK-NEXT:    store i8 [[INC]], i8* [[Q:%.*]], align 1
; CHECK-NEXT:    br label [[BB57]]
; CHECK:       bb237:
; CHECK-NEXT:    ret void
;
bb56:
  br label %bb57

bb57:
  %n59 = phi i1 [ false, %bb229 ], [ true, %bb56 ]
  %idx = phi i8 [0, %bb56], [%inc, %bb229]
  %n60 = load i8, i8* %p
  %n62 = icmp ne i8 %n60, 2
  %n63 = or i1 %n59, %n62
  br i1 %n63, label %bb229, label %bb237

bb229:
  %inc = add i8 %idx, 1
  store i8 %inc, i8* %q
  br label %bb57

bb237:
  ret void
}

; TODO: we should support this case
define void @no-alias-store-in-loop(i8* noalias %p, i8* noalias %q) {
; CHECK-LABEL: @no-alias-store-in-loop(
; CHECK-NEXT:  bb56:
; CHECK-NEXT:    br label [[BB57:%.*]]
; CHECK:       bb57:
; CHECK-NEXT:    [[N59:%.*]] = phi i1 [ false, [[BB229:%.*]] ], [ true, [[BB56:%.*]] ]
; CHECK-NEXT:    [[IDX:%.*]] = phi i8 [ 0, [[BB56]] ], [ [[INC:%.*]], [[BB229]] ]
; CHECK-NEXT:    [[N60:%.*]] = load i8, i8* [[P:%.*]], align 1
; CHECK-NEXT:    [[N62:%.*]] = icmp ne i8 [[N60]], 2
; CHECK-NEXT:    [[N63:%.*]] = or i1 [[N59]], [[N62]]
; CHECK-NEXT:    br i1 [[N63]], label [[BB229]], label [[BB237:%.*]]
; CHECK:       bb229:
; CHECK-NEXT:    [[INC]] = add i8 [[IDX]], 1
; CHECK-NEXT:    store i8 [[INC]], i8* [[Q:%.*]], align 1
; CHECK-NEXT:    br label [[BB57]]
; CHECK:       bb237:
; CHECK-NEXT:    ret void
;
bb56:
  br label %bb57

bb57:
  %n59 = phi i1 [ false, %bb229 ], [ true, %bb56 ]
  %idx = phi i8 [0, %bb56], [%inc, %bb229]
  %n60 = load i8, i8* %p
  %n62 = icmp ne i8 %n60, 2
  %n63 = or i1 %n59, %n62
  br i1 %n63, label %bb229, label %bb237

bb229:
  %inc = add i8 %idx, 1
  store i8 %inc, i8* %q
  br label %bb57

bb237:
  ret void
}

define void @function-in-loop(i8* %p) {
; CHECK-LABEL: @function-in-loop(
; CHECK-NEXT:  bb56:
; CHECK-NEXT:    br label [[BB57:%.*]]
; CHECK:       bb57:
; CHECK-NEXT:    [[N59:%.*]] = phi i1 [ false, [[BB229:%.*]] ], [ true, [[BB56:%.*]] ]
; CHECK-NEXT:    [[N60:%.*]] = load i8, i8* [[P:%.*]], align 1
; CHECK-NEXT:    [[N62:%.*]] = icmp ne i8 [[N60]], 2
; CHECK-NEXT:    [[N63:%.*]] = or i1 [[N59]], [[N62]]
; CHECK-NEXT:    br i1 [[N63]], label [[BB229]], label [[BB237:%.*]]
; CHECK:       bb229:
; CHECK-NEXT:    call void @f()
; CHECK-NEXT:    br label [[BB57]]
; CHECK:       bb237:
; CHECK-NEXT:    ret void
;
bb56:
  br label %bb57

bb57:
  %n59 = phi i1 [ false, %bb229 ], [ true, %bb56 ]
  %n60 = load i8, i8* %p
  %n62 = icmp ne i8 %n60, 2
  %n63 = or i1 %n59, %n62
  br i1 %n63, label %bb229, label %bb237

bb229:
  call void @f()
  br label %bb57

bb237:
  ret void
}

; TODO: we should support this case
define void @nowrite-function-in-loop(i8* %p) {
; CHECK-LABEL: @nowrite-function-in-loop(
; CHECK-NEXT:  bb56:
; CHECK-NEXT:    br label [[BB57:%.*]]
; CHECK:       bb57:
; CHECK-NEXT:    [[N59:%.*]] = phi i1 [ false, [[BB229:%.*]] ], [ true, [[BB56:%.*]] ]
; CHECK-NEXT:    [[N60:%.*]] = load i8, i8* [[P:%.*]], align 1
; CHECK-NEXT:    [[N62:%.*]] = icmp ne i8 [[N60]], 2
; CHECK-NEXT:    [[N63:%.*]] = or i1 [[N59]], [[N62]]
; CHECK-NEXT:    br i1 [[N63]], label [[BB229]], label [[BB237:%.*]]
; CHECK:       bb229:
; CHECK-NEXT:    call void @f() #[[ATTR0:[0-9]+]]
; CHECK-NEXT:    br label [[BB57]]
; CHECK:       bb237:
; CHECK-NEXT:    ret void
;
bb56:
  br label %bb57

bb57:
  %n59 = phi i1 [ false, %bb229 ], [ true, %bb56 ]
  %n60 = load i8, i8* %p
  %n62 = icmp ne i8 %n60, 2
  %n63 = or i1 %n59, %n62
  br i1 %n63, label %bb229, label %bb237

bb229:
  call void @f() inaccessiblememonly
  br label %bb57

bb237:
  ret void
}

declare void @f()