LLVM 24.0.0git
SparcISelDAGToDAG.cpp
Go to the documentation of this file.
1//===-- SparcISelDAGToDAG.cpp - A dag to dag inst selector for Sparc ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines an instruction selector for the SPARC target.
10//
11//===----------------------------------------------------------------------===//
12
14#include "SparcTargetMachine.h"
18using namespace llvm;
19
20#define DEBUG_TYPE "sparc-isel"
21#define PASS_NAME "SPARC DAG->DAG Pattern Instruction Selection"
22
23//===----------------------------------------------------------------------===//
24// Instruction Selector Implementation
25//===----------------------------------------------------------------------===//
26
27//===--------------------------------------------------------------------===//
28/// SparcDAGToDAGISel - SPARC specific code to select SPARC machine
29/// instructions for SelectionDAG operations.
30///
31namespace {
32class SparcDAGToDAGISel : public SelectionDAGISel {
33 /// Subtarget - Keep a pointer to the Sparc Subtarget around so that we can
34 /// make the right decision when generating code for different targets.
35 const SparcSubtarget *Subtarget = nullptr;
36
37public:
38 SparcDAGToDAGISel() = delete;
39
40 explicit SparcDAGToDAGISel(SparcTargetMachine &tm) : SelectionDAGISel(tm) {}
41
42 bool runOnMachineFunction(MachineFunction &MF) override {
43 Subtarget = &MF.getSubtarget<SparcSubtarget>();
45 }
46
47 void Select(SDNode *N) override;
48
49 // Complex Pattern Selectors.
50 bool SelectADDRrr(SDValue N, SDValue &R1, SDValue &R2);
51 bool SelectADDRri(SDValue N, SDValue &Base, SDValue &Offset);
52 bool SelectForceADDRrr(SDValue N, SDValue &Base, SDValue &Disp);
53
54 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
55 /// inline asm expressions.
56 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
57 InlineAsm::ConstraintCode ConstraintID,
58 std::vector<SDValue> &OutOps) override;
59
60 // Include the pieces autogenerated from the target description.
61#include "SparcGenDAGISel.inc"
62
63private:
64 SDNode* getGlobalBaseReg();
65 bool tryInlineAsm(SDNode *N);
66};
67
68class SparcDAGToDAGISelLegacy : public SelectionDAGISelLegacy {
69public:
70 static char ID;
71 explicit SparcDAGToDAGISelLegacy(SparcTargetMachine &tm)
72 : SelectionDAGISelLegacy(ID, std::make_unique<SparcDAGToDAGISel>(tm)) {}
73};
74} // end anonymous namespace
75
76char SparcDAGToDAGISelLegacy::ID = 0;
77
78INITIALIZE_PASS(SparcDAGToDAGISelLegacy, DEBUG_TYPE, PASS_NAME, false, false)
79
80SDNode* SparcDAGToDAGISel::getGlobalBaseReg() {
81 Register GlobalBaseReg = Subtarget->getInstrInfo()->getGlobalBaseReg(MF);
82 return CurDAG->getRegister(GlobalBaseReg,
83 TLI->getPointerTy(CurDAG->getDataLayout()))
84 .getNode();
85}
86
87bool SparcDAGToDAGISel::SelectADDRri(SDValue Addr,
89 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
90 Base = CurDAG->getTargetFrameIndex(
91 FIN->getIndex(), TLI->getPointerTy(CurDAG->getDataLayout()));
92 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32);
93 return true;
94 }
98 return false; // direct calls.
99
100 if (Addr.getOpcode() == ISD::ADD) {
101 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
102 if (isInt<13>(CN->getSExtValue())) {
103 if (FrameIndexSDNode *FIN =
105 // Constant offset from frame ref.
106 Base = CurDAG->getTargetFrameIndex(
107 FIN->getIndex(), TLI->getPointerTy(CurDAG->getDataLayout()));
108 } else {
109 Base = Addr.getOperand(0);
110 }
111 Offset = CurDAG->getSignedTargetConstant(CN->getSExtValue(),
112 SDLoc(Addr), MVT::i32);
113 return true;
114 }
115 }
116 if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
117 Base = Addr.getOperand(1);
118 Offset = Addr.getOperand(0).getOperand(0);
119 return true;
120 }
121 if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
122 Base = Addr.getOperand(0);
123 Offset = Addr.getOperand(1).getOperand(0);
124 return true;
125 }
126 }
127 Base = Addr;
128 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32);
129 return true;
130}
131
132bool SparcDAGToDAGISel::SelectADDRrr(SDValue Addr, SDValue &R1, SDValue &R2) {
133 if (Addr.getOpcode() == ISD::FrameIndex) return false;
134 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
137 return false; // direct calls.
138
139 if (Addr.getOpcode() == ISD::ADD) {
140 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
141 if (isInt<13>(CN->getSExtValue()))
142 return false; // Let the reg+imm pattern catch this!
143 if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
144 Addr.getOperand(1).getOpcode() == SPISD::Lo)
145 return false; // Let the reg+imm pattern catch this!
146 R1 = Addr.getOperand(0);
147 R2 = Addr.getOperand(1);
148 return true;
149 }
150
151 R1 = Addr;
152 R2 = CurDAG->getRegister(SP::G0, TLI->getPointerTy(CurDAG->getDataLayout()));
153 return true;
154}
155
156bool SparcDAGToDAGISel::SelectForceADDRrr(SDValue Addr, SDValue &Base,
157 SDValue &Disp) {
158 if (SelectADDRrr(Addr, Base, Disp))
159 return true;
160
161 // Otherwise we'll use the full address in base and set the offset part to
162 // zero.
163 Base = Addr;
164 Disp =
165 CurDAG->getRegister(SP::G0, TLI->getPointerTy(CurDAG->getDataLayout()));
166 return true;
167}
168
169// Re-assemble i64 arguments split up in SelectionDAGBuilder's
170// visitInlineAsm / GetRegistersForValue functions.
171//
172// Note: This function was copied from, and is essentially identical
173// to ARMISelDAGToDAG::SelectInlineAsm. It is very unfortunate that
174// such hacking-up is necessary; a rethink of how inline asm operands
175// are handled may be in order to make doing this more sane.
176//
177// TODO: fix inline asm support so I can simply tell it that 'i64'
178// inputs to asm need to be allocated to the IntPair register type,
179// and have that work. Then, delete this function.
180bool SparcDAGToDAGISel::tryInlineAsm(SDNode *N){
181 std::vector<SDValue> AsmNodeOperands;
182 InlineAsm::Flag Flag;
183 bool Changed = false;
184 unsigned NumOps = N->getNumOperands();
185
186 // Normally, i64 data is bounded to two arbitrary GPRs for "%r"
187 // constraint. However, some instructions (e.g. ldd/std) require
188 // (even/even+1) GPRs.
189
190 // So, here, we check for this case, and mutate the inlineasm to use
191 // a single IntPair register instead, which guarantees such even/odd
192 // placement.
193
194 SDLoc dl(N);
195 SDValue Glue = N->getGluedNode() ? N->getOperand(NumOps - 1) : SDValue();
196
197 SmallVector<bool, 8> OpChanged;
198 // Glue node will be appended late.
199 for(unsigned i = 0, e = N->getGluedNode() ? NumOps - 1 : NumOps; i < e; ++i) {
200 SDValue op = N->getOperand(i);
201 AsmNodeOperands.push_back(op);
202
204 continue;
205
206 if (const auto *C = dyn_cast<ConstantSDNode>(N->getOperand(i)))
207 Flag = InlineAsm::Flag(C->getZExtValue());
208 else
209 continue;
210
211 // Immediate operands to inline asm in the SelectionDAG are modeled with
212 // two operands. The first is a constant of value InlineAsm::Kind::Imm, and
213 // the second is a constant with the value of the immediate. If we get here
214 // and we have a Kind::Imm, skip the next operand, and continue.
215 if (Flag.isImmKind()) {
216 SDValue op = N->getOperand(++i);
217 AsmNodeOperands.push_back(op);
218 continue;
219 }
220
221 const unsigned NumRegs = Flag.getNumOperandRegisters();
222 if (NumRegs)
223 OpChanged.push_back(false);
224
225 unsigned DefIdx = 0;
226 bool IsTiedToChangedOp = false;
227 // If it's a use that is tied with a previous def, it has no
228 // reg class constraint.
229 if (Changed && Flag.isUseOperandTiedToDef(DefIdx))
230 IsTiedToChangedOp = OpChanged[DefIdx];
231
232 if (!Flag.isRegUseKind() && !Flag.isRegDefKind() &&
233 !Flag.isRegDefEarlyClobberKind())
234 continue;
235
236 unsigned RC;
237 const bool HasRC = Flag.hasRegClassConstraint(RC);
238 if ((!IsTiedToChangedOp && (!HasRC || RC != SP::IntRegsRegClassID))
239 || NumRegs != 2)
240 continue;
241
242 assert((i+2 < NumOps) && "Invalid number of operands in inline asm");
243 SDValue V0 = N->getOperand(i+1);
244 SDValue V1 = N->getOperand(i+2);
245 Register Reg0 = cast<RegisterSDNode>(V0)->getReg();
246 Register Reg1 = cast<RegisterSDNode>(V1)->getReg();
247 SDValue PairedReg;
248 MachineRegisterInfo &MRI = MF->getRegInfo();
249
250 if (Flag.isRegDefKind() || Flag.isRegDefEarlyClobberKind()) {
251 // Replace the two GPRs with 1 GPRPair and copy values from GPRPair to
252 // the original GPRs.
253
254 Register GPVR = MRI.createVirtualRegister(&SP::IntPairRegClass);
255 PairedReg = CurDAG->getRegister(GPVR, MVT::v2i32);
256 SDValue Chain = SDValue(N,0);
257
258 SDNode *GU = N->getGluedUser();
259 SDValue RegCopy = CurDAG->getCopyFromReg(Chain, dl, GPVR, MVT::v2i32,
260 Chain.getValue(1));
261
262 // Extract values from a GPRPair reg and copy to the original GPR reg.
263 SDValue Sub0 = CurDAG->getTargetExtractSubreg(SP::sub_even, dl, MVT::i32,
264 RegCopy);
265 SDValue Sub1 = CurDAG->getTargetExtractSubreg(SP::sub_odd, dl, MVT::i32,
266 RegCopy);
267 SDValue T0 = CurDAG->getCopyToReg(Sub0, dl, Reg0, Sub0,
268 RegCopy.getValue(1));
269 SDValue T1 = CurDAG->getCopyToReg(Sub1, dl, Reg1, Sub1, T0.getValue(1));
270
271 // Update the original glue user.
272 std::vector<SDValue> Ops(GU->op_begin(), GU->op_end()-1);
273 Ops.push_back(T1.getValue(1));
274 CurDAG->UpdateNodeOperands(GU, Ops);
275 } else {
276 // For Kind == InlineAsm::Kind::RegUse, we first copy two GPRs into a
277 // GPRPair and then pass the GPRPair to the inline asm.
278 SDValue Chain = AsmNodeOperands[InlineAsm::Op_InputChain];
279
280 // As REG_SEQ doesn't take RegisterSDNode, we copy them first.
281 SDValue T0 = CurDAG->getCopyFromReg(Chain, dl, Reg0, MVT::i32,
282 Chain.getValue(1));
283 SDValue T1 = CurDAG->getCopyFromReg(Chain, dl, Reg1, MVT::i32,
284 T0.getValue(1));
285 SDValue Pair = SDValue(
286 CurDAG->getMachineNode(
287 TargetOpcode::REG_SEQUENCE, dl, MVT::v2i32,
288 {
289 CurDAG->getTargetConstant(SP::IntPairRegClassID, dl,
290 MVT::i32),
291 T0,
292 CurDAG->getTargetConstant(SP::sub_even, dl, MVT::i32),
293 T1,
294 CurDAG->getTargetConstant(SP::sub_odd, dl, MVT::i32),
295 }),
296 0);
297
298 // Copy REG_SEQ into a GPRPair-typed VR and replace the original two
299 // i32 VRs of inline asm with it.
300 Register GPVR = MRI.createVirtualRegister(&SP::IntPairRegClass);
301 PairedReg = CurDAG->getRegister(GPVR, MVT::v2i32);
302 Chain = CurDAG->getCopyToReg(T1, dl, GPVR, Pair, T1.getValue(1));
303
304 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
305 Glue = Chain.getValue(1);
306 }
307
308 Changed = true;
309
310 if(PairedReg.getNode()) {
311 OpChanged[OpChanged.size() -1 ] = true;
312 Flag = InlineAsm::Flag(Flag.getKind(), 1 /* RegNum*/);
313 if (IsTiedToChangedOp)
314 Flag.setMatchingOp(DefIdx);
315 else
316 Flag.setRegClass(SP::IntPairRegClassID);
317 // Replace the current flag.
318 AsmNodeOperands[AsmNodeOperands.size() -1] = CurDAG->getTargetConstant(
319 Flag, dl, MVT::i32);
320 // Add the new register node and skip the original two GPRs.
321 AsmNodeOperands.push_back(PairedReg);
322 // Skip the next two GPRs.
323 i += 2;
324 }
325 }
326
327 if (Glue.getNode())
328 AsmNodeOperands.push_back(Glue);
329 if (!Changed)
330 return false;
331
332 SelectInlineAsmMemoryOperands(AsmNodeOperands, SDLoc(N));
333
334 SDValue New = CurDAG->getNode(N->getOpcode(), SDLoc(N),
335 CurDAG->getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
336 New->setNodeId(-1);
337 ReplaceNode(N, New.getNode());
338 return true;
339}
340
341void SparcDAGToDAGISel::Select(SDNode *N) {
342 SDLoc dl(N);
343 if (N->isMachineOpcode()) {
344 N->setNodeId(-1);
345 return; // Already selected.
346 }
347
348 switch (N->getOpcode()) {
349 default: break;
350 case ISD::INLINEASM:
351 case ISD::INLINEASM_BR: {
352 if (tryInlineAsm(N))
353 return;
354 break;
355 }
356 case SPISD::GLOBAL_BASE_REG:
357 ReplaceNode(N, getGlobalBaseReg());
358 return;
359
360 case ISD::SDIV:
361 case ISD::UDIV: {
362 // sdivx / udivx handle 64-bit divides.
363 if (N->getValueType(0) == MVT::i64)
364 break;
365 // FIXME: should use a custom expander to expose the SRA to the dag.
366 SDValue DivLHS = N->getOperand(0);
367 SDValue DivRHS = N->getOperand(1);
368
369 // Set the Y register to the high-part.
370 SDValue TopPart;
371 if (N->getOpcode() == ISD::SDIV) {
372 TopPart = SDValue(CurDAG->getMachineNode(SP::SRAri, dl, MVT::i32, DivLHS,
373 CurDAG->getTargetConstant(31, dl, MVT::i32)),
374 0);
375 } else {
376 TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
377 }
378 TopPart = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, SP::Y, TopPart,
379 SDValue())
380 .getValue(1);
381
382 // FIXME: Handle div by immediate.
383 unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
384 CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS, TopPart);
385 return;
386 }
387 }
388
389 SelectCode(N);
390}
391
392
393/// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
394/// inline asm expressions.
395bool SparcDAGToDAGISel::SelectInlineAsmMemoryOperand(
396 const SDValue &Op, InlineAsm::ConstraintCode ConstraintID,
397 std::vector<SDValue> &OutOps) {
398 SDValue Op0, Op1;
399 switch (ConstraintID) {
400 default: return true;
401 case InlineAsm::ConstraintCode::o:
402 case InlineAsm::ConstraintCode::m: // memory
403 if (!SelectADDRrr(Op, Op0, Op1))
404 SelectADDRri(Op, Op0, Op1);
405 break;
406 }
407
408 OutOps.push_back(Op0);
409 OutOps.push_back(Op1);
410 return false;
411}
412
413/// createSparcISelDag - This pass converts a legalized DAG into a
414/// SPARC-specific DAG, ready for instruction scheduling.
415///
417 return new SparcDAGToDAGISelLegacy(TM);
418}
return SDValue()
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define DEBUG_TYPE
#define op(i)
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define R2(n)
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define T1
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
#define PASS_NAME
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Represents one node in the SelectionDAG.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
op_iterator op_end() const
op_iterator op_begin() const
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getValue(unsigned R) const
const SDValue & getOperand(unsigned i) const
unsigned getOpcode() const
SelectionDAGISel - This is the common base class used for SelectionDAG-based pattern-matching instruc...
virtual bool runOnMachineFunction(MachineFunction &mf)
void push_back(const T &Elt)
Changed
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:264
@ TargetExternalSymbol
Definition ISDOpcodes.h:190
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition ISDOpcodes.h:185
@ INLINEASM_BR
INLINEASM_BR - Branching version of inline asm. Used by asm-goto.
@ INLINEASM
INLINEASM - Represents an inline asm block.
@ TargetGlobalTLSAddress
Definition ISDOpcodes.h:186
Flag
These should be considered private to the implementation of the MCInstrDesc class.
@ GlobalBaseReg
The result of the mflr at function entry, used for PIC code.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
FunctionPass * createSparcISelDag(SparcTargetMachine &TM)
createSparcISelDag - This pass converts a legalized DAG into a SPARC-specific DAG,...
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
#define N