blob: 3c37d9ebb0ebc5f3eab7115866b939f281114c89 [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
62 HexagonGenMux() : MachineFunctionPass(ID), HII(nullptr), HRI(nullptr) {
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000063 initializeHexagonGenMuxPass(*PassRegistry::getPassRegistry());
64 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000065
Mehdi Amini117296c2016-10-01 02:56:57 +000066 StringRef getPassName() const override {
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000067 return "Hexagon generate mux instructions";
68 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000069
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000070 void getAnalysisUsage(AnalysisUsage &AU) const override {
71 MachineFunctionPass::getAnalysisUsage(AU);
72 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000073
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000074 bool runOnMachineFunction(MachineFunction &MF) override;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000075
Derek Schuff1dbf7a52016-04-04 17:09:25 +000076 MachineFunctionProperties getRequiredProperties() const override {
77 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000078 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000079 }
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000080
81 private:
82 const HexagonInstrInfo *HII;
83 const HexagonRegisterInfo *HRI;
84
85 struct CondsetInfo {
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000086 unsigned PredR = 0;
87 unsigned TrueX = std::numeric_limits<unsigned>::max();
88 unsigned FalseX = std::numeric_limits<unsigned>::max();
89
90 CondsetInfo() = default;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000091 };
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000092
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000093 struct DefUseInfo {
94 BitVector Defs, Uses;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000095
96 DefUseInfo() = default;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000097 DefUseInfo(const BitVector &D, const BitVector &U) : Defs(D), Uses(U) {}
98 };
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000099
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000100 struct MuxInfo {
101 MachineBasicBlock::iterator At;
102 unsigned DefR, PredR;
103 MachineOperand *SrcT, *SrcF;
104 MachineInstr *Def1, *Def2;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000105
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000106 MuxInfo(MachineBasicBlock::iterator It, unsigned DR, unsigned PR,
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000107 MachineOperand *TOp, MachineOperand *FOp, MachineInstr &D1,
108 MachineInstr &D2)
109 : At(It), DefR(DR), PredR(PR), SrcT(TOp), SrcF(FOp), Def1(&D1),
110 Def2(&D2) {}
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000111 };
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000112
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000113 typedef DenseMap<MachineInstr*,unsigned> InstrIndexMap;
114 typedef DenseMap<unsigned,DefUseInfo> DefUseInfoMap;
115 typedef SmallVector<MuxInfo,4> MuxInfoList;
116
117 bool isRegPair(unsigned Reg) const {
118 return Hexagon::DoubleRegsRegClass.contains(Reg);
119 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000120
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000121 void getSubRegs(unsigned Reg, BitVector &SRs) const;
122 void expandReg(unsigned Reg, BitVector &Set) const;
123 void getDefsUses(const MachineInstr *MI, BitVector &Defs,
124 BitVector &Uses) const;
125 void buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,
126 DefUseInfoMap &DUM);
127 bool isCondTransfer(unsigned Opc) const;
128 unsigned getMuxOpcode(const MachineOperand &Src1,
129 const MachineOperand &Src2) const;
130 bool genMuxInBlock(MachineBasicBlock &B);
131 };
132
133 char HexagonGenMux::ID = 0;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000134
135} // end anonymous namespace
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000136
137INITIALIZE_PASS(HexagonGenMux, "hexagon-mux",
138 "Hexagon generate mux instructions", false, false)
139
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000140void HexagonGenMux::getSubRegs(unsigned Reg, BitVector &SRs) const {
141 for (MCSubRegIterator I(Reg, HRI); I.isValid(); ++I)
142 SRs[*I] = true;
143}
144
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000145void HexagonGenMux::expandReg(unsigned Reg, BitVector &Set) const {
146 if (isRegPair(Reg))
147 getSubRegs(Reg, Set);
148 else
149 Set[Reg] = true;
150}
151
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000152void HexagonGenMux::getDefsUses(const MachineInstr *MI, BitVector &Defs,
153 BitVector &Uses) const {
154 // First, get the implicit defs and uses for this instruction.
155 unsigned Opc = MI->getOpcode();
156 const MCInstrDesc &D = HII->get(Opc);
Craig Toppere5e035a32015-12-05 07:13:35 +0000157 if (const MCPhysReg *R = D.ImplicitDefs)
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000158 while (*R)
159 expandReg(*R++, Defs);
Craig Toppere5e035a32015-12-05 07:13:35 +0000160 if (const MCPhysReg *R = D.ImplicitUses)
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000161 while (*R)
162 expandReg(*R++, Uses);
163
164 // Look over all operands, and collect explicit defs and uses.
Matthias Braunfc371552016-10-24 21:36:43 +0000165 for (const MachineOperand &MO : MI->operands()) {
166 if (!MO.isReg() || MO.isImplicit())
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000167 continue;
Matthias Braunfc371552016-10-24 21:36:43 +0000168 unsigned R = MO.getReg();
169 BitVector &Set = MO.isDef() ? Defs : Uses;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000170 expandReg(R, Set);
171 }
172}
173
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000174void HexagonGenMux::buildMaps(MachineBasicBlock &B, InstrIndexMap &I2X,
175 DefUseInfoMap &DUM) {
176 unsigned Index = 0;
177 unsigned NR = HRI->getNumRegs();
178 BitVector Defs(NR), Uses(NR);
179
180 for (MachineBasicBlock::iterator I = B.begin(), E = B.end(); I != E; ++I) {
181 MachineInstr *MI = &*I;
182 I2X.insert(std::make_pair(MI, Index));
183 Defs.reset();
184 Uses.reset();
185 getDefsUses(MI, Defs, Uses);
186 DUM.insert(std::make_pair(Index, DefUseInfo(Defs, Uses)));
187 Index++;
188 }
189}
190
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000191bool HexagonGenMux::isCondTransfer(unsigned Opc) const {
192 switch (Opc) {
193 case Hexagon::A2_tfrt:
194 case Hexagon::A2_tfrf:
195 case Hexagon::C2_cmoveit:
196 case Hexagon::C2_cmoveif:
197 return true;
198 }
199 return false;
200}
201
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000202unsigned HexagonGenMux::getMuxOpcode(const MachineOperand &Src1,
203 const MachineOperand &Src2) const {
204 bool IsReg1 = Src1.isReg(), IsReg2 = Src2.isReg();
205 if (IsReg1)
206 return IsReg2 ? Hexagon::C2_mux : Hexagon::C2_muxir;
207 if (IsReg2)
208 return Hexagon::C2_muxri;
209
210 // Neither is a register. The first source is extendable, but the second
211 // is not (s8).
212 if (Src2.isImm() && isInt<8>(Src2.getImm()))
213 return Hexagon::C2_muxii;
214
215 return 0;
216}
217
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000218bool HexagonGenMux::genMuxInBlock(MachineBasicBlock &B) {
219 bool Changed = false;
220 InstrIndexMap I2X;
221 DefUseInfoMap DUM;
222 buildMaps(B, I2X, DUM);
223
224 typedef DenseMap<unsigned,CondsetInfo> CondsetMap;
225 CondsetMap CM;
226 MuxInfoList ML;
227
228 MachineBasicBlock::iterator NextI, End = B.end();
229 for (MachineBasicBlock::iterator I = B.begin(); I != End; I = NextI) {
230 MachineInstr *MI = &*I;
231 NextI = std::next(I);
232 unsigned Opc = MI->getOpcode();
233 if (!isCondTransfer(Opc))
234 continue;
235 unsigned DR = MI->getOperand(0).getReg();
236 if (isRegPair(DR))
237 continue;
Krzysztof Parzyszek8a7fb0f2017-06-08 20:56:36 +0000238 MachineOperand &PredOp = MI->getOperand(1);
239 if (PredOp.isUndef())
240 continue;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000241
Krzysztof Parzyszek8a7fb0f2017-06-08 20:56:36 +0000242 unsigned PR = PredOp.getReg();
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000243 unsigned Idx = I2X.lookup(MI);
244 CondsetMap::iterator F = CM.find(DR);
245 bool IfTrue = HII->isPredicatedTrue(Opc);
246
247 // If there is no record of a conditional transfer for this register,
248 // or the predicate register differs, create a new record for it.
249 if (F != CM.end() && F->second.PredR != PR) {
250 CM.erase(F);
251 F = CM.end();
252 }
253 if (F == CM.end()) {
254 auto It = CM.insert(std::make_pair(DR, CondsetInfo()));
255 F = It.first;
256 F->second.PredR = PR;
257 }
258 CondsetInfo &CI = F->second;
259 if (IfTrue)
260 CI.TrueX = Idx;
261 else
262 CI.FalseX = Idx;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000263 if (CI.TrueX == std::numeric_limits<unsigned>::max() ||
264 CI.FalseX == std::numeric_limits<unsigned>::max())
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000265 continue;
266
267 // There is now a complete definition of DR, i.e. we have the predicate
268 // register, the definition if-true, and definition if-false.
269
270 // First, check if both definitions are far enough from the definition
271 // of the predicate register.
272 unsigned MinX = std::min(CI.TrueX, CI.FalseX);
273 unsigned MaxX = std::max(CI.TrueX, CI.FalseX);
274 unsigned SearchX = (MaxX > 4) ? MaxX-4 : 0;
275 bool NearDef = false;
276 for (unsigned X = SearchX; X < MaxX; ++X) {
277 const DefUseInfo &DU = DUM.lookup(X);
278 if (!DU.Defs[PR])
279 continue;
280 NearDef = true;
281 break;
282 }
283 if (NearDef)
284 continue;
285
286 // The predicate register is not defined in the last few instructions.
287 // Check if the conversion to MUX is possible (either "up", i.e. at the
288 // place of the earlier partial definition, or "down", where the later
289 // definition is located). Examine all defs and uses between these two
290 // definitions.
291 // SR1, SR2 - source registers from the first and the second definition.
292 MachineBasicBlock::iterator It1 = B.begin(), It2 = B.begin();
293 std::advance(It1, MinX);
294 std::advance(It2, MaxX);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000295 MachineInstr &Def1 = *It1, &Def2 = *It2;
296 MachineOperand *Src1 = &Def1.getOperand(2), *Src2 = &Def2.getOperand(2);
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000297 unsigned SR1 = Src1->isReg() ? Src1->getReg() : 0;
298 unsigned SR2 = Src2->isReg() ? Src2->getReg() : 0;
299 bool Failure = false, CanUp = true, CanDown = true;
300 for (unsigned X = MinX+1; X < MaxX; X++) {
301 const DefUseInfo &DU = DUM.lookup(X);
302 if (DU.Defs[PR] || DU.Defs[DR] || DU.Uses[DR]) {
303 Failure = true;
304 break;
305 }
306 if (CanDown && DU.Defs[SR1])
307 CanDown = false;
308 if (CanUp && DU.Defs[SR2])
309 CanUp = false;
310 }
311 if (Failure || (!CanUp && !CanDown))
312 continue;
313
314 MachineOperand *SrcT = (MinX == CI.TrueX) ? Src1 : Src2;
315 MachineOperand *SrcF = (MinX == CI.FalseX) ? Src1 : Src2;
316 // Prefer "down", since this will move the MUX farther away from the
317 // predicate definition.
318 MachineBasicBlock::iterator At = CanDown ? Def2 : Def1;
319 ML.push_back(MuxInfo(At, DR, PR, SrcT, SrcF, Def1, Def2));
320 }
321
322 for (unsigned I = 0, N = ML.size(); I < N; ++I) {
323 MuxInfo &MX = ML[I];
324 MachineBasicBlock &B = *MX.At->getParent();
325 DebugLoc DL = MX.At->getDebugLoc();
326 unsigned MxOpc = getMuxOpcode(*MX.SrcT, *MX.SrcF);
327 if (!MxOpc)
328 continue;
329 BuildMI(B, MX.At, DL, HII->get(MxOpc), MX.DefR)
Diana Picus116bbab2017-01-13 09:58:52 +0000330 .addReg(MX.PredR)
331 .add(*MX.SrcT)
332 .add(*MX.SrcF);
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000333 B.erase(MX.Def1);
334 B.erase(MX.Def2);
335 Changed = true;
336 }
337
338 return Changed;
339}
340
341bool HexagonGenMux::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000342 if (skipFunction(*MF.getFunction()))
343 return false;
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000344 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
345 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
346 bool Changed = false;
347 for (auto &I : MF)
348 Changed |= genMuxInBlock(I);
349 return Changed;
350}
351
352FunctionPass *llvm::createHexagonGenMux() {
353 return new HexagonGenMux();
354}