1 //===------- HexagonCopyToCombine.cpp - Hexagon Copy-To-Combine Pass ------===// 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 // This pass replaces transfer instructions by combine instructions. 9 // We walk along a basic block and look for two combinable instructions and try 10 // to move them together. If we can move them next to each other we do so and 11 // replace them with a combine instruction. 12 //===----------------------------------------------------------------------===// 13 #include "HexagonInstrInfo.h" 14 #include "HexagonSubtarget.h" 15 #include "llvm/ADT/DenseMap.h" 16 #include "llvm/ADT/DenseSet.h" 17 #include "llvm/CodeGen/MachineBasicBlock.h" 18 #include "llvm/CodeGen/MachineFunction.h" 19 #include "llvm/CodeGen/MachineFunctionPass.h" 20 #include "llvm/CodeGen/MachineInstr.h" 21 #include "llvm/CodeGen/MachineInstrBuilder.h" 22 #include "llvm/CodeGen/Passes.h" 23 #include "llvm/CodeGen/TargetRegisterInfo.h" 24 #include "llvm/Pass.h" 25 #include "llvm/Support/CodeGen.h" 26 #include "llvm/Support/CommandLine.h" 27 #include "llvm/Support/Debug.h" 28 #include "llvm/Support/raw_ostream.h" 29 30 using namespace llvm; 31 32 #define DEBUG_TYPE "hexagon-copy-combine" 33 34 static 35 cl::opt<bool> IsCombinesDisabled("disable-merge-into-combines", 36 cl::Hidden, cl::ZeroOrMore, 37 cl::init(false), 38 cl::desc("Disable merging into combines")); 39 static 40 cl::opt<bool> IsConst64Disabled("disable-const64", 41 cl::Hidden, cl::ZeroOrMore, 42 cl::init(false), 43 cl::desc("Disable generation of const64")); 44 static 45 cl::opt<unsigned> 46 MaxNumOfInstsBetweenNewValueStoreAndTFR("max-num-inst-between-tfr-and-nv-store", 47 cl::Hidden, cl::init(4), 48 cl::desc("Maximum distance between a tfr feeding a store we " 49 "consider the store still to be newifiable")); 50 51 namespace llvm { 52 FunctionPass *createHexagonCopyToCombine(); 53 void initializeHexagonCopyToCombinePass(PassRegistry&); 54 } 55 56 57 namespace { 58 59 class HexagonCopyToCombine : public MachineFunctionPass { 60 const HexagonInstrInfo *TII; 61 const TargetRegisterInfo *TRI; 62 const HexagonSubtarget *ST; 63 bool ShouldCombineAggressively; 64 65 DenseSet<MachineInstr *> PotentiallyNewifiableTFR; 66 SmallVector<MachineInstr *, 8> DbgMItoMove; 67 68 public: 69 static char ID; 70 71 HexagonCopyToCombine() : MachineFunctionPass(ID) { 72 initializeHexagonCopyToCombinePass(*PassRegistry::getPassRegistry()); 73 } 74 75 void getAnalysisUsage(AnalysisUsage &AU) const override { 76 MachineFunctionPass::getAnalysisUsage(AU); 77 } 78 79 StringRef getPassName() const override { 80 return "Hexagon Copy-To-Combine Pass"; 81 } 82 83 bool runOnMachineFunction(MachineFunction &Fn) override; 84 85 MachineFunctionProperties getRequiredProperties() const override { 86 return MachineFunctionProperties().set( 87 MachineFunctionProperties::Property::NoVRegs); 88 } 89 90 private: 91 MachineInstr *findPairable(MachineInstr &I1, bool &DoInsertAtI1, 92 bool AllowC64); 93 94 void findPotentialNewifiableTFRs(MachineBasicBlock &); 95 96 void combine(MachineInstr &I1, MachineInstr &I2, 97 MachineBasicBlock::iterator &MI, bool DoInsertAtI1, 98 bool OptForSize); 99 100 bool isSafeToMoveTogether(MachineInstr &I1, MachineInstr &I2, 101 unsigned I1DestReg, unsigned I2DestReg, 102 bool &DoInsertAtI1); 103 104 void emitCombineRR(MachineBasicBlock::iterator &Before, unsigned DestReg, 105 MachineOperand &HiOperand, MachineOperand &LoOperand); 106 107 void emitCombineRI(MachineBasicBlock::iterator &Before, unsigned DestReg, 108 MachineOperand &HiOperand, MachineOperand &LoOperand); 109 110 void emitCombineIR(MachineBasicBlock::iterator &Before, unsigned DestReg, 111 MachineOperand &HiOperand, MachineOperand &LoOperand); 112 113 void emitCombineII(MachineBasicBlock::iterator &Before, unsigned DestReg, 114 MachineOperand &HiOperand, MachineOperand &LoOperand); 115 116 void emitConst64(MachineBasicBlock::iterator &Before, unsigned DestReg, 117 MachineOperand &HiOperand, MachineOperand &LoOperand); 118 }; 119 120 } // End anonymous namespace. 121 122 char HexagonCopyToCombine::ID = 0; 123 124 INITIALIZE_PASS(HexagonCopyToCombine, "hexagon-copy-combine", 125 "Hexagon Copy-To-Combine Pass", false, false) 126 127 static bool isCombinableInstType(MachineInstr &MI, const HexagonInstrInfo *TII, 128 bool ShouldCombineAggressively) { 129 switch (MI.getOpcode()) { 130 case Hexagon::A2_tfr: { 131 // A COPY instruction can be combined if its arguments are IntRegs (32bit). 132 const MachineOperand &Op0 = MI.getOperand(0); 133 const MachineOperand &Op1 = MI.getOperand(1); 134 assert(Op0.isReg() && Op1.isReg()); 135 136 Register DestReg = Op0.getReg(); 137 Register SrcReg = Op1.getReg(); 138 return Hexagon::IntRegsRegClass.contains(DestReg) && 139 Hexagon::IntRegsRegClass.contains(SrcReg); 140 } 141 142 case Hexagon::A2_tfrsi: { 143 // A transfer-immediate can be combined if its argument is a signed 8bit 144 // value. 145 const MachineOperand &Op0 = MI.getOperand(0); 146 const MachineOperand &Op1 = MI.getOperand(1); 147 assert(Op0.isReg()); 148 149 Register DestReg = Op0.getReg(); 150 // Ensure that TargetFlags are MO_NO_FLAG for a global. This is a 151 // workaround for an ABI bug that prevents GOT relocations on combine 152 // instructions 153 if (!Op1.isImm() && Op1.getTargetFlags() != HexagonII::MO_NO_FLAG) 154 return false; 155 156 // Only combine constant extended A2_tfrsi if we are in aggressive mode. 157 bool NotExt = Op1.isImm() && isInt<8>(Op1.getImm()); 158 return Hexagon::IntRegsRegClass.contains(DestReg) && 159 (ShouldCombineAggressively || NotExt); 160 } 161 162 case Hexagon::V6_vassign: 163 return true; 164 165 default: 166 break; 167 } 168 169 return false; 170 } 171 172 template <unsigned N> static bool isGreaterThanNBitTFRI(const MachineInstr &I) { 173 if (I.getOpcode() == Hexagon::TFRI64_V4 || 174 I.getOpcode() == Hexagon::A2_tfrsi) { 175 const MachineOperand &Op = I.getOperand(1); 176 return !Op.isImm() || !isInt<N>(Op.getImm()); 177 } 178 return false; 179 } 180 181 /// areCombinableOperations - Returns true if the two instruction can be merge 182 /// into a combine (ignoring register constraints). 183 static bool areCombinableOperations(const TargetRegisterInfo *TRI, 184 MachineInstr &HighRegInst, 185 MachineInstr &LowRegInst, bool AllowC64) { 186 unsigned HiOpc = HighRegInst.getOpcode(); 187 unsigned LoOpc = LowRegInst.getOpcode(); 188 189 auto verifyOpc = [](unsigned Opc) -> void { 190 switch (Opc) { 191 case Hexagon::A2_tfr: 192 case Hexagon::A2_tfrsi: 193 case Hexagon::V6_vassign: 194 break; 195 default: 196 llvm_unreachable("Unexpected opcode"); 197 } 198 }; 199 verifyOpc(HiOpc); 200 verifyOpc(LoOpc); 201 202 if (HiOpc == Hexagon::V6_vassign || LoOpc == Hexagon::V6_vassign) 203 return HiOpc == LoOpc; 204 205 if (!AllowC64) { 206 // There is no combine of two constant extended values. 207 if (isGreaterThanNBitTFRI<8>(HighRegInst) && 208 isGreaterThanNBitTFRI<6>(LowRegInst)) 209 return false; 210 } 211 212 // There is a combine of two constant extended values into CONST64, 213 // provided both constants are true immediates. 214 if (isGreaterThanNBitTFRI<16>(HighRegInst) && 215 isGreaterThanNBitTFRI<16>(LowRegInst) && !IsConst64Disabled) 216 return (HighRegInst.getOperand(1).isImm() && 217 LowRegInst.getOperand(1).isImm()); 218 219 // There is no combine of two constant extended values, unless handled above 220 // Make both 8-bit size checks to allow both combine (#,##) and combine(##,#) 221 if (isGreaterThanNBitTFRI<8>(HighRegInst) && 222 isGreaterThanNBitTFRI<8>(LowRegInst)) 223 return false; 224 225 return true; 226 } 227 228 static bool isEvenReg(unsigned Reg) { 229 assert(Register::isPhysicalRegister(Reg)); 230 if (Hexagon::IntRegsRegClass.contains(Reg)) 231 return (Reg - Hexagon::R0) % 2 == 0; 232 if (Hexagon::HvxVRRegClass.contains(Reg)) 233 return (Reg - Hexagon::V0) % 2 == 0; 234 llvm_unreachable("Invalid register"); 235 } 236 237 static void removeKillInfo(MachineInstr &MI, unsigned RegNotKilled) { 238 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) { 239 MachineOperand &Op = MI.getOperand(I); 240 if (!Op.isReg() || Op.getReg() != RegNotKilled || !Op.isKill()) 241 continue; 242 Op.setIsKill(false); 243 } 244 } 245 246 /// Returns true if it is unsafe to move a copy instruction from \p UseReg to 247 /// \p DestReg over the instruction \p MI. 248 static bool isUnsafeToMoveAcross(MachineInstr &MI, unsigned UseReg, 249 unsigned DestReg, 250 const TargetRegisterInfo *TRI) { 251 return (UseReg && (MI.modifiesRegister(UseReg, TRI))) || 252 MI.modifiesRegister(DestReg, TRI) || MI.readsRegister(DestReg, TRI) || 253 MI.hasUnmodeledSideEffects() || MI.isInlineAsm() || 254 MI.isMetaInstruction(); 255 } 256 257 static Register UseReg(const MachineOperand& MO) { 258 return MO.isReg() ? MO.getReg() : Register(); 259 } 260 261 /// isSafeToMoveTogether - Returns true if it is safe to move I1 next to I2 such 262 /// that the two instructions can be paired in a combine. 263 bool HexagonCopyToCombine::isSafeToMoveTogether(MachineInstr &I1, 264 MachineInstr &I2, 265 unsigned I1DestReg, 266 unsigned I2DestReg, 267 bool &DoInsertAtI1) { 268 Register I2UseReg = UseReg(I2.getOperand(1)); 269 270 // It is not safe to move I1 and I2 into one combine if I2 has a true 271 // dependence on I1. 272 if (I2UseReg && I1.modifiesRegister(I2UseReg, TRI)) 273 return false; 274 275 bool isSafe = true; 276 277 // First try to move I2 towards I1. 278 { 279 // A reverse_iterator instantiated like below starts before I2, and I1 280 // respectively. 281 // Look at instructions I in between I2 and (excluding) I1. 282 MachineBasicBlock::reverse_iterator I = ++I2.getIterator().getReverse(); 283 MachineBasicBlock::reverse_iterator End = I1.getIterator().getReverse(); 284 // At 03 we got better results (dhrystone!) by being more conservative. 285 if (!ShouldCombineAggressively) 286 End = ++I1.getIterator().getReverse(); 287 // If I2 kills its operand and we move I2 over an instruction that also 288 // uses I2's use reg we need to modify that (first) instruction to now kill 289 // this reg. 290 unsigned KilledOperand = 0; 291 if (I2.killsRegister(I2UseReg)) 292 KilledOperand = I2UseReg; 293 MachineInstr *KillingInstr = nullptr; 294 295 for (; I != End; ++I) { 296 // If the intervening instruction I: 297 // * modifies I2's use reg 298 // * modifies I2's def reg 299 // * reads I2's def reg 300 // * or has unmodelled side effects 301 // we can't move I2 across it. 302 if (I->isDebugInstr()) 303 continue; 304 305 if (isUnsafeToMoveAcross(*I, I2UseReg, I2DestReg, TRI)) { 306 isSafe = false; 307 break; 308 } 309 310 // Update first use of the killed operand. 311 if (!KillingInstr && KilledOperand && 312 I->readsRegister(KilledOperand, TRI)) 313 KillingInstr = &*I; 314 } 315 if (isSafe) { 316 // Update the intermediate instruction to with the kill flag. 317 if (KillingInstr) { 318 bool Added = KillingInstr->addRegisterKilled(KilledOperand, TRI, true); 319 (void)Added; // suppress compiler warning 320 assert(Added && "Must successfully update kill flag"); 321 removeKillInfo(I2, KilledOperand); 322 } 323 DoInsertAtI1 = true; 324 return true; 325 } 326 } 327 328 // Try to move I1 towards I2. 329 { 330 // Look at instructions I in between I1 and (excluding) I2. 331 MachineBasicBlock::iterator I(I1), End(I2); 332 // At O3 we got better results (dhrystone) by being more conservative here. 333 if (!ShouldCombineAggressively) 334 End = std::next(MachineBasicBlock::iterator(I2)); 335 Register I1UseReg = UseReg(I1.getOperand(1)); 336 // Track killed operands. If we move across an instruction that kills our 337 // operand, we need to update the kill information on the moved I1. It kills 338 // the operand now. 339 MachineInstr *KillingInstr = nullptr; 340 unsigned KilledOperand = 0; 341 342 while(++I != End) { 343 MachineInstr &MI = *I; 344 // If the intervening instruction MI: 345 // * modifies I1's use reg 346 // * modifies I1's def reg 347 // * reads I1's def reg 348 // * or has unmodelled side effects 349 // We introduce this special case because llvm has no api to remove a 350 // kill flag for a register (a removeRegisterKilled() analogous to 351 // addRegisterKilled) that handles aliased register correctly. 352 // * or has a killed aliased register use of I1's use reg 353 // %d4 = A2_tfrpi 16 354 // %r6 = A2_tfr %r9 355 // %r8 = KILL %r8, implicit killed %d4 356 // If we want to move R6 = across the KILL instruction we would have 357 // to remove the implicit killed %d4 operand. For now, we are 358 // conservative and disallow the move. 359 // we can't move I1 across it. 360 if (MI.isDebugInstr()) { 361 if (MI.readsRegister(I1DestReg, TRI)) // Move this instruction after I2. 362 DbgMItoMove.push_back(&MI); 363 continue; 364 } 365 366 if (isUnsafeToMoveAcross(MI, I1UseReg, I1DestReg, TRI) || 367 // Check for an aliased register kill. Bail out if we see one. 368 (!MI.killsRegister(I1UseReg) && MI.killsRegister(I1UseReg, TRI))) 369 return false; 370 371 // Check for an exact kill (registers match). 372 if (I1UseReg && MI.killsRegister(I1UseReg)) { 373 assert(!KillingInstr && "Should only see one killing instruction"); 374 KilledOperand = I1UseReg; 375 KillingInstr = &MI; 376 } 377 } 378 if (KillingInstr) { 379 removeKillInfo(*KillingInstr, KilledOperand); 380 // Update I1 to set the kill flag. This flag will later be picked up by 381 // the new COMBINE instruction. 382 bool Added = I1.addRegisterKilled(KilledOperand, TRI); 383 (void)Added; // suppress compiler warning 384 assert(Added && "Must successfully update kill flag"); 385 } 386 DoInsertAtI1 = false; 387 } 388 389 return true; 390 } 391 392 /// findPotentialNewifiableTFRs - Finds tranfers that feed stores that could be 393 /// newified. (A use of a 64 bit register define can not be newified) 394 void 395 HexagonCopyToCombine::findPotentialNewifiableTFRs(MachineBasicBlock &BB) { 396 DenseMap<unsigned, MachineInstr *> LastDef; 397 for (MachineInstr &MI : BB) { 398 if (MI.isDebugInstr()) 399 continue; 400 401 // Mark TFRs that feed a potential new value store as such. 402 if (TII->mayBeNewStore(MI)) { 403 // Look for uses of TFR instructions. 404 for (unsigned OpdIdx = 0, OpdE = MI.getNumOperands(); OpdIdx != OpdE; 405 ++OpdIdx) { 406 MachineOperand &Op = MI.getOperand(OpdIdx); 407 408 // Skip over anything except register uses. 409 if (!Op.isReg() || !Op.isUse() || !Op.getReg()) 410 continue; 411 412 // Look for the defining instruction. 413 Register Reg = Op.getReg(); 414 MachineInstr *DefInst = LastDef[Reg]; 415 if (!DefInst) 416 continue; 417 if (!isCombinableInstType(*DefInst, TII, ShouldCombineAggressively)) 418 continue; 419 420 // Only close newifiable stores should influence the decision. 421 // Ignore the debug instructions in between. 422 MachineBasicBlock::iterator It(DefInst); 423 unsigned NumInstsToDef = 0; 424 while (&*It != &MI) { 425 if (!It->isDebugInstr()) 426 ++NumInstsToDef; 427 ++It; 428 } 429 430 if (NumInstsToDef > MaxNumOfInstsBetweenNewValueStoreAndTFR) 431 continue; 432 433 PotentiallyNewifiableTFR.insert(DefInst); 434 } 435 // Skip to next instruction. 436 continue; 437 } 438 439 // Put instructions that last defined integer or double registers into the 440 // map. 441 for (MachineOperand &Op : MI.operands()) { 442 if (Op.isReg()) { 443 if (!Op.isDef() || !Op.getReg()) 444 continue; 445 Register Reg = Op.getReg(); 446 if (Hexagon::DoubleRegsRegClass.contains(Reg)) { 447 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) 448 LastDef[*SubRegs] = &MI; 449 } else if (Hexagon::IntRegsRegClass.contains(Reg)) 450 LastDef[Reg] = &MI; 451 } else if (Op.isRegMask()) { 452 for (unsigned Reg : Hexagon::IntRegsRegClass) 453 if (Op.clobbersPhysReg(Reg)) 454 LastDef[Reg] = &MI; 455 } 456 } 457 } 458 } 459 460 bool HexagonCopyToCombine::runOnMachineFunction(MachineFunction &MF) { 461 if (skipFunction(MF.getFunction())) 462 return false; 463 464 if (IsCombinesDisabled) return false; 465 466 bool HasChanged = false; 467 468 // Get target info. 469 ST = &MF.getSubtarget<HexagonSubtarget>(); 470 TRI = ST->getRegisterInfo(); 471 TII = ST->getInstrInfo(); 472 473 const Function &F = MF.getFunction(); 474 bool OptForSize = F.hasFnAttribute(Attribute::OptimizeForSize); 475 476 // Combine aggressively (for code size) 477 ShouldCombineAggressively = 478 MF.getTarget().getOptLevel() <= CodeGenOpt::Default; 479 480 // Disable CONST64 for tiny core since it takes a LD resource. 481 if (!OptForSize && ST->isTinyCore()) 482 IsConst64Disabled = true; 483 484 // Traverse basic blocks. 485 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); BI != BE; 486 ++BI) { 487 PotentiallyNewifiableTFR.clear(); 488 findPotentialNewifiableTFRs(*BI); 489 490 // Traverse instructions in basic block. 491 for(MachineBasicBlock::iterator MI = BI->begin(), End = BI->end(); 492 MI != End;) { 493 MachineInstr &I1 = *MI++; 494 495 if (I1.isDebugInstr()) 496 continue; 497 498 // Don't combine a TFR whose user could be newified (instructions that 499 // define double registers can not be newified - Programmer's Ref Manual 500 // 5.4.2 New-value stores). 501 if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(&I1)) 502 continue; 503 504 // Ignore instructions that are not combinable. 505 if (!isCombinableInstType(I1, TII, ShouldCombineAggressively)) 506 continue; 507 508 // Find a second instruction that can be merged into a combine 509 // instruction. In addition, also find all the debug instructions that 510 // need to be moved along with it. 511 bool DoInsertAtI1 = false; 512 DbgMItoMove.clear(); 513 MachineInstr *I2 = findPairable(I1, DoInsertAtI1, OptForSize); 514 if (I2) { 515 HasChanged = true; 516 combine(I1, *I2, MI, DoInsertAtI1, OptForSize); 517 } 518 } 519 } 520 521 return HasChanged; 522 } 523 524 /// findPairable - Returns an instruction that can be merged with \p I1 into a 525 /// COMBINE instruction or 0 if no such instruction can be found. Returns true 526 /// in \p DoInsertAtI1 if the combine must be inserted at instruction \p I1 527 /// false if the combine must be inserted at the returned instruction. 528 MachineInstr *HexagonCopyToCombine::findPairable(MachineInstr &I1, 529 bool &DoInsertAtI1, 530 bool AllowC64) { 531 MachineBasicBlock::iterator I2 = std::next(MachineBasicBlock::iterator(I1)); 532 while (I2 != I1.getParent()->end() && I2->isDebugInstr()) 533 ++I2; 534 535 Register I1DestReg = I1.getOperand(0).getReg(); 536 537 for (MachineBasicBlock::iterator End = I1.getParent()->end(); I2 != End; 538 ++I2) { 539 // Bail out early if we see a second definition of I1DestReg. 540 if (I2->modifiesRegister(I1DestReg, TRI)) 541 break; 542 543 // Ignore non-combinable instructions. 544 if (!isCombinableInstType(*I2, TII, ShouldCombineAggressively)) 545 continue; 546 547 // Don't combine a TFR whose user could be newified. 548 if (ShouldCombineAggressively && PotentiallyNewifiableTFR.count(&*I2)) 549 continue; 550 551 Register I2DestReg = I2->getOperand(0).getReg(); 552 553 // Check that registers are adjacent and that the first destination register 554 // is even. 555 bool IsI1LowReg = (I2DestReg - I1DestReg) == 1; 556 bool IsI2LowReg = (I1DestReg - I2DestReg) == 1; 557 unsigned FirstRegIndex = IsI1LowReg ? I1DestReg : I2DestReg; 558 if ((!IsI1LowReg && !IsI2LowReg) || !isEvenReg(FirstRegIndex)) 559 continue; 560 561 // Check that the two instructions are combinable. 562 // The order matters because in a A2_tfrsi we might can encode a int8 as 563 // the hi reg operand but only a uint6 as the low reg operand. 564 if ((IsI2LowReg && !areCombinableOperations(TRI, I1, *I2, AllowC64)) || 565 (IsI1LowReg && !areCombinableOperations(TRI, *I2, I1, AllowC64))) 566 break; 567 568 if (isSafeToMoveTogether(I1, *I2, I1DestReg, I2DestReg, DoInsertAtI1)) 569 return &*I2; 570 571 // Not safe. Stop searching. 572 break; 573 } 574 return nullptr; 575 } 576 577 void HexagonCopyToCombine::combine(MachineInstr &I1, MachineInstr &I2, 578 MachineBasicBlock::iterator &MI, 579 bool DoInsertAtI1, bool OptForSize) { 580 // We are going to delete I2. If MI points to I2 advance it to the next 581 // instruction. 582 if (MI == I2.getIterator()) 583 ++MI; 584 585 // Figure out whether I1 or I2 goes into the lowreg part. 586 Register I1DestReg = I1.getOperand(0).getReg(); 587 Register I2DestReg = I2.getOperand(0).getReg(); 588 bool IsI1Loreg = (I2DestReg - I1DestReg) == 1; 589 unsigned LoRegDef = IsI1Loreg ? I1DestReg : I2DestReg; 590 unsigned SubLo; 591 592 const TargetRegisterClass *SuperRC = nullptr; 593 if (Hexagon::IntRegsRegClass.contains(LoRegDef)) { 594 SuperRC = &Hexagon::DoubleRegsRegClass; 595 SubLo = Hexagon::isub_lo; 596 } else if (Hexagon::HvxVRRegClass.contains(LoRegDef)) { 597 assert(ST->useHVXOps()); 598 SuperRC = &Hexagon::HvxWRRegClass; 599 SubLo = Hexagon::vsub_lo; 600 } else 601 llvm_unreachable("Unexpected register class"); 602 603 // Get the double word register. 604 unsigned DoubleRegDest = TRI->getMatchingSuperReg(LoRegDef, SubLo, SuperRC); 605 assert(DoubleRegDest != 0 && "Expect a valid register"); 606 607 // Setup source operands. 608 MachineOperand &LoOperand = IsI1Loreg ? I1.getOperand(1) : I2.getOperand(1); 609 MachineOperand &HiOperand = IsI1Loreg ? I2.getOperand(1) : I1.getOperand(1); 610 611 // Figure out which source is a register and which a constant. 612 bool IsHiReg = HiOperand.isReg(); 613 bool IsLoReg = LoOperand.isReg(); 614 615 // There is a combine of two constant extended values into CONST64. 616 bool IsC64 = OptForSize && LoOperand.isImm() && HiOperand.isImm() && 617 isGreaterThanNBitTFRI<16>(I1) && isGreaterThanNBitTFRI<16>(I2); 618 619 MachineBasicBlock::iterator InsertPt(DoInsertAtI1 ? I1 : I2); 620 // Emit combine. 621 if (IsHiReg && IsLoReg) 622 emitCombineRR(InsertPt, DoubleRegDest, HiOperand, LoOperand); 623 else if (IsHiReg) 624 emitCombineRI(InsertPt, DoubleRegDest, HiOperand, LoOperand); 625 else if (IsLoReg) 626 emitCombineIR(InsertPt, DoubleRegDest, HiOperand, LoOperand); 627 else if (IsC64 && !IsConst64Disabled) 628 emitConst64(InsertPt, DoubleRegDest, HiOperand, LoOperand); 629 else 630 emitCombineII(InsertPt, DoubleRegDest, HiOperand, LoOperand); 631 632 // Move debug instructions along with I1 if it's being 633 // moved towards I2. 634 if (!DoInsertAtI1 && DbgMItoMove.size() != 0) { 635 // Insert debug instructions at the new location before I2. 636 MachineBasicBlock *BB = InsertPt->getParent(); 637 for (auto NewMI : DbgMItoMove) { 638 // If iterator MI is pointing to DEBUG_VAL, make sure 639 // MI now points to next relevant instruction. 640 if (NewMI == MI) 641 ++MI; 642 BB->splice(InsertPt, BB, NewMI); 643 } 644 } 645 646 I1.eraseFromParent(); 647 I2.eraseFromParent(); 648 } 649 650 void HexagonCopyToCombine::emitConst64(MachineBasicBlock::iterator &InsertPt, 651 unsigned DoubleDestReg, 652 MachineOperand &HiOperand, 653 MachineOperand &LoOperand) { 654 LLVM_DEBUG(dbgs() << "Found a CONST64\n"); 655 656 DebugLoc DL = InsertPt->getDebugLoc(); 657 MachineBasicBlock *BB = InsertPt->getParent(); 658 assert(LoOperand.isImm() && HiOperand.isImm() && 659 "Both operands must be immediate"); 660 661 int64_t V = HiOperand.getImm(); 662 V = (V << 32) | (0x0ffffffffLL & LoOperand.getImm()); 663 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::CONST64), DoubleDestReg) 664 .addImm(V); 665 } 666 667 void HexagonCopyToCombine::emitCombineII(MachineBasicBlock::iterator &InsertPt, 668 unsigned DoubleDestReg, 669 MachineOperand &HiOperand, 670 MachineOperand &LoOperand) { 671 DebugLoc DL = InsertPt->getDebugLoc(); 672 MachineBasicBlock *BB = InsertPt->getParent(); 673 674 // Handle globals. 675 if (HiOperand.isGlobal()) { 676 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg) 677 .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(), 678 HiOperand.getTargetFlags()) 679 .addImm(LoOperand.getImm()); 680 return; 681 } 682 if (LoOperand.isGlobal()) { 683 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg) 684 .addImm(HiOperand.getImm()) 685 .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(), 686 LoOperand.getTargetFlags()); 687 return; 688 } 689 690 // Handle block addresses. 691 if (HiOperand.isBlockAddress()) { 692 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg) 693 .addBlockAddress(HiOperand.getBlockAddress(), HiOperand.getOffset(), 694 HiOperand.getTargetFlags()) 695 .addImm(LoOperand.getImm()); 696 return; 697 } 698 if (LoOperand.isBlockAddress()) { 699 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg) 700 .addImm(HiOperand.getImm()) 701 .addBlockAddress(LoOperand.getBlockAddress(), LoOperand.getOffset(), 702 LoOperand.getTargetFlags()); 703 return; 704 } 705 706 // Handle jump tables. 707 if (HiOperand.isJTI()) { 708 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg) 709 .addJumpTableIndex(HiOperand.getIndex(), HiOperand.getTargetFlags()) 710 .addImm(LoOperand.getImm()); 711 return; 712 } 713 if (LoOperand.isJTI()) { 714 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg) 715 .addImm(HiOperand.getImm()) 716 .addJumpTableIndex(LoOperand.getIndex(), LoOperand.getTargetFlags()); 717 return; 718 } 719 720 // Handle constant pools. 721 if (HiOperand.isCPI()) { 722 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg) 723 .addConstantPoolIndex(HiOperand.getIndex(), HiOperand.getOffset(), 724 HiOperand.getTargetFlags()) 725 .addImm(LoOperand.getImm()); 726 return; 727 } 728 if (LoOperand.isCPI()) { 729 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg) 730 .addImm(HiOperand.getImm()) 731 .addConstantPoolIndex(LoOperand.getIndex(), LoOperand.getOffset(), 732 LoOperand.getTargetFlags()); 733 return; 734 } 735 736 // First preference should be given to Hexagon::A2_combineii instruction 737 // as it can include U6 (in Hexagon::A4_combineii) as well. 738 // In this instruction, HiOperand is const extended, if required. 739 if (isInt<8>(LoOperand.getImm())) { 740 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg) 741 .addImm(HiOperand.getImm()) 742 .addImm(LoOperand.getImm()); 743 return; 744 } 745 746 // In this instruction, LoOperand is const extended, if required. 747 if (isInt<8>(HiOperand.getImm())) { 748 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineii), DoubleDestReg) 749 .addImm(HiOperand.getImm()) 750 .addImm(LoOperand.getImm()); 751 return; 752 } 753 754 // Insert new combine instruction. 755 // DoubleRegDest = combine #HiImm, #LoImm 756 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A2_combineii), DoubleDestReg) 757 .addImm(HiOperand.getImm()) 758 .addImm(LoOperand.getImm()); 759 } 760 761 void HexagonCopyToCombine::emitCombineIR(MachineBasicBlock::iterator &InsertPt, 762 unsigned DoubleDestReg, 763 MachineOperand &HiOperand, 764 MachineOperand &LoOperand) { 765 Register LoReg = LoOperand.getReg(); 766 unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill()); 767 768 DebugLoc DL = InsertPt->getDebugLoc(); 769 MachineBasicBlock *BB = InsertPt->getParent(); 770 771 // Handle globals. 772 if (HiOperand.isGlobal()) { 773 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg) 774 .addGlobalAddress(HiOperand.getGlobal(), HiOperand.getOffset(), 775 HiOperand.getTargetFlags()) 776 .addReg(LoReg, LoRegKillFlag); 777 return; 778 } 779 // Handle block addresses. 780 if (HiOperand.isBlockAddress()) { 781 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg) 782 .addBlockAddress(HiOperand.getBlockAddress(), HiOperand.getOffset(), 783 HiOperand.getTargetFlags()) 784 .addReg(LoReg, LoRegKillFlag); 785 return; 786 } 787 // Handle jump tables. 788 if (HiOperand.isJTI()) { 789 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg) 790 .addJumpTableIndex(HiOperand.getIndex(), HiOperand.getTargetFlags()) 791 .addReg(LoReg, LoRegKillFlag); 792 return; 793 } 794 // Handle constant pools. 795 if (HiOperand.isCPI()) { 796 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg) 797 .addConstantPoolIndex(HiOperand.getIndex(), HiOperand.getOffset(), 798 HiOperand.getTargetFlags()) 799 .addReg(LoReg, LoRegKillFlag); 800 return; 801 } 802 // Insert new combine instruction. 803 // DoubleRegDest = combine #HiImm, LoReg 804 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineir), DoubleDestReg) 805 .addImm(HiOperand.getImm()) 806 .addReg(LoReg, LoRegKillFlag); 807 } 808 809 void HexagonCopyToCombine::emitCombineRI(MachineBasicBlock::iterator &InsertPt, 810 unsigned DoubleDestReg, 811 MachineOperand &HiOperand, 812 MachineOperand &LoOperand) { 813 unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill()); 814 Register HiReg = HiOperand.getReg(); 815 816 DebugLoc DL = InsertPt->getDebugLoc(); 817 MachineBasicBlock *BB = InsertPt->getParent(); 818 819 // Handle global. 820 if (LoOperand.isGlobal()) { 821 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg) 822 .addReg(HiReg, HiRegKillFlag) 823 .addGlobalAddress(LoOperand.getGlobal(), LoOperand.getOffset(), 824 LoOperand.getTargetFlags()); 825 return; 826 } 827 // Handle block addresses. 828 if (LoOperand.isBlockAddress()) { 829 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg) 830 .addReg(HiReg, HiRegKillFlag) 831 .addBlockAddress(LoOperand.getBlockAddress(), LoOperand.getOffset(), 832 LoOperand.getTargetFlags()); 833 return; 834 } 835 // Handle jump tables. 836 if (LoOperand.isJTI()) { 837 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg) 838 .addReg(HiOperand.getReg(), HiRegKillFlag) 839 .addJumpTableIndex(LoOperand.getIndex(), LoOperand.getTargetFlags()); 840 return; 841 } 842 // Handle constant pools. 843 if (LoOperand.isCPI()) { 844 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg) 845 .addReg(HiOperand.getReg(), HiRegKillFlag) 846 .addConstantPoolIndex(LoOperand.getIndex(), LoOperand.getOffset(), 847 LoOperand.getTargetFlags()); 848 return; 849 } 850 851 // Insert new combine instruction. 852 // DoubleRegDest = combine HiReg, #LoImm 853 BuildMI(*BB, InsertPt, DL, TII->get(Hexagon::A4_combineri), DoubleDestReg) 854 .addReg(HiReg, HiRegKillFlag) 855 .addImm(LoOperand.getImm()); 856 } 857 858 void HexagonCopyToCombine::emitCombineRR(MachineBasicBlock::iterator &InsertPt, 859 unsigned DoubleDestReg, 860 MachineOperand &HiOperand, 861 MachineOperand &LoOperand) { 862 unsigned LoRegKillFlag = getKillRegState(LoOperand.isKill()); 863 unsigned HiRegKillFlag = getKillRegState(HiOperand.isKill()); 864 Register LoReg = LoOperand.getReg(); 865 Register HiReg = HiOperand.getReg(); 866 867 DebugLoc DL = InsertPt->getDebugLoc(); 868 MachineBasicBlock *BB = InsertPt->getParent(); 869 870 // Insert new combine instruction. 871 // DoubleRegDest = combine HiReg, LoReg 872 unsigned NewOpc; 873 if (Hexagon::DoubleRegsRegClass.contains(DoubleDestReg)) { 874 NewOpc = Hexagon::A2_combinew; 875 } else if (Hexagon::HvxWRRegClass.contains(DoubleDestReg)) { 876 assert(ST->useHVXOps()); 877 NewOpc = Hexagon::V6_vcombine; 878 } else 879 llvm_unreachable("Unexpected register"); 880 881 BuildMI(*BB, InsertPt, DL, TII->get(NewOpc), DoubleDestReg) 882 .addReg(HiReg, HiRegKillFlag) 883 .addReg(LoReg, LoRegKillFlag); 884 } 885 886 FunctionPass *llvm::createHexagonCopyToCombine() { 887 return new HexagonCopyToCombine(); 888 } 889