1 //===-- AVRExpandPseudoInsts.cpp - Expand pseudo instructions -------------===//
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 // This file contains a pass that expands pseudo instructions into target
10 // instructions. This pass should be run after register allocation but before
11 // the post-regalloc scheduling pass.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "AVR.h"
16 #include "AVRInstrInfo.h"
17 #include "AVRTargetMachine.h"
18 #include "MCTargetDesc/AVRMCTargetDesc.h"
19 
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/RegisterScavenging.h"
24 #include "llvm/CodeGen/TargetRegisterInfo.h"
25 
26 using namespace llvm;
27 
28 #define AVR_EXPAND_PSEUDO_NAME "AVR pseudo instruction expansion pass"
29 
30 namespace {
31 
32 /// Expands "placeholder" instructions marked as pseudo into
33 /// actual AVR instructions.
34 class AVRExpandPseudo : public MachineFunctionPass {
35 public:
36   static char ID;
37 
38   AVRExpandPseudo() : MachineFunctionPass(ID) {
39     initializeAVRExpandPseudoPass(*PassRegistry::getPassRegistry());
40   }
41 
42   bool runOnMachineFunction(MachineFunction &MF) override;
43 
44   StringRef getPassName() const override { return AVR_EXPAND_PSEUDO_NAME; }
45 
46 private:
47   typedef MachineBasicBlock Block;
48   typedef Block::iterator BlockIt;
49 
50   const AVRRegisterInfo *TRI;
51   const TargetInstrInfo *TII;
52 
53   /// The register to be used for temporary storage.
54   const Register SCRATCH_REGISTER = AVR::R0;
55   /// The register that will always contain zero.
56   const Register ZERO_REGISTER = AVR::R1;
57 
58   bool expandMBB(Block &MBB);
59   bool expandMI(Block &MBB, BlockIt MBBI);
60   template <unsigned OP> bool expand(Block &MBB, BlockIt MBBI);
61 
62   MachineInstrBuilder buildMI(Block &MBB, BlockIt MBBI, unsigned Opcode) {
63     return BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(Opcode));
64   }
65 
66   MachineInstrBuilder buildMI(Block &MBB, BlockIt MBBI, unsigned Opcode,
67                               Register DstReg) {
68     return BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(Opcode), DstReg);
69   }
70 
71   MachineRegisterInfo &getRegInfo(Block &MBB) {
72     return MBB.getParent()->getRegInfo();
73   }
74 
75   bool expandArith(unsigned OpLo, unsigned OpHi, Block &MBB, BlockIt MBBI);
76   bool expandLogic(unsigned Op, Block &MBB, BlockIt MBBI);
77   bool expandLogicImm(unsigned Op, Block &MBB, BlockIt MBBI);
78   bool isLogicImmOpRedundant(unsigned Op, unsigned ImmVal) const;
79 
80   template <typename Func> bool expandAtomic(Block &MBB, BlockIt MBBI, Func f);
81 
82   template <typename Func>
83   bool expandAtomicBinaryOp(unsigned Opcode, Block &MBB, BlockIt MBBI, Func f);
84 
85   bool expandAtomicBinaryOp(unsigned Opcode, Block &MBB, BlockIt MBBI);
86 
87   /// Specific shift implementation.
88   bool expandLSLB7Rd(Block &MBB, BlockIt MBBI);
89   bool expandLSRB7Rd(Block &MBB, BlockIt MBBI);
90   bool expandASRB6Rd(Block &MBB, BlockIt MBBI);
91   bool expandASRB7Rd(Block &MBB, BlockIt MBBI);
92   bool expandLSLW4Rd(Block &MBB, BlockIt MBBI);
93   bool expandLSRW4Rd(Block &MBB, BlockIt MBBI);
94   bool expandLSLW8Rd(Block &MBB, BlockIt MBBI);
95   bool expandLSRW8Rd(Block &MBB, BlockIt MBBI);
96   bool expandASRW8Rd(Block &MBB, BlockIt MBBI);
97   bool expandLSLW12Rd(Block &MBB, BlockIt MBBI);
98   bool expandLSRW12Rd(Block &MBB, BlockIt MBBI);
99 
100   // Common implementation of LPMWRdZ and ELPMWRdZ.
101   bool expandLPMWELPMW(Block &MBB, BlockIt MBBI, bool IsExt);
102 
103   /// Scavenges a free GPR8 register for use.
104   Register scavengeGPR8(MachineInstr &MI);
105 };
106 
107 char AVRExpandPseudo::ID = 0;
108 
109 bool AVRExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
110   bool Modified = false;
111 
112   BlockIt MBBI = MBB.begin(), E = MBB.end();
113   while (MBBI != E) {
114     BlockIt NMBBI = std::next(MBBI);
115     Modified |= expandMI(MBB, MBBI);
116     MBBI = NMBBI;
117   }
118 
119   return Modified;
120 }
121 
122 bool AVRExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
123   bool Modified = false;
124 
125   const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
126   TRI = STI.getRegisterInfo();
127   TII = STI.getInstrInfo();
128 
129   // We need to track liveness in order to use register scavenging.
130   MF.getProperties().set(MachineFunctionProperties::Property::TracksLiveness);
131 
132   for (Block &MBB : MF) {
133     bool ContinueExpanding = true;
134     unsigned ExpandCount = 0;
135 
136     // Continue expanding the block until all pseudos are expanded.
137     do {
138       assert(ExpandCount < 10 && "pseudo expand limit reached");
139 
140       bool BlockModified = expandMBB(MBB);
141       Modified |= BlockModified;
142       ExpandCount++;
143 
144       ContinueExpanding = BlockModified;
145     } while (ContinueExpanding);
146   }
147 
148   return Modified;
149 }
150 
151 bool AVRExpandPseudo::expandArith(unsigned OpLo, unsigned OpHi, Block &MBB,
152                                   BlockIt MBBI) {
153   MachineInstr &MI = *MBBI;
154   Register SrcLoReg, SrcHiReg, DstLoReg, DstHiReg;
155   Register DstReg = MI.getOperand(0).getReg();
156   Register SrcReg = MI.getOperand(2).getReg();
157   bool DstIsDead = MI.getOperand(0).isDead();
158   bool DstIsKill = MI.getOperand(1).isKill();
159   bool SrcIsKill = MI.getOperand(2).isKill();
160   bool ImpIsDead = MI.getOperand(3).isDead();
161   TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
162   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
163 
164   buildMI(MBB, MBBI, OpLo)
165       .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
166       .addReg(DstLoReg, getKillRegState(DstIsKill))
167       .addReg(SrcLoReg, getKillRegState(SrcIsKill));
168 
169   auto MIBHI =
170       buildMI(MBB, MBBI, OpHi)
171           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
172           .addReg(DstHiReg, getKillRegState(DstIsKill))
173           .addReg(SrcHiReg, getKillRegState(SrcIsKill));
174 
175   if (ImpIsDead)
176     MIBHI->getOperand(3).setIsDead();
177 
178   // SREG is always implicitly killed
179   MIBHI->getOperand(4).setIsKill();
180 
181   MI.eraseFromParent();
182   return true;
183 }
184 
185 bool AVRExpandPseudo::expandLogic(unsigned Op, Block &MBB, BlockIt MBBI) {
186   MachineInstr &MI = *MBBI;
187   Register SrcLoReg, SrcHiReg, DstLoReg, DstHiReg;
188   Register DstReg = MI.getOperand(0).getReg();
189   Register SrcReg = MI.getOperand(2).getReg();
190   bool DstIsDead = MI.getOperand(0).isDead();
191   bool DstIsKill = MI.getOperand(1).isKill();
192   bool SrcIsKill = MI.getOperand(2).isKill();
193   bool ImpIsDead = MI.getOperand(3).isDead();
194   TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
195   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
196 
197   auto MIBLO =
198       buildMI(MBB, MBBI, Op)
199           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
200           .addReg(DstLoReg, getKillRegState(DstIsKill))
201           .addReg(SrcLoReg, getKillRegState(SrcIsKill));
202 
203   // SREG is always implicitly dead
204   MIBLO->getOperand(3).setIsDead();
205 
206   auto MIBHI =
207       buildMI(MBB, MBBI, Op)
208           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
209           .addReg(DstHiReg, getKillRegState(DstIsKill))
210           .addReg(SrcHiReg, getKillRegState(SrcIsKill));
211 
212   if (ImpIsDead)
213     MIBHI->getOperand(3).setIsDead();
214 
215   MI.eraseFromParent();
216   return true;
217 }
218 
219 bool AVRExpandPseudo::isLogicImmOpRedundant(unsigned Op,
220                                             unsigned ImmVal) const {
221 
222   // ANDI Rd, 0xff is redundant.
223   if (Op == AVR::ANDIRdK && ImmVal == 0xff)
224     return true;
225 
226   // ORI Rd, 0x0 is redundant.
227   if (Op == AVR::ORIRdK && ImmVal == 0x0)
228     return true;
229 
230   return false;
231 }
232 
233 bool AVRExpandPseudo::expandLogicImm(unsigned Op, Block &MBB, BlockIt MBBI) {
234   MachineInstr &MI = *MBBI;
235   Register DstLoReg, DstHiReg;
236   Register DstReg = MI.getOperand(0).getReg();
237   bool DstIsDead = MI.getOperand(0).isDead();
238   bool SrcIsKill = MI.getOperand(1).isKill();
239   bool ImpIsDead = MI.getOperand(3).isDead();
240   unsigned Imm = MI.getOperand(2).getImm();
241   unsigned Lo8 = Imm & 0xff;
242   unsigned Hi8 = (Imm >> 8) & 0xff;
243   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
244 
245   if (!isLogicImmOpRedundant(Op, Lo8)) {
246     auto MIBLO =
247         buildMI(MBB, MBBI, Op)
248             .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
249             .addReg(DstLoReg, getKillRegState(SrcIsKill))
250             .addImm(Lo8);
251 
252     // SREG is always implicitly dead
253     MIBLO->getOperand(3).setIsDead();
254   }
255 
256   if (!isLogicImmOpRedundant(Op, Hi8)) {
257     auto MIBHI =
258         buildMI(MBB, MBBI, Op)
259             .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
260             .addReg(DstHiReg, getKillRegState(SrcIsKill))
261             .addImm(Hi8);
262 
263     if (ImpIsDead)
264       MIBHI->getOperand(3).setIsDead();
265   }
266 
267   MI.eraseFromParent();
268   return true;
269 }
270 
271 template <>
272 bool AVRExpandPseudo::expand<AVR::ADDWRdRr>(Block &MBB, BlockIt MBBI) {
273   return expandArith(AVR::ADDRdRr, AVR::ADCRdRr, MBB, MBBI);
274 }
275 
276 template <>
277 bool AVRExpandPseudo::expand<AVR::ADCWRdRr>(Block &MBB, BlockIt MBBI) {
278   return expandArith(AVR::ADCRdRr, AVR::ADCRdRr, MBB, MBBI);
279 }
280 
281 template <>
282 bool AVRExpandPseudo::expand<AVR::SUBWRdRr>(Block &MBB, BlockIt MBBI) {
283   return expandArith(AVR::SUBRdRr, AVR::SBCRdRr, MBB, MBBI);
284 }
285 
286 template <>
287 bool AVRExpandPseudo::expand<AVR::SUBIWRdK>(Block &MBB, BlockIt MBBI) {
288   MachineInstr &MI = *MBBI;
289   Register DstLoReg, DstHiReg;
290   Register DstReg = MI.getOperand(0).getReg();
291   bool DstIsDead = MI.getOperand(0).isDead();
292   bool SrcIsKill = MI.getOperand(1).isKill();
293   bool ImpIsDead = MI.getOperand(3).isDead();
294   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
295 
296   auto MIBLO =
297       buildMI(MBB, MBBI, AVR::SUBIRdK)
298           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
299           .addReg(DstLoReg, getKillRegState(SrcIsKill));
300 
301   auto MIBHI =
302       buildMI(MBB, MBBI, AVR::SBCIRdK)
303           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
304           .addReg(DstHiReg, getKillRegState(SrcIsKill));
305 
306   switch (MI.getOperand(2).getType()) {
307   case MachineOperand::MO_GlobalAddress: {
308     const GlobalValue *GV = MI.getOperand(2).getGlobal();
309     int64_t Offs = MI.getOperand(2).getOffset();
310     unsigned TF = MI.getOperand(2).getTargetFlags();
311     MIBLO.addGlobalAddress(GV, Offs, TF | AVRII::MO_NEG | AVRII::MO_LO);
312     MIBHI.addGlobalAddress(GV, Offs, TF | AVRII::MO_NEG | AVRII::MO_HI);
313     break;
314   }
315   case MachineOperand::MO_Immediate: {
316     unsigned Imm = MI.getOperand(2).getImm();
317     MIBLO.addImm(Imm & 0xff);
318     MIBHI.addImm((Imm >> 8) & 0xff);
319     break;
320   }
321   default:
322     llvm_unreachable("Unknown operand type!");
323   }
324 
325   if (ImpIsDead)
326     MIBHI->getOperand(3).setIsDead();
327 
328   // SREG is always implicitly killed
329   MIBHI->getOperand(4).setIsKill();
330 
331   MI.eraseFromParent();
332   return true;
333 }
334 
335 template <>
336 bool AVRExpandPseudo::expand<AVR::SBCWRdRr>(Block &MBB, BlockIt MBBI) {
337   return expandArith(AVR::SBCRdRr, AVR::SBCRdRr, MBB, MBBI);
338 }
339 
340 template <>
341 bool AVRExpandPseudo::expand<AVR::SBCIWRdK>(Block &MBB, BlockIt MBBI) {
342   MachineInstr &MI = *MBBI;
343   Register DstLoReg, DstHiReg;
344   Register DstReg = MI.getOperand(0).getReg();
345   bool DstIsDead = MI.getOperand(0).isDead();
346   bool SrcIsKill = MI.getOperand(1).isKill();
347   bool ImpIsDead = MI.getOperand(3).isDead();
348   unsigned Imm = MI.getOperand(2).getImm();
349   unsigned Lo8 = Imm & 0xff;
350   unsigned Hi8 = (Imm >> 8) & 0xff;
351   unsigned OpLo = AVR::SBCIRdK;
352   unsigned OpHi = AVR::SBCIRdK;
353   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
354 
355   auto MIBLO =
356       buildMI(MBB, MBBI, OpLo)
357           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
358           .addReg(DstLoReg, getKillRegState(SrcIsKill))
359           .addImm(Lo8);
360 
361   // SREG is always implicitly killed
362   MIBLO->getOperand(4).setIsKill();
363 
364   auto MIBHI =
365       buildMI(MBB, MBBI, OpHi)
366           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
367           .addReg(DstHiReg, getKillRegState(SrcIsKill))
368           .addImm(Hi8);
369 
370   if (ImpIsDead)
371     MIBHI->getOperand(3).setIsDead();
372 
373   // SREG is always implicitly killed
374   MIBHI->getOperand(4).setIsKill();
375 
376   MI.eraseFromParent();
377   return true;
378 }
379 
380 template <>
381 bool AVRExpandPseudo::expand<AVR::ANDWRdRr>(Block &MBB, BlockIt MBBI) {
382   return expandLogic(AVR::ANDRdRr, MBB, MBBI);
383 }
384 
385 template <>
386 bool AVRExpandPseudo::expand<AVR::ANDIWRdK>(Block &MBB, BlockIt MBBI) {
387   return expandLogicImm(AVR::ANDIRdK, MBB, MBBI);
388 }
389 
390 template <>
391 bool AVRExpandPseudo::expand<AVR::ORWRdRr>(Block &MBB, BlockIt MBBI) {
392   return expandLogic(AVR::ORRdRr, MBB, MBBI);
393 }
394 
395 template <>
396 bool AVRExpandPseudo::expand<AVR::ORIWRdK>(Block &MBB, BlockIt MBBI) {
397   return expandLogicImm(AVR::ORIRdK, MBB, MBBI);
398 }
399 
400 template <>
401 bool AVRExpandPseudo::expand<AVR::EORWRdRr>(Block &MBB, BlockIt MBBI) {
402   return expandLogic(AVR::EORRdRr, MBB, MBBI);
403 }
404 
405 template <>
406 bool AVRExpandPseudo::expand<AVR::COMWRd>(Block &MBB, BlockIt MBBI) {
407   MachineInstr &MI = *MBBI;
408   Register DstLoReg, DstHiReg;
409   Register DstReg = MI.getOperand(0).getReg();
410   bool DstIsDead = MI.getOperand(0).isDead();
411   bool DstIsKill = MI.getOperand(1).isKill();
412   bool ImpIsDead = MI.getOperand(2).isDead();
413   unsigned OpLo = AVR::COMRd;
414   unsigned OpHi = AVR::COMRd;
415   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
416 
417   auto MIBLO =
418       buildMI(MBB, MBBI, OpLo)
419           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
420           .addReg(DstLoReg, getKillRegState(DstIsKill));
421 
422   // SREG is always implicitly dead
423   MIBLO->getOperand(2).setIsDead();
424 
425   auto MIBHI =
426       buildMI(MBB, MBBI, OpHi)
427           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
428           .addReg(DstHiReg, getKillRegState(DstIsKill));
429 
430   if (ImpIsDead)
431     MIBHI->getOperand(2).setIsDead();
432 
433   MI.eraseFromParent();
434   return true;
435 }
436 
437 template <>
438 bool AVRExpandPseudo::expand<AVR::NEGWRd>(Block &MBB, BlockIt MBBI) {
439   MachineInstr &MI = *MBBI;
440   Register DstLoReg, DstHiReg;
441   Register DstReg = MI.getOperand(0).getReg();
442   bool DstIsDead = MI.getOperand(0).isDead();
443   bool DstIsKill = MI.getOperand(1).isKill();
444   bool ImpIsDead = MI.getOperand(2).isDead();
445   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
446 
447   // Do NEG on the upper byte.
448   auto MIBHI =
449       buildMI(MBB, MBBI, AVR::NEGRd)
450           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
451           .addReg(DstHiReg, getKillRegState(DstIsKill));
452   // SREG is always implicitly dead
453   MIBHI->getOperand(2).setIsDead();
454 
455   // Do NEG on the lower byte.
456   buildMI(MBB, MBBI, AVR::NEGRd)
457       .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
458       .addReg(DstLoReg, getKillRegState(DstIsKill));
459 
460   // Do an extra SBC.
461   auto MISBCI =
462       buildMI(MBB, MBBI, AVR::SBCRdRr)
463           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
464           .addReg(DstHiReg, getKillRegState(DstIsKill))
465           .addReg(ZERO_REGISTER);
466   if (ImpIsDead)
467     MISBCI->getOperand(3).setIsDead();
468   // SREG is always implicitly killed
469   MISBCI->getOperand(4).setIsKill();
470 
471   MI.eraseFromParent();
472   return true;
473 }
474 
475 template <>
476 bool AVRExpandPseudo::expand<AVR::CPWRdRr>(Block &MBB, BlockIt MBBI) {
477   MachineInstr &MI = *MBBI;
478   Register SrcLoReg, SrcHiReg, DstLoReg, DstHiReg;
479   Register DstReg = MI.getOperand(0).getReg();
480   Register SrcReg = MI.getOperand(1).getReg();
481   bool DstIsKill = MI.getOperand(0).isKill();
482   bool SrcIsKill = MI.getOperand(1).isKill();
483   bool ImpIsDead = MI.getOperand(2).isDead();
484   unsigned OpLo = AVR::CPRdRr;
485   unsigned OpHi = AVR::CPCRdRr;
486   TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
487   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
488 
489   // Low part
490   buildMI(MBB, MBBI, OpLo)
491       .addReg(DstLoReg, getKillRegState(DstIsKill))
492       .addReg(SrcLoReg, getKillRegState(SrcIsKill));
493 
494   auto MIBHI = buildMI(MBB, MBBI, OpHi)
495                    .addReg(DstHiReg, getKillRegState(DstIsKill))
496                    .addReg(SrcHiReg, getKillRegState(SrcIsKill));
497 
498   if (ImpIsDead)
499     MIBHI->getOperand(2).setIsDead();
500 
501   // SREG is always implicitly killed
502   MIBHI->getOperand(3).setIsKill();
503 
504   MI.eraseFromParent();
505   return true;
506 }
507 
508 template <>
509 bool AVRExpandPseudo::expand<AVR::CPCWRdRr>(Block &MBB, BlockIt MBBI) {
510   MachineInstr &MI = *MBBI;
511   Register SrcLoReg, SrcHiReg, DstLoReg, DstHiReg;
512   Register DstReg = MI.getOperand(0).getReg();
513   Register SrcReg = MI.getOperand(1).getReg();
514   bool DstIsKill = MI.getOperand(0).isKill();
515   bool SrcIsKill = MI.getOperand(1).isKill();
516   bool ImpIsDead = MI.getOperand(2).isDead();
517   unsigned OpLo = AVR::CPCRdRr;
518   unsigned OpHi = AVR::CPCRdRr;
519   TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
520   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
521 
522   auto MIBLO = buildMI(MBB, MBBI, OpLo)
523                    .addReg(DstLoReg, getKillRegState(DstIsKill))
524                    .addReg(SrcLoReg, getKillRegState(SrcIsKill));
525 
526   // SREG is always implicitly killed
527   MIBLO->getOperand(3).setIsKill();
528 
529   auto MIBHI = buildMI(MBB, MBBI, OpHi)
530                    .addReg(DstHiReg, getKillRegState(DstIsKill))
531                    .addReg(SrcHiReg, getKillRegState(SrcIsKill));
532 
533   if (ImpIsDead)
534     MIBHI->getOperand(2).setIsDead();
535 
536   // SREG is always implicitly killed
537   MIBHI->getOperand(3).setIsKill();
538 
539   MI.eraseFromParent();
540   return true;
541 }
542 
543 template <>
544 bool AVRExpandPseudo::expand<AVR::LDIWRdK>(Block &MBB, BlockIt MBBI) {
545   MachineInstr &MI = *MBBI;
546   Register DstLoReg, DstHiReg;
547   Register DstReg = MI.getOperand(0).getReg();
548   bool DstIsDead = MI.getOperand(0).isDead();
549   unsigned OpLo = AVR::LDIRdK;
550   unsigned OpHi = AVR::LDIRdK;
551   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
552 
553   auto MIBLO =
554       buildMI(MBB, MBBI, OpLo)
555           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead));
556 
557   auto MIBHI =
558       buildMI(MBB, MBBI, OpHi)
559           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead));
560 
561   switch (MI.getOperand(1).getType()) {
562   case MachineOperand::MO_GlobalAddress: {
563     const GlobalValue *GV = MI.getOperand(1).getGlobal();
564     int64_t Offs = MI.getOperand(1).getOffset();
565     unsigned TF = MI.getOperand(1).getTargetFlags();
566 
567     MIBLO.addGlobalAddress(GV, Offs, TF | AVRII::MO_LO);
568     MIBHI.addGlobalAddress(GV, Offs, TF | AVRII::MO_HI);
569     break;
570   }
571   case MachineOperand::MO_BlockAddress: {
572     const BlockAddress *BA = MI.getOperand(1).getBlockAddress();
573     unsigned TF = MI.getOperand(1).getTargetFlags();
574 
575     MIBLO.add(MachineOperand::CreateBA(BA, TF | AVRII::MO_LO));
576     MIBHI.add(MachineOperand::CreateBA(BA, TF | AVRII::MO_HI));
577     break;
578   }
579   case MachineOperand::MO_Immediate: {
580     unsigned Imm = MI.getOperand(1).getImm();
581 
582     MIBLO.addImm(Imm & 0xff);
583     MIBHI.addImm((Imm >> 8) & 0xff);
584     break;
585   }
586   default:
587     llvm_unreachable("Unknown operand type!");
588   }
589 
590   MI.eraseFromParent();
591   return true;
592 }
593 
594 template <>
595 bool AVRExpandPseudo::expand<AVR::LDSWRdK>(Block &MBB, BlockIt MBBI) {
596   MachineInstr &MI = *MBBI;
597   Register DstLoReg, DstHiReg;
598   Register DstReg = MI.getOperand(0).getReg();
599   bool DstIsDead = MI.getOperand(0).isDead();
600   unsigned OpLo = AVR::LDSRdK;
601   unsigned OpHi = AVR::LDSRdK;
602   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
603 
604   auto MIBLO =
605       buildMI(MBB, MBBI, OpLo)
606           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead));
607 
608   auto MIBHI =
609       buildMI(MBB, MBBI, OpHi)
610           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead));
611 
612   switch (MI.getOperand(1).getType()) {
613   case MachineOperand::MO_GlobalAddress: {
614     const GlobalValue *GV = MI.getOperand(1).getGlobal();
615     int64_t Offs = MI.getOperand(1).getOffset();
616     unsigned TF = MI.getOperand(1).getTargetFlags();
617 
618     MIBLO.addGlobalAddress(GV, Offs, TF);
619     MIBHI.addGlobalAddress(GV, Offs + 1, TF);
620     break;
621   }
622   case MachineOperand::MO_Immediate: {
623     unsigned Imm = MI.getOperand(1).getImm();
624 
625     MIBLO.addImm(Imm);
626     MIBHI.addImm(Imm + 1);
627     break;
628   }
629   default:
630     llvm_unreachable("Unknown operand type!");
631   }
632 
633   MIBLO.setMemRefs(MI.memoperands());
634   MIBHI.setMemRefs(MI.memoperands());
635 
636   MI.eraseFromParent();
637   return true;
638 }
639 
640 template <>
641 bool AVRExpandPseudo::expand<AVR::LDWRdPtr>(Block &MBB, BlockIt MBBI) {
642   MachineInstr &MI = *MBBI;
643   Register DstLoReg, DstHiReg;
644   Register DstReg = MI.getOperand(0).getReg();
645   Register TmpReg = 0; // 0 for no temporary register
646   Register SrcReg = MI.getOperand(1).getReg();
647   bool SrcIsKill = MI.getOperand(1).isKill();
648   unsigned OpLo = AVR::LDRdPtr;
649   unsigned OpHi = AVR::LDDRdPtrQ;
650   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
651 
652   // Use a temporary register if src and dst registers are the same.
653   if (DstReg == SrcReg)
654     TmpReg = scavengeGPR8(MI);
655 
656   Register CurDstLoReg = (DstReg == SrcReg) ? TmpReg : DstLoReg;
657   Register CurDstHiReg = (DstReg == SrcReg) ? TmpReg : DstHiReg;
658 
659   // Load low byte.
660   auto MIBLO = buildMI(MBB, MBBI, OpLo)
661                    .addReg(CurDstLoReg, RegState::Define)
662                    .addReg(SrcReg);
663 
664   // Push low byte onto stack if necessary.
665   if (TmpReg)
666     buildMI(MBB, MBBI, AVR::PUSHRr).addReg(TmpReg);
667 
668   // Load high byte.
669   auto MIBHI = buildMI(MBB, MBBI, OpHi)
670                    .addReg(CurDstHiReg, RegState::Define)
671                    .addReg(SrcReg, getKillRegState(SrcIsKill))
672                    .addImm(1);
673 
674   if (TmpReg) {
675     // Move the high byte into the final destination.
676     buildMI(MBB, MBBI, AVR::MOVRdRr, DstHiReg).addReg(TmpReg);
677 
678     // Move the low byte from the scratch space into the final destination.
679     buildMI(MBB, MBBI, AVR::POPRd, DstLoReg);
680   }
681 
682   MIBLO.setMemRefs(MI.memoperands());
683   MIBHI.setMemRefs(MI.memoperands());
684 
685   MI.eraseFromParent();
686   return true;
687 }
688 
689 template <>
690 bool AVRExpandPseudo::expand<AVR::LDWRdPtrPi>(Block &MBB, BlockIt MBBI) {
691   MachineInstr &MI = *MBBI;
692   Register DstLoReg, DstHiReg;
693   Register DstReg = MI.getOperand(0).getReg();
694   Register SrcReg = MI.getOperand(1).getReg();
695   bool DstIsDead = MI.getOperand(0).isDead();
696   bool SrcIsDead = MI.getOperand(1).isKill();
697   unsigned OpLo = AVR::LDRdPtrPi;
698   unsigned OpHi = AVR::LDRdPtrPi;
699   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
700 
701   assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same");
702 
703   auto MIBLO =
704       buildMI(MBB, MBBI, OpLo)
705           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
706           .addReg(SrcReg, RegState::Define)
707           .addReg(SrcReg, RegState::Kill);
708 
709   auto MIBHI =
710       buildMI(MBB, MBBI, OpHi)
711           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
712           .addReg(SrcReg, RegState::Define | getDeadRegState(SrcIsDead))
713           .addReg(SrcReg, RegState::Kill);
714 
715   MIBLO.setMemRefs(MI.memoperands());
716   MIBHI.setMemRefs(MI.memoperands());
717 
718   MI.eraseFromParent();
719   return true;
720 }
721 
722 template <>
723 bool AVRExpandPseudo::expand<AVR::LDWRdPtrPd>(Block &MBB, BlockIt MBBI) {
724   MachineInstr &MI = *MBBI;
725   Register DstLoReg, DstHiReg;
726   Register DstReg = MI.getOperand(0).getReg();
727   Register SrcReg = MI.getOperand(1).getReg();
728   bool DstIsDead = MI.getOperand(0).isDead();
729   bool SrcIsDead = MI.getOperand(1).isKill();
730   unsigned OpLo = AVR::LDRdPtrPd;
731   unsigned OpHi = AVR::LDRdPtrPd;
732   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
733 
734   assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same");
735 
736   auto MIBHI =
737       buildMI(MBB, MBBI, OpHi)
738           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
739           .addReg(SrcReg, RegState::Define)
740           .addReg(SrcReg, RegState::Kill);
741 
742   auto MIBLO =
743       buildMI(MBB, MBBI, OpLo)
744           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
745           .addReg(SrcReg, RegState::Define | getDeadRegState(SrcIsDead))
746           .addReg(SrcReg, RegState::Kill);
747 
748   MIBLO.setMemRefs(MI.memoperands());
749   MIBHI.setMemRefs(MI.memoperands());
750 
751   MI.eraseFromParent();
752   return true;
753 }
754 
755 template <>
756 bool AVRExpandPseudo::expand<AVR::LDDWRdPtrQ>(Block &MBB, BlockIt MBBI) {
757   MachineInstr &MI = *MBBI;
758   Register DstLoReg, DstHiReg;
759   Register DstReg = MI.getOperand(0).getReg();
760   Register TmpReg = 0; // 0 for no temporary register
761   Register SrcReg = MI.getOperand(1).getReg();
762   unsigned Imm = MI.getOperand(2).getImm();
763   bool SrcIsKill = MI.getOperand(1).isKill();
764   unsigned OpLo = AVR::LDDRdPtrQ;
765   unsigned OpHi = AVR::LDDRdPtrQ;
766   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
767 
768   // Since we add 1 to the Imm value for the high byte below, and 63 is the
769   // highest Imm value allowed for the instruction, 62 is the limit here.
770   assert(Imm <= 62 && "Offset is out of range");
771 
772   // Use a temporary register if src and dst registers are the same.
773   if (DstReg == SrcReg)
774     TmpReg = scavengeGPR8(MI);
775 
776   Register CurDstLoReg = (DstReg == SrcReg) ? TmpReg : DstLoReg;
777   Register CurDstHiReg = (DstReg == SrcReg) ? TmpReg : DstHiReg;
778 
779   // Load low byte.
780   auto MIBLO = buildMI(MBB, MBBI, OpLo)
781                    .addReg(CurDstLoReg, RegState::Define)
782                    .addReg(SrcReg)
783                    .addImm(Imm);
784 
785   // Push low byte onto stack if necessary.
786   if (TmpReg)
787     buildMI(MBB, MBBI, AVR::PUSHRr).addReg(TmpReg);
788 
789   // Load high byte.
790   auto MIBHI = buildMI(MBB, MBBI, OpHi)
791                    .addReg(CurDstHiReg, RegState::Define)
792                    .addReg(SrcReg, getKillRegState(SrcIsKill))
793                    .addImm(Imm + 1);
794 
795   if (TmpReg) {
796     // Move the high byte into the final destination.
797     buildMI(MBB, MBBI, AVR::MOVRdRr, DstHiReg).addReg(TmpReg);
798 
799     // Move the low byte from the scratch space into the final destination.
800     buildMI(MBB, MBBI, AVR::POPRd, DstLoReg);
801   }
802 
803   MIBLO.setMemRefs(MI.memoperands());
804   MIBHI.setMemRefs(MI.memoperands());
805 
806   MI.eraseFromParent();
807   return true;
808 }
809 
810 bool AVRExpandPseudo::expandLPMWELPMW(Block &MBB, BlockIt MBBI, bool IsExt) {
811   MachineInstr &MI = *MBBI;
812   Register DstLoReg, DstHiReg;
813   Register DstReg = MI.getOperand(0).getReg();
814   Register TmpReg = 0; // 0 for no temporary register
815   Register SrcReg = MI.getOperand(1).getReg();
816   bool SrcIsKill = MI.getOperand(1).isKill();
817   unsigned OpLo = IsExt ? AVR::ELPMRdZPi : AVR::LPMRdZPi;
818   unsigned OpHi = IsExt ? AVR::ELPMRdZ : AVR::LPMRdZ;
819   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
820 
821   // Set the I/O register RAMPZ for ELPM.
822   if (IsExt) {
823     const AVRSubtarget &STI = MBB.getParent()->getSubtarget<AVRSubtarget>();
824     Register Bank = MI.getOperand(2).getReg();
825     // out RAMPZ, rtmp
826     buildMI(MBB, MBBI, AVR::OUTARr).addImm(STI.getIORegRAMPZ()).addReg(Bank);
827   }
828 
829   // Use a temporary register if src and dst registers are the same.
830   if (DstReg == SrcReg)
831     TmpReg = scavengeGPR8(MI);
832 
833   Register CurDstLoReg = (DstReg == SrcReg) ? TmpReg : DstLoReg;
834   Register CurDstHiReg = (DstReg == SrcReg) ? TmpReg : DstHiReg;
835 
836   // Load low byte.
837   auto MIBLO = buildMI(MBB, MBBI, OpLo)
838                    .addReg(CurDstLoReg, RegState::Define)
839                    .addReg(SrcReg);
840 
841   // Push low byte onto stack if necessary.
842   if (TmpReg)
843     buildMI(MBB, MBBI, AVR::PUSHRr).addReg(TmpReg);
844 
845   // Load high byte.
846   auto MIBHI = buildMI(MBB, MBBI, OpHi)
847                    .addReg(CurDstHiReg, RegState::Define)
848                    .addReg(SrcReg, getKillRegState(SrcIsKill));
849 
850   if (TmpReg) {
851     // Move the high byte into the final destination.
852     buildMI(MBB, MBBI, AVR::MOVRdRr, DstHiReg).addReg(TmpReg);
853 
854     // Move the low byte from the scratch space into the final destination.
855     buildMI(MBB, MBBI, AVR::POPRd, DstLoReg);
856   }
857 
858   MIBLO.setMemRefs(MI.memoperands());
859   MIBHI.setMemRefs(MI.memoperands());
860 
861   MI.eraseFromParent();
862   return true;
863 }
864 
865 template <>
866 bool AVRExpandPseudo::expand<AVR::LPMWRdZ>(Block &MBB, BlockIt MBBI) {
867   return expandLPMWELPMW(MBB, MBBI, false);
868 }
869 
870 template <>
871 bool AVRExpandPseudo::expand<AVR::ELPMWRdZ>(Block &MBB, BlockIt MBBI) {
872   return expandLPMWELPMW(MBB, MBBI, true);
873 }
874 
875 template <>
876 bool AVRExpandPseudo::expand<AVR::ELPMBRdZ>(Block &MBB, BlockIt MBBI) {
877   MachineInstr &MI = *MBBI;
878   Register DstReg = MI.getOperand(0).getReg();
879   Register SrcReg = MI.getOperand(1).getReg();
880   Register BankReg = MI.getOperand(2).getReg();
881   bool SrcIsKill = MI.getOperand(1).isKill();
882   const AVRSubtarget &STI = MBB.getParent()->getSubtarget<AVRSubtarget>();
883 
884   // Set the I/O register RAMPZ for ELPM (out RAMPZ, rtmp).
885   buildMI(MBB, MBBI, AVR::OUTARr).addImm(STI.getIORegRAMPZ()).addReg(BankReg);
886 
887   // Load byte.
888   auto MILB = buildMI(MBB, MBBI, AVR::ELPMRdZ)
889                   .addReg(DstReg, RegState::Define)
890                   .addReg(SrcReg, getKillRegState(SrcIsKill));
891 
892   MILB.setMemRefs(MI.memoperands());
893 
894   MI.eraseFromParent();
895   return true;
896 }
897 
898 template <>
899 bool AVRExpandPseudo::expand<AVR::LPMWRdZPi>(Block &MBB, BlockIt MBBI) {
900   llvm_unreachable("16-bit LPMPi is unimplemented");
901 }
902 
903 template <>
904 bool AVRExpandPseudo::expand<AVR::ELPMBRdZPi>(Block &MBB, BlockIt MBBI) {
905   llvm_unreachable("byte ELPMPi is unimplemented");
906 }
907 
908 template <>
909 bool AVRExpandPseudo::expand<AVR::ELPMWRdZPi>(Block &MBB, BlockIt MBBI) {
910   llvm_unreachable("16-bit ELPMPi is unimplemented");
911 }
912 
913 template <typename Func>
914 bool AVRExpandPseudo::expandAtomic(Block &MBB, BlockIt MBBI, Func f) {
915   MachineInstr &MI = *MBBI;
916   const AVRSubtarget &STI = MBB.getParent()->getSubtarget<AVRSubtarget>();
917 
918   // Store the SREG.
919   buildMI(MBB, MBBI, AVR::INRdA)
920       .addReg(SCRATCH_REGISTER, RegState::Define)
921       .addImm(STI.getIORegSREG());
922 
923   // Disable exceptions.
924   buildMI(MBB, MBBI, AVR::BCLRs).addImm(7); // CLI
925 
926   f(MI);
927 
928   // Restore the status reg.
929   buildMI(MBB, MBBI, AVR::OUTARr)
930       .addImm(STI.getIORegSREG())
931       .addReg(SCRATCH_REGISTER);
932 
933   MI.eraseFromParent();
934   return true;
935 }
936 
937 template <typename Func>
938 bool AVRExpandPseudo::expandAtomicBinaryOp(unsigned Opcode, Block &MBB,
939                                            BlockIt MBBI, Func f) {
940   return expandAtomic(MBB, MBBI, [&](MachineInstr &MI) {
941     auto Op1 = MI.getOperand(0);
942     auto Op2 = MI.getOperand(1);
943 
944     MachineInstr &NewInst =
945         *buildMI(MBB, MBBI, Opcode).add(Op1).add(Op2).getInstr();
946     f(NewInst);
947   });
948 }
949 
950 bool AVRExpandPseudo::expandAtomicBinaryOp(unsigned Opcode, Block &MBB,
951                                            BlockIt MBBI) {
952   return expandAtomicBinaryOp(Opcode, MBB, MBBI, [](MachineInstr &MI) {});
953 }
954 
955 Register AVRExpandPseudo::scavengeGPR8(MachineInstr &MI) {
956   MachineBasicBlock &MBB = *MI.getParent();
957   RegScavenger RS;
958 
959   RS.enterBasicBlock(MBB);
960   RS.forward(MI);
961 
962   BitVector Candidates =
963       TRI->getAllocatableSet(*MBB.getParent(), &AVR::GPR8RegClass);
964 
965   // Exclude all the registers being used by the instruction.
966   for (MachineOperand &MO : MI.operands()) {
967     if (MO.isReg() && MO.getReg() != 0 && !MO.isDef() &&
968         !Register::isVirtualRegister(MO.getReg()))
969       Candidates.reset(MO.getReg());
970   }
971 
972   BitVector Available = RS.getRegsAvailable(&AVR::GPR8RegClass);
973   Available &= Candidates;
974 
975   signed Reg = Available.find_first();
976   assert(Reg != -1 && "ran out of registers");
977   return Reg;
978 }
979 
980 template <>
981 bool AVRExpandPseudo::expand<AVR::AtomicLoad8>(Block &MBB, BlockIt MBBI) {
982   return expandAtomicBinaryOp(AVR::LDRdPtr, MBB, MBBI);
983 }
984 
985 template <>
986 bool AVRExpandPseudo::expand<AVR::AtomicLoad16>(Block &MBB, BlockIt MBBI) {
987   return expandAtomicBinaryOp(AVR::LDWRdPtr, MBB, MBBI);
988 }
989 
990 template <>
991 bool AVRExpandPseudo::expand<AVR::AtomicStore8>(Block &MBB, BlockIt MBBI) {
992   return expandAtomicBinaryOp(AVR::STPtrRr, MBB, MBBI);
993 }
994 
995 template <>
996 bool AVRExpandPseudo::expand<AVR::AtomicStore16>(Block &MBB, BlockIt MBBI) {
997   return expandAtomicBinaryOp(AVR::STWPtrRr, MBB, MBBI);
998 }
999 
1000 template <>
1001 bool AVRExpandPseudo::expand<AVR::AtomicFence>(Block &MBB, BlockIt MBBI) {
1002   // On AVR, there is only one core and so atomic fences do nothing.
1003   MBBI->eraseFromParent();
1004   return true;
1005 }
1006 
1007 template <>
1008 bool AVRExpandPseudo::expand<AVR::STSWKRr>(Block &MBB, BlockIt MBBI) {
1009   MachineInstr &MI = *MBBI;
1010   Register SrcLoReg, SrcHiReg;
1011   Register SrcReg = MI.getOperand(1).getReg();
1012   bool SrcIsKill = MI.getOperand(1).isKill();
1013   unsigned OpLo = AVR::STSKRr;
1014   unsigned OpHi = AVR::STSKRr;
1015   TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1016 
1017   // Write the high byte first in case this address belongs to a special
1018   // I/O address with a special temporary register.
1019   auto MIBHI = buildMI(MBB, MBBI, OpHi);
1020   auto MIBLO = buildMI(MBB, MBBI, OpLo);
1021 
1022   switch (MI.getOperand(0).getType()) {
1023   case MachineOperand::MO_GlobalAddress: {
1024     const GlobalValue *GV = MI.getOperand(0).getGlobal();
1025     int64_t Offs = MI.getOperand(0).getOffset();
1026     unsigned TF = MI.getOperand(0).getTargetFlags();
1027 
1028     MIBLO.addGlobalAddress(GV, Offs, TF);
1029     MIBHI.addGlobalAddress(GV, Offs + 1, TF);
1030     break;
1031   }
1032   case MachineOperand::MO_Immediate: {
1033     unsigned Imm = MI.getOperand(0).getImm();
1034 
1035     MIBLO.addImm(Imm);
1036     MIBHI.addImm(Imm + 1);
1037     break;
1038   }
1039   default:
1040     llvm_unreachable("Unknown operand type!");
1041   }
1042 
1043   MIBLO.addReg(SrcLoReg, getKillRegState(SrcIsKill));
1044   MIBHI.addReg(SrcHiReg, getKillRegState(SrcIsKill));
1045 
1046   MIBLO.setMemRefs(MI.memoperands());
1047   MIBHI.setMemRefs(MI.memoperands());
1048 
1049   MI.eraseFromParent();
1050   return true;
1051 }
1052 
1053 template <>
1054 bool AVRExpandPseudo::expand<AVR::STWPtrRr>(Block &MBB, BlockIt MBBI) {
1055   MachineInstr &MI = *MBBI;
1056   Register SrcLoReg, SrcHiReg;
1057   Register DstReg = MI.getOperand(0).getReg();
1058   Register SrcReg = MI.getOperand(1).getReg();
1059   bool DstIsUndef = MI.getOperand(0).isUndef();
1060   bool SrcIsKill = MI.getOperand(1).isKill();
1061   unsigned OpLo = AVR::STPtrRr;
1062   unsigned OpHi = AVR::STDPtrQRr;
1063   TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1064 
1065   //: TODO: need to reverse this order like inw and stsw?
1066   auto MIBLO = buildMI(MBB, MBBI, OpLo)
1067                    .addReg(DstReg, getUndefRegState(DstIsUndef))
1068                    .addReg(SrcLoReg, getKillRegState(SrcIsKill));
1069 
1070   auto MIBHI = buildMI(MBB, MBBI, OpHi)
1071                    .addReg(DstReg, getUndefRegState(DstIsUndef))
1072                    .addImm(1)
1073                    .addReg(SrcHiReg, getKillRegState(SrcIsKill));
1074 
1075   MIBLO.setMemRefs(MI.memoperands());
1076   MIBHI.setMemRefs(MI.memoperands());
1077 
1078   MI.eraseFromParent();
1079   return true;
1080 }
1081 
1082 template <>
1083 bool AVRExpandPseudo::expand<AVR::STWPtrPiRr>(Block &MBB, BlockIt MBBI) {
1084   MachineInstr &MI = *MBBI;
1085   Register SrcLoReg, SrcHiReg;
1086   Register DstReg = MI.getOperand(0).getReg();
1087   Register SrcReg = MI.getOperand(2).getReg();
1088   unsigned Imm = MI.getOperand(3).getImm();
1089   bool DstIsDead = MI.getOperand(0).isDead();
1090   bool SrcIsKill = MI.getOperand(2).isKill();
1091   unsigned OpLo = AVR::STPtrPiRr;
1092   unsigned OpHi = AVR::STPtrPiRr;
1093   TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1094 
1095   assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same");
1096 
1097   auto MIBLO = buildMI(MBB, MBBI, OpLo)
1098                    .addReg(DstReg, RegState::Define)
1099                    .addReg(DstReg, RegState::Kill)
1100                    .addReg(SrcLoReg, getKillRegState(SrcIsKill))
1101                    .addImm(Imm);
1102 
1103   auto MIBHI =
1104       buildMI(MBB, MBBI, OpHi)
1105           .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1106           .addReg(DstReg, RegState::Kill)
1107           .addReg(SrcHiReg, getKillRegState(SrcIsKill))
1108           .addImm(Imm);
1109 
1110   MIBLO.setMemRefs(MI.memoperands());
1111   MIBHI.setMemRefs(MI.memoperands());
1112 
1113   MI.eraseFromParent();
1114   return true;
1115 }
1116 
1117 template <>
1118 bool AVRExpandPseudo::expand<AVR::STWPtrPdRr>(Block &MBB, BlockIt MBBI) {
1119   MachineInstr &MI = *MBBI;
1120   Register SrcLoReg, SrcHiReg;
1121   Register DstReg = MI.getOperand(0).getReg();
1122   Register SrcReg = MI.getOperand(2).getReg();
1123   unsigned Imm = MI.getOperand(3).getImm();
1124   bool DstIsDead = MI.getOperand(0).isDead();
1125   bool SrcIsKill = MI.getOperand(2).isKill();
1126   unsigned OpLo = AVR::STPtrPdRr;
1127   unsigned OpHi = AVR::STPtrPdRr;
1128   TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1129 
1130   assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same");
1131 
1132   auto MIBHI = buildMI(MBB, MBBI, OpHi)
1133                    .addReg(DstReg, RegState::Define)
1134                    .addReg(DstReg, RegState::Kill)
1135                    .addReg(SrcHiReg, getKillRegState(SrcIsKill))
1136                    .addImm(Imm);
1137 
1138   auto MIBLO =
1139       buildMI(MBB, MBBI, OpLo)
1140           .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1141           .addReg(DstReg, RegState::Kill)
1142           .addReg(SrcLoReg, getKillRegState(SrcIsKill))
1143           .addImm(Imm);
1144 
1145   MIBLO.setMemRefs(MI.memoperands());
1146   MIBHI.setMemRefs(MI.memoperands());
1147 
1148   MI.eraseFromParent();
1149   return true;
1150 }
1151 
1152 template <>
1153 bool AVRExpandPseudo::expand<AVR::STDWPtrQRr>(Block &MBB, BlockIt MBBI) {
1154   MachineInstr &MI = *MBBI;
1155   Register SrcLoReg, SrcHiReg;
1156   Register DstReg = MI.getOperand(0).getReg();
1157   Register SrcReg = MI.getOperand(2).getReg();
1158   unsigned Imm = MI.getOperand(1).getImm();
1159   bool DstIsKill = MI.getOperand(0).isKill();
1160   bool SrcIsKill = MI.getOperand(2).isKill();
1161   unsigned OpLo = AVR::STDPtrQRr;
1162   unsigned OpHi = AVR::STDPtrQRr;
1163   TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1164 
1165   // Since we add 1 to the Imm value for the high byte below, and 63 is the
1166   // highest Imm value allowed for the instruction, 62 is the limit here.
1167   assert(Imm <= 62 && "Offset is out of range");
1168 
1169   auto MIBLO = buildMI(MBB, MBBI, OpLo)
1170                    .addReg(DstReg)
1171                    .addImm(Imm)
1172                    .addReg(SrcLoReg, getKillRegState(SrcIsKill));
1173 
1174   auto MIBHI = buildMI(MBB, MBBI, OpHi)
1175                    .addReg(DstReg, getKillRegState(DstIsKill))
1176                    .addImm(Imm + 1)
1177                    .addReg(SrcHiReg, getKillRegState(SrcIsKill));
1178 
1179   MIBLO.setMemRefs(MI.memoperands());
1180   MIBHI.setMemRefs(MI.memoperands());
1181 
1182   MI.eraseFromParent();
1183   return true;
1184 }
1185 
1186 template <>
1187 bool AVRExpandPseudo::expand<AVR::INWRdA>(Block &MBB, BlockIt MBBI) {
1188   MachineInstr &MI = *MBBI;
1189   Register DstLoReg, DstHiReg;
1190   unsigned Imm = MI.getOperand(1).getImm();
1191   Register DstReg = MI.getOperand(0).getReg();
1192   bool DstIsDead = MI.getOperand(0).isDead();
1193   unsigned OpLo = AVR::INRdA;
1194   unsigned OpHi = AVR::INRdA;
1195   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1196 
1197   // Since we add 1 to the Imm value for the high byte below, and 63 is the
1198   // highest Imm value allowed for the instruction, 62 is the limit here.
1199   assert(Imm <= 62 && "Address is out of range");
1200 
1201   auto MIBLO =
1202       buildMI(MBB, MBBI, OpLo)
1203           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1204           .addImm(Imm);
1205 
1206   auto MIBHI =
1207       buildMI(MBB, MBBI, OpHi)
1208           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1209           .addImm(Imm + 1);
1210 
1211   MIBLO.setMemRefs(MI.memoperands());
1212   MIBHI.setMemRefs(MI.memoperands());
1213 
1214   MI.eraseFromParent();
1215   return true;
1216 }
1217 
1218 template <>
1219 bool AVRExpandPseudo::expand<AVR::OUTWARr>(Block &MBB, BlockIt MBBI) {
1220   MachineInstr &MI = *MBBI;
1221   Register SrcLoReg, SrcHiReg;
1222   unsigned Imm = MI.getOperand(0).getImm();
1223   Register SrcReg = MI.getOperand(1).getReg();
1224   bool SrcIsKill = MI.getOperand(1).isKill();
1225   unsigned OpLo = AVR::OUTARr;
1226   unsigned OpHi = AVR::OUTARr;
1227   TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1228 
1229   // Since we add 1 to the Imm value for the high byte below, and 63 is the
1230   // highest Imm value allowed for the instruction, 62 is the limit here.
1231   assert(Imm <= 62 && "Address is out of range");
1232 
1233   // 16 bit I/O writes need the high byte first
1234   auto MIBHI = buildMI(MBB, MBBI, OpHi)
1235                    .addImm(Imm + 1)
1236                    .addReg(SrcHiReg, getKillRegState(SrcIsKill));
1237 
1238   auto MIBLO = buildMI(MBB, MBBI, OpLo)
1239                    .addImm(Imm)
1240                    .addReg(SrcLoReg, getKillRegState(SrcIsKill));
1241 
1242   MIBLO.setMemRefs(MI.memoperands());
1243   MIBHI.setMemRefs(MI.memoperands());
1244 
1245   MI.eraseFromParent();
1246   return true;
1247 }
1248 
1249 template <>
1250 bool AVRExpandPseudo::expand<AVR::PUSHWRr>(Block &MBB, BlockIt MBBI) {
1251   MachineInstr &MI = *MBBI;
1252   Register SrcLoReg, SrcHiReg;
1253   Register SrcReg = MI.getOperand(0).getReg();
1254   bool SrcIsKill = MI.getOperand(0).isKill();
1255   unsigned Flags = MI.getFlags();
1256   unsigned OpLo = AVR::PUSHRr;
1257   unsigned OpHi = AVR::PUSHRr;
1258   TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1259 
1260   // Low part
1261   buildMI(MBB, MBBI, OpLo)
1262       .addReg(SrcLoReg, getKillRegState(SrcIsKill))
1263       .setMIFlags(Flags);
1264 
1265   // High part
1266   buildMI(MBB, MBBI, OpHi)
1267       .addReg(SrcHiReg, getKillRegState(SrcIsKill))
1268       .setMIFlags(Flags);
1269 
1270   MI.eraseFromParent();
1271   return true;
1272 }
1273 
1274 template <>
1275 bool AVRExpandPseudo::expand<AVR::POPWRd>(Block &MBB, BlockIt MBBI) {
1276   MachineInstr &MI = *MBBI;
1277   Register DstLoReg, DstHiReg;
1278   Register DstReg = MI.getOperand(0).getReg();
1279   unsigned Flags = MI.getFlags();
1280   unsigned OpLo = AVR::POPRd;
1281   unsigned OpHi = AVR::POPRd;
1282   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1283 
1284   buildMI(MBB, MBBI, OpHi, DstHiReg).setMIFlags(Flags); // High
1285   buildMI(MBB, MBBI, OpLo, DstLoReg).setMIFlags(Flags); // Low
1286 
1287   MI.eraseFromParent();
1288   return true;
1289 }
1290 
1291 template <>
1292 bool AVRExpandPseudo::expand<AVR::ROLBRd>(Block &MBB, BlockIt MBBI) {
1293   // In AVR, the rotate instructions behave quite unintuitively. They rotate
1294   // bits through the carry bit in SREG, effectively rotating over 9 bits,
1295   // instead of 8. This is useful when we are dealing with numbers over
1296   // multiple registers, but when we actually need to rotate stuff, we have
1297   // to explicitly add the carry bit.
1298 
1299   MachineInstr &MI = *MBBI;
1300   unsigned OpShift, OpCarry;
1301   Register DstReg = MI.getOperand(0).getReg();
1302   bool DstIsDead = MI.getOperand(0).isDead();
1303   OpShift = AVR::ADDRdRr;
1304   OpCarry = AVR::ADCRdRr;
1305 
1306   // add r16, r16
1307   // adc r16, r1
1308 
1309   // Shift part
1310   buildMI(MBB, MBBI, OpShift)
1311       .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1312       .addReg(DstReg)
1313       .addReg(DstReg);
1314 
1315   // Add the carry bit
1316   auto MIB = buildMI(MBB, MBBI, OpCarry)
1317                  .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1318                  .addReg(DstReg)
1319                  .addReg(ZERO_REGISTER);
1320 
1321   // SREG is always implicitly killed
1322   MIB->getOperand(2).setIsKill();
1323 
1324   MI.eraseFromParent();
1325   return true;
1326 }
1327 
1328 template <>
1329 bool AVRExpandPseudo::expand<AVR::RORBRd>(Block &MBB, BlockIt MBBI) {
1330   // In AVR, the rotate instructions behave quite unintuitively. They rotate
1331   // bits through the carry bit in SREG, effectively rotating over 9 bits,
1332   // instead of 8. This is useful when we are dealing with numbers over
1333   // multiple registers, but when we actually need to rotate stuff, we have
1334   // to explicitly add the carry bit.
1335 
1336   MachineInstr &MI = *MBBI;
1337   Register DstReg = MI.getOperand(0).getReg();
1338 
1339   // bst r16, 0
1340   // ror r16
1341   // bld r16, 7
1342 
1343   // Move the lowest bit from DstReg into the T bit
1344   buildMI(MBB, MBBI, AVR::BST).addReg(DstReg).addImm(0);
1345 
1346   // Rotate to the right
1347   buildMI(MBB, MBBI, AVR::RORRd, DstReg).addReg(DstReg);
1348 
1349   // Move the T bit into the highest bit of DstReg.
1350   buildMI(MBB, MBBI, AVR::BLD, DstReg).addReg(DstReg).addImm(7);
1351 
1352   MI.eraseFromParent();
1353   return true;
1354 }
1355 
1356 template <>
1357 bool AVRExpandPseudo::expand<AVR::LSLWRd>(Block &MBB, BlockIt MBBI) {
1358   MachineInstr &MI = *MBBI;
1359   Register DstLoReg, DstHiReg;
1360   Register DstReg = MI.getOperand(0).getReg();
1361   bool DstIsDead = MI.getOperand(0).isDead();
1362   bool DstIsKill = MI.getOperand(1).isKill();
1363   bool ImpIsDead = MI.getOperand(2).isDead();
1364   unsigned OpLo = AVR::ADDRdRr; // ADD Rd, Rd <==> LSL Rd
1365   unsigned OpHi = AVR::ADCRdRr; // ADC Rd, Rd <==> ROL Rd
1366   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1367 
1368   // Low part
1369   buildMI(MBB, MBBI, OpLo)
1370       .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1371       .addReg(DstLoReg)
1372       .addReg(DstLoReg, getKillRegState(DstIsKill));
1373 
1374   auto MIBHI =
1375       buildMI(MBB, MBBI, OpHi)
1376           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1377           .addReg(DstHiReg)
1378           .addReg(DstHiReg, getKillRegState(DstIsKill));
1379 
1380   if (ImpIsDead)
1381     MIBHI->getOperand(3).setIsDead();
1382 
1383   // SREG is always implicitly killed
1384   MIBHI->getOperand(4).setIsKill();
1385 
1386   MI.eraseFromParent();
1387   return true;
1388 }
1389 
1390 template <>
1391 bool AVRExpandPseudo::expand<AVR::LSLWHiRd>(Block &MBB, BlockIt MBBI) {
1392   MachineInstr &MI = *MBBI;
1393   Register DstLoReg, DstHiReg;
1394   Register DstReg = MI.getOperand(0).getReg();
1395   bool DstIsDead = MI.getOperand(0).isDead();
1396   bool DstIsKill = MI.getOperand(1).isKill();
1397   bool ImpIsDead = MI.getOperand(2).isDead();
1398   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1399 
1400   // add hireg, hireg <==> lsl hireg
1401   auto MILSL =
1402       buildMI(MBB, MBBI, AVR::ADDRdRr)
1403           .addReg(DstHiReg, RegState::Define, getDeadRegState(DstIsDead))
1404           .addReg(DstHiReg, getKillRegState(DstIsKill))
1405           .addReg(DstHiReg, getKillRegState(DstIsKill));
1406 
1407   if (ImpIsDead)
1408     MILSL->getOperand(3).setIsDead();
1409 
1410   MI.eraseFromParent();
1411   return true;
1412 }
1413 
1414 bool AVRExpandPseudo::expandLSLW4Rd(Block &MBB, BlockIt MBBI) {
1415   MachineInstr &MI = *MBBI;
1416   Register DstLoReg, DstHiReg;
1417   Register DstReg = MI.getOperand(0).getReg();
1418   bool DstIsDead = MI.getOperand(0).isDead();
1419   bool DstIsKill = MI.getOperand(1).isKill();
1420   bool ImpIsDead = MI.getOperand(3).isDead();
1421   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1422 
1423   // swap Rh
1424   // swap Rl
1425   buildMI(MBB, MBBI, AVR::SWAPRd)
1426       .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1427       .addReg(DstHiReg, getKillRegState(DstIsKill));
1428   buildMI(MBB, MBBI, AVR::SWAPRd)
1429       .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1430       .addReg(DstLoReg, getKillRegState(DstIsKill));
1431 
1432   // andi Rh, 0xf0
1433   auto MI0 =
1434       buildMI(MBB, MBBI, AVR::ANDIRdK)
1435           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1436           .addReg(DstHiReg, getKillRegState(DstIsKill))
1437           .addImm(0xf0);
1438   // SREG is implicitly dead.
1439   MI0->getOperand(3).setIsDead();
1440 
1441   // eor Rh, Rl
1442   auto MI1 =
1443       buildMI(MBB, MBBI, AVR::EORRdRr)
1444           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1445           .addReg(DstHiReg, getKillRegState(DstIsKill))
1446           .addReg(DstLoReg);
1447   // SREG is implicitly dead.
1448   MI1->getOperand(3).setIsDead();
1449 
1450   // andi Rl, 0xf0
1451   auto MI2 =
1452       buildMI(MBB, MBBI, AVR::ANDIRdK)
1453           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1454           .addReg(DstLoReg, getKillRegState(DstIsKill))
1455           .addImm(0xf0);
1456   // SREG is implicitly dead.
1457   MI2->getOperand(3).setIsDead();
1458 
1459   // eor Rh, Rl
1460   auto MI3 =
1461       buildMI(MBB, MBBI, AVR::EORRdRr)
1462           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1463           .addReg(DstHiReg, getKillRegState(DstIsKill))
1464           .addReg(DstLoReg);
1465   if (ImpIsDead)
1466     MI3->getOperand(3).setIsDead();
1467 
1468   MI.eraseFromParent();
1469   return true;
1470 }
1471 
1472 bool AVRExpandPseudo::expandLSLW8Rd(Block &MBB, BlockIt MBBI) {
1473   MachineInstr &MI = *MBBI;
1474   Register DstLoReg, DstHiReg;
1475   Register DstReg = MI.getOperand(0).getReg();
1476   bool DstIsDead = MI.getOperand(0).isDead();
1477   bool DstIsKill = MI.getOperand(1).isKill();
1478   bool ImpIsDead = MI.getOperand(3).isDead();
1479   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1480 
1481   // mov Rh, Rl
1482   buildMI(MBB, MBBI, AVR::MOVRdRr)
1483       .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1484       .addReg(DstLoReg);
1485 
1486   // clr Rl
1487   auto MIBLO =
1488       buildMI(MBB, MBBI, AVR::EORRdRr)
1489           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1490           .addReg(DstLoReg, getKillRegState(DstIsKill))
1491           .addReg(DstLoReg, getKillRegState(DstIsKill));
1492   if (ImpIsDead)
1493     MIBLO->getOperand(3).setIsDead();
1494 
1495   MI.eraseFromParent();
1496   return true;
1497 }
1498 
1499 bool AVRExpandPseudo::expandLSLW12Rd(Block &MBB, BlockIt MBBI) {
1500   MachineInstr &MI = *MBBI;
1501   Register DstLoReg, DstHiReg;
1502   Register DstReg = MI.getOperand(0).getReg();
1503   bool DstIsDead = MI.getOperand(0).isDead();
1504   bool DstIsKill = MI.getOperand(1).isKill();
1505   bool ImpIsDead = MI.getOperand(3).isDead();
1506   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1507 
1508   // mov Rh, Rl
1509   buildMI(MBB, MBBI, AVR::MOVRdRr)
1510       .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1511       .addReg(DstLoReg);
1512 
1513   // swap Rh
1514   buildMI(MBB, MBBI, AVR::SWAPRd)
1515       .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1516       .addReg(DstHiReg, getKillRegState(DstIsKill));
1517 
1518   // andi Rh, 0xf0
1519   auto MI0 =
1520       buildMI(MBB, MBBI, AVR::ANDIRdK)
1521           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1522           .addReg(DstHiReg, getKillRegState(DstIsKill))
1523           .addImm(0xf0);
1524   // SREG is implicitly dead.
1525   MI0->getOperand(3).setIsDead();
1526 
1527   // clr Rl
1528   auto MI1 =
1529       buildMI(MBB, MBBI, AVR::EORRdRr)
1530           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1531           .addReg(DstLoReg, getKillRegState(DstIsKill))
1532           .addReg(DstLoReg, getKillRegState(DstIsKill));
1533   if (ImpIsDead)
1534     MI1->getOperand(3).setIsDead();
1535 
1536   MI.eraseFromParent();
1537   return true;
1538 }
1539 
1540 template <>
1541 bool AVRExpandPseudo::expand<AVR::LSLWNRd>(Block &MBB, BlockIt MBBI) {
1542   MachineInstr &MI = *MBBI;
1543   unsigned Imm = MI.getOperand(2).getImm();
1544   switch (Imm) {
1545   case 4:
1546     return expandLSLW4Rd(MBB, MBBI);
1547   case 8:
1548     return expandLSLW8Rd(MBB, MBBI);
1549   case 12:
1550     return expandLSLW12Rd(MBB, MBBI);
1551   default:
1552     llvm_unreachable("unimplemented lslwn");
1553     return false;
1554   }
1555 }
1556 
1557 template <>
1558 bool AVRExpandPseudo::expand<AVR::LSRWRd>(Block &MBB, BlockIt MBBI) {
1559   MachineInstr &MI = *MBBI;
1560   Register DstLoReg, DstHiReg;
1561   Register DstReg = MI.getOperand(0).getReg();
1562   bool DstIsDead = MI.getOperand(0).isDead();
1563   bool DstIsKill = MI.getOperand(1).isKill();
1564   bool ImpIsDead = MI.getOperand(2).isDead();
1565   unsigned OpLo = AVR::RORRd;
1566   unsigned OpHi = AVR::LSRRd;
1567   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1568 
1569   // High part
1570   buildMI(MBB, MBBI, OpHi)
1571       .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1572       .addReg(DstHiReg, getKillRegState(DstIsKill));
1573 
1574   auto MIBLO =
1575       buildMI(MBB, MBBI, OpLo)
1576           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1577           .addReg(DstLoReg, getKillRegState(DstIsKill));
1578 
1579   if (ImpIsDead)
1580     MIBLO->getOperand(2).setIsDead();
1581 
1582   // SREG is always implicitly killed
1583   MIBLO->getOperand(3).setIsKill();
1584 
1585   MI.eraseFromParent();
1586   return true;
1587 }
1588 
1589 template <>
1590 bool AVRExpandPseudo::expand<AVR::LSRWLoRd>(Block &MBB, BlockIt MBBI) {
1591   MachineInstr &MI = *MBBI;
1592   Register DstLoReg, DstHiReg;
1593   Register DstReg = MI.getOperand(0).getReg();
1594   bool DstIsDead = MI.getOperand(0).isDead();
1595   bool DstIsKill = MI.getOperand(1).isKill();
1596   bool ImpIsDead = MI.getOperand(2).isDead();
1597   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1598 
1599   // lsr loreg
1600   auto MILSR =
1601       buildMI(MBB, MBBI, AVR::LSRRd)
1602           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1603           .addReg(DstLoReg, getKillRegState(DstIsKill));
1604 
1605   if (ImpIsDead)
1606     MILSR->getOperand(2).setIsDead();
1607 
1608   MI.eraseFromParent();
1609   return true;
1610 }
1611 
1612 bool AVRExpandPseudo::expandLSRW4Rd(Block &MBB, BlockIt MBBI) {
1613   MachineInstr &MI = *MBBI;
1614   Register DstLoReg, DstHiReg;
1615   Register DstReg = MI.getOperand(0).getReg();
1616   bool DstIsDead = MI.getOperand(0).isDead();
1617   bool DstIsKill = MI.getOperand(1).isKill();
1618   bool ImpIsDead = MI.getOperand(3).isDead();
1619   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1620 
1621   // swap Rh
1622   // swap Rl
1623   buildMI(MBB, MBBI, AVR::SWAPRd)
1624       .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1625       .addReg(DstHiReg, getKillRegState(DstIsKill));
1626   buildMI(MBB, MBBI, AVR::SWAPRd)
1627       .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1628       .addReg(DstLoReg, getKillRegState(DstIsKill));
1629 
1630   // andi Rl, 0xf
1631   auto MI0 =
1632       buildMI(MBB, MBBI, AVR::ANDIRdK)
1633           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1634           .addReg(DstLoReg, getKillRegState(DstIsKill))
1635           .addImm(0xf);
1636   // SREG is implicitly dead.
1637   MI0->getOperand(3).setIsDead();
1638 
1639   // eor Rl, Rh
1640   auto MI1 =
1641       buildMI(MBB, MBBI, AVR::EORRdRr)
1642           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1643           .addReg(DstLoReg, getKillRegState(DstIsKill))
1644           .addReg(DstHiReg);
1645   // SREG is implicitly dead.
1646   MI1->getOperand(3).setIsDead();
1647 
1648   // andi Rh, 0xf
1649   auto MI2 =
1650       buildMI(MBB, MBBI, AVR::ANDIRdK)
1651           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1652           .addReg(DstHiReg, getKillRegState(DstIsKill))
1653           .addImm(0xf);
1654   // SREG is implicitly dead.
1655   MI2->getOperand(3).setIsDead();
1656 
1657   // eor Rl, Rh
1658   auto MI3 =
1659       buildMI(MBB, MBBI, AVR::EORRdRr)
1660           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1661           .addReg(DstLoReg, getKillRegState(DstIsKill))
1662           .addReg(DstHiReg);
1663   if (ImpIsDead)
1664     MI3->getOperand(3).setIsDead();
1665 
1666   MI.eraseFromParent();
1667   return true;
1668 }
1669 
1670 bool AVRExpandPseudo::expandLSRW8Rd(Block &MBB, BlockIt MBBI) {
1671   MachineInstr &MI = *MBBI;
1672   Register DstLoReg, DstHiReg;
1673   Register DstReg = MI.getOperand(0).getReg();
1674   bool DstIsDead = MI.getOperand(0).isDead();
1675   bool DstIsKill = MI.getOperand(1).isKill();
1676   bool ImpIsDead = MI.getOperand(3).isDead();
1677   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1678 
1679   // Move upper byte to lower byte.
1680   buildMI(MBB, MBBI, AVR::MOVRdRr)
1681       .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1682       .addReg(DstHiReg);
1683 
1684   // Clear upper byte.
1685   auto MIBHI =
1686       buildMI(MBB, MBBI, AVR::EORRdRr)
1687           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1688           .addReg(DstHiReg, getKillRegState(DstIsKill))
1689           .addReg(DstHiReg, getKillRegState(DstIsKill));
1690   if (ImpIsDead)
1691     MIBHI->getOperand(3).setIsDead();
1692 
1693   MI.eraseFromParent();
1694   return true;
1695 }
1696 
1697 bool AVRExpandPseudo::expandLSRW12Rd(Block &MBB, BlockIt MBBI) {
1698   MachineInstr &MI = *MBBI;
1699   Register DstLoReg, DstHiReg;
1700   Register DstReg = MI.getOperand(0).getReg();
1701   bool DstIsDead = MI.getOperand(0).isDead();
1702   bool DstIsKill = MI.getOperand(1).isKill();
1703   bool ImpIsDead = MI.getOperand(3).isDead();
1704   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1705 
1706   // Move upper byte to lower byte.
1707   buildMI(MBB, MBBI, AVR::MOVRdRr)
1708       .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1709       .addReg(DstHiReg);
1710 
1711   // swap Rl
1712   buildMI(MBB, MBBI, AVR::SWAPRd)
1713       .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1714       .addReg(DstLoReg, getKillRegState(DstIsKill));
1715 
1716   // andi Rl, 0xf
1717   auto MI0 =
1718       buildMI(MBB, MBBI, AVR::ANDIRdK)
1719           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1720           .addReg(DstLoReg, getKillRegState(DstIsKill))
1721           .addImm(0xf);
1722   // SREG is implicitly dead.
1723   MI0->getOperand(3).setIsDead();
1724 
1725   // Clear upper byte.
1726   auto MIBHI =
1727       buildMI(MBB, MBBI, AVR::EORRdRr)
1728           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1729           .addReg(DstHiReg, getKillRegState(DstIsKill))
1730           .addReg(DstHiReg, getKillRegState(DstIsKill));
1731   if (ImpIsDead)
1732     MIBHI->getOperand(3).setIsDead();
1733 
1734   MI.eraseFromParent();
1735   return true;
1736 }
1737 
1738 template <>
1739 bool AVRExpandPseudo::expand<AVR::LSRWNRd>(Block &MBB, BlockIt MBBI) {
1740   MachineInstr &MI = *MBBI;
1741   unsigned Imm = MI.getOperand(2).getImm();
1742   switch (Imm) {
1743   case 4:
1744     return expandLSRW4Rd(MBB, MBBI);
1745   case 8:
1746     return expandLSRW8Rd(MBB, MBBI);
1747   case 12:
1748     return expandLSRW12Rd(MBB, MBBI);
1749   default:
1750     llvm_unreachable("unimplemented lsrwn");
1751     return false;
1752   }
1753 }
1754 
1755 template <>
1756 bool AVRExpandPseudo::expand<AVR::RORWRd>(Block &MBB, BlockIt MBBI) {
1757   llvm_unreachable("RORW unimplemented");
1758   return false;
1759 }
1760 
1761 template <>
1762 bool AVRExpandPseudo::expand<AVR::ROLWRd>(Block &MBB, BlockIt MBBI) {
1763   llvm_unreachable("ROLW unimplemented");
1764   return false;
1765 }
1766 
1767 template <>
1768 bool AVRExpandPseudo::expand<AVR::ASRWRd>(Block &MBB, BlockIt MBBI) {
1769   MachineInstr &MI = *MBBI;
1770   Register DstLoReg, DstHiReg;
1771   Register DstReg = MI.getOperand(0).getReg();
1772   bool DstIsDead = MI.getOperand(0).isDead();
1773   bool DstIsKill = MI.getOperand(1).isKill();
1774   bool ImpIsDead = MI.getOperand(2).isDead();
1775   unsigned OpLo = AVR::RORRd;
1776   unsigned OpHi = AVR::ASRRd;
1777   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1778 
1779   // High part
1780   buildMI(MBB, MBBI, OpHi)
1781       .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1782       .addReg(DstHiReg, getKillRegState(DstIsKill));
1783 
1784   auto MIBLO =
1785       buildMI(MBB, MBBI, OpLo)
1786           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1787           .addReg(DstLoReg, getKillRegState(DstIsKill));
1788 
1789   if (ImpIsDead)
1790     MIBLO->getOperand(2).setIsDead();
1791 
1792   // SREG is always implicitly killed
1793   MIBLO->getOperand(3).setIsKill();
1794 
1795   MI.eraseFromParent();
1796   return true;
1797 }
1798 
1799 template <>
1800 bool AVRExpandPseudo::expand<AVR::ASRWLoRd>(Block &MBB, BlockIt MBBI) {
1801   MachineInstr &MI = *MBBI;
1802   Register DstLoReg, DstHiReg;
1803   Register DstReg = MI.getOperand(0).getReg();
1804   bool DstIsDead = MI.getOperand(0).isDead();
1805   bool DstIsKill = MI.getOperand(1).isKill();
1806   bool ImpIsDead = MI.getOperand(2).isDead();
1807   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1808 
1809   // asr loreg
1810   auto MIASR =
1811       buildMI(MBB, MBBI, AVR::ASRRd)
1812           .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1813           .addReg(DstLoReg, getKillRegState(DstIsKill));
1814 
1815   if (ImpIsDead)
1816     MIASR->getOperand(2).setIsDead();
1817 
1818   MI.eraseFromParent();
1819   return true;
1820 }
1821 
1822 bool AVRExpandPseudo::expandASRW8Rd(Block &MBB, BlockIt MBBI) {
1823   MachineInstr &MI = *MBBI;
1824   Register DstLoReg, DstHiReg;
1825   Register DstReg = MI.getOperand(0).getReg();
1826   bool DstIsDead = MI.getOperand(0).isDead();
1827   bool DstIsKill = MI.getOperand(1).isKill();
1828   bool ImpIsDead = MI.getOperand(3).isDead();
1829   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1830 
1831   // Move upper byte to lower byte.
1832   buildMI(MBB, MBBI, AVR::MOVRdRr)
1833       .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1834       .addReg(DstHiReg);
1835 
1836   // Move the sign bit to the C flag.
1837   buildMI(MBB, MBBI, AVR::ADDRdRr)
1838       .addReg(DstHiReg, RegState::Define, getDeadRegState(DstIsDead))
1839       .addReg(DstHiReg, getKillRegState(DstIsKill) | getDeadRegState(DstIsDead))
1840       .addReg(DstHiReg, getKillRegState(DstIsKill));
1841 
1842   // Set upper byte to 0 or -1.
1843   auto MIBHI =
1844       buildMI(MBB, MBBI, AVR::SBCRdRr)
1845           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1846           .addReg(DstHiReg, getKillRegState(DstIsKill))
1847           .addReg(DstHiReg, getKillRegState(DstIsKill));
1848   if (ImpIsDead)
1849     MIBHI->getOperand(3).setIsDead();
1850 
1851   MI.eraseFromParent();
1852   return true;
1853 }
1854 
1855 template <>
1856 bool AVRExpandPseudo::expand<AVR::ASRWNRd>(Block &MBB, BlockIt MBBI) {
1857   MachineInstr &MI = *MBBI;
1858   unsigned Imm = MI.getOperand(2).getImm();
1859   switch (Imm) {
1860   case 8:
1861     return expandASRW8Rd(MBB, MBBI);
1862   default:
1863     llvm_unreachable("unimplemented asrwn");
1864     return false;
1865   }
1866 }
1867 
1868 bool AVRExpandPseudo::expandLSLB7Rd(Block &MBB, BlockIt MBBI) {
1869   MachineInstr &MI = *MBBI;
1870   Register DstReg = MI.getOperand(0).getReg();
1871   bool DstIsDead = MI.getOperand(0).isDead();
1872   bool DstIsKill = MI.getOperand(1).isKill();
1873   bool ImpIsDead = MI.getOperand(3).isDead();
1874 
1875   // ror r24
1876   // clr r24
1877   // ror r24
1878 
1879   buildMI(MBB, MBBI, AVR::RORRd)
1880       .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1881       .addReg(DstReg, getKillRegState(DstIsKill))
1882       ->getOperand(3)
1883       .setIsUndef(true);
1884 
1885   buildMI(MBB, MBBI, AVR::EORRdRr)
1886       .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1887       .addReg(DstReg, getKillRegState(DstIsKill))
1888       .addReg(DstReg, getKillRegState(DstIsKill));
1889 
1890   auto MIRRC =
1891       buildMI(MBB, MBBI, AVR::RORRd)
1892           .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1893           .addReg(DstReg, getKillRegState(DstIsKill));
1894 
1895   if (ImpIsDead)
1896     MIRRC->getOperand(2).setIsDead();
1897 
1898   // SREG is always implicitly killed
1899   MIRRC->getOperand(3).setIsKill();
1900 
1901   MI.eraseFromParent();
1902   return true;
1903 }
1904 
1905 template <>
1906 bool AVRExpandPseudo::expand<AVR::LSLBNRd>(Block &MBB, BlockIt MBBI) {
1907   MachineInstr &MI = *MBBI;
1908   unsigned Imm = MI.getOperand(2).getImm();
1909   switch (Imm) {
1910   case 7:
1911     return expandLSLB7Rd(MBB, MBBI);
1912   default:
1913     llvm_unreachable("unimplemented lslbn");
1914     return false;
1915   }
1916 }
1917 
1918 bool AVRExpandPseudo::expandLSRB7Rd(Block &MBB, BlockIt MBBI) {
1919   MachineInstr &MI = *MBBI;
1920   Register DstReg = MI.getOperand(0).getReg();
1921   bool DstIsDead = MI.getOperand(0).isDead();
1922   bool DstIsKill = MI.getOperand(1).isKill();
1923   bool ImpIsDead = MI.getOperand(3).isDead();
1924 
1925   // rol r24
1926   // clr r24
1927   // rol r24
1928 
1929   buildMI(MBB, MBBI, AVR::ADCRdRr)
1930       .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1931       .addReg(DstReg, getKillRegState(DstIsKill))
1932       .addReg(DstReg, getKillRegState(DstIsKill))
1933       ->getOperand(4)
1934       .setIsUndef(true);
1935 
1936   buildMI(MBB, MBBI, AVR::EORRdRr)
1937       .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1938       .addReg(DstReg, getKillRegState(DstIsKill))
1939       .addReg(DstReg, getKillRegState(DstIsKill));
1940 
1941   auto MIRRC =
1942       buildMI(MBB, MBBI, AVR::ADCRdRr)
1943           .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1944           .addReg(DstReg, getKillRegState(DstIsKill))
1945           .addReg(DstReg, getKillRegState(DstIsKill));
1946 
1947   if (ImpIsDead)
1948     MIRRC->getOperand(3).setIsDead();
1949 
1950   // SREG is always implicitly killed
1951   MIRRC->getOperand(4).setIsKill();
1952 
1953   MI.eraseFromParent();
1954   return true;
1955 }
1956 
1957 template <>
1958 bool AVRExpandPseudo::expand<AVR::LSRBNRd>(Block &MBB, BlockIt MBBI) {
1959   MachineInstr &MI = *MBBI;
1960   unsigned Imm = MI.getOperand(2).getImm();
1961   switch (Imm) {
1962   case 7:
1963     return expandLSRB7Rd(MBB, MBBI);
1964   default:
1965     llvm_unreachable("unimplemented lsrbn");
1966     return false;
1967   }
1968 }
1969 
1970 bool AVRExpandPseudo::expandASRB6Rd(Block &MBB, BlockIt MBBI) {
1971   MachineInstr &MI = *MBBI;
1972   Register DstReg = MI.getOperand(0).getReg();
1973   bool DstIsDead = MI.getOperand(0).isDead();
1974   bool DstIsKill = MI.getOperand(1).isKill();
1975 
1976   // bst r24, 6
1977   // lsl r24
1978   // sbc r24, r24
1979   // bld r24, 0
1980 
1981   buildMI(MBB, MBBI, AVR::BST)
1982       .addReg(DstReg)
1983       .addImm(6)
1984       ->getOperand(2)
1985       .setIsUndef(true);
1986 
1987   buildMI(MBB, MBBI, AVR::ADDRdRr) // LSL Rd <==> ADD Rd, Rd
1988       .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1989       .addReg(DstReg, getKillRegState(DstIsKill))
1990       .addReg(DstReg, getKillRegState(DstIsKill));
1991 
1992   buildMI(MBB, MBBI, AVR::SBCRdRr)
1993       .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1994       .addReg(DstReg, getKillRegState(DstIsKill))
1995       .addReg(DstReg, getKillRegState(DstIsKill));
1996 
1997   buildMI(MBB, MBBI, AVR::BLD)
1998       .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1999       .addReg(DstReg, getKillRegState(DstIsKill))
2000       .addImm(0)
2001       ->getOperand(3)
2002       .setIsKill();
2003 
2004   MI.eraseFromParent();
2005   return true;
2006 }
2007 
2008 bool AVRExpandPseudo::expandASRB7Rd(Block &MBB, BlockIt MBBI) {
2009   MachineInstr &MI = *MBBI;
2010   Register DstReg = MI.getOperand(0).getReg();
2011   bool DstIsDead = MI.getOperand(0).isDead();
2012   bool DstIsKill = MI.getOperand(1).isKill();
2013   bool ImpIsDead = MI.getOperand(3).isDead();
2014 
2015   // lsl r24
2016   // sbc r24, r24
2017 
2018   buildMI(MBB, MBBI, AVR::ADDRdRr)
2019       .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
2020       .addReg(DstReg, getKillRegState(DstIsKill))
2021       .addReg(DstReg, getKillRegState(DstIsKill));
2022 
2023   auto MIRRC =
2024       buildMI(MBB, MBBI, AVR::SBCRdRr)
2025           .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
2026           .addReg(DstReg, getKillRegState(DstIsKill))
2027           .addReg(DstReg, getKillRegState(DstIsKill));
2028 
2029   if (ImpIsDead)
2030     MIRRC->getOperand(3).setIsDead();
2031 
2032   // SREG is always implicitly killed
2033   MIRRC->getOperand(4).setIsKill();
2034 
2035   MI.eraseFromParent();
2036   return true;
2037 }
2038 
2039 template <>
2040 bool AVRExpandPseudo::expand<AVR::ASRBNRd>(Block &MBB, BlockIt MBBI) {
2041   MachineInstr &MI = *MBBI;
2042   unsigned Imm = MI.getOperand(2).getImm();
2043   switch (Imm) {
2044   case 6:
2045     return expandASRB6Rd(MBB, MBBI);
2046   case 7:
2047     return expandASRB7Rd(MBB, MBBI);
2048   default:
2049     llvm_unreachable("unimplemented asrbn");
2050     return false;
2051   }
2052 }
2053 
2054 template <> bool AVRExpandPseudo::expand<AVR::SEXT>(Block &MBB, BlockIt MBBI) {
2055   MachineInstr &MI = *MBBI;
2056   Register DstLoReg, DstHiReg;
2057   // sext R17:R16, R17
2058   // mov     r16, r17
2059   // lsl     r17
2060   // sbc     r17, r17
2061   // sext R17:R16, R13
2062   // mov     r16, r13
2063   // mov     r17, r13
2064   // lsl     r17
2065   // sbc     r17, r17
2066   // sext R17:R16, R16
2067   // mov     r17, r16
2068   // lsl     r17
2069   // sbc     r17, r17
2070   Register DstReg = MI.getOperand(0).getReg();
2071   Register SrcReg = MI.getOperand(1).getReg();
2072   bool DstIsDead = MI.getOperand(0).isDead();
2073   bool SrcIsKill = MI.getOperand(1).isKill();
2074   bool ImpIsDead = MI.getOperand(2).isDead();
2075   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
2076 
2077   if (SrcReg != DstLoReg) {
2078     auto MOV =
2079         buildMI(MBB, MBBI, AVR::MOVRdRr)
2080             .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
2081             .addReg(SrcReg);
2082 
2083     if (SrcReg == DstHiReg) {
2084       MOV->getOperand(1).setIsKill();
2085     }
2086   }
2087 
2088   if (SrcReg != DstHiReg) {
2089     buildMI(MBB, MBBI, AVR::MOVRdRr)
2090         .addReg(DstHiReg, RegState::Define)
2091         .addReg(SrcReg, getKillRegState(SrcIsKill));
2092   }
2093 
2094   buildMI(MBB, MBBI, AVR::ADDRdRr) // LSL Rd <==> ADD Rd, Rr
2095       .addReg(DstHiReg, RegState::Define)
2096       .addReg(DstHiReg)
2097       .addReg(DstHiReg, RegState::Kill);
2098 
2099   auto SBC =
2100       buildMI(MBB, MBBI, AVR::SBCRdRr)
2101           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
2102           .addReg(DstHiReg, RegState::Kill)
2103           .addReg(DstHiReg, RegState::Kill);
2104 
2105   if (ImpIsDead)
2106     SBC->getOperand(3).setIsDead();
2107 
2108   // SREG is always implicitly killed
2109   SBC->getOperand(4).setIsKill();
2110 
2111   MI.eraseFromParent();
2112   return true;
2113 }
2114 
2115 template <> bool AVRExpandPseudo::expand<AVR::ZEXT>(Block &MBB, BlockIt MBBI) {
2116   MachineInstr &MI = *MBBI;
2117   Register DstLoReg, DstHiReg;
2118   // zext R25:R24, R20
2119   // mov      R24, R20
2120   // eor      R25, R25
2121   // zext R25:R24, R24
2122   // eor      R25, R25
2123   // zext R25:R24, R25
2124   // mov      R24, R25
2125   // eor      R25, R25
2126   Register DstReg = MI.getOperand(0).getReg();
2127   Register SrcReg = MI.getOperand(1).getReg();
2128   bool DstIsDead = MI.getOperand(0).isDead();
2129   bool SrcIsKill = MI.getOperand(1).isKill();
2130   bool ImpIsDead = MI.getOperand(2).isDead();
2131   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
2132 
2133   if (SrcReg != DstLoReg) {
2134     buildMI(MBB, MBBI, AVR::MOVRdRr)
2135         .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
2136         .addReg(SrcReg, getKillRegState(SrcIsKill));
2137   }
2138 
2139   auto EOR =
2140       buildMI(MBB, MBBI, AVR::EORRdRr)
2141           .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
2142           .addReg(DstHiReg, RegState::Kill | RegState::Undef)
2143           .addReg(DstHiReg, RegState::Kill | RegState::Undef);
2144 
2145   if (ImpIsDead)
2146     EOR->getOperand(3).setIsDead();
2147 
2148   MI.eraseFromParent();
2149   return true;
2150 }
2151 
2152 template <>
2153 bool AVRExpandPseudo::expand<AVR::SPREAD>(Block &MBB, BlockIt MBBI) {
2154   MachineInstr &MI = *MBBI;
2155   Register DstLoReg, DstHiReg;
2156   Register DstReg = MI.getOperand(0).getReg();
2157   bool DstIsDead = MI.getOperand(0).isDead();
2158   unsigned Flags = MI.getFlags();
2159   unsigned OpLo = AVR::INRdA;
2160   unsigned OpHi = AVR::INRdA;
2161   TRI->splitReg(DstReg, DstLoReg, DstHiReg);
2162 
2163   // Low part
2164   buildMI(MBB, MBBI, OpLo)
2165       .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
2166       .addImm(0x3d)
2167       .setMIFlags(Flags);
2168 
2169   // High part
2170   buildMI(MBB, MBBI, OpHi)
2171       .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
2172       .addImm(0x3e)
2173       .setMIFlags(Flags);
2174 
2175   MI.eraseFromParent();
2176   return true;
2177 }
2178 
2179 template <>
2180 bool AVRExpandPseudo::expand<AVR::SPWRITE>(Block &MBB, BlockIt MBBI) {
2181   const AVRSubtarget &STI = MBB.getParent()->getSubtarget<AVRSubtarget>();
2182   MachineInstr &MI = *MBBI;
2183   Register SrcLoReg, SrcHiReg;
2184   Register SrcReg = MI.getOperand(1).getReg();
2185   bool SrcIsKill = MI.getOperand(1).isKill();
2186   unsigned Flags = MI.getFlags();
2187   TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
2188 
2189   buildMI(MBB, MBBI, AVR::INRdA)
2190       .addReg(AVR::R0, RegState::Define)
2191       .addImm(STI.getIORegSREG())
2192       .setMIFlags(Flags);
2193 
2194   buildMI(MBB, MBBI, AVR::BCLRs).addImm(0x07).setMIFlags(Flags);
2195 
2196   buildMI(MBB, MBBI, AVR::OUTARr)
2197       .addImm(0x3e)
2198       .addReg(SrcHiReg, getKillRegState(SrcIsKill))
2199       .setMIFlags(Flags);
2200 
2201   buildMI(MBB, MBBI, AVR::OUTARr)
2202       .addImm(STI.getIORegSREG())
2203       .addReg(AVR::R0, RegState::Kill)
2204       .setMIFlags(Flags);
2205 
2206   buildMI(MBB, MBBI, AVR::OUTARr)
2207       .addImm(0x3d)
2208       .addReg(SrcLoReg, getKillRegState(SrcIsKill))
2209       .setMIFlags(Flags);
2210 
2211   MI.eraseFromParent();
2212   return true;
2213 }
2214 
2215 bool AVRExpandPseudo::expandMI(Block &MBB, BlockIt MBBI) {
2216   MachineInstr &MI = *MBBI;
2217   int Opcode = MBBI->getOpcode();
2218 
2219 #define EXPAND(Op)                                                             \
2220   case Op:                                                                     \
2221     return expand<Op>(MBB, MI)
2222 
2223   switch (Opcode) {
2224     EXPAND(AVR::ADDWRdRr);
2225     EXPAND(AVR::ADCWRdRr);
2226     EXPAND(AVR::SUBWRdRr);
2227     EXPAND(AVR::SUBIWRdK);
2228     EXPAND(AVR::SBCWRdRr);
2229     EXPAND(AVR::SBCIWRdK);
2230     EXPAND(AVR::ANDWRdRr);
2231     EXPAND(AVR::ANDIWRdK);
2232     EXPAND(AVR::ORWRdRr);
2233     EXPAND(AVR::ORIWRdK);
2234     EXPAND(AVR::EORWRdRr);
2235     EXPAND(AVR::COMWRd);
2236     EXPAND(AVR::NEGWRd);
2237     EXPAND(AVR::CPWRdRr);
2238     EXPAND(AVR::CPCWRdRr);
2239     EXPAND(AVR::LDIWRdK);
2240     EXPAND(AVR::LDSWRdK);
2241     EXPAND(AVR::LDWRdPtr);
2242     EXPAND(AVR::LDWRdPtrPi);
2243     EXPAND(AVR::LDWRdPtrPd);
2244   case AVR::LDDWRdYQ: //: FIXME: remove this once PR13375 gets fixed
2245     EXPAND(AVR::LDDWRdPtrQ);
2246     EXPAND(AVR::LPMWRdZ);
2247     EXPAND(AVR::LPMWRdZPi);
2248     EXPAND(AVR::ELPMBRdZ);
2249     EXPAND(AVR::ELPMWRdZ);
2250     EXPAND(AVR::ELPMBRdZPi);
2251     EXPAND(AVR::ELPMWRdZPi);
2252     EXPAND(AVR::AtomicLoad8);
2253     EXPAND(AVR::AtomicLoad16);
2254     EXPAND(AVR::AtomicStore8);
2255     EXPAND(AVR::AtomicStore16);
2256     EXPAND(AVR::AtomicFence);
2257     EXPAND(AVR::STSWKRr);
2258     EXPAND(AVR::STWPtrRr);
2259     EXPAND(AVR::STWPtrPiRr);
2260     EXPAND(AVR::STWPtrPdRr);
2261     EXPAND(AVR::STDWPtrQRr);
2262     EXPAND(AVR::INWRdA);
2263     EXPAND(AVR::OUTWARr);
2264     EXPAND(AVR::PUSHWRr);
2265     EXPAND(AVR::POPWRd);
2266     EXPAND(AVR::ROLBRd);
2267     EXPAND(AVR::RORBRd);
2268     EXPAND(AVR::LSLWRd);
2269     EXPAND(AVR::LSRWRd);
2270     EXPAND(AVR::RORWRd);
2271     EXPAND(AVR::ROLWRd);
2272     EXPAND(AVR::ASRWRd);
2273     EXPAND(AVR::LSLWHiRd);
2274     EXPAND(AVR::LSRWLoRd);
2275     EXPAND(AVR::ASRWLoRd);
2276     EXPAND(AVR::LSLWNRd);
2277     EXPAND(AVR::LSRWNRd);
2278     EXPAND(AVR::ASRWNRd);
2279     EXPAND(AVR::LSLBNRd);
2280     EXPAND(AVR::LSRBNRd);
2281     EXPAND(AVR::ASRBNRd);
2282     EXPAND(AVR::SEXT);
2283     EXPAND(AVR::ZEXT);
2284     EXPAND(AVR::SPREAD);
2285     EXPAND(AVR::SPWRITE);
2286   }
2287 #undef EXPAND
2288   return false;
2289 }
2290 
2291 } // end of anonymous namespace
2292 
2293 INITIALIZE_PASS(AVRExpandPseudo, "avr-expand-pseudo", AVR_EXPAND_PSEUDO_NAME,
2294                 false, false)
2295 namespace llvm {
2296 
2297 FunctionPass *createAVRExpandPseudoPass() { return new AVRExpandPseudo(); }
2298 
2299 } // end of namespace llvm
2300