blob: 657ddb30791952af164493ff81a6a93bebe83993 [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
Tim Northover69fa84a2016-10-14 22:18:18 +000027#define DEBUG_TYPE "legalizer"
Tim Northover33b07d62016-07-22 20:03:43 +000028
29using namespace llvm;
30
Tim Northover69fa84a2016-10-14 22:18:18 +000031char Legalizer::ID = 0;
32INITIALIZE_PASS_BEGIN(Legalizer, DEBUG_TYPE,
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000033 "Legalize the Machine IR a function's Machine IR", false,
34 false)
35INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
Tim Northover69fa84a2016-10-14 22:18:18 +000036INITIALIZE_PASS_END(Legalizer, DEBUG_TYPE,
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000037 "Legalize the Machine IR a function's Machine IR", false,
38 false)
Tim Northover33b07d62016-07-22 20:03:43 +000039
Tim Northover69fa84a2016-10-14 22:18:18 +000040Legalizer::Legalizer() : MachineFunctionPass(ID) {
41 initializeLegalizerPass(*PassRegistry::getPassRegistry());
Tim Northover33b07d62016-07-22 20:03:43 +000042}
43
Tim Northover69fa84a2016-10-14 22:18:18 +000044void Legalizer::getAnalysisUsage(AnalysisUsage &AU) const {
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000045 AU.addRequired<TargetPassConfig>();
46 MachineFunctionPass::getAnalysisUsage(AU);
47}
48
Tim Northover69fa84a2016-10-14 22:18:18 +000049void Legalizer::init(MachineFunction &MF) {
Tim Northover33b07d62016-07-22 20:03:43 +000050}
51
Tim Northover69fa84a2016-10-14 22:18:18 +000052bool Legalizer::combineExtracts(MachineInstr &MI, MachineRegisterInfo &MRI,
53 const TargetInstrInfo &TII) {
Tim Northover991b12b2016-08-30 20:51:25 +000054 bool Changed = false;
55 if (MI.getOpcode() != TargetOpcode::G_EXTRACT)
56 return Changed;
57
58 unsigned NumDefs = (MI.getNumOperands() - 1) / 2;
59 unsigned SrcReg = MI.getOperand(NumDefs).getReg();
60 MachineInstr &SeqI = *MRI.def_instr_begin(SrcReg);
61 if (SeqI.getOpcode() != TargetOpcode::G_SEQUENCE)
62 return Changed;
63
64 unsigned NumSeqSrcs = (SeqI.getNumOperands() - 1) / 2;
65 bool AllDefsReplaced = true;
66
67 // Try to match each register extracted with a corresponding insertion formed
68 // by the G_SEQUENCE.
69 for (unsigned Idx = 0, SeqIdx = 0; Idx < NumDefs; ++Idx) {
70 MachineOperand &ExtractMO = MI.getOperand(Idx);
71 assert(ExtractMO.isReg() && ExtractMO.isDef() &&
72 "unexpected extract operand");
73
74 unsigned ExtractReg = ExtractMO.getReg();
75 unsigned ExtractPos = MI.getOperand(NumDefs + Idx + 1).getImm();
76
77 while (SeqIdx < NumSeqSrcs &&
78 SeqI.getOperand(2 * SeqIdx + 2).getImm() < ExtractPos)
79 ++SeqIdx;
80
Tim Northover0f140c72016-09-09 11:46:34 +000081 if (SeqIdx == NumSeqSrcs) {
Tim Northover991b12b2016-08-30 20:51:25 +000082 AllDefsReplaced = false;
83 continue;
84 }
85
86 unsigned OrigReg = SeqI.getOperand(2 * SeqIdx + 1).getReg();
Tim Northover0f140c72016-09-09 11:46:34 +000087 if (SeqI.getOperand(2 * SeqIdx + 2).getImm() != ExtractPos ||
88 MRI.getType(OrigReg) != MRI.getType(ExtractReg)) {
89 AllDefsReplaced = false;
90 continue;
91 }
92
Tim Northover991b12b2016-08-30 20:51:25 +000093 assert(!TargetRegisterInfo::isPhysicalRegister(OrigReg) &&
94 "unexpected physical register in G_SEQUENCE");
95
96 // Finally we can replace the uses.
Ahmed Bougacha67d1c7c2017-03-01 00:43:39 +000097 MRI.replaceRegWith(ExtractReg, OrigReg);
Tim Northover991b12b2016-08-30 20:51:25 +000098 }
99
100 if (AllDefsReplaced) {
101 // If SeqI was the next instruction in the BB and we removed it, we'd break
102 // the outer iteration.
103 assert(std::next(MachineBasicBlock::iterator(MI)) != SeqI &&
104 "G_SEQUENCE does not dominate G_EXTRACT");
105
106 MI.eraseFromParent();
107
108 if (MRI.use_empty(SrcReg))
109 SeqI.eraseFromParent();
110 Changed = true;
111 }
112
113 return Changed;
114}
115
Tim Northoverbf017292017-03-03 22:46:09 +0000116bool Legalizer::combineMerges(MachineInstr &MI, MachineRegisterInfo &MRI,
117 const TargetInstrInfo &TII) {
118 if (MI.getOpcode() != TargetOpcode::G_UNMERGE_VALUES)
119 return false;
120
121 unsigned NumDefs = MI.getNumOperands() - 1;
122 unsigned SrcReg = MI.getOperand(NumDefs).getReg();
123 MachineInstr &MergeI = *MRI.def_instr_begin(SrcReg);
124 if (MergeI.getOpcode() != TargetOpcode::G_MERGE_VALUES)
125 return false;
126
127 if (MergeI.getNumOperands() - 1 != NumDefs)
128 return false;
129
130 // FIXME: is a COPY appropriate if the types mismatch? We know both registers
131 // are allocatable by now.
132 if (MRI.getType(MI.getOperand(0).getReg()) !=
133 MRI.getType(MergeI.getOperand(1).getReg()))
134 return false;
135
136 for (unsigned Idx = 0; Idx < NumDefs; ++Idx)
137 MRI.replaceRegWith(MI.getOperand(Idx).getReg(),
138 MergeI.getOperand(Idx + 1).getReg());
139
140 MI.eraseFromParent();
141 if (MRI.use_empty(MergeI.getOperand(0).getReg()))
142 MergeI.eraseFromParent();
143 return true;
144}
145
Tim Northover69fa84a2016-10-14 22:18:18 +0000146bool Legalizer::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet60495242016-08-27 00:18:24 +0000147 // If the ISel pipeline failed, do not bother running that pass.
148 if (MF.getProperties().hasProperty(
149 MachineFunctionProperties::Property::FailedISel))
150 return false;
Tim Northover33b07d62016-07-22 20:03:43 +0000151 DEBUG(dbgs() << "Legalize Machine IR for: " << MF.getName() << '\n');
152 init(MF);
Quentin Colombet5e60bcd2016-08-27 02:38:21 +0000153 const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
Ahmed Bougachaae9dade2017-02-23 21:05:42 +0000154 MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
Tim Northover69fa84a2016-10-14 22:18:18 +0000155 LegalizerHelper Helper(MF);
Tim Northover33b07d62016-07-22 20:03:43 +0000156
157 // FIXME: an instruction may need more than one pass before it is legal. For
158 // example on most architectures <3 x i3> is doubly-illegal. It would
159 // typically proceed along a path like: <3 x i3> -> <3 x i8> -> <8 x i8>. We
160 // probably want a worklist of instructions rather than naive iterate until
161 // convergence for performance reasons.
162 bool Changed = false;
163 MachineBasicBlock::iterator NextMI;
164 for (auto &MBB : MF)
165 for (auto MI = MBB.begin(); MI != MBB.end(); MI = NextMI) {
166 // Get the next Instruction before we try to legalize, because there's a
167 // good chance MI will be deleted.
168 NextMI = std::next(MI);
Ahmed Bougachafaf8e9f2016-08-02 11:41:09 +0000169
170 // Only legalize pre-isel generic instructions: others don't have types
171 // and are assumed to be legal.
172 if (!isPreISelGenericOpcode(MI->getOpcode()))
173 continue;
Aditya Nandakumareb80a512017-04-07 21:49:30 +0000174 SmallVector<MachineInstr *, 4> WorkList;
175 Helper.MIRBuilder.recordInsertions(
176 [&](MachineInstr *MI) { WorkList.push_back(MI); });
177 WorkList.push_back(&*MI);
Ahmed Bougachafaf8e9f2016-08-02 11:41:09 +0000178
Aditya Nandakumareb80a512017-04-07 21:49:30 +0000179 LegalizerHelper::LegalizeResult Res;
180 unsigned Idx = 0;
181 do {
182 Res = Helper.legalizeInstrStep(*WorkList[Idx]);
183 // Error out if we couldn't legalize this instruction. We may want to
184 // fall
185 // back to DAG ISel instead in the future.
186 if (Res == LegalizerHelper::UnableToLegalize) {
187 Helper.MIRBuilder.stopRecordingInsertions();
188 if (Res == LegalizerHelper::UnableToLegalize) {
189 reportGISelFailure(MF, TPC, MORE, "gisel-legalize",
190 "unable to legalize instruction",
191 *WorkList[Idx]);
192 return false;
193 }
194 }
195 Changed |= Res == LegalizerHelper::Legalized;
196 ++Idx;
197 } while (Idx < WorkList.size());
Tim Northover33b07d62016-07-22 20:03:43 +0000198
Aditya Nandakumareb80a512017-04-07 21:49:30 +0000199 Helper.MIRBuilder.stopRecordingInsertions();
Tim Northover33b07d62016-07-22 20:03:43 +0000200 }
Tim Northover991b12b2016-08-30 20:51:25 +0000201
Tim Northover991b12b2016-08-30 20:51:25 +0000202 MachineRegisterInfo &MRI = MF.getRegInfo();
203 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
204 for (auto &MBB : MF) {
205 for (auto MI = MBB.begin(); MI != MBB.end(); MI = NextMI) {
206 // Get the next Instruction before we try to legalize, because there's a
207 // good chance MI will be deleted.
208 NextMI = std::next(MI);
209
210 Changed |= combineExtracts(*MI, MRI, TII);
Tim Northoverbf017292017-03-03 22:46:09 +0000211 Changed |= combineMerges(*MI, MRI, TII);
Tim Northover991b12b2016-08-30 20:51:25 +0000212 }
213 }
214
Tim Northover33b07d62016-07-22 20:03:43 +0000215 return Changed;
216}