aboutsummaryrefslogtreecommitdiff
path: root/llvm/test/Transforms/InstSimplify/structured-gep.ll
blob: 6f3ebb98d86fd33725b86bb3a6a16f0b834d9ceb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
; RUN: opt -S -passes=instsimplify < %s | FileCheck %s

%struct.S = type { i32, float }
%struct.Nested = type { %struct.S, i32 }

@g = external global i32
@arr = external global [10 x i32]

define ptr @zero_index_local(ptr %p) {
; CHECK-LABEL: @zero_index_local(ptr %p
; CHECK-NEXT:    ret ptr %p
;
  %sgep = call ptr (ptr, ...) @llvm.structured.gep.p0(ptr elementtype(i32) %p)
  ret ptr %sgep
}

define ptr @zero_index_global() {
; CHECK-LABEL: @zero_index_global(
; CHECK-NEXT:    ret ptr @g
;
  %sgep = call ptr (ptr, ...) @llvm.structured.gep.p0(ptr elementtype(i32) @g)
  ret ptr %sgep
}

define ptr @zero_index_array(ptr %p) {
; CHECK-LABEL: @zero_index_array(ptr %p
; CHECK-NEXT:    ret ptr %p
;
  %sgep = call ptr (ptr, ...) @llvm.structured.gep.p0(ptr elementtype([10 x i32]) %p)
  ret ptr %sgep
}

define ptr addrspace(11) @zero_index_addrspace(ptr addrspace(11) %p) {
; CHECK-LABEL: @zero_index_addrspace(ptr addrspace(11) %p
; CHECK-NEXT:    ret ptr addrspace(11) %p
;
  %sgep = call ptr addrspace(11) (ptr addrspace(11), ...) @llvm.structured.gep.p11(ptr addrspace(11) elementtype(i32) %p)
  ret ptr addrspace(11) %sgep
}

define i32 @zero_index_with_load(ptr %p) {
; CHECK-LABEL: @zero_index_with_load(ptr %p
; CHECK-NEXT:    [[V:%.*]] = load i32, ptr %p, align 4
; CHECK-NEXT:    ret i32 [[V]]
;
  %sgep = call ptr (ptr, ...) @llvm.structured.gep.p0(ptr elementtype(i32) %p)
  %v = load i32, ptr %sgep, align 4
  ret i32 %v
}

; GEP with a zero index can be simplified, but SGEP cannot.
define ptr @zero_index_struct(ptr %p) {
; CHECK-LABEL: @zero_index_struct(ptr %p
; CHECK-NEXT:    [[SGEP:%.*]] = call ptr (ptr, ...) @llvm.structured.gep.p0(ptr elementtype(%struct.S) %p, i32 0)
; CHECK-NEXT:    ret ptr [[SGEP]]
;
  %sgep = call ptr (ptr, ...) @llvm.structured.gep.p0(ptr elementtype(%struct.S) %p, i32 0)
  ret ptr %sgep
}

define ptr @one_index_struct(ptr %p) {
; CHECK-LABEL: @one_index_struct(ptr %p
; CHECK-NEXT:    [[SGEP:%.*]] = call ptr (ptr, ...) @llvm.structured.gep.p0(ptr elementtype(%struct.S) %p, i32 1)
; CHECK-NEXT:    ret ptr [[SGEP]]
;
  %sgep = call ptr (ptr, ...) @llvm.structured.gep.p0(ptr elementtype(%struct.S) %p, i32 1)
  ret ptr %sgep
}

define ptr @multi_index_nested(ptr %p) {
; CHECK-LABEL: @multi_index_nested(
; CHECK-NEXT:    [[SGEP:%.*]] = call ptr (ptr, ...) @llvm.structured.gep.p0(ptr elementtype(%struct.Nested) %p, i32 0, i32 0)
; CHECK-NEXT:    ret ptr [[SGEP]]
;
  %sgep = call ptr (ptr, ...) @llvm.structured.gep.p0(ptr elementtype(%struct.Nested) %p, i32 0, i32 0)
  ret ptr %sgep
}

define ptr @dynamic_index(ptr %p, i32 %i) {
; CHECK-LABEL: @dynamic_index(
; CHECK-NEXT:    [[SGEP:%.*]] = call ptr (ptr, ...) @llvm.structured.gep.p0(ptr elementtype([10 x i32]) %p, i32 %i)
; CHECK-NEXT:    ret ptr [[SGEP]]
;
  %sgep = call ptr (ptr, ...) @llvm.structured.gep.p0(ptr elementtype([10 x i32]) %p, i32 %i)
  ret ptr %sgep
}

declare ptr @llvm.structured.gep.p0(ptr, ...) #0
declare ptr addrspace(11) @llvm.structured.gep.p11(ptr addrspace(11), ...) #0

attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }