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