blob: 74ed58e8d0493f05963ebc83c94da127ab327a7a [file] [log] [blame]
Tim Northover69fa84a2016-10-14 22:18:18 +00001//===-- llvm/CodeGen/GlobalISel/Legalizer.cpp -----------------------------===//
Tim Northover33b07d62016-07-22 20:03:43 +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//
Tim Northover69fa84a2016-10-14 22:18:18 +000010/// \file This file implements the LegalizerHelper class to legalize individual
11/// instructions and the LegalizePass wrapper pass for the primary
Tim Northover33b07d62016-07-22 20:03:43 +000012/// legalization.
13//
14//===----------------------------------------------------------------------===//
15
Tim Northover69fa84a2016-10-14 22:18:18 +000016#include "llvm/CodeGen/GlobalISel/Legalizer.h"
17#include "llvm/CodeGen/GlobalISel/LegalizerHelper.h"
18#include "llvm/CodeGen/GlobalISel/Legalizer.h"
Ahmed Bougachaae9dade2017-02-23 21:05:42 +000019#include "llvm/CodeGen/GlobalISel/Utils.h"
20#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
22#include "llvm/CodeGen/TargetPassConfig.h"
Tim Northover33b07d62016-07-22 20:03:43 +000023#include "llvm/Support/Debug.h"
Tim Northover991b12b2016-08-30 20:51:25 +000024#include "llvm/Target/TargetInstrInfo.h"
Tim Northover33b07d62016-07-22 20:03:43 +000025#include "llvm/Target/TargetSubtargetInfo.h"
26
Daniel Sanders5377fb32017-04-20 15:46:12 +000027#include <iterator>
28
Tim Northover69fa84a2016-10-14 22:18:18 +000029#define DEBUG_TYPE "legalizer"
Tim Northover33b07d62016-07-22 20:03:43 +000030
31using namespace llvm;
32
Tim Northover69fa84a2016-10-14 22:18:18 +000033char Legalizer::ID = 0;
34INITIALIZE_PASS_BEGIN(Legalizer, DEBUG_TYPE,
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000035 "Legalize the Machine IR a function's Machine IR", false,
36 false)
37INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
Tim Northover69fa84a2016-10-14 22:18:18 +000038INITIALIZE_PASS_END(Legalizer, DEBUG_TYPE,
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000039 "Legalize the Machine IR a function's Machine IR", false,
40 false)
Tim Northover33b07d62016-07-22 20:03:43 +000041
Tim Northover69fa84a2016-10-14 22:18:18 +000042Legalizer::Legalizer() : MachineFunctionPass(ID) {
43 initializeLegalizerPass(*PassRegistry::getPassRegistry());
Tim Northover33b07d62016-07-22 20:03:43 +000044}
45
Tim Northover69fa84a2016-10-14 22:18:18 +000046void Legalizer::getAnalysisUsage(AnalysisUsage &AU) const {
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000047 AU.addRequired<TargetPassConfig>();
48 MachineFunctionPass::getAnalysisUsage(AU);
49}
50
Tim Northover69fa84a2016-10-14 22:18:18 +000051void Legalizer::init(MachineFunction &MF) {
Tim Northover33b07d62016-07-22 20:03:43 +000052}
53
Tim Northover69fa84a2016-10-14 22:18:18 +000054bool Legalizer::combineExtracts(MachineInstr &MI, MachineRegisterInfo &MRI,
55 const TargetInstrInfo &TII) {
Tim Northover991b12b2016-08-30 20:51:25 +000056 bool Changed = false;
57 if (MI.getOpcode() != TargetOpcode::G_EXTRACT)
58 return Changed;
59
60 unsigned NumDefs = (MI.getNumOperands() - 1) / 2;
61 unsigned SrcReg = MI.getOperand(NumDefs).getReg();
62 MachineInstr &SeqI = *MRI.def_instr_begin(SrcReg);
63 if (SeqI.getOpcode() != TargetOpcode::G_SEQUENCE)
64 return Changed;
65
66 unsigned NumSeqSrcs = (SeqI.getNumOperands() - 1) / 2;
67 bool AllDefsReplaced = true;
68
69 // Try to match each register extracted with a corresponding insertion formed
70 // by the G_SEQUENCE.
71 for (unsigned Idx = 0, SeqIdx = 0; Idx < NumDefs; ++Idx) {
72 MachineOperand &ExtractMO = MI.getOperand(Idx);
73 assert(ExtractMO.isReg() && ExtractMO.isDef() &&
74 "unexpected extract operand");
75
76 unsigned ExtractReg = ExtractMO.getReg();
77 unsigned ExtractPos = MI.getOperand(NumDefs + Idx + 1).getImm();
78
79 while (SeqIdx < NumSeqSrcs &&
80 SeqI.getOperand(2 * SeqIdx + 2).getImm() < ExtractPos)
81 ++SeqIdx;
82
Tim Northover0f140c72016-09-09 11:46:34 +000083 if (SeqIdx == NumSeqSrcs) {
Tim Northover991b12b2016-08-30 20:51:25 +000084 AllDefsReplaced = false;
85 continue;
86 }
87
88 unsigned OrigReg = SeqI.getOperand(2 * SeqIdx + 1).getReg();
Tim Northover0f140c72016-09-09 11:46:34 +000089 if (SeqI.getOperand(2 * SeqIdx + 2).getImm() != ExtractPos ||
90 MRI.getType(OrigReg) != MRI.getType(ExtractReg)) {
91 AllDefsReplaced = false;
92 continue;
93 }
94
Tim Northover991b12b2016-08-30 20:51:25 +000095 assert(!TargetRegisterInfo::isPhysicalRegister(OrigReg) &&
96 "unexpected physical register in G_SEQUENCE");
97
98 // Finally we can replace the uses.
Ahmed Bougacha67d1c7c2017-03-01 00:43:39 +000099 MRI.replaceRegWith(ExtractReg, OrigReg);
Tim Northover991b12b2016-08-30 20:51:25 +0000100 }
101
102 if (AllDefsReplaced) {
103 // If SeqI was the next instruction in the BB and we removed it, we'd break
104 // the outer iteration.
105 assert(std::next(MachineBasicBlock::iterator(MI)) != SeqI &&
106 "G_SEQUENCE does not dominate G_EXTRACT");
107
108 MI.eraseFromParent();
109
110 if (MRI.use_empty(SrcReg))
111 SeqI.eraseFromParent();
112 Changed = true;
113 }
114
115 return Changed;
116}
117
Tim Northoverbf017292017-03-03 22:46:09 +0000118bool Legalizer::combineMerges(MachineInstr &MI, MachineRegisterInfo &MRI,
119 const TargetInstrInfo &TII) {
120 if (MI.getOpcode() != TargetOpcode::G_UNMERGE_VALUES)
121 return false;
122
123 unsigned NumDefs = MI.getNumOperands() - 1;
124 unsigned SrcReg = MI.getOperand(NumDefs).getReg();
125 MachineInstr &MergeI = *MRI.def_instr_begin(SrcReg);
126 if (MergeI.getOpcode() != TargetOpcode::G_MERGE_VALUES)
127 return false;
128
129 if (MergeI.getNumOperands() - 1 != NumDefs)
130 return false;
131
132 // FIXME: is a COPY appropriate if the types mismatch? We know both registers
133 // are allocatable by now.
134 if (MRI.getType(MI.getOperand(0).getReg()) !=
135 MRI.getType(MergeI.getOperand(1).getReg()))
136 return false;
137
138 for (unsigned Idx = 0; Idx < NumDefs; ++Idx)
139 MRI.replaceRegWith(MI.getOperand(Idx).getReg(),
140 MergeI.getOperand(Idx + 1).getReg());
141
142 MI.eraseFromParent();
143 if (MRI.use_empty(MergeI.getOperand(0).getReg()))
144 MergeI.eraseFromParent();
145 return true;
146}
147
Tim Northover69fa84a2016-10-14 22:18:18 +0000148bool Legalizer::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet60495242016-08-27 00:18:24 +0000149 // If the ISel pipeline failed, do not bother running that pass.
150 if (MF.getProperties().hasProperty(
151 MachineFunctionProperties::Property::FailedISel))
152 return false;
Tim Northover33b07d62016-07-22 20:03:43 +0000153 DEBUG(dbgs() << "Legalize Machine IR for: " << MF.getName() << '\n');
154 init(MF);
Quentin Colombet5e60bcd2016-08-27 02:38:21 +0000155 const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
Ahmed Bougachaae9dade2017-02-23 21:05:42 +0000156 MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
Tim Northover69fa84a2016-10-14 22:18:18 +0000157 LegalizerHelper Helper(MF);
Tim Northover33b07d62016-07-22 20:03:43 +0000158
159 // FIXME: an instruction may need more than one pass before it is legal. For
160 // example on most architectures <3 x i3> is doubly-illegal. It would
161 // typically proceed along a path like: <3 x i3> -> <3 x i8> -> <8 x i8>. We
162 // probably want a worklist of instructions rather than naive iterate until
163 // convergence for performance reasons.
164 bool Changed = false;
165 MachineBasicBlock::iterator NextMI;
Daniel Sanders5377fb32017-04-20 15:46:12 +0000166 for (auto &MBB : MF) {
Tim Northover33b07d62016-07-22 20:03:43 +0000167 for (auto MI = MBB.begin(); MI != MBB.end(); MI = NextMI) {
168 // Get the next Instruction before we try to legalize, because there's a
169 // good chance MI will be deleted.
170 NextMI = std::next(MI);
Ahmed Bougachafaf8e9f2016-08-02 11:41:09 +0000171
172 // Only legalize pre-isel generic instructions: others don't have types
173 // and are assumed to be legal.
174 if (!isPreISelGenericOpcode(MI->getOpcode()))
175 continue;
Daniel Sanders5377fb32017-04-20 15:46:12 +0000176 unsigned NumNewInsns = 0;
Aditya Nandakumareb80a512017-04-07 21:49:30 +0000177 SmallVector<MachineInstr *, 4> WorkList;
Daniel Sanders5377fb32017-04-20 15:46:12 +0000178 Helper.MIRBuilder.recordInsertions([&](MachineInstr *MI) {
179 ++NumNewInsns;
180 WorkList.push_back(MI);
181 });
Aditya Nandakumareb80a512017-04-07 21:49:30 +0000182 WorkList.push_back(&*MI);
Ahmed Bougachafaf8e9f2016-08-02 11:41:09 +0000183
Daniel Sanders5377fb32017-04-20 15:46:12 +0000184 bool Changed = false;
Aditya Nandakumareb80a512017-04-07 21:49:30 +0000185 LegalizerHelper::LegalizeResult Res;
186 unsigned Idx = 0;
187 do {
188 Res = Helper.legalizeInstrStep(*WorkList[Idx]);
189 // Error out if we couldn't legalize this instruction. We may want to
Daniel Sanders5377fb32017-04-20 15:46:12 +0000190 // fall back to DAG ISel instead in the future.
Aditya Nandakumareb80a512017-04-07 21:49:30 +0000191 if (Res == LegalizerHelper::UnableToLegalize) {
192 Helper.MIRBuilder.stopRecordingInsertions();
193 if (Res == LegalizerHelper::UnableToLegalize) {
194 reportGISelFailure(MF, TPC, MORE, "gisel-legalize",
195 "unable to legalize instruction",
196 *WorkList[Idx]);
197 return false;
198 }
199 }
200 Changed |= Res == LegalizerHelper::Legalized;
201 ++Idx;
Daniel Sanders5377fb32017-04-20 15:46:12 +0000202
203#ifndef NDEBUG
204 if (NumNewInsns) {
205 DEBUG(dbgs() << ".. .. Emitted " << NumNewInsns << " insns\n");
206 for (auto I = WorkList.end() - NumNewInsns, E = WorkList.end();
207 I != E; ++I)
208 DEBUG(dbgs() << ".. .. New MI: "; (*I)->print(dbgs()));
209 NumNewInsns = 0;
210 }
211#endif
Aditya Nandakumareb80a512017-04-07 21:49:30 +0000212 } while (Idx < WorkList.size());
Tim Northover33b07d62016-07-22 20:03:43 +0000213
Aditya Nandakumareb80a512017-04-07 21:49:30 +0000214 Helper.MIRBuilder.stopRecordingInsertions();
Tim Northover33b07d62016-07-22 20:03:43 +0000215 }
Daniel Sanders5377fb32017-04-20 15:46:12 +0000216 }
Tim Northover991b12b2016-08-30 20:51:25 +0000217
Tim Northover991b12b2016-08-30 20:51:25 +0000218 MachineRegisterInfo &MRI = MF.getRegInfo();
219 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
220 for (auto &MBB : MF) {
221 for (auto MI = MBB.begin(); MI != MBB.end(); MI = NextMI) {
222 // Get the next Instruction before we try to legalize, because there's a
223 // good chance MI will be deleted.
224 NextMI = std::next(MI);
225
Benjamin Kramer58dadd52017-04-20 18:29:14 +0000226 // combineExtracts erases MI.
227 if (combineExtracts(*MI, MRI, TII)) {
228 Changed = true;
229 continue;
230 }
Tim Northoverbf017292017-03-03 22:46:09 +0000231 Changed |= combineMerges(*MI, MRI, TII);
Tim Northover991b12b2016-08-30 20:51:25 +0000232 }
233 }
234
Tim Northover33b07d62016-07-22 20:03:43 +0000235 return Changed;
236}