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