1 //===-- AVRExpandPseudoInsts.cpp - Expand pseudo instructions -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a pass that expands pseudo instructions into target
11 // instructions. This pass should be run after register allocation but before
12 // the post-regalloc scheduling pass.
14 //===----------------------------------------------------------------------===//
17 #include "AVRInstrInfo.h"
18 #include "AVRTargetMachine.h"
19 #include "MCTargetDesc/AVRMCTargetDesc.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/CodeGen/RegisterScavenging.h"
25 #include "llvm/Target/TargetRegisterInfo.h"
29 #define AVR_EXPAND_PSEUDO_NAME "AVR pseudo instruction expansion pass"
33 /// Expands "placeholder" instructions marked as pseudo into
34 /// actual AVR instructions.
35 class AVRExpandPseudo : public MachineFunctionPass {
39 AVRExpandPseudo() : MachineFunctionPass(ID) {
40 initializeAVRExpandPseudoPass(*PassRegistry::getPassRegistry());
43 bool runOnMachineFunction(MachineFunction &MF) override;
45 StringRef getPassName() const override { return AVR_EXPAND_PSEUDO_NAME; }
48 typedef MachineBasicBlock Block;
49 typedef Block::iterator BlockIt;
51 const AVRRegisterInfo *TRI;
52 const TargetInstrInfo *TII;
54 /// The register to be used for temporary storage.
55 const unsigned SCRATCH_REGISTER = AVR::R0;
56 /// The IO address of the status register.
57 const unsigned SREG_ADDR = 0x3f;
59 bool expandMBB(Block &MBB);
60 bool expandMI(Block &MBB, BlockIt MBBI);
61 template <unsigned OP> bool expand(Block &MBB, BlockIt MBBI);
63 MachineInstrBuilder buildMI(Block &MBB, BlockIt MBBI, unsigned Opcode) {
64 return BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(Opcode));
67 MachineInstrBuilder buildMI(Block &MBB, BlockIt MBBI, unsigned Opcode,
69 return BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(Opcode), DstReg);
72 MachineRegisterInfo &getRegInfo(Block &MBB) { return MBB.getParent()->getRegInfo(); }
74 bool expandArith(unsigned OpLo, unsigned OpHi, Block &MBB, BlockIt MBBI);
75 bool expandLogic(unsigned Op, Block &MBB, BlockIt MBBI);
76 bool expandLogicImm(unsigned Op, Block &MBB, BlockIt MBBI);
77 bool isLogicImmOpRedundant(unsigned Op, unsigned ImmVal) const;
79 template<typename Func>
80 bool expandAtomic(Block &MBB, BlockIt MBBI, Func f);
82 template<typename Func>
83 bool expandAtomicBinaryOp(unsigned Opcode, Block &MBB, BlockIt MBBI, Func f);
85 bool expandAtomicBinaryOp(unsigned Opcode, Block &MBB, BlockIt MBBI);
87 bool expandAtomicArithmeticOp(unsigned MemOpcode,
92 /// Scavenges a free GPR8 register for use.
93 unsigned scavengeGPR8(MachineInstr &MI);
96 char AVRExpandPseudo::ID = 0;
98 bool AVRExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
99 bool Modified = false;
101 BlockIt MBBI = MBB.begin(), E = MBB.end();
103 BlockIt NMBBI = std::next(MBBI);
104 Modified |= expandMI(MBB, MBBI);
111 bool AVRExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
112 bool Modified = false;
114 const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
115 TRI = STI.getRegisterInfo();
116 TII = STI.getInstrInfo();
118 // We need to track liveness in order to use register scavenging.
119 MF.getProperties().set(MachineFunctionProperties::Property::TracksLiveness);
121 for (Block &MBB : MF) {
122 bool ContinueExpanding = true;
123 unsigned ExpandCount = 0;
125 // Continue expanding the block until all pseudos are expanded.
127 assert(ExpandCount < 10 && "pseudo expand limit reached");
129 bool BlockModified = expandMBB(MBB);
130 Modified |= BlockModified;
133 ContinueExpanding = BlockModified;
134 } while (ContinueExpanding);
140 bool AVRExpandPseudo::
141 expandArith(unsigned OpLo, unsigned OpHi, Block &MBB, BlockIt MBBI) {
142 MachineInstr &MI = *MBBI;
143 unsigned SrcLoReg, SrcHiReg, DstLoReg, DstHiReg;
144 unsigned DstReg = MI.getOperand(0).getReg();
145 unsigned SrcReg = MI.getOperand(2).getReg();
146 bool DstIsDead = MI.getOperand(0).isDead();
147 bool DstIsKill = MI.getOperand(1).isKill();
148 bool SrcIsKill = MI.getOperand(2).isKill();
149 bool ImpIsDead = MI.getOperand(3).isDead();
150 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
151 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
153 buildMI(MBB, MBBI, OpLo)
154 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
155 .addReg(DstLoReg, getKillRegState(DstIsKill))
156 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
158 auto MIBHI = buildMI(MBB, MBBI, OpHi)
159 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
160 .addReg(DstHiReg, getKillRegState(DstIsKill))
161 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
164 MIBHI->getOperand(3).setIsDead();
166 // SREG is always implicitly killed
167 MIBHI->getOperand(4).setIsKill();
169 MI.eraseFromParent();
173 bool AVRExpandPseudo::
174 expandLogic(unsigned Op, Block &MBB, BlockIt MBBI) {
175 MachineInstr &MI = *MBBI;
176 unsigned SrcLoReg, SrcHiReg, DstLoReg, DstHiReg;
177 unsigned DstReg = MI.getOperand(0).getReg();
178 unsigned SrcReg = MI.getOperand(2).getReg();
179 bool DstIsDead = MI.getOperand(0).isDead();
180 bool DstIsKill = MI.getOperand(1).isKill();
181 bool SrcIsKill = MI.getOperand(2).isKill();
182 bool ImpIsDead = MI.getOperand(3).isDead();
183 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
184 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
186 auto MIBLO = buildMI(MBB, MBBI, Op)
187 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
188 .addReg(DstLoReg, getKillRegState(DstIsKill))
189 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
191 // SREG is always implicitly dead
192 MIBLO->getOperand(3).setIsDead();
194 auto MIBHI = buildMI(MBB, MBBI, Op)
195 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
196 .addReg(DstHiReg, getKillRegState(DstIsKill))
197 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
200 MIBHI->getOperand(3).setIsDead();
202 MI.eraseFromParent();
206 bool AVRExpandPseudo::
207 isLogicImmOpRedundant(unsigned Op, unsigned ImmVal) const {
209 // ANDI Rd, 0xff is redundant.
210 if (Op == AVR::ANDIRdK && ImmVal == 0xff)
213 // ORI Rd, 0x0 is redundant.
214 if (Op == AVR::ORIRdK && ImmVal == 0x0)
220 bool AVRExpandPseudo::
221 expandLogicImm(unsigned Op, Block &MBB, BlockIt MBBI) {
222 MachineInstr &MI = *MBBI;
223 unsigned DstLoReg, DstHiReg;
224 unsigned DstReg = MI.getOperand(0).getReg();
225 bool DstIsDead = MI.getOperand(0).isDead();
226 bool SrcIsKill = MI.getOperand(1).isKill();
227 bool ImpIsDead = MI.getOperand(3).isDead();
228 unsigned Imm = MI.getOperand(2).getImm();
229 unsigned Lo8 = Imm & 0xff;
230 unsigned Hi8 = (Imm >> 8) & 0xff;
231 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
233 if (!isLogicImmOpRedundant(Op, Lo8)) {
234 auto MIBLO = buildMI(MBB, MBBI, Op)
235 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
236 .addReg(DstLoReg, getKillRegState(SrcIsKill))
239 // SREG is always implicitly dead
240 MIBLO->getOperand(3).setIsDead();
243 if (!isLogicImmOpRedundant(Op, Hi8)) {
244 auto MIBHI = buildMI(MBB, MBBI, Op)
245 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
246 .addReg(DstHiReg, getKillRegState(SrcIsKill))
250 MIBHI->getOperand(3).setIsDead();
253 MI.eraseFromParent();
258 bool AVRExpandPseudo::expand<AVR::ADDWRdRr>(Block &MBB, BlockIt MBBI) {
259 return expandArith(AVR::ADDRdRr, AVR::ADCRdRr, MBB, MBBI);
263 bool AVRExpandPseudo::expand<AVR::ADCWRdRr>(Block &MBB, BlockIt MBBI) {
264 return expandArith(AVR::ADCRdRr, AVR::ADCRdRr, MBB, MBBI);
268 bool AVRExpandPseudo::expand<AVR::SUBWRdRr>(Block &MBB, BlockIt MBBI) {
269 return expandArith(AVR::SUBRdRr, AVR::SBCRdRr, MBB, MBBI);
273 bool AVRExpandPseudo::expand<AVR::SUBIWRdK>(Block &MBB, BlockIt MBBI) {
274 MachineInstr &MI = *MBBI;
275 unsigned DstLoReg, DstHiReg;
276 unsigned DstReg = MI.getOperand(0).getReg();
277 bool DstIsDead = MI.getOperand(0).isDead();
278 bool SrcIsKill = MI.getOperand(1).isKill();
279 bool ImpIsDead = MI.getOperand(3).isDead();
280 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
282 auto MIBLO = buildMI(MBB, MBBI, AVR::SUBIRdK)
283 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
284 .addReg(DstLoReg, getKillRegState(SrcIsKill));
286 auto MIBHI = buildMI(MBB, MBBI, AVR::SBCIRdK)
287 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
288 .addReg(DstHiReg, getKillRegState(SrcIsKill));
290 switch (MI.getOperand(2).getType()) {
291 case MachineOperand::MO_GlobalAddress: {
292 const GlobalValue *GV = MI.getOperand(2).getGlobal();
293 int64_t Offs = MI.getOperand(2).getOffset();
294 unsigned TF = MI.getOperand(2).getTargetFlags();
295 MIBLO.addGlobalAddress(GV, Offs, TF | AVRII::MO_NEG | AVRII::MO_LO);
296 MIBHI.addGlobalAddress(GV, Offs, TF | AVRII::MO_NEG | AVRII::MO_HI);
299 case MachineOperand::MO_Immediate: {
300 unsigned Imm = MI.getOperand(2).getImm();
301 MIBLO.addImm(Imm & 0xff);
302 MIBHI.addImm((Imm >> 8) & 0xff);
306 llvm_unreachable("Unknown operand type!");
310 MIBHI->getOperand(3).setIsDead();
312 // SREG is always implicitly killed
313 MIBHI->getOperand(4).setIsKill();
315 MI.eraseFromParent();
320 bool AVRExpandPseudo::expand<AVR::SBCWRdRr>(Block &MBB, BlockIt MBBI) {
321 return expandArith(AVR::SBCRdRr, AVR::SBCRdRr, MBB, MBBI);
325 bool AVRExpandPseudo::expand<AVR::SBCIWRdK>(Block &MBB, BlockIt MBBI) {
326 MachineInstr &MI = *MBBI;
327 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
328 unsigned DstReg = MI.getOperand(0).getReg();
329 bool DstIsDead = MI.getOperand(0).isDead();
330 bool SrcIsKill = MI.getOperand(1).isKill();
331 bool ImpIsDead = MI.getOperand(3).isDead();
332 unsigned Imm = MI.getOperand(2).getImm();
333 unsigned Lo8 = Imm & 0xff;
334 unsigned Hi8 = (Imm >> 8) & 0xff;
337 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
339 auto MIBLO = buildMI(MBB, MBBI, OpLo)
340 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
341 .addReg(DstLoReg, getKillRegState(SrcIsKill))
344 // SREG is always implicitly killed
345 MIBLO->getOperand(4).setIsKill();
347 auto MIBHI = buildMI(MBB, MBBI, OpHi)
348 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
349 .addReg(DstHiReg, getKillRegState(SrcIsKill))
353 MIBHI->getOperand(3).setIsDead();
355 // SREG is always implicitly killed
356 MIBHI->getOperand(4).setIsKill();
358 MI.eraseFromParent();
363 bool AVRExpandPseudo::expand<AVR::ANDWRdRr>(Block &MBB, BlockIt MBBI) {
364 return expandLogic(AVR::ANDRdRr, MBB, MBBI);
368 bool AVRExpandPseudo::expand<AVR::ANDIWRdK>(Block &MBB, BlockIt MBBI) {
369 return expandLogicImm(AVR::ANDIRdK, MBB, MBBI);
373 bool AVRExpandPseudo::expand<AVR::ORWRdRr>(Block &MBB, BlockIt MBBI) {
374 return expandLogic(AVR::ORRdRr, MBB, MBBI);
378 bool AVRExpandPseudo::expand<AVR::ORIWRdK>(Block &MBB, BlockIt MBBI) {
379 return expandLogicImm(AVR::ORIRdK, MBB, MBBI);
383 bool AVRExpandPseudo::expand<AVR::EORWRdRr>(Block &MBB, BlockIt MBBI) {
384 return expandLogic(AVR::EORRdRr, MBB, MBBI);
388 bool AVRExpandPseudo::expand<AVR::COMWRd>(Block &MBB, BlockIt MBBI) {
389 MachineInstr &MI = *MBBI;
390 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
391 unsigned DstReg = MI.getOperand(0).getReg();
392 bool DstIsDead = MI.getOperand(0).isDead();
393 bool DstIsKill = MI.getOperand(1).isKill();
394 bool ImpIsDead = MI.getOperand(2).isDead();
397 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
399 auto MIBLO = buildMI(MBB, MBBI, OpLo)
400 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
401 .addReg(DstLoReg, getKillRegState(DstIsKill));
403 // SREG is always implicitly dead
404 MIBLO->getOperand(2).setIsDead();
406 auto MIBHI = buildMI(MBB, MBBI, OpHi)
407 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
408 .addReg(DstHiReg, getKillRegState(DstIsKill));
411 MIBHI->getOperand(2).setIsDead();
413 MI.eraseFromParent();
418 bool AVRExpandPseudo::expand<AVR::CPWRdRr>(Block &MBB, BlockIt MBBI) {
419 MachineInstr &MI = *MBBI;
420 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg, DstLoReg, DstHiReg;
421 unsigned DstReg = MI.getOperand(0).getReg();
422 unsigned SrcReg = MI.getOperand(1).getReg();
423 bool DstIsKill = MI.getOperand(0).isKill();
424 bool SrcIsKill = MI.getOperand(1).isKill();
425 bool ImpIsDead = MI.getOperand(2).isDead();
428 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
429 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
432 buildMI(MBB, MBBI, OpLo)
433 .addReg(DstLoReg, getKillRegState(DstIsKill))
434 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
436 auto MIBHI = buildMI(MBB, MBBI, OpHi)
437 .addReg(DstHiReg, getKillRegState(DstIsKill))
438 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
441 MIBHI->getOperand(2).setIsDead();
443 // SREG is always implicitly killed
444 MIBHI->getOperand(3).setIsKill();
446 MI.eraseFromParent();
451 bool AVRExpandPseudo::expand<AVR::CPCWRdRr>(Block &MBB, BlockIt MBBI) {
452 MachineInstr &MI = *MBBI;
453 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg, DstLoReg, DstHiReg;
454 unsigned DstReg = MI.getOperand(0).getReg();
455 unsigned SrcReg = MI.getOperand(1).getReg();
456 bool DstIsKill = MI.getOperand(0).isKill();
457 bool SrcIsKill = MI.getOperand(1).isKill();
458 bool ImpIsDead = MI.getOperand(2).isDead();
461 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
462 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
464 auto MIBLO = buildMI(MBB, MBBI, OpLo)
465 .addReg(DstLoReg, getKillRegState(DstIsKill))
466 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
468 // SREG is always implicitly killed
469 MIBLO->getOperand(3).setIsKill();
471 auto MIBHI = buildMI(MBB, MBBI, OpHi)
472 .addReg(DstHiReg, getKillRegState(DstIsKill))
473 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
476 MIBHI->getOperand(2).setIsDead();
478 // SREG is always implicitly killed
479 MIBHI->getOperand(3).setIsKill();
481 MI.eraseFromParent();
486 bool AVRExpandPseudo::expand<AVR::LDIWRdK>(Block &MBB, BlockIt MBBI) {
487 MachineInstr &MI = *MBBI;
488 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
489 unsigned DstReg = MI.getOperand(0).getReg();
490 bool DstIsDead = MI.getOperand(0).isDead();
493 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
495 auto MIBLO = buildMI(MBB, MBBI, OpLo)
496 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead));
498 auto MIBHI = buildMI(MBB, MBBI, OpHi)
499 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead));
501 switch (MI.getOperand(1).getType()) {
502 case MachineOperand::MO_GlobalAddress: {
503 const GlobalValue *GV = MI.getOperand(1).getGlobal();
504 int64_t Offs = MI.getOperand(1).getOffset();
505 unsigned TF = MI.getOperand(1).getTargetFlags();
507 MIBLO.addGlobalAddress(GV, Offs, TF | AVRII::MO_LO);
508 MIBHI.addGlobalAddress(GV, Offs, TF | AVRII::MO_HI);
511 case MachineOperand::MO_BlockAddress: {
512 const BlockAddress *BA = MI.getOperand(1).getBlockAddress();
513 unsigned TF = MI.getOperand(1).getTargetFlags();
515 MIBLO.add(MachineOperand::CreateBA(BA, TF | AVRII::MO_LO));
516 MIBHI.add(MachineOperand::CreateBA(BA, TF | AVRII::MO_HI));
519 case MachineOperand::MO_Immediate: {
520 unsigned Imm = MI.getOperand(1).getImm();
522 MIBLO.addImm(Imm & 0xff);
523 MIBHI.addImm((Imm >> 8) & 0xff);
527 llvm_unreachable("Unknown operand type!");
530 MI.eraseFromParent();
535 bool AVRExpandPseudo::expand<AVR::LDSWRdK>(Block &MBB, BlockIt MBBI) {
536 MachineInstr &MI = *MBBI;
537 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
538 unsigned DstReg = MI.getOperand(0).getReg();
539 bool DstIsDead = MI.getOperand(0).isDead();
542 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
544 auto MIBLO = buildMI(MBB, MBBI, OpLo)
545 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead));
547 auto MIBHI = buildMI(MBB, MBBI, OpHi)
548 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead));
550 switch (MI.getOperand(1).getType()) {
551 case MachineOperand::MO_GlobalAddress: {
552 const GlobalValue *GV = MI.getOperand(1).getGlobal();
553 int64_t Offs = MI.getOperand(1).getOffset();
554 unsigned TF = MI.getOperand(1).getTargetFlags();
556 MIBLO.addGlobalAddress(GV, Offs, TF);
557 MIBHI.addGlobalAddress(GV, Offs + 1, TF);
560 case MachineOperand::MO_Immediate: {
561 unsigned Imm = MI.getOperand(1).getImm();
564 MIBHI.addImm(Imm + 1);
568 llvm_unreachable("Unknown operand type!");
571 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
572 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
574 MI.eraseFromParent();
579 bool AVRExpandPseudo::expand<AVR::LDWRdPtr>(Block &MBB, BlockIt MBBI) {
580 MachineInstr &MI = *MBBI;
581 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
582 unsigned DstReg = MI.getOperand(0).getReg();
583 unsigned TmpReg = 0; // 0 for no temporary register
584 unsigned SrcReg = MI.getOperand(1).getReg();
585 bool SrcIsKill = MI.getOperand(1).isKill();
587 OpHi = AVR::LDDRdPtrQ;
588 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
590 // Use a temporary register if src and dst registers are the same.
591 if (DstReg == SrcReg)
592 TmpReg = scavengeGPR8(MI);
594 unsigned CurDstLoReg = (DstReg == SrcReg) ? TmpReg : DstLoReg;
595 unsigned CurDstHiReg = (DstReg == SrcReg) ? TmpReg : DstHiReg;
598 auto MIBLO = buildMI(MBB, MBBI, OpLo)
599 .addReg(CurDstLoReg, RegState::Define)
602 // Push low byte onto stack if necessary.
604 buildMI(MBB, MBBI, AVR::PUSHRr).addReg(TmpReg);
607 auto MIBHI = buildMI(MBB, MBBI, OpHi)
608 .addReg(CurDstHiReg, RegState::Define)
609 .addReg(SrcReg, getKillRegState(SrcIsKill))
613 // Move the high byte into the final destination.
614 buildMI(MBB, MBBI, AVR::MOVRdRr).addReg(DstHiReg).addReg(TmpReg);
616 // Move the low byte from the scratch space into the final destination.
617 buildMI(MBB, MBBI, AVR::POPRd).addReg(DstLoReg);
620 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
621 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
623 MI.eraseFromParent();
628 bool AVRExpandPseudo::expand<AVR::LDWRdPtrPi>(Block &MBB, BlockIt MBBI) {
629 MachineInstr &MI = *MBBI;
630 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
631 unsigned DstReg = MI.getOperand(0).getReg();
632 unsigned SrcReg = MI.getOperand(1).getReg();
633 bool DstIsDead = MI.getOperand(0).isDead();
634 bool SrcIsDead = MI.getOperand(1).isKill();
635 OpLo = AVR::LDRdPtrPi;
636 OpHi = AVR::LDRdPtrPi;
637 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
639 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same");
641 auto MIBLO = buildMI(MBB, MBBI, OpLo)
642 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
643 .addReg(SrcReg, RegState::Define)
644 .addReg(SrcReg, RegState::Kill);
646 auto MIBHI = buildMI(MBB, MBBI, OpHi)
647 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
648 .addReg(SrcReg, RegState::Define | getDeadRegState(SrcIsDead))
649 .addReg(SrcReg, RegState::Kill);
651 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
652 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
654 MI.eraseFromParent();
659 bool AVRExpandPseudo::expand<AVR::LDWRdPtrPd>(Block &MBB, BlockIt MBBI) {
660 MachineInstr &MI = *MBBI;
661 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
662 unsigned DstReg = MI.getOperand(0).getReg();
663 unsigned SrcReg = MI.getOperand(1).getReg();
664 bool DstIsDead = MI.getOperand(0).isDead();
665 bool SrcIsDead = MI.getOperand(1).isKill();
666 OpLo = AVR::LDRdPtrPd;
667 OpHi = AVR::LDRdPtrPd;
668 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
670 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same");
672 auto MIBHI = buildMI(MBB, MBBI, OpHi)
673 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
674 .addReg(SrcReg, RegState::Define)
675 .addReg(SrcReg, RegState::Kill);
677 auto MIBLO = buildMI(MBB, MBBI, OpLo)
678 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
679 .addReg(SrcReg, RegState::Define | getDeadRegState(SrcIsDead))
680 .addReg(SrcReg, RegState::Kill);
682 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
683 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
685 MI.eraseFromParent();
690 bool AVRExpandPseudo::expand<AVR::LDDWRdPtrQ>(Block &MBB, BlockIt MBBI) {
691 MachineInstr &MI = *MBBI;
692 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
693 unsigned DstReg = MI.getOperand(0).getReg();
694 unsigned TmpReg = 0; // 0 for no temporary register
695 unsigned SrcReg = MI.getOperand(1).getReg();
696 unsigned Imm = MI.getOperand(2).getImm();
697 bool SrcIsKill = MI.getOperand(1).isKill();
698 OpLo = AVR::LDDRdPtrQ;
699 OpHi = AVR::LDDRdPtrQ;
700 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
702 assert(Imm <= 63 && "Offset is out of range");
704 // Use a temporary register if src and dst registers are the same.
705 if (DstReg == SrcReg)
706 TmpReg = scavengeGPR8(MI);
708 unsigned CurDstLoReg = (DstReg == SrcReg) ? TmpReg : DstLoReg;
709 unsigned CurDstHiReg = (DstReg == SrcReg) ? TmpReg : DstHiReg;
712 auto MIBLO = buildMI(MBB, MBBI, OpLo)
713 .addReg(CurDstLoReg, RegState::Define)
717 // Push low byte onto stack if necessary.
719 buildMI(MBB, MBBI, AVR::PUSHRr).addReg(TmpReg);
722 auto MIBHI = buildMI(MBB, MBBI, OpHi)
723 .addReg(CurDstHiReg, RegState::Define)
724 .addReg(SrcReg, getKillRegState(SrcIsKill))
728 // Move the high byte into the final destination.
729 buildMI(MBB, MBBI, AVR::MOVRdRr).addReg(DstHiReg).addReg(TmpReg);
731 // Move the low byte from the scratch space into the final destination.
732 buildMI(MBB, MBBI, AVR::POPRd).addReg(DstLoReg);
735 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
736 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
738 MI.eraseFromParent();
743 bool AVRExpandPseudo::expand<AVR::LPMWRdZ>(Block &MBB, BlockIt MBBI) {
744 llvm_unreachable("wide LPM is unimplemented");
748 bool AVRExpandPseudo::expand<AVR::LPMWRdZPi>(Block &MBB, BlockIt MBBI) {
749 llvm_unreachable("wide LPMPi is unimplemented");
752 template<typename Func>
753 bool AVRExpandPseudo::expandAtomic(Block &MBB, BlockIt MBBI, Func f) {
754 // Remove the pseudo instruction.
755 MachineInstr &MI = *MBBI;
758 buildMI(MBB, MBBI, AVR::INRdA)
759 .addReg(SCRATCH_REGISTER, RegState::Define)
762 // Disable exceptions.
763 buildMI(MBB, MBBI, AVR::BCLRs).addImm(7); // CLI
767 // Restore the status reg.
768 buildMI(MBB, MBBI, AVR::OUTARr)
770 .addReg(SCRATCH_REGISTER);
772 MI.eraseFromParent();
776 template<typename Func>
777 bool AVRExpandPseudo::expandAtomicBinaryOp(unsigned Opcode,
781 return expandAtomic(MBB, MBBI, [&](MachineInstr &MI) {
782 auto Op1 = MI.getOperand(0);
783 auto Op2 = MI.getOperand(1);
785 MachineInstr &NewInst =
786 *buildMI(MBB, MBBI, Opcode).add(Op1).add(Op2).getInstr();
791 bool AVRExpandPseudo::expandAtomicBinaryOp(unsigned Opcode,
794 return expandAtomicBinaryOp(Opcode, MBB, MBBI, [](MachineInstr &MI) {});
797 bool AVRExpandPseudo::expandAtomicArithmeticOp(unsigned Width,
798 unsigned ArithOpcode,
801 return expandAtomic(MBB, MBBI, [&](MachineInstr &MI) {
802 auto Op1 = MI.getOperand(0);
803 auto Op2 = MI.getOperand(1);
805 unsigned LoadOpcode = (Width == 8) ? AVR::LDRdPtr : AVR::LDWRdPtr;
806 unsigned StoreOpcode = (Width == 8) ? AVR::STPtrRr : AVR::STWPtrRr;
809 buildMI(MBB, MBBI, LoadOpcode).add(Op1).add(Op2);
811 // Create the arithmetic op
812 buildMI(MBB, MBBI, ArithOpcode).add(Op1).add(Op1).add(Op2);
815 buildMI(MBB, MBBI, StoreOpcode).add(Op2).add(Op1);
819 unsigned AVRExpandPseudo::scavengeGPR8(MachineInstr &MI) {
820 MachineBasicBlock &MBB = *MI.getParent();
823 RS.enterBasicBlock(MBB);
826 BitVector Candidates =
827 TRI->getAllocatableSet
828 (*MBB.getParent(), &AVR::GPR8RegClass);
830 // Exclude all the registers being used by the instruction.
831 for (MachineOperand &MO : MI.operands()) {
832 if (MO.isReg() && MO.getReg() != 0 && !MO.isDef() &&
833 !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
834 Candidates.reset(MO.getReg());
837 BitVector Available = RS.getRegsAvailable(&AVR::GPR8RegClass);
838 Available &= Candidates;
840 signed Reg = Available.find_first();
841 assert(Reg != -1 && "ran out of registers");
846 bool AVRExpandPseudo::expand<AVR::AtomicLoad8>(Block &MBB, BlockIt MBBI) {
847 return expandAtomicBinaryOp(AVR::LDRdPtr, MBB, MBBI);
851 bool AVRExpandPseudo::expand<AVR::AtomicLoad16>(Block &MBB, BlockIt MBBI) {
852 return expandAtomicBinaryOp(AVR::LDWRdPtr, MBB, MBBI);
856 bool AVRExpandPseudo::expand<AVR::AtomicStore8>(Block &MBB, BlockIt MBBI) {
857 return expandAtomicBinaryOp(AVR::STPtrRr, MBB, MBBI);
861 bool AVRExpandPseudo::expand<AVR::AtomicStore16>(Block &MBB, BlockIt MBBI) {
862 return expandAtomicBinaryOp(AVR::STWPtrRr, MBB, MBBI);
866 bool AVRExpandPseudo::expand<AVR::AtomicLoadAdd8>(Block &MBB, BlockIt MBBI) {
867 return expandAtomicArithmeticOp(8, AVR::ADDRdRr, MBB, MBBI);
871 bool AVRExpandPseudo::expand<AVR::AtomicLoadAdd16>(Block &MBB, BlockIt MBBI) {
872 return expandAtomicArithmeticOp(16, AVR::ADDWRdRr, MBB, MBBI);
876 bool AVRExpandPseudo::expand<AVR::AtomicLoadSub8>(Block &MBB, BlockIt MBBI) {
877 return expandAtomicArithmeticOp(8, AVR::SUBRdRr, MBB, MBBI);
881 bool AVRExpandPseudo::expand<AVR::AtomicLoadSub16>(Block &MBB, BlockIt MBBI) {
882 return expandAtomicArithmeticOp(16, AVR::SUBWRdRr, MBB, MBBI);
886 bool AVRExpandPseudo::expand<AVR::AtomicLoadAnd8>(Block &MBB, BlockIt MBBI) {
887 return expandAtomicArithmeticOp(8, AVR::ANDRdRr, MBB, MBBI);
891 bool AVRExpandPseudo::expand<AVR::AtomicLoadAnd16>(Block &MBB, BlockIt MBBI) {
892 return expandAtomicArithmeticOp(16, AVR::ANDWRdRr, MBB, MBBI);
896 bool AVRExpandPseudo::expand<AVR::AtomicLoadOr8>(Block &MBB, BlockIt MBBI) {
897 return expandAtomicArithmeticOp(8, AVR::ORRdRr, MBB, MBBI);
901 bool AVRExpandPseudo::expand<AVR::AtomicLoadOr16>(Block &MBB, BlockIt MBBI) {
902 return expandAtomicArithmeticOp(16, AVR::ORWRdRr, MBB, MBBI);
906 bool AVRExpandPseudo::expand<AVR::AtomicLoadXor8>(Block &MBB, BlockIt MBBI) {
907 return expandAtomicArithmeticOp(8, AVR::EORRdRr, MBB, MBBI);
911 bool AVRExpandPseudo::expand<AVR::AtomicLoadXor16>(Block &MBB, BlockIt MBBI) {
912 return expandAtomicArithmeticOp(16, AVR::EORWRdRr, MBB, MBBI);
916 bool AVRExpandPseudo::expand<AVR::AtomicFence>(Block &MBB, BlockIt MBBI) {
917 // On AVR, there is only one core and so atomic fences do nothing.
918 MBBI->eraseFromParent();
923 bool AVRExpandPseudo::expand<AVR::STSWKRr>(Block &MBB, BlockIt MBBI) {
924 MachineInstr &MI = *MBBI;
925 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
926 unsigned SrcReg = MI.getOperand(1).getReg();
927 bool SrcIsKill = MI.getOperand(1).isKill();
930 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
932 // Write the high byte first in case this address belongs to a special
933 // I/O address with a special temporary register.
934 auto MIBHI = buildMI(MBB, MBBI, OpHi);
935 auto MIBLO = buildMI(MBB, MBBI, OpLo);
937 switch (MI.getOperand(0).getType()) {
938 case MachineOperand::MO_GlobalAddress: {
939 const GlobalValue *GV = MI.getOperand(0).getGlobal();
940 int64_t Offs = MI.getOperand(0).getOffset();
941 unsigned TF = MI.getOperand(0).getTargetFlags();
943 MIBLO.addGlobalAddress(GV, Offs, TF);
944 MIBHI.addGlobalAddress(GV, Offs + 1, TF);
947 case MachineOperand::MO_Immediate: {
948 unsigned Imm = MI.getOperand(0).getImm();
951 MIBHI.addImm(Imm + 1);
955 llvm_unreachable("Unknown operand type!");
958 MIBLO.addReg(SrcLoReg, getKillRegState(SrcIsKill));
959 MIBHI.addReg(SrcHiReg, getKillRegState(SrcIsKill));
961 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
962 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
964 MI.eraseFromParent();
969 bool AVRExpandPseudo::expand<AVR::STWPtrRr>(Block &MBB, BlockIt MBBI) {
970 MachineInstr &MI = *MBBI;
971 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
972 unsigned DstReg = MI.getOperand(0).getReg();
973 unsigned SrcReg = MI.getOperand(1).getReg();
974 bool SrcIsKill = MI.getOperand(1).isKill();
976 OpHi = AVR::STDPtrQRr;
977 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
979 //:TODO: need to reverse this order like inw and stsw?
980 auto MIBLO = buildMI(MBB, MBBI, OpLo)
982 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
984 auto MIBHI = buildMI(MBB, MBBI, OpHi)
987 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
989 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
990 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
992 MI.eraseFromParent();
997 bool AVRExpandPseudo::expand<AVR::STWPtrPiRr>(Block &MBB, BlockIt MBBI) {
998 MachineInstr &MI = *MBBI;
999 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
1000 unsigned DstReg = MI.getOperand(0).getReg();
1001 unsigned SrcReg = MI.getOperand(2).getReg();
1002 unsigned Imm = MI.getOperand(3).getImm();
1003 bool DstIsDead = MI.getOperand(0).isDead();
1004 bool SrcIsKill = MI.getOperand(2).isKill();
1005 OpLo = AVR::STPtrPiRr;
1006 OpHi = AVR::STPtrPiRr;
1007 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1009 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same");
1011 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1012 .addReg(DstReg, RegState::Define)
1013 .addReg(DstReg, RegState::Kill)
1014 .addReg(SrcLoReg, getKillRegState(SrcIsKill))
1017 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1018 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1019 .addReg(DstReg, RegState::Kill)
1020 .addReg(SrcHiReg, getKillRegState(SrcIsKill))
1023 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
1024 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
1026 MI.eraseFromParent();
1031 bool AVRExpandPseudo::expand<AVR::STWPtrPdRr>(Block &MBB, BlockIt MBBI) {
1032 MachineInstr &MI = *MBBI;
1033 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
1034 unsigned DstReg = MI.getOperand(0).getReg();
1035 unsigned SrcReg = MI.getOperand(2).getReg();
1036 unsigned Imm = MI.getOperand(3).getImm();
1037 bool DstIsDead = MI.getOperand(0).isDead();
1038 bool SrcIsKill = MI.getOperand(2).isKill();
1039 OpLo = AVR::STPtrPdRr;
1040 OpHi = AVR::STPtrPdRr;
1041 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1043 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same");
1045 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1046 .addReg(DstReg, RegState::Define)
1047 .addReg(DstReg, RegState::Kill)
1048 .addReg(SrcHiReg, getKillRegState(SrcIsKill))
1051 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1052 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1053 .addReg(DstReg, RegState::Kill)
1054 .addReg(SrcLoReg, getKillRegState(SrcIsKill))
1057 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
1058 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
1060 MI.eraseFromParent();
1065 bool AVRExpandPseudo::expand<AVR::STDWPtrQRr>(Block &MBB, BlockIt MBBI) {
1066 MachineInstr &MI = *MBBI;
1067 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
1068 unsigned DstReg = MI.getOperand(0).getReg();
1069 unsigned SrcReg = MI.getOperand(2).getReg();
1070 unsigned Imm = MI.getOperand(1).getImm();
1071 bool DstIsKill = MI.getOperand(0).isKill();
1072 bool SrcIsKill = MI.getOperand(2).isKill();
1073 OpLo = AVR::STDPtrQRr;
1074 OpHi = AVR::STDPtrQRr;
1075 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1077 assert(Imm <= 63 && "Offset is out of range");
1079 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1082 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
1084 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1085 .addReg(DstReg, getKillRegState(DstIsKill))
1087 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
1089 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
1090 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
1092 MI.eraseFromParent();
1097 bool AVRExpandPseudo::expand<AVR::INWRdA>(Block &MBB, BlockIt MBBI) {
1098 MachineInstr &MI = *MBBI;
1099 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1100 unsigned Imm = MI.getOperand(1).getImm();
1101 unsigned DstReg = MI.getOperand(0).getReg();
1102 bool DstIsDead = MI.getOperand(0).isDead();
1105 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1107 assert(Imm <= 63 && "Address is out of range");
1109 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1110 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1113 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1114 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1117 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
1118 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
1120 MI.eraseFromParent();
1125 bool AVRExpandPseudo::expand<AVR::OUTWARr>(Block &MBB, BlockIt MBBI) {
1126 MachineInstr &MI = *MBBI;
1127 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
1128 unsigned Imm = MI.getOperand(0).getImm();
1129 unsigned SrcReg = MI.getOperand(1).getReg();
1130 bool SrcIsKill = MI.getOperand(1).isKill();
1133 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1135 assert(Imm <= 63 && "Address is out of range");
1137 // 16 bit I/O writes need the high byte first
1138 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1140 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
1142 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1144 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
1146 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
1147 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
1149 MI.eraseFromParent();
1154 bool AVRExpandPseudo::expand<AVR::PUSHWRr>(Block &MBB, BlockIt MBBI) {
1155 MachineInstr &MI = *MBBI;
1156 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
1157 unsigned SrcReg = MI.getOperand(0).getReg();
1158 bool SrcIsKill = MI.getOperand(0).isKill();
1159 unsigned Flags = MI.getFlags();
1162 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1165 buildMI(MBB, MBBI, OpLo)
1166 .addReg(SrcLoReg, getKillRegState(SrcIsKill))
1170 buildMI(MBB, MBBI, OpHi)
1171 .addReg(SrcHiReg, getKillRegState(SrcIsKill))
1174 MI.eraseFromParent();
1179 bool AVRExpandPseudo::expand<AVR::POPWRd>(Block &MBB, BlockIt MBBI) {
1180 MachineInstr &MI = *MBBI;
1181 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1182 unsigned DstReg = MI.getOperand(0).getReg();
1183 unsigned Flags = MI.getFlags();
1186 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1188 buildMI(MBB, MBBI, OpHi, DstHiReg).setMIFlags(Flags); // High
1189 buildMI(MBB, MBBI, OpLo, DstLoReg).setMIFlags(Flags); // Low
1191 MI.eraseFromParent();
1196 bool AVRExpandPseudo::expand<AVR::LSLWRd>(Block &MBB, BlockIt MBBI) {
1197 MachineInstr &MI = *MBBI;
1198 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1199 unsigned DstReg = MI.getOperand(0).getReg();
1200 bool DstIsDead = MI.getOperand(0).isDead();
1201 bool DstIsKill = MI.getOperand(1).isKill();
1202 bool ImpIsDead = MI.getOperand(2).isDead();
1205 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1208 buildMI(MBB, MBBI, OpLo)
1209 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1210 .addReg(DstLoReg, getKillRegState(DstIsKill));
1212 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1213 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1214 .addReg(DstHiReg, getKillRegState(DstIsKill));
1217 MIBHI->getOperand(2).setIsDead();
1219 // SREG is always implicitly killed
1220 MIBHI->getOperand(3).setIsKill();
1222 MI.eraseFromParent();
1227 bool AVRExpandPseudo::expand<AVR::LSRWRd>(Block &MBB, BlockIt MBBI) {
1228 MachineInstr &MI = *MBBI;
1229 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1230 unsigned DstReg = MI.getOperand(0).getReg();
1231 bool DstIsDead = MI.getOperand(0).isDead();
1232 bool DstIsKill = MI.getOperand(1).isKill();
1233 bool ImpIsDead = MI.getOperand(2).isDead();
1236 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1239 buildMI(MBB, MBBI, OpHi)
1240 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1241 .addReg(DstHiReg, getKillRegState(DstIsKill));
1243 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1244 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1245 .addReg(DstLoReg, getKillRegState(DstIsKill));
1248 MIBLO->getOperand(2).setIsDead();
1250 // SREG is always implicitly killed
1251 MIBLO->getOperand(3).setIsKill();
1253 MI.eraseFromParent();
1258 bool AVRExpandPseudo::expand<AVR::RORWRd>(Block &MBB, BlockIt MBBI) {
1259 llvm_unreachable("RORW unimplemented");
1264 bool AVRExpandPseudo::expand<AVR::ROLWRd>(Block &MBB, BlockIt MBBI) {
1265 llvm_unreachable("ROLW unimplemented");
1270 bool AVRExpandPseudo::expand<AVR::ASRWRd>(Block &MBB, BlockIt MBBI) {
1271 MachineInstr &MI = *MBBI;
1272 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1273 unsigned DstReg = MI.getOperand(0).getReg();
1274 bool DstIsDead = MI.getOperand(0).isDead();
1275 bool DstIsKill = MI.getOperand(1).isKill();
1276 bool ImpIsDead = MI.getOperand(2).isDead();
1279 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1282 buildMI(MBB, MBBI, OpHi)
1283 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1284 .addReg(DstHiReg, getKillRegState(DstIsKill));
1286 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1287 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1288 .addReg(DstLoReg, getKillRegState(DstIsKill));
1291 MIBLO->getOperand(2).setIsDead();
1293 // SREG is always implicitly killed
1294 MIBLO->getOperand(3).setIsKill();
1296 MI.eraseFromParent();
1300 template <> bool AVRExpandPseudo::expand<AVR::SEXT>(Block &MBB, BlockIt MBBI) {
1301 MachineInstr &MI = *MBBI;
1302 unsigned DstLoReg, DstHiReg;
1303 // sext R17:R16, R17
1307 // sext R17:R16, R13
1312 // sext R17:R16, R16
1316 unsigned DstReg = MI.getOperand(0).getReg();
1317 unsigned SrcReg = MI.getOperand(1).getReg();
1318 bool DstIsDead = MI.getOperand(0).isDead();
1319 bool SrcIsKill = MI.getOperand(1).isKill();
1320 bool ImpIsDead = MI.getOperand(2).isDead();
1321 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1323 if (SrcReg != DstLoReg) {
1324 auto MOV = buildMI(MBB, MBBI, AVR::MOVRdRr)
1325 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1328 if (SrcReg == DstHiReg) {
1329 MOV->getOperand(1).setIsKill();
1333 if (SrcReg != DstHiReg) {
1334 buildMI(MBB, MBBI, AVR::MOVRdRr)
1335 .addReg(DstHiReg, RegState::Define)
1336 .addReg(SrcReg, getKillRegState(SrcIsKill));
1339 buildMI(MBB, MBBI, AVR::LSLRd)
1340 .addReg(DstHiReg, RegState::Define)
1341 .addReg(DstHiReg, RegState::Kill);
1343 auto SBC = buildMI(MBB, MBBI, AVR::SBCRdRr)
1344 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1345 .addReg(DstHiReg, RegState::Kill)
1346 .addReg(DstHiReg, RegState::Kill);
1349 SBC->getOperand(3).setIsDead();
1351 // SREG is always implicitly killed
1352 SBC->getOperand(4).setIsKill();
1354 MI.eraseFromParent();
1358 template <> bool AVRExpandPseudo::expand<AVR::ZEXT>(Block &MBB, BlockIt MBBI) {
1359 MachineInstr &MI = *MBBI;
1360 unsigned DstLoReg, DstHiReg;
1361 // zext R25:R24, R20
1364 // zext R25:R24, R24
1366 // zext R25:R24, R25
1369 unsigned DstReg = MI.getOperand(0).getReg();
1370 unsigned SrcReg = MI.getOperand(1).getReg();
1371 bool DstIsDead = MI.getOperand(0).isDead();
1372 bool SrcIsKill = MI.getOperand(1).isKill();
1373 bool ImpIsDead = MI.getOperand(2).isDead();
1374 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1376 if (SrcReg != DstLoReg) {
1377 buildMI(MBB, MBBI, AVR::MOVRdRr)
1378 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1379 .addReg(SrcReg, getKillRegState(SrcIsKill));
1382 auto EOR = buildMI(MBB, MBBI, AVR::EORRdRr)
1383 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1384 .addReg(DstHiReg, RegState::Kill)
1385 .addReg(DstHiReg, RegState::Kill);
1388 EOR->getOperand(3).setIsDead();
1390 MI.eraseFromParent();
1395 bool AVRExpandPseudo::expand<AVR::SPREAD>(Block &MBB, BlockIt MBBI) {
1396 MachineInstr &MI = *MBBI;
1397 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1398 unsigned DstReg = MI.getOperand(0).getReg();
1399 bool DstIsDead = MI.getOperand(0).isDead();
1400 unsigned Flags = MI.getFlags();
1403 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1406 buildMI(MBB, MBBI, OpLo)
1407 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1412 buildMI(MBB, MBBI, OpHi)
1413 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1417 MI.eraseFromParent();
1422 bool AVRExpandPseudo::expand<AVR::SPWRITE>(Block &MBB, BlockIt MBBI) {
1423 MachineInstr &MI = *MBBI;
1424 unsigned SrcLoReg, SrcHiReg;
1425 unsigned SrcReg = MI.getOperand(1).getReg();
1426 bool SrcIsKill = MI.getOperand(1).isKill();
1427 unsigned Flags = MI.getFlags();
1428 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1430 buildMI(MBB, MBBI, AVR::INRdA)
1431 .addReg(AVR::R0, RegState::Define)
1435 buildMI(MBB, MBBI, AVR::BCLRs).addImm(0x07).setMIFlags(Flags);
1437 buildMI(MBB, MBBI, AVR::OUTARr)
1439 .addReg(SrcHiReg, getKillRegState(SrcIsKill))
1442 buildMI(MBB, MBBI, AVR::OUTARr)
1444 .addReg(AVR::R0, RegState::Kill)
1447 buildMI(MBB, MBBI, AVR::OUTARr)
1449 .addReg(SrcLoReg, getKillRegState(SrcIsKill))
1452 MI.eraseFromParent();
1456 bool AVRExpandPseudo::expandMI(Block &MBB, BlockIt MBBI) {
1457 MachineInstr &MI = *MBBI;
1458 int Opcode = MBBI->getOpcode();
1460 #define EXPAND(Op) \
1462 return expand<Op>(MBB, MI)
1465 EXPAND(AVR::ADDWRdRr);
1466 EXPAND(AVR::ADCWRdRr);
1467 EXPAND(AVR::SUBWRdRr);
1468 EXPAND(AVR::SUBIWRdK);
1469 EXPAND(AVR::SBCWRdRr);
1470 EXPAND(AVR::SBCIWRdK);
1471 EXPAND(AVR::ANDWRdRr);
1472 EXPAND(AVR::ANDIWRdK);
1473 EXPAND(AVR::ORWRdRr);
1474 EXPAND(AVR::ORIWRdK);
1475 EXPAND(AVR::EORWRdRr);
1476 EXPAND(AVR::COMWRd);
1477 EXPAND(AVR::CPWRdRr);
1478 EXPAND(AVR::CPCWRdRr);
1479 EXPAND(AVR::LDIWRdK);
1480 EXPAND(AVR::LDSWRdK);
1481 EXPAND(AVR::LDWRdPtr);
1482 EXPAND(AVR::LDWRdPtrPi);
1483 EXPAND(AVR::LDWRdPtrPd);
1484 case AVR::LDDWRdYQ: //:FIXME: remove this once PR13375 gets fixed
1485 EXPAND(AVR::LDDWRdPtrQ);
1486 EXPAND(AVR::LPMWRdZ);
1487 EXPAND(AVR::LPMWRdZPi);
1488 EXPAND(AVR::AtomicLoad8);
1489 EXPAND(AVR::AtomicLoad16);
1490 EXPAND(AVR::AtomicStore8);
1491 EXPAND(AVR::AtomicStore16);
1492 EXPAND(AVR::AtomicLoadAdd8);
1493 EXPAND(AVR::AtomicLoadAdd16);
1494 EXPAND(AVR::AtomicLoadSub8);
1495 EXPAND(AVR::AtomicLoadSub16);
1496 EXPAND(AVR::AtomicLoadAnd8);
1497 EXPAND(AVR::AtomicLoadAnd16);
1498 EXPAND(AVR::AtomicLoadOr8);
1499 EXPAND(AVR::AtomicLoadOr16);
1500 EXPAND(AVR::AtomicLoadXor8);
1501 EXPAND(AVR::AtomicLoadXor16);
1502 EXPAND(AVR::AtomicFence);
1503 EXPAND(AVR::STSWKRr);
1504 EXPAND(AVR::STWPtrRr);
1505 EXPAND(AVR::STWPtrPiRr);
1506 EXPAND(AVR::STWPtrPdRr);
1507 EXPAND(AVR::STDWPtrQRr);
1508 EXPAND(AVR::INWRdA);
1509 EXPAND(AVR::OUTWARr);
1510 EXPAND(AVR::PUSHWRr);
1511 EXPAND(AVR::POPWRd);
1512 EXPAND(AVR::LSLWRd);
1513 EXPAND(AVR::LSRWRd);
1514 EXPAND(AVR::RORWRd);
1515 EXPAND(AVR::ROLWRd);
1516 EXPAND(AVR::ASRWRd);
1519 EXPAND(AVR::SPREAD);
1520 EXPAND(AVR::SPWRITE);
1526 } // end of anonymous namespace
1528 INITIALIZE_PASS(AVRExpandPseudo, "avr-expand-pseudo",
1529 AVR_EXPAND_PSEUDO_NAME, false, false)
1532 FunctionPass *createAVRExpandPseudoPass() { return new AVRExpandPseudo(); }
1534 } // end of namespace llvm