1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 declare void @bar({i32, i32} %a)
5 declare i32 @baz(i32 %a)
7 ; Instcombine should fold various combinations of insertvalue and extractvalue
9 define i32 @foo(i32 %a, i32 %b) {
11 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i32, i32 } undef, i32 [[A:%.*]], 0
12 ; CHECK-NEXT: [[S2:%.*]] = insertvalue { i32, i32 } [[TMP1]], i32 [[B:%.*]], 1
13 ; CHECK-NEXT: call void @bar({ i32, i32 } [[S2]])
14 ; CHECK-NEXT: [[TMP2:%.*]] = insertvalue { i32, i32 } undef, i32 [[A]], 0
15 ; CHECK-NEXT: [[S3:%.*]] = insertvalue { i32, i32 } [[TMP2]], i32 [[B]], 1
16 ; CHECK-NEXT: call void @bar({ i32, i32 } [[S3]])
17 ; CHECK-NEXT: ret i32 [[B]]
19 %s1.1 = insertvalue {i32, i32} undef, i32 %a, 0
20 %s1 = insertvalue {i32, i32} %s1.1, i32 %b, 1
21 %v1 = extractvalue {i32, i32} %s1, 0
22 %v2 = extractvalue {i32, i32} %s1, 1
24 ; Build a nested struct and pull a sub struct out of it
25 ; This requires instcombine to insert a few insertvalue instructions
26 %ns1.1 = insertvalue {i32, {i32, i32}} undef, i32 %v1, 0
27 %ns1.2 = insertvalue {i32, {i32, i32}} %ns1.1, i32 %v1, 1, 0
28 %ns1 = insertvalue {i32, {i32, i32}} %ns1.2, i32 %v2, 1, 1
29 %s2 = extractvalue {i32, {i32, i32}} %ns1, 1
30 %v3 = extractvalue {i32, {i32, i32}} %ns1, 1, 1
31 call void @bar({i32, i32} %s2)
33 ; Use nested extractvalues to get to a value
34 %s3 = extractvalue {i32, {i32, i32}} %ns1, 1
35 %v4 = extractvalue {i32, i32} %s3, 1
36 call void @bar({i32, i32} %s3)
38 ; Use nested insertvalues to build a nested struct
39 %s4.1 = insertvalue {i32, i32} undef, i32 %v3, 0
40 %s4 = insertvalue {i32, i32} %s4.1, i32 %v4, 1
41 %ns2 = insertvalue {i32, {i32, i32}} undef, {i32, i32} %s4, 1
43 ; And now extract a single value from there
44 %v5 = extractvalue {i32, {i32, i32}} %ns2, 1, 1
49 ; The load + extractvalue should be converted
50 ; to an inbounds gep + smaller load.
51 ; The new load should be in the same spot as the old load.
52 define i32 @extract2gep(ptr %pair, ptr %P) {
53 ; CHECK-LABEL: @extract2gep(
54 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[PAIR:%.*]], i64 4
55 ; CHECK-NEXT: [[E:%.*]] = load i32, ptr [[TMP1]], align 4
56 ; CHECK-NEXT: store i32 0, ptr [[P:%.*]], align 4
57 ; CHECK-NEXT: br label [[LOOP:%.*]]
59 ; CHECK-NEXT: [[C:%.*]] = call i32 @baz(i32 [[E]])
60 ; CHECK-NEXT: store i32 [[C]], ptr [[P]], align 4
61 ; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[C]], 0
62 ; CHECK-NEXT: br i1 [[COND]], label [[END:%.*]], label [[LOOP]]
64 ; CHECK-NEXT: ret i32 [[E]]
66 %L = load {i16, i32}, ptr %pair
71 %E = extractvalue {i16, i32} %L, 1
72 %C = call i32 @baz(i32 %E)
74 %cond = icmp eq i32 %C, 0
75 br i1 %cond, label %end, label %loop
81 ; The load + extractvalues should be converted
82 ; to a 3-index inbounds gep + smaller load.
83 define i16 @doubleextract2gep(ptr %arg) {
84 ; CHECK-LABEL: @doubleextract2gep(
85 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[ARG:%.*]], i64 8
86 ; CHECK-NEXT: [[E2:%.*]] = load i16, ptr [[TMP1]], align 2
87 ; CHECK-NEXT: ret i16 [[E2]]
89 %L = load {i16, {i32, i16}}, ptr %arg
90 %E1 = extractvalue {i16, {i32, i16}} %L, 1
91 %E2 = extractvalue {i32, i16} %E1, 1
95 ; The load should be left unchanged since both parts are needed.
96 define i32 @nogep-multiuse(ptr %pair) {
97 ; CHECK-LABEL: @nogep-multiuse(
98 ; CHECK-NEXT: [[L:%.*]] = load volatile { i32, i32 }, ptr [[PAIR:%.*]], align 4
99 ; CHECK-NEXT: [[LHS:%.*]] = extractvalue { i32, i32 } [[L]], 0
100 ; CHECK-NEXT: [[RHS:%.*]] = extractvalue { i32, i32 } [[L]], 1
101 ; CHECK-NEXT: [[R:%.*]] = add i32 [[LHS]], [[RHS]]
102 ; CHECK-NEXT: ret i32 [[R]]
104 %L = load volatile {i32, i32}, ptr %pair
105 %LHS = extractvalue {i32, i32} %L, 0
106 %RHS = extractvalue {i32, i32} %L, 1
107 %R = add i32 %LHS, %RHS
111 ; The load volatile should be left unchanged.
112 define i32 @nogep-volatile(ptr %pair) {
113 ; CHECK-LABEL: @nogep-volatile(
114 ; CHECK-NEXT: [[L:%.*]] = load volatile { i32, i32 }, ptr [[PAIR:%.*]], align 4
115 ; CHECK-NEXT: [[E:%.*]] = extractvalue { i32, i32 } [[L]], 1
116 ; CHECK-NEXT: ret i32 [[E]]
118 %L = load volatile {i32, i32}, ptr %pair
119 %E = extractvalue {i32, i32} %L, 1