; RUN: opt -inline -S %s | FileCheck %s

declare void @g()


;;; Test with a call in a funclet that needs to remain a call
;;; when inlined because the funclet doesn't unwind to caller.
;;; CHECK-LABEL: define void @test1(
define void @test1() personality void ()* @g {
entry:
; CHECK-NEXT: entry:
  invoke void @test1_inlinee()
    to label %exit unwind label %cleanup
cleanup:
  %pad = cleanuppad within none []
  call void @g() [ "funclet"(token %pad) ]
  cleanupret from %pad unwind to caller
exit:
  ret void
}

define void @test1_inlinee() alwaysinline personality void ()* @g {
entry:
  invoke void @g()
    to label %exit unwind label %cleanup.inner
; CHECK-NEXT:  invoke void @g()
; CHECK-NEXT:    unwind label %[[cleanup_inner:.+]]

cleanup.inner:
  %pad.inner = cleanuppad within none []
  call void @g() [ "funclet"(token %pad.inner) ]
  cleanupret from %pad.inner unwind label %cleanup.outer
; CHECK: [[cleanup_inner]]:
; The call here needs to remain a call becuase pad.inner has a cleanupret
; that stays within the inlinee.
; CHECK-NEXT:  %[[pad_inner:[^ ]+]] = cleanuppad within none
; CHECK-NEXT:  call void @g() [ "funclet"(token %[[pad_inner]]) ]
; CHECK-NEXT:  cleanupret from %[[pad_inner]] unwind label %[[cleanup_outer:.+]]

cleanup.outer:
  %pad.outer = cleanuppad within none []
  call void @g() [ "funclet"(token %pad.outer) ]
  cleanupret from %pad.outer unwind to caller
; CHECK: [[cleanup_outer]]:
; The call and cleanupret here need to be redirected to caller cleanup
; CHECK-NEXT: %[[pad_outer:[^ ]+]] = cleanuppad within none
; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[pad_outer]]) ]
; CHECK-NEXT:   unwind label %cleanup
; CHECK: cleanupret from %[[pad_outer]] unwind label %cleanup{{$}}

exit:
  ret void
}



;;; Test with an "unwind to caller" catchswitch in a parent funclet
;;; that needs to remain "unwind to caller" because the parent
;;; doesn't unwind to caller.
;;; CHECK-LABEL: define void @test2(
define void @test2() personality void ()* @g {
entry:
; CHECK-NEXT: entry:
  invoke void @test2_inlinee()
    to label %exit unwind label %cleanup
cleanup:
  %pad = cleanuppad within none []
  call void @g() [ "funclet"(token %pad) ]
  cleanupret from %pad unwind to caller
exit:
  ret void
}

define void @test2_inlinee() alwaysinline personality void ()* @g {
entry:
  invoke void @g()
    to label %exit unwind label %cleanup1
; CHECK-NEXT:   invoke void @g()
; CHECK-NEXT:     unwind label %[[cleanup1:.+]]

cleanup1:
  %outer = cleanuppad within none []
  invoke void @g() [ "funclet"(token %outer) ]
    to label %ret1 unwind label %catchswitch
; CHECK: [[cleanup1]]:
; CHECK-NEXT: %[[outer:[^ ]+]] = cleanuppad within none
; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[outer]]) ]
; CHECK-NEXT:   unwind label %[[catchswitch:.+]]

catchswitch:
  %cs = catchswitch within %outer [label %catch] unwind to caller
; CHECK: [[catchswitch]]:
; The catchswitch here needs to remain "unwind to caller" since %outer
; has a cleanupret that remains within the inlinee.
; CHECK-NEXT: %[[cs:[^ ]+]] = catchswitch within %[[outer]] [label %[[catch:.+]]] unwind to caller

catch:
  %inner = catchpad within %cs []
  call void @g() [ "funclet"(token %inner) ]
  catchret from %inner to label %ret1
; CHECK: [[catch]]:
; The call here needs to remain a call since it too is within %outer
; CHECK:   %[[inner:[^ ]+]] = catchpad within %[[cs]]
; CHECK-NEXT: call void @g() [ "funclet"(token %[[inner]]) ]

ret1:
  cleanupret from %outer unwind label %cleanup2
