1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; REQUIRES: x86-registered-target
3 ; RUN: opt < %s -passes=rel-lookup-table-converter -relocation-model=pic -S | FileCheck %s
4 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
5 target triple = "x86_64-unknown-linux-gnu"
7 @.str = private unnamed_addr constant [5 x i8] c"zero\00", align 1
8 @.str.1 = private unnamed_addr constant [4 x i8] c"one\00", align 1
9 @.str.2 = private unnamed_addr constant [4 x i8] c"two\00", align 1
10 @.str.3 = private unnamed_addr constant [8 x i8] c"default\00", align 1
11 @.str.4 = private unnamed_addr constant [6 x i8] c"three\00", align 1
12 @.str.5 = private unnamed_addr constant [5 x i8] c"str1\00", align 1
13 @.str.6 = private unnamed_addr constant [5 x i8] c"str2\00", align 1
14 @.str.7 = private unnamed_addr constant [12 x i8] c"singlevalue\00", align 1
15 @.str.8 = private unnamed_addr constant [2 x i8] c"a\00", align 1
16 @.str.9 = private unnamed_addr constant [2 x i8] c"b\00", align 1
17 @.str.10 = private unnamed_addr constant [2 x i8] c"c\00", align 1
19 @a1 = external global i32, align 4
20 @b1 = external global i32, align 4
21 @c1 = external global i32, align 4
22 @d1 = external global i32, align 4
24 @a2 = internal constant i32 0, align 4
25 @b2 = internal constant i32 0, align 4
26 @c2 = internal constant i32 0, align 4
27 @d2 = internal constant i32 0, align 4
29 @switch.table.external_linkage = private unnamed_addr constant [3 x ptr] [ptr @a1, ptr @b1, ptr @c1], align 8
31 @switch.table.internal_linkage = private unnamed_addr constant [3 x ptr] [ptr @a2, ptr @b2, ptr @c2], align 8
33 @switch.table.string_table = private unnamed_addr constant [3 x ptr]
40 @switch.table.string_table_holes = private unnamed_addr constant [4 x ptr]
48 @switch.table.single_value = private unnamed_addr constant [3 x ptr]
55 @user_defined_lookup_table.table = internal unnamed_addr constant [3 x ptr]
62 @table = internal constant [2 x ptr] [
67 @table2 = internal constant [2 x ptr] [
72 ; Lookup table check for integer pointers that have external linkage
73 ; CHECK: @switch.table.external_linkage = private unnamed_addr constant [3 x ptr] [ptr @a1, ptr @b1, ptr @c1], align
75 ; Lookup table check for integer pointers that have internal linkage
76 ; CHECK: @switch.table.internal_linkage.rel = private unnamed_addr constant [3 x i32]
78 ; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr @a2 to i64), i64 ptrtoint (ptr @switch.table.internal_linkage.rel to i64)) to i32),
79 ; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr @b2 to i64), i64 ptrtoint (ptr @switch.table.internal_linkage.rel to i64)) to i32),
80 ; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr @c2 to i64), i64 ptrtoint (ptr @switch.table.internal_linkage.rel to i64)) to i32)
81 ; CHECK-SAME: ], align 4
83 ; Relative switch lookup table for strings
84 ; CHECK: @switch.table.string_table.rel = private unnamed_addr constant [3 x i32]
86 ; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr @.str to i64), i64 ptrtoint (ptr @switch.table.string_table.rel to i64)) to i32),
87 ; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr @.str.1 to i64), i64 ptrtoint (ptr @switch.table.string_table.rel to i64)) to i32),
88 ; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr @.str.2 to i64), i64 ptrtoint (ptr @switch.table.string_table.rel to i64)) to i32)
89 ; CHECK-SAME: ], align 4
91 ; Relative switch lookup table for strings with holes, where holes are filled with relative offset to default values
92 ; CHECK: @switch.table.string_table_holes.rel = private unnamed_addr constant [4 x i32]
94 ; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr @.str to i64), i64 ptrtoint (ptr @switch.table.string_table_holes.rel to i64)) to i32),
95 ; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr @.str.3 to i64), i64 ptrtoint (ptr @switch.table.string_table_holes.rel to i64)) to i32),
96 ; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr @.str.2 to i64), i64 ptrtoint (ptr @switch.table.string_table_holes.rel to i64)) to i32),
97 ; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr @.str.4 to i64), i64 ptrtoint (ptr @switch.table.string_table_holes.rel to i64)) to i32)
98 ; CHECK-SAME: ], align 4
101 ; CHECK: @switch.table.single_value.rel = private unnamed_addr constant [3 x i32]
103 ; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr @.str to i64), i64 ptrtoint (ptr @switch.table.single_value.rel to i64)) to i32),
104 ; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr @.str.1 to i64), i64 ptrtoint (ptr @switch.table.single_value.rel to i64)) to i32),
105 ; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr @.str.2 to i64), i64 ptrtoint (ptr @switch.table.single_value.rel to i64)) to i32)
106 ; CHECK-SAME: ], align 4
109 ; Relative lookup table for the loop hoist check test
110 ; CHECK: @table.rel = internal unnamed_addr constant [2 x i32]
112 ; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr @.str.8 to i64), i64 ptrtoint (ptr @table.rel to i64)) to i32),
113 ; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr @.str.9 to i64), i64 ptrtoint (ptr @table.rel to i64)) to i32)
114 ; CHECK-SAME: ], align 4
116 ; Relative look up table for the test where gep is not immediately followed by a load check
117 ; CHECK: @table2.rel = internal unnamed_addr constant [2 x i32]
119 ; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr @.str.8 to i64), i64 ptrtoint (ptr @table2.rel to i64)) to i32),
120 ; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr @.str.9 to i64), i64 ptrtoint (ptr @table2.rel to i64)) to i32)
121 ; CHECK-SAME: ], align 4
123 ; Lookup table check for integer pointers that have external linkage
124 define ptr @external_linkage(i32 %cond) {
125 ; CHECK-LABEL: @external_linkage(
127 ; CHECK-NEXT: [[TMP0:%.*]] = icmp ult i32 [[COND:%.*]], 3
128 ; CHECK-NEXT: br i1 [[TMP0]], label [[SWITCH_LOOKUP:%.*]], label [[RETURN:%.*]]
129 ; CHECK: switch.lookup:
130 ; CHECK-NEXT: [[SWITCH_GEP:%.*]] = getelementptr inbounds [3 x ptr], ptr @switch.table.external_linkage, i32 0, i32 [[COND:%.*]]
131 ; CHECK-NEXT: [[SWITCH_LOAD:%.*]] = load ptr, ptr [[SWITCH_GEP]], align 8
132 ; CHECK-NEXT: ret ptr [[SWITCH_LOAD]]
134 ; CHECK-NEXT: ret ptr @d1
137 %0 = icmp ult i32 %cond, 3
138 br i1 %0, label %switch.lookup, label %return
140 switch.lookup: ; preds = %entry
141 %switch.gep = getelementptr inbounds [3 x ptr], ptr @switch.table.external_linkage, i32 0, i32 %cond
142 %switch.load = load ptr, ptr %switch.gep, align 8
145 return: ; preds = %entry
149 ; Relative switch lookup table for integer pointers that have internal linkage
150 define ptr @internal_linkage(i32 %cond) {
151 ; CHECK-LABEL: @internal_linkage(
153 ; CHECK-NEXT: [[TMP0:%.*]] = icmp ult i32 [[COND:%.*]], 3
154 ; CHECK-NEXT: br i1 [[TMP0]], label [[SWITCH_LOOKUP:%.*]], label [[RETURN:%.*]]
155 ; CHECK: switch.lookup:
156 ; CHECK-NEXT: [[RELTABLE_SHIFT:%.*]] = shl i32 %cond, 2
157 ; CHECK-NEXT: [[RELTABLE_INTRINSIC:%.*]] = call ptr @llvm.load.relative.i32(ptr @switch.table.internal_linkage.rel, i32 [[RELTABLE_SHIFT]])
158 ; CHECK-NEXT: ret ptr [[RELTABLE_INTRINSIC]]
160 ; CHECK-NEXT: ret ptr @d2
163 %0 = icmp ult i32 %cond, 3
164 br i1 %0, label %switch.lookup, label %return
166 switch.lookup: ; preds = %entry
167 %switch.gep = getelementptr inbounds [3 x ptr], ptr @switch.table.internal_linkage, i32 0, i32 %cond
168 %switch.load = load ptr, ptr %switch.gep, align 8
171 return: ; preds = %entry
175 ; ; Relative switch lookup table for strings
176 define ptr @string_table(i32 %cond) {
177 ; CHECK-LABEL: @string_table(
179 ; CHECK-NEXT: [[TMP0:%.*]] = icmp ult i32 [[COND:%.*]], 3
180 ; CHECK-NEXT: br i1 [[TMP0]], label [[SWITCH_LOOKUP:%.*]], label [[RETURN:%.*]]
181 ; CHECK: switch.lookup:
182 ; CHECK-NEXT: [[RELTABLE_SHIFT:%.*]] = shl i32 %cond, 2
183 ; CHECK-NEXT: [[RELTABLE_INTRINSIC:%.*]] = call ptr @llvm.load.relative.i32(ptr @switch.table.string_table.rel, i32 [[RELTABLE_SHIFT]])
184 ; CHECK-NEXT: ret ptr [[RELTABLE_INTRINSIC]]
186 ; CHECK-NEXT: ret ptr @.str.3
189 %0 = icmp ult i32 %cond, 3
190 br i1 %0, label %switch.lookup, label %return
192 switch.lookup: ; preds = %entry
193 %switch.gep = getelementptr inbounds [3 x ptr], ptr @switch.table.string_table, i32 0, i32 %cond
194 %switch.load = load ptr, ptr %switch.gep, align 8
197 return: ; preds = %entry
201 ; Relative switch lookup table for strings with holes, where holes are filled with relative offset to default values
202 define ptr @string_table_holes(i32 %cond) {
203 ; CHECK-LABEL: @string_table_holes(
205 ; CHECK-NEXT: [[TMP0:%.*]] = icmp ult i32 [[COND:%.*]], 4
206 ; CHECK-NEXT: br i1 [[TMP0]], label [[SWITCH_LOOKUP:%.*]], label [[RETURN:%.*]]
207 ; CHECK: switch.lookup:
208 ; CHECK-NEXT: [[RELTABLE_SHIFT:%.*]] = shl i32 [[COND]], 2
209 ; CHECK-NEXT: [[RELTABLE_INTRINSIC:%.*]] = call ptr @llvm.load.relative.i32(ptr @switch.table.string_table_holes.rel, i32 [[RELTABLE_SHIFT]])
210 ; CHECK-NEXT: ret ptr [[RELTABLE_INTRINSIC]]
212 ; CHECK-NEXT: ret ptr @.str.3
215 %0 = icmp ult i32 %cond, 4
216 br i1 %0, label %switch.lookup, label %return
218 switch.lookup: ; preds = %entry
219 %switch.gep = getelementptr inbounds [4 x ptr], ptr @switch.table.string_table_holes, i32 0, i32 %cond
220 %switch.load = load ptr, ptr %switch.gep, align 8
223 return: ; preds = %entry
229 ; If there is a lookup table, where each element contains the same value,
230 ; a relative lookup should not be generated
231 define void @single_value(i32 %cond) {
232 ; CHECK-LABEL: @single_value(
234 ; CHECK-NEXT: [[TMP0:%.*]] = icmp ult i32 [[COND:%.*]], 3
235 ; CHECK-NEXT: br i1 [[TMP0]], label [[SWITCH_LOOKUP:%.*]], label [[RETURN:%.*]]
236 ; CHECK: switch.lookup:
237 ; CHECK-NEXT: [[RELTABLE_SHIFT:%.*]] = shl i32 [[COND]], 2
238 ; CHECK-NEXT: [[RELTABLE_INTRINSIC:%.*]] = call ptr @llvm.load.relative.i32(ptr @switch.table.single_value.rel, i32 [[RELTABLE_SHIFT]])
240 ; CHECK-NEXT: [[STR1:%.*]] = phi ptr [ @.str.5, %entry ], [ @.str.7, %switch.lookup ]
241 ; CHECK-NEXT: [[STR2:%.*]] = phi ptr [ @.str.6, %entry ], [ [[RELTABLE_INTRINSIC]], [[SWITCH_LOOKUP]] ]
242 ; CHECK-NEXT: ret void
245 %0 = icmp ult i32 %cond, 3
246 br i1 %0, label %switch.lookup, label %sw.epilog
248 switch.lookup: ; preds = %entry
249 %switch.gep = getelementptr inbounds [3 x ptr], ptr @switch.table.single_value, i32 0, i32 %cond
250 %switch.load = load ptr, ptr %switch.gep, align 8
253 sw.epilog: ; preds = %switch.lookup, %entry
254 %str1.0 = phi ptr [ @.str.5, %entry ], [ @.str.7, %switch.lookup ]
255 %str2.0 = phi ptr [ @.str.6, %entry ], [ %switch.load, %switch.lookup ]
259 ; Relative lookup table generated for a user-defined lookup table
260 define ptr @user_defined_lookup_table(i32 %cond) {
261 ; CHECK-LABEL: @user_defined_lookup_table(
263 ; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i32 [[COND:%.*]], 3
264 ; CHECK-NEXT: br i1 [[TMP0]], label [[SWITCH_LOOKUP:%.*]], label [[RETURN:%.*]]
266 ; CHECK-NEXT: [[IDX_PROM:%.*]] = sext i32 [[COND]] to i64
267 ; CHECK-NEXT: [[RELTABLE_SHIFT:%.*]] = shl i64 [[IDX_PROM]], 2
268 ; CHECK-NEXT: [[RELTABLE_INTRINSIC:%.*]] = call ptr @llvm.load.relative.i64(ptr @user_defined_lookup_table.table.rel, i64 [[RELTABLE_SHIFT]])
269 ; CHECK-NEXT: br label %cond.end
271 ; CHECK-NEXT: [[COND1:%.*]] = phi ptr [ [[RELTABLE_INTRINSIC]], %cond.false ], [ @.str.3, %entry ]
272 ; CHECK-NEXT: ret ptr [[COND1]]
275 %cmp = icmp sgt i32 %cond, 3
276 br i1 %cmp, label %cond.end, label %cond.false
278 cond.false: ; preds = %entry
279 %idxprom = sext i32 %cond to i64
280 %arrayidx = getelementptr inbounds [3 x ptr], ptr @user_defined_lookup_table.table, i64 0, i64 %idxprom
281 %0 = load ptr, ptr %arrayidx, align 8, !tbaa !4
284 cond.end: ; preds = %entry, %cond.false
285 %cond1 = phi ptr [ %0, %cond.false ], [ @.str.3, %entry ]
289 ; Check to ensure that call @llvm.load.relative is inserted before load, not before gep.
290 ; When a lookup table is accessed inside a loop, and a gep is hosted outside the loop via licm,
291 ; make sure that call @llvm.load.relative is inserted before load.
292 define ptr @loop_hoist(i32 %x) {
293 ; CHECK-LABEL: @loop_hoist(i32 %x)
295 ; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i32 [[X:%.*]], 1
296 ; CHECK-NEXT: [[RELTABLE_SHIFT:%.*]] = shl i32 [[X:%.*]], 2
297 ; CHECK-NEXT: br i1 [[TMP0]], label %if.done, label %if.false
299 ; CHECK-NEXT: [[RELTABLE_INTRINSIC:%.*]] = call ptr @llvm.load.relative.i32(ptr @table.rel, i32 [[RELTABLE_SHIFT]])
300 ; CHECK-NEXT: br label %if.done
302 ; CHECK-NEXT: [[TMP2:%.*]] = phi ptr [ @.str.10, %entry ], [ [[RELTABLE_INTRINSIC]], %if.false ]
303 ; CHECK-NEXT: ret ptr [[TMP2]]
306 %0 = icmp sgt i32 %x, 1
307 %1 = getelementptr [2 x ptr], ptr @table, i32 0, i32 %x
308 br i1 %0, label %if.done, label %if.false
311 %2 = load ptr, ptr %1
315 %3 = phi ptr [ @.str.10, %entry ], [ %2, %if.false ]
319 ; Another check to ensure that call @llvm.load.relative is inserted before load but not before gep.
320 ; When a lookup table is accessed, and gep is not immediately followed by a load (like if there is a function call
321 ; or an exception in between), make sure that call @llvm.load.relative is inserted before load.
322 ; CHECK-LABEL: @may_not_return()
323 declare void @may_not_return()
325 define ptr @gep_is_not_imm_followed_by_load(i32 %x) {
326 ; CHECK-LABEL: @gep_is_not_imm_followed_by_load(i32 %x)
328 ; CHECK-NEXT: [[RELTABLE_SHIFT:%.*]] = shl i32 [[X:%.*]], 2
329 ; CHECK-NEXT: call void @may_not_return()
330 ; CHECK-NEXT: [[RELTABLE_INTRINSIC:%.*]] = call ptr @llvm.load.relative.i32(ptr @table2.rel, i32 [[RELTABLE_SHIFT]])
331 ; CHECK-NEXT: ret ptr [[RELTABLE_INTRINSIC]]
334 %0 = getelementptr [2 x ptr], ptr @table2, i32 0, i32 %x
335 call void @may_not_return()
336 %1 = load ptr, ptr %0
340 !llvm.module.flags = !{!0, !1}
341 !0 = !{i32 7, !"PIC Level", i32 2}
342 !1 = !{i32 1, !"Code Model", i32 1}
343 !4 = !{!"any pointer", !5, i64 0}
344 !5 = !{!"omnipotent char", !6, i64 0}
345 !6 = !{!"Simple C/C++ TBAA"}