Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 77579f5

Browse files
author
Hal Finkel
committed
Add ISD::EH_DWARF_CFA, simplify @llvm.eh.dwarf.cfa on Mips, fix on PowerPC
LLVM has an @llvm.eh.dwarf.cfa intrinsic, used to lower the GCC-compatible __builtin_dwarf_cfa() builtin. As pointed out in PR26761, this is currently broken on PowerPC (and likely on ARM as well). Currently, @llvm.eh.dwarf.cfa is lowered using: ADD(FRAMEADDR, FRAME_TO_ARGS_OFFSET) where FRAME_TO_ARGS_OFFSET defaults to the constant zero. On x86, FRAME_TO_ARGS_OFFSET is lowered to 2*SlotSize. This setup, however, does not work for PowerPC. Because of the way that the stack layout works, the canonical frame address is not exactly (FRAMEADDR + FRAME_TO_ARGS_OFFSET) on PowerPC (there is a lower save-area offset as well), so it is not just a matter of implementing FRAME_TO_ARGS_OFFSET for PowerPC (unless we redefine its semantics -- We can do that, since it is currently used only for @llvm.eh.dwarf.cfa lowering, but the better to directly lower the CFA construct itself (since it can be easily represented as a fixed-offset FrameIndex)). Mips currently does this, but by using a custom lowering for ADD that specifically recognizes the (FRAMEADDR, FRAME_TO_ARGS_OFFSET) pattern. This change introduces a ISD::EH_DWARF_CFA node, which by default expands using the existing logic, but can be directly lowered by the target. Mips is updated to use this method (which simplifies its implementation, and I suspect makes it more robust), and updates PowerPC to do the same. Fixes PR26761. Differential Revision: https://reviews.llvm.org/D24038 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@280350 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 8127683 commit 77579f5

File tree

9 files changed

+79
-32
lines changed

9 files changed

+79
-32
lines changed

include/llvm/CodeGen/ISDOpcodes.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ namespace ISD {
9090
/// adjustment during unwind.
9191
FRAME_TO_ARGS_OFFSET,
9292

93+
/// EH_DWARF_CFA - This node represents the pointer to the DWARF Canonical
94+
/// Frame Address (CFA), generally the value of the stack pointer at the
95+
/// call site in the previous frame.
96+
EH_DWARF_CFA,
97+
9398
/// OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER) - This node represents
9499
/// 'eh_return' gcc dwarf builtin, which is used to return from
95100
/// exception. The general meaning is: adjust stack by OFFSET and pass

lib/CodeGen/SelectionDAG/LegalizeDAG.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,7 @@ void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
10051005
case ISD::MERGE_VALUES:
10061006
case ISD::EH_RETURN:
10071007
case ISD::FRAME_TO_ARGS_OFFSET:
1008+
case ISD::EH_DWARF_CFA:
10081009
case ISD::EH_SJLJ_SETJMP:
10091010
case ISD::EH_SJLJ_LONGJMP:
10101011
case ISD::EH_SJLJ_SETUP_DISPATCH:
@@ -2782,6 +2783,21 @@ bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
27822783
case ISD::FRAME_TO_ARGS_OFFSET:
27832784
Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
27842785
break;
2786+
case ISD::EH_DWARF_CFA: {
2787+
SDValue CfaArg = DAG.getSExtOrTrunc(Node->getOperand(0), dl,
2788+
TLI.getPointerTy(DAG.getDataLayout()));
2789+
SDValue Offset = DAG.getNode(ISD::ADD, dl,
2790+
CfaArg.getValueType(),
2791+
DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
2792+
CfaArg.getValueType()),
2793+
CfaArg);
2794+
SDValue FA = DAG.getNode(
2795+
ISD::FRAMEADDR, dl, TLI.getPointerTy(DAG.getDataLayout()),
2796+
DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout())));
2797+
Results.push_back(DAG.getNode(ISD::ADD, dl, FA.getValueType(),
2798+
FA, Offset));
2799+
break;
2800+
}
27852801
case ISD::FLT_ROUNDS_:
27862802
Results.push_back(DAG.getConstant(1, dl, Node->getValueType(0)));
27872803
break;

lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5017,18 +5017,9 @@ SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
50175017
DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
50185018
return nullptr;
50195019
case Intrinsic::eh_dwarf_cfa: {
5020-
SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), sdl,
5021-
TLI.getPointerTy(DAG.getDataLayout()));
5022-
SDValue Offset = DAG.getNode(ISD::ADD, sdl,
5023-
CfaArg.getValueType(),
5024-
DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, sdl,
5025-
CfaArg.getValueType()),
5026-
CfaArg);
5027-
SDValue FA = DAG.getNode(
5028-
ISD::FRAMEADDR, sdl, TLI.getPointerTy(DAG.getDataLayout()),
5029-
DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout())));
5030-
setValue(&I, DAG.getNode(ISD::ADD, sdl, FA.getValueType(),
5031-
FA, Offset));
5020+
setValue(&I, DAG.getNode(ISD::EH_DWARF_CFA, sdl,
5021+
TLI.getPointerTy(DAG.getDataLayout()),
5022+
getValue(I.getArgOperand(0))));
50325023
return nullptr;
50335024
}
50345025
case Intrinsic::eh_sjlj_callsite: {

lib/CodeGen/SelectionDAG/SelectionDAGDumper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ std::string SDNode::getOperationName(const SelectionDAG *G) const {
105105
case ISD::READ_REGISTER: return "READ_REGISTER";
106106
case ISD::WRITE_REGISTER: return "WRITE_REGISTER";
107107
case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
108+
case ISD::EH_DWARF_CFA: return "EH_DWARF_CFA";
108109
case ISD::EH_RETURN: return "EH_RETURN";
109110
case ISD::EH_SJLJ_SETJMP: return "EH_SJLJ_SETJMP";
110111
case ISD::EH_SJLJ_LONGJMP: return "EH_SJLJ_LONGJMP";

lib/Target/Mips/MipsISelLowering.cpp

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,9 @@ MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
305305
setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
306306
}
307307

308-
setOperationAction(ISD::ADD, MVT::i32, Custom);
308+
setOperationAction(ISD::EH_DWARF_CFA, MVT::i32, Custom);
309309
if (Subtarget.isGP64bit())
310-
setOperationAction(ISD::ADD, MVT::i64, Custom);
310+
setOperationAction(ISD::EH_DWARF_CFA, MVT::i64, Custom);
311311

312312
setOperationAction(ISD::SDIV, MVT::i32, Expand);
313313
setOperationAction(ISD::SREM, MVT::i32, Expand);
@@ -914,7 +914,7 @@ LowerOperation(SDValue Op, SelectionDAG &DAG) const
914914
case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
915915
case ISD::LOAD: return lowerLOAD(Op, DAG);
916916
case ISD::STORE: return lowerSTORE(Op, DAG);
917-
case ISD::ADD: return lowerADD(Op, DAG);
917+
case ISD::EH_DWARF_CFA: return lowerEH_DWARF_CFA(Op, DAG);
918918
case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
919919
}
920920
return SDValue();
@@ -2393,26 +2393,15 @@ SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
23932393
return lowerFP_TO_SINT_STORE(SD, DAG);
23942394
}
23952395