; CHECK: cleanupret from %[[outer]] unwind label %[[cleanup2:.+]]

cleanup2:
  %later = cleanuppad within none []
  cleanupret from %later unwind to caller
; CHECK: [[cleanup2]]:
; The cleanupret here needs to get redirected to the caller cleanup
; CHECK-NEXT: %[[later:[^ ]+]] = cleanuppad within none
; CHECK-NEXT: cleanupret from %[[later]] unwind label %cleanup{{$}}

exit:
  ret void
}


;;; Test with a call in a cleanup that has no definitive unwind
;;; destination, that must be rewritten to an invoke.
;;; CHECK-LABEL: define void @test3(
define void @test3() personality void ()* @g {
entry:
; CHECK-NEXT: entry:
  invoke void @test3_inlinee()
    to label %exit unwind label %cleanup
cleanup:
  %pad = cleanuppad within none []
  call void @g() [ "funclet"(token %pad) ]
  cleanupret from %pad unwind to caller
exit:
  ret void
}

define void @test3_inlinee() alwaysinline personality void ()* @g {
entry:
  invoke void @g()
    to label %exit unwind label %cleanup
; CHECK-NEXT:  invoke void @g()
; CHECK-NEXT:    unwind label %[[cleanup:.+]]

cleanup:
  %pad = cleanuppad within none []
  call void @g() [ "funclet"(token %pad) ]
  unreachable
; CHECK: [[cleanup]]:
; The call must be rewritten to an invoke targeting the caller cleanup
; because it may well unwind to there.
; CHECK-NEXT: %[[pad:[^ ]+]] = cleanuppad within none
; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[pad]]) ]
; CHECK-NEXT:   unwind label %cleanup{{$}}

exit:
  ret void
}


;;; Test with a catchswitch in a cleanup that has no definitive
;;; unwind destination, that must be rewritten to unwind to the
;;; inlined invoke's unwind dest
;;; CHECK-LABEL: define void @test4(
define void @test4() personality void ()* @g {
entry:
; CHECK-NEXT: entry:
  invoke void @test4_inlinee()
    to label %exit unwind label %cleanup
cleanup:
  %pad = cleanuppad within none []
  call void @g() [ "funclet"(token %pad) ]
  cleanupret from %pad unwind to caller
exit:
  ret void
}

define void @test4_inlinee() alwaysinline personality void ()* @g {
entry:
  invoke void @g()
    to label %exit unwind label %cleanup
; CHECK-NEXT: invoke void @g()
; CHECK-NEXT:   unwind label %[[cleanup:.+]]

cleanup:
  %clean = cleanuppad within none []
  invoke void @g() [ "funclet"(token %clean) ]
    to label %unreachable unwind label %dispatch
; CHECK: [[cleanup]]:
; CHECK-NEXT: %[[clean:[^ ]+]] = cleanuppad within none
; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[clean]]) ]
; CHECK-NEXT:   unwind label %[[dispatch:.+]]

dispatch:
  %cs = catchswitch within %clean [label %catch] unwind to caller
; CHECK: [[dispatch]]:
; The catchswitch must be rewritten to unwind to %cleanup in the caller
; because it may well unwind to there.
; CHECK-NEXT: %[[cs:[^ ]+]] = catchswitch within %[[clean]] [label %[[catch:.+]]] unwind label %cleanup{{$}}

catch:
  catchpad within %cs []
  br label %unreachable
unreachable:
  unreachable
exit:
  ret void
}


