Skip to content

Commit 65722fc

Browse files
committed
Use X86ISD::POP_FROM_X87_REG rather than ISD::CopyFromReg in call lowering
1 parent 51b94f8 commit 65722fc

10 files changed

+40
-41
lines changed

llvm/include/llvm/CodeGen/ISDOpcodes.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,11 +1400,6 @@ enum NodeType {
14001400
/// debugging purposes.
14011401
FAKE_USE,
14021402

1403-
/// OutChain = CHAIN_BARRIER(InChain) marks that optimizations should not
1404-
/// optimize any users of a chain that contains a CHAIN_BARRIER to use a chain
1405-
/// from a point earlier than the CHAIN_BARRIER.
1406-
CHAIN_BARRIER,
1407-
14081403
/// GC_TRANSITION_START/GC_TRANSITION_END - These operators mark the
14091404
/// beginning and end of GC transition sequence, and carry arbitrary
14101405
/// information that target might need for lowering. The first operand is

llvm/include/llvm/CodeGen/SelectionDAGISel.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,6 @@ class SelectionDAGISel {
472472
void Select_WRITE_REGISTER(SDNode *Op);
473473
void Select_UNDEF(SDNode *N);
474474
void Select_FAKE_USE(SDNode *N);
475-
void Select_CHAIN_BARRIER(SDNode *N);
476475
void CannotYetSelect(SDNode *N);
477476

478477
void Select_FREEZE(SDNode *N);

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,14 +1182,6 @@ static void VerifySDNode(SDNode *N, const TargetLowering *TLI) {
11821182
}
11831183
break;
11841184
}
1185-
case ISD::CHAIN_BARRIER: {
1186-
assert(N->getNumValues() == 1 && "Expected single result!");
1187-
assert(N->getNumOperands() == 1 && "Expected single operand!");
1188-
assert(N->getValueType(0) == MVT::Other &&
1189-
N->getOperand(0).getValueType() == MVT::Other &&
1190-
"Expected result and operand to be chains!");
1191-
break;
1192-
}
11931185
}
11941186
}
11951187
#endif // NDEBUG
@@ -2624,18 +2616,6 @@ bool SelectionDAG::expandMultipleResultFPLibCall(
26242616

26252617
auto [Call, CallChain] = TLI->LowerCallTo(CLI);
26262618

2627-
if (CallRetResNo && !Node->hasAnyUseOfValue(*CallRetResNo)) {
2628-
// This is needed for x87, which uses a floating-point stack. If (for
2629-
// example) the node to be expanded has two results one floating-point which
2630-
// is returned by the call, and one integer result, returned via an output
2631-
// pointer. If only the integer result is used then the `CopyFromReg` for
2632-
// the FP result may be optimized out. This prevents an FP stack pop from
2633-
// being emitted for it. The `CHAIN_BARRIER` node prevents optimizations
2634-
// from removing the `CopyFromReg` from the chain, and ensures the FP pop
2635-
// will be emitted.
2636-
CallChain = getNode(ISD::CHAIN_BARRIER, DL, MVT::Other, CallChain);
2637-
}
2638-
26392619
for (auto [ResNo, ResultPtr] : llvm::enumerate(ResultPtrs)) {
26402620
if (ResNo == CallRetResNo) {
26412621
Results.push_back(Call);

llvm/lib/CodeGen/SelectionDAG/SelectionDAGDumper.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,6 @@ std::string SDNode::getOperationName(const SelectionDAG *G) const {
462462
case ISD::LIFETIME_END: return "lifetime.end";
463463
case ISD::FAKE_USE:
464464
return "fake_use";
465-
case ISD::CHAIN_BARRIER:
466-
return "chain_barrier";
467465
case ISD::PSEUDO_PROBE:
468466
return "pseudoprobe";
469467
case ISD::GC_TRANSITION_START: return "gc_transition.start";

llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,12 +2507,6 @@ void SelectionDAGISel::Select_FAKE_USE(SDNode *N) {
25072507
N->getOperand(1), N->getOperand(0));
25082508
}
25092509

2510-
void SelectionDAGISel::Select_CHAIN_BARRIER(SDNode *N) {
2511-
// CHAIN_BARRIER exists only for SDAG. Remove it before lowering to MIs.
2512-
CurDAG->ReplaceAllUsesWith(SDValue(N, 0), N->getOperand(0));
2513-
CurDAG->RemoveDeadNode(N);
2514-
}
2515-
25162510
void SelectionDAGISel::Select_FREEZE(SDNode *N) {
25172511
// TODO: We don't have FREEZE pseudo-instruction in MachineInstr-level now.
25182512
// If FREEZE instruction is added later, the code below must be changed as
@@ -3287,9 +3281,6 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
32873281
case ISD::FAKE_USE:
32883282
Select_FAKE_USE(NodeToMatch);
32893283
return;
3290-
case ISD::CHAIN_BARRIER:
3291-
Select_CHAIN_BARRIER(NodeToMatch);
3292-
return;
32933284
case ISD::FREEZE:
32943285
Select_FREEZE(NodeToMatch);
32953286
return;

llvm/lib/Target/X86/X86ISelDAGToDAG.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6717,6 +6717,17 @@ void X86DAGToDAGISel::Select(SDNode *Node) {
67176717
ReplaceNode(Node, Res);
67186718
return;
67196719
}
6720+
case X86ISD::POP_FROM_X87_REG: {
6721+
SDValue Chain = Node->getOperand(0);
6722+
Register Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
6723+
SDValue Glue;
6724+
if (Node->getNumValues() == 3)
6725+
Glue = Node->getOperand(2);
6726+
SDValue Copy =
6727+
CurDAG->getCopyFromReg(Chain, dl, Reg, Node->getValueType(0), Glue);
6728+
ReplaceNode(Node, Copy.getNode());
6729+
return;
6730+
}
67206731
}
67216732

67226733
SelectCode(Node);

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35113,6 +35113,7 @@ const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
3511335113
NODE_NAME_CASE(CVTTP2SIS_SAE)
3511435114
NODE_NAME_CASE(CVTTP2UIS)
3511535115
NODE_NAME_CASE(MCVTTP2UIS)
35116+
NODE_NAME_CASE(POP_FROM_X87_REG)
3511635117
}
3511735118
return nullptr;
3511835119
#undef NODE_NAME_CASE

llvm/lib/Target/X86/X86ISelLowering.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ namespace llvm {
8181
// marker instruction.
8282
CALL_RVMARKER,
8383

84+
/// The same as ISD::CopyFromReg except that this node makes it explicit
85+
/// that it may lower to an x87 FPU stack pop. Optimizations should be more
86+
/// cautious when handling this node than a normal CopyFromReg to avoid
87+
/// removing a required FPU stack pop. A key requirement is optimizations
88+
/// should not optimize any users of a chain that contains a
89+
/// POP_FROM_X87_REG to use a chain from a point earlier than the
90+
/// POP_FROM_X87_REG (which may remove a required FPU stack pop).
91+
POP_FROM_X87_REG,
92+
8493
/// X86 compare and logical compare instructions.
8594
CMP,
8695
FCMP,

llvm/lib/Target/X86/X86ISelLoweringCall.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,15 @@ static SDValue lowerRegToMasks(const SDValue &ValArg, const EVT &ValVT,
10951095
return DAG.getBitcast(ValVT, ValReturned);
10961096
}
10971097

1098+
static SDValue getPopFromX87Reg(SelectionDAG &DAG, SDValue Chain,
1099+
const SDLoc &dl, Register Reg, EVT VT,
1100+
SDValue Glue) {
1101+
SDVTList VTs = DAG.getVTList(VT, MVT::Other, MVT::Glue);
1102+
SDValue Ops[] = {Chain, DAG.getRegister(Reg, VT), Glue};
1103+
return DAG.getNode(X86ISD::POP_FROM_X87_REG, dl, VTs,
1104+
ArrayRef(Ops, Glue.getNode() ? 3 : 2));
1105+
}
1106+
10981107
/// Lower the result values of a call into the
10991108
/// appropriate copies out of appropriate physical registers.
11001109
///
@@ -1145,8 +1154,8 @@ SDValue X86TargetLowering::LowerCallResult(
11451154
// If we prefer to use the value in xmm registers, copy it out as f80 and
11461155
// use a truncate to move it from fp stack reg to xmm reg.
11471156
bool RoundAfterCopy = false;
1148-
if ((VA.getLocReg() == X86::FP0 || VA.getLocReg() == X86::FP1) &&
1149-
isScalarFPTypeInSSEReg(VA.getValVT())) {
1157+
bool X87Result = VA.getLocReg() == X86::FP0 || VA.getLocReg() == X86::FP1;
1158+
if (X87Result && isScalarFPTypeInSSEReg(VA.getValVT())) {
11501159
if (!Subtarget.hasX87())
11511160
report_fatal_error("X87 register return with X87 disabled");
11521161
CopyVT = MVT::f80;
@@ -1160,8 +1169,12 @@ SDValue X86TargetLowering::LowerCallResult(
11601169
Val =
11611170
getv64i1Argument(VA, RVLocs[++I], Chain, DAG, dl, Subtarget, &InGlue);
11621171
} else {
1163-
Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), CopyVT, InGlue)
1164-
.getValue(1);
1172+
Chain =
1173+
X87Result
1174+
? getPopFromX87Reg(DAG, Chain, dl, VA.getLocReg(), CopyVT, InGlue)
1175+
.getValue(1)
1176+
: DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), CopyVT, InGlue)
1177+
.getValue(1);
11651178
Val = Chain.getValue(0);
11661179
InGlue = Chain.getValue(2);
11671180
}

llvm/lib/Target/X86/X86InstrFragments.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ def X86call_rvmarker : SDNode<"X86ISD::CALL_RVMARKER", SDT_X86Call,
210210
[SDNPHasChain, SDNPOutGlue, SDNPOptInGlue,
211211
SDNPVariadic]>;
212212

213+
def X86pop_from_x87_reg : SDNode<"X86ISD::POP_FROM_X87_REG", SDTypeProfile<0, 0, []>,
214+
[SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
213215

214216
def X86NoTrackCall : SDNode<"X86ISD::NT_CALL", SDT_X86Call,
215217
[SDNPHasChain, SDNPOutGlue, SDNPOptInGlue,

0 commit comments

Comments
 (0)