xref: /llvm-project/llvm/test/CodeGen/X86/patchable-prologue.ll (revision e6bf48d11047e970cb24554a01b65b566d6b5d22)
1; RUN: llc -verify-machineinstrs -filetype=obj -o - -mtriple=x86_64-apple-macosx < %s | llvm-objdump --no-print-imm-hex --triple=x86_64-apple-macosx -d - | FileCheck %s
2; RUN: llc -verify-machineinstrs -mtriple=x86_64-apple-macosx < %s | FileCheck %s --check-prefix=CHECK-ALIGN
3; RUN: llc -verify-machineinstrs -show-mc-encoding -mtriple=i386 < %s | FileCheck %s --check-prefixes=X86,X86CFI,XCHG
4; RUN: llc -verify-machineinstrs -show-mc-encoding -mtriple=i386-windows-msvc < %s | FileCheck %s --check-prefixes=X86,MOV
5; RUN: llc -verify-machineinstrs -show-mc-encoding -mtriple=i386-windows-msvc -mcpu=pentium3 < %s | FileCheck %s --check-prefixes=X86,MOV
6; RUN: llc -verify-machineinstrs -show-mc-encoding -mtriple=i386-windows-msvc -mcpu=pentium4 < %s | FileCheck %s --check-prefixes=X86,XCHG
7; RUN: llc -verify-machineinstrs -show-mc-encoding -mtriple=x86_64-windows-msvc < %s | FileCheck %s --check-prefix=X64
8
9declare void @callee(ptr)
10
11define void @f0() "patchable-function"="prologue-short-redirect" {
12; CHECK-LABEL: _f0{{>?}}:
13; CHECK-NEXT:  66 90 	nop
14
15; CHECK-ALIGN: 	.p2align	4
16; CHECK-ALIGN: _f0:
17
18; X86: f0:
19; X86CFI-NEXT: .cfi_startproc
20; X86-NEXT: # %bb.0:
21; XCHG-NEXT: xchgw   %ax, %ax                # encoding: [0x66,0x90]
22; MOV-NEXT: movl    %edi, %edi              # encoding: [0x8b,0xff]
23; X86-NEXT: retl
24
25; X64: f0:
26; X64-NEXT: # %bb.0:
27; X64-NEXT: xchgw   %ax, %ax                # encoding: [0x66,0x90]
28; X64-NEXT: retq
29
30  ret void
31}
32
33define void @f1() "patchable-function"="prologue-short-redirect" "frame-pointer"="all" {
34; CHECK-LABEL: _f1
35; CHECK-NEXT: 66 90     nop
36; CHECK-NEXT: 55		pushq	%rbp
37
38; CHECK-ALIGN: 	.p2align	4
39; CHECK-ALIGN: _f1:
40
41; X86: f1:
42; X86CFI-NEXT: .cfi_startproc
43; X86-NEXT: # %bb.0:
44; XCHG-NEXT: xchgw   %ax, %ax                # encoding: [0x66,0x90]
45; MOV-NEXT: movl    %edi, %edi              # encoding: [0x8b,0xff]
46; X86-NEXT: pushl   %ebp
47
48; X64: f1:
49; X64-NEXT: .seh_proc f1
50; X64-NEXT: # %bb.0:
51; X64-NEXT: xchgw %ax, %ax
52; X64-NEXT: pushq   %rbp
53
54  ret void
55}
56
57define void @f2() "patchable-function"="prologue-short-redirect" {
58; CHECK-LABEL: _f2
59; CHECK-NEXT: 48 81 ec a8 00 00 00 	subq	$168, %rsp
60
61; CHECK-ALIGN: 	.p2align	4
62; CHECK-ALIGN: _f2:
63
64; X86: f2:
65; X86CFI-NEXT: .cfi_startproc
66; X86-NEXT: # %bb.0:
67; XCHG-NEXT: xchgw   %ax, %ax                # encoding: [0x66,0x90]
68; MOV-NEXT: movl    %edi, %edi              # encoding: [0x8b,0xff]
69; X86-NEXT: pushl   %ebp
70
71; X64: f2:
72; X64-NEXT: .seh_proc f2
73; X64-NEXT: # %bb.0:
74; X64-NEXT: subq    $200, %rsp
75
76  %ptr = alloca i64, i32 20
77  call void @callee(ptr %ptr)
78  ret void
79}
80
81define void @f3() "patchable-function"="prologue-short-redirect" optsize {
82; CHECK-LABEL: _f3
83; CHECK-NEXT: 66 90 	nop
84
85; CHECK-ALIGN: 	.p2align	4
86; CHECK-ALIGN: _f3:
87
88; X86: f3:
89; X86CFI-NEXT: .cfi_startproc
90; X86-NEXT: # %bb.0:
91; XCHG-NEXT: xchgw   %ax, %ax
92; MOV-NEXT: movl   %edi, %edi
93; X86-NEXT: retl
94
95; X64: f3:
96; X64-NEXT: # %bb.0:
97; X64-NEXT: xchgw   %ax, %ax
98; X64-NEXT: retq
99
100  ret void
101}
102
103; This testcase happens to produce a KILL instruction at the beginning of the
104; first basic block. In this case the 2nd instruction should be turned into a
105; patchable one.
106; CHECK-LABEL: f4{{>?}}:
107; CHECK-NEXT: 8b 0c 37  movl  (%rdi,%rsi), %ecx
108; X86: f4:
109; X86CFI-NEXT: .cfi_startproc
110; X86-NEXT: # %bb.0:
111; XCHG-NEXT: xchgw   %ax, %ax
112; MOV-NEXT: movl   %edi, %edi
113; X86-NEXT: pushl   %ebx
114
115; X64: f4:
116; X64-NEXT: # %bb.0:
117; X64-NOT: xchgw   %ax, %ax
118
119define i32 @f4(ptr %arg1, i64 %arg2, i32 %arg3) "patchable-function"="prologue-short-redirect" {
120bb:
121  %tmp10 = getelementptr i8, ptr %arg1, i64 %arg2
122  %tmp12 = load i32, ptr %tmp10, align 4
123  fence acquire
124  %tmp13 = add i32 %tmp12, %arg3
125  %tmp14 = cmpxchg ptr %tmp10, i32 %tmp12, i32 %tmp13 seq_cst monotonic
126  %tmp15 = extractvalue { i32, i1 } %tmp14, 1
127  br i1 %tmp15, label %bb21, label %bb16
128
129bb16:
130  br label %bb21
131
132bb21:
133  %tmp22 = phi i32 [ %tmp12, %bb ], [ %arg3, %bb16 ]
134  ret i32 %tmp22
135}
136
137; This testcase produces an empty function (not even a ret on some targets).
138; This scenario can happen with undefined behavior.
139; Ensure that the "patchable-function" pass supports this case.
140; CHECK-LABEL: _emptyfunc
141; CHECK-NEXT: 0f 0b 	ud2
142
143; CHECK-ALIGN: 	.p2align	4
144; CHECK-ALIGN: _emptyfunc:
145
146; X86: emptyfunc:
147; X86CFI-NEXT: .cfi_startproc
148; X86-NEXT: # %bb.0:
149; XCHG-NEXT: xchgw   %ax, %ax
150; MOV-NEXT: movl   %edi, %edi
151
152; X64: emptyfunc:
153; X64-NEXT: # %bb.0:
154; X64-NEXT: xchgw   %ax, %ax
155
156; From code: int emptyfunc() {}
157define i32 @emptyfunc() "patchable-function"="prologue-short-redirect" {
158  unreachable
159}
160
161
162; Hotpatch feature must ensure no jump within the function goes to the first instruction.
163; From code:
164; void jmp_to_start(char *b) {
165;   do {
166;   } while ((++(*b++)));
167; }
168
169; CHECK-ALIGN: 	.p2align	4
170; CHECK-ALIGN: _jmp_to_start:
171
172; X86: jmp_to_start:
173; X86CFI-NEXT: .cfi_startproc
174; X86-NEXT: # %bb.0:
175; XCHG-NEXT: xchgw   %ax, %ax
176; MOV-NEXT: movl   %edi, %edi
177
178; X64: jmp_to_start:
179; X64-NEXT: # %bb.0:
180; X64-NEXT: xchgw   %ax, %ax
181
182define dso_local void @jmp_to_start(ptr inreg nocapture noundef %b) "patchable-function"="prologue-short-redirect" {
183entry:
184  br label %do.body
185do.body:                                          ; preds = %do.body, %entry
186  %b.addr.0 = phi ptr [ %b, %entry ], [ %incdec.ptr, %do.body ]
187  %incdec.ptr = getelementptr inbounds i8, ptr %b.addr.0, i64 1
188  %0 = load i8, ptr %b.addr.0, align 1
189  %inc = add i8 %0, 1
190  store i8 %inc, ptr %b.addr.0, align 1
191  %tobool.not = icmp eq i8 %inc, 0
192  br i1 %tobool.not, label %do.end, label %do.body
193do.end:                                           ; preds = %do.body
194  ret void
195}
196
197
198; Test that inline asm is properly hotpatched. We currently don't examine the
199; asm instruction when printing it, thus we always emit patching NOPs.
200
201; X64: inline_asm:
202; X64-NEXT: # %bb.0:
203; X64-NEXT: xchgw   %ax, %ax                        # encoding: [0x66,0x90]
204; X64-NEXT: #APP
205; X64-NEXT: int3                                    # encoding: [0xcc]
206; X64-NEXT: #NO_APP
207
208define dso_local void @inline_asm() "patchable-function"="prologue-short-redirect" {
209entry:
210  call void asm sideeffect "int3", "~{dirflag},~{fpsr},~{flags}"()
211  ret void
212}
213