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