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