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/TargetRegisterInfo.h"
27 #define AVR_EXPAND_PSEUDO_NAME "AVR pseudo instruction expansion pass"
31 /// Expands "placeholder" instructions marked as pseudo into
32 /// actual AVR instructions.
33 class AVRExpandPseudo
: public MachineFunctionPass
{
37 AVRExpandPseudo() : MachineFunctionPass(ID
) {
38 initializeAVRExpandPseudoPass(*PassRegistry::getPassRegistry());
41 bool runOnMachineFunction(MachineFunction
&MF
) override
;
43 StringRef
getPassName() const override
{ return AVR_EXPAND_PSEUDO_NAME
; }
46 typedef MachineBasicBlock Block
;
47 typedef Block::iterator BlockIt
;
49 const AVRRegisterInfo
*TRI
;
50 const TargetInstrInfo
*TII
;
52 bool expandMBB(Block
&MBB
);
53 bool expandMI(Block
&MBB
, BlockIt MBBI
);
54 template <unsigned OP
> bool expand(Block
&MBB
, BlockIt MBBI
);
56 MachineInstrBuilder
buildMI(Block
&MBB
, BlockIt MBBI
, unsigned Opcode
) {
57 return BuildMI(MBB
, MBBI
, MBBI
->getDebugLoc(), TII
->get(Opcode
));
60 MachineInstrBuilder
buildMI(Block
&MBB
, BlockIt MBBI
, unsigned Opcode
,
62 return BuildMI(MBB
, MBBI
, MBBI
->getDebugLoc(), TII
->get(Opcode
), DstReg
);
65 MachineRegisterInfo
&getRegInfo(Block
&MBB
) {
66 return MBB
.getParent()->getRegInfo();
69 bool expandArith(unsigned OpLo
, unsigned OpHi
, Block
&MBB
, BlockIt MBBI
);
70 bool expandLogic(unsigned Op
, Block
&MBB
, BlockIt MBBI
);
71 bool expandLogicImm(unsigned Op
, Block
&MBB
, BlockIt MBBI
);
72 bool isLogicImmOpRedundant(unsigned Op
, unsigned ImmVal
) const;
73 bool isLogicRegOpUndef(unsigned Op
, unsigned ImmVal
) const;
75 template <typename Func
> bool expandAtomic(Block
&MBB
, BlockIt MBBI
, Func f
);
77 template <typename Func
>
78 bool expandAtomicBinaryOp(unsigned Opcode
, Block
&MBB
, BlockIt MBBI
, Func f
);
80 bool expandAtomicBinaryOp(unsigned Opcode
, Block
&MBB
, BlockIt MBBI
);
82 /// Specific shift implementation for int8.
83 bool expandLSLB7Rd(Block
&MBB
, BlockIt MBBI
);
84 bool expandLSRB7Rd(Block
&MBB
, BlockIt MBBI
);
85 bool expandASRB6Rd(Block
&MBB
, BlockIt MBBI
);
86 bool expandASRB7Rd(Block
&MBB
, BlockIt MBBI
);
88 /// Specific shift implementation for int16.
89 bool expandLSLW4Rd(Block
&MBB
, BlockIt MBBI
);
90 bool expandLSRW4Rd(Block
&MBB
, BlockIt MBBI
);
91 bool expandASRW7Rd(Block
&MBB
, BlockIt MBBI
);
92 bool expandLSLW8Rd(Block
&MBB
, BlockIt MBBI
);
93 bool expandLSRW8Rd(Block
&MBB
, BlockIt MBBI
);
94 bool expandASRW8Rd(Block
&MBB
, BlockIt MBBI
);
95 bool expandLSLW12Rd(Block
&MBB
, BlockIt MBBI
);
96 bool expandLSRW12Rd(Block
&MBB
, BlockIt MBBI
);
97 bool expandASRW14Rd(Block
&MBB
, BlockIt MBBI
);
98 bool expandASRW15Rd(Block
&MBB
, BlockIt MBBI
);
100 // Common implementation of LPMWRdZ and ELPMWRdZ.
101 bool expandLPMWELPMW(Block
&MBB
, BlockIt MBBI
, bool IsELPM
);
102 // Common implementation of LPMBRdZ and ELPMBRdZ.
103 bool expandLPMBELPMB(Block
&MBB
, BlockIt MBBI
, bool IsELPM
);
104 // Common implementation of ROLBRdR1 and ROLBRdR17.
105 bool expandROLBRd(Block
&MBB
, BlockIt MBBI
);
108 char AVRExpandPseudo::ID
= 0;
110 bool AVRExpandPseudo::expandMBB(MachineBasicBlock
&MBB
) {
111 bool Modified
= false;
113 BlockIt MBBI
= MBB
.begin(), E
= MBB
.end();
115 BlockIt NMBBI
= std::next(MBBI
);
116 Modified
|= expandMI(MBB
, MBBI
);
123 bool AVRExpandPseudo::runOnMachineFunction(MachineFunction
&MF
) {
124 bool Modified
= false;
126 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
127 TRI
= STI
.getRegisterInfo();
128 TII
= STI
.getInstrInfo();
130 for (Block
&MBB
: MF
) {
131 bool ContinueExpanding
= true;
132 unsigned ExpandCount
= 0;
134 // Continue expanding the block until all pseudos are expanded.
136 assert(ExpandCount
< 10 && "pseudo expand limit reached");
139 bool BlockModified
= expandMBB(MBB
);
140 Modified
|= BlockModified
;
143 ContinueExpanding
= BlockModified
;
144 } while (ContinueExpanding
);
150 bool AVRExpandPseudo::expandArith(unsigned OpLo
, unsigned OpHi
, Block
&MBB
,
152 MachineInstr
&MI
= *MBBI
;
153 Register SrcLoReg
, SrcHiReg
, DstLoReg
, DstHiReg
;
154 Register DstReg
= MI
.getOperand(0).getReg();
155 Register SrcReg
= MI
.getOperand(2).getReg();
156 bool DstIsDead
= MI
.getOperand(0).isDead();
157 bool DstIsKill
= MI
.getOperand(1).isKill();
158 bool SrcIsKill
= MI
.getOperand(2).isKill();
159 bool ImpIsDead
= MI
.getOperand(3).isDead();
160 TRI
->splitReg(SrcReg
, SrcLoReg
, SrcHiReg
);
161 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
163 buildMI(MBB
, MBBI
, OpLo
)
164 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
165 .addReg(DstLoReg
, getKillRegState(DstIsKill
))
166 .addReg(SrcLoReg
, getKillRegState(SrcIsKill
));
169 buildMI(MBB
, MBBI
, OpHi
)
170 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
171 .addReg(DstHiReg
, getKillRegState(DstIsKill
))
172 .addReg(SrcHiReg
, getKillRegState(SrcIsKill
));
175 MIBHI
->getOperand(3).setIsDead();
177 // SREG is always implicitly killed
178 MIBHI
->getOperand(4).setIsKill();
180 MI
.eraseFromParent();
184 bool AVRExpandPseudo::expandLogic(unsigned Op
, Block
&MBB
, BlockIt MBBI
) {
185 MachineInstr
&MI
= *MBBI
;
186 Register SrcLoReg
, SrcHiReg
, DstLoReg
, DstHiReg
;
187 Register DstReg
= MI
.getOperand(0).getReg();
188 Register SrcReg
= MI
.getOperand(2).getReg();
189 bool DstIsDead
= MI
.getOperand(0).isDead();
190 bool DstIsKill
= MI
.getOperand(1).isKill();
191 bool SrcIsKill
= MI
.getOperand(2).isKill();
192 bool ImpIsDead
= MI
.getOperand(3).isDead();
193 TRI
->splitReg(SrcReg
, SrcLoReg
, SrcHiReg
);
194 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
197 buildMI(MBB
, MBBI
, Op
)
198 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
199 .addReg(DstLoReg
, getKillRegState(DstIsKill
))
200 .addReg(SrcLoReg
, getKillRegState(SrcIsKill
));
202 // SREG is always implicitly dead
203 MIBLO
->getOperand(3).setIsDead();
206 buildMI(MBB
, MBBI
, Op
)
207 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
208 .addReg(DstHiReg
, getKillRegState(DstIsKill
))
209 .addReg(SrcHiReg
, getKillRegState(SrcIsKill
));
212 MIBHI
->getOperand(3).setIsDead();
214 MI
.eraseFromParent();
218 bool AVRExpandPseudo::isLogicImmOpRedundant(unsigned Op
,
219 unsigned ImmVal
) const {
221 // ANDI Rd, 0xff is redundant.
222 if (Op
== AVR::ANDIRdK
&& ImmVal
== 0xff)
225 // ORI Rd, 0x0 is redundant.
226 if (Op
== AVR::ORIRdK
&& ImmVal
== 0x0)
232 bool AVRExpandPseudo::isLogicRegOpUndef(unsigned Op
, unsigned ImmVal
) const {
233 // ANDI Rd, 0x00 clears all input bits.
234 if (Op
== AVR::ANDIRdK
&& ImmVal
== 0x00)
237 // ORI Rd, 0xff sets all input bits.
238 if (Op
== AVR::ORIRdK
&& ImmVal
== 0xff)
244 bool AVRExpandPseudo::expandLogicImm(unsigned Op
, Block
&MBB
, BlockIt MBBI
) {
245 MachineInstr
&MI
= *MBBI
;
246 Register DstLoReg
, DstHiReg
;
247 Register DstReg
= MI
.getOperand(0).getReg();
248 bool DstIsDead
= MI
.getOperand(0).isDead();
249 bool SrcIsKill
= MI
.getOperand(1).isKill();
250 bool ImpIsDead
= MI
.getOperand(3).isDead();
251 unsigned Imm
= MI
.getOperand(2).getImm();
252 unsigned Lo8
= Imm
& 0xff;
253 unsigned Hi8
= (Imm
>> 8) & 0xff;
254 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
256 if (!isLogicImmOpRedundant(Op
, Lo8
)) {
258 buildMI(MBB
, MBBI
, Op
)
259 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
260 .addReg(DstLoReg
, getKillRegState(SrcIsKill
))
263 // SREG is always implicitly dead
264 MIBLO
->getOperand(3).setIsDead();
266 if (isLogicRegOpUndef(Op
, Lo8
))
267 MIBLO
->getOperand(1).setIsUndef(true);
270 if (!isLogicImmOpRedundant(Op
, Hi8
)) {
272 buildMI(MBB
, MBBI
, Op
)
273 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
274 .addReg(DstHiReg
, getKillRegState(SrcIsKill
))
278 MIBHI
->getOperand(3).setIsDead();
280 if (isLogicRegOpUndef(Op
, Hi8
))
281 MIBHI
->getOperand(1).setIsUndef(true);
284 MI
.eraseFromParent();
289 bool AVRExpandPseudo::expand
<AVR::ADDWRdRr
>(Block
&MBB
, BlockIt MBBI
) {
290 return expandArith(AVR::ADDRdRr
, AVR::ADCRdRr
, MBB
, MBBI
);
294 bool AVRExpandPseudo::expand
<AVR::ADCWRdRr
>(Block
&MBB
, BlockIt MBBI
) {
295 return expandArith(AVR::ADCRdRr
, AVR::ADCRdRr
, MBB
, MBBI
);
299 bool AVRExpandPseudo::expand
<AVR::SUBWRdRr
>(Block
&MBB
, BlockIt MBBI
) {
300 return expandArith(AVR::SUBRdRr
, AVR::SBCRdRr
, MBB
, MBBI
);
304 bool AVRExpandPseudo::expand
<AVR::SUBIWRdK
>(Block
&MBB
, BlockIt MBBI
) {
305 MachineInstr
&MI
= *MBBI
;
306 Register DstLoReg
, DstHiReg
;
307 Register DstReg
= MI
.getOperand(0).getReg();
308 bool DstIsDead
= MI
.getOperand(0).isDead();
309 bool SrcIsKill
= MI
.getOperand(1).isKill();
310 bool ImpIsDead
= MI
.getOperand(3).isDead();
311 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
314 buildMI(MBB
, MBBI
, AVR::SUBIRdK
)
315 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
316 .addReg(DstLoReg
, getKillRegState(SrcIsKill
));
319 buildMI(MBB
, MBBI
, AVR::SBCIRdK
)
320 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
321 .addReg(DstHiReg
, getKillRegState(SrcIsKill
));
323 switch (MI
.getOperand(2).getType()) {
324 case MachineOperand::MO_GlobalAddress
: {
325 const GlobalValue
*GV
= MI
.getOperand(2).getGlobal();
326 int64_t Offs
= MI
.getOperand(2).getOffset();
327 unsigned TF
= MI
.getOperand(2).getTargetFlags();
328 MIBLO
.addGlobalAddress(GV
, Offs
, TF
| AVRII::MO_NEG
| AVRII::MO_LO
);
329 MIBHI
.addGlobalAddress(GV
, Offs
, TF
| AVRII::MO_NEG
| AVRII::MO_HI
);
332 case MachineOperand::MO_Immediate
: {
333 unsigned Imm
= MI
.getOperand(2).getImm();
334 MIBLO
.addImm(Imm
& 0xff);
335 MIBHI
.addImm((Imm
>> 8) & 0xff);
339 llvm_unreachable("Unknown operand type!");
343 MIBHI
->getOperand(3).setIsDead();
345 // SREG is always implicitly killed
346 MIBHI
->getOperand(4).setIsKill();
348 MI
.eraseFromParent();
353 bool AVRExpandPseudo::expand
<AVR::SBCWRdRr
>(Block
&MBB
, BlockIt MBBI
) {
354 return expandArith(AVR::SBCRdRr
, AVR::SBCRdRr
, MBB
, MBBI
);
358 bool AVRExpandPseudo::expand
<AVR::SBCIWRdK
>(Block
&MBB
, BlockIt MBBI
) {
359 MachineInstr
&MI
= *MBBI
;
360 Register DstLoReg
, DstHiReg
;
361 Register DstReg
= MI
.getOperand(0).getReg();
362 bool DstIsDead
= MI
.getOperand(0).isDead();
363 bool SrcIsKill
= MI
.getOperand(1).isKill();
364 bool ImpIsDead
= MI
.getOperand(3).isDead();
365 unsigned Imm
= MI
.getOperand(2).getImm();
366 unsigned Lo8
= Imm
& 0xff;
367 unsigned Hi8
= (Imm
>> 8) & 0xff;
368 unsigned OpLo
= AVR::SBCIRdK
;
369 unsigned OpHi
= AVR::SBCIRdK
;
370 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
373 buildMI(MBB
, MBBI
, OpLo
)
374 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
375 .addReg(DstLoReg
, getKillRegState(SrcIsKill
))
378 // SREG is always implicitly killed
379 MIBLO
->getOperand(4).setIsKill();
382 buildMI(MBB
, MBBI
, OpHi
)
383 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
384 .addReg(DstHiReg
, getKillRegState(SrcIsKill
))
388 MIBHI
->getOperand(3).setIsDead();
390 // SREG is always implicitly killed
391 MIBHI
->getOperand(4).setIsKill();
393 MI
.eraseFromParent();
398 bool AVRExpandPseudo::expand
<AVR::ANDWRdRr
>(Block
&MBB
, BlockIt MBBI
) {
399 return expandLogic(AVR::ANDRdRr
, MBB
, MBBI
);
403 bool AVRExpandPseudo::expand
<AVR::ANDIWRdK
>(Block
&MBB
, BlockIt MBBI
) {
404 return expandLogicImm(AVR::ANDIRdK
, MBB
, MBBI
);
408 bool AVRExpandPseudo::expand
<AVR::ORWRdRr
>(Block
&MBB
, BlockIt MBBI
) {
409 return expandLogic(AVR::ORRdRr
, MBB
, MBBI
);
413 bool AVRExpandPseudo::expand
<AVR::ORIWRdK
>(Block
&MBB
, BlockIt MBBI
) {
414 return expandLogicImm(AVR::ORIRdK
, MBB
, MBBI
);
418 bool AVRExpandPseudo::expand
<AVR::EORWRdRr
>(Block
&MBB
, BlockIt MBBI
) {
419 return expandLogic(AVR::EORRdRr
, MBB
, MBBI
);
423 bool AVRExpandPseudo::expand
<AVR::COMWRd
>(Block
&MBB
, BlockIt MBBI
) {
424 MachineInstr
&MI
= *MBBI
;
425 Register DstLoReg
, DstHiReg
;
426 Register DstReg
= MI
.getOperand(0).getReg();
427 bool DstIsDead
= MI
.getOperand(0).isDead();
428 bool DstIsKill
= MI
.getOperand(1).isKill();
429 bool ImpIsDead
= MI
.getOperand(2).isDead();
430 unsigned OpLo
= AVR::COMRd
;
431 unsigned OpHi
= AVR::COMRd
;
432 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
435 buildMI(MBB
, MBBI
, OpLo
)
436 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
437 .addReg(DstLoReg
, getKillRegState(DstIsKill
));
439 // SREG is always implicitly dead
440 MIBLO
->getOperand(2).setIsDead();
443 buildMI(MBB
, MBBI
, OpHi
)
444 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
445 .addReg(DstHiReg
, getKillRegState(DstIsKill
));
448 MIBHI
->getOperand(2).setIsDead();
450 MI
.eraseFromParent();
455 bool AVRExpandPseudo::expand
<AVR::NEGWRd
>(Block
&MBB
, BlockIt MBBI
) {
456 MachineInstr
&MI
= *MBBI
;
457 Register DstLoReg
, DstHiReg
;
458 Register DstReg
= MI
.getOperand(0).getReg();
459 Register ZeroReg
= MI
.getOperand(2).getReg();
460 bool DstIsDead
= MI
.getOperand(0).isDead();
461 bool DstIsKill
= MI
.getOperand(1).isKill();
462 bool ImpIsDead
= MI
.getOperand(2).isDead();
463 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
465 // Do NEG on the upper byte.
467 buildMI(MBB
, MBBI
, AVR::NEGRd
)
468 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
469 .addReg(DstHiReg
, RegState::Kill
);
470 // SREG is always implicitly dead
471 MIBHI
->getOperand(2).setIsDead();
473 // Do NEG on the lower byte.
474 buildMI(MBB
, MBBI
, AVR::NEGRd
)
475 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
476 .addReg(DstLoReg
, getKillRegState(DstIsKill
));
480 buildMI(MBB
, MBBI
, AVR::SBCRdRr
)
481 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
482 .addReg(DstHiReg
, getKillRegState(DstIsKill
))
485 MISBCI
->getOperand(3).setIsDead();
486 // SREG is always implicitly killed
487 MISBCI
->getOperand(4).setIsKill();
489 MI
.eraseFromParent();
494 bool AVRExpandPseudo::expand
<AVR::CPWRdRr
>(Block
&MBB
, BlockIt MBBI
) {
495 MachineInstr
&MI
= *MBBI
;
496 Register SrcLoReg
, SrcHiReg
, DstLoReg
, DstHiReg
;
497 Register DstReg
= MI
.getOperand(0).getReg();
498 Register SrcReg
= MI
.getOperand(1).getReg();
499 bool DstIsKill
= MI
.getOperand(0).isKill();
500 bool SrcIsKill
= MI
.getOperand(1).isKill();
501 bool ImpIsDead
= MI
.getOperand(2).isDead();
502 unsigned OpLo
= AVR::CPRdRr
;
503 unsigned OpHi
= AVR::CPCRdRr
;
504 TRI
->splitReg(SrcReg
, SrcLoReg
, SrcHiReg
);
505 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
508 buildMI(MBB
, MBBI
, OpLo
)
509 .addReg(DstLoReg
, getKillRegState(DstIsKill
))
510 .addReg(SrcLoReg
, getKillRegState(SrcIsKill
));
512 auto MIBHI
= buildMI(MBB
, MBBI
, OpHi
)
513 .addReg(DstHiReg
, getKillRegState(DstIsKill
))
514 .addReg(SrcHiReg
, getKillRegState(SrcIsKill
));
517 MIBHI
->getOperand(2).setIsDead();
519 // SREG is always implicitly killed
520 MIBHI
->getOperand(3).setIsKill();
522 MI
.eraseFromParent();
527 bool AVRExpandPseudo::expand
<AVR::CPCWRdRr
>(Block
&MBB
, BlockIt MBBI
) {
528 MachineInstr
&MI
= *MBBI
;
529 Register SrcLoReg
, SrcHiReg
, DstLoReg
, DstHiReg
;
530 Register DstReg
= MI
.getOperand(0).getReg();
531 Register SrcReg
= MI
.getOperand(1).getReg();
532 bool DstIsKill
= MI
.getOperand(0).isKill();
533 bool SrcIsKill
= MI
.getOperand(1).isKill();
534 bool ImpIsDead
= MI
.getOperand(2).isDead();
535 unsigned OpLo
= AVR::CPCRdRr
;
536 unsigned OpHi
= AVR::CPCRdRr
;
537 TRI
->splitReg(SrcReg
, SrcLoReg
, SrcHiReg
);
538 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
540 auto MIBLO
= buildMI(MBB
, MBBI
, OpLo
)
541 .addReg(DstLoReg
, getKillRegState(DstIsKill
))
542 .addReg(SrcLoReg
, getKillRegState(SrcIsKill
));
544 // SREG is always implicitly killed
545 MIBLO
->getOperand(3).setIsKill();
547 auto MIBHI
= buildMI(MBB
, MBBI
, OpHi
)
548 .addReg(DstHiReg
, getKillRegState(DstIsKill
))
549 .addReg(SrcHiReg
, getKillRegState(SrcIsKill
));
552 MIBHI
->getOperand(2).setIsDead();
554 // SREG is always implicitly killed
555 MIBHI
->getOperand(3).setIsKill();
557 MI
.eraseFromParent();
562 bool AVRExpandPseudo::expand
<AVR::LDIWRdK
>(Block
&MBB
, BlockIt MBBI
) {
563 MachineInstr
&MI
= *MBBI
;
564 Register DstLoReg
, DstHiReg
;
565 Register DstReg
= MI
.getOperand(0).getReg();
566 bool DstIsDead
= MI
.getOperand(0).isDead();
567 unsigned OpLo
= AVR::LDIRdK
;
568 unsigned OpHi
= AVR::LDIRdK
;
569 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
572 buildMI(MBB
, MBBI
, OpLo
)
573 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
));
576 buildMI(MBB
, MBBI
, OpHi
)
577 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
));
579 switch (MI
.getOperand(1).getType()) {
580 case MachineOperand::MO_GlobalAddress
: {
581 const GlobalValue
*GV
= MI
.getOperand(1).getGlobal();
582 int64_t Offs
= MI
.getOperand(1).getOffset();
583 unsigned TF
= MI
.getOperand(1).getTargetFlags();
585 MIBLO
.addGlobalAddress(GV
, Offs
, TF
| AVRII::MO_LO
);
586 MIBHI
.addGlobalAddress(GV
, Offs
, TF
| AVRII::MO_HI
);
589 case MachineOperand::MO_BlockAddress
: {
590 const BlockAddress
*BA
= MI
.getOperand(1).getBlockAddress();
591 unsigned TF
= MI
.getOperand(1).getTargetFlags();
593 MIBLO
.add(MachineOperand::CreateBA(BA
, TF
| AVRII::MO_LO
));
594 MIBHI
.add(MachineOperand::CreateBA(BA
, TF
| AVRII::MO_HI
));
597 case MachineOperand::MO_Immediate
: {
598 unsigned Imm
= MI
.getOperand(1).getImm();
600 MIBLO
.addImm(Imm
& 0xff);
601 MIBHI
.addImm((Imm
>> 8) & 0xff);
605 llvm_unreachable("Unknown operand type!");
608 MI
.eraseFromParent();
613 bool AVRExpandPseudo::expand
<AVR::LDSWRdK
>(Block
&MBB
, BlockIt MBBI
) {
614 MachineInstr
&MI
= *MBBI
;
615 Register DstLoReg
, DstHiReg
;
616 Register DstReg
= MI
.getOperand(0).getReg();
617 bool DstIsDead
= MI
.getOperand(0).isDead();
618 unsigned OpLo
= AVR::LDSRdK
;
619 unsigned OpHi
= AVR::LDSRdK
;
620 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
623 buildMI(MBB
, MBBI
, OpLo
)
624 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
));
627 buildMI(MBB
, MBBI
, OpHi
)
628 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
));
630 switch (MI
.getOperand(1).getType()) {
631 case MachineOperand::MO_GlobalAddress
: {
632 const GlobalValue
*GV
= MI
.getOperand(1).getGlobal();
633 int64_t Offs
= MI
.getOperand(1).getOffset();
634 unsigned TF
= MI
.getOperand(1).getTargetFlags();
636 MIBLO
.addGlobalAddress(GV
, Offs
, TF
);
637 MIBHI
.addGlobalAddress(GV
, Offs
+ 1, TF
);
640 case MachineOperand::MO_Immediate
: {
641 unsigned Imm
= MI
.getOperand(1).getImm();
644 MIBHI
.addImm(Imm
+ 1);
648 llvm_unreachable("Unknown operand type!");
651 MIBLO
.setMemRefs(MI
.memoperands());
652 MIBHI
.setMemRefs(MI
.memoperands());
654 MI
.eraseFromParent();
659 bool AVRExpandPseudo::expand
<AVR::LDWRdPtr
>(Block
&MBB
, BlockIt MBBI
) {
660 MachineInstr
&MI
= *MBBI
;
661 Register DstReg
= MI
.getOperand(0).getReg();
662 Register SrcReg
= MI
.getOperand(1).getReg();
663 bool DstIsKill
= MI
.getOperand(0).isKill();
664 bool SrcIsKill
= MI
.getOperand(1).isKill();
665 const AVRSubtarget
&STI
= MBB
.getParent()->getSubtarget
<AVRSubtarget
>();
667 // DstReg has an earlyclobber so the register allocator will allocate them in
668 // separate registers.
669 assert(DstReg
!= SrcReg
&& "Dst and Src registers are the same!");
671 if (STI
.hasTinyEncoding()) {
672 // Handle this case in the expansion of LDDWRdPtrQ because it is very
674 buildMI(MBB
, MBBI
, AVR::LDDWRdPtrQ
)
675 .addDef(DstReg
, getKillRegState(DstIsKill
))
676 .addReg(SrcReg
, getKillRegState(SrcIsKill
))
678 .setMemRefs(MI
.memoperands());
681 Register DstLoReg
, DstHiReg
;
682 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
685 buildMI(MBB
, MBBI
, AVR::LDRdPtr
)
686 .addReg(DstLoReg
, RegState::Define
)
688 .setMemRefs(MI
.memoperands());
691 buildMI(MBB
, MBBI
, AVR::LDDRdPtrQ
)
692 .addReg(DstHiReg
, RegState::Define
)
693 .addReg(SrcReg
, getKillRegState(SrcIsKill
))
695 .setMemRefs(MI
.memoperands());
698 MI
.eraseFromParent();
703 bool AVRExpandPseudo::expand
<AVR::LDWRdPtrPi
>(Block
&MBB
, BlockIt MBBI
) {
704 MachineInstr
&MI
= *MBBI
;
705 Register DstLoReg
, DstHiReg
;
706 Register DstReg
= MI
.getOperand(0).getReg();
707 Register SrcReg
= MI
.getOperand(1).getReg();
708 bool DstIsDead
= MI
.getOperand(0).isDead();
709 bool SrcIsDead
= MI
.getOperand(1).isKill();
710 unsigned OpLo
= AVR::LDRdPtrPi
;
711 unsigned OpHi
= AVR::LDRdPtrPi
;
712 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
714 assert(DstReg
!= SrcReg
&& "SrcReg and DstReg cannot be the same");
717 buildMI(MBB
, MBBI
, OpLo
)
718 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
719 .addReg(SrcReg
, RegState::Define
)
720 .addReg(SrcReg
, RegState::Kill
);
723 buildMI(MBB
, MBBI
, OpHi
)
724 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
725 .addReg(SrcReg
, RegState::Define
| getDeadRegState(SrcIsDead
))
726 .addReg(SrcReg
, RegState::Kill
);
728 MIBLO
.setMemRefs(MI
.memoperands());
729 MIBHI
.setMemRefs(MI
.memoperands());
731 MI
.eraseFromParent();
736 bool AVRExpandPseudo::expand
<AVR::LDWRdPtrPd
>(Block
&MBB
, BlockIt MBBI
) {
737 MachineInstr
&MI
= *MBBI
;
738 Register DstLoReg
, DstHiReg
;
739 Register DstReg
= MI
.getOperand(0).getReg();
740 Register SrcReg
= MI
.getOperand(1).getReg();
741 bool DstIsDead
= MI
.getOperand(0).isDead();
742 bool SrcIsDead
= MI
.getOperand(1).isKill();
743 unsigned OpLo
= AVR::LDRdPtrPd
;
744 unsigned OpHi
= AVR::LDRdPtrPd
;
745 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
747 assert(DstReg
!= SrcReg
&& "SrcReg and DstReg cannot be the same");
750 buildMI(MBB
, MBBI
, OpHi
)
751 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
752 .addReg(SrcReg
, RegState::Define
)
753 .addReg(SrcReg
, RegState::Kill
);
756 buildMI(MBB
, MBBI
, OpLo
)
757 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
758 .addReg(SrcReg
, RegState::Define
| getDeadRegState(SrcIsDead
))
759 .addReg(SrcReg
, RegState::Kill
);
761 MIBLO
.setMemRefs(MI
.memoperands());
762 MIBHI
.setMemRefs(MI
.memoperands());
764 MI
.eraseFromParent();
769 bool AVRExpandPseudo::expand
<AVR::LDDWRdPtrQ
>(Block
&MBB
, BlockIt MBBI
) {
770 MachineInstr
&MI
= *MBBI
;
771 Register DstReg
= MI
.getOperand(0).getReg();
772 Register SrcReg
= MI
.getOperand(1).getReg();
773 unsigned Imm
= MI
.getOperand(2).getImm();
774 bool DstIsKill
= MI
.getOperand(0).isKill();
775 bool SrcIsKill
= MI
.getOperand(1).isKill();
776 const AVRSubtarget
&STI
= MBB
.getParent()->getSubtarget
<AVRSubtarget
>();
778 // Since we add 1 to the Imm value for the high byte below, and 63 is the
779 // highest Imm value allowed for the instruction, 62 is the limit here.
780 assert(Imm
<= 62 && "Offset is out of range");
782 // DstReg has an earlyclobber so the register allocator will allocate them in
783 // separate registers.
784 assert(DstReg
!= SrcReg
&& "Dst and Src registers are the same!");
786 if (STI
.hasTinyEncoding()) {
787 // Reduced tiny cores don't support load/store with displacement. However,
788 // they do support postincrement. So we'll simply adjust the pointer before
789 // and after and use postincrement to load multiple registers.
791 // Add offset. The offset can be 0 when expanding this instruction from the
792 // more specific LDWRdPtr instruction.
794 buildMI(MBB
, MBBI
, AVR::SUBIWRdK
, SrcReg
)
796 .addImm(0x10000 - Imm
);
799 // Do a word load with postincrement. This will be lowered to a two byte
801 buildMI(MBB
, MBBI
, AVR::LDWRdPtrPi
)
802 .addDef(DstReg
, getKillRegState(DstIsKill
))
803 .addReg(SrcReg
, getKillRegState(SrcIsKill
))
805 .setMemRefs(MI
.memoperands());
807 // If the pointer is used after the store instruction, subtract the new
808 // offset (with 2 added after the postincrement instructions) so it is the
811 buildMI(MBB
, MBBI
, AVR::SUBIWRdK
, SrcReg
).addReg(SrcReg
).addImm(Imm
+ 2);
814 Register DstLoReg
, DstHiReg
;
815 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
818 buildMI(MBB
, MBBI
, AVR::LDDRdPtrQ
)
819 .addReg(DstLoReg
, RegState::Define
)
822 .setMemRefs(MI
.memoperands());
825 buildMI(MBB
, MBBI
, AVR::LDDRdPtrQ
)
826 .addReg(DstHiReg
, RegState::Define
)
827 .addReg(SrcReg
, getKillRegState(SrcIsKill
))
829 .setMemRefs(MI
.memoperands());
832 MI
.eraseFromParent();
836 bool AVRExpandPseudo::expandLPMWELPMW(Block
&MBB
, BlockIt MBBI
, bool IsELPM
) {
837 MachineInstr
&MI
= *MBBI
;
838 Register DstLoReg
, DstHiReg
;
839 Register DstReg
= MI
.getOperand(0).getReg();
840 Register SrcReg
= MI
.getOperand(1).getReg();
841 Register SrcLoReg
, SrcHiReg
;
842 bool SrcIsKill
= MI
.getOperand(1).isKill();
843 const AVRSubtarget
&STI
= MBB
.getParent()->getSubtarget
<AVRSubtarget
>();
844 bool IsLPMRn
= IsELPM
? STI
.hasELPMX() : STI
.hasLPMX();
846 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
847 TRI
->splitReg(SrcReg
, SrcLoReg
, SrcHiReg
);
849 // Set the I/O register RAMPZ for ELPM.
851 Register Bank
= MI
.getOperand(2).getReg();
853 buildMI(MBB
, MBBI
, AVR::OUTARr
).addImm(STI
.getIORegRAMPZ()).addReg(Bank
);
856 // This is enforced by the @earlyclobber constraint.
857 assert(DstReg
!= SrcReg
&& "SrcReg and DstReg cannot be the same");
860 unsigned OpLo
= IsELPM
? AVR::ELPMRdZPi
: AVR::LPMRdZPi
;
861 unsigned OpHi
= IsELPM
? AVR::ELPMRdZ
: AVR::LPMRdZ
;
863 auto MIBLO
= buildMI(MBB
, MBBI
, OpLo
)
864 .addReg(DstLoReg
, RegState::Define
)
867 auto MIBHI
= buildMI(MBB
, MBBI
, OpHi
)
868 .addReg(DstHiReg
, RegState::Define
)
869 .addReg(SrcReg
, getKillRegState(SrcIsKill
));
870 MIBLO
.setMemRefs(MI
.memoperands());
871 MIBHI
.setMemRefs(MI
.memoperands());
873 unsigned Opc
= IsELPM
? AVR::ELPM
: AVR::LPM
;
874 // Load low byte, and copy to the low destination register.
875 auto MIBLO
= buildMI(MBB
, MBBI
, Opc
);
876 buildMI(MBB
, MBBI
, AVR::MOVRdRr
)
877 .addReg(DstLoReg
, RegState::Define
)
878 .addReg(AVR::R0
, RegState::Kill
);
879 MIBLO
.setMemRefs(MI
.memoperands());
880 // Increase the Z register by 1.
881 if (STI
.hasADDSUBIW()) {
883 auto MIINC
= buildMI(MBB
, MBBI
, AVR::ADIWRdK
)
884 .addReg(SrcReg
, RegState::Define
)
885 .addReg(SrcReg
, getKillRegState(SrcIsKill
))
887 MIINC
->getOperand(3).setIsDead();
891 buildMI(MBB
, MBBI
, AVR::SUBIRdK
)
892 .addReg(SrcLoReg
, RegState::Define
)
893 .addReg(SrcLoReg
, getKillRegState(SrcIsKill
))
895 auto MIZHI
= buildMI(MBB
, MBBI
, AVR::SBCIRdK
)
896 .addReg(SrcHiReg
, RegState::Define
)
897 .addReg(SrcHiReg
, getKillRegState(SrcIsKill
))
899 MIZHI
->getOperand(3).setIsDead();
900 MIZHI
->getOperand(4).setIsKill();
902 // Load high byte, and copy to the high destination register.
903 auto MIBHI
= buildMI(MBB
, MBBI
, Opc
);
904 buildMI(MBB
, MBBI
, AVR::MOVRdRr
)
905 .addReg(DstHiReg
, RegState::Define
)
906 .addReg(AVR::R0
, RegState::Kill
);
907 MIBHI
.setMemRefs(MI
.memoperands());
910 // Restore the Z register if it is not killed.
912 if (STI
.hasADDSUBIW()) {
914 auto MIDEC
= buildMI(MBB
, MBBI
, AVR::SBIWRdK
)
915 .addReg(SrcReg
, RegState::Define
)
916 .addReg(SrcReg
, getKillRegState(SrcIsKill
))
918 MIDEC
->getOperand(3).setIsDead();
922 buildMI(MBB
, MBBI
, AVR::SUBIRdK
)
923 .addReg(SrcLoReg
, RegState::Define
)
924 .addReg(SrcLoReg
, getKillRegState(SrcIsKill
))
926 auto MIZHI
= buildMI(MBB
, MBBI
, AVR::SBCIRdK
)
927 .addReg(SrcHiReg
, RegState::Define
)
928 .addReg(SrcHiReg
, getKillRegState(SrcIsKill
))
930 MIZHI
->getOperand(3).setIsDead();
931 MIZHI
->getOperand(4).setIsKill();
935 MI
.eraseFromParent();
940 bool AVRExpandPseudo::expand
<AVR::LPMWRdZ
>(Block
&MBB
, BlockIt MBBI
) {
941 return expandLPMWELPMW(MBB
, MBBI
, false);
945 bool AVRExpandPseudo::expand
<AVR::ELPMWRdZ
>(Block
&MBB
, BlockIt MBBI
) {
946 return expandLPMWELPMW(MBB
, MBBI
, true);
949 bool AVRExpandPseudo::expandLPMBELPMB(Block
&MBB
, BlockIt MBBI
, bool IsELPM
) {
950 MachineInstr
&MI
= *MBBI
;
951 Register DstReg
= MI
.getOperand(0).getReg();
952 Register SrcReg
= MI
.getOperand(1).getReg();
953 bool SrcIsKill
= MI
.getOperand(1).isKill();
954 const AVRSubtarget
&STI
= MBB
.getParent()->getSubtarget
<AVRSubtarget
>();
955 bool IsLPMRn
= IsELPM
? STI
.hasELPMX() : STI
.hasLPMX();
957 // Set the I/O register RAMPZ for ELPM (out RAMPZ, rtmp).
959 Register BankReg
= MI
.getOperand(2).getReg();
960 buildMI(MBB
, MBBI
, AVR::OUTARr
).addImm(STI
.getIORegRAMPZ()).addReg(BankReg
);
965 unsigned Opc
= IsELPM
? AVR::ELPMRdZ
: AVR::LPMRdZ
;
966 auto MILB
= buildMI(MBB
, MBBI
, Opc
)
967 .addReg(DstReg
, RegState::Define
)
968 .addReg(SrcReg
, getKillRegState(SrcIsKill
));
969 MILB
.setMemRefs(MI
.memoperands());
971 // For the basic ELPM/LPM instruction, its operand[0] is the implicit
972 // 'Z' register, and its operand[1] is the implicit 'R0' register.
973 unsigned Opc
= IsELPM
? AVR::ELPM
: AVR::LPM
;
974 auto MILB
= buildMI(MBB
, MBBI
, Opc
);
975 buildMI(MBB
, MBBI
, AVR::MOVRdRr
)
976 .addReg(DstReg
, RegState::Define
)
977 .addReg(AVR::R0
, RegState::Kill
);
978 MILB
.setMemRefs(MI
.memoperands());
981 MI
.eraseFromParent();
986 bool AVRExpandPseudo::expand
<AVR::ELPMBRdZ
>(Block
&MBB
, BlockIt MBBI
) {
987 return expandLPMBELPMB(MBB
, MBBI
, true);
991 bool AVRExpandPseudo::expand
<AVR::LPMBRdZ
>(Block
&MBB
, BlockIt MBBI
) {
992 return expandLPMBELPMB(MBB
, MBBI
, false);
996 bool AVRExpandPseudo::expand
<AVR::LPMWRdZPi
>(Block
&MBB
, BlockIt MBBI
) {
997 llvm_unreachable("16-bit LPMPi is unimplemented");
1001 bool AVRExpandPseudo::expand
<AVR::ELPMBRdZPi
>(Block
&MBB
, BlockIt MBBI
) {
1002 llvm_unreachable("8-bit ELPMPi is unimplemented");
1006 bool AVRExpandPseudo::expand
<AVR::ELPMWRdZPi
>(Block
&MBB
, BlockIt MBBI
) {
1007 llvm_unreachable("16-bit ELPMPi is unimplemented");
1010 template <typename Func
>
1011 bool AVRExpandPseudo::expandAtomic(Block
&MBB
, BlockIt MBBI
, Func f
) {
1012 MachineInstr
&MI
= *MBBI
;
1013 const AVRSubtarget
&STI
= MBB
.getParent()->getSubtarget
<AVRSubtarget
>();
1016 buildMI(MBB
, MBBI
, AVR::INRdA
)
1017 .addReg(STI
.getTmpRegister(), RegState::Define
)
1018 .addImm(STI
.getIORegSREG());
1020 // Disable exceptions.
1021 buildMI(MBB
, MBBI
, AVR::BCLRs
).addImm(7); // CLI
1025 // Restore the status reg.
1026 buildMI(MBB
, MBBI
, AVR::OUTARr
)
1027 .addImm(STI
.getIORegSREG())
1028 .addReg(STI
.getTmpRegister());
1030 MI
.eraseFromParent();
1034 template <typename Func
>
1035 bool AVRExpandPseudo::expandAtomicBinaryOp(unsigned Opcode
, Block
&MBB
,
1036 BlockIt MBBI
, Func f
) {
1037 return expandAtomic(MBB
, MBBI
, [&](MachineInstr
&MI
) {
1038 auto Op1
= MI
.getOperand(0);
1039 auto Op2
= MI
.getOperand(1);
1041 MachineInstr
&NewInst
=
1042 *buildMI(MBB
, MBBI
, Opcode
).add(Op1
).add(Op2
).getInstr();
1047 bool AVRExpandPseudo::expandAtomicBinaryOp(unsigned Opcode
, Block
&MBB
,
1049 return expandAtomicBinaryOp(Opcode
, MBB
, MBBI
, [](MachineInstr
&MI
) {});
1053 bool AVRExpandPseudo::expand
<AVR::AtomicLoad8
>(Block
&MBB
, BlockIt MBBI
) {
1054 return expandAtomicBinaryOp(AVR::LDRdPtr
, MBB
, MBBI
);
1058 bool AVRExpandPseudo::expand
<AVR::AtomicLoad16
>(Block
&MBB
, BlockIt MBBI
) {
1059 return expandAtomicBinaryOp(AVR::LDWRdPtr
, MBB
, MBBI
);
1063 bool AVRExpandPseudo::expand
<AVR::AtomicStore8
>(Block
&MBB
, BlockIt MBBI
) {
1064 return expandAtomicBinaryOp(AVR::STPtrRr
, MBB
, MBBI
);
1068 bool AVRExpandPseudo::expand
<AVR::AtomicStore16
>(Block
&MBB
, BlockIt MBBI
) {
1069 return expandAtomicBinaryOp(AVR::STWPtrRr
, MBB
, MBBI
);
1073 bool AVRExpandPseudo::expand
<AVR::AtomicFence
>(Block
&MBB
, BlockIt MBBI
) {
1074 // On AVR, there is only one core and so atomic fences do nothing.
1075 MBBI
->eraseFromParent();
1080 bool AVRExpandPseudo::expand
<AVR::STSWKRr
>(Block
&MBB
, BlockIt MBBI
) {
1081 const AVRSubtarget
&STI
= MBB
.getParent()->getSubtarget
<AVRSubtarget
>();
1082 MachineInstr
&MI
= *MBBI
;
1083 Register SrcLoReg
, SrcHiReg
;
1084 Register SrcReg
= MI
.getOperand(1).getReg();
1085 bool SrcIsKill
= MI
.getOperand(1).isKill();
1086 TRI
->splitReg(SrcReg
, SrcLoReg
, SrcHiReg
);
1088 auto MIB0
= buildMI(MBB
, MBBI
, AVR::STSKRr
);
1089 auto MIB1
= buildMI(MBB
, MBBI
, AVR::STSKRr
);
1091 switch (MI
.getOperand(0).getType()) {
1092 case MachineOperand::MO_GlobalAddress
: {
1093 const GlobalValue
*GV
= MI
.getOperand(0).getGlobal();
1094 int64_t Offs
= MI
.getOperand(0).getOffset();
1095 unsigned TF
= MI
.getOperand(0).getTargetFlags();
1097 if (STI
.hasLowByteFirst()) {
1098 // Write the low byte first for XMEGA devices.
1099 MIB0
.addGlobalAddress(GV
, Offs
, TF
);
1100 MIB1
.addGlobalAddress(GV
, Offs
+ 1, TF
);
1102 // Write the high byte first for traditional devices.
1103 MIB0
.addGlobalAddress(GV
, Offs
+ 1, TF
);
1104 MIB1
.addGlobalAddress(GV
, Offs
, TF
);
1109 case MachineOperand::MO_Immediate
: {
1110 unsigned Imm
= MI
.getOperand(0).getImm();
1112 if (STI
.hasLowByteFirst()) {
1113 // Write the low byte first for XMEGA devices.
1115 MIB1
.addImm(Imm
+ 1);
1117 // Write the high byte first for traditional devices.
1118 MIB0
.addImm(Imm
+ 1);
1125 llvm_unreachable("Unknown operand type!");
1128 if (STI
.hasLowByteFirst()) {
1129 // Write the low byte first for XMEGA devices.
1130 MIB0
.addReg(SrcLoReg
, getKillRegState(SrcIsKill
))
1131 .setMemRefs(MI
.memoperands());
1132 MIB1
.addReg(SrcHiReg
, getKillRegState(SrcIsKill
))
1133 .setMemRefs(MI
.memoperands());
1135 // Write the high byte first for traditional devices.
1136 MIB0
.addReg(SrcHiReg
, getKillRegState(SrcIsKill
))
1137 .setMemRefs(MI
.memoperands());
1138 MIB1
.addReg(SrcLoReg
, getKillRegState(SrcIsKill
))
1139 .setMemRefs(MI
.memoperands());
1142 MI
.eraseFromParent();
1147 bool AVRExpandPseudo::expand
<AVR::STWPtrRr
>(Block
&MBB
, BlockIt MBBI
) {
1148 MachineInstr
&MI
= *MBBI
;
1149 Register DstReg
= MI
.getOperand(0).getReg();
1150 Register SrcReg
= MI
.getOperand(1).getReg();
1151 bool DstIsKill
= MI
.getOperand(0).isKill();
1152 bool DstIsUndef
= MI
.getOperand(0).isUndef();
1153 bool SrcIsKill
= MI
.getOperand(1).isKill();
1154 const AVRSubtarget
&STI
= MBB
.getParent()->getSubtarget
<AVRSubtarget
>();
1156 //: TODO: need to reverse this order like inw and stsw?
1158 if (STI
.hasTinyEncoding()) {
1159 // Handle this case in the expansion of STDWPtrQRr because it is very
1161 buildMI(MBB
, MBBI
, AVR::STDWPtrQRr
)
1163 getKillRegState(DstIsKill
) | getUndefRegState(DstIsUndef
))
1165 .addReg(SrcReg
, getKillRegState(SrcIsKill
))
1166 .setMemRefs(MI
.memoperands());
1169 Register SrcLoReg
, SrcHiReg
;
1170 TRI
->splitReg(SrcReg
, SrcLoReg
, SrcHiReg
);
1171 if (STI
.hasLowByteFirst()) {
1172 buildMI(MBB
, MBBI
, AVR::STPtrRr
)
1173 .addReg(DstReg
, getUndefRegState(DstIsUndef
))
1174 .addReg(SrcLoReg
, getKillRegState(SrcIsKill
))
1175 .setMemRefs(MI
.memoperands());
1176 buildMI(MBB
, MBBI
, AVR::STDPtrQRr
)
1177 .addReg(DstReg
, getUndefRegState(DstIsUndef
))
1179 .addReg(SrcHiReg
, getKillRegState(SrcIsKill
))
1180 .setMemRefs(MI
.memoperands());
1182 buildMI(MBB
, MBBI
, AVR::STDPtrQRr
)
1183 .addReg(DstReg
, getUndefRegState(DstIsUndef
))
1185 .addReg(SrcHiReg
, getKillRegState(SrcIsKill
))
1186 .setMemRefs(MI
.memoperands());
1187 buildMI(MBB
, MBBI
, AVR::STPtrRr
)
1188 .addReg(DstReg
, getUndefRegState(DstIsUndef
))
1189 .addReg(SrcLoReg
, getKillRegState(SrcIsKill
))
1190 .setMemRefs(MI
.memoperands());
1194 MI
.eraseFromParent();
1199 bool AVRExpandPseudo::expand
<AVR::STWPtrPiRr
>(Block
&MBB
, BlockIt MBBI
) {
1200 MachineInstr
&MI
= *MBBI
;
1201 Register SrcLoReg
, SrcHiReg
;
1202 Register DstReg
= MI
.getOperand(0).getReg();
1203 Register SrcReg
= MI
.getOperand(2).getReg();
1204 unsigned Imm
= MI
.getOperand(3).getImm();
1205 bool DstIsDead
= MI
.getOperand(0).isDead();
1206 bool SrcIsKill
= MI
.getOperand(2).isKill();
1207 unsigned OpLo
= AVR::STPtrPiRr
;
1208 unsigned OpHi
= AVR::STPtrPiRr
;
1209 TRI
->splitReg(SrcReg
, SrcLoReg
, SrcHiReg
);
1211 assert(DstReg
!= SrcReg
&& "SrcReg and DstReg cannot be the same");
1213 auto MIBLO
= buildMI(MBB
, MBBI
, OpLo
)
1214 .addReg(DstReg
, RegState::Define
)
1215 .addReg(DstReg
, RegState::Kill
)
1216 .addReg(SrcLoReg
, getKillRegState(SrcIsKill
))
1220 buildMI(MBB
, MBBI
, OpHi
)
1221 .addReg(DstReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1222 .addReg(DstReg
, RegState::Kill
)
1223 .addReg(SrcHiReg
, getKillRegState(SrcIsKill
))
1226 MIBLO
.setMemRefs(MI
.memoperands());
1227 MIBHI
.setMemRefs(MI
.memoperands());
1229 MI
.eraseFromParent();
1234 bool AVRExpandPseudo::expand
<AVR::STWPtrPdRr
>(Block
&MBB
, BlockIt MBBI
) {
1235 MachineInstr
&MI
= *MBBI
;
1236 Register SrcLoReg
, SrcHiReg
;
1237 Register DstReg
= MI
.getOperand(0).getReg();
1238 Register SrcReg
= MI
.getOperand(2).getReg();
1239 unsigned Imm
= MI
.getOperand(3).getImm();
1240 bool DstIsDead
= MI
.getOperand(0).isDead();
1241 bool SrcIsKill
= MI
.getOperand(2).isKill();
1242 unsigned OpLo
= AVR::STPtrPdRr
;
1243 unsigned OpHi
= AVR::STPtrPdRr
;
1244 TRI
->splitReg(SrcReg
, SrcLoReg
, SrcHiReg
);
1246 assert(DstReg
!= SrcReg
&& "SrcReg and DstReg cannot be the same");
1248 auto MIBHI
= buildMI(MBB
, MBBI
, OpHi
)
1249 .addReg(DstReg
, RegState::Define
)
1250 .addReg(DstReg
, RegState::Kill
)
1251 .addReg(SrcHiReg
, getKillRegState(SrcIsKill
))
1255 buildMI(MBB
, MBBI
, OpLo
)
1256 .addReg(DstReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1257 .addReg(DstReg
, RegState::Kill
)
1258 .addReg(SrcLoReg
, getKillRegState(SrcIsKill
))
1261 MIBLO
.setMemRefs(MI
.memoperands());
1262 MIBHI
.setMemRefs(MI
.memoperands());
1264 MI
.eraseFromParent();
1269 bool AVRExpandPseudo::expand
<AVR::STDWPtrQRr
>(Block
&MBB
, BlockIt MBBI
) {
1270 MachineInstr
&MI
= *MBBI
;
1271 const AVRSubtarget
&STI
= MBB
.getParent()->getSubtarget
<AVRSubtarget
>();
1273 Register DstReg
= MI
.getOperand(0).getReg();
1274 bool DstIsKill
= MI
.getOperand(0).isKill();
1275 unsigned Imm
= MI
.getOperand(1).getImm();
1276 Register SrcReg
= MI
.getOperand(2).getReg();
1277 bool SrcIsKill
= MI
.getOperand(2).isKill();
1279 // STD's maximum displacement is 63, so larger stores have to be split into a
1280 // set of operations.
1281 // For avrtiny chips, STD is not available at all so we always have to fall
1282 // back to manual pointer adjustments.
1283 if (Imm
>= 63 || STI
.hasTinyEncoding()) {
1284 // Add offset. The offset can be 0 when expanding this instruction from the
1285 // more specific STWPtrRr instruction.
1287 buildMI(MBB
, MBBI
, AVR::SUBIWRdK
, DstReg
)
1288 .addReg(DstReg
, RegState::Kill
)
1289 .addImm(0x10000 - Imm
);
1292 // Do the store. This is a word store, that will be expanded further.
1293 buildMI(MBB
, MBBI
, AVR::STWPtrPiRr
, DstReg
)
1294 .addReg(DstReg
, getKillRegState(DstIsKill
))
1295 .addReg(SrcReg
, getKillRegState(SrcIsKill
))
1297 .setMemRefs(MI
.memoperands());
1299 // If the pointer is used after the store instruction, subtract the new
1300 // offset (with 2 added after the postincrement instructions) so it is the
1303 buildMI(MBB
, MBBI
, AVR::SUBIWRdK
, DstReg
)
1304 .addReg(DstReg
, RegState::Kill
)
1308 Register SrcLoReg
, SrcHiReg
;
1309 TRI
->splitReg(SrcReg
, SrcLoReg
, SrcHiReg
);
1311 if (STI
.hasLowByteFirst()) {
1312 buildMI(MBB
, MBBI
, AVR::STDPtrQRr
)
1315 .addReg(SrcLoReg
, getKillRegState(SrcIsKill
))
1316 .setMemRefs(MI
.memoperands());
1317 buildMI(MBB
, MBBI
, AVR::STDPtrQRr
)
1318 .addReg(DstReg
, getKillRegState(DstIsKill
))
1320 .addReg(SrcHiReg
, getKillRegState(SrcIsKill
))
1321 .setMemRefs(MI
.memoperands());
1323 buildMI(MBB
, MBBI
, AVR::STDPtrQRr
)
1326 .addReg(SrcHiReg
, getKillRegState(SrcIsKill
))
1327 .setMemRefs(MI
.memoperands());
1328 buildMI(MBB
, MBBI
, AVR::STDPtrQRr
)
1329 .addReg(DstReg
, getKillRegState(DstIsKill
))
1331 .addReg(SrcLoReg
, getKillRegState(SrcIsKill
))
1332 .setMemRefs(MI
.memoperands());
1336 MI
.eraseFromParent();
1341 bool AVRExpandPseudo::expand
<AVR::STDSPQRr
>(Block
&MBB
, BlockIt MBBI
) {
1342 MachineInstr
&MI
= *MBBI
;
1343 const MachineFunction
&MF
= *MBB
.getParent();
1344 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
1346 assert(MI
.getOperand(0).getReg() == AVR::SP
&&
1347 "SP is expected as base pointer");
1349 assert(STI
.getFrameLowering()->hasReservedCallFrame(MF
) &&
1350 "unexpected STDSPQRr pseudo instruction");
1353 MI
.setDesc(TII
->get(AVR::STDPtrQRr
));
1354 MI
.getOperand(0).setReg(AVR::R29R28
);
1360 bool AVRExpandPseudo::expand
<AVR::STDWSPQRr
>(Block
&MBB
, BlockIt MBBI
) {
1361 MachineInstr
&MI
= *MBBI
;
1362 const MachineFunction
&MF
= *MBB
.getParent();
1363 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
1365 assert(MI
.getOperand(0).getReg() == AVR::SP
&&
1366 "SP is expected as base pointer");
1368 assert(STI
.getFrameLowering()->hasReservedCallFrame(MF
) &&
1369 "unexpected STDWSPQRr pseudo instruction");
1372 MI
.setDesc(TII
->get(AVR::STDWPtrQRr
));
1373 MI
.getOperand(0).setReg(AVR::R29R28
);
1379 bool AVRExpandPseudo::expand
<AVR::INWRdA
>(Block
&MBB
, BlockIt MBBI
) {
1380 MachineInstr
&MI
= *MBBI
;
1381 Register DstLoReg
, DstHiReg
;
1382 unsigned Imm
= MI
.getOperand(1).getImm();
1383 Register DstReg
= MI
.getOperand(0).getReg();
1384 bool DstIsDead
= MI
.getOperand(0).isDead();
1385 unsigned OpLo
= AVR::INRdA
;
1386 unsigned OpHi
= AVR::INRdA
;
1387 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
1389 // Since we add 1 to the Imm value for the high byte below, and 63 is the
1390 // highest Imm value allowed for the instruction, 62 is the limit here.
1391 assert(Imm
<= 62 && "Address is out of range");
1394 buildMI(MBB
, MBBI
, OpLo
)
1395 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1399 buildMI(MBB
, MBBI
, OpHi
)
1400 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1403 MIBLO
.setMemRefs(MI
.memoperands());
1404 MIBHI
.setMemRefs(MI
.memoperands());
1406 MI
.eraseFromParent();
1411 bool AVRExpandPseudo::expand
<AVR::OUTWARr
>(Block
&MBB
, BlockIt MBBI
) {
1412 const AVRSubtarget
&STI
= MBB
.getParent()->getSubtarget
<AVRSubtarget
>();
1413 MachineInstr
&MI
= *MBBI
;
1414 Register SrcLoReg
, SrcHiReg
;
1415 unsigned Imm
= MI
.getOperand(0).getImm();
1416 Register SrcReg
= MI
.getOperand(1).getReg();
1417 bool SrcIsKill
= MI
.getOperand(1).isKill();
1418 TRI
->splitReg(SrcReg
, SrcLoReg
, SrcHiReg
);
1420 // Since we add 1 to the Imm value for the high byte below, and 63 is the
1421 // highest Imm value allowed for the instruction, 62 is the limit here.
1422 assert(Imm
<= 62 && "Address is out of range");
1424 // 16 bit I/O writes need the high byte first on normal AVR devices,
1425 // and in reverse order for the XMEGA/XMEGA3/XMEGAU families.
1426 auto MIBHI
= buildMI(MBB
, MBBI
, AVR::OUTARr
)
1427 .addImm(STI
.hasLowByteFirst() ? Imm
: Imm
+ 1)
1428 .addReg(STI
.hasLowByteFirst() ? SrcLoReg
: SrcHiReg
,
1429 getKillRegState(SrcIsKill
));
1430 auto MIBLO
= buildMI(MBB
, MBBI
, AVR::OUTARr
)
1431 .addImm(STI
.hasLowByteFirst() ? Imm
+ 1 : Imm
)
1432 .addReg(STI
.hasLowByteFirst() ? SrcHiReg
: SrcLoReg
,
1433 getKillRegState(SrcIsKill
));
1435 MIBLO
.setMemRefs(MI
.memoperands());
1436 MIBHI
.setMemRefs(MI
.memoperands());
1438 MI
.eraseFromParent();
1443 bool AVRExpandPseudo::expand
<AVR::PUSHWRr
>(Block
&MBB
, BlockIt MBBI
) {
1444 MachineInstr
&MI
= *MBBI
;
1445 Register SrcLoReg
, SrcHiReg
;
1446 Register SrcReg
= MI
.getOperand(0).getReg();
1447 bool SrcIsKill
= MI
.getOperand(0).isKill();
1448 unsigned Flags
= MI
.getFlags();
1449 unsigned OpLo
= AVR::PUSHRr
;
1450 unsigned OpHi
= AVR::PUSHRr
;
1451 TRI
->splitReg(SrcReg
, SrcLoReg
, SrcHiReg
);
1454 buildMI(MBB
, MBBI
, OpLo
)
1455 .addReg(SrcLoReg
, getKillRegState(SrcIsKill
))
1459 buildMI(MBB
, MBBI
, OpHi
)
1460 .addReg(SrcHiReg
, getKillRegState(SrcIsKill
))
1463 MI
.eraseFromParent();
1468 bool AVRExpandPseudo::expand
<AVR::POPWRd
>(Block
&MBB
, BlockIt MBBI
) {
1469 MachineInstr
&MI
= *MBBI
;
1470 Register DstLoReg
, DstHiReg
;
1471 Register DstReg
= MI
.getOperand(0).getReg();
1472 unsigned Flags
= MI
.getFlags();
1473 unsigned OpLo
= AVR::POPRd
;
1474 unsigned OpHi
= AVR::POPRd
;
1475 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
1477 buildMI(MBB
, MBBI
, OpHi
, DstHiReg
).setMIFlags(Flags
); // High
1478 buildMI(MBB
, MBBI
, OpLo
, DstLoReg
).setMIFlags(Flags
); // Low
1480 MI
.eraseFromParent();
1484 bool AVRExpandPseudo::expandROLBRd(Block
&MBB
, BlockIt MBBI
) {
1485 // In AVR, the rotate instructions behave quite unintuitively. They rotate
1486 // bits through the carry bit in SREG, effectively rotating over 9 bits,
1487 // instead of 8. This is useful when we are dealing with numbers over
1488 // multiple registers, but when we actually need to rotate stuff, we have
1489 // to explicitly add the carry bit.
1491 MachineInstr
&MI
= *MBBI
;
1492 unsigned OpShift
, OpCarry
;
1493 Register DstReg
= MI
.getOperand(0).getReg();
1494 Register ZeroReg
= MI
.getOperand(3).getReg();
1495 bool DstIsDead
= MI
.getOperand(0).isDead();
1496 bool DstIsKill
= MI
.getOperand(1).isKill();
1497 OpShift
= AVR::ADDRdRr
;
1498 OpCarry
= AVR::ADCRdRr
;
1504 buildMI(MBB
, MBBI
, OpShift
)
1505 .addReg(DstReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1506 .addReg(DstReg
, RegState::Kill
)
1507 .addReg(DstReg
, RegState::Kill
);
1509 // Add the carry bit
1510 auto MIB
= buildMI(MBB
, MBBI
, OpCarry
)
1511 .addReg(DstReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1512 .addReg(DstReg
, getKillRegState(DstIsKill
))
1515 MIB
->getOperand(3).setIsDead(); // SREG is always dead
1516 MIB
->getOperand(4).setIsKill(); // SREG is always implicitly killed
1518 MI
.eraseFromParent();
1523 bool AVRExpandPseudo::expand
<AVR::ROLBRdR1
>(Block
&MBB
, BlockIt MBBI
) {
1524 return expandROLBRd(MBB
, MBBI
);
1528 bool AVRExpandPseudo::expand
<AVR::ROLBRdR17
>(Block
&MBB
, BlockIt MBBI
) {
1529 return expandROLBRd(MBB
, MBBI
);
1533 bool AVRExpandPseudo::expand
<AVR::RORBRd
>(Block
&MBB
, BlockIt MBBI
) {
1534 // In AVR, the rotate instructions behave quite unintuitively. They rotate
1535 // bits through the carry bit in SREG, effectively rotating over 9 bits,
1536 // instead of 8. This is useful when we are dealing with numbers over
1537 // multiple registers, but when we actually need to rotate stuff, we have
1538 // to explicitly add the carry bit.
1540 MachineInstr
&MI
= *MBBI
;
1541 Register DstReg
= MI
.getOperand(0).getReg();
1547 // Move the lowest bit from DstReg into the T bit
1548 buildMI(MBB
, MBBI
, AVR::BST
).addReg(DstReg
).addImm(0);
1550 // Rotate to the right
1551 buildMI(MBB
, MBBI
, AVR::RORRd
, DstReg
).addReg(DstReg
);
1553 // Move the T bit into the highest bit of DstReg.
1554 buildMI(MBB
, MBBI
, AVR::BLD
, DstReg
).addReg(DstReg
).addImm(7);
1556 MI
.eraseFromParent();
1561 bool AVRExpandPseudo::expand
<AVR::LSLWRd
>(Block
&MBB
, BlockIt MBBI
) {
1562 MachineInstr
&MI
= *MBBI
;
1563 Register DstLoReg
, DstHiReg
;
1564 Register DstReg
= MI
.getOperand(0).getReg();
1565 bool DstIsDead
= MI
.getOperand(0).isDead();
1566 bool DstIsKill
= MI
.getOperand(1).isKill();
1567 bool ImpIsDead
= MI
.getOperand(2).isDead();
1568 unsigned OpLo
= AVR::ADDRdRr
; // ADD Rd, Rd <==> LSL Rd
1569 unsigned OpHi
= AVR::ADCRdRr
; // ADC Rd, Rd <==> ROL Rd
1570 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
1573 buildMI(MBB
, MBBI
, OpLo
)
1574 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1575 .addReg(DstLoReg
, getKillRegState(DstIsKill
))
1576 .addReg(DstLoReg
, getKillRegState(DstIsKill
));
1579 buildMI(MBB
, MBBI
, OpHi
)
1580 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1581 .addReg(DstHiReg
, getKillRegState(DstIsKill
))
1582 .addReg(DstHiReg
, getKillRegState(DstIsKill
));
1585 MIBHI
->getOperand(3).setIsDead();
1587 // SREG is always implicitly killed
1588 MIBHI
->getOperand(4).setIsKill();
1590 MI
.eraseFromParent();
1595 bool AVRExpandPseudo::expand
<AVR::LSLWHiRd
>(Block
&MBB
, BlockIt MBBI
) {
1596 MachineInstr
&MI
= *MBBI
;
1597 Register DstLoReg
, DstHiReg
;
1598 Register DstReg
= MI
.getOperand(0).getReg();
1599 bool DstIsDead
= MI
.getOperand(0).isDead();
1600 bool DstIsKill
= MI
.getOperand(1).isKill();
1601 bool ImpIsDead
= MI
.getOperand(2).isDead();
1602 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
1604 // add hireg, hireg <==> lsl hireg
1606 buildMI(MBB
, MBBI
, AVR::ADDRdRr
)
1607 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1608 .addReg(DstHiReg
, getKillRegState(DstIsKill
))
1609 .addReg(DstHiReg
, getKillRegState(DstIsKill
));
1612 MILSL
->getOperand(3).setIsDead();
1614 MI
.eraseFromParent();
1618 bool AVRExpandPseudo::expandLSLW4Rd(Block
&MBB
, BlockIt MBBI
) {
1619 MachineInstr
&MI
= *MBBI
;
1620 Register DstLoReg
, DstHiReg
;
1621 Register DstReg
= MI
.getOperand(0).getReg();
1622 bool DstIsDead
= MI
.getOperand(0).isDead();
1623 bool DstIsKill
= MI
.getOperand(1).isKill();
1624 bool ImpIsDead
= MI
.getOperand(3).isDead();
1625 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
1629 buildMI(MBB
, MBBI
, AVR::SWAPRd
)
1630 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1631 .addReg(DstHiReg
, RegState::Kill
);
1632 buildMI(MBB
, MBBI
, AVR::SWAPRd
)
1633 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1634 .addReg(DstLoReg
, RegState::Kill
);
1638 buildMI(MBB
, MBBI
, AVR::ANDIRdK
)
1639 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1640 .addReg(DstHiReg
, RegState::Kill
)
1642 // SREG is implicitly dead.
1643 MI0
->getOperand(3).setIsDead();
1647 buildMI(MBB
, MBBI
, AVR::EORRdRr
)
1648 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1649 .addReg(DstHiReg
, RegState::Kill
)
1651 // SREG is implicitly dead.
1652 MI1
->getOperand(3).setIsDead();
1656 buildMI(MBB
, MBBI
, AVR::ANDIRdK
)
1657 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1658 .addReg(DstLoReg
, getKillRegState(DstIsKill
))
1660 // SREG is implicitly dead.
1661 MI2
->getOperand(3).setIsDead();
1665 buildMI(MBB
, MBBI
, AVR::EORRdRr
)
1666 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1667 .addReg(DstHiReg
, getKillRegState(DstIsKill
))
1670 MI3
->getOperand(3).setIsDead();
1672 MI
.eraseFromParent();
1676 bool AVRExpandPseudo::expandLSLW8Rd(Block
&MBB
, BlockIt MBBI
) {
1677 MachineInstr
&MI
= *MBBI
;
1678 Register DstLoReg
, DstHiReg
;
1679 Register DstReg
= MI
.getOperand(0).getReg();
1680 bool DstIsDead
= MI
.getOperand(0).isDead();
1681 bool DstIsKill
= MI
.getOperand(1).isKill();
1682 bool ImpIsDead
= MI
.getOperand(3).isDead();
1683 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
1686 buildMI(MBB
, MBBI
, AVR::MOVRdRr
)
1687 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1692 buildMI(MBB
, MBBI
, AVR::EORRdRr
)
1693 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1694 .addReg(DstLoReg
, getKillRegState(DstIsKill
))
1695 .addReg(DstLoReg
, getKillRegState(DstIsKill
));
1697 MIBLO
->getOperand(3).setIsDead();
1699 MI
.eraseFromParent();
1703 bool AVRExpandPseudo::expandLSLW12Rd(Block
&MBB
, BlockIt MBBI
) {
1704 MachineInstr
&MI
= *MBBI
;
1705 Register DstLoReg
, DstHiReg
;
1706 Register DstReg
= MI
.getOperand(0).getReg();
1707 bool DstIsDead
= MI
.getOperand(0).isDead();
1708 bool DstIsKill
= MI
.getOperand(1).isKill();
1709 bool ImpIsDead
= MI
.getOperand(3).isDead();
1710 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
1713 buildMI(MBB
, MBBI
, AVR::MOVRdRr
)
1714 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1718 buildMI(MBB
, MBBI
, AVR::SWAPRd
)
1719 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1720 .addReg(DstHiReg
, RegState::Kill
);
1724 buildMI(MBB
, MBBI
, AVR::ANDIRdK
)
1725 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1726 .addReg(DstHiReg
, getKillRegState(DstIsKill
))
1728 // SREG is implicitly dead.
1729 MI0
->getOperand(3).setIsDead();
1733 buildMI(MBB
, MBBI
, AVR::EORRdRr
)
1734 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1735 .addReg(DstLoReg
, getKillRegState(DstIsKill
))
1736 .addReg(DstLoReg
, getKillRegState(DstIsKill
));
1738 MI1
->getOperand(3).setIsDead();
1740 MI
.eraseFromParent();
1745 bool AVRExpandPseudo::expand
<AVR::LSLWNRd
>(Block
&MBB
, BlockIt MBBI
) {
1746 MachineInstr
&MI
= *MBBI
;
1747 unsigned Imm
= MI
.getOperand(2).getImm();
1750 return expandLSLW4Rd(MBB
, MBBI
);
1752 return expandLSLW8Rd(MBB
, MBBI
);
1754 return expandLSLW12Rd(MBB
, MBBI
);
1756 llvm_unreachable("unimplemented lslwn");
1762 bool AVRExpandPseudo::expand
<AVR::LSRWRd
>(Block
&MBB
, BlockIt MBBI
) {
1763 MachineInstr
&MI
= *MBBI
;
1764 Register DstLoReg
, DstHiReg
;
1765 Register DstReg
= MI
.getOperand(0).getReg();
1766 bool DstIsDead
= MI
.getOperand(0).isDead();
1767 bool DstIsKill
= MI
.getOperand(1).isKill();
1768 bool ImpIsDead
= MI
.getOperand(2).isDead();
1769 unsigned OpLo
= AVR::RORRd
;
1770 unsigned OpHi
= AVR::LSRRd
;
1771 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
1774 buildMI(MBB
, MBBI
, OpHi
)
1775 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1776 .addReg(DstHiReg
, getKillRegState(DstIsKill
));
1779 buildMI(MBB
, MBBI
, OpLo
)
1780 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1781 .addReg(DstLoReg
, getKillRegState(DstIsKill
));
1784 MIBLO
->getOperand(2).setIsDead();
1786 // SREG is always implicitly killed
1787 MIBLO
->getOperand(3).setIsKill();
1789 MI
.eraseFromParent();
1794 bool AVRExpandPseudo::expand
<AVR::LSRWLoRd
>(Block
&MBB
, BlockIt MBBI
) {
1795 MachineInstr
&MI
= *MBBI
;
1796 Register DstLoReg
, DstHiReg
;
1797 Register DstReg
= MI
.getOperand(0).getReg();
1798 bool DstIsDead
= MI
.getOperand(0).isDead();
1799 bool DstIsKill
= MI
.getOperand(1).isKill();
1800 bool ImpIsDead
= MI
.getOperand(2).isDead();
1801 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
1805 buildMI(MBB
, MBBI
, AVR::LSRRd
)
1806 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1807 .addReg(DstLoReg
, getKillRegState(DstIsKill
));
1810 MILSR
->getOperand(2).setIsDead();
1812 MI
.eraseFromParent();
1816 bool AVRExpandPseudo::expandLSRW4Rd(Block
&MBB
, BlockIt MBBI
) {
1817 MachineInstr
&MI
= *MBBI
;
1818 Register DstLoReg
, DstHiReg
;
1819 Register DstReg
= MI
.getOperand(0).getReg();
1820 bool DstIsDead
= MI
.getOperand(0).isDead();
1821 bool DstIsKill
= MI
.getOperand(1).isKill();
1822 bool ImpIsDead
= MI
.getOperand(3).isDead();
1823 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
1827 buildMI(MBB
, MBBI
, AVR::SWAPRd
)
1828 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1829 .addReg(DstHiReg
, RegState::Kill
);
1830 buildMI(MBB
, MBBI
, AVR::SWAPRd
)
1831 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1832 .addReg(DstLoReg
, RegState::Kill
);
1836 buildMI(MBB
, MBBI
, AVR::ANDIRdK
)
1837 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1838 .addReg(DstLoReg
, RegState::Kill
)
1840 // SREG is implicitly dead.
1841 MI0
->getOperand(3).setIsDead();
1845 buildMI(MBB
, MBBI
, AVR::EORRdRr
)
1846 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1847 .addReg(DstLoReg
, RegState::Kill
)
1849 // SREG is implicitly dead.
1850 MI1
->getOperand(3).setIsDead();
1854 buildMI(MBB
, MBBI
, AVR::ANDIRdK
)
1855 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1856 .addReg(DstHiReg
, getKillRegState(DstIsKill
))
1858 // SREG is implicitly dead.
1859 MI2
->getOperand(3).setIsDead();
1863 buildMI(MBB
, MBBI
, AVR::EORRdRr
)
1864 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1865 .addReg(DstLoReg
, getKillRegState(DstIsKill
))
1868 MI3
->getOperand(3).setIsDead();
1870 MI
.eraseFromParent();
1874 bool AVRExpandPseudo::expandLSRW8Rd(Block
&MBB
, BlockIt MBBI
) {
1875 MachineInstr
&MI
= *MBBI
;
1876 Register DstLoReg
, DstHiReg
;
1877 Register DstReg
= MI
.getOperand(0).getReg();
1878 bool DstIsDead
= MI
.getOperand(0).isDead();
1879 bool DstIsKill
= MI
.getOperand(1).isKill();
1880 bool ImpIsDead
= MI
.getOperand(3).isDead();
1881 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
1883 // Move upper byte to lower byte.
1884 buildMI(MBB
, MBBI
, AVR::MOVRdRr
)
1885 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1888 // Clear upper byte.
1890 buildMI(MBB
, MBBI
, AVR::EORRdRr
)
1891 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1892 .addReg(DstHiReg
, getKillRegState(DstIsKill
))
1893 .addReg(DstHiReg
, getKillRegState(DstIsKill
));
1895 MIBHI
->getOperand(3).setIsDead();
1897 MI
.eraseFromParent();
1901 bool AVRExpandPseudo::expandLSRW12Rd(Block
&MBB
, BlockIt MBBI
) {
1902 MachineInstr
&MI
= *MBBI
;
1903 Register DstLoReg
, DstHiReg
;
1904 Register DstReg
= MI
.getOperand(0).getReg();
1905 bool DstIsDead
= MI
.getOperand(0).isDead();
1906 bool DstIsKill
= MI
.getOperand(1).isKill();
1907 bool ImpIsDead
= MI
.getOperand(3).isDead();
1908 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
1910 // Move upper byte to lower byte.
1911 buildMI(MBB
, MBBI
, AVR::MOVRdRr
)
1912 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1916 buildMI(MBB
, MBBI
, AVR::SWAPRd
)
1917 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1918 .addReg(DstLoReg
, RegState::Kill
);
1922 buildMI(MBB
, MBBI
, AVR::ANDIRdK
)
1923 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1924 .addReg(DstLoReg
, getKillRegState(DstIsKill
))
1926 // SREG is implicitly dead.
1927 MI0
->getOperand(3).setIsDead();
1929 // Clear upper byte.
1931 buildMI(MBB
, MBBI
, AVR::EORRdRr
)
1932 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1933 .addReg(DstHiReg
, getKillRegState(DstIsKill
))
1934 .addReg(DstHiReg
, getKillRegState(DstIsKill
));
1936 MIBHI
->getOperand(3).setIsDead();
1938 MI
.eraseFromParent();
1943 bool AVRExpandPseudo::expand
<AVR::LSRWNRd
>(Block
&MBB
, BlockIt MBBI
) {
1944 MachineInstr
&MI
= *MBBI
;
1945 unsigned Imm
= MI
.getOperand(2).getImm();
1948 return expandLSRW4Rd(MBB
, MBBI
);
1950 return expandLSRW8Rd(MBB
, MBBI
);
1952 return expandLSRW12Rd(MBB
, MBBI
);
1954 llvm_unreachable("unimplemented lsrwn");
1960 bool AVRExpandPseudo::expand
<AVR::RORWRd
>(Block
&MBB
, BlockIt MBBI
) {
1961 llvm_unreachable("RORW unimplemented");
1966 bool AVRExpandPseudo::expand
<AVR::ROLWRd
>(Block
&MBB
, BlockIt MBBI
) {
1967 llvm_unreachable("ROLW unimplemented");
1972 bool AVRExpandPseudo::expand
<AVR::ASRWRd
>(Block
&MBB
, BlockIt MBBI
) {
1973 MachineInstr
&MI
= *MBBI
;
1974 Register DstLoReg
, DstHiReg
;
1975 Register DstReg
= MI
.getOperand(0).getReg();
1976 bool DstIsDead
= MI
.getOperand(0).isDead();
1977 bool DstIsKill
= MI
.getOperand(1).isKill();
1978 bool ImpIsDead
= MI
.getOperand(2).isDead();
1979 unsigned OpLo
= AVR::RORRd
;
1980 unsigned OpHi
= AVR::ASRRd
;
1981 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
1984 buildMI(MBB
, MBBI
, OpHi
)
1985 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1986 .addReg(DstHiReg
, getKillRegState(DstIsKill
));
1989 buildMI(MBB
, MBBI
, OpLo
)
1990 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
1991 .addReg(DstLoReg
, getKillRegState(DstIsKill
));
1994 MIBLO
->getOperand(2).setIsDead();
1996 // SREG is always implicitly killed
1997 MIBLO
->getOperand(3).setIsKill();
1999 MI
.eraseFromParent();
2004 bool AVRExpandPseudo::expand
<AVR::ASRWLoRd
>(Block
&MBB
, BlockIt MBBI
) {
2005 MachineInstr
&MI
= *MBBI
;
2006 Register DstLoReg
, DstHiReg
;
2007 Register DstReg
= MI
.getOperand(0).getReg();
2008 bool DstIsDead
= MI
.getOperand(0).isDead();
2009 bool DstIsKill
= MI
.getOperand(1).isKill();
2010 bool ImpIsDead
= MI
.getOperand(2).isDead();
2011 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
2015 buildMI(MBB
, MBBI
, AVR::ASRRd
)
2016 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2017 .addReg(DstLoReg
, getKillRegState(DstIsKill
));
2020 MIASR
->getOperand(2).setIsDead();
2022 MI
.eraseFromParent();
2026 bool AVRExpandPseudo::expandASRW7Rd(Block
&MBB
, BlockIt MBBI
) {
2027 MachineInstr
&MI
= *MBBI
;
2028 Register DstLoReg
, DstHiReg
;
2029 Register DstReg
= MI
.getOperand(0).getReg();
2030 bool DstIsDead
= MI
.getOperand(0).isDead();
2031 bool DstIsKill
= MI
.getOperand(1).isKill();
2032 bool ImpIsDead
= MI
.getOperand(3).isDead();
2033 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
2040 // lsl r24 <=> add r24, r24
2041 buildMI(MBB
, MBBI
, AVR::ADDRdRr
)
2042 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2043 .addReg(DstLoReg
, RegState::Kill
)
2044 .addReg(DstLoReg
, RegState::Kill
);
2047 buildMI(MBB
, MBBI
, AVR::MOVRdRr
)
2048 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2051 // rol r24 <=> adc r24, r24
2052 buildMI(MBB
, MBBI
, AVR::ADCRdRr
)
2053 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2054 .addReg(DstLoReg
, getKillRegState(DstIsKill
))
2055 .addReg(DstLoReg
, getKillRegState(DstIsKill
));
2059 buildMI(MBB
, MBBI
, AVR::SBCRdRr
)
2060 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2061 .addReg(DstHiReg
, getKillRegState(DstIsKill
))
2062 .addReg(DstHiReg
, getKillRegState(DstIsKill
));
2065 MISBC
->getOperand(3).setIsDead();
2066 // SREG is always implicitly killed
2067 MISBC
->getOperand(4).setIsKill();
2069 MI
.eraseFromParent();
2073 bool AVRExpandPseudo::expandASRW8Rd(Block
&MBB
, BlockIt MBBI
) {
2074 MachineInstr
&MI
= *MBBI
;
2075 Register DstLoReg
, DstHiReg
;
2076 Register DstReg
= MI
.getOperand(0).getReg();
2077 bool DstIsDead
= MI
.getOperand(0).isDead();
2078 bool DstIsKill
= MI
.getOperand(1).isKill();
2079 bool ImpIsDead
= MI
.getOperand(3).isDead();
2080 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
2082 // Move upper byte to lower byte.
2083 buildMI(MBB
, MBBI
, AVR::MOVRdRr
)
2084 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2087 // Move the sign bit to the C flag.
2088 buildMI(MBB
, MBBI
, AVR::ADDRdRr
)
2089 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2090 .addReg(DstHiReg
, RegState::Kill
)
2091 .addReg(DstHiReg
, RegState::Kill
);
2093 // Set upper byte to 0 or -1.
2095 buildMI(MBB
, MBBI
, AVR::SBCRdRr
)
2096 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2097 .addReg(DstHiReg
, getKillRegState(DstIsKill
))
2098 .addReg(DstHiReg
, getKillRegState(DstIsKill
));
2101 MIBHI
->getOperand(3).setIsDead();
2102 // SREG is always implicitly killed
2103 MIBHI
->getOperand(4).setIsKill();
2105 MI
.eraseFromParent();
2108 bool AVRExpandPseudo::expandASRW14Rd(Block
&MBB
, BlockIt MBBI
) {
2109 MachineInstr
&MI
= *MBBI
;
2110 Register DstLoReg
, DstHiReg
;
2111 Register DstReg
= MI
.getOperand(0).getReg();
2112 bool DstIsDead
= MI
.getOperand(0).isDead();
2113 bool DstIsKill
= MI
.getOperand(1).isKill();
2114 bool ImpIsDead
= MI
.getOperand(3).isDead();
2115 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
2123 // lsl r25 <=> add r25, r25
2124 buildMI(MBB
, MBBI
, AVR::ADDRdRr
)
2125 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2126 .addReg(DstHiReg
, RegState::Kill
)
2127 .addReg(DstHiReg
, RegState::Kill
);
2130 buildMI(MBB
, MBBI
, AVR::SBCRdRr
)
2131 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2132 .addReg(DstLoReg
, RegState::Kill
)
2133 .addReg(DstLoReg
, RegState::Kill
);
2135 // lsl r25 <=> add r25, r25
2136 buildMI(MBB
, MBBI
, AVR::ADDRdRr
)
2137 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2138 .addReg(DstHiReg
, RegState::Kill
)
2139 .addReg(DstHiReg
, RegState::Kill
);
2142 buildMI(MBB
, MBBI
, AVR::MOVRdRr
)
2143 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2146 // rol r24 <=> adc r24, r24
2148 buildMI(MBB
, MBBI
, AVR::ADCRdRr
)
2149 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2150 .addReg(DstLoReg
, getKillRegState(DstIsKill
))
2151 .addReg(DstLoReg
, getKillRegState(DstIsKill
));
2154 MIROL
->getOperand(3).setIsDead();
2155 // SREG is always implicitly killed
2156 MIROL
->getOperand(4).setIsKill();
2158 MI
.eraseFromParent();
2162 bool AVRExpandPseudo::expandASRW15Rd(Block
&MBB
, BlockIt MBBI
) {
2163 MachineInstr
&MI
= *MBBI
;
2164 Register DstLoReg
, DstHiReg
;
2165 Register DstReg
= MI
.getOperand(0).getReg();
2166 bool DstIsDead
= MI
.getOperand(0).isDead();
2167 bool ImpIsDead
= MI
.getOperand(3).isDead();
2168 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
2174 // lsl r25 <=> add r25, r25
2175 buildMI(MBB
, MBBI
, AVR::ADDRdRr
)
2176 .addReg(DstHiReg
, RegState::Define
)
2177 .addReg(DstHiReg
, RegState::Kill
)
2178 .addReg(DstHiReg
, RegState::Kill
);
2182 buildMI(MBB
, MBBI
, AVR::SBCRdRr
)
2183 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2184 .addReg(DstHiReg
, RegState::Kill
)
2185 .addReg(DstHiReg
, RegState::Kill
);
2187 MISBC
->getOperand(3).setIsDead();
2188 // SREG is always implicitly killed
2189 MISBC
->getOperand(4).setIsKill();
2192 buildMI(MBB
, MBBI
, AVR::MOVRdRr
)
2193 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2196 MI
.eraseFromParent();
2201 bool AVRExpandPseudo::expand
<AVR::ASRWNRd
>(Block
&MBB
, BlockIt MBBI
) {
2202 MachineInstr
&MI
= *MBBI
;
2203 unsigned Imm
= MI
.getOperand(2).getImm();
2206 return expandASRW7Rd(MBB
, MBBI
);
2208 return expandASRW8Rd(MBB
, MBBI
);
2210 return expandASRW14Rd(MBB
, MBBI
);
2212 return expandASRW15Rd(MBB
, MBBI
);
2214 llvm_unreachable("unimplemented asrwn");
2219 bool AVRExpandPseudo::expandLSLB7Rd(Block
&MBB
, BlockIt MBBI
) {
2220 MachineInstr
&MI
= *MBBI
;
2221 Register DstReg
= MI
.getOperand(0).getReg();
2222 bool DstIsDead
= MI
.getOperand(0).isDead();
2223 bool DstIsKill
= MI
.getOperand(1).isKill();
2224 bool ImpIsDead
= MI
.getOperand(3).isDead();
2230 buildMI(MBB
, MBBI
, AVR::RORRd
)
2231 .addReg(DstReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2232 .addReg(DstReg
, RegState::Kill
)
2236 buildMI(MBB
, MBBI
, AVR::EORRdRr
)
2237 .addReg(DstReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2238 .addReg(DstReg
, RegState::Kill
)
2239 .addReg(DstReg
, RegState::Kill
);
2242 buildMI(MBB
, MBBI
, AVR::RORRd
)
2243 .addReg(DstReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2244 .addReg(DstReg
, getKillRegState(DstIsKill
));
2247 MIRRC
->getOperand(2).setIsDead();
2249 // SREG is always implicitly killed
2250 MIRRC
->getOperand(3).setIsKill();
2252 MI
.eraseFromParent();
2257 bool AVRExpandPseudo::expand
<AVR::LSLBNRd
>(Block
&MBB
, BlockIt MBBI
) {
2258 MachineInstr
&MI
= *MBBI
;
2259 unsigned Imm
= MI
.getOperand(2).getImm();
2262 return expandLSLB7Rd(MBB
, MBBI
);
2264 llvm_unreachable("unimplemented lslbn");
2269 bool AVRExpandPseudo::expandLSRB7Rd(Block
&MBB
, BlockIt MBBI
) {
2270 MachineInstr
&MI
= *MBBI
;
2271 Register DstReg
= MI
.getOperand(0).getReg();
2272 bool DstIsDead
= MI
.getOperand(0).isDead();
2273 bool DstIsKill
= MI
.getOperand(1).isKill();
2274 bool ImpIsDead
= MI
.getOperand(3).isDead();
2280 buildMI(MBB
, MBBI
, AVR::ADCRdRr
)
2281 .addReg(DstReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2282 .addReg(DstReg
, RegState::Kill
)
2283 .addReg(DstReg
, RegState::Kill
)
2287 buildMI(MBB
, MBBI
, AVR::EORRdRr
)
2288 .addReg(DstReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2289 .addReg(DstReg
, RegState::Kill
)
2290 .addReg(DstReg
, RegState::Kill
);
2293 buildMI(MBB
, MBBI
, AVR::ADCRdRr
)
2294 .addReg(DstReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2295 .addReg(DstReg
, getKillRegState(DstIsKill
))
2296 .addReg(DstReg
, getKillRegState(DstIsKill
));
2299 MIRRC
->getOperand(3).setIsDead();
2301 // SREG is always implicitly killed
2302 MIRRC
->getOperand(4).setIsKill();
2304 MI
.eraseFromParent();
2309 bool AVRExpandPseudo::expand
<AVR::LSRBNRd
>(Block
&MBB
, BlockIt MBBI
) {
2310 MachineInstr
&MI
= *MBBI
;
2311 unsigned Imm
= MI
.getOperand(2).getImm();
2314 return expandLSRB7Rd(MBB
, MBBI
);
2316 llvm_unreachable("unimplemented lsrbn");
2321 bool AVRExpandPseudo::expandASRB6Rd(Block
&MBB
, BlockIt MBBI
) {
2322 MachineInstr
&MI
= *MBBI
;
2323 Register DstReg
= MI
.getOperand(0).getReg();
2324 bool DstIsDead
= MI
.getOperand(0).isDead();
2325 bool DstIsKill
= MI
.getOperand(1).isKill();
2332 buildMI(MBB
, MBBI
, AVR::BST
)
2338 buildMI(MBB
, MBBI
, AVR::ADDRdRr
) // LSL Rd <==> ADD Rd, Rd
2339 .addReg(DstReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2340 .addReg(DstReg
, RegState::Kill
)
2341 .addReg(DstReg
, RegState::Kill
);
2343 buildMI(MBB
, MBBI
, AVR::SBCRdRr
)
2344 .addReg(DstReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2345 .addReg(DstReg
, RegState::Kill
)
2346 .addReg(DstReg
, RegState::Kill
);
2348 buildMI(MBB
, MBBI
, AVR::BLD
)
2349 .addReg(DstReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2350 .addReg(DstReg
, getKillRegState(DstIsKill
))
2355 MI
.eraseFromParent();
2359 bool AVRExpandPseudo::expandASRB7Rd(Block
&MBB
, BlockIt MBBI
) {
2360 MachineInstr
&MI
= *MBBI
;
2361 Register DstReg
= MI
.getOperand(0).getReg();
2362 bool DstIsDead
= MI
.getOperand(0).isDead();
2363 bool DstIsKill
= MI
.getOperand(1).isKill();
2364 bool ImpIsDead
= MI
.getOperand(3).isDead();
2369 buildMI(MBB
, MBBI
, AVR::ADDRdRr
)
2370 .addReg(DstReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2371 .addReg(DstReg
, RegState::Kill
)
2372 .addReg(DstReg
, RegState::Kill
);
2375 buildMI(MBB
, MBBI
, AVR::SBCRdRr
)
2376 .addReg(DstReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2377 .addReg(DstReg
, getKillRegState(DstIsKill
))
2378 .addReg(DstReg
, getKillRegState(DstIsKill
));
2381 MIRRC
->getOperand(3).setIsDead();
2383 // SREG is always implicitly killed
2384 MIRRC
->getOperand(4).setIsKill();
2386 MI
.eraseFromParent();
2391 bool AVRExpandPseudo::expand
<AVR::ASRBNRd
>(Block
&MBB
, BlockIt MBBI
) {
2392 MachineInstr
&MI
= *MBBI
;
2393 unsigned Imm
= MI
.getOperand(2).getImm();
2396 return expandASRB6Rd(MBB
, MBBI
);
2398 return expandASRB7Rd(MBB
, MBBI
);
2400 llvm_unreachable("unimplemented asrbn");
2405 template <> bool AVRExpandPseudo::expand
<AVR::SEXT
>(Block
&MBB
, BlockIt MBBI
) {
2406 MachineInstr
&MI
= *MBBI
;
2407 Register DstLoReg
, DstHiReg
;
2408 // sext R17:R16, R17
2412 // sext R17:R16, R13
2417 // sext R17:R16, R16
2421 Register DstReg
= MI
.getOperand(0).getReg();
2422 Register SrcReg
= MI
.getOperand(1).getReg();
2423 bool DstIsDead
= MI
.getOperand(0).isDead();
2424 bool SrcIsKill
= MI
.getOperand(1).isKill();
2425 bool ImpIsDead
= MI
.getOperand(2).isDead();
2426 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
2428 if (SrcReg
!= DstLoReg
)
2429 buildMI(MBB
, MBBI
, AVR::MOVRdRr
)
2430 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2433 if (SrcReg
!= DstHiReg
) {
2434 auto MOV
= buildMI(MBB
, MBBI
, AVR::MOVRdRr
)
2435 .addReg(DstHiReg
, RegState::Define
)
2437 if (SrcReg
!= DstLoReg
&& SrcIsKill
)
2438 MOV
->getOperand(1).setIsKill();
2441 buildMI(MBB
, MBBI
, AVR::ADDRdRr
) // LSL Rd <==> ADD Rd, Rr
2442 .addReg(DstHiReg
, RegState::Define
)
2443 .addReg(DstHiReg
, RegState::Kill
)
2444 .addReg(DstHiReg
, RegState::Kill
);
2447 buildMI(MBB
, MBBI
, AVR::SBCRdRr
)
2448 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2449 .addReg(DstHiReg
, RegState::Kill
)
2450 .addReg(DstHiReg
, RegState::Kill
);
2453 SBC
->getOperand(3).setIsDead();
2455 // SREG is always implicitly killed
2456 SBC
->getOperand(4).setIsKill();
2458 MI
.eraseFromParent();
2462 template <> bool AVRExpandPseudo::expand
<AVR::ZEXT
>(Block
&MBB
, BlockIt MBBI
) {
2463 MachineInstr
&MI
= *MBBI
;
2464 Register DstLoReg
, DstHiReg
;
2465 // zext R25:R24, R20
2468 // zext R25:R24, R24
2470 // zext R25:R24, R25
2473 Register DstReg
= MI
.getOperand(0).getReg();
2474 Register SrcReg
= MI
.getOperand(1).getReg();
2475 bool DstIsDead
= MI
.getOperand(0).isDead();
2476 bool SrcIsKill
= MI
.getOperand(1).isKill();
2477 bool ImpIsDead
= MI
.getOperand(2).isDead();
2478 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
2480 if (SrcReg
!= DstLoReg
) {
2481 buildMI(MBB
, MBBI
, AVR::MOVRdRr
)
2482 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2483 .addReg(SrcReg
, getKillRegState(SrcIsKill
));
2487 buildMI(MBB
, MBBI
, AVR::EORRdRr
)
2488 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2489 .addReg(DstHiReg
, RegState::Kill
| RegState::Undef
)
2490 .addReg(DstHiReg
, RegState::Kill
| RegState::Undef
);
2493 EOR
->getOperand(3).setIsDead();
2495 MI
.eraseFromParent();
2500 bool AVRExpandPseudo::expand
<AVR::SPREAD
>(Block
&MBB
, BlockIt MBBI
) {
2501 MachineInstr
&MI
= *MBBI
;
2502 Register DstLoReg
, DstHiReg
;
2503 Register DstReg
= MI
.getOperand(0).getReg();
2504 bool DstIsDead
= MI
.getOperand(0).isDead();
2505 unsigned Flags
= MI
.getFlags();
2506 unsigned OpLo
= AVR::INRdA
;
2507 unsigned OpHi
= AVR::INRdA
;
2508 TRI
->splitReg(DstReg
, DstLoReg
, DstHiReg
);
2511 buildMI(MBB
, MBBI
, OpLo
)
2512 .addReg(DstLoReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2517 buildMI(MBB
, MBBI
, OpHi
)
2518 .addReg(DstHiReg
, RegState::Define
| getDeadRegState(DstIsDead
))
2522 MI
.eraseFromParent();
2527 bool AVRExpandPseudo::expand
<AVR::SPWRITE
>(Block
&MBB
, BlockIt MBBI
) {
2528 const AVRSubtarget
&STI
= MBB
.getParent()->getSubtarget
<AVRSubtarget
>();
2529 MachineInstr
&MI
= *MBBI
;
2530 Register SrcLoReg
, SrcHiReg
;
2531 Register SrcReg
= MI
.getOperand(1).getReg();
2532 bool SrcIsKill
= MI
.getOperand(1).isKill();
2533 unsigned Flags
= MI
.getFlags();
2534 TRI
->splitReg(SrcReg
, SrcLoReg
, SrcHiReg
);
2536 buildMI(MBB
, MBBI
, AVR::INRdA
)
2537 .addReg(STI
.getTmpRegister(), RegState::Define
)
2538 .addImm(STI
.getIORegSREG())
2541 buildMI(MBB
, MBBI
, AVR::BCLRs
).addImm(0x07).setMIFlags(Flags
);
2543 buildMI(MBB
, MBBI
, AVR::OUTARr
)
2545 .addReg(SrcHiReg
, getKillRegState(SrcIsKill
))
2548 buildMI(MBB
, MBBI
, AVR::OUTARr
)
2549 .addImm(STI
.getIORegSREG())
2550 .addReg(STI
.getTmpRegister(), RegState::Kill
)
2553 buildMI(MBB
, MBBI
, AVR::OUTARr
)
2555 .addReg(SrcLoReg
, getKillRegState(SrcIsKill
))
2558 MI
.eraseFromParent();
2562 bool AVRExpandPseudo::expandMI(Block
&MBB
, BlockIt MBBI
) {
2563 MachineInstr
&MI
= *MBBI
;
2564 int Opcode
= MBBI
->getOpcode();
2566 #define EXPAND(Op) \
2568 return expand<Op>(MBB, MI)
2571 EXPAND(AVR::ADDWRdRr
);
2572 EXPAND(AVR::ADCWRdRr
);
2573 EXPAND(AVR::SUBWRdRr
);
2574 EXPAND(AVR::SUBIWRdK
);
2575 EXPAND(AVR::SBCWRdRr
);
2576 EXPAND(AVR::SBCIWRdK
);
2577 EXPAND(AVR::ANDWRdRr
);
2578 EXPAND(AVR::ANDIWRdK
);
2579 EXPAND(AVR::ORWRdRr
);
2580 EXPAND(AVR::ORIWRdK
);
2581 EXPAND(AVR::EORWRdRr
);
2582 EXPAND(AVR::COMWRd
);
2583 EXPAND(AVR::NEGWRd
);
2584 EXPAND(AVR::CPWRdRr
);
2585 EXPAND(AVR::CPCWRdRr
);
2586 EXPAND(AVR::LDIWRdK
);
2587 EXPAND(AVR::LDSWRdK
);
2588 EXPAND(AVR::LDWRdPtr
);
2589 EXPAND(AVR::LDWRdPtrPi
);
2590 EXPAND(AVR::LDWRdPtrPd
);
2591 case AVR::LDDWRdYQ
: //: FIXME: remove this once PR13375 gets fixed
2592 EXPAND(AVR::LDDWRdPtrQ
);
2593 EXPAND(AVR::LPMBRdZ
);
2594 EXPAND(AVR::LPMWRdZ
);
2595 EXPAND(AVR::LPMWRdZPi
);
2596 EXPAND(AVR::ELPMBRdZ
);
2597 EXPAND(AVR::ELPMWRdZ
);
2598 EXPAND(AVR::ELPMBRdZPi
);
2599 EXPAND(AVR::ELPMWRdZPi
);
2600 EXPAND(AVR::AtomicLoad8
);
2601 EXPAND(AVR::AtomicLoad16
);
2602 EXPAND(AVR::AtomicStore8
);
2603 EXPAND(AVR::AtomicStore16
);
2604 EXPAND(AVR::AtomicFence
);
2605 EXPAND(AVR::STSWKRr
);
2606 EXPAND(AVR::STWPtrRr
);
2607 EXPAND(AVR::STWPtrPiRr
);
2608 EXPAND(AVR::STWPtrPdRr
);
2609 EXPAND(AVR::STDWPtrQRr
);
2610 EXPAND(AVR::STDSPQRr
);
2611 EXPAND(AVR::STDWSPQRr
);
2612 EXPAND(AVR::INWRdA
);
2613 EXPAND(AVR::OUTWARr
);
2614 EXPAND(AVR::PUSHWRr
);
2615 EXPAND(AVR::POPWRd
);
2616 EXPAND(AVR::ROLBRdR1
);
2617 EXPAND(AVR::ROLBRdR17
);
2618 EXPAND(AVR::RORBRd
);
2619 EXPAND(AVR::LSLWRd
);
2620 EXPAND(AVR::LSRWRd
);
2621 EXPAND(AVR::RORWRd
);
2622 EXPAND(AVR::ROLWRd
);
2623 EXPAND(AVR::ASRWRd
);
2624 EXPAND(AVR::LSLWHiRd
);
2625 EXPAND(AVR::LSRWLoRd
);
2626 EXPAND(AVR::ASRWLoRd
);
2627 EXPAND(AVR::LSLWNRd
);
2628 EXPAND(AVR::LSRWNRd
);
2629 EXPAND(AVR::ASRWNRd
);
2630 EXPAND(AVR::LSLBNRd
);
2631 EXPAND(AVR::LSRBNRd
);
2632 EXPAND(AVR::ASRBNRd
);
2635 EXPAND(AVR::SPREAD
);
2636 EXPAND(AVR::SPWRITE
);
2642 } // end of anonymous namespace
2644 INITIALIZE_PASS(AVRExpandPseudo
, "avr-expand-pseudo", AVR_EXPAND_PSEUDO_NAME
,
2648 FunctionPass
*createAVRExpandPseudoPass() { return new AVRExpandPseudo(); }
2650 } // end of namespace llvm