xref: /aosp_15_r20/external/llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker; RUN: opt -S -codegenprepare %s -o - | FileCheck %s
2*9880d681SAndroid Build Coastguard Worker; RUN: opt -S -codegenprepare -addr-sink-using-gep=1 %s -o - | FileCheck -check-prefix=CHECK-GEP %s
3*9880d681SAndroid Build Coastguard Worker; This file tests the different cases what are involved when codegen prepare
4*9880d681SAndroid Build Coastguard Worker; tries to get sign/zero extension out of the way of addressing mode.
5*9880d681SAndroid Build Coastguard Worker; This tests require an actual target as addressing mode decisions depends
6*9880d681SAndroid Build Coastguard Worker; on the target.
7*9880d681SAndroid Build Coastguard Worker
8*9880d681SAndroid Build Coastguard Workertarget datalayout = "e-i64:64-f80:128-s:64-n8:16:32:64-S128"
9*9880d681SAndroid Build Coastguard Workertarget triple = "x86_64-apple-macosx"
10*9880d681SAndroid Build Coastguard Worker
11*9880d681SAndroid Build Coastguard Worker
12*9880d681SAndroid Build Coastguard Worker; Check that we correctly promote both operands of the promotable add.
13*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @twoArgsPromotion
14*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1SEXT:%[a-zA-Z_0-9-]+]] = sext i32 %arg1 to i64
15*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG2SEXT:%[a-zA-Z_0-9-]+]] = sext i32 %arg2 to i64
16*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = add nsw i64 [[ARG1SEXT]], [[ARG2SEXT]]
17*9880d681SAndroid Build Coastguard Worker; CHECK: inttoptr i64 [[PROMOTED]] to i8*
18*9880d681SAndroid Build Coastguard Worker; CHECK: ret
19*9880d681SAndroid Build Coastguard Workerdefine i8 @twoArgsPromotion(i32 %arg1, i32 %arg2) {
20*9880d681SAndroid Build Coastguard Worker  %add = add nsw i32 %arg1, %arg2
21*9880d681SAndroid Build Coastguard Worker  %sextadd = sext i32 %add to i64
22*9880d681SAndroid Build Coastguard Worker  %base = inttoptr i64 %sextadd to i8*
23*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %base
24*9880d681SAndroid Build Coastguard Worker  ret i8 %res
25*9880d681SAndroid Build Coastguard Worker}
26*9880d681SAndroid Build Coastguard Worker
27*9880d681SAndroid Build Coastguard Worker; Check that we do not promote both operands of the promotable add when
28*9880d681SAndroid Build Coastguard Worker; the instruction will not be folded into the addressing mode.
29*9880d681SAndroid Build Coastguard Worker; Otherwise, we will increase the number of instruction executed.
30*9880d681SAndroid Build Coastguard Worker; (This is a heuristic of course, because the new sext could have been
31*9880d681SAndroid Build Coastguard Worker; merged with something else.)
32*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @twoArgsNoPromotion
33*9880d681SAndroid Build Coastguard Worker; CHECK: add nsw i32 %arg1, %arg2
34*9880d681SAndroid Build Coastguard Worker; CHECK: ret
35*9880d681SAndroid Build Coastguard Workerdefine i8 @twoArgsNoPromotion(i32 %arg1, i32 %arg2, i8* %base) {
36*9880d681SAndroid Build Coastguard Worker  %add = add nsw i32 %arg1, %arg2
37*9880d681SAndroid Build Coastguard Worker  %sextadd = sext i32 %add to i64
38*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %sextadd
39*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
40*9880d681SAndroid Build Coastguard Worker  ret i8 %res
41*9880d681SAndroid Build Coastguard Worker}
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard Worker; Check that we do not promote when the related instruction does not have
44*9880d681SAndroid Build Coastguard Worker; the nsw flag.
45*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @noPromotion
46*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: add i64
47*9880d681SAndroid Build Coastguard Worker; CHECK: ret
48*9880d681SAndroid Build Coastguard Workerdefine i8 @noPromotion(i32 %arg1, i32 %arg2, i8* %base) {
49*9880d681SAndroid Build Coastguard Worker  %add = add i32 %arg1, %arg2
50*9880d681SAndroid Build Coastguard Worker  %sextadd = sext i32 %add to i64
51*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %sextadd
52*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
53*9880d681SAndroid Build Coastguard Worker  ret i8 %res
54*9880d681SAndroid Build Coastguard Worker}
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Worker; Check that we correctly promote constant arguments.
57*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @oneArgPromotion
58*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1SEXT:%[a-zA-Z_0-9-]+]] = sext i32 %arg1 to i64
59*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = add nsw i64 [[ARG1SEXT]], 1
60*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr inbounds i8, i8* %base, i64 [[PROMOTED]]
61*9880d681SAndroid Build Coastguard Worker; CHECK: ret
62*9880d681SAndroid Build Coastguard Workerdefine i8 @oneArgPromotion(i32 %arg1, i8* %base) {
63*9880d681SAndroid Build Coastguard Worker  %add = add nsw i32 %arg1, 1
64*9880d681SAndroid Build Coastguard Worker  %sextadd = sext i32 %add to i64
65*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %sextadd
66*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
67*9880d681SAndroid Build Coastguard Worker  ret i8 %res
68*9880d681SAndroid Build Coastguard Worker}
69*9880d681SAndroid Build Coastguard Worker
70*9880d681SAndroid Build Coastguard Worker; Check that we are able to merge a sign extension with a zero extension.
71*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @oneArgPromotionZExt
72*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1ZEXT:%[a-zA-Z_0-9-]+]] = zext i8 %arg1 to i64
73*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = add nsw i64 [[ARG1ZEXT]], 1
74*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr inbounds i8, i8* %base, i64 [[PROMOTED]]
75*9880d681SAndroid Build Coastguard Worker; CHECK: ret
76*9880d681SAndroid Build Coastguard Workerdefine i8 @oneArgPromotionZExt(i8 %arg1, i8* %base) {
77*9880d681SAndroid Build Coastguard Worker  %zext = zext i8 %arg1 to i32
78*9880d681SAndroid Build Coastguard Worker  %add = add nsw i32 %zext, 1
79*9880d681SAndroid Build Coastguard Worker  %sextadd = sext i32 %add to i64
80*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %sextadd
81*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
82*9880d681SAndroid Build Coastguard Worker  ret i8 %res
83*9880d681SAndroid Build Coastguard Worker}
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker; When promoting a constant zext, the IR builder returns a constant,
86*9880d681SAndroid Build Coastguard Worker; not an instruction. Make sure this is properly handled. This used
87*9880d681SAndroid Build Coastguard Worker; to crash.
88*9880d681SAndroid Build Coastguard Worker; Note: The constant zext is promoted, but does not help matching
89*9880d681SAndroid Build Coastguard Worker; more thing in the addressing mode. Therefore the modification is
90*9880d681SAndroid Build Coastguard Worker; rolled back.
91*9880d681SAndroid Build Coastguard Worker; Still, this test case exercises the desired code path.
92*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @oneArgPromotionCstZExt
93*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = add nsw i64 0, 1
94*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr inbounds i8, i8* %base, i64 [[PROMOTED]]
95*9880d681SAndroid Build Coastguard Worker; CHECK: ret
96*9880d681SAndroid Build Coastguard Workerdefine i8 @oneArgPromotionCstZExt(i8* %base) {
97*9880d681SAndroid Build Coastguard Worker  %cst = zext i16 undef to i32
98*9880d681SAndroid Build Coastguard Worker  %add = add nsw i32 %cst, 1
99*9880d681SAndroid Build Coastguard Worker  %sextadd = sext i32 %add to i64
100*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %sextadd
101*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
102*9880d681SAndroid Build Coastguard Worker  ret i8 %res
103*9880d681SAndroid Build Coastguard Worker}
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker; Check that we do not promote truncate when we cannot determine the
106*9880d681SAndroid Build Coastguard Worker; bits that are dropped.
107*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @oneArgPromotionBlockTrunc1
108*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1TRUNC:%[a-zA-Z_0-9-]+]] = trunc i32 %arg1 to i8
109*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1SEXT:%[a-zA-Z_0-9-]+]] = sext i8 [[ARG1TRUNC]] to i64
110*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = add nsw i64 [[ARG1SEXT]], 1
111*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr inbounds i8, i8* %base, i64 [[PROMOTED]]
112*9880d681SAndroid Build Coastguard Worker; CHECK: ret
113*9880d681SAndroid Build Coastguard Workerdefine i8 @oneArgPromotionBlockTrunc1(i32 %arg1, i8* %base) {
114*9880d681SAndroid Build Coastguard Worker  %trunc = trunc i32 %arg1 to i8
115*9880d681SAndroid Build Coastguard Worker  %add = add nsw i8 %trunc, 1
116*9880d681SAndroid Build Coastguard Worker  %sextadd = sext i8 %add to i64
117*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %sextadd
118*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
119*9880d681SAndroid Build Coastguard Worker  ret i8 %res
120*9880d681SAndroid Build Coastguard Worker}
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard Worker; Check that we do not promote truncate when we cannot determine all the
123*9880d681SAndroid Build Coastguard Worker; bits that are dropped.
124*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @oneArgPromotionBlockTrunc2
125*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1SEXT:%[a-zA-Z_0-9-]+]] = sext i16 %arg1 to i32
126*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1TRUNC:%[a-zA-Z_0-9-]+]] = trunc i32 [[ARG1SEXT]] to i8
127*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1SEXT64:%[a-zA-Z_0-9-]+]] = sext i8 [[ARG1TRUNC]] to i64
128*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = add nsw i64 [[ARG1SEXT64]], 1
129*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr inbounds i8, i8* %base, i64 [[PROMOTED]]
130*9880d681SAndroid Build Coastguard Worker; CHECK: ret
131*9880d681SAndroid Build Coastguard Workerdefine i8 @oneArgPromotionBlockTrunc2(i16 %arg1, i8* %base) {
132*9880d681SAndroid Build Coastguard Worker  %sextarg1 = sext i16 %arg1 to i32
133*9880d681SAndroid Build Coastguard Worker  %trunc = trunc i32 %sextarg1 to i8
134*9880d681SAndroid Build Coastguard Worker  %add = add nsw i8 %trunc, 1
135*9880d681SAndroid Build Coastguard Worker  %sextadd = sext i8 %add to i64
136*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %sextadd
137*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
138*9880d681SAndroid Build Coastguard Worker  ret i8 %res
139*9880d681SAndroid Build Coastguard Worker}
140*9880d681SAndroid Build Coastguard Worker
141*9880d681SAndroid Build Coastguard Worker; Check that we are able to promote truncate when we know all the bits
142*9880d681SAndroid Build Coastguard Worker; that are dropped.
143*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @oneArgPromotionPassTruncKeepSExt
144*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1SEXT:%[a-zA-Z_0-9-]+]] = sext i1 %arg1 to i64
145*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = add nsw i64 [[ARG1SEXT]], 1
146*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr inbounds i8, i8* %base, i64 [[PROMOTED]]
147*9880d681SAndroid Build Coastguard Worker; CHECK: ret
148*9880d681SAndroid Build Coastguard Workerdefine i8 @oneArgPromotionPassTruncKeepSExt(i1 %arg1, i8* %base) {
149*9880d681SAndroid Build Coastguard Worker  %sextarg1 = sext i1 %arg1 to i32
150*9880d681SAndroid Build Coastguard Worker  %trunc = trunc i32 %sextarg1 to i8
151*9880d681SAndroid Build Coastguard Worker  %add = add nsw i8 %trunc, 1
152*9880d681SAndroid Build Coastguard Worker  %sextadd = sext i8 %add to i64
153*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %sextadd
154*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
155*9880d681SAndroid Build Coastguard Worker  ret i8 %res
156*9880d681SAndroid Build Coastguard Worker}
157*9880d681SAndroid Build Coastguard Worker
158*9880d681SAndroid Build Coastguard Worker; On X86 truncate are free. Check that we are able to promote the add
159*9880d681SAndroid Build Coastguard Worker; to be used as addressing mode and that we insert a truncate for the other
160*9880d681SAndroid Build Coastguard Worker; use.
161*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @oneArgPromotionTruncInsert
162*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1SEXT:%[a-zA-Z_0-9-]+]] = sext i8 %arg1 to i64
163*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = add nsw i64 [[ARG1SEXT]], 1
164*9880d681SAndroid Build Coastguard Worker; CHECK: [[TRUNC:%[a-zA-Z_0-9-]+]] = trunc i64 [[PROMOTED]] to i8
165*9880d681SAndroid Build Coastguard Worker; CHECK: [[GEP:%[a-zA-Z_0-9-]+]] = getelementptr inbounds i8, i8* %base, i64 [[PROMOTED]]
166*9880d681SAndroid Build Coastguard Worker; CHECK: [[LOAD:%[a-zA-Z_0-9-]+]] = load i8, i8* [[GEP]]
167*9880d681SAndroid Build Coastguard Worker; CHECK: add i8 [[LOAD]], [[TRUNC]]
168*9880d681SAndroid Build Coastguard Worker; CHECK: ret
169*9880d681SAndroid Build Coastguard Workerdefine i8 @oneArgPromotionTruncInsert(i8 %arg1, i8* %base) {
170*9880d681SAndroid Build Coastguard Worker  %add = add nsw i8 %arg1, 1
171*9880d681SAndroid Build Coastguard Worker  %sextadd = sext i8 %add to i64
172*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %sextadd
173*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
174*9880d681SAndroid Build Coastguard Worker  %finalres = add i8 %res, %add
175*9880d681SAndroid Build Coastguard Worker  ret i8 %finalres
176*9880d681SAndroid Build Coastguard Worker}
177*9880d681SAndroid Build Coastguard Worker
178*9880d681SAndroid Build Coastguard Worker; Cannot sext from a larger type than the promoted type.
179*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @oneArgPromotionLargerType
180*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1TRUNC:%[a-zA-Z_0-9-]+]] = trunc i128 %arg1 to i8
181*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1SEXT64:%[a-zA-Z_0-9-]+]] = sext i8 [[ARG1TRUNC]] to i64
182*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = add nsw i64 [[ARG1SEXT64]], 1
183*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr inbounds i8, i8* %base, i64 [[PROMOTED]]
184*9880d681SAndroid Build Coastguard Worker; CHECK: ret
185*9880d681SAndroid Build Coastguard Workerdefine i8 @oneArgPromotionLargerType(i128 %arg1, i8* %base) {
186*9880d681SAndroid Build Coastguard Worker  %trunc = trunc i128 %arg1 to i8
187*9880d681SAndroid Build Coastguard Worker  %add = add nsw i8 %trunc, 1
188*9880d681SAndroid Build Coastguard Worker  %sextadd = sext i8 %add to i64
189*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %sextadd
190*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
191*9880d681SAndroid Build Coastguard Worker  %finalres = add i8 %res, %add
192*9880d681SAndroid Build Coastguard Worker  ret i8 %finalres
193*9880d681SAndroid Build Coastguard Worker}
194*9880d681SAndroid Build Coastguard Worker
195*9880d681SAndroid Build Coastguard Worker; Use same inserted trunc
196*9880d681SAndroid Build Coastguard Worker; On X86 truncate are free. Check that we are able to promote the add
197*9880d681SAndroid Build Coastguard Worker; to be used as addressing mode and that we insert a truncate for
198*9880d681SAndroid Build Coastguard Worker; *all* the other uses.
199*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @oneArgPromotionTruncInsertSeveralUse
200*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1SEXT:%[a-zA-Z_0-9-]+]] = sext i8 %arg1 to i64
201*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = add nsw i64 [[ARG1SEXT]], 1
202*9880d681SAndroid Build Coastguard Worker; CHECK: [[TRUNC:%[a-zA-Z_0-9-]+]] = trunc i64 [[PROMOTED]] to i8
203*9880d681SAndroid Build Coastguard Worker; CHECK: [[GEP:%[a-zA-Z_0-9-]+]] = getelementptr inbounds i8, i8* %base, i64 [[PROMOTED]]
204*9880d681SAndroid Build Coastguard Worker; CHECK: [[LOAD:%[a-zA-Z_0-9-]+]] = load i8, i8* [[GEP]]
205*9880d681SAndroid Build Coastguard Worker; CHECK: [[ADDRES:%[a-zA-Z_0-9-]+]] = add i8 [[LOAD]], [[TRUNC]]
206*9880d681SAndroid Build Coastguard Worker; CHECK: add i8 [[ADDRES]], [[TRUNC]]
207*9880d681SAndroid Build Coastguard Worker; CHECK: ret
208*9880d681SAndroid Build Coastguard Workerdefine i8 @oneArgPromotionTruncInsertSeveralUse(i8 %arg1, i8* %base) {
209*9880d681SAndroid Build Coastguard Worker  %add = add nsw i8 %arg1, 1
210*9880d681SAndroid Build Coastguard Worker  %sextadd = sext i8 %add to i64
211*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %sextadd
212*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
213*9880d681SAndroid Build Coastguard Worker  %almostfinalres = add i8 %res, %add
214*9880d681SAndroid Build Coastguard Worker  %finalres = add i8 %almostfinalres, %add
215*9880d681SAndroid Build Coastguard Worker  ret i8 %finalres
216*9880d681SAndroid Build Coastguard Worker}
217*9880d681SAndroid Build Coastguard Worker
218*9880d681SAndroid Build Coastguard Worker; Check that the promoted instruction is used for all uses of the original
219*9880d681SAndroid Build Coastguard Worker; sign extension.
220*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @oneArgPromotionSExtSeveralUse
221*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1SEXT:%[a-zA-Z_0-9-]+]] = sext i8 %arg1 to i64
222*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = add nsw i64 [[ARG1SEXT]], 1
223*9880d681SAndroid Build Coastguard Worker; CHECK: [[GEP:%[a-zA-Z_0-9-]+]] = getelementptr inbounds i8, i8* %base, i64 [[PROMOTED]]
224*9880d681SAndroid Build Coastguard Worker; CHECK: [[LOAD:%[a-zA-Z_0-9-]+]] = load i8, i8* [[GEP]]
225*9880d681SAndroid Build Coastguard Worker; CHECK: [[ADDRES:%[a-zA-Z_0-9-]+]] = zext i8 [[LOAD]] to i64
226*9880d681SAndroid Build Coastguard Worker; CHECK: add i64 [[ADDRES]], [[PROMOTED]]
227*9880d681SAndroid Build Coastguard Worker; CHECK: ret
228*9880d681SAndroid Build Coastguard Workerdefine i64 @oneArgPromotionSExtSeveralUse(i8 %arg1, i8* %base) {
229*9880d681SAndroid Build Coastguard Worker  %add = add nsw i8 %arg1, 1
230*9880d681SAndroid Build Coastguard Worker  %sextadd = sext i8 %add to i64
231*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %sextadd
232*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
233*9880d681SAndroid Build Coastguard Worker  %almostfinalres = zext i8 %res to i64
234*9880d681SAndroid Build Coastguard Worker  %finalres = add i64 %almostfinalres, %sextadd
235*9880d681SAndroid Build Coastguard Worker  ret i64 %finalres
236*9880d681SAndroid Build Coastguard Worker}
237*9880d681SAndroid Build Coastguard Worker
238*9880d681SAndroid Build Coastguard Worker; Check all types of rollback mechanism.
239*9880d681SAndroid Build Coastguard Worker; For this test, the sign extension stays in place.
240*9880d681SAndroid Build Coastguard Worker; However, the matching process goes until promoting both the operands
241*9880d681SAndroid Build Coastguard Worker; of the first promotable add implies.
242*9880d681SAndroid Build Coastguard Worker; At this point the rollback mechanism kicks in and restores the states
243*9880d681SAndroid Build Coastguard Worker; until the addressing mode matcher is able to match something: in that
244*9880d681SAndroid Build Coastguard Worker; case promote nothing.
245*9880d681SAndroid Build Coastguard Worker; Along the way, the promotion mechanism involves:
246*9880d681SAndroid Build Coastguard Worker; - Mutating the type of %promotableadd1 and %promotableadd2.
247*9880d681SAndroid Build Coastguard Worker; - Creating a sext for %arg1 and %arg2.
248*9880d681SAndroid Build Coastguard Worker; - Creating a trunc for a use of %promotableadd1.
249*9880d681SAndroid Build Coastguard Worker; - Replacing a bunch of uses.
250*9880d681SAndroid Build Coastguard Worker; - Setting the operands of the promoted instruction with the promoted values.
251*9880d681SAndroid Build Coastguard Worker; - Moving instruction around (mainly sext when promoting instruction).
252*9880d681SAndroid Build Coastguard Worker; Each type of those promotions has to be undo at least once during this
253*9880d681SAndroid Build Coastguard Worker; specific test.
254*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @twoArgsPromotionNest
255*9880d681SAndroid Build Coastguard Worker; CHECK: [[ORIG:%[a-zA-Z_0-9-]+]] = add nsw i32 %arg1, %arg2
256*9880d681SAndroid Build Coastguard Worker; CHECK: [[ADD:%[a-zA-Z_0-9-]+]] = add nsw i32 [[ORIG]], [[ORIG]]
257*9880d681SAndroid Build Coastguard Worker; CHECK: [[SEXT:%[a-zA-Z_0-9-]+]] = sext i32 [[ADD]] to i64
258*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr inbounds i8, i8* %base, i64 [[SEXT]]
259*9880d681SAndroid Build Coastguard Worker; CHECK: ret
260*9880d681SAndroid Build Coastguard Workerdefine i8 @twoArgsPromotionNest(i32 %arg1, i32 %arg2, i8* %base) {
261*9880d681SAndroid Build Coastguard Worker  %promotableadd1 = add nsw i32 %arg1, %arg2
262*9880d681SAndroid Build Coastguard Worker  %promotableadd2 = add nsw i32 %promotableadd1, %promotableadd1
263*9880d681SAndroid Build Coastguard Worker  %sextadd = sext i32 %promotableadd2 to i64
264*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %sextadd
265*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
266*9880d681SAndroid Build Coastguard Worker  ret i8 %res
267*9880d681SAndroid Build Coastguard Worker}
268*9880d681SAndroid Build Coastguard Worker
269*9880d681SAndroid Build Coastguard Worker; Test the InstructionRemover undo, which was the only one not
270*9880d681SAndroid Build Coastguard Worker; kicked in the previous test.
271*9880d681SAndroid Build Coastguard Worker; The matcher first promotes the add, removes the trunc and promotes
272*9880d681SAndroid Build Coastguard Worker; the sext of arg1.
273*9880d681SAndroid Build Coastguard Worker; Then, the matcher cannot use an addressing mode r + r + r, thus it
274*9880d681SAndroid Build Coastguard Worker; rolls back.
275*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @twoArgsNoPromotionRemove
276*9880d681SAndroid Build Coastguard Worker; CHECK: [[SEXTARG1:%[a-zA-Z_0-9-]+]] = sext i1 %arg1 to i32
277*9880d681SAndroid Build Coastguard Worker; CHECK: [[TRUNC:%[a-zA-Z_0-9-]+]] = trunc i32 [[SEXTARG1]] to i8
278*9880d681SAndroid Build Coastguard Worker; CHECK: [[ADD:%[a-zA-Z_0-9-]+]] = add nsw i8 [[TRUNC]], %arg2
279*9880d681SAndroid Build Coastguard Worker; CHECK: [[SEXT:%[a-zA-Z_0-9-]+]] = sext i8 [[ADD]] to i64
280*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr inbounds i8, i8* %base, i64 [[SEXT]]
281*9880d681SAndroid Build Coastguard Worker; CHECK: ret
282*9880d681SAndroid Build Coastguard Workerdefine i8 @twoArgsNoPromotionRemove(i1 %arg1, i8 %arg2, i8* %base) {
283*9880d681SAndroid Build Coastguard Worker  %sextarg1 = sext i1 %arg1 to i32
284*9880d681SAndroid Build Coastguard Worker  %trunc = trunc i32 %sextarg1 to i8
285*9880d681SAndroid Build Coastguard Worker  %add = add nsw i8 %trunc, %arg2
286*9880d681SAndroid Build Coastguard Worker  %sextadd = sext i8 %add to i64
287*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %sextadd
288*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
289*9880d681SAndroid Build Coastguard Worker  ret i8 %res
290*9880d681SAndroid Build Coastguard Worker}
291*9880d681SAndroid Build Coastguard Worker
292*9880d681SAndroid Build Coastguard Worker; Ensure that when the profitability checks kicks in, the IR is not modified
293*9880d681SAndroid Build Coastguard Worker; will IgnoreProfitability is on.
294*9880d681SAndroid Build Coastguard Worker; The profitabily check happens when a candidate instruction has several uses.
295*9880d681SAndroid Build Coastguard Worker; The matcher will create a new matcher for each use and check if the
296*9880d681SAndroid Build Coastguard Worker; instruction is in the list of the matched instructions of this new matcher.
297*9880d681SAndroid Build Coastguard Worker; All changes made by the new matchers must be dropped before pursuing
298*9880d681SAndroid Build Coastguard Worker; otherwise the state of the original matcher will be wrong.
299*9880d681SAndroid Build Coastguard Worker;
300*9880d681SAndroid Build Coastguard Worker; Without the profitability check, when checking for the second use of
301*9880d681SAndroid Build Coastguard Worker; arrayidx, the matcher promotes everything all the way to %arg1, %arg2.
302*9880d681SAndroid Build Coastguard Worker; Check that we did not promote anything in the final matching.
303*9880d681SAndroid Build Coastguard Worker;
304*9880d681SAndroid Build Coastguard Worker; <rdar://problem/16020230>
305*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @checkProfitability
306*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: {{%[a-zA-Z_0-9-]+}} = sext i32 %arg1 to i64
307*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: {{%[a-zA-Z_0-9-]+}} = sext i32 %arg2 to i64
308*9880d681SAndroid Build Coastguard Worker; CHECK: [[SHL:%[a-zA-Z_0-9-]+]] = shl nsw i32 %arg1, 1
309*9880d681SAndroid Build Coastguard Worker; CHECK: [[ADD:%[a-zA-Z_0-9-]+]] = add nsw i32 [[SHL]], %arg2
310*9880d681SAndroid Build Coastguard Worker; CHECK: [[SEXTADD:%[a-zA-Z_0-9-]+]] = sext i32 [[ADD]] to i64
311*9880d681SAndroid Build Coastguard Worker; BB then
312*9880d681SAndroid Build Coastguard Worker; CHECK: [[BASE1:%[a-zA-Z_0-9-]+]] = add i64 [[SEXTADD]], 48
313*9880d681SAndroid Build Coastguard Worker; CHECK: [[ADDR1:%[a-zA-Z_0-9-]+]] = inttoptr i64 [[BASE1]] to i32*
314*9880d681SAndroid Build Coastguard Worker; CHECK: load i32, i32* [[ADDR1]]
315*9880d681SAndroid Build Coastguard Worker; BB else
316*9880d681SAndroid Build Coastguard Worker; CHECK: [[BASE2:%[a-zA-Z_0-9-]+]] = add i64 [[SEXTADD]], 48
317*9880d681SAndroid Build Coastguard Worker; CHECK: [[ADDR2:%[a-zA-Z_0-9-]+]] = inttoptr i64 [[BASE2]] to i32*
318*9880d681SAndroid Build Coastguard Worker; CHECK: load i32, i32* [[ADDR2]]
319*9880d681SAndroid Build Coastguard Worker; CHECK: ret
320*9880d681SAndroid Build Coastguard Worker; CHECK-GEP-LABEL: @checkProfitability
321*9880d681SAndroid Build Coastguard Worker; CHECK-GEP-NOT: {{%[a-zA-Z_0-9-]+}} = sext i32 %arg1 to i64
322*9880d681SAndroid Build Coastguard Worker; CHECK-GEP-NOT: {{%[a-zA-Z_0-9-]+}} = sext i32 %arg2 to i64
323*9880d681SAndroid Build Coastguard Worker; CHECK-GEP: [[SHL:%[a-zA-Z_0-9-]+]] = shl nsw i32 %arg1, 1
324*9880d681SAndroid Build Coastguard Worker; CHECK-GEP: [[ADD:%[a-zA-Z_0-9-]+]] = add nsw i32 [[SHL]], %arg2
325*9880d681SAndroid Build Coastguard Worker; CHECK-GEP: [[SEXTADD:%[a-zA-Z_0-9-]+]] = sext i32 [[ADD]] to i64
326*9880d681SAndroid Build Coastguard Worker; BB then
327*9880d681SAndroid Build Coastguard Worker; CHECK-GEP: [[BASE1:%[a-zA-Z_0-9-]+]] = inttoptr i64 [[SEXTADD]] to i32*
328*9880d681SAndroid Build Coastguard Worker; CHECK-GEP: [[BCC1:%[a-zA-Z_0-9-]+]] = bitcast i32* [[BASE1]] to i8*
329*9880d681SAndroid Build Coastguard Worker; CHECK-GEP: [[FULL1:%[a-zA-Z_0-9-]+]] = getelementptr i8, i8* [[BCC1]], i64 48
330*9880d681SAndroid Build Coastguard Worker; CHECK-GEP: [[ADDR1:%[a-zA-Z_0-9-]+]] = bitcast i8* [[FULL1]] to i32*
331*9880d681SAndroid Build Coastguard Worker; CHECK-GEP: load i32, i32* [[ADDR1]]
332*9880d681SAndroid Build Coastguard Worker; BB else
333*9880d681SAndroid Build Coastguard Worker; CHECK-GEP: [[BASE2:%[a-zA-Z_0-9-]+]] = inttoptr i64 [[SEXTADD]] to i32*
334*9880d681SAndroid Build Coastguard Worker; CHECK-GEP: [[BCC2:%[a-zA-Z_0-9-]+]] = bitcast i32* [[BASE2]] to i8*
335*9880d681SAndroid Build Coastguard Worker; CHECK-GEP: [[FULL2:%[a-zA-Z_0-9-]+]] = getelementptr i8, i8* [[BCC2]], i64 48
336*9880d681SAndroid Build Coastguard Worker; CHECK-GEP: [[ADDR2:%[a-zA-Z_0-9-]+]] = bitcast i8* [[FULL2]] to i32*
337*9880d681SAndroid Build Coastguard Worker; CHECK-GEP: load i32, i32* [[ADDR2]]
338*9880d681SAndroid Build Coastguard Worker; CHECK-GEP: ret
339*9880d681SAndroid Build Coastguard Workerdefine i32 @checkProfitability(i32 %arg1, i32 %arg2, i1 %test) {
340*9880d681SAndroid Build Coastguard Worker  %shl = shl nsw i32 %arg1, 1
341*9880d681SAndroid Build Coastguard Worker  %add1 = add nsw i32 %shl, %arg2
342*9880d681SAndroid Build Coastguard Worker  %sextidx1 = sext i32 %add1 to i64
343*9880d681SAndroid Build Coastguard Worker  %tmpptr = inttoptr i64 %sextidx1 to i32*
344*9880d681SAndroid Build Coastguard Worker  %arrayidx1 = getelementptr i32, i32* %tmpptr, i64 12
345*9880d681SAndroid Build Coastguard Worker  br i1 %test, label %then, label %else
346*9880d681SAndroid Build Coastguard Workerthen:
347*9880d681SAndroid Build Coastguard Worker  %res1 = load i32, i32* %arrayidx1
348*9880d681SAndroid Build Coastguard Worker  br label %end
349*9880d681SAndroid Build Coastguard Workerelse:
350*9880d681SAndroid Build Coastguard Worker  %res2 = load i32, i32* %arrayidx1
351*9880d681SAndroid Build Coastguard Worker  br label %end
352*9880d681SAndroid Build Coastguard Workerend:
353*9880d681SAndroid Build Coastguard Worker  %tmp = phi i32 [%res1, %then], [%res2, %else]
354*9880d681SAndroid Build Coastguard Worker  %res = add i32 %tmp, %add1
355*9880d681SAndroid Build Coastguard Worker  %addr = inttoptr i32 %res to i32*
356*9880d681SAndroid Build Coastguard Worker  %final = load i32, i32* %addr
357*9880d681SAndroid Build Coastguard Worker  ret i32 %final
358*9880d681SAndroid Build Coastguard Worker}
359*9880d681SAndroid Build Coastguard Worker
360*9880d681SAndroid Build Coastguard Worker%struct.dns_packet = type { i32, i32, %union.anon }
361*9880d681SAndroid Build Coastguard Worker%union.anon = type { i32 }
362*9880d681SAndroid Build Coastguard Worker
363*9880d681SAndroid Build Coastguard Worker@a = common global i32 0, align 4
364*9880d681SAndroid Build Coastguard Worker@b = common global i16 0, align 2
365*9880d681SAndroid Build Coastguard Worker
366*9880d681SAndroid Build Coastguard Worker; We used to crash on this function because we did not return the right
367*9880d681SAndroid Build Coastguard Worker; promoted instruction for %conv.i.
368*9880d681SAndroid Build Coastguard Worker; Make sure we generate the right code now.
369*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @fn3
370*9880d681SAndroid Build Coastguard Worker; %conv.i is used twice and only one of its use is being promoted.
371*9880d681SAndroid Build Coastguard Worker; Use it at the starting point for the matching.
372*9880d681SAndroid Build Coastguard Worker; CHECK: %conv.i = zext i16 [[PLAIN_OPND:%[.a-zA-Z_0-9-]+]] to i32
373*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: [[PROMOTED_CONV:%[.a-zA-Z_0-9-]+]] = zext i16 [[PLAIN_OPND]] to i64
374*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: [[BASE:%[a-zA-Z_0-9-]+]] = ptrtoint %struct.dns_packet* %P to i64
375*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: [[ADD:%[a-zA-Z_0-9-]+]] = add i64 [[BASE]], [[PROMOTED_CONV]]
376*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: [[ADDR:%[a-zA-Z_0-9-]+]] = add i64 [[ADD]], 7
377*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: [[CAST:%[a-zA-Z_0-9-]+]] = inttoptr i64 [[ADDR]] to i8*
378*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: load i8, i8* [[CAST]], align 1
379*9880d681SAndroid Build Coastguard Workerdefine signext i16 @fn3(%struct.dns_packet* nocapture readonly %P) {
380*9880d681SAndroid Build Coastguard Workerentry:
381*9880d681SAndroid Build Coastguard Worker  %tmp = getelementptr inbounds %struct.dns_packet, %struct.dns_packet* %P, i64 0, i32 2
382*9880d681SAndroid Build Coastguard Worker  %data.i.i = bitcast %union.anon* %tmp to [0 x i8]*
383*9880d681SAndroid Build Coastguard Worker  br label %while.body.i.i
384*9880d681SAndroid Build Coastguard Worker
385*9880d681SAndroid Build Coastguard Workerwhile.body.i.i:                                   ; preds = %while.body.i.i, %entry
386*9880d681SAndroid Build Coastguard Worker  %src.addr.0.i.i = phi i16 [ 0, %entry ], [ %inc.i.i, %while.body.i.i ]
387*9880d681SAndroid Build Coastguard Worker  %inc.i.i = add i16 %src.addr.0.i.i, 1
388*9880d681SAndroid Build Coastguard Worker  %idxprom.i.i = sext i16 %src.addr.0.i.i to i64
389*9880d681SAndroid Build Coastguard Worker  %arrayidx.i.i = getelementptr inbounds [0 x i8], [0 x i8]* %data.i.i, i64 0, i64 %idxprom.i.i
390*9880d681SAndroid Build Coastguard Worker  %tmp1 = load i8, i8* %arrayidx.i.i, align 1
391*9880d681SAndroid Build Coastguard Worker  %conv2.i.i = zext i8 %tmp1 to i32
392*9880d681SAndroid Build Coastguard Worker  %and.i.i = and i32 %conv2.i.i, 15
393*9880d681SAndroid Build Coastguard Worker  store i32 %and.i.i, i32* @a, align 4
394*9880d681SAndroid Build Coastguard Worker  %tobool.i.i = icmp eq i32 %and.i.i, 0
395*9880d681SAndroid Build Coastguard Worker  br i1 %tobool.i.i, label %while.body.i.i, label %fn1.exit.i
396*9880d681SAndroid Build Coastguard Worker
397*9880d681SAndroid Build Coastguard Workerfn1.exit.i:                                       ; preds = %while.body.i.i
398*9880d681SAndroid Build Coastguard Worker  %inc.i.i.lcssa = phi i16 [ %inc.i.i, %while.body.i.i ]
399*9880d681SAndroid Build Coastguard Worker  %conv.i = zext i16 %inc.i.i.lcssa to i32
400*9880d681SAndroid Build Coastguard Worker  %sub.i = add nsw i32 %conv.i, -1
401*9880d681SAndroid Build Coastguard Worker  %idxprom.i = sext i32 %sub.i to i64
402*9880d681SAndroid Build Coastguard Worker  %arrayidx.i = getelementptr inbounds [0 x i8], [0 x i8]* %data.i.i, i64 0, i64 %idxprom.i
403*9880d681SAndroid Build Coastguard Worker  %tmp2 = load i8, i8* %arrayidx.i, align 1
404*9880d681SAndroid Build Coastguard Worker  %conv2.i = sext i8 %tmp2 to i16
405*9880d681SAndroid Build Coastguard Worker  store i16 %conv2.i, i16* @b, align 2
406*9880d681SAndroid Build Coastguard Worker  %sub4.i = sub nsw i32 0, %conv.i
407*9880d681SAndroid Build Coastguard Worker  %conv5.i = zext i16 %conv2.i to i32
408*9880d681SAndroid Build Coastguard Worker  %cmp.i = icmp sgt i32 %conv5.i, %sub4.i
409*9880d681SAndroid Build Coastguard Worker  br i1 %cmp.i, label %if.then.i, label %fn2.exit
410*9880d681SAndroid Build Coastguard Worker
411*9880d681SAndroid Build Coastguard Workerif.then.i:                                        ; preds = %fn1.exit.i
412*9880d681SAndroid Build Coastguard Worker  %end.i = getelementptr inbounds %struct.dns_packet, %struct.dns_packet* %P, i64 0, i32 1
413*9880d681SAndroid Build Coastguard Worker  %tmp3 = load i32, i32* %end.i, align 4
414*9880d681SAndroid Build Coastguard Worker  %sub7.i = add i32 %tmp3, 65535
415*9880d681SAndroid Build Coastguard Worker  %conv8.i = trunc i32 %sub7.i to i16
416*9880d681SAndroid Build Coastguard Worker  br label %fn2.exit
417*9880d681SAndroid Build Coastguard Worker
418*9880d681SAndroid Build Coastguard Workerfn2.exit:                                         ; preds = %if.then.i, %fn1.exit.i
419*9880d681SAndroid Build Coastguard Worker  %retval.0.i = phi i16 [ %conv8.i, %if.then.i ], [ undef, %fn1.exit.i ]
420*9880d681SAndroid Build Coastguard Worker  ret i16 %retval.0.i
421*9880d681SAndroid Build Coastguard Worker}
422*9880d681SAndroid Build Coastguard Worker
423*9880d681SAndroid Build Coastguard Worker; Check that we do not promote an extension if the non-wrapping flag does not
424*9880d681SAndroid Build Coastguard Worker; match the kind of the extension.
425*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @noPromotionFlag
426*9880d681SAndroid Build Coastguard Worker; CHECK: [[ADD:%[a-zA-Z_0-9-]+]] = add nsw i32 %arg1, %arg2
427*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = zext i32 [[ADD]] to i64
428*9880d681SAndroid Build Coastguard Worker; CHECK: inttoptr i64 [[PROMOTED]] to i8*
429*9880d681SAndroid Build Coastguard Worker; CHECK: ret
430*9880d681SAndroid Build Coastguard Workerdefine i8 @noPromotionFlag(i32 %arg1, i32 %arg2) {
431*9880d681SAndroid Build Coastguard Worker  %add = add nsw i32 %arg1, %arg2
432*9880d681SAndroid Build Coastguard Worker  %zextadd = zext i32 %add to i64
433*9880d681SAndroid Build Coastguard Worker  %base = inttoptr i64 %zextadd to i8*
434*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %base
435*9880d681SAndroid Build Coastguard Worker  ret i8 %res
436*9880d681SAndroid Build Coastguard Worker}
437*9880d681SAndroid Build Coastguard Worker
438*9880d681SAndroid Build Coastguard Worker; Check that we correctly promote both operands of the promotable add with zext.
439*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @twoArgsPromotionZExt
440*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1ZEXT:%[a-zA-Z_0-9-]+]] = zext i32 %arg1 to i64
441*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG2ZEXT:%[a-zA-Z_0-9-]+]] = zext i32 %arg2 to i64
442*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = add nuw i64 [[ARG1ZEXT]], [[ARG2ZEXT]]
443*9880d681SAndroid Build Coastguard Worker; CHECK: inttoptr i64 [[PROMOTED]] to i8*
444*9880d681SAndroid Build Coastguard Worker; CHECK: ret
445*9880d681SAndroid Build Coastguard Workerdefine i8 @twoArgsPromotionZExt(i32 %arg1, i32 %arg2) {
446*9880d681SAndroid Build Coastguard Worker  %add = add nuw i32 %arg1, %arg2
447*9880d681SAndroid Build Coastguard Worker  %zextadd = zext i32 %add to i64
448*9880d681SAndroid Build Coastguard Worker  %base = inttoptr i64 %zextadd to i8*
449*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %base
450*9880d681SAndroid Build Coastguard Worker  ret i8 %res
451*9880d681SAndroid Build Coastguard Worker}
452*9880d681SAndroid Build Coastguard Worker
453*9880d681SAndroid Build Coastguard Worker; Check that we correctly promote constant arguments.
454*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @oneArgPromotionNegativeCstZExt
455*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1ZEXT:%[a-zA-Z_0-9-]+]] = zext i8 %arg1 to i64
456*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = add nuw i64 [[ARG1ZEXT]], 255
457*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr inbounds i8, i8* %base, i64 [[PROMOTED]]
458*9880d681SAndroid Build Coastguard Worker; CHECK: ret
459*9880d681SAndroid Build Coastguard Workerdefine i8 @oneArgPromotionNegativeCstZExt(i8 %arg1, i8* %base) {
460*9880d681SAndroid Build Coastguard Worker  %add = add nuw i8 %arg1, -1
461*9880d681SAndroid Build Coastguard Worker  %zextadd = zext i8 %add to i64
462*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %zextadd
463*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
464*9880d681SAndroid Build Coastguard Worker  ret i8 %res
465*9880d681SAndroid Build Coastguard Worker}
466*9880d681SAndroid Build Coastguard Worker
467*9880d681SAndroid Build Coastguard Worker; Check that we are able to merge two zero extensions.
468*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @oneArgPromotionZExtZExt
469*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1ZEXT:%[a-zA-Z_0-9-]+]] = zext i8 %arg1 to i64
470*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = add nuw i64 [[ARG1ZEXT]], 1
471*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr inbounds i8, i8* %base, i64 [[PROMOTED]]
472*9880d681SAndroid Build Coastguard Worker; CHECK: ret
473*9880d681SAndroid Build Coastguard Workerdefine i8 @oneArgPromotionZExtZExt(i8 %arg1, i8* %base) {
474*9880d681SAndroid Build Coastguard Worker  %zext = zext i8 %arg1 to i32
475*9880d681SAndroid Build Coastguard Worker  %add = add nuw i32 %zext, 1
476*9880d681SAndroid Build Coastguard Worker  %zextadd = zext i32 %add to i64
477*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %zextadd
478*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
479*9880d681SAndroid Build Coastguard Worker  ret i8 %res
480*9880d681SAndroid Build Coastguard Worker}
481*9880d681SAndroid Build Coastguard Worker
482*9880d681SAndroid Build Coastguard Worker; Check that we do not promote truncate when the dropped bits
483*9880d681SAndroid Build Coastguard Worker; are of a different kind.
484*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @oneArgPromotionBlockTruncZExt
485*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1SEXT:%[a-zA-Z_0-9-]+]] = sext i1 %arg1 to i32
486*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1TRUNC:%[a-zA-Z_0-9-]+]] = trunc i32 [[ARG1SEXT]] to i8
487*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1ZEXT:%[a-zA-Z_0-9-]+]] = zext i8 [[ARG1TRUNC]] to i64
488*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = add nuw i64 [[ARG1ZEXT]], 1
489*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr inbounds i8, i8* %base, i64 [[PROMOTED]]
490*9880d681SAndroid Build Coastguard Worker; CHECK: ret
491*9880d681SAndroid Build Coastguard Workerdefine i8 @oneArgPromotionBlockTruncZExt(i1 %arg1, i8* %base) {
492*9880d681SAndroid Build Coastguard Worker  %sextarg1 = sext i1 %arg1 to i32
493*9880d681SAndroid Build Coastguard Worker  %trunc = trunc i32 %sextarg1 to i8
494*9880d681SAndroid Build Coastguard Worker  %add = add nuw i8 %trunc, 1
495*9880d681SAndroid Build Coastguard Worker  %zextadd = zext i8 %add to i64
496*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %zextadd
497*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
498*9880d681SAndroid Build Coastguard Worker  ret i8 %res
499*9880d681SAndroid Build Coastguard Worker}
500*9880d681SAndroid Build Coastguard Worker
501*9880d681SAndroid Build Coastguard Worker; Check that we are able to promote truncate when we know all the bits
502*9880d681SAndroid Build Coastguard Worker; that are dropped.
503*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @oneArgPromotionPassTruncZExt
504*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1ZEXT:%[a-zA-Z_0-9-]+]] = zext i1 %arg1 to i64
505*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = add nuw i64 [[ARG1ZEXT]], 1
506*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr inbounds i8, i8* %base, i64 [[PROMOTED]]
507*9880d681SAndroid Build Coastguard Worker; CHECK: ret
508*9880d681SAndroid Build Coastguard Workerdefine i8 @oneArgPromotionPassTruncZExt(i1 %arg1, i8* %base) {
509*9880d681SAndroid Build Coastguard Worker  %sextarg1 = zext i1 %arg1 to i32
510*9880d681SAndroid Build Coastguard Worker  %trunc = trunc i32 %sextarg1 to i8
511*9880d681SAndroid Build Coastguard Worker  %add = add nuw i8 %trunc, 1
512*9880d681SAndroid Build Coastguard Worker  %zextadd = zext i8 %add to i64
513*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %zextadd
514*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
515*9880d681SAndroid Build Coastguard Worker  ret i8 %res
516*9880d681SAndroid Build Coastguard Worker}
517*9880d681SAndroid Build Coastguard Worker
518*9880d681SAndroid Build Coastguard Worker; Check that we do not promote sext with zext.
519*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @oneArgPromotionBlockSExtZExt
520*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1SEXT:%[a-zA-Z_0-9-]+]] = sext i1 %arg1 to i8
521*9880d681SAndroid Build Coastguard Worker; CHECK: [[ARG1ZEXT:%[a-zA-Z_0-9-]+]] = zext i8 [[ARG1SEXT]] to i64
522*9880d681SAndroid Build Coastguard Worker; CHECK: [[PROMOTED:%[a-zA-Z_0-9-]+]] = add nuw i64 [[ARG1ZEXT]], 1
523*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr inbounds i8, i8* %base, i64 [[PROMOTED]]
524*9880d681SAndroid Build Coastguard Worker; CHECK: ret
525*9880d681SAndroid Build Coastguard Workerdefine i8 @oneArgPromotionBlockSExtZExt(i1 %arg1, i8* %base) {
526*9880d681SAndroid Build Coastguard Worker  %sextarg1 = sext i1 %arg1 to i8
527*9880d681SAndroid Build Coastguard Worker  %add = add nuw i8 %sextarg1, 1
528*9880d681SAndroid Build Coastguard Worker  %zextadd = zext i8 %add to i64
529*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds i8, i8* %base, i64 %zextadd
530*9880d681SAndroid Build Coastguard Worker  %res = load i8, i8* %arrayidx
531*9880d681SAndroid Build Coastguard Worker  ret i8 %res
532*9880d681SAndroid Build Coastguard Worker}
533