1 //===-- AVRExpandPseudoInsts.cpp - Expand pseudo instructions -------------===//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
16 #include "AVRInstrInfo.h"
17 #include "AVRTargetMachine.h"
18 #include "MCTargetDesc/AVRMCTargetDesc.h"
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"
28 #define AVR_EXPAND_PSEUDO_NAME "AVR pseudo instruction expansion pass"
32 /// Expands "placeholder" instructions marked as pseudo into
33 /// actual AVR instructions.
34 class AVRExpandPseudo : public MachineFunctionPass {
38 AVRExpandPseudo() : MachineFunctionPass(ID) {
39 initializeAVRExpandPseudoPass(*PassRegistry::getPassRegistry());
42 bool runOnMachineFunction(MachineFunction &MF) override;
44 StringRef getPassName() const override { return AVR_EXPAND_PSEUDO_NAME; }
47 typedef MachineBasicBlock Block;
48 typedef Block::iterator BlockIt;
50 const AVRRegisterInfo *TRI;
51 const TargetInstrInfo *TII;
53 /// The register to be used for temporary storage.
54 const unsigned SCRATCH_REGISTER = AVR::R0;
55 /// The register that will always contain zero.
56 const unsigned ZERO_REGISTER = AVR::R1;
57 /// The IO address of the status register.
58 const unsigned SREG_ADDR = 0x3f;
60 bool expandMBB(Block &MBB);
61 bool expandMI(Block &MBB, BlockIt MBBI);
62 template <unsigned OP> bool expand(Block &MBB, BlockIt MBBI);
64 MachineInstrBuilder buildMI(Block &MBB, BlockIt MBBI, unsigned Opcode) {
65 return BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(Opcode));
68 MachineInstrBuilder buildMI(Block &MBB, BlockIt MBBI, unsigned Opcode,
70 return BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(Opcode), DstReg);
73 MachineRegisterInfo &getRegInfo(Block &MBB) { return MBB.getParent()->getRegInfo(); }
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;
80 template<typename Func>
81 bool expandAtomic(Block &MBB, BlockIt MBBI, Func f);
83 template<typename Func>
84 bool expandAtomicBinaryOp(unsigned Opcode, Block &MBB, BlockIt MBBI, Func f);
86 bool expandAtomicBinaryOp(unsigned Opcode, Block &MBB, BlockIt MBBI);
88 bool expandAtomicArithmeticOp(unsigned MemOpcode,
93 /// Scavenges a free GPR8 register for use.
94 unsigned scavengeGPR8(MachineInstr &MI);
97 char AVRExpandPseudo::ID = 0;
99 bool AVRExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
100 bool Modified = false;
102 BlockIt MBBI = MBB.begin(), E = MBB.end();
104 BlockIt NMBBI = std::next(MBBI);
105 Modified |= expandMI(MBB, MBBI);
112 bool AVRExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
113 bool Modified = false;
115 const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
116 TRI = STI.getRegisterInfo();
117 TII = STI.getInstrInfo();
119 // We need to track liveness in order to use register scavenging.
120 MF.getProperties().set(MachineFunctionProperties::Property::TracksLiveness);
122 for (Block &MBB : MF) {
123 bool ContinueExpanding = true;
124 unsigned ExpandCount = 0;
126 // Continue expanding the block until all pseudos are expanded.
128 assert(ExpandCount < 10 && "pseudo expand limit reached");
130 bool BlockModified = expandMBB(MBB);
131 Modified |= BlockModified;
134 ContinueExpanding = BlockModified;
135 } while (ContinueExpanding);
141 bool AVRExpandPseudo::
142 expandArith(unsigned OpLo, unsigned OpHi, Block &MBB, BlockIt MBBI) {
143 MachineInstr &MI = *MBBI;
144 unsigned SrcLoReg, SrcHiReg, DstLoReg, DstHiReg;
145 Register DstReg = MI.getOperand(0).getReg();
146 Register SrcReg = MI.getOperand(2).getReg();
147 bool DstIsDead = MI.getOperand(0).isDead();
148 bool DstIsKill = MI.getOperand(1).isKill();
149 bool SrcIsKill = MI.getOperand(2).isKill();
150 bool ImpIsDead = MI.getOperand(3).isDead();
151 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
152 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
154 buildMI(MBB, MBBI, OpLo)
155 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
156 .addReg(DstLoReg, getKillRegState(DstIsKill))
157 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
159 auto MIBHI = buildMI(MBB, MBBI, OpHi)
160 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
161 .addReg(DstHiReg, getKillRegState(DstIsKill))
162 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
165 MIBHI->getOperand(3).setIsDead();
167 // SREG is always implicitly killed
168 MIBHI->getOperand(4).setIsKill();
170 MI.eraseFromParent();
174 bool AVRExpandPseudo::
175 expandLogic(unsigned Op, Block &MBB, BlockIt MBBI) {
176 MachineInstr &MI = *MBBI;
177 unsigned SrcLoReg, SrcHiReg, DstLoReg, DstHiReg;
178 Register DstReg = MI.getOperand(0).getReg();
179 Register SrcReg = MI.getOperand(2).getReg();
180 bool DstIsDead = MI.getOperand(0).isDead();
181 bool DstIsKill = MI.getOperand(1).isKill();
182 bool SrcIsKill = MI.getOperand(2).isKill();
183 bool ImpIsDead = MI.getOperand(3).isDead();
184 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
185 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
187 auto MIBLO = buildMI(MBB, MBBI, Op)
188 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
189 .addReg(DstLoReg, getKillRegState(DstIsKill))
190 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
192 // SREG is always implicitly dead
193 MIBLO->getOperand(3).setIsDead();
195 auto MIBHI = buildMI(MBB, MBBI, Op)
196 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
197 .addReg(DstHiReg, getKillRegState(DstIsKill))
198 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
201 MIBHI->getOperand(3).setIsDead();
203 MI.eraseFromParent();
207 bool AVRExpandPseudo::
208 isLogicImmOpRedundant(unsigned Op, unsigned ImmVal) const {
210 // ANDI Rd, 0xff is redundant.
211 if (Op == AVR::ANDIRdK && ImmVal == 0xff)
214 // ORI Rd, 0x0 is redundant.
215 if (Op == AVR::ORIRdK && ImmVal == 0x0)
221 bool AVRExpandPseudo::
222 expandLogicImm(unsigned Op, Block &MBB, BlockIt MBBI) {
223 MachineInstr &MI = *MBBI;
224 unsigned DstLoReg, DstHiReg;
225 Register DstReg = MI.getOperand(0).getReg();
226 bool DstIsDead = MI.getOperand(0).isDead();
227 bool SrcIsKill = MI.getOperand(1).isKill();
228 bool ImpIsDead = MI.getOperand(3).isDead();
229 unsigned Imm = MI.getOperand(2).getImm();
230 unsigned Lo8 = Imm & 0xff;
231 unsigned Hi8 = (Imm >> 8) & 0xff;
232 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
234 if (!isLogicImmOpRedundant(Op, Lo8)) {
235 auto MIBLO = buildMI(MBB, MBBI, Op)
236 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
237 .addReg(DstLoReg, getKillRegState(SrcIsKill))
240 // SREG is always implicitly dead
241 MIBLO->getOperand(3).setIsDead();
244 if (!isLogicImmOpRedundant(Op, Hi8)) {
245 auto MIBHI = buildMI(MBB, MBBI, Op)
246 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
247 .addReg(DstHiReg, getKillRegState(SrcIsKill))
251 MIBHI->getOperand(3).setIsDead();
254 MI.eraseFromParent();
259 bool AVRExpandPseudo::expand<AVR::ADDWRdRr>(Block &MBB, BlockIt MBBI) {
260 return expandArith(AVR::ADDRdRr, AVR::ADCRdRr, MBB, MBBI);
264 bool AVRExpandPseudo::expand<AVR::ADCWRdRr>(Block &MBB, BlockIt MBBI) {
265 return expandArith(AVR::ADCRdRr, AVR::ADCRdRr, MBB, MBBI);
269 bool AVRExpandPseudo::expand<AVR::SUBWRdRr>(Block &MBB, BlockIt MBBI) {
270 return expandArith(AVR::SUBRdRr, AVR::SBCRdRr, MBB, MBBI);
274 bool AVRExpandPseudo::expand<AVR::SUBIWRdK>(Block &MBB, BlockIt MBBI) {
275 MachineInstr &MI = *MBBI;
276 unsigned DstLoReg, DstHiReg;
277 unsigned DstReg = MI.getOperand(0).getReg();
278 bool DstIsDead = MI.getOperand(0).isDead();
279 bool SrcIsKill = MI.getOperand(1).isKill();
280 bool ImpIsDead = MI.getOperand(3).isDead();
281 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
283 auto MIBLO = buildMI(MBB, MBBI, AVR::SUBIRdK)
284 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
285 .addReg(DstLoReg, getKillRegState(SrcIsKill));
287 auto MIBHI = buildMI(MBB, MBBI, AVR::SBCIRdK)
288 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
289 .addReg(DstHiReg, getKillRegState(SrcIsKill));
291 switch (MI.getOperand(2).getType()) {
292 case MachineOperand::MO_GlobalAddress: {
293 const GlobalValue *GV = MI.getOperand(2).getGlobal();
294 int64_t Offs = MI.getOperand(2).getOffset();
295 unsigned TF = MI.getOperand(2).getTargetFlags();
296 MIBLO.addGlobalAddress(GV, Offs, TF | AVRII::MO_NEG | AVRII::MO_LO);
297 MIBHI.addGlobalAddress(GV, Offs, TF | AVRII::MO_NEG | AVRII::MO_HI);
300 case MachineOperand::MO_Immediate: {
301 unsigned Imm = MI.getOperand(2).getImm();
302 MIBLO.addImm(Imm & 0xff);
303 MIBHI.addImm((Imm >> 8) & 0xff);
307 llvm_unreachable("Unknown operand type!");
311 MIBHI->getOperand(3).setIsDead();
313 // SREG is always implicitly killed
314 MIBHI->getOperand(4).setIsKill();
316 MI.eraseFromParent();
321 bool AVRExpandPseudo::expand<AVR::SBCWRdRr>(Block &MBB, BlockIt MBBI) {
322 return expandArith(AVR::SBCRdRr, AVR::SBCRdRr, MBB, MBBI);
326 bool AVRExpandPseudo::expand<AVR::SBCIWRdK>(Block &MBB, BlockIt MBBI) {
327 MachineInstr &MI = *MBBI;
328 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
329 unsigned DstReg = MI.getOperand(0).getReg();
330 bool DstIsDead = MI.getOperand(0).isDead();
331 bool SrcIsKill = MI.getOperand(1).isKill();
332 bool ImpIsDead = MI.getOperand(3).isDead();
333 unsigned Imm = MI.getOperand(2).getImm();
334 unsigned Lo8 = Imm & 0xff;
335 unsigned Hi8 = (Imm >> 8) & 0xff;
338 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
340 auto MIBLO = buildMI(MBB, MBBI, OpLo)
341 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
342 .addReg(DstLoReg, getKillRegState(SrcIsKill))
345 // SREG is always implicitly killed
346 MIBLO->getOperand(4).setIsKill();
348 auto MIBHI = buildMI(MBB, MBBI, OpHi)
349 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
350 .addReg(DstHiReg, getKillRegState(SrcIsKill))
354 MIBHI->getOperand(3).setIsDead();
356 // SREG is always implicitly killed
357 MIBHI->getOperand(4).setIsKill();
359 MI.eraseFromParent();
364 bool AVRExpandPseudo::expand<AVR::ANDWRdRr>(Block &MBB, BlockIt MBBI) {
365 return expandLogic(AVR::ANDRdRr, MBB, MBBI);
369 bool AVRExpandPseudo::expand<AVR::ANDIWRdK>(Block &MBB, BlockIt MBBI) {
370 return expandLogicImm(AVR::ANDIRdK, MBB, MBBI);
374 bool AVRExpandPseudo::expand<AVR::ORWRdRr>(Block &MBB, BlockIt MBBI) {
375 return expandLogic(AVR::ORRdRr, MBB, MBBI);
379 bool AVRExpandPseudo::expand<AVR::ORIWRdK>(Block &MBB, BlockIt MBBI) {
380 return expandLogicImm(AVR::ORIRdK, MBB, MBBI);
384 bool AVRExpandPseudo::expand<AVR::EORWRdRr>(Block &MBB, BlockIt MBBI) {
385 return expandLogic(AVR::EORRdRr, MBB, MBBI);
389 bool AVRExpandPseudo::expand<AVR::COMWRd>(Block &MBB, BlockIt MBBI) {
390 MachineInstr &MI = *MBBI;
391 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
392 unsigned DstReg = MI.getOperand(0).getReg();
393 bool DstIsDead = MI.getOperand(0).isDead();
394 bool DstIsKill = MI.getOperand(1).isKill();
395 bool ImpIsDead = MI.getOperand(2).isDead();
398 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
400 auto MIBLO = buildMI(MBB, MBBI, OpLo)
401 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
402 .addReg(DstLoReg, getKillRegState(DstIsKill));
404 // SREG is always implicitly dead
405 MIBLO->getOperand(2).setIsDead();
407 auto MIBHI = buildMI(MBB, MBBI, OpHi)
408 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
409 .addReg(DstHiReg, getKillRegState(DstIsKill));
412 MIBHI->getOperand(2).setIsDead();
414 MI.eraseFromParent();
419 bool AVRExpandPseudo::expand<AVR::CPWRdRr>(Block &MBB, BlockIt MBBI) {
420 MachineInstr &MI = *MBBI;
421 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg, DstLoReg, DstHiReg;
422 unsigned DstReg = MI.getOperand(0).getReg();
423 unsigned SrcReg = MI.getOperand(1).getReg();
424 bool DstIsKill = MI.getOperand(0).isKill();
425 bool SrcIsKill = MI.getOperand(1).isKill();
426 bool ImpIsDead = MI.getOperand(2).isDead();
429 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
430 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
433 buildMI(MBB, MBBI, OpLo)
434 .addReg(DstLoReg, getKillRegState(DstIsKill))
435 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
437 auto MIBHI = buildMI(MBB, MBBI, OpHi)
438 .addReg(DstHiReg, getKillRegState(DstIsKill))
439 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
442 MIBHI->getOperand(2).setIsDead();
444 // SREG is always implicitly killed
445 MIBHI->getOperand(3).setIsKill();
447 MI.eraseFromParent();
452 bool AVRExpandPseudo::expand<AVR::CPCWRdRr>(Block &MBB, BlockIt MBBI) {
453 MachineInstr &MI = *MBBI;
454 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg, DstLoReg, DstHiReg;
455 unsigned DstReg = MI.getOperand(0).getReg();
456 unsigned SrcReg = MI.getOperand(1).getReg();
457 bool DstIsKill = MI.getOperand(0).isKill();
458 bool SrcIsKill = MI.getOperand(1).isKill();
459 bool ImpIsDead = MI.getOperand(2).isDead();
462 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
463 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
465 auto MIBLO = buildMI(MBB, MBBI, OpLo)
466 .addReg(DstLoReg, getKillRegState(DstIsKill))
467 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
469 // SREG is always implicitly killed
470 MIBLO->getOperand(3).setIsKill();
472 auto MIBHI = buildMI(MBB, MBBI, OpHi)
473 .addReg(DstHiReg, getKillRegState(DstIsKill))
474 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
477 MIBHI->getOperand(2).setIsDead();
479 // SREG is always implicitly killed
480 MIBHI->getOperand(3).setIsKill();
482 MI.eraseFromParent();
487 bool AVRExpandPseudo::expand<AVR::LDIWRdK>(Block &MBB, BlockIt MBBI) {
488 MachineInstr &MI = *MBBI;
489 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
490 unsigned DstReg = MI.getOperand(0).getReg();
491 bool DstIsDead = MI.getOperand(0).isDead();
494 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
496 auto MIBLO = buildMI(MBB, MBBI, OpLo)
497 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead));
499 auto MIBHI = buildMI(MBB, MBBI, OpHi)
500 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead));
502 switch (MI.getOperand(1).getType()) {
503 case MachineOperand::MO_GlobalAddress: {
504 const GlobalValue *GV = MI.getOperand(1).getGlobal();
505 int64_t Offs = MI.getOperand(1).getOffset();
506 unsigned TF = MI.getOperand(1).getTargetFlags();
508 MIBLO.addGlobalAddress(GV, Offs, TF | AVRII::MO_LO);
509 MIBHI.addGlobalAddress(GV, Offs, TF | AVRII::MO_HI);
512 case MachineOperand::MO_BlockAddress: {
513 const BlockAddress *BA = MI.getOperand(1).getBlockAddress();
514 unsigned TF = MI.getOperand(1).getTargetFlags();
516 MIBLO.add(MachineOperand::CreateBA(BA, TF | AVRII::MO_LO));
517 MIBHI.add(MachineOperand::CreateBA(BA, TF | AVRII::MO_HI));
520 case MachineOperand::MO_Immediate: {
521 unsigned Imm = MI.getOperand(1).getImm();
523 MIBLO.addImm(Imm & 0xff);
524 MIBHI.addImm((Imm >> 8) & 0xff);
528 llvm_unreachable("Unknown operand type!");
531 MI.eraseFromParent();
536 bool AVRExpandPseudo::expand<AVR::LDSWRdK>(Block &MBB, BlockIt MBBI) {
537 MachineInstr &MI = *MBBI;
538 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
539 unsigned DstReg = MI.getOperand(0).getReg();
540 bool DstIsDead = MI.getOperand(0).isDead();
543 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
545 auto MIBLO = buildMI(MBB, MBBI, OpLo)
546 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead));
548 auto MIBHI = buildMI(MBB, MBBI, OpHi)
549 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead));
551 switch (MI.getOperand(1).getType()) {
552 case MachineOperand::MO_GlobalAddress: {
553 const GlobalValue *GV = MI.getOperand(1).getGlobal();
554 int64_t Offs = MI.getOperand(1).getOffset();
555 unsigned TF = MI.getOperand(1).getTargetFlags();
557 MIBLO.addGlobalAddress(GV, Offs, TF);
558 MIBHI.addGlobalAddress(GV, Offs + 1, TF);
561 case MachineOperand::MO_Immediate: {
562 unsigned Imm = MI.getOperand(1).getImm();
565 MIBHI.addImm(Imm + 1);
569 llvm_unreachable("Unknown operand type!");
572 MIBLO.setMemRefs(MI.memoperands());
573 MIBHI.setMemRefs(MI.memoperands());
575 MI.eraseFromParent();
580 bool AVRExpandPseudo::expand<AVR::LDWRdPtr>(Block &MBB, BlockIt MBBI) {
581 MachineInstr &MI = *MBBI;
582 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
583 unsigned DstReg = MI.getOperand(0).getReg();
584 unsigned TmpReg = 0; // 0 for no temporary register
585 unsigned SrcReg = MI.getOperand(1).getReg();
586 bool SrcIsKill = MI.getOperand(1).isKill();
588 OpHi = AVR::LDDRdPtrQ;
589 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
591 // Use a temporary register if src and dst registers are the same.
592 if (DstReg == SrcReg)
593 TmpReg = scavengeGPR8(MI);
595 unsigned CurDstLoReg = (DstReg == SrcReg) ? TmpReg : DstLoReg;
596 unsigned CurDstHiReg = (DstReg == SrcReg) ? TmpReg : DstHiReg;
599 auto MIBLO = buildMI(MBB, MBBI, OpLo)
600 .addReg(CurDstLoReg, RegState::Define)
601 .addReg(SrcReg, RegState::Define);
603 // Push low byte onto stack if necessary.
605 buildMI(MBB, MBBI, AVR::PUSHRr).addReg(TmpReg);
608 auto MIBHI = buildMI(MBB, MBBI, OpHi)
609 .addReg(CurDstHiReg, RegState::Define)
610 .addReg(SrcReg, getKillRegState(SrcIsKill))
614 // Move the high byte into the final destination.
615 buildMI(MBB, MBBI, AVR::MOVRdRr).addReg(DstHiReg).addReg(TmpReg);
617 // Move the low byte from the scratch space into the final destination.
618 buildMI(MBB, MBBI, AVR::POPRd).addReg(DstLoReg);
621 MIBLO.setMemRefs(MI.memoperands());
622 MIBHI.setMemRefs(MI.memoperands());
624 MI.eraseFromParent();
629 bool AVRExpandPseudo::expand<AVR::LDWRdPtrPi>(Block &MBB, BlockIt MBBI) {
630 MachineInstr &MI = *MBBI;
631 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
632 unsigned DstReg = MI.getOperand(0).getReg();
633 unsigned SrcReg = MI.getOperand(1).getReg();
634 bool DstIsDead = MI.getOperand(0).isDead();
635 bool SrcIsDead = MI.getOperand(1).isKill();
636 OpLo = AVR::LDRdPtrPi;
637 OpHi = AVR::LDRdPtrPi;
638 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
640 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same");
642 auto MIBLO = buildMI(MBB, MBBI, OpLo)
643 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
644 .addReg(SrcReg, RegState::Define)
645 .addReg(SrcReg, RegState::Kill);
647 auto MIBHI = buildMI(MBB, MBBI, OpHi)
648 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
649 .addReg(SrcReg, RegState::Define | getDeadRegState(SrcIsDead))
650 .addReg(SrcReg, RegState::Kill);
652 MIBLO.setMemRefs(MI.memoperands());
653 MIBHI.setMemRefs(MI.memoperands());
655 MI.eraseFromParent();
660 bool AVRExpandPseudo::expand<AVR::LDWRdPtrPd>(Block &MBB, BlockIt MBBI) {
661 MachineInstr &MI = *MBBI;
662 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
663 unsigned DstReg = MI.getOperand(0).getReg();
664 unsigned SrcReg = MI.getOperand(1).getReg();
665 bool DstIsDead = MI.getOperand(0).isDead();
666 bool SrcIsDead = MI.getOperand(1).isKill();
667 OpLo = AVR::LDRdPtrPd;
668 OpHi = AVR::LDRdPtrPd;
669 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
671 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same");
673 auto MIBHI = buildMI(MBB, MBBI, OpHi)
674 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
675 .addReg(SrcReg, RegState::Define)
676 .addReg(SrcReg, RegState::Kill);
678 auto MIBLO = buildMI(MBB, MBBI, OpLo)
679 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
680 .addReg(SrcReg, RegState::Define | getDeadRegState(SrcIsDead))
681 .addReg(SrcReg, RegState::Kill);
683 MIBLO.setMemRefs(MI.memoperands());
684 MIBHI.setMemRefs(MI.memoperands());
686 MI.eraseFromParent();
691 bool AVRExpandPseudo::expand<AVR::LDDWRdPtrQ>(Block &MBB, BlockIt MBBI) {
692 MachineInstr &MI = *MBBI;
693 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
694 unsigned DstReg = MI.getOperand(0).getReg();
695 unsigned TmpReg = 0; // 0 for no temporary register
696 unsigned SrcReg = MI.getOperand(1).getReg();
697 unsigned Imm = MI.getOperand(2).getImm();
698 bool SrcIsKill = MI.getOperand(1).isKill();
699 OpLo = AVR::LDDRdPtrQ;
700 OpHi = AVR::LDDRdPtrQ;
701 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
703 // Since we add 1 to the Imm value for the high byte below, and 63 is the highest Imm value
704 // allowed for the instruction, 62 is the limit here.
705 assert(Imm <= 62 && "Offset is out of range");
707 // Use a temporary register if src and dst registers are the same.
708 if (DstReg == SrcReg)
709 TmpReg = scavengeGPR8(MI);
711 unsigned CurDstLoReg = (DstReg == SrcReg) ? TmpReg : DstLoReg;
712 unsigned CurDstHiReg = (DstReg == SrcReg) ? TmpReg : DstHiReg;
715 auto MIBLO = buildMI(MBB, MBBI, OpLo)
716 .addReg(CurDstLoReg, RegState::Define)
720 // Push low byte onto stack if necessary.
722 buildMI(MBB, MBBI, AVR::PUSHRr).addReg(TmpReg);
725 auto MIBHI = buildMI(MBB, MBBI, OpHi)
726 .addReg(CurDstHiReg, RegState::Define)
727 .addReg(SrcReg, getKillRegState(SrcIsKill))
731 // Move the high byte into the final destination.
732 buildMI(MBB, MBBI, AVR::MOVRdRr).addReg(DstHiReg).addReg(TmpReg);
734 // Move the low byte from the scratch space into the final destination.
735 buildMI(MBB, MBBI, AVR::POPRd).addReg(DstLoReg);
738 MIBLO.setMemRefs(MI.memoperands());
739 MIBHI.setMemRefs(MI.memoperands());
741 MI.eraseFromParent();
746 bool AVRExpandPseudo::expand<AVR::LPMWRdZ>(Block &MBB, BlockIt MBBI) {
747 MachineInstr &MI = *MBBI;
748 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
749 unsigned DstReg = MI.getOperand(0).getReg();
750 unsigned TmpReg = 0; // 0 for no temporary register
751 unsigned SrcReg = MI.getOperand(1).getReg();
752 bool SrcIsKill = MI.getOperand(1).isKill();
753 OpLo = AVR::LPMRdZPi;
755 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
757 // Use a temporary register if src and dst registers are the same.
758 if (DstReg == SrcReg)
759 TmpReg = scavengeGPR8(MI);
761 unsigned CurDstLoReg = (DstReg == SrcReg) ? TmpReg : DstLoReg;
762 unsigned CurDstHiReg = (DstReg == SrcReg) ? TmpReg : DstHiReg;
765 auto MIBLO = buildMI(MBB, MBBI, OpLo)
766 .addReg(CurDstLoReg, RegState::Define)
769 // Push low byte onto stack if necessary.
771 buildMI(MBB, MBBI, AVR::PUSHRr).addReg(TmpReg);
774 auto MIBHI = buildMI(MBB, MBBI, OpHi)
775 .addReg(CurDstHiReg, RegState::Define)
776 .addReg(SrcReg, getKillRegState(SrcIsKill));
779 // Move the high byte into the final destination.
780 buildMI(MBB, MBBI, AVR::MOVRdRr).addReg(DstHiReg).addReg(TmpReg);
782 // Move the low byte from the scratch space into the final destination.
783 buildMI(MBB, MBBI, AVR::POPRd).addReg(DstLoReg);
786 MIBLO.setMemRefs(MI.memoperands());
787 MIBHI.setMemRefs(MI.memoperands());
789 MI.eraseFromParent();
794 bool AVRExpandPseudo::expand<AVR::LPMWRdZPi>(Block &MBB, BlockIt MBBI) {
795 llvm_unreachable("wide LPMPi is unimplemented");
798 template<typename Func>
799 bool AVRExpandPseudo::expandAtomic(Block &MBB, BlockIt MBBI, Func f) {
800 // Remove the pseudo instruction.
801 MachineInstr &MI = *MBBI;
804 buildMI(MBB, MBBI, AVR::INRdA)
805 .addReg(SCRATCH_REGISTER, RegState::Define)
808 // Disable exceptions.
809 buildMI(MBB, MBBI, AVR::BCLRs).addImm(7); // CLI
813 // Restore the status reg.
814 buildMI(MBB, MBBI, AVR::OUTARr)
816 .addReg(SCRATCH_REGISTER);
818 MI.eraseFromParent();
822 template<typename Func>
823 bool AVRExpandPseudo::expandAtomicBinaryOp(unsigned Opcode,
827 return expandAtomic(MBB, MBBI, [&](MachineInstr &MI) {
828 auto Op1 = MI.getOperand(0);
829 auto Op2 = MI.getOperand(1);
831 MachineInstr &NewInst =
832 *buildMI(MBB, MBBI, Opcode).add(Op1).add(Op2).getInstr();
837 bool AVRExpandPseudo::expandAtomicBinaryOp(unsigned Opcode,
840 return expandAtomicBinaryOp(Opcode, MBB, MBBI, [](MachineInstr &MI) {});
843 bool AVRExpandPseudo::expandAtomicArithmeticOp(unsigned Width,
844 unsigned ArithOpcode,
847 return expandAtomic(MBB, MBBI, [&](MachineInstr &MI) {
848 auto Op1 = MI.getOperand(0);
849 auto Op2 = MI.getOperand(1);
851 unsigned LoadOpcode = (Width == 8) ? AVR::LDRdPtr : AVR::LDWRdPtr;
852 unsigned StoreOpcode = (Width == 8) ? AVR::STPtrRr : AVR::STWPtrRr;
855 buildMI(MBB, MBBI, LoadOpcode).add(Op1).add(Op2);
857 // Create the arithmetic op
858 buildMI(MBB, MBBI, ArithOpcode).add(Op1).add(Op1).add(Op2);
861 buildMI(MBB, MBBI, StoreOpcode).add(Op2).add(Op1);
865 unsigned AVRExpandPseudo::scavengeGPR8(MachineInstr &MI) {
866 MachineBasicBlock &MBB = *MI.getParent();
869 RS.enterBasicBlock(MBB);
872 BitVector Candidates =
873 TRI->getAllocatableSet
874 (*MBB.getParent(), &AVR::GPR8RegClass);
876 // Exclude all the registers being used by the instruction.
877 for (MachineOperand &MO : MI.operands()) {
878 if (MO.isReg() && MO.getReg() != 0 && !MO.isDef() &&
879 !Register::isVirtualRegister(MO.getReg()))
880 Candidates.reset(MO.getReg());
883 BitVector Available = RS.getRegsAvailable(&AVR::GPR8RegClass);
884 Available &= Candidates;
886 signed Reg = Available.find_first();
887 assert(Reg != -1 && "ran out of registers");
892 bool AVRExpandPseudo::expand<AVR::AtomicLoad8>(Block &MBB, BlockIt MBBI) {
893 return expandAtomicBinaryOp(AVR::LDRdPtr, MBB, MBBI);
897 bool AVRExpandPseudo::expand<AVR::AtomicLoad16>(Block &MBB, BlockIt MBBI) {
898 return expandAtomicBinaryOp(AVR::LDWRdPtr, MBB, MBBI);
902 bool AVRExpandPseudo::expand<AVR::AtomicStore8>(Block &MBB, BlockIt MBBI) {
903 return expandAtomicBinaryOp(AVR::STPtrRr, MBB, MBBI);
907 bool AVRExpandPseudo::expand<AVR::AtomicStore16>(Block &MBB, BlockIt MBBI) {
908 return expandAtomicBinaryOp(AVR::STWPtrRr, MBB, MBBI);
912 bool AVRExpandPseudo::expand<AVR::AtomicLoadAdd8>(Block &MBB, BlockIt MBBI) {
913 return expandAtomicArithmeticOp(8, AVR::ADDRdRr, MBB, MBBI);
917 bool AVRExpandPseudo::expand<AVR::AtomicLoadAdd16>(Block &MBB, BlockIt MBBI) {
918 return expandAtomicArithmeticOp(16, AVR::ADDWRdRr, MBB, MBBI);
922 bool AVRExpandPseudo::expand<AVR::AtomicLoadSub8>(Block &MBB, BlockIt MBBI) {
923 return expandAtomicArithmeticOp(8, AVR::SUBRdRr, MBB, MBBI);
927 bool AVRExpandPseudo::expand<AVR::AtomicLoadSub16>(Block &MBB, BlockIt MBBI) {
928 return expandAtomicArithmeticOp(16, AVR::SUBWRdRr, MBB, MBBI);
932 bool AVRExpandPseudo::expand<AVR::AtomicLoadAnd8>(Block &MBB, BlockIt MBBI) {
933 return expandAtomicArithmeticOp(8, AVR::ANDRdRr, MBB, MBBI);
937 bool AVRExpandPseudo::expand<AVR::AtomicLoadAnd16>(Block &MBB, BlockIt MBBI) {
938 return expandAtomicArithmeticOp(16, AVR::ANDWRdRr, MBB, MBBI);
942 bool AVRExpandPseudo::expand<AVR::AtomicLoadOr8>(Block &MBB, BlockIt MBBI) {
943 return expandAtomicArithmeticOp(8, AVR::ORRdRr, MBB, MBBI);
947 bool AVRExpandPseudo::expand<AVR::AtomicLoadOr16>(Block &MBB, BlockIt MBBI) {
948 return expandAtomicArithmeticOp(16, AVR::ORWRdRr, MBB, MBBI);
952 bool AVRExpandPseudo::expand<AVR::AtomicLoadXor8>(Block &MBB, BlockIt MBBI) {
953 return expandAtomicArithmeticOp(8, AVR::EORRdRr, MBB, MBBI);
957 bool AVRExpandPseudo::expand<AVR::AtomicLoadXor16>(Block &MBB, BlockIt MBBI) {
958 return expandAtomicArithmeticOp(16, AVR::EORWRdRr, MBB, MBBI);
962 bool AVRExpandPseudo::expand<AVR::AtomicFence>(Block &MBB, BlockIt MBBI) {
963 // On AVR, there is only one core and so atomic fences do nothing.
964 MBBI->eraseFromParent();
969 bool AVRExpandPseudo::expand<AVR::STSWKRr>(Block &MBB, BlockIt MBBI) {
970 MachineInstr &MI = *MBBI;
971 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
972 unsigned SrcReg = MI.getOperand(1).getReg();
973 bool SrcIsKill = MI.getOperand(1).isKill();
976 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
978 // Write the high byte first in case this address belongs to a special
979 // I/O address with a special temporary register.
980 auto MIBHI = buildMI(MBB, MBBI, OpHi);
981 auto MIBLO = buildMI(MBB, MBBI, OpLo);
983 switch (MI.getOperand(0).getType()) {
984 case MachineOperand::MO_GlobalAddress: {
985 const GlobalValue *GV = MI.getOperand(0).getGlobal();
986 int64_t Offs = MI.getOperand(0).getOffset();
987 unsigned TF = MI.getOperand(0).getTargetFlags();
989 MIBLO.addGlobalAddress(GV, Offs, TF);
990 MIBHI.addGlobalAddress(GV, Offs + 1, TF);
993 case MachineOperand::MO_Immediate: {
994 unsigned Imm = MI.getOperand(0).getImm();
997 MIBHI.addImm(Imm + 1);
1001 llvm_unreachable("Unknown operand type!");
1004 MIBLO.addReg(SrcLoReg, getKillRegState(SrcIsKill));
1005 MIBHI.addReg(SrcHiReg, getKillRegState(SrcIsKill));
1007 MIBLO.setMemRefs(MI.memoperands());
1008 MIBHI.setMemRefs(MI.memoperands());
1010 MI.eraseFromParent();
1015 bool AVRExpandPseudo::expand<AVR::STWPtrRr>(Block &MBB, BlockIt MBBI) {
1016 MachineInstr &MI = *MBBI;
1017 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
1018 unsigned DstReg = MI.getOperand(0).getReg();
1019 unsigned SrcReg = MI.getOperand(1).getReg();
1020 bool SrcIsKill = MI.getOperand(1).isKill();
1021 OpLo = AVR::STPtrRr;
1022 OpHi = AVR::STDPtrQRr;
1023 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1025 //:TODO: need to reverse this order like inw and stsw?
1026 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1028 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
1030 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1033 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
1035 MIBLO.setMemRefs(MI.memoperands());
1036 MIBHI.setMemRefs(MI.memoperands());
1038 MI.eraseFromParent();
1043 bool AVRExpandPseudo::expand<AVR::STWPtrPiRr>(Block &MBB, BlockIt MBBI) {
1044 MachineInstr &MI = *MBBI;
1045 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
1046 unsigned DstReg = MI.getOperand(0).getReg();
1047 unsigned SrcReg = MI.getOperand(2).getReg();
1048 unsigned Imm = MI.getOperand(3).getImm();
1049 bool DstIsDead = MI.getOperand(0).isDead();
1050 bool SrcIsKill = MI.getOperand(2).isKill();
1051 OpLo = AVR::STPtrPiRr;
1052 OpHi = AVR::STPtrPiRr;
1053 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1055 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same");
1057 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1058 .addReg(DstReg, RegState::Define)
1059 .addReg(DstReg, RegState::Kill)
1060 .addReg(SrcLoReg, getKillRegState(SrcIsKill))
1063 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1064 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1065 .addReg(DstReg, RegState::Kill)
1066 .addReg(SrcHiReg, getKillRegState(SrcIsKill))
1069 MIBLO.setMemRefs(MI.memoperands());
1070 MIBHI.setMemRefs(MI.memoperands());
1072 MI.eraseFromParent();
1077 bool AVRExpandPseudo::expand<AVR::STWPtrPdRr>(Block &MBB, BlockIt MBBI) {
1078 MachineInstr &MI = *MBBI;
1079 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
1080 unsigned DstReg = MI.getOperand(0).getReg();
1081 unsigned SrcReg = MI.getOperand(2).getReg();
1082 unsigned Imm = MI.getOperand(3).getImm();
1083 bool DstIsDead = MI.getOperand(0).isDead();
1084 bool SrcIsKill = MI.getOperand(2).isKill();
1085 OpLo = AVR::STPtrPdRr;
1086 OpHi = AVR::STPtrPdRr;
1087 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1089 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same");
1091 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1092 .addReg(DstReg, RegState::Define)
1093 .addReg(DstReg, RegState::Kill)
1094 .addReg(SrcHiReg, getKillRegState(SrcIsKill))
1097 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1098 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1099 .addReg(DstReg, RegState::Kill)
1100 .addReg(SrcLoReg, getKillRegState(SrcIsKill))
1103 MIBLO.setMemRefs(MI.memoperands());
1104 MIBHI.setMemRefs(MI.memoperands());
1106 MI.eraseFromParent();
1111 bool AVRExpandPseudo::expand<AVR::STDWPtrQRr>(Block &MBB, BlockIt MBBI) {
1112 MachineInstr &MI = *MBBI;
1113 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
1114 unsigned DstReg = MI.getOperand(0).getReg();
1115 unsigned SrcReg = MI.getOperand(2).getReg();
1116 unsigned Imm = MI.getOperand(1).getImm();
1117 bool DstIsKill = MI.getOperand(0).isKill();
1118 bool SrcIsKill = MI.getOperand(2).isKill();
1119 OpLo = AVR::STDPtrQRr;
1120 OpHi = AVR::STDPtrQRr;
1121 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1123 // Since we add 1 to the Imm value for the high byte below, and 63 is the highest Imm value
1124 // allowed for the instruction, 62 is the limit here.
1125 assert(Imm <= 62 && "Offset is out of range");
1127 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1130 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
1132 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1133 .addReg(DstReg, getKillRegState(DstIsKill))
1135 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
1137 MIBLO.setMemRefs(MI.memoperands());
1138 MIBHI.setMemRefs(MI.memoperands());
1140 MI.eraseFromParent();
1145 bool AVRExpandPseudo::expand<AVR::INWRdA>(Block &MBB, BlockIt MBBI) {
1146 MachineInstr &MI = *MBBI;
1147 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1148 unsigned Imm = MI.getOperand(1).getImm();
1149 unsigned DstReg = MI.getOperand(0).getReg();
1150 bool DstIsDead = MI.getOperand(0).isDead();
1153 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1155 // Since we add 1 to the Imm value for the high byte below, and 63 is the highest Imm value
1156 // allowed for the instruction, 62 is the limit here.
1157 assert(Imm <= 62 && "Address is out of range");
1159 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1160 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1163 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1164 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1167 MIBLO.setMemRefs(MI.memoperands());
1168 MIBHI.setMemRefs(MI.memoperands());
1170 MI.eraseFromParent();
1175 bool AVRExpandPseudo::expand<AVR::OUTWARr>(Block &MBB, BlockIt MBBI) {
1176 MachineInstr &MI = *MBBI;
1177 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
1178 unsigned Imm = MI.getOperand(0).getImm();
1179 unsigned SrcReg = MI.getOperand(1).getReg();
1180 bool SrcIsKill = MI.getOperand(1).isKill();
1183 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1185 // Since we add 1 to the Imm value for the high byte below, and 63 is the highest Imm value
1186 // allowed for the instruction, 62 is the limit here.
1187 assert(Imm <= 62 && "Address is out of range");
1189 // 16 bit I/O writes need the high byte first
1190 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1192 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
1194 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1196 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
1198 MIBLO.setMemRefs(MI.memoperands());
1199 MIBHI.setMemRefs(MI.memoperands());
1201 MI.eraseFromParent();
1206 bool AVRExpandPseudo::expand<AVR::PUSHWRr>(Block &MBB, BlockIt MBBI) {
1207 MachineInstr &MI = *MBBI;
1208 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
1209 unsigned SrcReg = MI.getOperand(0).getReg();
1210 bool SrcIsKill = MI.getOperand(0).isKill();
1211 unsigned Flags = MI.getFlags();
1214 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1217 buildMI(MBB, MBBI, OpLo)
1218 .addReg(SrcLoReg, getKillRegState(SrcIsKill))
1222 buildMI(MBB, MBBI, OpHi)
1223 .addReg(SrcHiReg, getKillRegState(SrcIsKill))
1226 MI.eraseFromParent();
1231 bool AVRExpandPseudo::expand<AVR::POPWRd>(Block &MBB, BlockIt MBBI) {
1232 MachineInstr &MI = *MBBI;
1233 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1234 unsigned DstReg = MI.getOperand(0).getReg();
1235 unsigned Flags = MI.getFlags();
1238 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1240 buildMI(MBB, MBBI, OpHi, DstHiReg).setMIFlags(Flags); // High
1241 buildMI(MBB, MBBI, OpLo, DstLoReg).setMIFlags(Flags); // Low
1243 MI.eraseFromParent();
1248 bool AVRExpandPseudo::expand<AVR::ROLBRd>(Block &MBB, BlockIt MBBI) {
1249 // In AVR, the rotate instructions behave quite unintuitively. They rotate
1250 // bits through the carry bit in SREG, effectively rotating over 9 bits,
1251 // instead of 8. This is useful when we are dealing with numbers over
1252 // multiple registers, but when we actually need to rotate stuff, we have
1253 // to explicitly add the carry bit.
1255 MachineInstr &MI = *MBBI;
1256 unsigned OpShift, OpCarry;
1257 unsigned DstReg = MI.getOperand(0).getReg();
1258 bool DstIsDead = MI.getOperand(0).isDead();
1259 OpShift = AVR::ADDRdRr;
1260 OpCarry = AVR::ADCRdRr;
1266 buildMI(MBB, MBBI, OpShift)
1267 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1271 // Add the carry bit
1272 auto MIB = buildMI(MBB, MBBI, OpCarry)
1273 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1275 .addReg(ZERO_REGISTER);
1277 // SREG is always implicitly killed
1278 MIB->getOperand(2).setIsKill();
1280 MI.eraseFromParent();
1285 bool AVRExpandPseudo::expand<AVR::RORBRd>(Block &MBB, BlockIt MBBI) {
1286 // In AVR, the rotate instructions behave quite unintuitively. They rotate
1287 // bits through the carry bit in SREG, effectively rotating over 9 bits,
1288 // instead of 8. This is useful when we are dealing with numbers over
1289 // multiple registers, but when we actually need to rotate stuff, we have
1290 // to explicitly add the carry bit.
1292 MachineInstr &MI = *MBBI;
1293 unsigned OpShiftOut, OpLoad, OpShiftIn, OpAdd;
1294 unsigned DstReg = MI.getOperand(0).getReg();
1295 bool DstIsDead = MI.getOperand(0).isDead();
1296 OpShiftOut = AVR::LSRRd;
1297 OpLoad = AVR::LDIRdK;
1298 OpShiftIn = AVR::RORRd;
1299 OpAdd = AVR::ORRdRr;
1307 buildMI(MBB, MBBI, OpShiftOut)
1308 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1311 // Put 0 in temporary register
1312 buildMI(MBB, MBBI, OpLoad)
1313 .addReg(SCRATCH_REGISTER, RegState::Define | getDeadRegState(true))
1317 buildMI(MBB, MBBI, OpShiftIn)
1318 .addReg(SCRATCH_REGISTER, RegState::Define | getDeadRegState(true))
1319 .addReg(SCRATCH_REGISTER);
1321 // Add the results together using an or-instruction
1322 auto MIB = buildMI(MBB, MBBI, OpAdd)
1323 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1325 .addReg(SCRATCH_REGISTER);
1327 // SREG is always implicitly killed
1328 MIB->getOperand(2).setIsKill();
1330 MI.eraseFromParent();
1335 bool AVRExpandPseudo::expand<AVR::LSLWRd>(Block &MBB, BlockIt MBBI) {
1336 MachineInstr &MI = *MBBI;
1337 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1338 unsigned DstReg = MI.getOperand(0).getReg();
1339 bool DstIsDead = MI.getOperand(0).isDead();
1340 bool DstIsKill = MI.getOperand(1).isKill();
1341 bool ImpIsDead = MI.getOperand(2).isDead();
1342 OpLo = AVR::ADDRdRr; // ADD Rd, Rd <==> LSL Rd
1343 OpHi = AVR::ADCRdRr; // ADC Rd, Rd <==> ROL Rd
1344 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1347 buildMI(MBB, MBBI, OpLo)
1348 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1350 .addReg(DstLoReg, getKillRegState(DstIsKill));
1352 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1353 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1355 .addReg(DstHiReg, getKillRegState(DstIsKill));
1358 MIBHI->getOperand(3).setIsDead();
1360 // SREG is always implicitly killed
1361 MIBHI->getOperand(4).setIsKill();
1363 MI.eraseFromParent();
1368 bool AVRExpandPseudo::expand<AVR::LSRWRd>(Block &MBB, BlockIt MBBI) {
1369 MachineInstr &MI = *MBBI;
1370 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1371 unsigned DstReg = MI.getOperand(0).getReg();
1372 bool DstIsDead = MI.getOperand(0).isDead();
1373 bool DstIsKill = MI.getOperand(1).isKill();
1374 bool ImpIsDead = MI.getOperand(2).isDead();
1377 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1380 buildMI(MBB, MBBI, OpHi)
1381 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1382 .addReg(DstHiReg, getKillRegState(DstIsKill));
1384 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1385 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1386 .addReg(DstLoReg, getKillRegState(DstIsKill));
1389 MIBLO->getOperand(2).setIsDead();
1391 // SREG is always implicitly killed
1392 MIBLO->getOperand(3).setIsKill();
1394 MI.eraseFromParent();
1399 bool AVRExpandPseudo::expand<AVR::RORWRd>(Block &MBB, BlockIt MBBI) {
1400 llvm_unreachable("RORW unimplemented");
1405 bool AVRExpandPseudo::expand<AVR::ROLWRd>(Block &MBB, BlockIt MBBI) {
1406 llvm_unreachable("ROLW unimplemented");
1411 bool AVRExpandPseudo::expand<AVR::ASRWRd>(Block &MBB, BlockIt MBBI) {
1412 MachineInstr &MI = *MBBI;
1413 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1414 unsigned DstReg = MI.getOperand(0).getReg();
1415 bool DstIsDead = MI.getOperand(0).isDead();
1416 bool DstIsKill = MI.getOperand(1).isKill();
1417 bool ImpIsDead = MI.getOperand(2).isDead();
1420 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1423 buildMI(MBB, MBBI, OpHi)
1424 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1425 .addReg(DstHiReg, getKillRegState(DstIsKill));
1427 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1428 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1429 .addReg(DstLoReg, getKillRegState(DstIsKill));
1432 MIBLO->getOperand(2).setIsDead();
1434 // SREG is always implicitly killed
1435 MIBLO->getOperand(3).setIsKill();
1437 MI.eraseFromParent();
1441 template <> bool AVRExpandPseudo::expand<AVR::SEXT>(Block &MBB, BlockIt MBBI) {
1442 MachineInstr &MI = *MBBI;
1443 unsigned DstLoReg, DstHiReg;
1444 // sext R17:R16, R17
1448 // sext R17:R16, R13
1453 // sext R17:R16, R16
1457 unsigned DstReg = MI.getOperand(0).getReg();
1458 unsigned SrcReg = MI.getOperand(1).getReg();
1459 bool DstIsDead = MI.getOperand(0).isDead();
1460 bool SrcIsKill = MI.getOperand(1).isKill();
1461 bool ImpIsDead = MI.getOperand(2).isDead();
1462 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1464 if (SrcReg != DstLoReg) {
1465 auto MOV = buildMI(MBB, MBBI, AVR::MOVRdRr)
1466 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1469 if (SrcReg == DstHiReg) {
1470 MOV->getOperand(1).setIsKill();
1474 if (SrcReg != DstHiReg) {
1475 buildMI(MBB, MBBI, AVR::MOVRdRr)
1476 .addReg(DstHiReg, RegState::Define)
1477 .addReg(SrcReg, getKillRegState(SrcIsKill));
1480 buildMI(MBB, MBBI, AVR::ADDRdRr) // LSL Rd <==> ADD Rd, Rr
1481 .addReg(DstHiReg, RegState::Define)
1483 .addReg(DstHiReg, RegState::Kill);
1485 auto SBC = buildMI(MBB, MBBI, AVR::SBCRdRr)
1486 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1487 .addReg(DstHiReg, RegState::Kill)
1488 .addReg(DstHiReg, RegState::Kill);
1491 SBC->getOperand(3).setIsDead();
1493 // SREG is always implicitly killed
1494 SBC->getOperand(4).setIsKill();
1496 MI.eraseFromParent();
1500 template <> bool AVRExpandPseudo::expand<AVR::ZEXT>(Block &MBB, BlockIt MBBI) {
1501 MachineInstr &MI = *MBBI;
1502 unsigned DstLoReg, DstHiReg;
1503 // zext R25:R24, R20
1506 // zext R25:R24, R24
1508 // zext R25:R24, R25
1511 unsigned DstReg = MI.getOperand(0).getReg();
1512 unsigned SrcReg = MI.getOperand(1).getReg();
1513 bool DstIsDead = MI.getOperand(0).isDead();
1514 bool SrcIsKill = MI.getOperand(1).isKill();
1515 bool ImpIsDead = MI.getOperand(2).isDead();
1516 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1518 if (SrcReg != DstLoReg) {
1519 buildMI(MBB, MBBI, AVR::MOVRdRr)
1520 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1521 .addReg(SrcReg, getKillRegState(SrcIsKill));
1524 auto EOR = buildMI(MBB, MBBI, AVR::EORRdRr)
1525 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1526 .addReg(DstHiReg, RegState::Kill)
1527 .addReg(DstHiReg, RegState::Kill);
1530 EOR->getOperand(3).setIsDead();
1532 MI.eraseFromParent();
1537 bool AVRExpandPseudo::expand<AVR::SPREAD>(Block &MBB, BlockIt MBBI) {
1538 MachineInstr &MI = *MBBI;
1539 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1540 unsigned DstReg = MI.getOperand(0).getReg();
1541 bool DstIsDead = MI.getOperand(0).isDead();
1542 unsigned Flags = MI.getFlags();
1545 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1548 buildMI(MBB, MBBI, OpLo)
1549 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1554 buildMI(MBB, MBBI, OpHi)
1555 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1559 MI.eraseFromParent();
1564 bool AVRExpandPseudo::expand<AVR::SPWRITE>(Block &MBB, BlockIt MBBI) {
1565 MachineInstr &MI = *MBBI;
1566 unsigned SrcLoReg, SrcHiReg;
1567 unsigned SrcReg = MI.getOperand(1).getReg();
1568 bool SrcIsKill = MI.getOperand(1).isKill();
1569 unsigned Flags = MI.getFlags();
1570 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1572 buildMI(MBB, MBBI, AVR::INRdA)
1573 .addReg(AVR::R0, RegState::Define)
1577 buildMI(MBB, MBBI, AVR::BCLRs).addImm(0x07).setMIFlags(Flags);
1579 buildMI(MBB, MBBI, AVR::OUTARr)
1581 .addReg(SrcHiReg, getKillRegState(SrcIsKill))
1584 buildMI(MBB, MBBI, AVR::OUTARr)
1586 .addReg(AVR::R0, RegState::Kill)
1589 buildMI(MBB, MBBI, AVR::OUTARr)
1591 .addReg(SrcLoReg, getKillRegState(SrcIsKill))
1594 MI.eraseFromParent();
1598 bool AVRExpandPseudo::expandMI(Block &MBB, BlockIt MBBI) {
1599 MachineInstr &MI = *MBBI;
1600 int Opcode = MBBI->getOpcode();
1602 #define EXPAND(Op) \
1604 return expand<Op>(MBB, MI)
1607 EXPAND(AVR::ADDWRdRr);
1608 EXPAND(AVR::ADCWRdRr);
1609 EXPAND(AVR::SUBWRdRr);
1610 EXPAND(AVR::SUBIWRdK);
1611 EXPAND(AVR::SBCWRdRr);
1612 EXPAND(AVR::SBCIWRdK);
1613 EXPAND(AVR::ANDWRdRr);
1614 EXPAND(AVR::ANDIWRdK);
1615 EXPAND(AVR::ORWRdRr);
1616 EXPAND(AVR::ORIWRdK);
1617 EXPAND(AVR::EORWRdRr);
1618 EXPAND(AVR::COMWRd);
1619 EXPAND(AVR::CPWRdRr);
1620 EXPAND(AVR::CPCWRdRr);
1621 EXPAND(AVR::LDIWRdK);
1622 EXPAND(AVR::LDSWRdK);
1623 EXPAND(AVR::LDWRdPtr);
1624 EXPAND(AVR::LDWRdPtrPi);
1625 EXPAND(AVR::LDWRdPtrPd);
1626 case AVR::LDDWRdYQ: //:FIXME: remove this once PR13375 gets fixed
1627 EXPAND(AVR::LDDWRdPtrQ);
1628 EXPAND(AVR::LPMWRdZ);
1629 EXPAND(AVR::LPMWRdZPi);
1630 EXPAND(AVR::AtomicLoad8);
1631 EXPAND(AVR::AtomicLoad16);
1632 EXPAND(AVR::AtomicStore8);
1633 EXPAND(AVR::AtomicStore16);
1634 EXPAND(AVR::AtomicLoadAdd8);
1635 EXPAND(AVR::AtomicLoadAdd16);
1636 EXPAND(AVR::AtomicLoadSub8);
1637 EXPAND(AVR::AtomicLoadSub16);
1638 EXPAND(AVR::AtomicLoadAnd8);
1639 EXPAND(AVR::AtomicLoadAnd16);
1640 EXPAND(AVR::AtomicLoadOr8);
1641 EXPAND(AVR::AtomicLoadOr16);
1642 EXPAND(AVR::AtomicLoadXor8);
1643 EXPAND(AVR::AtomicLoadXor16);
1644 EXPAND(AVR::AtomicFence);
1645 EXPAND(AVR::STSWKRr);
1646 EXPAND(AVR::STWPtrRr);
1647 EXPAND(AVR::STWPtrPiRr);
1648 EXPAND(AVR::STWPtrPdRr);
1649 EXPAND(AVR::STDWPtrQRr);
1650 EXPAND(AVR::INWRdA);
1651 EXPAND(AVR::OUTWARr);
1652 EXPAND(AVR::PUSHWRr);
1653 EXPAND(AVR::POPWRd);
1654 EXPAND(AVR::ROLBRd);
1655 EXPAND(AVR::RORBRd);
1656 EXPAND(AVR::LSLWRd);
1657 EXPAND(AVR::LSRWRd);
1658 EXPAND(AVR::RORWRd);
1659 EXPAND(AVR::ROLWRd);
1660 EXPAND(AVR::ASRWRd);
1663 EXPAND(AVR::SPREAD);
1664 EXPAND(AVR::SPWRITE);
1670 } // end of anonymous namespace
1672 INITIALIZE_PASS(AVRExpandPseudo, "avr-expand-pseudo",
1673 AVR_EXPAND_PSEUDO_NAME, false, false)
1676 FunctionPass *createAVRExpandPseudoPass() { return new AVRExpandPseudo(); }
1678 } // end of namespace llvm