File: CSKYISelDAGToDAG.cpp

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-20
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,496,436 kB
  • sloc: cpp: 5,593,990; ansic: 986,873; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,547; xml: 953; cs: 573; fortran: 567
file content (180 lines) | stat: -rw-r--r-- 5,906 bytes parent folder | download | duplicates (3)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//===-- CSKYISelDAGToDAG.cpp - A dag to dag inst selector for CSKY---------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines an instruction selector for the CSKY target.
//
//===----------------------------------------------------------------------===//

#include "CSKY.h"
#include "CSKYSubtarget.h"
#include "CSKYTargetMachine.h"
#include "MCTargetDesc/CSKYMCTargetDesc.h"
#include "llvm/CodeGen/SelectionDAG.h"
#include "llvm/CodeGen/SelectionDAGISel.h"

using namespace llvm;

#define DEBUG_TYPE "csky-isel"

namespace {
class CSKYDAGToDAGISel : public SelectionDAGISel {
  const CSKYSubtarget *Subtarget;

public:
  explicit CSKYDAGToDAGISel(CSKYTargetMachine &TM) : SelectionDAGISel(TM) {}

  StringRef getPassName() const override {
    return "CSKY DAG->DAG Pattern Instruction Selection";
  }

  bool runOnMachineFunction(MachineFunction &MF) override {
    // Reset the subtarget each time through.
    Subtarget = &MF.getSubtarget<CSKYSubtarget>();
    SelectionDAGISel::runOnMachineFunction(MF);
    return true;
  }

  void Select(SDNode *N) override;
  bool selectAddCarry(SDNode *N);
  bool selectSubCarry(SDNode *N);

#include "CSKYGenDAGISel.inc"
};
} // namespace

void CSKYDAGToDAGISel::Select(SDNode *N) {
  // If we have a custom node, we have already selected
  if (N->isMachineOpcode()) {
    LLVM_DEBUG(dbgs() << "== "; N->dump(CurDAG); dbgs() << "\n");
    N->setNodeId(-1);
    return;
  }

  SDLoc Dl(N);
  unsigned Opcode = N->getOpcode();
  bool IsSelected = false;

  switch (Opcode) {
  default:
    break;
  case ISD::ADDCARRY:
    IsSelected = selectAddCarry(N);
    break;
  case ISD::SUBCARRY:
    IsSelected = selectSubCarry(N);
    break;
  case ISD::GLOBAL_OFFSET_TABLE: {
    Register GP = Subtarget->getInstrInfo()->getGlobalBaseReg(*MF);
    ReplaceNode(N, CurDAG->getRegister(GP, N->getValueType(0)).getNode());

    IsSelected = true;
    break;
  }
  case ISD::FrameIndex: {
    SDValue Imm = CurDAG->getTargetConstant(0, Dl, MVT::i32);
    int FI = cast<FrameIndexSDNode>(N)->getIndex();
    SDValue TFI = CurDAG->getTargetFrameIndex(FI, MVT::i32);
    ReplaceNode(N, CurDAG->getMachineNode(Subtarget->hasE2() ? CSKY::ADDI32
                                                             : CSKY::ADDI16XZ,
                                          Dl, MVT::i32, TFI, Imm));

    IsSelected = true;
    break;
  }
  }

  if (IsSelected)
    return;

  // Select the default instruction.
  SelectCode(N);
}

