1 //===-- AVRExpandPseudoInsts.cpp - Expand pseudo instructions -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains a pass that expands pseudo instructions into target 10 // instructions. This pass should be run after register allocation but before 11 // the post-regalloc scheduling pass. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "AVR.h" 16 #include "AVRInstrInfo.h" 17 #include "AVRTargetMachine.h" 18 #include "MCTargetDesc/AVRMCTargetDesc.h" 19 20 #include "llvm/CodeGen/MachineFunctionPass.h" 21 #include "llvm/CodeGen/MachineInstrBuilder.h" 22 #include "llvm/CodeGen/MachineRegisterInfo.h" 23 #include "llvm/CodeGen/RegisterScavenging.h" 24 #include "llvm/CodeGen/TargetRegisterInfo.h" 25 26 using namespace llvm; 27 28 #define AVR_EXPAND_PSEUDO_NAME "AVR pseudo instruction expansion pass" 29 30 namespace { 31 32 /// Expands "placeholder" instructions marked as pseudo into 33 /// actual AVR instructions. 34 class AVRExpandPseudo : public MachineFunctionPass { 35 public: 36 static char ID; 37 38 AVRExpandPseudo() : MachineFunctionPass(ID) { 39 initializeAVRExpandPseudoPass(*PassRegistry::getPassRegistry()); 40 } 41 42 bool runOnMachineFunction(MachineFunction &MF) override; 43 44 StringRef getPassName() const override { return AVR_EXPAND_PSEUDO_NAME; } 45 46 private: 47 typedef MachineBasicBlock Block; 48 typedef Block::iterator BlockIt; 49 50 const AVRRegisterInfo *TRI; 51 const TargetInstrInfo *TII; 52 53 /// The register to be used for temporary storage. 54 const Register SCRATCH_REGISTER = AVR::R0; 55 /// The register that will always contain zero. 56 const Register ZERO_REGISTER = AVR::R1; 57 /// The IO address of the status register. 58 const unsigned SREG_ADDR = 0x3f; 59 60 bool expandMBB(Block &MBB); 61 bool expandMI(Block &MBB, BlockIt MBBI); 62 template <unsigned OP> bool expand(Block &MBB, BlockIt MBBI); 63 64 MachineInstrBuilder buildMI(Block &MBB, BlockIt MBBI, unsigned Opcode) { 65 return BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(Opcode)); 66 } 67 68 MachineInstrBuilder buildMI(Block &MBB, BlockIt MBBI, unsigned Opcode, 69 Register DstReg) { 70 return BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(Opcode), DstReg); 71 } 72 73 MachineRegisterInfo &getRegInfo(Block &MBB) { 74 return MBB.getParent()->getRegInfo(); 75 } 76 77 bool expandArith(unsigned OpLo, unsigned OpHi, Block &MBB, BlockIt MBBI); 78 bool expandLogic(unsigned Op, Block &MBB, BlockIt MBBI); 79 bool expandLogicImm(unsigned Op, Block &MBB, BlockIt MBBI); 80 bool isLogicImmOpRedundant(unsigned Op, unsigned ImmVal) const; 81 82 template <typename Func> bool expandAtomic(Block &MBB, BlockIt MBBI, Func f); 83 84 template <typename Func> 85 bool expandAtomicBinaryOp(unsigned Opcode, Block &MBB, BlockIt MBBI, Func f); 86 87 bool expandAtomicBinaryOp(unsigned Opcode, Block &MBB, BlockIt MBBI); 88 89 bool expandAtomicArithmeticOp(unsigned MemOpcode, unsigned ArithOpcode, 90 Block &MBB, BlockIt MBBI); 91 92 /// Specific shift implementation. 93 bool expandLSLB7Rd(Block &MBB, BlockIt MBBI); 94 bool expandLSRB7Rd(Block &MBB, BlockIt MBBI); 95 bool expandASRB6Rd(Block &MBB, BlockIt MBBI); 96 bool expandASRB7Rd(Block &MBB, BlockIt MBBI); 97 bool expandLSLW4Rd(Block &MBB, BlockIt MBBI); 98 bool expandLSRW4Rd(Block &MBB, BlockIt MBBI); 99 bool expandLSLW8Rd(Block &MBB, BlockIt MBBI); 100 bool expandLSRW8Rd(Block &MBB, BlockIt MBBI); 101 bool expandASRW8Rd(Block &MBB, BlockIt MBBI); 102 bool expandLSLW12Rd(Block &MBB, BlockIt MBBI); 103 bool expandLSRW12Rd(Block &MBB, BlockIt MBBI); 104 105 // Common implementation of LPMWRdZ and ELPMWRdZ. 106 bool expandLPMWELPMW(Block &MBB, BlockIt MBBI, bool IsExt); 107 108 /// Scavenges a free GPR8 register for use. 109 Register scavengeGPR8(MachineInstr &MI); 110 }; 111 112 char AVRExpandPseudo::ID = 0; 113 114 bool AVRExpandPseudo::expandMBB(MachineBasicBlock &MBB) { 115 bool Modified = false; 116 117 BlockIt MBBI = MBB.begin(), E = MBB.end(); 118 while (MBBI != E) { 119 BlockIt NMBBI = std::next(MBBI); 120 Modified |= expandMI(MBB, MBBI); 121 MBBI = NMBBI; 122 } 123 124 return Modified; 125 } 126 127 bool AVRExpandPseudo::runOnMachineFunction(MachineFunction &MF) { 128 bool Modified = false; 129 130 const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>(); 131 TRI = STI.getRegisterInfo(); 132 TII = STI.getInstrInfo(); 133 134 // We need to track liveness in order to use register scavenging. 135 MF.getProperties().set(MachineFunctionProperties::Property::TracksLiveness); 136 137 for (Block &MBB : MF) { 138 bool ContinueExpanding = true; 139 unsigned ExpandCount = 0; 140 141 // Continue expanding the block until all pseudos are expanded. 142 do { 143 assert(ExpandCount < 10 && "pseudo expand limit reached"); 144 145 bool BlockModified = expandMBB(MBB); 146 Modified |= BlockModified; 147 ExpandCount++; 148 149 ContinueExpanding = BlockModified; 150 } while (ContinueExpanding); 151 } 152 153 return Modified; 154 } 155 156 bool AVRExpandPseudo::expandArith(unsigned OpLo, unsigned OpHi, Block &MBB, 157 BlockIt MBBI) { 158 MachineInstr &MI = *MBBI; 159 Register SrcLoReg, SrcHiReg, DstLoReg, DstHiReg; 160 Register DstReg = MI.getOperand(0).getReg(); 161 Register SrcReg = MI.getOperand(2).getReg(); 162 bool DstIsDead = MI.getOperand(0).isDead(); 163 bool DstIsKill = MI.getOperand(1).isKill(); 164 bool SrcIsKill = MI.getOperand(2).isKill(); 165 bool ImpIsDead = MI.getOperand(3).isDead(); 166 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 167 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 168 169 buildMI(MBB, MBBI, OpLo) 170 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 171 .addReg(DstLoReg, getKillRegState(DstIsKill)) 172 .addReg(SrcLoReg, getKillRegState(SrcIsKill)); 173 174 auto MIBHI = 175 buildMI(MBB, MBBI, OpHi) 176 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 177 .addReg(DstHiReg, getKillRegState(DstIsKill)) 178 .addReg(SrcHiReg, getKillRegState(SrcIsKill)); 179 180 if (ImpIsDead) 181 MIBHI->getOperand(3).setIsDead(); 182 183 // SREG is always implicitly killed 184 MIBHI->getOperand(4).setIsKill(); 185 186 MI.eraseFromParent(); 187 return true; 188 } 189 190 bool AVRExpandPseudo::expandLogic(unsigned Op, Block &MBB, BlockIt MBBI) { 191 MachineInstr &MI = *MBBI; 192 Register SrcLoReg, SrcHiReg, DstLoReg, DstHiReg; 193 Register DstReg = MI.getOperand(0).getReg(); 194 Register SrcReg = MI.getOperand(2).getReg(); 195 bool DstIsDead = MI.getOperand(0).isDead(); 196 bool DstIsKill = MI.getOperand(1).isKill(); 197 bool SrcIsKill = MI.getOperand(2).isKill(); 198 bool ImpIsDead = MI.getOperand(3).isDead(); 199 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 200 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 201 202 auto MIBLO = 203 buildMI(MBB, MBBI, Op) 204 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 205 .addReg(DstLoReg, getKillRegState(DstIsKill)) 206 .addReg(SrcLoReg, getKillRegState(SrcIsKill)); 207 208 // SREG is always implicitly dead 209 MIBLO->getOperand(3).setIsDead(); 210 211 auto MIBHI = 212 buildMI(MBB, MBBI, Op) 213 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 214 .addReg(DstHiReg, getKillRegState(DstIsKill)) 215 .addReg(SrcHiReg, getKillRegState(SrcIsKill)); 216 217 if (ImpIsDead) 218 MIBHI->getOperand(3).setIsDead(); 219 220 MI.eraseFromParent(); 221 return true; 222 } 223 224 bool AVRExpandPseudo::isLogicImmOpRedundant(unsigned Op, 225 unsigned ImmVal) const { 226 227 // ANDI Rd, 0xff is redundant. 228 if (Op == AVR::ANDIRdK && ImmVal == 0xff) 229 return true; 230 231 // ORI Rd, 0x0 is redundant. 232 if (Op == AVR::ORIRdK && ImmVal == 0x0) 233 return true; 234 235 return false; 236 } 237 238 bool AVRExpandPseudo::expandLogicImm(unsigned Op, Block &MBB, BlockIt MBBI) { 239 MachineInstr &MI = *MBBI; 240 Register DstLoReg, DstHiReg; 241 Register DstReg = MI.getOperand(0).getReg(); 242 bool DstIsDead = MI.getOperand(0).isDead(); 243 bool SrcIsKill = MI.getOperand(1).isKill(); 244 bool ImpIsDead = MI.getOperand(3).isDead(); 245 unsigned Imm = MI.getOperand(2).getImm(); 246 unsigned Lo8 = Imm & 0xff; 247 unsigned Hi8 = (Imm >> 8) & 0xff; 248 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 249 250 if (!isLogicImmOpRedundant(Op, Lo8)) { 251 auto MIBLO = 252 buildMI(MBB, MBBI, Op) 253 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 254 .addReg(DstLoReg, getKillRegState(SrcIsKill)) 255 .addImm(Lo8); 256 257 // SREG is always implicitly dead 258 MIBLO->getOperand(3).setIsDead(); 259 } 260 261 if (!isLogicImmOpRedundant(Op, Hi8)) { 262 auto MIBHI = 263 buildMI(MBB, MBBI, Op) 264 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 265 .addReg(DstHiReg, getKillRegState(SrcIsKill)) 266 .addImm(Hi8); 267 268 if (ImpIsDead) 269 MIBHI->getOperand(3).setIsDead(); 270 } 271 272 MI.eraseFromParent(); 273 return true; 274 } 275 276 template <> 277 bool AVRExpandPseudo::expand<AVR::ADDWRdRr>(Block &MBB, BlockIt MBBI) { 278 return expandArith(AVR::ADDRdRr, AVR::ADCRdRr, MBB, MBBI); 279 } 280 281 template <> 282 bool AVRExpandPseudo::expand<AVR::ADCWRdRr>(Block &MBB, BlockIt MBBI) { 283 return expandArith(AVR::ADCRdRr, AVR::ADCRdRr, MBB, MBBI); 284 } 285 286 template <> 287 bool AVRExpandPseudo::expand<AVR::SUBWRdRr>(Block &MBB, BlockIt MBBI) { 288 return expandArith(AVR::SUBRdRr, AVR::SBCRdRr, MBB, MBBI); 289 } 290 291 template <> 292 bool AVRExpandPseudo::expand<AVR::SUBIWRdK>(Block &MBB, BlockIt MBBI) { 293 MachineInstr &MI = *MBBI; 294 Register DstLoReg, DstHiReg; 295 Register DstReg = MI.getOperand(0).getReg(); 296 bool DstIsDead = MI.getOperand(0).isDead(); 297 bool SrcIsKill = MI.getOperand(1).isKill(); 298 bool ImpIsDead = MI.getOperand(3).isDead(); 299 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 300 301 auto MIBLO = 302 buildMI(MBB, MBBI, AVR::SUBIRdK) 303 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 304 .addReg(DstLoReg, getKillRegState(SrcIsKill)); 305 306 auto MIBHI = 307 buildMI(MBB, MBBI, AVR::SBCIRdK) 308 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 309 .addReg(DstHiReg, getKillRegState(SrcIsKill)); 310 311 switch (MI.getOperand(2).getType()) { 312 case MachineOperand::MO_GlobalAddress: { 313 const GlobalValue *GV = MI.getOperand(2).getGlobal(); 314 int64_t Offs = MI.getOperand(2).getOffset(); 315 unsigned TF = MI.getOperand(2).getTargetFlags(); 316 MIBLO.addGlobalAddress(GV, Offs, TF | AVRII::MO_NEG | AVRII::MO_LO); 317 MIBHI.addGlobalAddress(GV, Offs, TF | AVRII::MO_NEG | AVRII::MO_HI); 318 break; 319 } 320 case MachineOperand::MO_Immediate: { 321 unsigned Imm = MI.getOperand(2).getImm(); 322 MIBLO.addImm(Imm & 0xff); 323 MIBHI.addImm((Imm >> 8) & 0xff); 324 break; 325 } 326 default: 327 llvm_unreachable("Unknown operand type!"); 328 } 329 330 if (ImpIsDead) 331 MIBHI->getOperand(3).setIsDead(); 332 333 // SREG is always implicitly killed 334 MIBHI->getOperand(4).setIsKill(); 335 336 MI.eraseFromParent(); 337 return true; 338 } 339 340 template <> 341 bool AVRExpandPseudo::expand<AVR::SBCWRdRr>(Block &MBB, BlockIt MBBI) { 342 return expandArith(AVR::SBCRdRr, AVR::SBCRdRr, MBB, MBBI); 343 } 344 345 template <> 346 bool AVRExpandPseudo::expand<AVR::SBCIWRdK>(Block &MBB, BlockIt MBBI) { 347 MachineInstr &MI = *MBBI; 348 Register DstLoReg, DstHiReg; 349 Register DstReg = MI.getOperand(0).getReg(); 350 bool DstIsDead = MI.getOperand(0).isDead(); 351 bool SrcIsKill = MI.getOperand(1).isKill(); 352 bool ImpIsDead = MI.getOperand(3).isDead(); 353 unsigned Imm = MI.getOperand(2).getImm(); 354 unsigned Lo8 = Imm & 0xff; 355 unsigned Hi8 = (Imm >> 8) & 0xff; 356 unsigned OpLo = AVR::SBCIRdK; 357 unsigned OpHi = AVR::SBCIRdK; 358 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 359 360 auto MIBLO = 361 buildMI(MBB, MBBI, OpLo) 362 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 363 .addReg(DstLoReg, getKillRegState(SrcIsKill)) 364 .addImm(Lo8); 365 366 // SREG is always implicitly killed 367 MIBLO->getOperand(4).setIsKill(); 368 369 auto MIBHI = 370 buildMI(MBB, MBBI, OpHi) 371 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 372 .addReg(DstHiReg, getKillRegState(SrcIsKill)) 373 .addImm(Hi8); 374 375 if (ImpIsDead) 376 MIBHI->getOperand(3).setIsDead(); 377 378 // SREG is always implicitly killed 379 MIBHI->getOperand(4).setIsKill(); 380 381 MI.eraseFromParent(); 382 return true; 383 } 384 385 template <> 386 bool AVRExpandPseudo::expand<AVR::ANDWRdRr>(Block &MBB, BlockIt MBBI) { 387 return expandLogic(AVR::ANDRdRr, MBB, MBBI); 388 } 389 390 template <> 391 bool AVRExpandPseudo::expand<AVR::ANDIWRdK>(Block &MBB, BlockIt MBBI) { 392 return expandLogicImm(AVR::ANDIRdK, MBB, MBBI); 393 } 394 395 template <> 396 bool AVRExpandPseudo::expand<AVR::ORWRdRr>(Block &MBB, BlockIt MBBI) { 397 return expandLogic(AVR::ORRdRr, MBB, MBBI); 398 } 399 400 template <> 401 bool AVRExpandPseudo::expand<AVR::ORIWRdK>(Block &MBB, BlockIt MBBI) { 402 return expandLogicImm(AVR::ORIRdK, MBB, MBBI); 403 } 404 405 template <> 406 bool AVRExpandPseudo::expand<AVR::EORWRdRr>(Block &MBB, BlockIt MBBI) { 407 return expandLogic(AVR::EORRdRr, MBB, MBBI); 408 } 409 410 template <> 411 bool AVRExpandPseudo::expand<AVR::COMWRd>(Block &MBB, BlockIt MBBI) { 412 MachineInstr &MI = *MBBI; 413 Register DstLoReg, DstHiReg; 414 Register DstReg = MI.getOperand(0).getReg(); 415 bool DstIsDead = MI.getOperand(0).isDead(); 416 bool DstIsKill = MI.getOperand(1).isKill(); 417 bool ImpIsDead = MI.getOperand(2).isDead(); 418 unsigned OpLo = AVR::COMRd; 419 unsigned OpHi = AVR::COMRd; 420 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 421 422 auto MIBLO = 423 buildMI(MBB, MBBI, OpLo) 424 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 425 .addReg(DstLoReg, getKillRegState(DstIsKill)); 426 427 // SREG is always implicitly dead 428 MIBLO->getOperand(2).setIsDead(); 429 430 auto MIBHI = 431 buildMI(MBB, MBBI, OpHi) 432 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 433 .addReg(DstHiReg, getKillRegState(DstIsKill)); 434 435 if (ImpIsDead) 436 MIBHI->getOperand(2).setIsDead(); 437 438 MI.eraseFromParent(); 439 return true; 440 } 441 442 template <> 443 bool AVRExpandPseudo::expand<AVR::NEGWRd>(Block &MBB, BlockIt MBBI) { 444 MachineInstr &MI = *MBBI; 445 Register DstLoReg, DstHiReg; 446 Register DstReg = MI.getOperand(0).getReg(); 447 bool DstIsDead = MI.getOperand(0).isDead(); 448 bool DstIsKill = MI.getOperand(1).isKill(); 449 bool ImpIsDead = MI.getOperand(2).isDead(); 450 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 451 452 // Do NEG on the upper byte. 453 auto MIBHI = 454 buildMI(MBB, MBBI, AVR::NEGRd) 455 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 456 .addReg(DstHiReg, getKillRegState(DstIsKill)); 457 // SREG is always implicitly dead 458 MIBHI->getOperand(2).setIsDead(); 459 460 // Do NEG on the lower byte. 461 buildMI(MBB, MBBI, AVR::NEGRd) 462 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 463 .addReg(DstLoReg, getKillRegState(DstIsKill)); 464 465 // Do an extra SBC. 466 auto MISBCI = 467 buildMI(MBB, MBBI, AVR::SBCRdRr) 468 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 469 .addReg(DstHiReg, getKillRegState(DstIsKill)) 470 .addReg(ZERO_REGISTER); 471 if (ImpIsDead) 472 MISBCI->getOperand(3).setIsDead(); 473 // SREG is always implicitly killed 474 MISBCI->getOperand(4).setIsKill(); 475 476 MI.eraseFromParent(); 477 return true; 478 } 479 480 template <> 481 bool AVRExpandPseudo::expand<AVR::CPWRdRr>(Block &MBB, BlockIt MBBI) { 482 MachineInstr &MI = *MBBI; 483 Register SrcLoReg, SrcHiReg, DstLoReg, DstHiReg; 484 Register DstReg = MI.getOperand(0).getReg(); 485 Register SrcReg = MI.getOperand(1).getReg(); 486 bool DstIsKill = MI.getOperand(0).isKill(); 487 bool SrcIsKill = MI.getOperand(1).isKill(); 488 bool ImpIsDead = MI.getOperand(2).isDead(); 489 unsigned OpLo = AVR::CPRdRr; 490 unsigned OpHi = AVR::CPCRdRr; 491 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 492 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 493 494 // Low part 495 buildMI(MBB, MBBI, OpLo) 496 .addReg(DstLoReg, getKillRegState(DstIsKill)) 497 .addReg(SrcLoReg, getKillRegState(SrcIsKill)); 498 499 auto MIBHI = buildMI(MBB, MBBI, OpHi) 500 .addReg(DstHiReg, getKillRegState(DstIsKill)) 501 .addReg(SrcHiReg, getKillRegState(SrcIsKill)); 502 503 if (ImpIsDead) 504 MIBHI->getOperand(2).setIsDead(); 505 506 // SREG is always implicitly killed 507 MIBHI->getOperand(3).setIsKill(); 508 509 MI.eraseFromParent(); 510 return true; 511 } 512 513 template <> 514 bool AVRExpandPseudo::expand<AVR::CPCWRdRr>(Block &MBB, BlockIt MBBI) { 515 MachineInstr &MI = *MBBI; 516 Register SrcLoReg, SrcHiReg, DstLoReg, DstHiReg; 517 Register DstReg = MI.getOperand(0).getReg(); 518 Register SrcReg = MI.getOperand(1).getReg(); 519 bool DstIsKill = MI.getOperand(0).isKill(); 520 bool SrcIsKill = MI.getOperand(1).isKill(); 521 bool ImpIsDead = MI.getOperand(2).isDead(); 522 unsigned OpLo = AVR::CPCRdRr; 523 unsigned OpHi = AVR::CPCRdRr; 524 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 525 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 526 527 auto MIBLO = buildMI(MBB, MBBI, OpLo) 528 .addReg(DstLoReg, getKillRegState(DstIsKill)) 529 .addReg(SrcLoReg, getKillRegState(SrcIsKill)); 530 531 // SREG is always implicitly killed 532 MIBLO->getOperand(3).setIsKill(); 533 534 auto MIBHI = buildMI(MBB, MBBI, OpHi) 535 .addReg(DstHiReg, getKillRegState(DstIsKill)) 536 .addReg(SrcHiReg, getKillRegState(SrcIsKill)); 537 538 if (ImpIsDead) 539 MIBHI->getOperand(2).setIsDead(); 540 541 // SREG is always implicitly killed 542 MIBHI->getOperand(3).setIsKill(); 543 544 MI.eraseFromParent(); 545 return true; 546 } 547 548 template <> 549 bool AVRExpandPseudo::expand<AVR::LDIWRdK>(Block &MBB, BlockIt MBBI) { 550 MachineInstr &MI = *MBBI; 551 Register DstLoReg, DstHiReg; 552 Register DstReg = MI.getOperand(0).getReg(); 553 bool DstIsDead = MI.getOperand(0).isDead(); 554 unsigned OpLo = AVR::LDIRdK; 555 unsigned OpHi = AVR::LDIRdK; 556 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 557 558 auto MIBLO = 559 buildMI(MBB, MBBI, OpLo) 560 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)); 561 562 auto MIBHI = 563 buildMI(MBB, MBBI, OpHi) 564 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)); 565 566 switch (MI.getOperand(1).getType()) { 567 case MachineOperand::MO_GlobalAddress: { 568 const GlobalValue *GV = MI.getOperand(1).getGlobal(); 569 int64_t Offs = MI.getOperand(1).getOffset(); 570 unsigned TF = MI.getOperand(1).getTargetFlags(); 571 572 MIBLO.addGlobalAddress(GV, Offs, TF | AVRII::MO_LO); 573 MIBHI.addGlobalAddress(GV, Offs, TF | AVRII::MO_HI); 574 break; 575 } 576 case MachineOperand::MO_BlockAddress: { 577 const BlockAddress *BA = MI.getOperand(1).getBlockAddress(); 578 unsigned TF = MI.getOperand(1).getTargetFlags(); 579 580 MIBLO.add(MachineOperand::CreateBA(BA, TF | AVRII::MO_LO)); 581 MIBHI.add(MachineOperand::CreateBA(BA, TF | AVRII::MO_HI)); 582 break; 583 } 584 case MachineOperand::MO_Immediate: { 585 unsigned Imm = MI.getOperand(1).getImm(); 586 587 MIBLO.addImm(Imm & 0xff); 588 MIBHI.addImm((Imm >> 8) & 0xff); 589 break; 590 } 591 default: 592 llvm_unreachable("Unknown operand type!"); 593 } 594 595 MI.eraseFromParent(); 596 return true; 597 } 598 599 template <> 600 bool AVRExpandPseudo::expand<AVR::LDSWRdK>(Block &MBB, BlockIt MBBI) { 601 MachineInstr &MI = *MBBI; 602 Register DstLoReg, DstHiReg; 603 Register DstReg = MI.getOperand(0).getReg(); 604 bool DstIsDead = MI.getOperand(0).isDead(); 605 unsigned OpLo = AVR::LDSRdK; 606 unsigned OpHi = AVR::LDSRdK; 607 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 608 609 auto MIBLO = 610 buildMI(MBB, MBBI, OpLo) 611 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)); 612 613 auto MIBHI = 614 buildMI(MBB, MBBI, OpHi) 615 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)); 616 617 switch (MI.getOperand(1).getType()) { 618 case MachineOperand::MO_GlobalAddress: { 619 const GlobalValue *GV = MI.getOperand(1).getGlobal(); 620 int64_t Offs = MI.getOperand(1).getOffset(); 621 unsigned TF = MI.getOperand(1).getTargetFlags(); 622 623 MIBLO.addGlobalAddress(GV, Offs, TF); 624 MIBHI.addGlobalAddress(GV, Offs + 1, TF); 625 break; 626 } 627 case MachineOperand::MO_Immediate: { 628 unsigned Imm = MI.getOperand(1).getImm(); 629 630 MIBLO.addImm(Imm); 631 MIBHI.addImm(Imm + 1); 632 break; 633 } 634 default: 635 llvm_unreachable("Unknown operand type!"); 636 } 637 638 MIBLO.setMemRefs(MI.memoperands()); 639 MIBHI.setMemRefs(MI.memoperands()); 640 641 MI.eraseFromParent(); 642 return true; 643 } 644 645 template <> 646 bool AVRExpandPseudo::expand<AVR::LDWRdPtr>(Block &MBB, BlockIt MBBI) { 647 MachineInstr &MI = *MBBI; 648 Register DstLoReg, DstHiReg; 649 Register DstReg = MI.getOperand(0).getReg(); 650 Register TmpReg = 0; // 0 for no temporary register 651 Register SrcReg = MI.getOperand(1).getReg(); 652 bool SrcIsKill = MI.getOperand(1).isKill(); 653 unsigned OpLo = AVR::LDRdPtr; 654 unsigned OpHi = AVR::LDDRdPtrQ; 655 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 656 657 // Use a temporary register if src and dst registers are the same. 658 if (DstReg == SrcReg) 659 TmpReg = scavengeGPR8(MI); 660 661 Register CurDstLoReg = (DstReg == SrcReg) ? TmpReg : DstLoReg; 662 Register CurDstHiReg = (DstReg == SrcReg) ? TmpReg : DstHiReg; 663 664 // Load low byte. 665 auto MIBLO = buildMI(MBB, MBBI, OpLo) 666 .addReg(CurDstLoReg, RegState::Define) 667 .addReg(SrcReg); 668 669 // Push low byte onto stack if necessary. 670 if (TmpReg) 671 buildMI(MBB, MBBI, AVR::PUSHRr).addReg(TmpReg); 672 673 // Load high byte. 674 auto MIBHI = buildMI(MBB, MBBI, OpHi) 675 .addReg(CurDstHiReg, RegState::Define) 676 .addReg(SrcReg, getKillRegState(SrcIsKill)) 677 .addImm(1); 678 679 if (TmpReg) { 680 // Move the high byte into the final destination. 681 buildMI(MBB, MBBI, AVR::MOVRdRr, DstHiReg).addReg(TmpReg); 682 683 // Move the low byte from the scratch space into the final destination. 684 buildMI(MBB, MBBI, AVR::POPRd, DstLoReg); 685 } 686 687 MIBLO.setMemRefs(MI.memoperands()); 688 MIBHI.setMemRefs(MI.memoperands()); 689 690 MI.eraseFromParent(); 691 return true; 692 } 693 694 template <> 695 bool AVRExpandPseudo::expand<AVR::LDWRdPtrPi>(Block &MBB, BlockIt MBBI) { 696 MachineInstr &MI = *MBBI; 697 Register DstLoReg, DstHiReg; 698 Register DstReg = MI.getOperand(0).getReg(); 699 Register SrcReg = MI.getOperand(1).getReg(); 700 bool DstIsDead = MI.getOperand(0).isDead(); 701 bool SrcIsDead = MI.getOperand(1).isKill(); 702 unsigned OpLo = AVR::LDRdPtrPi; 703 unsigned OpHi = AVR::LDRdPtrPi; 704 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 705 706 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same"); 707 708 auto MIBLO = 709 buildMI(MBB, MBBI, OpLo) 710 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 711 .addReg(SrcReg, RegState::Define) 712 .addReg(SrcReg, RegState::Kill); 713 714 auto MIBHI = 715 buildMI(MBB, MBBI, OpHi) 716 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 717 .addReg(SrcReg, RegState::Define | getDeadRegState(SrcIsDead)) 718 .addReg(SrcReg, RegState::Kill); 719 720 MIBLO.setMemRefs(MI.memoperands()); 721 MIBHI.setMemRefs(MI.memoperands()); 722 723 MI.eraseFromParent(); 724 return true; 725 } 726 727 template <> 728 bool AVRExpandPseudo::expand<AVR::LDWRdPtrPd>(Block &MBB, BlockIt MBBI) { 729 MachineInstr &MI = *MBBI; 730 Register DstLoReg, DstHiReg; 731 Register DstReg = MI.getOperand(0).getReg(); 732 Register SrcReg = MI.getOperand(1).getReg(); 733 bool DstIsDead = MI.getOperand(0).isDead(); 734 bool SrcIsDead = MI.getOperand(1).isKill(); 735 unsigned OpLo = AVR::LDRdPtrPd; 736 unsigned OpHi = AVR::LDRdPtrPd; 737 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 738 739 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same"); 740 741 auto MIBHI = 742 buildMI(MBB, MBBI, OpHi) 743 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 744 .addReg(SrcReg, RegState::Define) 745 .addReg(SrcReg, RegState::Kill); 746 747 auto MIBLO = 748 buildMI(MBB, MBBI, OpLo) 749 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 750 .addReg(SrcReg, RegState::Define | getDeadRegState(SrcIsDead)) 751 .addReg(SrcReg, RegState::Kill); 752 753 MIBLO.setMemRefs(MI.memoperands()); 754 MIBHI.setMemRefs(MI.memoperands()); 755 756 MI.eraseFromParent(); 757 return true; 758 } 759 760 template <> 761 bool AVRExpandPseudo::expand<AVR::LDDWRdPtrQ>(Block &MBB, BlockIt MBBI) { 762 MachineInstr &MI = *MBBI; 763 Register DstLoReg, DstHiReg; 764 Register DstReg = MI.getOperand(0).getReg(); 765 Register TmpReg = 0; // 0 for no temporary register 766 Register SrcReg = MI.getOperand(1).getReg(); 767 unsigned Imm = MI.getOperand(2).getImm(); 768 bool SrcIsKill = MI.getOperand(1).isKill(); 769 unsigned OpLo = AVR::LDDRdPtrQ; 770 unsigned OpHi = AVR::LDDRdPtrQ; 771 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 772 773 // Since we add 1 to the Imm value for the high byte below, and 63 is the 774 // highest Imm value allowed for the instruction, 62 is the limit here. 775 assert(Imm <= 62 && "Offset is out of range"); 776 777 // Use a temporary register if src and dst registers are the same. 778 if (DstReg == SrcReg) 779 TmpReg = scavengeGPR8(MI); 780 781 Register CurDstLoReg = (DstReg == SrcReg) ? TmpReg : DstLoReg; 782 Register CurDstHiReg = (DstReg == SrcReg) ? TmpReg : DstHiReg; 783 784 // Load low byte. 785 auto MIBLO = buildMI(MBB, MBBI, OpLo) 786 .addReg(CurDstLoReg, RegState::Define) 787 .addReg(SrcReg) 788 .addImm(Imm); 789 790 // Push low byte onto stack if necessary. 791 if (TmpReg) 792 buildMI(MBB, MBBI, AVR::PUSHRr).addReg(TmpReg); 793 794 // Load high byte. 795 auto MIBHI = buildMI(MBB, MBBI, OpHi) 796 .addReg(CurDstHiReg, RegState::Define) 797 .addReg(SrcReg, getKillRegState(SrcIsKill)) 798 .addImm(Imm + 1); 799 800 if (TmpReg) { 801 // Move the high byte into the final destination. 802 buildMI(MBB, MBBI, AVR::MOVRdRr, DstHiReg).addReg(TmpReg); 803 804 // Move the low byte from the scratch space into the final destination. 805 buildMI(MBB, MBBI, AVR::POPRd, DstLoReg); 806 } 807 808 MIBLO.setMemRefs(MI.memoperands()); 809 MIBHI.setMemRefs(MI.memoperands()); 810 811 MI.eraseFromParent(); 812 return true; 813 } 814 815 bool AVRExpandPseudo::expandLPMWELPMW(Block &MBB, BlockIt MBBI, bool IsExt) { 816 MachineInstr &MI = *MBBI; 817 Register DstLoReg, DstHiReg; 818 Register DstReg = MI.getOperand(0).getReg(); 819 Register TmpReg = 0; // 0 for no temporary register 820 Register SrcReg = MI.getOperand(1).getReg(); 821 bool SrcIsKill = MI.getOperand(1).isKill(); 822 unsigned OpLo = IsExt ? AVR::ELPMRdZPi : AVR::LPMRdZPi; 823 unsigned OpHi = IsExt ? AVR::ELPMRdZ : AVR::LPMRdZ; 824 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 825 826 // Set the I/O register RAMPZ for ELPM. 827 if (IsExt) { 828 const AVRSubtarget &STI = MBB.getParent()->getSubtarget<AVRSubtarget>(); 829 Register Bank = MI.getOperand(2).getReg(); 830 // out RAMPZ, rtmp 831 buildMI(MBB, MBBI, AVR::OUTARr).addImm(STI.getIORegRAMPZ()).addReg(Bank); 832 } 833 834 // Use a temporary register if src and dst registers are the same. 835 if (DstReg == SrcReg) 836 TmpReg = scavengeGPR8(MI); 837 838 Register CurDstLoReg = (DstReg == SrcReg) ? TmpReg : DstLoReg; 839 Register CurDstHiReg = (DstReg == SrcReg) ? TmpReg : DstHiReg; 840 841 // Load low byte. 842 auto MIBLO = buildMI(MBB, MBBI, OpLo) 843 .addReg(CurDstLoReg, RegState::Define) 844 .addReg(SrcReg); 845 846 // Push low byte onto stack if necessary. 847 if (TmpReg) 848 buildMI(MBB, MBBI, AVR::PUSHRr).addReg(TmpReg); 849 850 // Load high byte. 851 auto MIBHI = buildMI(MBB, MBBI, OpHi) 852 .addReg(CurDstHiReg, RegState::Define) 853 .addReg(SrcReg, getKillRegState(SrcIsKill)); 854 855 if (TmpReg) { 856 // Move the high byte into the final destination. 857 buildMI(MBB, MBBI, AVR::MOVRdRr, DstHiReg).addReg(TmpReg); 858 859 // Move the low byte from the scratch space into the final destination. 860 buildMI(MBB, MBBI, AVR::POPRd, DstLoReg); 861 } 862 863 MIBLO.setMemRefs(MI.memoperands()); 864 MIBHI.setMemRefs(MI.memoperands()); 865 866 MI.eraseFromParent(); 867 return true; 868 } 869 870 template <> 871 bool AVRExpandPseudo::expand<AVR::LPMWRdZ>(Block &MBB, BlockIt MBBI) { 872 return expandLPMWELPMW(MBB, MBBI, false); 873 } 874 875 template <> 876 bool AVRExpandPseudo::expand<AVR::ELPMWRdZ>(Block &MBB, BlockIt MBBI) { 877 return expandLPMWELPMW(MBB, MBBI, true); 878 } 879 880 template <> 881 bool AVRExpandPseudo::expand<AVR::ELPMBRdZ>(Block &MBB, BlockIt MBBI) { 882 MachineInstr &MI = *MBBI; 883 Register DstReg = MI.getOperand(0).getReg(); 884 Register SrcReg = MI.getOperand(1).getReg(); 885 Register BankReg = MI.getOperand(2).getReg(); 886 bool SrcIsKill = MI.getOperand(1).isKill(); 887 const AVRSubtarget &STI = MBB.getParent()->getSubtarget<AVRSubtarget>(); 888 889 // Set the I/O register RAMPZ for ELPM (out RAMPZ, rtmp). 890 buildMI(MBB, MBBI, AVR::OUTARr).addImm(STI.getIORegRAMPZ()).addReg(BankReg); 891 892 // Load byte. 893 auto MILB = buildMI(MBB, MBBI, AVR::ELPMRdZ) 894 .addReg(DstReg, RegState::Define) 895 .addReg(SrcReg, getKillRegState(SrcIsKill)); 896 897 MILB.setMemRefs(MI.memoperands()); 898 899 MI.eraseFromParent(); 900 return true; 901 } 902 903 template <> 904 bool AVRExpandPseudo::expand<AVR::LPMWRdZPi>(Block &MBB, BlockIt MBBI) { 905 llvm_unreachable("16-bit LPMPi is unimplemented"); 906 } 907 908 template <> 909 bool AVRExpandPseudo::expand<AVR::ELPMBRdZPi>(Block &MBB, BlockIt MBBI) { 910 llvm_unreachable("byte ELPMPi is unimplemented"); 911 } 912 913 template <> 914 bool AVRExpandPseudo::expand<AVR::ELPMWRdZPi>(Block &MBB, BlockIt MBBI) { 915 llvm_unreachable("16-bit ELPMPi is unimplemented"); 916 } 917 918 template <typename Func> 919 bool AVRExpandPseudo::expandAtomic(Block &MBB, BlockIt MBBI, Func f) { 920 // Remove the pseudo instruction. 921 MachineInstr &MI = *MBBI; 922 923 // Store the SREG. 924 buildMI(MBB, MBBI, AVR::INRdA) 925 .addReg(SCRATCH_REGISTER, RegState::Define) 926 .addImm(SREG_ADDR); 927 928 // Disable exceptions. 929 buildMI(MBB, MBBI, AVR::BCLRs).addImm(7); // CLI 930 931 f(MI); 932 933 // Restore the status reg. 934 buildMI(MBB, MBBI, AVR::OUTARr).addImm(SREG_ADDR).addReg(SCRATCH_REGISTER); 935 936 MI.eraseFromParent(); 937 return true; 938 } 939 940 template <typename Func> 941 bool AVRExpandPseudo::expandAtomicBinaryOp(unsigned Opcode, Block &MBB, 942 BlockIt MBBI, Func f) { 943 return expandAtomic(MBB, MBBI, [&](MachineInstr &MI) { 944 auto Op1 = MI.getOperand(0); 945 auto Op2 = MI.getOperand(1); 946 947 MachineInstr &NewInst = 948 *buildMI(MBB, MBBI, Opcode).add(Op1).add(Op2).getInstr(); 949 f(NewInst); 950 }); 951 } 952 953 bool AVRExpandPseudo::expandAtomicBinaryOp(unsigned Opcode, Block &MBB, 954 BlockIt MBBI) { 955 return expandAtomicBinaryOp(Opcode, MBB, MBBI, [](MachineInstr &MI) {}); 956 } 957 958 bool AVRExpandPseudo::expandAtomicArithmeticOp(unsigned Width, 959 unsigned ArithOpcode, Block &MBB, 960 BlockIt MBBI) { 961 return expandAtomic(MBB, MBBI, [&](MachineInstr &MI) { 962 auto DstReg = MI.getOperand(0).getReg(); 963 auto PtrOp = MI.getOperand(1); 964 auto SrcReg = MI.getOperand(2).getReg(); 965 966 unsigned LoadOpcode = (Width == 8) ? AVR::LDRdPtr : AVR::LDWRdPtr; 967 unsigned StoreOpcode = (Width == 8) ? AVR::STPtrRr : AVR::STWPtrRr; 968 969 // FIXME: this returns the new value (after the operation), not the old 970 // value as the atomicrmw instruction is supposed to do! 971 972 // Create the load 973 buildMI(MBB, MBBI, LoadOpcode, DstReg).addReg(PtrOp.getReg()); 974 975 // Create the arithmetic op 976 buildMI(MBB, MBBI, ArithOpcode, DstReg).addReg(DstReg).addReg(SrcReg); 977 978 // Create the store 979 buildMI(MBB, MBBI, StoreOpcode).add(PtrOp).addReg(DstReg); 980 }); 981 } 982 983 Register AVRExpandPseudo::scavengeGPR8(MachineInstr &MI) { 984 MachineBasicBlock &MBB = *MI.getParent(); 985 RegScavenger RS; 986 987 RS.enterBasicBlock(MBB); 988 RS.forward(MI); 989 990 BitVector Candidates = 991 TRI->getAllocatableSet(*MBB.getParent(), &AVR::GPR8RegClass); 992 993 // Exclude all the registers being used by the instruction. 994 for (MachineOperand &MO : MI.operands()) { 995 if (MO.isReg() && MO.getReg() != 0 && !MO.isDef() && 996 !Register::isVirtualRegister(MO.getReg())) 997 Candidates.reset(MO.getReg()); 998 } 999 1000 BitVector Available = RS.getRegsAvailable(&AVR::GPR8RegClass); 1001 Available &= Candidates; 1002 1003 signed Reg = Available.find_first(); 1004 assert(Reg != -1 && "ran out of registers"); 1005 return Reg; 1006 } 1007 1008 template <> 1009 bool AVRExpandPseudo::expand<AVR::AtomicLoad8>(Block &MBB, BlockIt MBBI) { 1010 return expandAtomicBinaryOp(AVR::LDRdPtr, MBB, MBBI); 1011 } 1012 1013 template <> 1014 bool AVRExpandPseudo::expand<AVR::AtomicLoad16>(Block &MBB, BlockIt MBBI) { 1015 return expandAtomicBinaryOp(AVR::LDWRdPtr, MBB, MBBI); 1016 } 1017 1018 template <> 1019 bool AVRExpandPseudo::expand<AVR::AtomicStore8>(Block &MBB, BlockIt MBBI) { 1020 return expandAtomicBinaryOp(AVR::STPtrRr, MBB, MBBI); 1021 } 1022 1023 template <> 1024 bool AVRExpandPseudo::expand<AVR::AtomicStore16>(Block &MBB, BlockIt MBBI) { 1025 return expandAtomicBinaryOp(AVR::STWPtrRr, MBB, MBBI); 1026 } 1027 1028 template <> 1029 bool AVRExpandPseudo::expand<AVR::AtomicLoadAdd8>(Block &MBB, BlockIt MBBI) { 1030 return expandAtomicArithmeticOp(8, AVR::ADDRdRr, MBB, MBBI); 1031 } 1032 1033 template <> 1034 bool AVRExpandPseudo::expand<AVR::AtomicLoadAdd16>(Block &MBB, BlockIt MBBI) { 1035 return expandAtomicArithmeticOp(16, AVR::ADDWRdRr, MBB, MBBI); 1036 } 1037 1038 template <> 1039 bool AVRExpandPseudo::expand<AVR::AtomicLoadSub8>(Block &MBB, BlockIt MBBI) { 1040 return expandAtomicArithmeticOp(8, AVR::SUBRdRr, MBB, MBBI); 1041 } 1042 1043 template <> 1044 bool AVRExpandPseudo::expand<AVR::AtomicLoadSub16>(Block &MBB, BlockIt MBBI) { 1045 return expandAtomicArithmeticOp(16, AVR::SUBWRdRr, MBB, MBBI); 1046 } 1047 1048 template <> 1049 bool AVRExpandPseudo::expand<AVR::AtomicLoadAnd8>(Block &MBB, BlockIt MBBI) { 1050 return expandAtomicArithmeticOp(8, AVR::ANDRdRr, MBB, MBBI); 1051 } 1052 1053 template <> 1054 bool AVRExpandPseudo::expand<AVR::AtomicLoadAnd16>(Block &MBB, BlockIt MBBI) { 1055 return expandAtomicArithmeticOp(16, AVR::ANDWRdRr, MBB, MBBI); 1056 } 1057 1058 template <> 1059 bool AVRExpandPseudo::expand<AVR::AtomicLoadOr8>(Block &MBB, BlockIt MBBI) { 1060 return expandAtomicArithmeticOp(8, AVR::ORRdRr, MBB, MBBI); 1061 } 1062 1063 template <> 1064 bool AVRExpandPseudo::expand<AVR::AtomicLoadOr16>(Block &MBB, BlockIt MBBI) { 1065 return expandAtomicArithmeticOp(16, AVR::ORWRdRr, MBB, MBBI); 1066 } 1067 1068 template <> 1069 bool AVRExpandPseudo::expand<AVR::AtomicLoadXor8>(Block &MBB, BlockIt MBBI) { 1070 return expandAtomicArithmeticOp(8, AVR::EORRdRr, MBB, MBBI); 1071 } 1072 1073 template <> 1074 bool AVRExpandPseudo::expand<AVR::AtomicLoadXor16>(Block &MBB, BlockIt MBBI) { 1075 return expandAtomicArithmeticOp(16, AVR::EORWRdRr, MBB, MBBI); 1076 } 1077 1078 template <> 1079 bool AVRExpandPseudo::expand<AVR::AtomicFence>(Block &MBB, BlockIt MBBI) { 1080 // On AVR, there is only one core and so atomic fences do nothing. 1081 MBBI->eraseFromParent(); 1082 return true; 1083 } 1084 1085 template <> 1086 bool AVRExpandPseudo::expand<AVR::STSWKRr>(Block &MBB, BlockIt MBBI) { 1087 MachineInstr &MI = *MBBI; 1088 Register SrcLoReg, SrcHiReg; 1089 Register SrcReg = MI.getOperand(1).getReg(); 1090 bool SrcIsKill = MI.getOperand(1).isKill(); 1091 unsigned OpLo = AVR::STSKRr; 1092 unsigned OpHi = AVR::STSKRr; 1093 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1094 1095 // Write the high byte first in case this address belongs to a special 1096 // I/O address with a special temporary register. 1097 auto MIBHI = buildMI(MBB, MBBI, OpHi); 1098 auto MIBLO = buildMI(MBB, MBBI, OpLo); 1099 1100 switch (MI.getOperand(0).getType()) { 1101 case MachineOperand::MO_GlobalAddress: { 1102 const GlobalValue *GV = MI.getOperand(0).getGlobal(); 1103 int64_t Offs = MI.getOperand(0).getOffset(); 1104 unsigned TF = MI.getOperand(0).getTargetFlags(); 1105 1106 MIBLO.addGlobalAddress(GV, Offs, TF); 1107 MIBHI.addGlobalAddress(GV, Offs + 1, TF); 1108 break; 1109 } 1110 case MachineOperand::MO_Immediate: { 1111 unsigned Imm = MI.getOperand(0).getImm(); 1112 1113 MIBLO.addImm(Imm); 1114 MIBHI.addImm(Imm + 1); 1115 break; 1116 } 1117 default: 1118 llvm_unreachable("Unknown operand type!"); 1119 } 1120 1121 MIBLO.addReg(SrcLoReg, getKillRegState(SrcIsKill)); 1122 MIBHI.addReg(SrcHiReg, getKillRegState(SrcIsKill)); 1123 1124 MIBLO.setMemRefs(MI.memoperands()); 1125 MIBHI.setMemRefs(MI.memoperands()); 1126 1127 MI.eraseFromParent(); 1128 return true; 1129 } 1130 1131 template <> 1132 bool AVRExpandPseudo::expand<AVR::STWPtrRr>(Block &MBB, BlockIt MBBI) { 1133 MachineInstr &MI = *MBBI; 1134 Register SrcLoReg, SrcHiReg; 1135 Register DstReg = MI.getOperand(0).getReg(); 1136 Register SrcReg = MI.getOperand(1).getReg(); 1137 bool DstIsUndef = MI.getOperand(0).isUndef(); 1138 bool SrcIsKill = MI.getOperand(1).isKill(); 1139 unsigned OpLo = AVR::STPtrRr; 1140 unsigned OpHi = AVR::STDPtrQRr; 1141 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1142 1143 //: TODO: need to reverse this order like inw and stsw? 1144 auto MIBLO = buildMI(MBB, MBBI, OpLo) 1145 .addReg(DstReg, getUndefRegState(DstIsUndef)) 1146 .addReg(SrcLoReg, getKillRegState(SrcIsKill)); 1147 1148 auto MIBHI = buildMI(MBB, MBBI, OpHi) 1149 .addReg(DstReg, getUndefRegState(DstIsUndef)) 1150 .addImm(1) 1151 .addReg(SrcHiReg, getKillRegState(SrcIsKill)); 1152 1153 MIBLO.setMemRefs(MI.memoperands()); 1154 MIBHI.setMemRefs(MI.memoperands()); 1155 1156 MI.eraseFromParent(); 1157 return true; 1158 } 1159 1160 template <> 1161 bool AVRExpandPseudo::expand<AVR::STWPtrPiRr>(Block &MBB, BlockIt MBBI) { 1162 MachineInstr &MI = *MBBI; 1163 Register SrcLoReg, SrcHiReg; 1164 Register DstReg = MI.getOperand(0).getReg(); 1165 Register SrcReg = MI.getOperand(2).getReg(); 1166 unsigned Imm = MI.getOperand(3).getImm(); 1167 bool DstIsDead = MI.getOperand(0).isDead(); 1168 bool SrcIsKill = MI.getOperand(2).isKill(); 1169 unsigned OpLo = AVR::STPtrPiRr; 1170 unsigned OpHi = AVR::STPtrPiRr; 1171 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1172 1173 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same"); 1174 1175 auto MIBLO = buildMI(MBB, MBBI, OpLo) 1176 .addReg(DstReg, RegState::Define) 1177 .addReg(DstReg, RegState::Kill) 1178 .addReg(SrcLoReg, getKillRegState(SrcIsKill)) 1179 .addImm(Imm); 1180 1181 auto MIBHI = 1182 buildMI(MBB, MBBI, OpHi) 1183 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 1184 .addReg(DstReg, RegState::Kill) 1185 .addReg(SrcHiReg, getKillRegState(SrcIsKill)) 1186 .addImm(Imm); 1187 1188 MIBLO.setMemRefs(MI.memoperands()); 1189 MIBHI.setMemRefs(MI.memoperands()); 1190 1191 MI.eraseFromParent(); 1192 return true; 1193 } 1194 1195 template <> 1196 bool AVRExpandPseudo::expand<AVR::STWPtrPdRr>(Block &MBB, BlockIt MBBI) { 1197 MachineInstr &MI = *MBBI; 1198 Register SrcLoReg, SrcHiReg; 1199 Register DstReg = MI.getOperand(0).getReg(); 1200 Register SrcReg = MI.getOperand(2).getReg(); 1201 unsigned Imm = MI.getOperand(3).getImm(); 1202 bool DstIsDead = MI.getOperand(0).isDead(); 1203 bool SrcIsKill = MI.getOperand(2).isKill(); 1204 unsigned OpLo = AVR::STPtrPdRr; 1205 unsigned OpHi = AVR::STPtrPdRr; 1206 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1207 1208 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same"); 1209 1210 auto MIBHI = buildMI(MBB, MBBI, OpHi) 1211 .addReg(DstReg, RegState::Define) 1212 .addReg(DstReg, RegState::Kill) 1213 .addReg(SrcHiReg, getKillRegState(SrcIsKill)) 1214 .addImm(Imm); 1215 1216 auto MIBLO = 1217 buildMI(MBB, MBBI, OpLo) 1218 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 1219 .addReg(DstReg, RegState::Kill) 1220 .addReg(SrcLoReg, getKillRegState(SrcIsKill)) 1221 .addImm(Imm); 1222 1223 MIBLO.setMemRefs(MI.memoperands()); 1224 MIBHI.setMemRefs(MI.memoperands()); 1225 1226 MI.eraseFromParent(); 1227 return true; 1228 } 1229 1230 template <> 1231 bool AVRExpandPseudo::expand<AVR::STDWPtrQRr>(Block &MBB, BlockIt MBBI) { 1232 MachineInstr &MI = *MBBI; 1233 Register SrcLoReg, SrcHiReg; 1234 Register DstReg = MI.getOperand(0).getReg(); 1235 Register SrcReg = MI.getOperand(2).getReg(); 1236 unsigned Imm = MI.getOperand(1).getImm(); 1237 bool DstIsKill = MI.getOperand(0).isKill(); 1238 bool SrcIsKill = MI.getOperand(2).isKill(); 1239 unsigned OpLo = AVR::STDPtrQRr; 1240 unsigned OpHi = AVR::STDPtrQRr; 1241 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1242 1243 // Since we add 1 to the Imm value for the high byte below, and 63 is the 1244 // highest Imm value allowed for the instruction, 62 is the limit here. 1245 assert(Imm <= 62 && "Offset is out of range"); 1246 1247 auto MIBLO = buildMI(MBB, MBBI, OpLo) 1248 .addReg(DstReg) 1249 .addImm(Imm) 1250 .addReg(SrcLoReg, getKillRegState(SrcIsKill)); 1251 1252 auto MIBHI = buildMI(MBB, MBBI, OpHi) 1253 .addReg(DstReg, getKillRegState(DstIsKill)) 1254 .addImm(Imm + 1) 1255 .addReg(SrcHiReg, getKillRegState(SrcIsKill)); 1256 1257 MIBLO.setMemRefs(MI.memoperands()); 1258 MIBHI.setMemRefs(MI.memoperands()); 1259 1260 MI.eraseFromParent(); 1261 return true; 1262 } 1263 1264 template <> 1265 bool AVRExpandPseudo::expand<AVR::INWRdA>(Block &MBB, BlockIt MBBI) { 1266 MachineInstr &MI = *MBBI; 1267 Register DstLoReg, DstHiReg; 1268 unsigned Imm = MI.getOperand(1).getImm(); 1269 Register DstReg = MI.getOperand(0).getReg(); 1270 bool DstIsDead = MI.getOperand(0).isDead(); 1271 unsigned OpLo = AVR::INRdA; 1272 unsigned OpHi = AVR::INRdA; 1273 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1274 1275 // Since we add 1 to the Imm value for the high byte below, and 63 is the 1276 // highest Imm value allowed for the instruction, 62 is the limit here. 1277 assert(Imm <= 62 && "Address is out of range"); 1278 1279 auto MIBLO = 1280 buildMI(MBB, MBBI, OpLo) 1281 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1282 .addImm(Imm); 1283 1284 auto MIBHI = 1285 buildMI(MBB, MBBI, OpHi) 1286 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1287 .addImm(Imm + 1); 1288 1289 MIBLO.setMemRefs(MI.memoperands()); 1290 MIBHI.setMemRefs(MI.memoperands()); 1291 1292 MI.eraseFromParent(); 1293 return true; 1294 } 1295 1296 template <> 1297 bool AVRExpandPseudo::expand<AVR::OUTWARr>(Block &MBB, BlockIt MBBI) { 1298 MachineInstr &MI = *MBBI; 1299 Register SrcLoReg, SrcHiReg; 1300 unsigned Imm = MI.getOperand(0).getImm(); 1301 Register SrcReg = MI.getOperand(1).getReg(); 1302 bool SrcIsKill = MI.getOperand(1).isKill(); 1303 unsigned OpLo = AVR::OUTARr; 1304 unsigned OpHi = AVR::OUTARr; 1305 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1306 1307 // Since we add 1 to the Imm value for the high byte below, and 63 is the 1308 // highest Imm value allowed for the instruction, 62 is the limit here. 1309 assert(Imm <= 62 && "Address is out of range"); 1310 1311 // 16 bit I/O writes need the high byte first 1312 auto MIBHI = buildMI(MBB, MBBI, OpHi) 1313 .addImm(Imm + 1) 1314 .addReg(SrcHiReg, getKillRegState(SrcIsKill)); 1315 1316 auto MIBLO = buildMI(MBB, MBBI, OpLo) 1317 .addImm(Imm) 1318 .addReg(SrcLoReg, getKillRegState(SrcIsKill)); 1319 1320 MIBLO.setMemRefs(MI.memoperands()); 1321 MIBHI.setMemRefs(MI.memoperands()); 1322 1323 MI.eraseFromParent(); 1324 return true; 1325 } 1326 1327 template <> 1328 bool AVRExpandPseudo::expand<AVR::PUSHWRr>(Block &MBB, BlockIt MBBI) { 1329 MachineInstr &MI = *MBBI; 1330 Register SrcLoReg, SrcHiReg; 1331 Register SrcReg = MI.getOperand(0).getReg(); 1332 bool SrcIsKill = MI.getOperand(0).isKill(); 1333 unsigned Flags = MI.getFlags(); 1334 unsigned OpLo = AVR::PUSHRr; 1335 unsigned OpHi = AVR::PUSHRr; 1336 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1337 1338 // Low part 1339 buildMI(MBB, MBBI, OpLo) 1340 .addReg(SrcLoReg, getKillRegState(SrcIsKill)) 1341 .setMIFlags(Flags); 1342 1343 // High part 1344 buildMI(MBB, MBBI, OpHi) 1345 .addReg(SrcHiReg, getKillRegState(SrcIsKill)) 1346 .setMIFlags(Flags); 1347 1348 MI.eraseFromParent(); 1349 return true; 1350 } 1351 1352 template <> 1353 bool AVRExpandPseudo::expand<AVR::POPWRd>(Block &MBB, BlockIt MBBI) { 1354 MachineInstr &MI = *MBBI; 1355 Register DstLoReg, DstHiReg; 1356 Register DstReg = MI.getOperand(0).getReg(); 1357 unsigned Flags = MI.getFlags(); 1358 unsigned OpLo = AVR::POPRd; 1359 unsigned OpHi = AVR::POPRd; 1360 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1361 1362 buildMI(MBB, MBBI, OpHi, DstHiReg).setMIFlags(Flags); // High 1363 buildMI(MBB, MBBI, OpLo, DstLoReg).setMIFlags(Flags); // Low 1364 1365 MI.eraseFromParent(); 1366 return true; 1367 } 1368 1369 template <> 1370 bool AVRExpandPseudo::expand<AVR::ROLBRd>(Block &MBB, BlockIt MBBI) { 1371 // In AVR, the rotate instructions behave quite unintuitively. They rotate 1372 // bits through the carry bit in SREG, effectively rotating over 9 bits, 1373 // instead of 8. This is useful when we are dealing with numbers over 1374 // multiple registers, but when we actually need to rotate stuff, we have 1375 // to explicitly add the carry bit. 1376 1377 MachineInstr &MI = *MBBI; 1378 unsigned OpShift, OpCarry; 1379 Register DstReg = MI.getOperand(0).getReg(); 1380 bool DstIsDead = MI.getOperand(0).isDead(); 1381 OpShift = AVR::ADDRdRr; 1382 OpCarry = AVR::ADCRdRr; 1383 1384 // add r16, r16 1385 // adc r16, r1 1386 1387 // Shift part 1388 buildMI(MBB, MBBI, OpShift) 1389 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 1390 .addReg(DstReg) 1391 .addReg(DstReg); 1392 1393 // Add the carry bit 1394 auto MIB = buildMI(MBB, MBBI, OpCarry) 1395 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 1396 .addReg(DstReg) 1397 .addReg(ZERO_REGISTER); 1398 1399 // SREG is always implicitly killed 1400 MIB->getOperand(2).setIsKill(); 1401 1402 MI.eraseFromParent(); 1403 return true; 1404 } 1405 1406 template <> 1407 bool AVRExpandPseudo::expand<AVR::RORBRd>(Block &MBB, BlockIt MBBI) { 1408 // In AVR, the rotate instructions behave quite unintuitively. They rotate 1409 // bits through the carry bit in SREG, effectively rotating over 9 bits, 1410 // instead of 8. This is useful when we are dealing with numbers over 1411 // multiple registers, but when we actually need to rotate stuff, we have 1412 // to explicitly add the carry bit. 1413 1414 MachineInstr &MI = *MBBI; 1415 Register DstReg = MI.getOperand(0).getReg(); 1416 1417 // bst r16, 0 1418 // ror r16 1419 // bld r16, 7 1420 1421 // Move the lowest bit from DstReg into the T bit 1422 buildMI(MBB, MBBI, AVR::BST).addReg(DstReg).addImm(0); 1423 1424 // Rotate to the right 1425 buildMI(MBB, MBBI, AVR::RORRd, DstReg).addReg(DstReg); 1426 1427 // Move the T bit into the highest bit of DstReg. 1428 buildMI(MBB, MBBI, AVR::BLD, DstReg).addReg(DstReg).addImm(7); 1429 1430 MI.eraseFromParent(); 1431 return true; 1432 } 1433 1434 template <> 1435 bool AVRExpandPseudo::expand<AVR::LSLWRd>(Block &MBB, BlockIt MBBI) { 1436 MachineInstr &MI = *MBBI; 1437 Register DstLoReg, DstHiReg; 1438 Register DstReg = MI.getOperand(0).getReg(); 1439 bool DstIsDead = MI.getOperand(0).isDead(); 1440 bool DstIsKill = MI.getOperand(1).isKill(); 1441 bool ImpIsDead = MI.getOperand(2).isDead(); 1442 unsigned OpLo = AVR::ADDRdRr; // ADD Rd, Rd <==> LSL Rd 1443 unsigned OpHi = AVR::ADCRdRr; // ADC Rd, Rd <==> ROL Rd 1444 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1445 1446 // Low part 1447 buildMI(MBB, MBBI, OpLo) 1448 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1449 .addReg(DstLoReg) 1450 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1451 1452 auto MIBHI = 1453 buildMI(MBB, MBBI, OpHi) 1454 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1455 .addReg(DstHiReg) 1456 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1457 1458 if (ImpIsDead) 1459 MIBHI->getOperand(3).setIsDead(); 1460 1461 // SREG is always implicitly killed 1462 MIBHI->getOperand(4).setIsKill(); 1463 1464 MI.eraseFromParent(); 1465 return true; 1466 } 1467 1468 template <> 1469 bool AVRExpandPseudo::expand<AVR::LSLWHiRd>(Block &MBB, BlockIt MBBI) { 1470 MachineInstr &MI = *MBBI; 1471 Register DstLoReg, DstHiReg; 1472 Register DstReg = MI.getOperand(0).getReg(); 1473 bool DstIsDead = MI.getOperand(0).isDead(); 1474 bool DstIsKill = MI.getOperand(1).isKill(); 1475 bool ImpIsDead = MI.getOperand(2).isDead(); 1476 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1477 1478 // add hireg, hireg <==> lsl hireg 1479 auto MILSL = 1480 buildMI(MBB, MBBI, AVR::ADDRdRr) 1481 .addReg(DstHiReg, RegState::Define, getDeadRegState(DstIsDead)) 1482 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1483 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1484 1485 if (ImpIsDead) 1486 MILSL->getOperand(3).setIsDead(); 1487 1488 MI.eraseFromParent(); 1489 return true; 1490 } 1491 1492 bool AVRExpandPseudo::expandLSLW4Rd(Block &MBB, BlockIt MBBI) { 1493 MachineInstr &MI = *MBBI; 1494 Register DstLoReg, DstHiReg; 1495 Register DstReg = MI.getOperand(0).getReg(); 1496 bool DstIsDead = MI.getOperand(0).isDead(); 1497 bool DstIsKill = MI.getOperand(1).isKill(); 1498 bool ImpIsDead = MI.getOperand(3).isDead(); 1499 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1500 1501 // swap Rh 1502 // swap Rl 1503 buildMI(MBB, MBBI, AVR::SWAPRd) 1504 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1505 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1506 buildMI(MBB, MBBI, AVR::SWAPRd) 1507 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1508 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1509 1510 // andi Rh, 0xf0 1511 auto MI0 = 1512 buildMI(MBB, MBBI, AVR::ANDIRdK) 1513 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1514 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1515 .addImm(0xf0); 1516 // SREG is implicitly dead. 1517 MI0->getOperand(3).setIsDead(); 1518 1519 // eor Rh, Rl 1520 auto MI1 = 1521 buildMI(MBB, MBBI, AVR::EORRdRr) 1522 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1523 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1524 .addReg(DstLoReg); 1525 // SREG is implicitly dead. 1526 MI1->getOperand(3).setIsDead(); 1527 1528 // andi Rl, 0xf0 1529 auto MI2 = 1530 buildMI(MBB, MBBI, AVR::ANDIRdK) 1531 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1532 .addReg(DstLoReg, getKillRegState(DstIsKill)) 1533 .addImm(0xf0); 1534 // SREG is implicitly dead. 1535 MI2->getOperand(3).setIsDead(); 1536 1537 // eor Rh, Rl 1538 auto MI3 = 1539 buildMI(MBB, MBBI, AVR::EORRdRr) 1540 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1541 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1542 .addReg(DstLoReg); 1543 if (ImpIsDead) 1544 MI3->getOperand(3).setIsDead(); 1545 1546 MI.eraseFromParent(); 1547 return true; 1548 } 1549 1550 bool AVRExpandPseudo::expandLSLW8Rd(Block &MBB, BlockIt MBBI) { 1551 MachineInstr &MI = *MBBI; 1552 Register DstLoReg, DstHiReg; 1553 Register DstReg = MI.getOperand(0).getReg(); 1554 bool DstIsDead = MI.getOperand(0).isDead(); 1555 bool DstIsKill = MI.getOperand(1).isKill(); 1556 bool ImpIsDead = MI.getOperand(3).isDead(); 1557 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1558 1559 // mov Rh, Rl 1560 buildMI(MBB, MBBI, AVR::MOVRdRr) 1561 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1562 .addReg(DstLoReg); 1563 1564 // clr Rl 1565 auto MIBLO = 1566 buildMI(MBB, MBBI, AVR::EORRdRr) 1567 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1568 .addReg(DstLoReg, getKillRegState(DstIsKill)) 1569 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1570 if (ImpIsDead) 1571 MIBLO->getOperand(3).setIsDead(); 1572 1573 MI.eraseFromParent(); 1574 return true; 1575 } 1576 1577 bool AVRExpandPseudo::expandLSLW12Rd(Block &MBB, BlockIt MBBI) { 1578 MachineInstr &MI = *MBBI; 1579 Register DstLoReg, DstHiReg; 1580 Register DstReg = MI.getOperand(0).getReg(); 1581 bool DstIsDead = MI.getOperand(0).isDead(); 1582 bool DstIsKill = MI.getOperand(1).isKill(); 1583 bool ImpIsDead = MI.getOperand(3).isDead(); 1584 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1585 1586 // mov Rh, Rl 1587 buildMI(MBB, MBBI, AVR::MOVRdRr) 1588 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1589 .addReg(DstLoReg); 1590 1591 // swap Rh 1592 buildMI(MBB, MBBI, AVR::SWAPRd) 1593 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1594 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1595 1596 // andi Rh, 0xf0 1597 auto MI0 = 1598 buildMI(MBB, MBBI, AVR::ANDIRdK) 1599 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1600 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1601 .addImm(0xf0); 1602 // SREG is implicitly dead. 1603 MI0->getOperand(3).setIsDead(); 1604 1605 // clr Rl 1606 auto MI1 = 1607 buildMI(MBB, MBBI, AVR::EORRdRr) 1608 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1609 .addReg(DstLoReg, getKillRegState(DstIsKill)) 1610 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1611 if (ImpIsDead) 1612 MI1->getOperand(3).setIsDead(); 1613 1614 MI.eraseFromParent(); 1615 return true; 1616 } 1617 1618 template <> 1619 bool AVRExpandPseudo::expand<AVR::LSLWNRd>(Block &MBB, BlockIt MBBI) { 1620 MachineInstr &MI = *MBBI; 1621 unsigned Imm = MI.getOperand(2).getImm(); 1622 switch (Imm) { 1623 case 4: 1624 return expandLSLW4Rd(MBB, MBBI); 1625 case 8: 1626 return expandLSLW8Rd(MBB, MBBI); 1627 case 12: 1628 return expandLSLW12Rd(MBB, MBBI); 1629 default: 1630 llvm_unreachable("unimplemented lslwn"); 1631 return false; 1632 } 1633 } 1634 1635 template <> 1636 bool AVRExpandPseudo::expand<AVR::LSRWRd>(Block &MBB, BlockIt MBBI) { 1637 MachineInstr &MI = *MBBI; 1638 Register DstLoReg, DstHiReg; 1639 Register DstReg = MI.getOperand(0).getReg(); 1640 bool DstIsDead = MI.getOperand(0).isDead(); 1641 bool DstIsKill = MI.getOperand(1).isKill(); 1642 bool ImpIsDead = MI.getOperand(2).isDead(); 1643 unsigned OpLo = AVR::RORRd; 1644 unsigned OpHi = AVR::LSRRd; 1645 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1646 1647 // High part 1648 buildMI(MBB, MBBI, OpHi) 1649 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1650 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1651 1652 auto MIBLO = 1653 buildMI(MBB, MBBI, OpLo) 1654 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1655 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1656 1657 if (ImpIsDead) 1658 MIBLO->getOperand(2).setIsDead(); 1659 1660 // SREG is always implicitly killed 1661 MIBLO->getOperand(3).setIsKill(); 1662 1663 MI.eraseFromParent(); 1664 return true; 1665 } 1666 1667 template <> 1668 bool AVRExpandPseudo::expand<AVR::LSRWLoRd>(Block &MBB, BlockIt MBBI) { 1669 MachineInstr &MI = *MBBI; 1670 Register DstLoReg, DstHiReg; 1671 Register DstReg = MI.getOperand(0).getReg(); 1672 bool DstIsDead = MI.getOperand(0).isDead(); 1673 bool DstIsKill = MI.getOperand(1).isKill(); 1674 bool ImpIsDead = MI.getOperand(2).isDead(); 1675 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1676 1677 // lsr loreg 1678 auto MILSR = 1679 buildMI(MBB, MBBI, AVR::LSRRd) 1680 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1681 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1682 1683 if (ImpIsDead) 1684 MILSR->getOperand(2).setIsDead(); 1685 1686 MI.eraseFromParent(); 1687 return true; 1688 } 1689 1690 bool AVRExpandPseudo::expandLSRW4Rd(Block &MBB, BlockIt MBBI) { 1691 MachineInstr &MI = *MBBI; 1692 Register DstLoReg, DstHiReg; 1693 Register DstReg = MI.getOperand(0).getReg(); 1694 bool DstIsDead = MI.getOperand(0).isDead(); 1695 bool DstIsKill = MI.getOperand(1).isKill(); 1696 bool ImpIsDead = MI.getOperand(3).isDead(); 1697 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1698 1699 // swap Rh 1700 // swap Rl 1701 buildMI(MBB, MBBI, AVR::SWAPRd) 1702 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1703 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1704 buildMI(MBB, MBBI, AVR::SWAPRd) 1705 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1706 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1707 1708 // andi Rl, 0xf 1709 auto MI0 = 1710 buildMI(MBB, MBBI, AVR::ANDIRdK) 1711 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1712 .addReg(DstLoReg, getKillRegState(DstIsKill)) 1713 .addImm(0xf); 1714 // SREG is implicitly dead. 1715 MI0->getOperand(3).setIsDead(); 1716 1717 // eor Rl, Rh 1718 auto MI1 = 1719 buildMI(MBB, MBBI, AVR::EORRdRr) 1720 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1721 .addReg(DstLoReg, getKillRegState(DstIsKill)) 1722 .addReg(DstHiReg); 1723 // SREG is implicitly dead. 1724 MI1->getOperand(3).setIsDead(); 1725 1726 // andi Rh, 0xf 1727 auto MI2 = 1728 buildMI(MBB, MBBI, AVR::ANDIRdK) 1729 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1730 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1731 .addImm(0xf); 1732 // SREG is implicitly dead. 1733 MI2->getOperand(3).setIsDead(); 1734 1735 // eor Rl, Rh 1736 auto MI3 = 1737 buildMI(MBB, MBBI, AVR::EORRdRr) 1738 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1739 .addReg(DstLoReg, getKillRegState(DstIsKill)) 1740 .addReg(DstHiReg); 1741 if (ImpIsDead) 1742 MI3->getOperand(3).setIsDead(); 1743 1744 MI.eraseFromParent(); 1745 return true; 1746 } 1747 1748 bool AVRExpandPseudo::expandLSRW8Rd(Block &MBB, BlockIt MBBI) { 1749 MachineInstr &MI = *MBBI; 1750 Register DstLoReg, DstHiReg; 1751 Register DstReg = MI.getOperand(0).getReg(); 1752 bool DstIsDead = MI.getOperand(0).isDead(); 1753 bool DstIsKill = MI.getOperand(1).isKill(); 1754 bool ImpIsDead = MI.getOperand(3).isDead(); 1755 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1756 1757 // Move upper byte to lower byte. 1758 buildMI(MBB, MBBI, AVR::MOVRdRr) 1759 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1760 .addReg(DstHiReg); 1761 1762 // Clear upper byte. 1763 auto MIBHI = 1764 buildMI(MBB, MBBI, AVR::EORRdRr) 1765 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1766 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1767 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1768 if (ImpIsDead) 1769 MIBHI->getOperand(3).setIsDead(); 1770 1771 MI.eraseFromParent(); 1772 return true; 1773 } 1774 1775 bool AVRExpandPseudo::expandLSRW12Rd(Block &MBB, BlockIt MBBI) { 1776 MachineInstr &MI = *MBBI; 1777 Register DstLoReg, DstHiReg; 1778 Register DstReg = MI.getOperand(0).getReg(); 1779 bool DstIsDead = MI.getOperand(0).isDead(); 1780 bool DstIsKill = MI.getOperand(1).isKill(); 1781 bool ImpIsDead = MI.getOperand(3).isDead(); 1782 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1783 1784 // Move upper byte to lower byte. 1785 buildMI(MBB, MBBI, AVR::MOVRdRr) 1786 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1787 .addReg(DstHiReg); 1788 1789 // swap Rl 1790 buildMI(MBB, MBBI, AVR::SWAPRd) 1791 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1792 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1793 1794 // andi Rl, 0xf 1795 auto MI0 = 1796 buildMI(MBB, MBBI, AVR::ANDIRdK) 1797 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1798 .addReg(DstLoReg, getKillRegState(DstIsKill)) 1799 .addImm(0xf); 1800 // SREG is implicitly dead. 1801 MI0->getOperand(3).setIsDead(); 1802 1803 // Clear upper byte. 1804 auto MIBHI = 1805 buildMI(MBB, MBBI, AVR::EORRdRr) 1806 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1807 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1808 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1809 if (ImpIsDead) 1810 MIBHI->getOperand(3).setIsDead(); 1811 1812 MI.eraseFromParent(); 1813 return true; 1814 } 1815 1816 template <> 1817 bool AVRExpandPseudo::expand<AVR::LSRWNRd>(Block &MBB, BlockIt MBBI) { 1818 MachineInstr &MI = *MBBI; 1819 unsigned Imm = MI.getOperand(2).getImm(); 1820 switch (Imm) { 1821 case 4: 1822 return expandLSRW4Rd(MBB, MBBI); 1823 case 8: 1824 return expandLSRW8Rd(MBB, MBBI); 1825 case 12: 1826 return expandLSRW12Rd(MBB, MBBI); 1827 default: 1828 llvm_unreachable("unimplemented lsrwn"); 1829 return false; 1830 } 1831 } 1832 1833 template <> 1834 bool AVRExpandPseudo::expand<AVR::RORWRd>(Block &MBB, BlockIt MBBI) { 1835 llvm_unreachable("RORW unimplemented"); 1836 return false; 1837 } 1838 1839 template <> 1840 bool AVRExpandPseudo::expand<AVR::ROLWRd>(Block &MBB, BlockIt MBBI) { 1841 llvm_unreachable("ROLW unimplemented"); 1842 return false; 1843 } 1844 1845 template <> 1846 bool AVRExpandPseudo::expand<AVR::ASRWRd>(Block &MBB, BlockIt MBBI) { 1847 MachineInstr &MI = *MBBI; 1848 Register DstLoReg, DstHiReg; 1849 Register DstReg = MI.getOperand(0).getReg(); 1850 bool DstIsDead = MI.getOperand(0).isDead(); 1851 bool DstIsKill = MI.getOperand(1).isKill(); 1852 bool ImpIsDead = MI.getOperand(2).isDead(); 1853 unsigned OpLo = AVR::RORRd; 1854 unsigned OpHi = AVR::ASRRd; 1855 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1856 1857 // High part 1858 buildMI(MBB, MBBI, OpHi) 1859 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1860 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1861 1862 auto MIBLO = 1863 buildMI(MBB, MBBI, OpLo) 1864 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1865 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1866 1867 if (ImpIsDead) 1868 MIBLO->getOperand(2).setIsDead(); 1869 1870 // SREG is always implicitly killed 1871 MIBLO->getOperand(3).setIsKill(); 1872 1873 MI.eraseFromParent(); 1874 return true; 1875 } 1876 1877 template <> 1878 bool AVRExpandPseudo::expand<AVR::ASRWLoRd>(Block &MBB, BlockIt MBBI) { 1879 MachineInstr &MI = *MBBI; 1880 Register DstLoReg, DstHiReg; 1881 Register DstReg = MI.getOperand(0).getReg(); 1882 bool DstIsDead = MI.getOperand(0).isDead(); 1883 bool DstIsKill = MI.getOperand(1).isKill(); 1884 bool ImpIsDead = MI.getOperand(2).isDead(); 1885 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1886 1887 // asr loreg 1888 auto MIASR = 1889 buildMI(MBB, MBBI, AVR::ASRRd) 1890 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1891 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1892 1893 if (ImpIsDead) 1894 MIASR->getOperand(2).setIsDead(); 1895 1896 MI.eraseFromParent(); 1897 return true; 1898 } 1899 1900 bool AVRExpandPseudo::expandASRW8Rd(Block &MBB, BlockIt MBBI) { 1901 MachineInstr &MI = *MBBI; 1902 Register DstLoReg, DstHiReg; 1903 Register DstReg = MI.getOperand(0).getReg(); 1904 bool DstIsDead = MI.getOperand(0).isDead(); 1905 bool DstIsKill = MI.getOperand(1).isKill(); 1906 bool ImpIsDead = MI.getOperand(3).isDead(); 1907 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1908 1909 // Move upper byte to lower byte. 1910 buildMI(MBB, MBBI, AVR::MOVRdRr) 1911 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1912 .addReg(DstHiReg); 1913 1914 // Move the sign bit to the C flag. 1915 buildMI(MBB, MBBI, AVR::ADDRdRr) 1916 .addReg(DstHiReg, RegState::Define, getDeadRegState(DstIsDead)) 1917 .addReg(DstHiReg, getKillRegState(DstIsKill) | getDeadRegState(DstIsDead)) 1918 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1919 1920 // Set upper byte to 0 or -1. 1921 auto MIBHI = 1922 buildMI(MBB, MBBI, AVR::SBCRdRr) 1923 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1924 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1925 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1926 if (ImpIsDead) 1927 MIBHI->getOperand(3).setIsDead(); 1928 1929 MI.eraseFromParent(); 1930 return true; 1931 } 1932 1933 template <> 1934 bool AVRExpandPseudo::expand<AVR::ASRWNRd>(Block &MBB, BlockIt MBBI) { 1935 MachineInstr &MI = *MBBI; 1936 unsigned Imm = MI.getOperand(2).getImm(); 1937 switch (Imm) { 1938 case 8: 1939 return expandASRW8Rd(MBB, MBBI); 1940 default: 1941 llvm_unreachable("unimplemented asrwn"); 1942 return false; 1943 } 1944 } 1945 1946 bool AVRExpandPseudo::expandLSLB7Rd(Block &MBB, BlockIt MBBI) { 1947 MachineInstr &MI = *MBBI; 1948 Register DstReg = MI.getOperand(0).getReg(); 1949 bool DstIsDead = MI.getOperand(0).isDead(); 1950 bool DstIsKill = MI.getOperand(1).isKill(); 1951 bool ImpIsDead = MI.getOperand(3).isDead(); 1952 1953 // ror r24 1954 // clr r24 1955 // ror r24 1956 1957 buildMI(MBB, MBBI, AVR::RORRd) 1958 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 1959 .addReg(DstReg, getKillRegState(DstIsKill)) 1960 ->getOperand(3) 1961 .setIsUndef(true); 1962 1963 buildMI(MBB, MBBI, AVR::EORRdRr) 1964 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 1965 .addReg(DstReg, getKillRegState(DstIsKill)) 1966 .addReg(DstReg, getKillRegState(DstIsKill)); 1967 1968 auto MIRRC = 1969 buildMI(MBB, MBBI, AVR::RORRd) 1970 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 1971 .addReg(DstReg, getKillRegState(DstIsKill)); 1972 1973 if (ImpIsDead) 1974 MIRRC->getOperand(2).setIsDead(); 1975 1976 // SREG is always implicitly killed 1977 MIRRC->getOperand(3).setIsKill(); 1978 1979 MI.eraseFromParent(); 1980 return true; 1981 } 1982 1983 template <> 1984 bool AVRExpandPseudo::expand<AVR::LSLBNRd>(Block &MBB, BlockIt MBBI) { 1985 MachineInstr &MI = *MBBI; 1986 unsigned Imm = MI.getOperand(2).getImm(); 1987 switch (Imm) { 1988 case 7: 1989 return expandLSLB7Rd(MBB, MBBI); 1990 default: 1991 llvm_unreachable("unimplemented lslbn"); 1992 return false; 1993 } 1994 } 1995 1996 bool AVRExpandPseudo::expandLSRB7Rd(Block &MBB, BlockIt MBBI) { 1997 MachineInstr &MI = *MBBI; 1998 Register DstReg = MI.getOperand(0).getReg(); 1999 bool DstIsDead = MI.getOperand(0).isDead(); 2000 bool DstIsKill = MI.getOperand(1).isKill(); 2001 bool ImpIsDead = MI.getOperand(3).isDead(); 2002 2003 // rol r24 2004 // clr r24 2005 // rol r24 2006 2007 buildMI(MBB, MBBI, AVR::ADCRdRr) 2008 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2009 .addReg(DstReg, getKillRegState(DstIsKill)) 2010 .addReg(DstReg, getKillRegState(DstIsKill)) 2011 ->getOperand(4) 2012 .setIsUndef(true); 2013 2014 buildMI(MBB, MBBI, AVR::EORRdRr) 2015 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2016 .addReg(DstReg, getKillRegState(DstIsKill)) 2017 .addReg(DstReg, getKillRegState(DstIsKill)); 2018 2019 auto MIRRC = 2020 buildMI(MBB, MBBI, AVR::ADCRdRr) 2021 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2022 .addReg(DstReg, getKillRegState(DstIsKill)) 2023 .addReg(DstReg, getKillRegState(DstIsKill)); 2024 2025 if (ImpIsDead) 2026 MIRRC->getOperand(3).setIsDead(); 2027 2028 // SREG is always implicitly killed 2029 MIRRC->getOperand(4).setIsKill(); 2030 2031 MI.eraseFromParent(); 2032 return true; 2033 } 2034 2035 template <> 2036 bool AVRExpandPseudo::expand<AVR::LSRBNRd>(Block &MBB, BlockIt MBBI) { 2037 MachineInstr &MI = *MBBI; 2038 unsigned Imm = MI.getOperand(2).getImm(); 2039 switch (Imm) { 2040 case 7: 2041 return expandLSRB7Rd(MBB, MBBI); 2042 default: 2043 llvm_unreachable("unimplemented lsrbn"); 2044 return false; 2045 } 2046 } 2047 2048 bool AVRExpandPseudo::expandASRB6Rd(Block &MBB, BlockIt MBBI) { 2049 MachineInstr &MI = *MBBI; 2050 Register DstReg = MI.getOperand(0).getReg(); 2051 bool DstIsDead = MI.getOperand(0).isDead(); 2052 bool DstIsKill = MI.getOperand(1).isKill(); 2053 2054 // bst r24, 6 2055 // lsl r24 2056 // sbc r24, r24 2057 // bld r24, 0 2058 2059 buildMI(MBB, MBBI, AVR::BST) 2060 .addReg(DstReg) 2061 .addImm(6) 2062 ->getOperand(2) 2063 .setIsUndef(true); 2064 2065 buildMI(MBB, MBBI, AVR::ADDRdRr) // LSL Rd <==> ADD Rd, Rd 2066 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2067 .addReg(DstReg, getKillRegState(DstIsKill)) 2068 .addReg(DstReg, getKillRegState(DstIsKill)); 2069 2070 buildMI(MBB, MBBI, AVR::SBCRdRr) 2071 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2072 .addReg(DstReg, getKillRegState(DstIsKill)) 2073 .addReg(DstReg, getKillRegState(DstIsKill)); 2074 2075 buildMI(MBB, MBBI, AVR::BLD) 2076 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2077 .addReg(DstReg, getKillRegState(DstIsKill)) 2078 .addImm(0) 2079 ->getOperand(3) 2080 .setIsKill(); 2081 2082 MI.eraseFromParent(); 2083 return true; 2084 } 2085 2086 bool AVRExpandPseudo::expandASRB7Rd(Block &MBB, BlockIt MBBI) { 2087 MachineInstr &MI = *MBBI; 2088 Register DstReg = MI.getOperand(0).getReg(); 2089 bool DstIsDead = MI.getOperand(0).isDead(); 2090 bool DstIsKill = MI.getOperand(1).isKill(); 2091 bool ImpIsDead = MI.getOperand(3).isDead(); 2092 2093 // lsl r24 2094 // sbc r24, r24 2095 2096 buildMI(MBB, MBBI, AVR::ADDRdRr) 2097 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2098 .addReg(DstReg, getKillRegState(DstIsKill)) 2099 .addReg(DstReg, getKillRegState(DstIsKill)); 2100 2101 auto MIRRC = 2102 buildMI(MBB, MBBI, AVR::SBCRdRr) 2103 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2104 .addReg(DstReg, getKillRegState(DstIsKill)) 2105 .addReg(DstReg, getKillRegState(DstIsKill)); 2106 2107 if (ImpIsDead) 2108 MIRRC->getOperand(3).setIsDead(); 2109 2110 // SREG is always implicitly killed 2111 MIRRC->getOperand(4).setIsKill(); 2112 2113 MI.eraseFromParent(); 2114 return true; 2115 } 2116 2117 template <> 2118 bool AVRExpandPseudo::expand<AVR::ASRBNRd>(Block &MBB, BlockIt MBBI) { 2119 MachineInstr &MI = *MBBI; 2120 unsigned Imm = MI.getOperand(2).getImm(); 2121 switch (Imm) { 2122 case 6: 2123 return expandASRB6Rd(MBB, MBBI); 2124 case 7: 2125 return expandASRB7Rd(MBB, MBBI); 2126 default: 2127 llvm_unreachable("unimplemented asrbn"); 2128 return false; 2129 } 2130 } 2131 2132 template <> bool AVRExpandPseudo::expand<AVR::SEXT>(Block &MBB, BlockIt MBBI) { 2133 MachineInstr &MI = *MBBI; 2134 Register DstLoReg, DstHiReg; 2135 // sext R17:R16, R17 2136 // mov r16, r17 2137 // lsl r17 2138 // sbc r17, r17 2139 // sext R17:R16, R13 2140 // mov r16, r13 2141 // mov r17, r13 2142 // lsl r17 2143 // sbc r17, r17 2144 // sext R17:R16, R16 2145 // mov r17, r16 2146 // lsl r17 2147 // sbc r17, r17 2148 Register DstReg = MI.getOperand(0).getReg(); 2149 Register SrcReg = MI.getOperand(1).getReg(); 2150 bool DstIsDead = MI.getOperand(0).isDead(); 2151 bool SrcIsKill = MI.getOperand(1).isKill(); 2152 bool ImpIsDead = MI.getOperand(2).isDead(); 2153 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 2154 2155 if (SrcReg != DstLoReg) { 2156 auto MOV = 2157 buildMI(MBB, MBBI, AVR::MOVRdRr) 2158 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 2159 .addReg(SrcReg); 2160 2161 if (SrcReg == DstHiReg) { 2162 MOV->getOperand(1).setIsKill(); 2163 } 2164 } 2165 2166 if (SrcReg != DstHiReg) { 2167 buildMI(MBB, MBBI, AVR::MOVRdRr) 2168 .addReg(DstHiReg, RegState::Define) 2169 .addReg(SrcReg, getKillRegState(SrcIsKill)); 2170 } 2171 2172 buildMI(MBB, MBBI, AVR::ADDRdRr) // LSL Rd <==> ADD Rd, Rr 2173 .addReg(DstHiReg, RegState::Define) 2174 .addReg(DstHiReg) 2175 .addReg(DstHiReg, RegState::Kill); 2176 2177 auto SBC = 2178 buildMI(MBB, MBBI, AVR::SBCRdRr) 2179 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 2180 .addReg(DstHiReg, RegState::Kill) 2181 .addReg(DstHiReg, RegState::Kill); 2182 2183 if (ImpIsDead) 2184 SBC->getOperand(3).setIsDead(); 2185 2186 // SREG is always implicitly killed 2187 SBC->getOperand(4).setIsKill(); 2188 2189 MI.eraseFromParent(); 2190 return true; 2191 } 2192 2193 template <> bool AVRExpandPseudo::expand<AVR::ZEXT>(Block &MBB, BlockIt MBBI) { 2194 MachineInstr &MI = *MBBI; 2195 Register DstLoReg, DstHiReg; 2196 // zext R25:R24, R20 2197 // mov R24, R20 2198 // eor R25, R25 2199 // zext R25:R24, R24 2200 // eor R25, R25 2201 // zext R25:R24, R25 2202 // mov R24, R25 2203 // eor R25, R25 2204 Register DstReg = MI.getOperand(0).getReg(); 2205 Register SrcReg = MI.getOperand(1).getReg(); 2206 bool DstIsDead = MI.getOperand(0).isDead(); 2207 bool SrcIsKill = MI.getOperand(1).isKill(); 2208 bool ImpIsDead = MI.getOperand(2).isDead(); 2209 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 2210 2211 if (SrcReg != DstLoReg) { 2212 buildMI(MBB, MBBI, AVR::MOVRdRr) 2213 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 2214 .addReg(SrcReg, getKillRegState(SrcIsKill)); 2215 } 2216 2217 auto EOR = 2218 buildMI(MBB, MBBI, AVR::EORRdRr) 2219 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 2220 .addReg(DstHiReg, RegState::Kill | RegState::Undef) 2221 .addReg(DstHiReg, RegState::Kill | RegState::Undef); 2222 2223 if (ImpIsDead) 2224 EOR->getOperand(3).setIsDead(); 2225 2226 MI.eraseFromParent(); 2227 return true; 2228 } 2229 2230 template <> 2231 bool AVRExpandPseudo::expand<AVR::SPREAD>(Block &MBB, BlockIt MBBI) { 2232 MachineInstr &MI = *MBBI; 2233 Register DstLoReg, DstHiReg; 2234 Register DstReg = MI.getOperand(0).getReg(); 2235 bool DstIsDead = MI.getOperand(0).isDead(); 2236 unsigned Flags = MI.getFlags(); 2237 unsigned OpLo = AVR::INRdA; 2238 unsigned OpHi = AVR::INRdA; 2239 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 2240 2241 // Low part 2242 buildMI(MBB, MBBI, OpLo) 2243 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 2244 .addImm(0x3d) 2245 .setMIFlags(Flags); 2246 2247 // High part 2248 buildMI(MBB, MBBI, OpHi) 2249 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 2250 .addImm(0x3e) 2251 .setMIFlags(Flags); 2252 2253 MI.eraseFromParent(); 2254 return true; 2255 } 2256 2257 template <> 2258 bool AVRExpandPseudo::expand<AVR::SPWRITE>(Block &MBB, BlockIt MBBI) { 2259 MachineInstr &MI = *MBBI; 2260 Register SrcLoReg, SrcHiReg; 2261 Register SrcReg = MI.getOperand(1).getReg(); 2262 bool SrcIsKill = MI.getOperand(1).isKill(); 2263 unsigned Flags = MI.getFlags(); 2264 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 2265 2266 buildMI(MBB, MBBI, AVR::INRdA) 2267 .addReg(AVR::R0, RegState::Define) 2268 .addImm(SREG_ADDR) 2269 .setMIFlags(Flags); 2270 2271 buildMI(MBB, MBBI, AVR::BCLRs).addImm(0x07).setMIFlags(Flags); 2272 2273 buildMI(MBB, MBBI, AVR::OUTARr) 2274 .addImm(0x3e) 2275 .addReg(SrcHiReg, getKillRegState(SrcIsKill)) 2276 .setMIFlags(Flags); 2277 2278 buildMI(MBB, MBBI, AVR::OUTARr) 2279 .addImm(SREG_ADDR) 2280 .addReg(AVR::R0, RegState::Kill) 2281 .setMIFlags(Flags); 2282 2283 buildMI(MBB, MBBI, AVR::OUTARr) 2284 .addImm(0x3d) 2285 .addReg(SrcLoReg, getKillRegState(SrcIsKill)) 2286 .setMIFlags(Flags); 2287 2288 MI.eraseFromParent(); 2289 return true; 2290 } 2291 2292 bool AVRExpandPseudo::expandMI(Block &MBB, BlockIt MBBI) { 2293 MachineInstr &MI = *MBBI; 2294 int Opcode = MBBI->getOpcode(); 2295 2296 #define EXPAND(Op) \ 2297 case Op: \ 2298 return expand<Op>(MBB, MI) 2299 2300 switch (Opcode) { 2301 EXPAND(AVR::ADDWRdRr); 2302 EXPAND(AVR::ADCWRdRr); 2303 EXPAND(AVR::SUBWRdRr); 2304 EXPAND(AVR::SUBIWRdK); 2305 EXPAND(AVR::SBCWRdRr); 2306 EXPAND(AVR::SBCIWRdK); 2307 EXPAND(AVR::ANDWRdRr); 2308 EXPAND(AVR::ANDIWRdK); 2309 EXPAND(AVR::ORWRdRr); 2310 EXPAND(AVR::ORIWRdK); 2311 EXPAND(AVR::EORWRdRr); 2312 EXPAND(AVR::COMWRd); 2313 EXPAND(AVR::NEGWRd); 2314 EXPAND(AVR::CPWRdRr); 2315 EXPAND(AVR::CPCWRdRr); 2316 EXPAND(AVR::LDIWRdK); 2317 EXPAND(AVR::LDSWRdK); 2318 EXPAND(AVR::LDWRdPtr); 2319 EXPAND(AVR::LDWRdPtrPi); 2320 EXPAND(AVR::LDWRdPtrPd); 2321 case AVR::LDDWRdYQ: //: FIXME: remove this once PR13375 gets fixed 2322 EXPAND(AVR::LDDWRdPtrQ); 2323 EXPAND(AVR::LPMWRdZ); 2324 EXPAND(AVR::LPMWRdZPi); 2325 EXPAND(AVR::ELPMBRdZ); 2326 EXPAND(AVR::ELPMWRdZ); 2327 EXPAND(AVR::ELPMBRdZPi); 2328 EXPAND(AVR::ELPMWRdZPi); 2329 EXPAND(AVR::AtomicLoad8); 2330 EXPAND(AVR::AtomicLoad16); 2331 EXPAND(AVR::AtomicStore8); 2332 EXPAND(AVR::AtomicStore16); 2333 EXPAND(AVR::AtomicLoadAdd8); 2334 EXPAND(AVR::AtomicLoadAdd16); 2335 EXPAND(AVR::AtomicLoadSub8); 2336 EXPAND(AVR::AtomicLoadSub16); 2337 EXPAND(AVR::AtomicLoadAnd8); 2338 EXPAND(AVR::AtomicLoadAnd16); 2339 EXPAND(AVR::AtomicLoadOr8); 2340 EXPAND(AVR::AtomicLoadOr16); 2341 EXPAND(AVR::AtomicLoadXor8); 2342 EXPAND(AVR::AtomicLoadXor16); 2343 EXPAND(AVR::AtomicFence); 2344 EXPAND(AVR::STSWKRr); 2345 EXPAND(AVR::STWPtrRr); 2346 EXPAND(AVR::STWPtrPiRr); 2347 EXPAND(AVR::STWPtrPdRr); 2348 EXPAND(AVR::STDWPtrQRr); 2349 EXPAND(AVR::INWRdA); 2350 EXPAND(AVR::OUTWARr); 2351 EXPAND(AVR::PUSHWRr); 2352 EXPAND(AVR::POPWRd); 2353 EXPAND(AVR::ROLBRd); 2354 EXPAND(AVR::RORBRd); 2355 EXPAND(AVR::LSLWRd); 2356 EXPAND(AVR::LSRWRd); 2357 EXPAND(AVR::RORWRd); 2358 EXPAND(AVR::ROLWRd); 2359 EXPAND(AVR::ASRWRd); 2360 EXPAND(AVR::LSLWHiRd); 2361 EXPAND(AVR::LSRWLoRd); 2362 EXPAND(AVR::ASRWLoRd); 2363 EXPAND(AVR::LSLWNRd); 2364 EXPAND(AVR::LSRWNRd); 2365 EXPAND(AVR::ASRWNRd); 2366 EXPAND(AVR::LSLBNRd); 2367 EXPAND(AVR::LSRBNRd); 2368 EXPAND(AVR::ASRBNRd); 2369 EXPAND(AVR::SEXT); 2370 EXPAND(AVR::ZEXT); 2371 EXPAND(AVR::SPREAD); 2372 EXPAND(AVR::SPWRITE); 2373 } 2374 #undef EXPAND 2375 return false; 2376 } 2377 2378 } // end of anonymous namespace 2379 2380 INITIALIZE_PASS(AVRExpandPseudo, "avr-expand-pseudo", AVR_EXPAND_PSEUDO_NAME, 2381 false, false) 2382 namespace llvm { 2383 2384 FunctionPass *createAVRExpandPseudoPass() { return new AVRExpandPseudo(); } 2385 2386 } // end of namespace llvm 2387