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