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