blob: 063aaa7af3da56ec4b1c6b1074b0add0d540644a [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"));
49
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000050namespace llvm {
Eugene Zelenko82085922016-12-13 22:13:50 +000051
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000052 void initializeHexagonBitSimplifyPass(PassRegistry& Registry);
53 FunctionPass *createHexagonBitSimplify();
Eugene Zelenko82085922016-12-13 22:13:50 +000054
55} // end namespace llvm
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000056
57namespace {
Eugene Zelenko82085922016-12-13 22:13:50 +000058
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000059 // Set of virtual registers, based on BitVector.
60 struct RegisterSet : private BitVector {
Eugene Zelenko82085922016-12-13 22:13:50 +000061 RegisterSet() = default;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000062 explicit RegisterSet(unsigned s, bool t = false) : BitVector(s, t) {}
Eugene Zelenko82085922016-12-13 22:13:50 +000063 RegisterSet(const RegisterSet &RS) = default;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000064
65 using BitVector::clear;
66 using BitVector::count;
67
68 unsigned find_first() const {
69 int First = BitVector::find_first();
70 if (First < 0)
71 return 0;
72 return x2v(First);
73 }
74
75 unsigned find_next(unsigned Prev) const {
76 int Next = BitVector::find_next(v2x(Prev));
77 if (Next < 0)
78 return 0;
79 return x2v(Next);
80 }
81
82 RegisterSet &insert(unsigned R) {
83 unsigned Idx = v2x(R);
84 ensure(Idx);
85 return static_cast<RegisterSet&>(BitVector::set(Idx));
86 }
87 RegisterSet &remove(unsigned R) {
88 unsigned Idx = v2x(R);
89 if (Idx >= size())
90 return *this;
91 return static_cast<RegisterSet&>(BitVector::reset(Idx));
92 }
93
94 RegisterSet &insert(const RegisterSet &Rs) {
95 return static_cast<RegisterSet&>(BitVector::operator|=(Rs));
96 }
97 RegisterSet &remove(const RegisterSet &Rs) {
98 return static_cast<RegisterSet&>(BitVector::reset(Rs));
99 }
100
101 reference operator[](unsigned R) {
102 unsigned Idx = v2x(R);
103 ensure(Idx);
104 return BitVector::operator[](Idx);
105 }
106 bool operator[](unsigned R) const {
107 unsigned Idx = v2x(R);
108 assert(Idx < size());
109 return BitVector::operator[](Idx);
110 }
111 bool has(unsigned R) const {
112 unsigned Idx = v2x(R);
113 if (Idx >= size())
114 return false;
115 return BitVector::test(Idx);
116 }
117
118 bool empty() const {
119 return !BitVector::any();
120 }
121 bool includes(const RegisterSet &Rs) const {
122 // A.BitVector::test(B) <=> A-B != {}
123 return !Rs.BitVector::test(*this);
124 }
125 bool intersects(const RegisterSet &Rs) const {
126 return BitVector::anyCommon(Rs);
127 }
128
129 private:
130 void ensure(unsigned Idx) {
131 if (size() <= Idx)
132 resize(std::max(Idx+1, 32U));
133 }
Eugene Zelenko82085922016-12-13 22:13:50 +0000134
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000135 static inline unsigned v2x(unsigned v) {
136 return TargetRegisterInfo::virtReg2Index(v);
137 }
Eugene Zelenko82085922016-12-13 22:13:50 +0000138
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000139 static inline unsigned x2v(unsigned x) {
140 return TargetRegisterInfo::index2VirtReg(x);
141 }
142 };
143
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000144 struct PrintRegSet {
145 PrintRegSet(const RegisterSet &S, const TargetRegisterInfo *RI)
146 : RS(S), TRI(RI) {}
Eugene Zelenko82085922016-12-13 22:13:50 +0000147
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000148 friend raw_ostream &operator<< (raw_ostream &OS,
149 const PrintRegSet &P);
Eugene Zelenko82085922016-12-13 22:13:50 +0000150
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000151 private:
152 const RegisterSet &RS;
153 const TargetRegisterInfo *TRI;
154 };
155
156 raw_ostream &operator<< (raw_ostream &OS, const PrintRegSet &P)
157 LLVM_ATTRIBUTE_UNUSED;
158 raw_ostream &operator<< (raw_ostream &OS, const PrintRegSet &P) {
159 OS << '{';
160 for (unsigned R = P.RS.find_first(); R; R = P.RS.find_next(R))
161 OS << ' ' << PrintReg(R, P.TRI);
162 OS << " }";
163 return OS;
164 }
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000165
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000166 class Transformation;
167
168 class HexagonBitSimplify : public MachineFunctionPass {
169 public:
170 static char ID;
Eugene Zelenko82085922016-12-13 22:13:50 +0000171
172 HexagonBitSimplify() : MachineFunctionPass(ID), MDT(nullptr) {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000173 initializeHexagonBitSimplifyPass(*PassRegistry::getPassRegistry());
174 }
Eugene Zelenko82085922016-12-13 22:13:50 +0000175
176 StringRef getPassName() const override {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000177 return "Hexagon bit simplification";
178 }
Eugene Zelenko82085922016-12-13 22:13:50 +0000179
180 void getAnalysisUsage(AnalysisUsage &AU) const override {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000181 AU.addRequired<MachineDominatorTree>();
182 AU.addPreserved<MachineDominatorTree>();
183 MachineFunctionPass::getAnalysisUsage(AU);
184 }
Eugene Zelenko82085922016-12-13 22:13:50 +0000185
186 bool runOnMachineFunction(MachineFunction &MF) override;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000187
188 static void getInstrDefs(const MachineInstr &MI, RegisterSet &Defs);
189 static void getInstrUses(const MachineInstr &MI, RegisterSet &Uses);
190 static bool isEqual(const BitTracker::RegisterCell &RC1, uint16_t B1,
191 const BitTracker::RegisterCell &RC2, uint16_t B2, uint16_t W);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000192 static bool isZero(const BitTracker::RegisterCell &RC, uint16_t B,
193 uint16_t W);
194 static bool getConst(const BitTracker::RegisterCell &RC, uint16_t B,
195 uint16_t W, uint64_t &U);
196 static bool replaceReg(unsigned OldR, unsigned NewR,
197 MachineRegisterInfo &MRI);
198 static bool getSubregMask(const BitTracker::RegisterRef &RR,
199 unsigned &Begin, unsigned &Width, MachineRegisterInfo &MRI);
200 static bool replaceRegWithSub(unsigned OldR, unsigned NewR,
201 unsigned NewSR, MachineRegisterInfo &MRI);
202 static bool replaceSubWithSub(unsigned OldR, unsigned OldSR,
203 unsigned NewR, unsigned NewSR, MachineRegisterInfo &MRI);
204 static bool parseRegSequence(const MachineInstr &I,
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000205 BitTracker::RegisterRef &SL, BitTracker::RegisterRef &SH,
206 const MachineRegisterInfo &MRI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000207
208 static bool getUsedBitsInStore(unsigned Opc, BitVector &Bits,
209 uint16_t Begin);
210 static bool getUsedBits(unsigned Opc, unsigned OpN, BitVector &Bits,
211 uint16_t Begin, const HexagonInstrInfo &HII);
212
213 static const TargetRegisterClass *getFinalVRegClass(
214 const BitTracker::RegisterRef &RR, MachineRegisterInfo &MRI);
215 static bool isTransparentCopy(const BitTracker::RegisterRef &RD,
216 const BitTracker::RegisterRef &RS, MachineRegisterInfo &MRI);
217
218 private:
219 MachineDominatorTree *MDT;
220
221 bool visitBlock(MachineBasicBlock &B, Transformation &T, RegisterSet &AVs);
Krzysztof Parzyszekd391d6f2016-10-06 16:18:04 +0000222 static bool hasTiedUse(unsigned Reg, MachineRegisterInfo &MRI,
223 unsigned NewSub = Hexagon::NoSubRegister);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000224 };
225
226 char HexagonBitSimplify::ID = 0;
227 typedef HexagonBitSimplify HBS;
228
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000229 // The purpose of this class is to provide a common facility to traverse
230 // the function top-down or bottom-up via the dominator tree, and keep
231 // track of the available registers.
232 class Transformation {
233 public:
234 bool TopDown;
Eugene Zelenko82085922016-12-13 22:13:50 +0000235
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000236 Transformation(bool TD) : TopDown(TD) {}
Eugene Zelenko82085922016-12-13 22:13:50 +0000237 virtual ~Transformation() = default;
238
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000239 virtual bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) = 0;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000240 };
Eugene Zelenko82085922016-12-13 22:13:50 +0000241
242} // end anonymous namespace
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000243
244INITIALIZE_PASS_BEGIN(HexagonBitSimplify, "hexbit",
245 "Hexagon bit simplification", false, false)
246INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
247INITIALIZE_PASS_END(HexagonBitSimplify, "hexbit",
248 "Hexagon bit simplification", false, false)
249
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000250bool HexagonBitSimplify::visitBlock(MachineBasicBlock &B, Transformation &T,
251 RegisterSet &AVs) {
252 MachineDomTreeNode *N = MDT->getNode(&B);
253 typedef GraphTraits<MachineDomTreeNode*> GTN;
254 bool Changed = false;
255
256 if (T.TopDown)
257 Changed = T.processBlock(B, AVs);
258
259 RegisterSet Defs;
260 for (auto &I : B)
261 getInstrDefs(I, Defs);
262 RegisterSet NewAVs = AVs;
263 NewAVs.insert(Defs);
264
265 for (auto I = GTN::child_begin(N), E = GTN::child_end(N); I != E; ++I) {
266 MachineBasicBlock *SB = (*I)->getBlock();
267 Changed |= visitBlock(*SB, T, NewAVs);
268 }
269 if (!T.TopDown)
270 Changed |= T.processBlock(B, AVs);
271
272 return Changed;
273}
274
275//
276// Utility functions:
277//
278void HexagonBitSimplify::getInstrDefs(const MachineInstr &MI,
279 RegisterSet &Defs) {
280 for (auto &Op : MI.operands()) {
281 if (!Op.isReg() || !Op.isDef())
282 continue;
283 unsigned R = Op.getReg();
284 if (!TargetRegisterInfo::isVirtualRegister(R))
285 continue;
286 Defs.insert(R);
287 }
288}
289
290void HexagonBitSimplify::getInstrUses(const MachineInstr &MI,
291 RegisterSet &Uses) {
292 for (auto &Op : MI.operands()) {
293 if (!Op.isReg() || !Op.isUse())
294 continue;
295 unsigned R = Op.getReg();
296 if (!TargetRegisterInfo::isVirtualRegister(R))
297 continue;
298 Uses.insert(R);
299 }
300}
301
302// Check if all the bits in range [B, E) in both cells are equal.
303bool HexagonBitSimplify::isEqual(const BitTracker::RegisterCell &RC1,
304 uint16_t B1, const BitTracker::RegisterCell &RC2, uint16_t B2,
305 uint16_t W) {
306 for (uint16_t i = 0; i < W; ++i) {
307 // If RC1[i] is "bottom", it cannot be proven equal to RC2[i].
308 if (RC1[B1+i].Type == BitTracker::BitValue::Ref && RC1[B1+i].RefI.Reg == 0)
309 return false;
310 // Same for RC2[i].
311 if (RC2[B2+i].Type == BitTracker::BitValue::Ref && RC2[B2+i].RefI.Reg == 0)
312 return false;
313 if (RC1[B1+i] != RC2[B2+i])
314 return false;
315 }
316 return true;
317}
318
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000319bool HexagonBitSimplify::isZero(const BitTracker::RegisterCell &RC,
320 uint16_t B, uint16_t W) {
321 assert(B < RC.width() && B+W <= RC.width());
322 for (uint16_t i = B; i < B+W; ++i)
323 if (!RC[i].is(0))
324 return false;
325 return true;
326}
327
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000328bool HexagonBitSimplify::getConst(const BitTracker::RegisterCell &RC,
329 uint16_t B, uint16_t W, uint64_t &U) {
330 assert(B < RC.width() && B+W <= RC.width());
331 int64_t T = 0;
332 for (uint16_t i = B+W; i > B; --i) {
333 const BitTracker::BitValue &BV = RC[i-1];
334 T <<= 1;
335 if (BV.is(1))
336 T |= 1;
337 else if (!BV.is(0))
338 return false;
339 }
340 U = T;
341 return true;
342}
343
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000344bool HexagonBitSimplify::replaceReg(unsigned OldR, unsigned NewR,
345 MachineRegisterInfo &MRI) {
346 if (!TargetRegisterInfo::isVirtualRegister(OldR) ||
347 !TargetRegisterInfo::isVirtualRegister(NewR))
348 return false;
349 auto Begin = MRI.use_begin(OldR), End = MRI.use_end();
350 decltype(End) NextI;
351 for (auto I = Begin; I != End; I = NextI) {
352 NextI = std::next(I);
353 I->setReg(NewR);
354 }
355 return Begin != End;
356}
357
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000358bool HexagonBitSimplify::replaceRegWithSub(unsigned OldR, unsigned NewR,
359 unsigned NewSR, MachineRegisterInfo &MRI) {
360 if (!TargetRegisterInfo::isVirtualRegister(OldR) ||
361 !TargetRegisterInfo::isVirtualRegister(NewR))
362 return false;
Krzysztof Parzyszekd391d6f2016-10-06 16:18:04 +0000363 if (hasTiedUse(OldR, MRI, NewSR))
364 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000365 auto Begin = MRI.use_begin(OldR), End = MRI.use_end();
366 decltype(End) NextI;
367 for (auto I = Begin; I != End; I = NextI) {
368 NextI = std::next(I);
369 I->setReg(NewR);
370 I->setSubReg(NewSR);
371 }
372 return Begin != End;
373}
374
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000375bool HexagonBitSimplify::replaceSubWithSub(unsigned OldR, unsigned OldSR,
376 unsigned NewR, unsigned NewSR, MachineRegisterInfo &MRI) {
377 if (!TargetRegisterInfo::isVirtualRegister(OldR) ||
378 !TargetRegisterInfo::isVirtualRegister(NewR))
379 return false;
Krzysztof Parzyszekd391d6f2016-10-06 16:18:04 +0000380 if (OldSR != NewSR && hasTiedUse(OldR, MRI, NewSR))
381 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000382 auto Begin = MRI.use_begin(OldR), End = MRI.use_end();
383 decltype(End) NextI;
384 for (auto I = Begin; I != End; I = NextI) {
385 NextI = std::next(I);
386 if (I->getSubReg() != OldSR)
387 continue;
388 I->setReg(NewR);
389 I->setSubReg(NewSR);
390 }
391 return Begin != End;
392}
393
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000394// For a register ref (pair Reg:Sub), set Begin to the position of the LSB
395// of Sub in Reg, and set Width to the size of Sub in bits. Return true,
396// if this succeeded, otherwise return false.
397bool HexagonBitSimplify::getSubregMask(const BitTracker::RegisterRef &RR,
398 unsigned &Begin, unsigned &Width, MachineRegisterInfo &MRI) {
399 const TargetRegisterClass *RC = MRI.getRegClass(RR.Reg);
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +0000400 if (RR.Sub == 0) {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000401 Begin = 0;
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +0000402 Width = RC->getSize()*8;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000403 return true;
404 }
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +0000405
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000406 Begin = 0;
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +0000407
408 switch (RC->getID()) {
409 case Hexagon::DoubleRegsRegClassID:
410 case Hexagon::VecDblRegsRegClassID:
411 case Hexagon::VecDblRegs128BRegClassID:
412 Width = RC->getSize()*8 / 2;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000413 if (RR.Sub == Hexagon::isub_hi || RR.Sub == Hexagon::vsub_hi)
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +0000414 Begin = Width;
415 break;
416 default:
417 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000418 }
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +0000419 return true;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000420}
421
422
423// For a REG_SEQUENCE, set SL to the low subregister and SH to the high
424// subregister.
425bool HexagonBitSimplify::parseRegSequence(const MachineInstr &I,
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000426 BitTracker::RegisterRef &SL, BitTracker::RegisterRef &SH,
427 const MachineRegisterInfo &MRI) {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000428 assert(I.getOpcode() == TargetOpcode::REG_SEQUENCE);
429 unsigned Sub1 = I.getOperand(2).getImm(), Sub2 = I.getOperand(4).getImm();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000430 auto *DstRC = MRI.getRegClass(I.getOperand(0).getReg());
431 auto &HRI = static_cast<const HexagonRegisterInfo&>(
432 *MRI.getTargetRegisterInfo());
433 unsigned SubLo = HRI.getHexagonSubRegIndex(DstRC, Hexagon::ps_sub_lo);
434 unsigned SubHi = HRI.getHexagonSubRegIndex(DstRC, Hexagon::ps_sub_hi);
435 assert((Sub1 == SubLo && Sub2 == SubHi) || (Sub1 == SubHi && Sub2 == SubLo));
436 if (Sub1 == SubLo && Sub2 == SubHi) {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000437 SL = I.getOperand(1);
438 SH = I.getOperand(3);
439 return true;
440 }
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000441 if (Sub1 == SubHi && Sub2 == SubLo) {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000442 SH = I.getOperand(1);
443 SL = I.getOperand(3);
444 return true;
445 }
446 return false;
447}
448
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000449// All stores (except 64-bit stores) take a 32-bit register as the source
450// of the value to be stored. If the instruction stores into a location
451// that is shorter than 32 bits, some bits of the source register are not
452// used. For each store instruction, calculate the set of used bits in
453// the source register, and set appropriate bits in Bits. Return true if
454// the bits are calculated, false otherwise.
455bool HexagonBitSimplify::getUsedBitsInStore(unsigned Opc, BitVector &Bits,
456 uint16_t Begin) {
457 using namespace Hexagon;
458
459 switch (Opc) {
460 // Store byte
461 case S2_storerb_io: // memb(Rs32+#s11:0)=Rt32
462 case S2_storerbnew_io: // memb(Rs32+#s11:0)=Nt8.new
463 case S2_pstorerbt_io: // if (Pv4) memb(Rs32+#u6:0)=Rt32
464 case S2_pstorerbf_io: // if (!Pv4) memb(Rs32+#u6:0)=Rt32
465 case S4_pstorerbtnew_io: // if (Pv4.new) memb(Rs32+#u6:0)=Rt32
466 case S4_pstorerbfnew_io: // if (!Pv4.new) memb(Rs32+#u6:0)=Rt32
467 case S2_pstorerbnewt_io: // if (Pv4) memb(Rs32+#u6:0)=Nt8.new
468 case S2_pstorerbnewf_io: // if (!Pv4) memb(Rs32+#u6:0)=Nt8.new
469 case S4_pstorerbnewtnew_io: // if (Pv4.new) memb(Rs32+#u6:0)=Nt8.new
470 case S4_pstorerbnewfnew_io: // if (!Pv4.new) memb(Rs32+#u6:0)=Nt8.new
471 case S2_storerb_pi: // memb(Rx32++#s4:0)=Rt32
472 case S2_storerbnew_pi: // memb(Rx32++#s4:0)=Nt8.new
473 case S2_pstorerbt_pi: // if (Pv4) memb(Rx32++#s4:0)=Rt32
474 case S2_pstorerbf_pi: // if (!Pv4) memb(Rx32++#s4:0)=Rt32
475 case S2_pstorerbtnew_pi: // if (Pv4.new) memb(Rx32++#s4:0)=Rt32
476 case S2_pstorerbfnew_pi: // if (!Pv4.new) memb(Rx32++#s4:0)=Rt32
477 case S2_pstorerbnewt_pi: // if (Pv4) memb(Rx32++#s4:0)=Nt8.new
478 case S2_pstorerbnewf_pi: // if (!Pv4) memb(Rx32++#s4:0)=Nt8.new
479 case S2_pstorerbnewtnew_pi: // if (Pv4.new) memb(Rx32++#s4:0)=Nt8.new
480 case S2_pstorerbnewfnew_pi: // if (!Pv4.new) memb(Rx32++#s4:0)=Nt8.new
481 case S4_storerb_ap: // memb(Re32=#U6)=Rt32
482 case S4_storerbnew_ap: // memb(Re32=#U6)=Nt8.new
483 case S2_storerb_pr: // memb(Rx32++Mu2)=Rt32
484 case S2_storerbnew_pr: // memb(Rx32++Mu2)=Nt8.new
485 case S4_storerb_ur: // memb(Ru32<<#u2+#U6)=Rt32
486 case S4_storerbnew_ur: // memb(Ru32<<#u2+#U6)=Nt8.new
487 case S2_storerb_pbr: // memb(Rx32++Mu2:brev)=Rt32
488 case S2_storerbnew_pbr: // memb(Rx32++Mu2:brev)=Nt8.new
489 case S2_storerb_pci: // memb(Rx32++#s4:0:circ(Mu2))=Rt32
490 case S2_storerbnew_pci: // memb(Rx32++#s4:0:circ(Mu2))=Nt8.new
491 case S2_storerb_pcr: // memb(Rx32++I:circ(Mu2))=Rt32
492 case S2_storerbnew_pcr: // memb(Rx32++I:circ(Mu2))=Nt8.new
493 case S4_storerb_rr: // memb(Rs32+Ru32<<#u2)=Rt32
494 case S4_storerbnew_rr: // memb(Rs32+Ru32<<#u2)=Nt8.new
495 case S4_pstorerbt_rr: // if (Pv4) memb(Rs32+Ru32<<#u2)=Rt32
496 case S4_pstorerbf_rr: // if (!Pv4) memb(Rs32+Ru32<<#u2)=Rt32
497 case S4_pstorerbtnew_rr: // if (Pv4.new) memb(Rs32+Ru32<<#u2)=Rt32
498 case S4_pstorerbfnew_rr: // if (!Pv4.new) memb(Rs32+Ru32<<#u2)=Rt32
499 case S4_pstorerbnewt_rr: // if (Pv4) memb(Rs32+Ru32<<#u2)=Nt8.new
500 case S4_pstorerbnewf_rr: // if (!Pv4) memb(Rs32+Ru32<<#u2)=Nt8.new
501 case S4_pstorerbnewtnew_rr: // if (Pv4.new) memb(Rs32+Ru32<<#u2)=Nt8.new
502 case S4_pstorerbnewfnew_rr: // if (!Pv4.new) memb(Rs32+Ru32<<#u2)=Nt8.new
503 case S2_storerbgp: // memb(gp+#u16:0)=Rt32
504 case S2_storerbnewgp: // memb(gp+#u16:0)=Nt8.new
505 case S4_pstorerbt_abs: // if (Pv4) memb(#u6)=Rt32
506 case S4_pstorerbf_abs: // if (!Pv4) memb(#u6)=Rt32
507 case S4_pstorerbtnew_abs: // if (Pv4.new) memb(#u6)=Rt32
508 case S4_pstorerbfnew_abs: // if (!Pv4.new) memb(#u6)=Rt32
509 case S4_pstorerbnewt_abs: // if (Pv4) memb(#u6)=Nt8.new
510 case S4_pstorerbnewf_abs: // if (!Pv4) memb(#u6)=Nt8.new
511 case S4_pstorerbnewtnew_abs: // if (Pv4.new) memb(#u6)=Nt8.new
512 case S4_pstorerbnewfnew_abs: // if (!Pv4.new) memb(#u6)=Nt8.new
513 Bits.set(Begin, Begin+8);
514 return true;
515
516 // Store low half
517 case S2_storerh_io: // memh(Rs32+#s11:1)=Rt32
518 case S2_storerhnew_io: // memh(Rs32+#s11:1)=Nt8.new
519 case S2_pstorerht_io: // if (Pv4) memh(Rs32+#u6:1)=Rt32
520 case S2_pstorerhf_io: // if (!Pv4) memh(Rs32+#u6:1)=Rt32
521 case S4_pstorerhtnew_io: // if (Pv4.new) memh(Rs32+#u6:1)=Rt32
522 case S4_pstorerhfnew_io: // if (!Pv4.new) memh(Rs32+#u6:1)=Rt32
523 case S2_pstorerhnewt_io: // if (Pv4) memh(Rs32+#u6:1)=Nt8.new
524 case S2_pstorerhnewf_io: // if (!Pv4) memh(Rs32+#u6:1)=Nt8.new
525 case S4_pstorerhnewtnew_io: // if (Pv4.new) memh(Rs32+#u6:1)=Nt8.new
526 case S4_pstorerhnewfnew_io: // if (!Pv4.new) memh(Rs32+#u6:1)=Nt8.new
527 case S2_storerh_pi: // memh(Rx32++#s4:1)=Rt32
528 case S2_storerhnew_pi: // memh(Rx32++#s4:1)=Nt8.new
529 case S2_pstorerht_pi: // if (Pv4) memh(Rx32++#s4:1)=Rt32
530 case S2_pstorerhf_pi: // if (!Pv4) memh(Rx32++#s4:1)=Rt32
531 case S2_pstorerhtnew_pi: // if (Pv4.new) memh(Rx32++#s4:1)=Rt32
532 case S2_pstorerhfnew_pi: // if (!Pv4.new) memh(Rx32++#s4:1)=Rt32
533 case S2_pstorerhnewt_pi: // if (Pv4) memh(Rx32++#s4:1)=Nt8.new
534 case S2_pstorerhnewf_pi: // if (!Pv4) memh(Rx32++#s4:1)=Nt8.new
535 case S2_pstorerhnewtnew_pi: // if (Pv4.new) memh(Rx32++#s4:1)=Nt8.new
536 case S2_pstorerhnewfnew_pi: // if (!Pv4.new) memh(Rx32++#s4:1)=Nt8.new
537 case S4_storerh_ap: // memh(Re32=#U6)=Rt32
538 case S4_storerhnew_ap: // memh(Re32=#U6)=Nt8.new
539 case S2_storerh_pr: // memh(Rx32++Mu2)=Rt32
540 case S2_storerhnew_pr: // memh(Rx32++Mu2)=Nt8.new
541 case S4_storerh_ur: // memh(Ru32<<#u2+#U6)=Rt32
542 case S4_storerhnew_ur: // memh(Ru32<<#u2+#U6)=Nt8.new
543 case S2_storerh_pbr: // memh(Rx32++Mu2:brev)=Rt32
544 case S2_storerhnew_pbr: // memh(Rx32++Mu2:brev)=Nt8.new
545 case S2_storerh_pci: // memh(Rx32++#s4:1:circ(Mu2))=Rt32
546 case S2_storerhnew_pci: // memh(Rx32++#s4:1:circ(Mu2))=Nt8.new
547 case S2_storerh_pcr: // memh(Rx32++I:circ(Mu2))=Rt32
548 case S2_storerhnew_pcr: // memh(Rx32++I:circ(Mu2))=Nt8.new
549 case S4_storerh_rr: // memh(Rs32+Ru32<<#u2)=Rt32
550 case S4_pstorerht_rr: // if (Pv4) memh(Rs32+Ru32<<#u2)=Rt32
551 case S4_pstorerhf_rr: // if (!Pv4) memh(Rs32+Ru32<<#u2)=Rt32
552 case S4_pstorerhtnew_rr: // if (Pv4.new) memh(Rs32+Ru32<<#u2)=Rt32
553 case S4_pstorerhfnew_rr: // if (!Pv4.new) memh(Rs32+Ru32<<#u2)=Rt32
554 case S4_storerhnew_rr: // memh(Rs32+Ru32<<#u2)=Nt8.new
555 case S4_pstorerhnewt_rr: // if (Pv4) memh(Rs32+Ru32<<#u2)=Nt8.new
556 case S4_pstorerhnewf_rr: // if (!Pv4) memh(Rs32+Ru32<<#u2)=Nt8.new
557 case S4_pstorerhnewtnew_rr: // if (Pv4.new) memh(Rs32+Ru32<<#u2)=Nt8.new
558 case S4_pstorerhnewfnew_rr: // if (!Pv4.new) memh(Rs32+Ru32<<#u2)=Nt8.new
559 case S2_storerhgp: // memh(gp+#u16:1)=Rt32
560 case S2_storerhnewgp: // memh(gp+#u16:1)=Nt8.new
561 case S4_pstorerht_abs: // if (Pv4) memh(#u6)=Rt32
562 case S4_pstorerhf_abs: // if (!Pv4) memh(#u6)=Rt32
563 case S4_pstorerhtnew_abs: // if (Pv4.new) memh(#u6)=Rt32
564 case S4_pstorerhfnew_abs: // if (!Pv4.new) memh(#u6)=Rt32
565 case S4_pstorerhnewt_abs: // if (Pv4) memh(#u6)=Nt8.new
566 case S4_pstorerhnewf_abs: // if (!Pv4) memh(#u6)=Nt8.new
567 case S4_pstorerhnewtnew_abs: // if (Pv4.new) memh(#u6)=Nt8.new
568 case S4_pstorerhnewfnew_abs: // if (!Pv4.new) memh(#u6)=Nt8.new
569 Bits.set(Begin, Begin+16);
570 return true;
571
572 // Store high half
573 case S2_storerf_io: // memh(Rs32+#s11:1)=Rt.H32
574 case S2_pstorerft_io: // if (Pv4) memh(Rs32+#u6:1)=Rt.H32
575 case S2_pstorerff_io: // if (!Pv4) memh(Rs32+#u6:1)=Rt.H32
576 case S4_pstorerftnew_io: // if (Pv4.new) memh(Rs32+#u6:1)=Rt.H32
577 case S4_pstorerffnew_io: // if (!Pv4.new) memh(Rs32+#u6:1)=Rt.H32
578 case S2_storerf_pi: // memh(Rx32++#s4:1)=Rt.H32
579 case S2_pstorerft_pi: // if (Pv4) memh(Rx32++#s4:1)=Rt.H32
580 case S2_pstorerff_pi: // if (!Pv4) memh(Rx32++#s4:1)=Rt.H32
581 case S2_pstorerftnew_pi: // if (Pv4.new) memh(Rx32++#s4:1)=Rt.H32
582 case S2_pstorerffnew_pi: // if (!Pv4.new) memh(Rx32++#s4:1)=Rt.H32
583 case S4_storerf_ap: // memh(Re32=#U6)=Rt.H32
584 case S2_storerf_pr: // memh(Rx32++Mu2)=Rt.H32
585 case S4_storerf_ur: // memh(Ru32<<#u2+#U6)=Rt.H32
586 case S2_storerf_pbr: // memh(Rx32++Mu2:brev)=Rt.H32
587 case S2_storerf_pci: // memh(Rx32++#s4:1:circ(Mu2))=Rt.H32
588 case S2_storerf_pcr: // memh(Rx32++I:circ(Mu2))=Rt.H32
589 case S4_storerf_rr: // memh(Rs32+Ru32<<#u2)=Rt.H32
590 case S4_pstorerft_rr: // if (Pv4) memh(Rs32+Ru32<<#u2)=Rt.H32
591 case S4_pstorerff_rr: // if (!Pv4) memh(Rs32+Ru32<<#u2)=Rt.H32
592 case S4_pstorerftnew_rr: // if (Pv4.new) memh(Rs32+Ru32<<#u2)=Rt.H32
593 case S4_pstorerffnew_rr: // if (!Pv4.new) memh(Rs32+Ru32<<#u2)=Rt.H32
594 case S2_storerfgp: // memh(gp+#u16:1)=Rt.H32
595 case S4_pstorerft_abs: // if (Pv4) memh(#u6)=Rt.H32
596 case S4_pstorerff_abs: // if (!Pv4) memh(#u6)=Rt.H32
597 case S4_pstorerftnew_abs: // if (Pv4.new) memh(#u6)=Rt.H32
598 case S4_pstorerffnew_abs: // if (!Pv4.new) memh(#u6)=Rt.H32
599 Bits.set(Begin+16, Begin+32);
600 return true;
601 }
602
603 return false;
604}
605
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000606// For an instruction with opcode Opc, calculate the set of bits that it
607// uses in a register in operand OpN. This only calculates the set of used
608// bits for cases where it does not depend on any operands (as is the case
609// in shifts, for example). For concrete instructions from a program, the
610// operand may be a subregister of a larger register, while Bits would
611// correspond to the larger register in its entirety. Because of that,
612// the parameter Begin can be used to indicate which bit of Bits should be
613// considered the LSB of of the operand.
614bool HexagonBitSimplify::getUsedBits(unsigned Opc, unsigned OpN,
615 BitVector &Bits, uint16_t Begin, const HexagonInstrInfo &HII) {
616 using namespace Hexagon;
617
618 const MCInstrDesc &D = HII.get(Opc);
619 if (D.mayStore()) {
620 if (OpN == D.getNumOperands()-1)
621 return getUsedBitsInStore(Opc, Bits, Begin);
622 return false;
623 }
624
625 switch (Opc) {
626 // One register source. Used bits: R1[0-7].
627 case A2_sxtb:
628 case A2_zxtb:
629 case A4_cmpbeqi:
630 case A4_cmpbgti:
631 case A4_cmpbgtui:
632 if (OpN == 1) {
633 Bits.set(Begin, Begin+8);
634 return true;
635 }
636 break;
637
638 // One register source. Used bits: R1[0-15].
639 case A2_aslh:
640 case A2_sxth:
641 case A2_zxth:
642 case A4_cmpheqi:
643 case A4_cmphgti:
644 case A4_cmphgtui:
645 if (OpN == 1) {
646 Bits.set(Begin, Begin+16);
647 return true;
648 }
649 break;
650
651 // One register source. Used bits: R1[16-31].
652 case A2_asrh:
653 if (OpN == 1) {
654 Bits.set(Begin+16, Begin+32);
655 return true;
656 }
657 break;
658
659 // Two register sources. Used bits: R1[0-7], R2[0-7].
660 case A4_cmpbeq:
661 case A4_cmpbgt:
662 case A4_cmpbgtu:
663 if (OpN == 1) {
664 Bits.set(Begin, Begin+8);
665 return true;
666 }
667 break;
668
669 // Two register sources. Used bits: R1[0-15], R2[0-15].
670 case A4_cmpheq:
671 case A4_cmphgt:
672 case A4_cmphgtu:
673 case A2_addh_h16_ll:
674 case A2_addh_h16_sat_ll:
675 case A2_addh_l16_ll:
676 case A2_addh_l16_sat_ll:
677 case A2_combine_ll:
678 case A2_subh_h16_ll:
679 case A2_subh_h16_sat_ll:
680 case A2_subh_l16_ll:
681 case A2_subh_l16_sat_ll:
682 case M2_mpy_acc_ll_s0:
683 case M2_mpy_acc_ll_s1:
684 case M2_mpy_acc_sat_ll_s0:
685 case M2_mpy_acc_sat_ll_s1:
686 case M2_mpy_ll_s0:
687 case M2_mpy_ll_s1:
688 case M2_mpy_nac_ll_s0:
689 case M2_mpy_nac_ll_s1:
690 case M2_mpy_nac_sat_ll_s0:
691 case M2_mpy_nac_sat_ll_s1:
692 case M2_mpy_rnd_ll_s0:
693 case M2_mpy_rnd_ll_s1:
694 case M2_mpy_sat_ll_s0:
695 case M2_mpy_sat_ll_s1:
696 case M2_mpy_sat_rnd_ll_s0:
697 case M2_mpy_sat_rnd_ll_s1:
698 case M2_mpyd_acc_ll_s0:
699 case M2_mpyd_acc_ll_s1:
700 case M2_mpyd_ll_s0:
701 case M2_mpyd_ll_s1:
702 case M2_mpyd_nac_ll_s0:
703 case M2_mpyd_nac_ll_s1:
704 case M2_mpyd_rnd_ll_s0:
705 case M2_mpyd_rnd_ll_s1:
706 case M2_mpyu_acc_ll_s0:
707 case M2_mpyu_acc_ll_s1:
708 case M2_mpyu_ll_s0:
709 case M2_mpyu_ll_s1:
710 case M2_mpyu_nac_ll_s0:
711 case M2_mpyu_nac_ll_s1:
712 case M2_mpyud_acc_ll_s0:
713 case M2_mpyud_acc_ll_s1:
714 case M2_mpyud_ll_s0:
715 case M2_mpyud_ll_s1:
716 case M2_mpyud_nac_ll_s0:
717 case M2_mpyud_nac_ll_s1:
718 if (OpN == 1 || OpN == 2) {
719 Bits.set(Begin, Begin+16);
720 return true;
721 }
722 break;
723
724 // Two register sources. Used bits: R1[0-15], R2[16-31].
725 case A2_addh_h16_lh:
726 case A2_addh_h16_sat_lh:
727 case A2_combine_lh:
728 case A2_subh_h16_lh:
729 case A2_subh_h16_sat_lh:
730 case M2_mpy_acc_lh_s0:
731 case M2_mpy_acc_lh_s1:
732 case M2_mpy_acc_sat_lh_s0:
733 case M2_mpy_acc_sat_lh_s1:
734 case M2_mpy_lh_s0:
735 case M2_mpy_lh_s1:
736 case M2_mpy_nac_lh_s0:
737 case M2_mpy_nac_lh_s1:
738 case M2_mpy_nac_sat_lh_s0:
739 case M2_mpy_nac_sat_lh_s1:
740 case M2_mpy_rnd_lh_s0:
741 case M2_mpy_rnd_lh_s1:
742 case M2_mpy_sat_lh_s0:
743 case M2_mpy_sat_lh_s1:
744 case M2_mpy_sat_rnd_lh_s0:
745 case M2_mpy_sat_rnd_lh_s1:
746 case M2_mpyd_acc_lh_s0:
747 case M2_mpyd_acc_lh_s1:
748 case M2_mpyd_lh_s0:
749 case M2_mpyd_lh_s1:
750 case M2_mpyd_nac_lh_s0:
751 case M2_mpyd_nac_lh_s1:
752 case M2_mpyd_rnd_lh_s0:
753 case M2_mpyd_rnd_lh_s1:
754 case M2_mpyu_acc_lh_s0:
755 case M2_mpyu_acc_lh_s1:
756 case M2_mpyu_lh_s0:
757 case M2_mpyu_lh_s1:
758 case M2_mpyu_nac_lh_s0:
759 case M2_mpyu_nac_lh_s1:
760 case M2_mpyud_acc_lh_s0:
761 case M2_mpyud_acc_lh_s1:
762 case M2_mpyud_lh_s0:
763 case M2_mpyud_lh_s1:
764 case M2_mpyud_nac_lh_s0:
765 case M2_mpyud_nac_lh_s1:
766 // These four are actually LH.
767 case A2_addh_l16_hl:
768 case A2_addh_l16_sat_hl:
769 case A2_subh_l16_hl:
770 case A2_subh_l16_sat_hl:
771 if (OpN == 1) {
772 Bits.set(Begin, Begin+16);
773 return true;
774 }
775 if (OpN == 2) {
776 Bits.set(Begin+16, Begin+32);
777 return true;
778 }
779 break;
780
781 // Two register sources, used bits: R1[16-31], R2[0-15].
782 case A2_addh_h16_hl:
783 case A2_addh_h16_sat_hl:
784 case A2_combine_hl:
785 case A2_subh_h16_hl:
786 case A2_subh_h16_sat_hl:
787 case M2_mpy_acc_hl_s0:
788 case M2_mpy_acc_hl_s1:
789 case M2_mpy_acc_sat_hl_s0:
790 case M2_mpy_acc_sat_hl_s1:
791 case M2_mpy_hl_s0:
792 case M2_mpy_hl_s1:
793 case M2_mpy_nac_hl_s0:
794 case M2_mpy_nac_hl_s1:
795 case M2_mpy_nac_sat_hl_s0:
796 case M2_mpy_nac_sat_hl_s1:
797 case M2_mpy_rnd_hl_s0:
798 case M2_mpy_rnd_hl_s1:
799 case M2_mpy_sat_hl_s0:
800 case M2_mpy_sat_hl_s1:
801 case M2_mpy_sat_rnd_hl_s0:
802 case M2_mpy_sat_rnd_hl_s1:
803 case M2_mpyd_acc_hl_s0:
804 case M2_mpyd_acc_hl_s1:
805 case M2_mpyd_hl_s0:
806 case M2_mpyd_hl_s1:
807 case M2_mpyd_nac_hl_s0:
808 case M2_mpyd_nac_hl_s1:
809 case M2_mpyd_rnd_hl_s0:
810 case M2_mpyd_rnd_hl_s1:
811 case M2_mpyu_acc_hl_s0:
812 case M2_mpyu_acc_hl_s1:
813 case M2_mpyu_hl_s0:
814 case M2_mpyu_hl_s1:
815 case M2_mpyu_nac_hl_s0:
816 case M2_mpyu_nac_hl_s1:
817 case M2_mpyud_acc_hl_s0:
818 case M2_mpyud_acc_hl_s1:
819 case M2_mpyud_hl_s0:
820 case M2_mpyud_hl_s1:
821 case M2_mpyud_nac_hl_s0:
822 case M2_mpyud_nac_hl_s1:
823 if (OpN == 1) {
824 Bits.set(Begin+16, Begin+32);
825 return true;
826 }
827 if (OpN == 2) {
828 Bits.set(Begin, Begin+16);
829 return true;
830 }
831 break;
832
833 // Two register sources, used bits: R1[16-31], R2[16-31].
834 case A2_addh_h16_hh:
835 case A2_addh_h16_sat_hh:
836 case A2_combine_hh:
837 case A2_subh_h16_hh:
838 case A2_subh_h16_sat_hh:
839 case M2_mpy_acc_hh_s0:
840 case M2_mpy_acc_hh_s1:
841 case M2_mpy_acc_sat_hh_s0:
842 case M2_mpy_acc_sat_hh_s1:
843 case M2_mpy_hh_s0:
844 case M2_mpy_hh_s1:
845 case M2_mpy_nac_hh_s0:
846 case M2_mpy_nac_hh_s1:
847 case M2_mpy_nac_sat_hh_s0:
848 case M2_mpy_nac_sat_hh_s1:
849 case M2_mpy_rnd_hh_s0:
850 case M2_mpy_rnd_hh_s1:
851 case M2_mpy_sat_hh_s0:
852 case M2_mpy_sat_hh_s1:
853 case M2_mpy_sat_rnd_hh_s0:
854 case M2_mpy_sat_rnd_hh_s1:
855 case M2_mpyd_acc_hh_s0:
856 case M2_mpyd_acc_hh_s1:
857 case M2_mpyd_hh_s0:
858 case M2_mpyd_hh_s1:
859 case M2_mpyd_nac_hh_s0:
860 case M2_mpyd_nac_hh_s1:
861 case M2_mpyd_rnd_hh_s0:
862 case M2_mpyd_rnd_hh_s1:
863 case M2_mpyu_acc_hh_s0:
864 case M2_mpyu_acc_hh_s1:
865 case M2_mpyu_hh_s0:
866 case M2_mpyu_hh_s1:
867 case M2_mpyu_nac_hh_s0:
868 case M2_mpyu_nac_hh_s1:
869 case M2_mpyud_acc_hh_s0:
870 case M2_mpyud_acc_hh_s1:
871 case M2_mpyud_hh_s0:
872 case M2_mpyud_hh_s1:
873 case M2_mpyud_nac_hh_s0:
874 case M2_mpyud_nac_hh_s1:
875 if (OpN == 1 || OpN == 2) {
876 Bits.set(Begin+16, Begin+32);
877 return true;
878 }
879 break;
880 }
881
882 return false;
883}
884
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000885// Calculate the register class that matches Reg:Sub. For example, if
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000886// vreg1 is a double register, then vreg1:isub_hi would match the "int"
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000887// register class.
888const TargetRegisterClass *HexagonBitSimplify::getFinalVRegClass(
889 const BitTracker::RegisterRef &RR, MachineRegisterInfo &MRI) {
890 if (!TargetRegisterInfo::isVirtualRegister(RR.Reg))
891 return nullptr;
892 auto *RC = MRI.getRegClass(RR.Reg);
893 if (RR.Sub == 0)
894 return RC;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000895 auto &HRI = static_cast<const HexagonRegisterInfo&>(
896 *MRI.getTargetRegisterInfo());
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000897
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000898 auto VerifySR = [&HRI] (const TargetRegisterClass *RC, unsigned Sub) -> void {
David L. Jones41cecba2017-01-13 21:02:41 +0000899 (void)HRI;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000900 assert(Sub == HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_lo) ||
901 Sub == HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_hi));
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000902 };
903
904 switch (RC->getID()) {
905 case Hexagon::DoubleRegsRegClassID:
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000906 VerifySR(RC, RR.Sub);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000907 return &Hexagon::IntRegsRegClass;
Krzysztof Parzyszek5337a3e2016-01-14 21:45:43 +0000908 case Hexagon::VecDblRegsRegClassID:
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000909 VerifySR(RC, RR.Sub);
Krzysztof Parzyszek5337a3e2016-01-14 21:45:43 +0000910 return &Hexagon::VectorRegsRegClass;
911 case Hexagon::VecDblRegs128BRegClassID:
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000912 VerifySR(RC, RR.Sub);
Krzysztof Parzyszek5337a3e2016-01-14 21:45:43 +0000913 return &Hexagon::VectorRegs128BRegClass;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000914 }
915 return nullptr;
916}
917
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000918// Check if RD could be replaced with RS at any possible use of RD.
919// For example a predicate register cannot be replaced with a integer
920// register, but a 64-bit register with a subregister can be replaced
921// with a 32-bit register.
922bool HexagonBitSimplify::isTransparentCopy(const BitTracker::RegisterRef &RD,
923 const BitTracker::RegisterRef &RS, MachineRegisterInfo &MRI) {
924 if (!TargetRegisterInfo::isVirtualRegister(RD.Reg) ||
925 !TargetRegisterInfo::isVirtualRegister(RS.Reg))
926 return false;
927 // Return false if one (or both) classes are nullptr.
928 auto *DRC = getFinalVRegClass(RD, MRI);
929 if (!DRC)
930 return false;
931
932 return DRC == getFinalVRegClass(RS, MRI);
933}
934
Krzysztof Parzyszekd391d6f2016-10-06 16:18:04 +0000935bool HexagonBitSimplify::hasTiedUse(unsigned Reg, MachineRegisterInfo &MRI,
936 unsigned NewSub) {
937 if (!PreserveTiedOps)
938 return false;
Eugene Zelenko82085922016-12-13 22:13:50 +0000939 return llvm::any_of(MRI.use_operands(Reg),
940 [NewSub] (const MachineOperand &Op) -> bool {
941 return Op.getSubReg() != NewSub && Op.isTied();
942 });
Krzysztof Parzyszekd391d6f2016-10-06 16:18:04 +0000943}
944
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000945namespace {
Eugene Zelenko82085922016-12-13 22:13:50 +0000946
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000947 class DeadCodeElimination {
948 public:
949 DeadCodeElimination(MachineFunction &mf, MachineDominatorTree &mdt)
950 : MF(mf), HII(*MF.getSubtarget<HexagonSubtarget>().getInstrInfo()),
951 MDT(mdt), MRI(mf.getRegInfo()) {}
952
953 bool run() {
954 return runOnNode(MDT.getRootNode());
955 }
956
957 private:
958 bool isDead(unsigned R) const;
959 bool runOnNode(MachineDomTreeNode *N);
960
961 MachineFunction &MF;
962 const HexagonInstrInfo &HII;
963 MachineDominatorTree &MDT;
964 MachineRegisterInfo &MRI;
965 };
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000966
Eugene Zelenko82085922016-12-13 22:13:50 +0000967} // end anonymous namespace
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000968
969bool DeadCodeElimination::isDead(unsigned R) const {
970 for (auto I = MRI.use_begin(R), E = MRI.use_end(); I != E; ++I) {
971 MachineInstr *UseI = I->getParent();
972 if (UseI->isDebugValue())
973 continue;
974 if (UseI->isPHI()) {
975 assert(!UseI->getOperand(0).getSubReg());
976 unsigned DR = UseI->getOperand(0).getReg();
977 if (DR == R)
978 continue;
979 }
980 return false;
981 }
982 return true;
983}
984
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000985bool DeadCodeElimination::runOnNode(MachineDomTreeNode *N) {
986 bool Changed = false;
987 typedef GraphTraits<MachineDomTreeNode*> GTN;
988 for (auto I = GTN::child_begin(N), E = GTN::child_end(N); I != E; ++I)
989 Changed |= runOnNode(*I);
990
991 MachineBasicBlock *B = N->getBlock();
992 std::vector<MachineInstr*> Instrs;
993 for (auto I = B->rbegin(), E = B->rend(); I != E; ++I)
994 Instrs.push_back(&*I);
995
996 for (auto MI : Instrs) {
997 unsigned Opc = MI->getOpcode();
998 // Do not touch lifetime markers. This is why the target-independent DCE
999 // cannot be used.
1000 if (Opc == TargetOpcode::LIFETIME_START ||
1001 Opc == TargetOpcode::LIFETIME_END)
1002 continue;
1003 bool Store = false;
1004 if (MI->isInlineAsm())
1005 continue;
1006 // Delete PHIs if possible.
1007 if (!MI->isPHI() && !MI->isSafeToMove(nullptr, Store))
1008 continue;
1009
1010 bool AllDead = true;
1011 SmallVector<unsigned,2> Regs;
1012 for (auto &Op : MI->operands()) {
1013 if (!Op.isReg() || !Op.isDef())
1014 continue;
1015 unsigned R = Op.getReg();
1016 if (!TargetRegisterInfo::isVirtualRegister(R) || !isDead(R)) {
1017 AllDead = false;
1018 break;
1019 }
1020 Regs.push_back(R);
1021 }
1022 if (!AllDead)
1023 continue;
1024
1025 B->erase(MI);
1026 for (unsigned i = 0, n = Regs.size(); i != n; ++i)
1027 MRI.markUsesInDebugValueAsUndef(Regs[i]);
1028 Changed = true;
1029 }
1030
1031 return Changed;
1032}
1033
Eugene Zelenko82085922016-12-13 22:13:50 +00001034namespace {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001035
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001036// Eliminate redundant instructions
1037//
1038// This transformation will identify instructions where the output register
1039// is the same as one of its input registers. This only works on instructions
1040// that define a single register (unlike post-increment loads, for example).
1041// The equality check is actually more detailed: the code calculates which
1042// bits of the output are used, and only compares these bits with the input
1043// registers.
1044// If the output matches an input, the instruction is replaced with COPY.
1045// The copies will be removed by another transformation.
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001046 class RedundantInstrElimination : public Transformation {
1047 public:
1048 RedundantInstrElimination(BitTracker &bt, const HexagonInstrInfo &hii,
1049 MachineRegisterInfo &mri)
1050 : Transformation(true), HII(hii), MRI(mri), BT(bt) {}
Eugene Zelenko82085922016-12-13 22:13:50 +00001051
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001052 bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override;
Eugene Zelenko82085922016-12-13 22:13:50 +00001053
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001054 private:
1055 bool isLossyShiftLeft(const MachineInstr &MI, unsigned OpN,
1056 unsigned &LostB, unsigned &LostE);
1057 bool isLossyShiftRight(const MachineInstr &MI, unsigned OpN,
1058 unsigned &LostB, unsigned &LostE);
1059 bool computeUsedBits(unsigned Reg, BitVector &Bits);
1060 bool computeUsedBits(const MachineInstr &MI, unsigned OpN, BitVector &Bits,
1061 uint16_t Begin);
1062 bool usedBitsEqual(BitTracker::RegisterRef RD, BitTracker::RegisterRef RS);
1063
1064 const HexagonInstrInfo &HII;
1065 MachineRegisterInfo &MRI;
1066 BitTracker &BT;
1067 };
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001068
Eugene Zelenko82085922016-12-13 22:13:50 +00001069} // end anonymous namespace
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001070
1071// Check if the instruction is a lossy shift left, where the input being
1072// shifted is the operand OpN of MI. If true, [LostB, LostE) is the range
1073// of bit indices that are lost.
1074bool RedundantInstrElimination::isLossyShiftLeft(const MachineInstr &MI,
1075 unsigned OpN, unsigned &LostB, unsigned &LostE) {
1076 using namespace Hexagon;
Eugene Zelenko82085922016-12-13 22:13:50 +00001077
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001078 unsigned Opc = MI.getOpcode();
1079 unsigned ImN, RegN, Width;
1080 switch (Opc) {
1081 case S2_asl_i_p:
1082 ImN = 2;
1083 RegN = 1;
1084 Width = 64;
1085 break;
1086 case S2_asl_i_p_acc:
1087 case S2_asl_i_p_and:
1088 case S2_asl_i_p_nac:
1089 case S2_asl_i_p_or:
1090 case S2_asl_i_p_xacc:
1091 ImN = 3;
1092 RegN = 2;
1093 Width = 64;
1094 break;
1095 case S2_asl_i_r:
1096 ImN = 2;
1097 RegN = 1;
1098 Width = 32;
1099 break;
1100 case S2_addasl_rrri:
1101 case S4_andi_asl_ri:
1102 case S4_ori_asl_ri:
1103 case S4_addi_asl_ri:
1104 case S4_subi_asl_ri:
1105 case S2_asl_i_r_acc:
1106 case S2_asl_i_r_and:
1107 case S2_asl_i_r_nac:
1108 case S2_asl_i_r_or:
1109 case S2_asl_i_r_sat:
1110 case S2_asl_i_r_xacc:
1111 ImN = 3;
1112 RegN = 2;
1113 Width = 32;
1114 break;
1115 default:
1116 return false;
1117 }
1118
1119 if (RegN != OpN)
1120 return false;
1121
1122 assert(MI.getOperand(ImN).isImm());
1123 unsigned S = MI.getOperand(ImN).getImm();
1124 if (S == 0)
1125 return false;
1126 LostB = Width-S;
1127 LostE = Width;
1128 return true;
1129}
1130
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001131// Check if the instruction is a lossy shift right, where the input being
1132// shifted is the operand OpN of MI. If true, [LostB, LostE) is the range
1133// of bit indices that are lost.
1134bool RedundantInstrElimination::isLossyShiftRight(const MachineInstr &MI,
1135 unsigned OpN, unsigned &LostB, unsigned &LostE) {
1136 using namespace Hexagon;
Eugene Zelenko82085922016-12-13 22:13:50 +00001137
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001138 unsigned Opc = MI.getOpcode();
1139 unsigned ImN, RegN;
1140 switch (Opc) {
1141 case S2_asr_i_p:
1142 case S2_lsr_i_p:
1143 ImN = 2;
1144 RegN = 1;
1145 break;
1146 case S2_asr_i_p_acc:
1147 case S2_asr_i_p_and:
1148 case S2_asr_i_p_nac:
1149 case S2_asr_i_p_or:
1150 case S2_lsr_i_p_acc:
1151 case S2_lsr_i_p_and:
1152 case S2_lsr_i_p_nac:
1153 case S2_lsr_i_p_or:
1154 case S2_lsr_i_p_xacc:
1155 ImN = 3;
1156 RegN = 2;
1157 break;
1158 case S2_asr_i_r:
1159 case S2_lsr_i_r:
1160 ImN = 2;
1161 RegN = 1;
1162 break;
1163 case S4_andi_lsr_ri:
1164 case S4_ori_lsr_ri:
1165 case S4_addi_lsr_ri:
1166 case S4_subi_lsr_ri:
1167 case S2_asr_i_r_acc:
1168 case S2_asr_i_r_and:
1169 case S2_asr_i_r_nac:
1170 case S2_asr_i_r_or:
1171 case S2_lsr_i_r_acc:
1172 case S2_lsr_i_r_and:
1173 case S2_lsr_i_r_nac:
1174 case S2_lsr_i_r_or:
1175 case S2_lsr_i_r_xacc:
1176 ImN = 3;
1177 RegN = 2;
1178 break;
1179
1180 default:
1181 return false;
1182 }
1183
1184 if (RegN != OpN)
1185 return false;
1186
1187 assert(MI.getOperand(ImN).isImm());
1188 unsigned S = MI.getOperand(ImN).getImm();
1189 LostB = 0;
1190 LostE = S;
1191 return true;
1192}
1193
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001194// Calculate the bit vector that corresponds to the used bits of register Reg.
1195// The vector Bits has the same size, as the size of Reg in bits. If the cal-
1196// culation fails (i.e. the used bits are unknown), it returns false. Other-
1197// wise, it returns true and sets the corresponding bits in Bits.
1198bool RedundantInstrElimination::computeUsedBits(unsigned Reg, BitVector &Bits) {
1199 BitVector Used(Bits.size());
1200 RegisterSet Visited;
1201 std::vector<unsigned> Pending;
1202 Pending.push_back(Reg);
1203
1204 for (unsigned i = 0; i < Pending.size(); ++i) {
1205 unsigned R = Pending[i];
1206 if (Visited.has(R))
1207 continue;
1208 Visited.insert(R);
1209 for (auto I = MRI.use_begin(R), E = MRI.use_end(); I != E; ++I) {
1210 BitTracker::RegisterRef UR = *I;
1211 unsigned B, W;
1212 if (!HBS::getSubregMask(UR, B, W, MRI))
1213 return false;
1214 MachineInstr &UseI = *I->getParent();
1215 if (UseI.isPHI() || UseI.isCopy()) {
1216 unsigned DefR = UseI.getOperand(0).getReg();
1217 if (!TargetRegisterInfo::isVirtualRegister(DefR))
1218 return false;
1219 Pending.push_back(DefR);
1220 } else {
1221 if (!computeUsedBits(UseI, I.getOperandNo(), Used, B))
1222 return false;
1223 }
1224 }
1225 }
1226 Bits |= Used;
1227 return true;
1228}
1229
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001230// Calculate the bits used by instruction MI in a register in operand OpN.
1231// Return true/false if the calculation succeeds/fails. If is succeeds, set
1232// used bits in Bits. This function does not reset any bits in Bits, so
1233// subsequent calls over different instructions will result in the union
1234// of the used bits in all these instructions.
1235// The register in question may be used with a sub-register, whereas Bits
1236// holds the bits for the entire register. To keep track of that, the
1237// argument Begin indicates where in Bits is the lowest-significant bit
1238// of the register used in operand OpN. For example, in instruction:
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001239// vreg1 = S2_lsr_i_r vreg2:isub_hi, 10
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001240// the operand 1 is a 32-bit register, which happens to be a subregister
1241// of the 64-bit register vreg2, and that subregister starts at position 32.
1242// In this case Begin=32, since Bits[32] would be the lowest-significant bit
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001243// of vreg2:isub_hi.
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001244bool RedundantInstrElimination::computeUsedBits(const MachineInstr &MI,
1245 unsigned OpN, BitVector &Bits, uint16_t Begin) {
1246 unsigned Opc = MI.getOpcode();
1247 BitVector T(Bits.size());
1248 bool GotBits = HBS::getUsedBits(Opc, OpN, T, Begin, HII);
1249 // Even if we don't have bits yet, we could still provide some information
1250 // if the instruction is a lossy shift: the lost bits will be marked as
1251 // not used.
1252 unsigned LB, LE;
1253 if (isLossyShiftLeft(MI, OpN, LB, LE) || isLossyShiftRight(MI, OpN, LB, LE)) {
1254 assert(MI.getOperand(OpN).isReg());
1255 BitTracker::RegisterRef RR = MI.getOperand(OpN);
1256 const TargetRegisterClass *RC = HBS::getFinalVRegClass(RR, MRI);
1257 uint16_t Width = RC->getSize()*8;
1258
1259 if (!GotBits)
1260 T.set(Begin, Begin+Width);
1261 assert(LB <= LE && LB < Width && LE <= Width);
1262 T.reset(Begin+LB, Begin+LE);
1263 GotBits = true;
1264 }
1265 if (GotBits)
1266 Bits |= T;
1267 return GotBits;
1268}
1269
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001270// Calculates the used bits in RD ("defined register"), and checks if these
1271// bits in RS ("used register") and RD are identical.
1272bool RedundantInstrElimination::usedBitsEqual(BitTracker::RegisterRef RD,
1273 BitTracker::RegisterRef RS) {
1274 const BitTracker::RegisterCell &DC = BT.lookup(RD.Reg);
1275 const BitTracker::RegisterCell &SC = BT.lookup(RS.Reg);
1276
1277 unsigned DB, DW;
1278 if (!HBS::getSubregMask(RD, DB, DW, MRI))
1279 return false;
1280 unsigned SB, SW;
1281 if (!HBS::getSubregMask(RS, SB, SW, MRI))
1282 return false;
1283 if (SW != DW)
1284 return false;
1285
1286 BitVector Used(DC.width());
1287 if (!computeUsedBits(RD.Reg, Used))
1288 return false;
1289
1290 for (unsigned i = 0; i != DW; ++i)
1291 if (Used[i+DB] && DC[DB+i] != SC[SB+i])
1292 return false;
1293 return true;
1294}
1295
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001296bool RedundantInstrElimination::processBlock(MachineBasicBlock &B,
1297 const RegisterSet&) {
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001298 if (!BT.reached(&B))
1299 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001300 bool Changed = false;
1301
1302 for (auto I = B.begin(), E = B.end(), NextI = I; I != E; ++I) {
1303 NextI = std::next(I);
1304 MachineInstr *MI = &*I;
1305
1306 if (MI->getOpcode() == TargetOpcode::COPY)
1307 continue;
1308 if (MI->hasUnmodeledSideEffects() || MI->isInlineAsm())
1309 continue;
1310 unsigned NumD = MI->getDesc().getNumDefs();
1311 if (NumD != 1)
1312 continue;
1313
1314 BitTracker::RegisterRef RD = MI->getOperand(0);
1315 if (!BT.has(RD.Reg))
1316 continue;
1317 const BitTracker::RegisterCell &DC = BT.lookup(RD.Reg);
Krzysztof Parzyszeka3c5d442016-01-13 15:48:18 +00001318 auto At = MI->isPHI() ? B.getFirstNonPHI()
1319 : MachineBasicBlock::iterator(MI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001320
1321 // Find a source operand that is equal to the result.
1322 for (auto &Op : MI->uses()) {
1323 if (!Op.isReg())
1324 continue;
1325 BitTracker::RegisterRef RS = Op;
1326 if (!BT.has(RS.Reg))
1327 continue;
1328 if (!HBS::isTransparentCopy(RD, RS, MRI))
1329 continue;
1330
1331 unsigned BN, BW;
1332 if (!HBS::getSubregMask(RS, BN, BW, MRI))
1333 continue;
1334
1335 const BitTracker::RegisterCell &SC = BT.lookup(RS.Reg);
1336 if (!usedBitsEqual(RD, RS) && !HBS::isEqual(DC, 0, SC, BN, BW))
1337 continue;
1338
1339 // If found, replace the instruction with a COPY.
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +00001340 const DebugLoc &DL = MI->getDebugLoc();
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001341 const TargetRegisterClass *FRC = HBS::getFinalVRegClass(RD, MRI);
1342 unsigned NewR = MRI.createVirtualRegister(FRC);
Krzysztof Parzyszek6eba5b82016-07-26 19:08:45 +00001343 MachineInstr *CopyI =
1344 BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
1345 .addReg(RS.Reg, 0, RS.Sub);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001346 HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI);
Krzysztof Parzyszek6eba5b82016-07-26 19:08:45 +00001347 // This pass can create copies between registers that don't have the
1348 // exact same values. Updating the tracker has to involve updating
1349 // all dependent cells. Example:
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001350 // vreg1 = inst vreg2 ; vreg1 != vreg2, but used bits are equal
1351 //
1352 // vreg3 = copy vreg2 ; <- inserted
1353 // ... = vreg3 ; <- replaced from vreg2
1354 // Indirectly, we can create a "copy" between vreg1 and vreg2 even
1355 // though their exact values do not match.
Krzysztof Parzyszek6eba5b82016-07-26 19:08:45 +00001356 BT.visit(*CopyI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001357 Changed = true;
1358 break;
1359 }
1360 }
1361
1362 return Changed;
1363}
1364
Eugene Zelenko82085922016-12-13 22:13:50 +00001365namespace {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001366
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001367// Recognize instructions that produce constant values known at compile-time.
1368// Replace them with register definitions that load these constants directly.
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001369 class ConstGeneration : public Transformation {
1370 public:
1371 ConstGeneration(BitTracker &bt, const HexagonInstrInfo &hii,
1372 MachineRegisterInfo &mri)
1373 : Transformation(true), HII(hii), MRI(mri), BT(bt) {}
Eugene Zelenko82085922016-12-13 22:13:50 +00001374
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001375 bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override;
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001376 static bool isTfrConst(const MachineInstr &MI);
Eugene Zelenko82085922016-12-13 22:13:50 +00001377
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001378 private:
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001379 unsigned genTfrConst(const TargetRegisterClass *RC, int64_t C,
1380 MachineBasicBlock &B, MachineBasicBlock::iterator At, DebugLoc &DL);
1381
1382 const HexagonInstrInfo &HII;
1383 MachineRegisterInfo &MRI;
1384 BitTracker &BT;
1385 };
Eugene Zelenko82085922016-12-13 22:13:50 +00001386
1387} // end anonymous namespace
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001388
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001389bool ConstGeneration::isTfrConst(const MachineInstr &MI) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001390 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001391 switch (Opc) {
1392 case Hexagon::A2_combineii:
1393 case Hexagon::A4_combineii:
1394 case Hexagon::A2_tfrsi:
1395 case Hexagon::A2_tfrpi:
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001396 case Hexagon::PS_true:
1397 case Hexagon::PS_false:
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001398 case Hexagon::CONST32:
1399 case Hexagon::CONST64:
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001400 return true;
1401 }
1402 return false;
1403}
1404
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001405// Generate a transfer-immediate instruction that is appropriate for the
1406// register class and the actual value being transferred.
1407unsigned ConstGeneration::genTfrConst(const TargetRegisterClass *RC, int64_t C,
1408 MachineBasicBlock &B, MachineBasicBlock::iterator At, DebugLoc &DL) {
1409 unsigned Reg = MRI.createVirtualRegister(RC);
1410 if (RC == &Hexagon::IntRegsRegClass) {
1411 BuildMI(B, At, DL, HII.get(Hexagon::A2_tfrsi), Reg)
1412 .addImm(int32_t(C));
1413 return Reg;
1414 }
1415
1416 if (RC == &Hexagon::DoubleRegsRegClass) {
1417 if (isInt<8>(C)) {
1418 BuildMI(B, At, DL, HII.get(Hexagon::A2_tfrpi), Reg)
1419 .addImm(C);
1420 return Reg;
1421 }
1422
1423 unsigned Lo = Lo_32(C), Hi = Hi_32(C);
1424 if (isInt<8>(Lo) || isInt<8>(Hi)) {
1425 unsigned Opc = isInt<8>(Lo) ? Hexagon::A2_combineii
1426 : Hexagon::A4_combineii;
1427 BuildMI(B, At, DL, HII.get(Opc), Reg)
1428 .addImm(int32_t(Hi))
1429 .addImm(int32_t(Lo));
1430 return Reg;
1431 }
1432
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001433 BuildMI(B, At, DL, HII.get(Hexagon::CONST64), Reg)
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001434 .addImm(C);
1435 return Reg;
1436 }
1437
1438 if (RC == &Hexagon::PredRegsRegClass) {
1439 unsigned Opc;
1440 if (C == 0)
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001441 Opc = Hexagon::PS_false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001442 else if ((C & 0xFF) == 0xFF)
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001443 Opc = Hexagon::PS_true;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001444 else
1445 return 0;
1446 BuildMI(B, At, DL, HII.get(Opc), Reg);
1447 return Reg;
1448 }
1449
1450 return 0;
1451}
1452
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001453bool ConstGeneration::processBlock(MachineBasicBlock &B, const RegisterSet&) {
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001454 if (!BT.reached(&B))
1455 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001456 bool Changed = false;
1457 RegisterSet Defs;
1458
1459 for (auto I = B.begin(), E = B.end(); I != E; ++I) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001460 if (isTfrConst(*I))
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001461 continue;
1462 Defs.clear();
1463 HBS::getInstrDefs(*I, Defs);
1464 if (Defs.count() != 1)
1465 continue;
1466 unsigned DR = Defs.find_first();
1467 if (!TargetRegisterInfo::isVirtualRegister(DR))
1468 continue;
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001469 uint64_t U;
1470 const BitTracker::RegisterCell &DRC = BT.lookup(DR);
1471 if (HBS::getConst(DRC, 0, DRC.width(), U)) {
1472 int64_t C = U;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001473 DebugLoc DL = I->getDebugLoc();
1474 auto At = I->isPHI() ? B.getFirstNonPHI() : I;
1475 unsigned ImmReg = genTfrConst(MRI.getRegClass(DR), C, B, At, DL);
1476 if (ImmReg) {
1477 HBS::replaceReg(DR, ImmReg, MRI);
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001478 BT.put(ImmReg, DRC);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001479 Changed = true;
1480 }
1481 }
1482 }
1483 return Changed;
1484}
1485
Eugene Zelenko82085922016-12-13 22:13:50 +00001486namespace {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001487
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001488// Identify pairs of available registers which hold identical values.
1489// In such cases, only one of them needs to be calculated, the other one
1490// will be defined as a copy of the first.
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001491 class CopyGeneration : public Transformation {
1492 public:
1493 CopyGeneration(BitTracker &bt, const HexagonInstrInfo &hii,
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001494 const HexagonRegisterInfo &hri, MachineRegisterInfo &mri)
1495 : Transformation(true), HII(hii), HRI(hri), MRI(mri), BT(bt) {}
Eugene Zelenko82085922016-12-13 22:13:50 +00001496
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001497 bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override;
Eugene Zelenko82085922016-12-13 22:13:50 +00001498
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001499 private:
1500 bool findMatch(const BitTracker::RegisterRef &Inp,
1501 BitTracker::RegisterRef &Out, const RegisterSet &AVs);
1502
1503 const HexagonInstrInfo &HII;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001504 const HexagonRegisterInfo &HRI;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001505 MachineRegisterInfo &MRI;
1506 BitTracker &BT;
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001507 RegisterSet Forbidden;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001508 };
1509
Eugene Zelenko82085922016-12-13 22:13:50 +00001510// Eliminate register copies RD = RS, by replacing the uses of RD with
1511// with uses of RS.
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001512 class CopyPropagation : public Transformation {
1513 public:
1514 CopyPropagation(const HexagonRegisterInfo &hri, MachineRegisterInfo &mri)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001515 : Transformation(false), HRI(hri), MRI(mri) {}
Eugene Zelenko82085922016-12-13 22:13:50 +00001516
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001517 bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override;
Eugene Zelenko82085922016-12-13 22:13:50 +00001518
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +00001519 static bool isCopyReg(unsigned Opc, bool NoConv);
Eugene Zelenko82085922016-12-13 22:13:50 +00001520
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001521 private:
1522 bool propagateRegCopy(MachineInstr &MI);
1523
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001524 const HexagonRegisterInfo &HRI;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001525 MachineRegisterInfo &MRI;
1526 };
1527
Eugene Zelenko82085922016-12-13 22:13:50 +00001528} // end anonymous namespace
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001529
1530/// Check if there is a register in AVs that is identical to Inp. If so,
1531/// set Out to the found register. The output may be a pair Reg:Sub.
1532bool CopyGeneration::findMatch(const BitTracker::RegisterRef &Inp,
1533 BitTracker::RegisterRef &Out, const RegisterSet &AVs) {
1534 if (!BT.has(Inp.Reg))
1535 return false;
1536 const BitTracker::RegisterCell &InpRC = BT.lookup(Inp.Reg);
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001537 auto *FRC = HBS::getFinalVRegClass(Inp, MRI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001538 unsigned B, W;
1539 if (!HBS::getSubregMask(Inp, B, W, MRI))
1540 return false;
1541
1542 for (unsigned R = AVs.find_first(); R; R = AVs.find_next(R)) {
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001543 if (!BT.has(R) || Forbidden[R])
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001544 continue;
1545 const BitTracker::RegisterCell &RC = BT.lookup(R);
1546 unsigned RW = RC.width();
1547 if (W == RW) {
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001548 if (FRC != MRI.getRegClass(R))
1549 continue;
1550 if (!HBS::isTransparentCopy(R, Inp, MRI))
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001551 continue;
1552 if (!HBS::isEqual(InpRC, B, RC, 0, W))
1553 continue;
1554 Out.Reg = R;
1555 Out.Sub = 0;
1556 return true;
1557 }
1558 // Check if there is a super-register, whose part (with a subregister)
1559 // is equal to the input.
1560 // Only do double registers for now.
1561 if (W*2 != RW)
1562 continue;
1563 if (MRI.getRegClass(R) != &Hexagon::DoubleRegsRegClass)
1564 continue;
1565
1566 if (HBS::isEqual(InpRC, B, RC, 0, W))
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001567 Out.Sub = Hexagon::isub_lo;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001568 else if (HBS::isEqual(InpRC, B, RC, W, W))
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001569 Out.Sub = Hexagon::isub_hi;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001570 else
1571 continue;
1572 Out.Reg = R;
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001573 if (HBS::isTransparentCopy(Out, Inp, MRI))
1574 return true;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001575 }
1576 return false;
1577}
1578
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001579bool CopyGeneration::processBlock(MachineBasicBlock &B,
1580 const RegisterSet &AVs) {
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001581 if (!BT.reached(&B))
1582 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001583 RegisterSet AVB(AVs);
1584 bool Changed = false;
1585 RegisterSet Defs;
1586
1587 for (auto I = B.begin(), E = B.end(), NextI = I; I != E;
1588 ++I, AVB.insert(Defs)) {
1589 NextI = std::next(I);
1590 Defs.clear();
1591 HBS::getInstrDefs(*I, Defs);
1592
1593 unsigned Opc = I->getOpcode();
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +00001594 if (CopyPropagation::isCopyReg(Opc, false) ||
1595 ConstGeneration::isTfrConst(*I))
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001596 continue;
1597
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001598 DebugLoc DL = I->getDebugLoc();
1599 auto At = I->isPHI() ? B.getFirstNonPHI() : I;
1600
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001601 for (unsigned R = Defs.find_first(); R; R = Defs.find_next(R)) {
1602 BitTracker::RegisterRef MR;
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001603 auto *FRC = HBS::getFinalVRegClass(R, MRI);
1604
1605 if (findMatch(R, MR, AVB)) {
1606 unsigned NewR = MRI.createVirtualRegister(FRC);
1607 BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
1608 .addReg(MR.Reg, 0, MR.Sub);
1609 BT.put(BitTracker::RegisterRef(NewR), BT.get(MR));
1610 HBS::replaceReg(R, NewR, MRI);
1611 Forbidden.insert(R);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001612 continue;
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001613 }
1614
Krzysztof Parzyszek824d3472016-08-02 21:49:20 +00001615 if (FRC == &Hexagon::DoubleRegsRegClass ||
1616 FRC == &Hexagon::VecDblRegsRegClass ||
1617 FRC == &Hexagon::VecDblRegs128BRegClass) {
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001618 // Try to generate REG_SEQUENCE.
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001619 unsigned SubLo = HRI.getHexagonSubRegIndex(FRC, Hexagon::ps_sub_lo);
1620 unsigned SubHi = HRI.getHexagonSubRegIndex(FRC, Hexagon::ps_sub_hi);
1621 BitTracker::RegisterRef TL = { R, SubLo };
1622 BitTracker::RegisterRef TH = { R, SubHi };
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001623 BitTracker::RegisterRef ML, MH;
1624 if (findMatch(TL, ML, AVB) && findMatch(TH, MH, AVB)) {
1625 auto *FRC = HBS::getFinalVRegClass(R, MRI);
1626 unsigned NewR = MRI.createVirtualRegister(FRC);
1627 BuildMI(B, At, DL, HII.get(TargetOpcode::REG_SEQUENCE), NewR)
1628 .addReg(ML.Reg, 0, ML.Sub)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001629 .addImm(SubLo)
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001630 .addReg(MH.Reg, 0, MH.Sub)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001631 .addImm(SubHi);
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00001632 BT.put(BitTracker::RegisterRef(NewR), BT.get(R));
1633 HBS::replaceReg(R, NewR, MRI);
1634 Forbidden.insert(R);
1635 }
1636 }
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001637 }
1638 }
1639
1640 return Changed;
1641}
1642
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +00001643bool CopyPropagation::isCopyReg(unsigned Opc, bool NoConv) {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001644 switch (Opc) {
1645 case TargetOpcode::COPY:
1646 case TargetOpcode::REG_SEQUENCE:
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +00001647 case Hexagon::A4_combineir:
1648 case Hexagon::A4_combineri:
1649 return true;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001650 case Hexagon::A2_tfr:
1651 case Hexagon::A2_tfrp:
1652 case Hexagon::A2_combinew:
Krzysztof Parzyszek824d3472016-08-02 21:49:20 +00001653 case Hexagon::V6_vcombine:
1654 case Hexagon::V6_vcombine_128B:
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +00001655 return NoConv;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001656 default:
1657 break;
1658 }
1659 return false;
1660}
1661
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001662bool CopyPropagation::propagateRegCopy(MachineInstr &MI) {
1663 bool Changed = false;
1664 unsigned Opc = MI.getOpcode();
1665 BitTracker::RegisterRef RD = MI.getOperand(0);
1666 assert(MI.getOperand(0).getSubReg() == 0);
1667
1668 switch (Opc) {
1669 case TargetOpcode::COPY:
1670 case Hexagon::A2_tfr:
1671 case Hexagon::A2_tfrp: {
1672 BitTracker::RegisterRef RS = MI.getOperand(1);
1673 if (!HBS::isTransparentCopy(RD, RS, MRI))
1674 break;
1675 if (RS.Sub != 0)
1676 Changed = HBS::replaceRegWithSub(RD.Reg, RS.Reg, RS.Sub, MRI);
1677 else
1678 Changed = HBS::replaceReg(RD.Reg, RS.Reg, MRI);
1679 break;
1680 }
1681 case TargetOpcode::REG_SEQUENCE: {
1682 BitTracker::RegisterRef SL, SH;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001683 if (HBS::parseRegSequence(MI, SL, SH, MRI)) {
1684 const TargetRegisterClass *RC = MRI.getRegClass(RD.Reg);
1685 unsigned SubLo = HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_lo);
1686 unsigned SubHi = HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_hi);
1687 Changed = HBS::replaceSubWithSub(RD.Reg, SubLo, SL.Reg, SL.Sub, MRI);
1688 Changed |= HBS::replaceSubWithSub(RD.Reg, SubHi, SH.Reg, SH.Sub, MRI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001689 }
1690 break;
1691 }
Krzysztof Parzyszek824d3472016-08-02 21:49:20 +00001692 case Hexagon::A2_combinew:
1693 case Hexagon::V6_vcombine:
1694 case Hexagon::V6_vcombine_128B: {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001695 const TargetRegisterClass *RC = MRI.getRegClass(RD.Reg);
1696 unsigned SubLo = HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_lo);
1697 unsigned SubHi = HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_hi);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001698 BitTracker::RegisterRef RH = MI.getOperand(1), RL = MI.getOperand(2);
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001699 Changed = HBS::replaceSubWithSub(RD.Reg, SubLo, RL.Reg, RL.Sub, MRI);
1700 Changed |= HBS::replaceSubWithSub(RD.Reg, SubHi, RH.Reg, RH.Sub, MRI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001701 break;
1702 }
1703 case Hexagon::A4_combineir:
1704 case Hexagon::A4_combineri: {
1705 unsigned SrcX = (Opc == Hexagon::A4_combineir) ? 2 : 1;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001706 unsigned Sub = (Opc == Hexagon::A4_combineir) ? Hexagon::isub_lo
1707 : Hexagon::isub_hi;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001708 BitTracker::RegisterRef RS = MI.getOperand(SrcX);
1709 Changed = HBS::replaceSubWithSub(RD.Reg, Sub, RS.Reg, RS.Sub, MRI);
1710 break;
1711 }
1712 }
1713 return Changed;
1714}
1715
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001716bool CopyPropagation::processBlock(MachineBasicBlock &B, const RegisterSet&) {
1717 std::vector<MachineInstr*> Instrs;
1718 for (auto I = B.rbegin(), E = B.rend(); I != E; ++I)
1719 Instrs.push_back(&*I);
1720
1721 bool Changed = false;
1722 for (auto I : Instrs) {
1723 unsigned Opc = I->getOpcode();
Krzysztof Parzyszek23ee12e2016-08-03 18:35:48 +00001724 if (!CopyPropagation::isCopyReg(Opc, true))
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001725 continue;
1726 Changed |= propagateRegCopy(*I);
1727 }
1728
1729 return Changed;
1730}
1731
Eugene Zelenko82085922016-12-13 22:13:50 +00001732namespace {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001733
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001734// Recognize patterns that can be simplified and replace them with the
1735// simpler forms.
1736// This is by no means complete
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001737 class BitSimplification : public Transformation {
1738 public:
1739 BitSimplification(BitTracker &bt, const HexagonInstrInfo &hii,
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00001740 const HexagonRegisterInfo &hri, MachineRegisterInfo &mri,
1741 MachineFunction &mf)
1742 : Transformation(true), HII(hii), HRI(hri), MRI(mri), MF(mf), BT(bt) {}
Eugene Zelenko82085922016-12-13 22:13:50 +00001743
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001744 bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override;
Eugene Zelenko82085922016-12-13 22:13:50 +00001745
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001746 private:
1747 struct RegHalf : public BitTracker::RegisterRef {
1748 bool Low; // Low/High halfword.
1749 };
1750
1751 bool matchHalf(unsigned SelfR, const BitTracker::RegisterCell &RC,
1752 unsigned B, RegHalf &RH);
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00001753 bool validateReg(BitTracker::RegisterRef R, unsigned Opc, unsigned OpNum);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001754
1755 bool matchPackhl(unsigned SelfR, const BitTracker::RegisterCell &RC,
1756 BitTracker::RegisterRef &Rs, BitTracker::RegisterRef &Rt);
1757 unsigned getCombineOpcode(bool HLow, bool LLow);
1758
1759 bool genStoreUpperHalf(MachineInstr *MI);
1760 bool genStoreImmediate(MachineInstr *MI);
1761 bool genPackhl(MachineInstr *MI, BitTracker::RegisterRef RD,
1762 const BitTracker::RegisterCell &RC);
1763 bool genExtractHalf(MachineInstr *MI, BitTracker::RegisterRef RD,
1764 const BitTracker::RegisterCell &RC);
1765 bool genCombineHalf(MachineInstr *MI, BitTracker::RegisterRef RD,
1766 const BitTracker::RegisterCell &RC);
1767 bool genExtractLow(MachineInstr *MI, BitTracker::RegisterRef RD,
1768 const BitTracker::RegisterCell &RC);
1769 bool simplifyTstbit(MachineInstr *MI, BitTracker::RegisterRef RD,
1770 const BitTracker::RegisterCell &RC);
1771
1772 const HexagonInstrInfo &HII;
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00001773 const HexagonRegisterInfo &HRI;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001774 MachineRegisterInfo &MRI;
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00001775 MachineFunction &MF;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001776 BitTracker &BT;
1777 };
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001778
Eugene Zelenko82085922016-12-13 22:13:50 +00001779} // end anonymous namespace
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001780
1781// Check if the bits [B..B+16) in register cell RC form a valid halfword,
1782// i.e. [0..16), [16..32), etc. of some register. If so, return true and
1783// set the information about the found register in RH.
1784bool BitSimplification::matchHalf(unsigned SelfR,
1785 const BitTracker::RegisterCell &RC, unsigned B, RegHalf &RH) {
1786 // XXX This could be searching in the set of available registers, in case
1787 // the match is not exact.
1788
1789 // Match 16-bit chunks, where the RC[B..B+15] references exactly one
1790 // register and all the bits B..B+15 match between RC and the register.
1791 // This is meant to match "v1[0-15]", where v1 = { [0]:0 [1-15]:v1... },
1792 // and RC = { [0]:0 [1-15]:v1[1-15]... }.
1793 bool Low = false;
1794 unsigned I = B;
1795 while (I < B+16 && RC[I].num())
1796 I++;
1797 if (I == B+16)
1798 return false;
1799
1800 unsigned Reg = RC[I].RefI.Reg;
1801 unsigned P = RC[I].RefI.Pos; // The RefI.Pos will be advanced by I-B.
1802 if (P < I-B)
1803 return false;
1804 unsigned Pos = P - (I-B);
1805
1806 if (Reg == 0 || Reg == SelfR) // Don't match "self".
1807 return false;
1808 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1809 return false;
1810 if (!BT.has(Reg))
1811 return false;
1812
1813 const BitTracker::RegisterCell &SC = BT.lookup(Reg);
1814 if (Pos+16 > SC.width())
1815 return false;
1816
1817 for (unsigned i = 0; i < 16; ++i) {
1818 const BitTracker::BitValue &RV = RC[i+B];
1819 if (RV.Type == BitTracker::BitValue::Ref) {
1820 if (RV.RefI.Reg != Reg)
1821 return false;
1822 if (RV.RefI.Pos != i+Pos)
1823 return false;
1824 continue;
1825 }
1826 if (RC[i+B] != SC[i+Pos])
1827 return false;
1828 }
1829
1830 unsigned Sub = 0;
1831 switch (Pos) {
1832 case 0:
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001833 Sub = Hexagon::isub_lo;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001834 Low = true;
1835 break;
1836 case 16:
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001837 Sub = Hexagon::isub_lo;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001838 Low = false;
1839 break;
1840 case 32:
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001841 Sub = Hexagon::isub_hi;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001842 Low = true;
1843 break;
1844 case 48:
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001845 Sub = Hexagon::isub_hi;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001846 Low = false;
1847 break;
1848 default:
1849 return false;
1850 }
1851
1852 RH.Reg = Reg;
1853 RH.Sub = Sub;
1854 RH.Low = Low;
1855 // If the subregister is not valid with the register, set it to 0.
1856 if (!HBS::getFinalVRegClass(RH, MRI))
1857 RH.Sub = 0;
1858
1859 return true;
1860}
1861
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00001862bool BitSimplification::validateReg(BitTracker::RegisterRef R, unsigned Opc,
1863 unsigned OpNum) {
1864 auto *OpRC = HII.getRegClass(HII.get(Opc), OpNum, &HRI, MF);
1865 auto *RRC = HBS::getFinalVRegClass(R, MRI);
1866 return OpRC->hasSubClassEq(RRC);
1867}
1868
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001869// Check if RC matches the pattern of a S2_packhl. If so, return true and
1870// set the inputs Rs and Rt.
1871bool BitSimplification::matchPackhl(unsigned SelfR,
1872 const BitTracker::RegisterCell &RC, BitTracker::RegisterRef &Rs,
1873 BitTracker::RegisterRef &Rt) {
1874 RegHalf L1, H1, L2, H2;
1875
1876 if (!matchHalf(SelfR, RC, 0, L2) || !matchHalf(SelfR, RC, 16, L1))
1877 return false;
1878 if (!matchHalf(SelfR, RC, 32, H2) || !matchHalf(SelfR, RC, 48, H1))
1879 return false;
1880
1881 // Rs = H1.L1, Rt = H2.L2
1882 if (H1.Reg != L1.Reg || H1.Sub != L1.Sub || H1.Low || !L1.Low)
1883 return false;
1884 if (H2.Reg != L2.Reg || H2.Sub != L2.Sub || H2.Low || !L2.Low)
1885 return false;
1886
1887 Rs = H1;
1888 Rt = H2;
1889 return true;
1890}
1891
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001892unsigned BitSimplification::getCombineOpcode(bool HLow, bool LLow) {
1893 return HLow ? LLow ? Hexagon::A2_combine_ll
1894 : Hexagon::A2_combine_lh
1895 : LLow ? Hexagon::A2_combine_hl
1896 : Hexagon::A2_combine_hh;
1897}
1898
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001899// If MI stores the upper halfword of a register (potentially obtained via
1900// shifts or extracts), replace it with a storerf instruction. This could
1901// cause the "extraction" code to become dead.
1902bool BitSimplification::genStoreUpperHalf(MachineInstr *MI) {
1903 unsigned Opc = MI->getOpcode();
1904 if (Opc != Hexagon::S2_storerh_io)
1905 return false;
1906
1907 MachineOperand &ValOp = MI->getOperand(2);
1908 BitTracker::RegisterRef RS = ValOp;
1909 if (!BT.has(RS.Reg))
1910 return false;
1911 const BitTracker::RegisterCell &RC = BT.lookup(RS.Reg);
1912 RegHalf H;
1913 if (!matchHalf(0, RC, 0, H))
1914 return false;
1915 if (H.Low)
1916 return false;
1917 MI->setDesc(HII.get(Hexagon::S2_storerf_io));
1918 ValOp.setReg(H.Reg);
1919 ValOp.setSubReg(H.Sub);
1920 return true;
1921}
1922
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001923// If MI stores a value known at compile-time, and the value is within a range
1924// that avoids using constant-extenders, replace it with a store-immediate.
1925bool BitSimplification::genStoreImmediate(MachineInstr *MI) {
1926 unsigned Opc = MI->getOpcode();
1927 unsigned Align = 0;
1928 switch (Opc) {
1929 case Hexagon::S2_storeri_io:
1930 Align++;
1931 case Hexagon::S2_storerh_io:
1932 Align++;
1933 case Hexagon::S2_storerb_io:
1934 break;
1935 default:
1936 return false;
1937 }
1938
1939 // Avoid stores to frame-indices (due to an unknown offset).
1940 if (!MI->getOperand(0).isReg())
1941 return false;
1942 MachineOperand &OffOp = MI->getOperand(1);
1943 if (!OffOp.isImm())
1944 return false;
1945
1946 int64_t Off = OffOp.getImm();
1947 // Offset is u6:a. Sadly, there is no isShiftedUInt(n,x).
1948 if (!isUIntN(6+Align, Off) || (Off & ((1<<Align)-1)))
1949 return false;
1950 // Source register:
1951 BitTracker::RegisterRef RS = MI->getOperand(2);
1952 if (!BT.has(RS.Reg))
1953 return false;
1954 const BitTracker::RegisterCell &RC = BT.lookup(RS.Reg);
1955 uint64_t U;
1956 if (!HBS::getConst(RC, 0, RC.width(), U))
1957 return false;
1958
1959 // Only consider 8-bit values to avoid constant-extenders.
1960 int V;
1961 switch (Opc) {
1962 case Hexagon::S2_storerb_io:
1963 V = int8_t(U);
1964 break;
1965 case Hexagon::S2_storerh_io:
1966 V = int16_t(U);
1967 break;
1968 case Hexagon::S2_storeri_io:
1969 V = int32_t(U);
1970 break;
1971 }
1972 if (!isInt<8>(V))
1973 return false;
1974
1975 MI->RemoveOperand(2);
1976 switch (Opc) {
1977 case Hexagon::S2_storerb_io:
1978 MI->setDesc(HII.get(Hexagon::S4_storeirb_io));
1979 break;
1980 case Hexagon::S2_storerh_io:
1981 MI->setDesc(HII.get(Hexagon::S4_storeirh_io));
1982 break;
1983 case Hexagon::S2_storeri_io:
1984 MI->setDesc(HII.get(Hexagon::S4_storeiri_io));
1985 break;
1986 }
1987 MI->addOperand(MachineOperand::CreateImm(V));
1988 return true;
1989}
1990
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00001991// If MI is equivalent o S2_packhl, generate the S2_packhl. MI could be the
1992// last instruction in a sequence that results in something equivalent to
1993// the pack-halfwords. The intent is to cause the entire sequence to become
1994// dead.
1995bool BitSimplification::genPackhl(MachineInstr *MI,
1996 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) {
1997 unsigned Opc = MI->getOpcode();
1998 if (Opc == Hexagon::S2_packhl)
1999 return false;
2000 BitTracker::RegisterRef Rs, Rt;
2001 if (!matchPackhl(RD.Reg, RC, Rs, Rt))
2002 return false;
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00002003 if (!validateReg(Rs, Hexagon::S2_packhl, 1) ||
2004 !validateReg(Rt, Hexagon::S2_packhl, 2))
2005 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002006
2007 MachineBasicBlock &B = *MI->getParent();
2008 unsigned NewR = MRI.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
2009 DebugLoc DL = MI->getDebugLoc();
Krzysztof Parzyszeka3c5d442016-01-13 15:48:18 +00002010 auto At = MI->isPHI() ? B.getFirstNonPHI()
2011 : MachineBasicBlock::iterator(MI);
2012 BuildMI(B, At, DL, HII.get(Hexagon::S2_packhl), NewR)
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002013 .addReg(Rs.Reg, 0, Rs.Sub)
2014 .addReg(Rt.Reg, 0, Rt.Sub);
2015 HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI);
2016 BT.put(BitTracker::RegisterRef(NewR), RC);
2017 return true;
2018}
2019
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002020// If MI produces halfword of the input in the low half of the output,
2021// replace it with zero-extend or extractu.
2022bool BitSimplification::genExtractHalf(MachineInstr *MI,
2023 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) {
2024 RegHalf L;
2025 // Check for halfword in low 16 bits, zeros elsewhere.
2026 if (!matchHalf(RD.Reg, RC, 0, L) || !HBS::isZero(RC, 16, 16))
2027 return false;
2028
2029 unsigned Opc = MI->getOpcode();
2030 MachineBasicBlock &B = *MI->getParent();
2031 DebugLoc DL = MI->getDebugLoc();
2032
2033 // Prefer zxth, since zxth can go in any slot, while extractu only in
2034 // slots 2 and 3.
2035 unsigned NewR = 0;
Krzysztof Parzyszeka3c5d442016-01-13 15:48:18 +00002036 auto At = MI->isPHI() ? B.getFirstNonPHI()
2037 : MachineBasicBlock::iterator(MI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002038 if (L.Low && Opc != Hexagon::A2_zxth) {
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00002039 if (validateReg(L, Hexagon::A2_zxth, 1)) {
2040 NewR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2041 BuildMI(B, At, DL, HII.get(Hexagon::A2_zxth), NewR)
2042 .addReg(L.Reg, 0, L.Sub);
2043 }
Krzysztof Parzyszek0d112122016-01-14 21:59:22 +00002044 } else if (!L.Low && Opc != Hexagon::S2_lsr_i_r) {
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00002045 if (validateReg(L, Hexagon::S2_lsr_i_r, 1)) {
2046 NewR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2047 BuildMI(B, MI, DL, HII.get(Hexagon::S2_lsr_i_r), NewR)
2048 .addReg(L.Reg, 0, L.Sub)
2049 .addImm(16);
2050 }
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002051 }
2052 if (NewR == 0)
2053 return false;
2054 HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI);
2055 BT.put(BitTracker::RegisterRef(NewR), RC);
2056 return true;
2057}
2058
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002059// If MI is equivalent to a combine(.L/.H, .L/.H) replace with with the
2060// combine.
2061bool BitSimplification::genCombineHalf(MachineInstr *MI,
2062 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) {
2063 RegHalf L, H;
2064 // Check for combine h/l
2065 if (!matchHalf(RD.Reg, RC, 0, L) || !matchHalf(RD.Reg, RC, 16, H))
2066 return false;
2067 // Do nothing if this is just a reg copy.
2068 if (L.Reg == H.Reg && L.Sub == H.Sub && !H.Low && L.Low)
2069 return false;
2070
2071 unsigned Opc = MI->getOpcode();
2072 unsigned COpc = getCombineOpcode(H.Low, L.Low);
2073 if (COpc == Opc)
2074 return false;
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00002075 if (!validateReg(H, COpc, 1) || !validateReg(L, COpc, 2))
2076 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002077
2078 MachineBasicBlock &B = *MI->getParent();
2079 DebugLoc DL = MI->getDebugLoc();
2080 unsigned NewR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
Krzysztof Parzyszeka3c5d442016-01-13 15:48:18 +00002081 auto At = MI->isPHI() ? B.getFirstNonPHI()
2082 : MachineBasicBlock::iterator(MI);
2083 BuildMI(B, At, DL, HII.get(COpc), NewR)
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002084 .addReg(H.Reg, 0, H.Sub)
2085 .addReg(L.Reg, 0, L.Sub);
2086 HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI);
2087 BT.put(BitTracker::RegisterRef(NewR), RC);
2088 return true;
2089}
2090
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002091// If MI resets high bits of a register and keeps the lower ones, replace it
2092// with zero-extend byte/half, and-immediate, or extractu, as appropriate.
2093bool BitSimplification::genExtractLow(MachineInstr *MI,
2094 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) {
2095 unsigned Opc = MI->getOpcode();
2096 switch (Opc) {
2097 case Hexagon::A2_zxtb:
2098 case Hexagon::A2_zxth:
2099 case Hexagon::S2_extractu:
2100 return false;
2101 }
2102 if (Opc == Hexagon::A2_andir && MI->getOperand(2).isImm()) {
2103 int32_t Imm = MI->getOperand(2).getImm();
2104 if (isInt<10>(Imm))
2105 return false;
2106 }
2107
2108 if (MI->hasUnmodeledSideEffects() || MI->isInlineAsm())
2109 return false;
2110 unsigned W = RC.width();
2111 while (W > 0 && RC[W-1].is(0))
2112 W--;
2113 if (W == 0 || W == RC.width())
2114 return false;
2115 unsigned NewOpc = (W == 8) ? Hexagon::A2_zxtb
2116 : (W == 16) ? Hexagon::A2_zxth
2117 : (W < 10) ? Hexagon::A2_andir
2118 : Hexagon::S2_extractu;
2119 MachineBasicBlock &B = *MI->getParent();
2120 DebugLoc DL = MI->getDebugLoc();
2121
2122 for (auto &Op : MI->uses()) {
2123 if (!Op.isReg())
2124 continue;
2125 BitTracker::RegisterRef RS = Op;
2126 if (!BT.has(RS.Reg))
2127 continue;
2128 const BitTracker::RegisterCell &SC = BT.lookup(RS.Reg);
2129 unsigned BN, BW;
2130 if (!HBS::getSubregMask(RS, BN, BW, MRI))
2131 continue;
2132 if (BW < W || !HBS::isEqual(RC, 0, SC, BN, W))
2133 continue;
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00002134 if (!validateReg(RS, NewOpc, 1))
2135 continue;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002136
2137 unsigned NewR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
Krzysztof Parzyszeka3c5d442016-01-13 15:48:18 +00002138 auto At = MI->isPHI() ? B.getFirstNonPHI()
2139 : MachineBasicBlock::iterator(MI);
2140 auto MIB = BuildMI(B, At, DL, HII.get(NewOpc), NewR)
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002141 .addReg(RS.Reg, 0, RS.Sub);
2142 if (NewOpc == Hexagon::A2_andir)
2143 MIB.addImm((1 << W) - 1);
2144 else if (NewOpc == Hexagon::S2_extractu)
2145 MIB.addImm(W).addImm(0);
2146 HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI);
2147 BT.put(BitTracker::RegisterRef(NewR), RC);
2148 return true;
2149 }
2150 return false;
2151}
2152
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002153// Check for tstbit simplification opportunity, where the bit being checked
2154// can be tracked back to another register. For example:
2155// vreg2 = S2_lsr_i_r vreg1, 5
2156// vreg3 = S2_tstbit_i vreg2, 0
2157// =>
2158// vreg3 = S2_tstbit_i vreg1, 5
2159bool BitSimplification::simplifyTstbit(MachineInstr *MI,
2160 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) {
2161 unsigned Opc = MI->getOpcode();
2162 if (Opc != Hexagon::S2_tstbit_i)
2163 return false;
2164
2165 unsigned BN = MI->getOperand(2).getImm();
2166 BitTracker::RegisterRef RS = MI->getOperand(1);
2167 unsigned F, W;
2168 DebugLoc DL = MI->getDebugLoc();
2169 if (!BT.has(RS.Reg) || !HBS::getSubregMask(RS, F, W, MRI))
2170 return false;
2171 MachineBasicBlock &B = *MI->getParent();
Krzysztof Parzyszeka3c5d442016-01-13 15:48:18 +00002172 auto At = MI->isPHI() ? B.getFirstNonPHI()
2173 : MachineBasicBlock::iterator(MI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002174
2175 const BitTracker::RegisterCell &SC = BT.lookup(RS.Reg);
2176 const BitTracker::BitValue &V = SC[F+BN];
2177 if (V.Type == BitTracker::BitValue::Ref && V.RefI.Reg != RS.Reg) {
2178 const TargetRegisterClass *TC = MRI.getRegClass(V.RefI.Reg);
2179 // Need to map V.RefI.Reg to a 32-bit register, i.e. if it is
2180 // a double register, need to use a subregister and adjust bit
2181 // number.
Eugene Zelenko82085922016-12-13 22:13:50 +00002182 unsigned P = std::numeric_limits<unsigned>::max();
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002183 BitTracker::RegisterRef RR(V.RefI.Reg, 0);
2184 if (TC == &Hexagon::DoubleRegsRegClass) {
2185 P = V.RefI.Pos;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002186 RR.Sub = Hexagon::isub_lo;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002187 if (P >= 32) {
2188 P -= 32;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002189 RR.Sub = Hexagon::isub_hi;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002190 }
2191 } else if (TC == &Hexagon::IntRegsRegClass) {
2192 P = V.RefI.Pos;
2193 }
Eugene Zelenko82085922016-12-13 22:13:50 +00002194 if (P != std::numeric_limits<unsigned>::max()) {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002195 unsigned NewR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
Krzysztof Parzyszeka3c5d442016-01-13 15:48:18 +00002196 BuildMI(B, At, DL, HII.get(Hexagon::S2_tstbit_i), NewR)
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002197 .addReg(RR.Reg, 0, RR.Sub)
2198 .addImm(P);
2199 HBS::replaceReg(RD.Reg, NewR, MRI);
2200 BT.put(NewR, RC);
2201 return true;
2202 }
2203 } else if (V.is(0) || V.is(1)) {
2204 unsigned NewR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002205 unsigned NewOpc = V.is(0) ? Hexagon::PS_false : Hexagon::PS_true;
Krzysztof Parzyszeka3c5d442016-01-13 15:48:18 +00002206 BuildMI(B, At, DL, HII.get(NewOpc), NewR);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002207 HBS::replaceReg(RD.Reg, NewR, MRI);
2208 return true;
2209 }
2210
2211 return false;
2212}
2213
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002214bool BitSimplification::processBlock(MachineBasicBlock &B,
2215 const RegisterSet &AVs) {
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00002216 if (!BT.reached(&B))
2217 return false;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002218 bool Changed = false;
2219 RegisterSet AVB = AVs;
2220 RegisterSet Defs;
2221
2222 for (auto I = B.begin(), E = B.end(); I != E; ++I, AVB.insert(Defs)) {
2223 MachineInstr *MI = &*I;
2224 Defs.clear();
2225 HBS::getInstrDefs(*MI, Defs);
2226
2227 unsigned Opc = MI->getOpcode();
2228 if (Opc == TargetOpcode::COPY || Opc == TargetOpcode::REG_SEQUENCE)
2229 continue;
2230
2231 if (MI->mayStore()) {
2232 bool T = genStoreUpperHalf(MI);
2233 T = T || genStoreImmediate(MI);
2234 Changed |= T;
2235 continue;
2236 }
2237
2238 if (Defs.count() != 1)
2239 continue;
2240 const MachineOperand &Op0 = MI->getOperand(0);
2241 if (!Op0.isReg() || !Op0.isDef())
2242 continue;
2243 BitTracker::RegisterRef RD = Op0;
2244 if (!BT.has(RD.Reg))
2245 continue;
2246 const TargetRegisterClass *FRC = HBS::getFinalVRegClass(RD, MRI);
2247 const BitTracker::RegisterCell &RC = BT.lookup(RD.Reg);
2248
2249 if (FRC->getID() == Hexagon::DoubleRegsRegClassID) {
2250 bool T = genPackhl(MI, RD, RC);
2251 Changed |= T;
2252 continue;
2253 }
2254
2255 if (FRC->getID() == Hexagon::IntRegsRegClassID) {
2256 bool T = genExtractHalf(MI, RD, RC);
2257 T = T || genCombineHalf(MI, RD, RC);
2258 T = T || genExtractLow(MI, RD, RC);
2259 Changed |= T;
2260 continue;
2261 }
2262
2263 if (FRC->getID() == Hexagon::PredRegsRegClassID) {
2264 bool T = simplifyTstbit(MI, RD, RC);
2265 Changed |= T;
2266 continue;
2267 }
2268 }
2269 return Changed;
2270}
2271
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002272bool HexagonBitSimplify::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +00002273 if (skipFunction(*MF.getFunction()))
2274 return false;
2275
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002276 auto &HST = MF.getSubtarget<HexagonSubtarget>();
2277 auto &HRI = *HST.getRegisterInfo();
2278 auto &HII = *HST.getInstrInfo();
2279
2280 MDT = &getAnalysis<MachineDominatorTree>();
2281 MachineRegisterInfo &MRI = MF.getRegInfo();
2282 bool Changed;
2283
2284 Changed = DeadCodeElimination(MF, *MDT).run();
2285
2286 const HexagonEvaluator HE(HRI, MRI, HII, MF);
2287 BitTracker BT(HE, MF);
2288 DEBUG(BT.trace(true));
2289 BT.run();
2290
2291 MachineBasicBlock &Entry = MF.front();
2292
2293 RegisterSet AIG; // Available registers for IG.
2294 ConstGeneration ImmG(BT, HII, MRI);
2295 Changed |= visitBlock(Entry, ImmG, AIG);
2296
2297 RegisterSet ARE; // Available registers for RIE.
2298 RedundantInstrElimination RIE(BT, HII, MRI);
Krzysztof Parzyszek1adca302016-07-26 18:30:11 +00002299 bool Ried = visitBlock(Entry, RIE, ARE);
2300 if (Ried) {
2301 Changed = true;
2302 BT.run();
2303 }
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002304
2305 RegisterSet ACG; // Available registers for CG.
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00002306 CopyGeneration CopyG(BT, HII, HRI, MRI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002307 Changed |= visitBlock(Entry, CopyG, ACG);
2308
2309 RegisterSet ACP; // Available registers for CP.
2310 CopyPropagation CopyP(HRI, MRI);
2311 Changed |= visitBlock(Entry, CopyP, ACP);
2312
2313 Changed = DeadCodeElimination(MF, *MDT).run() || Changed;
2314
2315 BT.run();
2316 RegisterSet ABS; // Available registers for BS.
Krzysztof Parzyszek04c07962016-08-04 17:56:19 +00002317 BitSimplification BitS(BT, HII, HRI, MRI, MF);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002318 Changed |= visitBlock(Entry, BitS, ABS);
2319
2320 Changed = DeadCodeElimination(MF, *MDT).run() || Changed;
2321
2322 if (Changed) {
2323 for (auto &B : MF)
2324 for (auto &I : B)
2325 I.clearKillInfo();
2326 DeadCodeElimination(MF, *MDT).run();
2327 }
2328 return Changed;
2329}
2330
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002331// Recognize loops where the code at the end of the loop matches the code
2332// before the entry of the loop, and the matching code is such that is can
2333// be simplified. This pass relies on the bit simplification above and only
2334// prepares code in a way that can be handled by the bit simplifcation.
2335//
2336// This is the motivating testcase (and explanation):
2337//
2338// {
2339// loop0(.LBB0_2, r1) // %for.body.preheader
2340// r5:4 = memd(r0++#8)
2341// }
2342// {
2343// r3 = lsr(r4, #16)
2344// r7:6 = combine(r5, r5)
2345// }
2346// {
2347// r3 = insert(r5, #16, #16)
2348// r7:6 = vlsrw(r7:6, #16)
2349// }
2350// .LBB0_2:
2351// {
2352// memh(r2+#4) = r5
2353// memh(r2+#6) = r6 # R6 is really R5.H
2354// }
2355// {
2356// r2 = add(r2, #8)
2357// memh(r2+#0) = r4
2358// memh(r2+#2) = r3 # R3 is really R4.H
2359// }
2360// {
2361// r5:4 = memd(r0++#8)
2362// }
2363// { # "Shuffling" code that sets up R3 and R6
2364// r3 = lsr(r4, #16) # so that their halves can be stored in the
2365// r7:6 = combine(r5, r5) # next iteration. This could be folded into
2366// } # the stores if the code was at the beginning
2367// { # of the loop iteration. Since the same code
2368// r3 = insert(r5, #16, #16) # precedes the loop, it can actually be moved
2369// r7:6 = vlsrw(r7:6, #16) # there.
2370// }:endloop0
2371//
2372//
2373// The outcome:
2374//
2375// {
2376// loop0(.LBB0_2, r1)
2377// r5:4 = memd(r0++#8)
2378// }
2379// .LBB0_2:
2380// {
2381// memh(r2+#4) = r5
2382// memh(r2+#6) = r5.h
2383// }
2384// {
2385// r2 = add(r2, #8)
2386// memh(r2+#0) = r4
2387// memh(r2+#2) = r4.h
2388// }
2389// {
2390// r5:4 = memd(r0++#8)
2391// }:endloop0
2392
2393namespace llvm {
Eugene Zelenko82085922016-12-13 22:13:50 +00002394
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002395 FunctionPass *createHexagonLoopRescheduling();
2396 void initializeHexagonLoopReschedulingPass(PassRegistry&);
Eugene Zelenko82085922016-12-13 22:13:50 +00002397
2398} // end namespace llvm
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002399
2400namespace {
Eugene Zelenko82085922016-12-13 22:13:50 +00002401
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002402 class HexagonLoopRescheduling : public MachineFunctionPass {
2403 public:
2404 static char ID;
Eugene Zelenko82085922016-12-13 22:13:50 +00002405
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002406 HexagonLoopRescheduling() : MachineFunctionPass(ID),
Eugene Zelenko82085922016-12-13 22:13:50 +00002407 HII(nullptr), HRI(nullptr), MRI(nullptr), BTP(nullptr) {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002408 initializeHexagonLoopReschedulingPass(*PassRegistry::getPassRegistry());
2409 }
2410
2411 bool runOnMachineFunction(MachineFunction &MF) override;
2412
2413 private:
2414 const HexagonInstrInfo *HII;
2415 const HexagonRegisterInfo *HRI;
2416 MachineRegisterInfo *MRI;
2417 BitTracker *BTP;
2418
2419 struct LoopCand {
2420 LoopCand(MachineBasicBlock *lb, MachineBasicBlock *pb,
2421 MachineBasicBlock *eb) : LB(lb), PB(pb), EB(eb) {}
2422 MachineBasicBlock *LB, *PB, *EB;
2423 };
2424 typedef std::vector<MachineInstr*> InstrList;
2425 struct InstrGroup {
2426 BitTracker::RegisterRef Inp, Out;
2427 InstrList Ins;
2428 };
2429 struct PhiInfo {
2430 PhiInfo(MachineInstr &P, MachineBasicBlock &B);
2431 unsigned DefR;
Krzysztof Parzyszek57c3ddd2016-07-26 19:17:13 +00002432 BitTracker::RegisterRef LR, PR; // Loop Register, Preheader Register
2433 MachineBasicBlock *LB, *PB; // Loop Block, Preheader Block
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002434 };
2435
2436 static unsigned getDefReg(const MachineInstr *MI);
2437 bool isConst(unsigned Reg) const;
2438 bool isBitShuffle(const MachineInstr *MI, unsigned DefR) const;
2439 bool isStoreInput(const MachineInstr *MI, unsigned DefR) const;
2440 bool isShuffleOf(unsigned OutR, unsigned InpR) const;
2441 bool isSameShuffle(unsigned OutR1, unsigned InpR1, unsigned OutR2,
2442 unsigned &InpR2) const;
2443 void moveGroup(InstrGroup &G, MachineBasicBlock &LB, MachineBasicBlock &PB,
2444 MachineBasicBlock::iterator At, unsigned OldPhiR, unsigned NewPredR);
2445 bool processLoop(LoopCand &C);
2446 };
Eugene Zelenko82085922016-12-13 22:13:50 +00002447
2448} // end anonymous namespace
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002449
2450char HexagonLoopRescheduling::ID = 0;
2451
2452INITIALIZE_PASS(HexagonLoopRescheduling, "hexagon-loop-resched",
2453 "Hexagon Loop Rescheduling", false, false)
2454
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002455HexagonLoopRescheduling::PhiInfo::PhiInfo(MachineInstr &P,
2456 MachineBasicBlock &B) {
2457 DefR = HexagonLoopRescheduling::getDefReg(&P);
2458 LB = &B;
2459 PB = nullptr;
2460 for (unsigned i = 1, n = P.getNumOperands(); i < n; i += 2) {
2461 const MachineOperand &OpB = P.getOperand(i+1);
2462 if (OpB.getMBB() == &B) {
2463 LR = P.getOperand(i);
2464 continue;
2465 }
2466 PB = OpB.getMBB();
2467 PR = P.getOperand(i);
2468 }
2469}
2470
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002471unsigned HexagonLoopRescheduling::getDefReg(const MachineInstr *MI) {
2472 RegisterSet Defs;
2473 HBS::getInstrDefs(*MI, Defs);
2474 if (Defs.count() != 1)
2475 return 0;
2476 return Defs.find_first();
2477}
2478
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002479bool HexagonLoopRescheduling::isConst(unsigned Reg) const {
2480 if (!BTP->has(Reg))
2481 return false;
2482 const BitTracker::RegisterCell &RC = BTP->lookup(Reg);
2483 for (unsigned i = 0, w = RC.width(); i < w; ++i) {
2484 const BitTracker::BitValue &V = RC[i];
2485 if (!V.is(0) && !V.is(1))
2486 return false;
2487 }
2488 return true;
2489}
2490
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002491bool HexagonLoopRescheduling::isBitShuffle(const MachineInstr *MI,
2492 unsigned DefR) const {
2493 unsigned Opc = MI->getOpcode();
2494 switch (Opc) {
2495 case TargetOpcode::COPY:
2496 case Hexagon::S2_lsr_i_r:
2497 case Hexagon::S2_asr_i_r:
2498 case Hexagon::S2_asl_i_r:
2499 case Hexagon::S2_lsr_i_p:
2500 case Hexagon::S2_asr_i_p:
2501 case Hexagon::S2_asl_i_p:
2502 case Hexagon::S2_insert:
2503 case Hexagon::A2_or:
2504 case Hexagon::A2_orp:
2505 case Hexagon::A2_and:
2506 case Hexagon::A2_andp:
2507 case Hexagon::A2_combinew:
2508 case Hexagon::A4_combineri:
2509 case Hexagon::A4_combineir:
2510 case Hexagon::A2_combineii:
2511 case Hexagon::A4_combineii:
2512 case Hexagon::A2_combine_ll:
2513 case Hexagon::A2_combine_lh:
2514 case Hexagon::A2_combine_hl:
2515 case Hexagon::A2_combine_hh:
2516 return true;
2517 }
2518 return false;
2519}
2520
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002521bool HexagonLoopRescheduling::isStoreInput(const MachineInstr *MI,
2522 unsigned InpR) const {
2523 for (unsigned i = 0, n = MI->getNumOperands(); i < n; ++i) {
2524 const MachineOperand &Op = MI->getOperand(i);
2525 if (!Op.isReg())
2526 continue;
2527 if (Op.getReg() == InpR)
2528 return i == n-1;
2529 }
2530 return false;
2531}
2532
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002533bool HexagonLoopRescheduling::isShuffleOf(unsigned OutR, unsigned InpR) const {
2534 if (!BTP->has(OutR) || !BTP->has(InpR))
2535 return false;
2536 const BitTracker::RegisterCell &OutC = BTP->lookup(OutR);
2537 for (unsigned i = 0, w = OutC.width(); i < w; ++i) {
2538 const BitTracker::BitValue &V = OutC[i];
2539 if (V.Type != BitTracker::BitValue::Ref)
2540 continue;
2541 if (V.RefI.Reg != InpR)
2542 return false;
2543 }
2544 return true;
2545}
2546
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002547bool HexagonLoopRescheduling::isSameShuffle(unsigned OutR1, unsigned InpR1,
2548 unsigned OutR2, unsigned &InpR2) const {
2549 if (!BTP->has(OutR1) || !BTP->has(InpR1) || !BTP->has(OutR2))
2550 return false;
2551 const BitTracker::RegisterCell &OutC1 = BTP->lookup(OutR1);
2552 const BitTracker::RegisterCell &OutC2 = BTP->lookup(OutR2);
2553 unsigned W = OutC1.width();
2554 unsigned MatchR = 0;
2555 if (W != OutC2.width())
2556 return false;
2557 for (unsigned i = 0; i < W; ++i) {
2558 const BitTracker::BitValue &V1 = OutC1[i], &V2 = OutC2[i];
2559 if (V1.Type != V2.Type || V1.Type == BitTracker::BitValue::One)
2560 return false;
2561 if (V1.Type != BitTracker::BitValue::Ref)
2562 continue;
2563 if (V1.RefI.Pos != V2.RefI.Pos)
2564 return false;
2565 if (V1.RefI.Reg != InpR1)
2566 return false;
2567 if (V2.RefI.Reg == 0 || V2.RefI.Reg == OutR2)
2568 return false;
2569 if (!MatchR)
2570 MatchR = V2.RefI.Reg;
2571 else if (V2.RefI.Reg != MatchR)
2572 return false;
2573 }
2574 InpR2 = MatchR;
2575 return true;
2576}
2577
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002578void HexagonLoopRescheduling::moveGroup(InstrGroup &G, MachineBasicBlock &LB,
2579 MachineBasicBlock &PB, MachineBasicBlock::iterator At, unsigned OldPhiR,
2580 unsigned NewPredR) {
2581 DenseMap<unsigned,unsigned> RegMap;
2582
2583 const TargetRegisterClass *PhiRC = MRI->getRegClass(NewPredR);
2584 unsigned PhiR = MRI->createVirtualRegister(PhiRC);
2585 BuildMI(LB, At, At->getDebugLoc(), HII->get(TargetOpcode::PHI), PhiR)
2586 .addReg(NewPredR)
2587 .addMBB(&PB)
2588 .addReg(G.Inp.Reg)
2589 .addMBB(&LB);
2590 RegMap.insert(std::make_pair(G.Inp.Reg, PhiR));
2591
2592 for (unsigned i = G.Ins.size(); i > 0; --i) {
2593 const MachineInstr *SI = G.Ins[i-1];
2594 unsigned DR = getDefReg(SI);
2595 const TargetRegisterClass *RC = MRI->getRegClass(DR);
2596 unsigned NewDR = MRI->createVirtualRegister(RC);
2597 DebugLoc DL = SI->getDebugLoc();
2598
2599 auto MIB = BuildMI(LB, At, DL, HII->get(SI->getOpcode()), NewDR);
2600 for (unsigned j = 0, m = SI->getNumOperands(); j < m; ++j) {
2601 const MachineOperand &Op = SI->getOperand(j);
2602 if (!Op.isReg()) {
Diana Picus116bbab2017-01-13 09:58:52 +00002603 MIB.add(Op);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002604 continue;
2605 }
2606 if (!Op.isUse())
2607 continue;
2608 unsigned UseR = RegMap[Op.getReg()];
2609 MIB.addReg(UseR, 0, Op.getSubReg());
2610 }
2611 RegMap.insert(std::make_pair(DR, NewDR));
2612 }
2613
2614 HBS::replaceReg(OldPhiR, RegMap[G.Out.Reg], *MRI);
2615}
2616
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002617bool HexagonLoopRescheduling::processLoop(LoopCand &C) {
2618 DEBUG(dbgs() << "Processing loop in BB#" << C.LB->getNumber() << "\n");
2619 std::vector<PhiInfo> Phis;
2620 for (auto &I : *C.LB) {
2621 if (!I.isPHI())
2622 break;
2623 unsigned PR = getDefReg(&I);
2624 if (isConst(PR))
2625 continue;
2626 bool BadUse = false, GoodUse = false;
2627 for (auto UI = MRI->use_begin(PR), UE = MRI->use_end(); UI != UE; ++UI) {
2628 MachineInstr *UseI = UI->getParent();
2629 if (UseI->getParent() != C.LB) {
2630 BadUse = true;
2631 break;
2632 }
2633 if (isBitShuffle(UseI, PR) || isStoreInput(UseI, PR))
2634 GoodUse = true;
2635 }
2636 if (BadUse || !GoodUse)
2637 continue;
2638
2639 Phis.push_back(PhiInfo(I, *C.LB));
2640 }
2641
2642 DEBUG({
2643 dbgs() << "Phis: {";
2644 for (auto &I : Phis) {
2645 dbgs() << ' ' << PrintReg(I.DefR, HRI) << "=phi("
2646 << PrintReg(I.PR.Reg, HRI, I.PR.Sub) << ":b" << I.PB->getNumber()
2647 << ',' << PrintReg(I.LR.Reg, HRI, I.LR.Sub) << ":b"
2648 << I.LB->getNumber() << ')';
2649 }
2650 dbgs() << " }\n";
2651 });
2652
2653 if (Phis.empty())
2654 return false;
2655
2656 bool Changed = false;
2657 InstrList ShufIns;
2658
2659 // Go backwards in the block: for each bit shuffling instruction, check
2660 // if that instruction could potentially be moved to the front of the loop:
2661 // the output of the loop cannot be used in a non-shuffling instruction
2662 // in this loop.
2663 for (auto I = C.LB->rbegin(), E = C.LB->rend(); I != E; ++I) {
2664 if (I->isTerminator())
2665 continue;
2666 if (I->isPHI())
2667 break;
2668
2669 RegisterSet Defs;
2670 HBS::getInstrDefs(*I, Defs);
2671 if (Defs.count() != 1)
2672 continue;
2673 unsigned DefR = Defs.find_first();
2674 if (!TargetRegisterInfo::isVirtualRegister(DefR))
2675 continue;
2676 if (!isBitShuffle(&*I, DefR))
2677 continue;
2678
2679 bool BadUse = false;
2680 for (auto UI = MRI->use_begin(DefR), UE = MRI->use_end(); UI != UE; ++UI) {
2681 MachineInstr *UseI = UI->getParent();
2682 if (UseI->getParent() == C.LB) {
2683 if (UseI->isPHI()) {
2684 // If the use is in a phi node in this loop, then it should be
2685 // the value corresponding to the back edge.
2686 unsigned Idx = UI.getOperandNo();
2687 if (UseI->getOperand(Idx+1).getMBB() != C.LB)
2688 BadUse = true;
2689 } else {
David Majnemer0d955d02016-08-11 22:21:41 +00002690 auto F = find(ShufIns, UseI);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002691 if (F == ShufIns.end())
2692 BadUse = true;
2693 }
2694 } else {
2695 // There is a use outside of the loop, but there is no epilog block
2696 // suitable for a copy-out.
2697 if (C.EB == nullptr)
2698 BadUse = true;
2699 }
2700 if (BadUse)
2701 break;
2702 }
2703
2704 if (BadUse)
2705 continue;
2706 ShufIns.push_back(&*I);
2707 }
2708
2709 // Partition the list of shuffling instructions into instruction groups,
2710 // where each group has to be moved as a whole (i.e. a group is a chain of
2711 // dependent instructions). A group produces a single live output register,
2712 // which is meant to be the input of the loop phi node (although this is
2713 // not checked here yet). It also uses a single register as its input,
2714 // which is some value produced in the loop body. After moving the group
2715 // to the beginning of the loop, that input register would need to be
2716 // the loop-carried register (through a phi node) instead of the (currently
2717 // loop-carried) output register.
2718 typedef std::vector<InstrGroup> InstrGroupList;
2719 InstrGroupList Groups;
2720
2721 for (unsigned i = 0, n = ShufIns.size(); i < n; ++i) {
2722 MachineInstr *SI = ShufIns[i];
2723 if (SI == nullptr)
2724 continue;
2725
2726 InstrGroup G;
2727 G.Ins.push_back(SI);
2728 G.Out.Reg = getDefReg(SI);
2729 RegisterSet Inputs;
2730 HBS::getInstrUses(*SI, Inputs);
2731
2732 for (unsigned j = i+1; j < n; ++j) {
2733 MachineInstr *MI = ShufIns[j];
2734 if (MI == nullptr)
2735 continue;
2736 RegisterSet Defs;
2737 HBS::getInstrDefs(*MI, Defs);
2738 // If this instruction does not define any pending inputs, skip it.
2739 if (!Defs.intersects(Inputs))
2740 continue;
2741 // Otherwise, add it to the current group and remove the inputs that
2742 // are defined by MI.
2743 G.Ins.push_back(MI);
2744 Inputs.remove(Defs);
2745 // Then add all registers used by MI.
2746 HBS::getInstrUses(*MI, Inputs);
2747 ShufIns[j] = nullptr;
2748 }
2749
2750 // Only add a group if it requires at most one register.
2751 if (Inputs.count() > 1)
2752 continue;
2753 auto LoopInpEq = [G] (const PhiInfo &P) -> bool {
2754 return G.Out.Reg == P.LR.Reg;
2755 };
Eugene Zelenko82085922016-12-13 22:13:50 +00002756 if (llvm::find_if(Phis, LoopInpEq) == Phis.end())
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002757 continue;
2758
2759 G.Inp.Reg = Inputs.find_first();
2760 Groups.push_back(G);
2761 }
2762
2763 DEBUG({
2764 for (unsigned i = 0, n = Groups.size(); i < n; ++i) {
2765 InstrGroup &G = Groups[i];
2766 dbgs() << "Group[" << i << "] inp: "
2767 << PrintReg(G.Inp.Reg, HRI, G.Inp.Sub)
2768 << " out: " << PrintReg(G.Out.Reg, HRI, G.Out.Sub) << "\n";
2769 for (unsigned j = 0, m = G.Ins.size(); j < m; ++j)
2770 dbgs() << " " << *G.Ins[j];
2771 }
2772 });
2773
2774 for (unsigned i = 0, n = Groups.size(); i < n; ++i) {
2775 InstrGroup &G = Groups[i];
2776 if (!isShuffleOf(G.Out.Reg, G.Inp.Reg))
2777 continue;
2778 auto LoopInpEq = [G] (const PhiInfo &P) -> bool {
2779 return G.Out.Reg == P.LR.Reg;
2780 };
Eugene Zelenko82085922016-12-13 22:13:50 +00002781 auto F = llvm::find_if(Phis, LoopInpEq);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002782 if (F == Phis.end())
2783 continue;
Krzysztof Parzyszek57c3ddd2016-07-26 19:17:13 +00002784 unsigned PrehR = 0;
2785 if (!isSameShuffle(G.Out.Reg, G.Inp.Reg, F->PR.Reg, PrehR)) {
2786 const MachineInstr *DefPrehR = MRI->getVRegDef(F->PR.Reg);
2787 unsigned Opc = DefPrehR->getOpcode();
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002788 if (Opc != Hexagon::A2_tfrsi && Opc != Hexagon::A2_tfrpi)
2789 continue;
Krzysztof Parzyszek57c3ddd2016-07-26 19:17:13 +00002790 if (!DefPrehR->getOperand(1).isImm())
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002791 continue;
Krzysztof Parzyszek57c3ddd2016-07-26 19:17:13 +00002792 if (DefPrehR->getOperand(1).getImm() != 0)
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002793 continue;
2794 const TargetRegisterClass *RC = MRI->getRegClass(G.Inp.Reg);
2795 if (RC != MRI->getRegClass(F->PR.Reg)) {
Krzysztof Parzyszek57c3ddd2016-07-26 19:17:13 +00002796 PrehR = MRI->createVirtualRegister(RC);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002797 unsigned TfrI = (RC == &Hexagon::IntRegsRegClass) ? Hexagon::A2_tfrsi
2798 : Hexagon::A2_tfrpi;
2799 auto T = C.PB->getFirstTerminator();
2800 DebugLoc DL = (T != C.PB->end()) ? T->getDebugLoc() : DebugLoc();
Krzysztof Parzyszek57c3ddd2016-07-26 19:17:13 +00002801 BuildMI(*C.PB, T, DL, HII->get(TfrI), PrehR)
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002802 .addImm(0);
2803 } else {
Krzysztof Parzyszek57c3ddd2016-07-26 19:17:13 +00002804 PrehR = F->PR.Reg;
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002805 }
2806 }
Krzysztof Parzyszek57c3ddd2016-07-26 19:17:13 +00002807 // isSameShuffle could match with PrehR being of a wider class than
2808 // G.Inp.Reg, for example if G shuffles the low 32 bits of its input,
2809 // it would match for the input being a 32-bit register, and PrehR
2810 // being a 64-bit register (where the low 32 bits match). This could
2811 // be handled, but for now skip these cases.
2812 if (MRI->getRegClass(PrehR) != MRI->getRegClass(G.Inp.Reg))
2813 continue;
2814 moveGroup(G, *F->LB, *F->PB, F->LB->getFirstNonPHI(), F->DefR, PrehR);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002815 Changed = true;
2816 }
2817
2818 return Changed;
2819}
2820
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002821bool HexagonLoopRescheduling::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +00002822 if (skipFunction(*MF.getFunction()))
2823 return false;
2824
Krzysztof Parzyszekced99412015-10-20 22:57:13 +00002825 auto &HST = MF.getSubtarget<HexagonSubtarget>();
2826 HII = HST.getInstrInfo();
2827 HRI = HST.getRegisterInfo();
2828 MRI = &MF.getRegInfo();
2829 const HexagonEvaluator HE(*HRI, *MRI, *HII, MF);
2830 BitTracker BT(HE, MF);
2831 DEBUG(BT.trace(true));
2832 BT.run();
2833 BTP = &BT;
2834
2835 std::vector<LoopCand> Cand;
2836
2837 for (auto &B : MF) {
2838 if (B.pred_size() != 2 || B.succ_size() != 2)
2839 continue;
2840 MachineBasicBlock *PB = nullptr;
2841 bool IsLoop = false;
2842 for (auto PI = B.pred_begin(), PE = B.pred_end(); PI != PE; ++PI) {
2843 if (*PI != &B)
2844 PB = *PI;
2845 else
2846 IsLoop = true;
2847 }
2848 if (!IsLoop)
2849 continue;
2850
2851 MachineBasicBlock *EB = nullptr;
2852 for (auto SI = B.succ_begin(), SE = B.succ_end(); SI != SE; ++SI) {
2853 if (*SI == &B)
2854 continue;
2855 // Set EP to the epilog block, if it has only 1 predecessor (i.e. the
2856 // edge from B to EP is non-critical.
2857 if ((*SI)->pred_size() == 1)
2858 EB = *SI;
2859 break;
2860 }
2861
2862 Cand.push_back(LoopCand(&B, PB, EB));
2863 }
2864
2865 bool Changed = false;
2866 for (auto &C : Cand)
2867 Changed |= processLoop(C);
2868
2869 return Changed;
2870}
2871
2872//===----------------------------------------------------------------------===//
2873// Public Constructor Functions
2874//===----------------------------------------------------------------------===//
2875
2876FunctionPass *llvm::createHexagonLoopRescheduling() {
2877 return new HexagonLoopRescheduling();
2878}
2879
2880FunctionPass *llvm::createHexagonBitSimplify() {
2881 return new HexagonBitSimplify();
2882}