blob: a2502ce5ec34b31838bddd47c8fe2898d21dddbd [file] [log] [blame]
Krzysztof Parzyszek92172202015-07-20 21:23:25 +00001//===--- HexagonGenMux.cpp ------------------------------------------------===//
2//
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
10// During instruction selection, MUX instructions are generated for
11// conditional assignments. Since such assignments often present an
12// opportunity to predicate instructions, HexagonExpandCondsets
13// expands MUXes into pairs of conditional transfers, and then proceeds
14// with predication of the producers/consumers of the registers involved.
15// This happens after exiting from the SSA form, but before the machine
16// instruction scheduler. After the scheduler and after the register
17// allocation there can be cases of pairs of conditional transfers
18// resulting from a MUX where neither of them was further predicated. If
19// these transfers are now placed far enough from the instruction defining
20// the predicate register, they cannot use the .new form. In such cases it
21// is better to collapse them back to a single MUX instruction.
22
23#define DEBUG_TYPE "hexmux"
24
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000025#include "HexagonInstrInfo.h"
26#include "HexagonRegisterInfo.h"
27#include "HexagonSubtarget.h"
28#include "llvm/ADT/BitVector.h"
29#include "llvm/ADT/SmallVector.h"
30#include "llvm/ADT/StringRef.h"
31#include "llvm/CodeGen/MachineBasicBlock.h"
32#include "llvm/CodeGen/MachineFunction.h"
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000033#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000034#include "llvm/CodeGen/MachineInstr.h"
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000035#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000036#include "llvm/CodeGen/MachineOperand.h"
37#include "llvm/IR/DebugLoc.h"
38#include "llvm/MC/MCInstrDesc.h"
39#include "llvm/MC/MCRegisterInfo.h"
40#include "llvm/Pass.h"
41#include "llvm/Support/MathExtras.h"
42#include <algorithm>
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000043#include <iterator>
Chandler Carruth6bda14b2017-06-06 11:49:48 +000044#include <limits>
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000045#include <utility>
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000046
47using namespace llvm;
48
49namespace llvm {
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000050
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000051 FunctionPass *createHexagonGenMux();
52 void initializeHexagonGenMuxPass(PassRegistry& Registry);
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000053
54} // end namespace llvm
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000055
56namespace {
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000057
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000058 class HexagonGenMux : public MachineFunctionPass {
59 public:
60 static char ID;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000061
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +000062 HexagonGenMux() : MachineFunctionPass(ID) {}
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000063
Mehdi Amini117296c2016-10-01 02:56:57 +000064 StringRef getPassName() const override {
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000065 return "Hexagon generate mux instructions";
66 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000067
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000068 void getAnalysisUsage(AnalysisUsage &AU) const override {
69 MachineFunctionPass::getAnalysisUsage(AU);
70 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000071
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000072 bool runOnMachineFunction(MachineFunction &MF) override;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000073
Derek Schuff1dbf7a52016-04-04 17:09:25 +000074 MachineFunctionProperties getRequiredProperties() const override {
75 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000076 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000077 }
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000078
79 private:
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +000080 const HexagonInstrInfo *HII = nullptr;
81 const HexagonRegisterInfo *HRI = nullptr;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000082
83 struct CondsetInfo {
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000084 unsigned PredR = 0;
85 unsigned TrueX = std::numeric_limits<unsigned>::max();
86 unsigned FalseX = std::numeric_limits<unsigned>::max();
87
88 CondsetInfo() = default;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000089 };
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000090
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000091 struct DefUseInfo {
92 BitVector Defs, Uses;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000093
94 DefUseInfo() = default;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000095 DefUseInfo(const BitVector &D, const BitVector &U) : Defs(D), Uses(U) {}
96 };
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000097
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000098 struct MuxInfo {
99 MachineBasicBlock::iterator At;
100 unsigned DefR, PredR;
101 MachineOperand *SrcT, *SrcF;
102 MachineInstr *Def1, *Def2;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000103
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000104 MuxInfo(MachineBasicBlock::iterator It, unsigned DR, unsigned PR,
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000105 MachineOperand *TOp, MachineOperand *FOp, MachineInstr &D1,
106 MachineInstr &D2)
107 : At(It), DefR(DR), PredR(PR), SrcT(TOp), SrcF(FOp), Def1(&D1),
108 Def2(&D2) {}
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000109 };
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000110
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000111 typedef DenseMap<MachineInstr*,unsigned> InstrIndexMap;
112 typedef DenseMap<unsigned,DefUseInfo> DefUseInfoMap;
113 typedef SmallVector<MuxInfo,4> MuxInfoList;
114
115 bool isRegPair(unsigned Reg) const {
116 return Hexagon::DoubleRegsRegClass.contains(Reg);
117 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000118
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000119 void getSubRegs(unsigned Reg, BitVector &SRs) const;
120 void expandReg(unsigned Reg, BitVector &Set) const;
121 void getDefsUses(const MachineInstr *MI, BitVector &Defs,
122 BitVector &Uses) const;
123 void buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,
124 DefUseInfoMap &DUM);
125 bool isCondTransfer(unsigned Opc) const;
126 unsigned getMuxOpcode(const MachineOperand &Src1,
127 const MachineOperand &Src2) const;
128 bool genMuxInBlock(MachineBasicBlock &B);
129 };
130
131 char HexagonGenMux::ID = 0;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000132
133} // end anonymous namespace
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000134
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +0000135INITIALIZE_PASS(HexagonGenMux, "hexagon-gen-mux",
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000136 "Hexagon generate mux instructions", false, false)
137
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000138void HexagonGenMux::getSubRegs(unsigned Reg, BitVector &SRs) const {
139 for (MCSubRegIterator I(Reg, HRI); I.isValid(); ++I)
140 SRs[*I] = true;
141}
142
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000143void HexagonGenMux::expandReg(unsigned Reg, BitVector &Set) const {
144 if (isRegPair(Reg))
145 getSubRegs(Reg, Set);
146 else
147 Set[Reg] = true;
148}
149
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000150void HexagonGenMux::getDefsUses(const MachineInstr *MI, BitVector &Defs,
151 BitVector &Uses) const {
152 // First, get the implicit defs and uses for this instruction.
153 unsigned Opc = MI->getOpcode();
154 const MCInstrDesc &D = HII->get(Opc);
Craig Toppere5e035a32015-12-05 07:13:35 +0000155 if (const MCPhysReg *R = D.ImplicitDefs)
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000156 while (*R)
157 expandReg(*R++, Defs);
Craig Toppere5e035a32015-12-05 07:13:35 +0000158 if (const MCPhysReg *R = D.ImplicitUses)
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000159 while (*R)
160 expandReg(*R++, Uses);
161
162 // Look over all operands, and collect explicit defs and uses.
Matthias Braunfc371552016-10-24 21:36:43 +0000163 for (const MachineOperand &MO : MI->operands()) {
164 if (!MO.isReg() || MO.isImplicit())
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000165 continue;
Matthias Braunfc371552016-10-24 21:36:43 +0000166 unsigned R = MO.getReg();
167 BitVector &Set = MO.isDef() ? Defs : Uses;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000168 expandReg(R, Set);
169 }
170}
171
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000172void HexagonGenMux::buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,
173 DefUseInfoMap &DUM) {
174 unsigned Index = 0;
175 unsigned NR = HRI->getNumRegs();
176 BitVector Defs(NR), Uses(NR);
177
178 for (MachineBasicBlock::iterator I = B.begin(), E = B.end(); I != E; ++I) {
179 MachineInstr *MI = &*I;
180 I2X.insert(std::make_pair(MI, Index));
181 Defs.reset();
182 Uses.reset();
183 getDefsUses(MI, Defs, Uses);
184 DUM.insert(std::make_pair(Index, DefUseInfo(Defs, Uses)));
185 Index++;
186 }
187}
188
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000189bool HexagonGenMux::isCondTransfer(unsigned Opc) const {
190 switch (Opc) {
191 case Hexagon::A2_tfrt:
192 case Hexagon::A2_tfrf:
193 case Hexagon::C2_cmoveit:
194 case Hexagon::C2_cmoveif:
195 return true;
196 }
197 return false;
198}
199
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000200unsigned HexagonGenMux::getMuxOpcode(const MachineOperand &Src1,
201 const MachineOperand &Src2) const {
202 bool IsReg1 = Src1.isReg(), IsReg2 = Src2.isReg();
203 if (IsReg1)
204 return IsReg2 ? Hexagon::C2_mux : Hexagon::C2_muxir;
205 if (IsReg2)
206 return Hexagon::C2_muxri;
207
208 // Neither is a register. The first source is extendable, but the second
209 // is not (s8).
210 if (Src2.isImm() && isInt<8>(Src2.getImm()))
211 return Hexagon::C2_muxii;
212
213 return 0;
214}
215
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000216bool HexagonGenMux::genMuxInBlock(MachineBasicBlock &B) {
217 bool Changed = false;
218 InstrIndexMap I2X;
219 DefUseInfoMap DUM;
220 buildMaps(B, I2X, DUM);
221
222 typedef DenseMap<unsigned,CondsetInfo> CondsetMap;
223 CondsetMap CM;
224 MuxInfoList ML;
225
226 MachineBasicBlock::iterator NextI, End = B.end();
227 for (MachineBasicBlock::iterator I = B.begin(); I != End; I = NextI) {
228 MachineInstr *MI = &*I;
229 NextI = std::next(I);
230 unsigned Opc = MI->getOpcode();
231 if (!isCondTransfer(Opc))
232 continue;
233 unsigned DR = MI->getOperand(0).getReg();
234 if (isRegPair(DR))
235 continue;
Krzysztof Parzyszek8a7fb0f2017-06-08 20:56:36 +0000236 MachineOperand &PredOp = MI->getOperand(1);
237 if (PredOp.isUndef())
238 continue;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000239
Krzysztof Parzyszek8a7fb0f2017-06-08 20:56:36 +0000240 unsigned PR = PredOp.getReg();
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000241 unsigned Idx = I2X.lookup(MI);
242 CondsetMap::iterator F = CM.find(DR);
243 bool IfTrue = HII->isPredicatedTrue(Opc);
244
245 // If there is no record of a conditional transfer for this register,
246 // or the predicate register differs, create a new record for it.
247 if (F != CM.end() && F->second.PredR != PR) {
248 CM.erase(F);
249 F = CM.end();
250 }
251 if (F == CM.end()) {
252 auto It = CM.insert(std::make_pair(DR, CondsetInfo()));
253 F = It.first;
254 F->second.PredR = PR;
255 }
256 CondsetInfo &CI = F->second;
257 if (IfTrue)
258 CI.TrueX = Idx;
259 else
260 CI.FalseX = Idx;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000261 if (CI.TrueX == std::numeric_limits<unsigned>::max() ||
262 CI.FalseX == std::numeric_limits<unsigned>::max())
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000263 continue;
264
265 // There is now a complete definition of DR, i.e. we have the predicate
266 // register, the definition if-true, and definition if-false.
267
268 // First, check if both definitions are far enough from the definition
269 // of the predicate register.
270 unsigned MinX = std::min(CI.TrueX, CI.FalseX);
271 unsigned MaxX = std::max(CI.TrueX, CI.FalseX);
272 unsigned SearchX = (MaxX > 4) ? MaxX-4 : 0;
273 bool NearDef = false;
274 for (unsigned X = SearchX; X < MaxX; ++X) {
275 const DefUseInfo &DU = DUM.lookup(X);
276 if (!DU.Defs[PR])
277 continue;
278 NearDef = true;
279 break;
280 }
281 if (NearDef)
282 continue;
283
284 // The predicate register is not defined in the last few instructions.
285 // Check if the conversion to MUX is possible (either "up", i.e. at the
286 // place of the earlier partial definition, or "down", where the later
287 // definition is located). Examine all defs and uses between these two
288 // definitions.
289 // SR1, SR2 - source registers from the first and the second definition.
290 MachineBasicBlock::iterator It1 = B.begin(), It2 = B.begin();
291 std::advance(It1, MinX);
292 std::advance(It2, MaxX);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000293 MachineInstr &Def1 = *It1, &Def2 = *It2;
294 MachineOperand *Src1 = &Def1.getOperand(2), *Src2 = &Def2.getOperand(2);
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000295 unsigned SR1 = Src1->isReg() ? Src1->getReg() : 0;
296 unsigned SR2 = Src2->isReg() ? Src2->getReg() : 0;
297 bool Failure = false, CanUp = true, CanDown = true;
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +0000298 bool Used1 = false, Used2 = false;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000299 for (unsigned X = MinX+1; X < MaxX; X++) {
300 const DefUseInfo &DU = DUM.lookup(X);
301 if (DU.Defs[PR] || DU.Defs[DR] || DU.Uses[DR]) {
302 Failure = true;
303 break;
304 }
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +0000305 Used1 |= DU.Uses[SR1];
306 Used2 |= DU.Uses[SR2];
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000307 if (CanDown && DU.Defs[SR1])
308 CanDown = false;
309 if (CanUp && DU.Defs[SR2])
310 CanUp = false;
311 }
312 if (Failure || (!CanUp && !CanDown))
313 continue;
314
315 MachineOperand *SrcT = (MinX == CI.TrueX) ? Src1 : Src2;
316 MachineOperand *SrcF = (MinX == CI.FalseX) ? Src1 : Src2;
317 // Prefer "down", since this will move the MUX farther away from the
318 // predicate definition.
319 MachineBasicBlock::iterator At = CanDown ? Def2 : Def1;
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +0000320 if (!CanDown) {
321 // If the MUX is placed "up", it shouldn't kill any source registers
322 // that are still used afterwards. We can reset the kill flags directly
323 // on the operands, because the source instructions will be erased.
324 if (Used1 && Src1->isReg())
325 Src1->setIsKill(false);
326 if (Used2 && Src2->isReg())
327 Src2->setIsKill(false);
328 }
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000329 ML.push_back(MuxInfo(At, DR, PR, SrcT, SrcF, Def1, Def2));
330 }
331
332 for (unsigned I = 0, N = ML.size(); I < N; ++I) {
333 MuxInfo &MX = ML[I];
334 MachineBasicBlock &B = *MX.At->getParent();
335 DebugLoc DL = MX.At->getDebugLoc();
336 unsigned MxOpc = getMuxOpcode(*MX.SrcT, *MX.SrcF);
337 if (!MxOpc)
338 continue;
339 BuildMI(B, MX.At, DL, HII->get(MxOpc), MX.DefR)
Diana Picus116bbab2017-01-13 09:58:52 +0000340 .addReg(MX.PredR)
341 .add(*MX.SrcT)
342 .add(*MX.SrcF);
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000343 B.erase(MX.Def1);
344 B.erase(MX.Def2);
345 Changed = true;
346 }
347
348 return Changed;
349}
350
351bool HexagonGenMux::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000352 if (skipFunction(*MF.getFunction()))
353 return false;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000354 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
355 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
356 bool Changed = false;
357 for (auto &I : MF)
358 Changed |= genMuxInBlock(I);
359 return Changed;
360}
361
362FunctionPass *llvm::createHexagonGenMux() {
363 return new HexagonGenMux();
364}