Skip to content

Commit cea8596

Browse files
committed
[InstCombine] Skip tranformConstExprCastCall for naked function
1 parent 90c397f commit cea8596

File tree

3 files changed

+52
-26
lines changed

3 files changed

+52
-26
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3850,6 +3850,12 @@ bool InstCombinerImpl::transformConstExprCastCall(CallBase &Call) {
38503850
if (Callee->hasFnAttribute("thunk"))
38513851
return false;
38523852

3853+
// If this is a call to a naked function, the assembly might be
3854+
// using an argument, or otherwise rely on the frame layout,
3855+
// the function prototype will mismatch.
3856+
if (Callee->hasFnAttribute(Attribute::Naked))
3857+
return false;
3858+
38533859
// If this is a musttail call, the callee's prototype must match the caller's
38543860
// prototype with the exception of pointee types. The code below doesn't
38553861
// implement that, so we can't do this transform.

llvm/test/Transforms/InstCombine/call-cast-attrs.ll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@ define void @d(i32 %x, ...) {
1616
ret void
1717
}
1818

19+
define void @naked_func() naked {
20+
tail call void asm sideeffect "mov r1, r0", ""()
21+
unreachable
22+
}
23+
1924
define void @g(ptr %y) {
2025
call i32 @b(i32 zeroext 0)
2126
call void @c(ptr %y)
2227
call void @c(ptr sret(i32) %y)
2328
call void @d(i32 0, ptr sret(i32) %y)
2429
call void @d(i32 0, ptr nocapture %y)
2530
call void @d(ptr nocapture noundef %y)
31+
call void @naked_func(i32 1)
2632
ret void
2733
}
2834
; CHECK-LABEL: define void @g(ptr %y)
@@ -34,3 +40,4 @@ define void @g(ptr %y) {
3440
; CHECK32: %2 = ptrtoint ptr %y to i32
3541
; CHECK32: call void (i32, ...) @d(i32 noundef %2)
3642
; CHECK64: call void @d(ptr nocapture noundef %y)
43+
; CHECK: call void @naked_func(i32 1)

llvm/test/Transforms/LowerTypeTests/cfi-unwind-direct-call.ll

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-attributes --include-generated-funcs --version 2
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-attributes --include-generated-funcs --version 4
22
; RUN: opt < %s -passes='lowertypetests,default<O3>' -S | FileCheck %s
33

44
; This IR is based of the following C++
@@ -156,38 +156,38 @@ attributes #8 = { noreturn nounwind }
156156
!13 = !{}
157157
!14 = !{!"branch_weights", i32 1048575, i32 1}
158158
; CHECK: Function Attrs: minsize mustprogress optsize
159-
; CHECK-LABEL: define dso_local void @_Z7throw_ei
160-
; CHECK-SAME: (i32 noundef [[NUM:%.*]]) #[[ATTR0:[0-9]+]] !type !4 !type !5 !type !6 {
159+
; CHECK-LABEL: define dso_local void @_Z7throw_ei(
160+
; CHECK-SAME: i32 noundef [[NUM:%.*]]) #[[ATTR0:[0-9]+]] !type [[META4:![0-9]+]] !type [[META5:![0-9]+]] !type [[META6:![0-9]+]] {
161161
; CHECK-NEXT: entry:
162162
; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i32 [[NUM]], 0
163163
; CHECK-NEXT: br i1 [[TOBOOL_NOT]], label [[IF_END:%.*]], label [[IF_THEN:%.*]]
164164
; CHECK: if.then:
165-
; CHECK-NEXT: [[EXCEPTION:%.*]] = tail call ptr @__cxa_allocate_exception(i64 4) #[[ATTR5:[0-9]+]]
165+
; CHECK-NEXT: [[EXCEPTION:%.*]] = tail call ptr @__cxa_allocate_exception(i64 4) #[[ATTR6:[0-9]+]]
166166
; CHECK-NEXT: store i32 20, ptr [[EXCEPTION]], align 16, !tbaa [[TBAA7:![0-9]+]]
167-
; CHECK-NEXT: tail call void @__cxa_throw(ptr nonnull [[EXCEPTION]], ptr nonnull @_ZTIi, ptr null) #[[ATTR6:[0-9]+]]
167+
; CHECK-NEXT: tail call void @__cxa_throw(ptr nonnull [[EXCEPTION]], ptr nonnull @_ZTIi, ptr null) #[[ATTR7:[0-9]+]]
168168
; CHECK-NEXT: unreachable
169169
; CHECK: if.end:
170170
; CHECK-NEXT: ret void
171171
;
172172
;
173173
; CHECK: Function Attrs: minsize mustprogress optsize
174-
; CHECK-LABEL: define dso_local void @_Z10call_catchi
175-
; CHECK-SAME: (i32 noundef [[NUM:%.*]]) local_unnamed_addr #[[ATTR0]] personality ptr @__gxx_personality_v0 !type !4 !type !5 !type !6 {
174+
; CHECK-LABEL: define dso_local void @_Z10call_catchi(
175+
; CHECK-SAME: i32 noundef [[NUM:%.*]]) local_unnamed_addr #[[ATTR0]] personality ptr @__gxx_personality_v0 !type [[META4]] !type [[META5]] !type [[META6]] {
176176
; CHECK-NEXT: entry:
177177
; CHECK-NEXT: store ptr @_Z7throw_ei.cfi_jt, ptr @catch_ptr, align 8, !tbaa [[TBAA11:![0-9]+]]
178-
; CHECK-NEXT: invoke void @_Z7throw_ei.cfi_jt() #[[ATTR7:[0-9]+]]
179-
; CHECK-NEXT: to label [[TRY_CONT:%.*]] unwind label [[LPAD:%.*]]
178+
; CHECK-NEXT: invoke void @_Z7throw_ei.cfi_jt(i32 noundef [[NUM]]) #[[ATTR8:[0-9]+]]
179+
; CHECK-NEXT: to label [[TRY_CONT:%.*]] unwind label [[LPAD:%.*]], !callees [[META13:![0-9]+]]
180180
; CHECK: lpad:
181181
; CHECK-NEXT: [[TMP0:%.*]] = landingpad { ptr, i32 }
182-
; CHECK-NEXT: catch ptr @_ZTIi
182+
; CHECK-NEXT: catch ptr @_ZTIi
183183
; CHECK-NEXT: [[TMP1:%.*]] = extractvalue { ptr, i32 } [[TMP0]], 1
184-
; CHECK-NEXT: [[TMP2:%.*]] = tail call i32 @llvm.eh.typeid.for(ptr nonnull @_ZTIi) #[[ATTR5]]
184+
; CHECK-NEXT: [[TMP2:%.*]] = tail call i32 @llvm.eh.typeid.for(ptr nonnull @_ZTIi) #[[ATTR6]]
185185
; CHECK-NEXT: [[MATCHES:%.*]] = icmp eq i32 [[TMP1]], [[TMP2]]
186186
; CHECK-NEXT: br i1 [[MATCHES]], label [[CATCH:%.*]], label [[EH_RESUME:%.*]]
187187
; CHECK: catch:
188188
; CHECK-NEXT: [[TMP3:%.*]] = extractvalue { ptr, i32 } [[TMP0]], 0
189-
; CHECK-NEXT: [[TMP4:%.*]] = tail call ptr @__cxa_begin_catch(ptr [[TMP3]]) #[[ATTR5]]
190-
; CHECK-NEXT: tail call void @__cxa_end_catch() #[[ATTR5]]
189+
; CHECK-NEXT: [[TMP4:%.*]] = tail call ptr @__cxa_begin_catch(ptr [[TMP3]]) #[[ATTR6]]
190+
; CHECK-NEXT: tail call void @__cxa_end_catch() #[[ATTR6]]
191191
; CHECK-NEXT: br label [[TRY_CONT]]
192192
; CHECK: try.cont:
193193
; CHECK-NEXT: ret void
@@ -196,33 +196,46 @@ attributes #8 = { noreturn nounwind }
196196
;
197197
;
198198
; CHECK: Function Attrs: minsize optsize
199-
; CHECK-LABEL: define weak_odr hidden void @__cfi_check_fail
200-
; CHECK-SAME: (ptr noundef [[TMP0:%.*]], ptr noundef [[TMP1:%.*]]) #[[ATTR2:[0-9]+]] {
199+
; CHECK-LABEL: define weak_odr hidden void @__cfi_check_fail(
200+
; CHECK-SAME: ptr noundef [[TMP0:%.*]], ptr noundef [[TMP1:%.*]]) #[[ATTR2:[0-9]+]] {
201201
; CHECK-NEXT: entry:
202-
; CHECK-NEXT: [[DOTNOT:%.*]] = icmp eq ptr [[TMP0]], null, !nosanitize !13
203-
; CHECK-NEXT: br i1 [[DOTNOT]], label [[TRAP:%.*]], label [[CONT:%.*]], !nosanitize !13
202+
; CHECK-NEXT: [[DOTNOT:%.*]] = icmp eq ptr [[TMP0]], null, !nosanitize [[META14:![0-9]+]]
203+
; CHECK-NEXT: br i1 [[DOTNOT]], label [[TRAP:%.*]], label [[CONT:%.*]], !nosanitize [[META14]]
204204
; CHECK: trap:
205-
; CHECK-NEXT: tail call void @llvm.ubsantrap(i8 2) #[[ATTR8:[0-9]+]], !nosanitize !13
206-
; CHECK-NEXT: unreachable, !nosanitize !13
205+
; CHECK-NEXT: tail call void @llvm.ubsantrap(i8 2) #[[ATTR9:[0-9]+]], !nosanitize [[META14]]
206+
; CHECK-NEXT: unreachable, !nosanitize [[META14]]
207207
; CHECK: cont:
208-
; CHECK-NEXT: [[TMP2:%.*]] = load i8, ptr [[TMP0]], align 4, !nosanitize !13
208+
; CHECK-NEXT: [[TMP2:%.*]] = load i8, ptr [[TMP0]], align 4, !nosanitize [[META14]]
209209
; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i8 [[TMP2]], 5
210210
; CHECK-NEXT: br i1 [[SWITCH]], label [[TRAP]], label [[CONT6:%.*]]
211211
; CHECK: cont6:
212-
; CHECK-NEXT: ret void, !nosanitize !13
212+
; CHECK-NEXT: ret void, !nosanitize [[META14]]
213213
;
214214
;
215-
; CHECK-LABEL: define weak void @__cfi_check
216-
; CHECK-SAME: (i64 [[TMP0:%.*]], ptr [[TMP1:%.*]], ptr [[TMP2:%.*]]) local_unnamed_addr {
215+
; CHECK-LABEL: define weak void @__cfi_check(
216+
; CHECK-SAME: i64 [[TMP0:%.*]], ptr [[TMP1:%.*]], ptr [[TMP2:%.*]]) local_unnamed_addr {
217217
; CHECK-NEXT: entry:
218218
; CHECK-NEXT: tail call void @llvm.trap()
219219
; CHECK-NEXT: unreachable
220220
;
221221
;
222222
; CHECK: Function Attrs: naked nocf_check noinline
223-
; CHECK-LABEL: define internal void @_Z7throw_ei.cfi_jt
224-
; CHECK-SAME: () #[[ATTR4:[0-9]+]] align 8 {
223+
; CHECK-LABEL: define internal void @_Z7throw_ei.cfi_jt(
224+
; CHECK-SAME: ) #[[ATTR5:[0-9]+]] align 8 {
225225
; CHECK-NEXT: entry:
226-
; CHECK-NEXT: tail call void asm sideeffect "jmp ${0:c}@plt\0Aint3\0Aint3\0Aint3\0A", "s"(ptr nonnull @_Z7throw_ei) #[[ATTR5]]
226+
; CHECK-NEXT: tail call void asm sideeffect "jmp ${0:c}@plt\0Aint3\0Aint3\0Aint3\0A", "s"(ptr nonnull @_Z7throw_ei) #[[ATTR6]]
227227
; CHECK-NEXT: unreachable
228228
;
229+
;.
230+
; CHECK: [[META4]] = !{i64 0, !"_ZTSFviE"}
231+
; CHECK: [[META5]] = !{i64 0, !"_ZTSFviE.generalized"}
232+
; CHECK: [[META6]] = !{i64 0, i64 -8738933900360652027}
233+
; CHECK: [[TBAA7]] = !{[[META8:![0-9]+]], [[META8]], i64 0}
234+
; CHECK: [[META8]] = !{!"int", [[META9:![0-9]+]], i64 0}
235+
; CHECK: [[META9]] = !{!"omnipotent char", [[META10:![0-9]+]], i64 0}
236+
; CHECK: [[META10]] = !{!"Simple C++ TBAA"}
237+
; CHECK: [[TBAA11]] = !{[[META12:![0-9]+]], [[META12]], i64 0}
238+
; CHECK: [[META12]] = !{!"any pointer", [[META9]], i64 0}
239+
; CHECK: [[META13]] = !{ptr @_Z7throw_ei.cfi_jt}
240+
; CHECK: [[META14]] = !{}
241+
;.

0 commit comments

Comments
 (0)