LLVM 24.0.0git
HexagonBitSimplify.cpp
Go to the documentation of this file.
1//===- HexagonBitSimplify.cpp ---------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "BitTracker.h"
10#include "Hexagon.h"
11#include "HexagonBitTracker.h"
12#include "HexagonInstrInfo.h"
13#include "HexagonRegisterInfo.h"
14#include "HexagonSubtarget.h"
15#include "llvm/ADT/BitVector.h"
17#include "llvm/ADT/StringRef.h"
26#include "llvm/IR/DebugLoc.h"
28#include "llvm/MC/MCInstrDesc.h"
29#include "llvm/Pass.h"
30#include "llvm/Support/Debug.h"
32
33#define DEBUG_TYPE "hexbit"
34
35using namespace llvm;
36
37static cl::opt<bool> PreserveTiedOps("hexbit-keep-tied", cl::Hidden,
38 cl::init(true), cl::desc("Preserve subregisters in tied operands"));
39static cl::opt<bool> GenExtract("hexbit-extract", cl::Hidden,
40 cl::init(true), cl::desc("Generate extract instructions"));
41static cl::opt<bool> GenBitSplit("hexbit-bitsplit", cl::Hidden,
42 cl::init(true), cl::desc("Generate bitsplit instructions"));
43
44static cl::opt<unsigned> MaxExtract("hexbit-max-extract", cl::Hidden,
45 cl::init(std::numeric_limits<unsigned>::max()));
46static unsigned CountExtract = 0;
47static cl::opt<unsigned> MaxBitSplit("hexbit-max-bitsplit", cl::Hidden,
48 cl::init(std::numeric_limits<unsigned>::max()));
49static unsigned CountBitSplit = 0;
50
51static cl::opt<unsigned> RegisterSetLimit("hexbit-registerset-limit",
52 cl::Hidden, cl::init(1000));
53
54namespace {
55
56 // Set of virtual registers, based on BitVector.
57 struct RegisterSet {
58 RegisterSet() = default;
59 explicit RegisterSet(unsigned s, bool t = false) : Bits(s, t) {}
60 RegisterSet(const RegisterSet &RS) = default;
61
62 void clear() {
63 Bits.clear();
64 LRU.clear();
65 }
66
67 unsigned count() const {
68 return Bits.count();
69 }
70
71 unsigned find_first() const {
72 int First = Bits.find_first();
73 if (First < 0)
74 return 0;
75 return x2v(First);
76 }
77
78 unsigned find_next(unsigned Prev) const {
79 int Next = Bits.find_next(v2x(Prev));
80 if (Next < 0)
81 return 0;
82 return x2v(Next);
83 }
84
85 RegisterSet &insert(unsigned R) {
86 unsigned Idx = v2x(R);
87 ensure(Idx);
88 bool Exists = Bits.test(Idx);
89 Bits.set(Idx);
90 if (!Exists) {
91 LRU.push_back(Idx);
92 if (LRU.size() > RegisterSetLimit) {
93 unsigned T = LRU.front();
94 Bits.reset(T);
95 LRU.pop_front();
96 }
97 }
98 return *this;
99 }
100 RegisterSet &remove(unsigned R) {
101 unsigned Idx = v2x(R);
102 if (Idx < Bits.size()) {
103 bool Exists = Bits.test(Idx);
104 Bits.reset(Idx);
105 if (Exists) {
106 auto F = llvm::find(LRU, Idx);
107 assert(F != LRU.end());
108 LRU.erase(F);
109 }
110 }
111 return *this;
112 }
113
114 RegisterSet &insert(const RegisterSet &Rs) {
115 for (unsigned R = Rs.find_first(); R; R = Rs.find_next(R))
116 insert(R);
117 return *this;
118 }
119 RegisterSet &remove(const RegisterSet &Rs) {
120 for (unsigned R = Rs.find_first(); R; R = Rs.find_next(R))
121 remove(R);
122 return *this;
123 }
124
125 bool operator[](unsigned R) const {
126 unsigned Idx = v2x(R);
127 return Idx < Bits.size() ? Bits[Idx] : false;
128 }
129 bool has(unsigned R) const {
130 unsigned Idx = v2x(R);
131 if (Idx >= Bits.size())
132 return false;
133 return Bits.test(Idx);
134 }
135
136 bool empty() const {
137 return !Bits.any();
138 }
139 bool includes(const RegisterSet &Rs) const {
140 return Rs.Bits.subsetOf(Bits);
141 }
142 bool intersects(const RegisterSet &Rs) const {
143 return Bits.anyCommon(Rs.Bits);
144 }
145
146 private:
147 BitVector Bits;
148 std::deque<unsigned> LRU;
149
150 void ensure(unsigned Idx) {
151 if (Bits.size() <= Idx)
152 Bits.resize(std::max(Idx+1, 32U));
153 }
154
155 static inline unsigned v2x(unsigned v) {
156 return Register(v).virtRegIndex();
157 }
158
159 static inline unsigned x2v(unsigned x) {
160 return Register::index2VirtReg(x);
161 }
162 };
163
164 struct PrintRegSet {
165 PrintRegSet(const RegisterSet &S, const TargetRegisterInfo *RI)
166 : RS(S), TRI(RI) {}
167
168 friend raw_ostream &operator<< (raw_ostream &OS,
169 const PrintRegSet &P);
170
171 private:
172 const RegisterSet &RS;
173 const TargetRegisterInfo *TRI;
174 };
175
176 [[maybe_unused]] raw_ostream &operator<<(raw_ostream &OS,
177 const PrintRegSet &P);
178 raw_ostream &operator<< (raw_ostream &OS, const PrintRegSet &P) {
179 OS << '{';
180 for (unsigned R = P.RS.find_first(); R; R = P.RS.find_next(R))
181 OS << ' ' << printReg(R, P.TRI);
182 OS << " }";
183 return OS;
184 }
185
186 class Transformation;
187
188 class HexagonBitSimplify : public MachineFunctionPass {
189 public:
190 static char ID;
191
192 HexagonBitSimplify() : MachineFunctionPass(ID) {}
193
194 StringRef getPassName() const override {
195 return "Hexagon bit simplification";
196 }
197
198 void getAnalysisUsage(AnalysisUsage &AU) const override {
199 AU.addRequired<MachineDominatorTreeWrapperPass>();
200 AU.addPreserved<MachineDominatorTreeWrapperPass>();
202 }
203
204 bool runOnMachineFunction(MachineFunction &MF) override;
205
206 static void getInstrDefs(const MachineInstr &MI, RegisterSet &Defs);
207 static void getInstrUses(const MachineInstr &MI, RegisterSet &Uses);
208 static bool isEqual(const BitTracker::RegisterCell &RC1, uint16_t B1,
209 const BitTracker::RegisterCell &RC2, uint16_t B2, uint16_t W);
210 static bool isZero(const BitTracker::RegisterCell &RC, uint16_t B,
211 uint16_t W);
212 static bool getConst(const BitTracker::RegisterCell &RC, uint16_t B,
213 uint16_t W, uint64_t &U);
214 static bool replaceReg(Register OldR, Register NewR,
215 MachineRegisterInfo &MRI);
216 static bool getSubregMask(const BitTracker::RegisterRef &RR,
217 unsigned &Begin, unsigned &Width, MachineRegisterInfo &MRI);
218 static bool replaceRegWithSub(Register OldR, Register NewR, unsigned NewSR,
219 MachineRegisterInfo &MRI);
220 static bool replaceSubWithSub(Register OldR, unsigned OldSR, Register NewR,
221 unsigned NewSR, MachineRegisterInfo &MRI);
222 static bool parseRegSequence(const MachineInstr &I,
223 BitTracker::RegisterRef &SL, BitTracker::RegisterRef &SH,
224 const MachineRegisterInfo &MRI);
225
226 static bool getUsedBitsInStore(unsigned Opc, BitVector &Bits,
227 uint16_t Begin);
228 static bool getUsedBits(unsigned Opc, unsigned OpN, BitVector &Bits,
229 uint16_t Begin, const HexagonInstrInfo &HII);
230
231 static const TargetRegisterClass *getFinalVRegClass(
232 const BitTracker::RegisterRef &RR, MachineRegisterInfo &MRI);
233 static bool isTransparentCopy(const BitTracker::RegisterRef &RD,
234 const BitTracker::RegisterRef &RS, MachineRegisterInfo &MRI);
235
236 private:
237 MachineDominatorTree *MDT = nullptr;
238
239 bool visitBlock(MachineBasicBlock &B, Transformation &T, RegisterSet &AVs);
240 static bool hasTiedUse(unsigned Reg, MachineRegisterInfo &MRI,
241 unsigned NewSub = Hexagon::NoSubRegister);
242 };
243
244 using HBS = HexagonBitSimplify;
245
246 // The purpose of this class is to provide a common facility to traverse
247 // the function top-down or bottom-up via the dominator tree, and keep
248 // track of the available registers.
249 class Transformation {
250 public:
251 bool TopDown;
252
253 Transformation(bool TD) : TopDown(TD) {}
254 virtual ~Transformation() = default;
255
256 virtual bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) = 0;
257 };
258
259} // end anonymous namespace
260
261char HexagonBitSimplify::ID = 0;
262
263INITIALIZE_PASS_BEGIN(HexagonBitSimplify, "hexagon-bit-simplify",
264 "Hexagon bit simplification", false, false)
266INITIALIZE_PASS_END(HexagonBitSimplify, "hexagon-bit-simplify",
267 "Hexagon bit simplification", false, false)
268
269bool HexagonBitSimplify::visitBlock(MachineBasicBlock &B, Transformation &T,
270 RegisterSet &AVs) {
271 bool Changed = false;
272
273 if (T.TopDown)
274 Changed = T.processBlock(B, AVs);
275
276 RegisterSet Defs;
277 for (auto &I : B)
278 getInstrDefs(I, Defs);
279 RegisterSet NewAVs = AVs;
280 NewAVs.insert(Defs);
281
282 for (auto *DTN : children<MachineDomTreeNode*>(MDT->getNode(&B)))
283 Changed |= visitBlock(*(DTN->getBlock()), T, NewAVs);
284
285 if (!T.TopDown)
286 Changed |= T.processBlock(B, AVs);
287
288 return Changed;
289}
290
291//
292// Utility functions:
293//
294void HexagonBitSimplify::getInstrDefs(const MachineInstr &MI,
295 RegisterSet &Defs) {
296 for (auto &Op : MI.operands()) {
297 if (!Op.isReg() || !Op.isDef())
298 continue;
299 Register R = Op.getReg();
300 if (!R.isVirtual())
301 continue;
302 Defs.insert(R);
303 }
304}
305
306void HexagonBitSimplify::getInstrUses(const MachineInstr &MI,
307 RegisterSet &Uses) {
308 for (auto &Op : MI.operands()) {
309 if (!Op.isReg() || !Op.isUse())
310 continue;
311 Register R = Op.getReg();
312 if (!R.isVirtual())
313 continue;
314 Uses.insert(R);
315 }
316}
317
318// Check if all the bits in range [B, E) in both cells are equal.
319bool HexagonBitSimplify::isEqual(const BitTracker::RegisterCell &RC1,
320 uint16_t B1, const BitTracker::RegisterCell &RC2, uint16_t B2,
321 uint16_t W) {
322 for (uint16_t i = 0; i < W; ++i) {
323 // If RC1[i] is "bottom", it cannot be proven equal to RC2[i].
324 if (RC1[B1+i].Type == BitTracker::BitValue::Ref && RC1[B1+i].RefI.Reg == 0)
325 return false;
326 // Same for RC2[i].
327 if (RC2[B2+i].Type == BitTracker::BitValue::Ref && RC2[B2+i].RefI.Reg == 0)
328 return false;
329 if (RC1[B1+i] != RC2[B2+i])
330 return false;
331 }
332 return true;
333}
334
335bool HexagonBitSimplify::isZero(const BitTracker::RegisterCell &RC,
336 uint16_t B, uint16_t W) {
337 assert(B < RC.width() && B+W <= RC.width());
338 for (uint16_t i = B; i < B+W; ++i)
339 if (!RC[i].is(0))
340 return false;
341 return true;
342}
343
344bool HexagonBitSimplify::getConst(const BitTracker::RegisterCell &RC,
345 uint16_t B, uint16_t W, uint64_t &U) {
346 assert(B < RC.width() && B+W <= RC.width());
347 int64_t T = 0;
348 for (uint16_t i = B+W; i > B; --i) {
349 const BitTracker::BitValue &BV = RC[i-1];
350 T <<= 1;
351 if (BV.is(1))
352 T |= 1;
353 else if (!BV.is(0))
354 return false;
355 }
356 U = T;
357 return true;
358}
359
360bool HexagonBitSimplify::replaceReg(Register OldR, Register NewR,
361 MachineRegisterInfo &MRI) {
362 if (!OldR.isVirtual() || !NewR.isVirtual())
363 return false;
364 auto Begin = MRI.use_begin(OldR), End = MRI.use_end();
365 decltype(End) NextI;
366 for (auto I = Begin; I != End; I = NextI) {
367 NextI = std::next(I);
368 I->setReg(NewR);
369 }
370 return Begin != End;
371}
372
373bool HexagonBitSimplify::replaceRegWithSub(Register OldR, Register NewR,
374 unsigned NewSR,
375 MachineRegisterInfo &MRI) {
376 if (!OldR.isVirtual() || !NewR.isVirtual())
377 return false;
378 if (hasTiedUse(OldR, MRI, NewSR))
379 return false;
380 auto Begin = MRI.use_begin(OldR), End = MRI.use_end();
381 decltype(End) NextI;
382 for (auto I = Begin; I != End; I = NextI) {
383 NextI = std::next(I);
384 I->setReg(NewR);
385 I->setSubReg(NewSR);
386 }
387 return Begin != End;
388}
389
390bool HexagonBitSimplify::replaceSubWithSub(Register OldR, unsigned OldSR,
391 Register NewR, unsigned NewSR,
392 MachineRegisterInfo &MRI) {
393 if (!OldR.isVirtual() || !NewR.isVirtual())
394 return false;
395 if (OldSR != NewSR && hasTiedUse(OldR, MRI, NewSR))
396 return false;
397 auto Begin = MRI.use_begin(OldR), End = MRI.use_end();
398 decltype(End) NextI;
399 for (auto I = Begin; I != End; I = NextI) {
400 NextI = std::next(I);
401 if (I->getSubReg() != OldSR)
402 continue;
403 I->setReg(NewR);
404 I->setSubReg(NewSR);
405 }
406 return Begin != End;
407}
408
409// For a register ref (pair Reg:Sub), set Begin to the position of the LSB
410// of Sub in Reg, and set Width to the size of Sub in bits. Return true,
411// if this succeeded, otherwise return false.
412bool HexagonBitSimplify::getSubregMask(const BitTracker::RegisterRef &RR,
413 unsigned &Begin, unsigned &Width, MachineRegisterInfo &MRI) {
414 const TargetRegisterClass *RC = MRI.getRegClass(RR.Reg);
415 if (RR.Sub == 0) {
416 Begin = 0;
418 return true;
419 }
420
421 Begin = 0;
422
423 switch (RC->getID()) {
424 case Hexagon::DoubleRegsRegClassID:
425 case Hexagon::HvxWRRegClassID:
426 case Hexagon::HlxLLRRegClassID:
427 Width = MRI.getTargetRegisterInfo()->getRegSizeInBits(*RC) / 2;
428 if (RR.Sub == Hexagon::isub_hi || RR.Sub == Hexagon::vsub_hi)
429 Begin = Width;
430 break;
431 default:
432 return false;
433 }
434 return true;
435}
436
437
438// For a REG_SEQUENCE, set SL to the low subregister and SH to the high
439// subregister.
440bool HexagonBitSimplify::parseRegSequence(const MachineInstr &I,
441 BitTracker::RegisterRef &SL, BitTracker::RegisterRef &SH,
442 const MachineRegisterInfo &MRI) {
443 assert(I.getOpcode() == TargetOpcode::REG_SEQUENCE);
444 unsigned Sub1 = I.getOperand(2).getImm(), Sub2 = I.getOperand(4).getImm();
445 auto &DstRC = *MRI.getRegClass(I.getOperand(0).getReg());
446 auto &HRI = static_cast<const HexagonRegisterInfo&>(
448 unsigned SubLo = HRI.getHexagonSubRegIndex(DstRC, Hexagon::ps_sub_lo);
449 unsigned SubHi = HRI.getHexagonSubRegIndex(DstRC, Hexagon::ps_sub_hi);
450 assert((Sub1 == SubLo && Sub2 == SubHi) || (Sub1 == SubHi && Sub2 == SubLo));
451 if (Sub1 == SubLo && Sub2 == SubHi) {
452 SL = I.getOperand(1);
453 SH = I.getOperand(3);
454 return true;
455 }
456 if (Sub1 == SubHi && Sub2 == SubLo) {
457 SH = I.getOperand(1);
458 SL = I.getOperand(3);
459 return true;
460 }
461 return false;
462}
463
464// All stores (except 64-bit stores) take a 32-bit register as the source
465// of the value to be stored. If the instruction stores into a location
466// that is shorter than 32 bits, some bits of the source register are not
467// used. For each store instruction, calculate the set of used bits in
468// the source register, and set appropriate bits in Bits. Return true if
469// the bits are calculated, false otherwise.
470bool HexagonBitSimplify::getUsedBitsInStore(unsigned Opc, BitVector &Bits,
471 uint16_t Begin) {
472 using namespace Hexagon;
473
474 switch (Opc) {
475 // Store byte
476 case S2_storerb_io: // memb(Rs32+#s11:0)=Rt32
477 case S2_storerbnew_io: // memb(Rs32+#s11:0)=Nt8.new
478 case S2_pstorerbt_io: // if (Pv4) memb(Rs32+#u6:0)=Rt32
479 case S2_pstorerbf_io: // if (!Pv4) memb(Rs32+#u6:0)=Rt32
480 case S4_pstorerbtnew_io: // if (Pv4.new) memb(Rs32+#u6:0)=Rt32
481 case S4_pstorerbfnew_io: // if (!Pv4.new) memb(Rs32+#u6:0)=Rt32
482 case S2_pstorerbnewt_io: // if (Pv4) memb(Rs32+#u6:0)=Nt8.new
483 case S2_pstorerbnewf_io: // if (!Pv4) memb(Rs32+#u6:0)=Nt8.new
484 case S4_pstorerbnewtnew_io: // if (Pv4.new) memb(Rs32+#u6:0)=Nt8.new
485 case S4_pstorerbnewfnew_io: // if (!Pv4.new) memb(Rs32+#u6:0)=Nt8.new
486 case S2_storerb_pi: // memb(Rx32++#s4:0)=Rt32
487 case S2_storerbnew_pi: // memb(Rx32++#s4:0)=Nt8.new
488 case S2_pstorerbt_pi: // if (Pv4) memb(Rx32++#s4:0)=Rt32
489 case S2_pstorerbf_pi: // if (!Pv4) memb(Rx32++#s4:0)=Rt32
490 case S2_pstorerbtnew_pi: // if (Pv4.new) memb(Rx32++#s4:0)=Rt32
491 case S2_pstorerbfnew_pi: // if (!Pv4.new) memb(Rx32++#s4:0)=Rt32
492 case S2_pstorerbnewt_pi: // if (Pv4) memb(Rx32++#s4:0)=Nt8.new
493 case S2_pstorerbnewf_pi: // if (!Pv4) memb(Rx32++#s4:0)=Nt8.new
494 case S2_pstorerbnewtnew_pi: // if (Pv4.new) memb(Rx32++#s4:0)=Nt8.new
495 case S2_pstorerbnewfnew_pi: // if (!Pv4.new) memb(Rx32++#s4:0)=Nt8.new
496 case S4_storerb_ap: // memb(Re32=#U6)=Rt32
497 case S4_storerbnew_ap: // memb(Re32=#U6)=Nt8.new
498 case S2_storerb_pr: // memb(Rx32++Mu2)=Rt32
499 case S2_storerbnew_pr: // memb(Rx32++Mu2)=Nt8.new
500 case S4_storerb_ur: // memb(Ru32<<#u2+#U6)=Rt32
501 case S4_storerbnew_ur: // memb(Ru32<<#u2+#U6)=Nt8.new
502 case S2_storerb_pbr: // memb(Rx32++Mu2:brev)=Rt32
503 case S2_storerbnew_pbr: // memb(Rx32++Mu2:brev)=Nt8.new
504 case S2_storerb_pci: // memb(Rx32++#s4:0:circ(Mu2))=Rt32
505 case S2_storerbnew_pci: // memb(Rx32++#s4:0:circ(Mu2))=Nt8.new
506 case S2_storerb_pcr: // memb(Rx32++I:circ(Mu2))=Rt32
507 case S2_storerbnew_pcr: // memb(Rx32++I:circ(Mu2))=Nt8.new
508 case S4_storerb_rr: // memb(Rs32+Ru32<<#u2)=Rt32
509 case S4_storerbnew_rr: // memb(Rs32+Ru32<<#u2)=Nt8.new
510 case S4_pstorerbt_rr: // if (Pv4) memb(Rs32+Ru32<<#u2)=Rt32
511 case S4_pstorerbf_rr: // if (!Pv4) memb(Rs32+Ru32<<#u2)=Rt32
512 case S4_pstorerbtnew_rr: // if (Pv4.new) memb(Rs32+Ru32<<#u2)=Rt32
513 case S4_pstorerbfnew_rr: // if (!Pv4.new) memb(Rs32+Ru32<<#u2)=Rt32
514 case S4_pstorerbnewt_rr: // if (Pv4) memb(Rs32+Ru32<<#u2)=Nt8.new
515 case S4_pstorerbnewf_rr: // if (!Pv4) memb(Rs32+Ru32<<#u2)=Nt8.new
516 case S4_pstorerbnewtnew_rr: // if (Pv4.new) memb(Rs32+Ru32<<#u2)=Nt8.new
517 case S4_pstorerbnewfnew_rr: // if (!Pv4.new) memb(Rs32+Ru32<<#u2)=Nt8.new
518 case S2_storerbgp: // memb(gp+#u16:0)=Rt32
519 case S2_storerbnewgp: // memb(gp+#u16:0)=Nt8.new
520 case S4_pstorerbt_abs: // if (Pv4) memb(#u6)=Rt32
521 case S4_pstorerbf_abs: // if (!Pv4) memb(#u6)=Rt32
522 case S4_pstorerbtnew_abs: // if (Pv4.new) memb(#u6)=Rt32
523 case S4_pstorerbfnew_abs: // if (!Pv4.new) memb(#u6)=Rt32
524 case S4_pstorerbnewt_abs: // if (Pv4) memb(#u6)=Nt8.new
525 case S4_pstorerbnewf_abs: // if (!Pv4) memb(#u6)=Nt8.new
526 case S4_pstorerbnewtnew_abs: // if (Pv4.new) memb(#u6)=Nt8.new
527 case S4_pstorerbnewfnew_abs: // if (!Pv4.new) memb(#u6)=Nt8.new
528 Bits.set(Begin, Begin+8);
529 return true;
530
531 // Store low half
532 case S2_storerh_io: // memh(Rs32+#s11:1)=Rt32
533 case S2_storerhnew_io: // memh(Rs32+#s11:1)=Nt8.new
534 case S2_pstorerht_io: // if (Pv4) memh(Rs32+#u6:1)=Rt32
535 case S2_pstorerhf_io: // if (!Pv4) memh(Rs32+#u6:1)=Rt32
536 case S4_pstorerhtnew_io: // if (Pv4.new) memh(Rs32+#u6:1)=Rt32
537 case S4_pstorerhfnew_io: // if (!Pv4.new) memh(Rs32+#u6:1)=Rt32
538 case S2_pstorerhnewt_io: // if (Pv4) memh(Rs32+#u6:1)=Nt8.new
539 case S2_pstorerhnewf_io: // if (!Pv4) memh(Rs32+#u6:1)=Nt8.new
540 case S4_pstorerhnewtnew_io: // if (Pv4.new) memh(Rs32+#u6:1)=Nt8.new
541 case S4_pstorerhnewfnew_io: // if (!Pv4.new) memh(Rs32+#u6:1)=Nt8.new
542 case S2_storerh_pi: // memh(Rx32++#s4:1)=Rt32
543 case S2_storerhnew_pi: // memh(Rx32++#s4:1)=Nt8.new
544 case S2_pstorerht_pi: // if (Pv4) memh(Rx32++#s4:1)=Rt32
545 case S2_pstorerhf_pi: // if (!Pv4) memh(Rx32++#s4:1)=Rt32
546 case S2_pstorerhtnew_pi: // if (Pv4.new) memh(Rx32++#s4:1)=Rt32
547 case S2_pstorerhfnew_pi: // if (!Pv4.new) memh(Rx32++#s4:1)=Rt32
548 case S2_pstorerhnewt_pi: // if (Pv4) memh(Rx32++#s4:1)=Nt8.new
549 case S2_pstorerhnewf_pi: // if (!Pv4) memh(Rx32++#s4:1)=Nt8.new
550 case S2_pstorerhnewtnew_pi: // if (Pv4.new) memh(Rx32++#s4:1)=Nt8.new
551 case S2_pstorerhnewfnew_pi: // if (!Pv4.new) memh(Rx32++#s4:1)=Nt8.new
552 case S4_storerh_ap: // memh(Re32=#U6)=Rt32
553 case S4_storerhnew_ap: // memh(Re32=#U6)=Nt8.new
554 case S2_storerh_pr: // memh(Rx32++Mu2)=Rt32
555 case S2_storerhnew_pr: // memh(Rx32++Mu2)=Nt8.new
556 case S4_storerh_ur: // memh(Ru32<<#u2+#U6)=Rt32
557 case S4_storerhnew_ur: // memh(Ru32<<#u2+#U6)=Nt8.new
558 case S2_storerh_pbr: // memh(Rx32++Mu2:brev)=Rt32
559 case S2_storerhnew_pbr: // memh(Rx32++Mu2:brev)=Nt8.new
560 case S2_storerh_pci: // memh(Rx32++#s4:1:circ(Mu2))=Rt32
561 case S2_storerhnew_pci: // memh(Rx32++#s4:1:circ(Mu2))=Nt8.new
562 case S2_storerh_pcr: // memh(Rx32++I:circ(Mu2))=Rt32
563 case S2_storerhnew_pcr: // memh(Rx32++I:circ(Mu2))=Nt8.new
564 case S4_storerh_rr: // memh(Rs32+Ru32<<#u2)=Rt32
565 case S4_pstorerht_rr: // if (Pv4) memh(Rs32+Ru32<<#u2)=Rt32
566 case S4_pstorerhf_rr: // if (!Pv4) memh(Rs32+Ru32<<#u2)=Rt32
567 case S4_pstorerhtnew_rr: // if (Pv4.new) memh(Rs32+Ru32<<#u2)=Rt32
568 case S4_pstorerhfnew_rr: // if (!Pv4.new) memh(Rs32+Ru32<<#u2)=Rt32
569 case S4_storerhnew_rr: // memh(Rs32+Ru32<<#u2)=Nt8.new
570 case S4_pstorerhnewt_rr: // if (Pv4) memh(Rs32+Ru32<<#u2)=Nt8.new
571 case S4_pstorerhnewf_rr: // if (!Pv4) memh(Rs32+Ru32<<#u2)=Nt8.new
572 case S4_pstorerhnewtnew_rr: // if (Pv4.new) memh(Rs32+Ru32<<#u2)=Nt8.new
573 case S4_pstorerhnewfnew_rr: // if (!Pv4.new) memh(Rs32+Ru32<<#u2)=Nt8.new
574 case S2_storerhgp: // memh(gp+#u16:1)=Rt32
575 case S2_storerhnewgp: // memh(gp+#u16:1)=Nt8.new
576 case S4_pstorerht_abs: // if (Pv4) memh(#u6)=Rt32
577 case S4_pstorerhf_abs: // if (!Pv4) memh(#u6)=Rt32
578 case S4_pstorerhtnew_abs: // if (Pv4.new) memh(#u6)=Rt32
579 case S4_pstorerhfnew_abs: // if (!Pv4.new) memh(#u6)=Rt32
580 case S4_pstorerhnewt_abs: // if (Pv4) memh(#u6)=Nt8.new
581 case S4_pstorerhnewf_abs: // if (!Pv4) memh(#u6)=Nt8.new
582 case S4_pstorerhnewtnew_abs: // if (Pv4.new) memh(#u6)=Nt8.new
583 case S4_pstorerhnewfnew_abs: // if (!Pv4.new) memh(#u6)=Nt8.new
584 Bits.set(Begin, Begin+16);
585 return true;
586
587 // Store high half
588 case S2_storerf_io: // memh(Rs32+#s11:1)=Rt.H32
589 case S2_pstorerft_io: // if (Pv4) memh(Rs32+#u6:1)=Rt.H32
590 case S2_pstorerff_io: // if (!Pv4) memh(Rs32+#u6:1)=Rt.H32
591 case S4_pstorerftnew_io: // if (Pv4.new) memh(Rs32+#u6:1)=Rt.H32
592 case S4_pstorerffnew_io: // if (!Pv4.new) memh(Rs32+#u6:1)=Rt.H32
593 case S2_storerf_pi: // memh(Rx32++#s4:1)=Rt.H32
594 case S2_pstorerft_pi: // if (Pv4) memh(Rx32++#s4:1)=Rt.H32
595 case S2_pstorerff_pi: // if (!Pv4) memh(Rx32++#s4:1)=Rt.H32
596 case S2_pstorerftnew_pi: // if (Pv4.new) memh(Rx32++#s4:1)=Rt.H32
597 case S2_pstorerffnew_pi: // if (!Pv4.new) memh(Rx32++#s4:1)=Rt.H32
598 case S4_storerf_ap: // memh(Re32=#U6)=Rt.H32
599 case S2_storerf_pr: // memh(Rx32++Mu2)=Rt.H32
600 case S4_storerf_ur: // memh(Ru32<<#u2+#U6)=Rt.H32
601 case S2_storerf_pbr: // memh(Rx32++Mu2:brev)=Rt.H32
602 case S2_storerf_pci: // memh(Rx32++#s4:1:circ(Mu2))=Rt.H32
603 case S2_storerf_pcr: // memh(Rx32++I:circ(Mu2))=Rt.H32
604 case S4_storerf_rr: // memh(Rs32+Ru32<<#u2)=Rt.H32
605 case S4_pstorerft_rr: // if (Pv4) memh(Rs32+Ru32<<#u2)=Rt.H32
606 case S4_pstorerff_rr: // if (!Pv4) memh(Rs32+Ru32<<#u2)=Rt.H32
607 case S4_pstorerftnew_rr: // if (Pv4.new) memh(Rs32+Ru32<<#u2)=Rt.H32
608 case S4_pstorerffnew_rr: // if (!Pv4.new) memh(Rs32+Ru32<<#u2)=Rt.H32
609 case S2_storerfgp: // memh(gp+#u16:1)=Rt.H32
610 case S4_pstorerft_abs: // if (Pv4) memh(#u6)=Rt.H32
611 case S4_pstorerff_abs: // if (!Pv4) memh(#u6)=Rt.H32
612 case S4_pstorerftnew_abs: // if (Pv4.new) memh(#u6)=Rt.H32
613 case S4_pstorerffnew_abs: // if (!Pv4.new) memh(#u6)=Rt.H32
614 Bits.set(Begin+16, Begin+32);
615 return true;
616 }
617
618 return false;
619}
620
621// For an instruction with opcode Opc, calculate the set of bits that it
622// uses in a register in operand OpN. This only calculates the set of used
623// bits for cases where it does not depend on any operands (as is the case
624// in shifts, for example). For concrete instructions from a program, the
625// operand may be a subregister of a larger register, while Bits would
626// correspond to the larger register in its entirety. Because of that,
627// the parameter Begin can be used to indicate which bit of Bits should be
628// considered the LSB of the operand.
629bool HexagonBitSimplify::getUsedBits(unsigned Opc, unsigned OpN,
630 BitVector &Bits, uint16_t Begin, const HexagonInstrInfo &HII) {
631 using namespace Hexagon;
632
633 const MCInstrDesc &D = HII.get(Opc);
634 if (D.mayStore()) {
635 if (OpN == D.getNumOperands()-1)
636 return getUsedBitsInStore(Opc, Bits, Begin);
637 return false;
638 }
639
640 switch (Opc) {
641 // One register source. Used bits: R1[0-7].
642 case A2_sxtb:
643 case A2_zxtb:
644 case A4_cmpbeqi:
645 case A4_cmpbgti:
646 case A4_cmpbgtui:
647 if (OpN == 1) {
648 Bits.set(Begin, Begin+8);
649 return true;
650 }
651 break;
652
653 // One register source. Used bits: R1[0-15].
654 case A2_aslh:
655 case A2_sxth:
656 case A2_zxth:
657 case A4_cmpheqi:
658 case A4_cmphgti:
659 case A4_cmphgtui:
660 if (OpN == 1) {
661 Bits.set(Begin, Begin+16);
662 return true;
663 }
664 break;
665
666 // One register source. Used bits: R1[16-31].
667 case A2_asrh:
668 if (OpN == 1) {
669 Bits.set(Begin+16, Begin+32);
670 return true;
671 }
672 break;
673
674 // Two register sources. Used bits: R1[0-7], R2[0-7].
675 case A4_cmpbeq:
676 case A4_cmpbgt:
677 case A4_cmpbgtu:
678 if (OpN == 1) {
679 Bits.set(Begin, Begin+8);
680 return true;
681 }
682 break;
683
684 // Two register sources. Used bits: R1[0-15], R2[0-15].
685 case A4_cmpheq:
686 case A4_cmphgt:
687 case A4_cmphgtu:
688 case A2_addh_h16_ll:
689 case A2_addh_h16_sat_ll:
690 case A2_addh_l16_ll:
691 case A2_addh_l16_sat_ll:
692 case A2_combine_ll:
693 case A2_subh_h16_ll:
694 case A2_subh_h16_sat_ll:
695 case A2_subh_l16_ll:
696 case A2_subh_l16_sat_ll:
697 case M2_mpy_acc_ll_s0:
698 case M2_mpy_acc_ll_s1:
699 case M2_mpy_acc_sat_ll_s0:
700 case M2_mpy_acc_sat_ll_s1:
701 case M2_mpy_ll_s0:
702 case M2_mpy_ll_s1:
703 case M2_mpy_nac_ll_s0:
704 case M2_mpy_nac_ll_s1:
705 case M2_mpy_nac_sat_ll_s0:
706 case M2_mpy_nac_sat_ll_s1:
707 case M2_mpy_rnd_ll_s0:
708 case M2_mpy_rnd_ll_s1:
709 case M2_mpy_sat_ll_s0:
710 case M2_mpy_sat_ll_s1:
711 case M2_mpy_sat_rnd_ll_s0:
712 case M2_mpy_sat_rnd_ll_s1:
713 case M2_mpyd_acc_ll_s0:
714 case M2_mpyd_acc_ll_s1:
715 case M2_mpyd_ll_s0:
716 case M2_mpyd_ll_s1:
717 case M2_mpyd_nac_ll_s0:
718 case M2_mpyd_nac_ll_s1:
719 case M2_mpyd_rnd_ll_s0:
720 case M2_mpyd_rnd_ll_s1:
721 case M2_mpyu_acc_ll_s0:
722 case M2_mpyu_acc_ll_s1:
723 case M2_mpyu_ll_s0:
724 case M2_mpyu_ll_s1:
725 case M2_mpyu_nac_ll_s0:
726 case M2_mpyu_nac_ll_s1:
727 case M2_mpyud_acc_ll_s0:
728 case M2_mpyud_acc_ll_s1:
729 case M2_mpyud_ll_s0:
730 case M2_mpyud_ll_s1:
731 case M2_mpyud_nac_ll_s0:
732 case M2_mpyud_nac_ll_s1:
733 if (OpN == 1 || OpN == 2) {
734 Bits.set(Begin, Begin+16);
735 return true;
736 }
737 break;
738
739 // Two register sources. Used bits: R1[0-15], R2[16-31].
740 case A2_addh_h16_lh:
741 case A2_addh_h16_sat_lh:
742 case A2_combine_lh:
743 case A2_subh_h16_lh:
744 case A2_subh_h16_sat_lh:
745 case M2_mpy_acc_lh_s0:
746 case M2_mpy_acc_lh_s1:
747 case M2_mpy_acc_sat_lh_s0:
748 case M2_mpy_acc_sat_lh_s1:
749 case M2_mpy_lh_s0:
750 case M2_mpy_lh_s1:
751 case M2_mpy_nac_lh_s0:
752 case M2_mpy_nac_lh_s1:
753 case M2_mpy_nac_sat_lh_s0:
754 case M2_mpy_nac_sat_lh_s1:
755 case M2_mpy_rnd_lh_s0:
756 case M2_mpy_rnd_lh_s1:
757 case M2_mpy_sat_lh_s0:
758 case M2_mpy_sat_lh_s1:
759 case M2_mpy_sat_rnd_lh_s0:
760 case M2_mpy_sat_rnd_lh_s1:
761 case M2_mpyd_acc_lh_s0:
762 case M2_mpyd_acc_lh_s1:
763 case M2_mpyd_lh_s0:
764 case M2_mpyd_lh_s1:
765 case M2_mpyd_nac_lh_s0:
766 case M2_mpyd_nac_lh_s1:
767 case M2_mpyd_rnd_lh_s0:
768 case M2_mpyd_rnd_lh_s1:
769 case M2_mpyu_acc_lh_s0:
770 case M2_mpyu_acc_lh_s1:
771 case M2_mpyu_lh_s0:
772 case M2_mpyu_lh_s1:
773 case M2_mpyu_nac_lh_s0:
774 case M2_mpyu_nac_lh_s1:
775 case M2_mpyud_acc_lh_s0:
776 case M2_mpyud_acc_lh_s1:
777 case M2_mpyud_lh_s0:
778 case M2_mpyud_lh_s1:
779 case M2_mpyud_nac_lh_s0:
780 case M2_mpyud_nac_lh_s1:
781 // These four are actually LH.
782 case A2_addh_l16_hl:
783 case A2_addh_l16_sat_hl:
784 case A2_subh_l16_hl:
785 case A2_subh_l16_sat_hl:
786 if (OpN == 1) {
787 Bits.set(Begin, Begin+16);
788 return true;
789 }
790 if (OpN == 2) {
791 Bits.set(Begin+16, Begin+32);
792 return true;
793 }
794 break;
795
796 // Two register sources, used bits: R1[16-31], R2[0-15].
797 case A2_addh_h16_hl:
798 case A2_addh_h16_sat_hl:
799 case A2_combine_hl:
800 case A2_subh_h16_hl:
801 case A2_subh_h16_sat_hl:
802 case M2_mpy_acc_hl_s0:
803 case M2_mpy_acc_hl_s1:
804 case M2_mpy_acc_sat_hl_s0:
805 case M2_mpy_acc_sat_hl_s1:
806 case M2_mpy_hl_s0:
807 case M2_mpy_hl_s1:
808 case M2_mpy_nac_hl_s0:
809 case M2_mpy_nac_hl_s1:
810 case M2_mpy_nac_sat_hl_s0:
811 case M2_mpy_nac_sat_hl_s1:
812 case M2_mpy_rnd_hl_s0:
813 case M2_mpy_rnd_hl_s1:
814 case M2_mpy_sat_hl_s0:
815 case M2_mpy_sat_hl_s1:
816 case M2_mpy_sat_rnd_hl_s0:
817 case M2_mpy_sat_rnd_hl_s1:
818 case M2_mpyd_acc_hl_s0:
819 case M2_mpyd_acc_hl_s1:
820 case M2_mpyd_hl_s0:
821 case M2_mpyd_hl_s1:
822 case M2_mpyd_nac_hl_s0:
823 case M2_mpyd_nac_hl_s1:
824 case M2_mpyd_rnd_hl_s0:
825 case M2_mpyd_rnd_hl_s1:
826 case M2_mpyu_acc_hl_s0:
827 case M2_mpyu_acc_hl_s1:
828 case M2_mpyu_hl_s0:
829 case M2_mpyu_hl_s1:
830 case M2_mpyu_nac_hl_s0:
831 case M2_mpyu_nac_hl_s1:
832 case M2_mpyud_acc_hl_s0:
833 case M2_mpyud_acc_hl_s1:
834 case M2_mpyud_hl_s0:
835 case M2_mpyud_hl_s1:
836 case M2_mpyud_nac_hl_s0:
837 case M2_mpyud_nac_hl_s1:
838 if (OpN == 1) {
839 Bits.set(Begin+16, Begin+32);
840 return true;
841 }
842 if (OpN == 2) {
843 Bits.set(Begin, Begin+16);
844 return true;
845 }
846 break;
847
848 // Two register sources, used bits: R1[16-31], R2[16-31].
849 case A2_addh_h16_hh:
850 case A2_addh_h16_sat_hh:
851 case A2_combine_hh:
852 case A2_subh_h16_hh:
853 case A2_subh_h16_sat_hh:
854 case M2_mpy_acc_hh_s0:
855 case M2_mpy_acc_hh_s1:
856 case M2_mpy_acc_sat_hh_s0:
857 case M2_mpy_acc_sat_hh_s1:
858 case M2_mpy_hh_s0:
859 case M2_mpy_hh_s1:
860 case M2_mpy_nac_hh_s0:
861 case M2_mpy_nac_hh_s1:
862 case M2_mpy_nac_sat_hh_s0:
863 case M2_mpy_nac_sat_hh_s1:
864 case M2_mpy_rnd_hh_s0:
865 case M2_mpy_rnd_hh_s1:
866 case M2_mpy_sat_hh_s0:
867 case M2_mpy_sat_hh_s1:
868 case M2_mpy_sat_rnd_hh_s0:
869 case M2_mpy_sat_rnd_hh_s1:
870 case M2_mpyd_acc_hh_s0:
871 case M2_mpyd_acc_hh_s1:
872 case M2_mpyd_hh_s0:
873 case M2_mpyd_hh_s1:
874 case M2_mpyd_nac_hh_s0:
875 case M2_mpyd_nac_hh_s1:
876 case M2_mpyd_rnd_hh_s0:
877 case M2_mpyd_rnd_hh_s1:
878 case M2_mpyu_acc_hh_s0:
879 case M2_mpyu_acc_hh_s1:
880 case M2_mpyu_hh_s0:
881 case M2_mpyu_hh_s1:
882 case M2_mpyu_nac_hh_s0:
883 case M2_mpyu_nac_hh_s1:
884 case M2_mpyud_acc_hh_s0:
885 case M2_mpyud_acc_hh_s1:
886 case M2_mpyud_hh_s0:
887 case M2_mpyud_hh_s1:
888 case M2_mpyud_nac_hh_s0:
889 case M2_mpyud_nac_hh_s1:
890 if (OpN == 1 || OpN == 2) {
891 Bits.set(Begin+16, Begin+32);
892 return true;
893 }
894 break;
895 }
896
897 return false;
898}
899
900// Calculate the register class that matches Reg:Sub. For example, if
901// %1 is a double register, then %1:isub_hi would match the "int"
902// register class.
903const TargetRegisterClass *HexagonBitSimplify::getFinalVRegClass(
904 const BitTracker::RegisterRef &RR, MachineRegisterInfo &MRI) {
905 if (!RR.Reg.isVirtual())
906 return nullptr;
907 auto *RC = MRI.getRegClass(RR.Reg);
908 if (RR.Sub == 0)
909 return RC;
910 auto &HRI = static_cast<const HexagonRegisterInfo&>(
912
913 auto VerifySR = [&HRI] (const TargetRegisterClass *RC, unsigned Sub) -> void {
914 (void)HRI;
915 assert(Sub == HRI.getHexagonSubRegIndex(*RC, Hexagon::ps_sub_lo) ||
916 Sub == HRI.getHexagonSubRegIndex(*RC, Hexagon::ps_sub_hi));
917 };
918
919 switch (RC->getID()) {
920 case Hexagon::DoubleRegsRegClassID:
921 VerifySR(RC, RR.Sub);
922 return &Hexagon::IntRegsRegClass;
923 case Hexagon::HvxWRRegClassID:
924 VerifySR(RC, RR.Sub);
925 return &Hexagon::HvxVRRegClass;
926 case Hexagon::HlxLLRRegClassID:
927 VerifySR(RC, RR.Sub);
928 return &Hexagon::HlxLRRegClass;
929 }
930 return nullptr;
931}
932
933// Check if RD could be replaced with RS at any possible use of RD.
934// For example a predicate register cannot be replaced with a integer
935// register, but a 64-bit register with a subregister can be replaced
936// with a 32-bit register.
937bool HexagonBitSimplify::isTransparentCopy(const BitTracker::RegisterRef &RD,
938 const BitTracker::RegisterRef &RS, MachineRegisterInfo &MRI) {
939 if (!RD.Reg.isVirtual() || !RS.Reg.isVirtual())
940 return false;
941 // Return false if one (or both) classes are nullptr.
942 auto *DRC = getFinalVRegClass(RD, MRI);
943 if (!DRC)
944 return false;
945
946 return DRC == getFinalVRegClass(RS, MRI);
947}
948
949bool HexagonBitSimplify::hasTiedUse(unsigned Reg, MachineRegisterInfo &MRI,
950 unsigned NewSub) {
951 if (!PreserveTiedOps)
952 return false;
954 [NewSub] (const MachineOperand &Op) -> bool {
955 return Op.getSubReg() != NewSub && Op.isTied();
956 });
957}
958
959namespace {
960
961 class DeadCodeElimination {
962 public:
963 DeadCodeElimination(MachineFunction &mf, MachineDominatorTree &mdt)
964 : MF(mf), HII(*MF.getSubtarget<HexagonSubtarget>().getInstrInfo()),
965 MDT(mdt), MRI(mf.getRegInfo()) {}
966
967 bool run() {
968 return runOnNode(MDT.getRootNode());
969 }
970
971 private:
972 bool isDead(unsigned R) const;
973 bool runOnNode(MachineDomTreeNode *N);
974
975 MachineFunction &MF;
976 const HexagonInstrInfo &HII;
977 MachineDominatorTree &MDT;
978 MachineRegisterInfo &MRI;
979 };
980
981} // end anonymous namespace
982
983bool DeadCodeElimination::isDead(unsigned R) const {
984 for (const MachineInstr &UseI : MRI.use_instructions(R)) {
985 if (UseI.isDebugInstr())
986 continue;
987 if (UseI.isPHI()) {
988 assert(!UseI.getOperand(0).getSubReg());
989 Register DR = UseI.getOperand(0).getReg();
990 if (DR == R)
991 continue;
992 }
993 return false;
994 }
995 return true;
996}
997
998bool DeadCodeElimination::runOnNode(MachineDomTreeNode *N) {
999 bool Changed = false;
1000
1001 for (auto *DTN : children<MachineDomTreeNode*>(N))
1002 Changed |= runOnNode(DTN);
1003
1004 MachineBasicBlock *B = N->getBlock();
1005 std::vector<MachineInstr*> Instrs;
1006 for (MachineInstr &MI : llvm::reverse(*B))
1007 Instrs.push_back(&MI);
1008
1009 for (auto *MI : Instrs) {
1010 unsigned Opc = MI->getOpcode();
1011 // Do not touch lifetime markers. This is why the target-independent DCE
1012 // cannot be used.
1013 if (Opc == TargetOpcode::LIFETIME_START ||
1014 Opc == TargetOpcode::LIFETIME_END)
1015 continue;
1016 bool Store = false;
1017 if (MI->isInlineAsm())
1018 continue;
1019 // Delete PHIs if possible.
1020 if (!MI->isPHI() && !MI->isSafeToMove(Store))
1021 continue;
1022
1023 bool AllDead = true;
1024 SmallVector<unsigned,2> Regs;
1025 for (auto &Op : MI->operands()) {
1026 if (!Op.isReg() || !Op.isDef())
1027 continue;
1028 Register R = Op.getReg();
1029 if (!R.isVirtual() || !isDead(R)) {
1030 AllDead = false;
1031 break;
1032 }
1033 Regs.push_back(R);
1034 }
1035 if (!AllDead)
1036 continue;
1037
1038 B->erase(MI);
1039 for (unsigned Reg : Regs)
1041 Changed = true;
1042 }
1043
1044 return Changed;
1045}
1046
1047namespace {
1048
1049// Eliminate redundant instructions
1050//
1051// This transformation will identify instructions where the output register
1052// is the same as one of its input registers. This only works on instructions
1053// that define a single register (unlike post-increment loads, for example).
1054// The equality check is actually more detailed: the code calculates which
1055// bits of the output are used, and only compares these bits with the input
1056// registers.
1057// If the output matches an input, the instruction is replaced with COPY.
1058// The copies will be removed by another transformation.
1059 class RedundantInstrElimination : public Transformation {
1060 public:
1061 RedundantInstrElimination(BitTracker &bt, const HexagonInstrInfo &hii,
1062 const HexagonRegisterInfo &hri, MachineRegisterInfo &mri)
1063 : Transformation(true), HII(hii), HRI(hri), MRI(mri), BT(bt) {}
1064
1065 bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override;
1066
1067 private:
1068 bool isLossyShiftLeft(const MachineInstr &MI, unsigned OpN,
1069 unsigned &LostB, unsigned &LostE);
1070 bool isLossyShiftRight(const MachineInstr &MI, unsigned OpN,
1071 unsigned &LostB, unsigned &LostE);
1072 bool computeUsedBits(unsigned Reg, BitVector &Bits);
1073 bool computeUsedBits(const MachineInstr &MI, unsigned OpN, BitVector &Bits,
1074 uint16_t Begin);
1075 bool usedBitsEqual(BitTracker::RegisterRef RD, BitTracker::RegisterRef RS);
1076
1077 const HexagonInstrInfo &HII;
1078 const HexagonRegisterInfo &HRI;
1079 MachineRegisterInfo &MRI;
1080 BitTracker &BT;
1081 };
1082
1083} // end anonymous namespace
1084
1085// Check if the instruction is a lossy shift left, where the input being
1086// shifted is the operand OpN of MI. If true, [LostB, LostE) is the range
1087// of bit indices that are lost.
1088bool RedundantInstrElimination::isLossyShiftLeft(const MachineInstr &MI,
1089 unsigned OpN, unsigned &LostB, unsigned &LostE) {
1090 using namespace Hexagon;
1091
1092 unsigned Opc = MI.getOpcode();
1093 unsigned ImN, RegN, Width;
1094 switch (Opc) {
1095 case S2_asl_i_p:
1096 ImN = 2;
1097 RegN = 1;
1098 Width = 64;
1099 break;
1100 case S2_asl_i_p_acc:
1101 case S2_asl_i_p_and:
1102 case S2_asl_i_p_nac:
1103 case S2_asl_i_p_or:
1104 case S2_asl_i_p_xacc:
1105 ImN = 3;
1106 RegN = 2;
1107 Width = 64;
1108 break;
1109 case S2_asl_i_r:
1110 ImN = 2;
1111 RegN = 1;
1112 Width = 32;
1113 break;
1114 case S2_addasl_rrri:
1115 case S4_andi_asl_ri:
1116 case S4_ori_asl_ri:
1117 case S4_addi_asl_ri:
1118 case S4_subi_asl_ri:
1119 case S2_asl_i_r_acc:
1120 case S2_asl_i_r_and:
1121 case S2_asl_i_r_nac:
1122 case S2_asl_i_r_or:
1123 case S2_asl_i_r_sat:
1124 case S2_asl_i_r_xacc:
1125 ImN = 3;
1126 RegN = 2;
1127 Width = 32;
1128 break;
1129 default:
1130 return false;
1131 }
1132
1133 if (RegN != OpN)
1134 return false;
1135
1136 assert(MI.getOperand(ImN).isImm());
1137 unsigned S = MI.getOperand(ImN).getImm();
1138 if (S == 0)
1139 return false;
1140 LostB = Width-S;
1141 LostE = Width;
1142 return true;
1143}
1144
1145// Check if the instruction is a lossy shift right, where the input being
1146// shifted is the operand OpN of MI. If true, [LostB, LostE) is the range
1147// of bit indices that are lost.
1148bool RedundantInstrElimination::isLossyShiftRight(const MachineInstr &MI,
1149 unsigned OpN, unsigned &LostB, unsigned &LostE) {
1150 using namespace Hexagon;
1151
1152 unsigned Opc = MI.getOpcode();
1153 unsigned ImN, RegN;
1154 switch (Opc) {
1155 case S2_asr_i_p:
1156 case S2_lsr_i_p:
1157 ImN = 2;
1158 RegN = 1;
1159 break;
1160 case S2_asr_i_p_acc:
1161 case S2_asr_i_p_and:
1162 case S2_asr_i_p_nac:
1163 case S2_asr_i_p_or:
1164 case S2_lsr_i_p_acc:
1165 case S2_lsr_i_p_and:
1166 case S2_lsr_i_p_nac:
1167 case S2_lsr_i_p_or:
1168 case S2_lsr_i_p_xacc:
1169 ImN = 3;
1170 RegN = 2;
1171 break;
1172 case S2_asr_i_r:
1173 case S2_lsr_i_r:
1174 ImN = 2;
1175 RegN = 1;
1176 break;
1177 case S4_andi_lsr_ri:
1178 case S4_ori_lsr_ri:
1179 case S4_addi_lsr_ri:
1180 case S4_subi_lsr_ri:
1181 case S2_asr_i_r_acc:
1182 case S2_asr_i_r_and:
1183 case S2_asr_i_r_nac:
1184 case S2_asr_i_r_or:
1185 case S2_lsr_i_r_acc:
1186 case S2_lsr_i_r_and:
1187 case S2_lsr_i_r_nac:
1188 case S2_lsr_i_r_or:
1189 case S2_lsr_i_r_xacc:
1190 ImN = 3;
1191 RegN = 2;
1192 break;
1193
1194 default:
1195 return false;
1196 }
1197
1198 if (RegN != OpN)
1199 return false;
1200
1201 assert(MI.getOperand(ImN).isImm());
1202 unsigned S = MI.getOperand(ImN).getImm();
1203 LostB = 0;
1204 LostE = S;
1205 return true;
1206}
1207
1208// Calculate the bit vector that corresponds to the used bits of register Reg.
1209// The vector Bits has the same size, as the size of Reg in bits. If the cal-
1210// culation fails (i.e. the used bits are unknown), it returns false. Other-
1211// wise, it returns true and sets the corresponding bits in Bits.
1212bool RedundantInstrElimination::computeUsedBits(unsigned Reg, BitVector &Bits) {
1213 BitVector Used(Bits.size());
1214 RegisterSet Visited;
1215 std::vector<unsigned> Pending;
1216 Pending.push_back(Reg);
1217
1218 for (unsigned i = 0; i < Pending.size(); ++i) {
1219 unsigned R = Pending[i];
1220 if (Visited.has(R))
1221 continue;
1222 Visited.insert(R);
1223 for (auto I = MRI.use_begin(R), E = MRI.use_end(); I != E; ++I) {
1224 BitTracker::RegisterRef UR = *I;
1225 unsigned B, W;
1226 if (!HBS::getSubregMask(UR, B, W, MRI))
1227 return false;
1228 MachineInstr &UseI = *I->getParent();
1229 if (UseI.isPHI() || UseI.isCopy()) {
1230 Register DefR = UseI.getOperand(0).getReg();
1231 if (!DefR.isVirtual())
1232 return false;
1233 Pending.push_back(DefR);
1234 } else {
1235 if (!computeUsedBits(UseI, I.getOperandNo(), Used, B))
1236 return false;
1237 }
1238 }
1239 }
1240 Bits |= Used;
1241 return true;
1242}
1243
1244// Calculate the bits used by instruction MI in a register in operand OpN.
1245// Return true/false if the calculation succeeds/fails. If is succeeds, set
1246// used bits in Bits. This function does not reset any bits in Bits, so
1247// subsequent calls over different instructions will result in the union
1248// of the used bits in all these instructions.
1249// The register in question may be used with a sub-register, whereas Bits
1250// holds the bits for the entire register. To keep track of that, the
1251// argument Begin indicates where in Bits is the lowest-significant bit
1252// of the register used in operand OpN. For example, in instruction:
1253// %1 = S2_lsr_i_r %2:isub_hi, 10
1254// the operand 1 is a 32-bit register, which happens to be a subregister
1255// of the 64-bit register %2, and that subregister starts at position 32.
1256// In this case Begin=32, since Bits[32] would be the lowest-significant bit
1257// of %2:isub_hi.
1258bool RedundantInstrElimination::computeUsedBits(const MachineInstr &MI,
1259 unsigned OpN, BitVector &Bits, uint16_t Begin) {
1260 unsigned Opc = MI.getOpcode();
1261 BitVector T(Bits.size());
1262 bool GotBits = HBS::getUsedBits(Opc, OpN, T, Begin, HII);
1263 // Even if we don't have bits yet, we could still provide some information
1264 // if the instruction is a lossy shift: the lost bits will be marked as
1265 // not used.
1266 unsigned LB, LE;
1267 if (isLossyShiftLeft(MI, OpN, LB, LE) || isLossyShiftRight(MI, OpN, LB, LE)) {
1268 assert(MI.getOperand(OpN).isReg());
1269 BitTracker::RegisterRef RR = MI.getOperand(OpN);
1270 const TargetRegisterClass *RC = HBS::getFinalVRegClass(RR, MRI);
1271 uint16_t Width = HRI.getRegSizeInBits(*RC);
1272
1273 if (!GotBits)
1274 T.set(Begin, Begin+Width);
1275 assert(LB <= LE && LB < Width && LE <= Width);
1276 T.reset(Begin+LB, Begin+LE);
1277 GotBits = true;
1278 }
1279 if (GotBits)
1280 Bits |= T;
1281 return GotBits;
1282}
1283
1284// Calculates the used bits in RD ("defined register"), and checks if these
1285// bits in RS ("used register") and RD are identical.
1286bool RedundantInstrElimination::usedBitsEqual(BitTracker::RegisterRef RD,
1287 BitTracker::RegisterRef RS) {
1288 const BitTracker::RegisterCell &DC = BT.lookup(RD.Reg);
1289 const BitTracker::RegisterCell &SC = BT.lookup(RS.Reg);
1290
1291 unsigned DB, DW;
1292 if (!HBS::getSubregMask(RD, DB, DW, MRI))
1293 return false;
1294 unsigned SB, SW;
1295 if (!HBS::getSubregMask(RS, SB, SW, MRI))
1296 return false;
1297 if (SW != DW)
1298 return false;
1299
1300 BitVector Used(DC.width());
1301 if (!computeUsedBits(RD.Reg, Used))
1302 return false;
1303
1304 for (unsigned i = 0; i != DW; ++i)
1305 if (Used[i+DB] && DC[DB+i] != SC[SB+i])
1306 return false;
1307 return true;
1308}
1309
1310bool RedundantInstrElimination::processBlock(MachineBasicBlock &B,
1311 const RegisterSet&) {
1312 if (!BT.reached(&B))
1313 return false;
1314 bool Changed = false;
1315
1316 for (auto I = B.begin(), E = B.end(); I != E; ++I) {
1317 MachineInstr *MI = &*I;
1318
1319 if (MI->getOpcode() == TargetOpcode::COPY)
1320 continue;
1321 if (MI->isPHI() || MI->hasUnmodeledSideEffects() || MI->isInlineAsm())
1322 continue;
1323 unsigned NumD = MI->getDesc().getNumDefs();
1324 if (NumD != 1)
1325 continue;
1326
1327 BitTracker::RegisterRef RD = MI->getOperand(0);
1328 if (!BT.has(RD.Reg))
1329 continue;
1330 const BitTracker::RegisterCell &DC = BT.lookup(RD.Reg);
1332
1333 // Find a source operand that is equal to the result.
1334 for (auto &Op : MI->uses()) {
1335 if (!Op.isReg())
1336 continue;
1337 BitTracker::RegisterRef RS = Op;
1338 if (!BT.has(RS.Reg))
1339 continue;
1340 if (!HBS::isTransparentCopy(RD, RS, MRI))
1341 continue;
1342
1343 unsigned BN, BW;
1344 if (!HBS::getSubregMask(RS, BN, BW, MRI))
1345 continue;
1346
1347 const BitTracker::RegisterCell &SC = BT.lookup(RS.Reg);
1348 if (!usedBitsEqual(RD, RS) && !HBS::isEqual(DC, 0, SC, BN, BW))
1349 continue;
1350
1351 // If found, replace the instruction with a COPY.
1352 const DebugLoc &DL = MI->getDebugLoc();
1353 const TargetRegisterClass *FRC = HBS::getFinalVRegClass(RD, MRI);
1355 MachineInstr *CopyI =
1356 BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
1357 .addReg(RS.Reg, {}, RS.Sub);
1358 HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI);
1359 // This pass can create copies between registers that don't have the
1360 // exact same values. Updating the tracker has to involve updating
1361 // all dependent cells. Example:
1362 // %1 = inst %2 ; %1 != %2, but used bits are equal
1363 //
1364 // %3 = copy %2 ; <- inserted
1365 // ... = %3 ; <- replaced from %2
1366 // Indirectly, we can create a "copy" between %1 and %2 even
1367 // though their exact values do not match.
1368 BT.visit(*CopyI);
1369 Changed = true;
1370 break;
1371 }
1372 }
1373
1374 return Changed;
1375}
1376
1377namespace {
1378
1379// Recognize instructions that produce constant values known at compile-time.
1380// Replace them with register definitions that load these constants directly.
1381 class ConstGeneration : public Transformation {
1382 public:
1383 ConstGeneration(BitTracker &bt, const HexagonInstrInfo &hii,
1384 MachineRegisterInfo &mri)
1385 : Transformation(true), HII(hii), MRI(mri), BT(bt) {}
1386
1387 bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override;
1388 static bool isTfrConst(const MachineInstr &MI);
1389
1390 private:
1391 Register genTfrConst(const TargetRegisterClass *RC, int64_t C,
1392 MachineBasicBlock &B, MachineBasicBlock::iterator At,
1393 DebugLoc &DL);
1394
1395 const HexagonInstrInfo &HII;
1396 MachineRegisterInfo &MRI;
1397 BitTracker &BT;
1398 };
1399
1400} // end anonymous namespace
1401
1402bool ConstGeneration::isTfrConst(const MachineInstr &MI) {
1403 unsigned Opc = MI.getOpcode();
1404 switch (Opc) {
1405 case Hexagon::A2_combineii:
1406 case Hexagon::A4_combineii:
1407 case Hexagon::A2_tfrsi:
1408 case Hexagon::A2_tfrpi:
1409 case Hexagon::PS_true:
1410 case Hexagon::PS_false:
1411 case Hexagon::CONST32:
1412 case Hexagon::CONST64:
1413 return true;
1414 }
1415 return false;
1416}
1417
1418// Generate a transfer-immediate instruction that is appropriate for the
1419// register class and the actual value being transferred.
1420Register ConstGeneration::genTfrConst(const TargetRegisterClass *RC, int64_t C,
1421 MachineBasicBlock &B,
1423 DebugLoc &DL) {
1425 if (RC == &Hexagon::IntRegsRegClass) {
1426 BuildMI(B, At, DL, HII.get(Hexagon::A2_tfrsi), Reg)
1427 .addImm(int32_t(C));
1428 return Reg;
1429 }
1430
1431 if (RC == &Hexagon::DoubleRegsRegClass) {
1432 if (isInt<8>(C)) {
1433 BuildMI(B, At, DL, HII.get(Hexagon::A2_tfrpi), Reg)
1434 .addImm(C);
1435 return Reg;
1436 }
1437
1438 unsigned Lo = Lo_32(C), Hi = Hi_32(C);
1439 if (isInt<8>(Lo) || isInt<8>(Hi)) {
1440 unsigned Opc = isInt<8>(Lo) ? Hexagon::A2_combineii
1441 : Hexagon::A4_combineii;
1442 BuildMI(B, At, DL, HII.get(Opc), Reg)
1443 .addImm(int32_t(Hi))
1444 .addImm(int32_t(Lo));
1445 return Reg;
1446 }
1447 MachineFunction *MF = B.getParent();
1448 auto &HST = MF->getSubtarget<HexagonSubtarget>();
1449
1450 // Disable CONST64 for tiny core since it takes a LD resource.
1451 if (!HST.isTinyCore() ||
1452 MF->getFunction().hasOptSize()) {
1453 BuildMI(B, At, DL, HII.get(Hexagon::CONST64), Reg)
1454 .addImm(C);
1455 return Reg;
1456 }
1457 }
1458
1459 if (RC == &Hexagon::PredRegsRegClass) {
1460 unsigned Opc;
1461 if (C == 0)
1462 Opc = Hexagon::PS_false;
1463 else if ((C & 0xFF) == 0xFF)
1464 Opc = Hexagon::PS_true;
1465 else
1466 return 0;
1467 BuildMI(B, At, DL, HII.get(Opc), Reg);
1468 return Reg;
1469 }
1470
1471 return 0;
1472}
1473
1474bool ConstGeneration::processBlock(MachineBasicBlock &B, const RegisterSet&) {
1475 if (!BT.reached(&B))
1476 return false;
1477 bool Changed = false;
1478 RegisterSet Defs;
1479
1480 for (auto I = B.begin(), E = B.end(); I != E; ++I) {
1481 if (isTfrConst(*I))
1482 continue;
1483 Defs.clear();
1484 HBS::getInstrDefs(*I, Defs);
1485 if (Defs.count() != 1)
1486 continue;
1487 Register DR = Defs.find_first();
1488 if (!DR.isVirtual())
1489 continue;
1490 uint64_t U;
1491 const BitTracker::RegisterCell &DRC = BT.lookup(DR);
1492 if (HBS::getConst(DRC, 0, DRC.width(), U)) {
1493 int64_t C = U;
1494 DebugLoc DL = I->getDebugLoc();
1495 auto At = I->isPHI() ? B.getFirstNonPHI() : I;
1496 Register ImmReg = genTfrConst(MRI.getRegClass(DR), C, B, At, DL);
1497 if (ImmReg) {
1498 HBS::replaceReg(DR, ImmReg, MRI);
1499 BT.put(ImmReg, DRC);
1500 Changed = true;
1501 }
1502 }
1503 }
1504 return Changed;
1505}
1506
1507namespace {
1508
1509// Identify pairs of available registers which hold identical values.
1510// In such cases, only one of them needs to be calculated, the other one
1511// will be defined as a copy of the first.
1512 class CopyGeneration : public Transformation {
1513 public:
1514 CopyGeneration(BitTracker &bt, const HexagonInstrInfo &hii,
1515 const HexagonRegisterInfo &hri, MachineRegisterInfo &mri)
1516 : Transformation(true), HII(hii), HRI(hri), MRI(mri), BT(bt) {}
1517
1518 bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override;
1519
1520 private:
1521 bool findMatch(const BitTracker::RegisterRef &Inp,
1522 BitTracker::RegisterRef &Out, const RegisterSet &AVs);
1523
1524 const HexagonInstrInfo &HII;
1525 const HexagonRegisterInfo &HRI;
1526 MachineRegisterInfo &MRI;
1527 BitTracker &BT;
1528 RegisterSet Forbidden;
1529 };
1530
1531// Eliminate register copies RD = RS, by replacing the uses of RD with
1532// with uses of RS.
1533 class CopyPropagation : public Transformation {
1534 public:
1535 CopyPropagation(const HexagonRegisterInfo &hri, MachineRegisterInfo &mri)
1536 : Transformation(false), HRI(hri), MRI(mri) {}
1537
1538 bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override;
1539
1540 static bool isCopyReg(unsigned Opc, bool NoConv);
1541
1542 private:
1543 bool propagateRegCopy(MachineInstr &MI);
1544
1545 const HexagonRegisterInfo &HRI;
1546 MachineRegisterInfo &MRI;
1547 };
1548
1549} // end anonymous namespace
1550
1551/// Check if there is a register in AVs that is identical to Inp. If so,
1552/// set Out to the found register. The output may be a pair Reg:Sub.
1553bool CopyGeneration::findMatch(const BitTracker::RegisterRef &Inp,
1554 BitTracker::RegisterRef &Out, const RegisterSet &AVs) {
1555 if (!BT.has(Inp.Reg))
1556 return false;
1557 const BitTracker::RegisterCell &InpRC = BT.lookup(Inp.Reg);
1558 auto *FRC = HBS::getFinalVRegClass(Inp, MRI);
1559 unsigned B, W;
1560 if (!HBS::getSubregMask(Inp, B, W, MRI))
1561 return false;
1562
1563 for (Register R = AVs.find_first(); R; R = AVs.find_next(R)) {
1564 if (!BT.has(R) || Forbidden[R])
1565 continue;
1566 const BitTracker::RegisterCell &RC = BT.lookup(R);
1567 unsigned RW = RC.width();
1568 if (W == RW) {
1569 if (FRC != MRI.getRegClass(R))
1570 continue;
1571 if (!HBS::isTransparentCopy(R, Inp, MRI))
1572 continue;
1573 if (!HBS::isEqual(InpRC, B, RC, 0, W))
1574 continue;
1575 Out.Reg = R;
1576 Out.Sub = 0;
1577 return true;
1578 }
1579 // Check if there is a super-register, whose part (with a subregister)
1580 // is equal to the input.
1581 // Only do double registers for now.
1582 if (W*2 != RW)
1583 continue;
1584 if (MRI.getRegClass(R) != &Hexagon::DoubleRegsRegClass)
1585 continue;
1586
1587 if (HBS::isEqual(InpRC, B, RC, 0, W))
1588 Out.Sub = Hexagon::isub_lo;
1589 else if (HBS::isEqual(InpRC, B, RC, W, W))
1590 Out.Sub = Hexagon::isub_hi;
1591 else
1592 continue;
1593 Out.Reg = R;
1594 if (HBS::isTransparentCopy(Out, Inp, MRI))
1595 return true;
1596 }
1597 return false;
1598}
1599
1600bool CopyGeneration::processBlock(MachineBasicBlock &B,
1601 const RegisterSet &AVs) {
1602 if (!BT.reached(&B))
1603 return false;
1604 RegisterSet AVB(AVs);
1605 bool Changed = false;
1606 RegisterSet Defs;
1607
1608 for (auto I = B.begin(), E = B.end(); I != E; ++I, AVB.insert(Defs)) {
1609 Defs.clear();
1610 HBS::getInstrDefs(*I, Defs);
1611
1612 unsigned Opc = I->getOpcode();
1613 if (CopyPropagation::isCopyReg(Opc, false) ||
1614 ConstGeneration::isTfrConst(*I))
1615 continue;
1616
1617 DebugLoc DL = I->getDebugLoc();
1618 auto At = I->isPHI() ? B.getFirstNonPHI() : I;
1619
1620 for (Register R = Defs.find_first(); R; R = Defs.find_next(R)) {
1621 BitTracker::RegisterRef MR;
1622 auto *FRC = HBS::getFinalVRegClass(R, MRI);
1623
1624 if (findMatch(R, MR, AVB)) {
1626 BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
1627 .addReg(MR.Reg, {}, MR.Sub);
1628 BT.put(BitTracker::RegisterRef(NewR), BT.get(MR));
1629 HBS::replaceReg(R, NewR, MRI);
1630 Forbidden.insert(R);
1631 continue;
1632 }
1633
1634 if (FRC == &Hexagon::DoubleRegsRegClass ||
1635 FRC == &Hexagon::HvxWRRegClass) {
1636 // Try to generate REG_SEQUENCE.
1637 unsigned SubLo = HRI.getHexagonSubRegIndex(*FRC, Hexagon::ps_sub_lo);
1638 unsigned SubHi = HRI.getHexagonSubRegIndex(*FRC, Hexagon::ps_sub_hi);
1639 BitTracker::RegisterRef TL = { R, SubLo };
1640 BitTracker::RegisterRef TH = { R, SubHi };
1641 BitTracker::RegisterRef ML, MH;
1642 if (findMatch(TL, ML, AVB) && findMatch(TH, MH, AVB)) {
1643 auto *FRC = HBS::getFinalVRegClass(R, MRI);
1645 BuildMI(B, At, DL, HII.get(TargetOpcode::REG_SEQUENCE), NewR)
1646 .addReg(ML.Reg, {}, ML.Sub)
1647 .addImm(SubLo)
1648 .addReg(MH.Reg, {}, MH.Sub)
1649 .addImm(SubHi);
1650 BT.put(BitTracker::RegisterRef(NewR), BT.get(R));
1651 HBS::replaceReg(R, NewR, MRI);
1652 Forbidden.insert(R);
1653 }
1654 }
1655 }
1656 }
1657
1658 return Changed;
1659}
1660
1661bool CopyPropagation::isCopyReg(unsigned Opc, bool NoConv) {
1662 switch (Opc) {
1663 case TargetOpcode::COPY:
1664 case TargetOpcode::REG_SEQUENCE:
1665 case Hexagon::A4_combineir:
1666 case Hexagon::A4_combineri:
1667 return true;
1668 case Hexagon::A2_tfr:
1669 case Hexagon::A2_tfrp:
1670 case Hexagon::A2_combinew:
1671 case Hexagon::V6_vcombine:
1672 return NoConv;
1673 default:
1674 break;
1675 }
1676 return false;
1677}
1678
1679bool CopyPropagation::propagateRegCopy(MachineInstr &MI) {
1680 bool Changed = false;
1681 unsigned Opc = MI.getOpcode();
1682 BitTracker::RegisterRef RD = MI.getOperand(0);
1683 assert(MI.getOperand(0).getSubReg() == 0);
1684
1685 switch (Opc) {
1686 case TargetOpcode::COPY:
1687 case Hexagon::A2_tfr:
1688 case Hexagon::A2_tfrp: {
1689 BitTracker::RegisterRef RS = MI.getOperand(1);
1690 if (!HBS::isTransparentCopy(RD, RS, MRI))
1691 break;
1692 if (RS.Sub != 0)
1693 Changed = HBS::replaceRegWithSub(RD.Reg, RS.Reg, RS.Sub, MRI);
1694 else
1695 Changed = HBS::replaceReg(RD.Reg, RS.Reg, MRI);
1696 break;
1697 }
1698 case TargetOpcode::REG_SEQUENCE: {
1699 BitTracker::RegisterRef SL, SH;
1700 if (HBS::parseRegSequence(MI, SL, SH, MRI)) {
1701 const TargetRegisterClass &RC = *MRI.getRegClass(RD.Reg);
1702 unsigned SubLo = HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_lo);
1703 unsigned SubHi = HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_hi);
1704 Changed = HBS::replaceSubWithSub(RD.Reg, SubLo, SL.Reg, SL.Sub, MRI);
1705 Changed |= HBS::replaceSubWithSub(RD.Reg, SubHi, SH.Reg, SH.Sub, MRI);
1706 }
1707 break;
1708 }
1709 case Hexagon::A2_combinew:
1710 case Hexagon::V6_vcombine: {
1711 const TargetRegisterClass &RC = *MRI.getRegClass(RD.Reg);
1712 unsigned SubLo = HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_lo);
1713 unsigned SubHi = HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_hi);
1714 BitTracker::RegisterRef RH = MI.getOperand(1), RL = MI.getOperand(2);
1715 Changed = HBS::replaceSubWithSub(RD.Reg, SubLo, RL.Reg, RL.Sub, MRI);
1716 Changed |= HBS::replaceSubWithSub(RD.Reg, SubHi, RH.Reg, RH.Sub, MRI);
1717 break;
1718 }
1719 case Hexagon::A4_combineir:
1720 case Hexagon::A4_combineri: {
1721 unsigned SrcX = (Opc == Hexagon::A4_combineir) ? 2 : 1;
1722 unsigned Sub = (Opc == Hexagon::A4_combineir) ? Hexagon::isub_lo
1723 : Hexagon::isub_hi;
1724 BitTracker::RegisterRef RS = MI.getOperand(SrcX);
1725 Changed = HBS::replaceSubWithSub(RD.Reg, Sub, RS.Reg, RS.Sub, MRI);
1726 break;
1727 }
1728 }
1729 return Changed;
1730}
1731
1732bool CopyPropagation::processBlock(MachineBasicBlock &B, const RegisterSet&) {
1733 std::vector<MachineInstr*> Instrs;
1734 for (MachineInstr &MI : llvm::reverse(B))
1735 Instrs.push_back(&MI);
1736
1737 bool Changed = false;
1738 for (auto *I : Instrs) {
1739 unsigned Opc = I->getOpcode();
1740 if (!CopyPropagation::isCopyReg(Opc, true))
1741 continue;
1742 Changed |= propagateRegCopy(*I);
1743 }
1744
1745 return Changed;
1746}
1747
1748namespace {
1749
1750// Recognize patterns that can be simplified and replace them with the
1751// simpler forms.
1752// This is by no means complete
1753 class BitSimplification : public Transformation {
1754 public:
1755 BitSimplification(BitTracker &bt, const MachineDominatorTree &mdt,
1756 const HexagonInstrInfo &hii,
1757 const HexagonRegisterInfo &hri, MachineRegisterInfo &mri,
1758 MachineFunction &mf)
1759 : Transformation(true), MDT(mdt), HII(hii), HRI(hri), MRI(mri), BT(bt) {
1760 }
1761
1762 bool processBlock(MachineBasicBlock &B, const RegisterSet &AVs) override;
1763
1764 private:
1765 struct RegHalf : public BitTracker::RegisterRef {
1766 bool Low; // Low/High halfword.
1767 };
1768
1769 bool matchHalf(unsigned SelfR, const BitTracker::RegisterCell &RC,
1770 unsigned B, RegHalf &RH);
1771 bool validateReg(BitTracker::RegisterRef R, unsigned Opc, unsigned OpNum);
1772
1773 bool matchPackhl(unsigned SelfR, const BitTracker::RegisterCell &RC,
1774 BitTracker::RegisterRef &Rs, BitTracker::RegisterRef &Rt);
1775 unsigned getCombineOpcode(bool HLow, bool LLow);
1776
1777 bool genStoreUpperHalf(MachineInstr *MI);
1778 bool genStoreImmediate(MachineInstr *MI);
1779 bool genPackhl(MachineInstr *MI, BitTracker::RegisterRef RD,
1780 const BitTracker::RegisterCell &RC);
1781 bool genExtractHalf(MachineInstr *MI, BitTracker::RegisterRef RD,
1782 const BitTracker::RegisterCell &RC);
1783 bool genCombineHalf(MachineInstr *MI, BitTracker::RegisterRef RD,
1784 const BitTracker::RegisterCell &RC);
1785 bool genExtractLow(MachineInstr *MI, BitTracker::RegisterRef RD,
1786 const BitTracker::RegisterCell &RC);
1787 bool genBitSplit(MachineInstr *MI, BitTracker::RegisterRef RD,
1788 const BitTracker::RegisterCell &RC, const RegisterSet &AVs);
1789 bool simplifyTstbit(MachineInstr *MI, BitTracker::RegisterRef RD,
1790 const BitTracker::RegisterCell &RC);
1791 bool simplifyExtractLow(MachineInstr *MI, BitTracker::RegisterRef RD,
1792 const BitTracker::RegisterCell &RC, const RegisterSet &AVs);
1793 bool simplifyRCmp0(MachineInstr *MI, BitTracker::RegisterRef RD);
1794
1795 // Cache of created instructions to avoid creating duplicates.
1796 // XXX Currently only used by genBitSplit.
1797 std::vector<MachineInstr*> NewMIs;
1798
1799 const MachineDominatorTree &MDT;
1800 const HexagonInstrInfo &HII;
1801 [[maybe_unused]] const HexagonRegisterInfo &HRI;
1802 MachineRegisterInfo &MRI;
1803 BitTracker &BT;
1804 };
1805
1806} // end anonymous namespace
1807
1808// Check if the bits [B..B+16) in register cell RC form a valid halfword,
1809// i.e. [0..16), [16..32), etc. of some register. If so, return true and
1810// set the information about the found register in RH.
1811bool BitSimplification::matchHalf(unsigned SelfR,
1812 const BitTracker::RegisterCell &RC, unsigned B, RegHalf &RH) {
1813 // XXX This could be searching in the set of available registers, in case
1814 // the match is not exact.
1815
1816 // Match 16-bit chunks, where the RC[B..B+15] references exactly one
1817 // register and all the bits B..B+15 match between RC and the register.
1818 // This is meant to match "v1[0-15]", where v1 = { [0]:0 [1-15]:v1... },
1819 // and RC = { [0]:0 [1-15]:v1[1-15]... }.
1820 bool Low = false;
1821 unsigned I = B;
1822 while (I < B+16 && RC[I].num())
1823 I++;
1824 if (I == B+16)
1825 return false;
1826
1827 Register Reg = RC[I].RefI.Reg;
1828 unsigned P = RC[I].RefI.Pos; // The RefI.Pos will be advanced by I-B.
1829 if (P < I-B)
1830 return false;
1831 unsigned Pos = P - (I-B);
1832
1833 if (Reg == 0 || Reg == SelfR) // Don't match "self".
1834 return false;
1835 if (!Reg.isVirtual())
1836 return false;
1837 if (!BT.has(Reg))
1838 return false;
1839
1840 const BitTracker::RegisterCell &SC = BT.lookup(Reg);
1841 if (Pos+16 > SC.width())
1842 return false;
1843
1844 for (unsigned i = 0; i < 16; ++i) {
1845 const BitTracker::BitValue &RV = RC[i+B];
1846 if (RV.Type == BitTracker::BitValue::Ref) {
1847 if (RV.RefI.Reg != Reg)
1848 return false;
1849 if (RV.RefI.Pos != i+Pos)
1850 return false;
1851 continue;
1852 }
1853 if (RC[i+B] != SC[i+Pos])
1854 return false;
1855 }
1856
1857 unsigned Sub = 0;
1858 switch (Pos) {
1859 case 0:
1860 Sub = Hexagon::isub_lo;
1861 Low = true;
1862 break;
1863 case 16:
1864 Sub = Hexagon::isub_lo;
1865 Low = false;
1866 break;
1867 case 32:
1868 Sub = Hexagon::isub_hi;
1869 Low = true;
1870 break;
1871 case 48:
1872 Sub = Hexagon::isub_hi;
1873 Low = false;
1874 break;
1875 default:
1876 return false;
1877 }
1878
1879 RH.Reg = Reg;
1880 RH.Sub = Sub;
1881 RH.Low = Low;
1882 // If the subregister is not valid with the register, set it to 0.
1883 if (!HBS::getFinalVRegClass(RH, MRI))
1884 RH.Sub = 0;
1885
1886 return true;
1887}
1888
1889bool BitSimplification::validateReg(BitTracker::RegisterRef R, unsigned Opc,
1890 unsigned OpNum) {
1891 auto *OpRC = HII.getRegClass(HII.get(Opc), OpNum);
1892 auto *RRC = HBS::getFinalVRegClass(R, MRI);
1893 return OpRC->hasSubClassEq(RRC);
1894}
1895
1896// Check if RC matches the pattern of a S2_packhl. If so, return true and
1897// set the inputs Rs and Rt.
1898bool BitSimplification::matchPackhl(unsigned SelfR,
1899 const BitTracker::RegisterCell &RC, BitTracker::RegisterRef &Rs,
1900 BitTracker::RegisterRef &Rt) {
1901 RegHalf L1, H1, L2, H2;
1902
1903 if (!matchHalf(SelfR, RC, 0, L2) || !matchHalf(SelfR, RC, 16, L1))
1904 return false;
1905 if (!matchHalf(SelfR, RC, 32, H2) || !matchHalf(SelfR, RC, 48, H1))
1906 return false;
1907
1908 // Rs = H1.L1, Rt = H2.L2
1909 if (H1.Reg != L1.Reg || H1.Sub != L1.Sub || H1.Low || !L1.Low)
1910 return false;
1911 if (H2.Reg != L2.Reg || H2.Sub != L2.Sub || H2.Low || !L2.Low)
1912 return false;
1913
1914 Rs = H1;
1915 Rt = H2;
1916 return true;
1917}
1918
1919unsigned BitSimplification::getCombineOpcode(bool HLow, bool LLow) {
1920 return HLow ? LLow ? Hexagon::A2_combine_ll
1921 : Hexagon::A2_combine_lh
1922 : LLow ? Hexagon::A2_combine_hl
1923 : Hexagon::A2_combine_hh;
1924}
1925
1926// If MI stores the upper halfword of a register (potentially obtained via
1927// shifts or extracts), replace it with a storerf instruction. This could
1928// cause the "extraction" code to become dead.
1929bool BitSimplification::genStoreUpperHalf(MachineInstr *MI) {
1930 unsigned Opc = MI->getOpcode();
1931 if (Opc != Hexagon::S2_storerh_io)
1932 return false;
1933
1934 MachineOperand &ValOp = MI->getOperand(2);
1935 BitTracker::RegisterRef RS = ValOp;
1936 if (!BT.has(RS.Reg))
1937 return false;
1938 const BitTracker::RegisterCell &RC = BT.lookup(RS.Reg);
1939 RegHalf H;
1940 unsigned B = (RS.Sub == Hexagon::isub_hi) ? 32 : 0;
1941 if (!matchHalf(0, RC, B, H))
1942 return false;
1943 if (H.Low)
1944 return false;
1945 MI->setDesc(HII.get(Hexagon::S2_storerf_io));
1946 ValOp.setReg(H.Reg);
1947 ValOp.setSubReg(H.Sub);
1948 return true;
1949}
1950
1951// If MI stores a value known at compile-time, and the value is within a range
1952// that avoids using constant-extenders, replace it with a store-immediate.
1953bool BitSimplification::genStoreImmediate(MachineInstr *MI) {
1954 unsigned Opc = MI->getOpcode();
1955 unsigned Align = 0;
1956 switch (Opc) {
1957 case Hexagon::S2_storeri_io:
1958 Align++;
1959 [[fallthrough]];
1960 case Hexagon::S2_storerh_io:
1961 Align++;
1962 [[fallthrough]];
1963 case Hexagon::S2_storerb_io:
1964 break;
1965 default:
1966 return false;
1967 }
1968
1969 // Avoid stores to frame-indices (due to an unknown offset).
1970 if (!MI->getOperand(0).isReg())
1971 return false;
1972 MachineOperand &OffOp = MI->getOperand(1);
1973 if (!OffOp.isImm())
1974 return false;
1975
1976 int64_t Off = OffOp.getImm();
1977 // Offset is u6:a. Sadly, there is no isShiftedUInt(n,x).
1978 if (!isUIntN(6+Align, Off) || (Off & ((1<<Align)-1)))
1979 return false;
1980 // Source register:
1981 BitTracker::RegisterRef RS = MI->getOperand(2);
1982 if (!BT.has(RS.Reg))
1983 return false;
1984 const BitTracker::RegisterCell &RC = BT.lookup(RS.Reg);
1985 uint64_t U;
1986 if (!HBS::getConst(RC, 0, RC.width(), U))
1987 return false;
1988
1989 // Only consider 8-bit values to avoid constant-extenders.
1990 int V;
1991 switch (Opc) {
1992 case Hexagon::S2_storerb_io:
1993 V = int8_t(U);
1994 break;
1995 case Hexagon::S2_storerh_io:
1996 V = int16_t(U);
1997 break;
1998 case Hexagon::S2_storeri_io:
1999 V = int32_t(U);
2000 break;
2001 default:
2002 // Opc is already checked above to be one of the three store instructions.
2003 // This silences a -Wuninitialized false positive on GCC 5.4.
2004 llvm_unreachable("Unexpected store opcode");
2005 }
2006 if (!isInt<8>(V))
2007 return false;
2008
2009 MI->removeOperand(2);
2010 switch (Opc) {
2011 case Hexagon::S2_storerb_io:
2012 MI->setDesc(HII.get(Hexagon::S4_storeirb_io));
2013 break;
2014 case Hexagon::S2_storerh_io:
2015 MI->setDesc(HII.get(Hexagon::S4_storeirh_io));
2016 break;
2017 case Hexagon::S2_storeri_io:
2018 MI->setDesc(HII.get(Hexagon::S4_storeiri_io));
2019 break;
2020 }
2021 MI->addOperand(MachineOperand::CreateImm(V));
2022 return true;
2023}
2024
2025// If MI is equivalent o S2_packhl, generate the S2_packhl. MI could be the
2026// last instruction in a sequence that results in something equivalent to
2027// the pack-halfwords. The intent is to cause the entire sequence to become
2028// dead.
2029bool BitSimplification::genPackhl(MachineInstr *MI,
2030 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) {
2031 unsigned Opc = MI->getOpcode();
2032 if (Opc == Hexagon::S2_packhl)
2033 return false;
2034 BitTracker::RegisterRef Rs, Rt;
2035 if (!matchPackhl(RD.Reg, RC, Rs, Rt))
2036 return false;
2037 if (!validateReg(Rs, Hexagon::S2_packhl, 1) ||
2038 !validateReg(Rt, Hexagon::S2_packhl, 2))
2039 return false;
2040
2041 MachineBasicBlock &B = *MI->getParent();
2042 Register NewR = MRI.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
2043 DebugLoc DL = MI->getDebugLoc();
2044 auto At = MI->isPHI() ? B.getFirstNonPHI()
2046 BuildMI(B, At, DL, HII.get(Hexagon::S2_packhl), NewR)
2047 .addReg(Rs.Reg, {}, Rs.Sub)
2048 .addReg(Rt.Reg, {}, Rt.Sub);
2049 HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI);
2050 BT.put(BitTracker::RegisterRef(NewR), RC);
2051 return true;
2052}
2053
2054// If MI produces halfword of the input in the low half of the output,
2055// replace it with zero-extend or extractu.
2056bool BitSimplification::genExtractHalf(MachineInstr *MI,
2057 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) {
2058 RegHalf L;
2059 // Check for halfword in low 16 bits, zeros elsewhere.
2060 if (!matchHalf(RD.Reg, RC, 0, L) || !HBS::isZero(RC, 16, 16))
2061 return false;
2062
2063 unsigned Opc = MI->getOpcode();
2064 MachineBasicBlock &B = *MI->getParent();
2065 DebugLoc DL = MI->getDebugLoc();
2066
2067 // Prefer zxth, since zxth can go in any slot, while extractu only in
2068 // slots 2 and 3.
2069 unsigned NewR = 0;
2070 auto At = MI->isPHI() ? B.getFirstNonPHI()
2072 if (L.Low && Opc != Hexagon::A2_zxth) {
2073 if (validateReg(L, Hexagon::A2_zxth, 1)) {
2074 NewR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2075 BuildMI(B, At, DL, HII.get(Hexagon::A2_zxth), NewR)
2076 .addReg(L.Reg, {}, L.Sub);
2077 }
2078 } else if (!L.Low && Opc != Hexagon::S2_lsr_i_r) {
2079 if (validateReg(L, Hexagon::S2_lsr_i_r, 1)) {
2080 NewR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2081 BuildMI(B, MI, DL, HII.get(Hexagon::S2_lsr_i_r), NewR)
2082 .addReg(L.Reg, {}, L.Sub)
2083 .addImm(16);
2084 }
2085 }
2086 if (NewR == 0)
2087 return false;
2088 HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI);
2089 BT.put(BitTracker::RegisterRef(NewR), RC);
2090 return true;
2091}
2092
2093// If MI is equivalent to a combine(.L/.H, .L/.H) replace with with the
2094// combine.
2095bool BitSimplification::genCombineHalf(MachineInstr *MI,
2096 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) {
2097 RegHalf L, H;
2098 // Check for combine h/l
2099 if (!matchHalf(RD.Reg, RC, 0, L) || !matchHalf(RD.Reg, RC, 16, H))
2100 return false;
2101 // Do nothing if this is just a reg copy.
2102 if (L.Reg == H.Reg && L.Sub == H.Sub && !H.Low && L.Low)
2103 return false;
2104
2105 unsigned Opc = MI->getOpcode();
2106 unsigned COpc = getCombineOpcode(H.Low, L.Low);
2107 if (COpc == Opc)
2108 return false;
2109 if (!validateReg(H, COpc, 1) || !validateReg(L, COpc, 2))
2110 return false;
2111
2112 MachineBasicBlock &B = *MI->getParent();
2113 DebugLoc DL = MI->getDebugLoc();
2114 Register NewR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2115 auto At = MI->isPHI() ? B.getFirstNonPHI()
2117 BuildMI(B, At, DL, HII.get(COpc), NewR)
2118 .addReg(H.Reg, {}, H.Sub)
2119 .addReg(L.Reg, {}, L.Sub);
2120 HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI);
2121 BT.put(BitTracker::RegisterRef(NewR), RC);
2122 return true;
2123}
2124
2125// If MI resets high bits of a register and keeps the lower ones, replace it
2126// with zero-extend byte/half, and-immediate, or extractu, as appropriate.
2127bool BitSimplification::genExtractLow(MachineInstr *MI,
2128 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) {
2129 unsigned Opc = MI->getOpcode();
2130 switch (Opc) {
2131 case Hexagon::A2_zxtb:
2132 case Hexagon::A2_zxth:
2133 case Hexagon::S2_extractu:
2134 return false;
2135 }
2136 if (Opc == Hexagon::A2_andir && MI->getOperand(2).isImm()) {
2137 int32_t Imm = MI->getOperand(2).getImm();
2138 if (isInt<10>(Imm))
2139 return false;
2140 }
2141
2142 if (MI->hasUnmodeledSideEffects() || MI->isInlineAsm())
2143 return false;
2144 unsigned W = RC.width();
2145 while (W > 0 && RC[W-1].is(0))
2146 W--;
2147 if (W == 0 || W == RC.width())
2148 return false;
2149 unsigned NewOpc = (W == 8) ? Hexagon::A2_zxtb
2150 : (W == 16) ? Hexagon::A2_zxth
2151 : (W < 10) ? Hexagon::A2_andir
2152 : Hexagon::S2_extractu;
2153 MachineBasicBlock &B = *MI->getParent();
2154 DebugLoc DL = MI->getDebugLoc();
2155
2156 for (auto &Op : MI->uses()) {
2157 if (!Op.isReg())
2158 continue;
2159 BitTracker::RegisterRef RS = Op;
2160 if (!BT.has(RS.Reg))
2161 continue;
2162 const BitTracker::RegisterCell &SC = BT.lookup(RS.Reg);
2163 unsigned BN, BW;
2164 if (!HBS::getSubregMask(RS, BN, BW, MRI))
2165 continue;
2166 if (BW < W || !HBS::isEqual(RC, 0, SC, BN, W))
2167 continue;
2168 if (!validateReg(RS, NewOpc, 1))
2169 continue;
2170
2171 Register NewR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2172 auto At = MI->isPHI() ? B.getFirstNonPHI()
2173 : MachineBasicBlock::iterator(MI);
2174 auto MIB =
2175 BuildMI(B, At, DL, HII.get(NewOpc), NewR).addReg(RS.Reg, {}, RS.Sub);
2176 if (NewOpc == Hexagon::A2_andir)
2177 MIB.addImm((1 << W) - 1);
2178 else if (NewOpc == Hexagon::S2_extractu)
2179 MIB.addImm(W).addImm(0);
2180 HBS::replaceSubWithSub(RD.Reg, RD.Sub, NewR, 0, MRI);
2181 BT.put(BitTracker::RegisterRef(NewR), RC);
2182 return true;
2183 }
2184 return false;
2185}
2186
2187bool BitSimplification::genBitSplit(MachineInstr *MI,
2188 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC,
2189 const RegisterSet &AVs) {
2190 if (!GenBitSplit)
2191 return false;
2192 if (MaxBitSplit.getNumOccurrences()) {
2194 return false;
2195 }
2196
2197 unsigned Opc = MI->getOpcode();
2198 switch (Opc) {
2199 case Hexagon::A4_bitsplit:
2200 case Hexagon::A4_bitspliti:
2201 return false;
2202 }
2203
2204 unsigned W = RC.width();
2205 if (W != 32)
2206 return false;
2207
2208 auto ctlz = [] (const BitTracker::RegisterCell &C) -> unsigned {
2209 unsigned Z = C.width();
2210 while (Z > 0 && C[Z-1].is(0))
2211 --Z;
2212 return C.width() - Z;
2213 };
2214
2215 // Count the number of leading zeros in the target RC.
2216 unsigned Z = ctlz(RC);
2217 if (Z == 0 || Z == W)
2218 return false;
2219
2220 // A simplistic analysis: assume the source register (the one being split)
2221 // is fully unknown, and that all its bits are self-references.
2222 const BitTracker::BitValue &B0 = RC[0];
2223 if (B0.Type != BitTracker::BitValue::Ref)
2224 return false;
2225
2226 unsigned SrcR = B0.RefI.Reg;
2227 unsigned SrcSR = 0;
2228 unsigned Pos = B0.RefI.Pos;
2229
2230 // All the non-zero bits should be consecutive bits from the same register.
2231 for (unsigned i = 1; i < W-Z; ++i) {
2232 const BitTracker::BitValue &V = RC[i];
2233 if (V.Type != BitTracker::BitValue::Ref)
2234 return false;
2235 if (V.RefI.Reg != SrcR || V.RefI.Pos != Pos+i)
2236 return false;
2237 }
2238
2239 // Now, find the other bitfield among AVs.
2240 for (unsigned S = AVs.find_first(); S; S = AVs.find_next(S)) {
2241 // The number of leading zeros here should be the number of trailing
2242 // non-zeros in RC.
2243 unsigned SRC = MRI.getRegClass(S)->getID();
2244 if (SRC != Hexagon::IntRegsRegClassID &&
2245 SRC != Hexagon::DoubleRegsRegClassID)
2246 continue;
2247 if (!BT.has(S))
2248 continue;
2249 const BitTracker::RegisterCell &SC = BT.lookup(S);
2250 if (SC.width() != W || ctlz(SC) != W-Z)
2251 continue;
2252 // The Z lower bits should now match SrcR.
2253 const BitTracker::BitValue &S0 = SC[0];
2254 if (S0.Type != BitTracker::BitValue::Ref || S0.RefI.Reg != SrcR)
2255 continue;
2256 unsigned P = S0.RefI.Pos;
2257
2258 if (Pos <= P && (Pos + W-Z) != P)
2259 continue;
2260 if (P < Pos && (P + Z) != Pos)
2261 continue;
2262 // The starting bitfield position must be at a subregister boundary.
2263 if (std::min(P, Pos) != 0 && std::min(P, Pos) != 32)
2264 continue;
2265
2266 unsigned I;
2267 for (I = 1; I < Z; ++I) {
2268 const BitTracker::BitValue &V = SC[I];
2269 if (V.Type != BitTracker::BitValue::Ref)
2270 break;
2271 if (V.RefI.Reg != SrcR || V.RefI.Pos != P+I)
2272 break;
2273 }
2274 if (I != Z)
2275 continue;
2276
2277 // Generate bitsplit where S is defined.
2278 if (MaxBitSplit.getNumOccurrences())
2279 CountBitSplit++;
2280 MachineInstr *DefS = MRI.getVRegDef(S);
2281 assert(DefS != nullptr);
2282 DebugLoc DL = DefS->getDebugLoc();
2283 MachineBasicBlock &B = *DefS->getParent();
2284 auto At = DefS->isPHI() ? B.getFirstNonPHI()
2286 if (MRI.getRegClass(SrcR)->getID() == Hexagon::DoubleRegsRegClassID)
2287 SrcSR = (std::min(Pos, P) == 32) ? Hexagon::isub_hi : Hexagon::isub_lo;
2288 if (!validateReg({SrcR,SrcSR}, Hexagon::A4_bitspliti, 1))
2289 continue;
2290 unsigned ImmOp = Pos <= P ? W-Z : Z;
2291
2292 // Find an existing bitsplit instruction if one already exists.
2293 unsigned NewR = 0;
2294 for (MachineInstr *In : NewMIs) {
2295 if (In->getOpcode() != Hexagon::A4_bitspliti)
2296 continue;
2297 MachineOperand &Op1 = In->getOperand(1);
2298 if (Op1.getReg() != SrcR || Op1.getSubReg() != SrcSR)
2299 continue;
2300 if (In->getOperand(2).getImm() != ImmOp)
2301 continue;
2302 // Check if the target register is available here.
2303 MachineOperand &Op0 = In->getOperand(0);
2304 MachineInstr *DefI = MRI.getVRegDef(Op0.getReg());
2305 assert(DefI != nullptr);
2306 if (!MDT.dominates(DefI, &*At))
2307 continue;
2308
2309 // Found one that can be reused.
2310 assert(Op0.getSubReg() == 0);
2311 NewR = Op0.getReg();
2312 break;
2313 }
2314 if (!NewR) {
2315 NewR = MRI.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
2316 auto NewBS = BuildMI(B, At, DL, HII.get(Hexagon::A4_bitspliti), NewR)
2317 .addReg(SrcR, {}, SrcSR)
2318 .addImm(ImmOp);
2319 NewMIs.push_back(NewBS);
2320 }
2321 if (Pos <= P) {
2322 HBS::replaceRegWithSub(RD.Reg, NewR, Hexagon::isub_lo, MRI);
2323 HBS::replaceRegWithSub(S, NewR, Hexagon::isub_hi, MRI);
2324 } else {
2325 HBS::replaceRegWithSub(S, NewR, Hexagon::isub_lo, MRI);
2326 HBS::replaceRegWithSub(RD.Reg, NewR, Hexagon::isub_hi, MRI);
2327 }
2328 return true;
2329 }
2330
2331 return false;
2332}
2333
2334// Check for tstbit simplification opportunity, where the bit being checked
2335// can be tracked back to another register. For example:
2336// %2 = S2_lsr_i_r %1, 5
2337// %3 = S2_tstbit_i %2, 0
2338// =>
2339// %3 = S2_tstbit_i %1, 5
2340bool BitSimplification::simplifyTstbit(MachineInstr *MI,
2341 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC) {
2342 unsigned Opc = MI->getOpcode();
2343 if (Opc != Hexagon::S2_tstbit_i)
2344 return false;
2345
2346 unsigned BN = MI->getOperand(2).getImm();
2347 BitTracker::RegisterRef RS = MI->getOperand(1);
2348 unsigned F, W;
2349 DebugLoc DL = MI->getDebugLoc();
2350 if (!BT.has(RS.Reg) || !HBS::getSubregMask(RS, F, W, MRI))
2351 return false;
2352 MachineBasicBlock &B = *MI->getParent();
2353 auto At = MI->isPHI() ? B.getFirstNonPHI()
2355
2356 const BitTracker::RegisterCell &SC = BT.lookup(RS.Reg);
2357 const BitTracker::BitValue &V = SC[F+BN];
2358 if (V.Type == BitTracker::BitValue::Ref && V.RefI.Reg != RS.Reg) {
2359 const TargetRegisterClass *TC = MRI.getRegClass(V.RefI.Reg);
2360 // Need to map V.RefI.Reg to a 32-bit register, i.e. if it is
2361 // a double register, need to use a subregister and adjust bit
2362 // number.
2363 unsigned P = std::numeric_limits<unsigned>::max();
2364 BitTracker::RegisterRef RR(V.RefI.Reg, 0);
2365 if (TC == &Hexagon::DoubleRegsRegClass) {
2366 P = V.RefI.Pos;
2367 RR.Sub = Hexagon::isub_lo;
2368 if (P >= 32) {
2369 P -= 32;
2370 RR.Sub = Hexagon::isub_hi;
2371 }
2372 } else if (TC == &Hexagon::IntRegsRegClass) {
2373 P = V.RefI.Pos;
2374 }
2375 if (P != std::numeric_limits<unsigned>::max()) {
2376 Register NewR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
2377 BuildMI(B, At, DL, HII.get(Hexagon::S2_tstbit_i), NewR)
2378 .addReg(RR.Reg, {}, RR.Sub)
2379 .addImm(P);
2380 HBS::replaceReg(RD.Reg, NewR, MRI);
2381 BT.put(NewR, RC);
2382 return true;
2383 }
2384 } else if (V.is(0) || V.is(1)) {
2385 Register NewR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
2386 unsigned NewOpc = V.is(0) ? Hexagon::PS_false : Hexagon::PS_true;
2387 BuildMI(B, At, DL, HII.get(NewOpc), NewR);
2388 HBS::replaceReg(RD.Reg, NewR, MRI);
2389 return true;
2390 }
2391
2392 return false;
2393}
2394
2395// Detect whether RD is a bitfield extract (sign- or zero-extended) of
2396// some register from the AVs set. Create a new corresponding instruction
2397// at the location of MI. The intent is to recognize situations where
2398// a sequence of instructions performs an operation that is equivalent to
2399// an extract operation, such as a shift left followed by a shift right.
2400bool BitSimplification::simplifyExtractLow(MachineInstr *MI,
2401 BitTracker::RegisterRef RD, const BitTracker::RegisterCell &RC,
2402 const RegisterSet &AVs) {
2403 if (!GenExtract)
2404 return false;
2405 if (MaxExtract.getNumOccurrences()) {
2406 if (CountExtract >= MaxExtract)
2407 return false;
2408 CountExtract++;
2409 }
2410
2411 unsigned W = RC.width();
2412 unsigned RW = W;
2413 unsigned Len;
2414 bool Signed;
2415
2416 // The code is mostly class-independent, except for the part that generates
2417 // the extract instruction, and establishes the source register (in case it
2418 // needs to use a subregister).
2419 const TargetRegisterClass *FRC = HBS::getFinalVRegClass(RD, MRI);
2420 if (FRC != &Hexagon::IntRegsRegClass && FRC != &Hexagon::DoubleRegsRegClass)
2421 return false;
2422 assert(RD.Sub == 0);
2423
2424 // Observation:
2425 // If the cell has a form of 00..0xx..x with k zeros and n remaining
2426 // bits, this could be an extractu of the n bits, but it could also be
2427 // an extractu of a longer field which happens to have 0s in the top
2428 // bit positions.
2429 // The same logic applies to sign-extended fields.
2430 //
2431 // Do not check for the extended extracts, since it would expand the
2432 // search space quite a bit. The search may be expensive as it is.
2433
2434 const BitTracker::BitValue &TopV = RC[W-1];
2435
2436 // Eliminate candidates that have self-referential bits, since they
2437 // cannot be extracts from other registers. Also, skip registers that
2438 // have compile-time constant values.
2439 bool IsConst = true;
2440 for (unsigned I = 0; I != W; ++I) {
2441 const BitTracker::BitValue &V = RC[I];
2442 if (V.Type == BitTracker::BitValue::Ref && V.RefI.Reg == RD.Reg)
2443 return false;
2444 IsConst = IsConst && (V.is(0) || V.is(1));
2445 }
2446 if (IsConst)
2447 return false;
2448
2449 if (TopV.is(0) || TopV.is(1)) {
2450 bool S = TopV.is(1);
2451 for (--W; W > 0 && RC[W-1].is(S); --W)
2452 ;
2453 Len = W;
2454 Signed = S;
2455 // The sign bit must be a part of the field being extended.
2456 if (Signed)
2457 ++Len;
2458 } else {
2459 // This could still be a sign-extended extract.
2461 if (TopV.RefI.Reg == RD.Reg || TopV.RefI.Pos == W-1)
2462 return false;
2463 for (--W; W > 0 && RC[W-1] == TopV; --W)
2464 ;
2465 // The top bits of RC are copies of TopV. One occurrence of TopV will
2466 // be a part of the field.
2467 Len = W + 1;
2468 Signed = true;
2469 }
2470
2471 // This would be just a copy. It should be handled elsewhere.
2472 if (Len == RW)
2473 return false;
2474
2475 LLVM_DEBUG({
2476 dbgs() << __func__ << " on reg: " << printReg(RD.Reg, &HRI, RD.Sub)
2477 << ", MI: " << *MI;
2478 dbgs() << "Cell: " << RC << '\n';
2479 dbgs() << "Expected bitfield size: " << Len << " bits, "
2480 << (Signed ? "sign" : "zero") << "-extended\n";
2481 });
2482
2483 bool Changed = false;
2484
2485 for (unsigned R = AVs.find_first(); R != 0; R = AVs.find_next(R)) {
2486 if (!BT.has(R))
2487 continue;
2488 const BitTracker::RegisterCell &SC = BT.lookup(R);
2489 unsigned SW = SC.width();
2490
2491 // The source can be longer than the destination, as long as its size is
2492 // a multiple of the size of the destination. Also, we would need to be
2493 // able to refer to the subregister in the source that would be of the
2494 // same size as the destination, but only check the sizes here.
2495 if (SW < RW || (SW % RW) != 0)
2496 continue;
2497
2498 // The field can start at any offset in SC as long as it contains Len
2499 // bits and does not cross subregister boundary (if the source register
2500 // is longer than the destination).
2501 unsigned Off = 0;
2502 while (Off <= SW-Len) {
2503 unsigned OE = (Off+Len)/RW;
2504 if (OE != Off/RW) {
2505 // The assumption here is that if the source (R) is longer than the
2506 // destination, then the destination is a sequence of words of
2507 // size RW, and each such word in R can be accessed via a subregister.
2508 //
2509 // If the beginning and the end of the field cross the subregister
2510 // boundary, advance to the next subregister.
2511 Off = OE*RW;
2512 continue;
2513 }
2514 if (HBS::isEqual(RC, 0, SC, Off, Len))
2515 break;
2516 ++Off;
2517 }
2518
2519 if (Off > SW-Len)
2520 continue;
2521
2522 // Found match.
2523 unsigned ExtOpc = 0;
2524 if (Off == 0) {
2525 if (Len == 8)
2526 ExtOpc = Signed ? Hexagon::A2_sxtb : Hexagon::A2_zxtb;
2527 else if (Len == 16)
2528 ExtOpc = Signed ? Hexagon::A2_sxth : Hexagon::A2_zxth;
2529 else if (Len < 10 && !Signed)
2530 ExtOpc = Hexagon::A2_andir;
2531 }
2532 if (ExtOpc == 0) {
2533 ExtOpc =
2534 Signed ? (RW == 32 ? Hexagon::S4_extract : Hexagon::S4_extractp)
2535 : (RW == 32 ? Hexagon::S2_extractu : Hexagon::S2_extractup);
2536 }
2537 unsigned SR = 0;
2538 // This only recognizes isub_lo and isub_hi.
2539 if (RW != SW && RW*2 != SW)
2540 continue;
2541 if (RW != SW)
2542 SR = (Off/RW == 0) ? Hexagon::isub_lo : Hexagon::isub_hi;
2543 Off = Off % RW;
2544
2545 if (!validateReg({R,SR}, ExtOpc, 1))
2546 continue;
2547
2548 // Don't generate the same instruction as the one being optimized.
2549 if (MI->getOpcode() == ExtOpc) {
2550 // All possible ExtOpc's have the source in operand(1).
2551 const MachineOperand &SrcOp = MI->getOperand(1);
2552 if (SrcOp.getReg() == R)
2553 continue;
2554 }
2555
2556 DebugLoc DL = MI->getDebugLoc();
2557 MachineBasicBlock &B = *MI->getParent();
2559 auto At = MI->isPHI() ? B.getFirstNonPHI()
2561 auto MIB = BuildMI(B, At, DL, HII.get(ExtOpc), NewR).addReg(R, {}, SR);
2562 switch (ExtOpc) {
2563 case Hexagon::A2_sxtb:
2564 case Hexagon::A2_zxtb:
2565 case Hexagon::A2_sxth:
2566 case Hexagon::A2_zxth:
2567 break;
2568 case Hexagon::A2_andir:
2569 MIB.addImm((1u << Len) - 1);
2570 break;
2571 case Hexagon::S4_extract:
2572 case Hexagon::S2_extractu:
2573 case Hexagon::S4_extractp:
2574 case Hexagon::S2_extractup:
2575 MIB.addImm(Len)
2576 .addImm(Off);
2577 break;
2578 default:
2579 llvm_unreachable("Unexpected opcode");
2580 }
2581
2582 HBS::replaceReg(RD.Reg, NewR, MRI);
2583 BT.put(BitTracker::RegisterRef(NewR), RC);
2584 Changed = true;
2585 break;
2586 }
2587
2588 return Changed;
2589}
2590
2591bool BitSimplification::simplifyRCmp0(MachineInstr *MI,
2592 BitTracker::RegisterRef RD) {
2593 unsigned Opc = MI->getOpcode();
2594 if (Opc != Hexagon::A4_rcmpeqi && Opc != Hexagon::A4_rcmpneqi)
2595 return false;
2596 MachineOperand &CmpOp = MI->getOperand(2);
2597 if (!CmpOp.isImm() || CmpOp.getImm() != 0)
2598 return false;
2599
2600 const TargetRegisterClass *FRC = HBS::getFinalVRegClass(RD, MRI);
2601 if (FRC != &Hexagon::IntRegsRegClass && FRC != &Hexagon::DoubleRegsRegClass)
2602 return false;
2603 assert(RD.Sub == 0);
2604
2605 MachineBasicBlock &B = *MI->getParent();
2606 const DebugLoc &DL = MI->getDebugLoc();
2607 auto At = MI->isPHI() ? B.getFirstNonPHI()
2609 bool KnownZ = true;
2610 bool KnownNZ = false;
2611
2612 BitTracker::RegisterRef SR = MI->getOperand(1);
2613 if (!BT.has(SR.Reg))
2614 return false;
2615 const BitTracker::RegisterCell &SC = BT.lookup(SR.Reg);
2616 unsigned F, W;
2617 if (!HBS::getSubregMask(SR, F, W, MRI))
2618 return false;
2619
2620 for (uint16_t I = F; I != F+W; ++I) {
2621 const BitTracker::BitValue &V = SC[I];
2622 if (!V.is(0))
2623 KnownZ = false;
2624 if (V.is(1))
2625 KnownNZ = true;
2626 }
2627
2628 auto ReplaceWithConst = [&](int C) {
2630 BuildMI(B, At, DL, HII.get(Hexagon::A2_tfrsi), NewR)
2631 .addImm(C);
2632 HBS::replaceReg(RD.Reg, NewR, MRI);
2633 BitTracker::RegisterCell NewRC(W);
2634 for (uint16_t I = 0; I != W; ++I) {
2635 NewRC[I] = BitTracker::BitValue(C & 1);
2636 C = unsigned(C) >> 1;
2637 }
2638 BT.put(BitTracker::RegisterRef(NewR), NewRC);
2639 return true;
2640 };
2641
2642 auto IsNonZero = [] (const MachineOperand &Op) {
2643 if (Op.isGlobal() || Op.isBlockAddress())
2644 return true;
2645 if (Op.isImm())
2646 return Op.getImm() != 0;
2647 if (Op.isCImm())
2648 return !Op.getCImm()->isZero();
2649 if (Op.isFPImm())
2650 return !Op.getFPImm()->isZero();
2651 return false;
2652 };
2653
2654 auto IsZero = [] (const MachineOperand &Op) {
2655 if (Op.isGlobal() || Op.isBlockAddress())
2656 return false;
2657 if (Op.isImm())
2658 return Op.getImm() == 0;
2659 if (Op.isCImm())
2660 return Op.getCImm()->isZero();
2661 if (Op.isFPImm())
2662 return Op.getFPImm()->isZero();
2663 return false;
2664 };
2665
2666 // If the source register is known to be 0 or non-0, the comparison can
2667 // be folded to a load of a constant.
2668 if (KnownZ || KnownNZ) {
2669 assert(KnownZ != KnownNZ && "Register cannot be both 0 and non-0");
2670 return ReplaceWithConst(KnownZ == (Opc == Hexagon::A4_rcmpeqi));
2671 }
2672
2673 // Special case: if the compare comes from a C2_muxii, then we know the
2674 // two possible constants that can be the source value.
2675 MachineInstr *InpDef = MRI.getVRegDef(SR.Reg);
2676 if (!InpDef)
2677 return false;
2678 if (SR.Sub == 0 && InpDef->getOpcode() == Hexagon::C2_muxii) {
2679 MachineOperand &Src1 = InpDef->getOperand(2);
2680 MachineOperand &Src2 = InpDef->getOperand(3);
2681 // Check if both are non-zero.
2682 bool KnownNZ1 = IsNonZero(Src1), KnownNZ2 = IsNonZero(Src2);
2683 if (KnownNZ1 && KnownNZ2)
2684 return ReplaceWithConst(Opc == Hexagon::A4_rcmpneqi);
2685 // Check if both are zero.
2686 bool KnownZ1 = IsZero(Src1), KnownZ2 = IsZero(Src2);
2687 if (KnownZ1 && KnownZ2)
2688 return ReplaceWithConst(Opc == Hexagon::A4_rcmpeqi);
2689
2690 // If for both operands we know that they are either 0 or non-0,
2691 // replace the comparison with a C2_muxii, using the same predicate
2692 // register, but with operands substituted with 0/1 accordingly.
2693 if ((KnownZ1 || KnownNZ1) && (KnownZ2 || KnownNZ2)) {
2695 BuildMI(B, At, DL, HII.get(Hexagon::C2_muxii), NewR)
2696 .addReg(InpDef->getOperand(1).getReg())
2697 .addImm(KnownZ1 == (Opc == Hexagon::A4_rcmpeqi))
2698 .addImm(KnownZ2 == (Opc == Hexagon::A4_rcmpeqi));
2699 HBS::replaceReg(RD.Reg, NewR, MRI);
2700 // Create a new cell with only the least significant bit unknown.
2701 BitTracker::RegisterCell NewRC(W);
2702 NewRC[0] = BitTracker::BitValue::self();
2703 NewRC.fill(1, W, BitTracker::BitValue::Zero);
2704 BT.put(BitTracker::RegisterRef(NewR), NewRC);
2705 return true;
2706 }
2707 }
2708
2709 return false;
2710}
2711
2712bool BitSimplification::processBlock(MachineBasicBlock &B,
2713 const RegisterSet &AVs) {
2714 if (!BT.reached(&B))
2715 return false;
2716 bool Changed = false;
2717 RegisterSet AVB = AVs;
2718 RegisterSet Defs;
2719
2720 for (auto I = B.begin(), E = B.end(); I != E; ++I, AVB.insert(Defs)) {
2721 MachineInstr *MI = &*I;
2722 Defs.clear();
2723 HBS::getInstrDefs(*MI, Defs);
2724
2725 unsigned Opc = MI->getOpcode();
2726 if (Opc == TargetOpcode::COPY || Opc == TargetOpcode::REG_SEQUENCE)
2727 continue;
2728
2729 if (MI->mayStore()) {
2730 bool T = genStoreUpperHalf(MI);
2731 T = T || genStoreImmediate(MI);
2732 Changed |= T;
2733 continue;
2734 }
2735
2736 if (Defs.count() != 1)
2737 continue;
2738 const MachineOperand &Op0 = MI->getOperand(0);
2739 if (!Op0.isReg() || !Op0.isDef())
2740 continue;
2741 BitTracker::RegisterRef RD = Op0;
2742 if (!BT.has(RD.Reg))
2743 continue;
2744 const TargetRegisterClass *FRC = HBS::getFinalVRegClass(RD, MRI);
2745 const BitTracker::RegisterCell &RC = BT.lookup(RD.Reg);
2746
2747 if (FRC->getID() == Hexagon::DoubleRegsRegClassID) {
2748 bool T = genPackhl(MI, RD, RC);
2749 T = T || simplifyExtractLow(MI, RD, RC, AVB);
2750 Changed |= T;
2751 continue;
2752 }
2753
2754 if (FRC->getID() == Hexagon::IntRegsRegClassID) {
2755 bool T = genBitSplit(MI, RD, RC, AVB);
2756 T = T || simplifyExtractLow(MI, RD, RC, AVB);
2757 T = T || genExtractHalf(MI, RD, RC);
2758 T = T || genCombineHalf(MI, RD, RC);
2759 T = T || genExtractLow(MI, RD, RC);
2760 T = T || simplifyRCmp0(MI, RD);
2761 Changed |= T;
2762 continue;
2763 }
2764
2765 if (FRC->getID() == Hexagon::PredRegsRegClassID) {
2766 bool T = simplifyTstbit(MI, RD, RC);
2767 Changed |= T;
2768 continue;
2769 }
2770 }
2771 return Changed;
2772}
2773
2774bool HexagonBitSimplify::runOnMachineFunction(MachineFunction &MF) {
2775 if (skipFunction(MF.getFunction()))
2776 return false;
2777
2778 auto &HST = MF.getSubtarget<HexagonSubtarget>();
2779 auto &HRI = *HST.getRegisterInfo();
2780 auto &HII = *HST.getInstrInfo();
2781
2782 MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
2783 MachineRegisterInfo &MRI = MF.getRegInfo();
2784 bool Changed;
2785
2786 Changed = DeadCodeElimination(MF, *MDT).run();
2787
2788 const HexagonEvaluator HE(HRI, MRI, HII, MF);
2789 BitTracker BT(HE, MF);
2790 LLVM_DEBUG(BT.trace(true));
2791 BT.run();
2792
2793 MachineBasicBlock &Entry = MF.front();
2794
2795 RegisterSet AIG; // Available registers for IG.
2796 ConstGeneration ImmG(BT, HII, MRI);
2797 Changed |= visitBlock(Entry, ImmG, AIG);
2798
2799 RegisterSet ARE; // Available registers for RIE.
2800 RedundantInstrElimination RIE(BT, HII, HRI, MRI);
2801 bool Ried = visitBlock(Entry, RIE, ARE);
2802 if (Ried) {
2803 Changed = true;
2804 BT.run();
2805 }
2806
2807 RegisterSet ACG; // Available registers for CG.
2808 CopyGeneration CopyG(BT, HII, HRI, MRI);
2809 Changed |= visitBlock(Entry, CopyG, ACG);
2810
2811 RegisterSet ACP; // Available registers for CP.
2812 CopyPropagation CopyP(HRI, MRI);
2813 Changed |= visitBlock(Entry, CopyP, ACP);
2814
2815 Changed = DeadCodeElimination(MF, *MDT).run() || Changed;
2816
2817 BT.run();
2818 RegisterSet ABS; // Available registers for BS.
2819 BitSimplification BitS(BT, *MDT, HII, HRI, MRI, MF);
2820 Changed |= visitBlock(Entry, BitS, ABS);
2821
2822 Changed = DeadCodeElimination(MF, *MDT).run() || Changed;
2823
2824 if (Changed) {
2825 for (auto &B : MF)
2826 for (auto &I : B)
2827 I.clearKillInfo();
2828 DeadCodeElimination(MF, *MDT).run();
2829 }
2830 return Changed;
2831}
2832
2833// Recognize loops where the code at the end of the loop matches the code
2834// before the entry of the loop, and the matching code is such that is can
2835// be simplified. This pass relies on the bit simplification above and only
2836// prepares code in a way that can be handled by the bit simplification.
2837//
2838// This is the motivating testcase (and explanation):
2839//
2840// {
2841// loop0(.LBB0_2, r1) // %for.body.preheader
2842// r5:4 = memd(r0++#8)
2843// }
2844// {
2845// r3 = lsr(r4, #16)
2846// r7:6 = combine(r5, r5)
2847// }
2848// {
2849// r3 = insert(r5, #16, #16)
2850// r7:6 = vlsrw(r7:6, #16)
2851// }
2852// .LBB0_2:
2853// {
2854// memh(r2+#4) = r5
2855// memh(r2+#6) = r6 # R6 is really R5.H
2856// }
2857// {
2858// r2 = add(r2, #8)
2859// memh(r2+#0) = r4
2860// memh(r2+#2) = r3 # R3 is really R4.H
2861// }
2862// {
2863// r5:4 = memd(r0++#8)
2864// }
2865// { # "Shuffling" code that sets up R3 and R6
2866// r3 = lsr(r4, #16) # so that their halves can be stored in the
2867// r7:6 = combine(r5, r5) # next iteration. This could be folded into
2868// } # the stores if the code was at the beginning
2869// { # of the loop iteration. Since the same code
2870// r3 = insert(r5, #16, #16) # precedes the loop, it can actually be moved
2871// r7:6 = vlsrw(r7:6, #16) # there.
2872// }:endloop0
2873//
2874//
2875// The outcome:
2876//
2877// {
2878// loop0(.LBB0_2, r1)
2879// r5:4 = memd(r0++#8)
2880// }
2881// .LBB0_2:
2882// {
2883// memh(r2+#4) = r5
2884// memh(r2+#6) = r5.h
2885// }
2886// {
2887// r2 = add(r2, #8)
2888// memh(r2+#0) = r4
2889// memh(r2+#2) = r4.h
2890// }
2891// {
2892// r5:4 = memd(r0++#8)
2893// }:endloop0
2894
2895namespace {
2896
2897 class HexagonLoopRescheduling : public MachineFunctionPass {
2898 public:
2899 static char ID;
2900
2901 HexagonLoopRescheduling() : MachineFunctionPass(ID) {}
2902
2903 bool runOnMachineFunction(MachineFunction &MF) override;
2904
2905 private:
2906 const HexagonInstrInfo *HII = nullptr;
2907 const HexagonRegisterInfo *HRI = nullptr;
2908 MachineRegisterInfo *MRI = nullptr;
2909 BitTracker *BTP = nullptr;
2910
2911 struct LoopCand {
2912 LoopCand(MachineBasicBlock *lb, MachineBasicBlock *pb,
2913 MachineBasicBlock *eb) : LB(lb), PB(pb), EB(eb) {}
2914
2915 MachineBasicBlock *LB, *PB, *EB;
2916 };
2917 using InstrList = std::vector<MachineInstr *>;
2918 struct InstrGroup {
2919 BitTracker::RegisterRef Inp, Out;
2920 InstrList Ins;
2921 };
2922 struct PhiInfo {
2923 PhiInfo(MachineInstr &P, MachineBasicBlock &B);
2924
2925 unsigned DefR;
2926 BitTracker::RegisterRef LR, PR; // Loop Register, Preheader Register
2927 MachineBasicBlock *LB, *PB; // Loop Block, Preheader Block
2928 };
2929
2930 static unsigned getDefReg(const MachineInstr *MI);
2931 bool isConst(unsigned Reg) const;
2932 bool isBitShuffle(const MachineInstr *MI, unsigned DefR) const;
2933 bool isStoreInput(const MachineInstr *MI, unsigned DefR) const;
2934 bool isShuffleOf(unsigned OutR, unsigned InpR) const;
2935 bool isSameShuffle(unsigned OutR1, unsigned InpR1, unsigned OutR2,
2936 unsigned &InpR2) const;
2937 void moveGroup(InstrGroup &G, MachineBasicBlock &LB, MachineBasicBlock &PB,
2938 MachineBasicBlock::iterator At, unsigned OldPhiR, unsigned NewPredR);
2939 bool processLoop(LoopCand &C);
2940 };
2941
2942} // end anonymous namespace
2943
2944char HexagonLoopRescheduling::ID = 0;
2945
2946INITIALIZE_PASS(HexagonLoopRescheduling, "hexagon-loop-resched-pass",
2947 "Hexagon Loop Rescheduling", false, false)
2948
2949HexagonLoopRescheduling::PhiInfo::PhiInfo(MachineInstr &P,
2951 DefR = HexagonLoopRescheduling::getDefReg(&P);
2952 LB = &B;
2953 PB = nullptr;
2954 for (unsigned i = 1, n = P.getNumOperands(); i < n; i += 2) {
2955 const MachineOperand &OpB = P.getOperand(i+1);
2956 if (OpB.getMBB() == &B) {
2957 LR = P.getOperand(i);
2958 continue;
2959 }
2960 PB = OpB.getMBB();
2961 PR = P.getOperand(i);
2962 }
2963}
2964
2965unsigned HexagonLoopRescheduling::getDefReg(const MachineInstr *MI) {
2966 RegisterSet Defs;
2967 HBS::getInstrDefs(*MI, Defs);
2968 if (Defs.count() != 1)
2969 return 0;
2970 return Defs.find_first();
2971}
2972
2973bool HexagonLoopRescheduling::isConst(unsigned Reg) const {
2974 if (!BTP->has(Reg))
2975 return false;
2976 const BitTracker::RegisterCell &RC = BTP->lookup(Reg);
2977 for (unsigned i = 0, w = RC.width(); i < w; ++i) {
2978 const BitTracker::BitValue &V = RC[i];
2979 if (!V.is(0) && !V.is(1))
2980 return false;
2981 }
2982 return true;
2983}
2984
2985bool HexagonLoopRescheduling::isBitShuffle(const MachineInstr *MI,
2986 unsigned DefR) const {
2987 unsigned Opc = MI->getOpcode();
2988 switch (Opc) {
2989 case TargetOpcode::COPY:
2990 case Hexagon::S2_lsr_i_r:
2991 case Hexagon::S2_asr_i_r:
2992 case Hexagon::S2_asl_i_r:
2993 case Hexagon::S2_lsr_i_p:
2994 case Hexagon::S2_asr_i_p:
2995 case Hexagon::S2_asl_i_p:
2996 case Hexagon::S2_insert:
2997 case Hexagon::A2_or:
2998 case Hexagon::A2_orp:
2999 case Hexagon::A2_and:
3000 case Hexagon::A2_andp:
3001 case Hexagon::A2_combinew:
3002 case Hexagon::A4_combineri:
3003 case Hexagon::A4_combineir:
3004 case Hexagon::A2_combineii:
3005 case Hexagon::A4_combineii:
3006 case Hexagon::A2_combine_ll:
3007 case Hexagon::A2_combine_lh:
3008 case Hexagon::A2_combine_hl:
3009 case Hexagon::A2_combine_hh:
3010 return true;
3011 }
3012 return false;
3013}
3014
3015bool HexagonLoopRescheduling::isStoreInput(const MachineInstr *MI,
3016 unsigned InpR) const {
3017 for (unsigned i = 0, n = MI->getNumOperands(); i < n; ++i) {
3018 const MachineOperand &Op = MI->getOperand(i);
3019 if (!Op.isReg())
3020 continue;
3021 if (Op.getReg() == InpR)
3022 return i == n-1;
3023 }
3024 return false;
3025}
3026
3027bool HexagonLoopRescheduling::isShuffleOf(unsigned OutR, unsigned InpR) const {
3028 if (!BTP->has(OutR) || !BTP->has(InpR))
3029 return false;
3030 const BitTracker::RegisterCell &OutC = BTP->lookup(OutR);
3031 for (unsigned i = 0, w = OutC.width(); i < w; ++i) {
3032 const BitTracker::BitValue &V = OutC[i];
3033 if (V.Type != BitTracker::BitValue::Ref)
3034 continue;
3035 if (V.RefI.Reg != InpR)
3036 return false;
3037 }
3038 return true;
3039}
3040
3041bool HexagonLoopRescheduling::isSameShuffle(unsigned OutR1, unsigned InpR1,
3042 unsigned OutR2, unsigned &InpR2) const {
3043 if (!BTP->has(OutR1) || !BTP->has(InpR1) || !BTP->has(OutR2))
3044 return false;
3045 const BitTracker::RegisterCell &OutC1 = BTP->lookup(OutR1);
3046 const BitTracker::RegisterCell &OutC2 = BTP->lookup(OutR2);
3047 unsigned W = OutC1.width();
3048 unsigned MatchR = 0;
3049 if (W != OutC2.width())
3050 return false;
3051 for (unsigned i = 0; i < W; ++i) {
3052 const BitTracker::BitValue &V1 = OutC1[i], &V2 = OutC2[i];
3053 if (V1.Type != V2.Type || V1.Type == BitTracker::BitValue::One)
3054 return false;
3055 if (V1.Type != BitTracker::BitValue::Ref)
3056 continue;
3057 if (V1.RefI.Pos != V2.RefI.Pos)
3058 return false;
3059 if (V1.RefI.Reg != InpR1)
3060 return false;
3061 if (V2.RefI.Reg == 0 || V2.RefI.Reg == OutR2)
3062 return false;
3063 if (!MatchR)
3064 MatchR = V2.RefI.Reg;
3065 else if (V2.RefI.Reg != MatchR)
3066 return false;
3067 }
3068 InpR2 = MatchR;
3069 return true;
3070}
3071
3072void HexagonLoopRescheduling::moveGroup(InstrGroup &G, MachineBasicBlock &LB,
3073 MachineBasicBlock &PB, MachineBasicBlock::iterator At, unsigned OldPhiR,
3074 unsigned NewPredR) {
3075 DenseMap<unsigned,unsigned> RegMap;
3076
3077 const TargetRegisterClass *PhiRC = MRI->getRegClass(NewPredR);
3078 Register PhiR = MRI->createVirtualRegister(PhiRC);
3079 BuildMI(LB, At, At->getDebugLoc(), HII->get(TargetOpcode::PHI), PhiR)
3080 .addReg(NewPredR)
3081 .addMBB(&PB)
3082 .addReg(G.Inp.Reg)
3083 .addMBB(&LB);
3084 RegMap.insert(std::make_pair(G.Inp.Reg, PhiR));
3085
3086 for (const MachineInstr *SI : llvm::reverse(G.Ins)) {
3087 unsigned DR = getDefReg(SI);
3088 const TargetRegisterClass *RC = MRI->getRegClass(DR);
3089 Register NewDR = MRI->createVirtualRegister(RC);
3090 DebugLoc DL = SI->getDebugLoc();
3091
3092 auto MIB = BuildMI(LB, At, DL, HII->get(SI->getOpcode()), NewDR);
3093 for (const MachineOperand &Op : SI->operands()) {
3094 if (!Op.isReg()) {
3095 MIB.add(Op);
3096 continue;
3097 }
3098 if (!Op.isUse())
3099 continue;
3100 unsigned UseR = RegMap[Op.getReg()];
3101 MIB.addReg(UseR, {}, Op.getSubReg());
3102 }
3103 RegMap.insert(std::make_pair(DR, NewDR));
3104 }
3105
3106 HBS::replaceReg(OldPhiR, RegMap[G.Out.Reg], *MRI);
3107}
3108
3109bool HexagonLoopRescheduling::processLoop(LoopCand &C) {
3110 LLVM_DEBUG(dbgs() << "Processing loop in " << printMBBReference(*C.LB)
3111 << "\n");
3112 std::vector<PhiInfo> Phis;
3113 for (auto &I : *C.LB) {
3114 if (!I.isPHI())
3115 break;
3116 unsigned PR = getDefReg(&I);
3117 if (isConst(PR))
3118 continue;
3119 bool BadUse = false, GoodUse = false;
3120 for (const MachineInstr &UseI : MRI->use_instructions(PR)) {
3121 if (UseI.getParent() != C.LB) {
3122 BadUse = true;
3123 break;
3124 }
3125 if (isBitShuffle(&UseI, PR) || isStoreInput(&UseI, PR))
3126 GoodUse = true;
3127 }
3128 if (BadUse || !GoodUse)
3129 continue;
3130
3131 Phis.push_back(PhiInfo(I, *C.LB));
3132 }
3133
3134 LLVM_DEBUG({
3135 dbgs() << "Phis: {";
3136 for (auto &I : Phis) {
3137 dbgs() << ' ' << printReg(I.DefR, HRI) << "=phi("
3138 << printReg(I.PR.Reg, HRI, I.PR.Sub) << ":b" << I.PB->getNumber()
3139 << ',' << printReg(I.LR.Reg, HRI, I.LR.Sub) << ":b"
3140 << I.LB->getNumber() << ')';
3141 }
3142 dbgs() << " }\n";
3143 });
3144
3145 if (Phis.empty())
3146 return false;
3147
3148 bool Changed = false;
3149 InstrList ShufIns;
3150
3151 // Go backwards in the block: for each bit shuffling instruction, check
3152 // if that instruction could potentially be moved to the front of the loop:
3153 // the output of the loop cannot be used in a non-shuffling instruction
3154 // in this loop.
3155 for (MachineInstr &MI : llvm::reverse(*C.LB)) {
3156 if (MI.isTerminator())
3157 continue;
3158 if (MI.isPHI())
3159 break;
3160
3161 RegisterSet Defs;
3162 HBS::getInstrDefs(MI, Defs);
3163 if (Defs.count() != 1)
3164 continue;
3165 Register DefR = Defs.find_first();
3166 if (!DefR.isVirtual())
3167 continue;
3168 if (!isBitShuffle(&MI, DefR))
3169 continue;
3170
3171 bool BadUse = false;
3172 for (auto UI = MRI->use_begin(DefR), UE = MRI->use_end(); UI != UE; ++UI) {
3173 MachineInstr *UseI = UI->getParent();
3174 if (UseI->getParent() == C.LB) {
3175 if (UseI->isPHI()) {
3176 // If the use is in a phi node in this loop, then it should be
3177 // the value corresponding to the back edge.
3178 unsigned Idx = UI.getOperandNo();
3179 if (UseI->getOperand(Idx+1).getMBB() != C.LB)
3180 BadUse = true;
3181 } else {
3182 if (!llvm::is_contained(ShufIns, UseI))
3183 BadUse = true;
3184 }
3185 } else {
3186 // There is a use outside of the loop, but there is no epilog block
3187 // suitable for a copy-out.
3188 if (C.EB == nullptr)
3189 BadUse = true;
3190 }
3191 if (BadUse)
3192 break;
3193 }
3194
3195 if (BadUse)
3196 continue;
3197 ShufIns.push_back(&MI);
3198 }
3199
3200 // Partition the list of shuffling instructions into instruction groups,
3201 // where each group has to be moved as a whole (i.e. a group is a chain of
3202 // dependent instructions). A group produces a single live output register,
3203 // which is meant to be the input of the loop phi node (although this is
3204 // not checked here yet). It also uses a single register as its input,
3205 // which is some value produced in the loop body. After moving the group
3206 // to the beginning of the loop, that input register would need to be
3207 // the loop-carried register (through a phi node) instead of the (currently
3208 // loop-carried) output register.
3209 using InstrGroupList = std::vector<InstrGroup>;
3210 InstrGroupList Groups;
3211
3212 for (unsigned i = 0, n = ShufIns.size(); i < n; ++i) {
3213 MachineInstr *SI = ShufIns[i];
3214 if (SI == nullptr)
3215 continue;
3216
3217 InstrGroup G;
3218 G.Ins.push_back(SI);
3219 G.Out.Reg = getDefReg(SI);
3220 RegisterSet Inputs;
3221 HBS::getInstrUses(*SI, Inputs);
3222
3223 for (unsigned j = i+1; j < n; ++j) {
3224 MachineInstr *MI = ShufIns[j];
3225 if (MI == nullptr)
3226 continue;
3227 RegisterSet Defs;
3228 HBS::getInstrDefs(*MI, Defs);
3229 // If this instruction does not define any pending inputs, skip it.
3230 if (!Defs.intersects(Inputs))
3231 continue;
3232 // Otherwise, add it to the current group and remove the inputs that
3233 // are defined by MI.
3234 G.Ins.push_back(MI);
3235 Inputs.remove(Defs);
3236 // Then add all registers used by MI.
3237 HBS::getInstrUses(*MI, Inputs);
3238 ShufIns[j] = nullptr;
3239 }
3240
3241 // Only add a group if it requires at most one register.
3242 if (Inputs.count() > 1)
3243 continue;
3244 auto LoopInpEq = [G] (const PhiInfo &P) -> bool {
3245 return G.Out.Reg == P.LR.Reg;
3246 };
3247 if (llvm::none_of(Phis, LoopInpEq))
3248 continue;
3249
3250 G.Inp.Reg = Inputs.find_first();
3251 Groups.push_back(G);
3252 }
3253
3254 LLVM_DEBUG({
3255 for (unsigned i = 0, n = Groups.size(); i < n; ++i) {
3256 InstrGroup &G = Groups[i];
3257 dbgs() << "Group[" << i << "] inp: "
3258 << printReg(G.Inp.Reg, HRI, G.Inp.Sub)
3259 << " out: " << printReg(G.Out.Reg, HRI, G.Out.Sub) << "\n";
3260 for (const MachineInstr *MI : G.Ins)
3261 dbgs() << " " << MI;
3262 }
3263 });
3264
3265 for (InstrGroup &G : Groups) {
3266 if (!isShuffleOf(G.Out.Reg, G.Inp.Reg))
3267 continue;
3268 auto LoopInpEq = [G] (const PhiInfo &P) -> bool {
3269 return G.Out.Reg == P.LR.Reg;
3270 };
3271 auto F = llvm::find_if(Phis, LoopInpEq);
3272 if (F == Phis.end())
3273 continue;
3274 unsigned PrehR = 0;
3275 if (!isSameShuffle(G.Out.Reg, G.Inp.Reg, F->PR.Reg, PrehR)) {
3276 const MachineInstr *DefPrehR = MRI->getVRegDef(F->PR.Reg);
3277 unsigned Opc = DefPrehR->getOpcode();
3278 if (Opc != Hexagon::A2_tfrsi && Opc != Hexagon::A2_tfrpi)
3279 continue;
3280 if (!DefPrehR->getOperand(1).isImm())
3281 continue;
3282 if (DefPrehR->getOperand(1).getImm() != 0)
3283 continue;
3284 const TargetRegisterClass *RC = MRI->getRegClass(G.Inp.Reg);
3285 if (RC != MRI->getRegClass(F->PR.Reg)) {
3286 PrehR = MRI->createVirtualRegister(RC);
3287 unsigned TfrI = (RC == &Hexagon::IntRegsRegClass) ? Hexagon::A2_tfrsi
3288 : Hexagon::A2_tfrpi;
3289 auto T = C.PB->getFirstTerminator();
3290 DebugLoc DL = (T != C.PB->end()) ? T->getDebugLoc() : DebugLoc();
3291 BuildMI(*C.PB, T, DL, HII->get(TfrI), PrehR)
3292 .addImm(0);
3293 } else {
3294 PrehR = F->PR.Reg;
3295 }
3296 }
3297 // isSameShuffle could match with PrehR being of a wider class than
3298 // G.Inp.Reg, for example if G shuffles the low 32 bits of its input,
3299 // it would match for the input being a 32-bit register, and PrehR
3300 // being a 64-bit register (where the low 32 bits match). This could
3301 // be handled, but for now skip these cases.
3302 if (MRI->getRegClass(PrehR) != MRI->getRegClass(G.Inp.Reg))
3303 continue;
3304 moveGroup(G, *F->LB, *F->PB, F->LB->getFirstNonPHI(), F->DefR, PrehR);
3305 Changed = true;
3306 }
3307
3308 return Changed;
3309}
3310
3311bool HexagonLoopRescheduling::runOnMachineFunction(MachineFunction &MF) {
3312 if (skipFunction(MF.getFunction()))
3313 return false;
3314
3315 auto &HST = MF.getSubtarget<HexagonSubtarget>();
3316 HII = HST.getInstrInfo();
3317 HRI = HST.getRegisterInfo();
3318 MRI = &MF.getRegInfo();
3319 const HexagonEvaluator HE(*HRI, *MRI, *HII, MF);
3320 BitTracker BT(HE, MF);
3321 LLVM_DEBUG(BT.trace(true));
3322 BT.run();
3323 BTP = &BT;
3324
3325 std::vector<LoopCand> Cand;
3326
3327 for (auto &B : MF) {
3328 if (B.pred_size() != 2 || B.succ_size() != 2)
3329 continue;
3330 MachineBasicBlock *PB = nullptr;
3331 bool IsLoop = false;
3332 for (MachineBasicBlock *Pred : B.predecessors()) {
3333 if (Pred != &B)
3334 PB = Pred;
3335 else
3336 IsLoop = true;
3337 }
3338 if (!IsLoop)
3339 continue;
3340
3341 MachineBasicBlock *EB = nullptr;
3342 for (MachineBasicBlock *Succ : B.successors()) {
3343 if (Succ == &B)
3344 continue;
3345 // Set EP to the epilog block, if it has only 1 predecessor (i.e. the
3346 // edge from B to EP is non-critical.
3347 if (Succ->pred_size() == 1)
3348 EB = Succ;
3349 break;
3350 }
3351
3352 Cand.push_back(LoopCand(&B, PB, EB));
3353 }
3354
3355 bool Changed = false;
3356 for (auto &C : Cand)
3357 Changed |= processLoop(C);
3358
3359 return Changed;
3360}
3361
3362//===----------------------------------------------------------------------===//
3363// Public Constructor Functions
3364//===----------------------------------------------------------------------===//
3365
3367 return new HexagonLoopRescheduling();
3368}
3369
3371 return new HexagonBitSimplify();
3372}
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
SDValue Pred
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
uint64_t Imm
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis false
basic Basic Alias true
BitTracker BT
This file implements the BitVector class.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static std::optional< ArrayRef< InsnRange >::iterator > intersects(const MachineInstr *StartMI, const MachineInstr *EndMI, ArrayRef< InsnRange > Ranges, const InstructionOrdering &Ordering)
Check if the instruction range [StartMI, EndMI] intersects any instruction range in Ranges.
static unsigned CountBitSplit
static cl::opt< bool > PreserveTiedOps("hexbit-keep-tied", cl::Hidden, cl::init(true), cl::desc("Preserve subregisters in tied operands"))
static cl::opt< bool > GenExtract("hexbit-extract", cl::Hidden, cl::init(true), cl::desc("Generate extract instructions"))
static cl::opt< unsigned > MaxBitSplit("hexbit-max-bitsplit", cl::Hidden, cl::init(std::numeric_limits< unsigned >::max()))
static cl::opt< bool > GenBitSplit("hexbit-bitsplit", cl::Hidden, cl::init(true), cl::desc("Generate bitsplit instructions"))
static cl::opt< unsigned > MaxExtract("hexbit-max-extract", cl::Hidden, cl::init(std::numeric_limits< unsigned >::max()))
static cl::opt< unsigned > RegisterSetLimit("hexbit-registerset-limit", cl::Hidden, cl::init(1000))
static unsigned CountExtract
IRTranslator LLVM IR MI
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:550
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
#define H(x, y, z)
Definition MD5.cpp:56
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define T
#define P(N)
PassBuilder PB(Mod->getContext(), Machine, PassOpts->PTO, std::nullopt, &PIC)
if(PassOpts->AAPipeline)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
MachineRegisterInfo & MRI
MachineInstr *& Lo
Remove Loads Into Fake Uses
bool isDead(const MachineInstr &MI, const MachineRegisterInfo &MRI)
static bool processLoop(Loop &L, const AArch64Subtarget &ST, DataLayout DL)
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
static bool processBlock(MachineBasicBlock &MBB, const SystemZInstrInfo *TII, const TargetRegisterInfo *TRI)
SmallSet< unsigned, 4 > RegisterSet
static const X86InstrFMA3Group Groups[]
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
bool subsetOf(const BitVector &RHS) const
Check if This is a subset of RHS.
Definition BitVector.h:592
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:827
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition Function.h:708
unsigned getHexagonSubRegIndex(const TargetRegisterClass &RC, unsigned GenIdx) const
const HexagonRegisterInfo * getRegisterInfo() const override
unsigned getID() const
getID() - Return the register class ID number.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineInstrBundleIterator< MachineInstr > iterator
Analysis pass which computes a MachineDominatorTree.
bool dominates(const MachineInstr *A, const MachineInstr *B) const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineBasicBlock & front() const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool isCopy() const
const MachineBasicBlock * getParent() const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
void setSubReg(unsigned subReg)
unsigned getSubReg() const
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
static MachineOperand CreateImm(int64_t Val)
Register getReg() const
getReg - Returns the register number.
LLVM_ABI void markUsesInDebugValueAsUndef(Register Reg) const
markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the specified register as undefined wh...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
LLVM_ABI LLVM_READONLY MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
iterator_range< use_instr_iterator > use_instructions(Register Reg) const
const TargetRegisterInfo * getTargetRegisterInfo() const
use_iterator use_begin(Register RegNo) const
static use_iterator use_end()
iterator_range< use_iterator > use_operands(Register Reg) const
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
std::pair< const_iterator, bool > insert(const unsigned &V)
Definition SmallSet.h:184
void push_back(const T &Elt)
TypeSize getRegSizeInBits(const TargetRegisterClass &RC) const
Return the size in bits of a register from class RC.
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char IsConst[]
Key for Kernel::Arg::Metadata::mIsConst.
@ Entry
Definition COFF.h:862
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:771
SmallVector< const MachineInstr * > InstrList
bool isConst(unsigned Opc)
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
LLVM_ABI std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
This is an optimization pass for GlobalISel generic memory operations.
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:280
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1781
bool includes(R1 &&Range1, R2 &&Range2)
Provide wrappers to std::includes which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:2008
bool isEqual(const GCNRPTracker::LiveRegSet &S1, const GCNRPTracker::LiveRegSet &S2)
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
@ Store
The extracted value is stored (ExtractElement only).
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:244
FunctionPass * createHexagonBitSimplify()
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
Definition MathExtras.h:151
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1769
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
Definition MathExtras.h:156
DomTreeNodeBase< MachineBasicBlock > MachineDomTreeNode
FunctionPass * createHexagonLoopRescheduling()
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
@ Sub
Subtraction of integers.
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition STLExtras.h:2028
DWARFExpression::Operation Op
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1788
iterator_range< typename GraphTraits< GraphType >::ChildIteratorType > children(const typename GraphTraits< GraphType >::NodeRef &G)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
#define N
Maximum remaining allocation size observed for a phi node, and how often the allocation size has alre...
bool is(unsigned T) const
Definition BitTracker.h:207
static BitValue self(const BitRef &Self=BitRef())
Definition BitTracker.h:278
bool has(unsigned Reg) const
Definition BitTracker.h:350
const RegisterCell & lookup(unsigned Reg) const
Definition BitTracker.h:355
bool reached(const MachineBasicBlock *B) const
void trace(bool On=false)
Definition BitTracker.h:48
void put(RegisterRef RR, const RegisterCell &RC)
void visit(const MachineInstr &MI)
RegisterCell get(RegisterRef RR) const