1 //===-- 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" 24 #include "llvm/CodeGen/RegisterScavenging.h" 25 #include "llvm/Target/TargetRegisterInfo.h" 26 27 using namespace llvm; 28 29 #define AVR_EXPAND_PSEUDO_NAME "AVR pseudo instruction expansion pass" 30 31 namespace { 32 33 /// Expands "placeholder" instructions marked as pseudo into 34 /// actual AVR instructions. 35 class AVRExpandPseudo : public MachineFunctionPass { 36 public: 37 static char ID; 38 39 AVRExpandPseudo() : MachineFunctionPass(ID) { 40 initializeAVRExpandPseudoPass(*PassRegistry::getPassRegistry()); 41 } 42 43 bool runOnMachineFunction(MachineFunction &MF) override; 44 45 StringRef getPassName() const override { return AVR_EXPAND_PSEUDO_NAME; } 46 47 private: 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); 77 bool isLogicImmOpRedundant(unsigned Op, unsigned ImmVal) const; 78 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); 91 92 /// Scavenges a free GPR8 register for use. 93 unsigned scavengeGPR8(MachineInstr &MI); 94 }; 95 96 char AVRExpandPseudo::ID = 0; 97 98 bool 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 111 bool 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 118 // We need to track liveness in order to use register scavenging. 119 MF.getProperties().set(MachineFunctionProperties::Property::TracksLiveness); 120 121 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 140 bool AVRExpandPseudo:: 141 expandArith(unsigned OpLo, unsigned OpHi, Block &MBB, BlockIt MBBI) { 142 MachineInstr &MI = *MBBI; 143 unsigned SrcLoReg, SrcHiReg, DstLoReg, DstHiReg; 144 unsigned DstReg = MI.getOperand(0).getReg(); 145 unsigned SrcReg = MI.getOperand(2).getReg(); 146 bool DstIsDead = MI.getOperand(0).isDead(); 147 bool DstIsKill = MI.getOperand(1).isKill(); 148 bool SrcIsKill = MI.getOperand(2).isKill(); 149 bool ImpIsDead = MI.getOperand(3).isDead(); 150 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 151 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 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 173 bool AVRExpandPseudo:: 174 expandLogic(unsigned Op, Block &MBB, BlockIt MBBI) { 175 MachineInstr &MI = *MBBI; 176 unsigned SrcLoReg, SrcHiReg, DstLoReg, DstHiReg; 177 unsigned DstReg = MI.getOperand(0).getReg(); 178 unsigned SrcReg = MI.getOperand(2).getReg(); 179 bool DstIsDead = MI.getOperand(0).isDead(); 180 bool DstIsKill = MI.getOperand(1).isKill(); 181 bool SrcIsKill = MI.getOperand(2).isKill(); 182 bool ImpIsDead = MI.getOperand(3).isDead(); 183 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 184 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 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 206 bool AVRExpandPseudo:: 207 isLogicImmOpRedundant(unsigned Op, unsigned ImmVal) const { 208 209 // ANDI Rd, 0xff is redundant. 210 if (Op == AVR::ANDIRdK && ImmVal == 0xff) 211 return true; 212 213 // ORI Rd, 0x0 is redundant. 214 if (Op == AVR::ORIRdK && ImmVal == 0x0) 215 return true; 216 217 return false; 218 } 219 220 bool AVRExpandPseudo:: 221 expandLogicImm(unsigned Op, Block &MBB, BlockIt MBBI) { 222 MachineInstr &MI = *MBBI; 223 unsigned DstLoReg, DstHiReg; 224 unsigned DstReg = MI.getOperand(0).getReg(); 225 bool DstIsDead = MI.getOperand(0).isDead(); 226 bool SrcIsKill = MI.getOperand(1).isKill(); 227 bool ImpIsDead = MI.getOperand(3).isDead(); 228 unsigned Imm = MI.getOperand(2).getImm(); 229 unsigned Lo8 = Imm & 0xff; 230 unsigned Hi8 = (Imm >> 8) & 0xff; 231 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 232 233 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); 238 239 // SREG is always implicitly dead 240 MIBLO->getOperand(3).setIsDead(); 241 } 242 243 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); 248 249 if (ImpIsDead) 250 MIBHI->getOperand(3).setIsDead(); 251 } 252 253 MI.eraseFromParent(); 254 return true; 255 } 256 257 template <> 258 bool AVRExpandPseudo::expand<AVR::ADDWRdRr>(Block &MBB, BlockIt MBBI) { 259 return expandArith(AVR::ADDRdRr, AVR::ADCRdRr, MBB, MBBI); 260 } 261 262 template <> 263 bool AVRExpandPseudo::expand<AVR::ADCWRdRr>(Block &MBB, BlockIt MBBI) { 264 return expandArith(AVR::ADCRdRr, AVR::ADCRdRr, MBB, MBBI); 265 } 266 267 template <> 268 bool AVRExpandPseudo::expand<AVR::SUBWRdRr>(Block &MBB, BlockIt MBBI) { 269 return expandArith(AVR::SUBRdRr, AVR::SBCRdRr, MBB, MBBI); 270 } 271 272 template <> 273 bool AVRExpandPseudo::expand<AVR::SUBIWRdK>(Block &MBB, BlockIt MBBI) { 274 MachineInstr &MI = *MBBI; 275 unsigned DstLoReg, DstHiReg; 276 unsigned DstReg = MI.getOperand(0).getReg(); 277 bool DstIsDead = MI.getOperand(0).isDead(); 278 bool SrcIsKill = MI.getOperand(1).isKill(); 279 bool ImpIsDead = MI.getOperand(3).isDead(); 280 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 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 319 template <> 320 bool AVRExpandPseudo::expand<AVR::SBCWRdRr>(Block &MBB, BlockIt MBBI) { 321 return expandArith(AVR::SBCRdRr, AVR::SBCRdRr, MBB, MBBI); 322 } 323 324 template <> 325 bool AVRExpandPseudo::expand<AVR::SBCIWRdK>(Block &MBB, BlockIt MBBI) { 326 MachineInstr &MI = *MBBI; 327 unsigned OpLo, OpHi, DstLoReg, DstHiReg; 328 unsigned DstReg = MI.getOperand(0).getReg(); 329 bool DstIsDead = MI.getOperand(0).isDead(); 330 bool SrcIsKill = MI.getOperand(1).isKill(); 331 bool ImpIsDead = MI.getOperand(3).isDead(); 332 unsigned Imm = MI.getOperand(2).getImm(); 333 unsigned Lo8 = Imm & 0xff; 334 unsigned Hi8 = (Imm >> 8) & 0xff; 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 362 template <> 363 bool AVRExpandPseudo::expand<AVR::ANDWRdRr>(Block &MBB, BlockIt MBBI) { 364 return expandLogic(AVR::ANDRdRr, MBB, MBBI); 365 } 366 367 template <> 368 bool AVRExpandPseudo::expand<AVR::ANDIWRdK>(Block &MBB, BlockIt MBBI) { 369 return expandLogicImm(AVR::ANDIRdK, MBB, MBBI); 370 } 371 372 template <> 373 bool AVRExpandPseudo::expand<AVR::ORWRdRr>(Block &MBB, BlockIt MBBI) { 374 return expandLogic(AVR::ORRdRr, MBB, MBBI); 375 } 376 377 template <> 378 bool AVRExpandPseudo::expand<AVR::ORIWRdK>(Block &MBB, BlockIt MBBI) { 379 return expandLogicImm(AVR::ORIRdK, MBB, MBBI); 380 } 381 382 template <> 383 bool AVRExpandPseudo::expand<AVR::EORWRdRr>(Block &MBB, BlockIt MBBI) { 384 return expandLogic(AVR::EORRdRr, MBB, MBBI); 385 } 386 387 template <> 388 bool AVRExpandPseudo::expand<AVR::COMWRd>(Block &MBB, BlockIt MBBI) { 389 MachineInstr &MI = *MBBI; 390 unsigned OpLo, OpHi, DstLoReg, DstHiReg; 391 unsigned DstReg = MI.getOperand(0).getReg(); 392 bool DstIsDead = MI.getOperand(0).isDead(); 393 bool DstIsKill = MI.getOperand(1).isKill(); 394 bool ImpIsDead = MI.getOperand(2).isDead(); 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 417 template <> 418 bool AVRExpandPseudo::expand<AVR::CPWRdRr>(Block &MBB, BlockIt MBBI) { 419 MachineInstr &MI = *MBBI; 420 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg, DstLoReg, DstHiReg; 421 unsigned DstReg = MI.getOperand(0).getReg(); 422 unsigned SrcReg = MI.getOperand(1).getReg(); 423 bool DstIsKill = MI.getOperand(0).isKill(); 424 bool SrcIsKill = MI.getOperand(1).isKill(); 425 bool ImpIsDead = MI.getOperand(2).isDead(); 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 450 template <> 451 bool AVRExpandPseudo::expand<AVR::CPCWRdRr>(Block &MBB, BlockIt MBBI) { 452 MachineInstr &MI = *MBBI; 453 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg, DstLoReg, DstHiReg; 454 unsigned DstReg = MI.getOperand(0).getReg(); 455 unsigned SrcReg = MI.getOperand(1).getReg(); 456 bool DstIsKill = MI.getOperand(0).isKill(); 457 bool SrcIsKill = MI.getOperand(1).isKill(); 458 bool ImpIsDead = MI.getOperand(2).isDead(); 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 485 template <> 486 bool AVRExpandPseudo::expand<AVR::LDIWRdK>(Block &MBB, BlockIt MBBI) { 487 MachineInstr &MI = *MBBI; 488 unsigned OpLo, OpHi, DstLoReg, DstHiReg; 489 unsigned DstReg = MI.getOperand(0).getReg(); 490 bool DstIsDead = MI.getOperand(0).isDead(); 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 515 MIBLO.add(MachineOperand::CreateBA(BA, TF | AVRII::MO_LO)); 516 MIBHI.add(MachineOperand::CreateBA(BA, TF | AVRII::MO_HI)); 517 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 534 template <> 535 bool AVRExpandPseudo::expand<AVR::LDSWRdK>(Block &MBB, BlockIt MBBI) { 536 MachineInstr &MI = *MBBI; 537 unsigned OpLo, OpHi, DstLoReg, DstHiReg; 538 unsigned DstReg = MI.getOperand(0).getReg(); 539 bool DstIsDead = MI.getOperand(0).isDead(); 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 571 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 572 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 573 574 MI.eraseFromParent(); 575 return true; 576 } 577 578 template <> 579 bool AVRExpandPseudo::expand<AVR::LDWRdPtr>(Block &MBB, BlockIt MBBI) { 580 MachineInstr &MI = *MBBI; 581 unsigned OpLo, OpHi, DstLoReg, DstHiReg; 582 unsigned DstReg = MI.getOperand(0).getReg(); 583 unsigned TmpReg = 0; // 0 for no temporary register 584 unsigned SrcReg = MI.getOperand(1).getReg(); 585 bool SrcIsKill = MI.getOperand(1).isKill(); 586 OpLo = AVR::LDRdPtr; 587 OpHi = AVR::LDDRdPtrQ; 588 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 589 590 // Use a temporary register if src and dst registers are the same. 591 if (DstReg == SrcReg) 592 TmpReg = scavengeGPR8(MI); 593 594 unsigned CurDstLoReg = (DstReg == SrcReg) ? TmpReg : DstLoReg; 595 unsigned CurDstHiReg = (DstReg == SrcReg) ? TmpReg : DstHiReg; 596 597 // Load low byte. 598 auto MIBLO = buildMI(MBB, MBBI, OpLo) 599 .addReg(CurDstLoReg, RegState::Define) 600 .addReg(SrcReg); 601 602 // Push low byte onto stack if necessary. 603 if (TmpReg) 604 buildMI(MBB, MBBI, AVR::PUSHRr).addReg(TmpReg); 605 606 // Load high byte. 607 auto MIBHI = buildMI(MBB, MBBI, OpHi) 608 .addReg(CurDstHiReg, RegState::Define) 609 .addReg(SrcReg, getKillRegState(SrcIsKill)) 610 .addImm(1); 611 612 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 620 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 621 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 622 623 MI.eraseFromParent(); 624 return true; 625 } 626 627 template <> 628 bool AVRExpandPseudo::expand<AVR::LDWRdPtrPi>(Block &MBB, BlockIt MBBI) { 629 MachineInstr &MI = *MBBI; 630 unsigned OpLo, OpHi, DstLoReg, DstHiReg; 631 unsigned DstReg = MI.getOperand(0).getReg(); 632 unsigned SrcReg = MI.getOperand(1).getReg(); 633 bool DstIsDead = MI.getOperand(0).isDead(); 634 bool SrcIsDead = MI.getOperand(1).isKill(); 635 OpLo = AVR::LDRdPtrPi; 636 OpHi = AVR::LDRdPtrPi; 637 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 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 651 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 652 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 653 654 MI.eraseFromParent(); 655 return true; 656 } 657 658 template <> 659 bool AVRExpandPseudo::expand<AVR::LDWRdPtrPd>(Block &MBB, BlockIt MBBI) { 660 MachineInstr &MI = *MBBI; 661 unsigned OpLo, OpHi, DstLoReg, DstHiReg; 662 unsigned DstReg = MI.getOperand(0).getReg(); 663 unsigned SrcReg = MI.getOperand(1).getReg(); 664 bool DstIsDead = MI.getOperand(0).isDead(); 665 bool SrcIsDead = MI.getOperand(1).isKill(); 666 OpLo = AVR::LDRdPtrPd; 667 OpHi = AVR::LDRdPtrPd; 668 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 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 682 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 683 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 684 685 MI.eraseFromParent(); 686 return true; 687 } 688 689 template <> 690 bool AVRExpandPseudo::expand<AVR::LDDWRdPtrQ>(Block &MBB, BlockIt MBBI) { 691 MachineInstr &MI = *MBBI; 692 unsigned OpLo, OpHi, DstLoReg, DstHiReg; 693 unsigned DstReg = MI.getOperand(0).getReg(); 694 unsigned TmpReg = 0; // 0 for no temporary register 695 unsigned SrcReg = MI.getOperand(1).getReg(); 696 unsigned Imm = MI.getOperand(2).getImm(); 697 bool SrcIsKill = MI.getOperand(1).isKill(); 698 OpLo = AVR::LDDRdPtrQ; 699 OpHi = AVR::LDDRdPtrQ; 700 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 701 702 assert(Imm <= 63 && "Offset is out of range"); 703 704 // Use a temporary register if src and dst registers are the same. 705 if (DstReg == SrcReg) 706 TmpReg = scavengeGPR8(MI); 707 708 unsigned CurDstLoReg = (DstReg == SrcReg) ? TmpReg : DstLoReg; 709 unsigned CurDstHiReg = (DstReg == SrcReg) ? TmpReg : DstHiReg; 710 711 // Load low byte. 712 auto MIBLO = buildMI(MBB, MBBI, OpLo) 713 .addReg(CurDstLoReg, RegState::Define) 714 .addReg(SrcReg) 715 .addImm(Imm); 716 717 // Push low byte onto stack if necessary. 718 if (TmpReg) 719 buildMI(MBB, MBBI, AVR::PUSHRr).addReg(TmpReg); 720 721 // Load high byte. 722 auto MIBHI = buildMI(MBB, MBBI, OpHi) 723 .addReg(CurDstHiReg, RegState::Define) 724 .addReg(SrcReg, getKillRegState(SrcIsKill)) 725 .addImm(Imm + 1); 726 727 if (TmpReg) { 728 // Move the high byte into the final destination. 729 buildMI(MBB, MBBI, AVR::MOVRdRr).addReg(DstHiReg).addReg(TmpReg); 730 731 // Move the low byte from the scratch space into the final destination. 732 buildMI(MBB, MBBI, AVR::POPRd).addReg(DstLoReg); 733 } 734 735 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 736 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 737 738 MI.eraseFromParent(); 739 return true; 740 } 741 742 template <> 743 bool AVRExpandPseudo::expand<AVR::LPMWRdZ>(Block &MBB, BlockIt MBBI) { 744 llvm_unreachable("wide LPM is unimplemented"); 745 } 746 747 template <> 748 bool AVRExpandPseudo::expand<AVR::LPMWRdZPi>(Block &MBB, BlockIt MBBI) { 749 llvm_unreachable("wide LPMPi is unimplemented"); 750 } 751 752 template<typename Func> 753 bool AVRExpandPseudo::expandAtomic(Block &MBB, BlockIt MBBI, Func f) { 754 // Remove the pseudo instruction. 755 MachineInstr &MI = *MBBI; 756 757 // Store the SREG. 758 buildMI(MBB, MBBI, AVR::INRdA) 759 .addReg(SCRATCH_REGISTER, RegState::Define) 760 .addImm(SREG_ADDR); 761 762 // Disable exceptions. 763 buildMI(MBB, MBBI, AVR::BCLRs).addImm(7); // CLI 764 765 f(MI); 766 767 // Restore the status reg. 768 buildMI(MBB, MBBI, AVR::OUTARr) 769 .addImm(SREG_ADDR) 770 .addReg(SCRATCH_REGISTER); 771 772 MI.eraseFromParent(); 773 return true; 774 } 775 776 template<typename Func> 777 bool AVRExpandPseudo::expandAtomicBinaryOp(unsigned Opcode, 778 Block &MBB, 779 BlockIt MBBI, 780 Func f) { 781 return expandAtomic(MBB, MBBI, [&](MachineInstr &MI) { 782 auto Op1 = MI.getOperand(0); 783 auto Op2 = MI.getOperand(1); 784 785 MachineInstr &NewInst = 786 *buildMI(MBB, MBBI, Opcode).add(Op1).add(Op2).getInstr(); 787 f(NewInst); 788 }); 789 } 790 791 bool AVRExpandPseudo::expandAtomicBinaryOp(unsigned Opcode, 792 Block &MBB, 793 BlockIt MBBI) { 794 return expandAtomicBinaryOp(Opcode, MBB, MBBI, [](MachineInstr &MI) {}); 795 } 796 797 bool AVRExpandPseudo::expandAtomicArithmeticOp(unsigned Width, 798 unsigned ArithOpcode, 799 Block &MBB, 800 BlockIt MBBI) { 801 return expandAtomic(MBB, MBBI, [&](MachineInstr &MI) { 802 auto Op1 = MI.getOperand(0); 803 auto Op2 = MI.getOperand(1); 804 805 unsigned LoadOpcode = (Width == 8) ? AVR::LDRdPtr : AVR::LDWRdPtr; 806 unsigned StoreOpcode = (Width == 8) ? AVR::STPtrRr : AVR::STWPtrRr; 807 808 // Create the load 809 buildMI(MBB, MBBI, LoadOpcode).add(Op1).add(Op2); 810 811 // Create the arithmetic op 812 buildMI(MBB, MBBI, ArithOpcode).add(Op1).add(Op1).add(Op2); 813 814 // Create the store 815 buildMI(MBB, MBBI, StoreOpcode).add(Op2).add(Op1); 816 }); 817 } 818 819 unsigned AVRExpandPseudo::scavengeGPR8(MachineInstr &MI) { 820 MachineBasicBlock &MBB = *MI.getParent(); 821 RegScavenger RS; 822 823 RS.enterBasicBlock(MBB); 824 RS.forward(MI); 825 826 BitVector Candidates = 827 TRI->getAllocatableSet 828 (*MBB.getParent(), &AVR::GPR8RegClass); 829 830 // Exclude all the registers being used by the instruction. 831 for (MachineOperand &MO : MI.operands()) { 832 if (MO.isReg() && MO.getReg() != 0 && !MO.isDef() && 833 !TargetRegisterInfo::isVirtualRegister(MO.getReg())) 834 Candidates.reset(MO.getReg()); 835 } 836 837 BitVector Available = RS.getRegsAvailable(&AVR::GPR8RegClass); 838 Available &= Candidates; 839 840 signed Reg = Available.find_first(); 841 assert(Reg != -1 && "ran out of registers"); 842 return Reg; 843 } 844 845 template<> 846 bool AVRExpandPseudo::expand<AVR::AtomicLoad8>(Block &MBB, BlockIt MBBI) { 847 return expandAtomicBinaryOp(AVR::LDRdPtr, MBB, MBBI); 848 } 849 850 template<> 851 bool AVRExpandPseudo::expand<AVR::AtomicLoad16>(Block &MBB, BlockIt MBBI) { 852 return expandAtomicBinaryOp(AVR::LDWRdPtr, MBB, MBBI); 853 } 854 855 template<> 856 bool AVRExpandPseudo::expand<AVR::AtomicStore8>(Block &MBB, BlockIt MBBI) { 857 return expandAtomicBinaryOp(AVR::STPtrRr, MBB, MBBI); 858 } 859 860 template<> 861 bool AVRExpandPseudo::expand<AVR::AtomicStore16>(Block &MBB, BlockIt MBBI) { 862 return expandAtomicBinaryOp(AVR::STWPtrRr, MBB, MBBI); 863 } 864 865 template<> 866 bool AVRExpandPseudo::expand<AVR::AtomicLoadAdd8>(Block &MBB, BlockIt MBBI) { 867 return expandAtomicArithmeticOp(8, AVR::ADDRdRr, MBB, MBBI); 868 } 869 870 template<> 871 bool AVRExpandPseudo::expand<AVR::AtomicLoadAdd16>(Block &MBB, BlockIt MBBI) { 872 return expandAtomicArithmeticOp(16, AVR::ADDWRdRr, MBB, MBBI); 873 } 874 875 template<> 876 bool AVRExpandPseudo::expand<AVR::AtomicLoadSub8>(Block &MBB, BlockIt MBBI) { 877 return expandAtomicArithmeticOp(8, AVR::SUBRdRr, MBB, MBBI); 878 } 879 880 template<> 881 bool AVRExpandPseudo::expand<AVR::AtomicLoadSub16>(Block &MBB, BlockIt MBBI) { 882 return expandAtomicArithmeticOp(16, AVR::SUBWRdRr, MBB, MBBI); 883 } 884 885 template<> 886 bool AVRExpandPseudo::expand<AVR::AtomicLoadAnd8>(Block &MBB, BlockIt MBBI) { 887 return expandAtomicArithmeticOp(8, AVR::ANDRdRr, MBB, MBBI); 888 } 889 890 template<> 891 bool AVRExpandPseudo::expand<AVR::AtomicLoadAnd16>(Block &MBB, BlockIt MBBI) { 892 return expandAtomicArithmeticOp(16, AVR::ANDWRdRr, MBB, MBBI); 893 } 894 895 template<> 896 bool AVRExpandPseudo::expand<AVR::AtomicLoadOr8>(Block &MBB, BlockIt MBBI) { 897 return expandAtomicArithmeticOp(8, AVR::ORRdRr, MBB, MBBI); 898 } 899 900 template<> 901 bool AVRExpandPseudo::expand<AVR::AtomicLoadOr16>(Block &MBB, BlockIt MBBI) { 902 return expandAtomicArithmeticOp(16, AVR::ORWRdRr, MBB, MBBI); 903 } 904 905 template<> 906 bool AVRExpandPseudo::expand<AVR::AtomicLoadXor8>(Block &MBB, BlockIt MBBI) { 907 return expandAtomicArithmeticOp(8, AVR::EORRdRr, MBB, MBBI); 908 } 909 910 template<> 911 bool AVRExpandPseudo::expand<AVR::AtomicLoadXor16>(Block &MBB, BlockIt MBBI) { 912 return expandAtomicArithmeticOp(16, AVR::EORWRdRr, MBB, MBBI); 913 } 914 915 template<> 916 bool AVRExpandPseudo::expand<AVR::AtomicFence>(Block &MBB, BlockIt MBBI) { 917 // On AVR, there is only one core and so atomic fences do nothing. 918 MBBI->eraseFromParent(); 919 return true; 920 } 921 922 template <> 923 bool AVRExpandPseudo::expand<AVR::STSWKRr>(Block &MBB, BlockIt MBBI) { 924 MachineInstr &MI = *MBBI; 925 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg; 926 unsigned SrcReg = MI.getOperand(1).getReg(); 927 bool SrcIsKill = MI.getOperand(1).isKill(); 928 OpLo = AVR::STSKRr; 929 OpHi = AVR::STSKRr; 930 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 931 932 // Write the high byte first in case this address belongs to a special 933 // I/O address with a special temporary register. 934 auto MIBHI = buildMI(MBB, MBBI, OpHi); 935 auto MIBLO = buildMI(MBB, MBBI, OpLo); 936 937 switch (MI.getOperand(0).getType()) { 938 case MachineOperand::MO_GlobalAddress: { 939 const GlobalValue *GV = MI.getOperand(0).getGlobal(); 940 int64_t Offs = MI.getOperand(0).getOffset(); 941 unsigned TF = MI.getOperand(0).getTargetFlags(); 942 943 MIBLO.addGlobalAddress(GV, Offs, TF); 944 MIBHI.addGlobalAddress(GV, Offs + 1, TF); 945 break; 946 } 947 case MachineOperand::MO_Immediate: { 948 unsigned Imm = MI.getOperand(0).getImm(); 949 950 MIBLO.addImm(Imm); 951 MIBHI.addImm(Imm + 1); 952 break; 953 } 954 default: 955 llvm_unreachable("Unknown operand type!"); 956 } 957 958 MIBLO.addReg(SrcLoReg, getKillRegState(SrcIsKill)); 959 MIBHI.addReg(SrcHiReg, getKillRegState(SrcIsKill)); 960 961 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 962 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 963 964 MI.eraseFromParent(); 965 return true; 966 } 967 968 template <> 969 bool AVRExpandPseudo::expand<AVR::STWPtrRr>(Block &MBB, BlockIt MBBI) { 970 MachineInstr &MI = *MBBI; 971 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg; 972 unsigned DstReg = MI.getOperand(0).getReg(); 973 unsigned SrcReg = MI.getOperand(1).getReg(); 974 bool SrcIsKill = MI.getOperand(1).isKill(); 975 OpLo = AVR::STPtrRr; 976 OpHi = AVR::STDPtrQRr; 977 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 978 979 //:TODO: need to reverse this order like inw and stsw? 980 auto MIBLO = buildMI(MBB, MBBI, OpLo) 981 .addReg(DstReg) 982 .addReg(SrcLoReg, getKillRegState(SrcIsKill)); 983 984 auto MIBHI = buildMI(MBB, MBBI, OpHi) 985 .addReg(DstReg) 986 .addImm(1) 987 .addReg(SrcHiReg, getKillRegState(SrcIsKill)); 988 989 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 990 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 991 992 MI.eraseFromParent(); 993 return true; 994 } 995 996 template <> 997 bool AVRExpandPseudo::expand<AVR::STWPtrPiRr>(Block &MBB, BlockIt MBBI) { 998 MachineInstr &MI = *MBBI; 999 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg; 1000 unsigned DstReg = MI.getOperand(0).getReg(); 1001 unsigned SrcReg = MI.getOperand(2).getReg(); 1002 unsigned Imm = MI.getOperand(3).getImm(); 1003 bool DstIsDead = MI.getOperand(0).isDead(); 1004 bool SrcIsKill = MI.getOperand(2).isKill(); 1005 OpLo = AVR::STPtrPiRr; 1006 OpHi = AVR::STPtrPiRr; 1007 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1008 1009 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same"); 1010 1011 auto MIBLO = buildMI(MBB, MBBI, OpLo) 1012 .addReg(DstReg, RegState::Define) 1013 .addReg(DstReg, RegState::Kill) 1014 .addReg(SrcLoReg, getKillRegState(SrcIsKill)) 1015 .addImm(Imm); 1016 1017 auto MIBHI = buildMI(MBB, MBBI, OpHi) 1018 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 1019 .addReg(DstReg, RegState::Kill) 1020 .addReg(SrcHiReg, getKillRegState(SrcIsKill)) 1021 .addImm(Imm); 1022 1023 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 1024 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 1025 1026 MI.eraseFromParent(); 1027 return true; 1028 } 1029 1030 template <> 1031 bool AVRExpandPseudo::expand<AVR::STWPtrPdRr>(Block &MBB, BlockIt MBBI) { 1032 MachineInstr &MI = *MBBI; 1033 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg; 1034 unsigned DstReg = MI.getOperand(0).getReg(); 1035 unsigned SrcReg = MI.getOperand(2).getReg(); 1036 unsigned Imm = MI.getOperand(3).getImm(); 1037 bool DstIsDead = MI.getOperand(0).isDead(); 1038 bool SrcIsKill = MI.getOperand(2).isKill(); 1039 OpLo = AVR::STPtrPdRr; 1040 OpHi = AVR::STPtrPdRr; 1041 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1042 1043 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same"); 1044 1045 auto MIBHI = buildMI(MBB, MBBI, OpHi) 1046 .addReg(DstReg, RegState::Define) 1047 .addReg(DstReg, RegState::Kill) 1048 .addReg(SrcHiReg, getKillRegState(SrcIsKill)) 1049 .addImm(Imm); 1050 1051 auto MIBLO = buildMI(MBB, MBBI, OpLo) 1052 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 1053 .addReg(DstReg, RegState::Kill) 1054 .addReg(SrcLoReg, getKillRegState(SrcIsKill)) 1055 .addImm(Imm); 1056 1057 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 1058 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 1059 1060 MI.eraseFromParent(); 1061 return true; 1062 } 1063 1064 template <> 1065 bool AVRExpandPseudo::expand<AVR::STDWPtrQRr>(Block &MBB, BlockIt MBBI) { 1066 MachineInstr &MI = *MBBI; 1067 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg; 1068 unsigned DstReg = MI.getOperand(0).getReg(); 1069 unsigned SrcReg = MI.getOperand(2).getReg(); 1070 unsigned Imm = MI.getOperand(1).getImm(); 1071 bool DstIsKill = MI.getOperand(0).isKill(); 1072 bool SrcIsKill = MI.getOperand(2).isKill(); 1073 OpLo = AVR::STDPtrQRr; 1074 OpHi = AVR::STDPtrQRr; 1075 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1076 1077 assert(Imm <= 63 && "Offset is out of range"); 1078 1079 auto MIBLO = buildMI(MBB, MBBI, OpLo) 1080 .addReg(DstReg) 1081 .addImm(Imm) 1082 .addReg(SrcLoReg, getKillRegState(SrcIsKill)); 1083 1084 auto MIBHI = buildMI(MBB, MBBI, OpHi) 1085 .addReg(DstReg, getKillRegState(DstIsKill)) 1086 .addImm(Imm + 1) 1087 .addReg(SrcHiReg, getKillRegState(SrcIsKill)); 1088 1089 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 1090 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 1091 1092 MI.eraseFromParent(); 1093 return true; 1094 } 1095 1096 template <> 1097 bool AVRExpandPseudo::expand<AVR::INWRdA>(Block &MBB, BlockIt MBBI) { 1098 MachineInstr &MI = *MBBI; 1099 unsigned OpLo, OpHi, DstLoReg, DstHiReg; 1100 unsigned Imm = MI.getOperand(1).getImm(); 1101 unsigned DstReg = MI.getOperand(0).getReg(); 1102 bool DstIsDead = MI.getOperand(0).isDead(); 1103 OpLo = AVR::INRdA; 1104 OpHi = AVR::INRdA; 1105 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1106 1107 assert(Imm <= 63 && "Address is out of range"); 1108 1109 auto MIBLO = buildMI(MBB, MBBI, OpLo) 1110 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1111 .addImm(Imm); 1112 1113 auto MIBHI = buildMI(MBB, MBBI, OpHi) 1114 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1115 .addImm(Imm + 1); 1116 1117 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 1118 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 1119 1120 MI.eraseFromParent(); 1121 return true; 1122 } 1123 1124 template <> 1125 bool AVRExpandPseudo::expand<AVR::OUTWARr>(Block &MBB, BlockIt MBBI) { 1126 MachineInstr &MI = *MBBI; 1127 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg; 1128 unsigned Imm = MI.getOperand(0).getImm(); 1129 unsigned SrcReg = MI.getOperand(1).getReg(); 1130 bool SrcIsKill = MI.getOperand(1).isKill(); 1131 OpLo = AVR::OUTARr; 1132 OpHi = AVR::OUTARr; 1133 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1134 1135 assert(Imm <= 63 && "Address is out of range"); 1136 1137 // 16 bit I/O writes need the high byte first 1138 auto MIBHI = buildMI(MBB, MBBI, OpHi) 1139 .addImm(Imm + 1) 1140 .addReg(SrcHiReg, getKillRegState(SrcIsKill)); 1141 1142 auto MIBLO = buildMI(MBB, MBBI, OpLo) 1143 .addImm(Imm) 1144 .addReg(SrcLoReg, getKillRegState(SrcIsKill)); 1145 1146 MIBLO->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 1147 MIBHI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 1148 1149 MI.eraseFromParent(); 1150 return true; 1151 } 1152 1153 template <> 1154 bool AVRExpandPseudo::expand<AVR::PUSHWRr>(Block &MBB, BlockIt MBBI) { 1155 MachineInstr &MI = *MBBI; 1156 unsigned OpLo, OpHi, SrcLoReg, SrcHiReg; 1157 unsigned SrcReg = MI.getOperand(0).getReg(); 1158 bool SrcIsKill = MI.getOperand(0).isKill(); 1159 unsigned Flags = MI.getFlags(); 1160 OpLo = AVR::PUSHRr; 1161 OpHi = AVR::PUSHRr; 1162 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1163 1164 // Low part 1165 buildMI(MBB, MBBI, OpLo) 1166 .addReg(SrcLoReg, getKillRegState(SrcIsKill)) 1167 .setMIFlags(Flags); 1168 1169 // High part 1170 buildMI(MBB, MBBI, OpHi) 1171 .addReg(SrcHiReg, getKillRegState(SrcIsKill)) 1172 .setMIFlags(Flags); 1173 1174 MI.eraseFromParent(); 1175 return true; 1176 } 1177 1178 template <> 1179 bool AVRExpandPseudo::expand<AVR::POPWRd>(Block &MBB, BlockIt MBBI) { 1180 MachineInstr &MI = *MBBI; 1181 unsigned OpLo, OpHi, DstLoReg, DstHiReg; 1182 unsigned DstReg = MI.getOperand(0).getReg(); 1183 unsigned Flags = MI.getFlags(); 1184 OpLo = AVR::POPRd; 1185 OpHi = AVR::POPRd; 1186 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1187 1188 buildMI(MBB, MBBI, OpHi, DstHiReg).setMIFlags(Flags); // High 1189 buildMI(MBB, MBBI, OpLo, DstLoReg).setMIFlags(Flags); // Low 1190 1191 MI.eraseFromParent(); 1192 return true; 1193 } 1194 1195 template <> 1196 bool AVRExpandPseudo::expand<AVR::LSLWRd>(Block &MBB, BlockIt MBBI) { 1197 MachineInstr &MI = *MBBI; 1198 unsigned OpLo, OpHi, DstLoReg, DstHiReg; 1199 unsigned DstReg = MI.getOperand(0).getReg(); 1200 bool DstIsDead = MI.getOperand(0).isDead(); 1201 bool DstIsKill = MI.getOperand(1).isKill(); 1202 bool ImpIsDead = MI.getOperand(2).isDead(); 1203 OpLo = AVR::LSLRd; 1204 OpHi = AVR::ROLRd; 1205 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1206 1207 // Low part 1208 buildMI(MBB, MBBI, OpLo) 1209 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1210 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1211 1212 auto MIBHI = buildMI(MBB, MBBI, OpHi) 1213 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1214 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1215 1216 if (ImpIsDead) 1217 MIBHI->getOperand(2).setIsDead(); 1218 1219 // SREG is always implicitly killed 1220 MIBHI->getOperand(3).setIsKill(); 1221 1222 MI.eraseFromParent(); 1223 return true; 1224 } 1225 1226 template <> 1227 bool AVRExpandPseudo::expand<AVR::LSRWRd>(Block &MBB, BlockIt MBBI) { 1228 MachineInstr &MI = *MBBI; 1229 unsigned OpLo, OpHi, DstLoReg, DstHiReg; 1230 unsigned DstReg = MI.getOperand(0).getReg(); 1231 bool DstIsDead = MI.getOperand(0).isDead(); 1232 bool DstIsKill = MI.getOperand(1).isKill(); 1233 bool ImpIsDead = MI.getOperand(2).isDead(); 1234 OpLo = AVR::RORRd; 1235 OpHi = AVR::LSRRd; 1236 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1237 1238 // High part 1239 buildMI(MBB, MBBI, OpHi) 1240 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1241 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1242 1243 auto MIBLO = buildMI(MBB, MBBI, OpLo) 1244 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1245 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1246 1247 if (ImpIsDead) 1248 MIBLO->getOperand(2).setIsDead(); 1249 1250 // SREG is always implicitly killed 1251 MIBLO->getOperand(3).setIsKill(); 1252 1253 MI.eraseFromParent(); 1254 return true; 1255 } 1256 1257 template <> 1258 bool AVRExpandPseudo::expand<AVR::RORWRd>(Block &MBB, BlockIt MBBI) { 1259 llvm_unreachable("RORW unimplemented"); 1260 return false; 1261 } 1262 1263 template <> 1264 bool AVRExpandPseudo::expand<AVR::ROLWRd>(Block &MBB, BlockIt MBBI) { 1265 llvm_unreachable("ROLW unimplemented"); 1266 return false; 1267 } 1268 1269 template <> 1270 bool AVRExpandPseudo::expand<AVR::ASRWRd>(Block &MBB, BlockIt MBBI) { 1271 MachineInstr &MI = *MBBI; 1272 unsigned OpLo, OpHi, DstLoReg, DstHiReg; 1273 unsigned DstReg = MI.getOperand(0).getReg(); 1274 bool DstIsDead = MI.getOperand(0).isDead(); 1275 bool DstIsKill = MI.getOperand(1).isKill(); 1276 bool ImpIsDead = MI.getOperand(2).isDead(); 1277 OpLo = AVR::RORRd; 1278 OpHi = AVR::ASRRd; 1279 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1280 1281 // High part 1282 buildMI(MBB, MBBI, OpHi) 1283 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1284 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1285 1286 auto MIBLO = buildMI(MBB, MBBI, OpLo) 1287 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1288 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1289 1290 if (ImpIsDead) 1291 MIBLO->getOperand(2).setIsDead(); 1292 1293 // SREG is always implicitly killed 1294 MIBLO->getOperand(3).setIsKill(); 1295 1296 MI.eraseFromParent(); 1297 return true; 1298 } 1299 1300 template <> bool AVRExpandPseudo::expand<AVR::SEXT>(Block &MBB, BlockIt MBBI) { 1301 MachineInstr &MI = *MBBI; 1302 unsigned DstLoReg, DstHiReg; 1303 // sext R17:R16, R17 1304 // mov r16, r17 1305 // lsl r17 1306 // sbc r17, r17 1307 // sext R17:R16, R13 1308 // mov r16, r13 1309 // mov r17, r13 1310 // lsl r17 1311 // sbc r17, r17 1312 // sext R17:R16, R16 1313 // mov r17, r16 1314 // lsl r17 1315 // sbc r17, r17 1316 unsigned DstReg = MI.getOperand(0).getReg(); 1317 unsigned SrcReg = MI.getOperand(1).getReg(); 1318 bool DstIsDead = MI.getOperand(0).isDead(); 1319 bool SrcIsKill = MI.getOperand(1).isKill(); 1320 bool ImpIsDead = MI.getOperand(2).isDead(); 1321 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1322 1323 if (SrcReg != DstLoReg) { 1324 auto MOV = buildMI(MBB, MBBI, AVR::MOVRdRr) 1325 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1326 .addReg(SrcReg); 1327 1328 if (SrcReg == DstHiReg) { 1329 MOV->getOperand(1).setIsKill(); 1330 } 1331 } 1332 1333 if (SrcReg != DstHiReg) { 1334 buildMI(MBB, MBBI, AVR::MOVRdRr) 1335 .addReg(DstHiReg, RegState::Define) 1336 .addReg(SrcReg, getKillRegState(SrcIsKill)); 1337 } 1338 1339 buildMI(MBB, MBBI, AVR::LSLRd) 1340 .addReg(DstHiReg, RegState::Define) 1341 .addReg(DstHiReg, RegState::Kill); 1342 1343 auto SBC = buildMI(MBB, MBBI, AVR::SBCRdRr) 1344 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1345 .addReg(DstHiReg, RegState::Kill) 1346 .addReg(DstHiReg, RegState::Kill); 1347 1348 if (ImpIsDead) 1349 SBC->getOperand(3).setIsDead(); 1350 1351 // SREG is always implicitly killed 1352 SBC->getOperand(4).setIsKill(); 1353 1354 MI.eraseFromParent(); 1355 return true; 1356 } 1357 1358 template <> bool AVRExpandPseudo::expand<AVR::ZEXT>(Block &MBB, BlockIt MBBI) { 1359 MachineInstr &MI = *MBBI; 1360 unsigned DstLoReg, DstHiReg; 1361 // zext R25:R24, R20 1362 // mov R24, R20 1363 // eor R25, R25 1364 // zext R25:R24, R24 1365 // eor R25, R25 1366 // zext R25:R24, R25 1367 // mov R24, R25 1368 // eor R25, R25 1369 unsigned DstReg = MI.getOperand(0).getReg(); 1370 unsigned SrcReg = MI.getOperand(1).getReg(); 1371 bool DstIsDead = MI.getOperand(0).isDead(); 1372 bool SrcIsKill = MI.getOperand(1).isKill(); 1373 bool ImpIsDead = MI.getOperand(2).isDead(); 1374 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1375 1376 if (SrcReg != DstLoReg) { 1377 buildMI(MBB, MBBI, AVR::MOVRdRr) 1378 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1379 .addReg(SrcReg, getKillRegState(SrcIsKill)); 1380 } 1381 1382 auto EOR = buildMI(MBB, MBBI, AVR::EORRdRr) 1383 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1384 .addReg(DstHiReg, RegState::Kill) 1385 .addReg(DstHiReg, RegState::Kill); 1386 1387 if (ImpIsDead) 1388 EOR->getOperand(3).setIsDead(); 1389 1390 MI.eraseFromParent(); 1391 return true; 1392 } 1393 1394 template <> 1395 bool AVRExpandPseudo::expand<AVR::SPREAD>(Block &MBB, BlockIt MBBI) { 1396 MachineInstr &MI = *MBBI; 1397 unsigned OpLo, OpHi, DstLoReg, DstHiReg; 1398 unsigned DstReg = MI.getOperand(0).getReg(); 1399 bool DstIsDead = MI.getOperand(0).isDead(); 1400 unsigned Flags = MI.getFlags(); 1401 OpLo = AVR::INRdA; 1402 OpHi = AVR::INRdA; 1403 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1404 1405 // Low part 1406 buildMI(MBB, MBBI, OpLo) 1407 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1408 .addImm(0x3d) 1409 .setMIFlags(Flags); 1410 1411 // High part 1412 buildMI(MBB, MBBI, OpHi) 1413 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1414 .addImm(0x3e) 1415 .setMIFlags(Flags); 1416 1417 MI.eraseFromParent(); 1418 return true; 1419 } 1420 1421 template <> 1422 bool AVRExpandPseudo::expand<AVR::SPWRITE>(Block &MBB, BlockIt MBBI) { 1423 MachineInstr &MI = *MBBI; 1424 unsigned SrcLoReg, SrcHiReg; 1425 unsigned SrcReg = MI.getOperand(1).getReg(); 1426 bool SrcIsKill = MI.getOperand(1).isKill(); 1427 unsigned Flags = MI.getFlags(); 1428 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1429 1430 buildMI(MBB, MBBI, AVR::INRdA) 1431 .addReg(AVR::R0, RegState::Define) 1432 .addImm(SREG_ADDR) 1433 .setMIFlags(Flags); 1434 1435 buildMI(MBB, MBBI, AVR::BCLRs).addImm(0x07).setMIFlags(Flags); 1436 1437 buildMI(MBB, MBBI, AVR::OUTARr) 1438 .addImm(0x3e) 1439 .addReg(SrcHiReg, getKillRegState(SrcIsKill)) 1440 .setMIFlags(Flags); 1441 1442 buildMI(MBB, MBBI, AVR::OUTARr) 1443 .addImm(SREG_ADDR) 1444 .addReg(AVR::R0, RegState::Kill) 1445 .setMIFlags(Flags); 1446 1447 buildMI(MBB, MBBI, AVR::OUTARr) 1448 .addImm(0x3d) 1449 .addReg(SrcLoReg, getKillRegState(SrcIsKill)) 1450 .setMIFlags(Flags); 1451 1452 MI.eraseFromParent(); 1453 return true; 1454 } 1455 1456 bool AVRExpandPseudo::expandMI(Block &MBB, BlockIt MBBI) { 1457 MachineInstr &MI = *MBBI; 1458 int Opcode = MBBI->getOpcode(); 1459 1460 #define EXPAND(Op) \ 1461 case Op: \ 1462 return expand<Op>(MBB, MI) 1463 1464 switch (Opcode) { 1465 EXPAND(AVR::ADDWRdRr); 1466 EXPAND(AVR::ADCWRdRr); 1467 EXPAND(AVR::SUBWRdRr); 1468 EXPAND(AVR::SUBIWRdK); 1469 EXPAND(AVR::SBCWRdRr); 1470 EXPAND(AVR::SBCIWRdK); 1471 EXPAND(AVR::ANDWRdRr); 1472 EXPAND(AVR::ANDIWRdK); 1473 EXPAND(AVR::ORWRdRr); 1474 EXPAND(AVR::ORIWRdK); 1475 EXPAND(AVR::EORWRdRr); 1476 EXPAND(AVR::COMWRd); 1477 EXPAND(AVR::CPWRdRr); 1478 EXPAND(AVR::CPCWRdRr); 1479 EXPAND(AVR::LDIWRdK); 1480 EXPAND(AVR::LDSWRdK); 1481 EXPAND(AVR::LDWRdPtr); 1482 EXPAND(AVR::LDWRdPtrPi); 1483 EXPAND(AVR::LDWRdPtrPd); 1484 case AVR::LDDWRdYQ: //:FIXME: remove this once PR13375 gets fixed 1485 EXPAND(AVR::LDDWRdPtrQ); 1486 EXPAND(AVR::LPMWRdZ); 1487 EXPAND(AVR::LPMWRdZPi); 1488 EXPAND(AVR::AtomicLoad8); 1489 EXPAND(AVR::AtomicLoad16); 1490 EXPAND(AVR::AtomicStore8); 1491 EXPAND(AVR::AtomicStore16); 1492 EXPAND(AVR::AtomicLoadAdd8); 1493 EXPAND(AVR::AtomicLoadAdd16); 1494 EXPAND(AVR::AtomicLoadSub8); 1495 EXPAND(AVR::AtomicLoadSub16); 1496 EXPAND(AVR::AtomicLoadAnd8); 1497 EXPAND(AVR::AtomicLoadAnd16); 1498 EXPAND(AVR::AtomicLoadOr8); 1499 EXPAND(AVR::AtomicLoadOr16); 1500 EXPAND(AVR::AtomicLoadXor8); 1501 EXPAND(AVR::AtomicLoadXor16); 1502 EXPAND(AVR::AtomicFence); 1503 EXPAND(AVR::STSWKRr); 1504 EXPAND(AVR::STWPtrRr); 1505 EXPAND(AVR::STWPtrPiRr); 1506 EXPAND(AVR::STWPtrPdRr); 1507 EXPAND(AVR::STDWPtrQRr); 1508 EXPAND(AVR::INWRdA); 1509 EXPAND(AVR::OUTWARr); 1510 EXPAND(AVR::PUSHWRr); 1511 EXPAND(AVR::POPWRd); 1512 EXPAND(AVR::LSLWRd); 1513 EXPAND(AVR::LSRWRd); 1514 EXPAND(AVR::RORWRd); 1515 EXPAND(AVR::ROLWRd); 1516 EXPAND(AVR::ASRWRd); 1517 EXPAND(AVR::SEXT); 1518 EXPAND(AVR::ZEXT); 1519 EXPAND(AVR::SPREAD); 1520 EXPAND(AVR::SPWRITE); 1521 } 1522 #undef EXPAND 1523 return false; 1524 } 1525 1526 } // end of anonymous namespace 1527 1528 INITIALIZE_PASS(AVRExpandPseudo, "avr-expand-pseudo", 1529 AVR_EXPAND_PSEUDO_NAME, false, false) 1530 namespace llvm { 1531 1532 FunctionPass *createAVRExpandPseudoPass() { return new AVRExpandPseudo(); } 1533 1534 } // end of namespace llvm 1535