blob: e5af96468af1974beaa3a2a67fe01923be375135 [file] [log] [blame]
Eugene Zelenko4d060b72017-07-29 00:56:56 +00001//===- HexagonGenMux.cpp --------------------------------------------------===//
Krzysztof Parzyszek92172202015-07-20 21:23:25 +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
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"
Eugene Zelenko4d060b72017-07-29 00:56:56 +000029#include "llvm/ADT/DenseMap.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000030#include "llvm/ADT/SmallVector.h"
31#include "llvm/ADT/StringRef.h"
Krzysztof Parzyszek1a0da8d2017-06-22 20:43:02 +000032#include "llvm/CodeGen/LivePhysRegs.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000033#include "llvm/CodeGen/MachineBasicBlock.h"
34#include "llvm/CodeGen/MachineFunction.h"
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000035#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000036#include "llvm/CodeGen/MachineInstr.h"
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000037#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000038#include "llvm/CodeGen/MachineOperand.h"
39#include "llvm/IR/DebugLoc.h"
40#include "llvm/MC/MCInstrDesc.h"
41#include "llvm/MC/MCRegisterInfo.h"
42#include "llvm/Pass.h"
Krzysztof Parzyszek5f7ba9a2018-03-23 18:43:09 +000043#include "llvm/Support/CommandLine.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000044#include "llvm/Support/MathExtras.h"
45#include <algorithm>
Eugene Zelenko4d060b72017-07-29 00:56:56 +000046#include <cassert>
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000047#include <iterator>
Chandler Carruth6bda14b2017-06-06 11:49:48 +000048#include <limits>
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000049#include <utility>
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000050
51using namespace llvm;
52
53namespace llvm {
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000054
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000055 FunctionPass *createHexagonGenMux();
56 void initializeHexagonGenMuxPass(PassRegistry& Registry);
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000057
58} // end namespace llvm
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000059
Krzysztof Parzyszek5f7ba9a2018-03-23 18:43:09 +000060// Initialize this to 0 to always prefer generating mux by default.
61static cl::opt<unsigned> MinPredDist("hexagon-gen-mux-threshold", cl::Hidden,
62 cl::init(0), cl::desc("Minimum distance between predicate definition and "
63 "farther of the two predicated uses"));
64
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000065namespace {
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000066
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000067 class HexagonGenMux : public MachineFunctionPass {
68 public:
69 static char ID;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000070
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +000071 HexagonGenMux() : MachineFunctionPass(ID) {}
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000072
Mehdi Amini117296c2016-10-01 02:56:57 +000073 StringRef getPassName() const override {
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000074 return "Hexagon generate mux instructions";
75 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000076
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000077 void getAnalysisUsage(AnalysisUsage &AU) const override {
78 MachineFunctionPass::getAnalysisUsage(AU);
79 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000080
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000081 bool runOnMachineFunction(MachineFunction &MF) override;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000082
Derek Schuff1dbf7a52016-04-04 17:09:25 +000083 MachineFunctionProperties getRequiredProperties() const override {
84 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000085 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000086 }
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000087
88 private:
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +000089 const HexagonInstrInfo *HII = nullptr;
90 const HexagonRegisterInfo *HRI = nullptr;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000091
92 struct CondsetInfo {
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000093 unsigned PredR = 0;
94 unsigned TrueX = std::numeric_limits<unsigned>::max();
95 unsigned FalseX = std::numeric_limits<unsigned>::max();
96
97 CondsetInfo() = default;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000098 };
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000099
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000100 struct DefUseInfo {
101 BitVector Defs, Uses;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000102
103 DefUseInfo() = default;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000104 DefUseInfo(const BitVector &D, const BitVector &U) : Defs(D), Uses(U) {}
105 };
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000106
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000107 struct MuxInfo {
108 MachineBasicBlock::iterator At;
109 unsigned DefR, PredR;
110 MachineOperand *SrcT, *SrcF;
111 MachineInstr *Def1, *Def2;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000112
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000113 MuxInfo(MachineBasicBlock::iterator It, unsigned DR, unsigned PR,
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000114 MachineOperand *TOp, MachineOperand *FOp, MachineInstr &D1,
115 MachineInstr &D2)
116 : At(It), DefR(DR), PredR(PR), SrcT(TOp), SrcF(FOp), Def1(&D1),
117 Def2(&D2) {}
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000118 };
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000119
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000120 using InstrIndexMap = DenseMap<MachineInstr *, unsigned>;
121 using DefUseInfoMap = DenseMap<unsigned, DefUseInfo>;
122 using MuxInfoList = SmallVector<MuxInfo, 4>;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000123
124 bool isRegPair(unsigned Reg) const {
125 return Hexagon::DoubleRegsRegClass.contains(Reg);
126 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000127
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000128 void getSubRegs(unsigned Reg, BitVector &SRs) const;
129 void expandReg(unsigned Reg, BitVector &Set) const;
130 void getDefsUses(const MachineInstr *MI, BitVector &Defs,
131 BitVector &Uses) const;
132 void buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,
133 DefUseInfoMap &DUM);
134 bool isCondTransfer(unsigned Opc) const;
135 unsigned getMuxOpcode(const MachineOperand &Src1,
136 const MachineOperand &Src2) const;
137 bool genMuxInBlock(MachineBasicBlock &B);
138 };
139
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000140} // end anonymous namespace
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000141
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000142char HexagonGenMux::ID = 0;
143
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +0000144INITIALIZE_PASS(HexagonGenMux, "hexagon-gen-mux",
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000145 "Hexagon generate mux instructions", false, false)
146
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000147void HexagonGenMux::getSubRegs(unsigned Reg, BitVector &SRs) const {
148 for (MCSubRegIterator I(Reg, HRI); I.isValid(); ++I)
149 SRs[*I] = true;
150}
151
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000152void HexagonGenMux::expandReg(unsigned Reg, BitVector &Set) const {
153 if (isRegPair(Reg))
154 getSubRegs(Reg, Set);
155 else
156 Set[Reg] = true;
157}
158
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000159void HexagonGenMux::getDefsUses(const MachineInstr *MI, BitVector &Defs,
160 BitVector &Uses) const {
161 // First, get the implicit defs and uses for this instruction.
162 unsigned Opc = MI->getOpcode();
163 const MCInstrDesc &D = HII->get(Opc);
Craig Toppere5e035a32015-12-05 07:13:35 +0000164 if (const MCPhysReg *R = D.ImplicitDefs)
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000165 while (*R)
166 expandReg(*R++, Defs);
Craig Toppere5e035a32015-12-05 07:13:35 +0000167 if (const MCPhysReg *R = D.ImplicitUses)
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000168 while (*R)
169 expandReg(*R++, Uses);
170
171 // Look over all operands, and collect explicit defs and uses.
Matthias Braunfc371552016-10-24 21:36:43 +0000172 for (const MachineOperand &MO : MI->operands()) {
173 if (!MO.isReg() || MO.isImplicit())
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000174 continue;
Matthias Braunfc371552016-10-24 21:36:43 +0000175 unsigned R = MO.getReg();
176 BitVector &Set = MO.isDef() ? Defs : Uses;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000177 expandReg(R, Set);
178 }
179}
180
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000181void HexagonGenMux::buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,
182 DefUseInfoMap &DUM) {
183 unsigned Index = 0;
184 unsigned NR = HRI->getNumRegs();
185 BitVector Defs(NR), Uses(NR);
186
187 for (MachineBasicBlock::iterator I = B.begin(), E = B.end(); I != E; ++I) {
188 MachineInstr *MI = &*I;
189 I2X.insert(std::make_pair(MI, Index));
190 Defs.reset();
191 Uses.reset();
192 getDefsUses(MI, Defs, Uses);
193 DUM.insert(std::make_pair(Index, DefUseInfo(Defs, Uses)));
194 Index++;
195 }
196}
197
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000198bool HexagonGenMux::isCondTransfer(unsigned Opc) const {
199 switch (Opc) {
200 case Hexagon::A2_tfrt:
201 case Hexagon::A2_tfrf:
202 case Hexagon::C2_cmoveit:
203 case Hexagon::C2_cmoveif:
204 return true;
205 }
206 return false;
207}
208
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000209unsigned HexagonGenMux::getMuxOpcode(const MachineOperand &Src1,
210 const MachineOperand &Src2) const {
211 bool IsReg1 = Src1.isReg(), IsReg2 = Src2.isReg();
212 if (IsReg1)
213 return IsReg2 ? Hexagon::C2_mux : Hexagon::C2_muxir;
214 if (IsReg2)
215 return Hexagon::C2_muxri;
216
217 // Neither is a register. The first source is extendable, but the second
218 // is not (s8).
219 if (Src2.isImm() && isInt<8>(Src2.getImm()))
220 return Hexagon::C2_muxii;
221
222 return 0;
223}
224
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000225bool HexagonGenMux::genMuxInBlock(MachineBasicBlock &B) {
226 bool Changed = false;
227 InstrIndexMap I2X;
228 DefUseInfoMap DUM;
229 buildMaps(B, I2X, DUM);
230
Eugene Zelenko4d060b72017-07-29 00:56:56 +0000231 using CondsetMap = DenseMap<unsigned, CondsetInfo>;
232
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000233 CondsetMap CM;
234 MuxInfoList ML;
235
236 MachineBasicBlock::iterator NextI, End = B.end();
237 for (MachineBasicBlock::iterator I = B.begin(); I != End; I = NextI) {
238 MachineInstr *MI = &*I;
239 NextI = std::next(I);
240 unsigned Opc = MI->getOpcode();
241 if (!isCondTransfer(Opc))
242 continue;
243 unsigned DR = MI->getOperand(0).getReg();
244 if (isRegPair(DR))
245 continue;
Krzysztof Parzyszek8a7fb0f2017-06-08 20:56:36 +0000246 MachineOperand &PredOp = MI->getOperand(1);
247 if (PredOp.isUndef())
248 continue;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000249
Krzysztof Parzyszek8a7fb0f2017-06-08 20:56:36 +0000250 unsigned PR = PredOp.getReg();
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000251 unsigned Idx = I2X.lookup(MI);
252 CondsetMap::iterator F = CM.find(DR);
253 bool IfTrue = HII->isPredicatedTrue(Opc);
254
255 // If there is no record of a conditional transfer for this register,
256 // or the predicate register differs, create a new record for it.
257 if (F != CM.end() && F->second.PredR != PR) {
258 CM.erase(F);
259 F = CM.end();
260 }
261 if (F == CM.end()) {
262 auto It = CM.insert(std::make_pair(DR, CondsetInfo()));
263 F = It.first;
264 F->second.PredR = PR;
265 }
266 CondsetInfo &CI = F->second;
267 if (IfTrue)
268 CI.TrueX = Idx;
269 else
270 CI.FalseX = Idx;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000271 if (CI.TrueX == std::numeric_limits<unsigned>::max() ||
272 CI.FalseX == std::numeric_limits<unsigned>::max())
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000273 continue;
274
275 // There is now a complete definition of DR, i.e. we have the predicate
276 // register, the definition if-true, and definition if-false.
277
Krzysztof Parzyszek5f7ba9a2018-03-23 18:43:09 +0000278 // First, check if the definitions are far enough from the definition
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000279 // of the predicate register.
280 unsigned MinX = std::min(CI.TrueX, CI.FalseX);
281 unsigned MaxX = std::max(CI.TrueX, CI.FalseX);
Krzysztof Parzyszek5f7ba9a2018-03-23 18:43:09 +0000282 // Specifically, check if the predicate definition is within a prescribed
283 // distance from the farther of the two predicated instructions.
284 unsigned SearchX = (MaxX >= MinPredDist) ? MaxX-MinPredDist : 0;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000285 bool NearDef = false;
286 for (unsigned X = SearchX; X < MaxX; ++X) {
287 const DefUseInfo &DU = DUM.lookup(X);
288 if (!DU.Defs[PR])
289 continue;
290 NearDef = true;
291 break;
292 }
293 if (NearDef)
294 continue;
295
296 // The predicate register is not defined in the last few instructions.
297 // Check if the conversion to MUX is possible (either "up", i.e. at the
298 // place of the earlier partial definition, or "down", where the later
299 // definition is located). Examine all defs and uses between these two
300 // definitions.
301 // SR1, SR2 - source registers from the first and the second definition.
302 MachineBasicBlock::iterator It1 = B.begin(), It2 = B.begin();
303 std::advance(It1, MinX);
304 std::advance(It2, MaxX);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000305 MachineInstr &Def1 = *It1, &Def2 = *It2;
306 MachineOperand *Src1 = &Def1.getOperand(2), *Src2 = &Def2.getOperand(2);
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000307 unsigned SR1 = Src1->isReg() ? Src1->getReg() : 0;
308 unsigned SR2 = Src2->isReg() ? Src2->getReg() : 0;
309 bool Failure = false, CanUp = true, CanDown = true;
310 for (unsigned X = MinX+1; X < MaxX; X++) {
311 const DefUseInfo &DU = DUM.lookup(X);
312 if (DU.Defs[PR] || DU.Defs[DR] || DU.Uses[DR]) {
313 Failure = true;
314 break;
315 }
316 if (CanDown && DU.Defs[SR1])
317 CanDown = false;
318 if (CanUp && DU.Defs[SR2])
319 CanUp = false;
320 }
321 if (Failure || (!CanUp && !CanDown))
322 continue;
323
324 MachineOperand *SrcT = (MinX == CI.TrueX) ? Src1 : Src2;
325 MachineOperand *SrcF = (MinX == CI.FalseX) ? Src1 : Src2;
326 // Prefer "down", since this will move the MUX farther away from the
327 // predicate definition.
328 MachineBasicBlock::iterator At = CanDown ? Def2 : Def1;
329 ML.push_back(MuxInfo(At, DR, PR, SrcT, SrcF, Def1, Def2));
330 }
331
Krzysztof Parzyszek1a0da8d2017-06-22 20:43:02 +0000332 for (MuxInfo &MX : ML) {
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000333 unsigned MxOpc = getMuxOpcode(*MX.SrcT, *MX.SrcF);
334 if (!MxOpc)
335 continue;
Krzysztof Parzyszek1a0da8d2017-06-22 20:43:02 +0000336 MachineBasicBlock &B = *MX.At->getParent();
337 const DebugLoc &DL = B.findDebugLoc(MX.At);
338 auto NewMux = BuildMI(B, MX.At, DL, HII->get(MxOpc), MX.DefR)
339 .addReg(MX.PredR)
340 .add(*MX.SrcT)
341 .add(*MX.SrcF);
342 NewMux->clearKillInfo();
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000343 B.erase(MX.Def1);
344 B.erase(MX.Def2);
345 Changed = true;
346 }
347
Krzysztof Parzyszek1a0da8d2017-06-22 20:43:02 +0000348 // Fix up kill flags.
349
350 LivePhysRegs LPR(*HRI);
351 LPR.addLiveOuts(B);
352 auto IsLive = [&LPR,this] (unsigned Reg) -> bool {
353 for (MCSubRegIterator S(Reg, HRI, true); S.isValid(); ++S)
354 if (LPR.contains(*S))
355 return true;
356 return false;
357 };
358 for (auto I = B.rbegin(), E = B.rend(); I != E; ++I) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000359 if (I->isDebugInstr())
Krzysztof Parzyszek1a0da8d2017-06-22 20:43:02 +0000360 continue;
361 // This isn't 100% accurate, but it's safe.
362 // It won't detect (as a kill) a case like this
363 // r0 = add r0, 1 <-- r0 should be "killed"
364 // ... = r0
365 for (MachineOperand &Op : I->operands()) {
366 if (!Op.isReg() || !Op.isUse())
367 continue;
368 assert(Op.getSubReg() == 0 && "Should have physical registers only");
369 bool Live = IsLive(Op.getReg());
370 Op.setIsKill(!Live);
371 }
372 LPR.stepBackward(*I);
373 }
374
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000375 return Changed;
376}
377
378bool HexagonGenMux::runOnMachineFunction(MachineFunction &MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000379 if (skipFunction(MF.getFunction()))
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000380 return false;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000381 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
382 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
383 bool Changed = false;
384 for (auto &I : MF)
385 Changed |= genMuxInBlock(I);
386 return Changed;
387}
388
389FunctionPass *llvm::createHexagonGenMux() {
390 return new HexagonGenMux();
391}