1 //=- AArch64LoadStoreOptimizer.cpp - AArch64 load/store opt. pass -*- C++ -*-=// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains a pass that performs load / store related peephole 11 // optimizations. This pass should be run after register allocation. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "AArch64InstrInfo.h" 16 #include "AArch64Subtarget.h" 17 #include "MCTargetDesc/AArch64AddressingModes.h" 18 #include "llvm/ADT/BitVector.h" 19 #include "llvm/ADT/iterator_range.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/Statistic.h" 22 #include "llvm/ADT/StringRef.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/MachineOperand.h" 29 #include "llvm/IR/DebugLoc.h" 30 #include "llvm/MC/MCRegisterInfo.h" 31 #include "llvm/Pass.h" 32 #include "llvm/Support/CommandLine.h" 33 #include "llvm/Support/Debug.h" 34 #include "llvm/Support/ErrorHandling.h" 35 #include "llvm/Support/raw_ostream.h" 36 #include "llvm/Target/TargetRegisterInfo.h" 37 #include <cassert> 38 #include <cstdint> 39 #include <iterator> 40 #include <limits> 41 42 using namespace llvm; 43 44 #define DEBUG_TYPE "aarch64-ldst-opt" 45 46 STATISTIC(NumPairCreated, "Number of load/store pair instructions generated"); 47 STATISTIC(NumPostFolded, "Number of post-index updates folded"); 48 STATISTIC(NumPreFolded, "Number of pre-index updates folded"); 49 STATISTIC(NumUnscaledPairCreated, 50 "Number of load/store from unscaled generated"); 51 STATISTIC(NumZeroStoresPromoted, "Number of narrow zero stores promoted"); 52 STATISTIC(NumLoadsFromStoresPromoted, "Number of loads from stores promoted"); 53 54 // The LdStLimit limits how far we search for load/store pairs. 55 static cl::opt<unsigned> LdStLimit("aarch64-load-store-scan-limit", 56 cl::init(20), cl::Hidden); 57 58 // The UpdateLimit limits how far we search for update instructions when we form 59 // pre-/post-index instructions. 60 static cl::opt<unsigned> UpdateLimit("aarch64-update-scan-limit", cl::init(100), 61 cl::Hidden); 62 63 #define AARCH64_LOAD_STORE_OPT_NAME "AArch64 load / store optimization pass" 64 65 namespace { 66 67 typedef struct LdStPairFlags { 68 // If a matching instruction is found, MergeForward is set to true if the 69 // merge is to remove the first instruction and replace the second with 70 // a pair-wise insn, and false if the reverse is true. 71 bool MergeForward = false; 72 73 // SExtIdx gives the index of the result of the load pair that must be 74 // extended. The value of SExtIdx assumes that the paired load produces the 75 // value in this order: (I, returned iterator), i.e., -1 means no value has 76 // to be extended, 0 means I, and 1 means the returned iterator. 77 int SExtIdx = -1; 78 79 LdStPairFlags() = default; 80 81 void setMergeForward(bool V = true) { MergeForward = V; } 82 bool getMergeForward() const { return MergeForward; } 83 84 void setSExtIdx(int V) { SExtIdx = V; } 85 int getSExtIdx() const { return SExtIdx; } 86 87 } LdStPairFlags; 88 89 struct AArch64LoadStoreOpt : public MachineFunctionPass { 90 static char ID; 91 92 AArch64LoadStoreOpt() : MachineFunctionPass(ID) { 93 initializeAArch64LoadStoreOptPass(*PassRegistry::getPassRegistry()); 94 } 95 96 const AArch64InstrInfo *TII; 97 const TargetRegisterInfo *TRI; 98 const AArch64Subtarget *Subtarget; 99 100 // Track which registers have been modified and used. 101 BitVector ModifiedRegs, UsedRegs; 102 103 // Scan the instructions looking for a load/store that can be combined 104 // with the current instruction into a load/store pair. 105 // Return the matching instruction if one is found, else MBB->end(). 106 MachineBasicBlock::iterator findMatchingInsn(MachineBasicBlock::iterator I, 107 LdStPairFlags &Flags, 108 unsigned Limit, 109 bool FindNarrowMerge); 110 111 // Scan the instructions looking for a store that writes to the address from 112 // which the current load instruction reads. Return true if one is found. 113 bool findMatchingStore(MachineBasicBlock::iterator I, unsigned Limit, 114 MachineBasicBlock::iterator &StoreI); 115 116 // Merge the two instructions indicated into a wider narrow store instruction. 117 MachineBasicBlock::iterator 118 mergeNarrowZeroStores(MachineBasicBlock::iterator I, 119 MachineBasicBlock::iterator MergeMI, 120 const LdStPairFlags &Flags); 121 122 // Merge the two instructions indicated into a single pair-wise instruction. 123 MachineBasicBlock::iterator 124 mergePairedInsns(MachineBasicBlock::iterator I, 125 MachineBasicBlock::iterator Paired, 126 const LdStPairFlags &Flags); 127 128 // Promote the load that reads directly from the address stored to. 129 MachineBasicBlock::iterator 130 promoteLoadFromStore(MachineBasicBlock::iterator LoadI, 131 MachineBasicBlock::iterator StoreI); 132 133 // Scan the instruction list to find a base register update that can 134 // be combined with the current instruction (a load or store) using 135 // pre or post indexed addressing with writeback. Scan forwards. 136 MachineBasicBlock::iterator 137 findMatchingUpdateInsnForward(MachineBasicBlock::iterator I, 138 int UnscaledOffset, unsigned Limit); 139 140 // Scan the instruction list to find a base register update that can 141 // be combined with the current instruction (a load or store) using 142 // pre or post indexed addressing with writeback. Scan backwards. 143 MachineBasicBlock::iterator 144 findMatchingUpdateInsnBackward(MachineBasicBlock::iterator I, unsigned Limit); 145 146 // Find an instruction that updates the base register of the ld/st 147 // instruction. 148 bool isMatchingUpdateInsn(MachineInstr &MemMI, MachineInstr &MI, 149 unsigned BaseReg, int Offset); 150 151 // Merge a pre- or post-index base register update into a ld/st instruction. 152 MachineBasicBlock::iterator 153 mergeUpdateInsn(MachineBasicBlock::iterator I, 154 MachineBasicBlock::iterator Update, bool IsPreIdx); 155 156 // Find and merge zero store instructions. 157 bool tryToMergeZeroStInst(MachineBasicBlock::iterator &MBBI); 158 159 // Find and pair ldr/str instructions. 160 bool tryToPairLdStInst(MachineBasicBlock::iterator &MBBI); 161 162 // Find and promote load instructions which read directly from store. 163 bool tryToPromoteLoadFromStore(MachineBasicBlock::iterator &MBBI); 164 165 bool optimizeBlock(MachineBasicBlock &MBB, bool EnableNarrowZeroStOpt); 166 167 bool runOnMachineFunction(MachineFunction &Fn) override; 168 169 MachineFunctionProperties getRequiredProperties() const override { 170 return MachineFunctionProperties().set( 171 MachineFunctionProperties::Property::NoVRegs); 172 } 173 174 StringRef getPassName() const override { return AARCH64_LOAD_STORE_OPT_NAME; } 175 }; 176 177 char AArch64LoadStoreOpt::ID = 0; 178 179 } // end anonymous namespace 180 181 INITIALIZE_PASS(AArch64LoadStoreOpt, "aarch64-ldst-opt", 182 AARCH64_LOAD_STORE_OPT_NAME, false, false) 183 184 static bool isNarrowStore(unsigned Opc) { 185 switch (Opc) { 186 default: 187 return false; 188 case AArch64::STRBBui: 189 case AArch64::STURBBi: 190 case AArch64::STRHHui: 191 case AArch64::STURHHi: 192 return true; 193 } 194 } 195 196 // Scaling factor for unscaled load or store. 197 static int getMemScale(MachineInstr &MI) { 198 switch (MI.getOpcode()) { 199 default: 200 llvm_unreachable("Opcode has unknown scale!"); 201 case AArch64::LDRBBui: 202 case AArch64::LDURBBi: 203 case AArch64::LDRSBWui: 204 case AArch64::LDURSBWi: 205 case AArch64::STRBBui: 206 case AArch64::STURBBi: 207 return 1; 208 case AArch64::LDRHHui: 209 case AArch64::LDURHHi: 210 case AArch64::LDRSHWui: 211 case AArch64::LDURSHWi: 212 case AArch64::STRHHui: 213 case AArch64::STURHHi: 214 return 2; 215 case AArch64::LDRSui: 216 case AArch64::LDURSi: 217 case AArch64::LDRSWui: 218 case AArch64::LDURSWi: 219 case AArch64::LDRWui: 220 case AArch64::LDURWi: 221 case AArch64::STRSui: 222 case AArch64::STURSi: 223 case AArch64::STRWui: 224 case AArch64::STURWi: 225 case AArch64::LDPSi: 226 case AArch64::LDPSWi: 227 case AArch64::LDPWi: 228 case AArch64::STPSi: 229 case AArch64::STPWi: 230 return 4; 231 case AArch64::LDRDui: 232 case AArch64::LDURDi: 233 case AArch64::LDRXui: 234 case AArch64::LDURXi: 235 case AArch64::STRDui: 236 case AArch64::STURDi: 237 case AArch64::STRXui: 238 case AArch64::STURXi: 239 case AArch64::LDPDi: 240 case AArch64::LDPXi: 241 case AArch64::STPDi: 242 case AArch64::STPXi: 243 return 8; 244 case AArch64::LDRQui: 245 case AArch64::LDURQi: 246 case AArch64::STRQui: 247 case AArch64::STURQi: 248 case AArch64::LDPQi: 249 case AArch64::STPQi: 250 return 16; 251 } 252 } 253 254 static unsigned getMatchingNonSExtOpcode(unsigned Opc, 255 bool *IsValidLdStrOpc = nullptr) { 256 if (IsValidLdStrOpc) 257 *IsValidLdStrOpc = true; 258 switch (Opc) { 259 default: 260 if (IsValidLdStrOpc) 261 *IsValidLdStrOpc = false; 262 return std::numeric_limits<unsigned>::max(); 263 case AArch64::STRDui: 264 case AArch64::STURDi: 265 case AArch64::STRQui: 266 case AArch64::STURQi: 267 case AArch64::STRBBui: 268 case AArch64::STURBBi: 269 case AArch64::STRHHui: 270 case AArch64::STURHHi: 271 case AArch64::STRWui: 272 case AArch64::STURWi: 273 case AArch64::STRXui: 274 case AArch64::STURXi: 275 case AArch64::LDRDui: 276 case AArch64::LDURDi: 277 case AArch64::LDRQui: 278 case AArch64::LDURQi: 279 case AArch64::LDRWui: 280 case AArch64::LDURWi: 281 case AArch64::LDRXui: 282 case AArch64::LDURXi: 283 case AArch64::STRSui: 284 case AArch64::STURSi: 285 case AArch64::LDRSui: 286 case AArch64::LDURSi: 287 return Opc; 288 case AArch64::LDRSWui: 289 return AArch64::LDRWui; 290 case AArch64::LDURSWi: 291 return AArch64::LDURWi; 292 } 293 } 294 295 static unsigned getMatchingWideOpcode(unsigned Opc) { 296 switch (Opc) { 297 default: 298 llvm_unreachable("Opcode has no wide equivalent!"); 299 case AArch64::STRBBui: 300 return AArch64::STRHHui; 301 case AArch64::STRHHui: 302 return AArch64::STRWui; 303 case AArch64::STURBBi: 304 return AArch64::STURHHi; 305 case AArch64::STURHHi: 306 return AArch64::STURWi; 307 case AArch64::STURWi: 308 return AArch64::STURXi; 309 case AArch64::STRWui: 310 return AArch64::STRXui; 311 } 312 } 313 314 static unsigned getMatchingPairOpcode(unsigned Opc) { 315 switch (Opc) { 316 default: 317 llvm_unreachable("Opcode has no pairwise equivalent!"); 318 case AArch64::STRSui: 319 case AArch64::STURSi: 320 return AArch64::STPSi; 321 case AArch64::STRDui: 322 case AArch64::STURDi: 323 return AArch64::STPDi; 324 case AArch64::STRQui: 325 case AArch64::STURQi: 326 return AArch64::STPQi; 327 case AArch64::STRWui: 328 case AArch64::STURWi: 329 return AArch64::STPWi; 330 case AArch64::STRXui: 331 case AArch64::STURXi: 332 return AArch64::STPXi; 333 case AArch64::LDRSui: 334 case AArch64::LDURSi: 335 return AArch64::LDPSi; 336 case AArch64::LDRDui: 337 case AArch64::LDURDi: 338 return AArch64::LDPDi; 339 case AArch64::LDRQui: 340 case AArch64::LDURQi: 341 return AArch64::LDPQi; 342 case AArch64::LDRWui: 343 case AArch64::LDURWi: 344 return AArch64::LDPWi; 345 case AArch64::LDRXui: 346 case AArch64::LDURXi: 347 return AArch64::LDPXi; 348 case AArch64::LDRSWui: 349 case AArch64::LDURSWi: 350 return AArch64::LDPSWi; 351 } 352 } 353 354 static unsigned isMatchingStore(MachineInstr &LoadInst, 355 MachineInstr &StoreInst) { 356 unsigned LdOpc = LoadInst.getOpcode(); 357 unsigned StOpc = StoreInst.getOpcode(); 358 switch (LdOpc) { 359 default: 360 llvm_unreachable("Unsupported load instruction!"); 361 case AArch64::LDRBBui: 362 return StOpc == AArch64::STRBBui || StOpc == AArch64::STRHHui || 363 StOpc == AArch64::STRWui || StOpc == AArch64::STRXui; 364 case AArch64::LDURBBi: 365 return StOpc == AArch64::STURBBi || StOpc == AArch64::STURHHi || 366 StOpc == AArch64::STURWi || StOpc == AArch64::STURXi; 367 case AArch64::LDRHHui: 368 return StOpc == AArch64::STRHHui || StOpc == AArch64::STRWui || 369 StOpc == AArch64::STRXui; 370 case AArch64::LDURHHi: 371 return StOpc == AArch64::STURHHi || StOpc == AArch64::STURWi || 372 StOpc == AArch64::STURXi; 373 case AArch64::LDRWui: 374 return StOpc == AArch64::STRWui || StOpc == AArch64::STRXui; 375 case AArch64::LDURWi: 376 return StOpc == AArch64::STURWi || StOpc == AArch64::STURXi; 377 case AArch64::LDRXui: 378 return StOpc == AArch64::STRXui; 379 case AArch64::LDURXi: 380 return StOpc == AArch64::STURXi; 381 } 382 } 383 384 static unsigned getPreIndexedOpcode(unsigned Opc) { 385 switch (Opc) { 386 default: 387 llvm_unreachable("Opcode has no pre-indexed equivalent!"); 388 case AArch64::STRSui: 389 return AArch64::STRSpre; 390 case AArch64::STRDui: 391 return AArch64::STRDpre; 392 case AArch64::STRQui: 393 return AArch64::STRQpre; 394 case AArch64::STRBBui: 395 return AArch64::STRBBpre; 396 case AArch64::STRHHui: 397 return AArch64::STRHHpre; 398 case AArch64::STRWui: 399 return AArch64::STRWpre; 400 case AArch64::STRXui: 401 return AArch64::STRXpre; 402 case AArch64::LDRSui: 403 return AArch64::LDRSpre; 404 case AArch64::LDRDui: 405 return AArch64::LDRDpre; 406 case AArch64::LDRQui: 407 return AArch64::LDRQpre; 408 case AArch64::LDRBBui: 409 return AArch64::LDRBBpre; 410 case AArch64::LDRHHui: 411 return AArch64::LDRHHpre; 412 case AArch64::LDRWui: 413 return AArch64::LDRWpre; 414 case AArch64::LDRXui: 415 return AArch64::LDRXpre; 416 case AArch64::LDRSWui: 417 return AArch64::LDRSWpre; 418 case AArch64::LDPSi: 419 return AArch64::LDPSpre; 420 case AArch64::LDPSWi: 421 return AArch64::LDPSWpre; 422 case AArch64::LDPDi: 423 return AArch64::LDPDpre; 424 case AArch64::LDPQi: 425 return AArch64::LDPQpre; 426 case AArch64::LDPWi: 427 return AArch64::LDPWpre; 428 case AArch64::LDPXi: 429 return AArch64::LDPXpre; 430 case AArch64::STPSi: 431 return AArch64::STPSpre; 432 case AArch64::STPDi: 433 return AArch64::STPDpre; 434 case AArch64::STPQi: 435 return AArch64::STPQpre; 436 case AArch64::STPWi: 437 return AArch64::STPWpre; 438 case AArch64::STPXi: 439 return AArch64::STPXpre; 440 } 441 } 442 443 static unsigned getPostIndexedOpcode(unsigned Opc) { 444 switch (Opc) { 445 default: 446 llvm_unreachable("Opcode has no post-indexed wise equivalent!"); 447 case AArch64::STRSui: 448 return AArch64::STRSpost; 449 case AArch64::STRDui: 450 return AArch64::STRDpost; 451 case AArch64::STRQui: 452 return AArch64::STRQpost; 453 case AArch64::STRBBui: 454 return AArch64::STRBBpost; 455 case AArch64::STRHHui: 456 return AArch64::STRHHpost; 457 case AArch64::STRWui: 458 return AArch64::STRWpost; 459 case AArch64::STRXui: 460 return AArch64::STRXpost; 461 case AArch64::LDRSui: 462 return AArch64::LDRSpost; 463 case AArch64::LDRDui: 464 return AArch64::LDRDpost; 465 case AArch64::LDRQui: 466 return AArch64::LDRQpost; 467 case AArch64::LDRBBui: 468 return AArch64::LDRBBpost; 469 case AArch64::LDRHHui: 470 return AArch64::LDRHHpost; 471 case AArch64::LDRWui: 472 return AArch64::LDRWpost; 473 case AArch64::LDRXui: 474 return AArch64::LDRXpost; 475 case AArch64::LDRSWui: 476 return AArch64::LDRSWpost; 477 case AArch64::LDPSi: 478 return AArch64::LDPSpost; 479 case AArch64::LDPSWi: 480 return AArch64::LDPSWpost; 481 case AArch64::LDPDi: 482 return AArch64::LDPDpost; 483 case AArch64::LDPQi: 484 return AArch64::LDPQpost; 485 case AArch64::LDPWi: 486 return AArch64::LDPWpost; 487 case AArch64::LDPXi: 488 return AArch64::LDPXpost; 489 case AArch64::STPSi: 490 return AArch64::STPSpost; 491 case AArch64::STPDi: 492 return AArch64::STPDpost; 493 case AArch64::STPQi: 494 return AArch64::STPQpost; 495 case AArch64::STPWi: 496 return AArch64::STPWpost; 497 case AArch64::STPXi: 498 return AArch64::STPXpost; 499 } 500 } 501 502 static bool isPairedLdSt(const MachineInstr &MI) { 503 switch (MI.getOpcode()) { 504 default: 505 return false; 506 case AArch64::LDPSi: 507 case AArch64::LDPSWi: 508 case AArch64::LDPDi: 509 case AArch64::LDPQi: 510 case AArch64::LDPWi: 511 case AArch64::LDPXi: 512 case AArch64::STPSi: 513 case AArch64::STPDi: 514 case AArch64::STPQi: 515 case AArch64::STPWi: 516 case AArch64::STPXi: 517 return true; 518 } 519 } 520 521 static const MachineOperand &getLdStRegOp(const MachineInstr &MI, 522 unsigned PairedRegOp = 0) { 523 assert(PairedRegOp < 2 && "Unexpected register operand idx."); 524 unsigned Idx = isPairedLdSt(MI) ? PairedRegOp : 0; 525 return MI.getOperand(Idx); 526 } 527 528 static const MachineOperand &getLdStBaseOp(const MachineInstr &MI) { 529 unsigned Idx = isPairedLdSt(MI) ? 2 : 1; 530 return MI.getOperand(Idx); 531 } 532 533 static const MachineOperand &getLdStOffsetOp(const MachineInstr &MI) { 534 unsigned Idx = isPairedLdSt(MI) ? 3 : 2; 535 return MI.getOperand(Idx); 536 } 537 538 static bool isLdOffsetInRangeOfSt(MachineInstr &LoadInst, 539 MachineInstr &StoreInst, 540 const AArch64InstrInfo *TII) { 541 assert(isMatchingStore(LoadInst, StoreInst) && "Expect only matched ld/st."); 542 int LoadSize = getMemScale(LoadInst); 543 int StoreSize = getMemScale(StoreInst); 544 int UnscaledStOffset = TII->isUnscaledLdSt(StoreInst) 545 ? getLdStOffsetOp(StoreInst).getImm() 546 : getLdStOffsetOp(StoreInst).getImm() * StoreSize; 547 int UnscaledLdOffset = TII->isUnscaledLdSt(LoadInst) 548 ? getLdStOffsetOp(LoadInst).getImm() 549 : getLdStOffsetOp(LoadInst).getImm() * LoadSize; 550 return (UnscaledStOffset <= UnscaledLdOffset) && 551 (UnscaledLdOffset + LoadSize <= (UnscaledStOffset + StoreSize)); 552 } 553 554 static bool isPromotableZeroStoreInst(MachineInstr &MI) { 555 unsigned Opc = MI.getOpcode(); 556 return (Opc == AArch64::STRWui || Opc == AArch64::STURWi || 557 isNarrowStore(Opc)) && 558 getLdStRegOp(MI).getReg() == AArch64::WZR; 559 } 560 561 MachineBasicBlock::iterator 562 AArch64LoadStoreOpt::mergeNarrowZeroStores(MachineBasicBlock::iterator I, 563 MachineBasicBlock::iterator MergeMI, 564 const LdStPairFlags &Flags) { 565 assert(isPromotableZeroStoreInst(*I) && isPromotableZeroStoreInst(*MergeMI) && 566 "Expected promotable zero stores."); 567 568 MachineBasicBlock::iterator NextI = I; 569 ++NextI; 570 // If NextI is the second of the two instructions to be merged, we need 571 // to skip one further. Either way we merge will invalidate the iterator, 572 // and we don't need to scan the new instruction, as it's a pairwise 573 // instruction, which we're not considering for further action anyway. 574 if (NextI == MergeMI) 575 ++NextI; 576 577 unsigned Opc = I->getOpcode(); 578 bool IsScaled = !TII->isUnscaledLdSt(Opc); 579 int OffsetStride = IsScaled ? 1 : getMemScale(*I); 580 581 bool MergeForward = Flags.getMergeForward(); 582 // Insert our new paired instruction after whichever of the paired 583 // instructions MergeForward indicates. 584 MachineBasicBlock::iterator InsertionPoint = MergeForward ? MergeMI : I; 585 // Also based on MergeForward is from where we copy the base register operand 586 // so we get the flags compatible with the input code. 587 const MachineOperand &BaseRegOp = 588 MergeForward ? getLdStBaseOp(*MergeMI) : getLdStBaseOp(*I); 589 590 // Which register is Rt and which is Rt2 depends on the offset order. 591 MachineInstr *RtMI; 592 if (getLdStOffsetOp(*I).getImm() == 593 getLdStOffsetOp(*MergeMI).getImm() + OffsetStride) 594 RtMI = &*MergeMI; 595 else 596 RtMI = &*I; 597 598 int OffsetImm = getLdStOffsetOp(*RtMI).getImm(); 599 // Change the scaled offset from small to large type. 600 if (IsScaled) { 601 assert(((OffsetImm & 1) == 0) && "Unexpected offset to merge"); 602 OffsetImm /= 2; 603 } 604 605 // Construct the new instruction. 606 DebugLoc DL = I->getDebugLoc(); 607 MachineBasicBlock *MBB = I->getParent(); 608 MachineInstrBuilder MIB; 609 MIB = BuildMI(*MBB, InsertionPoint, DL, TII->get(getMatchingWideOpcode(Opc))) 610 .addReg(isNarrowStore(Opc) ? AArch64::WZR : AArch64::XZR) 611 .add(BaseRegOp) 612 .addImm(OffsetImm) 613 .setMemRefs(I->mergeMemRefsWith(*MergeMI)); 614 (void)MIB; 615 616 DEBUG(dbgs() << "Creating wider store. Replacing instructions:\n "); 617 DEBUG(I->print(dbgs())); 618 DEBUG(dbgs() << " "); 619 DEBUG(MergeMI->print(dbgs())); 620 DEBUG(dbgs() << " with instruction:\n "); 621 DEBUG(((MachineInstr *)MIB)->print(dbgs())); 622 DEBUG(dbgs() << "\n"); 623 624 // Erase the old instructions. 625 I->eraseFromParent(); 626 MergeMI->eraseFromParent(); 627 return NextI; 628 } 629 630 MachineBasicBlock::iterator 631 AArch64LoadStoreOpt::mergePairedInsns(MachineBasicBlock::iterator I, 632 MachineBasicBlock::iterator Paired, 633 const LdStPairFlags &Flags) { 634 MachineBasicBlock::iterator NextI = I; 635 ++NextI; 636 // If NextI is the second of the two instructions to be merged, we need 637 // to skip one further. Either way we merge will invalidate the iterator, 638 // and we don't need to scan the new instruction, as it's a pairwise 639 // instruction, which we're not considering for further action anyway. 640 if (NextI == Paired) 641 ++NextI; 642 643 int SExtIdx = Flags.getSExtIdx(); 644 unsigned Opc = 645 SExtIdx == -1 ? I->getOpcode() : getMatchingNonSExtOpcode(I->getOpcode()); 646 bool IsUnscaled = TII->isUnscaledLdSt(Opc); 647 int OffsetStride = IsUnscaled ? getMemScale(*I) : 1; 648 649 bool MergeForward = Flags.getMergeForward(); 650 // Insert our new paired instruction after whichever of the paired 651 // instructions MergeForward indicates. 652 MachineBasicBlock::iterator InsertionPoint = MergeForward ? Paired : I; 653 // Also based on MergeForward is from where we copy the base register operand 654 // so we get the flags compatible with the input code. 655 const MachineOperand &BaseRegOp = 656 MergeForward ? getLdStBaseOp(*Paired) : getLdStBaseOp(*I); 657 658 int Offset = getLdStOffsetOp(*I).getImm(); 659 int PairedOffset = getLdStOffsetOp(*Paired).getImm(); 660 bool PairedIsUnscaled = TII->isUnscaledLdSt(Paired->getOpcode()); 661 if (IsUnscaled != PairedIsUnscaled) { 662 // We're trying to pair instructions that differ in how they are scaled. If 663 // I is scaled then scale the offset of Paired accordingly. Otherwise, do 664 // the opposite (i.e., make Paired's offset unscaled). 665 int MemSize = getMemScale(*Paired); 666 if (PairedIsUnscaled) { 667 // If the unscaled offset isn't a multiple of the MemSize, we can't 668 // pair the operations together. 669 assert(!(PairedOffset % getMemScale(*Paired)) && 670 "Offset should be a multiple of the stride!"); 671 PairedOffset /= MemSize; 672 } else { 673 PairedOffset *= MemSize; 674 } 675 } 676 677 // Which register is Rt and which is Rt2 depends on the offset order. 678 MachineInstr *RtMI, *Rt2MI; 679 if (Offset == PairedOffset + OffsetStride) { 680 RtMI = &*Paired; 681 Rt2MI = &*I; 682 // Here we swapped the assumption made for SExtIdx. 683 // I.e., we turn ldp I, Paired into ldp Paired, I. 684 // Update the index accordingly. 685 if (SExtIdx != -1) 686 SExtIdx = (SExtIdx + 1) % 2; 687 } else { 688 RtMI = &*I; 689 Rt2MI = &*Paired; 690 } 691 int OffsetImm = getLdStOffsetOp(*RtMI).getImm(); 692 // Scale the immediate offset, if necessary. 693 if (TII->isUnscaledLdSt(RtMI->getOpcode())) { 694 assert(!(OffsetImm % getMemScale(*RtMI)) && 695 "Unscaled offset cannot be scaled."); 696 OffsetImm /= getMemScale(*RtMI); 697 } 698 699 // Construct the new instruction. 700 MachineInstrBuilder MIB; 701 DebugLoc DL = I->getDebugLoc(); 702 MachineBasicBlock *MBB = I->getParent(); 703 MachineOperand RegOp0 = getLdStRegOp(*RtMI); 704 MachineOperand RegOp1 = getLdStRegOp(*Rt2MI); 705 // Kill flags may become invalid when moving stores for pairing. 706 if (RegOp0.isUse()) { 707 if (!MergeForward) { 708 // Clear kill flags on store if moving upwards. Example: 709 // STRWui %w0, ... 710 // USE %w1 711 // STRWui kill %w1 ; need to clear kill flag when moving STRWui upwards 712 RegOp0.setIsKill(false); 713 RegOp1.setIsKill(false); 714 } else { 715 // Clear kill flags of the first stores register. Example: 716 // STRWui %w1, ... 717 // USE kill %w1 ; need to clear kill flag when moving STRWui downwards 718 // STRW %w0 719 unsigned Reg = getLdStRegOp(*I).getReg(); 720 for (MachineInstr &MI : make_range(std::next(I), Paired)) 721 MI.clearRegisterKills(Reg, TRI); 722 } 723 } 724 MIB = BuildMI(*MBB, InsertionPoint, DL, TII->get(getMatchingPairOpcode(Opc))) 725 .add(RegOp0) 726 .add(RegOp1) 727 .add(BaseRegOp) 728 .addImm(OffsetImm) 729 .setMemRefs(I->mergeMemRefsWith(*Paired)); 730 731 (void)MIB; 732 733 DEBUG(dbgs() << "Creating pair load/store. Replacing instructions:\n "); 734 DEBUG(I->print(dbgs())); 735 DEBUG(dbgs() << " "); 736 DEBUG(Paired->print(dbgs())); 737 DEBUG(dbgs() << " with instruction:\n "); 738 if (SExtIdx != -1) { 739 // Generate the sign extension for the proper result of the ldp. 740 // I.e., with X1, that would be: 741 // %W1<def> = KILL %W1, %X1<imp-def> 742 // %X1<def> = SBFMXri %X1<kill>, 0, 31 743 MachineOperand &DstMO = MIB->getOperand(SExtIdx); 744 // Right now, DstMO has the extended register, since it comes from an 745 // extended opcode. 746 unsigned DstRegX = DstMO.getReg(); 747 // Get the W variant of that register. 748 unsigned DstRegW = TRI->getSubReg(DstRegX, AArch64::sub_32); 749 // Update the result of LDP to use the W instead of the X variant. 750 DstMO.setReg(DstRegW); 751 DEBUG(((MachineInstr *)MIB)->print(dbgs())); 752 DEBUG(dbgs() << "\n"); 753 // Make the machine verifier happy by providing a definition for 754 // the X register. 755 // Insert this definition right after the generated LDP, i.e., before 756 // InsertionPoint. 757 MachineInstrBuilder MIBKill = 758 BuildMI(*MBB, InsertionPoint, DL, TII->get(TargetOpcode::KILL), DstRegW) 759 .addReg(DstRegW) 760 .addReg(DstRegX, RegState::Define); 761 MIBKill->getOperand(2).setImplicit(); 762 // Create the sign extension. 763 MachineInstrBuilder MIBSXTW = 764 BuildMI(*MBB, InsertionPoint, DL, TII->get(AArch64::SBFMXri), DstRegX) 765 .addReg(DstRegX) 766 .addImm(0) 767 .addImm(31); 768 (void)MIBSXTW; 769 DEBUG(dbgs() << " Extend operand:\n "); 770 DEBUG(((MachineInstr *)MIBSXTW)->print(dbgs())); 771 } else { 772 DEBUG(((MachineInstr *)MIB)->print(dbgs())); 773 } 774 DEBUG(dbgs() << "\n"); 775 776 // Erase the old instructions. 777 I->eraseFromParent(); 778 Paired->eraseFromParent(); 779 780 return NextI; 781 } 782 783 MachineBasicBlock::iterator 784 AArch64LoadStoreOpt::promoteLoadFromStore(MachineBasicBlock::iterator LoadI, 785 MachineBasicBlock::iterator StoreI) { 786 MachineBasicBlock::iterator NextI = LoadI; 787 ++NextI; 788 789 int LoadSize = getMemScale(*LoadI); 790 int StoreSize = getMemScale(*StoreI); 791 unsigned LdRt = getLdStRegOp(*LoadI).getReg(); 792 unsigned StRt = getLdStRegOp(*StoreI).getReg(); 793 bool IsStoreXReg = TRI->getRegClass(AArch64::GPR64RegClassID)->contains(StRt); 794 795 assert((IsStoreXReg || 796 TRI->getRegClass(AArch64::GPR32RegClassID)->contains(StRt)) && 797 "Unexpected RegClass"); 798 799 MachineInstr *BitExtMI; 800 if (LoadSize == StoreSize && (LoadSize == 4 || LoadSize == 8)) { 801 // Remove the load, if the destination register of the loads is the same 802 // register for stored value. 803 if (StRt == LdRt && LoadSize == 8) { 804 StoreI->clearRegisterKills(StRt, TRI); 805 DEBUG(dbgs() << "Remove load instruction:\n "); 806 DEBUG(LoadI->print(dbgs())); 807 DEBUG(dbgs() << "\n"); 808 LoadI->eraseFromParent(); 809 return NextI; 810 } 811 // Replace the load with a mov if the load and store are in the same size. 812 BitExtMI = 813 BuildMI(*LoadI->getParent(), LoadI, LoadI->getDebugLoc(), 814 TII->get(IsStoreXReg ? AArch64::ORRXrs : AArch64::ORRWrs), LdRt) 815 .addReg(IsStoreXReg ? AArch64::XZR : AArch64::WZR) 816 .addReg(StRt) 817 .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0)); 818 } else { 819 // FIXME: Currently we disable this transformation in big-endian targets as 820 // performance and correctness are verified only in little-endian. 821 if (!Subtarget->isLittleEndian()) 822 return NextI; 823 bool IsUnscaled = TII->isUnscaledLdSt(*LoadI); 824 assert(IsUnscaled == TII->isUnscaledLdSt(*StoreI) && 825 "Unsupported ld/st match"); 826 assert(LoadSize <= StoreSize && "Invalid load size"); 827 int UnscaledLdOffset = IsUnscaled 828 ? getLdStOffsetOp(*LoadI).getImm() 829 : getLdStOffsetOp(*LoadI).getImm() * LoadSize; 830 int UnscaledStOffset = IsUnscaled 831 ? getLdStOffsetOp(*StoreI).getImm() 832 : getLdStOffsetOp(*StoreI).getImm() * StoreSize; 833 int Width = LoadSize * 8; 834 int Immr = 8 * (UnscaledLdOffset - UnscaledStOffset); 835 int Imms = Immr + Width - 1; 836 unsigned DestReg = IsStoreXReg 837 ? TRI->getMatchingSuperReg(LdRt, AArch64::sub_32, 838 &AArch64::GPR64RegClass) 839 : LdRt; 840 841 assert((UnscaledLdOffset >= UnscaledStOffset && 842 (UnscaledLdOffset + LoadSize) <= UnscaledStOffset + StoreSize) && 843 "Invalid offset"); 844 845 Immr = 8 * (UnscaledLdOffset - UnscaledStOffset); 846 Imms = Immr + Width - 1; 847 if (UnscaledLdOffset == UnscaledStOffset) { 848 uint32_t AndMaskEncoded = ((IsStoreXReg ? 1 : 0) << 12) // N 849 | ((Immr) << 6) // immr 850 | ((Imms) << 0) // imms 851 ; 852 853 BitExtMI = 854 BuildMI(*LoadI->getParent(), LoadI, LoadI->getDebugLoc(), 855 TII->get(IsStoreXReg ? AArch64::ANDXri : AArch64::ANDWri), 856 DestReg) 857 .addReg(StRt) 858 .addImm(AndMaskEncoded); 859 } else { 860 BitExtMI = 861 BuildMI(*LoadI->getParent(), LoadI, LoadI->getDebugLoc(), 862 TII->get(IsStoreXReg ? AArch64::UBFMXri : AArch64::UBFMWri), 863 DestReg) 864 .addReg(StRt) 865 .addImm(Immr) 866 .addImm(Imms); 867 } 868 } 869 870 // Clear kill flags between store and load. 871 for (MachineInstr &MI : make_range(StoreI->getIterator(), 872 BitExtMI->getIterator())) 873 MI.clearRegisterKills(StRt, TRI); 874 875 DEBUG(dbgs() << "Promoting load by replacing :\n "); 876 DEBUG(StoreI->print(dbgs())); 877 DEBUG(dbgs() << " "); 878 DEBUG(LoadI->print(dbgs())); 879 DEBUG(dbgs() << " with instructions:\n "); 880 DEBUG(StoreI->print(dbgs())); 881 DEBUG(dbgs() << " "); 882 DEBUG((BitExtMI)->print(dbgs())); 883 DEBUG(dbgs() << "\n"); 884 885 // Erase the old instructions. 886 LoadI->eraseFromParent(); 887 return NextI; 888 } 889 890 /// trackRegDefsUses - Remember what registers the specified instruction uses 891 /// and modifies. 892 static void trackRegDefsUses(const MachineInstr &MI, BitVector &ModifiedRegs, 893 BitVector &UsedRegs, 894 const TargetRegisterInfo *TRI) { 895 for (const MachineOperand &MO : MI.operands()) { 896 if (MO.isRegMask()) 897 ModifiedRegs.setBitsNotInMask(MO.getRegMask()); 898 899 if (!MO.isReg()) 900 continue; 901 unsigned Reg = MO.getReg(); 902 if (!Reg) 903 continue; 904 if (MO.isDef()) { 905 // WZR/XZR are not modified even when used as a destination register. 906 if (Reg != AArch64::WZR && Reg != AArch64::XZR) 907 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) 908 ModifiedRegs.set(*AI); 909 } else { 910 assert(MO.isUse() && "Reg operand not a def and not a use?!?"); 911 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) 912 UsedRegs.set(*AI); 913 } 914 } 915 } 916 917 static bool inBoundsForPair(bool IsUnscaled, int Offset, int OffsetStride) { 918 // Convert the byte-offset used by unscaled into an "element" offset used 919 // by the scaled pair load/store instructions. 920 if (IsUnscaled) { 921 // If the byte-offset isn't a multiple of the stride, there's no point 922 // trying to match it. 923 if (Offset % OffsetStride) 924 return false; 925 Offset /= OffsetStride; 926 } 927 return Offset <= 63 && Offset >= -64; 928 } 929 930 // Do alignment, specialized to power of 2 and for signed ints, 931 // avoiding having to do a C-style cast from uint_64t to int when 932 // using alignTo from include/llvm/Support/MathExtras.h. 933 // FIXME: Move this function to include/MathExtras.h? 934 static int alignTo(int Num, int PowOf2) { 935 return (Num + PowOf2 - 1) & ~(PowOf2 - 1); 936 } 937 938 static bool mayAlias(MachineInstr &MIa, MachineInstr &MIb, 939 const AArch64InstrInfo *TII) { 940 // One of the instructions must modify memory. 941 if (!MIa.mayStore() && !MIb.mayStore()) 942 return false; 943 944 // Both instructions must be memory operations. 945 if (!MIa.mayLoadOrStore() && !MIb.mayLoadOrStore()) 946 return false; 947 948 return !TII->areMemAccessesTriviallyDisjoint(MIa, MIb); 949 } 950 951 static bool mayAlias(MachineInstr &MIa, 952 SmallVectorImpl<MachineInstr *> &MemInsns, 953 const AArch64InstrInfo *TII) { 954 for (MachineInstr *MIb : MemInsns) 955 if (mayAlias(MIa, *MIb, TII)) 956 return true; 957 958 return false; 959 } 960 961 bool AArch64LoadStoreOpt::findMatchingStore( 962 MachineBasicBlock::iterator I, unsigned Limit, 963 MachineBasicBlock::iterator &StoreI) { 964 MachineBasicBlock::iterator B = I->getParent()->begin(); 965 MachineBasicBlock::iterator MBBI = I; 966 MachineInstr &LoadMI = *I; 967 unsigned BaseReg = getLdStBaseOp(LoadMI).getReg(); 968 969 // If the load is the first instruction in the block, there's obviously 970 // not any matching store. 971 if (MBBI == B) 972 return false; 973 974 // Track which registers have been modified and used between the first insn 975 // and the second insn. 976 ModifiedRegs.reset(); 977 UsedRegs.reset(); 978 979 unsigned Count = 0; 980 do { 981 --MBBI; 982 MachineInstr &MI = *MBBI; 983 984 // Don't count transient instructions towards the search limit since there 985 // may be different numbers of them if e.g. debug information is present. 986 if (!MI.isTransient()) 987 ++Count; 988 989 // If the load instruction reads directly from the address to which the 990 // store instruction writes and the stored value is not modified, we can 991 // promote the load. Since we do not handle stores with pre-/post-index, 992 // it's unnecessary to check if BaseReg is modified by the store itself. 993 if (MI.mayStore() && isMatchingStore(LoadMI, MI) && 994 BaseReg == getLdStBaseOp(MI).getReg() && 995 isLdOffsetInRangeOfSt(LoadMI, MI, TII) && 996 !ModifiedRegs[getLdStRegOp(MI).getReg()]) { 997 StoreI = MBBI; 998 return true; 999 } 1000 1001 if (MI.isCall()) 1002 return false; 1003 1004 // Update modified / uses register lists. 1005 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI); 1006 1007 // Otherwise, if the base register is modified, we have no match, so 1008 // return early. 1009 if (ModifiedRegs[BaseReg]) 1010 return false; 1011 1012 // If we encounter a store aliased with the load, return early. 1013 if (MI.mayStore() && mayAlias(LoadMI, MI, TII)) 1014 return false; 1015 } while (MBBI != B && Count < Limit); 1016 return false; 1017 } 1018 1019 // Returns true if FirstMI and MI are candidates for merging or pairing. 1020 // Otherwise, returns false. 1021 static bool areCandidatesToMergeOrPair(MachineInstr &FirstMI, MachineInstr &MI, 1022 LdStPairFlags &Flags, 1023 const AArch64InstrInfo *TII) { 1024 // If this is volatile or if pairing is suppressed, not a candidate. 1025 if (MI.hasOrderedMemoryRef() || TII->isLdStPairSuppressed(MI)) 1026 return false; 1027 1028 // We should have already checked FirstMI for pair suppression and volatility. 1029 assert(!FirstMI.hasOrderedMemoryRef() && 1030 !TII->isLdStPairSuppressed(FirstMI) && 1031 "FirstMI shouldn't get here if either of these checks are true."); 1032 1033 unsigned OpcA = FirstMI.getOpcode(); 1034 unsigned OpcB = MI.getOpcode(); 1035 1036 // Opcodes match: nothing more to check. 1037 if (OpcA == OpcB) 1038 return true; 1039 1040 // Try to match a sign-extended load/store with a zero-extended load/store. 1041 bool IsValidLdStrOpc, PairIsValidLdStrOpc; 1042 unsigned NonSExtOpc = getMatchingNonSExtOpcode(OpcA, &IsValidLdStrOpc); 1043 assert(IsValidLdStrOpc && 1044 "Given Opc should be a Load or Store with an immediate"); 1045 // OpcA will be the first instruction in the pair. 1046 if (NonSExtOpc == getMatchingNonSExtOpcode(OpcB, &PairIsValidLdStrOpc)) { 1047 Flags.setSExtIdx(NonSExtOpc == (unsigned)OpcA ? 1 : 0); 1048 return true; 1049 } 1050 1051 // If the second instruction isn't even a mergable/pairable load/store, bail 1052 // out. 1053 if (!PairIsValidLdStrOpc) 1054 return false; 1055 1056 // FIXME: We don't support merging narrow stores with mixed scaled/unscaled 1057 // offsets. 1058 if (isNarrowStore(OpcA) || isNarrowStore(OpcB)) 1059 return false; 1060 1061 // Try to match an unscaled load/store with a scaled load/store. 1062 return TII->isUnscaledLdSt(OpcA) != TII->isUnscaledLdSt(OpcB) && 1063 getMatchingPairOpcode(OpcA) == getMatchingPairOpcode(OpcB); 1064 1065 // FIXME: Can we also match a mixed sext/zext unscaled/scaled pair? 1066 } 1067 1068 /// Scan the instructions looking for a load/store that can be combined with the 1069 /// current instruction into a wider equivalent or a load/store pair. 1070 MachineBasicBlock::iterator 1071 AArch64LoadStoreOpt::findMatchingInsn(MachineBasicBlock::iterator I, 1072 LdStPairFlags &Flags, unsigned Limit, 1073 bool FindNarrowMerge) { 1074 MachineBasicBlock::iterator E = I->getParent()->end(); 1075 MachineBasicBlock::iterator MBBI = I; 1076 MachineInstr &FirstMI = *I; 1077 ++MBBI; 1078 1079 bool MayLoad = FirstMI.mayLoad(); 1080 bool IsUnscaled = TII->isUnscaledLdSt(FirstMI); 1081 unsigned Reg = getLdStRegOp(FirstMI).getReg(); 1082 unsigned BaseReg = getLdStBaseOp(FirstMI).getReg(); 1083 int Offset = getLdStOffsetOp(FirstMI).getImm(); 1084 int OffsetStride = IsUnscaled ? getMemScale(FirstMI) : 1; 1085 bool IsPromotableZeroStore = isPromotableZeroStoreInst(FirstMI); 1086 1087 // Track which registers have been modified and used between the first insn 1088 // (inclusive) and the second insn. 1089 ModifiedRegs.reset(); 1090 UsedRegs.reset(); 1091 1092 // Remember any instructions that read/write memory between FirstMI and MI. 1093 SmallVector<MachineInstr *, 4> MemInsns; 1094 1095 for (unsigned Count = 0; MBBI != E && Count < Limit; ++MBBI) { 1096 MachineInstr &MI = *MBBI; 1097 1098 // Don't count transient instructions towards the search limit since there 1099 // may be different numbers of them if e.g. debug information is present. 1100 if (!MI.isTransient()) 1101 ++Count; 1102 1103 Flags.setSExtIdx(-1); 1104 if (areCandidatesToMergeOrPair(FirstMI, MI, Flags, TII) && 1105 getLdStOffsetOp(MI).isImm()) { 1106 assert(MI.mayLoadOrStore() && "Expected memory operation."); 1107 // If we've found another instruction with the same opcode, check to see 1108 // if the base and offset are compatible with our starting instruction. 1109 // These instructions all have scaled immediate operands, so we just 1110 // check for +1/-1. Make sure to check the new instruction offset is 1111 // actually an immediate and not a symbolic reference destined for 1112 // a relocation. 1113 unsigned MIBaseReg = getLdStBaseOp(MI).getReg(); 1114 int MIOffset = getLdStOffsetOp(MI).getImm(); 1115 bool MIIsUnscaled = TII->isUnscaledLdSt(MI); 1116 if (IsUnscaled != MIIsUnscaled) { 1117 // We're trying to pair instructions that differ in how they are scaled. 1118 // If FirstMI is scaled then scale the offset of MI accordingly. 1119 // Otherwise, do the opposite (i.e., make MI's offset unscaled). 1120 int MemSize = getMemScale(MI); 1121 if (MIIsUnscaled) { 1122 // If the unscaled offset isn't a multiple of the MemSize, we can't 1123 // pair the operations together: bail and keep looking. 1124 if (MIOffset % MemSize) { 1125 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI); 1126 MemInsns.push_back(&MI); 1127 continue; 1128 } 1129 MIOffset /= MemSize; 1130 } else { 1131 MIOffset *= MemSize; 1132 } 1133 } 1134 1135 if (BaseReg == MIBaseReg && ((Offset == MIOffset + OffsetStride) || 1136 (Offset + OffsetStride == MIOffset))) { 1137 int MinOffset = Offset < MIOffset ? Offset : MIOffset; 1138 if (FindNarrowMerge) { 1139 // If the alignment requirements of the scaled wide load/store 1140 // instruction can't express the offset of the scaled narrow input, 1141 // bail and keep looking. For promotable zero stores, allow only when 1142 // the stored value is the same (i.e., WZR). 1143 if ((!IsUnscaled && alignTo(MinOffset, 2) != MinOffset) || 1144 (IsPromotableZeroStore && Reg != getLdStRegOp(MI).getReg())) { 1145 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI); 1146 MemInsns.push_back(&MI); 1147 continue; 1148 } 1149 } else { 1150 // Pairwise instructions have a 7-bit signed offset field. Single 1151 // insns have a 12-bit unsigned offset field. If the resultant 1152 // immediate offset of merging these instructions is out of range for 1153 // a pairwise instruction, bail and keep looking. 1154 if (!inBoundsForPair(IsUnscaled, MinOffset, OffsetStride)) { 1155 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI); 1156 MemInsns.push_back(&MI); 1157 continue; 1158 } 1159 // If the alignment requirements of the paired (scaled) instruction 1160 // can't express the offset of the unscaled input, bail and keep 1161 // looking. 1162 if (IsUnscaled && (alignTo(MinOffset, OffsetStride) != MinOffset)) { 1163 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI); 1164 MemInsns.push_back(&MI); 1165 continue; 1166 } 1167 } 1168 // If the destination register of the loads is the same register, bail 1169 // and keep looking. A load-pair instruction with both destination 1170 // registers the same is UNPREDICTABLE and will result in an exception. 1171 if (MayLoad && Reg == getLdStRegOp(MI).getReg()) { 1172 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI); 1173 MemInsns.push_back(&MI); 1174 continue; 1175 } 1176 1177 // If the Rt of the second instruction was not modified or used between 1178 // the two instructions and none of the instructions between the second 1179 // and first alias with the second, we can combine the second into the 1180 // first. 1181 if (!ModifiedRegs[getLdStRegOp(MI).getReg()] && 1182 !(MI.mayLoad() && UsedRegs[getLdStRegOp(MI).getReg()]) && 1183 !mayAlias(MI, MemInsns, TII)) { 1184 Flags.setMergeForward(false); 1185 return MBBI; 1186 } 1187 1188 // Likewise, if the Rt of the first instruction is not modified or used 1189 // between the two instructions and none of the instructions between the 1190 // first and the second alias with the first, we can combine the first 1191 // into the second. 1192 if (!ModifiedRegs[getLdStRegOp(FirstMI).getReg()] && 1193 !(MayLoad && UsedRegs[getLdStRegOp(FirstMI).getReg()]) && 1194 !mayAlias(FirstMI, MemInsns, TII)) { 1195 Flags.setMergeForward(true); 1196 return MBBI; 1197 } 1198 // Unable to combine these instructions due to interference in between. 1199 // Keep looking. 1200 } 1201 } 1202 1203 // If the instruction wasn't a matching load or store. Stop searching if we 1204 // encounter a call instruction that might modify memory. 1205 if (MI.isCall()) 1206 return E; 1207 1208 // Update modified / uses register lists. 1209 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI); 1210 1211 // Otherwise, if the base register is modified, we have no match, so 1212 // return early. 1213 if (ModifiedRegs[BaseReg]) 1214 return E; 1215 1216 // Update list of instructions that read/write memory. 1217 if (MI.mayLoadOrStore()) 1218 MemInsns.push_back(&MI); 1219 } 1220 return E; 1221 } 1222 1223 MachineBasicBlock::iterator 1224 AArch64LoadStoreOpt::mergeUpdateInsn(MachineBasicBlock::iterator I, 1225 MachineBasicBlock::iterator Update, 1226 bool IsPreIdx) { 1227 assert((Update->getOpcode() == AArch64::ADDXri || 1228 Update->getOpcode() == AArch64::SUBXri) && 1229 "Unexpected base register update instruction to merge!"); 1230 MachineBasicBlock::iterator NextI = I; 1231 // Return the instruction following the merged instruction, which is 1232 // the instruction following our unmerged load. Unless that's the add/sub 1233 // instruction we're merging, in which case it's the one after that. 1234 if (++NextI == Update) 1235 ++NextI; 1236 1237 int Value = Update->getOperand(2).getImm(); 1238 assert(AArch64_AM::getShiftValue(Update->getOperand(3).getImm()) == 0 && 1239 "Can't merge 1 << 12 offset into pre-/post-indexed load / store"); 1240 if (Update->getOpcode() == AArch64::SUBXri) 1241 Value = -Value; 1242 1243 unsigned NewOpc = IsPreIdx ? getPreIndexedOpcode(I->getOpcode()) 1244 : getPostIndexedOpcode(I->getOpcode()); 1245 MachineInstrBuilder MIB; 1246 if (!isPairedLdSt(*I)) { 1247 // Non-paired instruction. 1248 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(NewOpc)) 1249 .add(getLdStRegOp(*Update)) 1250 .add(getLdStRegOp(*I)) 1251 .add(getLdStBaseOp(*I)) 1252 .addImm(Value) 1253 .setMemRefs(I->memoperands_begin(), I->memoperands_end()); 1254 } else { 1255 // Paired instruction. 1256 int Scale = getMemScale(*I); 1257 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(NewOpc)) 1258 .add(getLdStRegOp(*Update)) 1259 .add(getLdStRegOp(*I, 0)) 1260 .add(getLdStRegOp(*I, 1)) 1261 .add(getLdStBaseOp(*I)) 1262 .addImm(Value / Scale) 1263 .setMemRefs(I->memoperands_begin(), I->memoperands_end()); 1264 } 1265 (void)MIB; 1266 1267 if (IsPreIdx) 1268 DEBUG(dbgs() << "Creating pre-indexed load/store."); 1269 else 1270 DEBUG(dbgs() << "Creating post-indexed load/store."); 1271 DEBUG(dbgs() << " Replacing instructions:\n "); 1272 DEBUG(I->print(dbgs())); 1273 DEBUG(dbgs() << " "); 1274 DEBUG(Update->print(dbgs())); 1275 DEBUG(dbgs() << " with instruction:\n "); 1276 DEBUG(((MachineInstr *)MIB)->print(dbgs())); 1277 DEBUG(dbgs() << "\n"); 1278 1279 // Erase the old instructions for the block. 1280 I->eraseFromParent(); 1281 Update->eraseFromParent(); 1282 1283 return NextI; 1284 } 1285 1286 bool AArch64LoadStoreOpt::isMatchingUpdateInsn(MachineInstr &MemMI, 1287 MachineInstr &MI, 1288 unsigned BaseReg, int Offset) { 1289 switch (MI.getOpcode()) { 1290 default: 1291 break; 1292 case AArch64::SUBXri: 1293 case AArch64::ADDXri: 1294 // Make sure it's a vanilla immediate operand, not a relocation or 1295 // anything else we can't handle. 1296 if (!MI.getOperand(2).isImm()) 1297 break; 1298 // Watch out for 1 << 12 shifted value. 1299 if (AArch64_AM::getShiftValue(MI.getOperand(3).getImm())) 1300 break; 1301 1302 // The update instruction source and destination register must be the 1303 // same as the load/store base register. 1304 if (MI.getOperand(0).getReg() != BaseReg || 1305 MI.getOperand(1).getReg() != BaseReg) 1306 break; 1307 1308 bool IsPairedInsn = isPairedLdSt(MemMI); 1309 int UpdateOffset = MI.getOperand(2).getImm(); 1310 if (MI.getOpcode() == AArch64::SUBXri) 1311 UpdateOffset = -UpdateOffset; 1312 1313 // For non-paired load/store instructions, the immediate must fit in a 1314 // signed 9-bit integer. 1315 if (!IsPairedInsn && (UpdateOffset > 255 || UpdateOffset < -256)) 1316 break; 1317 1318 // For paired load/store instructions, the immediate must be a multiple of 1319 // the scaling factor. The scaled offset must also fit into a signed 7-bit 1320 // integer. 1321 if (IsPairedInsn) { 1322 int Scale = getMemScale(MemMI); 1323 if (UpdateOffset % Scale != 0) 1324 break; 1325 1326 int ScaledOffset = UpdateOffset / Scale; 1327 if (ScaledOffset > 63 || ScaledOffset < -64) 1328 break; 1329 } 1330 1331 // If we have a non-zero Offset, we check that it matches the amount 1332 // we're adding to the register. 1333 if (!Offset || Offset == UpdateOffset) 1334 return true; 1335 break; 1336 } 1337 return false; 1338 } 1339 1340 MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnForward( 1341 MachineBasicBlock::iterator I, int UnscaledOffset, unsigned Limit) { 1342 MachineBasicBlock::iterator E = I->getParent()->end(); 1343 MachineInstr &MemMI = *I; 1344 MachineBasicBlock::iterator MBBI = I; 1345 1346 unsigned BaseReg = getLdStBaseOp(MemMI).getReg(); 1347 int MIUnscaledOffset = getLdStOffsetOp(MemMI).getImm() * getMemScale(MemMI); 1348 1349 // Scan forward looking for post-index opportunities. Updating instructions 1350 // can't be formed if the memory instruction doesn't have the offset we're 1351 // looking for. 1352 if (MIUnscaledOffset != UnscaledOffset) 1353 return E; 1354 1355 // If the base register overlaps a destination register, we can't 1356 // merge the update. 1357 bool IsPairedInsn = isPairedLdSt(MemMI); 1358 for (unsigned i = 0, e = IsPairedInsn ? 2 : 1; i != e; ++i) { 1359 unsigned DestReg = getLdStRegOp(MemMI, i).getReg(); 1360 if (DestReg == BaseReg || TRI->isSubRegister(BaseReg, DestReg)) 1361 return E; 1362 } 1363 1364 // Track which registers have been modified and used between the first insn 1365 // (inclusive) and the second insn. 1366 ModifiedRegs.reset(); 1367 UsedRegs.reset(); 1368 ++MBBI; 1369 for (unsigned Count = 0; MBBI != E && Count < Limit; ++MBBI) { 1370 MachineInstr &MI = *MBBI; 1371 1372 // Don't count transient instructions towards the search limit since there 1373 // may be different numbers of them if e.g. debug information is present. 1374 if (!MI.isTransient()) 1375 ++Count; 1376 1377 // If we found a match, return it. 1378 if (isMatchingUpdateInsn(*I, MI, BaseReg, UnscaledOffset)) 1379 return MBBI; 1380 1381 // Update the status of what the instruction clobbered and used. 1382 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI); 1383 1384 // Otherwise, if the base register is used or modified, we have no match, so 1385 // return early. 1386 if (ModifiedRegs[BaseReg] || UsedRegs[BaseReg]) 1387 return E; 1388 } 1389 return E; 1390 } 1391 1392 MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnBackward( 1393 MachineBasicBlock::iterator I, unsigned Limit) { 1394 MachineBasicBlock::iterator B = I->getParent()->begin(); 1395 MachineBasicBlock::iterator E = I->getParent()->end(); 1396 MachineInstr &MemMI = *I; 1397 MachineBasicBlock::iterator MBBI = I; 1398 1399 unsigned BaseReg = getLdStBaseOp(MemMI).getReg(); 1400 int Offset = getLdStOffsetOp(MemMI).getImm(); 1401 1402 // If the load/store is the first instruction in the block, there's obviously 1403 // not any matching update. Ditto if the memory offset isn't zero. 1404 if (MBBI == B || Offset != 0) 1405 return E; 1406 // If the base register overlaps a destination register, we can't 1407 // merge the update. 1408 bool IsPairedInsn = isPairedLdSt(MemMI); 1409 for (unsigned i = 0, e = IsPairedInsn ? 2 : 1; i != e; ++i) { 1410 unsigned DestReg = getLdStRegOp(MemMI, i).getReg(); 1411 if (DestReg == BaseReg || TRI->isSubRegister(BaseReg, DestReg)) 1412 return E; 1413 } 1414 1415 // Track which registers have been modified and used between the first insn 1416 // (inclusive) and the second insn. 1417 ModifiedRegs.reset(); 1418 UsedRegs.reset(); 1419 unsigned Count = 0; 1420 do { 1421 --MBBI; 1422 MachineInstr &MI = *MBBI; 1423 1424 // Don't count transient instructions towards the search limit since there 1425 // may be different numbers of them if e.g. debug information is present. 1426 if (!MI.isTransient()) 1427 ++Count; 1428 1429 // If we found a match, return it. 1430 if (isMatchingUpdateInsn(*I, MI, BaseReg, Offset)) 1431 return MBBI; 1432 1433 // Update the status of what the instruction clobbered and used. 1434 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI); 1435 1436 // Otherwise, if the base register is used or modified, we have no match, so 1437 // return early. 1438 if (ModifiedRegs[BaseReg] || UsedRegs[BaseReg]) 1439 return E; 1440 } while (MBBI != B && Count < Limit); 1441 return E; 1442 } 1443 1444 bool AArch64LoadStoreOpt::tryToPromoteLoadFromStore( 1445 MachineBasicBlock::iterator &MBBI) { 1446 MachineInstr &MI = *MBBI; 1447 // If this is a volatile load, don't mess with it. 1448 if (MI.hasOrderedMemoryRef()) 1449 return false; 1450 1451 // Make sure this is a reg+imm. 1452 // FIXME: It is possible to extend it to handle reg+reg cases. 1453 if (!getLdStOffsetOp(MI).isImm()) 1454 return false; 1455 1456 // Look backward up to LdStLimit instructions. 1457 MachineBasicBlock::iterator StoreI; 1458 if (findMatchingStore(MBBI, LdStLimit, StoreI)) { 1459 ++NumLoadsFromStoresPromoted; 1460 // Promote the load. Keeping the iterator straight is a 1461 // pain, so we let the merge routine tell us what the next instruction 1462 // is after it's done mucking about. 1463 MBBI = promoteLoadFromStore(MBBI, StoreI); 1464 return true; 1465 } 1466 return false; 1467 } 1468 1469 // Merge adjacent zero stores into a wider store. 1470 bool AArch64LoadStoreOpt::tryToMergeZeroStInst( 1471 MachineBasicBlock::iterator &MBBI) { 1472 assert(isPromotableZeroStoreInst(*MBBI) && "Expected narrow store."); 1473 MachineInstr &MI = *MBBI; 1474 MachineBasicBlock::iterator E = MI.getParent()->end(); 1475 1476 if (!TII->isCandidateToMergeOrPair(MI)) 1477 return false; 1478 1479 // Look ahead up to LdStLimit instructions for a mergable instruction. 1480 LdStPairFlags Flags; 1481 MachineBasicBlock::iterator MergeMI = 1482 findMatchingInsn(MBBI, Flags, LdStLimit, /* FindNarrowMerge = */ true); 1483 if (MergeMI != E) { 1484 ++NumZeroStoresPromoted; 1485 1486 // Keeping the iterator straight is a pain, so we let the merge routine tell 1487 // us what the next instruction is after it's done mucking about. 1488 MBBI = mergeNarrowZeroStores(MBBI, MergeMI, Flags); 1489 return true; 1490 } 1491 return false; 1492 } 1493 1494 // Find loads and stores that can be merged into a single load or store pair 1495 // instruction. 1496 bool AArch64LoadStoreOpt::tryToPairLdStInst(MachineBasicBlock::iterator &MBBI) { 1497 MachineInstr &MI = *MBBI; 1498 MachineBasicBlock::iterator E = MI.getParent()->end(); 1499 1500 if (!TII->isCandidateToMergeOrPair(MI)) 1501 return false; 1502 1503 // Early exit if the offset is not possible to match. (6 bits of positive 1504 // range, plus allow an extra one in case we find a later insn that matches 1505 // with Offset-1) 1506 bool IsUnscaled = TII->isUnscaledLdSt(MI); 1507 int Offset = getLdStOffsetOp(MI).getImm(); 1508 int OffsetStride = IsUnscaled ? getMemScale(MI) : 1; 1509 // Allow one more for offset. 1510 if (Offset > 0) 1511 Offset -= OffsetStride; 1512 if (!inBoundsForPair(IsUnscaled, Offset, OffsetStride)) 1513 return false; 1514 1515 // Look ahead up to LdStLimit instructions for a pairable instruction. 1516 LdStPairFlags Flags; 1517 MachineBasicBlock::iterator Paired = 1518 findMatchingInsn(MBBI, Flags, LdStLimit, /* FindNarrowMerge = */ false); 1519 if (Paired != E) { 1520 ++NumPairCreated; 1521 if (TII->isUnscaledLdSt(MI)) 1522 ++NumUnscaledPairCreated; 1523 // Keeping the iterator straight is a pain, so we let the merge routine tell 1524 // us what the next instruction is after it's done mucking about. 1525 MBBI = mergePairedInsns(MBBI, Paired, Flags); 1526 return true; 1527 } 1528 return false; 1529 } 1530 1531 bool AArch64LoadStoreOpt::optimizeBlock(MachineBasicBlock &MBB, 1532 bool EnableNarrowZeroStOpt) { 1533 bool Modified = false; 1534 // Four tranformations to do here: 1535 // 1) Find loads that directly read from stores and promote them by 1536 // replacing with mov instructions. If the store is wider than the load, 1537 // the load will be replaced with a bitfield extract. 1538 // e.g., 1539 // str w1, [x0, #4] 1540 // ldrh w2, [x0, #6] 1541 // ; becomes 1542 // str w1, [x0, #4] 1543 // lsr w2, w1, #16 1544 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end(); 1545 MBBI != E;) { 1546 MachineInstr &MI = *MBBI; 1547 switch (MI.getOpcode()) { 1548 default: 1549 // Just move on to the next instruction. 1550 ++MBBI; 1551 break; 1552 // Scaled instructions. 1553 case AArch64::LDRBBui: 1554 case AArch64::LDRHHui: 1555 case AArch64::LDRWui: 1556 case AArch64::LDRXui: 1557 // Unscaled instructions. 1558 case AArch64::LDURBBi: 1559 case AArch64::LDURHHi: 1560 case AArch64::LDURWi: 1561 case AArch64::LDURXi: 1562 if (tryToPromoteLoadFromStore(MBBI)) { 1563 Modified = true; 1564 break; 1565 } 1566 ++MBBI; 1567 break; 1568 } 1569 } 1570 // 2) Merge adjacent zero stores into a wider store. 1571 // e.g., 1572 // strh wzr, [x0] 1573 // strh wzr, [x0, #2] 1574 // ; becomes 1575 // str wzr, [x0] 1576 // e.g., 1577 // str wzr, [x0] 1578 // str wzr, [x0, #4] 1579 // ; becomes 1580 // str xzr, [x0] 1581 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end(); 1582 EnableNarrowZeroStOpt && MBBI != E;) { 1583 if (isPromotableZeroStoreInst(*MBBI)) { 1584 if (tryToMergeZeroStInst(MBBI)) { 1585 Modified = true; 1586 } else 1587 ++MBBI; 1588 } else 1589 ++MBBI; 1590 } 1591 1592 // 3) Find loads and stores that can be merged into a single load or store 1593 // pair instruction. 1594 // e.g., 1595 // ldr x0, [x2] 1596 // ldr x1, [x2, #8] 1597 // ; becomes 1598 // ldp x0, x1, [x2] 1599 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end(); 1600 MBBI != E;) { 1601 if (TII->isPairableLdStInst(*MBBI) && tryToPairLdStInst(MBBI)) 1602 Modified = true; 1603 else 1604 ++MBBI; 1605 } 1606 // 4) Find base register updates that can be merged into the load or store 1607 // as a base-reg writeback. 1608 // e.g., 1609 // ldr x0, [x2] 1610 // add x2, x2, #4 1611 // ; becomes 1612 // ldr x0, [x2], #4 1613 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end(); 1614 MBBI != E;) { 1615 MachineInstr &MI = *MBBI; 1616 // Do update merging. It's simpler to keep this separate from the above 1617 // switchs, though not strictly necessary. 1618 unsigned Opc = MI.getOpcode(); 1619 switch (Opc) { 1620 default: 1621 // Just move on to the next instruction. 1622 ++MBBI; 1623 break; 1624 // Scaled instructions. 1625 case AArch64::STRSui: 1626 case AArch64::STRDui: 1627 case AArch64::STRQui: 1628 case AArch64::STRXui: 1629 case AArch64::STRWui: 1630 case AArch64::STRHHui: 1631 case AArch64::STRBBui: 1632 case AArch64::LDRSui: 1633 case AArch64::LDRDui: 1634 case AArch64::LDRQui: 1635 case AArch64::LDRXui: 1636 case AArch64::LDRWui: 1637 case AArch64::LDRHHui: 1638 case AArch64::LDRBBui: 1639 // Unscaled instructions. 1640 case AArch64::STURSi: 1641 case AArch64::STURDi: 1642 case AArch64::STURQi: 1643 case AArch64::STURWi: 1644 case AArch64::STURXi: 1645 case AArch64::LDURSi: 1646 case AArch64::LDURDi: 1647 case AArch64::LDURQi: 1648 case AArch64::LDURWi: 1649 case AArch64::LDURXi: 1650 // Paired instructions. 1651 case AArch64::LDPSi: 1652 case AArch64::LDPSWi: 1653 case AArch64::LDPDi: 1654 case AArch64::LDPQi: 1655 case AArch64::LDPWi: 1656 case AArch64::LDPXi: 1657 case AArch64::STPSi: 1658 case AArch64::STPDi: 1659 case AArch64::STPQi: 1660 case AArch64::STPWi: 1661 case AArch64::STPXi: { 1662 // Make sure this is a reg+imm (as opposed to an address reloc). 1663 if (!getLdStOffsetOp(MI).isImm()) { 1664 ++MBBI; 1665 break; 1666 } 1667 // Look forward to try to form a post-index instruction. For example, 1668 // ldr x0, [x20] 1669 // add x20, x20, #32 1670 // merged into: 1671 // ldr x0, [x20], #32 1672 MachineBasicBlock::iterator Update = 1673 findMatchingUpdateInsnForward(MBBI, 0, UpdateLimit); 1674 if (Update != E) { 1675 // Merge the update into the ld/st. 1676 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/false); 1677 Modified = true; 1678 ++NumPostFolded; 1679 break; 1680 } 1681 // Don't know how to handle pre/post-index versions, so move to the next 1682 // instruction. 1683 if (TII->isUnscaledLdSt(Opc)) { 1684 ++MBBI; 1685 break; 1686 } 1687 1688 // Look back to try to find a pre-index instruction. For example, 1689 // add x0, x0, #8 1690 // ldr x1, [x0] 1691 // merged into: 1692 // ldr x1, [x0, #8]! 1693 Update = findMatchingUpdateInsnBackward(MBBI, UpdateLimit); 1694 if (Update != E) { 1695 // Merge the update into the ld/st. 1696 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/true); 1697 Modified = true; 1698 ++NumPreFolded; 1699 break; 1700 } 1701 // The immediate in the load/store is scaled by the size of the memory 1702 // operation. The immediate in the add we're looking for, 1703 // however, is not, so adjust here. 1704 int UnscaledOffset = getLdStOffsetOp(MI).getImm() * getMemScale(MI); 1705 1706 // Look forward to try to find a post-index instruction. For example, 1707 // ldr x1, [x0, #64] 1708 // add x0, x0, #64 1709 // merged into: 1710 // ldr x1, [x0, #64]! 1711 Update = findMatchingUpdateInsnForward(MBBI, UnscaledOffset, UpdateLimit); 1712 if (Update != E) { 1713 // Merge the update into the ld/st. 1714 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/true); 1715 Modified = true; 1716 ++NumPreFolded; 1717 break; 1718 } 1719 1720 // Nothing found. Just move to the next instruction. 1721 ++MBBI; 1722 break; 1723 } 1724 } 1725 } 1726 1727 return Modified; 1728 } 1729 1730 bool AArch64LoadStoreOpt::runOnMachineFunction(MachineFunction &Fn) { 1731 if (skipFunction(*Fn.getFunction())) 1732 return false; 1733 1734 Subtarget = &static_cast<const AArch64Subtarget &>(Fn.getSubtarget()); 1735 TII = static_cast<const AArch64InstrInfo *>(Subtarget->getInstrInfo()); 1736 TRI = Subtarget->getRegisterInfo(); 1737 1738 // Resize the modified and used register bitfield trackers. We do this once 1739 // per function and then clear the bitfield each time we optimize a load or 1740 // store. 1741 ModifiedRegs.resize(TRI->getNumRegs()); 1742 UsedRegs.resize(TRI->getNumRegs()); 1743 1744 bool Modified = false; 1745 bool enableNarrowZeroStOpt = !Subtarget->requiresStrictAlign(); 1746 for (auto &MBB : Fn) 1747 Modified |= optimizeBlock(MBB, enableNarrowZeroStOpt); 1748 1749 return Modified; 1750 } 1751 1752 // FIXME: Do we need/want a pre-alloc pass like ARM has to try to keep loads and 1753 // stores near one another? Note: The pre-RA instruction scheduler already has 1754 // hooks to try and schedule pairable loads/stores together to improve pairing 1755 // opportunities. Thus, pre-RA pairing pass may not be worth the effort. 1756 1757 // FIXME: When pairing store instructions it's very possible for this pass to 1758 // hoist a store with a KILL marker above another use (without a KILL marker). 1759 // The resulting IR is invalid, but nothing uses the KILL markers after this 1760 // pass, so it's never caused a problem in practice. 1761 1762 /// createAArch64LoadStoreOptimizationPass - returns an instance of the 1763 /// load / store optimization pass. 1764 FunctionPass *llvm::createAArch64LoadStoreOptimizationPass() { 1765 return new AArch64LoadStoreOpt(); 1766 } 1767