blob: 5abbcbba72ddd653404c8463fed02d41db68de45 [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"
Krzysztof Parzyszek1a0da8d2017-06-22 20:43:02 +000031#include "llvm/CodeGen/LivePhysRegs.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000032#include "llvm/CodeGen/MachineBasicBlock.h"
33#include "llvm/CodeGen/MachineFunction.h"
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000034#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000035#include "llvm/CodeGen/MachineInstr.h"
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000036#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000037#include "llvm/CodeGen/MachineOperand.h"
38#include "llvm/IR/DebugLoc.h"
39#include "llvm/MC/MCInstrDesc.h"
40#include "llvm/MC/MCRegisterInfo.h"
41#include "llvm/Pass.h"
42#include "llvm/Support/MathExtras.h"
43#include <algorithm>
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000044#include <iterator>
Chandler Carruth6bda14b2017-06-06 11:49:48 +000045#include <limits>
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000046#include <utility>
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000047
48using namespace llvm;
49
50namespace llvm {
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000051
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000052 FunctionPass *createHexagonGenMux();
53 void initializeHexagonGenMuxPass(PassRegistry& Registry);
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000054
55} // end namespace llvm
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000056
57namespace {
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000058
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000059 class HexagonGenMux : public MachineFunctionPass {
60 public:
61 static char ID;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000062
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +000063 HexagonGenMux() : MachineFunctionPass(ID) {}
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000064
Mehdi Amini117296c2016-10-01 02:56:57 +000065 StringRef getPassName() const override {
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000066 return "Hexagon generate mux instructions";
67 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000068
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000069 void getAnalysisUsage(AnalysisUsage &AU) const override {
70 MachineFunctionPass::getAnalysisUsage(AU);
71 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000072
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000073 bool runOnMachineFunction(MachineFunction &MF) override;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000074
Derek Schuff1dbf7a52016-04-04 17:09:25 +000075 MachineFunctionProperties getRequiredProperties() const override {
76 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000077 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000078 }
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000079
80 private:
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +000081 const HexagonInstrInfo *HII = nullptr;
82 const HexagonRegisterInfo *HRI = nullptr;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000083
84 struct CondsetInfo {
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000085 unsigned PredR = 0;
86 unsigned TrueX = std::numeric_limits<unsigned>::max();
87 unsigned FalseX = std::numeric_limits<unsigned>::max();
88
89 CondsetInfo() = default;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000090 };
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000091
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000092 struct DefUseInfo {
93 BitVector Defs, Uses;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000094
95 DefUseInfo() = default;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000096 DefUseInfo(const BitVector &D, const BitVector &U) : Defs(D), Uses(U) {}
97 };
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000098
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000099 struct MuxInfo {
100 MachineBasicBlock::iterator At;
101 unsigned DefR, PredR;
102 MachineOperand *SrcT, *SrcF;
103 MachineInstr *Def1, *Def2;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000104
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000105 MuxInfo(MachineBasicBlock::iterator It, unsigned DR, unsigned PR,
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000106 MachineOperand *TOp, MachineOperand *FOp, MachineInstr &D1,
107 MachineInstr &D2)
108 : At(It), DefR(DR), PredR(PR), SrcT(TOp), SrcF(FOp), Def1(&D1),
109 Def2(&D2) {}
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000110 };
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000111
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000112 typedef DenseMap<MachineInstr*,unsigned> InstrIndexMap;
113 typedef DenseMap<unsigned,DefUseInfo> DefUseInfoMap;
114 typedef SmallVector<MuxInfo,4> MuxInfoList;
115
116 bool isRegPair(unsigned Reg) const {
117 return Hexagon::DoubleRegsRegClass.contains(Reg);
118 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000119
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000120 void getSubRegs(unsigned Reg, BitVector &SRs) const;
121 void expandReg(unsigned Reg, BitVector &Set) const;
122 void getDefsUses(const MachineInstr *MI, BitVector &Defs,
123 BitVector &Uses) const;
124 void buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,
125 DefUseInfoMap &DUM);
126 bool isCondTransfer(unsigned Opc) const;
127 unsigned getMuxOpcode(const MachineOperand &Src1,
128 const MachineOperand &Src2) const;
129 bool genMuxInBlock(MachineBasicBlock &B);
130 };
131
132 char HexagonGenMux::ID = 0;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000133
134} // end anonymous namespace
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000135
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +0000136INITIALIZE_PASS(HexagonGenMux, "hexagon-gen-mux",
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000137 "Hexagon generate mux instructions", false, false)
138
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000139void HexagonGenMux::getSubRegs(unsigned Reg, BitVector &SRs) const {
140 for (MCSubRegIterator I(Reg, HRI); I.isValid(); ++I)
141 SRs[*I] = true;
142}
143
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000144void HexagonGenMux::expandReg(unsigned Reg, BitVector &Set) const {
145 if (isRegPair(Reg))
146 getSubRegs(Reg, Set);
147 else
148 Set[Reg] = true;
149}
150
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000151void HexagonGenMux::getDefsUses(const MachineInstr *MI, BitVector &Defs,
152 BitVector &Uses) const {
153 // First, get the implicit defs and uses for this instruction.
154 unsigned Opc = MI->getOpcode();
155 const MCInstrDesc &D = HII->get(Opc);
Craig Toppere5e035a32015-12-05 07:13:35 +0000156 if (const MCPhysReg *R = D.ImplicitDefs)
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000157 while (*R)
158 expandReg(*R++, Defs);
Craig Toppere5e035a32015-12-05 07:13:35 +0000159 if (const MCPhysReg *R = D.ImplicitUses)
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000160 while (*R)
161 expandReg(*R++, Uses);
162
163 // Look over all operands, and collect explicit defs and uses.
Matthias Braunfc371552016-10-24 21:36:43 +0000164 for (const MachineOperand &MO : MI->operands()) {
165 if (!MO.isReg() || MO.isImplicit())
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000166 continue;
Matthias Braunfc371552016-10-24 21:36:43 +0000167 unsigned R = MO.getReg();
168 BitVector &Set = MO.isDef() ? Defs : Uses;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000169 expandReg(R, Set);
170 }
171}
172
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000173void HexagonGenMux::buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,
174 DefUseInfoMap &DUM) {
175 unsigned Index = 0;
176 unsigned NR = HRI->getNumRegs();
177 BitVector Defs(NR), Uses(NR);
178
179 for (MachineBasicBlock::iterator I = B.begin(), E = B.end(); I != E; ++I) {
180 MachineInstr *MI = &*I;
181 I2X.insert(std::make_pair(MI, Index));
182 Defs.reset();
183 Uses.reset();
184 getDefsUses(MI, Defs, Uses);
185 DUM.insert(std::make_pair(Index, DefUseInfo(Defs, Uses)));
186 Index++;
187 }
188}
189
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000190bool HexagonGenMux::isCondTransfer(unsigned Opc) const {
191 switch (Opc) {
192 case Hexagon::A2_tfrt:
193 case Hexagon::A2_tfrf:
194 case Hexagon::C2_cmoveit:
195 case Hexagon::C2_cmoveif:
196 return true;
197 }
198 return false;
199}
200
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000201unsigned HexagonGenMux::getMuxOpcode(const MachineOperand &Src1,
202 const MachineOperand &Src2) const {
203 bool IsReg1 = Src1.isReg(), IsReg2 = Src2.isReg();
204 if (IsReg1)
205 return IsReg2 ? Hexagon::C2_mux : Hexagon::C2_muxir;
206 if (IsReg2)
207 return Hexagon::C2_muxri;
208
209 // Neither is a register. The first source is extendable, but the second
210 // is not (s8).
211 if (Src2.isImm() && isInt<8>(Src2.getImm()))
212 return Hexagon::C2_muxii;
213
214 return 0;
215}
216
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000217bool HexagonGenMux::genMuxInBlock(MachineBasicBlock &B) {
218 bool Changed = false;
219 InstrIndexMap I2X;
220 DefUseInfoMap DUM;
221 buildMaps(B, I2X, DUM);
222
223 typedef DenseMap<unsigned,CondsetInfo> CondsetMap;
224 CondsetMap CM;
225 MuxInfoList ML;
226
227 MachineBasicBlock::iterator NextI, End = B.end();
228 for (MachineBasicBlock::iterator I = B.begin(); I != End; I = NextI) {
229 MachineInstr *MI = &*I;
230 NextI = std::next(I);
231 unsigned Opc = MI->getOpcode();
232 if (!isCondTransfer(Opc))
233 continue;
234 unsigned DR = MI->getOperand(0).getReg();
235 if (isRegPair(DR))
236 continue;
Krzysztof Parzyszek8a7fb0f2017-06-08 20:56:36 +0000237 MachineOperand &PredOp = MI->getOperand(1);
238 if (PredOp.isUndef())
239 continue;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000240
Krzysztof Parzyszek8a7fb0f2017-06-08 20:56:36 +0000241 unsigned PR = PredOp.getReg();
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000242 unsigned Idx = I2X.lookup(MI);
243 CondsetMap::iterator F = CM.find(DR);
244 bool IfTrue = HII->isPredicatedTrue(Opc);
245
246 // If there is no record of a conditional transfer for this register,
247 // or the predicate register differs, create a new record for it.
248 if (F != CM.end() && F->second.PredR != PR) {
249 CM.erase(F);
250 F = CM.end();
251 }
252 if (F == CM.end()) {
253 auto It = CM.insert(std::make_pair(DR, CondsetInfo()));
254 F = It.first;
255 F->second.PredR = PR;
256 }
257 CondsetInfo &CI = F->second;
258 if (IfTrue)
259 CI.TrueX = Idx;
260 else
261 CI.FalseX = Idx;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000262 if (CI.TrueX == std::numeric_limits<unsigned>::max() ||
263 CI.FalseX == std::numeric_limits<unsigned>::max())
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000264 continue;
265
266 // There is now a complete definition of DR, i.e. we have the predicate
267 // register, the definition if-true, and definition if-false.
268
269 // First, check if both definitions are far enough from the definition
270 // of the predicate register.
271 unsigned MinX = std::min(CI.TrueX, CI.FalseX);
272 unsigned MaxX = std::max(CI.TrueX, CI.FalseX);
273 unsigned SearchX = (MaxX > 4) ? MaxX-4 : 0;
274 bool NearDef = false;
275 for (unsigned X = SearchX; X < MaxX; ++X) {
276 const DefUseInfo &DU = DUM.lookup(X);
277 if (!DU.Defs[PR])
278 continue;
279 NearDef = true;
280 break;
281 }
282 if (NearDef)
283 continue;
284
285 // The predicate register is not defined in the last few instructions.
286 // Check if the conversion to MUX is possible (either "up", i.e. at the
287 // place of the earlier partial definition, or "down", where the later
288 // definition is located). Examine all defs and uses between these two
289 // definitions.
290 // SR1, SR2 - source registers from the first and the second definition.
291 MachineBasicBlock::iterator It1 = B.begin(), It2 = B.begin();
292 std::advance(It1, MinX);
293 std::advance(It2, MaxX);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000294 MachineInstr &Def1 = *It1, &Def2 = *It2;
295 MachineOperand *Src1 = &Def1.getOperand(2), *Src2 = &Def2.getOperand(2);
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000296 unsigned SR1 = Src1->isReg() ? Src1->getReg() : 0;
297 unsigned SR2 = Src2->isReg() ? Src2->getReg() : 0;
298 bool Failure = false, CanUp = true, CanDown = true;
299 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 }
305 if (CanDown && DU.Defs[SR1])
306 CanDown = false;
307 if (CanUp && DU.Defs[SR2])
308 CanUp = false;
309 }
310 if (Failure || (!CanUp && !CanDown))
311 continue;
312
313 MachineOperand *SrcT = (MinX == CI.TrueX) ? Src1 : Src2;
314 MachineOperand *SrcF = (MinX == CI.FalseX) ? Src1 : Src2;
315 // Prefer "down", since this will move the MUX farther away from the
316 // predicate definition.
317 MachineBasicBlock::iterator At = CanDown ? Def2 : Def1;
318 ML.push_back(MuxInfo(At, DR, PR, SrcT, SrcF, Def1, Def2));
319 }
320
Krzysztof Parzyszek1a0da8d2017-06-22 20:43:02 +0000321 for (MuxInfo &MX : ML) {
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000322 unsigned MxOpc = getMuxOpcode(*MX.SrcT, *MX.SrcF);
323 if (!MxOpc)
324 continue;
Krzysztof Parzyszek1a0da8d2017-06-22 20:43:02 +0000325 MachineBasicBlock &B = *MX.At->getParent();
326 const DebugLoc &DL = B.findDebugLoc(MX.At);
327 auto NewMux = BuildMI(B, MX.At, DL, HII->get(MxOpc), MX.DefR)
328 .addReg(MX.PredR)
329 .add(*MX.SrcT)
330 .add(*MX.SrcF);
331 NewMux->clearKillInfo();
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000332 B.erase(MX.Def1);
333 B.erase(MX.Def2);
334 Changed = true;
335 }
336
Krzysztof Parzyszek1a0da8d2017-06-22 20:43:02 +0000337 // Fix up kill flags.
338
339 LivePhysRegs LPR(*HRI);
340 LPR.addLiveOuts(B);
341 auto IsLive = [&LPR,this] (unsigned Reg) -> bool {
342 for (MCSubRegIterator S(Reg, HRI, true); S.isValid(); ++S)
343 if (LPR.contains(*S))
344 return true;
345 return false;
346 };
347 for (auto I = B.rbegin(), E = B.rend(); I != E; ++I) {
348 if (I->isDebugValue())
349 continue;
350 // This isn't 100% accurate, but it's safe.
351 // It won't detect (as a kill) a case like this
352 // r0 = add r0, 1 <-- r0 should be "killed"
353 // ... = r0
354 for (MachineOperand &Op : I->operands()) {
355 if (!Op.isReg() || !Op.isUse())
356 continue;
357 assert(Op.getSubReg() == 0 && "Should have physical registers only");
358 bool Live = IsLive(Op.getReg());
359 Op.setIsKill(!Live);
360 }
361 LPR.stepBackward(*I);
362 }
363
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000364 return Changed;
365}
366
367bool HexagonGenMux::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000368 if (skipFunction(*MF.getFunction()))
369 return false;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000370 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
371 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
372 bool Changed = false;
373 for (auto &I : MF)
374 Changed |= genMuxInBlock(I);
375 return Changed;
376}
377
378FunctionPass *llvm::createHexagonGenMux() {
379 return new HexagonGenMux();
380}