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 58 bool expandMBB(Block &MBB); 59 bool expandMI(Block &MBB, BlockIt MBBI); 60 template <unsigned OP> bool expand(Block &MBB, BlockIt MBBI); 61 62 MachineInstrBuilder buildMI(Block &MBB, BlockIt MBBI, unsigned Opcode) { 63 return BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(Opcode)); 64 } 65 66 MachineInstrBuilder buildMI(Block &MBB, BlockIt MBBI, unsigned Opcode, 67 Register DstReg) { 68 return BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(Opcode), DstReg); 69 } 70 71 MachineRegisterInfo &getRegInfo(Block &MBB) { 72 return MBB.getParent()->getRegInfo(); 73 } 74 75 bool expandArith(unsigned OpLo, unsigned OpHi, Block &MBB, BlockIt MBBI); 76 bool expandLogic(unsigned Op, Block &MBB, BlockIt MBBI); 77 bool expandLogicImm(unsigned Op, Block &MBB, BlockIt MBBI); 78 bool isLogicImmOpRedundant(unsigned Op, unsigned ImmVal) const; 79 80 template <typename Func> 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 /// Specific shift implementation for int8. 88 bool expandLSLB7Rd(Block &MBB, BlockIt MBBI); 89 bool expandLSRB7Rd(Block &MBB, BlockIt MBBI); 90 bool expandASRB6Rd(Block &MBB, BlockIt MBBI); 91 bool expandASRB7Rd(Block &MBB, BlockIt MBBI); 92 93 /// Specific shift implementation for int16. 94 bool expandLSLW4Rd(Block &MBB, BlockIt MBBI); 95 bool expandLSRW4Rd(Block &MBB, BlockIt MBBI); 96 bool expandASRW7Rd(Block &MBB, BlockIt MBBI); 97 bool expandLSLW8Rd(Block &MBB, BlockIt MBBI); 98 bool expandLSRW8Rd(Block &MBB, BlockIt MBBI); 99 bool expandASRW8Rd(Block &MBB, BlockIt MBBI); 100 bool expandLSLW12Rd(Block &MBB, BlockIt MBBI); 101 bool expandLSRW12Rd(Block &MBB, BlockIt MBBI); 102 bool expandASRW14Rd(Block &MBB, BlockIt MBBI); 103 bool expandASRW15Rd(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, RegState::Kill); 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 MachineInstr &MI = *MBBI; 921 const AVRSubtarget &STI = MBB.getParent()->getSubtarget<AVRSubtarget>(); 922 923 // Store the SREG. 924 buildMI(MBB, MBBI, AVR::INRdA) 925 .addReg(SCRATCH_REGISTER, RegState::Define) 926 .addImm(STI.getIORegSREG()); 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) 935 .addImm(STI.getIORegSREG()) 936 .addReg(SCRATCH_REGISTER); 937 938 MI.eraseFromParent(); 939 return true; 940 } 941 942 template <typename Func> 943 bool AVRExpandPseudo::expandAtomicBinaryOp(unsigned Opcode, Block &MBB, 944 BlockIt MBBI, Func f) { 945 return expandAtomic(MBB, MBBI, [&](MachineInstr &MI) { 946 auto Op1 = MI.getOperand(0); 947 auto Op2 = MI.getOperand(1); 948 949 MachineInstr &NewInst = 950 *buildMI(MBB, MBBI, Opcode).add(Op1).add(Op2).getInstr(); 951 f(NewInst); 952 }); 953 } 954 955 bool AVRExpandPseudo::expandAtomicBinaryOp(unsigned Opcode, Block &MBB, 956 BlockIt MBBI) { 957 return expandAtomicBinaryOp(Opcode, MBB, MBBI, [](MachineInstr &MI) {}); 958 } 959 960 Register AVRExpandPseudo::scavengeGPR8(MachineInstr &MI) { 961 MachineBasicBlock &MBB = *MI.getParent(); 962 RegScavenger RS; 963 964 RS.enterBasicBlock(MBB); 965 RS.forward(MI); 966 967 BitVector Candidates = 968 TRI->getAllocatableSet(*MBB.getParent(), &AVR::GPR8RegClass); 969 970 // Exclude all the registers being used by the instruction. 971 for (MachineOperand &MO : MI.operands()) { 972 if (MO.isReg() && MO.getReg() != 0 && !MO.isDef() && 973 !Register::isVirtualRegister(MO.getReg())) 974 Candidates.reset(MO.getReg()); 975 } 976 977 BitVector Available = RS.getRegsAvailable(&AVR::GPR8RegClass); 978 Available &= Candidates; 979 980 signed Reg = Available.find_first(); 981 assert(Reg != -1 && "ran out of registers"); 982 return Reg; 983 } 984 985 template <> 986 bool AVRExpandPseudo::expand<AVR::AtomicLoad8>(Block &MBB, BlockIt MBBI) { 987 return expandAtomicBinaryOp(AVR::LDRdPtr, MBB, MBBI); 988 } 989 990 template <> 991 bool AVRExpandPseudo::expand<AVR::AtomicLoad16>(Block &MBB, BlockIt MBBI) { 992 return expandAtomicBinaryOp(AVR::LDWRdPtr, MBB, MBBI); 993 } 994 995 template <> 996 bool AVRExpandPseudo::expand<AVR::AtomicStore8>(Block &MBB, BlockIt MBBI) { 997 return expandAtomicBinaryOp(AVR::STPtrRr, MBB, MBBI); 998 } 999 1000 template <> 1001 bool AVRExpandPseudo::expand<AVR::AtomicStore16>(Block &MBB, BlockIt MBBI) { 1002 return expandAtomicBinaryOp(AVR::STWPtrRr, MBB, MBBI); 1003 } 1004 1005 template <> 1006 bool AVRExpandPseudo::expand<AVR::AtomicFence>(Block &MBB, BlockIt MBBI) { 1007 // On AVR, there is only one core and so atomic fences do nothing. 1008 MBBI->eraseFromParent(); 1009 return true; 1010 } 1011 1012 template <> 1013 bool AVRExpandPseudo::expand<AVR::STSWKRr>(Block &MBB, BlockIt MBBI) { 1014 MachineInstr &MI = *MBBI; 1015 Register SrcLoReg, SrcHiReg; 1016 Register SrcReg = MI.getOperand(1).getReg(); 1017 bool SrcIsKill = MI.getOperand(1).isKill(); 1018 unsigned OpLo = AVR::STSKRr; 1019 unsigned OpHi = AVR::STSKRr; 1020 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1021 1022 // Write the high byte first in case this address belongs to a special 1023 // I/O address with a special temporary register. 1024 auto MIBHI = buildMI(MBB, MBBI, OpHi); 1025 auto MIBLO = buildMI(MBB, MBBI, OpLo); 1026 1027 switch (MI.getOperand(0).getType()) { 1028 case MachineOperand::MO_GlobalAddress: { 1029 const GlobalValue *GV = MI.getOperand(0).getGlobal(); 1030 int64_t Offs = MI.getOperand(0).getOffset(); 1031 unsigned TF = MI.getOperand(0).getTargetFlags(); 1032 1033 MIBLO.addGlobalAddress(GV, Offs, TF); 1034 MIBHI.addGlobalAddress(GV, Offs + 1, TF); 1035 break; 1036 } 1037 case MachineOperand::MO_Immediate: { 1038 unsigned Imm = MI.getOperand(0).getImm(); 1039 1040 MIBLO.addImm(Imm); 1041 MIBHI.addImm(Imm + 1); 1042 break; 1043 } 1044 default: 1045 llvm_unreachable("Unknown operand type!"); 1046 } 1047 1048 MIBLO.addReg(SrcLoReg, getKillRegState(SrcIsKill)); 1049 MIBHI.addReg(SrcHiReg, getKillRegState(SrcIsKill)); 1050 1051 MIBLO.setMemRefs(MI.memoperands()); 1052 MIBHI.setMemRefs(MI.memoperands()); 1053 1054 MI.eraseFromParent(); 1055 return true; 1056 } 1057 1058 template <> 1059 bool AVRExpandPseudo::expand<AVR::STWPtrRr>(Block &MBB, BlockIt MBBI) { 1060 MachineInstr &MI = *MBBI; 1061 Register SrcLoReg, SrcHiReg; 1062 Register DstReg = MI.getOperand(0).getReg(); 1063 Register SrcReg = MI.getOperand(1).getReg(); 1064 bool DstIsUndef = MI.getOperand(0).isUndef(); 1065 bool SrcIsKill = MI.getOperand(1).isKill(); 1066 unsigned OpLo = AVR::STPtrRr; 1067 unsigned OpHi = AVR::STDPtrQRr; 1068 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1069 1070 //: TODO: need to reverse this order like inw and stsw? 1071 auto MIBLO = buildMI(MBB, MBBI, OpLo) 1072 .addReg(DstReg, getUndefRegState(DstIsUndef)) 1073 .addReg(SrcLoReg, getKillRegState(SrcIsKill)); 1074 1075 auto MIBHI = buildMI(MBB, MBBI, OpHi) 1076 .addReg(DstReg, getUndefRegState(DstIsUndef)) 1077 .addImm(1) 1078 .addReg(SrcHiReg, getKillRegState(SrcIsKill)); 1079 1080 MIBLO.setMemRefs(MI.memoperands()); 1081 MIBHI.setMemRefs(MI.memoperands()); 1082 1083 MI.eraseFromParent(); 1084 return true; 1085 } 1086 1087 template <> 1088 bool AVRExpandPseudo::expand<AVR::STWPtrPiRr>(Block &MBB, BlockIt MBBI) { 1089 MachineInstr &MI = *MBBI; 1090 Register SrcLoReg, SrcHiReg; 1091 Register DstReg = MI.getOperand(0).getReg(); 1092 Register SrcReg = MI.getOperand(2).getReg(); 1093 unsigned Imm = MI.getOperand(3).getImm(); 1094 bool DstIsDead = MI.getOperand(0).isDead(); 1095 bool SrcIsKill = MI.getOperand(2).isKill(); 1096 unsigned OpLo = AVR::STPtrPiRr; 1097 unsigned OpHi = AVR::STPtrPiRr; 1098 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1099 1100 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same"); 1101 1102 auto MIBLO = buildMI(MBB, MBBI, OpLo) 1103 .addReg(DstReg, RegState::Define) 1104 .addReg(DstReg, RegState::Kill) 1105 .addReg(SrcLoReg, getKillRegState(SrcIsKill)) 1106 .addImm(Imm); 1107 1108 auto MIBHI = 1109 buildMI(MBB, MBBI, OpHi) 1110 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 1111 .addReg(DstReg, RegState::Kill) 1112 .addReg(SrcHiReg, getKillRegState(SrcIsKill)) 1113 .addImm(Imm); 1114 1115 MIBLO.setMemRefs(MI.memoperands()); 1116 MIBHI.setMemRefs(MI.memoperands()); 1117 1118 MI.eraseFromParent(); 1119 return true; 1120 } 1121 1122 template <> 1123 bool AVRExpandPseudo::expand<AVR::STWPtrPdRr>(Block &MBB, BlockIt MBBI) { 1124 MachineInstr &MI = *MBBI; 1125 Register SrcLoReg, SrcHiReg; 1126 Register DstReg = MI.getOperand(0).getReg(); 1127 Register SrcReg = MI.getOperand(2).getReg(); 1128 unsigned Imm = MI.getOperand(3).getImm(); 1129 bool DstIsDead = MI.getOperand(0).isDead(); 1130 bool SrcIsKill = MI.getOperand(2).isKill(); 1131 unsigned OpLo = AVR::STPtrPdRr; 1132 unsigned OpHi = AVR::STPtrPdRr; 1133 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1134 1135 assert(DstReg != SrcReg && "SrcReg and DstReg cannot be the same"); 1136 1137 auto MIBHI = buildMI(MBB, MBBI, OpHi) 1138 .addReg(DstReg, RegState::Define) 1139 .addReg(DstReg, RegState::Kill) 1140 .addReg(SrcHiReg, getKillRegState(SrcIsKill)) 1141 .addImm(Imm); 1142 1143 auto MIBLO = 1144 buildMI(MBB, MBBI, OpLo) 1145 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 1146 .addReg(DstReg, RegState::Kill) 1147 .addReg(SrcLoReg, getKillRegState(SrcIsKill)) 1148 .addImm(Imm); 1149 1150 MIBLO.setMemRefs(MI.memoperands()); 1151 MIBHI.setMemRefs(MI.memoperands()); 1152 1153 MI.eraseFromParent(); 1154 return true; 1155 } 1156 1157 template <> 1158 bool AVRExpandPseudo::expand<AVR::STDWPtrQRr>(Block &MBB, BlockIt MBBI) { 1159 MachineInstr &MI = *MBBI; 1160 1161 Register DstReg = MI.getOperand(0).getReg(); 1162 bool DstIsKill = MI.getOperand(0).isKill(); 1163 unsigned Imm = MI.getOperand(1).getImm(); 1164 Register SrcReg = MI.getOperand(2).getReg(); 1165 bool SrcIsKill = MI.getOperand(2).isKill(); 1166 1167 // STD's maximum displacement is 63, so larger stores have to be split into a 1168 // set of operations 1169 if (Imm >= 63) { 1170 if (!DstIsKill) { 1171 buildMI(MBB, MBBI, AVR::PUSHWRr).addReg(DstReg); 1172 } 1173 1174 buildMI(MBB, MBBI, AVR::SUBIWRdK) 1175 .addReg(DstReg, RegState::Define) 1176 .addReg(DstReg, RegState::Kill) 1177 .addImm(-Imm); 1178 1179 buildMI(MBB, MBBI, AVR::STWPtrRr) 1180 .addReg(DstReg, RegState::Kill) 1181 .addReg(SrcReg, getKillRegState(SrcIsKill)); 1182 1183 if (!DstIsKill) { 1184 buildMI(MBB, MBBI, AVR::POPWRd).addDef(DstReg, RegState::Define); 1185 } 1186 } else { 1187 unsigned OpLo = AVR::STDPtrQRr; 1188 unsigned OpHi = AVR::STDPtrQRr; 1189 Register SrcLoReg, SrcHiReg; 1190 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1191 1192 auto MIBLO = buildMI(MBB, MBBI, OpLo) 1193 .addReg(DstReg) 1194 .addImm(Imm) 1195 .addReg(SrcLoReg, getKillRegState(SrcIsKill)); 1196 1197 auto MIBHI = buildMI(MBB, MBBI, OpHi) 1198 .addReg(DstReg, getKillRegState(DstIsKill)) 1199 .addImm(Imm + 1) 1200 .addReg(SrcHiReg, getKillRegState(SrcIsKill)); 1201 1202 MIBLO.setMemRefs(MI.memoperands()); 1203 MIBHI.setMemRefs(MI.memoperands()); 1204 } 1205 1206 MI.eraseFromParent(); 1207 return true; 1208 } 1209 1210 template <> 1211 bool AVRExpandPseudo::expand<AVR::INWRdA>(Block &MBB, BlockIt MBBI) { 1212 MachineInstr &MI = *MBBI; 1213 Register DstLoReg, DstHiReg; 1214 unsigned Imm = MI.getOperand(1).getImm(); 1215 Register DstReg = MI.getOperand(0).getReg(); 1216 bool DstIsDead = MI.getOperand(0).isDead(); 1217 unsigned OpLo = AVR::INRdA; 1218 unsigned OpHi = AVR::INRdA; 1219 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1220 1221 // Since we add 1 to the Imm value for the high byte below, and 63 is the 1222 // highest Imm value allowed for the instruction, 62 is the limit here. 1223 assert(Imm <= 62 && "Address is out of range"); 1224 1225 auto MIBLO = 1226 buildMI(MBB, MBBI, OpLo) 1227 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1228 .addImm(Imm); 1229 1230 auto MIBHI = 1231 buildMI(MBB, MBBI, OpHi) 1232 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1233 .addImm(Imm + 1); 1234 1235 MIBLO.setMemRefs(MI.memoperands()); 1236 MIBHI.setMemRefs(MI.memoperands()); 1237 1238 MI.eraseFromParent(); 1239 return true; 1240 } 1241 1242 template <> 1243 bool AVRExpandPseudo::expand<AVR::OUTWARr>(Block &MBB, BlockIt MBBI) { 1244 MachineInstr &MI = *MBBI; 1245 Register SrcLoReg, SrcHiReg; 1246 unsigned Imm = MI.getOperand(0).getImm(); 1247 Register SrcReg = MI.getOperand(1).getReg(); 1248 bool SrcIsKill = MI.getOperand(1).isKill(); 1249 unsigned OpLo = AVR::OUTARr; 1250 unsigned OpHi = AVR::OUTARr; 1251 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1252 1253 // Since we add 1 to the Imm value for the high byte below, and 63 is the 1254 // highest Imm value allowed for the instruction, 62 is the limit here. 1255 assert(Imm <= 62 && "Address is out of range"); 1256 1257 // 16 bit I/O writes need the high byte first 1258 auto MIBHI = buildMI(MBB, MBBI, OpHi) 1259 .addImm(Imm + 1) 1260 .addReg(SrcHiReg, getKillRegState(SrcIsKill)); 1261 1262 auto MIBLO = buildMI(MBB, MBBI, OpLo) 1263 .addImm(Imm) 1264 .addReg(SrcLoReg, getKillRegState(SrcIsKill)); 1265 1266 MIBLO.setMemRefs(MI.memoperands()); 1267 MIBHI.setMemRefs(MI.memoperands()); 1268 1269 MI.eraseFromParent(); 1270 return true; 1271 } 1272 1273 template <> 1274 bool AVRExpandPseudo::expand<AVR::PUSHWRr>(Block &MBB, BlockIt MBBI) { 1275 MachineInstr &MI = *MBBI; 1276 Register SrcLoReg, SrcHiReg; 1277 Register SrcReg = MI.getOperand(0).getReg(); 1278 bool SrcIsKill = MI.getOperand(0).isKill(); 1279 unsigned Flags = MI.getFlags(); 1280 unsigned OpLo = AVR::PUSHRr; 1281 unsigned OpHi = AVR::PUSHRr; 1282 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 1283 1284 // Low part 1285 buildMI(MBB, MBBI, OpLo) 1286 .addReg(SrcLoReg, getKillRegState(SrcIsKill)) 1287 .setMIFlags(Flags); 1288 1289 // High part 1290 buildMI(MBB, MBBI, OpHi) 1291 .addReg(SrcHiReg, getKillRegState(SrcIsKill)) 1292 .setMIFlags(Flags); 1293 1294 MI.eraseFromParent(); 1295 return true; 1296 } 1297 1298 template <> 1299 bool AVRExpandPseudo::expand<AVR::POPWRd>(Block &MBB, BlockIt MBBI) { 1300 MachineInstr &MI = *MBBI; 1301 Register DstLoReg, DstHiReg; 1302 Register DstReg = MI.getOperand(0).getReg(); 1303 unsigned Flags = MI.getFlags(); 1304 unsigned OpLo = AVR::POPRd; 1305 unsigned OpHi = AVR::POPRd; 1306 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1307 1308 buildMI(MBB, MBBI, OpHi, DstHiReg).setMIFlags(Flags); // High 1309 buildMI(MBB, MBBI, OpLo, DstLoReg).setMIFlags(Flags); // Low 1310 1311 MI.eraseFromParent(); 1312 return true; 1313 } 1314 1315 template <> 1316 bool AVRExpandPseudo::expand<AVR::ROLBRd>(Block &MBB, BlockIt MBBI) { 1317 // In AVR, the rotate instructions behave quite unintuitively. They rotate 1318 // bits through the carry bit in SREG, effectively rotating over 9 bits, 1319 // instead of 8. This is useful when we are dealing with numbers over 1320 // multiple registers, but when we actually need to rotate stuff, we have 1321 // to explicitly add the carry bit. 1322 1323 MachineInstr &MI = *MBBI; 1324 unsigned OpShift, OpCarry; 1325 Register DstReg = MI.getOperand(0).getReg(); 1326 bool DstIsDead = MI.getOperand(0).isDead(); 1327 bool DstIsKill = MI.getOperand(1).isKill(); 1328 OpShift = AVR::ADDRdRr; 1329 OpCarry = AVR::ADCRdRr; 1330 1331 // add r16, r16 1332 // adc r16, r1 1333 1334 // Shift part 1335 buildMI(MBB, MBBI, OpShift) 1336 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 1337 .addReg(DstReg, RegState::Kill) 1338 .addReg(DstReg, RegState::Kill); 1339 1340 // Add the carry bit 1341 auto MIB = buildMI(MBB, MBBI, OpCarry) 1342 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 1343 .addReg(DstReg, getKillRegState(DstIsKill)) 1344 .addReg(ZERO_REGISTER); 1345 1346 // SREG is always implicitly killed 1347 MIB->getOperand(2).setIsKill(); 1348 1349 MI.eraseFromParent(); 1350 return true; 1351 } 1352 1353 template <> 1354 bool AVRExpandPseudo::expand<AVR::RORBRd>(Block &MBB, BlockIt MBBI) { 1355 // In AVR, the rotate instructions behave quite unintuitively. They rotate 1356 // bits through the carry bit in SREG, effectively rotating over 9 bits, 1357 // instead of 8. This is useful when we are dealing with numbers over 1358 // multiple registers, but when we actually need to rotate stuff, we have 1359 // to explicitly add the carry bit. 1360 1361 MachineInstr &MI = *MBBI; 1362 Register DstReg = MI.getOperand(0).getReg(); 1363 1364 // bst r16, 0 1365 // ror r16 1366 // bld r16, 7 1367 1368 // Move the lowest bit from DstReg into the T bit 1369 buildMI(MBB, MBBI, AVR::BST).addReg(DstReg).addImm(0); 1370 1371 // Rotate to the right 1372 buildMI(MBB, MBBI, AVR::RORRd, DstReg).addReg(DstReg); 1373 1374 // Move the T bit into the highest bit of DstReg. 1375 buildMI(MBB, MBBI, AVR::BLD, DstReg).addReg(DstReg).addImm(7); 1376 1377 MI.eraseFromParent(); 1378 return true; 1379 } 1380 1381 template <> 1382 bool AVRExpandPseudo::expand<AVR::LSLWRd>(Block &MBB, BlockIt MBBI) { 1383 MachineInstr &MI = *MBBI; 1384 Register DstLoReg, DstHiReg; 1385 Register DstReg = MI.getOperand(0).getReg(); 1386 bool DstIsDead = MI.getOperand(0).isDead(); 1387 bool DstIsKill = MI.getOperand(1).isKill(); 1388 bool ImpIsDead = MI.getOperand(2).isDead(); 1389 unsigned OpLo = AVR::ADDRdRr; // ADD Rd, Rd <==> LSL Rd 1390 unsigned OpHi = AVR::ADCRdRr; // ADC Rd, Rd <==> ROL Rd 1391 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1392 1393 // Low part 1394 buildMI(MBB, MBBI, OpLo) 1395 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1396 .addReg(DstLoReg, getKillRegState(DstIsKill)) 1397 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1398 1399 auto MIBHI = 1400 buildMI(MBB, MBBI, OpHi) 1401 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1402 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1403 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1404 1405 if (ImpIsDead) 1406 MIBHI->getOperand(3).setIsDead(); 1407 1408 // SREG is always implicitly killed 1409 MIBHI->getOperand(4).setIsKill(); 1410 1411 MI.eraseFromParent(); 1412 return true; 1413 } 1414 1415 template <> 1416 bool AVRExpandPseudo::expand<AVR::LSLWHiRd>(Block &MBB, BlockIt MBBI) { 1417 MachineInstr &MI = *MBBI; 1418 Register DstLoReg, DstHiReg; 1419 Register DstReg = MI.getOperand(0).getReg(); 1420 bool DstIsDead = MI.getOperand(0).isDead(); 1421 bool DstIsKill = MI.getOperand(1).isKill(); 1422 bool ImpIsDead = MI.getOperand(2).isDead(); 1423 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1424 1425 // add hireg, hireg <==> lsl hireg 1426 auto MILSL = 1427 buildMI(MBB, MBBI, AVR::ADDRdRr) 1428 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1429 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1430 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1431 1432 if (ImpIsDead) 1433 MILSL->getOperand(3).setIsDead(); 1434 1435 MI.eraseFromParent(); 1436 return true; 1437 } 1438 1439 bool AVRExpandPseudo::expandLSLW4Rd(Block &MBB, BlockIt MBBI) { 1440 MachineInstr &MI = *MBBI; 1441 Register DstLoReg, DstHiReg; 1442 Register DstReg = MI.getOperand(0).getReg(); 1443 bool DstIsDead = MI.getOperand(0).isDead(); 1444 bool DstIsKill = MI.getOperand(1).isKill(); 1445 bool ImpIsDead = MI.getOperand(3).isDead(); 1446 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1447 1448 // swap Rh 1449 // swap Rl 1450 buildMI(MBB, MBBI, AVR::SWAPRd) 1451 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1452 .addReg(DstHiReg, RegState::Kill); 1453 buildMI(MBB, MBBI, AVR::SWAPRd) 1454 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1455 .addReg(DstLoReg, RegState::Kill); 1456 1457 // andi Rh, 0xf0 1458 auto MI0 = 1459 buildMI(MBB, MBBI, AVR::ANDIRdK) 1460 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1461 .addReg(DstHiReg, RegState::Kill) 1462 .addImm(0xf0); 1463 // SREG is implicitly dead. 1464 MI0->getOperand(3).setIsDead(); 1465 1466 // eor Rh, Rl 1467 auto MI1 = 1468 buildMI(MBB, MBBI, AVR::EORRdRr) 1469 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1470 .addReg(DstHiReg, RegState::Kill) 1471 .addReg(DstLoReg); 1472 // SREG is implicitly dead. 1473 MI1->getOperand(3).setIsDead(); 1474 1475 // andi Rl, 0xf0 1476 auto MI2 = 1477 buildMI(MBB, MBBI, AVR::ANDIRdK) 1478 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1479 .addReg(DstLoReg, getKillRegState(DstIsKill)) 1480 .addImm(0xf0); 1481 // SREG is implicitly dead. 1482 MI2->getOperand(3).setIsDead(); 1483 1484 // eor Rh, Rl 1485 auto MI3 = 1486 buildMI(MBB, MBBI, AVR::EORRdRr) 1487 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1488 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1489 .addReg(DstLoReg); 1490 if (ImpIsDead) 1491 MI3->getOperand(3).setIsDead(); 1492 1493 MI.eraseFromParent(); 1494 return true; 1495 } 1496 1497 bool AVRExpandPseudo::expandLSLW8Rd(Block &MBB, BlockIt MBBI) { 1498 MachineInstr &MI = *MBBI; 1499 Register DstLoReg, DstHiReg; 1500 Register DstReg = MI.getOperand(0).getReg(); 1501 bool DstIsDead = MI.getOperand(0).isDead(); 1502 bool DstIsKill = MI.getOperand(1).isKill(); 1503 bool ImpIsDead = MI.getOperand(3).isDead(); 1504 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1505 1506 // mov Rh, Rl 1507 buildMI(MBB, MBBI, AVR::MOVRdRr) 1508 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1509 .addReg(DstLoReg); 1510 1511 // clr Rl 1512 auto MIBLO = 1513 buildMI(MBB, MBBI, AVR::EORRdRr) 1514 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1515 .addReg(DstLoReg, getKillRegState(DstIsKill)) 1516 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1517 if (ImpIsDead) 1518 MIBLO->getOperand(3).setIsDead(); 1519 1520 MI.eraseFromParent(); 1521 return true; 1522 } 1523 1524 bool AVRExpandPseudo::expandLSLW12Rd(Block &MBB, BlockIt MBBI) { 1525 MachineInstr &MI = *MBBI; 1526 Register DstLoReg, DstHiReg; 1527 Register DstReg = MI.getOperand(0).getReg(); 1528 bool DstIsDead = MI.getOperand(0).isDead(); 1529 bool DstIsKill = MI.getOperand(1).isKill(); 1530 bool ImpIsDead = MI.getOperand(3).isDead(); 1531 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1532 1533 // mov Rh, Rl 1534 buildMI(MBB, MBBI, AVR::MOVRdRr) 1535 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1536 .addReg(DstLoReg); 1537 1538 // swap Rh 1539 buildMI(MBB, MBBI, AVR::SWAPRd) 1540 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1541 .addReg(DstHiReg, RegState::Kill); 1542 1543 // andi Rh, 0xf0 1544 auto MI0 = 1545 buildMI(MBB, MBBI, AVR::ANDIRdK) 1546 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1547 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1548 .addImm(0xf0); 1549 // SREG is implicitly dead. 1550 MI0->getOperand(3).setIsDead(); 1551 1552 // clr Rl 1553 auto MI1 = 1554 buildMI(MBB, MBBI, AVR::EORRdRr) 1555 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1556 .addReg(DstLoReg, getKillRegState(DstIsKill)) 1557 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1558 if (ImpIsDead) 1559 MI1->getOperand(3).setIsDead(); 1560 1561 MI.eraseFromParent(); 1562 return true; 1563 } 1564 1565 template <> 1566 bool AVRExpandPseudo::expand<AVR::LSLWNRd>(Block &MBB, BlockIt MBBI) { 1567 MachineInstr &MI = *MBBI; 1568 unsigned Imm = MI.getOperand(2).getImm(); 1569 switch (Imm) { 1570 case 4: 1571 return expandLSLW4Rd(MBB, MBBI); 1572 case 8: 1573 return expandLSLW8Rd(MBB, MBBI); 1574 case 12: 1575 return expandLSLW12Rd(MBB, MBBI); 1576 default: 1577 llvm_unreachable("unimplemented lslwn"); 1578 return false; 1579 } 1580 } 1581 1582 template <> 1583 bool AVRExpandPseudo::expand<AVR::LSRWRd>(Block &MBB, BlockIt MBBI) { 1584 MachineInstr &MI = *MBBI; 1585 Register DstLoReg, DstHiReg; 1586 Register DstReg = MI.getOperand(0).getReg(); 1587 bool DstIsDead = MI.getOperand(0).isDead(); 1588 bool DstIsKill = MI.getOperand(1).isKill(); 1589 bool ImpIsDead = MI.getOperand(2).isDead(); 1590 unsigned OpLo = AVR::RORRd; 1591 unsigned OpHi = AVR::LSRRd; 1592 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1593 1594 // High part 1595 buildMI(MBB, MBBI, OpHi) 1596 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1597 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1598 1599 auto MIBLO = 1600 buildMI(MBB, MBBI, OpLo) 1601 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1602 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1603 1604 if (ImpIsDead) 1605 MIBLO->getOperand(2).setIsDead(); 1606 1607 // SREG is always implicitly killed 1608 MIBLO->getOperand(3).setIsKill(); 1609 1610 MI.eraseFromParent(); 1611 return true; 1612 } 1613 1614 template <> 1615 bool AVRExpandPseudo::expand<AVR::LSRWLoRd>(Block &MBB, BlockIt MBBI) { 1616 MachineInstr &MI = *MBBI; 1617 Register DstLoReg, DstHiReg; 1618 Register DstReg = MI.getOperand(0).getReg(); 1619 bool DstIsDead = MI.getOperand(0).isDead(); 1620 bool DstIsKill = MI.getOperand(1).isKill(); 1621 bool ImpIsDead = MI.getOperand(2).isDead(); 1622 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1623 1624 // lsr loreg 1625 auto MILSR = 1626 buildMI(MBB, MBBI, AVR::LSRRd) 1627 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1628 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1629 1630 if (ImpIsDead) 1631 MILSR->getOperand(2).setIsDead(); 1632 1633 MI.eraseFromParent(); 1634 return true; 1635 } 1636 1637 bool AVRExpandPseudo::expandLSRW4Rd(Block &MBB, BlockIt MBBI) { 1638 MachineInstr &MI = *MBBI; 1639 Register DstLoReg, DstHiReg; 1640 Register DstReg = MI.getOperand(0).getReg(); 1641 bool DstIsDead = MI.getOperand(0).isDead(); 1642 bool DstIsKill = MI.getOperand(1).isKill(); 1643 bool ImpIsDead = MI.getOperand(3).isDead(); 1644 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1645 1646 // swap Rh 1647 // swap Rl 1648 buildMI(MBB, MBBI, AVR::SWAPRd) 1649 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1650 .addReg(DstHiReg, RegState::Kill); 1651 buildMI(MBB, MBBI, AVR::SWAPRd) 1652 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1653 .addReg(DstLoReg, RegState::Kill); 1654 1655 // andi Rl, 0xf 1656 auto MI0 = 1657 buildMI(MBB, MBBI, AVR::ANDIRdK) 1658 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1659 .addReg(DstLoReg, RegState::Kill) 1660 .addImm(0xf); 1661 // SREG is implicitly dead. 1662 MI0->getOperand(3).setIsDead(); 1663 1664 // eor Rl, Rh 1665 auto MI1 = 1666 buildMI(MBB, MBBI, AVR::EORRdRr) 1667 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1668 .addReg(DstLoReg, RegState::Kill) 1669 .addReg(DstHiReg); 1670 // SREG is implicitly dead. 1671 MI1->getOperand(3).setIsDead(); 1672 1673 // andi Rh, 0xf 1674 auto MI2 = 1675 buildMI(MBB, MBBI, AVR::ANDIRdK) 1676 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1677 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1678 .addImm(0xf); 1679 // SREG is implicitly dead. 1680 MI2->getOperand(3).setIsDead(); 1681 1682 // eor Rl, Rh 1683 auto MI3 = 1684 buildMI(MBB, MBBI, AVR::EORRdRr) 1685 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1686 .addReg(DstLoReg, getKillRegState(DstIsKill)) 1687 .addReg(DstHiReg); 1688 if (ImpIsDead) 1689 MI3->getOperand(3).setIsDead(); 1690 1691 MI.eraseFromParent(); 1692 return true; 1693 } 1694 1695 bool AVRExpandPseudo::expandLSRW8Rd(Block &MBB, BlockIt MBBI) { 1696 MachineInstr &MI = *MBBI; 1697 Register DstLoReg, DstHiReg; 1698 Register DstReg = MI.getOperand(0).getReg(); 1699 bool DstIsDead = MI.getOperand(0).isDead(); 1700 bool DstIsKill = MI.getOperand(1).isKill(); 1701 bool ImpIsDead = MI.getOperand(3).isDead(); 1702 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1703 1704 // Move upper byte to lower byte. 1705 buildMI(MBB, MBBI, AVR::MOVRdRr) 1706 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1707 .addReg(DstHiReg); 1708 1709 // Clear upper byte. 1710 auto MIBHI = 1711 buildMI(MBB, MBBI, AVR::EORRdRr) 1712 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1713 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1714 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1715 if (ImpIsDead) 1716 MIBHI->getOperand(3).setIsDead(); 1717 1718 MI.eraseFromParent(); 1719 return true; 1720 } 1721 1722 bool AVRExpandPseudo::expandLSRW12Rd(Block &MBB, BlockIt MBBI) { 1723 MachineInstr &MI = *MBBI; 1724 Register DstLoReg, DstHiReg; 1725 Register DstReg = MI.getOperand(0).getReg(); 1726 bool DstIsDead = MI.getOperand(0).isDead(); 1727 bool DstIsKill = MI.getOperand(1).isKill(); 1728 bool ImpIsDead = MI.getOperand(3).isDead(); 1729 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1730 1731 // Move upper byte to lower byte. 1732 buildMI(MBB, MBBI, AVR::MOVRdRr) 1733 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1734 .addReg(DstHiReg); 1735 1736 // swap Rl 1737 buildMI(MBB, MBBI, AVR::SWAPRd) 1738 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1739 .addReg(DstLoReg, RegState::Kill); 1740 1741 // andi Rl, 0xf 1742 auto MI0 = 1743 buildMI(MBB, MBBI, AVR::ANDIRdK) 1744 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1745 .addReg(DstLoReg, getKillRegState(DstIsKill)) 1746 .addImm(0xf); 1747 // SREG is implicitly dead. 1748 MI0->getOperand(3).setIsDead(); 1749 1750 // Clear upper byte. 1751 auto MIBHI = 1752 buildMI(MBB, MBBI, AVR::EORRdRr) 1753 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1754 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1755 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1756 if (ImpIsDead) 1757 MIBHI->getOperand(3).setIsDead(); 1758 1759 MI.eraseFromParent(); 1760 return true; 1761 } 1762 1763 template <> 1764 bool AVRExpandPseudo::expand<AVR::LSRWNRd>(Block &MBB, BlockIt MBBI) { 1765 MachineInstr &MI = *MBBI; 1766 unsigned Imm = MI.getOperand(2).getImm(); 1767 switch (Imm) { 1768 case 4: 1769 return expandLSRW4Rd(MBB, MBBI); 1770 case 8: 1771 return expandLSRW8Rd(MBB, MBBI); 1772 case 12: 1773 return expandLSRW12Rd(MBB, MBBI); 1774 default: 1775 llvm_unreachable("unimplemented lsrwn"); 1776 return false; 1777 } 1778 } 1779 1780 template <> 1781 bool AVRExpandPseudo::expand<AVR::RORWRd>(Block &MBB, BlockIt MBBI) { 1782 llvm_unreachable("RORW unimplemented"); 1783 return false; 1784 } 1785 1786 template <> 1787 bool AVRExpandPseudo::expand<AVR::ROLWRd>(Block &MBB, BlockIt MBBI) { 1788 llvm_unreachable("ROLW unimplemented"); 1789 return false; 1790 } 1791 1792 template <> 1793 bool AVRExpandPseudo::expand<AVR::ASRWRd>(Block &MBB, BlockIt MBBI) { 1794 MachineInstr &MI = *MBBI; 1795 Register DstLoReg, DstHiReg; 1796 Register DstReg = MI.getOperand(0).getReg(); 1797 bool DstIsDead = MI.getOperand(0).isDead(); 1798 bool DstIsKill = MI.getOperand(1).isKill(); 1799 bool ImpIsDead = MI.getOperand(2).isDead(); 1800 unsigned OpLo = AVR::RORRd; 1801 unsigned OpHi = AVR::ASRRd; 1802 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1803 1804 // High part 1805 buildMI(MBB, MBBI, OpHi) 1806 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1807 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1808 1809 auto MIBLO = 1810 buildMI(MBB, MBBI, OpLo) 1811 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1812 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1813 1814 if (ImpIsDead) 1815 MIBLO->getOperand(2).setIsDead(); 1816 1817 // SREG is always implicitly killed 1818 MIBLO->getOperand(3).setIsKill(); 1819 1820 MI.eraseFromParent(); 1821 return true; 1822 } 1823 1824 template <> 1825 bool AVRExpandPseudo::expand<AVR::ASRWLoRd>(Block &MBB, BlockIt MBBI) { 1826 MachineInstr &MI = *MBBI; 1827 Register DstLoReg, DstHiReg; 1828 Register DstReg = MI.getOperand(0).getReg(); 1829 bool DstIsDead = MI.getOperand(0).isDead(); 1830 bool DstIsKill = MI.getOperand(1).isKill(); 1831 bool ImpIsDead = MI.getOperand(2).isDead(); 1832 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1833 1834 // asr loreg 1835 auto MIASR = 1836 buildMI(MBB, MBBI, AVR::ASRRd) 1837 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1838 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1839 1840 if (ImpIsDead) 1841 MIASR->getOperand(2).setIsDead(); 1842 1843 MI.eraseFromParent(); 1844 return true; 1845 } 1846 1847 bool AVRExpandPseudo::expandASRW7Rd(Block &MBB, BlockIt MBBI) { 1848 MachineInstr &MI = *MBBI; 1849 Register DstLoReg, DstHiReg; 1850 Register DstReg = MI.getOperand(0).getReg(); 1851 bool DstIsDead = MI.getOperand(0).isDead(); 1852 bool DstIsKill = MI.getOperand(1).isKill(); 1853 bool ImpIsDead = MI.getOperand(3).isDead(); 1854 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1855 1856 // lsl r24 1857 // mov r24,r25 1858 // rol r24 1859 // sbc r25,r25 1860 1861 // lsl r24 <=> add r24, r24 1862 buildMI(MBB, MBBI, AVR::ADDRdRr) 1863 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1864 .addReg(DstLoReg, RegState::Kill) 1865 .addReg(DstLoReg, RegState::Kill); 1866 1867 // mov r24, r25 1868 buildMI(MBB, MBBI, AVR::MOVRdRr) 1869 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1870 .addReg(DstHiReg); 1871 1872 // rol r24 <=> adc r24, r24 1873 buildMI(MBB, MBBI, AVR::ADCRdRr) 1874 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1875 .addReg(DstLoReg, getKillRegState(DstIsKill)) 1876 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1877 1878 // sbc r25, r25 1879 auto MISBC = 1880 buildMI(MBB, MBBI, AVR::SBCRdRr) 1881 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1882 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1883 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1884 1885 if (ImpIsDead) 1886 MISBC->getOperand(3).setIsDead(); 1887 // SREG is always implicitly killed 1888 MISBC->getOperand(4).setIsKill(); 1889 1890 MI.eraseFromParent(); 1891 return true; 1892 } 1893 1894 bool AVRExpandPseudo::expandASRW8Rd(Block &MBB, BlockIt MBBI) { 1895 MachineInstr &MI = *MBBI; 1896 Register DstLoReg, DstHiReg; 1897 Register DstReg = MI.getOperand(0).getReg(); 1898 bool DstIsDead = MI.getOperand(0).isDead(); 1899 bool DstIsKill = MI.getOperand(1).isKill(); 1900 bool ImpIsDead = MI.getOperand(3).isDead(); 1901 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1902 1903 // Move upper byte to lower byte. 1904 buildMI(MBB, MBBI, AVR::MOVRdRr) 1905 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1906 .addReg(DstHiReg); 1907 1908 // Move the sign bit to the C flag. 1909 buildMI(MBB, MBBI, AVR::ADDRdRr) 1910 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1911 .addReg(DstHiReg, RegState::Kill) 1912 .addReg(DstHiReg, RegState::Kill); 1913 1914 // Set upper byte to 0 or -1. 1915 auto MIBHI = 1916 buildMI(MBB, MBBI, AVR::SBCRdRr) 1917 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1918 .addReg(DstHiReg, getKillRegState(DstIsKill)) 1919 .addReg(DstHiReg, getKillRegState(DstIsKill)); 1920 1921 if (ImpIsDead) 1922 MIBHI->getOperand(3).setIsDead(); 1923 // SREG is always implicitly killed 1924 MIBHI->getOperand(4).setIsKill(); 1925 1926 MI.eraseFromParent(); 1927 return true; 1928 } 1929 bool AVRExpandPseudo::expandASRW14Rd(Block &MBB, BlockIt MBBI) { 1930 MachineInstr &MI = *MBBI; 1931 Register DstLoReg, DstHiReg; 1932 Register DstReg = MI.getOperand(0).getReg(); 1933 bool DstIsDead = MI.getOperand(0).isDead(); 1934 bool DstIsKill = MI.getOperand(1).isKill(); 1935 bool ImpIsDead = MI.getOperand(3).isDead(); 1936 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1937 1938 // lsl r25 1939 // sbc r24, r24 1940 // lsl r25 1941 // mov r25, r24 1942 // rol r24 1943 1944 // lsl r25 <=> add r25, r25 1945 buildMI(MBB, MBBI, AVR::ADDRdRr) 1946 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1947 .addReg(DstHiReg, RegState::Kill) 1948 .addReg(DstHiReg, RegState::Kill); 1949 1950 // sbc r24, r24 1951 buildMI(MBB, MBBI, AVR::SBCRdRr) 1952 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1953 .addReg(DstLoReg, RegState::Kill) 1954 .addReg(DstLoReg, RegState::Kill); 1955 1956 // lsl r25 <=> add r25, r25 1957 buildMI(MBB, MBBI, AVR::ADDRdRr) 1958 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1959 .addReg(DstHiReg, RegState::Kill) 1960 .addReg(DstHiReg, RegState::Kill); 1961 1962 // mov r25, r24 1963 buildMI(MBB, MBBI, AVR::MOVRdRr) 1964 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 1965 .addReg(DstLoReg); 1966 1967 // rol r24 <=> adc r24, r24 1968 auto MIROL = 1969 buildMI(MBB, MBBI, AVR::ADCRdRr) 1970 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 1971 .addReg(DstLoReg, getKillRegState(DstIsKill)) 1972 .addReg(DstLoReg, getKillRegState(DstIsKill)); 1973 1974 if (ImpIsDead) 1975 MIROL->getOperand(3).setIsDead(); 1976 // SREG is always implicitly killed 1977 MIROL->getOperand(4).setIsKill(); 1978 1979 MI.eraseFromParent(); 1980 return false; 1981 } 1982 1983 bool AVRExpandPseudo::expandASRW15Rd(Block &MBB, BlockIt MBBI) { 1984 MachineInstr &MI = *MBBI; 1985 Register DstLoReg, DstHiReg; 1986 Register DstReg = MI.getOperand(0).getReg(); 1987 bool DstIsDead = MI.getOperand(0).isDead(); 1988 bool ImpIsDead = MI.getOperand(3).isDead(); 1989 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 1990 1991 // lsl r25 1992 // sbc r25, r25 1993 // mov r24, r25 1994 1995 // lsl r25 <=> add r25, r25 1996 buildMI(MBB, MBBI, AVR::ADDRdRr) 1997 .addReg(DstHiReg, RegState::Define) 1998 .addReg(DstHiReg, RegState::Kill) 1999 .addReg(DstHiReg, RegState::Kill); 2000 2001 // sbc r25, r25 2002 auto MISBC = 2003 buildMI(MBB, MBBI, AVR::SBCRdRr) 2004 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 2005 .addReg(DstHiReg, RegState::Kill) 2006 .addReg(DstHiReg, RegState::Kill); 2007 if (ImpIsDead) 2008 MISBC->getOperand(3).setIsDead(); 2009 // SREG is always implicitly killed 2010 MISBC->getOperand(4).setIsKill(); 2011 2012 // mov r24, r25 2013 buildMI(MBB, MBBI, AVR::MOVRdRr) 2014 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 2015 .addReg(DstHiReg); 2016 2017 MI.eraseFromParent(); 2018 return true; 2019 } 2020 2021 template <> 2022 bool AVRExpandPseudo::expand<AVR::ASRWNRd>(Block &MBB, BlockIt MBBI) { 2023 MachineInstr &MI = *MBBI; 2024 unsigned Imm = MI.getOperand(2).getImm(); 2025 switch (Imm) { 2026 case 7: 2027 return expandASRW7Rd(MBB, MBBI); 2028 case 8: 2029 return expandASRW8Rd(MBB, MBBI); 2030 case 14: 2031 return expandASRW14Rd(MBB, MBBI); 2032 case 15: 2033 return expandASRW15Rd(MBB, MBBI); 2034 default: 2035 llvm_unreachable("unimplemented asrwn"); 2036 return false; 2037 } 2038 } 2039 2040 bool AVRExpandPseudo::expandLSLB7Rd(Block &MBB, BlockIt MBBI) { 2041 MachineInstr &MI = *MBBI; 2042 Register DstReg = MI.getOperand(0).getReg(); 2043 bool DstIsDead = MI.getOperand(0).isDead(); 2044 bool DstIsKill = MI.getOperand(1).isKill(); 2045 bool ImpIsDead = MI.getOperand(3).isDead(); 2046 2047 // ror r24 2048 // clr r24 2049 // ror r24 2050 2051 buildMI(MBB, MBBI, AVR::RORRd) 2052 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2053 .addReg(DstReg, RegState::Kill) 2054 ->getOperand(3) 2055 .setIsUndef(true); 2056 2057 buildMI(MBB, MBBI, AVR::EORRdRr) 2058 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2059 .addReg(DstReg, RegState::Kill) 2060 .addReg(DstReg, RegState::Kill); 2061 2062 auto MIRRC = 2063 buildMI(MBB, MBBI, AVR::RORRd) 2064 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2065 .addReg(DstReg, getKillRegState(DstIsKill)); 2066 2067 if (ImpIsDead) 2068 MIRRC->getOperand(2).setIsDead(); 2069 2070 // SREG is always implicitly killed 2071 MIRRC->getOperand(3).setIsKill(); 2072 2073 MI.eraseFromParent(); 2074 return true; 2075 } 2076 2077 template <> 2078 bool AVRExpandPseudo::expand<AVR::LSLBNRd>(Block &MBB, BlockIt MBBI) { 2079 MachineInstr &MI = *MBBI; 2080 unsigned Imm = MI.getOperand(2).getImm(); 2081 switch (Imm) { 2082 case 7: 2083 return expandLSLB7Rd(MBB, MBBI); 2084 default: 2085 llvm_unreachable("unimplemented lslbn"); 2086 return false; 2087 } 2088 } 2089 2090 bool AVRExpandPseudo::expandLSRB7Rd(Block &MBB, BlockIt MBBI) { 2091 MachineInstr &MI = *MBBI; 2092 Register DstReg = MI.getOperand(0).getReg(); 2093 bool DstIsDead = MI.getOperand(0).isDead(); 2094 bool DstIsKill = MI.getOperand(1).isKill(); 2095 bool ImpIsDead = MI.getOperand(3).isDead(); 2096 2097 // rol r24 2098 // clr r24 2099 // rol r24 2100 2101 buildMI(MBB, MBBI, AVR::ADCRdRr) 2102 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2103 .addReg(DstReg, RegState::Kill) 2104 .addReg(DstReg, RegState::Kill) 2105 ->getOperand(4) 2106 .setIsUndef(true); 2107 2108 buildMI(MBB, MBBI, AVR::EORRdRr) 2109 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2110 .addReg(DstReg, RegState::Kill) 2111 .addReg(DstReg, RegState::Kill); 2112 2113 auto MIRRC = 2114 buildMI(MBB, MBBI, AVR::ADCRdRr) 2115 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2116 .addReg(DstReg, getKillRegState(DstIsKill)) 2117 .addReg(DstReg, getKillRegState(DstIsKill)); 2118 2119 if (ImpIsDead) 2120 MIRRC->getOperand(3).setIsDead(); 2121 2122 // SREG is always implicitly killed 2123 MIRRC->getOperand(4).setIsKill(); 2124 2125 MI.eraseFromParent(); 2126 return true; 2127 } 2128 2129 template <> 2130 bool AVRExpandPseudo::expand<AVR::LSRBNRd>(Block &MBB, BlockIt MBBI) { 2131 MachineInstr &MI = *MBBI; 2132 unsigned Imm = MI.getOperand(2).getImm(); 2133 switch (Imm) { 2134 case 7: 2135 return expandLSRB7Rd(MBB, MBBI); 2136 default: 2137 llvm_unreachable("unimplemented lsrbn"); 2138 return false; 2139 } 2140 } 2141 2142 bool AVRExpandPseudo::expandASRB6Rd(Block &MBB, BlockIt MBBI) { 2143 MachineInstr &MI = *MBBI; 2144 Register DstReg = MI.getOperand(0).getReg(); 2145 bool DstIsDead = MI.getOperand(0).isDead(); 2146 bool DstIsKill = MI.getOperand(1).isKill(); 2147 2148 // bst r24, 6 2149 // lsl r24 2150 // sbc r24, r24 2151 // bld r24, 0 2152 2153 buildMI(MBB, MBBI, AVR::BST) 2154 .addReg(DstReg) 2155 .addImm(6) 2156 ->getOperand(2) 2157 .setIsUndef(true); 2158 2159 buildMI(MBB, MBBI, AVR::ADDRdRr) // LSL Rd <==> ADD Rd, Rd 2160 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2161 .addReg(DstReg, RegState::Kill) 2162 .addReg(DstReg, RegState::Kill); 2163 2164 buildMI(MBB, MBBI, AVR::SBCRdRr) 2165 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2166 .addReg(DstReg, RegState::Kill) 2167 .addReg(DstReg, RegState::Kill); 2168 2169 buildMI(MBB, MBBI, AVR::BLD) 2170 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2171 .addReg(DstReg, getKillRegState(DstIsKill)) 2172 .addImm(0) 2173 ->getOperand(3) 2174 .setIsKill(); 2175 2176 MI.eraseFromParent(); 2177 return true; 2178 } 2179 2180 bool AVRExpandPseudo::expandASRB7Rd(Block &MBB, BlockIt MBBI) { 2181 MachineInstr &MI = *MBBI; 2182 Register DstReg = MI.getOperand(0).getReg(); 2183 bool DstIsDead = MI.getOperand(0).isDead(); 2184 bool DstIsKill = MI.getOperand(1).isKill(); 2185 bool ImpIsDead = MI.getOperand(3).isDead(); 2186 2187 // lsl r24 2188 // sbc r24, r24 2189 2190 buildMI(MBB, MBBI, AVR::ADDRdRr) 2191 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2192 .addReg(DstReg, RegState::Kill) 2193 .addReg(DstReg, RegState::Kill); 2194 2195 auto MIRRC = 2196 buildMI(MBB, MBBI, AVR::SBCRdRr) 2197 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) 2198 .addReg(DstReg, getKillRegState(DstIsKill)) 2199 .addReg(DstReg, getKillRegState(DstIsKill)); 2200 2201 if (ImpIsDead) 2202 MIRRC->getOperand(3).setIsDead(); 2203 2204 // SREG is always implicitly killed 2205 MIRRC->getOperand(4).setIsKill(); 2206 2207 MI.eraseFromParent(); 2208 return true; 2209 } 2210 2211 template <> 2212 bool AVRExpandPseudo::expand<AVR::ASRBNRd>(Block &MBB, BlockIt MBBI) { 2213 MachineInstr &MI = *MBBI; 2214 unsigned Imm = MI.getOperand(2).getImm(); 2215 switch (Imm) { 2216 case 6: 2217 return expandASRB6Rd(MBB, MBBI); 2218 case 7: 2219 return expandASRB7Rd(MBB, MBBI); 2220 default: 2221 llvm_unreachable("unimplemented asrbn"); 2222 return false; 2223 } 2224 } 2225 2226 template <> bool AVRExpandPseudo::expand<AVR::SEXT>(Block &MBB, BlockIt MBBI) { 2227 MachineInstr &MI = *MBBI; 2228 Register DstLoReg, DstHiReg; 2229 // sext R17:R16, R17 2230 // mov r16, r17 2231 // lsl r17 2232 // sbc r17, r17 2233 // sext R17:R16, R13 2234 // mov r16, r13 2235 // mov r17, r13 2236 // lsl r17 2237 // sbc r17, r17 2238 // sext R17:R16, R16 2239 // mov r17, r16 2240 // lsl r17 2241 // sbc r17, r17 2242 Register DstReg = MI.getOperand(0).getReg(); 2243 Register SrcReg = MI.getOperand(1).getReg(); 2244 bool DstIsDead = MI.getOperand(0).isDead(); 2245 bool SrcIsKill = MI.getOperand(1).isKill(); 2246 bool ImpIsDead = MI.getOperand(2).isDead(); 2247 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 2248 2249 if (SrcReg != DstLoReg) 2250 buildMI(MBB, MBBI, AVR::MOVRdRr) 2251 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 2252 .addReg(SrcReg); 2253 2254 if (SrcReg != DstHiReg) { 2255 auto MOV = buildMI(MBB, MBBI, AVR::MOVRdRr) 2256 .addReg(DstHiReg, RegState::Define) 2257 .addReg(SrcReg); 2258 if (SrcReg != DstLoReg && SrcIsKill) 2259 MOV->getOperand(1).setIsKill(); 2260 } 2261 2262 buildMI(MBB, MBBI, AVR::ADDRdRr) // LSL Rd <==> ADD Rd, Rr 2263 .addReg(DstHiReg, RegState::Define) 2264 .addReg(DstHiReg, RegState::Kill) 2265 .addReg(DstHiReg, RegState::Kill); 2266 2267 auto SBC = 2268 buildMI(MBB, MBBI, AVR::SBCRdRr) 2269 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 2270 .addReg(DstHiReg, RegState::Kill) 2271 .addReg(DstHiReg, RegState::Kill); 2272 2273 if (ImpIsDead) 2274 SBC->getOperand(3).setIsDead(); 2275 2276 // SREG is always implicitly killed 2277 SBC->getOperand(4).setIsKill(); 2278 2279 MI.eraseFromParent(); 2280 return true; 2281 } 2282 2283 template <> bool AVRExpandPseudo::expand<AVR::ZEXT>(Block &MBB, BlockIt MBBI) { 2284 MachineInstr &MI = *MBBI; 2285 Register DstLoReg, DstHiReg; 2286 // zext R25:R24, R20 2287 // mov R24, R20 2288 // eor R25, R25 2289 // zext R25:R24, R24 2290 // eor R25, R25 2291 // zext R25:R24, R25 2292 // mov R24, R25 2293 // eor R25, R25 2294 Register DstReg = MI.getOperand(0).getReg(); 2295 Register SrcReg = MI.getOperand(1).getReg(); 2296 bool DstIsDead = MI.getOperand(0).isDead(); 2297 bool SrcIsKill = MI.getOperand(1).isKill(); 2298 bool ImpIsDead = MI.getOperand(2).isDead(); 2299 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 2300 2301 if (SrcReg != DstLoReg) { 2302 buildMI(MBB, MBBI, AVR::MOVRdRr) 2303 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 2304 .addReg(SrcReg, getKillRegState(SrcIsKill)); 2305 } 2306 2307 auto EOR = 2308 buildMI(MBB, MBBI, AVR::EORRdRr) 2309 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 2310 .addReg(DstHiReg, RegState::Kill | RegState::Undef) 2311 .addReg(DstHiReg, RegState::Kill | RegState::Undef); 2312 2313 if (ImpIsDead) 2314 EOR->getOperand(3).setIsDead(); 2315 2316 MI.eraseFromParent(); 2317 return true; 2318 } 2319 2320 template <> 2321 bool AVRExpandPseudo::expand<AVR::SPREAD>(Block &MBB, BlockIt MBBI) { 2322 MachineInstr &MI = *MBBI; 2323 Register DstLoReg, DstHiReg; 2324 Register DstReg = MI.getOperand(0).getReg(); 2325 bool DstIsDead = MI.getOperand(0).isDead(); 2326 unsigned Flags = MI.getFlags(); 2327 unsigned OpLo = AVR::INRdA; 2328 unsigned OpHi = AVR::INRdA; 2329 TRI->splitReg(DstReg, DstLoReg, DstHiReg); 2330 2331 // Low part 2332 buildMI(MBB, MBBI, OpLo) 2333 .addReg(DstLoReg, RegState::Define | getDeadRegState(DstIsDead)) 2334 .addImm(0x3d) 2335 .setMIFlags(Flags); 2336 2337 // High part 2338 buildMI(MBB, MBBI, OpHi) 2339 .addReg(DstHiReg, RegState::Define | getDeadRegState(DstIsDead)) 2340 .addImm(0x3e) 2341 .setMIFlags(Flags); 2342 2343 MI.eraseFromParent(); 2344 return true; 2345 } 2346 2347 template <> 2348 bool AVRExpandPseudo::expand<AVR::SPWRITE>(Block &MBB, BlockIt MBBI) { 2349 const AVRSubtarget &STI = MBB.getParent()->getSubtarget<AVRSubtarget>(); 2350 MachineInstr &MI = *MBBI; 2351 Register SrcLoReg, SrcHiReg; 2352 Register SrcReg = MI.getOperand(1).getReg(); 2353 bool SrcIsKill = MI.getOperand(1).isKill(); 2354 unsigned Flags = MI.getFlags(); 2355 TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg); 2356 2357 buildMI(MBB, MBBI, AVR::INRdA) 2358 .addReg(AVR::R0, RegState::Define) 2359 .addImm(STI.getIORegSREG()) 2360 .setMIFlags(Flags); 2361 2362 buildMI(MBB, MBBI, AVR::BCLRs).addImm(0x07).setMIFlags(Flags); 2363 2364 buildMI(MBB, MBBI, AVR::OUTARr) 2365 .addImm(0x3e) 2366 .addReg(SrcHiReg, getKillRegState(SrcIsKill)) 2367 .setMIFlags(Flags); 2368 2369 buildMI(MBB, MBBI, AVR::OUTARr) 2370 .addImm(STI.getIORegSREG()) 2371 .addReg(AVR::R0, RegState::Kill) 2372 .setMIFlags(Flags); 2373 2374 buildMI(MBB, MBBI, AVR::OUTARr) 2375 .addImm(0x3d) 2376 .addReg(SrcLoReg, getKillRegState(SrcIsKill)) 2377 .setMIFlags(Flags); 2378 2379 MI.eraseFromParent(); 2380 return true; 2381 } 2382 2383 bool AVRExpandPseudo::expandMI(Block &MBB, BlockIt MBBI) { 2384 MachineInstr &MI = *MBBI; 2385 int Opcode = MBBI->getOpcode(); 2386 2387 #define EXPAND(Op) \ 2388 case Op: \ 2389 return expand<Op>(MBB, MI) 2390 2391 switch (Opcode) { 2392 EXPAND(AVR::ADDWRdRr); 2393 EXPAND(AVR::ADCWRdRr); 2394 EXPAND(AVR::SUBWRdRr); 2395 EXPAND(AVR::SUBIWRdK); 2396 EXPAND(AVR::SBCWRdRr); 2397 EXPAND(AVR::SBCIWRdK); 2398 EXPAND(AVR::ANDWRdRr); 2399 EXPAND(AVR::ANDIWRdK); 2400 EXPAND(AVR::ORWRdRr); 2401 EXPAND(AVR::ORIWRdK); 2402 EXPAND(AVR::EORWRdRr); 2403 EXPAND(AVR::COMWRd); 2404 EXPAND(AVR::NEGWRd); 2405 EXPAND(AVR::CPWRdRr); 2406 EXPAND(AVR::CPCWRdRr); 2407 EXPAND(AVR::LDIWRdK); 2408 EXPAND(AVR::LDSWRdK); 2409 EXPAND(AVR::LDWRdPtr); 2410 EXPAND(AVR::LDWRdPtrPi); 2411 EXPAND(AVR::LDWRdPtrPd); 2412 case AVR::LDDWRdYQ: //: FIXME: remove this once PR13375 gets fixed 2413 EXPAND(AVR::LDDWRdPtrQ); 2414 EXPAND(AVR::LPMWRdZ); 2415 EXPAND(AVR::LPMWRdZPi); 2416 EXPAND(AVR::ELPMBRdZ); 2417 EXPAND(AVR::ELPMWRdZ); 2418 EXPAND(AVR::ELPMBRdZPi); 2419 EXPAND(AVR::ELPMWRdZPi); 2420 EXPAND(AVR::AtomicLoad8); 2421 EXPAND(AVR::AtomicLoad16); 2422 EXPAND(AVR::AtomicStore8); 2423 EXPAND(AVR::AtomicStore16); 2424 EXPAND(AVR::AtomicFence); 2425 EXPAND(AVR::STSWKRr); 2426 EXPAND(AVR::STWPtrRr); 2427 EXPAND(AVR::STWPtrPiRr); 2428 EXPAND(AVR::STWPtrPdRr); 2429 EXPAND(AVR::STDWPtrQRr); 2430 EXPAND(AVR::INWRdA); 2431 EXPAND(AVR::OUTWARr); 2432 EXPAND(AVR::PUSHWRr); 2433 EXPAND(AVR::POPWRd); 2434 EXPAND(AVR::ROLBRd); 2435 EXPAND(AVR::RORBRd); 2436 EXPAND(AVR::LSLWRd); 2437 EXPAND(AVR::LSRWRd); 2438 EXPAND(AVR::RORWRd); 2439 EXPAND(AVR::ROLWRd); 2440 EXPAND(AVR::ASRWRd); 2441 EXPAND(AVR::LSLWHiRd); 2442 EXPAND(AVR::LSRWLoRd); 2443 EXPAND(AVR::ASRWLoRd); 2444 EXPAND(AVR::LSLWNRd); 2445 EXPAND(AVR::LSRWNRd); 2446 EXPAND(AVR::ASRWNRd); 2447 EXPAND(AVR::LSLBNRd); 2448 EXPAND(AVR::LSRBNRd); 2449 EXPAND(AVR::ASRBNRd); 2450 EXPAND(AVR::SEXT); 2451 EXPAND(AVR::ZEXT); 2452 EXPAND(AVR::SPREAD); 2453 EXPAND(AVR::SPWRITE); 2454 } 2455 #undef EXPAND 2456 return false; 2457 } 2458 2459 } // end of anonymous namespace 2460 2461 INITIALIZE_PASS(AVRExpandPseudo, "avr-expand-pseudo", AVR_EXPAND_PSEUDO_NAME, 2462 false, false) 2463 namespace llvm { 2464 2465 FunctionPass *createAVRExpandPseudoPass() { return new AVRExpandPseudo(); } 2466 2467 } // end of namespace llvm 2468