blob: 079b7d4851f2452d467c81c0b32e4abdbe6e3d28 [file] [log] [blame]
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001//===--- HexagonBitSimplify.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#define DEBUG_TYPE "hexbit"
11
Mehdi Aminib550cb12016-04-18 09:17:29 +000012#include "HexagonBitTracker.h"
13#include "HexagonTargetMachine.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000014#include "llvm/ADT/BitVector.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/CodeGen/MachineBasicBlock.h"
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000020#include "llvm/CodeGen/MachineDominators.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000021#include "llvm/CodeGen/MachineFunction.h"
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000023#include "llvm/CodeGen/MachineInstr.h"
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000025#include "llvm/CodeGen/MachineOperand.h"
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000027#include "llvm/CodeGen/Passes.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000028#include "llvm/IR/DebugLoc.h"
29#include "llvm/MC/MCInstrDesc.h"
30#include "llvm/Pass.h"
Krzysztof Parzyszekd391d6f2016-10-06 16:18:04 +000031#include "llvm/Support/CommandLine.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000032#include "llvm/Support/Compiler.h"
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000033#include "llvm/Support/Debug.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000034#include "llvm/Support/MathExtras.h"
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000035#include "llvm/Support/raw_ostream.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000036#include "llvm/Target/TargetRegisterInfo.h"
37#include <algorithm>
38#include <cassert>
39#include <cstdint>
40#include <iterator>
41#include <limits>
42#include <utility>
43#include <vector>
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000044
45using namespace llvm;
46
Krzysztof Parzyszekd391d6f2016-10-06 16:18:04 +000047static cl::opt<bool> PreserveTiedOps("hexbit-keep-tied", cl::Hidden,
48 cl::init(true), cl::desc("Preserve subregisters in tied operands"));
Krzysztof Parzyszek33fd0bb2017-02-28 23:27:33 +000049static cl::opt<bool> GenExtract("hexbit-extract", cl::Hidden,
50 cl::init(true), cl::desc("Generate extract instructions"));
Krzysztof Parzyszek8e4d2e02017-03-07 23:08:35 +000051static cl::opt<bool> GenBitSplit("hexbit-bitsplit", cl::Hidden,
52 cl::init(true), cl::desc("Generate bitsplit instructions"));
Krzysztof Parzyszek33fd0bb2017-02-28 23:27:33 +000053
54static cl::opt<unsigned> MaxExtract("hexbit-max-extract", cl::Hidden,
55 cl::init(UINT_MAX));
56static unsigned CountExtract = 0;
Krzysztof Parzyszek8e4d2e02017-03-07 23:08:35 +000057static cl::opt<unsigned> MaxBitSplit("hexbit-max-bitsplit", cl::Hidden,
58 cl::init(UINT_MAX));
59static unsigned CountBitSplit = 0;
Krzysztof Parzyszekd391d6f2016-10-06 16:18:04 +000060
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000061namespace llvm {
Eugene Zelenko82085922016-12-13 22:13:50 +000062
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000063 void initializeHexagonBitSimplifyPass(PassRegistry& Registry);
64 FunctionPass *createHexagonBitSimplify();
Eugene Zelenko82085922016-12-13 22:13:50 +000065
66} // end namespace llvm
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000067
68namespace {
Eugene Zelenko82085922016-12-13 22:13:50 +000069
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000070 // Set of virtual registers, based on BitVector.
71 struct RegisterSet : private BitVector {
Eugene Zelenko82085922016-12-13 22:13:50 +000072 RegisterSet() = default;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000073 explicit RegisterSet(unsigned s, bool t = false) : BitVector(s, t) {}
Eugene Zelenko82085922016-12-13 22:13:50 +000074 RegisterSet(const RegisterSet &RS) = default;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000075
76 using BitVector::clear;
77 using BitVector::count;
78
79 unsigned find_first() const {
80 int First = BitVector::find_first();
81 if (First < 0)
82 return 0;
83 return x2v(First);
84 }
85
86 unsigned find_next(unsigned Prev) const {
87 int Next = BitVector::find_next(v2x(Prev));
88 if (Next < 0)
89 return 0;
90 return x2v(Next);
91 }
92
93 RegisterSet &insert(unsigned R) {
94 unsigned Idx = v2x(R);
95 ensure(Idx);
96 return static_cast<RegisterSet&>(BitVector::set(Idx));
97 }
98 RegisterSet &remove(unsigned R) {
99 unsigned Idx = v2x(R);
100 if (Idx >= size())
101 return *this;
102 return static_cast<RegisterSet&>(BitVector::reset(Idx));
103 }
104
105 RegisterSet &insert(const RegisterSet &Rs) {
106 return static_cast<RegisterSet&>(BitVector::operator|=(Rs));
107 }
108 RegisterSet &remove(const RegisterSet &Rs) {
109 return static_cast<RegisterSet&>(BitVector::reset(Rs));
110 }
111
112 reference operator[](unsigned R) {
113 unsigned Idx = v2x(R);
114 ensure(Idx);
115 return BitVector::operator[](Idx);
116 }
117 bool operator[](unsigned R) const {
118 unsigned Idx = v2x(R);
119 assert(Idx < size());
120 return BitVector::operator[](Idx);
121 }
122 bool has(unsigned R) const {
123 unsigned Idx = v2x(R);
124 if (Idx >= size())
125 return false;
126 return BitVector::test(Idx);
127 }
128
129 bool empty() const {
130 return !BitVector::any();
131 }
132 bool includes(const RegisterSet &Rs) const {
133 // A.BitVector::test(B) <=> A-B != {}
134 return !Rs.BitVector::test(*this);
135 }
136 bool intersects(const RegisterSet &Rs) const {
137 return BitVector::anyCommon(Rs);
138 }
139
140 private:
141 void ensure(unsigned Idx) {
142 if (size() <= Idx)
143 resize(std::max(Idx+1, 32U));
144 }
Eugene Zelenko82085922016-12-13 22:13:50 +0000145
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000146 static inline unsigned v2x(unsigned v) {
147 return TargetRegisterInfo::virtReg2Index(v);
148 }
Eugene Zelenko82085922016-12-13 22:13:50 +0000149
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000150 static inline unsigned x2v(unsigned x) {
151 return TargetRegisterInfo::index2VirtReg(x);
152 }
153 };
154
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000155 struct PrintRegSet {
156 PrintRegSet(const RegisterSet &S, const TargetRegisterInfo *RI)
157 : RS(S), TRI(RI) {}
Eugene Zelenko82085922016-12-13 22:13:50 +0000158
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000159 friend raw_ostream &operator<< (raw_ostream &OS,
160 const PrintRegSet &P);
Eugene Zelenko82085922016-12-13 22:13:50 +0000161
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000162 private:
163 const RegisterSet &RS;
164 const TargetRegisterInfo *TRI;
165 };
166
167 raw_ostream &operator<< (raw_ostream &OS, const PrintRegSet &P)
168 LLVM_ATTRIBUTE_UNUSED;
169 raw_ostream &operator<< (raw_ostream &OS, const PrintRegSet &P) {
170 OS << '{';
171 for (unsigned R = P.RS.find_first(); R; R = P.RS.find_next(R))
172 OS << ' ' << PrintReg(R, P.TRI);
173 OS << " }";
174 return OS;
175 }
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000176
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000177 class Transformation;
178
179 class HexagonBitSimplify : public MachineFunctionPass {
180 public:
181 static char ID;
Eugene Zelenko82085922016-12-13 22:13:50 +0000182
183 HexagonBitSimplify() : MachineFunctionPass(ID), MDT(nullptr) {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000184 initializeHexagonBitSimplifyPass(*PassRegistry::getPassRegistry());
185 }
Eugene Zelenko82085922016-12-13 22:13:50 +0000186
187 StringRef getPassName() const override {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000188 return "Hexagon bit simplification";
189 }
Eugene Zelenko82085922016-12-13 22:13:50 +0000190
191 void getAnalysisUsage(AnalysisUsage &AU) const override {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000192 AU.addRequired<MachineDominatorTree>();
193 AU.addPreserved<MachineDominatorTree>();
194 MachineFunctionPass::getAnalysisUsage(AU);
195 }
Eugene Zelenko82085922016-12-13 22:13:50 +0000196
197 bool runOnMachineFunction(MachineFunction &MF) override;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000198
199 static void getInstrDefs(const MachineInstr &MI, RegisterSet &Defs);
200 static void getInstrUses(const MachineInstr &MI, RegisterSet &Uses);
201 static bool isEqual(const BitTracker::RegisterCell &RC1, uint16_t B1,
202 const BitTracker::RegisterCell &RC2, uint16_t B2, uint16_t W);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000203 static bool isZero(const BitTracker::RegisterCell &RC, uint16_t B,
204 uint16_t W);
205 static bool getConst(const BitTracker::RegisterCell &RC, uint16_t B,
206 uint16_t W, uint64_t &U);
207 static bool replaceReg(unsigned OldR, unsigned NewR,
208 MachineRegisterInfo &MRI);
209 static bool getSubregMask(const BitTracker::RegisterRef &RR,
210 unsigned &Begin, unsigned &Width, MachineRegisterInfo &MRI);
211 static bool replaceRegWithSub(unsigned OldR, unsigned NewR,
212 unsigned NewSR, MachineRegisterInfo &MRI);
213 static bool replaceSubWithSub(unsigned OldR, unsigned OldSR,
214 unsigned NewR, unsigned NewSR, MachineRegisterInfo &MRI);
215 static bool parseRegSequence(const MachineInstr &I,
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000216 BitTracker::RegisterRef &SL, BitTracker::RegisterRef &SH,
217 const MachineRegisterInfo &MRI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000218
219 static bool getUsedBitsInStore(unsigned Opc, BitVector &Bits,
220 uint16_t Begin);
221 static bool getUsedBits(unsigned Opc, unsigned OpN, BitVector &Bits,
222 uint16_t Begin, const HexagonInstrInfo &HII);
223
224 static const TargetRegisterClass *getFinalVRegClass(
225 const BitTracker::RegisterRef &RR, MachineRegisterInfo &MRI);
226 static bool isTransparentCopy(const BitTracker::RegisterRef &RD,
227 const BitTracker::RegisterRef &RS, MachineRegisterInfo &MRI);
228
229 private:
230 MachineDominatorTree *MDT;
231
232 bool visitBlock(MachineBasicBlock &B, Transformation &T, RegisterSet &AVs);
Krzysztof Parzyszekd391d6f2016-10-06 16:18:04 +0000233 static bool hasTiedUse(unsigned Reg, MachineRegisterInfo &MRI,
234 unsigned NewSub = Hexagon::NoSubRegister);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000235 };
236
237 char HexagonBitSimplify::ID = 0;
238 typedef HexagonBitSimplify HBS;
239
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000240 // The purpose of this class is to provide a common facility to traverse
241 // the function top-down or bottom-up via the dominator tree, and keep
242 // track of the available registers.
243 class Transformation {
244 public:
245 bool TopDown;
Eugene Zelenko82085922016-12-13 22:13:50 +0000246
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000247 Transformation(bool TD) : TopDown(TD) {}
Eugene Zelenko82085922016-12-13 22:13:50 +0000248 virtual ~Transformation() = default;
249
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000250 virtual bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) = 0;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000251 };
Eugene Zelenko82085922016-12-13 22:13:50 +0000252
253} // end anonymous namespace
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000254
255INITIALIZE_PASS_BEGIN(HexagonBitSimplify, "hexbit",
256 "Hexagon bit simplification", false, false)
257INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
258INITIALIZE_PASS_END(HexagonBitSimplify, "hexbit",
259 "Hexagon bit simplification", false, false)
260
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000261bool HexagonBitSimplify::visitBlock(MachineBasicBlock &B, Transformation &T,
262 RegisterSet &AVs) {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000263 bool Changed = false;
264
265 if (T.TopDown)
266 Changed = T.processBlock(B, AVs);
267
268 RegisterSet Defs;
269 for (auto &I : B)
270 getInstrDefs(I, Defs);
271 RegisterSet NewAVs = AVs;
272 NewAVs.insert(Defs);
273
Daniel Berlin73ad5cb2017-02-09 20:37:46 +0000274 for (auto *DTN : children<MachineDomTreeNode*>(MDT->getNode(&B)))
Daniel Berlin58a6e572017-02-09 20:37:24 +0000275 Changed |= visitBlock(*(DTN->getBlock()), T, NewAVs);
276
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000277 if (!T.TopDown)
278 Changed |= T.processBlock(B, AVs);
279
280 return Changed;
281}
282
283//
284// Utility functions:
285//
286void HexagonBitSimplify::getInstrDefs(const MachineInstr &MI,
287 RegisterSet &Defs) {
288 for (auto &Op : MI.operands()) {
289 if (!Op.isReg() || !Op.isDef())
290 continue;
291 unsigned R = Op.getReg();
292 if (!TargetRegisterInfo::isVirtualRegister(R))
293 continue;
294 Defs.insert(R);
295 }
296}
297
298void HexagonBitSimplify::getInstrUses(const MachineInstr &MI,
299 RegisterSet &Uses) {
300 for (auto &Op : MI.operands()) {
301 if (!Op.isReg() || !Op.isUse())
302 continue;
303 unsigned R = Op.getReg();
304 if (!TargetRegisterInfo::isVirtualRegister(R))
305 continue;
306 Uses.insert(R);
307 }
308}
309
310// Check if all the bits in range [B, E) in both cells are equal.
311bool HexagonBitSimplify::isEqual(const BitTracker::RegisterCell &RC1,
312 uint16_t B1, const BitTracker::RegisterCell &RC2, uint16_t B2,
313 uint16_t W) {
314 for (uint16_t i = 0; i < W; ++i) {
315 // If RC1[i] is "bottom", it cannot be proven equal to RC2[i].
316 if (RC1[B1+i].Type == BitTracker::BitValue::Ref && RC1[B1+i].RefI.Reg == 0)
317 return false;
318 // Same for RC2[i].
319 if (RC2[B2+i].Type == BitTracker::BitValue::Ref && RC2[B2+i].RefI.Reg == 0)
320 return false;
321 if (RC1[B1+i] != RC2[B2+i])
322 return false;
323 }
324 return true;
325}
326
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000327bool HexagonBitSimplify::isZero(const BitTracker::RegisterCell &RC,
328 uint16_t B, uint16_t W) {
329 assert(B < RC.width() && B+W <= RC.width());
330 for (uint16_t i = B; i < B+W; ++i)
331 if (!RC[i].is(0))
332 return false;
333 return true;
334}
335
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000336bool HexagonBitSimplify::getConst(const BitTracker::RegisterCell &RC,
337 uint16_t B, uint16_t W, uint64_t &U) {
338 assert(B < RC.width() && B+W <= RC.width());
339 int64_t T = 0;
340 for (uint16_t i = B+W; i > B; --i) {
341 const BitTracker::BitValue &BV = RC[i-1];
342 T <<= 1;
343 if (BV.is(1))
344 T |= 1;
345 else if (!BV.is(0))
346 return false;
347 }
348 U = T;
349 return true;
350}
351
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000352bool HexagonBitSimplify::replaceReg(unsigned OldR, unsigned NewR,
353 MachineRegisterInfo &MRI) {
354 if (!TargetRegisterInfo::isVirtualRegister(OldR) ||
355 !TargetRegisterInfo::isVirtualRegister(NewR))
356 return false;
357 auto Begin = MRI.use_begin(OldR), End = MRI.use_end();
358 decltype(End) NextI;
359 for (auto I = Begin; I != End; I = NextI) {
360 NextI = std::next(I);
361 I->setReg(NewR);
362 }
363 return Begin != End;
364}
365
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000366bool HexagonBitSimplify::replaceRegWithSub(unsigned OldR, unsigned NewR,
367 unsigned NewSR, MachineRegisterInfo &MRI) {
368 if (!TargetRegisterInfo::isVirtualRegister(OldR) ||
369 !TargetRegisterInfo::isVirtualRegister(NewR))
370 return false;
Krzysztof Parzyszekd391d6f2016-10-06 16:18:04 +0000371 if (hasTiedUse(OldR, MRI, NewSR))
372 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000373 auto Begin = MRI.use_begin(OldR), End = MRI.use_end();
374 decltype(End) NextI;
375 for (auto I = Begin; I != End; I = NextI) {
376 NextI = std::next(I);
377 I->setReg(NewR);
378 I->setSubReg(NewSR);
379 }
380 return Begin != End;
381}
382
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000383bool HexagonBitSimplify::replaceSubWithSub(unsigned OldR, unsigned OldSR,
384 unsigned NewR, unsigned NewSR, MachineRegisterInfo &MRI) {
385 if (!TargetRegisterInfo::isVirtualRegister(OldR) ||
386 !TargetRegisterInfo::isVirtualRegister(NewR))
387 return false;
Krzysztof Parzyszekd391d6f2016-10-06 16:18:04 +0000388 if (OldSR != NewSR && hasTiedUse(OldR, MRI, NewSR))
389 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000390 auto Begin = MRI.use_begin(OldR), End = MRI.use_end();
391 decltype(End) NextI;
392 for (auto I = Begin; I != End; I = NextI) {
393 NextI = std::next(I);
394 if (I->getSubReg() != OldSR)
395 continue;
396 I->setReg(NewR);
397 I->setSubReg(NewSR);
398 }
399 return Begin != End;
400}
401
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000402// For a register ref (pair Reg:Sub), set Begin to the position of the LSB
403// of Sub in Reg, and set Width to the size of Sub in bits. Return true,
404// if this succeeded, otherwise return false.
405bool HexagonBitSimplify::getSubregMask(const BitTracker::RegisterRef &RR,
406 unsigned &Begin, unsigned &Width, MachineRegisterInfo &MRI) {
407 const TargetRegisterClass *RC = MRI.getRegClass(RR.Reg);
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +0000408 if (RR.Sub == 0) {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000409 Begin = 0;
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000410 Width = MRI.getTargetRegisterInfo()->getRegSizeInBits(*RC);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000411 return true;
412 }
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +0000413
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000414 Begin = 0;
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +0000415
416 switch (RC->getID()) {
417 case Hexagon::DoubleRegsRegClassID:
418 case Hexagon::VecDblRegsRegClassID:
419 case Hexagon::VecDblRegs128BRegClassID:
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000420 Width = MRI.getTargetRegisterInfo()->getRegSizeInBits(*RC) / 2;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000421 if (RR.Sub == Hexagon::isub_hi || RR.Sub == Hexagon::vsub_hi)
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +0000422 Begin = Width;
423 break;
424 default:
425 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000426 }
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +0000427 return true;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000428}
429
430
431// For a REG_SEQUENCE, set SL to the low subregister and SH to the high
432// subregister.
433bool HexagonBitSimplify::parseRegSequence(const MachineInstr &I,
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000434 BitTracker::RegisterRef &SL, BitTracker::RegisterRef &SH,
435 const MachineRegisterInfo &MRI) {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000436 assert(I.getOpcode() == TargetOpcode::REG_SEQUENCE);
437 unsigned Sub1 = I.getOperand(2).getImm(), Sub2 = I.getOperand(4).getImm();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000438 auto *DstRC = MRI.getRegClass(I.getOperand(0).getReg());
439 auto &HRI = static_cast<const HexagonRegisterInfo&>(
440 *MRI.getTargetRegisterInfo());
441 unsigned SubLo = HRI.getHexagonSubRegIndex(DstRC, Hexagon::ps_sub_lo);
442 unsigned SubHi = HRI.getHexagonSubRegIndex(DstRC, Hexagon::ps_sub_hi);
443 assert((Sub1 == SubLo && Sub2 == SubHi) || (Sub1 == SubHi && Sub2 == SubLo));
444 if (Sub1 == SubLo && Sub2 == SubHi) {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000445 SL = I.getOperand(1);
446 SH = I.getOperand(3);
447 return true;
448 }
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000449 if (Sub1 == SubHi && Sub2 == SubLo) {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000450 SH = I.getOperand(1);
451 SL = I.getOperand(3);
452 return true;
453 }
454 return false;
455}
456
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000457// All stores (except 64-bit stores) take a 32-bit register as the source
458// of the value to be stored. If the instruction stores into a location
459// that is shorter than 32 bits, some bits of the source register are not
460// used. For each store instruction, calculate the set of used bits in
461// the source register, and set appropriate bits in Bits. Return true if
462// the bits are calculated, false otherwise.
463bool HexagonBitSimplify::getUsedBitsInStore(unsigned Opc, BitVector &Bits,
464 uint16_t Begin) {
465 using namespace Hexagon;
466
467 switch (Opc) {
468 // Store byte
469 case S2_storerb_io: // memb(Rs32+#s11:0)=Rt32
470 case S2_storerbnew_io: // memb(Rs32+#s11:0)=Nt8.new
471 case S2_pstorerbt_io: // if (Pv4) memb(Rs32+#u6:0)=Rt32
472 case S2_pstorerbf_io: // if (!Pv4) memb(Rs32+#u6:0)=Rt32
473 case S4_pstorerbtnew_io: // if (Pv4.new) memb(Rs32+#u6:0)=Rt32
474 case S4_pstorerbfnew_io: // if (!Pv4.new) memb(Rs32+#u6:0)=Rt32
475 case S2_pstorerbnewt_io: // if (Pv4) memb(Rs32+#u6:0)=Nt8.new
476 case S2_pstorerbnewf_io: // if (!Pv4) memb(Rs32+#u6:0)=Nt8.new
477 case S4_pstorerbnewtnew_io: // if (Pv4.new) memb(Rs32+#u6:0)=Nt8.new
478 case S4_pstorerbnewfnew_io: // if (!Pv4.new) memb(Rs32+#u6:0)=Nt8.new
479 case S2_storerb_pi: // memb(Rx32++#s4:0)=Rt32
480 case S2_storerbnew_pi: // memb(Rx32++#s4:0)=Nt8.new
481 case S2_pstorerbt_pi: // if (Pv4) memb(Rx32++#s4:0)=Rt32
482 case S2_pstorerbf_pi: // if (!Pv4) memb(Rx32++#s4:0)=Rt32
483 case S2_pstorerbtnew_pi: // if (Pv4.new) memb(Rx32++#s4:0)=Rt32
484 case S2_pstorerbfnew_pi: // if (!Pv4.new) memb(Rx32++#s4:0)=Rt32
485 case S2_pstorerbnewt_pi: // if (Pv4) memb(Rx32++#s4:0)=Nt8.new
486 case S2_pstorerbnewf_pi: // if (!Pv4) memb(Rx32++#s4:0)=Nt8.new
487 case S2_pstorerbnewtnew_pi: // if (Pv4.new) memb(Rx32++#s4:0)=Nt8.new
488 case S2_pstorerbnewfnew_pi: // if (!Pv4.new) memb(Rx32++#s4:0)=Nt8.new
489 case S4_storerb_ap: // memb(Re32=#U6)=Rt32
490 case S4_storerbnew_ap: // memb(Re32=#U6)=Nt8.new
491 case S2_storerb_pr: // memb(Rx32++Mu2)=Rt32
492 case S2_storerbnew_pr: // memb(Rx32++Mu2)=Nt8.new
493 case S4_storerb_ur: // memb(Ru32<<#u2+#U6)=Rt32
494 case S4_storerbnew_ur: // memb(Ru32<<#u2+#U6)=Nt8.new
495 case S2_storerb_pbr: // memb(Rx32++Mu2:brev)=Rt32
496 case S2_storerbnew_pbr: // memb(Rx32++Mu2:brev)=Nt8.new
497 case S2_storerb_pci: // memb(Rx32++#s4:0:circ(Mu2))=Rt32
498 case S2_storerbnew_pci: // memb(Rx32++#s4:0:circ(Mu2))=Nt8.new
499 case S2_storerb_pcr: // memb(Rx32++I:circ(Mu2))=Rt32
500 case S2_storerbnew_pcr: // memb(Rx32++I:circ(Mu2))=Nt8.new
501 case S4_storerb_rr: // memb(Rs32+Ru32<<#u2)=Rt32
502 case S4_storerbnew_rr: // memb(Rs32+Ru32<<#u2)=Nt8.new
503 case S4_pstorerbt_rr: // if (Pv4) memb(Rs32+Ru32<<#u2)=Rt32
504 case S4_pstorerbf_rr: // if (!Pv4) memb(Rs32+Ru32<<#u2)=Rt32
505 case S4_pstorerbtnew_rr: // if (Pv4.new) memb(Rs32+Ru32<<#u2)=Rt32
506 case S4_pstorerbfnew_rr: // if (!Pv4.new) memb(Rs32+Ru32<<#u2)=Rt32
507 case S4_pstorerbnewt_rr: // if (Pv4) memb(Rs32+Ru32<<#u2)=Nt8.new
508 case S4_pstorerbnewf_rr: // if (!Pv4) memb(Rs32+Ru32<<#u2)=Nt8.new
509 case S4_pstorerbnewtnew_rr: // if (Pv4.new) memb(Rs32+Ru32<<#u2)=Nt8.new
510 case S4_pstorerbnewfnew_rr: // if (!Pv4.new) memb(Rs32+Ru32<<#u2)=Nt8.new
511 case S2_storerbgp: // memb(gp+#u16:0)=Rt32
512 case S2_storerbnewgp: // memb(gp+#u16:0)=Nt8.new
513 case S4_pstorerbt_abs: // if (Pv4) memb(#u6)=Rt32
514 case S4_pstorerbf_abs: // if (!Pv4) memb(#u6)=Rt32
515 case S4_pstorerbtnew_abs: // if (Pv4.new) memb(#u6)=Rt32
516 case S4_pstorerbfnew_abs: // if (!Pv4.new) memb(#u6)=Rt32
517 case S4_pstorerbnewt_abs: // if (Pv4) memb(#u6)=Nt8.new
518 case S4_pstorerbnewf_abs: // if (!Pv4) memb(#u6)=Nt8.new
519 case S4_pstorerbnewtnew_abs: // if (Pv4.new) memb(#u6)=Nt8.new
520 case S4_pstorerbnewfnew_abs: // if (!Pv4.new) memb(#u6)=Nt8.new
521 Bits.set(Begin, Begin+8);
522 return true;
523
524 // Store low half
525 case S2_storerh_io: // memh(Rs32+#s11:1)=Rt32
526 case S2_storerhnew_io: // memh(Rs32+#s11:1)=Nt8.new
527 case S2_pstorerht_io: // if (Pv4) memh(Rs32+#u6:1)=Rt32
528 case S2_pstorerhf_io: // if (!Pv4) memh(Rs32+#u6:1)=Rt32
529 case S4_pstorerhtnew_io: // if (Pv4.new) memh(Rs32+#u6:1)=Rt32
530 case S4_pstorerhfnew_io: // if (!Pv4.new) memh(Rs32+#u6:1)=Rt32
531 case S2_pstorerhnewt_io: // if (Pv4) memh(Rs32+#u6:1)=Nt8.new
532 case S2_pstorerhnewf_io: // if (!Pv4) memh(Rs32+#u6:1)=Nt8.new
533 case S4_pstorerhnewtnew_io: // if (Pv4.new) memh(Rs32+#u6:1)=Nt8.new
534 case S4_pstorerhnewfnew_io: // if (!Pv4.new) memh(Rs32+#u6:1)=Nt8.new
535 case S2_storerh_pi: // memh(Rx32++#s4:1)=Rt32
536 case S2_storerhnew_pi: // memh(Rx32++#s4:1)=Nt8.new
537 case S2_pstorerht_pi: // if (Pv4) memh(Rx32++#s4:1)=Rt32
538 case S2_pstorerhf_pi: // if (!Pv4) memh(Rx32++#s4:1)=Rt32
539 case S2_pstorerhtnew_pi: // if (Pv4.new) memh(Rx32++#s4:1)=Rt32
540 case S2_pstorerhfnew_pi: // if (!Pv4.new) memh(Rx32++#s4:1)=Rt32
541 case S2_pstorerhnewt_pi: // if (Pv4) memh(Rx32++#s4:1)=Nt8.new
542 case S2_pstorerhnewf_pi: // if (!Pv4) memh(Rx32++#s4:1)=Nt8.new
543 case S2_pstorerhnewtnew_pi: // if (Pv4.new) memh(Rx32++#s4:1)=Nt8.new
544 case S2_pstorerhnewfnew_pi: // if (!Pv4.new) memh(Rx32++#s4:1)=Nt8.new
545 case S4_storerh_ap: // memh(Re32=#U6)=Rt32
546 case S4_storerhnew_ap: // memh(Re32=#U6)=Nt8.new
547 case S2_storerh_pr: // memh(Rx32++Mu2)=Rt32
548 case S2_storerhnew_pr: // memh(Rx32++Mu2)=Nt8.new
549 case S4_storerh_ur: // memh(Ru32<<#u2+#U6)=Rt32
550 case S4_storerhnew_ur: // memh(Ru32<<#u2+#U6)=Nt8.new
551 case S2_storerh_pbr: // memh(Rx32++Mu2:brev)=Rt32
552 case S2_storerhnew_pbr: // memh(Rx32++Mu2:brev)=Nt8.new
553 case S2_storerh_pci: // memh(Rx32++#s4:1:circ(Mu2))=Rt32
554 case S2_storerhnew_pci: // memh(Rx32++#s4:1:circ(Mu2))=Nt8.new
555 case S2_storerh_pcr: // memh(Rx32++I:circ(Mu2))=Rt32
556 case S2_storerhnew_pcr: // memh(Rx32++I:circ(Mu2))=Nt8.new
557 case S4_storerh_rr: // memh(Rs32+Ru32<<#u2)=Rt32
558 case S4_pstorerht_rr: // if (Pv4) memh(Rs32+Ru32<<#u2)=Rt32
559 case S4_pstorerhf_rr: // if (!Pv4) memh(Rs32+Ru32<<#u2)=Rt32
560 case S4_pstorerhtnew_rr: // if (Pv4.new) memh(Rs32+Ru32<<#u2)=Rt32
561 case S4_pstorerhfnew_rr: // if (!Pv4.new) memh(Rs32+Ru32<<#u2)=Rt32
562 case S4_storerhnew_rr: // memh(Rs32+Ru32<<#u2)=Nt8.new
563 case S4_pstorerhnewt_rr: // if (Pv4) memh(Rs32+Ru32<<#u2)=Nt8.new
564 case S4_pstorerhnewf_rr: // if (!Pv4) memh(Rs32+Ru32<<#u2)=Nt8.new
565 case S4_pstorerhnewtnew_rr: // if (Pv4.new) memh(Rs32+Ru32<<#u2)=Nt8.new
566 case S4_pstorerhnewfnew_rr: // if (!Pv4.new) memh(Rs32+Ru32<<#u2)=Nt8.new
567 case S2_storerhgp: // memh(gp+#u16:1)=Rt32
568 case S2_storerhnewgp: // memh(gp+#u16:1)=Nt8.new
569 case S4_pstorerht_abs: // if (Pv4) memh(#u6)=Rt32
570 case S4_pstorerhf_abs: // if (!Pv4) memh(#u6)=Rt32
571 case S4_pstorerhtnew_abs: // if (Pv4.new) memh(#u6)=Rt32
572 case S4_pstorerhfnew_abs: // if (!Pv4.new) memh(#u6)=Rt32
573 case S4_pstorerhnewt_abs: // if (Pv4) memh(#u6)=Nt8.new
574 case S4_pstorerhnewf_abs: // if (!Pv4) memh(#u6)=Nt8.new
575 case S4_pstorerhnewtnew_abs: // if (Pv4.new) memh(#u6)=Nt8.new
576 case S4_pstorerhnewfnew_abs: // if (!Pv4.new) memh(#u6)=Nt8.new
577 Bits.set(Begin, Begin+16);
578 return true;
579
580 // Store high half
581 case S2_storerf_io: // memh(Rs32+#s11:1)=Rt.H32
582 case S2_pstorerft_io: // if (Pv4) memh(Rs32+#u6:1)=Rt.H32
583 case S2_pstorerff_io: // if (!Pv4) memh(Rs32+#u6:1)=Rt.H32
584 case S4_pstorerftnew_io: // if (Pv4.new) memh(Rs32+#u6:1)=Rt.H32
585 case S4_pstorerffnew_io: // if (!Pv4.new) memh(Rs32+#u6:1)=Rt.H32
586 case S2_storerf_pi: // memh(Rx32++#s4:1)=Rt.H32
587 case S2_pstorerft_pi: // if (Pv4) memh(Rx32++#s4:1)=Rt.H32
588 case S2_pstorerff_pi: // if (!Pv4) memh(Rx32++#s4:1)=Rt.H32
589 case S2_pstorerftnew_pi: // if (Pv4.new) memh(Rx32++#s4:1)=Rt.H32
590 case S2_pstorerffnew_pi: // if (!Pv4.new) memh(Rx32++#s4:1)=Rt.H32
591 case S4_storerf_ap: // memh(Re32=#U6)=Rt.H32
592 case S2_storerf_pr: // memh(Rx32++Mu2)=Rt.H32
593 case S4_storerf_ur: // memh(Ru32<<#u2+#U6)=Rt.H32
594 case S2_storerf_pbr: // memh(Rx32++Mu2:brev)=Rt.H32
595 case S2_storerf_pci: // memh(Rx32++#s4:1:circ(Mu2))=Rt.H32
596 case S2_storerf_pcr: // memh(Rx32++I:circ(Mu2))=Rt.H32
597 case S4_storerf_rr: // memh(Rs32+Ru32<<#u2)=Rt.H32
598 case S4_pstorerft_rr: // if (Pv4) memh(Rs32+Ru32<<#u2)=Rt.H32
599 case S4_pstorerff_rr: // if (!Pv4) memh(Rs32+Ru32<<#u2)=Rt.H32
600 case S4_pstorerftnew_rr: // if (Pv4.new) memh(Rs32+Ru32<<#u2)=Rt.H32
601 case S4_pstorerffnew_rr: // if (!Pv4.new) memh(Rs32+Ru32<<#u2)=Rt.H32
602 case S2_storerfgp: // memh(gp+#u16:1)=Rt.H32
603 case S4_pstorerft_abs: // if (Pv4) memh(#u6)=Rt.H32
604 case S4_pstorerff_abs: // if (!Pv4) memh(#u6)=Rt.H32
605 case S4_pstorerftnew_abs: // if (Pv4.new) memh(#u6)=Rt.H32
606 case S4_pstorerffnew_abs: // if (!Pv4.new) memh(#u6)=Rt.H32
607 Bits.set(Begin+16, Begin+32);
608 return true;
609 }
610
611 return false;
612}
613
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000614// For an instruction with opcode Opc, calculate the set of bits that it
615// uses in a register in operand OpN. This only calculates the set of used
616// bits for cases where it does not depend on any operands (as is the case
617// in shifts, for example). For concrete instructions from a program, the
618// operand may be a subregister of a larger register, while Bits would
619// correspond to the larger register in its entirety. Because of that,
620// the parameter Begin can be used to indicate which bit of Bits should be
621// considered the LSB of of the operand.
622bool HexagonBitSimplify::getUsedBits(unsigned Opc, unsigned OpN,
623 BitVector &Bits, uint16_t Begin, const HexagonInstrInfo &HII) {
624 using namespace Hexagon;
625
626 const MCInstrDesc &D = HII.get(Opc);
627 if (D.mayStore()) {
628 if (OpN == D.getNumOperands()-1)
629 return getUsedBitsInStore(Opc, Bits, Begin);
630 return false;
631 }
632
633 switch (Opc) {
634 // One register source. Used bits: R1[0-7].
635 case A2_sxtb:
636 case A2_zxtb:
637 case A4_cmpbeqi:
638 case A4_cmpbgti:
639 case A4_cmpbgtui:
640 if (OpN == 1) {
641 Bits.set(Begin, Begin+8);
642 return true;
643 }
644 break;
645
646 // One register source. Used bits: R1[0-15].
647 case A2_aslh:
648 case A2_sxth:
649 case A2_zxth:
650 case A4_cmpheqi:
651 case A4_cmphgti:
652 case A4_cmphgtui:
653 if (OpN == 1) {
654 Bits.set(Begin, Begin+16);
655 return true;
656 }
657 break;
658
659 // One register source. Used bits: R1[16-31].
660 case A2_asrh:
661 if (OpN == 1) {
662 Bits.set(Begin+16, Begin+32);
663 return true;
664 }
665 break;
666
667 // Two register sources. Used bits: R1[0-7], R2[0-7].
668 case A4_cmpbeq:
669 case A4_cmpbgt:
670 case A4_cmpbgtu:
671 if (OpN == 1) {
672 Bits.set(Begin, Begin+8);
673 return true;
674 }
675 break;
676
677 // Two register sources. Used bits: R1[0-15], R2[0-15].
678 case A4_cmpheq:
679 case A4_cmphgt:
680 case A4_cmphgtu:
681 case A2_addh_h16_ll:
682 case A2_addh_h16_sat_ll:
683 case A2_addh_l16_ll:
684 case A2_addh_l16_sat_ll:
685 case A2_combine_ll:
686 case A2_subh_h16_ll:
687 case A2_subh_h16_sat_ll:
688 case A2_subh_l16_ll:
689 case A2_subh_l16_sat_ll:
690 case M2_mpy_acc_ll_s0:
691 case M2_mpy_acc_ll_s1:
692 case M2_mpy_acc_sat_ll_s0:
693 case M2_mpy_acc_sat_ll_s1:
694 case M2_mpy_ll_s0:
695 case M2_mpy_ll_s1:
696 case M2_mpy_nac_ll_s0:
697 case M2_mpy_nac_ll_s1:
698 case M2_mpy_nac_sat_ll_s0:
699 case M2_mpy_nac_sat_ll_s1:
700 case M2_mpy_rnd_ll_s0:
701 case M2_mpy_rnd_ll_s1:
702 case M2_mpy_sat_ll_s0:
703 case M2_mpy_sat_ll_s1:
704 case M2_mpy_sat_rnd_ll_s0:
705 case M2_mpy_sat_rnd_ll_s1:
706 case M2_mpyd_acc_ll_s0:
707 case M2_mpyd_acc_ll_s1:
708 case M2_mpyd_ll_s0:
709 case M2_mpyd_ll_s1:
710 case M2_mpyd_nac_ll_s0:
711 case M2_mpyd_nac_ll_s1:
712 case M2_mpyd_rnd_ll_s0:
713 case M2_mpyd_rnd_ll_s1:
714 case M2_mpyu_acc_ll_s0:
715 case M2_mpyu_acc_ll_s1:
716 case M2_mpyu_ll_s0:
717 case M2_mpyu_ll_s1:
718 case M2_mpyu_nac_ll_s0:
719 case M2_mpyu_nac_ll_s1:
720 case M2_mpyud_acc_ll_s0:
721 case M2_mpyud_acc_ll_s1:
722 case M2_mpyud_ll_s0:
723 case M2_mpyud_ll_s1:
724 case M2_mpyud_nac_ll_s0:
725 case M2_mpyud_nac_ll_s1:
726 if (OpN == 1 || OpN == 2) {
727 Bits.set(Begin, Begin+16);
728 return true;
729 }
730 break;
731
732 // Two register sources. Used bits: R1[0-15], R2[16-31].
733 case A2_addh_h16_lh:
734 case A2_addh_h16_sat_lh:
735 case A2_combine_lh:
736 case A2_subh_h16_lh:
737 case A2_subh_h16_sat_lh:
738 case M2_mpy_acc_lh_s0:
739 case M2_mpy_acc_lh_s1:
740 case M2_mpy_acc_sat_lh_s0:
741 case M2_mpy_acc_sat_lh_s1:
742 case M2_mpy_lh_s0:
743 case M2_mpy_lh_s1:
744 case M2_mpy_nac_lh_s0:
745 case M2_mpy_nac_lh_s1:
746 case M2_mpy_nac_sat_lh_s0:
747 case M2_mpy_nac_sat_lh_s1:
748 case M2_mpy_rnd_lh_s0:
749 case M2_mpy_rnd_lh_s1:
750 case M2_mpy_sat_lh_s0:
751 case M2_mpy_sat_lh_s1:
752 case M2_mpy_sat_rnd_lh_s0:
753 case M2_mpy_sat_rnd_lh_s1:
754 case M2_mpyd_acc_lh_s0:
755 case M2_mpyd_acc_lh_s1:
756 case M2_mpyd_lh_s0:
757 case M2_mpyd_lh_s1:
758 case M2_mpyd_nac_lh_s0:
759 case M2_mpyd_nac_lh_s1:
760 case M2_mpyd_rnd_lh_s0:
761 case M2_mpyd_rnd_lh_s1:
762 case M2_mpyu_acc_lh_s0:
763 case M2_mpyu_acc_lh_s1:
764 case M2_mpyu_lh_s0:
765 case M2_mpyu_lh_s1:
766 case M2_mpyu_nac_lh_s0:
767 case M2_mpyu_nac_lh_s1:
768 case M2_mpyud_acc_lh_s0:
769 case M2_mpyud_acc_lh_s1:
770 case M2_mpyud_lh_s0:
771 case M2_mpyud_lh_s1:
772 case M2_mpyud_nac_lh_s0:
773 case M2_mpyud_nac_lh_s1:
774 // These four are actually LH.
775 case A2_addh_l16_hl:
776 case A2_addh_l16_sat_hl:
777 case A2_subh_l16_hl:
778 case A2_subh_l16_sat_hl:
779 if (OpN == 1) {
780 Bits.set(Begin, Begin+16);
781 return true;
782 }
783 if (OpN == 2) {
784 Bits.set(Begin+16, Begin+32);
785 return true;
786 }
787 break;
788
789 // Two register sources, used bits: R1[16-31], R2[0-15].
790 case A2_addh_h16_hl:
791 case A2_addh_h16_sat_hl:
792 case A2_combine_hl:
793 case A2_subh_h16_hl:
794 case A2_subh_h16_sat_hl:
795 case M2_mpy_acc_hl_s0:
796 case M2_mpy_acc_hl_s1:
797 case M2_mpy_acc_sat_hl_s0:
798 case M2_mpy_acc_sat_hl_s1:
799 case M2_mpy_hl_s0:
800 case M2_mpy_hl_s1:
801 case M2_mpy_nac_hl_s0:
802 case M2_mpy_nac_hl_s1:
803 case M2_mpy_nac_sat_hl_s0:
804 case M2_mpy_nac_sat_hl_s1:
805 case M2_mpy_rnd_hl_s0:
806 case M2_mpy_rnd_hl_s1:
807 case M2_mpy_sat_hl_s0:
808 case M2_mpy_sat_hl_s1:
809 case M2_mpy_sat_rnd_hl_s0:
810 case M2_mpy_sat_rnd_hl_s1:
811 case M2_mpyd_acc_hl_s0:
812 case M2_mpyd_acc_hl_s1:
813 case M2_mpyd_hl_s0:
814 case M2_mpyd_hl_s1:
815 case M2_mpyd_nac_hl_s0:
816 case M2_mpyd_nac_hl_s1:
817 case M2_mpyd_rnd_hl_s0:
818 case M2_mpyd_rnd_hl_s1:
819 case M2_mpyu_acc_hl_s0:
820 case M2_mpyu_acc_hl_s1:
821 case M2_mpyu_hl_s0:
822 case M2_mpyu_hl_s1:
823 case M2_mpyu_nac_hl_s0:
824 case M2_mpyu_nac_hl_s1:
825 case M2_mpyud_acc_hl_s0:
826 case M2_mpyud_acc_hl_s1:
827 case M2_mpyud_hl_s0:
828 case M2_mpyud_hl_s1:
829 case M2_mpyud_nac_hl_s0:
830 case M2_mpyud_nac_hl_s1:
831 if (OpN == 1) {
832 Bits.set(Begin+16, Begin+32);
833 return true;
834 }
835 if (OpN == 2) {
836 Bits.set(Begin, Begin+16);
837 return true;
838 }
839 break;
840
841 // Two register sources, used bits: R1[16-31], R2[16-31].
842 case A2_addh_h16_hh:
843 case A2_addh_h16_sat_hh:
844 case A2_combine_hh:
845 case A2_subh_h16_hh:
846 case A2_subh_h16_sat_hh:
847 case M2_mpy_acc_hh_s0:
848 case M2_mpy_acc_hh_s1:
849 case M2_mpy_acc_sat_hh_s0:
850 case M2_mpy_acc_sat_hh_s1:
851 case M2_mpy_hh_s0:
852 case M2_mpy_hh_s1:
853 case M2_mpy_nac_hh_s0:
854 case M2_mpy_nac_hh_s1:
855 case M2_mpy_nac_sat_hh_s0:
856 case M2_mpy_nac_sat_hh_s1:
857 case M2_mpy_rnd_hh_s0:
858 case M2_mpy_rnd_hh_s1:
859 case M2_mpy_sat_hh_s0:
860 case M2_mpy_sat_hh_s1:
861 case M2_mpy_sat_rnd_hh_s0:
862 case M2_mpy_sat_rnd_hh_s1:
863 case M2_mpyd_acc_hh_s0:
864 case M2_mpyd_acc_hh_s1:
865 case M2_mpyd_hh_s0:
866 case M2_mpyd_hh_s1:
867 case M2_mpyd_nac_hh_s0:
868 case M2_mpyd_nac_hh_s1:
869 case M2_mpyd_rnd_hh_s0:
870 case M2_mpyd_rnd_hh_s1:
871 case M2_mpyu_acc_hh_s0:
872 case M2_mpyu_acc_hh_s1:
873 case M2_mpyu_hh_s0:
874 case M2_mpyu_hh_s1:
875 case M2_mpyu_nac_hh_s0:
876 case M2_mpyu_nac_hh_s1:
877 case M2_mpyud_acc_hh_s0:
878 case M2_mpyud_acc_hh_s1:
879 case M2_mpyud_hh_s0:
880 case M2_mpyud_hh_s1:
881 case M2_mpyud_nac_hh_s0:
882 case M2_mpyud_nac_hh_s1:
883 if (OpN == 1 || OpN == 2) {
884 Bits.set(Begin+16, Begin+32);
885 return true;
886 }
887 break;
888 }
889
890 return false;
891}
892
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000893// Calculate the register class that matches Reg:Sub. For example, if
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000894// vreg1 is a double register, then vreg1:isub_hi would match the "int"
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000895// register class.
896const TargetRegisterClass *HexagonBitSimplify::getFinalVRegClass(
897 const BitTracker::RegisterRef &RR, MachineRegisterInfo &MRI) {
898 if (!TargetRegisterInfo::isVirtualRegister(RR.Reg))
899 return nullptr;
900 auto *RC = MRI.getRegClass(RR.Reg);
901 if (RR.Sub == 0)
902 return RC;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000903 auto &HRI = static_cast<const HexagonRegisterInfo&>(
904 *MRI.getTargetRegisterInfo());
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000905
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000906 auto VerifySR = [&HRI] (const TargetRegisterClass *RC, unsigned Sub) -> void {
David L. Jones41cecba2017-01-13 21:02:41 +0000907 (void)HRI;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000908 assert(Sub == HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_lo) ||
909 Sub == HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_hi));
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000910 };
911
912 switch (RC->getID()) {
913 case Hexagon::DoubleRegsRegClassID:
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000914 VerifySR(RC, RR.Sub);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000915 return &Hexagon::IntRegsRegClass;
Krzysztof Parzyszek5337a3e2016-01-14 21:45:43 +0000916 case Hexagon::VecDblRegsRegClassID:
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000917 VerifySR(RC, RR.Sub);
Krzysztof Parzyszek5337a3e2016-01-14 21:45:43 +0000918 return &Hexagon::VectorRegsRegClass;
919 case Hexagon::VecDblRegs128BRegClassID:
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000920 VerifySR(RC, RR.Sub);
Krzysztof Parzyszek5337a3e2016-01-14 21:45:43 +0000921 return &Hexagon::VectorRegs128BRegClass;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000922 }
923 return nullptr;
924}
925
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000926// Check if RD could be replaced with RS at any possible use of RD.
927// For example a predicate register cannot be replaced with a integer
928// register, but a 64-bit register with a subregister can be replaced
929// with a 32-bit register.
930bool HexagonBitSimplify::isTransparentCopy(const BitTracker::RegisterRef &RD,
931 const BitTracker::RegisterRef &RS, MachineRegisterInfo &MRI) {
932 if (!TargetRegisterInfo::isVirtualRegister(RD.Reg) ||
933 !TargetRegisterInfo::isVirtualRegister(RS.Reg))
934 return false;
935 // Return false if one (or both) classes are nullptr.
936 auto *DRC = getFinalVRegClass(RD, MRI);
937 if (!DRC)
938 return false;
939
940 return DRC == getFinalVRegClass(RS, MRI);
941}
942
Krzysztof Parzyszekd391d6f2016-10-06 16:18:04 +0000943bool HexagonBitSimplify::hasTiedUse(unsigned Reg, MachineRegisterInfo &MRI,
944 unsigned NewSub) {
945 if (!PreserveTiedOps)
946 return false;
Eugene Zelenko82085922016-12-13 22:13:50 +0000947 return llvm::any_of(MRI.use_operands(Reg),
948 [NewSub] (const MachineOperand &Op) -> bool {
949 return Op.getSubReg() != NewSub && Op.isTied();
950 });
Krzysztof Parzyszekd391d6f2016-10-06 16:18:04 +0000951}
952
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000953namespace {
Eugene Zelenko82085922016-12-13 22:13:50 +0000954
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000955 class DeadCodeElimination {
956 public:
957 DeadCodeElimination(MachineFunction &mf, MachineDominatorTree &mdt)
958 : MF(mf), HII(*MF.getSubtarget<HexagonSubtarget>().getInstrInfo()),
959 MDT(mdt), MRI(mf.getRegInfo()) {}
960
961 bool run() {
962 return runOnNode(MDT.getRootNode());
963 }
964
965 private:
966 bool isDead(unsigned R) const;
967 bool runOnNode(MachineDomTreeNode *N);
968
969 MachineFunction &MF;
970 const HexagonInstrInfo &HII;
971 MachineDominatorTree &MDT;
972 MachineRegisterInfo &MRI;
973 };
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000974
Eugene Zelenko82085922016-12-13 22:13:50 +0000975} // end anonymous namespace
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000976
977bool DeadCodeElimination::isDead(unsigned R) const {
978 for (auto I = MRI.use_begin(R), E = MRI.use_end(); I != E; ++I) {
979 MachineInstr *UseI = I->getParent();
980 if (UseI->isDebugValue())
981 continue;
982 if (UseI->isPHI()) {
983 assert(!UseI->getOperand(0).getSubReg());
984 unsigned DR = UseI->getOperand(0).getReg();
985 if (DR == R)
986 continue;
987 }
988 return false;
989 }
990 return true;
991}
992
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000993bool DeadCodeElimination::runOnNode(MachineDomTreeNode *N) {
994 bool Changed = false;
Daniel Berlin58a6e572017-02-09 20:37:24 +0000995
Daniel Berlin73ad5cb2017-02-09 20:37:46 +0000996 for (auto *DTN : children<MachineDomTreeNode*>(N))
Daniel Berlin58a6e572017-02-09 20:37:24 +0000997 Changed |= runOnNode(DTN);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000998
999 MachineBasicBlock *B = N->getBlock();
1000 std::vector<MachineInstr*> Instrs;
1001 for (auto I = B->rbegin(), E = B->rend(); I != E; ++I)
1002 Instrs.push_back(&*I);
1003
1004 for (auto MI : Instrs) {
1005 unsigned Opc = MI->getOpcode();
1006 // Do not touch lifetime markers. This is why the target-independent DCE
1007 // cannot be used.
1008 if (Opc == TargetOpcode::LIFETIME_START ||
1009 Opc == TargetOpcode::LIFETIME_END)
1010 continue;
1011 bool Store = false;
1012 if (MI->isInlineAsm())
1013 continue;
1014 // Delete PHIs if possible.
1015 if (!MI->isPHI() && !MI->isSafeToMove(nullptr, Store))
1016 continue;
1017
1018 bool AllDead = true;
1019 SmallVector<unsigned,2> Regs;
1020 for (auto &Op : MI->operands()) {
1021 if (!Op.isReg() || !Op.isDef())
1022 continue;
1023 unsigned R = Op.getReg();
1024 if (!TargetRegisterInfo::isVirtualRegister(R) || !isDead(R)) {
1025 AllDead = false;
1026 break;
1027 }
1028 Regs.push_back(R);
1029 }
1030 if (!AllDead)
1031 continue;
1032
1033 B->erase(MI);
1034 for (unsigned i = 0, n = Regs.size(); i != n; ++i)
1035 MRI.markUsesInDebugValueAsUndef(Regs[i]);
1036 Changed = true;
1037 }
1038
1039 return Changed;
1040}
1041
Eugene Zelenko82085922016-12-13 22:13:50 +00001042namespace {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001043
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001044// Eliminate redundant instructions
1045//
1046// This transformation will identify instructions where the output register
1047// is the same as one of its input registers. This only works on instructions
1048// that define a single register (unlike post-increment loads, for example).
1049// The equality check is actually more detailed: the code calculates which
1050// bits of the output are used, and only compares these bits with the input
1051// registers.
1052// If the output matches an input, the instruction is replaced with COPY.
1053// The copies will be removed by another transformation.
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001054 class RedundantInstrElimination : public Transformation {
1055 public:
1056 RedundantInstrElimination(BitTracker &bt, const HexagonInstrInfo &hii,
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001057 const HexagonRegisterInfo &hri, MachineRegisterInfo &mri)
1058 : Transformation(true), HII(hii), HRI(hri), MRI(mri), BT(bt) {}
Eugene Zelenko82085922016-12-13 22:13:50 +00001059
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001060 bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override;
Eugene Zelenko82085922016-12-13 22:13:50 +00001061
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001062 private:
1063 bool isLossyShiftLeft(const MachineInstr &MI, unsigned OpN,
1064 unsigned &LostB, unsigned &LostE);
1065 bool isLossyShiftRight(const MachineInstr &MI, unsigned OpN,
1066 unsigned &LostB, unsigned &LostE);
1067 bool computeUsedBits(unsigned Reg, BitVector &Bits);
1068 bool computeUsedBits(const MachineInstr &MI, unsigned OpN, BitVector &Bits,
1069 uint16_t Begin);
1070 bool usedBitsEqual(BitTracker::RegisterRef RD, BitTracker::RegisterRef RS);
1071
1072 const HexagonInstrInfo &HII;
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001073 const HexagonRegisterInfo &HRI;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001074 MachineRegisterInfo &MRI;
1075 BitTracker &BT;
1076 };
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001077
Eugene Zelenko82085922016-12-13 22:13:50 +00001078} // end anonymous namespace
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001079
1080// Check if the instruction is a lossy shift left, where the input being
1081// shifted is the operand OpN of MI. If true, [LostB, LostE) is the range
1082// of bit indices that are lost.
1083bool RedundantInstrElimination::isLossyShiftLeft(const MachineInstr &MI,
1084 unsigned OpN, unsigned &LostB, unsigned &LostE) {
1085 using namespace Hexagon;
Eugene Zelenko82085922016-12-13 22:13:50 +00001086
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001087 unsigned Opc = MI.getOpcode();
1088 unsigned ImN, RegN, Width;
1089 switch (Opc) {
1090 case S2_asl_i_p:
1091 ImN = 2;
1092 RegN = 1;
1093 Width = 64;
1094 break;
1095 case S2_asl_i_p_acc:
1096 case S2_asl_i_p_and:
1097 case S2_asl_i_p_nac:
1098 case S2_asl_i_p_or:
1099 case S2_asl_i_p_xacc:
1100 ImN = 3;
1101 RegN = 2;
1102 Width = 64;
1103 break;
1104 case S2_asl_i_r:
1105 ImN = 2;
1106 RegN = 1;
1107 Width = 32;
1108 break;
1109 case S2_addasl_rrri:
1110 case S4_andi_asl_ri:
1111 case S4_ori_asl_ri:
1112 case S4_addi_asl_ri:
1113 case S4_subi_asl_ri:
1114 case S2_asl_i_r_acc:
1115 case S2_asl_i_r_and:
1116 case S2_asl_i_r_nac:
1117 case S2_asl_i_r_or:
1118 case S2_asl_i_r_sat:
1119 case S2_asl_i_r_xacc:
1120 ImN = 3;
1121 RegN = 2;
1122 Width = 32;
1123 break;
1124 default:
1125 return false;
1126 }
1127
1128 if (RegN != OpN)
1129 return false;
1130
1131 assert(MI.getOperand(ImN).isImm());
1132 unsigned S = MI.getOperand(ImN).getImm();
1133 if (S == 0)
1134 return false;
1135 LostB = Width-S;
1136 LostE = Width;
1137 return true;
1138}
1139
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001140// Check if the instruction is a lossy shift right, where the input being
1141// shifted is the operand OpN of MI. If true, [LostB, LostE) is the range
1142// of bit indices that are lost.
1143bool RedundantInstrElimination::isLossyShiftRight(const MachineInstr &MI,
1144 unsigned OpN, unsigned &LostB, unsigned &LostE) {
1145 using namespace Hexagon;
Eugene Zelenko82085922016-12-13 22:13:50 +00001146
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001147 unsigned Opc = MI.getOpcode();
1148 unsigned ImN, RegN;
1149 switch (Opc) {
1150 case S2_asr_i_p:
1151 case S2_lsr_i_p:
1152 ImN = 2;
1153 RegN = 1;
1154 break;
1155 case S2_asr_i_p_acc:
1156 case S2_asr_i_p_and:
1157 case S2_asr_i_p_nac:
1158 case S2_asr_i_p_or:
1159 case S2_lsr_i_p_acc:
1160 case S2_lsr_i_p_and:
1161 case S2_lsr_i_p_nac:
1162 case S2_lsr_i_p_or:
1163 case S2_lsr_i_p_xacc:
1164 ImN = 3;
1165 RegN = 2;
1166 break;
1167 case S2_asr_i_r:
1168 case S2_lsr_i_r:
1169 ImN = 2;
1170 RegN = 1;
1171 break;
1172 case S4_andi_lsr_ri:
1173 case S4_ori_lsr_ri:
1174 case S4_addi_lsr_ri:
1175 case S4_subi_lsr_ri:
1176 case S2_asr_i_r_acc:
1177 case S2_asr_i_r_and:
1178 case S2_asr_i_r_nac:
1179 case S2_asr_i_r_or:
1180 case S2_lsr_i_r_acc:
1181 case S2_lsr_i_r_and:
1182 case S2_lsr_i_r_nac:
1183 case S2_lsr_i_r_or:
1184 case S2_lsr_i_r_xacc:
1185 ImN = 3;
1186 RegN = 2;
1187 break;
1188
1189 default:
1190 return false;
1191 }
1192
1193 if (RegN != OpN)
1194 return false;
1195
1196 assert(MI.getOperand(ImN).isImm());
1197 unsigned S = MI.getOperand(ImN).getImm();
1198 LostB = 0;
1199 LostE = S;
1200 return true;
1201}
1202
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001203// Calculate the bit vector that corresponds to the used bits of register Reg.
1204// The vector Bits has the same size, as the size of Reg in bits. If the cal-
1205// culation fails (i.e. the used bits are unknown), it returns false. Other-
1206// wise, it returns true and sets the corresponding bits in Bits.
1207bool RedundantInstrElimination::computeUsedBits(unsigned Reg, BitVector &Bits) {
1208 BitVector Used(Bits.size());
1209 RegisterSet Visited;
1210 std::vector<unsigned> Pending;
1211 Pending.push_back(Reg);
1212
1213 for (unsigned i = 0; i < Pending.size(); ++i) {
1214 unsigned R = Pending[i];
1215 if (Visited.has(R))
1216 continue;
1217 Visited.insert(R);
1218 for (auto I = MRI.use_begin(R), E = MRI.use_end(); I != E; ++I) {
1219 BitTracker::RegisterRef UR = *I;
1220 unsigned B, W;
1221 if (!HBS::getSubregMask(UR, B, W, MRI))
1222 return false;
1223 MachineInstr &UseI = *I->getParent();
1224 if (UseI.isPHI() || UseI.isCopy()) {
1225 unsigned DefR = UseI.getOperand(0).getReg();
1226 if (!TargetRegisterInfo::isVirtualRegister(DefR))
1227 return false;
1228 Pending.push_back(DefR);
1229 } else {
1230 if (!computeUsedBits(UseI, I.getOperandNo(), Used, B))
1231 return false;
1232 }
1233 }
1234 }
1235 Bits |= Used;
1236 return true;
1237}
1238
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001239// Calculate the bits used by instruction MI in a register in operand OpN.
1240// Return true/false if the calculation succeeds/fails. If is succeeds, set
1241// used bits in Bits. This function does not reset any bits in Bits, so
1242// subsequent calls over different instructions will result in the union
1243// of the used bits in all these instructions.
1244// The register in question may be used with a sub-register, whereas Bits
1245// holds the bits for the entire register. To keep track of that, the
1246// argument Begin indicates where in Bits is the lowest-significant bit
1247// of the register used in operand OpN. For example, in instruction:
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001248// vreg1 = S2_lsr_i_r vreg2:isub_hi, 10
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001249// the operand 1 is a 32-bit register, which happens to be a subregister
1250// of the 64-bit register vreg2, and that subregister starts at position 32.
1251// In this case Begin=32, since Bits[32] would be the lowest-significant bit
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001252// of vreg2:isub_hi.
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001253bool RedundantInstrElimination::computeUsedBits(const MachineInstr &MI,
1254 unsigned OpN, BitVector &Bits, uint16_t Begin) {
1255 unsigned Opc = MI.getOpcode();
1256 BitVector T(Bits.size());
1257 bool GotBits = HBS::getUsedBits(Opc, OpN, T, Begin, HII);
1258 // Even if we don't have bits yet, we could still provide some information
1259 // if the instruction is a lossy shift: the lost bits will be marked as
1260 // not used.
1261 unsigned LB, LE;
1262 if (isLossyShiftLeft(MI, OpN, LB, LE) || isLossyShiftRight(MI, OpN, LB, LE)) {
1263 assert(MI.getOperand(OpN).isReg());
1264 BitTracker::RegisterRef RR = MI.getOperand(OpN);
1265 const TargetRegisterClass *RC = HBS::getFinalVRegClass(RR, MRI);
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001266 uint16_t Width = HRI.getRegSizeInBits(*RC);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001267
1268 if (!GotBits)
1269 T.set(Begin, Begin+Width);
1270 assert(LB <= LE && LB < Width && LE <= Width);
1271 T.reset(Begin+LB, Begin+LE);
1272 GotBits = true;
1273 }
1274 if (GotBits)
1275 Bits |= T;
1276 return GotBits;
1277}
1278
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001279// Calculates the used bits in RD ("defined register"), and checks if these
1280// bits in RS ("used register") and RD are identical.
1281bool RedundantInstrElimination::usedBitsEqual(BitTracker::RegisterRef RD,
1282 BitTracker::RegisterRef RS) {
1283 const BitTracker::RegisterCell &DC = BT.lookup(RD.Reg);
1284 const BitTracker::RegisterCell &SC = BT.lookup(RS.Reg);
1285
1286 unsigned DB, DW;
1287 if (!HBS::getSubregMask(RD, DB, DW, MRI))
1288 return false;
1289 unsigned SB, SW;
1290 if (!HBS::getSubregMask(RS, SB, SW, MRI))
1291 return false;
1292 if (SW != DW)
1293 return false;
1294
1295 BitVector Used(DC.width());
1296 if (!computeUsedBits(RD.Reg, Used))
1297 return false;
1298
1299 for (unsigned i = 0; i != DW; ++i)
1300 if (Used[i+DB] && DC[DB+i] != SC[SB+i])
1301 return false;
1302 return true;
1303}
1304
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001305bool RedundantInstrElimination::processBlock(MachineBasicBlock &B,
1306 const RegisterSet&) {
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001307 if (!BT.reached(&B))
1308 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001309 bool Changed = false;
1310
1311 for (auto I = B.begin(), E = B.end(), NextI = I; I != E; ++I) {
1312 NextI = std::next(I);
1313 MachineInstr *MI = &*I;
1314
1315 if (MI->getOpcode() == TargetOpcode::COPY)
1316 continue;
1317 if (MI->hasUnmodeledSideEffects() || MI->isInlineAsm())
1318 continue;
1319 unsigned NumD = MI->getDesc().getNumDefs();
1320 if (NumD != 1)
1321 continue;
1322
1323 BitTracker::RegisterRef RD = MI->getOperand(0);
1324 if (!BT.has(RD.Reg))
1325 continue;
1326 const BitTracker::RegisterCell &DC = BT.lookup(RD.Reg);
Krzysztof Parzyszeka3c5d442016-01-13 15:48:18 +00001327 auto At = MI->isPHI() ? B.getFirstNonPHI()
1328 : MachineBasicBlock::iterator(MI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001329
1330 // Find a source operand that is equal to the result.
1331 for (auto &Op : MI->uses()) {
1332 if (!Op.isReg())
1333 continue;
1334 BitTracker::RegisterRef RS = Op;
1335 if (!BT.has(RS.Reg))
1336 continue;
1337 if (!HBS::isTransparentCopy(RD, RS, MRI))
1338 continue;
1339
1340 unsigned BN, BW;
1341 if (!HBS::getSubregMask(RS, BN, BW, MRI))
1342 continue;
1343
1344 const BitTracker::RegisterCell &SC = BT.lookup(RS.Reg);
1345 if (!usedBitsEqual(RD, RS) && !HBS::isEqual(DC, 0, SC, BN, BW))
1346 continue;
1347
1348 // If found, replace the instruction with a COPY.
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +00001349 const DebugLoc &DL = MI->getDebugLoc();
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001350 const TargetRegisterClass *FRC = HBS::getFinalVRegClass(RD, MRI);
1351 unsigned NewR = MRI.createVirtualRegister(FRC);
Krzysztof Parzyszek6eba5b82016-07-26 19:08:45 +00001352 MachineInstr *CopyI =
1353 BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
1354 .addReg(RS.Reg, 0, RS.Sub);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001355 HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI);
Krzysztof Parzyszek6eba5b82016-07-26 19:08:45 +00001356 // This pass can create copies between registers that don't have the
1357 // exact same values. Updating the tracker has to involve updating
1358 // all dependent cells. Example:
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001359 // vreg1 = inst vreg2 ; vreg1 != vreg2, but used bits are equal
1360 //
1361 // vreg3 = copy vreg2 ; <- inserted
1362 // ... = vreg3 ; <- replaced from vreg2
1363 // Indirectly, we can create a "copy" between vreg1 and vreg2 even
1364 // though their exact values do not match.
Krzysztof Parzyszek6eba5b82016-07-26 19:08:45 +00001365 BT.visit(*CopyI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001366 Changed = true;
1367 break;
1368 }
1369 }
1370
1371 return Changed;
1372}
1373
Eugene Zelenko82085922016-12-13 22:13:50 +00001374namespace {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001375
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001376// Recognize instructions that produce constant values known at compile-time.
1377// Replace them with register definitions that load these constants directly.
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001378 class ConstGeneration : public Transformation {
1379 public:
1380 ConstGeneration(BitTracker &bt, const HexagonInstrInfo &hii,
1381 MachineRegisterInfo &mri)
1382 : Transformation(true), HII(hii), MRI(mri), BT(bt) {}
Eugene Zelenko82085922016-12-13 22:13:50 +00001383
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001384 bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override;
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001385 static bool isTfrConst(const MachineInstr &MI);
Eugene Zelenko82085922016-12-13 22:13:50 +00001386
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001387 private:
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001388 unsigned genTfrConst(const TargetRegisterClass *RC, int64_t C,
1389 MachineBasicBlock &B, MachineBasicBlock::iterator At, DebugLoc &DL);
1390
1391 const HexagonInstrInfo &HII;
1392 MachineRegisterInfo &MRI;
1393 BitTracker &BT;
1394 };
Eugene Zelenko82085922016-12-13 22:13:50 +00001395
1396} // end anonymous namespace
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001397
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001398bool ConstGeneration::isTfrConst(const MachineInstr &MI) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001399 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001400 switch (Opc) {
1401 case Hexagon::A2_combineii:
1402 case Hexagon::A4_combineii:
1403 case Hexagon::A2_tfrsi:
1404 case Hexagon::A2_tfrpi:
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001405 case Hexagon::PS_true:
1406 case Hexagon::PS_false:
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001407 case Hexagon::CONST32:
1408 case Hexagon::CONST64:
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001409 return true;
1410 }
1411 return false;
1412}
1413
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001414// Generate a transfer-immediate instruction that is appropriate for the
1415// register class and the actual value being transferred.
1416unsigned ConstGeneration::genTfrConst(const TargetRegisterClass *RC, int64_t C,
1417 MachineBasicBlock &B, MachineBasicBlock::iterator At, DebugLoc &DL) {
1418 unsigned Reg = MRI.createVirtualRegister(RC);
1419 if (RC == &Hexagon::IntRegsRegClass) {
1420 BuildMI(B, At, DL, HII.get(Hexagon::A2_tfrsi), Reg)
1421 .addImm(int32_t(C));
1422 return Reg;
1423 }
1424
1425 if (RC == &Hexagon::DoubleRegsRegClass) {
1426 if (isInt<8>(C)) {
1427 BuildMI(B, At, DL, HII.get(Hexagon::A2_tfrpi), Reg)
1428 .addImm(C);
1429 return Reg;
1430 }
1431
1432 unsigned Lo = Lo_32(C), Hi = Hi_32(C);
1433 if (isInt<8>(Lo) || isInt<8>(Hi)) {
1434 unsigned Opc = isInt<8>(Lo) ? Hexagon::A2_combineii
1435 : Hexagon::A4_combineii;
1436 BuildMI(B, At, DL, HII.get(Opc), Reg)
1437 .addImm(int32_t(Hi))
1438 .addImm(int32_t(Lo));
1439 return Reg;
1440 }
1441
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001442 BuildMI(B, At, DL, HII.get(Hexagon::CONST64), Reg)
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001443 .addImm(C);
1444 return Reg;
1445 }
1446
1447 if (RC == &Hexagon::PredRegsRegClass) {
1448 unsigned Opc;
1449 if (C == 0)
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001450 Opc = Hexagon::PS_false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001451 else if ((C & 0xFF) == 0xFF)
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001452 Opc = Hexagon::PS_true;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001453 else
1454 return 0;
1455 BuildMI(B, At, DL, HII.get(Opc), Reg);
1456 return Reg;
1457 }
1458
1459 return 0;
1460}
1461
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001462bool ConstGeneration::processBlock(MachineBasicBlock &B, const RegisterSet&) {
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001463 if (!BT.reached(&B))
1464 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001465 bool Changed = false;
1466 RegisterSet Defs;
1467
1468 for (auto I = B.begin(), E = B.end(); I != E; ++I) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001469 if (isTfrConst(*I))
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001470 continue;
1471 Defs.clear();
1472 HBS::getInstrDefs(*I, Defs);
1473 if (Defs.count() != 1)
1474 continue;
1475 unsigned DR = Defs.find_first();
1476 if (!TargetRegisterInfo::isVirtualRegister(DR))
1477 continue;
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001478 uint64_t U;
1479 const BitTracker::RegisterCell &DRC = BT.lookup(DR);
1480 if (HBS::getConst(DRC, 0, DRC.width(), U)) {
1481 int64_t C = U;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001482 DebugLoc DL = I->getDebugLoc();
1483 auto At = I->isPHI() ? B.getFirstNonPHI() : I;
1484 unsigned ImmReg = genTfrConst(MRI.getRegClass(DR), C, B, At, DL);
1485 if (ImmReg) {
1486 HBS::replaceReg(DR, ImmReg, MRI);
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001487 BT.put(ImmReg, DRC);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001488 Changed = true;
1489 }
1490 }
1491 }
1492 return Changed;
1493}
1494
Eugene Zelenko82085922016-12-13 22:13:50 +00001495namespace {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001496
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001497// Identify pairs of available registers which hold identical values.
1498// In such cases, only one of them needs to be calculated, the other one
1499// will be defined as a copy of the first.
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001500 class CopyGeneration : public Transformation {
1501 public:
1502 CopyGeneration(BitTracker &bt, const HexagonInstrInfo &hii,
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001503 const HexagonRegisterInfo &hri, MachineRegisterInfo &mri)
1504 : Transformation(true), HII(hii), HRI(hri), MRI(mri), BT(bt) {}
Eugene Zelenko82085922016-12-13 22:13:50 +00001505
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001506 bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override;
Eugene Zelenko82085922016-12-13 22:13:50 +00001507
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001508 private:
1509 bool findMatch(const BitTracker::RegisterRef &Inp,
1510 BitTracker::RegisterRef &Out, const RegisterSet &AVs);
1511
1512 const HexagonInstrInfo &HII;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001513 const HexagonRegisterInfo &HRI;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001514 MachineRegisterInfo &MRI;
1515 BitTracker &BT;
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001516 RegisterSet Forbidden;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001517 };
1518
Eugene Zelenko82085922016-12-13 22:13:50 +00001519// Eliminate register copies RD = RS, by replacing the uses of RD with
1520// with uses of RS.
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001521 class CopyPropagation : public Transformation {
1522 public:
1523 CopyPropagation(const HexagonRegisterInfo &hri, MachineRegisterInfo &mri)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001524 : Transformation(false), HRI(hri), MRI(mri) {}
Eugene Zelenko82085922016-12-13 22:13:50 +00001525
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001526 bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override;
Eugene Zelenko82085922016-12-13 22:13:50 +00001527
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +00001528 static bool isCopyReg(unsigned Opc, bool NoConv);
Eugene Zelenko82085922016-12-13 22:13:50 +00001529
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001530 private:
1531 bool propagateRegCopy(MachineInstr &MI);
1532
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001533 const HexagonRegisterInfo &HRI;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001534 MachineRegisterInfo &MRI;
1535 };
1536
Eugene Zelenko82085922016-12-13 22:13:50 +00001537} // end anonymous namespace
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001538
1539/// Check if there is a register in AVs that is identical to Inp. If so,
1540/// set Out to the found register. The output may be a pair Reg:Sub.
1541bool CopyGeneration::findMatch(const BitTracker::RegisterRef &Inp,
1542 BitTracker::RegisterRef &Out, const RegisterSet &AVs) {
1543 if (!BT.has(Inp.Reg))
1544 return false;
1545 const BitTracker::RegisterCell &InpRC = BT.lookup(Inp.Reg);
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001546 auto *FRC = HBS::getFinalVRegClass(Inp, MRI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001547 unsigned B, W;
1548 if (!HBS::getSubregMask(Inp, B, W, MRI))
1549 return false;
1550
1551 for (unsigned R = AVs.find_first(); R; R = AVs.find_next(R)) {
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001552 if (!BT.has(R) || Forbidden[R])
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001553 continue;
1554 const BitTracker::RegisterCell &RC = BT.lookup(R);
1555 unsigned RW = RC.width();
1556 if (W == RW) {
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001557 if (FRC != MRI.getRegClass(R))
1558 continue;
1559 if (!HBS::isTransparentCopy(R, Inp, MRI))
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001560 continue;
1561 if (!HBS::isEqual(InpRC, B, RC, 0, W))
1562 continue;
1563 Out.Reg = R;
1564 Out.Sub = 0;
1565 return true;
1566 }
1567 // Check if there is a super-register, whose part (with a subregister)
1568 // is equal to the input.
1569 // Only do double registers for now.
1570 if (W*2 != RW)
1571 continue;
1572 if (MRI.getRegClass(R) != &Hexagon::DoubleRegsRegClass)
1573 continue;
1574
1575 if (HBS::isEqual(InpRC, B, RC, 0, W))
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001576 Out.Sub = Hexagon::isub_lo;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001577 else if (HBS::isEqual(InpRC, B, RC, W, W))
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001578 Out.Sub = Hexagon::isub_hi;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001579 else
1580 continue;
1581 Out.Reg = R;
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001582 if (HBS::isTransparentCopy(Out, Inp, MRI))
1583 return true;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001584 }
1585 return false;
1586}
1587
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001588bool CopyGeneration::processBlock(MachineBasicBlock &B,
1589 const RegisterSet &AVs) {
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001590 if (!BT.reached(&B))
1591 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001592 RegisterSet AVB(AVs);
1593 bool Changed = false;
1594 RegisterSet Defs;
1595
1596 for (auto I = B.begin(), E = B.end(), NextI = I; I != E;
1597 ++I, AVB.insert(Defs)) {
1598 NextI = std::next(I);
1599 Defs.clear();
1600 HBS::getInstrDefs(*I, Defs);
1601
1602 unsigned Opc = I->getOpcode();
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +00001603 if (CopyPropagation::isCopyReg(Opc, false) ||
1604 ConstGeneration::isTfrConst(*I))
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001605 continue;
1606
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001607 DebugLoc DL = I->getDebugLoc();
1608 auto At = I->isPHI() ? B.getFirstNonPHI() : I;
1609
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001610 for (unsigned R = Defs.find_first(); R; R = Defs.find_next(R)) {
1611 BitTracker::RegisterRef MR;
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001612 auto *FRC = HBS::getFinalVRegClass(R, MRI);
1613
1614 if (findMatch(R, MR, AVB)) {
1615 unsigned NewR = MRI.createVirtualRegister(FRC);
1616 BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
1617 .addReg(MR.Reg, 0, MR.Sub);
1618 BT.put(BitTracker::RegisterRef(NewR), BT.get(MR));
1619 HBS::replaceReg(R, NewR, MRI);
1620 Forbidden.insert(R);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001621 continue;
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001622 }
1623
Krzysztof Parzyszek824d3472016-08-02 21:49:20 +00001624 if (FRC == &Hexagon::DoubleRegsRegClass ||
1625 FRC == &Hexagon::VecDblRegsRegClass ||
1626 FRC == &Hexagon::VecDblRegs128BRegClass) {
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001627 // Try to generate REG_SEQUENCE.
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001628 unsigned SubLo = HRI.getHexagonSubRegIndex(FRC, Hexagon::ps_sub_lo);
1629 unsigned SubHi = HRI.getHexagonSubRegIndex(FRC, Hexagon::ps_sub_hi);
1630 BitTracker::RegisterRef TL = { R, SubLo };
1631 BitTracker::RegisterRef TH = { R, SubHi };
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001632 BitTracker::RegisterRef ML, MH;
1633 if (findMatch(TL, ML, AVB) && findMatch(TH, MH, AVB)) {
1634 auto *FRC = HBS::getFinalVRegClass(R, MRI);
1635 unsigned NewR = MRI.createVirtualRegister(FRC);
1636 BuildMI(B, At, DL, HII.get(TargetOpcode::REG_SEQUENCE), NewR)
1637 .addReg(ML.Reg, 0, ML.Sub)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001638 .addImm(SubLo)
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001639 .addReg(MH.Reg, 0, MH.Sub)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001640 .addImm(SubHi);
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001641 BT.put(BitTracker::RegisterRef(NewR), BT.get(R));
1642 HBS::replaceReg(R, NewR, MRI);
1643 Forbidden.insert(R);
1644 }
1645 }
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001646 }
1647 }
1648
1649 return Changed;
1650}
1651
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +00001652bool CopyPropagation::isCopyReg(unsigned Opc, bool NoConv) {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001653 switch (Opc) {
1654 case TargetOpcode::COPY:
1655 case TargetOpcode::REG_SEQUENCE:
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +00001656 case Hexagon::A4_combineir:
1657 case Hexagon::A4_combineri:
1658 return true;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001659 case Hexagon::A2_tfr:
1660 case Hexagon::A2_tfrp:
1661 case Hexagon::A2_combinew:
Krzysztof Parzyszek824d3472016-08-02 21:49:20 +00001662 case Hexagon::V6_vcombine:
1663 case Hexagon::V6_vcombine_128B:
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +00001664 return NoConv;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001665 default:
1666 break;
1667 }
1668 return false;
1669}
1670
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001671bool CopyPropagation::propagateRegCopy(MachineInstr &MI) {
1672 bool Changed = false;
1673 unsigned Opc = MI.getOpcode();
1674 BitTracker::RegisterRef RD = MI.getOperand(0);
1675 assert(MI.getOperand(0).getSubReg() == 0);
1676
1677 switch (Opc) {
1678 case TargetOpcode::COPY:
1679 case Hexagon::A2_tfr:
1680 case Hexagon::A2_tfrp: {
1681 BitTracker::RegisterRef RS = MI.getOperand(1);
1682 if (!HBS::isTransparentCopy(RD, RS, MRI))
1683 break;
1684 if (RS.Sub != 0)
1685 Changed = HBS::replaceRegWithSub(RD.Reg, RS.Reg, RS.Sub, MRI);
1686 else
1687 Changed = HBS::replaceReg(RD.Reg, RS.Reg, MRI);
1688 break;
1689 }
1690 case TargetOpcode::REG_SEQUENCE: {
1691 BitTracker::RegisterRef SL, SH;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001692 if (HBS::parseRegSequence(MI, SL, SH, MRI)) {
1693 const TargetRegisterClass *RC = MRI.getRegClass(RD.Reg);
1694 unsigned SubLo = HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_lo);
1695 unsigned SubHi = HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_hi);
1696 Changed = HBS::replaceSubWithSub(RD.Reg, SubLo, SL.Reg, SL.Sub, MRI);
1697 Changed |= HBS::replaceSubWithSub(RD.Reg, SubHi, SH.Reg, SH.Sub, MRI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001698 }
1699 break;
1700 }
Krzysztof Parzyszek824d3472016-08-02 21:49:20 +00001701 case Hexagon::A2_combinew:
1702 case Hexagon::V6_vcombine:
1703 case Hexagon::V6_vcombine_128B: {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001704 const TargetRegisterClass *RC = MRI.getRegClass(RD.Reg);
1705 unsigned SubLo = HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_lo);
1706 unsigned SubHi = HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_hi);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001707 BitTracker::RegisterRef RH = MI.getOperand(1), RL = MI.getOperand(2);
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001708 Changed = HBS::replaceSubWithSub(RD.Reg, SubLo, RL.Reg, RL.Sub, MRI);
1709 Changed |= HBS::replaceSubWithSub(RD.Reg, SubHi, RH.Reg, RH.Sub, MRI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001710 break;
1711 }
1712 case Hexagon::A4_combineir:
1713 case Hexagon::A4_combineri: {
1714 unsigned SrcX = (Opc == Hexagon::A4_combineir) ? 2 : 1;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001715 unsigned Sub = (Opc == Hexagon::A4_combineir) ? Hexagon::isub_lo
1716 : Hexagon::isub_hi;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001717 BitTracker::RegisterRef RS = MI.getOperand(SrcX);
1718 Changed = HBS::replaceSubWithSub(RD.Reg, Sub, RS.Reg, RS.Sub, MRI);
1719 break;
1720 }
1721 }
1722 return Changed;
1723}
1724
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001725bool CopyPropagation::processBlock(MachineBasicBlock &B, const RegisterSet&) {
1726 std::vector<MachineInstr*> Instrs;
1727 for (auto I = B.rbegin(), E = B.rend(); I != E; ++I)
1728 Instrs.push_back(&*I);
1729
1730 bool Changed = false;
1731 for (auto I : Instrs) {
1732 unsigned Opc = I->getOpcode();
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +00001733 if (!CopyPropagation::isCopyReg(Opc, true))
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001734 continue;
1735 Changed |= propagateRegCopy(*I);
1736 }
1737
1738 return Changed;
1739}
1740
Eugene Zelenko82085922016-12-13 22:13:50 +00001741namespace {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001742
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001743// Recognize patterns that can be simplified and replace them with the
1744// simpler forms.
1745// This is by no means complete
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001746 class BitSimplification : public Transformation {
1747 public:
Krzysztof Parzyszek54421032017-03-09 22:02:14 +00001748 BitSimplification(BitTracker &bt, const MachineDominatorTree &mdt,
1749 const HexagonInstrInfo &hii, const HexagonRegisterInfo &hri,
1750 MachineRegisterInfo &mri, MachineFunction &mf)
1751 : Transformation(true), MDT(mdt), HII(hii), HRI(hri), MRI(mri),
1752 MF(mf), BT(bt) {}
Eugene Zelenko82085922016-12-13 22:13:50 +00001753
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001754 bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override;
Eugene Zelenko82085922016-12-13 22:13:50 +00001755
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001756 private:
1757 struct RegHalf : public BitTracker::RegisterRef {
1758 bool Low; // Low/High halfword.
1759 };
1760
1761 bool matchHalf(unsigned SelfR, const BitTracker::RegisterCell &RC,
1762 unsigned B, RegHalf &RH);
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00001763 bool validateReg(BitTracker::RegisterRef R, unsigned Opc, unsigned OpNum);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001764
1765 bool matchPackhl(unsigned SelfR, const BitTracker::RegisterCell &RC,
1766 BitTracker::RegisterRef &Rs, BitTracker::RegisterRef &Rt);
1767 unsigned getCombineOpcode(bool HLow, bool LLow);
1768
1769 bool genStoreUpperHalf(MachineInstr *MI);
1770 bool genStoreImmediate(MachineInstr *MI);
1771 bool genPackhl(MachineInstr *MI, BitTracker::RegisterRef RD,
1772 const BitTracker::RegisterCell &RC);
1773 bool genExtractHalf(MachineInstr *MI, BitTracker::RegisterRef RD,
1774 const BitTracker::RegisterCell &RC);
1775 bool genCombineHalf(MachineInstr *MI, BitTracker::RegisterRef RD,
1776 const BitTracker::RegisterCell &RC);
1777 bool genExtractLow(MachineInstr *MI, BitTracker::RegisterRef RD,
1778 const BitTracker::RegisterCell &RC);
Krzysztof Parzyszek8e4d2e02017-03-07 23:08:35 +00001779 bool genBitSplit(MachineInstr *MI, BitTracker::RegisterRef RD,
1780 const BitTracker::RegisterCell &RC, const RegisterSet &AVs);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001781 bool simplifyTstbit(MachineInstr *MI, BitTracker::RegisterRef RD,
1782 const BitTracker::RegisterCell &RC);
Krzysztof Parzyszek33fd0bb2017-02-28 23:27:33 +00001783 bool simplifyExtractLow(MachineInstr *MI, BitTracker::RegisterRef RD,
1784 const BitTracker::RegisterCell &RC, const RegisterSet &AVs);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001785
Krzysztof Parzyszek54421032017-03-09 22:02:14 +00001786 // Cache of created instructions to avoid creating duplicates.
1787 // XXX Currently only used by genBitSplit.
1788 std::vector<MachineInstr*> NewMIs;
1789
1790 const MachineDominatorTree &MDT;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001791 const HexagonInstrInfo &HII;
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00001792 const HexagonRegisterInfo &HRI;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001793 MachineRegisterInfo &MRI;
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00001794 MachineFunction &MF;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001795 BitTracker &BT;
1796 };
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001797
Eugene Zelenko82085922016-12-13 22:13:50 +00001798} // end anonymous namespace
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001799
1800// Check if the bits [B..B+16) in register cell RC form a valid halfword,
1801// i.e. [0..16), [16..32), etc. of some register. If so, return true and
1802// set the information about the found register in RH.
1803bool BitSimplification::matchHalf(unsigned SelfR,
1804 const BitTracker::RegisterCell &RC, unsigned B, RegHalf &RH) {
1805 // XXX This could be searching in the set of available registers, in case
1806 // the match is not exact.
1807
1808 // Match 16-bit chunks, where the RC[B..B+15] references exactly one
1809 // register and all the bits B..B+15 match between RC and the register.
1810 // This is meant to match "v1[0-15]", where v1 = { [0]:0 [1-15]:v1... },
1811 // and RC = { [0]:0 [1-15]:v1[1-15]... }.
1812 bool Low = false;
1813 unsigned I = B;
1814 while (I < B+16 && RC[I].num())
1815 I++;
1816 if (I == B+16)
1817 return false;
1818
1819 unsigned Reg = RC[I].RefI.Reg;
1820 unsigned P = RC[I].RefI.Pos; // The RefI.Pos will be advanced by I-B.
1821 if (P < I-B)
1822 return false;
1823 unsigned Pos = P - (I-B);
1824
1825 if (Reg == 0 || Reg == SelfR) // Don't match "self".
1826 return false;
1827 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1828 return false;
1829 if (!BT.has(Reg))
1830 return false;
1831
1832 const BitTracker::RegisterCell &SC = BT.lookup(Reg);
1833 if (Pos+16 > SC.width())
1834 return false;
1835
1836 for (unsigned i = 0; i < 16; ++i) {
1837 const BitTracker::BitValue &RV = RC[i+B];
1838 if (RV.Type == BitTracker::BitValue::Ref) {
1839 if (RV.RefI.Reg != Reg)
1840 return false;
1841 if (RV.RefI.Pos != i+Pos)
1842 return false;
1843 continue;
1844 }
1845 if (RC[i+B] != SC[i+Pos])
1846 return false;
1847 }
1848
1849 unsigned Sub = 0;
1850 switch (Pos) {
1851 case 0:
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001852 Sub = Hexagon::isub_lo;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001853 Low = true;
1854 break;
1855 case 16:
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001856 Sub = Hexagon::isub_lo;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001857 Low = false;
1858 break;
1859 case 32:
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001860 Sub = Hexagon::isub_hi;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001861 Low = true;
1862 break;
1863 case 48:
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001864 Sub = Hexagon::isub_hi;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001865 Low = false;
1866 break;
1867 default:
1868 return false;
1869 }
1870
1871 RH.Reg = Reg;
1872 RH.Sub = Sub;
1873 RH.Low = Low;
1874 // If the subregister is not valid with the register, set it to 0.
1875 if (!HBS::getFinalVRegClass(RH, MRI))
1876 RH.Sub = 0;
1877
1878 return true;
1879}
1880
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00001881bool BitSimplification::validateReg(BitTracker::RegisterRef R, unsigned Opc,
1882 unsigned OpNum) {
1883 auto *OpRC = HII.getRegClass(HII.get(Opc), OpNum, &HRI, MF);
1884 auto *RRC = HBS::getFinalVRegClass(R, MRI);
1885 return OpRC->hasSubClassEq(RRC);
1886}
1887
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001888// Check if RC matches the pattern of a S2_packhl. If so, return true and
1889// set the inputs Rs and Rt.
1890bool BitSimplification::matchPackhl(unsigned SelfR,
1891 const BitTracker::RegisterCell &RC, BitTracker::RegisterRef &Rs,
1892 BitTracker::RegisterRef &Rt) {
1893 RegHalf L1, H1, L2, H2;
1894
1895 if (!matchHalf(SelfR, RC, 0, L2) || !matchHalf(SelfR, RC, 16, L1))
1896 return false;
1897 if (!matchHalf(SelfR, RC, 32, H2) || !matchHalf(SelfR, RC, 48, H1))
1898 return false;
1899
1900 // Rs = H1.L1, Rt = H2.L2
1901 if (H1.Reg != L1.Reg || H1.Sub != L1.Sub || H1.Low || !L1.Low)
1902 return false;
1903 if (H2.Reg != L2.Reg || H2.Sub != L2.Sub || H2.Low || !L2.Low)
1904 return false;
1905
1906 Rs = H1;
1907 Rt = H2;
1908 return true;
1909}
1910
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001911unsigned BitSimplification::getCombineOpcode(bool HLow, bool LLow) {
1912 return HLow ? LLow ? Hexagon::A2_combine_ll
1913 : Hexagon::A2_combine_lh
1914 : LLow ? Hexagon::A2_combine_hl
1915 : Hexagon::A2_combine_hh;
1916}
1917
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001918// If MI stores the upper halfword of a register (potentially obtained via
1919// shifts or extracts), replace it with a storerf instruction. This could
1920// cause the "extraction" code to become dead.
1921bool BitSimplification::genStoreUpperHalf(MachineInstr *MI) {
1922 unsigned Opc = MI->getOpcode();
1923 if (Opc != Hexagon::S2_storerh_io)
1924 return false;
1925
1926 MachineOperand &ValOp = MI->getOperand(2);
1927 BitTracker::RegisterRef RS = ValOp;
1928 if (!BT.has(RS.Reg))
1929 return false;
1930 const BitTracker::RegisterCell &RC = BT.lookup(RS.Reg);
1931 RegHalf H;
1932 if (!matchHalf(0, RC, 0, H))
1933 return false;
1934 if (H.Low)
1935 return false;
1936 MI->setDesc(HII.get(Hexagon::S2_storerf_io));
1937 ValOp.setReg(H.Reg);
1938 ValOp.setSubReg(H.Sub);
1939 return true;
1940}
1941
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001942// If MI stores a value known at compile-time, and the value is within a range
1943// that avoids using constant-extenders, replace it with a store-immediate.
1944bool BitSimplification::genStoreImmediate(MachineInstr *MI) {
1945 unsigned Opc = MI->getOpcode();
1946 unsigned Align = 0;
1947 switch (Opc) {
1948 case Hexagon::S2_storeri_io:
1949 Align++;
1950 case Hexagon::S2_storerh_io:
1951 Align++;
1952 case Hexagon::S2_storerb_io:
1953 break;
1954 default:
1955 return false;
1956 }
1957
1958 // Avoid stores to frame-indices (due to an unknown offset).
1959 if (!MI->getOperand(0).isReg())
1960 return false;
1961 MachineOperand &OffOp = MI->getOperand(1);
1962 if (!OffOp.isImm())
1963 return false;
1964
1965 int64_t Off = OffOp.getImm();
1966 // Offset is u6:a. Sadly, there is no isShiftedUInt(n,x).
1967 if (!isUIntN(6+Align, Off) || (Off & ((1<<Align)-1)))
1968 return false;
1969 // Source register:
1970 BitTracker::RegisterRef RS = MI->getOperand(2);
1971 if (!BT.has(RS.Reg))
1972 return false;
1973 const BitTracker::RegisterCell &RC = BT.lookup(RS.Reg);
1974 uint64_t U;
1975 if (!HBS::getConst(RC, 0, RC.width(), U))
1976 return false;
1977
1978 // Only consider 8-bit values to avoid constant-extenders.
1979 int V;
1980 switch (Opc) {
1981 case Hexagon::S2_storerb_io:
1982 V = int8_t(U);
1983 break;
1984 case Hexagon::S2_storerh_io:
1985 V = int16_t(U);
1986 break;
1987 case Hexagon::S2_storeri_io:
1988 V = int32_t(U);
1989 break;
1990 }
1991 if (!isInt<8>(V))
1992 return false;
1993
1994 MI->RemoveOperand(2);
1995 switch (Opc) {
1996 case Hexagon::S2_storerb_io:
1997 MI->setDesc(HII.get(Hexagon::S4_storeirb_io));
1998 break;
1999 case Hexagon::S2_storerh_io:
2000 MI->setDesc(HII.get(Hexagon::S4_storeirh_io));
2001 break;
2002 case Hexagon::S2_storeri_io:
2003 MI->setDesc(HII.get(Hexagon::S4_storeiri_io));
2004 break;
2005 }
2006 MI->addOperand(MachineOperand::CreateImm(V));
2007 return true;
2008}
2009
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002010// If MI is equivalent o S2_packhl, generate the S2_packhl. MI could be the
2011// last instruction in a sequence that results in something equivalent to
2012// the pack-halfwords. The intent is to cause the entire sequence to become
2013// dead.
2014bool BitSimplification::genPackhl(MachineInstr *MI,
2015 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) {
2016 unsigned Opc = MI->getOpcode();
2017 if (Opc == Hexagon::S2_packhl)
2018 return false;
2019 BitTracker::RegisterRef Rs, Rt;
2020 if (!matchPackhl(RD.Reg, RC, Rs, Rt))
2021 return false;
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00002022 if (!validateReg(Rs, Hexagon::S2_packhl, 1) ||
2023 !validateReg(Rt, Hexagon::S2_packhl, 2))
2024 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002025
2026 MachineBasicBlock &B = *MI->getParent();
2027 unsigned NewR = MRI.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
2028 DebugLoc DL = MI->getDebugLoc();
Krzysztof Parzyszeka3c5d442016-01-13 15:48:18 +00002029 auto At = MI->isPHI() ? B.getFirstNonPHI()
2030 : MachineBasicBlock::iterator(MI);
2031 BuildMI(B, At, DL, HII.get(Hexagon::S2_packhl), NewR)
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002032 .addReg(Rs.Reg, 0, Rs.Sub)
2033 .addReg(Rt.Reg, 0, Rt.Sub);
2034 HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI);
2035 BT.put(BitTracker::RegisterRef(NewR), RC);
2036 return true;
2037}
2038
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002039// If MI produces halfword of the input in the low half of the output,
2040// replace it with zero-extend or extractu.
2041bool BitSimplification::genExtractHalf(MachineInstr *MI,
2042 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) {
2043 RegHalf L;
2044 // Check for halfword in low 16 bits, zeros elsewhere.
2045 if (!matchHalf(RD.Reg, RC, 0, L) || !HBS::isZero(RC, 16, 16))
2046 return false;
2047
2048 unsigned Opc = MI->getOpcode();
2049 MachineBasicBlock &B = *MI->getParent();
2050 DebugLoc DL = MI->getDebugLoc();
2051
2052 // Prefer zxth, since zxth can go in any slot, while extractu only in
2053 // slots 2 and 3.
2054 unsigned NewR = 0;
Krzysztof Parzyszeka3c5d442016-01-13 15:48:18 +00002055 auto At = MI->isPHI() ? B.getFirstNonPHI()
2056 : MachineBasicBlock::iterator(MI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002057 if (L.Low && Opc != Hexagon::A2_zxth) {
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00002058 if (validateReg(L, Hexagon::A2_zxth, 1)) {
2059 NewR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2060 BuildMI(B, At, DL, HII.get(Hexagon::A2_zxth), NewR)
2061 .addReg(L.Reg, 0, L.Sub);
2062 }
Krzysztof Parzyszek0d112122016-01-14 21:59:22 +00002063 } else if (!L.Low && Opc != Hexagon::S2_lsr_i_r) {
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00002064 if (validateReg(L, Hexagon::S2_lsr_i_r, 1)) {
2065 NewR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2066 BuildMI(B, MI, DL, HII.get(Hexagon::S2_lsr_i_r), NewR)
2067 .addReg(L.Reg, 0, L.Sub)
2068 .addImm(16);
2069 }
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002070 }
2071 if (NewR == 0)
2072 return false;
2073 HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI);
2074 BT.put(BitTracker::RegisterRef(NewR), RC);
2075 return true;
2076}
2077
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002078// If MI is equivalent to a combine(.L/.H, .L/.H) replace with with the
2079// combine.
2080bool BitSimplification::genCombineHalf(MachineInstr *MI,
2081 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) {
2082 RegHalf L, H;
2083 // Check for combine h/l
2084 if (!matchHalf(RD.Reg, RC, 0, L) || !matchHalf(RD.Reg, RC, 16, H))
2085 return false;
2086 // Do nothing if this is just a reg copy.
2087 if (L.Reg == H.Reg && L.Sub == H.Sub && !H.Low && L.Low)
2088 return false;
2089
2090 unsigned Opc = MI->getOpcode();
2091 unsigned COpc = getCombineOpcode(H.Low, L.Low);
2092 if (COpc == Opc)
2093 return false;
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00002094 if (!validateReg(H, COpc, 1) || !validateReg(L, COpc, 2))
2095 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002096
2097 MachineBasicBlock &B = *MI->getParent();
2098 DebugLoc DL = MI->getDebugLoc();
2099 unsigned NewR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
Krzysztof Parzyszeka3c5d442016-01-13 15:48:18 +00002100 auto At = MI->isPHI() ? B.getFirstNonPHI()
2101 : MachineBasicBlock::iterator(MI);
2102 BuildMI(B, At, DL, HII.get(COpc), NewR)
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002103 .addReg(H.Reg, 0, H.Sub)
2104 .addReg(L.Reg, 0, L.Sub);
2105 HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI);
2106 BT.put(BitTracker::RegisterRef(NewR), RC);
2107 return true;
2108}
2109
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002110// If MI resets high bits of a register and keeps the lower ones, replace it
2111// with zero-extend byte/half, and-immediate, or extractu, as appropriate.
2112bool BitSimplification::genExtractLow(MachineInstr *MI,
2113 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) {
2114 unsigned Opc = MI->getOpcode();
2115 switch (Opc) {
2116 case Hexagon::A2_zxtb:
2117 case Hexagon::A2_zxth:
2118 case Hexagon::S2_extractu:
2119 return false;
2120 }
2121 if (Opc == Hexagon::A2_andir && MI->getOperand(2).isImm()) {
2122 int32_t Imm = MI->getOperand(2).getImm();
2123 if (isInt<10>(Imm))
2124 return false;
2125 }
2126
2127 if (MI->hasUnmodeledSideEffects() || MI->isInlineAsm())
2128 return false;
2129 unsigned W = RC.width();
2130 while (W > 0 && RC[W-1].is(0))
2131 W--;
2132 if (W == 0 || W == RC.width())
2133 return false;
2134 unsigned NewOpc = (W == 8) ? Hexagon::A2_zxtb
2135 : (W == 16) ? Hexagon::A2_zxth
2136 : (W < 10) ? Hexagon::A2_andir
2137 : Hexagon::S2_extractu;
2138 MachineBasicBlock &B = *MI->getParent();
2139 DebugLoc DL = MI->getDebugLoc();
2140
2141 for (auto &Op : MI->uses()) {
2142 if (!Op.isReg())
2143 continue;
2144 BitTracker::RegisterRef RS = Op;
2145 if (!BT.has(RS.Reg))
2146 continue;
2147 const BitTracker::RegisterCell &SC = BT.lookup(RS.Reg);
2148 unsigned BN, BW;
2149 if (!HBS::getSubregMask(RS, BN, BW, MRI))
2150 continue;
2151 if (BW < W || !HBS::isEqual(RC, 0, SC, BN, W))
2152 continue;
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00002153 if (!validateReg(RS, NewOpc, 1))
2154 continue;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002155
2156 unsigned NewR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
Krzysztof Parzyszeka3c5d442016-01-13 15:48:18 +00002157 auto At = MI->isPHI() ? B.getFirstNonPHI()
2158 : MachineBasicBlock::iterator(MI);
2159 auto MIB = BuildMI(B, At, DL, HII.get(NewOpc), NewR)
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002160 .addReg(RS.Reg, 0, RS.Sub);
2161 if (NewOpc == Hexagon::A2_andir)
2162 MIB.addImm((1 << W) - 1);
2163 else if (NewOpc == Hexagon::S2_extractu)
2164 MIB.addImm(W).addImm(0);
2165 HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI);
2166 BT.put(BitTracker::RegisterRef(NewR), RC);
2167 return true;
2168 }
2169 return false;
2170}
2171
Krzysztof Parzyszek8e4d2e02017-03-07 23:08:35 +00002172bool BitSimplification::genBitSplit(MachineInstr *MI,
2173 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC,
2174 const RegisterSet &AVs) {
2175 if (!GenBitSplit)
2176 return false;
2177 if (CountBitSplit >= MaxBitSplit)
2178 return false;
2179
2180 unsigned Opc = MI->getOpcode();
2181 switch (Opc) {
2182 case Hexagon::A4_bitsplit:
2183 case Hexagon::A4_bitspliti:
2184 return false;
2185 }
2186
2187 unsigned W = RC.width();
2188 if (W != 32)
2189 return false;
2190
2191 auto ctlz = [] (const BitTracker::RegisterCell &C) -> unsigned {
2192 unsigned Z = C.width();
2193 while (Z > 0 && C[Z-1].is(0))
2194 --Z;
2195 return C.width() - Z;
2196 };
2197
2198 // Count the number of leading zeros in the target RC.
2199 unsigned Z = ctlz(RC);
2200 if (Z == 0 || Z == W)
2201 return false;
2202
2203 // A simplistic analysis: assume the source register (the one being split)
2204 // is fully unknown, and that all its bits are self-references.
2205 const BitTracker::BitValue &B0 = RC[0];
2206 if (B0.Type != BitTracker::BitValue::Ref)
2207 return false;
2208
2209 unsigned SrcR = B0.RefI.Reg;
2210 unsigned SrcSR = 0;
2211 unsigned Pos = B0.RefI.Pos;
2212
2213 // All the non-zero bits should be consecutive bits from the same register.
2214 for (unsigned i = 1; i < W-Z; ++i) {
2215 const BitTracker::BitValue &V = RC[i];
2216 if (V.Type != BitTracker::BitValue::Ref)
2217 return false;
2218 if (V.RefI.Reg != SrcR || V.RefI.Pos != Pos+i)
2219 return false;
2220 }
2221
2222 // Now, find the other bitfield among AVs.
2223 for (unsigned S = AVs.find_first(); S; S = AVs.find_next(S)) {
2224 // The number of leading zeros here should be the number of trailing
2225 // non-zeros in RC.
Krzysztof Parzyszek434d50a2017-03-07 23:12:04 +00002226 if (!BT.has(S))
2227 continue;
Krzysztof Parzyszek8e4d2e02017-03-07 23:08:35 +00002228 const BitTracker::RegisterCell &SC = BT.lookup(S);
2229 if (SC.width() != W || ctlz(SC) != W-Z)
2230 continue;
2231 // The Z lower bits should now match SrcR.
2232 const BitTracker::BitValue &S0 = SC[0];
2233 if (S0.Type != BitTracker::BitValue::Ref || S0.RefI.Reg != SrcR)
2234 continue;
2235 unsigned P = S0.RefI.Pos;
2236
2237 if (Pos <= P && (Pos + W-Z) != P)
2238 continue;
2239 if (P < Pos && (P + Z) != Pos)
2240 continue;
2241 // The starting bitfield position must be at a subregister boundary.
2242 if (std::min(P, Pos) != 0 && std::min(P, Pos) != 32)
2243 continue;
2244
2245 unsigned I;
2246 for (I = 1; I < Z; ++I) {
2247 const BitTracker::BitValue &V = SC[I];
2248 if (V.Type != BitTracker::BitValue::Ref)
2249 break;
2250 if (V.RefI.Reg != SrcR || V.RefI.Pos != P+I)
2251 break;
2252 }
2253 if (I != Z)
2254 continue;
2255
2256 // Generate bitsplit where S is defined.
2257 CountBitSplit++;
2258 MachineInstr *DefS = MRI.getVRegDef(S);
2259 assert(DefS != nullptr);
2260 DebugLoc DL = DefS->getDebugLoc();
2261 MachineBasicBlock &B = *DefS->getParent();
Krzysztof Parzyszek54421032017-03-09 22:02:14 +00002262 auto At = DefS->isPHI() ? B.getFirstNonPHI()
2263 : MachineBasicBlock::iterator(DefS);
Krzysztof Parzyszek8e4d2e02017-03-07 23:08:35 +00002264 if (MRI.getRegClass(SrcR)->getID() == Hexagon::DoubleRegsRegClassID)
2265 SrcSR = (std::min(Pos, P) == 32) ? Hexagon::isub_hi : Hexagon::isub_lo;
Krzysztof Parzyszek54421032017-03-09 22:02:14 +00002266 if (!validateReg({SrcR,SrcSR}, Hexagon::A4_bitspliti, 1))
2267 continue;
2268 unsigned ImmOp = Pos <= P ? W-Z : Z;
2269
2270 // Find an existing bitsplit instruction if one already exists.
2271 unsigned NewR = 0;
2272 for (MachineInstr *In : NewMIs) {
2273 if (In->getOpcode() != Hexagon::A4_bitspliti)
2274 continue;
2275 MachineOperand &Op1 = In->getOperand(1);
2276 if (Op1.getReg() != SrcR || Op1.getSubReg() != SrcSR)
2277 continue;
2278 if (In->getOperand(2).getImm() != ImmOp)
2279 continue;
2280 // Check if the target register is available here.
2281 MachineOperand &Op0 = In->getOperand(0);
2282 MachineInstr *DefI = MRI.getVRegDef(Op0.getReg());
2283 assert(DefI != nullptr);
2284 if (!MDT.dominates(DefI, &*At))
2285 continue;
2286
2287 // Found one that can be reused.
2288 assert(Op0.getSubReg() == 0);
2289 NewR = Op0.getReg();
2290 break;
2291 }
2292 if (!NewR) {
2293 NewR = MRI.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
2294 auto NewBS = BuildMI(B, At, DL, HII.get(Hexagon::A4_bitspliti), NewR)
2295 .addReg(SrcR, 0, SrcSR)
2296 .addImm(ImmOp);
2297 NewMIs.push_back(NewBS);
2298 }
Krzysztof Parzyszek8e4d2e02017-03-07 23:08:35 +00002299 if (Pos <= P) {
2300 HBS::replaceRegWithSub(RD.Reg, NewR, Hexagon::isub_lo, MRI);
2301 HBS::replaceRegWithSub(S, NewR, Hexagon::isub_hi, MRI);
2302 } else {
2303 HBS::replaceRegWithSub(S, NewR, Hexagon::isub_lo, MRI);
2304 HBS::replaceRegWithSub(RD.Reg, NewR, Hexagon::isub_hi, MRI);
2305 }
2306 return true;
2307 }
2308
2309 return false;
2310}
2311
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002312// Check for tstbit simplification opportunity, where the bit being checked
2313// can be tracked back to another register. For example:
2314// vreg2 = S2_lsr_i_r vreg1, 5
2315// vreg3 = S2_tstbit_i vreg2, 0
2316// =>
2317// vreg3 = S2_tstbit_i vreg1, 5
2318bool BitSimplification::simplifyTstbit(MachineInstr *MI,
2319 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) {
2320 unsigned Opc = MI->getOpcode();
2321 if (Opc != Hexagon::S2_tstbit_i)
2322 return false;
2323
2324 unsigned BN = MI->getOperand(2).getImm();
2325 BitTracker::RegisterRef RS = MI->getOperand(1);
2326 unsigned F, W;
2327 DebugLoc DL = MI->getDebugLoc();
2328 if (!BT.has(RS.Reg) || !HBS::getSubregMask(RS, F, W, MRI))
2329 return false;
2330 MachineBasicBlock &B = *MI->getParent();
Krzysztof Parzyszeka3c5d442016-01-13 15:48:18 +00002331 auto At = MI->isPHI() ? B.getFirstNonPHI()
2332 : MachineBasicBlock::iterator(MI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002333
2334 const BitTracker::RegisterCell &SC = BT.lookup(RS.Reg);
2335 const BitTracker::BitValue &V = SC[F+BN];
2336 if (V.Type == BitTracker::BitValue::Ref && V.RefI.Reg != RS.Reg) {
2337 const TargetRegisterClass *TC = MRI.getRegClass(V.RefI.Reg);
2338 // Need to map V.RefI.Reg to a 32-bit register, i.e. if it is
2339 // a double register, need to use a subregister and adjust bit
2340 // number.
Eugene Zelenko82085922016-12-13 22:13:50 +00002341 unsigned P = std::numeric_limits<unsigned>::max();
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002342 BitTracker::RegisterRef RR(V.RefI.Reg, 0);
2343 if (TC == &Hexagon::DoubleRegsRegClass) {
2344 P = V.RefI.Pos;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002345 RR.Sub = Hexagon::isub_lo;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002346 if (P >= 32) {
2347 P -= 32;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002348 RR.Sub = Hexagon::isub_hi;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002349 }
2350 } else if (TC == &Hexagon::IntRegsRegClass) {
2351 P = V.RefI.Pos;
2352 }
Eugene Zelenko82085922016-12-13 22:13:50 +00002353 if (P != std::numeric_limits<unsigned>::max()) {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002354 unsigned NewR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
Krzysztof Parzyszeka3c5d442016-01-13 15:48:18 +00002355 BuildMI(B, At, DL, HII.get(Hexagon::S2_tstbit_i), NewR)
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002356 .addReg(RR.Reg, 0, RR.Sub)
2357 .addImm(P);
2358 HBS::replaceReg(RD.Reg, NewR, MRI);
2359 BT.put(NewR, RC);
2360 return true;
2361 }
2362 } else if (V.is(0) || V.is(1)) {
2363 unsigned NewR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002364 unsigned NewOpc = V.is(0) ? Hexagon::PS_false : Hexagon::PS_true;
Krzysztof Parzyszeka3c5d442016-01-13 15:48:18 +00002365 BuildMI(B, At, DL, HII.get(NewOpc), NewR);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002366 HBS::replaceReg(RD.Reg, NewR, MRI);
2367 return true;
2368 }
2369
2370 return false;
2371}
2372
Krzysztof Parzyszek33fd0bb2017-02-28 23:27:33 +00002373// Detect whether RD is a bitfield extract (sign- or zero-extended) of
2374// some register from the AVs set. Create a new corresponding instruction
2375// at the location of MI. The intent is to recognize situations where
2376// a sequence of instructions performs an operation that is equivalent to
2377// an extract operation, such as a shift left followed by a shift right.
2378bool BitSimplification::simplifyExtractLow(MachineInstr *MI,
2379 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC,
2380 const RegisterSet &AVs) {
2381 if (!GenExtract)
2382 return false;
2383 if (CountExtract >= MaxExtract)
2384 return false;
2385 CountExtract++;
2386
2387 unsigned W = RC.width();
2388 unsigned RW = W;
2389 unsigned Len;
2390 bool Signed;
2391
2392 // The code is mostly class-independent, except for the part that generates
2393 // the extract instruction, and establishes the source register (in case it
2394 // needs to use a subregister).
2395 const TargetRegisterClass *FRC = HBS::getFinalVRegClass(RD, MRI);
2396 if (FRC != &Hexagon::IntRegsRegClass && FRC != &Hexagon::DoubleRegsRegClass)
2397 return false;
2398 assert(RD.Sub == 0);
2399
2400 // Observation:
2401 // If the cell has a form of 00..0xx..x with k zeros and n remaining
2402 // bits, this could be an extractu of the n bits, but it could also be
2403 // an extractu of a longer field which happens to have 0s in the top
2404 // bit positions.
2405 // The same logic applies to sign-extended fields.
2406 //
2407 // Do not check for the extended extracts, since it would expand the
2408 // search space quite a bit. The search may be expensive as it is.
2409
2410 const BitTracker::BitValue &TopV = RC[W-1];
2411
2412 // Eliminate candidates that have self-referential bits, since they
2413 // cannot be extracts from other registers. Also, skip registers that
2414 // have compile-time constant values.
2415 bool IsConst = true;
2416 for (unsigned I = 0; I != W; ++I) {
2417 const BitTracker::BitValue &V = RC[I];
2418 if (V.Type == BitTracker::BitValue::Ref && V.RefI.Reg == RD.Reg)
2419 return false;
2420 IsConst = IsConst && (V.is(0) || V.is(1));
2421 }
2422 if (IsConst)
2423 return false;
2424
2425 if (TopV.is(0) || TopV.is(1)) {
2426 bool S = TopV.is(1);
2427 for (--W; W > 0 && RC[W-1].is(S); --W)
2428 ;
2429 Len = W;
2430 Signed = S;
2431 // The sign bit must be a part of the field being extended.
2432 if (Signed)
2433 ++Len;
2434 } else {
2435 // This could still be a sign-extended extract.
2436 assert(TopV.Type == BitTracker::BitValue::Ref);
2437 if (TopV.RefI.Reg == RD.Reg || TopV.RefI.Pos == W-1)
2438 return false;
2439 for (--W; W > 0 && RC[W-1] == TopV; --W)
2440 ;
2441 // The top bits of RC are copies of TopV. One occurrence of TopV will
2442 // be a part of the field.
2443 Len = W + 1;
2444 Signed = true;
2445 }
2446
2447 // This would be just a copy. It should be handled elsewhere.
2448 if (Len == RW)
2449 return false;
2450
2451 DEBUG({
2452 dbgs() << __func__ << " on reg: " << PrintReg(RD.Reg, &HRI, RD.Sub)
2453 << ", MI: " << *MI;
2454 dbgs() << "Cell: " << RC << '\n';
2455 dbgs() << "Expected bitfield size: " << Len << " bits, "
2456 << (Signed ? "sign" : "zero") << "-extended\n";
2457 });
2458
2459 bool Changed = false;
2460
2461 for (unsigned R = AVs.find_first(); R != 0; R = AVs.find_next(R)) {
Krzysztof Parzyszek434d50a2017-03-07 23:12:04 +00002462 if (!BT.has(R))
2463 continue;
Krzysztof Parzyszek33fd0bb2017-02-28 23:27:33 +00002464 const BitTracker::RegisterCell &SC = BT.lookup(R);
2465 unsigned SW = SC.width();
2466
2467 // The source can be longer than the destination, as long as its size is
2468 // a multiple of the size of the destination. Also, we would need to be
2469 // able to refer to the subregister in the source that would be of the
2470 // same size as the destination, but only check the sizes here.
2471 if (SW < RW || (SW % RW) != 0)
2472 continue;
2473
2474 // The field can start at any offset in SC as long as it contains Len
2475 // bits and does not cross subregister boundary (if the source register
2476 // is longer than the destination).
2477 unsigned Off = 0;
2478 while (Off <= SW-Len) {
2479 unsigned OE = (Off+Len)/RW;
2480 if (OE != Off/RW) {
2481 // The assumption here is that if the source (R) is longer than the
2482 // destination, then the destination is a sequence of words of
2483 // size RW, and each such word in R can be accessed via a subregister.
2484 //
2485 // If the beginning and the end of the field cross the subregister
2486 // boundary, advance to the next subregister.
2487 Off = OE*RW;
2488 continue;
2489 }
2490 if (HBS::isEqual(RC, 0, SC, Off, Len))
2491 break;
2492 ++Off;
2493 }
2494
2495 if (Off > SW-Len)
2496 continue;
2497
2498 // Found match.
2499 unsigned ExtOpc = 0;
2500 if (Off == 0) {
2501 if (Len == 8)
2502 ExtOpc = Signed ? Hexagon::A2_sxtb : Hexagon::A2_zxtb;
2503 else if (Len == 16)
2504 ExtOpc = Signed ? Hexagon::A2_sxth : Hexagon::A2_zxth;
2505 else if (Len < 10 && !Signed)
2506 ExtOpc = Hexagon::A2_andir;
2507 }
2508 if (ExtOpc == 0) {
2509 ExtOpc =
2510 Signed ? (RW == 32 ? Hexagon::S4_extract : Hexagon::S4_extractp)
2511 : (RW == 32 ? Hexagon::S2_extractu : Hexagon::S2_extractup);
2512 }
2513 unsigned SR = 0;
2514 // This only recognizes isub_lo and isub_hi.
2515 if (RW != SW && RW*2 != SW)
2516 continue;
2517 if (RW != SW)
2518 SR = (Off/RW == 0) ? Hexagon::isub_lo : Hexagon::isub_hi;
Krzysztof Parzyszek1b7197e2017-03-08 15:46:28 +00002519 Off = Off % RW;
Krzysztof Parzyszek33fd0bb2017-02-28 23:27:33 +00002520
2521 if (!validateReg({R,SR}, ExtOpc, 1))
2522 continue;
2523
2524 // Don't generate the same instruction as the one being optimized.
2525 if (MI->getOpcode() == ExtOpc) {
2526 // All possible ExtOpc's have the source in operand(1).
2527 const MachineOperand &SrcOp = MI->getOperand(1);
2528 if (SrcOp.getReg() == R)
2529 continue;
2530 }
2531
2532 DebugLoc DL = MI->getDebugLoc();
2533 MachineBasicBlock &B = *MI->getParent();
2534 unsigned NewR = MRI.createVirtualRegister(FRC);
Krzysztof Parzyszek3cceffb2017-03-07 14:20:19 +00002535 auto At = MI->isPHI() ? B.getFirstNonPHI()
2536 : MachineBasicBlock::iterator(MI);
2537 auto MIB = BuildMI(B, At, DL, HII.get(ExtOpc), NewR)
Krzysztof Parzyszek33fd0bb2017-02-28 23:27:33 +00002538 .addReg(R, 0, SR);
2539 switch (ExtOpc) {
2540 case Hexagon::A2_sxtb:
2541 case Hexagon::A2_zxtb:
2542 case Hexagon::A2_sxth:
2543 case Hexagon::A2_zxth:
2544 break;
2545 case Hexagon::A2_andir:
2546 MIB.addImm((1u << Len) - 1);
2547 break;
2548 case Hexagon::S4_extract:
2549 case Hexagon::S2_extractu:
2550 case Hexagon::S4_extractp:
2551 case Hexagon::S2_extractup:
2552 MIB.addImm(Len)
2553 .addImm(Off);
2554 break;
2555 default:
2556 llvm_unreachable("Unexpected opcode");
2557 }
2558
2559 HBS::replaceReg(RD.Reg, NewR, MRI);
2560 BT.put(BitTracker::RegisterRef(NewR), RC);
2561 Changed = true;
2562 break;
2563 }
2564
2565 return Changed;
2566}
2567
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002568bool BitSimplification::processBlock(MachineBasicBlock &B,
2569 const RegisterSet &AVs) {
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00002570 if (!BT.reached(&B))
2571 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002572 bool Changed = false;
2573 RegisterSet AVB = AVs;
2574 RegisterSet Defs;
2575
2576 for (auto I = B.begin(), E = B.end(); I != E; ++I, AVB.insert(Defs)) {
2577 MachineInstr *MI = &*I;
2578 Defs.clear();
2579 HBS::getInstrDefs(*MI, Defs);
2580
2581 unsigned Opc = MI->getOpcode();
2582 if (Opc == TargetOpcode::COPY || Opc == TargetOpcode::REG_SEQUENCE)
2583 continue;
2584
2585 if (MI->mayStore()) {
2586 bool T = genStoreUpperHalf(MI);
2587 T = T || genStoreImmediate(MI);
2588 Changed |= T;
2589 continue;
2590 }
2591
2592 if (Defs.count() != 1)
2593 continue;
2594 const MachineOperand &Op0 = MI->getOperand(0);
2595 if (!Op0.isReg() || !Op0.isDef())
2596 continue;
2597 BitTracker::RegisterRef RD = Op0;
2598 if (!BT.has(RD.Reg))
2599 continue;
2600 const TargetRegisterClass *FRC = HBS::getFinalVRegClass(RD, MRI);
2601 const BitTracker::RegisterCell &RC = BT.lookup(RD.Reg);
2602
2603 if (FRC->getID() == Hexagon::DoubleRegsRegClassID) {
2604 bool T = genPackhl(MI, RD, RC);
Krzysztof Parzyszek33fd0bb2017-02-28 23:27:33 +00002605 T = T || simplifyExtractLow(MI, RD, RC, AVB);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002606 Changed |= T;
2607 continue;
2608 }
2609
2610 if (FRC->getID() == Hexagon::IntRegsRegClassID) {
Krzysztof Parzyszek8e4d2e02017-03-07 23:08:35 +00002611 bool T = genBitSplit(MI, RD, RC, AVB);
2612 T = T || simplifyExtractLow(MI, RD, RC, AVB);
Krzysztof Parzyszek33fd0bb2017-02-28 23:27:33 +00002613 T = T || genExtractHalf(MI, RD, RC);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002614 T = T || genCombineHalf(MI, RD, RC);
2615 T = T || genExtractLow(MI, RD, RC);
2616 Changed |= T;
2617 continue;
2618 }
2619
2620 if (FRC->getID() == Hexagon::PredRegsRegClassID) {
2621 bool T = simplifyTstbit(MI, RD, RC);
2622 Changed |= T;
2623 continue;
2624 }
2625 }
2626 return Changed;
2627}
2628
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002629bool HexagonBitSimplify::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +00002630 if (skipFunction(*MF.getFunction()))
2631 return false;
2632
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002633 auto &HST = MF.getSubtarget<HexagonSubtarget>();
2634 auto &HRI = *HST.getRegisterInfo();
2635 auto &HII = *HST.getInstrInfo();
2636
2637 MDT = &getAnalysis<MachineDominatorTree>();
2638 MachineRegisterInfo &MRI = MF.getRegInfo();
2639 bool Changed;
2640
2641 Changed = DeadCodeElimination(MF, *MDT).run();
2642
2643 const HexagonEvaluator HE(HRI, MRI, HII, MF);
2644 BitTracker BT(HE, MF);
2645 DEBUG(BT.trace(true));
2646 BT.run();
2647
2648 MachineBasicBlock &Entry = MF.front();
2649
2650 RegisterSet AIG; // Available registers for IG.
2651 ConstGeneration ImmG(BT, HII, MRI);
2652 Changed |= visitBlock(Entry, ImmG, AIG);
2653
2654 RegisterSet ARE; // Available registers for RIE.
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00002655 RedundantInstrElimination RIE(BT, HII, HRI, MRI);
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00002656 bool Ried = visitBlock(Entry, RIE, ARE);
2657 if (Ried) {
2658 Changed = true;
2659 BT.run();
2660 }
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002661
2662 RegisterSet ACG; // Available registers for CG.
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002663 CopyGeneration CopyG(BT, HII, HRI, MRI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002664 Changed |= visitBlock(Entry, CopyG, ACG);
2665
2666 RegisterSet ACP; // Available registers for CP.
2667 CopyPropagation CopyP(HRI, MRI);
2668 Changed |= visitBlock(Entry, CopyP, ACP);
2669
2670 Changed = DeadCodeElimination(MF, *MDT).run() || Changed;
2671
2672 BT.run();
2673 RegisterSet ABS; // Available registers for BS.
Krzysztof Parzyszek54421032017-03-09 22:02:14 +00002674 BitSimplification BitS(BT, *MDT, HII, HRI, MRI, MF);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002675 Changed |= visitBlock(Entry, BitS, ABS);
2676
2677 Changed = DeadCodeElimination(MF, *MDT).run() || Changed;
2678
2679 if (Changed) {
2680 for (auto &B : MF)
2681 for (auto &I : B)
2682 I.clearKillInfo();
2683 DeadCodeElimination(MF, *MDT).run();
2684 }
2685 return Changed;
2686}
2687
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002688// Recognize loops where the code at the end of the loop matches the code
2689// before the entry of the loop, and the matching code is such that is can
2690// be simplified. This pass relies on the bit simplification above and only
2691// prepares code in a way that can be handled by the bit simplifcation.
2692//
2693// This is the motivating testcase (and explanation):
2694//
2695// {
2696// loop0(.LBB0_2, r1) // %for.body.preheader
2697// r5:4 = memd(r0++#8)
2698// }
2699// {
2700// r3 = lsr(r4, #16)
2701// r7:6 = combine(r5, r5)
2702// }
2703// {
2704// r3 = insert(r5, #16, #16)
2705// r7:6 = vlsrw(r7:6, #16)
2706// }
2707// .LBB0_2:
2708// {
2709// memh(r2+#4) = r5
2710// memh(r2+#6) = r6 # R6 is really R5.H
2711// }
2712// {
2713// r2 = add(r2, #8)
2714// memh(r2+#0) = r4
2715// memh(r2+#2) = r3 # R3 is really R4.H
2716// }
2717// {
2718// r5:4 = memd(r0++#8)
2719// }
2720// { # "Shuffling" code that sets up R3 and R6
2721// r3 = lsr(r4, #16) # so that their halves can be stored in the
2722// r7:6 = combine(r5, r5) # next iteration. This could be folded into
2723// } # the stores if the code was at the beginning
2724// { # of the loop iteration. Since the same code
2725// r3 = insert(r5, #16, #16) # precedes the loop, it can actually be moved
2726// r7:6 = vlsrw(r7:6, #16) # there.
2727// }:endloop0
2728//
2729//
2730// The outcome:
2731//
2732// {
2733// loop0(.LBB0_2, r1)
2734// r5:4 = memd(r0++#8)
2735// }
2736// .LBB0_2:
2737// {
2738// memh(r2+#4) = r5
2739// memh(r2+#6) = r5.h
2740// }
2741// {
2742// r2 = add(r2, #8)
2743// memh(r2+#0) = r4
2744// memh(r2+#2) = r4.h
2745// }
2746// {
2747// r5:4 = memd(r0++#8)
2748// }:endloop0
2749
2750namespace llvm {
Eugene Zelenko82085922016-12-13 22:13:50 +00002751
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002752 FunctionPass *createHexagonLoopRescheduling();
2753 void initializeHexagonLoopReschedulingPass(PassRegistry&);
Eugene Zelenko82085922016-12-13 22:13:50 +00002754
2755} // end namespace llvm
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002756
2757namespace {
Eugene Zelenko82085922016-12-13 22:13:50 +00002758
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002759 class HexagonLoopRescheduling : public MachineFunctionPass {
2760 public:
2761 static char ID;
Eugene Zelenko82085922016-12-13 22:13:50 +00002762
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002763 HexagonLoopRescheduling() : MachineFunctionPass(ID),
Eugene Zelenko82085922016-12-13 22:13:50 +00002764 HII(nullptr), HRI(nullptr), MRI(nullptr), BTP(nullptr) {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002765 initializeHexagonLoopReschedulingPass(*PassRegistry::getPassRegistry());
2766 }
2767
2768 bool runOnMachineFunction(MachineFunction &MF) override;
2769
2770 private:
2771 const HexagonInstrInfo *HII;
2772 const HexagonRegisterInfo *HRI;
2773 MachineRegisterInfo *MRI;
2774 BitTracker *BTP;
2775
2776 struct LoopCand {
2777 LoopCand(MachineBasicBlock *lb, MachineBasicBlock *pb,
2778 MachineBasicBlock *eb) : LB(lb), PB(pb), EB(eb) {}
2779 MachineBasicBlock *LB, *PB, *EB;
2780 };
2781 typedef std::vector<MachineInstr*> InstrList;
2782 struct InstrGroup {
2783 BitTracker::RegisterRef Inp, Out;
2784 InstrList Ins;
2785 };
2786 struct PhiInfo {
2787 PhiInfo(MachineInstr &P, MachineBasicBlock &B);
2788 unsigned DefR;
Krzysztof Parzyszek57c3ddd2016-07-26 19:17:13 +00002789 BitTracker::RegisterRef LR, PR; // Loop Register, Preheader Register
2790 MachineBasicBlock *LB, *PB; // Loop Block, Preheader Block
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002791 };
2792
2793 static unsigned getDefReg(const MachineInstr *MI);
2794 bool isConst(unsigned Reg) const;
2795 bool isBitShuffle(const MachineInstr *MI, unsigned DefR) const;
2796 bool isStoreInput(const MachineInstr *MI, unsigned DefR) const;
2797 bool isShuffleOf(unsigned OutR, unsigned InpR) const;
2798 bool isSameShuffle(unsigned OutR1, unsigned InpR1, unsigned OutR2,
2799 unsigned &InpR2) const;
2800 void moveGroup(InstrGroup &G, MachineBasicBlock &LB, MachineBasicBlock &PB,
2801 MachineBasicBlock::iterator At, unsigned OldPhiR, unsigned NewPredR);
2802 bool processLoop(LoopCand &C);
2803 };
Eugene Zelenko82085922016-12-13 22:13:50 +00002804
2805} // end anonymous namespace
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002806
2807char HexagonLoopRescheduling::ID = 0;
2808
2809INITIALIZE_PASS(HexagonLoopRescheduling, "hexagon-loop-resched",
2810 "Hexagon Loop Rescheduling", false, false)
2811
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002812HexagonLoopRescheduling::PhiInfo::PhiInfo(MachineInstr &P,
2813 MachineBasicBlock &B) {
2814 DefR = HexagonLoopRescheduling::getDefReg(&P);
2815 LB = &B;
2816 PB = nullptr;
2817 for (unsigned i = 1, n = P.getNumOperands(); i < n; i += 2) {
2818 const MachineOperand &OpB = P.getOperand(i+1);
2819 if (OpB.getMBB() == &B) {
2820 LR = P.getOperand(i);
2821 continue;
2822 }
2823 PB = OpB.getMBB();
2824 PR = P.getOperand(i);
2825 }
2826}
2827
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002828unsigned HexagonLoopRescheduling::getDefReg(const MachineInstr *MI) {
2829 RegisterSet Defs;
2830 HBS::getInstrDefs(*MI, Defs);
2831 if (Defs.count() != 1)
2832 return 0;
2833 return Defs.find_first();
2834}
2835
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002836bool HexagonLoopRescheduling::isConst(unsigned Reg) const {
2837 if (!BTP->has(Reg))
2838 return false;
2839 const BitTracker::RegisterCell &RC = BTP->lookup(Reg);
2840 for (unsigned i = 0, w = RC.width(); i < w; ++i) {
2841 const BitTracker::BitValue &V = RC[i];
2842 if (!V.is(0) && !V.is(1))
2843 return false;
2844 }
2845 return true;
2846}
2847
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002848bool HexagonLoopRescheduling::isBitShuffle(const MachineInstr *MI,
2849 unsigned DefR) const {
2850 unsigned Opc = MI->getOpcode();
2851 switch (Opc) {
2852 case TargetOpcode::COPY:
2853 case Hexagon::S2_lsr_i_r:
2854 case Hexagon::S2_asr_i_r:
2855 case Hexagon::S2_asl_i_r:
2856 case Hexagon::S2_lsr_i_p:
2857 case Hexagon::S2_asr_i_p:
2858 case Hexagon::S2_asl_i_p:
2859 case Hexagon::S2_insert:
2860 case Hexagon::A2_or:
2861 case Hexagon::A2_orp:
2862 case Hexagon::A2_and:
2863 case Hexagon::A2_andp:
2864 case Hexagon::A2_combinew:
2865 case Hexagon::A4_combineri:
2866 case Hexagon::A4_combineir:
2867 case Hexagon::A2_combineii:
2868 case Hexagon::A4_combineii:
2869 case Hexagon::A2_combine_ll:
2870 case Hexagon::A2_combine_lh:
2871 case Hexagon::A2_combine_hl:
2872 case Hexagon::A2_combine_hh:
2873 return true;
2874 }
2875 return false;
2876}
2877
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002878bool HexagonLoopRescheduling::isStoreInput(const MachineInstr *MI,
2879 unsigned InpR) const {
2880 for (unsigned i = 0, n = MI->getNumOperands(); i < n; ++i) {
2881 const MachineOperand &Op = MI->getOperand(i);
2882 if (!Op.isReg())
2883 continue;
2884 if (Op.getReg() == InpR)
2885 return i == n-1;
2886 }
2887 return false;
2888}
2889
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002890bool HexagonLoopRescheduling::isShuffleOf(unsigned OutR, unsigned InpR) const {
2891 if (!BTP->has(OutR) || !BTP->has(InpR))
2892 return false;
2893 const BitTracker::RegisterCell &OutC = BTP->lookup(OutR);
2894 for (unsigned i = 0, w = OutC.width(); i < w; ++i) {
2895 const BitTracker::BitValue &V = OutC[i];
2896 if (V.Type != BitTracker::BitValue::Ref)
2897 continue;
2898 if (V.RefI.Reg != InpR)
2899 return false;
2900 }
2901 return true;
2902}
2903
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002904bool HexagonLoopRescheduling::isSameShuffle(unsigned OutR1, unsigned InpR1,
2905 unsigned OutR2, unsigned &InpR2) const {
2906 if (!BTP->has(OutR1) || !BTP->has(InpR1) || !BTP->has(OutR2))
2907 return false;
2908 const BitTracker::RegisterCell &OutC1 = BTP->lookup(OutR1);
2909 const BitTracker::RegisterCell &OutC2 = BTP->lookup(OutR2);
2910 unsigned W = OutC1.width();
2911 unsigned MatchR = 0;
2912 if (W != OutC2.width())
2913 return false;
2914 for (unsigned i = 0; i < W; ++i) {
2915 const BitTracker::BitValue &V1 = OutC1[i], &V2 = OutC2[i];
2916 if (V1.Type != V2.Type || V1.Type == BitTracker::BitValue::One)
2917 return false;
2918 if (V1.Type != BitTracker::BitValue::Ref)
2919 continue;
2920 if (V1.RefI.Pos != V2.RefI.Pos)
2921 return false;
2922 if (V1.RefI.Reg != InpR1)
2923 return false;
2924 if (V2.RefI.Reg == 0 || V2.RefI.Reg == OutR2)
2925 return false;
2926 if (!MatchR)
2927 MatchR = V2.RefI.Reg;
2928 else if (V2.RefI.Reg != MatchR)
2929 return false;
2930 }
2931 InpR2 = MatchR;
2932 return true;
2933}
2934
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002935void HexagonLoopRescheduling::moveGroup(InstrGroup &G, MachineBasicBlock &LB,
2936 MachineBasicBlock &PB, MachineBasicBlock::iterator At, unsigned OldPhiR,
2937 unsigned NewPredR) {
2938 DenseMap<unsigned,unsigned> RegMap;
2939
2940 const TargetRegisterClass *PhiRC = MRI->getRegClass(NewPredR);
2941 unsigned PhiR = MRI->createVirtualRegister(PhiRC);
2942 BuildMI(LB, At, At->getDebugLoc(), HII->get(TargetOpcode::PHI), PhiR)
2943 .addReg(NewPredR)
2944 .addMBB(&PB)
2945 .addReg(G.Inp.Reg)
2946 .addMBB(&LB);
2947 RegMap.insert(std::make_pair(G.Inp.Reg, PhiR));
2948
2949 for (unsigned i = G.Ins.size(); i > 0; --i) {
2950 const MachineInstr *SI = G.Ins[i-1];
2951 unsigned DR = getDefReg(SI);
2952 const TargetRegisterClass *RC = MRI->getRegClass(DR);
2953 unsigned NewDR = MRI->createVirtualRegister(RC);
2954 DebugLoc DL = SI->getDebugLoc();
2955
2956 auto MIB = BuildMI(LB, At, DL, HII->get(SI->getOpcode()), NewDR);
2957 for (unsigned j = 0, m = SI->getNumOperands(); j < m; ++j) {
2958 const MachineOperand &Op = SI->getOperand(j);
2959 if (!Op.isReg()) {
Diana Picus116bbab2017-01-13 09:58:52 +00002960 MIB.add(Op);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002961 continue;
2962 }
2963 if (!Op.isUse())
2964 continue;
2965 unsigned UseR = RegMap[Op.getReg()];
2966 MIB.addReg(UseR, 0, Op.getSubReg());
2967 }
2968 RegMap.insert(std::make_pair(DR, NewDR));
2969 }
2970
2971 HBS::replaceReg(OldPhiR, RegMap[G.Out.Reg], *MRI);
2972}
2973
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002974bool HexagonLoopRescheduling::processLoop(LoopCand &C) {
2975 DEBUG(dbgs() << "Processing loop in BB#" << C.LB->getNumber() << "\n");
2976 std::vector<PhiInfo> Phis;
2977 for (auto &I : *C.LB) {
2978 if (!I.isPHI())
2979 break;
2980 unsigned PR = getDefReg(&I);
2981 if (isConst(PR))
2982 continue;
2983 bool BadUse = false, GoodUse = false;
2984 for (auto UI = MRI->use_begin(PR), UE = MRI->use_end(); UI != UE; ++UI) {
2985 MachineInstr *UseI = UI->getParent();
2986 if (UseI->getParent() != C.LB) {
2987 BadUse = true;
2988 break;
2989 }
2990 if (isBitShuffle(UseI, PR) || isStoreInput(UseI, PR))
2991 GoodUse = true;
2992 }
2993 if (BadUse || !GoodUse)
2994 continue;
2995
2996 Phis.push_back(PhiInfo(I, *C.LB));
2997 }
2998
2999 DEBUG({
3000 dbgs() << "Phis: {";
3001 for (auto &I : Phis) {
3002 dbgs() << ' ' << PrintReg(I.DefR, HRI) << "=phi("
3003 << PrintReg(I.PR.Reg, HRI, I.PR.Sub) << ":b" << I.PB->getNumber()
3004 << ',' << PrintReg(I.LR.Reg, HRI, I.LR.Sub) << ":b"
3005 << I.LB->getNumber() << ')';
3006 }
3007 dbgs() << " }\n";
3008 });
3009
3010 if (Phis.empty())
3011 return false;
3012
3013 bool Changed = false;
3014 InstrList ShufIns;
3015
3016 // Go backwards in the block: for each bit shuffling instruction, check
3017 // if that instruction could potentially be moved to the front of the loop:
3018 // the output of the loop cannot be used in a non-shuffling instruction
3019 // in this loop.
3020 for (auto I = C.LB->rbegin(), E = C.LB->rend(); I != E; ++I) {
3021 if (I->isTerminator())
3022 continue;
3023 if (I->isPHI())
3024 break;
3025
3026 RegisterSet Defs;
3027 HBS::getInstrDefs(*I, Defs);
3028 if (Defs.count() != 1)
3029 continue;
3030 unsigned DefR = Defs.find_first();
3031 if (!TargetRegisterInfo::isVirtualRegister(DefR))
3032 continue;
3033 if (!isBitShuffle(&*I, DefR))
3034 continue;
3035
3036 bool BadUse = false;
3037 for (auto UI = MRI->use_begin(DefR), UE = MRI->use_end(); UI != UE; ++UI) {
3038 MachineInstr *UseI = UI->getParent();
3039 if (UseI->getParent() == C.LB) {
3040 if (UseI->isPHI()) {
3041 // If the use is in a phi node in this loop, then it should be
3042 // the value corresponding to the back edge.
3043 unsigned Idx = UI.getOperandNo();
3044 if (UseI->getOperand(Idx+1).getMBB() != C.LB)
3045 BadUse = true;
3046 } else {
David Majnemer0d955d02016-08-11 22:21:41 +00003047 auto F = find(ShufIns, UseI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00003048 if (F == ShufIns.end())
3049 BadUse = true;
3050 }
3051 } else {
3052 // There is a use outside of the loop, but there is no epilog block
3053 // suitable for a copy-out.
3054 if (C.EB == nullptr)
3055 BadUse = true;
3056 }
3057 if (BadUse)
3058 break;
3059 }
3060
3061 if (BadUse)
3062 continue;
3063 ShufIns.push_back(&*I);
3064 }
3065
3066 // Partition the list of shuffling instructions into instruction groups,
3067 // where each group has to be moved as a whole (i.e. a group is a chain of
3068 // dependent instructions). A group produces a single live output register,
3069 // which is meant to be the input of the loop phi node (although this is
3070 // not checked here yet). It also uses a single register as its input,
3071 // which is some value produced in the loop body. After moving the group
3072 // to the beginning of the loop, that input register would need to be
3073 // the loop-carried register (through a phi node) instead of the (currently
3074 // loop-carried) output register.
3075 typedef std::vector<InstrGroup> InstrGroupList;
3076 InstrGroupList Groups;
3077
3078 for (unsigned i = 0, n = ShufIns.size(); i < n; ++i) {
3079 MachineInstr *SI = ShufIns[i];
3080 if (SI == nullptr)
3081 continue;
3082
3083 InstrGroup G;
3084 G.Ins.push_back(SI);
3085 G.Out.Reg = getDefReg(SI);
3086 RegisterSet Inputs;
3087 HBS::getInstrUses(*SI, Inputs);
3088
3089 for (unsigned j = i+1; j < n; ++j) {
3090 MachineInstr *MI = ShufIns[j];
3091 if (MI == nullptr)
3092 continue;
3093 RegisterSet Defs;
3094 HBS::getInstrDefs(*MI, Defs);
3095 // If this instruction does not define any pending inputs, skip it.
3096 if (!Defs.intersects(Inputs))
3097 continue;
3098 // Otherwise, add it to the current group and remove the inputs that
3099 // are defined by MI.
3100 G.Ins.push_back(MI);
3101 Inputs.remove(Defs);
3102 // Then add all registers used by MI.
3103 HBS::getInstrUses(*MI, Inputs);
3104 ShufIns[j] = nullptr;
3105 }
3106
3107 // Only add a group if it requires at most one register.
3108 if (Inputs.count() > 1)
3109 continue;
3110 auto LoopInpEq = [G] (const PhiInfo &P) -> bool {
3111 return G.Out.Reg == P.LR.Reg;
3112 };
Eugene Zelenko82085922016-12-13 22:13:50 +00003113 if (llvm::find_if(Phis, LoopInpEq) == Phis.end())
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00003114 continue;
3115
3116 G.Inp.Reg = Inputs.find_first();
3117 Groups.push_back(G);
3118 }
3119
3120 DEBUG({
3121 for (unsigned i = 0, n = Groups.size(); i < n; ++i) {
3122 InstrGroup &G = Groups[i];
3123 dbgs() << "Group[" << i << "] inp: "
3124 << PrintReg(G.Inp.Reg, HRI, G.Inp.Sub)
3125 << " out: " << PrintReg(G.Out.Reg, HRI, G.Out.Sub) << "\n";
3126 for (unsigned j = 0, m = G.Ins.size(); j < m; ++j)
3127 dbgs() << " " << *G.Ins[j];
3128 }
3129 });
3130
3131 for (unsigned i = 0, n = Groups.size(); i < n; ++i) {
3132 InstrGroup &G = Groups[i];
3133 if (!isShuffleOf(G.Out.Reg, G.Inp.Reg))
3134 continue;
3135 auto LoopInpEq = [G] (const PhiInfo &P) -> bool {
3136 return G.Out.Reg == P.LR.Reg;
3137 };
Eugene Zelenko82085922016-12-13 22:13:50 +00003138 auto F = llvm::find_if(Phis, LoopInpEq);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00003139 if (F == Phis.end())
3140 continue;
Krzysztof Parzyszek57c3ddd2016-07-26 19:17:13 +00003141 unsigned PrehR = 0;
3142 if (!isSameShuffle(G.Out.Reg, G.Inp.Reg, F->PR.Reg, PrehR)) {
3143 const MachineInstr *DefPrehR = MRI->getVRegDef(F->PR.Reg);
3144 unsigned Opc = DefPrehR->getOpcode();
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00003145 if (Opc != Hexagon::A2_tfrsi && Opc != Hexagon::A2_tfrpi)
3146 continue;
Krzysztof Parzyszek57c3ddd2016-07-26 19:17:13 +00003147 if (!DefPrehR->getOperand(1).isImm())
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00003148 continue;
Krzysztof Parzyszek57c3ddd2016-07-26 19:17:13 +00003149 if (DefPrehR->getOperand(1).getImm() != 0)
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00003150 continue;
3151 const TargetRegisterClass *RC = MRI->getRegClass(G.Inp.Reg);
3152 if (RC != MRI->getRegClass(F->PR.Reg)) {
Krzysztof Parzyszek57c3ddd2016-07-26 19:17:13 +00003153 PrehR = MRI->createVirtualRegister(RC);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00003154 unsigned TfrI = (RC == &Hexagon::IntRegsRegClass) ? Hexagon::A2_tfrsi
3155 : Hexagon::A2_tfrpi;
3156 auto T = C.PB->getFirstTerminator();
3157 DebugLoc DL = (T != C.PB->end()) ? T->getDebugLoc() : DebugLoc();
Krzysztof Parzyszek57c3ddd2016-07-26 19:17:13 +00003158 BuildMI(*C.PB, T, DL, HII->get(TfrI), PrehR)
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00003159 .addImm(0);
3160 } else {
Krzysztof Parzyszek57c3ddd2016-07-26 19:17:13 +00003161 PrehR = F->PR.Reg;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00003162 }
3163 }
Krzysztof Parzyszek57c3ddd2016-07-26 19:17:13 +00003164 // isSameShuffle could match with PrehR being of a wider class than
3165 // G.Inp.Reg, for example if G shuffles the low 32 bits of its input,
3166 // it would match for the input being a 32-bit register, and PrehR
3167 // being a 64-bit register (where the low 32 bits match). This could
3168 // be handled, but for now skip these cases.
3169 if (MRI->getRegClass(PrehR) != MRI->getRegClass(G.Inp.Reg))
3170 continue;
3171 moveGroup(G, *F->LB, *F->PB, F->LB->getFirstNonPHI(), F->DefR, PrehR);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00003172 Changed = true;
3173 }
3174
3175 return Changed;
3176}
3177
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00003178bool HexagonLoopRescheduling::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +00003179 if (skipFunction(*MF.getFunction()))
3180 return false;
3181
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00003182 auto &HST = MF.getSubtarget<HexagonSubtarget>();
3183 HII = HST.getInstrInfo();
3184 HRI = HST.getRegisterInfo();
3185 MRI = &MF.getRegInfo();
3186 const HexagonEvaluator HE(*HRI, *MRI, *HII, MF);
3187 BitTracker BT(HE, MF);
3188 DEBUG(BT.trace(true));
3189 BT.run();
3190 BTP = &BT;
3191
3192 std::vector<LoopCand> Cand;
3193
3194 for (auto &B : MF) {
3195 if (B.pred_size() != 2 || B.succ_size() != 2)
3196 continue;
3197 MachineBasicBlock *PB = nullptr;
3198 bool IsLoop = false;
3199 for (auto PI = B.pred_begin(), PE = B.pred_end(); PI != PE; ++PI) {
3200 if (*PI != &B)
3201 PB = *PI;
3202 else
3203 IsLoop = true;
3204 }
3205 if (!IsLoop)
3206 continue;
3207
3208 MachineBasicBlock *EB = nullptr;
3209 for (auto SI = B.succ_begin(), SE = B.succ_end(); SI != SE; ++SI) {
3210 if (*SI == &B)
3211 continue;
3212 // Set EP to the epilog block, if it has only 1 predecessor (i.e. the
3213 // edge from B to EP is non-critical.
3214 if ((*SI)->pred_size() == 1)
3215 EB = *SI;
3216 break;
3217 }
3218
3219 Cand.push_back(LoopCand(&B, PB, EB));
3220 }
3221
3222 bool Changed = false;
3223 for (auto &C : Cand)
3224 Changed |= processLoop(C);
3225
3226 return Changed;
3227}
3228
3229//===----------------------------------------------------------------------===//
3230// Public Constructor Functions
3231//===----------------------------------------------------------------------===//
3232
3233FunctionPass *llvm::createHexagonLoopRescheduling() {
3234 return new HexagonLoopRescheduling();
3235}
3236
3237FunctionPass *llvm::createHexagonBitSimplify() {
3238 return new HexagonBitSimplify();
3239}