bool CSKYDAGToDAGISel::selectAddCarry(SDNode *N) {
  MachineSDNode *NewNode = nullptr;
  auto Type0 = N->getValueType(0);
  auto Type1 = N->getValueType(1);
  auto Op0 = N->getOperand(0);
  auto Op1 = N->getOperand(1);
  auto Op2 = N->getOperand(2);

  SDLoc Dl(N);

  if (isNullConstant(Op2)) {
    auto *CA = CurDAG->getMachineNode(
        Subtarget->has2E3() ? CSKY::CLRC32 : CSKY::CLRC16, Dl, Type1);
    NewNode = CurDAG->getMachineNode(
        Subtarget->has2E3() ? CSKY::ADDC32 : CSKY::ADDC16, Dl, {Type0, Type1},
        {Op0, Op1, SDValue(CA, 0)});
  } else if (isOneConstant(Op2)) {
    auto *CA = CurDAG->getMachineNode(
        Subtarget->has2E3() ? CSKY::SETC32 : CSKY::SETC16, Dl, Type1);
    NewNode = CurDAG->getMachineNode(
        Subtarget->has2E3() ? CSKY::ADDC32 : CSKY::ADDC16, Dl, {Type0, Type1},
        {Op0, Op1, SDValue(CA, 0)});
  } else {
    NewNode = CurDAG->getMachineNode(Subtarget->has2E3() ? CSKY::ADDC32
                                                         : CSKY::ADDC16,
                                     Dl, {Type0, Type1}, {Op0, Op1, Op2});
  }
  ReplaceNode(N, NewNode);
  return true;
}

static SDValue InvertCarryFlag(const CSKYSubtarget *Subtarget,
                               SelectionDAG *DAG, SDLoc Dl, SDValue OldCarry) {
  auto NewCarryReg =
      DAG->getMachineNode(Subtarget->has2E3() ? CSKY::MVCV32 : CSKY::MVCV16, Dl,
                          MVT::i32, OldCarry);
  auto NewCarry =
      DAG->getMachineNode(Subtarget->hasE2() ? CSKY::BTSTI32 : CSKY::BTSTI16,
                          Dl, OldCarry.getValueType(), SDValue(NewCarryReg, 0),
                          DAG->getTargetConstant(0, Dl, MVT::i32));
  return SDValue(NewCarry, 0);
}

bool CSKYDAGToDAGISel::selectSubCarry(SDNode *N) {
  MachineSDNode *NewNode = nullptr;
  auto Type0 = N->getValueType(0);
  auto Type1 = N->getValueType(1);
  auto Op0 = N->getOperand(0);
  auto Op1 = N->getOperand(1);
  auto Op2 = N->getOperand(2);

  SDLoc Dl(N);

  if (isNullConstant(Op2)) {
    auto *CA = CurDAG->getMachineNode(
        Subtarget->has2E3() ? CSKY::SETC32 : CSKY::SETC16, Dl, Type1);
    NewNode = CurDAG->getMachineNode(
        Subtarget->has2E3() ? CSKY::SUBC32 : CSKY::SUBC16, Dl, {Type0, Type1},
        {Op0, Op1, SDValue(CA, 0)});
  } else if (isOneConstant(Op2)) {
    auto *CA = CurDAG->getMachineNode(
        Subtarget->has2E3() ? CSKY::CLRC32 : CSKY::CLRC16, Dl, Type1);
    NewNode = CurDAG->getMachineNode(
        Subtarget->has2E3() ? CSKY::SUBC32 : CSKY::SUBC16, Dl, {Type0, Type1},
        {Op0, Op1, SDValue(CA, 0)});
  } else {
    auto CarryIn = InvertCarryFlag(Subtarget, CurDAG, Dl, Op2);
    NewNode = CurDAG->getMachineNode(Subtarget->has2E3() ? CSKY::SUBC32
                                                         : CSKY::SUBC16,
                                     Dl, {Type0, Type1}, {Op0, Op1, CarryIn});
  }
  auto CarryOut = InvertCarryFlag(Subtarget, CurDAG, Dl, SDValue(NewNode, 1));

  ReplaceUses(SDValue(N, 0), SDValue(NewNode, 0));
  ReplaceUses(SDValue(N, 1), CarryOut);
  CurDAG->RemoveDeadNode(N);

  return true;
}

FunctionPass *llvm::createCSKYISelDag(CSKYTargetMachine &TM) {
  return new CSKYDAGToDAGISel(TM);
}