;;; Test with multiple levels of nesting, and unwind dests
;;; that need to be inferred from ancestors, descendants,
;;; and cousins.
;;; CHECK-LABEL: define void @test5(
define void @test5() personality void ()* @g {
entry:
; CHECK-NEXT: entry:
  invoke void @test5_inlinee()
    to label %exit unwind label %cleanup
cleanup:
  %pad = cleanuppad within none []
  call void @g() [ "funclet"(token %pad) ]
  cleanupret from %pad unwind to caller
exit:
  ret void
}

define void @test5_inlinee() alwaysinline personality void ()* @g {
entry:
  invoke void @g()
    to label %cont unwind label %noinfo.root
; CHECK-NEXT: invoke void @g()
; CHECK-NEXT:   to label %[[cont:[^ ]+]] unwind label %[[noinfo_root:.+]]

noinfo.root:
  %noinfo.root.pad = cleanuppad within none []
  call void @g() [ "funclet"(token %noinfo.root.pad) ]
  invoke void @g() [ "funclet"(token %noinfo.root.pad) ]
    to label %noinfo.root.cont unwind label %noinfo.left
; CHECK: [[noinfo_root]]:
; Nothing under "noinfo.root" has a definitive unwind destination, so
; we must assume all of it may actually unwind, and redirect unwinds
; to the cleanup in the caller.
; CHECK-NEXT: %[[noinfo_root_pad:[^ ]+]] = cleanuppad within none []
; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[noinfo_root_pad]]) ]
; CHECK-NEXT:   to label %[[next:[^ ]+]] unwind label %cleanup{{$}}
; CHECK: [[next]]:
; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[noinfo_root_pad]]) ]
; CHECK-NEXT:   to label %[[noinfo_root_cont:[^ ]+]] unwind label %[[noinfo_left:.+]]

noinfo.left:
  %noinfo.left.pad = cleanuppad within %noinfo.root.pad []
  invoke void @g() [ "funclet"(token %noinfo.left.pad) ]
    to label %unreachable unwind label %noinfo.left.child
; CHECK: [[noinfo_left]]:
; CHECK-NEXT: %[[noinfo_left_pad:[^ ]+]] = cleanuppad within %[[noinfo_root_pad]]
; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[noinfo_left_pad]]) ]
; CHECK-NEXT:   unwind label %[[noinfo_left_child:.+]]

noinfo.left.child:
  %noinfo.left.child.cs = catchswitch within %noinfo.left.pad [label %noinfo.left.child.catch] unwind to caller
; CHECK: [[noinfo_left_child]]:
; CHECK-NEXT: %[[noinfo_left_child_cs:[^ ]+]] = catchswitch within %[[noinfo_left_pad]] [label %[[noinfo_left_child_catch:[^ ]+]]] unwind label %cleanup{{$}}

noinfo.left.child.catch:
  %noinfo.left.child.pad = catchpad within %noinfo.left.child.cs []
  call void @g() [ "funclet"(token %noinfo.left.child.pad) ]
  br label %unreachable
; CHECK: [[noinfo_left_child_catch]]:
; CHECK-NEXT: %[[noinfo_left_child_pad:[^ ]+]] = catchpad within %[[noinfo_left_child_cs]] []
; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[noinfo_left_child_pad]]) ]
; CHECK-NEXT:   unwind label %cleanup{{$}}

noinfo.root.cont:
  invoke void @g() [ "funclet"(token %noinfo.root.pad) ]
    to label %unreachable unwind label %noinfo.right
; CHECK: [[noinfo_root_cont]]:
; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[noinfo_root_pad]]) ]
; CHECK-NEXT:   unwind label %[[noinfo_right:.+]]

noinfo.right:
  %noinfo.right.cs = catchswitch within %noinfo.root.pad [label %noinfo.right.catch] unwind to caller
; CHECK: [[noinfo_right]]:
; CHECK-NEXT: %[[noinfo_right_cs:[^ ]+]] = catchswitch within %[[noinfo_root_pad]] [label %[[noinfo_right_catch:[^ ]+]]] unwind label %cleanup{{$}}

noinfo.right.catch:
  %noinfo.right.pad = catchpad within %noinfo.right.cs []
  invoke void @g() [ "funclet"(token %noinfo.right.pad) ]
    to label %unreachable unwind label %noinfo.right.child
; CHECK: [[noinfo_right_catch]]:
; CHECK-NEXT: %[[noinfo_right_pad:[^ ]+]] = catchpad within %[[noinfo_right_cs]]
; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[noinfo_right_pad]]) ]
; CHECK-NEXT:   unwind label %[[noinfo_right_child:.+]]

noinfo.right.child:
  %noinfo.right.child.pad = cleanuppad within %noinfo.right.pad []
  call void @g() [ "funclet"(token %noinfo.right.child.pad) ]
  br label %unreachable
