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