extractvalue.ll 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ; RUN: opt < %s -instcombine -S | FileCheck %s
  2. declare void @bar({i32, i32} %a)
  3. declare i32 @baz(i32 %a)
  4. ; CHECK-LABEL: define i32 @foo(
  5. ; CHECK-NOT: extractvalue
  6. define i32 @foo(i32 %a, i32 %b) {
  7. ; Instcombine should fold various combinations of insertvalue and extractvalue
  8. ; together
  9. ; Build a simple struct and pull values out again
  10. %s1.1 = insertvalue {i32, i32} undef, i32 %a, 0
  11. %s1 = insertvalue {i32, i32} %s1.1, i32 %b, 1
  12. %v1 = extractvalue {i32, i32} %s1, 0
  13. %v2 = extractvalue {i32, i32} %s1, 1
  14. ; Build a nested struct and pull a sub struct out of it
  15. ; This requires instcombine to insert a few insertvalue instructions
  16. %ns1.1 = insertvalue {i32, {i32, i32}} undef, i32 %v1, 0
  17. %ns1.2 = insertvalue {i32, {i32, i32}} %ns1.1, i32 %v1, 1, 0
  18. %ns1 = insertvalue {i32, {i32, i32}} %ns1.2, i32 %v2, 1, 1
  19. %s2 = extractvalue {i32, {i32, i32}} %ns1, 1
  20. %v3 = extractvalue {i32, {i32, i32}} %ns1, 1, 1
  21. call void @bar({i32, i32} %s2)
  22. ; Use nested extractvalues to get to a value
  23. %s3 = extractvalue {i32, {i32, i32}} %ns1, 1
  24. %v4 = extractvalue {i32, i32} %s3, 1
  25. call void @bar({i32, i32} %s3)
  26. ; Use nested insertvalues to build a nested struct
  27. %s4.1 = insertvalue {i32, i32} undef, i32 %v3, 0
  28. %s4 = insertvalue {i32, i32} %s4.1, i32 %v4, 1
  29. %ns2 = insertvalue {i32, {i32, i32}} undef, {i32, i32} %s4, 1
  30. ; And now extract a single value from there
  31. %v5 = extractvalue {i32, {i32, i32}} %ns2, 1, 1
  32. ret i32 %v5
  33. }
  34. ; CHECK-LABEL: define i32 @extract2gep(
  35. ; CHECK-NEXT: [[GEP:%[a-z0-9]+]] = getelementptr inbounds {{.*}}, {{.*}}* %pair, i64 0, i32 1
  36. ; CHECK-NEXT: [[LOAD:%[A-Za-z0-9]+]] = load i32, i32* [[GEP]]
  37. ; CHECK-NEXT: store
  38. ; CHECK-NEXT: br label %loop
  39. ; CHECK-NOT: extractvalue
  40. ; CHECK: call {{.*}}(i32 [[LOAD]])
  41. ; CHECK-NOT: extractvalue
  42. ; CHECK: ret i32 [[LOAD]]
  43. define i32 @extract2gep({i32, i32}* %pair, i32* %P) {
  44. ; The load + extractvalue should be converted
  45. ; to an inbounds gep + smaller load.
  46. ; The new load should be in the same spot as the old load.
  47. %L = load {i32, i32}, {i32, i32}* %pair
  48. store i32 0, i32* %P
  49. br label %loop
  50. loop:
  51. %E = extractvalue {i32, i32} %L, 1
  52. %C = call i32 @baz(i32 %E)
  53. store i32 %C, i32* %P
  54. %cond = icmp eq i32 %C, 0
  55. br i1 %cond, label %end, label %loop
  56. end:
  57. ret i32 %E
  58. }
  59. ; CHECK-LABEL: define i32 @doubleextract2gep(
  60. ; CHECK-NEXT: [[GEP:%[a-z0-9]+]] = getelementptr inbounds {{.*}}, {{.*}}* %arg, i64 0, i32 1, i32 1
  61. ; CHECK-NEXT: [[LOAD:%[A-Za-z0-9]+]] = load i32, i32* [[GEP]]
  62. ; CHECK-NEXT: ret i32 [[LOAD]]
  63. define i32 @doubleextract2gep({i32, {i32, i32}}* %arg) {
  64. ; The load + extractvalues should be converted
  65. ; to a 3-index inbounds gep + smaller load.
  66. %L = load {i32, {i32, i32}}, {i32, {i32, i32}}* %arg
  67. %E1 = extractvalue {i32, {i32, i32}} %L, 1
  68. %E2 = extractvalue {i32, i32} %E1, 1
  69. ret i32 %E2
  70. }
  71. ; CHECK: define i32 @nogep-multiuse
  72. ; CHECK-NEXT: load {{.*}} %pair
  73. ; CHECK-NEXT: extractvalue
  74. ; CHECK-NEXT: extractvalue
  75. ; CHECK-NEXT: add
  76. ; CHECK-NEXT: ret
  77. define i32 @nogep-multiuse({i32, i32}* %pair) {
  78. ; The load should be left unchanged since both parts are needed.
  79. %L = load volatile {i32, i32}, {i32, i32}* %pair
  80. %LHS = extractvalue {i32, i32} %L, 0
  81. %RHS = extractvalue {i32, i32} %L, 1
  82. %R = add i32 %LHS, %RHS
  83. ret i32 %R
  84. }
  85. ; CHECK: define i32 @nogep-volatile
  86. ; CHECK-NEXT: load volatile {{.*}} %pair
  87. ; CHECK-NEXT: extractvalue
  88. ; CHECK-NEXT: ret
  89. define i32 @nogep-volatile({i32, i32}* %pair) {
  90. ; The load volatile should be left unchanged.
  91. %L = load volatile {i32, i32}, {i32, i32}* %pair
  92. %E = extractvalue {i32, i32} %L, 1
  93. ret i32 %E
  94. }