slsr-add.ll 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ; RUN: opt < %s -slsr -gvn -S | FileCheck %s
  2. target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"
  3. define void @shl(i32 %b, i32 %s) {
  4. ; CHECK-LABEL: @shl(
  5. %1 = add i32 %b, %s
  6. ; [[BASIS:%[a-zA-Z0-9]+]] = add i32 %b, %s
  7. call void @foo(i32 %1)
  8. %s2 = shl i32 %s, 1
  9. %2 = add i32 %b, %s2
  10. ; add i32 [[BASIS]], %s
  11. call void @foo(i32 %2)
  12. ret void
  13. }
  14. define void @stride_is_2s(i32 %b, i32 %s) {
  15. ; CHECK-LABEL: @stride_is_2s(
  16. %s2 = shl i32 %s, 1
  17. ; CHECK: %s2 = shl i32 %s, 1
  18. %1 = add i32 %b, %s2
  19. ; CHECK: [[t1:%[a-zA-Z0-9]+]] = add i32 %b, %s2
  20. call void @foo(i32 %1)
  21. %s4 = shl i32 %s, 2
  22. %2 = add i32 %b, %s4
  23. ; CHECK: [[t2:%[a-zA-Z0-9]+]] = add i32 [[t1]], %s2
  24. call void @foo(i32 %2)
  25. %s6 = mul i32 %s, 6
  26. %3 = add i32 %b, %s6
  27. ; CHECK: add i32 [[t2]], %s2
  28. call void @foo(i32 %3)
  29. ret void
  30. }
  31. define void @stride_is_3s(i32 %b, i32 %s) {
  32. ; CHECK-LABEL: @stride_is_3s(
  33. %1 = add i32 %s, %b
  34. ; CHECK: [[t1:%[a-zA-Z0-9]+]] = add i32 %s, %b
  35. call void @foo(i32 %1)
  36. %s4 = shl i32 %s, 2
  37. %2 = add i32 %s4, %b
  38. ; CHECK: [[bump:%[a-zA-Z0-9]+]] = mul i32 %s, 3
  39. ; CHECK: [[t2:%[a-zA-Z0-9]+]] = add i32 [[t1]], [[bump]]
  40. call void @foo(i32 %2)
  41. %s7 = mul i32 %s, 7
  42. %3 = add i32 %s7, %b
  43. ; CHECK: add i32 [[t2]], [[bump]]
  44. call void @foo(i32 %3)
  45. ret void
  46. }
  47. ; foo(b + 6 * s);
  48. ; foo(b + 4 * s);
  49. ; foo(b + 2 * s);
  50. ; =>
  51. ; t1 = b + 6 * s;
  52. ; foo(t1);
  53. ; s2 = 2 * s;
  54. ; t2 = t1 - s2;
  55. ; foo(t2);
  56. ; t3 = t2 - s2;
  57. ; foo(t3);
  58. define void @stride_is_minus_2s(i32 %b, i32 %s) {
  59. ; CHECK-LABEL: @stride_is_minus_2s(
  60. %s6 = mul i32 %s, 6
  61. %1 = add i32 %b, %s6
  62. ; CHECK: [[t1:%[a-zA-Z0-9]+]] = add i32 %b, %s6
  63. ; CHECK: call void @foo(i32 [[t1]])
  64. call void @foo(i32 %1)
  65. %s4 = shl i32 %s, 2
  66. %2 = add i32 %b, %s4
  67. ; CHECK: [[bump:%[a-zA-Z0-9]+]] = shl i32 %s, 1
  68. ; CHECK: [[t2:%[a-zA-Z0-9]+]] = sub i32 [[t1]], [[bump]]
  69. call void @foo(i32 %2)
  70. ; CHECK: call void @foo(i32 [[t2]])
  71. %s2 = shl i32 %s, 1
  72. %3 = add i32 %b, %s2
  73. ; CHECK: [[t3:%[a-zA-Z0-9]+]] = sub i32 [[t2]], [[bump]]
  74. call void @foo(i32 %3)
  75. ; CHECK: call void @foo(i32 [[t3]])
  76. ret void
  77. }
  78. ; t = b + (s << 3);
  79. ; foo(t);
  80. ; foo(b + s);
  81. ;
  82. ; do not rewrite b + s to t - 7 * s because the latter is more complicated.
  83. define void @simple_enough(i32 %b, i32 %s) {
  84. ; CHECK-LABEL: @simple_enough(
  85. %s8 = shl i32 %s, 3
  86. %1 = add i32 %b, %s8
  87. call void @foo(i32 %1)
  88. %2 = add i32 %b, %s
  89. ; CHECK: [[t:%[a-zA-Z0-9]+]] = add i32 %b, %s{{$}}
  90. call void @foo(i32 %2)
  91. ; CHECK: call void @foo(i32 [[t]])
  92. ret void
  93. }
  94. declare void @foo(i32)