2396-
SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
2397-
if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2398-
|| cast<ConstantSDNode>
2399-
(Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2400-
|| Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2401-
return SDValue();
2396+
SDValue MipsTargetLowering::lowerEH_DWARF_CFA(SDValue Op,
2397+
SelectionDAG &DAG) const {
24022398

2403-
// The pattern
2404-
// (add (frameaddr 0), (frame_to_args_offset))
2405-
// results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2406-
// (add FrameObject, 0)
2407-
// where FrameObject is a fixed StackObject with offset 0 which points to
2408-
// the old stack pointer.
2399+
// Return a fixed StackObject with offset 0 which points to the old stack
2400+
// pointer.
24092401
MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
24102402
EVT ValTy = Op->getValueType(0);
24112403
int FI = MFI.CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2412-
SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
2413-
SDLoc DL(Op);
2414-
return DAG.getNode(ISD::ADD, DL, ValTy, InArgsAddr,
2415-
DAG.getConstant(0, DL, ValTy));
2404+
return DAG.getFrameIndex(FI, ValTy);
24162405
}
24172406

24182407
SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,

lib/Target/Mips/MipsISelLowering.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ namespace llvm {
446446
SDValue lowerShiftLeftParts(SDValue Op, SelectionDAG& DAG) const;
447447
SDValue lowerShiftRightParts(SDValue Op, SelectionDAG& DAG,
448448
bool IsSRA) const;
449-
SDValue lowerADD(SDValue Op, SelectionDAG &DAG) const;
449+
SDValue lowerEH_DWARF_CFA(SDValue Op, SelectionDAG &DAG) const;
450450
SDValue lowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const;
451451

452452
/// isEligibleForTailCallOptimization - Check whether the call is eligible

lib/Target/PowerPC/PPCISelLowering.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ PPCTargetLowering::PPCTargetLowering(const PPCTargetMachine &TM,
344344
setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
345345
setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, MVT::i32, Custom);
346346
setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, MVT::i64, Custom);
347+
setOperationAction(ISD::EH_DWARF_CFA, MVT::i32, Custom);
348+
setOperationAction(ISD::EH_DWARF_CFA, MVT::i64, Custom);
347349

348350
// We want to custom lower some of our intrinsics.
349351
setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
@@ -6188,6 +6190,17 @@ SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
61886190
return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops);
61896191
}
61906192

6193+
SDValue PPCTargetLowering::LowerEH_DWARF_CFA(SDValue Op,
6194+
SelectionDAG &DAG) const {
6195+
MachineFunction &MF = DAG.getMachineFunction();
6196+
6197+
bool isPPC64 = Subtarget.isPPC64();
6198+
EVT PtrVT = getPointerTy(DAG.getDataLayout());
6199+
6200+
int FI = MF.getFrameInfo().CreateFixedObject(isPPC64 ? 8 : 4, 0, false);
6201+
return DAG.getFrameIndex(FI, PtrVT);
6202+
}
6203+
61916204
SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
61926205
SelectionDAG &DAG) const {
61936206
SDLoc DL(Op);
@@ -8246,6 +8259,9 @@ SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
82468259
case ISD::GET_DYNAMIC_AREA_OFFSET:
82478260
return LowerGET_DYNAMIC_AREA_OFFSET(Op, DAG);
82488261

8262+
case ISD::EH_DWARF_CFA:
8263+
return LowerEH_DWARF_CFA(Op, DAG);
8264+
82498265
case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
82508266
case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
82518267

lib/Target/PowerPC/PPCISelLowering.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@ namespace llvm {
819819
SDValue LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG) const;
820820
SDValue LowerGET_DYNAMIC_AREA_OFFSET(SDValue Op, SelectionDAG &DAG) const;
821821
SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
822+
SDValue LowerEH_DWARF_CFA(SDValue Op, SelectionDAG &DAG) const;
822823
SDValue LowerLOAD(SDValue Op, SelectionDAG &DAG) const;
823824
SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG) const;
824825
SDValue LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const;

test/CodeGen/PowerPC/eh-dwarf-cfa.ll

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; RUN: llc < %s | FileCheck %s
2+
target datalayout = "e-m:e-i64:64-n32:64"
3+
target triple = "powerpc64le-unknown-linux-gnu"
4+
5+
define void @_Z1fv() #0 {
6+
entry:
7+
%0 = call i8* @llvm.eh.dwarf.cfa(i32 0)
8+
call void @_Z1gPv(i8* %0)
9+
ret void
10+
11+
; CHECK-LABEL: @_Z1fv
12+
; CHECK: stdu 1, -[[SS:[0-9]+]](1)
13+
; CHECK: .cfi_def_cfa_offset [[SS]]
14+
; CHECK: mr 31, 1
15+
; CHECK: .cfi_def_cfa_register r31
16+
; CHECK: addi 3, 31, [[SS]]
17+
; CHECK-NEXT: bl _Z1gPv
18+
; CHECK: blr
19+
}
20+
21+
declare void @_Z1gPv(i8*)
22+
23+
; Function Attrs: nounwind
24+
declare i8* @llvm.eh.dwarf.cfa(i32) #1
25+
26+
attributes #0 = { "no-frame-pointer-elim"="true" "target-cpu"="ppc64le" }
27+
attributes #1 = { nounwind }
28+

0 commit comments

Comments
 (0)