reorder_load.ll 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ; RUN: opt < %s -tailcallelim -S | FileCheck %s
  2. ; PR4323
  3. target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
  4. ; Several cases where tail call elimination should move the load above the call,
  5. ; then eliminate the tail recursion.
  6. @global = external global i32 ; <i32*> [#uses=1]
  7. @extern_weak_global = extern_weak global i32 ; <i32*> [#uses=1]
  8. ; This load can be moved above the call because the function won't write to it
  9. ; and the call has no side effects.
  10. define fastcc i32 @raise_load_1(i32* %a_arg, i32 %a_len_arg, i32 %start_arg) nounwind readonly {
  11. ; CHECK-LABEL: @raise_load_1(
  12. ; CHECK-NOT: call
  13. ; CHECK: load i32, i32*
  14. ; CHECK-NOT: call
  15. ; CHECK: }
  16. entry:
  17. %tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1]
  18. br i1 %tmp2, label %if, label %else
  19. if: ; preds = %entry
  20. ret i32 0
  21. else: ; preds = %entry
  22. %tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1]
  23. %tmp8 = call fastcc i32 @raise_load_1(i32* %a_arg, i32 %a_len_arg, i32 %tmp7) ; <i32> [#uses=1]
  24. %tmp9 = load i32, i32* %a_arg ; <i32> [#uses=1]
  25. %tmp10 = add i32 %tmp9, %tmp8 ; <i32> [#uses=1]
  26. ret i32 %tmp10
  27. }
  28. ; This load can be moved above the call because the function won't write to it
  29. ; and the load provably can't trap.
  30. define fastcc i32 @raise_load_2(i32* %a_arg, i32 %a_len_arg, i32 %start_arg) readonly {
  31. ; CHECK-LABEL: @raise_load_2(
  32. ; CHECK-NOT: call
  33. ; CHECK: load i32, i32*
  34. ; CHECK-NOT: call
  35. ; CHECK: }
  36. entry:
  37. %tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1]
  38. br i1 %tmp2, label %if, label %else
  39. if: ; preds = %entry
  40. ret i32 0
  41. else: ; preds = %entry
  42. %nullcheck = icmp eq i32* %a_arg, null ; <i1> [#uses=1]
  43. br i1 %nullcheck, label %unwind, label %recurse
  44. unwind: ; preds = %else
  45. unreachable
  46. recurse: ; preds = %else
  47. %tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1]
  48. %tmp8 = call fastcc i32 @raise_load_2(i32* %a_arg, i32 %a_len_arg, i32 %tmp7) ; <i32> [#uses=1]
  49. %tmp9 = load i32, i32* @global ; <i32> [#uses=1]
  50. %tmp10 = add i32 %tmp9, %tmp8 ; <i32> [#uses=1]
  51. ret i32 %tmp10
  52. }
  53. ; This load can be safely moved above the call (even though it's from an
  54. ; extern_weak global) because the call has no side effects.
  55. define fastcc i32 @raise_load_3(i32* %a_arg, i32 %a_len_arg, i32 %start_arg) nounwind readonly {
  56. ; CHECK-LABEL: @raise_load_3(
  57. ; CHECK-NOT: call
  58. ; CHECK: load i32, i32*
  59. ; CHECK-NOT: call
  60. ; CHECK: }
  61. entry:
  62. %tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1]
  63. br i1 %tmp2, label %if, label %else
  64. if: ; preds = %entry
  65. ret i32 0
  66. else: ; preds = %entry
  67. %tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1]
  68. %tmp8 = call fastcc i32 @raise_load_3(i32* %a_arg, i32 %a_len_arg, i32 %tmp7) ; <i32> [#uses=1]
  69. %tmp9 = load i32, i32* @extern_weak_global ; <i32> [#uses=1]
  70. %tmp10 = add i32 %tmp9, %tmp8 ; <i32> [#uses=1]
  71. ret i32 %tmp10
  72. }
  73. ; The second load can be safely moved above the call even though it's from an
  74. ; unknown pointer (which normally means it might trap) because the first load
  75. ; proves it doesn't trap.
  76. define fastcc i32 @raise_load_4(i32* %a_arg, i32 %a_len_arg, i32 %start_arg) readonly {
  77. ; CHECK-LABEL: @raise_load_4(
  78. ; CHECK-NOT: call
  79. ; CHECK: load i32, i32*
  80. ; CHECK-NEXT: load i32, i32*
  81. ; CHECK-NOT: call
  82. ; CHECK: }
  83. entry:
  84. %tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1]
  85. br i1 %tmp2, label %if, label %else
  86. if: ; preds = %entry
  87. ret i32 0
  88. else: ; preds = %entry
  89. %nullcheck = icmp eq i32* %a_arg, null ; <i1> [#uses=1]
  90. br i1 %nullcheck, label %unwind, label %recurse
  91. unwind: ; preds = %else
  92. unreachable
  93. recurse: ; preds = %else
  94. %tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1]
  95. %first = load i32, i32* %a_arg ; <i32> [#uses=1]
  96. %tmp8 = call fastcc i32 @raise_load_4(i32* %a_arg, i32 %first, i32 %tmp7) ; <i32> [#uses=1]
  97. %second = load i32, i32* %a_arg ; <i32> [#uses=1]
  98. %tmp10 = add i32 %second, %tmp8 ; <i32> [#uses=1]
  99. ret i32 %tmp10
  100. }