blob: 1faebc29b8a8a6d7ffcc1f7c67d0e5b11e204058 [file] [log] [blame]
Eugene Zelenko76bf48d2017-06-26 22:44:03 +00001//===- llvm/CodeGen/GlobalISel/InstructionSelector.cpp --------------------===//
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file implements the InstructionSelector class.
11//===----------------------------------------------------------------------===//
12
13#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
Quentin Colombetb4e71182016-12-22 21:56:19 +000014#include "llvm/CodeGen/GlobalISel/Utils.h"
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000015#include "llvm/CodeGen/MachineBasicBlock.h"
16#include "llvm/CodeGen/MachineFunction.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000017#include "llvm/CodeGen/MachineInstr.h"
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000018#include "llvm/CodeGen/MachineOperand.h"
Daniel Sandersd93a35a2017-07-05 09:39:33 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000021#include "llvm/MC/MCInstrDesc.h"
Daniel Sandersd93a35a2017-07-05 09:39:33 +000022#include "llvm/IR/Constants.h"
23#include "llvm/Target/TargetInstrInfo.h"
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Support/raw_ostream.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000026#include "llvm/Target/TargetRegisterInfo.h"
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000027#include <cassert>
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000028
29#define DEBUG_TYPE "instructionselector"
30
31using namespace llvm;
32
Daniel Sanders6ab0daa2017-07-04 14:35:06 +000033InstructionSelector::MatcherState::MatcherState(unsigned MaxRenderers)
34 : Renderers(MaxRenderers, nullptr), MIs() {}
35
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000036InstructionSelector::InstructionSelector() = default;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000037
Daniel Sandersd93a35a2017-07-05 09:39:33 +000038void InstructionSelector::executeEmitTable(NewMIVector &OutMIs,
39 MatcherState &State,
40 const int64_t *EmitTable,
41 const TargetInstrInfo &TII,
42 const TargetRegisterInfo &TRI,
43 const RegisterBankInfo &RBI) const {
44 const int64_t *Command = EmitTable;
45 while (true) {
46 switch (*Command++) {
47 case GIR_MutateOpcode: {
48 int64_t OldInsnID = *Command++;
49 int64_t NewInsnID = *Command++;
50 int64_t NewOpcode = *Command++;
51 assert((size_t)NewInsnID == OutMIs.size() &&
52 "Expected to store MIs in order");
53 OutMIs.push_back(
54 MachineInstrBuilder(*State.MIs[OldInsnID]->getParent()->getParent(),
55 State.MIs[OldInsnID]));
56 OutMIs[NewInsnID]->setDesc(TII.get(NewOpcode));
57 DEBUG(dbgs() << "GIR_MutateOpcode(OutMIs[" << NewInsnID << "], MIs["
58 << OldInsnID << "], " << NewOpcode << ")\n");
59 break;
60 }
61 case GIR_BuildMI: {
62 int64_t InsnID = *Command++;
63 int64_t Opcode = *Command++;
64 assert((size_t)InsnID == OutMIs.size() &&
65 "Expected to store MIs in order");
66 OutMIs.push_back(BuildMI(*State.MIs[0]->getParent(), State.MIs[0],
67 State.MIs[0]->getDebugLoc(), TII.get(Opcode)));
68 DEBUG(dbgs() << "GIR_BuildMI(OutMIs[" << InsnID << "], " << Opcode
69 << ")\n");
70 break;
71 }
72
73 case GIR_Copy: {
74 int64_t NewInsnID = *Command++;
75 int64_t OldInsnID = *Command++;
76 int64_t OpIdx = *Command++;
77 assert(OutMIs[NewInsnID] && "Attempted to add to undefined instruction");
78 OutMIs[NewInsnID].add(State.MIs[OldInsnID]->getOperand(OpIdx));
79 DEBUG(dbgs() << "GIR_Copy(OutMIs[" << NewInsnID << "], MIs[" << OldInsnID
80 << "], " << OpIdx << ")\n");
81 break;
82 }
83 case GIR_CopySubReg: {
84 int64_t NewInsnID = *Command++;
85 int64_t OldInsnID = *Command++;
86 int64_t OpIdx = *Command++;
87 int64_t SubRegIdx = *Command++;
88 assert(OutMIs[NewInsnID] && "Attempted to add to undefined instruction");
89 OutMIs[NewInsnID].addReg(State.MIs[OldInsnID]->getOperand(OpIdx).getReg(),
90 0, SubRegIdx);
91 DEBUG(dbgs() << "GIR_CopySubReg(OutMIs[" << NewInsnID << "], MIs["
92 << OldInsnID << "], " << OpIdx << ", " << SubRegIdx
93 << ")\n");
94 break;
95 }
96 case GIR_AddImplicitDef: {
97 int64_t InsnID = *Command++;
98 int64_t RegNum = *Command++;
99 assert(OutMIs[InsnID] && "Attempted to add to undefined instruction");
100 OutMIs[InsnID].addDef(RegNum, RegState::Implicit);
101 DEBUG(dbgs() << "GIR_AddImplicitDef(OutMIs[" << InsnID << "], " << RegNum
102 << ")\n");
103 break;
104 }
105 case GIR_AddImplicitUse: {
106 int64_t InsnID = *Command++;
107 int64_t RegNum = *Command++;
108 assert(OutMIs[InsnID] && "Attempted to add to undefined instruction");
109 OutMIs[InsnID].addUse(RegNum, RegState::Implicit);
110 DEBUG(dbgs() << "GIR_AddImplicitUse(OutMIs[" << InsnID << "], " << RegNum
111 << ")\n");
112 break;
113 }
114 case GIR_AddRegister: {
115 int64_t InsnID = *Command++;
116 int64_t RegNum = *Command++;
117 assert(OutMIs[InsnID] && "Attempted to add to undefined instruction");
118 OutMIs[InsnID].addReg(RegNum);
119 DEBUG(dbgs() << "GIR_AddRegister(OutMIs[" << InsnID << "], " << RegNum
120 << ")\n");
121 break;
122 }
123 case GIR_AddImm: {
124 int64_t InsnID = *Command++;
125 int64_t Imm = *Command++;
126 assert(OutMIs[InsnID] && "Attempted to add to undefined instruction");
127 OutMIs[InsnID].addImm(Imm);
128 DEBUG(dbgs() << "GIR_AddImm(OutMIs[" << InsnID << "], " << Imm << ")\n");
129 break;
130 }
131 case GIR_ComplexRenderer: {
132 int64_t InsnID = *Command++;
133 int64_t RendererID = *Command++;
134 assert(OutMIs[InsnID] && "Attempted to add to undefined instruction");
135 State.Renderers[RendererID](OutMIs[InsnID]);
136 DEBUG(dbgs() << "GIR_ComplexRenderer(OutMIs[" << InsnID << "], "
137 << RendererID << ")\n");
138 break;
139 }
140
141 case GIR_ConstrainOperandRC: {
142 int64_t InsnID = *Command++;
143 int64_t OpIdx = *Command++;
144 int64_t RCEnum = *Command++;
145 assert(OutMIs[InsnID] && "Attempted to add to undefined instruction");
146 constrainOperandRegToRegClass(*OutMIs[InsnID].getInstr(), OpIdx,
147 *TRI.getRegClass(RCEnum), TII, TRI, RBI);
148 DEBUG(dbgs() << "GIR_ConstrainOperandRC(OutMIs[" << InsnID << "], "
149 << OpIdx << ", " << RCEnum << ")\n");
150 break;
151 }
152 case GIR_ConstrainSelectedInstOperands: {
153 int64_t InsnID = *Command++;
154 assert(OutMIs[InsnID] && "Attempted to add to undefined instruction");
155 constrainSelectedInstRegOperands(*OutMIs[InsnID].getInstr(), TII, TRI,
156 RBI);
157 DEBUG(dbgs() << "GIR_ConstrainSelectedInstOperands(OutMIs[" << InsnID
158 << "])\n");
159 break;
160 }
161 case GIR_MergeMemOperands: {
162 int64_t InsnID = *Command++;
163 assert(OutMIs[InsnID] && "Attempted to add to undefined instruction");
164 for (const auto *FromMI : State.MIs)
165 for (const auto &MMO : FromMI->memoperands())
166 OutMIs[InsnID].addMemOperand(MMO);
167 DEBUG(dbgs() << "GIR_MergeMemOperands(OutMIs[" << InsnID << "])\n");
168 break;
169 }
170 case GIR_EraseFromParent: {
171 int64_t InsnID = *Command++;
172 assert(State.MIs[InsnID] && "Attempted to erase an undefined instruction");
173 State.MIs[InsnID]->eraseFromParent();
174 DEBUG(dbgs() << "GIR_EraseFromParent(MIs[" << InsnID << "])\n");
175 break;
176 }
177
178 case GIR_Done:
179 DEBUG(dbgs() << "GIR_Done");
180 return;
181 default:
182 llvm_unreachable("Unexpected command");
183 }
184 }
185}
186
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +0000187bool InstructionSelector::constrainOperandRegToRegClass(
188 MachineInstr &I, unsigned OpIdx, const TargetRegisterClass &RC,
189 const TargetInstrInfo &TII, const TargetRegisterInfo &TRI,
190 const RegisterBankInfo &RBI) const {
191 MachineBasicBlock &MBB = *I.getParent();
192 MachineFunction &MF = *MBB.getParent();
193 MachineRegisterInfo &MRI = MF.getRegInfo();
194
Eugene Zelenko76bf48d2017-06-26 22:44:03 +0000195 return
196 constrainRegToClass(MRI, TII, RBI, I, I.getOperand(OpIdx).getReg(), RC);
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +0000197}
198
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000199bool InstructionSelector::constrainSelectedInstRegOperands(
200 MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI,
201 const RegisterBankInfo &RBI) const {
202 MachineBasicBlock &MBB = *I.getParent();
203 MachineFunction &MF = *MBB.getParent();
204 MachineRegisterInfo &MRI = MF.getRegInfo();
205
206 for (unsigned OpI = 0, OpE = I.getNumExplicitOperands(); OpI != OpE; ++OpI) {
207 MachineOperand &MO = I.getOperand(OpI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000208
Tim Northoverbdf16242016-10-10 21:50:00 +0000209 // There's nothing to be done on non-register operands.
210 if (!MO.isReg())
Ahmed Bougacha7adfac52016-07-29 16:56:16 +0000211 continue;
212
213 DEBUG(dbgs() << "Converting operand: " << MO << '\n');
214 assert(MO.isReg() && "Unsupported non-reg operand");
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000215
Quentin Colombetb4e71182016-12-22 21:56:19 +0000216 unsigned Reg = MO.getReg();
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000217 // Physical registers don't need to be constrained.
Quentin Colombetb4e71182016-12-22 21:56:19 +0000218 if (TRI.isPhysicalRegister(Reg))
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +0000219 continue;
220
Diana Picus812caee2016-12-16 12:54:46 +0000221 // Register operands with a value of 0 (e.g. predicate operands) don't need
222 // to be constrained.
Quentin Colombetb4e71182016-12-22 21:56:19 +0000223 if (Reg == 0)
Diana Picus812caee2016-12-16 12:54:46 +0000224 continue;
225
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000226 // If the operand is a vreg, we should constrain its regclass, and only
227 // insert COPYs if that's impossible.
Quentin Colombetb4e71182016-12-22 21:56:19 +0000228 // constrainOperandRegClass does that for us.
229 MO.setReg(constrainOperandRegClass(MF, TRI, MRI, TII, RBI, I, I.getDesc(),
230 Reg, OpI));
Igor Bregerf7359d82017-02-22 12:25:09 +0000231
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000232 // Tie uses to defs as indicated in MCInstrDesc if this hasn't already been
233 // done.
Igor Bregerf7359d82017-02-22 12:25:09 +0000234 if (MO.isUse()) {
235 int DefIdx = I.getDesc().getOperandConstraint(OpI, MCOI::TIED_TO);
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000236 if (DefIdx != -1 && !I.isRegTiedToUseOperand(DefIdx))
Igor Bregerf7359d82017-02-22 12:25:09 +0000237 I.tieOperands(DefIdx, OpI);
238 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000239 }
240 return true;
241}
Ahmed Bougacha7f2d1732017-03-19 16:12:48 +0000242
243bool InstructionSelector::isOperandImmEqual(
244 const MachineOperand &MO, int64_t Value,
245 const MachineRegisterInfo &MRI) const {
Daniel Sanders89e93082017-05-18 10:33:36 +0000246 if (MO.isReg() && MO.getReg())
Ahmed Bougacha2d29998f2017-03-27 16:35:27 +0000247 if (auto VRegVal = getConstantVRegVal(MO.getReg(), MRI))
248 return *VRegVal == Value;
Ahmed Bougacha7f2d1732017-03-19 16:12:48 +0000249 return false;
250}
Daniel Sandersbee57392017-04-04 13:25:23 +0000251
252bool InstructionSelector::isObviouslySafeToFold(MachineInstr &MI) const {
253 return !MI.mayLoadOrStore() && !MI.hasUnmodeledSideEffects() &&
254 MI.implicit_operands().begin() == MI.implicit_operands().end();
255}