blob: e86356880e99f098f4be470824663d47bd081b3b [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"
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
20#include "llvm/CodeGen/TargetPassConfig.h"
Tim Northover33b07d62016-07-22 20:03:43 +000021#include "llvm/Support/Debug.h"
Tim Northover991b12b2016-08-30 20:51:25 +000022#include "llvm/Target/TargetInstrInfo.h"
Tim Northover33b07d62016-07-22 20:03:43 +000023#include "llvm/Target/TargetSubtargetInfo.h"
24
Tim Northover69fa84a2016-10-14 22:18:18 +000025#define DEBUG_TYPE "legalizer"
Tim Northover33b07d62016-07-22 20:03:43 +000026
27using namespace llvm;
28
Tim Northover69fa84a2016-10-14 22:18:18 +000029char Legalizer::ID = 0;
30INITIALIZE_PASS_BEGIN(Legalizer, DEBUG_TYPE,
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000031 "Legalize the Machine IR a function's Machine IR", false,
32 false)
33INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
Tim Northover69fa84a2016-10-14 22:18:18 +000034INITIALIZE_PASS_END(Legalizer, DEBUG_TYPE,
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000035 "Legalize the Machine IR a function's Machine IR", false,
36 false)
Tim Northover33b07d62016-07-22 20:03:43 +000037
Tim Northover69fa84a2016-10-14 22:18:18 +000038Legalizer::Legalizer() : MachineFunctionPass(ID) {
39 initializeLegalizerPass(*PassRegistry::getPassRegistry());
Tim Northover33b07d62016-07-22 20:03:43 +000040}
41
Tim Northover69fa84a2016-10-14 22:18:18 +000042void Legalizer::getAnalysisUsage(AnalysisUsage &AU) const {
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000043 AU.addRequired<TargetPassConfig>();
44 MachineFunctionPass::getAnalysisUsage(AU);
45}
46
Tim Northover69fa84a2016-10-14 22:18:18 +000047void Legalizer::init(MachineFunction &MF) {
Tim Northover33b07d62016-07-22 20:03:43 +000048}
49
Tim Northover69fa84a2016-10-14 22:18:18 +000050bool Legalizer::combineExtracts(MachineInstr &MI, MachineRegisterInfo &MRI,
51 const TargetInstrInfo &TII) {
Tim Northover991b12b2016-08-30 20:51:25 +000052 bool Changed = false;
53 if (MI.getOpcode() != TargetOpcode::G_EXTRACT)
54 return Changed;
55
56 unsigned NumDefs = (MI.getNumOperands() - 1) / 2;
57 unsigned SrcReg = MI.getOperand(NumDefs).getReg();
58 MachineInstr &SeqI = *MRI.def_instr_begin(SrcReg);
59 if (SeqI.getOpcode() != TargetOpcode::G_SEQUENCE)
60 return Changed;
61
62 unsigned NumSeqSrcs = (SeqI.getNumOperands() - 1) / 2;
63 bool AllDefsReplaced = true;
64
65 // Try to match each register extracted with a corresponding insertion formed
66 // by the G_SEQUENCE.
67 for (unsigned Idx = 0, SeqIdx = 0; Idx < NumDefs; ++Idx) {
68 MachineOperand &ExtractMO = MI.getOperand(Idx);
69 assert(ExtractMO.isReg() && ExtractMO.isDef() &&
70 "unexpected extract operand");
71
72 unsigned ExtractReg = ExtractMO.getReg();
73 unsigned ExtractPos = MI.getOperand(NumDefs + Idx + 1).getImm();
74
75 while (SeqIdx < NumSeqSrcs &&
76 SeqI.getOperand(2 * SeqIdx + 2).getImm() < ExtractPos)
77 ++SeqIdx;
78
Tim Northover0f140c72016-09-09 11:46:34 +000079 if (SeqIdx == NumSeqSrcs) {
Tim Northover991b12b2016-08-30 20:51:25 +000080 AllDefsReplaced = false;
81 continue;
82 }
83
84 unsigned OrigReg = SeqI.getOperand(2 * SeqIdx + 1).getReg();
Tim Northover0f140c72016-09-09 11:46:34 +000085 if (SeqI.getOperand(2 * SeqIdx + 2).getImm() != ExtractPos ||
86 MRI.getType(OrigReg) != MRI.getType(ExtractReg)) {
87 AllDefsReplaced = false;
88 continue;
89 }
90
Tim Northover991b12b2016-08-30 20:51:25 +000091 assert(!TargetRegisterInfo::isPhysicalRegister(OrigReg) &&
92 "unexpected physical register in G_SEQUENCE");
93
94 // Finally we can replace the uses.
95 for (auto &Use : MRI.use_operands(ExtractReg)) {
96 Changed = true;
97 Use.setReg(OrigReg);
98 }
99 }
100
101 if (AllDefsReplaced) {
102 // If SeqI was the next instruction in the BB and we removed it, we'd break
103 // the outer iteration.
104 assert(std::next(MachineBasicBlock::iterator(MI)) != SeqI &&
105 "G_SEQUENCE does not dominate G_EXTRACT");
106
107 MI.eraseFromParent();
108
109 if (MRI.use_empty(SrcReg))
110 SeqI.eraseFromParent();
111 Changed = true;
112 }
113
114 return Changed;
115}
116
Tim Northover69fa84a2016-10-14 22:18:18 +0000117bool Legalizer::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet60495242016-08-27 00:18:24 +0000118 // If the ISel pipeline failed, do not bother running that pass.
119 if (MF.getProperties().hasProperty(
120 MachineFunctionProperties::Property::FailedISel))
121 return false;
Tim Northover33b07d62016-07-22 20:03:43 +0000122 DEBUG(dbgs() << "Legalize Machine IR for: " << MF.getName() << '\n');
123 init(MF);
Quentin Colombet5e60bcd2016-08-27 02:38:21 +0000124 const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
Tim Northover69fa84a2016-10-14 22:18:18 +0000125 const LegalizerInfo &LegalizerInfo = *MF.getSubtarget().getLegalizerInfo();
126 LegalizerHelper Helper(MF);
Tim Northover33b07d62016-07-22 20:03:43 +0000127
128 // FIXME: an instruction may need more than one pass before it is legal. For
129 // example on most architectures <3 x i3> is doubly-illegal. It would
130 // typically proceed along a path like: <3 x i3> -> <3 x i8> -> <8 x i8>. We
131 // probably want a worklist of instructions rather than naive iterate until
132 // convergence for performance reasons.
133 bool Changed = false;
134 MachineBasicBlock::iterator NextMI;
135 for (auto &MBB : MF)
136 for (auto MI = MBB.begin(); MI != MBB.end(); MI = NextMI) {
137 // Get the next Instruction before we try to legalize, because there's a
138 // good chance MI will be deleted.
139 NextMI = std::next(MI);
Ahmed Bougachafaf8e9f2016-08-02 11:41:09 +0000140
141 // Only legalize pre-isel generic instructions: others don't have types
142 // and are assumed to be legal.
143 if (!isPreISelGenericOpcode(MI->getOpcode()))
144 continue;
145
Tim Northover69fa84a2016-10-14 22:18:18 +0000146 auto Res = Helper.legalizeInstr(*MI, LegalizerInfo);
Tim Northover33b07d62016-07-22 20:03:43 +0000147
148 // Error out if we couldn't legalize this instruction. We may want to fall
149 // back to DAG ISel instead in the future.
Tim Northover69fa84a2016-10-14 22:18:18 +0000150 if (Res == LegalizerHelper::UnableToLegalize) {
Quentin Colombet5e60bcd2016-08-27 02:38:21 +0000151 if (!TPC.isGlobalISelAbortEnabled()) {
152 MF.getProperties().set(
153 MachineFunctionProperties::Property::FailedISel);
154 return false;
155 }
Tim Northover33b07d62016-07-22 20:03:43 +0000156 std::string Msg;
157 raw_string_ostream OS(Msg);
158 OS << "unable to legalize instruction: ";
159 MI->print(OS);
160 report_fatal_error(OS.str());
161 }
162
Tim Northover69fa84a2016-10-14 22:18:18 +0000163 Changed |= Res == LegalizerHelper::Legalized;
Tim Northover33b07d62016-07-22 20:03:43 +0000164 }
Tim Northover991b12b2016-08-30 20:51:25 +0000165
166
167 MachineRegisterInfo &MRI = MF.getRegInfo();
168 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
169 for (auto &MBB : MF) {
170 for (auto MI = MBB.begin(); MI != MBB.end(); MI = NextMI) {
171 // Get the next Instruction before we try to legalize, because there's a
172 // good chance MI will be deleted.
173 NextMI = std::next(MI);
174
175 Changed |= combineExtracts(*MI, MRI, TII);
176 }
177 }
178
Tim Northover33b07d62016-07-22 20:03:43 +0000179 return Changed;
180}