1//=- LoongArchISelDAGToDAG.cpp - A dag to dag inst selector for LoongArch -===//
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 LoongArch target.
10//
11//===----------------------------------------------------------------------===//
12
13#include "LoongArchISelDAGToDAG.h"
14#include "LoongArchISelLowering.h"
15#include "MCTargetDesc/LoongArchMCTargetDesc.h"
16#include "MCTargetDesc/LoongArchMatInt.h"
17#include "llvm/Support/KnownBits.h"
18#include "llvm/Support/raw_ostream.h"
19
20using namespace llvm;
21
22#define DEBUG_TYPE "loongarch-isel"
23#define PASS_NAME "LoongArch DAG->DAG Pattern Instruction Selection"
24
25char LoongArchDAGToDAGISelLegacy::ID;
26
27LoongArchDAGToDAGISelLegacy::LoongArchDAGToDAGISelLegacy(
28 LoongArchTargetMachine &TM, CodeGenOptLevel OptLevel)
29 : SelectionDAGISelLegacy(
30 ID, std::make_unique<LoongArchDAGToDAGISel>(args&: TM, args&: OptLevel)) {}
31
32INITIALIZE_PASS(LoongArchDAGToDAGISelLegacy, DEBUG_TYPE, PASS_NAME, false,
33 false)
34
35void LoongArchDAGToDAGISel::Select(SDNode *Node) {
36 // If we have a custom node, we have already selected.
37 if (Node->isMachineOpcode()) {
38 LLVM_DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << "\n");
39 Node->setNodeId(-1);
40 return;
41 }
42
43 // Instruction Selection not handled by the auto-generated tablegen selection
44 // should be handled here.
45 unsigned Opcode = Node->getOpcode();
46 MVT GRLenVT = Subtarget->getGRLenVT();
47 SDLoc DL(Node);
48 MVT VT = Node->getSimpleValueType(ResNo: 0);
49
50 switch (Opcode) {
51 default:
52 break;
53 case ISD::Constant: {
54 int64_t Imm = cast<ConstantSDNode>(Val: Node)->getSExtValue();
55 if (Imm == 0 && VT == GRLenVT) {
56 SDValue New = CurDAG->getCopyFromReg(Chain: CurDAG->getEntryNode(), dl: DL,
57 Reg: LoongArch::R0, VT: GRLenVT);
58 ReplaceNode(F: Node, T: New.getNode());
59 return;
60 }
61 SDNode *Result = nullptr;
62 SDValue SrcReg = CurDAG->getRegister(Reg: LoongArch::R0, VT: GRLenVT);
63 // The instructions in the sequence are handled here.
64 for (LoongArchMatInt::Inst &Inst : LoongArchMatInt::generateInstSeq(Val: Imm)) {
65 SDValue SDImm = CurDAG->getSignedTargetConstant(Val: Inst.Imm, DL, VT: GRLenVT);
66 switch (Inst.Opc) {
67 case LoongArch::LU12I_W:
68 Result = CurDAG->getMachineNode(Opcode: Inst.Opc, dl: DL, VT: GRLenVT, Op1: SDImm);
69 break;
70 case LoongArch::ADDI_W:
71 case LoongArch::ORI:
72 case LoongArch::LU32I_D:
73 case LoongArch::LU52I_D:
74 case LoongArch::SLLI_D:
75 Result = CurDAG->getMachineNode(Opcode: Inst.Opc, dl: DL, VT: GRLenVT, Op1: SrcReg, Op2: SDImm);
76 break;
77 case LoongArch::BSTRINS_D:
78 Result = CurDAG->getMachineNode(
79 Opcode: Inst.Opc, dl: DL, VT: GRLenVT,
80 Ops: {SrcReg, SrcReg,
81 CurDAG->getSignedTargetConstant(Val: Inst.Imm >> 32, DL, VT: GRLenVT),
82 CurDAG->getTargetConstant(Val: Inst.Imm & 0xFF, DL, VT: GRLenVT)});
83 break;
84 default:
85 llvm_unreachable("unexpected opcode generated by LoongArchMatInt");
86 }
87 SrcReg = SDValue(Result, 0);
88 }
89
90 ReplaceNode(F: Node, T: Result);
91 return;
92 }
93 case ISD::FrameIndex: {
94 SDValue Imm = CurDAG->getTargetConstant(Val: 0, DL, VT: GRLenVT);
95 int FI = cast<FrameIndexSDNode>(Val: Node)->getIndex();
96 SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT);
97 unsigned ADDIOp =
98 Subtarget->is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
99 ReplaceNode(F: Node, T: CurDAG->getMachineNode(Opcode: ADDIOp, dl: DL, VT, Op1: TFI, Op2: Imm));
100 return;
101 }
102 case ISD::BITCAST: {
103 if (VT.is128BitVector() || VT.is256BitVector()) {
104 ReplaceUses(F: SDValue(Node, 0), T: Node->getOperand(Num: 0));
105 CurDAG->RemoveDeadNode(N: Node);
106 return;
107 }
108 break;
109 }
110 case ISD::BUILD_VECTOR: {
111 // Select appropriate [x]vrepli.[bhwd] instructions for constant splats of
112 // 128/256-bit when LSX/LASX is enabled.
113 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Val: Node);
114 APInt SplatValue, SplatUndef;
115 unsigned SplatBitSize;
116 bool HasAnyUndefs;
117 unsigned Op = 0;
118 EVT ResTy = BVN->getValueType(ResNo: 0);
119 bool Is128Vec = BVN->getValueType(ResNo: 0).is128BitVector();
120 bool Is256Vec = BVN->getValueType(ResNo: 0).is256BitVector();
121 SDNode *Res;
122
123 if (!Subtarget->hasExtLSX() || (!Is128Vec && !Is256Vec))
124 break;
125 if (!BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
126 HasAnyUndefs, MinSplatBits: 8))
127 break;
128
129 // If we have a signed 10 bit integer, we can splat it directly.
130 if (SplatValue.isSignedIntN(N: 10)) {
131 switch (SplatBitSize) {
132 default:
133 break;
134 case 8:
135 Op = Is256Vec ? LoongArch::PseudoXVREPLI_B : LoongArch::PseudoVREPLI_B;
136 break;
137 case 16:
138 Op = Is256Vec ? LoongArch::PseudoXVREPLI_H : LoongArch::PseudoVREPLI_H;
139 break;
140 case 32:
141 Op = Is256Vec ? LoongArch::PseudoXVREPLI_W : LoongArch::PseudoVREPLI_W;
142 break;
143 case 64:
144 Op = Is256Vec ? LoongArch::PseudoXVREPLI_D : LoongArch::PseudoVREPLI_D;
145 break;
146 }
147
148 EVT EleType = ResTy.getVectorElementType();
149 APInt Val = SplatValue.sextOrTrunc(width: EleType.getSizeInBits());
150 SDValue Imm = CurDAG->getTargetConstant(Val, DL, VT: EleType);
151 Res = CurDAG->getMachineNode(Opcode: Op, dl: DL, VT: ResTy, Op1: Imm);
152 ReplaceNode(F: Node, T: Res);
153 return;
154 }
155
156 // Select appropriate [x]vldi instructions for some special constant splats,
157 // where the immediate value `imm[12] == 1` for used [x]vldi instructions.
158 const auto &TLI =
159 *static_cast<const LoongArchTargetLowering *>(getTargetLowering());
160 std::pair<bool, uint64_t> ConvertVLDI =
161 TLI.isImmVLDILegalForMode1(SplatValue, SplatBitSize);
162 if (ConvertVLDI.first) {
163 Op = Is256Vec ? LoongArch::XVLDI : LoongArch::VLDI;
164 SDValue Imm = CurDAG->getSignedTargetConstant(
165 Val: SignExtend32<13>(X: ConvertVLDI.second), DL, VT: MVT::i32);
166 Res = CurDAG->getMachineNode(Opcode: Op, dl: DL, VT: ResTy, Op1: Imm);
167 ReplaceNode(F: Node, T: Res);
168 return;
169 }
170 break;
171 }
172 }
173
174 // Select the default instruction.
175 SelectCode(N: Node);
176}
177
178bool LoongArchDAGToDAGISel::SelectInlineAsmMemoryOperand(
179 const SDValue &Op, InlineAsm::ConstraintCode ConstraintID,
180 std::vector<SDValue> &OutOps) {
181 SDValue Base = Op;
182 SDValue Offset =
183 CurDAG->getTargetConstant(Val: 0, DL: SDLoc(Op), VT: Subtarget->getGRLenVT());
184 switch (ConstraintID) {
185 default:
186 llvm_unreachable("unexpected asm memory constraint");
187 // Reg+Reg addressing.
188 case InlineAsm::ConstraintCode::k:
189 Base = Op.getOperand(i: 0);
190 Offset = Op.getOperand(i: 1);
191 break;
192 // Reg+simm12 addressing.
193 case InlineAsm::ConstraintCode::m:
194 if (CurDAG->isBaseWithConstantOffset(Op)) {
195 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val: Op.getOperand(i: 1));
196 if (isIntN(N: 12, x: CN->getSExtValue())) {
197 Base = Op.getOperand(i: 0);
198 Offset = CurDAG->getTargetConstant(Val: CN->getZExtValue(), DL: SDLoc(Op),
199 VT: Op.getValueType());
200 }
201 }
202 break;
203 // Reg+0 addressing.
204 case InlineAsm::ConstraintCode::ZB:
205 break;
206 // Reg+(simm14<<2) addressing.
207 case InlineAsm::ConstraintCode::ZC:
208 if (CurDAG->isBaseWithConstantOffset(Op)) {
209 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val: Op.getOperand(i: 1));
210 if (isIntN(N: 16, x: CN->getSExtValue()) &&
211 isAligned(Lhs: Align(4ULL), SizeInBytes: CN->getZExtValue())) {
212 Base = Op.getOperand(i: 0);
213 Offset = CurDAG->getTargetConstant(Val: CN->getZExtValue(), DL: SDLoc(Op),
214 VT: Op.getValueType());
215 }
216 }
217 break;
218 }
219 OutOps.push_back(x: Base);
220 OutOps.push_back(x: Offset);
221 return false;
222}
223
224bool LoongArchDAGToDAGISel::SelectBaseAddr(SDValue Addr, SDValue &Base) {
225 // If this is FrameIndex, select it directly. Otherwise just let it get
226 // selected to a register independently.
227 if (auto *FIN = dyn_cast<FrameIndexSDNode>(Val&: Addr))
228 Base =
229 CurDAG->getTargetFrameIndex(FI: FIN->getIndex(), VT: Subtarget->getGRLenVT());
230 else
231 Base = Addr;
232 return true;
233}
234
235// Fold constant addresses.
236bool LoongArchDAGToDAGISel::SelectAddrConstant(SDValue Addr, SDValue &Base,
237 SDValue &Offset) {
238 SDLoc DL(Addr);
239 MVT VT = Addr.getSimpleValueType();
240
241 if (!isa<ConstantSDNode>(Val: Addr))
242 return false;
243
244 // If the constant is a simm12, we can fold the whole constant and use R0 as
245 // the base.
246 int64_t CVal = cast<ConstantSDNode>(Val&: Addr)->getSExtValue();
247 if (!isInt<12>(x: CVal))
248 return false;
249 Base = CurDAG->getRegister(Reg: LoongArch::R0, VT);
250 Offset = CurDAG->getSignedTargetConstant(Val: SignExtend64<12>(x: CVal), DL, VT);
251 return true;
252}
253
254bool LoongArchDAGToDAGISel::selectNonFIBaseAddr(SDValue Addr, SDValue &Base) {
255 // If this is FrameIndex, don't select it.
256 if (isa<FrameIndexSDNode>(Val: Addr))
257 return false;
258 Base = Addr;
259 return true;
260}
261
262bool LoongArchDAGToDAGISel::SelectAddrRegImm12(SDValue Addr, SDValue &Base,
263 SDValue &Offset) {
264 SDLoc DL(Addr);
265 MVT VT = Addr.getSimpleValueType();
266
267 // The address is the result of an ADD. Here we only consider reg+simm12.
268 if (CurDAG->isBaseWithConstantOffset(Op: Addr)) {
269 int64_t Imm = cast<ConstantSDNode>(Val: Addr.getOperand(i: 1))->getSExtValue();
270 if (isInt<12>(x: Imm)) {
271 Base = Addr.getOperand(i: 0);
272 Offset = CurDAG->getSignedTargetConstant(Val: SignExtend64<12>(x: Imm), DL, VT);
273 return true;
274 }
275 }
276
277 // Otherwise, we assume Addr as the base address and use constant 0 as the
278 // offset.
279 Base = Addr;
280 Offset = CurDAG->getTargetConstant(Val: 0, DL, VT);
281 return true;
282}
283
284bool LoongArchDAGToDAGISel::selectShiftMask(SDValue N, unsigned ShiftWidth,
285 SDValue &ShAmt) {
286 // Shift instructions on LoongArch only read the lower 5 or 6 bits of the
287 // shift amount. If there is an AND on the shift amount, we can bypass it if
288 // it doesn't affect any of those bits.
289 if (N.getOpcode() == ISD::AND && isa<ConstantSDNode>(Val: N.getOperand(i: 1))) {
290 const APInt &AndMask = N->getConstantOperandAPInt(Num: 1);
291
292 // Since the max shift amount is a power of 2 we can subtract 1 to make a
293 // mask that covers the bits needed to represent all shift amounts.
294 assert(isPowerOf2_32(ShiftWidth) && "Unexpected max shift amount!");
295 APInt ShMask(AndMask.getBitWidth(), ShiftWidth - 1);
296
297 if (ShMask.isSubsetOf(RHS: AndMask)) {
298 ShAmt = N.getOperand(i: 0);
299 return true;
300 }
301
302 // SimplifyDemandedBits may have optimized the mask so try restoring any
303 // bits that are known zero.
304 KnownBits Known = CurDAG->computeKnownBits(Op: N->getOperand(Num: 0));
305 if (ShMask.isSubsetOf(RHS: AndMask | Known.Zero)) {
306 ShAmt = N.getOperand(i: 0);
307 return true;
308 }
309 } else if (N.getOpcode() == LoongArchISD::BSTRPICK) {
310 // Similar to the above AND, if there is a BSTRPICK on the shift amount, we
311 // can bypass it.
312 assert(isPowerOf2_32(ShiftWidth) && "Unexpected max shift amount!");
313 assert(isa<ConstantSDNode>(N.getOperand(1)) && "Illegal msb operand!");
314 assert(isa<ConstantSDNode>(N.getOperand(2)) && "Illegal lsb operand!");
315 uint64_t msb = N.getConstantOperandVal(i: 1), lsb = N.getConstantOperandVal(i: 2);
316 if (lsb == 0 && Log2_32(Value: ShiftWidth) <= msb + 1) {
317 ShAmt = N.getOperand(i: 0);
318 return true;
319 }
320 } else if (N.getOpcode() == ISD::SUB &&
321 isa<ConstantSDNode>(Val: N.getOperand(i: 0))) {
322 uint64_t Imm = N.getConstantOperandVal(i: 0);
323 // If we are shifting by N-X where N == 0 mod Size, then just shift by -X to
324 // generate a NEG instead of a SUB of a constant.
325 if (Imm != 0 && Imm % ShiftWidth == 0) {
326 SDLoc DL(N);
327 EVT VT = N.getValueType();
328 SDValue Zero =
329 CurDAG->getCopyFromReg(Chain: CurDAG->getEntryNode(), dl: DL, Reg: LoongArch::R0, VT);
330 unsigned NegOpc = VT == MVT::i64 ? LoongArch::SUB_D : LoongArch::SUB_W;
331 MachineSDNode *Neg =
332 CurDAG->getMachineNode(Opcode: NegOpc, dl: DL, VT, Op1: Zero, Op2: N.getOperand(i: 1));
333 ShAmt = SDValue(Neg, 0);
334 return true;
335 }
336 }
337
338 ShAmt = N;
339 return true;
340}
341
342bool LoongArchDAGToDAGISel::selectSExti32(SDValue N, SDValue &Val) {
343 if (N.getOpcode() == ISD::SIGN_EXTEND_INREG &&
344 cast<VTSDNode>(Val: N.getOperand(i: 1))->getVT() == MVT::i32) {
345 Val = N.getOperand(i: 0);
346 return true;
347 }
348 if (N.getOpcode() == LoongArchISD::BSTRPICK &&
349 N.getConstantOperandVal(i: 1) < UINT64_C(0X1F) &&
350 N.getConstantOperandVal(i: 2) == UINT64_C(0)) {
351 Val = N;
352 return true;
353 }
354 MVT VT = N.getSimpleValueType();
355 if (CurDAG->ComputeNumSignBits(Op: N) > (VT.getSizeInBits() - 32)) {
356 Val = N;
357 return true;
358 }
359
360 return false;
361}
362
363bool LoongArchDAGToDAGISel::selectZExti32(SDValue N, SDValue &Val) {
364 if (N.getOpcode() == ISD::AND) {
365 auto *C = dyn_cast<ConstantSDNode>(Val: N.getOperand(i: 1));
366 if (C && C->getZExtValue() == UINT64_C(0xFFFFFFFF)) {
367 Val = N.getOperand(i: 0);
368 return true;
369 }
370 }
371 MVT VT = N.getSimpleValueType();
372 APInt Mask = APInt::getHighBitsSet(numBits: VT.getSizeInBits(), hiBitsSet: 32);
373 if (CurDAG->MaskedValueIsZero(Op: N, Mask)) {
374 Val = N;
375 return true;
376 }
377
378 return false;
379}
380
381bool LoongArchDAGToDAGISel::selectVSplat(SDNode *N, APInt &Imm,
382 unsigned MinSizeInBits) const {
383 if (!Subtarget->hasExtLSX())
384 return false;
385
386 BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(Val: N);
387
388 if (!Node)
389 return false;
390
391 APInt SplatValue, SplatUndef;
392 unsigned SplatBitSize;
393 bool HasAnyUndefs;
394
395 if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs,
396 MinSplatBits: MinSizeInBits, /*IsBigEndian=*/isBigEndian: false))
397 return false;
398
399 Imm = SplatValue;
400
401 return true;
402}
403
404template <unsigned ImmBitSize, unsigned EltBitSize, bool IsSigned>
405bool LoongArchDAGToDAGISel::selectVSplatImm(SDValue N, SDValue &SplatVal) {
406 APInt ImmValue;
407 EVT EltTy = N->getValueType(ResNo: 0).getVectorElementType();
408 unsigned EltBitWidth = EltBitSize ? EltBitSize : EltTy.getSizeInBits();
409
410 if (N->getOpcode() == ISD::BITCAST)
411 N = N->getOperand(Num: 0);
412
413 if (selectVSplat(N: N.getNode(), Imm&: ImmValue, MinSizeInBits: EltBitWidth) &&
414 ImmValue.getBitWidth() == EltBitWidth) {
415 if (IsSigned && ImmValue.isSignedIntN(N: ImmBitSize)) {
416 SplatVal = CurDAG->getSignedTargetConstant(
417 Val: ImmValue.getSExtValue(), DL: SDLoc(N), VT: Subtarget->getGRLenVT());
418 return true;
419 }
420 if (!IsSigned && ImmValue.isIntN(N: ImmBitSize)) {
421 SplatVal = CurDAG->getTargetConstant(Val: ImmValue.getZExtValue(), DL: SDLoc(N),
422 VT: Subtarget->getGRLenVT());
423 return true;
424 }
425 }
426
427 return false;
428}
429
430template <unsigned ImmBitSize>
431bool LoongArchDAGToDAGISel::selectVSplatImmNeg(SDValue N,
432 SDValue &SplatVal) const {
433 APInt ImmValue;
434 EVT EltTy = N->getValueType(ResNo: 0).getVectorElementType();
435
436 if (N->getOpcode() == ISD::BITCAST)
437 N = N->getOperand(Num: 0);
438
439 if (selectVSplat(N: N.getNode(), Imm&: ImmValue, MinSizeInBits: EltTy.getSizeInBits()) &&
440 ImmValue.getBitWidth() == EltTy.getSizeInBits()) {
441 if ((-ImmValue).isIntN(N: ImmBitSize)) {
442 SplatVal = CurDAG->getTargetConstant(Val: -ImmValue.getSExtValue(), DL: SDLoc(N),
443 VT: Subtarget->getGRLenVT());
444 return true;
445 }
446 }
447
448 return false;
449}
450
451template <unsigned EltBitSize>
452bool LoongArchDAGToDAGISel::selectVSplatUimmInvPow2(SDValue N,
453 SDValue &SplatImm) const {
454 APInt ImmValue;
455 EVT EltTy = N->getValueType(ResNo: 0).getVectorElementType();
456 unsigned EltBitWidth = EltBitSize ? EltBitSize : EltTy.getSizeInBits();
457
458 if (N->getOpcode() == ISD::BITCAST)
459 N = N->getOperand(Num: 0);
460
461 if (selectVSplat(N: N.getNode(), Imm&: ImmValue, MinSizeInBits: EltBitWidth) &&
462 ImmValue.getBitWidth() == EltBitWidth) {
463 int32_t Log2 = (~ImmValue).exactLogBase2();
464
465 if (Log2 != -1) {
466 SplatImm = CurDAG->getSignedTargetConstant(Val: Log2, DL: SDLoc(N), VT: EltTy);
467 return true;
468 }
469 }
470
471 return false;
472}
473
474template <unsigned EltBitSize>
475bool LoongArchDAGToDAGISel::selectVSplatUimmPow2(SDValue N,
476 SDValue &SplatImm) const {
477 APInt ImmValue;
478 EVT EltTy = N->getValueType(ResNo: 0).getVectorElementType();
479 unsigned EltBitWidth = EltBitSize ? EltBitSize : EltTy.getSizeInBits();
480
481 if (N->getOpcode() == ISD::BITCAST)
482 N = N->getOperand(Num: 0);
483
484 if (selectVSplat(N: N.getNode(), Imm&: ImmValue, MinSizeInBits: EltBitWidth) &&
485 ImmValue.getBitWidth() == EltBitWidth) {
486 int32_t Log2 = ImmValue.exactLogBase2();
487
488 if (Log2 != -1) {
489 SplatImm = CurDAG->getSignedTargetConstant(Val: Log2, DL: SDLoc(N), VT: EltTy);
490 return true;
491 }
492 }
493
494 return false;
495}
496
497// This pass converts a legalized DAG into a LoongArch-specific DAG, ready
498// for instruction scheduling.
499FunctionPass *llvm::createLoongArchISelDag(LoongArchTargetMachine &TM,
500 CodeGenOptLevel OptLevel) {
501 return new LoongArchDAGToDAGISelLegacy(TM, OptLevel);
502}
503