slsr-gep.ll 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ; RUN: opt < %s -slsr -gvn -S | FileCheck %s
  2. target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"
  3. ; foo(input[0]);
  4. ; foo(input[s]);
  5. ; foo(input[s * 2]);
  6. ; =>
  7. ; p0 = &input[0];
  8. ; foo(*p);
  9. ; p1 = p0 + s;
  10. ; foo(*p1);
  11. ; p2 = p1 + s;
  12. ; foo(*p2);
  13. define void @slsr_gep(i32* %input, i64 %s) {
  14. ; CHECK-LABEL: @slsr_gep(
  15. ; v0 = input[0];
  16. %p0 = getelementptr inbounds i32, i32* %input, i64 0
  17. %v0 = load i32, i32* %p0
  18. call void @foo(i32 %v0)
  19. ; v1 = input[s];
  20. %p1 = getelementptr inbounds i32, i32* %input, i64 %s
  21. ; CHECK: %p1 = getelementptr inbounds i32, i32* %input, i64 %s
  22. %v1 = load i32, i32* %p1
  23. call void @foo(i32 %v1)
  24. ; v2 = input[s * 2];
  25. %s2 = shl nsw i64 %s, 1
  26. %p2 = getelementptr inbounds i32, i32* %input, i64 %s2
  27. ; CHECK: %p2 = getelementptr inbounds i32, i32* %p1, i64 %s
  28. %v2 = load i32, i32* %p2
  29. call void @foo(i32 %v2)
  30. ret void
  31. }
  32. ; foo(input[0]);
  33. ; foo(input[(long)s]);
  34. ; foo(input[(long)(s * 2)]);
  35. ; =>
  36. ; p0 = &input[0];
  37. ; foo(*p);
  38. ; p1 = p0 + (long)s;
  39. ; foo(*p1);
  40. ; p2 = p1 + (long)s;
  41. ; foo(*p2);
  42. define void @slsr_gep_sext(i32* %input, i32 %s) {
  43. ; CHECK-LABEL: @slsr_gep_sext(
  44. ; v0 = input[0];
  45. %p0 = getelementptr inbounds i32, i32* %input, i64 0
  46. %v0 = load i32, i32* %p0
  47. call void @foo(i32 %v0)
  48. ; v1 = input[s];
  49. %t = sext i32 %s to i64
  50. %p1 = getelementptr inbounds i32, i32* %input, i64 %t
  51. ; CHECK: %p1 = getelementptr inbounds i32, i32* %input, i64 %t
  52. %v1 = load i32, i32* %p1
  53. call void @foo(i32 %v1)
  54. ; v2 = input[s * 2];
  55. %s2 = shl nsw i32 %s, 1
  56. %t2 = sext i32 %s2 to i64
  57. %p2 = getelementptr inbounds i32, i32* %input, i64 %t2
  58. ; CHECK: %p2 = getelementptr inbounds i32, i32* %p1, i64 %t
  59. %v2 = load i32, i32* %p2
  60. call void @foo(i32 %v2)
  61. ret void
  62. }
  63. ; int input[10][5];
  64. ; foo(input[s][t]);
  65. ; foo(input[s * 2][t]);
  66. ; foo(input[s * 3][t]);
  67. ; =>
  68. ; p0 = &input[s][t];
  69. ; foo(*p0);
  70. ; p1 = p0 + 5s;
  71. ; foo(*p1);
  72. ; p2 = p1 + 5s;
  73. ; foo(*p2);
  74. define void @slsr_gep_2d([10 x [5 x i32]]* %input, i64 %s, i64 %t) {
  75. ; CHECK-LABEL: @slsr_gep_2d(
  76. ; v0 = input[s][t];
  77. %p0 = getelementptr inbounds [10 x [5 x i32]], [10 x [5 x i32]]* %input, i64 0, i64 %s, i64 %t
  78. %v0 = load i32, i32* %p0
  79. call void @foo(i32 %v0)
  80. ; v1 = input[s * 2][t];
  81. %s2 = shl nsw i64 %s, 1
  82. ; CHECK: [[BUMP:%[a-zA-Z0-9]+]] = mul i64 %s, 5
  83. %p1 = getelementptr inbounds [10 x [5 x i32]], [10 x [5 x i32]]* %input, i64 0, i64 %s2, i64 %t
  84. ; CHECK: %p1 = getelementptr inbounds i32, i32* %p0, i64 [[BUMP]]
  85. %v1 = load i32, i32* %p1
  86. call void @foo(i32 %v1)
  87. ; v3 = input[s * 3][t];
  88. %s3 = mul nsw i64 %s, 3
  89. %p2 = getelementptr inbounds [10 x [5 x i32]], [10 x [5 x i32]]* %input, i64 0, i64 %s3, i64 %t
  90. ; CHECK: %p2 = getelementptr inbounds i32, i32* %p1, i64 [[BUMP]]
  91. %v2 = load i32, i32* %p2
  92. call void @foo(i32 %v2)
  93. ret void
  94. }
  95. %struct.S = type <{ i64, i32 }>
  96. ; In this case, the bump
  97. ; = (char *)&input[s * 2][t].f1 - (char *)&input[s][t].f1
  98. ; = 60 * s
  99. ; which may not be divisible by typeof(input[s][t].f1) = 8. Therefore, we
  100. ; rewrite the candidates using byte offset instead of index offset as in
  101. ; @slsr_gep_2d.
  102. define void @slsr_gep_uglygep([10 x [5 x %struct.S]]* %input, i64 %s, i64 %t) {
  103. ; CHECK-LABEL: @slsr_gep_uglygep(
  104. ; v0 = input[s][t].f1;
  105. %p0 = getelementptr inbounds [10 x [5 x %struct.S]], [10 x [5 x %struct.S]]* %input, i64 0, i64 %s, i64 %t, i32 0
  106. %v0 = load i64, i64* %p0
  107. call void @bar(i64 %v0)
  108. ; v1 = input[s * 2][t].f1;
  109. %s2 = shl nsw i64 %s, 1
  110. ; CHECK: [[BUMP:%[a-zA-Z0-9]+]] = mul i64 %s, 60
  111. %p1 = getelementptr inbounds [10 x [5 x %struct.S]], [10 x [5 x %struct.S]]* %input, i64 0, i64 %s2, i64 %t, i32 0
  112. ; CHECK: getelementptr inbounds i8, i8* %{{[0-9]+}}, i64 [[BUMP]]
  113. %v1 = load i64, i64* %p1
  114. call void @bar(i64 %v1)
  115. ; v2 = input[s * 3][t].f1;
  116. %s3 = mul nsw i64 %s, 3
  117. %p2 = getelementptr inbounds [10 x [5 x %struct.S]], [10 x [5 x %struct.S]]* %input, i64 0, i64 %s3, i64 %t, i32 0
  118. ; CHECK: getelementptr inbounds i8, i8* %{{[0-9]+}}, i64 [[BUMP]]
  119. %v2 = load i64, i64* %p2
  120. call void @bar(i64 %v2)
  121. ret void
  122. }
  123. define void @slsr_out_of_bounds_gep(i32* %input, i32 %s) {
  124. ; CHECK-LABEL: @slsr_out_of_bounds_gep(
  125. ; v0 = input[0];
  126. %p0 = getelementptr i32, i32* %input, i64 0
  127. %v0 = load i32, i32* %p0
  128. call void @foo(i32 %v0)
  129. ; v1 = input[(long)s];
  130. %t = sext i32 %s to i64
  131. %p1 = getelementptr i32, i32* %input, i64 %t
  132. ; CHECK: %p1 = getelementptr i32, i32* %input, i64 %t
  133. %v1 = load i32, i32* %p1
  134. call void @foo(i32 %v1)
  135. ; v2 = input[(long)(s * 2)];
  136. %s2 = shl nsw i32 %s, 1
  137. %t2 = sext i32 %s2 to i64
  138. %p2 = getelementptr i32, i32* %input, i64 %t2
  139. ; CHECK: %p2 = getelementptr i32, i32* %p1, i64 %t
  140. %v2 = load i32, i32* %p2
  141. call void @foo(i32 %v2)
  142. ret void
  143. }
  144. declare void @foo(i32)
  145. declare void @bar(i64)