blob: 536a54759c777f9aa499da14abbdc33bf969a52e [file] [log] [blame]
Dylan McKaya789f402016-11-16 21:58:04 +00001//===-- AVRExpandPseudoInsts.cpp - Expand pseudo instructions -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a pass that expands pseudo instructions into target
11// instructions. This pass should be run after register allocation but before
12// the post-regalloc scheduling pass.
13//
14//===----------------------------------------------------------------------===//
15
16#include "AVR.h"
17#include "AVRInstrInfo.h"
18#include "AVRTargetMachine.h"
19#include "MCTargetDesc/AVRMCTargetDesc.h"
20
21#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
Dylan McKay5c90b8c2016-12-10 10:51:55 +000024#include "llvm/CodeGen/RegisterScavenging.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000025#include "llvm/CodeGen/TargetRegisterInfo.h"
Dylan McKaya789f402016-11-16 21:58:04 +000026
27using namespace llvm;
28
Dylan McKay8cec7eb2016-12-07 11:08:56 +000029#define AVR_EXPAND_PSEUDO_NAME "AVR pseudo instruction expansion pass"
30
Dylan McKaya789f402016-11-16 21:58:04 +000031namespace {
32
33/// Expands "placeholder" instructions marked as pseudo into
34/// actual AVR instructions.
35class AVRExpandPseudo : public MachineFunctionPass {
36public:
37 static char ID;
38
Dylan McKay8cec7eb2016-12-07 11:08:56 +000039 AVRExpandPseudo() : MachineFunctionPass(ID) {
40 initializeAVRExpandPseudoPass(*PassRegistry::getPassRegistry());
41 }
Dylan McKaya789f402016-11-16 21:58:04 +000042
43 bool runOnMachineFunction(MachineFunction &MF) override;
44
Dylan McKay8cec7eb2016-12-07 11:08:56 +000045 StringRef getPassName() const override { return AVR_EXPAND_PSEUDO_NAME; }
Dylan McKaya789f402016-11-16 21:58:04 +000046
47private:
48 typedef MachineBasicBlock Block;
49 typedef Block::iterator BlockIt;
50
51 const AVRRegisterInfo *TRI;
52 const TargetInstrInfo *TII;
53
54 /// The register to be used for temporary storage.
55 const unsigned SCRATCH_REGISTER = AVR::R0;
56 /// The IO address of the status register.
57 const unsigned SREG_ADDR = 0x3f;
58
59 bool expandMBB(Block &MBB);
60 bool expandMI(Block &MBB, BlockIt MBBI);
61 template <unsigned OP> bool expand(Block &MBB, BlockIt MBBI);
62
63 MachineInstrBuilder buildMI(Block &MBB, BlockIt MBBI, unsigned Opcode) {
64 return BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(Opcode));
65 }
66
67 MachineInstrBuilder buildMI(Block &MBB, BlockIt MBBI, unsigned Opcode,
68 unsigned DstReg) {
69 return BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(Opcode), DstReg);
70 }
71
72 MachineRegisterInfo &getRegInfo(Block &MBB) { return MBB.getParent()->getRegInfo(); }
73
74 bool expandArith(unsigned OpLo, unsigned OpHi, Block &MBB, BlockIt MBBI);
75 bool expandLogic(unsigned Op, Block &MBB, BlockIt MBBI);
76 bool expandLogicImm(unsigned Op, Block &MBB, BlockIt MBBI);
Dylan McKay453d0422016-12-30 00:21:56 +000077 bool isLogicImmOpRedundant(unsigned Op, unsigned ImmVal) const;
Dylan McKaya789f402016-11-16 21:58:04 +000078
79 template<typename Func>
80 bool expandAtomic(Block &MBB, BlockIt MBBI, Func f);
81
82 template<typename Func>
83 bool expandAtomicBinaryOp(unsigned Opcode, Block &MBB, BlockIt MBBI, Func f);
84
85 bool expandAtomicBinaryOp(unsigned Opcode, Block &MBB, BlockIt MBBI);
86
87 bool expandAtomicArithmeticOp(unsigned MemOpcode,
88 unsigned ArithOpcode,
89 Block &MBB,
90 BlockIt MBBI);
Dylan McKay8f515b12017-04-25 15:09:04 +000091
92 /// Scavenges a free GPR8 register for use.
93 unsigned scavengeGPR8(MachineInstr &MI);
Dylan McKaya789f402016-11-16 21:58:04 +000094};
95
96char AVRExpandPseudo::ID = 0;
97
Dylan McKaya789f402016-11-16 21:58:04 +000098bool AVRExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
99 bool Modified = false;
100
101 BlockIt MBBI = MBB.begin(), E = MBB.end();
102 while (MBBI != E) {
103 BlockIt NMBBI = std::next(MBBI);
104 Modified |= expandMI(MBB, MBBI);
105 MBBI = NMBBI;
106 }
107
108 return Modified;
109}
110
111bool AVRExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
112 bool Modified = false;
113
114 const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
115 TRI = STI.getRegisterInfo();
116 TII = STI.getInstrInfo();
117
Dylan McKay5c90b8c2016-12-10 10:51:55 +0000118 // We need to track liveness in order to use register scavenging.
119 MF.getProperties().set(MachineFunctionProperties::Property::TracksLiveness);
120
Dylan McKaya789f402016-11-16 21:58:04 +0000121 for (Block &MBB : MF) {
122 bool ContinueExpanding = true;
123 unsigned ExpandCount = 0;
124
125 // Continue expanding the block until all pseudos are expanded.
126 do {
127 assert(ExpandCount < 10 && "pseudo expand limit reached");
128
129 bool BlockModified = expandMBB(MBB);
130 Modified |= BlockModified;
131 ExpandCount++;
132
133 ContinueExpanding = BlockModified;
134 } while (ContinueExpanding);
135 }
136
137 return Modified;
138}
139
140bool AVRExpandPseudo::
141expandArith(unsigned OpLo, unsigned OpHi, Block &MBB, BlockIt MBBI) {
142 MachineInstr &MI = *MBBI;
143 unsigned SrcLoReg, SrcHiReg, DstLoReg, DstHiReg;
144 unsigned DstReg = MI.getOperand(0).getReg();
145 unsigned SrcReg = MI.getOperand(2).getReg();
146 bool DstIsDead = MI.getOperand(0).isDead();
147 bool DstIsKill = MI.getOperand(1).isKill();
148 bool SrcIsKill = MI.getOperand(2).isKill();
149 bool ImpIsDead = MI.getOperand(3).isDead();
150 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
151 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
152
153 buildMI(MBB, MBBI, OpLo)
154 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
155 .addReg(DstLoReg, getKillRegState(DstIsKill))
156 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
157
158 auto MIBHI = buildMI(MBB, MBBI, OpHi)
159 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
160 .addReg(DstHiReg, getKillRegState(DstIsKill))
161 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
162
163 if (ImpIsDead)
164 MIBHI->getOperand(3).setIsDead();
165
166 // SREG is always implicitly killed
167 MIBHI->getOperand(4).setIsKill();
168
169 MI.eraseFromParent();
170 return true;
171}
172
173bool AVRExpandPseudo::
174expandLogic(unsigned Op, Block &MBB, BlockIt MBBI) {
175 MachineInstr &MI = *MBBI;
176 unsigned SrcLoReg, SrcHiReg, DstLoReg, DstHiReg;
177 unsigned DstReg = MI.getOperand(0).getReg();
178 unsigned SrcReg = MI.getOperand(2).getReg();
179 bool DstIsDead = MI.getOperand(0).isDead();
180 bool DstIsKill = MI.getOperand(1).isKill();
181 bool SrcIsKill = MI.getOperand(2).isKill();
182 bool ImpIsDead = MI.getOperand(3).isDead();
183 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
184 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
185
186 auto MIBLO = buildMI(MBB, MBBI, Op)
187 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
188 .addReg(DstLoReg, getKillRegState(DstIsKill))
189 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
190
191 // SREG is always implicitly dead
192 MIBLO->getOperand(3).setIsDead();
193
194 auto MIBHI = buildMI(MBB, MBBI, Op)
195 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
196 .addReg(DstHiReg, getKillRegState(DstIsKill))
197 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
198
199 if (ImpIsDead)
200 MIBHI->getOperand(3).setIsDead();
201
202 MI.eraseFromParent();
203 return true;
204}
205
206bool AVRExpandPseudo::
Dylan McKay453d0422016-12-30 00:21:56 +0000207 isLogicImmOpRedundant(unsigned Op, unsigned ImmVal) const {
208
Dylan McKay97cf8372016-12-31 01:07:14 +0000209 // ANDI Rd, 0xff is redundant.
210 if (Op == AVR::ANDIRdK && ImmVal == 0xff)
211 return true;
212
Dylan McKay453d0422016-12-30 00:21:56 +0000213 // ORI Rd, 0x0 is redundant.
214 if (Op == AVR::ORIRdK && ImmVal == 0x0)
215 return true;
216
217 return false;
218}
219
220bool AVRExpandPseudo::
Dylan McKaya789f402016-11-16 21:58:04 +0000221expandLogicImm(unsigned Op, Block &MBB, BlockIt MBBI) {
222 MachineInstr &MI = *MBBI;
223 unsigned DstLoReg, DstHiReg;
224 unsigned DstReg = MI.getOperand(0).getReg();
225 bool DstIsDead = MI.getOperand(0).isDead();
226 bool SrcIsKill = MI.getOperand(1).isKill();
227 bool ImpIsDead = MI.getOperand(3).isDead();
228 unsigned Imm = MI.getOperand(2).getImm();
229 unsigned Lo8 = Imm & 0xff;
230 unsigned Hi8 = (Imm >> 8) & 0xff;
231 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
232
Dylan McKay453d0422016-12-30 00:21:56 +0000233 if (!isLogicImmOpRedundant(Op, Lo8)) {
234 auto MIBLO = buildMI(MBB, MBBI, Op)
235 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
236 .addReg(DstLoReg, getKillRegState(SrcIsKill))
237 .addImm(Lo8);
Dylan McKaya789f402016-11-16 21:58:04 +0000238
Dylan McKay453d0422016-12-30 00:21:56 +0000239 // SREG is always implicitly dead
240 MIBLO->getOperand(3).setIsDead();
241 }
Dylan McKaya789f402016-11-16 21:58:04 +0000242
Dylan McKay453d0422016-12-30 00:21:56 +0000243 if (!isLogicImmOpRedundant(Op, Hi8)) {
244 auto MIBHI = buildMI(MBB, MBBI, Op)
245 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
246 .addReg(DstHiReg, getKillRegState(SrcIsKill))
247 .addImm(Hi8);
Dylan McKaya789f402016-11-16 21:58:04 +0000248
Dylan McKay453d0422016-12-30 00:21:56 +0000249 if (ImpIsDead)
250 MIBHI->getOperand(3).setIsDead();
251 }
Dylan McKaya789f402016-11-16 21:58:04 +0000252
253 MI.eraseFromParent();
254 return true;
255}
256
257template <>
258bool AVRExpandPseudo::expand<AVR::ADDWRdRr>(Block &MBB, BlockIt MBBI) {
259 return expandArith(AVR::ADDRdRr, AVR::ADCRdRr, MBB, MBBI);
260}
261
262template <>
263bool AVRExpandPseudo::expand<AVR::ADCWRdRr>(Block &MBB, BlockIt MBBI) {
264 return expandArith(AVR::ADCRdRr, AVR::ADCRdRr, MBB, MBBI);
265}
266
267template <>
268bool AVRExpandPseudo::expand<AVR::SUBWRdRr>(Block &MBB, BlockIt MBBI) {
269 return expandArith(AVR::SUBRdRr, AVR::SBCRdRr, MBB, MBBI);
270}
271
272template <>
273bool AVRExpandPseudo::expand<AVR::SUBIWRdK>(Block &MBB, BlockIt MBBI) {
274 MachineInstr &MI = *MBBI;
275 unsigned DstLoReg, DstHiReg;
276 unsigned DstReg = MI.getOperand(0).getReg();
277 bool DstIsDead = MI.getOperand(0).isDead();
278 bool SrcIsKill = MI.getOperand(1).isKill();
279 bool ImpIsDead = MI.getOperand(3).isDead();
280 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
281
282 auto MIBLO = buildMI(MBB, MBBI, AVR::SUBIRdK)
283 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
284 .addReg(DstLoReg, getKillRegState(SrcIsKill));
285
286 auto MIBHI = buildMI(MBB, MBBI, AVR::SBCIRdK)
287 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
288 .addReg(DstHiReg, getKillRegState(SrcIsKill));
289
290 switch (MI.getOperand(2).getType()) {
291 case MachineOperand::MO_GlobalAddress: {
292 const GlobalValue *GV = MI.getOperand(2).getGlobal();
293 int64_t Offs = MI.getOperand(2).getOffset();
294 unsigned TF = MI.getOperand(2).getTargetFlags();
295 MIBLO.addGlobalAddress(GV, Offs, TF | AVRII::MO_NEG | AVRII::MO_LO);
296 MIBHI.addGlobalAddress(GV, Offs, TF | AVRII::MO_NEG | AVRII::MO_HI);
297 break;
298 }
299 case MachineOperand::MO_Immediate: {
300 unsigned Imm = MI.getOperand(2).getImm();
301 MIBLO.addImm(Imm & 0xff);
302 MIBHI.addImm((Imm >> 8) & 0xff);
303 break;
304 }
305 default:
306 llvm_unreachable("Unknown operand type!");
307 }
308
309 if (ImpIsDead)
310 MIBHI->getOperand(3).setIsDead();
311
312 // SREG is always implicitly killed
313 MIBHI->getOperand(4).setIsKill();
314
315 MI.eraseFromParent();
316 return true;
317}
318
319template <>
320bool AVRExpandPseudo::expand<AVR::SBCWRdRr>(Block &MBB, BlockIt MBBI) {
321 return expandArith(AVR::SBCRdRr, AVR::SBCRdRr, MBB, MBBI);
322}
323
324template <>
325bool AVRExpandPseudo::expand<AVR::SBCIWRdK>(Block &MBB, BlockIt MBBI) {
326 MachineInstr &MI = *MBBI;
327 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
328 unsigned DstReg = MI.getOperand(0).getReg();
329 bool DstIsDead = MI.getOperand(0).isDead();
330 bool SrcIsKill = MI.getOperand(1).isKill();
331 bool ImpIsDead = MI.getOperand(3).isDead();
332 unsigned Imm = MI.getOperand(2).getImm();
333 unsigned Lo8 = Imm & 0xff;
334 unsigned Hi8 = (Imm >> 8) & 0xff;
335 OpLo = AVR::SBCIRdK;
336 OpHi = AVR::SBCIRdK;
337 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
338
339 auto MIBLO = buildMI(MBB, MBBI, OpLo)
340 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
341 .addReg(DstLoReg, getKillRegState(SrcIsKill))
342 .addImm(Lo8);
343
344 // SREG is always implicitly killed
345 MIBLO->getOperand(4).setIsKill();
346
347 auto MIBHI = buildMI(MBB, MBBI, OpHi)
348 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
349 .addReg(DstHiReg, getKillRegState(SrcIsKill))
350 .addImm(Hi8);
351
352 if (ImpIsDead)
353 MIBHI->getOperand(3).setIsDead();
354
355 // SREG is always implicitly killed
356 MIBHI->getOperand(4).setIsKill();
357
358 MI.eraseFromParent();
359 return true;
360}
361
362template <>
363bool AVRExpandPseudo::expand<AVR::ANDWRdRr>(Block &MBB, BlockIt MBBI) {
364 return expandLogic(AVR::ANDRdRr, MBB, MBBI);
365}
366
367template <>
368bool AVRExpandPseudo::expand<AVR::ANDIWRdK>(Block &MBB, BlockIt MBBI) {
369 return expandLogicImm(AVR::ANDIRdK, MBB, MBBI);
370}
371
372template <>
373bool AVRExpandPseudo::expand<AVR::ORWRdRr>(Block &MBB, BlockIt MBBI) {
374 return expandLogic(AVR::ORRdRr, MBB, MBBI);
375}
376
377template <>
378bool AVRExpandPseudo::expand<AVR::ORIWRdK>(Block &MBB, BlockIt MBBI) {
379 return expandLogicImm(AVR::ORIRdK, MBB, MBBI);
380}
381
382template <>
383bool AVRExpandPseudo::expand<AVR::EORWRdRr>(Block &MBB, BlockIt MBBI) {
384 return expandLogic(AVR::EORRdRr, MBB, MBBI);
385}
386
387template <>
388bool AVRExpandPseudo::expand<AVR::COMWRd>(Block &MBB, BlockIt MBBI) {
389 MachineInstr &MI = *MBBI;
390 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
391 unsigned DstReg = MI.getOperand(0).getReg();
392 bool DstIsDead = MI.getOperand(0).isDead();
393 bool DstIsKill = MI.getOperand(1).isKill();
394 bool ImpIsDead = MI.getOperand(2).isDead();
395 OpLo = AVR::COMRd;
396 OpHi = AVR::COMRd;
397 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
398
399 auto MIBLO = buildMI(MBB, MBBI, OpLo)
400 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
401 .addReg(DstLoReg, getKillRegState(DstIsKill));
402
403 // SREG is always implicitly dead
404 MIBLO->getOperand(2).setIsDead();
405
406 auto MIBHI = buildMI(MBB, MBBI, OpHi)
407 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
408 .addReg(DstHiReg, getKillRegState(DstIsKill));
409
410 if (ImpIsDead)
411 MIBHI->getOperand(2).setIsDead();
412
413 MI.eraseFromParent();
414 return true;
415}
416
417template <>
418bool AVRExpandPseudo::expand<AVR::CPWRdRr>(Block &MBB, BlockIt MBBI) {
419 MachineInstr &MI = *MBBI;
420 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg, DstLoReg, DstHiReg;
421 unsigned DstReg = MI.getOperand(0).getReg();
422 unsigned SrcReg = MI.getOperand(1).getReg();
423 bool DstIsKill = MI.getOperand(0).isKill();
424 bool SrcIsKill = MI.getOperand(1).isKill();
425 bool ImpIsDead = MI.getOperand(2).isDead();
426 OpLo = AVR::CPRdRr;
427 OpHi = AVR::CPCRdRr;
428 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
429 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
430
431 // Low part
432 buildMI(MBB, MBBI, OpLo)
433 .addReg(DstLoReg, getKillRegState(DstIsKill))
434 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
435
436 auto MIBHI = buildMI(MBB, MBBI, OpHi)
437 .addReg(DstHiReg, getKillRegState(DstIsKill))
438 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
439
440 if (ImpIsDead)
441 MIBHI->getOperand(2).setIsDead();
442
443 // SREG is always implicitly killed
444 MIBHI->getOperand(3).setIsKill();
445
446 MI.eraseFromParent();
447 return true;
448}
449
450template <>
451bool AVRExpandPseudo::expand<AVR::CPCWRdRr>(Block &MBB, BlockIt MBBI) {
452 MachineInstr &MI = *MBBI;
453 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg, DstLoReg, DstHiReg;
454 unsigned DstReg = MI.getOperand(0).getReg();
455 unsigned SrcReg = MI.getOperand(1).getReg();
456 bool DstIsKill = MI.getOperand(0).isKill();
457 bool SrcIsKill = MI.getOperand(1).isKill();
458 bool ImpIsDead = MI.getOperand(2).isDead();
459 OpLo = AVR::CPCRdRr;
460 OpHi = AVR::CPCRdRr;
461 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
462 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
463
464 auto MIBLO = buildMI(MBB, MBBI, OpLo)
465 .addReg(DstLoReg, getKillRegState(DstIsKill))
466 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
467
468 // SREG is always implicitly killed
469 MIBLO->getOperand(3).setIsKill();
470
471 auto MIBHI = buildMI(MBB, MBBI, OpHi)
472 .addReg(DstHiReg, getKillRegState(DstIsKill))
473 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
474
475 if (ImpIsDead)
476 MIBHI->getOperand(2).setIsDead();
477
478 // SREG is always implicitly killed
479 MIBHI->getOperand(3).setIsKill();
480
481 MI.eraseFromParent();
482 return true;
483}
484
485template <>
486bool AVRExpandPseudo::expand<AVR::LDIWRdK>(Block &MBB, BlockIt MBBI) {
487 MachineInstr &MI = *MBBI;
488 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
489 unsigned DstReg = MI.getOperand(0).getReg();
490 bool DstIsDead = MI.getOperand(0).isDead();
491 OpLo = AVR::LDIRdK;
492 OpHi = AVR::LDIRdK;
493 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
494
495 auto MIBLO = buildMI(MBB, MBBI, OpLo)
496 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead));
497
498 auto MIBHI = buildMI(MBB, MBBI, OpHi)
499 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead));
500
501 switch (MI.getOperand(1).getType()) {
502 case MachineOperand::MO_GlobalAddress: {
503 const GlobalValue *GV = MI.getOperand(1).getGlobal();
504 int64_t Offs = MI.getOperand(1).getOffset();
505 unsigned TF = MI.getOperand(1).getTargetFlags();
506
507 MIBLO.addGlobalAddress(GV, Offs, TF | AVRII::MO_LO);
508 MIBHI.addGlobalAddress(GV, Offs, TF | AVRII::MO_HI);
509 break;
510 }
511 case MachineOperand::MO_BlockAddress: {
512 const BlockAddress *BA = MI.getOperand(1).getBlockAddress();
513 unsigned TF = MI.getOperand(1).getTargetFlags();
514
Diana Picus116bbab2017-01-13 09:58:52 +0000515 MIBLO.add(MachineOperand::CreateBA(BA, TF | AVRII::MO_LO));
516 MIBHI.add(MachineOperand::CreateBA(BA, TF | AVRII::MO_HI));
Dylan McKaya789f402016-11-16 21:58:04 +0000517 break;
518 }
519 case MachineOperand::MO_Immediate: {
520 unsigned Imm = MI.getOperand(1).getImm();
521
522 MIBLO.addImm(Imm & 0xff);
523 MIBHI.addImm((Imm >> 8) & 0xff);
524 break;
525 }
526 default:
527 llvm_unreachable("Unknown operand type!");
528 }
529
530 MI.eraseFromParent();
531 return true;
532}
533
534template <>
535bool AVRExpandPseudo::expand<AVR::LDSWRdK>(Block &MBB, BlockIt MBBI) {
536 MachineInstr &MI = *MBBI;
537 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
538 unsigned DstReg = MI.getOperand(0).getReg();
539 bool DstIsDead = MI.getOperand(0).isDead();
540 OpLo = AVR::LDSRdK;
541 OpHi = AVR::LDSRdK;
542 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
543
544 auto MIBLO = buildMI(MBB, MBBI, OpLo)
545 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead));
546
547 auto MIBHI = buildMI(MBB, MBBI, OpHi)
548 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead));
549
550 switch (MI.getOperand(1).getType()) {
551 case MachineOperand::MO_GlobalAddress: {
552 const GlobalValue *GV = MI.getOperand(1).getGlobal();
553 int64_t Offs = MI.getOperand(1).getOffset();
554 unsigned TF = MI.getOperand(1).getTargetFlags();
555
556 MIBLO.addGlobalAddress(GV, Offs, TF);
557 MIBHI.addGlobalAddress(GV, Offs + 1, TF);
558 break;
559 }
560 case MachineOperand::MO_Immediate: {
561 unsigned Imm = MI.getOperand(1).getImm();
562
563 MIBLO.addImm(Imm);
564 MIBHI.addImm(Imm + 1);
565 break;
566 }
567 default:
568 llvm_unreachable("Unknown operand type!");
569 }
570
Chandler Carruthc73c0302018-08-16 21:30:05 +0000571 MIBLO.setMemRefs(MI.memoperands());
572 MIBHI.setMemRefs(MI.memoperands());
Dylan McKaya789f402016-11-16 21:58:04 +0000573
574 MI.eraseFromParent();
575 return true;
576}
577
578template <>
579bool AVRExpandPseudo::expand<AVR::LDWRdPtr>(Block &MBB, BlockIt MBBI) {
580 MachineInstr &MI = *MBBI;
581 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
582 unsigned DstReg = MI.getOperand(0).getReg();
Dylan McKay8f515b12017-04-25 15:09:04 +0000583 unsigned TmpReg = 0; // 0 for no temporary register
Dylan McKaya789f402016-11-16 21:58:04 +0000584 unsigned SrcReg = MI.getOperand(1).getReg();
Dylan McKaya789f402016-11-16 21:58:04 +0000585 bool SrcIsKill = MI.getOperand(1).isKill();
Dylan McKayd00f9c12017-10-04 10:33:36 +0000586 OpLo = AVR::LDRdPtrPi;
587 OpHi = AVR::LDRdPtr;
Dylan McKaya789f402016-11-16 21:58:04 +0000588 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
589
Dylan McKay8f515b12017-04-25 15:09:04 +0000590 // Use a temporary register if src and dst registers are the same.
591 if (DstReg == SrcReg)
592 TmpReg = scavengeGPR8(MI);
Dylan McKaya789f402016-11-16 21:58:04 +0000593
Dylan McKay8f515b12017-04-25 15:09:04 +0000594 unsigned CurDstLoReg = (DstReg == SrcReg) ? TmpReg : DstLoReg;
595 unsigned CurDstHiReg = (DstReg == SrcReg) ? TmpReg : DstHiReg;
596
597 // Load low byte.
Dylan McKaya789f402016-11-16 21:58:04 +0000598 auto MIBLO = buildMI(MBB, MBBI, OpLo)
Dylan McKay8f515b12017-04-25 15:09:04 +0000599 .addReg(CurDstLoReg, RegState::Define)
Dylan McKayd00f9c12017-10-04 10:33:36 +0000600 .addReg(SrcReg, RegState::Define)
Dylan McKaya789f402016-11-16 21:58:04 +0000601 .addReg(SrcReg);
602
Dylan McKay8f515b12017-04-25 15:09:04 +0000603 // Push low byte onto stack if necessary.
604 if (TmpReg)
605 buildMI(MBB, MBBI, AVR::PUSHRr).addReg(TmpReg);
606
607 // Load high byte.
Dylan McKaya789f402016-11-16 21:58:04 +0000608 auto MIBHI = buildMI(MBB, MBBI, OpHi)
Dylan McKay8f515b12017-04-25 15:09:04 +0000609 .addReg(CurDstHiReg, RegState::Define)
Dylan McKayd00f9c12017-10-04 10:33:36 +0000610 .addReg(SrcReg, getKillRegState(SrcIsKill));
Dylan McKaya789f402016-11-16 21:58:04 +0000611
Dylan McKay8f515b12017-04-25 15:09:04 +0000612 if (TmpReg) {
613 // Move the high byte into the final destination.
614 buildMI(MBB, MBBI, AVR::MOVRdRr).addReg(DstHiReg).addReg(TmpReg);
615
616 // Move the low byte from the scratch space into the final destination.
617 buildMI(MBB, MBBI, AVR::POPRd).addReg(DstLoReg);
618 }
619
Chandler Carruthc73c0302018-08-16 21:30:05 +0000620 MIBLO.setMemRefs(MI.memoperands());
621 MIBHI.setMemRefs(MI.memoperands());
Dylan McKaya789f402016-11-16 21:58:04 +0000622
623 MI.eraseFromParent();
624 return true;
625}
626
627template <>
628bool AVRExpandPseudo::expand<AVR::LDWRdPtrPi>(Block &MBB, BlockIt MBBI) {
629 MachineInstr &MI = *MBBI;
630 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
631 unsigned DstReg = MI.getOperand(0).getReg();
632 unsigned SrcReg = MI.getOperand(1).getReg();
633 bool DstIsDead = MI.getOperand(0).isDead();
634 bool SrcIsDead = MI.getOperand(1).isKill();
635 OpLo = AVR::LDRdPtrPi;
636 OpHi = AVR::LDRdPtrPi;
637 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
638
639 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same");
640
641 auto MIBLO = buildMI(MBB, MBBI, OpLo)
642 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
643 .addReg(SrcReg, RegState::Define)
644 .addReg(SrcReg, RegState::Kill);
645
646 auto MIBHI = buildMI(MBB, MBBI, OpHi)
647 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
648 .addReg(SrcReg, RegState::Define | getDeadRegState(SrcIsDead))
649 .addReg(SrcReg, RegState::Kill);
650
Chandler Carruthc73c0302018-08-16 21:30:05 +0000651 MIBLO.setMemRefs(MI.memoperands());
652 MIBHI.setMemRefs(MI.memoperands());
Dylan McKaya789f402016-11-16 21:58:04 +0000653
654 MI.eraseFromParent();
655 return true;
656}
657
658template <>
659bool AVRExpandPseudo::expand<AVR::LDWRdPtrPd>(Block &MBB, BlockIt MBBI) {
660 MachineInstr &MI = *MBBI;
661 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
662 unsigned DstReg = MI.getOperand(0).getReg();
663 unsigned SrcReg = MI.getOperand(1).getReg();
664 bool DstIsDead = MI.getOperand(0).isDead();
665 bool SrcIsDead = MI.getOperand(1).isKill();
666 OpLo = AVR::LDRdPtrPd;
667 OpHi = AVR::LDRdPtrPd;
668 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
669
670 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same");
671
672 auto MIBHI = buildMI(MBB, MBBI, OpHi)
673 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
674 .addReg(SrcReg, RegState::Define)
675 .addReg(SrcReg, RegState::Kill);
676
677 auto MIBLO = buildMI(MBB, MBBI, OpLo)
678 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
679 .addReg(SrcReg, RegState::Define | getDeadRegState(SrcIsDead))
680 .addReg(SrcReg, RegState::Kill);
681
Chandler Carruthc73c0302018-08-16 21:30:05 +0000682 MIBLO.setMemRefs(MI.memoperands());
683 MIBHI.setMemRefs(MI.memoperands());
Dylan McKaya789f402016-11-16 21:58:04 +0000684
685 MI.eraseFromParent();
686 return true;
687}
688
689template <>
690bool AVRExpandPseudo::expand<AVR::LDDWRdPtrQ>(Block &MBB, BlockIt MBBI) {
691 MachineInstr &MI = *MBBI;
692 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
693 unsigned DstReg = MI.getOperand(0).getReg();
Dylan McKay8f515b12017-04-25 15:09:04 +0000694 unsigned TmpReg = 0; // 0 for no temporary register
Dylan McKaya789f402016-11-16 21:58:04 +0000695 unsigned SrcReg = MI.getOperand(1).getReg();
696 unsigned Imm = MI.getOperand(2).getImm();
Dylan McKaya789f402016-11-16 21:58:04 +0000697 bool SrcIsKill = MI.getOperand(1).isKill();
698 OpLo = AVR::LDDRdPtrQ;
699 OpHi = AVR::LDDRdPtrQ;
700 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
701
Dylan McKayc4b002b2017-10-04 09:51:21 +0000702 // Since we add 1 to the Imm value for the high byte below, and 63 is the highest Imm value
703 // allowed for the instruction, 62 is the limit here.
704 assert(Imm <= 62 && "Offset is out of range");
Dylan McKay8cec7eb2016-12-07 11:08:56 +0000705
Dylan McKay8f515b12017-04-25 15:09:04 +0000706 // Use a temporary register if src and dst registers are the same.
707 if (DstReg == SrcReg)
708 TmpReg = scavengeGPR8(MI);
Dylan McKay8cec7eb2016-12-07 11:08:56 +0000709
Dylan McKay8f515b12017-04-25 15:09:04 +0000710 unsigned CurDstLoReg = (DstReg == SrcReg) ? TmpReg : DstLoReg;
711 unsigned CurDstHiReg = (DstReg == SrcReg) ? TmpReg : DstHiReg;
Dylan McKaya789f402016-11-16 21:58:04 +0000712
Dylan McKay8f515b12017-04-25 15:09:04 +0000713 // Load low byte.
714 auto MIBLO = buildMI(MBB, MBBI, OpLo)
715 .addReg(CurDstLoReg, RegState::Define)
716 .addReg(SrcReg)
717 .addImm(Imm);
Dylan McKaya789f402016-11-16 21:58:04 +0000718
Dylan McKay8f515b12017-04-25 15:09:04 +0000719 // Push low byte onto stack if necessary.
720 if (TmpReg)
721 buildMI(MBB, MBBI, AVR::PUSHRr).addReg(TmpReg);
Dylan McKay8cec7eb2016-12-07 11:08:56 +0000722
Dylan McKay8f515b12017-04-25 15:09:04 +0000723 // Load high byte.
724 auto MIBHI = buildMI(MBB, MBBI, OpHi)
725 .addReg(CurDstHiReg, RegState::Define)
726 .addReg(SrcReg, getKillRegState(SrcIsKill))
727 .addImm(Imm + 1);
Dylan McKaya789f402016-11-16 21:58:04 +0000728
Dylan McKay8f515b12017-04-25 15:09:04 +0000729 if (TmpReg) {
730 // Move the high byte into the final destination.
Dylan McKay5c90b8c2016-12-10 10:51:55 +0000731 buildMI(MBB, MBBI, AVR::MOVRdRr).addReg(DstHiReg).addReg(TmpReg);
Dylan McKay5c90b8c2016-12-10 10:51:55 +0000732
Dylan McKay8f515b12017-04-25 15:09:04 +0000733 // Move the low byte from the scratch space into the final destination.
734 buildMI(MBB, MBBI, AVR::POPRd).addReg(DstLoReg);
Dylan McKay8cec7eb2016-12-07 11:08:56 +0000735 }
736
Chandler Carruthc73c0302018-08-16 21:30:05 +0000737 MIBLO.setMemRefs(MI.memoperands());
738 MIBHI.setMemRefs(MI.memoperands());
Dylan McKaya789f402016-11-16 21:58:04 +0000739
740 MI.eraseFromParent();
741 return true;
742}
743
Dylan McKaya5d49df2016-12-09 07:49:04 +0000744template <>
745bool AVRExpandPseudo::expand<AVR::LPMWRdZ>(Block &MBB, BlockIt MBBI) {
Dylan McKay8dd702c2017-10-04 10:37:22 +0000746 MachineInstr &MI = *MBBI;
747 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
748 unsigned DstReg = MI.getOperand(0).getReg();
749 unsigned TmpReg = 0; // 0 for no temporary register
750 unsigned SrcReg = MI.getOperand(1).getReg();
751 bool SrcIsKill = MI.getOperand(1).isKill();
752 OpLo = AVR::LPMRdZPi;
753 OpHi = AVR::LPMRdZ;
754 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
755
756 // Use a temporary register if src and dst registers are the same.
757 if (DstReg == SrcReg)
758 TmpReg = scavengeGPR8(MI);
759
760 unsigned CurDstLoReg = (DstReg == SrcReg) ? TmpReg : DstLoReg;
761 unsigned CurDstHiReg = (DstReg == SrcReg) ? TmpReg : DstHiReg;
762
763 // Load low byte.
764 auto MIBLO = buildMI(MBB, MBBI, OpLo)
765 .addReg(CurDstLoReg, RegState::Define)
766 .addReg(SrcReg);
767
768 // Push low byte onto stack if necessary.
769 if (TmpReg)
770 buildMI(MBB, MBBI, AVR::PUSHRr).addReg(TmpReg);
771
772 // Load high byte.
773 auto MIBHI = buildMI(MBB, MBBI, OpHi)
774 .addReg(CurDstHiReg, RegState::Define)
775 .addReg(SrcReg, getKillRegState(SrcIsKill));
776
777 if (TmpReg) {
778 // Move the high byte into the final destination.
779 buildMI(MBB, MBBI, AVR::MOVRdRr).addReg(DstHiReg).addReg(TmpReg);
780
781 // Move the low byte from the scratch space into the final destination.
782 buildMI(MBB, MBBI, AVR::POPRd).addReg(DstLoReg);
783 }
784
Chandler Carruthc73c0302018-08-16 21:30:05 +0000785 MIBLO.setMemRefs(MI.memoperands());
786 MIBHI.setMemRefs(MI.memoperands());
Dylan McKay8dd702c2017-10-04 10:37:22 +0000787
788 MI.eraseFromParent();
789 return true;
Dylan McKaya5d49df2016-12-09 07:49:04 +0000790}
791
792template <>
793bool AVRExpandPseudo::expand<AVR::LPMWRdZPi>(Block &MBB, BlockIt MBBI) {
794 llvm_unreachable("wide LPMPi is unimplemented");
795}
796
Dylan McKaya789f402016-11-16 21:58:04 +0000797template<typename Func>
798bool AVRExpandPseudo::expandAtomic(Block &MBB, BlockIt MBBI, Func f) {
799 // Remove the pseudo instruction.
800 MachineInstr &MI = *MBBI;
801
802 // Store the SREG.
803 buildMI(MBB, MBBI, AVR::INRdA)
804 .addReg(SCRATCH_REGISTER, RegState::Define)
805 .addImm(SREG_ADDR);
806
807 // Disable exceptions.
808 buildMI(MBB, MBBI, AVR::BCLRs).addImm(7); // CLI
809
810 f(MI);
811
812 // Restore the status reg.
813 buildMI(MBB, MBBI, AVR::OUTARr)
814 .addImm(SREG_ADDR)
815 .addReg(SCRATCH_REGISTER);
816
817 MI.eraseFromParent();
818 return true;
819}
820
821template<typename Func>
822bool AVRExpandPseudo::expandAtomicBinaryOp(unsigned Opcode,
823 Block &MBB,
824 BlockIt MBBI,
825 Func f) {
826 return expandAtomic(MBB, MBBI, [&](MachineInstr &MI) {
827 auto Op1 = MI.getOperand(0);
828 auto Op2 = MI.getOperand(1);
829
Diana Picus116bbab2017-01-13 09:58:52 +0000830 MachineInstr &NewInst =
831 *buildMI(MBB, MBBI, Opcode).add(Op1).add(Op2).getInstr();
Dylan McKaya789f402016-11-16 21:58:04 +0000832 f(NewInst);
833 });
834}
835
836bool AVRExpandPseudo::expandAtomicBinaryOp(unsigned Opcode,
837 Block &MBB,
838 BlockIt MBBI) {
839 return expandAtomicBinaryOp(Opcode, MBB, MBBI, [](MachineInstr &MI) {});
840}
841
842bool AVRExpandPseudo::expandAtomicArithmeticOp(unsigned Width,
843 unsigned ArithOpcode,
844 Block &MBB,
845 BlockIt MBBI) {
846 return expandAtomic(MBB, MBBI, [&](MachineInstr &MI) {
847 auto Op1 = MI.getOperand(0);
848 auto Op2 = MI.getOperand(1);
849
850 unsigned LoadOpcode = (Width == 8) ? AVR::LDRdPtr : AVR::LDWRdPtr;
851 unsigned StoreOpcode = (Width == 8) ? AVR::STPtrRr : AVR::STWPtrRr;
852
853 // Create the load
Diana Picus116bbab2017-01-13 09:58:52 +0000854 buildMI(MBB, MBBI, LoadOpcode).add(Op1).add(Op2);
Dylan McKaya789f402016-11-16 21:58:04 +0000855
856 // Create the arithmetic op
Diana Picus116bbab2017-01-13 09:58:52 +0000857 buildMI(MBB, MBBI, ArithOpcode).add(Op1).add(Op1).add(Op2);
Dylan McKaya789f402016-11-16 21:58:04 +0000858
859 // Create the store
Diana Picus116bbab2017-01-13 09:58:52 +0000860 buildMI(MBB, MBBI, StoreOpcode).add(Op2).add(Op1);
Dylan McKaya789f402016-11-16 21:58:04 +0000861 });
862}
863
Dylan McKay8f515b12017-04-25 15:09:04 +0000864unsigned AVRExpandPseudo::scavengeGPR8(MachineInstr &MI) {
865 MachineBasicBlock &MBB = *MI.getParent();
866 RegScavenger RS;
867
868 RS.enterBasicBlock(MBB);
869 RS.forward(MI);
870
871 BitVector Candidates =
872 TRI->getAllocatableSet
873 (*MBB.getParent(), &AVR::GPR8RegClass);
874
875 // Exclude all the registers being used by the instruction.
876 for (MachineOperand &MO : MI.operands()) {
877 if (MO.isReg() && MO.getReg() != 0 && !MO.isDef() &&
878 !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
879 Candidates.reset(MO.getReg());
880 }
881
882 BitVector Available = RS.getRegsAvailable(&AVR::GPR8RegClass);
883 Available &= Candidates;
884
885 signed Reg = Available.find_first();
886 assert(Reg != -1 && "ran out of registers");
887 return Reg;
888}
889
Dylan McKaya789f402016-11-16 21:58:04 +0000890template<>
891bool AVRExpandPseudo::expand<AVR::AtomicLoad8>(Block &MBB, BlockIt MBBI) {
892 return expandAtomicBinaryOp(AVR::LDRdPtr, MBB, MBBI);
893}
894
895template<>
896bool AVRExpandPseudo::expand<AVR::AtomicLoad16>(Block &MBB, BlockIt MBBI) {
897 return expandAtomicBinaryOp(AVR::LDWRdPtr, MBB, MBBI);
898}
899
900template<>
901bool AVRExpandPseudo::expand<AVR::AtomicStore8>(Block &MBB, BlockIt MBBI) {
902 return expandAtomicBinaryOp(AVR::STPtrRr, MBB, MBBI);
903}
904
905template<>
906bool AVRExpandPseudo::expand<AVR::AtomicStore16>(Block &MBB, BlockIt MBBI) {
907 return expandAtomicBinaryOp(AVR::STWPtrRr, MBB, MBBI);
908}
909
910template<>
911bool AVRExpandPseudo::expand<AVR::AtomicLoadAdd8>(Block &MBB, BlockIt MBBI) {
912 return expandAtomicArithmeticOp(8, AVR::ADDRdRr, MBB, MBBI);
913}
914
915template<>
916bool AVRExpandPseudo::expand<AVR::AtomicLoadAdd16>(Block &MBB, BlockIt MBBI) {
917 return expandAtomicArithmeticOp(16, AVR::ADDWRdRr, MBB, MBBI);
918}
919
920template<>
921bool AVRExpandPseudo::expand<AVR::AtomicLoadSub8>(Block &MBB, BlockIt MBBI) {
922 return expandAtomicArithmeticOp(8, AVR::SUBRdRr, MBB, MBBI);
923}
924
925template<>
926bool AVRExpandPseudo::expand<AVR::AtomicLoadSub16>(Block &MBB, BlockIt MBBI) {
927 return expandAtomicArithmeticOp(16, AVR::SUBWRdRr, MBB, MBBI);
928}
929
930template<>
931bool AVRExpandPseudo::expand<AVR::AtomicLoadAnd8>(Block &MBB, BlockIt MBBI) {
932 return expandAtomicArithmeticOp(8, AVR::ANDRdRr, MBB, MBBI);
933}
934
935template<>
936bool AVRExpandPseudo::expand<AVR::AtomicLoadAnd16>(Block &MBB, BlockIt MBBI) {
937 return expandAtomicArithmeticOp(16, AVR::ANDWRdRr, MBB, MBBI);
938}
939
940template<>
941bool AVRExpandPseudo::expand<AVR::AtomicLoadOr8>(Block &MBB, BlockIt MBBI) {
942 return expandAtomicArithmeticOp(8, AVR::ORRdRr, MBB, MBBI);
943}
944
945template<>
946bool AVRExpandPseudo::expand<AVR::AtomicLoadOr16>(Block &MBB, BlockIt MBBI) {
947 return expandAtomicArithmeticOp(16, AVR::ORWRdRr, MBB, MBBI);
948}
949
950template<>
951bool AVRExpandPseudo::expand<AVR::AtomicLoadXor8>(Block &MBB, BlockIt MBBI) {
952 return expandAtomicArithmeticOp(8, AVR::EORRdRr, MBB, MBBI);
953}
954
955template<>
956bool AVRExpandPseudo::expand<AVR::AtomicLoadXor16>(Block &MBB, BlockIt MBBI) {
957 return expandAtomicArithmeticOp(16, AVR::EORWRdRr, MBB, MBBI);
958}
959
960template<>
961bool AVRExpandPseudo::expand<AVR::AtomicFence>(Block &MBB, BlockIt MBBI) {
962 // On AVR, there is only one core and so atomic fences do nothing.
963 MBBI->eraseFromParent();
964 return true;
965}
966
967template <>
968bool AVRExpandPseudo::expand<AVR::STSWKRr>(Block &MBB, BlockIt MBBI) {
969 MachineInstr &MI = *MBBI;
970 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
971 unsigned SrcReg = MI.getOperand(1).getReg();
972 bool SrcIsKill = MI.getOperand(1).isKill();
973 OpLo = AVR::STSKRr;
974 OpHi = AVR::STSKRr;
975 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
976
977 // Write the high byte first in case this address belongs to a special
978 // I/O address with a special temporary register.
979 auto MIBHI = buildMI(MBB, MBBI, OpHi);
980 auto MIBLO = buildMI(MBB, MBBI, OpLo);
981
982 switch (MI.getOperand(0).getType()) {
983 case MachineOperand::MO_GlobalAddress: {
984 const GlobalValue *GV = MI.getOperand(0).getGlobal();
985 int64_t Offs = MI.getOperand(0).getOffset();
986 unsigned TF = MI.getOperand(0).getTargetFlags();
987
988 MIBLO.addGlobalAddress(GV, Offs, TF);
989 MIBHI.addGlobalAddress(GV, Offs + 1, TF);
990 break;
991 }
992 case MachineOperand::MO_Immediate: {
993 unsigned Imm = MI.getOperand(0).getImm();
994
995 MIBLO.addImm(Imm);
996 MIBHI.addImm(Imm + 1);
997 break;
998 }
999 default:
1000 llvm_unreachable("Unknown operand type!");
1001 }
1002
1003 MIBLO.addReg(SrcLoReg, getKillRegState(SrcIsKill));
1004 MIBHI.addReg(SrcHiReg, getKillRegState(SrcIsKill));
1005
Chandler Carruthc73c0302018-08-16 21:30:05 +00001006 MIBLO.setMemRefs(MI.memoperands());
1007 MIBHI.setMemRefs(MI.memoperands());
Dylan McKaya789f402016-11-16 21:58:04 +00001008
1009 MI.eraseFromParent();
1010 return true;
1011}
1012
1013template <>
1014bool AVRExpandPseudo::expand<AVR::STWPtrRr>(Block &MBB, BlockIt MBBI) {
1015 MachineInstr &MI = *MBBI;
1016 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
1017 unsigned DstReg = MI.getOperand(0).getReg();
1018 unsigned SrcReg = MI.getOperand(1).getReg();
Dylan McKaya789f402016-11-16 21:58:04 +00001019 bool SrcIsKill = MI.getOperand(1).isKill();
1020 OpLo = AVR::STPtrRr;
1021 OpHi = AVR::STDPtrQRr;
1022 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1023
1024 //:TODO: need to reverse this order like inw and stsw?
1025 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1026 .addReg(DstReg)
1027 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
1028
1029 auto MIBHI = buildMI(MBB, MBBI, OpHi)
Dylan McKayff49a052017-04-25 23:58:20 +00001030 .addReg(DstReg)
Dylan McKaya789f402016-11-16 21:58:04 +00001031 .addImm(1)
1032 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
1033
Chandler Carruthc73c0302018-08-16 21:30:05 +00001034 MIBLO.setMemRefs(MI.memoperands());
1035 MIBHI.setMemRefs(MI.memoperands());
Dylan McKaya789f402016-11-16 21:58:04 +00001036
1037 MI.eraseFromParent();
1038 return true;
1039}
1040
1041template <>
1042bool AVRExpandPseudo::expand<AVR::STWPtrPiRr>(Block &MBB, BlockIt MBBI) {
1043 MachineInstr &MI = *MBBI;
1044 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
1045 unsigned DstReg = MI.getOperand(0).getReg();
1046 unsigned SrcReg = MI.getOperand(2).getReg();
1047 unsigned Imm = MI.getOperand(3).getImm();
1048 bool DstIsDead = MI.getOperand(0).isDead();
1049 bool SrcIsKill = MI.getOperand(2).isKill();
1050 OpLo = AVR::STPtrPiRr;
1051 OpHi = AVR::STPtrPiRr;
1052 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1053
1054 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same");
1055
1056 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1057 .addReg(DstReg, RegState::Define)
1058 .addReg(DstReg, RegState::Kill)
1059 .addReg(SrcLoReg, getKillRegState(SrcIsKill))
1060 .addImm(Imm);
1061
1062 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1063 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1064 .addReg(DstReg, RegState::Kill)
1065 .addReg(SrcHiReg, getKillRegState(SrcIsKill))
1066 .addImm(Imm);
1067
Chandler Carruthc73c0302018-08-16 21:30:05 +00001068 MIBLO.setMemRefs(MI.memoperands());
1069 MIBHI.setMemRefs(MI.memoperands());
Dylan McKaya789f402016-11-16 21:58:04 +00001070
1071 MI.eraseFromParent();
1072 return true;
1073}
1074
1075template <>
1076bool AVRExpandPseudo::expand<AVR::STWPtrPdRr>(Block &MBB, BlockIt MBBI) {
1077 MachineInstr &MI = *MBBI;
1078 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
1079 unsigned DstReg = MI.getOperand(0).getReg();
1080 unsigned SrcReg = MI.getOperand(2).getReg();
1081 unsigned Imm = MI.getOperand(3).getImm();
1082 bool DstIsDead = MI.getOperand(0).isDead();
1083 bool SrcIsKill = MI.getOperand(2).isKill();
1084 OpLo = AVR::STPtrPdRr;
1085 OpHi = AVR::STPtrPdRr;
1086 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1087
1088 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same");
1089
1090 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1091 .addReg(DstReg, RegState::Define)
1092 .addReg(DstReg, RegState::Kill)
1093 .addReg(SrcHiReg, getKillRegState(SrcIsKill))
1094 .addImm(Imm);
1095
1096 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1097 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
1098 .addReg(DstReg, RegState::Kill)
1099 .addReg(SrcLoReg, getKillRegState(SrcIsKill))
1100 .addImm(Imm);
1101
Chandler Carruthc73c0302018-08-16 21:30:05 +00001102 MIBLO.setMemRefs(MI.memoperands());
1103 MIBHI.setMemRefs(MI.memoperands());
Dylan McKaya789f402016-11-16 21:58:04 +00001104
1105 MI.eraseFromParent();
1106 return true;
1107}
1108
1109template <>
1110bool AVRExpandPseudo::expand<AVR::STDWPtrQRr>(Block &MBB, BlockIt MBBI) {
1111 MachineInstr &MI = *MBBI;
1112 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
1113 unsigned DstReg = MI.getOperand(0).getReg();
1114 unsigned SrcReg = MI.getOperand(2).getReg();
1115 unsigned Imm = MI.getOperand(1).getImm();
1116 bool DstIsKill = MI.getOperand(0).isKill();
1117 bool SrcIsKill = MI.getOperand(2).isKill();
1118 OpLo = AVR::STDPtrQRr;
1119 OpHi = AVR::STDPtrQRr;
1120 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1121
Dylan McKayc4b002b2017-10-04 09:51:21 +00001122 // Since we add 1 to the Imm value for the high byte below, and 63 is the highest Imm value
1123 // allowed for the instruction, 62 is the limit here.
1124 assert(Imm <= 62 && "Offset is out of range");
Dylan McKaya789f402016-11-16 21:58:04 +00001125
1126 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1127 .addReg(DstReg)
1128 .addImm(Imm)
1129 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
1130
1131 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1132 .addReg(DstReg, getKillRegState(DstIsKill))
1133 .addImm(Imm + 1)
1134 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
1135
Chandler Carruthc73c0302018-08-16 21:30:05 +00001136 MIBLO.setMemRefs(MI.memoperands());
1137 MIBHI.setMemRefs(MI.memoperands());
Dylan McKaya789f402016-11-16 21:58:04 +00001138
1139 MI.eraseFromParent();
1140 return true;
1141}
1142
1143template <>
1144bool AVRExpandPseudo::expand<AVR::INWRdA>(Block &MBB, BlockIt MBBI) {
1145 MachineInstr &MI = *MBBI;
1146 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1147 unsigned Imm = MI.getOperand(1).getImm();
1148 unsigned DstReg = MI.getOperand(0).getReg();
1149 bool DstIsDead = MI.getOperand(0).isDead();
1150 OpLo = AVR::INRdA;
1151 OpHi = AVR::INRdA;
1152 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1153
Dylan McKayc4b002b2017-10-04 09:51:21 +00001154 // Since we add 1 to the Imm value for the high byte below, and 63 is the highest Imm value
1155 // allowed for the instruction, 62 is the limit here.
1156 assert(Imm <= 62 && "Address is out of range");
Dylan McKaya789f402016-11-16 21:58:04 +00001157
1158 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1159 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1160 .addImm(Imm);
1161
1162 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1163 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1164 .addImm(Imm + 1);
1165
Chandler Carruthc73c0302018-08-16 21:30:05 +00001166 MIBLO.setMemRefs(MI.memoperands());
1167 MIBHI.setMemRefs(MI.memoperands());
Dylan McKaya789f402016-11-16 21:58:04 +00001168
1169 MI.eraseFromParent();
1170 return true;
1171}
1172
1173template <>
1174bool AVRExpandPseudo::expand<AVR::OUTWARr>(Block &MBB, BlockIt MBBI) {
1175 MachineInstr &MI = *MBBI;
1176 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
1177 unsigned Imm = MI.getOperand(0).getImm();
1178 unsigned SrcReg = MI.getOperand(1).getReg();
1179 bool SrcIsKill = MI.getOperand(1).isKill();
1180 OpLo = AVR::OUTARr;
1181 OpHi = AVR::OUTARr;
1182 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1183
Dylan McKayc4b002b2017-10-04 09:51:21 +00001184 // Since we add 1 to the Imm value for the high byte below, and 63 is the highest Imm value
1185 // allowed for the instruction, 62 is the limit here.
1186 assert(Imm <= 62 && "Address is out of range");
Dylan McKaya789f402016-11-16 21:58:04 +00001187
1188 // 16 bit I/O writes need the high byte first
1189 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1190 .addImm(Imm + 1)
1191 .addReg(SrcHiReg, getKillRegState(SrcIsKill));
1192
1193 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1194 .addImm(Imm)
1195 .addReg(SrcLoReg, getKillRegState(SrcIsKill));
1196
Chandler Carruthc73c0302018-08-16 21:30:05 +00001197 MIBLO.setMemRefs(MI.memoperands());
1198 MIBHI.setMemRefs(MI.memoperands());
Dylan McKaya789f402016-11-16 21:58:04 +00001199
1200 MI.eraseFromParent();
1201 return true;
1202}
1203
1204template <>
1205bool AVRExpandPseudo::expand<AVR::PUSHWRr>(Block &MBB, BlockIt MBBI) {
1206 MachineInstr &MI = *MBBI;
1207 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg;
1208 unsigned SrcReg = MI.getOperand(0).getReg();
1209 bool SrcIsKill = MI.getOperand(0).isKill();
1210 unsigned Flags = MI.getFlags();
1211 OpLo = AVR::PUSHRr;
1212 OpHi = AVR::PUSHRr;
1213 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1214
1215 // Low part
1216 buildMI(MBB, MBBI, OpLo)
1217 .addReg(SrcLoReg, getKillRegState(SrcIsKill))
1218 .setMIFlags(Flags);
1219
1220 // High part
1221 buildMI(MBB, MBBI, OpHi)
1222 .addReg(SrcHiReg, getKillRegState(SrcIsKill))
1223 .setMIFlags(Flags);
1224
1225 MI.eraseFromParent();
1226 return true;
1227}
1228
1229template <>
1230bool AVRExpandPseudo::expand<AVR::POPWRd>(Block &MBB, BlockIt MBBI) {
1231 MachineInstr &MI = *MBBI;
1232 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1233 unsigned DstReg = MI.getOperand(0).getReg();
1234 unsigned Flags = MI.getFlags();
1235 OpLo = AVR::POPRd;
1236 OpHi = AVR::POPRd;
1237 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1238
1239 buildMI(MBB, MBBI, OpHi, DstHiReg).setMIFlags(Flags); // High
1240 buildMI(MBB, MBBI, OpLo, DstLoReg).setMIFlags(Flags); // Low
1241
1242 MI.eraseFromParent();
1243 return true;
1244}
1245
1246template <>
1247bool AVRExpandPseudo::expand<AVR::LSLWRd>(Block &MBB, BlockIt MBBI) {
1248 MachineInstr &MI = *MBBI;
1249 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1250 unsigned DstReg = MI.getOperand(0).getReg();
1251 bool DstIsDead = MI.getOperand(0).isDead();
1252 bool DstIsKill = MI.getOperand(1).isKill();
1253 bool ImpIsDead = MI.getOperand(2).isDead();
Dylan McKay45425862018-09-01 12:23:00 +00001254 OpLo = AVR::ADDRdRr; // ADD Rd, Rd <==> LSL Rd
Dylan McKay8b0f9d22018-09-01 12:22:07 +00001255 OpHi = AVR::ADCRdRr; // ADC Rd, Rd <==> ROL Rd
Dylan McKaya789f402016-11-16 21:58:04 +00001256 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1257
1258 // Low part
1259 buildMI(MBB, MBBI, OpLo)
1260 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
Dylan McKay45425862018-09-01 12:23:00 +00001261 .addReg(DstLoReg)
Dylan McKaya789f402016-11-16 21:58:04 +00001262 .addReg(DstLoReg, getKillRegState(DstIsKill));
1263
1264 auto MIBHI = buildMI(MBB, MBBI, OpHi)
1265 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
Dylan McKay8b0f9d22018-09-01 12:22:07 +00001266 .addReg(DstHiReg)
Dylan McKaya789f402016-11-16 21:58:04 +00001267 .addReg(DstHiReg, getKillRegState(DstIsKill));
1268
1269 if (ImpIsDead)
Dylan McKay8b0f9d22018-09-01 12:22:07 +00001270 MIBHI->getOperand(3).setIsDead();
Dylan McKaya789f402016-11-16 21:58:04 +00001271
1272 // SREG is always implicitly killed
Dylan McKay8b0f9d22018-09-01 12:22:07 +00001273 MIBHI->getOperand(4).setIsKill();
Dylan McKaya789f402016-11-16 21:58:04 +00001274
1275 MI.eraseFromParent();
1276 return true;
1277}
1278
1279template <>
1280bool AVRExpandPseudo::expand<AVR::LSRWRd>(Block &MBB, BlockIt MBBI) {
1281 MachineInstr &MI = *MBBI;
1282 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1283 unsigned DstReg = MI.getOperand(0).getReg();
1284 bool DstIsDead = MI.getOperand(0).isDead();
1285 bool DstIsKill = MI.getOperand(1).isKill();
1286 bool ImpIsDead = MI.getOperand(2).isDead();
1287 OpLo = AVR::RORRd;
1288 OpHi = AVR::LSRRd;
1289 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1290
1291 // High part
1292 buildMI(MBB, MBBI, OpHi)
1293 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1294 .addReg(DstHiReg, getKillRegState(DstIsKill));
1295
1296 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1297 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1298 .addReg(DstLoReg, getKillRegState(DstIsKill));
1299
1300 if (ImpIsDead)
1301 MIBLO->getOperand(2).setIsDead();
1302
1303 // SREG is always implicitly killed
1304 MIBLO->getOperand(3).setIsKill();
1305
1306 MI.eraseFromParent();
1307 return true;
1308}
1309
1310template <>
1311bool AVRExpandPseudo::expand<AVR::RORWRd>(Block &MBB, BlockIt MBBI) {
1312 llvm_unreachable("RORW unimplemented");
1313 return false;
1314}
1315
1316template <>
1317bool AVRExpandPseudo::expand<AVR::ROLWRd>(Block &MBB, BlockIt MBBI) {
1318 llvm_unreachable("ROLW unimplemented");
1319 return false;
1320}
1321
1322template <>
1323bool AVRExpandPseudo::expand<AVR::ASRWRd>(Block &MBB, BlockIt MBBI) {
1324 MachineInstr &MI = *MBBI;
1325 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1326 unsigned DstReg = MI.getOperand(0).getReg();
1327 bool DstIsDead = MI.getOperand(0).isDead();
1328 bool DstIsKill = MI.getOperand(1).isKill();
1329 bool ImpIsDead = MI.getOperand(2).isDead();
1330 OpLo = AVR::RORRd;
1331 OpHi = AVR::ASRRd;
1332 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1333
1334 // High part
1335 buildMI(MBB, MBBI, OpHi)
1336 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1337 .addReg(DstHiReg, getKillRegState(DstIsKill));
1338
1339 auto MIBLO = buildMI(MBB, MBBI, OpLo)
1340 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1341 .addReg(DstLoReg, getKillRegState(DstIsKill));
1342
1343 if (ImpIsDead)
1344 MIBLO->getOperand(2).setIsDead();
1345
1346 // SREG is always implicitly killed
1347 MIBLO->getOperand(3).setIsKill();
1348
1349 MI.eraseFromParent();
1350 return true;
1351}
1352
1353template <> bool AVRExpandPseudo::expand<AVR::SEXT>(Block &MBB, BlockIt MBBI) {
1354 MachineInstr &MI = *MBBI;
1355 unsigned DstLoReg, DstHiReg;
1356 // sext R17:R16, R17
1357 // mov r16, r17
1358 // lsl r17
1359 // sbc r17, r17
1360 // sext R17:R16, R13
1361 // mov r16, r13
1362 // mov r17, r13
1363 // lsl r17
1364 // sbc r17, r17
1365 // sext R17:R16, R16
1366 // mov r17, r16
1367 // lsl r17
1368 // sbc r17, r17
1369 unsigned DstReg = MI.getOperand(0).getReg();
1370 unsigned SrcReg = MI.getOperand(1).getReg();
1371 bool DstIsDead = MI.getOperand(0).isDead();
1372 bool SrcIsKill = MI.getOperand(1).isKill();
1373 bool ImpIsDead = MI.getOperand(2).isDead();
1374 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1375
1376 if (SrcReg != DstLoReg) {
1377 auto MOV = buildMI(MBB, MBBI, AVR::MOVRdRr)
1378 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1379 .addReg(SrcReg);
1380
1381 if (SrcReg == DstHiReg) {
1382 MOV->getOperand(1).setIsKill();
1383 }
1384 }
1385
1386 if (SrcReg != DstHiReg) {
1387 buildMI(MBB, MBBI, AVR::MOVRdRr)
1388 .addReg(DstHiReg, RegState::Define)
1389 .addReg(SrcReg, getKillRegState(SrcIsKill));
1390 }
1391
Dylan McKay45425862018-09-01 12:23:00 +00001392 buildMI(MBB, MBBI, AVR::ADDRdRr) // LSL Rd <==> ADD Rd, Rr
Dylan McKaya789f402016-11-16 21:58:04 +00001393 .addReg(DstHiReg, RegState::Define)
Dylan McKay45425862018-09-01 12:23:00 +00001394 .addReg(DstHiReg)
Dylan McKaya789f402016-11-16 21:58:04 +00001395 .addReg(DstHiReg, RegState::Kill);
1396
1397 auto SBC = buildMI(MBB, MBBI, AVR::SBCRdRr)
1398 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1399 .addReg(DstHiReg, RegState::Kill)
1400 .addReg(DstHiReg, RegState::Kill);
1401
1402 if (ImpIsDead)
1403 SBC->getOperand(3).setIsDead();
1404
1405 // SREG is always implicitly killed
1406 SBC->getOperand(4).setIsKill();
1407
1408 MI.eraseFromParent();
1409 return true;
1410}
1411
1412template <> bool AVRExpandPseudo::expand<AVR::ZEXT>(Block &MBB, BlockIt MBBI) {
1413 MachineInstr &MI = *MBBI;
1414 unsigned DstLoReg, DstHiReg;
1415 // zext R25:R24, R20
1416 // mov R24, R20
1417 // eor R25, R25
1418 // zext R25:R24, R24
1419 // eor R25, R25
1420 // zext R25:R24, R25
1421 // mov R24, R25
1422 // eor R25, R25
1423 unsigned DstReg = MI.getOperand(0).getReg();
1424 unsigned SrcReg = MI.getOperand(1).getReg();
1425 bool DstIsDead = MI.getOperand(0).isDead();
1426 bool SrcIsKill = MI.getOperand(1).isKill();
1427 bool ImpIsDead = MI.getOperand(2).isDead();
1428 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1429
1430 if (SrcReg != DstLoReg) {
1431 buildMI(MBB, MBBI, AVR::MOVRdRr)
1432 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1433 .addReg(SrcReg, getKillRegState(SrcIsKill));
1434 }
1435
1436 auto EOR = buildMI(MBB, MBBI, AVR::EORRdRr)
1437 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1438 .addReg(DstHiReg, RegState::Kill)
1439 .addReg(DstHiReg, RegState::Kill);
1440
1441 if (ImpIsDead)
1442 EOR->getOperand(3).setIsDead();
1443
1444 MI.eraseFromParent();
1445 return true;
1446}
1447
1448template <>
1449bool AVRExpandPseudo::expand<AVR::SPREAD>(Block &MBB, BlockIt MBBI) {
1450 MachineInstr &MI = *MBBI;
1451 unsigned OpLo, OpHi, DstLoReg, DstHiReg;
1452 unsigned DstReg = MI.getOperand(0).getReg();
1453 bool DstIsDead = MI.getOperand(0).isDead();
1454 unsigned Flags = MI.getFlags();
1455 OpLo = AVR::INRdA;
1456 OpHi = AVR::INRdA;
1457 TRI->splitReg(DstReg, DstLoReg, DstHiReg);
1458
1459 // Low part
1460 buildMI(MBB, MBBI, OpLo)
1461 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead))
1462 .addImm(0x3d)
1463 .setMIFlags(Flags);
1464
1465 // High part
1466 buildMI(MBB, MBBI, OpHi)
1467 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead))
1468 .addImm(0x3e)
1469 .setMIFlags(Flags);
1470
1471 MI.eraseFromParent();
1472 return true;
1473}
1474
1475template <>
1476bool AVRExpandPseudo::expand<AVR::SPWRITE>(Block &MBB, BlockIt MBBI) {
1477 MachineInstr &MI = *MBBI;
1478 unsigned SrcLoReg, SrcHiReg;
1479 unsigned SrcReg = MI.getOperand(1).getReg();
1480 bool SrcIsKill = MI.getOperand(1).isKill();
1481 unsigned Flags = MI.getFlags();
1482 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
1483
1484 buildMI(MBB, MBBI, AVR::INRdA)
1485 .addReg(AVR::R0, RegState::Define)
1486 .addImm(SREG_ADDR)
1487 .setMIFlags(Flags);
1488
1489 buildMI(MBB, MBBI, AVR::BCLRs).addImm(0x07).setMIFlags(Flags);
1490
1491 buildMI(MBB, MBBI, AVR::OUTARr)
1492 .addImm(0x3e)
1493 .addReg(SrcHiReg, getKillRegState(SrcIsKill))
1494 .setMIFlags(Flags);
1495
1496 buildMI(MBB, MBBI, AVR::OUTARr)
1497 .addImm(SREG_ADDR)
1498 .addReg(AVR::R0, RegState::Kill)
1499 .setMIFlags(Flags);
1500
1501 buildMI(MBB, MBBI, AVR::OUTARr)
1502 .addImm(0x3d)
1503 .addReg(SrcLoReg, getKillRegState(SrcIsKill))
1504 .setMIFlags(Flags);
1505
1506 MI.eraseFromParent();
1507 return true;
1508}
1509
1510bool AVRExpandPseudo::expandMI(Block &MBB, BlockIt MBBI) {
1511 MachineInstr &MI = *MBBI;
1512 int Opcode = MBBI->getOpcode();
1513
1514#define EXPAND(Op) \
1515 case Op: \
1516 return expand<Op>(MBB, MI)
1517
1518 switch (Opcode) {
1519 EXPAND(AVR::ADDWRdRr);
1520 EXPAND(AVR::ADCWRdRr);
1521 EXPAND(AVR::SUBWRdRr);
1522 EXPAND(AVR::SUBIWRdK);
1523 EXPAND(AVR::SBCWRdRr);
1524 EXPAND(AVR::SBCIWRdK);
1525 EXPAND(AVR::ANDWRdRr);
1526 EXPAND(AVR::ANDIWRdK);
1527 EXPAND(AVR::ORWRdRr);
1528 EXPAND(AVR::ORIWRdK);
1529 EXPAND(AVR::EORWRdRr);
1530 EXPAND(AVR::COMWRd);
1531 EXPAND(AVR::CPWRdRr);
1532 EXPAND(AVR::CPCWRdRr);
1533 EXPAND(AVR::LDIWRdK);
1534 EXPAND(AVR::LDSWRdK);
1535 EXPAND(AVR::LDWRdPtr);
1536 EXPAND(AVR::LDWRdPtrPi);
1537 EXPAND(AVR::LDWRdPtrPd);
1538 case AVR::LDDWRdYQ: //:FIXME: remove this once PR13375 gets fixed
1539 EXPAND(AVR::LDDWRdPtrQ);
Dylan McKaya5d49df2016-12-09 07:49:04 +00001540 EXPAND(AVR::LPMWRdZ);
1541 EXPAND(AVR::LPMWRdZPi);
Dylan McKaya789f402016-11-16 21:58:04 +00001542 EXPAND(AVR::AtomicLoad8);
1543 EXPAND(AVR::AtomicLoad16);
1544 EXPAND(AVR::AtomicStore8);
1545 EXPAND(AVR::AtomicStore16);
1546 EXPAND(AVR::AtomicLoadAdd8);
1547 EXPAND(AVR::AtomicLoadAdd16);
1548 EXPAND(AVR::AtomicLoadSub8);
1549 EXPAND(AVR::AtomicLoadSub16);
1550 EXPAND(AVR::AtomicLoadAnd8);
1551 EXPAND(AVR::AtomicLoadAnd16);
1552 EXPAND(AVR::AtomicLoadOr8);
1553 EXPAND(AVR::AtomicLoadOr16);
1554 EXPAND(AVR::AtomicLoadXor8);
1555 EXPAND(AVR::AtomicLoadXor16);
1556 EXPAND(AVR::AtomicFence);
1557 EXPAND(AVR::STSWKRr);
1558 EXPAND(AVR::STWPtrRr);
1559 EXPAND(AVR::STWPtrPiRr);
1560 EXPAND(AVR::STWPtrPdRr);
1561 EXPAND(AVR::STDWPtrQRr);
1562 EXPAND(AVR::INWRdA);
1563 EXPAND(AVR::OUTWARr);
1564 EXPAND(AVR::PUSHWRr);
1565 EXPAND(AVR::POPWRd);
1566 EXPAND(AVR::LSLWRd);
1567 EXPAND(AVR::LSRWRd);
1568 EXPAND(AVR::RORWRd);
1569 EXPAND(AVR::ROLWRd);
1570 EXPAND(AVR::ASRWRd);
1571 EXPAND(AVR::SEXT);
1572 EXPAND(AVR::ZEXT);
1573 EXPAND(AVR::SPREAD);
1574 EXPAND(AVR::SPWRITE);
1575 }
1576#undef EXPAND
1577 return false;
1578}
1579
Dylan McKay017a55b2016-11-16 23:06:14 +00001580} // end of anonymous namespace
1581
Dylan McKay8cec7eb2016-12-07 11:08:56 +00001582INITIALIZE_PASS(AVRExpandPseudo, "avr-expand-pseudo",
1583 AVR_EXPAND_PSEUDO_NAME, false, false)
Dylan McKaya789f402016-11-16 21:58:04 +00001584namespace llvm {
1585
1586FunctionPass *createAVRExpandPseudoPass() { return new AVRExpandPseudo(); }
1587
1588} // end of namespace llvm