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