; CHECK: [[noinfo_right_child]]:
; CHECK-NEXT: %[[noinfo_right_child_pad:[^ ]+]] = cleanuppad within %[[noinfo_right_pad]]
; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[noinfo_right_child_pad]]) ]
; CHECK-NEXT:   unwind label %cleanup{{$}}

cont:
  invoke void @g()
    to label %exit unwind label %implicit.root
; CHECK: [[cont]]:
; CHECK-NEXT: invoke void @g()
; CHECK-NEXT:   unwind label %[[implicit_root:.+]]

implicit.root:
  %implicit.root.pad = cleanuppad within none []
  call void @g() [ "funclet"(token %implicit.root.pad) ]
  invoke void @g() [ "funclet"(token %implicit.root.pad) ]
    to label %implicit.root.cont unwind label %implicit.left
; CHECK: [[implicit_root]]:
; There's an unwind edge to %internal in implicit.right, and we need to propagate that
; fact down to implicit.right.grandchild, up to implicit.root, and down to
; implicit.left.child.catch, leaving all calls and "unwind to caller" catchswitches
; alone to so they don't conflict with the unwind edge in implicit.right
; CHECK-NEXT: %[[implicit_root_pad:[^ ]+]] = cleanuppad within none
; CHECK-NEXT: call void @g() [ "funclet"(token %[[implicit_root_pad]]) ]
; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[implicit_root_pad]]) ]
; CHECK-NEXT:   to label %[[implicit_root_cont:[^ ]+]] unwind label %[[implicit_left:.+]]

implicit.left:
  %implicit.left.pad = cleanuppad within %implicit.root.pad []
  invoke void @g() [ "funclet"(token %implicit.left.pad) ]
    to label %unreachable unwind label %implicit.left.child
; CHECK: [[implicit_left]]:
; CHECK-NEXT: %[[implicit_left_pad:[^ ]+]] = cleanuppad within %[[implicit_root_pad:[^ ]+]]
; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[implicit_left_pad]]) ]
; CHECK-NEXT:   unwind label %[[implicit_left_child:.+]]

implicit.left.child:
  %implicit.left.child.cs = catchswitch within %implicit.left.pad [label %implicit.left.child.catch] unwind to caller
; CHECK: [[implicit_left_child]]:
; CHECK-NEXT: %[[implicit_left_child_cs:[^ ]+]] = catchswitch within %[[implicit_left_pad]] [label %[[implicit_left_child_catch:[^ ]+]]] unwind to caller

implicit.left.child.catch:
  %implicit.left.child.pad = catchpad within %implicit.left.child.cs []
  call void @g() [ "funclet"(token %implicit.left.child.pad) ]
  br label %unreachable
; CHECK: [[implicit_left_child_catch]]:
; CHECK-NEXT: %[[implicit_left_child_pad:[^ ]+]] = catchpad within %[[implicit_left_child_cs]]
; CHECK-NEXT: call void @g() [ "funclet"(token %[[implicit_left_child_pad]]) ]

implicit.root.cont:
  invoke void @g() [ "funclet"(token %implicit.root.pad) ]
    to label %unreachable unwind label %implicit.right
; CHECK: [[implicit_root_cont]]:
; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[implicit_root_pad]]) ]
; CHECK-NEXT:   unwind label %[[implicit_right:.+]]

implicit.right:
  %implicit.right.cs = catchswitch within %implicit.root.pad [label %implicit.right.catch] unwind label %internal
; CHECK: [[implicit_right]]:
; This is the unwind edge (to %internal) whose existence needs to get propagated around the "implicit" tree
; CHECK-NEXT: %[[implicit_right_cs:[^ ]+]] = catchswitch within %[[implicit_root_pad]] [label %[[implicit_right_catch:[^ ]+]]] unwind label %[[internal:.+]]

implicit.right.catch:
  %implicit.right.pad = catchpad within %implicit.right.cs []
  invoke void @g() [ "funclet"(token %implicit.right.pad) ]
    to label %unreachable unwind label %implicit.right.child
; CHECK: [[implicit_right_catch]]:
; CHECK-NEXT: %[[implicit_right_pad:[^ ]+]] = catchpad within %[[implicit_right_cs]]
; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[implicit_right_pad]]) ]
; CHECK-NEXT:   unwind label %[[implicit_right_child:.+]]

