-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0009-Xtensa-Lower-stack-operations.patch
104 lines (97 loc) · 4.25 KB
/
0009-Xtensa-Lower-stack-operations.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
From e11160270d506fce97d218f01391b7b785ff0613 Mon Sep 17 00:00:00 2001
From: Andrei Safronov <safronov@espressif.com>
Date: Wed, 5 Apr 2023 00:58:40 +0300
Subject: [PATCH 009/158] [Xtensa] Lower stack operations
Implement lowering of dynamic_stackalloc,
stacksave, stackrestore.
---
llvm/lib/Target/Xtensa/XtensaISelLowering.cpp | 49 +++++++++++++++++++
llvm/lib/Target/Xtensa/XtensaISelLowering.h | 4 ++
2 files changed, 53 insertions(+)
diff --git a/llvm/lib/Target/Xtensa/XtensaISelLowering.cpp b/llvm/lib/Target/Xtensa/XtensaISelLowering.cpp
index da282f11310f..7485824c1958 100644
--- a/llvm/lib/Target/Xtensa/XtensaISelLowering.cpp
+++ b/llvm/lib/Target/Xtensa/XtensaISelLowering.cpp
@@ -75,6 +75,12 @@ XtensaTargetLowering::XtensaTargetLowering(const TargetMachine &tm,
setOperationAction(ISD::BlockAddress, PtrVT, Custom);
setOperationAction(ISD::JumpTable, PtrVT, Custom);
+ // Implement custom stack allocations
+ setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
+ // Implement custom stack save and restore
+ setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
+ setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
+
// Compute derived properties from the register classes
computeRegisterProperties(STI.getRegisterInfo());
}
@@ -664,6 +670,43 @@ SDValue XtensaTargetLowering::LowerConstantPool(ConstantPoolSDNode *CP,
return getAddrPCRel(Result, DAG);
}
+SDValue XtensaTargetLowering::LowerSTACKSAVE(SDValue Op,
+ SelectionDAG &DAG) const {
+ unsigned sp = Xtensa::SP;
+ return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op), sp, Op.getValueType());
+}
+
+SDValue XtensaTargetLowering::LowerSTACKRESTORE(SDValue Op,
+ SelectionDAG &DAG) const {
+ unsigned sp = Xtensa::SP;
+ return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op), sp, Op.getOperand(1));
+}
+
+SDValue XtensaTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
+ SelectionDAG &DAG) const {
+ SDValue Chain = Op.getOperand(0); // Legalize the chain.
+ SDValue Size = Op.getOperand(1); // Legalize the size.
+ EVT VT = Size->getValueType(0);
+ SDLoc DL(Op);
+
+ // Round up Size to 32
+ SDValue Size1 =
+ DAG.getNode(ISD::ADD, DL, VT, Size, DAG.getConstant(31, DL, MVT::i32));
+ SDValue SizeRoundUp =
+ DAG.getNode(ISD::AND, DL, VT, Size1, DAG.getConstant(~31, DL, MVT::i32));
+
+ unsigned SPReg = Xtensa::SP;
+ SDValue SP = DAG.getCopyFromReg(Chain, DL, SPReg, VT);
+ SDValue NewSP = DAG.getNode(ISD::SUB, DL, VT, SP, SizeRoundUp); // Value
+ Chain = DAG.getCopyToReg(SP.getValue(1), DL, SPReg, NewSP); // Output chain
+
+ SDValue NewVal = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i32);
+ Chain = NewVal.getValue(1);
+
+ SDValue Ops[2] = {NewVal, Chain};
+ return DAG.getMergeValues(Ops, DL);
+}
+
SDValue XtensaTargetLowering::LowerOperation(SDValue Op,
SelectionDAG &DAG) const {
switch (Op.getOpcode()) {
@@ -679,6 +722,12 @@ SDValue XtensaTargetLowering::LowerOperation(SDValue Op,
return LowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
case ISD::ConstantPool:
return LowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
+ case ISD::STACKSAVE:
+ return LowerSTACKSAVE(Op, DAG);
+ case ISD::STACKRESTORE:
+ return LowerSTACKRESTORE(Op, DAG);
+ case ISD::DYNAMIC_STACKALLOC:
+ return LowerDYNAMIC_STACKALLOC(Op, DAG);
default:
llvm_unreachable("Unexpected node to lower");
}
diff --git a/llvm/lib/Target/Xtensa/XtensaISelLowering.h b/llvm/lib/Target/Xtensa/XtensaISelLowering.h
index 0fbfffe34d43..f2435cc18834 100644
--- a/llvm/lib/Target/Xtensa/XtensaISelLowering.h
+++ b/llvm/lib/Target/Xtensa/XtensaISelLowering.h
@@ -79,6 +79,10 @@ private:
SDValue LowerJumpTable(JumpTableSDNode *JT, SelectionDAG &DAG) const;
SDValue LowerConstantPool(ConstantPoolSDNode *CP, SelectionDAG &DAG) const;
+ SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
+ SDValue LowerSTACKSAVE(SDValue Op, SelectionDAG &DAG) const;
+ SDValue LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG) const;
+
SDValue getAddrPCRel(SDValue Op, SelectionDAG &DAG) const;
CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool IsVarArg) const;
--
2.40.1