[PATCH] D73699: [RISCV] Implement mayBeEmittedAsTailCall for tail call optimization
weiwei via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 30 05:45:28 PST 2020
wwei created this revision.
wwei added reviewers: asb, lenary, simoncook, edward-jones, lewis-revill.
wwei added a project: LLVM.
Herald added subscribers: llvm-commits, luismarques, apazos, sameer.abuasal, pzheng, s.egerton, Jim, benna, psnobl, jocewei, PkmX, rkruppe, the_o, brucehoult, MartinMosbeck, rogfer01, zzheng, MaskRay, jrtc27, shiva0217, kito-cheng, niosHD, sabuasal, johnrusso, rbar, hiraditya.
Implement TargetLowering callback `mayBeEmittedAsTailCall` for riscv in CodeGenPrepare,
which will duplicate return instructions to enable tailcall optimization.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D73699
Files:
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
llvm/lib/Target/RISCV/RISCVISelLowering.h
llvm/test/CodeGen/RISCV/tail-calls.ll
Index: llvm/test/CodeGen/RISCV/tail-calls.ll
===================================================================
--- llvm/test/CodeGen/RISCV/tail-calls.ll
+++ llvm/test/CodeGen/RISCV/tail-calls.ll
@@ -168,6 +168,54 @@
ret i32 %rv
}
+; Duplicate returns to enable tail call optimizations.
+declare i32 @test()
+declare i32 @test1()
+declare i32 @test2()
+declare i32 @test3()
+define i32 @duplicate_returns(i32 %a, i32 %b) nounwind {
+; CHECK-LABEL: duplicate_returns:
+; CHECK: # %bb.3: # %if.then6
+; CHECK-NEXT: tail test2
+; CHECK-NEXT: .LBB13_4: # %if.then
+; CHECK-NEXT: tail test
+; CHECK-NEXT: .LBB13_5: # %if.then2
+; CHECK-NEXT: tail test1
+; CHECK-NEXT: .LBB13_6: # %if.else8
+; CHECK-NEXT: tail test3
+entry:
+ %cmp = icmp eq i32 %a, 0
+ br i1 %cmp, label %if.then, label %if.else
+
+if.then: ; preds = %entry
+ %call = tail call i32 @test()
+ br label %return
+
+if.else: ; preds = %entry
+ %cmp1 = icmp eq i32 %b, 0
+ br i1 %cmp1, label %if.then2, label %if.else4
+
+if.then2: ; preds = %if.else
+ %call3 = tail call i32 @test1()
+ br label %return
+
+if.else4: ; preds = %if.else
+ %cmp5 = icmp sgt i32 %a, %b
+ br i1 %cmp5, label %if.then6, label %if.else8
+
+if.then6: ; preds = %if.else4
+ %call7 = tail call i32 @test2()
+ br label %return
+
+if.else8: ; preds = %if.else4
+ %call9 = tail call i32 @test3()
+ br label %return
+
+return: ; preds = %if.else8, %if.then6, %if.then2, %if.then
+ %retval = phi i32 [ %call, %if.then ], [ %call3, %if.then2 ], [ %call7, %if.then6 ], [ %call9, %if.else8 ]
+ ret i32 %retval
+}
+
!llvm.module.flags = !{!0}
!0 = !{i32 1, !"ProfileSummary", !1}
!1 = !{!2, !3, !4, !5, !6, !7, !8, !9}
Index: llvm/lib/Target/RISCV/RISCVISelLowering.h
===================================================================
--- llvm/lib/Target/RISCV/RISCVISelLowering.h
+++ llvm/lib/Target/RISCV/RISCVISelLowering.h
@@ -181,6 +181,7 @@
Type *Ty) const override {
return true;
}
+ bool mayBeEmittedAsTailCall(const CallInst *CI) const override;
template <class NodeTy>
SDValue getAddr(NodeTy *N, SelectionDAG &DAG, bool IsLocal = true) const;
Index: llvm/lib/Target/RISCV/RISCVISelLowering.cpp
===================================================================
--- llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -2494,6 +2494,10 @@
F, "Argument register required, but has been reserved."});
}
+bool RISCVTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
+ return CI->isTailCall();
+}
+
const char *RISCVTargetLowering::getTargetNodeName(unsigned Opcode) const {
switch ((RISCVISD::NodeType)Opcode) {
case RISCVISD::FIRST_NUMBER:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73699.241423.patch
Type: text/x-patch
Size: 3038 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200130/8abb7ca5/attachment-0001.bin>
More information about the llvm-commits
mailing list