OSDN Git Service

46b48a4242a79ccc3254542b04b9b051c0f3c2bb
[android-x86/external-llvm.git] / test / CodeGen / X86 / indirect-branch-tracking.ll
1 ; RUN: llc -mtriple=x86_64-unknown-unknown -mattr=+ibt -x86-indirect-branch-tracking < %s | FileCheck %s --check-prefix=ALL --check-prefix=X86_64
2 ; RUN: llc -mtriple=i386-unknown-unknown -mattr=+ibt -x86-indirect-branch-tracking < %s | FileCheck %s --check-prefix=ALL --check-prefix=X86
3
4 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 ;; Test1
6 ;; -----
7 ;; Checks ENDBR insertion in case of indirect branch IR instruction.
8 ;; Also since the function is not internal, make sure that endbr32/64 was 
9 ;; added at the beginning of the function.
10 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11
12 define i8 @test1(){
13 ; ALL-LABEL:   test1
14 ; X86_64:      endbr64
15 ; X86:         endbr32
16 ; ALL:         jmp{{q|l}} *
17 ; ALL:         .LBB0_1:
18 ; X86_64-NEXT: endbr64
19 ; X86-NEXT:    endbr32
20 ; ALL:         .LBB0_2:
21 ; X86_64-NEXT: endbr64
22 ; X86-NEXT:    endbr32
23 entry:
24   %0 = select i1 undef, i8* blockaddress(@test1, %bb), i8* blockaddress(@test1, %bb6) ; <i8*> [#uses=1]
25   indirectbr i8* %0, [label %bb, label %bb6]
26
27 bb:                                               ; preds = %entry
28   ret i8 1
29
30 bb6:                                              ; preds = %entry
31   ret i8 2
32 }
33
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 ;; Test2
36 ;; -----
37 ;; Checks ENDBR insertion in case of switch case statement.
38 ;; Also since the function is not internal, ENDBR instruction should be
39 ;; added to its first basic block.
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41
42 define i32 @test2(i32 %a) {
43 ; ALL-LABEL:   test2
44 ; X86_64:      endbr64
45 ; X86:         endbr32
46 ; ALL:         jmp{{q|l}} *
47 ; ALL:         .LBB1_2:
48 ; X86_64-NEXT: endbr64
49 ; X86-NEXT:    endbr32
50 ; ALL:         .LBB1_7:
51 ; X86_64-NOT:      endbr64
52 ; X86-NOT:         endbr32
53 ; ALL:         .LBB1_3:
54 ; X86_64-NEXT: endbr64
55 ; X86-NEXT:    endbr32
56 ; ALL:         .LBB1_4:
57 ; X86_64-NEXT: endbr64
58 ; X86-NEXT:    endbr32
59 ; ALL:         .LBB1_5:
60 ; X86_64-NEXT: endbr64
61 ; X86-NEXT:    endbr32
62 ; ALL:         .LBB1_6:
63 ; X86_64-NEXT: endbr64
64 ; X86-NEXT:    endbr32
65 entry:
66   %retval = alloca i32, align 4
67   %a.addr = alloca i32, align 4
68   store i32 %a, i32* %a.addr, align 4
69   %0 = load i32, i32* %a.addr, align 4
70   switch i32 %0, label %sw.default [
71     i32 0, label %sw.bb
72     i32 1, label %sw.bb1
73     i32 2, label %sw.bb2
74     i32 3, label %sw.bb3
75     i32 4, label %sw.bb4
76   ]
77
78 sw.bb:                                            ; preds = %entry
79   store i32 5, i32* %retval, align 4
80   br label %return
81
82 sw.bb1:                                           ; preds = %entry
83   store i32 7, i32* %retval, align 4
84   br label %return
85
86 sw.bb2:                                           ; preds = %entry
87   store i32 2, i32* %retval, align 4
88   br label %return
89
90 sw.bb3:                                           ; preds = %entry
91   store i32 32, i32* %retval, align 4
92   br label %return
93
94 sw.bb4:                                           ; preds = %entry
95   store i32 73, i32* %retval, align 4
96   br label %return
97
98 sw.default:                                       ; preds = %entry
99   store i32 0, i32* %retval, align 4
100   br label %return
101
102 return:                                           ; preds = %sw.default, %sw.bb4, %sw.bb3, %sw.bb2, %sw.bb1, %sw.bb
103   %1 = load i32, i32* %retval, align 4
104   ret i32 %1
105 }
106
107 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
108 ;; Test3
109 ;; -----
110 ;; Checks ENDBR insertion in case of indirect call instruction.
111 ;; The new instruction should be added to the called function (test6)
112 ;; although it is internal.
113 ;; Also since the function is not internal, ENDBR instruction should be
114 ;; added to its first basic block.
115 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
116
117 define void @test3() {
118 ; ALL-LABEL:   test3
119 ; X86_64:      endbr64
120 ; X86:         endbr32
121 ; ALL:         call{{q|l}} *
122 entry:
123   %f = alloca i32 (...)*, align 8
124   store i32 (...)* bitcast (i32 (i32)* @test6 to i32 (...)*), i32 (...)** %f, align 8
125   %0 = load i32 (...)*, i32 (...)** %f, align 8
126   %call = call i32 (...) %0()
127   ret void
128 }
129
130 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131 ;; Test4
132 ;; -----
133 ;; Checks ENDBR insertion in case of setjmp-like function calls.
134 ;; Also since the function is not internal, ENDBR instruction should be
135 ;; added to its first basic block.
136 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
137
138 @buf = internal global [5 x i8*] zeroinitializer
139 declare i8* @llvm.frameaddress(i32)
140 declare i8* @llvm.stacksave()
141 declare i32 @llvm.eh.sjlj.setjmp(i8*)
142
143 define i32 @test4() {
144 ; ALL-LABEL:   test4
145 ; X86_64:      endbr64
146 ; X86:         endbr32
147 ; ALL:         .LBB3_3:
148 ; X86_64-NEXT: endbr64
149 ; X86-NEXT:    endbr32
150   %fp = tail call i8* @llvm.frameaddress(i32 0)
151   store i8* %fp, i8** getelementptr inbounds ([5 x i8*], [5 x i8*]* @buf, i64 0, i64 0), align 16
152   %sp = tail call i8* @llvm.stacksave()
153   store i8* %sp, i8** getelementptr inbounds ([5 x i8*], [5 x i8*]* @buf, i64 0, i64 2), align 16
154   %r = tail call i32 @llvm.eh.sjlj.setjmp(i8* bitcast ([5 x i8*]* @buf to i8*))
155   ret i32 %r
156 }
157
158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159 ;; Test5
160 ;; -----
161 ;; Checks ENDBR insertion in case of internal function.
162 ;; Since the function is internal and its address was not taken,
163 ;; make sure that endbr32/64 was not added at the beginning of the 
164 ;; function.
165 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166
167 define internal i8 @test5(){
168 ; ALL-LABEL:   test5
169 ; X86_64-NOT:      endbr64
170 ; X86-NOT:         endbr32
171   ret i8 1
172 }
173
174 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
175 ;; Test6
176 ;; -----
177 ;; Checks ENDBR insertion in case of function that its was address taken.
178 ;; Since the function's address was taken by test3() and despite being
179 ;; internal, check for added endbr32/64 at the beginning of the function.
180 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
181
182 define internal i32 @test6(i32 %a) {
183 ; ALL-LABEL:   test6
184 ; X86_64:      endbr64
185 ; X86:         endbr32
186   ret i32 1
187 }
188
189 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
190 ;; Test7
191 ;; -----
192 ;; Checks ENDBR insertion in case of non-intrenal function.
193 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
194
195 define i32 @test7() {
196 ; ALL-LABEL:   test7
197 ; X86_64:      endbr64
198 ; X86:         endbr32
199   ret i32 1
200 }