]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/Hexagon/HexagonBitTracker.cpp
Merge ^/head r311306 through r311313.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Target / Hexagon / HexagonBitTracker.cpp
1 //===--- HexagonBitTracker.cpp --------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/CodeGen/MachineRegisterInfo.h"
11 #include "llvm/IR/Module.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/raw_ostream.h"
14
15 #include "Hexagon.h"
16 #include "HexagonInstrInfo.h"
17 #include "HexagonRegisterInfo.h"
18 #include "HexagonTargetMachine.h"
19 #include "HexagonBitTracker.h"
20
21 using namespace llvm;
22
23 typedef BitTracker BT;
24
25 HexagonEvaluator::HexagonEvaluator(const HexagonRegisterInfo &tri,
26                                    MachineRegisterInfo &mri,
27                                    const HexagonInstrInfo &tii,
28                                    MachineFunction &mf)
29     : MachineEvaluator(tri, mri), MF(mf), MFI(mf.getFrameInfo()), TII(tii) {
30   // Populate the VRX map (VR to extension-type).
31   // Go over all the formal parameters of the function. If a given parameter
32   // P is sign- or zero-extended, locate the virtual register holding that
33   // parameter and create an entry in the VRX map indicating the type of ex-
34   // tension (and the source type).
35   // This is a bit complicated to do accurately, since the memory layout in-
36   // formation is necessary to precisely determine whether an aggregate para-
37   // meter will be passed in a register or in memory. What is given in MRI
38   // is the association between the physical register that is live-in (i.e.
39   // holds an argument), and the virtual register that this value will be
40   // copied into. This, by itself, is not sufficient to map back the virtual
41   // register to a formal parameter from Function (since consecutive live-ins
42   // from MRI may not correspond to consecutive formal parameters from Func-
43   // tion). To avoid the complications with in-memory arguments, only consi-
44   // der the initial sequence of formal parameters that are known to be
45   // passed via registers.
46   unsigned AttrIdx = 0;
47   unsigned InVirtReg, InPhysReg = 0;
48   const Function &F = *MF.getFunction();
49   typedef Function::const_arg_iterator arg_iterator;
50   for (arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
51     AttrIdx++;
52     const Argument &Arg = *I;
53     Type *ATy = Arg.getType();
54     unsigned Width = 0;
55     if (ATy->isIntegerTy())
56       Width = ATy->getIntegerBitWidth();
57     else if (ATy->isPointerTy())
58       Width = 32;
59     // If pointer size is not set through target data, it will default to
60     // Module::AnyPointerSize.
61     if (Width == 0 || Width > 64)
62       break;
63     AttributeSet Attrs = F.getAttributes();
64     if (Attrs.hasAttribute(AttrIdx, Attribute::ByVal))
65       continue;
66     InPhysReg = getNextPhysReg(InPhysReg, Width);
67     if (!InPhysReg)
68       break;
69     InVirtReg = getVirtRegFor(InPhysReg);
70     if (!InVirtReg)
71       continue;
72     if (Attrs.hasAttribute(AttrIdx, Attribute::SExt))
73       VRX.insert(std::make_pair(InVirtReg, ExtType(ExtType::SExt, Width)));
74     else if (Attrs.hasAttribute(AttrIdx, Attribute::ZExt))
75       VRX.insert(std::make_pair(InVirtReg, ExtType(ExtType::ZExt, Width)));
76   }
77 }
78
79
80 BT::BitMask HexagonEvaluator::mask(unsigned Reg, unsigned Sub) const {
81   if (Sub == 0)
82     return MachineEvaluator::mask(Reg, 0);
83   using namespace Hexagon;
84   const TargetRegisterClass *RC = MRI.getRegClass(Reg);
85   unsigned ID = RC->getID();
86   uint16_t RW = getRegBitWidth(RegisterRef(Reg, Sub));
87   auto &HRI = static_cast<const HexagonRegisterInfo&>(TRI);
88   bool IsSubLo = (Sub == HRI.getHexagonSubRegIndex(RC, Hexagon::ps_sub_lo));
89   switch (ID) {
90     case DoubleRegsRegClassID:
91     case VecDblRegsRegClassID:
92     case VecDblRegs128BRegClassID:
93       return IsSubLo ? BT::BitMask(0, RW-1)
94                      : BT::BitMask(RW, 2*RW-1);
95     default:
96       break;
97   }
98 #ifndef NDEBUG
99   dbgs() << PrintReg(Reg, &TRI, Sub) << '\n';
100 #endif
101   llvm_unreachable("Unexpected register/subregister");
102 }
103
104 namespace {
105 class RegisterRefs {
106   std::vector<BT::RegisterRef> Vector;
107
108 public:
109   RegisterRefs(const MachineInstr &MI) : Vector(MI.getNumOperands()) {
110     for (unsigned i = 0, n = Vector.size(); i < n; ++i) {
111       const MachineOperand &MO = MI.getOperand(i);
112       if (MO.isReg())
113         Vector[i] = BT::RegisterRef(MO);
114       // For indices that don't correspond to registers, the entry will
115       // remain constructed via the default constructor.
116     }
117   }
118
119   size_t size() const { return Vector.size(); }
120   const BT::RegisterRef &operator[](unsigned n) const {
121     // The main purpose of this operator is to assert with bad argument.
122     assert(n < Vector.size());
123     return Vector[n];
124   }
125 };
126 }
127
128 bool HexagonEvaluator::evaluate(const MachineInstr &MI,
129                                 const CellMapType &Inputs,
130                                 CellMapType &Outputs) const {
131   unsigned NumDefs = 0;
132
133   // Sanity verification: there should not be any defs with subregisters.
134   for (unsigned i = 0, n = MI.getNumOperands(); i < n; ++i) {
135     const MachineOperand &MO = MI.getOperand(i);
136     if (!MO.isReg() || !MO.isDef())
137       continue;
138     NumDefs++;
139     assert(MO.getSubReg() == 0);
140   }
141
142   if (NumDefs == 0)
143     return false;
144
145   using namespace Hexagon;
146   unsigned Opc = MI.getOpcode();
147
148   if (MI.mayLoad()) {
149     switch (Opc) {
150       // These instructions may be marked as mayLoad, but they are generating
151       // immediate values, so skip them.
152       case CONST32:
153       case CONST64:
154         break;
155       default:
156         return evaluateLoad(MI, Inputs, Outputs);
157     }
158   }
159
160   // Check COPY instructions that copy formal parameters into virtual
161   // registers. Such parameters can be sign- or zero-extended at the
162   // call site, and we should take advantage of this knowledge. The MRI
163   // keeps a list of pairs of live-in physical and virtual registers,
164   // which provides information about which virtual registers will hold
165   // the argument values. The function will still contain instructions
166   // defining those virtual registers, and in practice those are COPY
167   // instructions from a physical to a virtual register. In such cases,
168   // applying the argument extension to the virtual register can be seen
169   // as simply mirroring the extension that had already been applied to
170   // the physical register at the call site. If the defining instruction
171   // was not a COPY, it would not be clear how to mirror that extension
172   // on the callee's side. For that reason, only check COPY instructions
173   // for potential extensions.
174   if (MI.isCopy()) {
175     if (evaluateFormalCopy(MI, Inputs, Outputs))
176       return true;
177   }
178
179   // Beyond this point, if any operand is a global, skip that instruction.
180   // The reason is that certain instructions that can take an immediate
181   // operand can also have a global symbol in that operand. To avoid
182   // checking what kind of operand a given instruction has individually
183   // for each instruction, do it here. Global symbols as operands gene-
184   // rally do not provide any useful information.
185   for (unsigned i = 0, n = MI.getNumOperands(); i < n; ++i) {
186     const MachineOperand &MO = MI.getOperand(i);
187     if (MO.isGlobal() || MO.isBlockAddress() || MO.isSymbol() || MO.isJTI() ||
188         MO.isCPI())
189       return false;
190   }
191
192   RegisterRefs Reg(MI);
193 #define op(i) MI.getOperand(i)
194 #define rc(i) RegisterCell::ref(getCell(Reg[i], Inputs))
195 #define im(i) MI.getOperand(i).getImm()
196
197   // If the instruction has no register operands, skip it.
198   if (Reg.size() == 0)
199     return false;
200
201   // Record result for register in operand 0.
202   auto rr0 = [this,Reg] (const BT::RegisterCell &Val, CellMapType &Outputs)
203         -> bool {
204     putCell(Reg[0], Val, Outputs);
205     return true;
206   };
207   // Get the cell corresponding to the N-th operand.
208   auto cop = [this, &Reg, &MI, &Inputs](unsigned N,
209                                         uint16_t W) -> BT::RegisterCell {
210     const MachineOperand &Op = MI.getOperand(N);
211     if (Op.isImm())
212       return eIMM(Op.getImm(), W);
213     if (!Op.isReg())
214       return RegisterCell::self(0, W);
215     assert(getRegBitWidth(Reg[N]) == W && "Register width mismatch");
216     return rc(N);
217   };
218   // Extract RW low bits of the cell.
219   auto lo = [this] (const BT::RegisterCell &RC, uint16_t RW)
220         -> BT::RegisterCell {
221     assert(RW <= RC.width());
222     return eXTR(RC, 0, RW);
223   };
224   // Extract RW high bits of the cell.
225   auto hi = [this] (const BT::RegisterCell &RC, uint16_t RW)
226         -> BT::RegisterCell {
227     uint16_t W = RC.width();
228     assert(RW <= W);
229     return eXTR(RC, W-RW, W);
230   };
231   // Extract N-th halfword (counting from the least significant position).
232   auto half = [this] (const BT::RegisterCell &RC, unsigned N)
233         -> BT::RegisterCell {
234     assert(N*16+16 <= RC.width());
235     return eXTR(RC, N*16, N*16+16);
236   };
237   // Shuffle bits (pick even/odd from cells and merge into result).
238   auto shuffle = [this] (const BT::RegisterCell &Rs, const BT::RegisterCell &Rt,
239                          uint16_t BW, bool Odd) -> BT::RegisterCell {
240     uint16_t I = Odd, Ws = Rs.width();
241     assert(Ws == Rt.width());
242     RegisterCell RC = eXTR(Rt, I*BW, I*BW+BW).cat(eXTR(Rs, I*BW, I*BW+BW));
243     I += 2;
244     while (I*BW < Ws) {
245       RC.cat(eXTR(Rt, I*BW, I*BW+BW)).cat(eXTR(Rs, I*BW, I*BW+BW));
246       I += 2;
247     }
248     return RC;
249   };
250
251   // The bitwidth of the 0th operand. In most (if not all) of the
252   // instructions below, the 0th operand is the defined register.
253   // Pre-compute the bitwidth here, because it is needed in many cases
254   // cases below.
255   uint16_t W0 = (Reg[0].Reg != 0) ? getRegBitWidth(Reg[0]) : 0;
256
257   switch (Opc) {
258     // Transfer immediate:
259
260     case A2_tfrsi:
261     case A2_tfrpi:
262     case CONST32:
263     case CONST64:
264       return rr0(eIMM(im(1), W0), Outputs);
265     case PS_false:
266       return rr0(RegisterCell(W0).fill(0, W0, BT::BitValue::Zero), Outputs);
267     case PS_true:
268       return rr0(RegisterCell(W0).fill(0, W0, BT::BitValue::One), Outputs);
269     case PS_fi: {
270       int FI = op(1).getIndex();
271       int Off = op(2).getImm();
272       unsigned A = MFI.getObjectAlignment(FI) + std::abs(Off);
273       unsigned L = Log2_32(A);
274       RegisterCell RC = RegisterCell::self(Reg[0].Reg, W0);
275       RC.fill(0, L, BT::BitValue::Zero);
276       return rr0(RC, Outputs);
277     }
278
279     // Transfer register:
280
281     case A2_tfr:
282     case A2_tfrp:
283     case C2_pxfer_map:
284       return rr0(rc(1), Outputs);
285     case C2_tfrpr: {
286       uint16_t RW = W0;
287       uint16_t PW = 8; // XXX Pred size: getRegBitWidth(Reg[1]);
288       assert(PW <= RW);
289       RegisterCell PC = eXTR(rc(1), 0, PW);
290       RegisterCell RC = RegisterCell(RW).insert(PC, BT::BitMask(0, PW-1));
291       RC.fill(PW, RW, BT::BitValue::Zero);
292       return rr0(RC, Outputs);
293     }
294     case C2_tfrrp: {
295       RegisterCell RC = RegisterCell::self(Reg[0].Reg, W0);
296       W0 = 8; // XXX Pred size
297       return rr0(eINS(RC, eXTR(rc(1), 0, W0), 0), Outputs);
298     }
299
300     // Arithmetic:
301
302     case A2_abs:
303     case A2_absp:
304       // TODO
305       break;
306
307     case A2_addsp: {
308       uint16_t W1 = getRegBitWidth(Reg[1]);
309       assert(W0 == 64 && W1 == 32);
310       RegisterCell CW = RegisterCell(W0).insert(rc(1), BT::BitMask(0, W1-1));
311       RegisterCell RC = eADD(eSXT(CW, W1), rc(2));
312       return rr0(RC, Outputs);
313     }
314     case A2_add:
315     case A2_addp:
316       return rr0(eADD(rc(1), rc(2)), Outputs);
317     case A2_addi:
318       return rr0(eADD(rc(1), eIMM(im(2), W0)), Outputs);
319     case S4_addi_asl_ri: {
320       RegisterCell RC = eADD(eIMM(im(1), W0), eASL(rc(2), im(3)));
321       return rr0(RC, Outputs);
322     }
323     case S4_addi_lsr_ri: {
324       RegisterCell RC = eADD(eIMM(im(1), W0), eLSR(rc(2), im(3)));
325       return rr0(RC, Outputs);
326     }
327     case S4_addaddi: {
328       RegisterCell RC = eADD(rc(1), eADD(rc(2), eIMM(im(3), W0)));
329       return rr0(RC, Outputs);
330     }
331     case M4_mpyri_addi: {
332       RegisterCell M = eMLS(rc(2), eIMM(im(3), W0));
333       RegisterCell RC = eADD(eIMM(im(1), W0), lo(M, W0));
334       return rr0(RC, Outputs);
335     }
336     case M4_mpyrr_addi: {
337       RegisterCell M = eMLS(rc(2), rc(3));
338       RegisterCell RC = eADD(eIMM(im(1), W0), lo(M, W0));
339       return rr0(RC, Outputs);
340     }
341     case M4_mpyri_addr_u2: {
342       RegisterCell M = eMLS(eIMM(im(2), W0), rc(3));
343       RegisterCell RC = eADD(rc(1), lo(M, W0));
344       return rr0(RC, Outputs);
345     }
346     case M4_mpyri_addr: {
347       RegisterCell M = eMLS(rc(2), eIMM(im(3), W0));
348       RegisterCell RC = eADD(rc(1), lo(M, W0));
349       return rr0(RC, Outputs);
350     }
351     case M4_mpyrr_addr: {
352       RegisterCell M = eMLS(rc(2), rc(3));
353       RegisterCell RC = eADD(rc(1), lo(M, W0));
354       return rr0(RC, Outputs);
355     }
356     case S4_subaddi: {
357       RegisterCell RC = eADD(rc(1), eSUB(eIMM(im(2), W0), rc(3)));
358       return rr0(RC, Outputs);
359     }
360     case M2_accii: {
361       RegisterCell RC = eADD(rc(1), eADD(rc(2), eIMM(im(3), W0)));
362       return rr0(RC, Outputs);
363     }
364     case M2_acci: {
365       RegisterCell RC = eADD(rc(1), eADD(rc(2), rc(3)));
366       return rr0(RC, Outputs);
367     }
368     case M2_subacc: {
369       RegisterCell RC = eADD(rc(1), eSUB(rc(2), rc(3)));
370       return rr0(RC, Outputs);
371     }
372     case S2_addasl_rrri: {
373       RegisterCell RC = eADD(rc(1), eASL(rc(2), im(3)));
374       return rr0(RC, Outputs);
375     }
376     case C4_addipc: {
377       RegisterCell RPC = RegisterCell::self(Reg[0].Reg, W0);
378       RPC.fill(0, 2, BT::BitValue::Zero);
379       return rr0(eADD(RPC, eIMM(im(2), W0)), Outputs);
380     }
381     case A2_sub:
382     case A2_subp:
383       return rr0(eSUB(rc(1), rc(2)), Outputs);
384     case A2_subri:
385       return rr0(eSUB(eIMM(im(1), W0), rc(2)), Outputs);
386     case S4_subi_asl_ri: {
387       RegisterCell RC = eSUB(eIMM(im(1), W0), eASL(rc(2), im(3)));
388       return rr0(RC, Outputs);
389     }
390     case S4_subi_lsr_ri: {
391       RegisterCell RC = eSUB(eIMM(im(1), W0), eLSR(rc(2), im(3)));
392       return rr0(RC, Outputs);
393     }
394     case M2_naccii: {
395       RegisterCell RC = eSUB(rc(1), eADD(rc(2), eIMM(im(3), W0)));
396       return rr0(RC, Outputs);
397     }
398     case M2_nacci: {
399       RegisterCell RC = eSUB(rc(1), eADD(rc(2), rc(3)));
400       return rr0(RC, Outputs);
401     }
402     // 32-bit negation is done by "Rd = A2_subri 0, Rs"
403     case A2_negp:
404       return rr0(eSUB(eIMM(0, W0), rc(1)), Outputs);
405
406     case M2_mpy_up: {
407       RegisterCell M = eMLS(rc(1), rc(2));
408       return rr0(hi(M, W0), Outputs);
409     }
410     case M2_dpmpyss_s0:
411       return rr0(eMLS(rc(1), rc(2)), Outputs);
412     case M2_dpmpyss_acc_s0:
413       return rr0(eADD(rc(1), eMLS(rc(2), rc(3))), Outputs);
414     case M2_dpmpyss_nac_s0:
415       return rr0(eSUB(rc(1), eMLS(rc(2), rc(3))), Outputs);
416     case M2_mpyi: {
417       RegisterCell M = eMLS(rc(1), rc(2));
418       return rr0(lo(M, W0), Outputs);
419     }
420     case M2_macsip: {
421       RegisterCell M = eMLS(rc(2), eIMM(im(3), W0));
422       RegisterCell RC = eADD(rc(1), lo(M, W0));
423       return rr0(RC, Outputs);
424     }
425     case M2_macsin: {
426       RegisterCell M = eMLS(rc(2), eIMM(im(3), W0));
427       RegisterCell RC = eSUB(rc(1), lo(M, W0));
428       return rr0(RC, Outputs);
429     }
430     case M2_maci: {
431       RegisterCell M = eMLS(rc(2), rc(3));
432       RegisterCell RC = eADD(rc(1), lo(M, W0));
433       return rr0(RC, Outputs);
434     }
435     case M2_mpysmi: {
436       RegisterCell M = eMLS(rc(1), eIMM(im(2), W0));
437       return rr0(lo(M, 32), Outputs);
438     }
439     case M2_mpysin: {
440       RegisterCell M = eMLS(rc(1), eIMM(-im(2), W0));
441       return rr0(lo(M, 32), Outputs);
442     }
443     case M2_mpysip: {
444       RegisterCell M = eMLS(rc(1), eIMM(im(2), W0));
445       return rr0(lo(M, 32), Outputs);
446     }
447     case M2_mpyu_up: {
448       RegisterCell M = eMLU(rc(1), rc(2));
449       return rr0(hi(M, W0), Outputs);
450     }
451     case M2_dpmpyuu_s0:
452       return rr0(eMLU(rc(1), rc(2)), Outputs);
453     case M2_dpmpyuu_acc_s0:
454       return rr0(eADD(rc(1), eMLU(rc(2), rc(3))), Outputs);
455     case M2_dpmpyuu_nac_s0:
456       return rr0(eSUB(rc(1), eMLU(rc(2), rc(3))), Outputs);
457     //case M2_mpysu_up:
458
459     // Logical/bitwise:
460
461     case A2_andir:
462       return rr0(eAND(rc(1), eIMM(im(2), W0)), Outputs);
463     case A2_and:
464     case A2_andp:
465       return rr0(eAND(rc(1), rc(2)), Outputs);
466     case A4_andn:
467     case A4_andnp:
468       return rr0(eAND(rc(1), eNOT(rc(2))), Outputs);
469     case S4_andi_asl_ri: {
470       RegisterCell RC = eAND(eIMM(im(1), W0), eASL(rc(2), im(3)));
471       return rr0(RC, Outputs);
472     }
473     case S4_andi_lsr_ri: {
474       RegisterCell RC = eAND(eIMM(im(1), W0), eLSR(rc(2), im(3)));
475       return rr0(RC, Outputs);
476     }
477     case M4_and_and:
478       return rr0(eAND(rc(1), eAND(rc(2), rc(3))), Outputs);
479     case M4_and_andn:
480       return rr0(eAND(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs);
481     case M4_and_or:
482       return rr0(eAND(rc(1), eORL(rc(2), rc(3))), Outputs);
483     case M4_and_xor:
484       return rr0(eAND(rc(1), eXOR(rc(2), rc(3))), Outputs);
485     case A2_orir:
486       return rr0(eORL(rc(1), eIMM(im(2), W0)), Outputs);
487     case A2_or:
488     case A2_orp:
489       return rr0(eORL(rc(1), rc(2)), Outputs);
490     case A4_orn:
491     case A4_ornp:
492       return rr0(eORL(rc(1), eNOT(rc(2))), Outputs);
493     case S4_ori_asl_ri: {
494       RegisterCell RC = eORL(eIMM(im(1), W0), eASL(rc(2), im(3)));
495       return rr0(RC, Outputs);
496     }
497     case S4_ori_lsr_ri: {
498       RegisterCell RC = eORL(eIMM(im(1), W0), eLSR(rc(2), im(3)));
499       return rr0(RC, Outputs);
500     }
501     case M4_or_and:
502       return rr0(eORL(rc(1), eAND(rc(2), rc(3))), Outputs);
503     case M4_or_andn:
504       return rr0(eORL(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs);
505     case S4_or_andi:
506     case S4_or_andix: {
507       RegisterCell RC = eORL(rc(1), eAND(rc(2), eIMM(im(3), W0)));
508       return rr0(RC, Outputs);
509     }
510     case S4_or_ori: {
511       RegisterCell RC = eORL(rc(1), eORL(rc(2), eIMM(im(3), W0)));
512       return rr0(RC, Outputs);
513     }
514     case M4_or_or:
515       return rr0(eORL(rc(1), eORL(rc(2), rc(3))), Outputs);
516     case M4_or_xor:
517       return rr0(eORL(rc(1), eXOR(rc(2), rc(3))), Outputs);
518     case A2_xor:
519     case A2_xorp:
520       return rr0(eXOR(rc(1), rc(2)), Outputs);
521     case M4_xor_and:
522       return rr0(eXOR(rc(1), eAND(rc(2), rc(3))), Outputs);
523     case M4_xor_andn:
524       return rr0(eXOR(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs);
525     case M4_xor_or:
526       return rr0(eXOR(rc(1), eORL(rc(2), rc(3))), Outputs);
527     case M4_xor_xacc:
528       return rr0(eXOR(rc(1), eXOR(rc(2), rc(3))), Outputs);
529     case A2_not:
530     case A2_notp:
531       return rr0(eNOT(rc(1)), Outputs);
532
533     case S2_asl_i_r:
534     case S2_asl_i_p:
535       return rr0(eASL(rc(1), im(2)), Outputs);
536     case A2_aslh:
537       return rr0(eASL(rc(1), 16), Outputs);
538     case S2_asl_i_r_acc:
539     case S2_asl_i_p_acc:
540       return rr0(eADD(rc(1), eASL(rc(2), im(3))), Outputs);
541     case S2_asl_i_r_nac:
542     case S2_asl_i_p_nac:
543       return rr0(eSUB(rc(1), eASL(rc(2), im(3))), Outputs);
544     case S2_asl_i_r_and:
545     case S2_asl_i_p_and:
546       return rr0(eAND(rc(1), eASL(rc(2), im(3))), Outputs);
547     case S2_asl_i_r_or:
548     case S2_asl_i_p_or:
549       return rr0(eORL(rc(1), eASL(rc(2), im(3))), Outputs);
550     case S2_asl_i_r_xacc:
551     case S2_asl_i_p_xacc:
552       return rr0(eXOR(rc(1), eASL(rc(2), im(3))), Outputs);
553     case S2_asl_i_vh:
554     case S2_asl_i_vw:
555       // TODO
556       break;
557
558     case S2_asr_i_r:
559     case S2_asr_i_p:
560       return rr0(eASR(rc(1), im(2)), Outputs);
561     case A2_asrh:
562       return rr0(eASR(rc(1), 16), Outputs);
563     case S2_asr_i_r_acc:
564     case S2_asr_i_p_acc:
565       return rr0(eADD(rc(1), eASR(rc(2), im(3))), Outputs);
566     case S2_asr_i_r_nac:
567     case S2_asr_i_p_nac:
568       return rr0(eSUB(rc(1), eASR(rc(2), im(3))), Outputs);
569     case S2_asr_i_r_and:
570     case S2_asr_i_p_and:
571       return rr0(eAND(rc(1), eASR(rc(2), im(3))), Outputs);
572     case S2_asr_i_r_or:
573     case S2_asr_i_p_or:
574       return rr0(eORL(rc(1), eASR(rc(2), im(3))), Outputs);
575     case S2_asr_i_r_rnd: {
576       // The input is first sign-extended to 64 bits, then the output
577       // is truncated back to 32 bits.
578       assert(W0 == 32);
579       RegisterCell XC = eSXT(rc(1).cat(eIMM(0, W0)), W0);
580       RegisterCell RC = eASR(eADD(eASR(XC, im(2)), eIMM(1, 2*W0)), 1);
581       return rr0(eXTR(RC, 0, W0), Outputs);
582     }
583     case S2_asr_i_r_rnd_goodsyntax: {
584       int64_t S = im(2);
585       if (S == 0)
586         return rr0(rc(1), Outputs);
587       // Result: S2_asr_i_r_rnd Rs, u5-1
588       RegisterCell XC = eSXT(rc(1).cat(eIMM(0, W0)), W0);
589       RegisterCell RC = eLSR(eADD(eASR(XC, S-1), eIMM(1, 2*W0)), 1);
590       return rr0(eXTR(RC, 0, W0), Outputs);
591     }
592     case S2_asr_r_vh:
593     case S2_asr_i_vw:
594     case S2_asr_i_svw_trun:
595       // TODO
596       break;
597
598     case S2_lsr_i_r:
599     case S2_lsr_i_p:
600       return rr0(eLSR(rc(1), im(2)), Outputs);
601     case S2_lsr_i_r_acc:
602     case S2_lsr_i_p_acc:
603       return rr0(eADD(rc(1), eLSR(rc(2), im(3))), Outputs);
604     case S2_lsr_i_r_nac:
605     case S2_lsr_i_p_nac:
606       return rr0(eSUB(rc(1), eLSR(rc(2), im(3))), Outputs);
607     case S2_lsr_i_r_and:
608     case S2_lsr_i_p_and:
609       return rr0(eAND(rc(1), eLSR(rc(2), im(3))), Outputs);
610     case S2_lsr_i_r_or:
611     case S2_lsr_i_p_or:
612       return rr0(eORL(rc(1), eLSR(rc(2), im(3))), Outputs);
613     case S2_lsr_i_r_xacc:
614     case S2_lsr_i_p_xacc:
615       return rr0(eXOR(rc(1), eLSR(rc(2), im(3))), Outputs);
616
617     case S2_clrbit_i: {
618       RegisterCell RC = rc(1);
619       RC[im(2)] = BT::BitValue::Zero;
620       return rr0(RC, Outputs);
621     }
622     case S2_setbit_i: {
623       RegisterCell RC = rc(1);
624       RC[im(2)] = BT::BitValue::One;
625       return rr0(RC, Outputs);
626     }
627     case S2_togglebit_i: {
628       RegisterCell RC = rc(1);
629       uint16_t BX = im(2);
630       RC[BX] = RC[BX].is(0) ? BT::BitValue::One
631                             : RC[BX].is(1) ? BT::BitValue::Zero
632                                            : BT::BitValue::self();
633       return rr0(RC, Outputs);
634     }
635
636     case A4_bitspliti: {
637       uint16_t W1 = getRegBitWidth(Reg[1]);
638       uint16_t BX = im(2);
639       // Res.uw[1] = Rs[bx+1:], Res.uw[0] = Rs[0:bx]
640       const BT::BitValue Zero = BT::BitValue::Zero;
641       RegisterCell RZ = RegisterCell(W0).fill(BX, W1, Zero)
642                                         .fill(W1+(W1-BX), W0, Zero);
643       RegisterCell BF1 = eXTR(rc(1), 0, BX), BF2 = eXTR(rc(1), BX, W1);
644       RegisterCell RC = eINS(eINS(RZ, BF1, 0), BF2, W1);
645       return rr0(RC, Outputs);
646     }
647     case S4_extract:
648     case S4_extractp:
649     case S2_extractu:
650     case S2_extractup: {
651       uint16_t Wd = im(2), Of = im(3);
652       assert(Wd <= W0);
653       if (Wd == 0)
654         return rr0(eIMM(0, W0), Outputs);
655       // If the width extends beyond the register size, pad the register
656       // with 0 bits.
657       RegisterCell Pad = (Wd+Of > W0) ? rc(1).cat(eIMM(0, Wd+Of-W0)) : rc(1);
658       RegisterCell Ext = eXTR(Pad, Of, Wd+Of);
659       // Ext is short, need to extend it with 0s or sign bit.
660       RegisterCell RC = RegisterCell(W0).insert(Ext, BT::BitMask(0, Wd-1));
661       if (Opc == S2_extractu || Opc == S2_extractup)
662         return rr0(eZXT(RC, Wd), Outputs);
663       return rr0(eSXT(RC, Wd), Outputs);
664     }
665     case S2_insert:
666     case S2_insertp: {
667       uint16_t Wd = im(3), Of = im(4);
668       assert(Wd < W0 && Of < W0);
669       // If Wd+Of exceeds W0, the inserted bits are truncated.
670       if (Wd+Of > W0)
671         Wd = W0-Of;
672       if (Wd == 0)
673         return rr0(rc(1), Outputs);
674       return rr0(eINS(rc(1), eXTR(rc(2), 0, Wd), Of), Outputs);
675     }
676
677     // Bit permutations:
678
679     case A2_combineii:
680     case A4_combineii:
681     case A4_combineir:
682     case A4_combineri:
683     case A2_combinew:
684     case V6_vcombine:
685     case V6_vcombine_128B:
686       assert(W0 % 2 == 0);
687       return rr0(cop(2, W0/2).cat(cop(1, W0/2)), Outputs);
688     case A2_combine_ll:
689     case A2_combine_lh:
690     case A2_combine_hl:
691     case A2_combine_hh: {
692       assert(W0 == 32);
693       assert(getRegBitWidth(Reg[1]) == 32 && getRegBitWidth(Reg[2]) == 32);
694       // Low half in the output is 0 for _ll and _hl, 1 otherwise:
695       unsigned LoH = !(Opc == A2_combine_ll || Opc == A2_combine_hl);
696       // High half in the output is 0 for _ll and _lh, 1 otherwise:
697       unsigned HiH = !(Opc == A2_combine_ll || Opc == A2_combine_lh);
698       RegisterCell R1 = rc(1);
699       RegisterCell R2 = rc(2);
700       RegisterCell RC = half(R2, LoH).cat(half(R1, HiH));
701       return rr0(RC, Outputs);
702     }
703     case S2_packhl: {
704       assert(W0 == 64);
705       assert(getRegBitWidth(Reg[1]) == 32 && getRegBitWidth(Reg[2]) == 32);
706       RegisterCell R1 = rc(1);
707       RegisterCell R2 = rc(2);
708       RegisterCell RC = half(R2, 0).cat(half(R1, 0)).cat(half(R2, 1))
709                                    .cat(half(R1, 1));
710       return rr0(RC, Outputs);
711     }
712     case S2_shuffeb: {
713       RegisterCell RC = shuffle(rc(1), rc(2), 8, false);
714       return rr0(RC, Outputs);
715     }
716     case S2_shuffeh: {
717       RegisterCell RC = shuffle(rc(1), rc(2), 16, false);
718       return rr0(RC, Outputs);
719     }
720     case S2_shuffob: {
721       RegisterCell RC = shuffle(rc(1), rc(2), 8, true);
722       return rr0(RC, Outputs);
723     }
724     case S2_shuffoh: {
725       RegisterCell RC = shuffle(rc(1), rc(2), 16, true);
726       return rr0(RC, Outputs);
727     }
728     case C2_mask: {
729       uint16_t WR = W0;
730       uint16_t WP = 8; // XXX Pred size: getRegBitWidth(Reg[1]);
731       assert(WR == 64 && WP == 8);
732       RegisterCell R1 = rc(1);
733       RegisterCell RC(WR);
734       for (uint16_t i = 0; i < WP; ++i) {
735         const BT::BitValue &V = R1[i];
736         BT::BitValue F = (V.is(0) || V.is(1)) ? V : BT::BitValue::self();
737         RC.fill(i*8, i*8+8, F);
738       }
739       return rr0(RC, Outputs);
740     }
741
742     // Mux:
743
744     case C2_muxii:
745     case C2_muxir:
746     case C2_muxri:
747     case C2_mux: {
748       BT::BitValue PC0 = rc(1)[0];
749       RegisterCell R2 = cop(2, W0);
750       RegisterCell R3 = cop(3, W0);
751       if (PC0.is(0) || PC0.is(1))
752         return rr0(RegisterCell::ref(PC0 ? R2 : R3), Outputs);
753       R2.meet(R3, Reg[0].Reg);
754       return rr0(R2, Outputs);
755     }
756     case C2_vmux:
757       // TODO
758       break;
759
760     // Sign- and zero-extension:
761
762     case A2_sxtb:
763       return rr0(eSXT(rc(1), 8), Outputs);
764     case A2_sxth:
765       return rr0(eSXT(rc(1), 16), Outputs);
766     case A2_sxtw: {
767       uint16_t W1 = getRegBitWidth(Reg[1]);
768       assert(W0 == 64 && W1 == 32);
769       RegisterCell RC = eSXT(rc(1).cat(eIMM(0, W1)), W1);
770       return rr0(RC, Outputs);
771     }
772     case A2_zxtb:
773       return rr0(eZXT(rc(1), 8), Outputs);
774     case A2_zxth:
775       return rr0(eZXT(rc(1), 16), Outputs);
776
777     // Bit count:
778
779     case S2_cl0:
780     case S2_cl0p:
781       // Always produce a 32-bit result.
782       return rr0(eCLB(rc(1), 0/*bit*/, 32), Outputs);
783     case S2_cl1:
784     case S2_cl1p:
785       return rr0(eCLB(rc(1), 1/*bit*/, 32), Outputs);
786     case S2_clb:
787     case S2_clbp: {
788       uint16_t W1 = getRegBitWidth(Reg[1]);
789       RegisterCell R1 = rc(1);
790       BT::BitValue TV = R1[W1-1];
791       if (TV.is(0) || TV.is(1))
792         return rr0(eCLB(R1, TV, 32), Outputs);
793       break;
794     }
795     case S2_ct0:
796     case S2_ct0p:
797       return rr0(eCTB(rc(1), 0/*bit*/, 32), Outputs);
798     case S2_ct1:
799     case S2_ct1p:
800       return rr0(eCTB(rc(1), 1/*bit*/, 32), Outputs);
801     case S5_popcountp:
802       // TODO
803       break;
804
805     case C2_all8: {
806       RegisterCell P1 = rc(1);
807       bool Has0 = false, All1 = true;
808       for (uint16_t i = 0; i < 8/*XXX*/; ++i) {
809         if (!P1[i].is(1))
810           All1 = false;
811         if (!P1[i].is(0))
812           continue;
813         Has0 = true;
814         break;
815       }
816       if (!Has0 && !All1)
817         break;
818       RegisterCell RC(W0);
819       RC.fill(0, W0, (All1 ? BT::BitValue::One : BT::BitValue::Zero));
820       return rr0(RC, Outputs);
821     }
822     case C2_any8: {
823       RegisterCell P1 = rc(1);
824       bool Has1 = false, All0 = true;
825       for (uint16_t i = 0; i < 8/*XXX*/; ++i) {
826         if (!P1[i].is(0))
827           All0 = false;
828         if (!P1[i].is(1))
829           continue;
830         Has1 = true;
831         break;
832       }
833       if (!Has1 && !All0)
834         break;
835       RegisterCell RC(W0);
836       RC.fill(0, W0, (Has1 ? BT::BitValue::One : BT::BitValue::Zero));
837       return rr0(RC, Outputs);
838     }
839     case C2_and:
840       return rr0(eAND(rc(1), rc(2)), Outputs);
841     case C2_andn:
842       return rr0(eAND(rc(1), eNOT(rc(2))), Outputs);
843     case C2_not:
844       return rr0(eNOT(rc(1)), Outputs);
845     case C2_or:
846       return rr0(eORL(rc(1), rc(2)), Outputs);
847     case C2_orn:
848       return rr0(eORL(rc(1), eNOT(rc(2))), Outputs);
849     case C2_xor:
850       return rr0(eXOR(rc(1), rc(2)), Outputs);
851     case C4_and_and:
852       return rr0(eAND(rc(1), eAND(rc(2), rc(3))), Outputs);
853     case C4_and_andn:
854       return rr0(eAND(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs);
855     case C4_and_or:
856       return rr0(eAND(rc(1), eORL(rc(2), rc(3))), Outputs);
857     case C4_and_orn:
858       return rr0(eAND(rc(1), eORL(rc(2), eNOT(rc(3)))), Outputs);
859     case C4_or_and:
860       return rr0(eORL(rc(1), eAND(rc(2), rc(3))), Outputs);
861     case C4_or_andn:
862       return rr0(eORL(rc(1), eAND(rc(2), eNOT(rc(3)))), Outputs);
863     case C4_or_or:
864       return rr0(eORL(rc(1), eORL(rc(2), rc(3))), Outputs);
865     case C4_or_orn:
866       return rr0(eORL(rc(1), eORL(rc(2), eNOT(rc(3)))), Outputs);
867     case C2_bitsclr:
868     case C2_bitsclri:
869     case C2_bitsset:
870     case C4_nbitsclr:
871     case C4_nbitsclri:
872     case C4_nbitsset:
873       // TODO
874       break;
875     case S2_tstbit_i:
876     case S4_ntstbit_i: {
877       BT::BitValue V = rc(1)[im(2)];
878       if (V.is(0) || V.is(1)) {
879         // If instruction is S2_tstbit_i, test for 1, otherwise test for 0.
880         bool TV = (Opc == S2_tstbit_i);
881         BT::BitValue F = V.is(TV) ? BT::BitValue::One : BT::BitValue::Zero;
882         return rr0(RegisterCell(W0).fill(0, W0, F), Outputs);
883       }
884       break;
885     }
886
887     default:
888       return MachineEvaluator::evaluate(MI, Inputs, Outputs);
889   }
890   #undef im
891   #undef rc
892   #undef op
893   return false;
894 }
895
896 bool HexagonEvaluator::evaluate(const MachineInstr &BI,
897                                 const CellMapType &Inputs,
898                                 BranchTargetList &Targets,
899                                 bool &FallsThru) const {
900   // We need to evaluate one branch at a time. TII::analyzeBranch checks
901   // all the branches in a basic block at once, so we cannot use it.
902   unsigned Opc = BI.getOpcode();
903   bool SimpleBranch = false;
904   bool Negated = false;
905   switch (Opc) {
906     case Hexagon::J2_jumpf:
907     case Hexagon::J2_jumpfpt:
908     case Hexagon::J2_jumpfnew:
909     case Hexagon::J2_jumpfnewpt:
910       Negated = true;
911     case Hexagon::J2_jumpt:
912     case Hexagon::J2_jumptpt:
913     case Hexagon::J2_jumptnew:
914     case Hexagon::J2_jumptnewpt:
915       // Simple branch:  if([!]Pn) jump ...
916       // i.e. Op0 = predicate, Op1 = branch target.
917       SimpleBranch = true;
918       break;
919     case Hexagon::J2_jump:
920       Targets.insert(BI.getOperand(0).getMBB());
921       FallsThru = false;
922       return true;
923     default:
924       // If the branch is of unknown type, assume that all successors are
925       // executable.
926       return false;
927   }
928
929   if (!SimpleBranch)
930     return false;
931
932   // BI is a conditional branch if we got here.
933   RegisterRef PR = BI.getOperand(0);
934   RegisterCell PC = getCell(PR, Inputs);
935   const BT::BitValue &Test = PC[0];
936
937   // If the condition is neither true nor false, then it's unknown.
938   if (!Test.is(0) && !Test.is(1))
939     return false;
940
941   // "Test.is(!Negated)" means "branch condition is true".
942   if (!Test.is(!Negated)) {
943     // Condition known to be false.
944     FallsThru = true;
945     return true;
946   }
947
948   Targets.insert(BI.getOperand(1).getMBB());
949   FallsThru = false;
950   return true;
951 }
952
953 bool HexagonEvaluator::evaluateLoad(const MachineInstr &MI,
954                                     const CellMapType &Inputs,
955                                     CellMapType &Outputs) const {
956   if (TII.isPredicated(MI))
957     return false;
958   assert(MI.mayLoad() && "A load that mayn't?");
959   unsigned Opc = MI.getOpcode();
960
961   uint16_t BitNum;
962   bool SignEx;
963   using namespace Hexagon;
964
965   switch (Opc) {
966     default:
967       return false;
968
969 #if 0
970     // memb_fifo
971     case L2_loadalignb_pbr:
972     case L2_loadalignb_pcr:
973     case L2_loadalignb_pi:
974     // memh_fifo
975     case L2_loadalignh_pbr:
976     case L2_loadalignh_pcr:
977     case L2_loadalignh_pi:
978     // membh
979     case L2_loadbsw2_pbr:
980     case L2_loadbsw2_pci:
981     case L2_loadbsw2_pcr:
982     case L2_loadbsw2_pi:
983     case L2_loadbsw4_pbr:
984     case L2_loadbsw4_pci:
985     case L2_loadbsw4_pcr:
986     case L2_loadbsw4_pi:
987     // memubh
988     case L2_loadbzw2_pbr:
989     case L2_loadbzw2_pci:
990     case L2_loadbzw2_pcr:
991     case L2_loadbzw2_pi:
992     case L2_loadbzw4_pbr:
993     case L2_loadbzw4_pci:
994     case L2_loadbzw4_pcr:
995     case L2_loadbzw4_pi:
996 #endif
997
998     case L2_loadrbgp:
999     case L2_loadrb_io:
1000     case L2_loadrb_pbr:
1001     case L2_loadrb_pci:
1002     case L2_loadrb_pcr:
1003     case L2_loadrb_pi:
1004     case PS_loadrbabs:
1005     case L4_loadrb_ap:
1006     case L4_loadrb_rr:
1007     case L4_loadrb_ur:
1008       BitNum = 8;
1009       SignEx = true;
1010       break;
1011
1012     case L2_loadrubgp:
1013     case L2_loadrub_io:
1014     case L2_loadrub_pbr:
1015     case L2_loadrub_pci:
1016     case L2_loadrub_pcr:
1017     case L2_loadrub_pi:
1018     case PS_loadrubabs:
1019     case L4_loadrub_ap:
1020     case L4_loadrub_rr:
1021     case L4_loadrub_ur:
1022       BitNum = 8;
1023       SignEx = false;
1024       break;
1025
1026     case L2_loadrhgp:
1027     case L2_loadrh_io:
1028     case L2_loadrh_pbr:
1029     case L2_loadrh_pci:
1030     case L2_loadrh_pcr:
1031     case L2_loadrh_pi:
1032     case PS_loadrhabs:
1033     case L4_loadrh_ap:
1034     case L4_loadrh_rr:
1035     case L4_loadrh_ur:
1036       BitNum = 16;
1037       SignEx = true;
1038       break;
1039
1040     case L2_loadruhgp:
1041     case L2_loadruh_io:
1042     case L2_loadruh_pbr:
1043     case L2_loadruh_pci:
1044     case L2_loadruh_pcr:
1045     case L2_loadruh_pi:
1046     case L4_loadruh_rr:
1047     case PS_loadruhabs:
1048     case L4_loadruh_ap:
1049     case L4_loadruh_ur:
1050       BitNum = 16;
1051       SignEx = false;
1052       break;
1053
1054     case L2_loadrigp:
1055     case L2_loadri_io:
1056     case L2_loadri_pbr:
1057     case L2_loadri_pci:
1058     case L2_loadri_pcr:
1059     case L2_loadri_pi:
1060     case L2_loadw_locked:
1061     case PS_loadriabs:
1062     case L4_loadri_ap:
1063     case L4_loadri_rr:
1064     case L4_loadri_ur:
1065     case LDriw_pred:
1066       BitNum = 32;
1067       SignEx = true;
1068       break;
1069
1070     case L2_loadrdgp:
1071     case L2_loadrd_io:
1072     case L2_loadrd_pbr:
1073     case L2_loadrd_pci:
1074     case L2_loadrd_pcr:
1075     case L2_loadrd_pi:
1076     case L4_loadd_locked:
1077     case PS_loadrdabs:
1078     case L4_loadrd_ap:
1079     case L4_loadrd_rr:
1080     case L4_loadrd_ur:
1081       BitNum = 64;
1082       SignEx = true;
1083       break;
1084   }
1085
1086   const MachineOperand &MD = MI.getOperand(0);
1087   assert(MD.isReg() && MD.isDef());
1088   RegisterRef RD = MD;
1089
1090   uint16_t W = getRegBitWidth(RD);
1091   assert(W >= BitNum && BitNum > 0);
1092   RegisterCell Res(W);
1093
1094   for (uint16_t i = 0; i < BitNum; ++i)
1095     Res[i] = BT::BitValue::self(BT::BitRef(RD.Reg, i));
1096
1097   if (SignEx) {
1098     const BT::BitValue &Sign = Res[BitNum-1];
1099     for (uint16_t i = BitNum; i < W; ++i)
1100       Res[i] = BT::BitValue::ref(Sign);
1101   } else {
1102     for (uint16_t i = BitNum; i < W; ++i)
1103       Res[i] = BT::BitValue::Zero;
1104   }
1105
1106   putCell(RD, Res, Outputs);
1107   return true;
1108 }
1109
1110 bool HexagonEvaluator::evaluateFormalCopy(const MachineInstr &MI,
1111                                           const CellMapType &Inputs,
1112                                           CellMapType &Outputs) const {
1113   // If MI defines a formal parameter, but is not a copy (loads are handled
1114   // in evaluateLoad), then it's not clear what to do.
1115   assert(MI.isCopy());
1116
1117   RegisterRef RD = MI.getOperand(0);
1118   RegisterRef RS = MI.getOperand(1);
1119   assert(RD.Sub == 0);
1120   if (!TargetRegisterInfo::isPhysicalRegister(RS.Reg))
1121     return false;
1122   RegExtMap::const_iterator F = VRX.find(RD.Reg);
1123   if (F == VRX.end())
1124     return false;
1125
1126   uint16_t EW = F->second.Width;
1127   // Store RD's cell into the map. This will associate the cell with a virtual
1128   // register, and make zero-/sign-extends possible (otherwise we would be ex-
1129   // tending "self" bit values, which will have no effect, since "self" values
1130   // cannot be references to anything).
1131   putCell(RD, getCell(RS, Inputs), Outputs);
1132
1133   RegisterCell Res;
1134   // Read RD's cell from the outputs instead of RS's cell from the inputs:
1135   if (F->second.Type == ExtType::SExt)
1136     Res = eSXT(getCell(RD, Outputs), EW);
1137   else if (F->second.Type == ExtType::ZExt)
1138     Res = eZXT(getCell(RD, Outputs), EW);
1139
1140   putCell(RD, Res, Outputs);
1141   return true;
1142 }
1143
1144
1145 unsigned HexagonEvaluator::getNextPhysReg(unsigned PReg, unsigned Width) const {
1146   using namespace Hexagon;
1147   bool Is64 = DoubleRegsRegClass.contains(PReg);
1148   assert(PReg == 0 || Is64 || IntRegsRegClass.contains(PReg));
1149
1150   static const unsigned Phys32[] = { R0, R1, R2, R3, R4, R5 };
1151   static const unsigned Phys64[] = { D0, D1, D2 };
1152   const unsigned Num32 = sizeof(Phys32)/sizeof(unsigned);
1153   const unsigned Num64 = sizeof(Phys64)/sizeof(unsigned);
1154
1155   // Return the first parameter register of the required width.
1156   if (PReg == 0)
1157     return (Width <= 32) ? Phys32[0] : Phys64[0];
1158
1159   // Set Idx32, Idx64 in such a way that Idx+1 would give the index of the
1160   // next register.
1161   unsigned Idx32 = 0, Idx64 = 0;
1162   if (!Is64) {
1163     while (Idx32 < Num32) {
1164       if (Phys32[Idx32] == PReg)
1165         break;
1166       Idx32++;
1167     }
1168     Idx64 = Idx32/2;
1169   } else {
1170     while (Idx64 < Num64) {
1171       if (Phys64[Idx64] == PReg)
1172         break;
1173       Idx64++;
1174     }
1175     Idx32 = Idx64*2+1;
1176   }
1177
1178   if (Width <= 32)
1179     return (Idx32+1 < Num32) ? Phys32[Idx32+1] : 0;
1180   return (Idx64+1 < Num64) ? Phys64[Idx64+1] : 0;
1181 }
1182
1183
1184 unsigned HexagonEvaluator::getVirtRegFor(unsigned PReg) const {
1185   typedef MachineRegisterInfo::livein_iterator iterator;
1186   for (iterator I = MRI.livein_begin(), E = MRI.livein_end(); I != E; ++I) {
1187     if (I->first == PReg)
1188       return I->second;
1189   }
1190   return 0;
1191 }