Skip to content

Commit 6b2399b

Browse files
author
Yonghong Song
committed
[RFC][SelectionDAG] Not issue TRAP node if naked function
In [1], Nikita Popov suggested that during lowering 'unreachable' insn should not generate extra code for naked functions, and this applies to all architectures. Note that for naked functions, 'unreachable' insn is necessary in IR since the basic block needs a terminator to end. This patch checked whether a function is naked function or not. If it is a naked function, 'unreachable' insn will not generate ISD::TRAP. I put the RFC in the subject as I am not sure how to find a test which can validate the change in FastISel.cpp. Any advice is welcome. [1] llvm#131731
1 parent fecd937 commit 6b2399b

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

llvm/lib/CodeGen/SelectionDAG/FastISel.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,9 @@ bool FastISel::selectOperator(const User *I, unsigned Opcode) {
18621862
return true;
18631863
}
18641864

1865+
if (cast<Instruction>(I)->getFunction()->hasFnAttribute(Attribute::Naked))
1866+
return true;
1867+
18651868
return fastEmit_(MVT::Other, MVT::Other, ISD::TRAP) != 0;
18661869
}
18671870
return true;

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3514,6 +3514,9 @@ void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {
35143514
return;
35153515
}
35163516

3517+
if (I.getFunction()->hasFnAttribute(Attribute::Naked))
3518+
return;
3519+
35173520
DAG.setRoot(DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot()));
35183521
}
35193522

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
; RUN: llc -o - %s -mtriple=x86_64-linux-gnu -trap-unreachable | FileCheck %s
2+
; RUN: llc -o - %s -mtriple=x86_64-linux-gnu -trap-unreachable -fast-isel | FileCheck %s
3+
4+
define dso_local void @foo() #0 {
5+
entry:
6+
tail call void asm sideeffect "movl 3,%eax", "~{dirflag},~{fpsr},~{flags}"()
7+
unreachable
8+
}
9+
; CHECK-NOT: ud2
10+
11+
attributes #0 = { naked }

0 commit comments

Comments
 (0)