implicit.right.child:
  %implicit.right.child.pad = cleanuppad within %implicit.right.pad []
  invoke void @g() [ "funclet"(token %implicit.right.child.pad) ]
    to label %unreachable unwind label %implicit.right.grandchild
; CHECK: [[implicit_right_child]]:
; CHECK-NEXT: %[[implicit_right_child_pad:[^ ]+]] = cleanuppad within %[[implicit_right_pad]]
; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[implicit_right_child_pad]]) ]
; CHECK-NEXT:   unwind label %[[implicit_right_grandchild:.+]]

implicit.right.grandchild:
  %implicit.right.grandchild.cs = catchswitch within %implicit.right.child.pad [label %implicit.right.grandchild.catch] unwind to caller
; CHECK: [[implicit_right_grandchild]]:
; CHECK-NEXT: %[[implicit_right_grandchild_cs:[^ ]+]] = catchswitch within %[[implicit_right_child_pad]] [label %[[implicit_right_grandchild_catch:[^ ]+]]] unwind to caller

implicit.right.grandchild.catch:
  %implicit.right.grandhcild.pad = catchpad within %implicit.right.grandchild.cs []
  call void @g() [ "funclet"(token %implicit.right.grandhcild.pad) ]
  br label %unreachable
; CHECK: [[implicit_right_grandchild_catch]]:
; CHECK-NEXT: %[[implicit_right_grandhcild_pad:[^ ]+]] = catchpad within %[[implicit_right_grandchild_cs]]
; CHECK-NEXT: call void @g() [ "funclet"(token %[[implicit_right_grandhcild_pad]]) ]

internal:
  %internal.pad = cleanuppad within none []
  call void @g() [ "funclet"(token %internal.pad) ]
  cleanupret from %internal.pad unwind to caller
; CHECK: [[internal]]:
; internal is a cleanup with a "return to caller" cleanuppad; that needs to get redirected
; to %cleanup in the caller, and the call needs to get similarly rewritten to an invoke.
; CHECK-NEXT: %[[internal_pad:[^ ]+]] = cleanuppad within none
; CHECK-NEXT: invoke void @g() [ "funclet"(token %internal.pad.i) ]
; CHECK-NEXT:   to label %[[next:[^ ]+]] unwind label %cleanup{{$}}
; CHECK: [[next]]:
; CHECK-NEXT: cleanupret from %[[internal_pad]] unwind label %cleanup{{$}}

unreachable:
  unreachable
exit:
  ret void
}


declare void @ProcessCLRException()

; Make sure the logic doesn't get tripped up when the inlined invoke is
; itself within a funclet in the caller.
; CHECK-LABEL: define void @test6(
define void @test6() personality void ()* @ProcessCLRException {
entry:
  invoke void @g()
    to label %exit unwind label %callsite_parent
callsite_parent:
  %callsite_parent.pad = cleanuppad within none []
; CHECK: %callsite_parent.pad = cleanuppad within none
  invoke void @test6_inlinee() [ "funclet"(token %callsite_parent.pad) ]
    to label %ret unwind label %cleanup
ret:
  cleanupret from %callsite_parent.pad unwind label %cleanup
cleanup:
  %pad = cleanuppad within none []
  call void @g() [ "funclet"(token %pad) ]
  cleanupret from %pad unwind to caller
exit:
  ret void
}

define void @test6_inlinee() alwaysinline personality void ()* @ProcessCLRException {
entry:
  invoke void @g()
    to label %exit unwind label %inlinee_cleanup
; CHECK-NEXT: invoke void @g() [ "funclet"(token %callsite_parent.pad) ]
; CHECK-NEXT:   unwind label %[[inlinee_cleanup:.+]]

inlinee_cleanup:
  %inlinee.pad = cleanuppad within none []
  call void @g() [ "funclet"(token %inlinee.pad) ]
  unreachable
; CHECK: [[inlinee_cleanup]]:
; CHECK-NEXT: %[[inlinee_pad:[^ ]+]] = cleanuppad within %callsite_parent.pad
; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[inlinee_pad]]) ]
; CHECK-NEXT:   unwind label %cleanup{{$}}

exit:
  ret void
}