noinline-recursive-fn.ll 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ; The inliner should never inline recursive functions into other functions.
  2. ; This effectively is just peeling off the first iteration of a loop, and the
  3. ; inliner heuristics are not set up for this.
  4. ; RUN: opt -inline -S < %s | FileCheck %s
  5. target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
  6. target triple = "x86_64-apple-darwin10.3"
  7. @g = common global i32 0 ; <i32*> [#uses=1]
  8. define internal void @foo(i32 %x) nounwind ssp {
  9. entry:
  10. %0 = icmp slt i32 %x, 0 ; <i1> [#uses=1]
  11. br i1 %0, label %return, label %bb
  12. bb: ; preds = %entry
  13. %1 = sub nsw i32 %x, 1 ; <i32> [#uses=1]
  14. call void @foo(i32 %1) nounwind ssp
  15. store volatile i32 1, i32* @g, align 4
  16. ret void
  17. return: ; preds = %entry
  18. ret void
  19. }
  20. ;; CHECK-LABEL: @bonk(
  21. ;; CHECK: call void @foo(i32 42)
  22. define void @bonk() nounwind ssp {
  23. entry:
  24. call void @foo(i32 42) nounwind ssp
  25. ret void
  26. }
  27. ;; Here is an indirect case that should not be infinitely inlined.
  28. define internal void @f1(i32 %x, i8* %Foo, i8* %Bar) nounwind ssp {
  29. entry:
  30. %0 = bitcast i8* %Bar to void (i32, i8*, i8*)*
  31. %1 = sub nsw i32 %x, 1
  32. call void %0(i32 %1, i8* %Foo, i8* %Bar) nounwind
  33. store volatile i32 42, i32* @g, align 4
  34. ret void
  35. }
  36. define internal void @f2(i32 %x, i8* %Foo, i8* %Bar) nounwind ssp {
  37. entry:
  38. %0 = icmp slt i32 %x, 0 ; <i1> [#uses=1]
  39. br i1 %0, label %return, label %bb
  40. bb: ; preds = %entry
  41. %1 = bitcast i8* %Foo to void (i32, i8*, i8*)* ; <void (i32, i8*, i8*)*> [#uses=1]
  42. call void %1(i32 %x, i8* %Foo, i8* %Bar) nounwind
  43. store volatile i32 13, i32* @g, align 4
  44. ret void
  45. return: ; preds = %entry
  46. ret void
  47. }
  48. ; CHECK-LABEL: @top_level(
  49. ; CHECK: call void @f2(i32 122
  50. ; Here we inline one instance of the cycle, but we don't want to completely
  51. ; unroll it.
  52. define void @top_level() nounwind ssp {
  53. entry:
  54. call void @f2(i32 123, i8* bitcast (void (i32, i8*, i8*)* @f1 to i8*), i8* bitcast (void (i32, i8*, i8*)* @f2 to i8*)) nounwind ssp
  55. ret void
  56. }
  57. ; Check that a recursive function, when called with a constant that makes the
  58. ; recursive path dead code can actually be inlined.
  59. define i32 @fib(i32 %i) {
  60. entry:
  61. %is.zero = icmp eq i32 %i, 0
  62. br i1 %is.zero, label %zero.then, label %zero.else
  63. zero.then:
  64. ret i32 0
  65. zero.else:
  66. %is.one = icmp eq i32 %i, 1
  67. br i1 %is.one, label %one.then, label %one.else
  68. one.then:
  69. ret i32 1
  70. one.else:
  71. %i1 = sub i32 %i, 1
  72. %f1 = call i32 @fib(i32 %i1)
  73. %i2 = sub i32 %i, 2
  74. %f2 = call i32 @fib(i32 %i2)
  75. %f = add i32 %f1, %f2
  76. ret i32 %f
  77. }
  78. define i32 @fib_caller() {
  79. ; CHECK-LABEL: @fib_caller(
  80. ; CHECK-NOT: call
  81. ; CHECK: ret
  82. %f1 = call i32 @fib(i32 0)
  83. %f2 = call i32 @fib(i32 1)
  84. %result = add i32 %f1, %f2
  85. ret i32 %result
  86. }