xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Hexagon/HexagonStoreWidening.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- HexagonStoreWidening.cpp -------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric // Replace sequences of "narrow" stores to adjacent memory locations with
90b57cec5SDimitry Andric // a fewer "wide" stores that have the same effect.
100b57cec5SDimitry Andric // For example, replace:
110b57cec5SDimitry Andric //   S4_storeirb_io  %100, 0, 0   ; store-immediate-byte
120b57cec5SDimitry Andric //   S4_storeirb_io  %100, 1, 0   ; store-immediate-byte
130b57cec5SDimitry Andric // with
140b57cec5SDimitry Andric //   S4_storeirh_io  %100, 0, 0   ; store-immediate-halfword
150b57cec5SDimitry Andric // The above is the general idea.  The actual cases handled by the code
160b57cec5SDimitry Andric // may be a bit more complex.
170b57cec5SDimitry Andric // The purpose of this pass is to reduce the number of outstanding stores,
180b57cec5SDimitry Andric // or as one could say, "reduce store queue pressure".  Also, wide stores
190b57cec5SDimitry Andric // mean fewer stores, and since there are only two memory instructions allowed
200b57cec5SDimitry Andric // per packet, it also means fewer packets, and ultimately fewer cycles.
210b57cec5SDimitry Andric //===---------------------------------------------------------------------===//
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric #include "HexagonInstrInfo.h"
240b57cec5SDimitry Andric #include "HexagonRegisterInfo.h"
250b57cec5SDimitry Andric #include "HexagonSubtarget.h"
260b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
270b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
280b57cec5SDimitry Andric #include "llvm/Analysis/MemoryLocation.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h"
350b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
360b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
370b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
38480093f4SDimitry Andric #include "llvm/InitializePasses.h"
390b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
400b57cec5SDimitry Andric #include "llvm/Pass.h"
410b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
420b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
430b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
440b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
450b57cec5SDimitry Andric #include <algorithm>
460b57cec5SDimitry Andric #include <cassert>
470b57cec5SDimitry Andric #include <cstdint>
480b57cec5SDimitry Andric #include <iterator>
490b57cec5SDimitry Andric #include <vector>
500b57cec5SDimitry Andric 
51480093f4SDimitry Andric #define DEBUG_TYPE "hexagon-widen-stores"
52480093f4SDimitry Andric 
530b57cec5SDimitry Andric using namespace llvm;
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric namespace llvm {
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric FunctionPass *createHexagonStoreWidening();
580b57cec5SDimitry Andric void initializeHexagonStoreWideningPass(PassRegistry&);
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric } // end namespace llvm
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric namespace {
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric   struct HexagonStoreWidening : public MachineFunctionPass {
650b57cec5SDimitry Andric     const HexagonInstrInfo      *TII;
660b57cec5SDimitry Andric     const HexagonRegisterInfo   *TRI;
670b57cec5SDimitry Andric     const MachineRegisterInfo   *MRI;
680b57cec5SDimitry Andric     AliasAnalysis               *AA;
690b57cec5SDimitry Andric     MachineFunction             *MF;
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric   public:
720b57cec5SDimitry Andric     static char ID;
730b57cec5SDimitry Andric 
HexagonStoreWidening__anonc64f720b0111::HexagonStoreWidening740b57cec5SDimitry Andric     HexagonStoreWidening() : MachineFunctionPass(ID) {
750b57cec5SDimitry Andric       initializeHexagonStoreWideningPass(*PassRegistry::getPassRegistry());
760b57cec5SDimitry Andric     }
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric     bool runOnMachineFunction(MachineFunction &MF) override;
790b57cec5SDimitry Andric 
getPassName__anonc64f720b0111::HexagonStoreWidening800b57cec5SDimitry Andric     StringRef getPassName() const override { return "Hexagon Store Widening"; }
810b57cec5SDimitry Andric 
getAnalysisUsage__anonc64f720b0111::HexagonStoreWidening820b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
830b57cec5SDimitry Andric       AU.addRequired<AAResultsWrapperPass>();
840b57cec5SDimitry Andric       AU.addPreserved<AAResultsWrapperPass>();
850b57cec5SDimitry Andric       MachineFunctionPass::getAnalysisUsage(AU);
860b57cec5SDimitry Andric     }
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric     static bool handledStoreType(const MachineInstr *MI);
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric   private:
910b57cec5SDimitry Andric     static const int MaxWideSize = 4;
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric     using InstrGroup = std::vector<MachineInstr *>;
940b57cec5SDimitry Andric     using InstrGroupList = std::vector<InstrGroup>;
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric     bool instrAliased(InstrGroup &Stores, const MachineMemOperand &MMO);
970b57cec5SDimitry Andric     bool instrAliased(InstrGroup &Stores, const MachineInstr *MI);
980b57cec5SDimitry Andric     void createStoreGroup(MachineInstr *BaseStore, InstrGroup::iterator Begin,
990b57cec5SDimitry Andric         InstrGroup::iterator End, InstrGroup &Group);
1000b57cec5SDimitry Andric     void createStoreGroups(MachineBasicBlock &MBB,
1010b57cec5SDimitry Andric         InstrGroupList &StoreGroups);
1020b57cec5SDimitry Andric     bool processBasicBlock(MachineBasicBlock &MBB);
1030b57cec5SDimitry Andric     bool processStoreGroup(InstrGroup &Group);
1040b57cec5SDimitry Andric     bool selectStores(InstrGroup::iterator Begin, InstrGroup::iterator End,
1050b57cec5SDimitry Andric         InstrGroup &OG, unsigned &TotalSize, unsigned MaxSize);
1060b57cec5SDimitry Andric     bool createWideStores(InstrGroup &OG, InstrGroup &NG, unsigned TotalSize);
1070b57cec5SDimitry Andric     bool replaceStores(InstrGroup &OG, InstrGroup &NG);
1080b57cec5SDimitry Andric     bool storesAreAdjacent(const MachineInstr *S1, const MachineInstr *S2);
1090b57cec5SDimitry Andric   };
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric } // end anonymous namespace
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric char HexagonStoreWidening::ID = 0;
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(HexagonStoreWidening, "hexagon-widen-stores",
1160b57cec5SDimitry Andric                 "Hexason Store Widening", false, false)
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)1170b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
1180b57cec5SDimitry Andric INITIALIZE_PASS_END(HexagonStoreWidening, "hexagon-widen-stores",
1190b57cec5SDimitry Andric                 "Hexagon Store Widening", false, false)
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric // Some local helper functions...
1220b57cec5SDimitry Andric static unsigned getBaseAddressRegister(const MachineInstr *MI) {
1230b57cec5SDimitry Andric   const MachineOperand &MO = MI->getOperand(0);
1240b57cec5SDimitry Andric   assert(MO.isReg() && "Expecting register operand");
1250b57cec5SDimitry Andric   return MO.getReg();
1260b57cec5SDimitry Andric }
1270b57cec5SDimitry Andric 
getStoreOffset(const MachineInstr * MI)1280b57cec5SDimitry Andric static int64_t getStoreOffset(const MachineInstr *MI) {
1290b57cec5SDimitry Andric   unsigned OpC = MI->getOpcode();
1300b57cec5SDimitry Andric   assert(HexagonStoreWidening::handledStoreType(MI) && "Unhandled opcode");
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric   switch (OpC) {
1330b57cec5SDimitry Andric     case Hexagon::S4_storeirb_io:
1340b57cec5SDimitry Andric     case Hexagon::S4_storeirh_io:
1350b57cec5SDimitry Andric     case Hexagon::S4_storeiri_io: {
1360b57cec5SDimitry Andric       const MachineOperand &MO = MI->getOperand(1);
1370b57cec5SDimitry Andric       assert(MO.isImm() && "Expecting immediate offset");
1380b57cec5SDimitry Andric       return MO.getImm();
1390b57cec5SDimitry Andric     }
1400b57cec5SDimitry Andric   }
1410b57cec5SDimitry Andric   dbgs() << *MI;
1420b57cec5SDimitry Andric   llvm_unreachable("Store offset calculation missing for a handled opcode");
1430b57cec5SDimitry Andric   return 0;
1440b57cec5SDimitry Andric }
1450b57cec5SDimitry Andric 
getStoreTarget(const MachineInstr * MI)1460b57cec5SDimitry Andric static const MachineMemOperand &getStoreTarget(const MachineInstr *MI) {
1470b57cec5SDimitry Andric   assert(!MI->memoperands_empty() && "Expecting memory operands");
1480b57cec5SDimitry Andric   return **MI->memoperands_begin();
1490b57cec5SDimitry Andric }
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric // Filtering function: any stores whose opcodes are not "approved" of by
1520b57cec5SDimitry Andric // this function will not be subjected to widening.
handledStoreType(const MachineInstr * MI)1530b57cec5SDimitry Andric inline bool HexagonStoreWidening::handledStoreType(const MachineInstr *MI) {
1540b57cec5SDimitry Andric   // For now, only handle stores of immediate values.
1550b57cec5SDimitry Andric   // Also, reject stores to stack slots.
1560b57cec5SDimitry Andric   unsigned Opc = MI->getOpcode();
1570b57cec5SDimitry Andric   switch (Opc) {
1580b57cec5SDimitry Andric     case Hexagon::S4_storeirb_io:
1590b57cec5SDimitry Andric     case Hexagon::S4_storeirh_io:
1600b57cec5SDimitry Andric     case Hexagon::S4_storeiri_io:
1610b57cec5SDimitry Andric       // Base address must be a register. (Implement FI later.)
1620b57cec5SDimitry Andric       return MI->getOperand(0).isReg();
1630b57cec5SDimitry Andric     default:
1640b57cec5SDimitry Andric       return false;
1650b57cec5SDimitry Andric   }
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric // Check if the machine memory operand MMO is aliased with any of the
1690b57cec5SDimitry Andric // stores in the store group Stores.
instrAliased(InstrGroup & Stores,const MachineMemOperand & MMO)1700b57cec5SDimitry Andric bool HexagonStoreWidening::instrAliased(InstrGroup &Stores,
1710b57cec5SDimitry Andric       const MachineMemOperand &MMO) {
1720b57cec5SDimitry Andric   if (!MMO.getValue())
1730b57cec5SDimitry Andric     return true;
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   MemoryLocation L(MMO.getValue(), MMO.getSize(), MMO.getAAInfo());
1760b57cec5SDimitry Andric 
177bdd1243dSDimitry Andric   for (auto *SI : Stores) {
1780b57cec5SDimitry Andric     const MachineMemOperand &SMO = getStoreTarget(SI);
1790b57cec5SDimitry Andric     if (!SMO.getValue())
1800b57cec5SDimitry Andric       return true;
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric     MemoryLocation SL(SMO.getValue(), SMO.getSize(), SMO.getAAInfo());
183fe6060f1SDimitry Andric     if (!AA->isNoAlias(L, SL))
1840b57cec5SDimitry Andric       return true;
1850b57cec5SDimitry Andric   }
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric   return false;
1880b57cec5SDimitry Andric }
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric // Check if the machine instruction MI accesses any storage aliased with
1910b57cec5SDimitry Andric // any store in the group Stores.
instrAliased(InstrGroup & Stores,const MachineInstr * MI)1920b57cec5SDimitry Andric bool HexagonStoreWidening::instrAliased(InstrGroup &Stores,
1930b57cec5SDimitry Andric       const MachineInstr *MI) {
1940b57cec5SDimitry Andric   for (auto &I : MI->memoperands())
1950b57cec5SDimitry Andric     if (instrAliased(Stores, *I))
1960b57cec5SDimitry Andric       return true;
1970b57cec5SDimitry Andric   return false;
1980b57cec5SDimitry Andric }
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric // Inspect a machine basic block, and generate store groups out of stores
2010b57cec5SDimitry Andric // encountered in the block.
2020b57cec5SDimitry Andric //
2030b57cec5SDimitry Andric // A store group is a group of stores that use the same base register,
2040b57cec5SDimitry Andric // and which can be reordered within that group without altering the
2050b57cec5SDimitry Andric // semantics of the program.  A single store group could be widened as
2060b57cec5SDimitry Andric // a whole, if there existed a single store instruction with the same
2070b57cec5SDimitry Andric // semantics as the entire group.  In many cases, a single store group
2080b57cec5SDimitry Andric // may need more than one wide store.
createStoreGroups(MachineBasicBlock & MBB,InstrGroupList & StoreGroups)2090b57cec5SDimitry Andric void HexagonStoreWidening::createStoreGroups(MachineBasicBlock &MBB,
2100b57cec5SDimitry Andric       InstrGroupList &StoreGroups) {
2110b57cec5SDimitry Andric   InstrGroup AllInsns;
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric   // Copy all instruction pointers from the basic block to a temporary
2140b57cec5SDimitry Andric   // list.  This will allow operating on the list, and modifying its
2150b57cec5SDimitry Andric   // elements without affecting the basic block.
2160b57cec5SDimitry Andric   for (auto &I : MBB)
2170b57cec5SDimitry Andric     AllInsns.push_back(&I);
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric   // Traverse all instructions in the AllInsns list, and if we encounter
2200b57cec5SDimitry Andric   // a store, then try to create a store group starting at that instruction
2210b57cec5SDimitry Andric   // i.e. a sequence of independent stores that can be widened.
2220b57cec5SDimitry Andric   for (auto I = AllInsns.begin(), E = AllInsns.end(); I != E; ++I) {
2230b57cec5SDimitry Andric     MachineInstr *MI = *I;
2240b57cec5SDimitry Andric     // Skip null pointers (processed instructions).
2250b57cec5SDimitry Andric     if (!MI || !handledStoreType(MI))
2260b57cec5SDimitry Andric       continue;
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric     // Found a store.  Try to create a store group.
2290b57cec5SDimitry Andric     InstrGroup G;
2300b57cec5SDimitry Andric     createStoreGroup(MI, I+1, E, G);
2310b57cec5SDimitry Andric     if (G.size() > 1)
2320b57cec5SDimitry Andric       StoreGroups.push_back(G);
2330b57cec5SDimitry Andric   }
2340b57cec5SDimitry Andric }
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric // Create a single store group.  The stores need to be independent between
2370b57cec5SDimitry Andric // themselves, and also there cannot be other instructions between them
2380b57cec5SDimitry Andric // that could read or modify storage being stored into.
createStoreGroup(MachineInstr * BaseStore,InstrGroup::iterator Begin,InstrGroup::iterator End,InstrGroup & Group)2390b57cec5SDimitry Andric void HexagonStoreWidening::createStoreGroup(MachineInstr *BaseStore,
2400b57cec5SDimitry Andric       InstrGroup::iterator Begin, InstrGroup::iterator End, InstrGroup &Group) {
2410b57cec5SDimitry Andric   assert(handledStoreType(BaseStore) && "Unexpected instruction");
2420b57cec5SDimitry Andric   unsigned BaseReg = getBaseAddressRegister(BaseStore);
2430b57cec5SDimitry Andric   InstrGroup Other;
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric   Group.push_back(BaseStore);
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric   for (auto I = Begin; I != End; ++I) {
2480b57cec5SDimitry Andric     MachineInstr *MI = *I;
2490b57cec5SDimitry Andric     if (!MI)
2500b57cec5SDimitry Andric       continue;
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric     if (handledStoreType(MI)) {
2530b57cec5SDimitry Andric       // If this store instruction is aliased with anything already in the
2540b57cec5SDimitry Andric       // group, terminate the group now.
2550b57cec5SDimitry Andric       if (instrAliased(Group, getStoreTarget(MI)))
2560b57cec5SDimitry Andric         return;
2570b57cec5SDimitry Andric       // If this store is aliased to any of the memory instructions we have
2580b57cec5SDimitry Andric       // seen so far (that are not a part of this group), terminate the group.
2590b57cec5SDimitry Andric       if (instrAliased(Other, getStoreTarget(MI)))
2600b57cec5SDimitry Andric         return;
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric       unsigned BR = getBaseAddressRegister(MI);
2630b57cec5SDimitry Andric       if (BR == BaseReg) {
2640b57cec5SDimitry Andric         Group.push_back(MI);
2650b57cec5SDimitry Andric         *I = nullptr;
2660b57cec5SDimitry Andric         continue;
2670b57cec5SDimitry Andric       }
2680b57cec5SDimitry Andric     }
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric     // Assume calls are aliased to everything.
2710b57cec5SDimitry Andric     if (MI->isCall() || MI->hasUnmodeledSideEffects())
2720b57cec5SDimitry Andric       return;
2730b57cec5SDimitry Andric 
274480093f4SDimitry Andric     if (MI->mayLoadOrStore()) {
2750b57cec5SDimitry Andric       if (MI->hasOrderedMemoryRef() || instrAliased(Group, MI))
2760b57cec5SDimitry Andric         return;
2770b57cec5SDimitry Andric       Other.push_back(MI);
2780b57cec5SDimitry Andric     }
2790b57cec5SDimitry Andric   } // for
2800b57cec5SDimitry Andric }
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric // Check if store instructions S1 and S2 are adjacent.  More precisely,
2830b57cec5SDimitry Andric // S2 has to access memory immediately following that accessed by S1.
storesAreAdjacent(const MachineInstr * S1,const MachineInstr * S2)2840b57cec5SDimitry Andric bool HexagonStoreWidening::storesAreAdjacent(const MachineInstr *S1,
2850b57cec5SDimitry Andric       const MachineInstr *S2) {
2860b57cec5SDimitry Andric   if (!handledStoreType(S1) || !handledStoreType(S2))
2870b57cec5SDimitry Andric     return false;
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric   const MachineMemOperand &S1MO = getStoreTarget(S1);
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric   // Currently only handling immediate stores.
2920b57cec5SDimitry Andric   int Off1 = S1->getOperand(1).getImm();
2930b57cec5SDimitry Andric   int Off2 = S2->getOperand(1).getImm();
2940b57cec5SDimitry Andric 
295*0fca6ea1SDimitry Andric   return (Off1 >= 0) ? Off1 + S1MO.getSize().getValue() == unsigned(Off2)
296*0fca6ea1SDimitry Andric                      : int(Off1 + S1MO.getSize().getValue()) == Off2;
2970b57cec5SDimitry Andric }
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric /// Given a sequence of adjacent stores, and a maximum size of a single wide
3000b57cec5SDimitry Andric /// store, pick a group of stores that  can be replaced by a single store
3010b57cec5SDimitry Andric /// of size not exceeding MaxSize.  The selected sequence will be recorded
3020b57cec5SDimitry Andric /// in OG ("old group" of instructions).
3030b57cec5SDimitry Andric /// OG should be empty on entry, and should be left empty if the function
3040b57cec5SDimitry Andric /// fails.
selectStores(InstrGroup::iterator Begin,InstrGroup::iterator End,InstrGroup & OG,unsigned & TotalSize,unsigned MaxSize)3050b57cec5SDimitry Andric bool HexagonStoreWidening::selectStores(InstrGroup::iterator Begin,
3060b57cec5SDimitry Andric       InstrGroup::iterator End, InstrGroup &OG, unsigned &TotalSize,
3070b57cec5SDimitry Andric       unsigned MaxSize) {
3080b57cec5SDimitry Andric   assert(Begin != End && "No instructions to analyze");
3090b57cec5SDimitry Andric   assert(OG.empty() && "Old group not empty on entry");
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric   if (std::distance(Begin, End) <= 1)
3120b57cec5SDimitry Andric     return false;
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric   MachineInstr *FirstMI = *Begin;
3150b57cec5SDimitry Andric   assert(!FirstMI->memoperands_empty() && "Expecting some memory operands");
3160b57cec5SDimitry Andric   const MachineMemOperand &FirstMMO = getStoreTarget(FirstMI);
3175ffd83dbSDimitry Andric   unsigned Alignment = FirstMMO.getAlign().value();
318*0fca6ea1SDimitry Andric   unsigned SizeAccum = FirstMMO.getSize().getValue();
3190b57cec5SDimitry Andric   unsigned FirstOffset = getStoreOffset(FirstMI);
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric   // The initial value of SizeAccum should always be a power of 2.
3220b57cec5SDimitry Andric   assert(isPowerOf2_32(SizeAccum) && "First store size not a power of 2");
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric   // If the size of the first store equals to or exceeds the limit, do nothing.
3250b57cec5SDimitry Andric   if (SizeAccum >= MaxSize)
3260b57cec5SDimitry Andric     return false;
3270b57cec5SDimitry Andric 
3280b57cec5SDimitry Andric   // If the size of the first store is greater than or equal to the address
3290b57cec5SDimitry Andric   // stored to, then the store cannot be made any wider.
3300b57cec5SDimitry Andric   if (SizeAccum >= Alignment)
3310b57cec5SDimitry Andric     return false;
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric   // The offset of a store will put restrictions on how wide the store can be.
3340b57cec5SDimitry Andric   // Offsets in stores of size 2^n bytes need to have the n lowest bits be 0.
3350b57cec5SDimitry Andric   // If the first store already exhausts the offset limits, quit.  Test this
3360b57cec5SDimitry Andric   // by checking if the next wider size would exceed the limit.
3370b57cec5SDimitry Andric   if ((2*SizeAccum-1) & FirstOffset)
3380b57cec5SDimitry Andric     return false;
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric   OG.push_back(FirstMI);
3410b57cec5SDimitry Andric   MachineInstr *S1 = FirstMI;
3420b57cec5SDimitry Andric 
3430b57cec5SDimitry Andric   // Pow2Num will be the largest number of elements in OG such that the sum
3440b57cec5SDimitry Andric   // of sizes of stores 0...Pow2Num-1 will be a power of 2.
3450b57cec5SDimitry Andric   unsigned Pow2Num = 1;
3460b57cec5SDimitry Andric   unsigned Pow2Size = SizeAccum;
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric   // Be greedy: keep accumulating stores as long as they are to adjacent
3490b57cec5SDimitry Andric   // memory locations, and as long as the total number of bytes stored
3500b57cec5SDimitry Andric   // does not exceed the limit (MaxSize).
3510b57cec5SDimitry Andric   // Keep track of when the total size covered is a power of 2, since
3520b57cec5SDimitry Andric   // this is a size a single store can cover.
3530b57cec5SDimitry Andric   for (InstrGroup::iterator I = Begin + 1; I != End; ++I) {
3540b57cec5SDimitry Andric     MachineInstr *S2 = *I;
3550b57cec5SDimitry Andric     // Stores are sorted, so if S1 and S2 are not adjacent, there won't be
3560b57cec5SDimitry Andric     // any other store to fill the "hole".
3570b57cec5SDimitry Andric     if (!storesAreAdjacent(S1, S2))
3580b57cec5SDimitry Andric       break;
3590b57cec5SDimitry Andric 
360*0fca6ea1SDimitry Andric     unsigned S2Size = getStoreTarget(S2).getSize().getValue();
3610b57cec5SDimitry Andric     if (SizeAccum + S2Size > std::min(MaxSize, Alignment))
3620b57cec5SDimitry Andric       break;
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric     OG.push_back(S2);
3650b57cec5SDimitry Andric     SizeAccum += S2Size;
3660b57cec5SDimitry Andric     if (isPowerOf2_32(SizeAccum)) {
3670b57cec5SDimitry Andric       Pow2Num = OG.size();
3680b57cec5SDimitry Andric       Pow2Size = SizeAccum;
3690b57cec5SDimitry Andric     }
3700b57cec5SDimitry Andric     if ((2*Pow2Size-1) & FirstOffset)
3710b57cec5SDimitry Andric       break;
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric     S1 = S2;
3740b57cec5SDimitry Andric   }
3750b57cec5SDimitry Andric 
3760b57cec5SDimitry Andric   // The stores don't add up to anything that can be widened.  Clean up.
3770b57cec5SDimitry Andric   if (Pow2Num <= 1) {
3780b57cec5SDimitry Andric     OG.clear();
3790b57cec5SDimitry Andric     return false;
3800b57cec5SDimitry Andric   }
3810b57cec5SDimitry Andric 
3820b57cec5SDimitry Andric   // Only leave the stored being widened.
3830b57cec5SDimitry Andric   OG.resize(Pow2Num);
3840b57cec5SDimitry Andric   TotalSize = Pow2Size;
3850b57cec5SDimitry Andric   return true;
3860b57cec5SDimitry Andric }
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric /// Given an "old group" OG of stores, create a "new group" NG of instructions
3890b57cec5SDimitry Andric /// to replace them.  Ideally, NG would only have a single instruction in it,
3900b57cec5SDimitry Andric /// but that may only be possible for store-immediate.
createWideStores(InstrGroup & OG,InstrGroup & NG,unsigned TotalSize)3910b57cec5SDimitry Andric bool HexagonStoreWidening::createWideStores(InstrGroup &OG, InstrGroup &NG,
3920b57cec5SDimitry Andric       unsigned TotalSize) {
3930b57cec5SDimitry Andric   // XXX Current limitations:
3940b57cec5SDimitry Andric   // - only expect stores of immediate values in OG,
3950b57cec5SDimitry Andric   // - only handle a TotalSize of up to 4.
3960b57cec5SDimitry Andric 
3970b57cec5SDimitry Andric   if (TotalSize > 4)
3980b57cec5SDimitry Andric     return false;
3990b57cec5SDimitry Andric 
4000b57cec5SDimitry Andric   unsigned Acc = 0;  // Value accumulator.
4010b57cec5SDimitry Andric   unsigned Shift = 0;
4020b57cec5SDimitry Andric 
4034824e7fdSDimitry Andric   for (MachineInstr *MI : OG) {
4040b57cec5SDimitry Andric     const MachineMemOperand &MMO = getStoreTarget(MI);
4050b57cec5SDimitry Andric     MachineOperand &SO = MI->getOperand(2);  // Source.
4060b57cec5SDimitry Andric     assert(SO.isImm() && "Expecting an immediate operand");
4070b57cec5SDimitry Andric 
408*0fca6ea1SDimitry Andric     unsigned NBits = MMO.getSize().getValue() * 8;
4090b57cec5SDimitry Andric     unsigned Mask = (0xFFFFFFFFU >> (32-NBits));
4100b57cec5SDimitry Andric     unsigned Val = (SO.getImm() & Mask) << Shift;
4110b57cec5SDimitry Andric     Acc |= Val;
4120b57cec5SDimitry Andric     Shift += NBits;
4130b57cec5SDimitry Andric   }
4140b57cec5SDimitry Andric 
4150b57cec5SDimitry Andric   MachineInstr *FirstSt = OG.front();
4160b57cec5SDimitry Andric   DebugLoc DL = OG.back()->getDebugLoc();
4170b57cec5SDimitry Andric   const MachineMemOperand &OldM = getStoreTarget(FirstSt);
4180b57cec5SDimitry Andric   MachineMemOperand *NewM =
4190b57cec5SDimitry Andric       MF->getMachineMemOperand(OldM.getPointerInfo(), OldM.getFlags(),
4205ffd83dbSDimitry Andric                                TotalSize, OldM.getAlign(), OldM.getAAInfo());
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric   if (Acc < 0x10000) {
4230b57cec5SDimitry Andric     // Create mem[hw] = #Acc
4240b57cec5SDimitry Andric     unsigned WOpc = (TotalSize == 2) ? Hexagon::S4_storeirh_io :
4250b57cec5SDimitry Andric                     (TotalSize == 4) ? Hexagon::S4_storeiri_io : 0;
4260b57cec5SDimitry Andric     assert(WOpc && "Unexpected size");
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric     int Val = (TotalSize == 2) ? int16_t(Acc) : int(Acc);
4290b57cec5SDimitry Andric     const MCInstrDesc &StD = TII->get(WOpc);
4300b57cec5SDimitry Andric     MachineOperand &MR = FirstSt->getOperand(0);
4310b57cec5SDimitry Andric     int64_t Off = FirstSt->getOperand(1).getImm();
4320b57cec5SDimitry Andric     MachineInstr *StI =
4330b57cec5SDimitry Andric         BuildMI(*MF, DL, StD)
4340b57cec5SDimitry Andric             .addReg(MR.getReg(), getKillRegState(MR.isKill()), MR.getSubReg())
4350b57cec5SDimitry Andric             .addImm(Off)
4360b57cec5SDimitry Andric             .addImm(Val);
4370b57cec5SDimitry Andric     StI->addMemOperand(*MF, NewM);
4380b57cec5SDimitry Andric     NG.push_back(StI);
4390b57cec5SDimitry Andric   } else {
4400b57cec5SDimitry Andric     // Create vreg = A2_tfrsi #Acc; mem[hw] = vreg
4410b57cec5SDimitry Andric     const MCInstrDesc &TfrD = TII->get(Hexagon::A2_tfrsi);
4420b57cec5SDimitry Andric     const TargetRegisterClass *RC = TII->getRegClass(TfrD, 0, TRI, *MF);
4438bcb0991SDimitry Andric     Register VReg = MF->getRegInfo().createVirtualRegister(RC);
4440b57cec5SDimitry Andric     MachineInstr *TfrI = BuildMI(*MF, DL, TfrD, VReg)
4450b57cec5SDimitry Andric                            .addImm(int(Acc));
4460b57cec5SDimitry Andric     NG.push_back(TfrI);
4470b57cec5SDimitry Andric 
4480b57cec5SDimitry Andric     unsigned WOpc = (TotalSize == 2) ? Hexagon::S2_storerh_io :
4490b57cec5SDimitry Andric                     (TotalSize == 4) ? Hexagon::S2_storeri_io : 0;
4500b57cec5SDimitry Andric     assert(WOpc && "Unexpected size");
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric     const MCInstrDesc &StD = TII->get(WOpc);
4530b57cec5SDimitry Andric     MachineOperand &MR = FirstSt->getOperand(0);
4540b57cec5SDimitry Andric     int64_t Off = FirstSt->getOperand(1).getImm();
4550b57cec5SDimitry Andric     MachineInstr *StI =
4560b57cec5SDimitry Andric         BuildMI(*MF, DL, StD)
4570b57cec5SDimitry Andric             .addReg(MR.getReg(), getKillRegState(MR.isKill()), MR.getSubReg())
4580b57cec5SDimitry Andric             .addImm(Off)
4590b57cec5SDimitry Andric             .addReg(VReg, RegState::Kill);
4600b57cec5SDimitry Andric     StI->addMemOperand(*MF, NewM);
4610b57cec5SDimitry Andric     NG.push_back(StI);
4620b57cec5SDimitry Andric   }
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric   return true;
4650b57cec5SDimitry Andric }
4660b57cec5SDimitry Andric 
4670b57cec5SDimitry Andric // Replace instructions from the old group OG with instructions from the
4680b57cec5SDimitry Andric // new group NG.  Conceptually, remove all instructions in OG, and then
4690b57cec5SDimitry Andric // insert all instructions in NG, starting at where the first instruction
4700b57cec5SDimitry Andric // from OG was (in the order in which they appeared in the basic block).
4710b57cec5SDimitry Andric // (The ordering in OG does not have to match the order in the basic block.)
replaceStores(InstrGroup & OG,InstrGroup & NG)4720b57cec5SDimitry Andric bool HexagonStoreWidening::replaceStores(InstrGroup &OG, InstrGroup &NG) {
4730b57cec5SDimitry Andric   LLVM_DEBUG({
4740b57cec5SDimitry Andric     dbgs() << "Replacing:\n";
4750b57cec5SDimitry Andric     for (auto I : OG)
4760b57cec5SDimitry Andric       dbgs() << "  " << *I;
4770b57cec5SDimitry Andric     dbgs() << "with\n";
4780b57cec5SDimitry Andric     for (auto I : NG)
4790b57cec5SDimitry Andric       dbgs() << "  " << *I;
4800b57cec5SDimitry Andric   });
4810b57cec5SDimitry Andric 
4820b57cec5SDimitry Andric   MachineBasicBlock *MBB = OG.back()->getParent();
4830b57cec5SDimitry Andric   MachineBasicBlock::iterator InsertAt = MBB->end();
4840b57cec5SDimitry Andric 
4850b57cec5SDimitry Andric   // Need to establish the insertion point.  The best one is right before
4860b57cec5SDimitry Andric   // the first store in the OG, but in the order in which the stores occur
4870b57cec5SDimitry Andric   // in the program list.  Since the ordering in OG does not correspond
4880b57cec5SDimitry Andric   // to the order in the program list, we need to do some work to find
4890b57cec5SDimitry Andric   // the insertion point.
4900b57cec5SDimitry Andric 
4910b57cec5SDimitry Andric   // Create a set of all instructions in OG (for quick lookup).
4920b57cec5SDimitry Andric   SmallPtrSet<MachineInstr*, 4> InstrSet;
493bdd1243dSDimitry Andric   for (auto *I : OG)
4940b57cec5SDimitry Andric     InstrSet.insert(I);
4950b57cec5SDimitry Andric 
4960b57cec5SDimitry Andric   // Traverse the block, until we hit an instruction from OG.
4970b57cec5SDimitry Andric   for (auto &I : *MBB) {
4980b57cec5SDimitry Andric     if (InstrSet.count(&I)) {
4990b57cec5SDimitry Andric       InsertAt = I;
5000b57cec5SDimitry Andric       break;
5010b57cec5SDimitry Andric     }
5020b57cec5SDimitry Andric   }
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric   assert((InsertAt != MBB->end()) && "Cannot locate any store from the group");
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric   bool AtBBStart = false;
5070b57cec5SDimitry Andric 
5080b57cec5SDimitry Andric   // InsertAt points at the first instruction that will be removed.  We need
5090b57cec5SDimitry Andric   // to move it out of the way, so it remains valid after removing all the
5100b57cec5SDimitry Andric   // old stores, and so we are able to recover it back to the proper insertion
5110b57cec5SDimitry Andric   // position.
5120b57cec5SDimitry Andric   if (InsertAt != MBB->begin())
5130b57cec5SDimitry Andric     --InsertAt;
5140b57cec5SDimitry Andric   else
5150b57cec5SDimitry Andric     AtBBStart = true;
5160b57cec5SDimitry Andric 
517bdd1243dSDimitry Andric   for (auto *I : OG)
5180b57cec5SDimitry Andric     I->eraseFromParent();
5190b57cec5SDimitry Andric 
5200b57cec5SDimitry Andric   if (!AtBBStart)
5210b57cec5SDimitry Andric     ++InsertAt;
5220b57cec5SDimitry Andric   else
5230b57cec5SDimitry Andric     InsertAt = MBB->begin();
5240b57cec5SDimitry Andric 
525bdd1243dSDimitry Andric   for (auto *I : NG)
5260b57cec5SDimitry Andric     MBB->insert(InsertAt, I);
5270b57cec5SDimitry Andric 
5280b57cec5SDimitry Andric   return true;
5290b57cec5SDimitry Andric }
5300b57cec5SDimitry Andric 
5310b57cec5SDimitry Andric // Break up the group into smaller groups, each of which can be replaced by
5320b57cec5SDimitry Andric // a single wide store.  Widen each such smaller group and replace the old
5330b57cec5SDimitry Andric // instructions with the widened ones.
processStoreGroup(InstrGroup & Group)5340b57cec5SDimitry Andric bool HexagonStoreWidening::processStoreGroup(InstrGroup &Group) {
5350b57cec5SDimitry Andric   bool Changed = false;
5360b57cec5SDimitry Andric   InstrGroup::iterator I = Group.begin(), E = Group.end();
5370b57cec5SDimitry Andric   InstrGroup OG, NG;   // Old and new groups.
5380b57cec5SDimitry Andric   unsigned CollectedSize;
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric   while (I != E) {
5410b57cec5SDimitry Andric     OG.clear();
5420b57cec5SDimitry Andric     NG.clear();
5430b57cec5SDimitry Andric 
5440b57cec5SDimitry Andric     bool Succ = selectStores(I++, E, OG, CollectedSize, MaxWideSize) &&
5450b57cec5SDimitry Andric                 createWideStores(OG, NG, CollectedSize)              &&
5460b57cec5SDimitry Andric                 replaceStores(OG, NG);
5470b57cec5SDimitry Andric     if (!Succ)
5480b57cec5SDimitry Andric       continue;
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric     assert(OG.size() > 1 && "Created invalid group");
5510b57cec5SDimitry Andric     assert(distance(I, E)+1 >= int(OG.size()) && "Too many elements");
5520b57cec5SDimitry Andric     I += OG.size()-1;
5530b57cec5SDimitry Andric 
5540b57cec5SDimitry Andric     Changed = true;
5550b57cec5SDimitry Andric   }
5560b57cec5SDimitry Andric 
5570b57cec5SDimitry Andric   return Changed;
5580b57cec5SDimitry Andric }
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric // Process a single basic block: create the store groups, and replace them
5610b57cec5SDimitry Andric // with the widened stores, if possible.  Processing of each basic block
5620b57cec5SDimitry Andric // is independent from processing of any other basic block.  This transfor-
5630b57cec5SDimitry Andric // mation could be stopped after having processed any basic block without
5640b57cec5SDimitry Andric // any ill effects (other than not having performed widening in the unpro-
5650b57cec5SDimitry Andric // cessed blocks).  Also, the basic blocks can be processed in any order.
processBasicBlock(MachineBasicBlock & MBB)5660b57cec5SDimitry Andric bool HexagonStoreWidening::processBasicBlock(MachineBasicBlock &MBB) {
5670b57cec5SDimitry Andric   InstrGroupList SGs;
5680b57cec5SDimitry Andric   bool Changed = false;
5690b57cec5SDimitry Andric 
5700b57cec5SDimitry Andric   createStoreGroups(MBB, SGs);
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric   auto Less = [] (const MachineInstr *A, const MachineInstr *B) -> bool {
5730b57cec5SDimitry Andric     return getStoreOffset(A) < getStoreOffset(B);
5740b57cec5SDimitry Andric   };
5750b57cec5SDimitry Andric   for (auto &G : SGs) {
5760b57cec5SDimitry Andric     assert(G.size() > 1 && "Store group with fewer than 2 elements");
5770b57cec5SDimitry Andric     llvm::sort(G, Less);
5780b57cec5SDimitry Andric 
5790b57cec5SDimitry Andric     Changed |= processStoreGroup(G);
5800b57cec5SDimitry Andric   }
5810b57cec5SDimitry Andric 
5820b57cec5SDimitry Andric   return Changed;
5830b57cec5SDimitry Andric }
5840b57cec5SDimitry Andric 
runOnMachineFunction(MachineFunction & MFn)5850b57cec5SDimitry Andric bool HexagonStoreWidening::runOnMachineFunction(MachineFunction &MFn) {
5860b57cec5SDimitry Andric   if (skipFunction(MFn.getFunction()))
5870b57cec5SDimitry Andric     return false;
5880b57cec5SDimitry Andric 
5890b57cec5SDimitry Andric   MF = &MFn;
5900b57cec5SDimitry Andric   auto &ST = MFn.getSubtarget<HexagonSubtarget>();
5910b57cec5SDimitry Andric   TII = ST.getInstrInfo();
5920b57cec5SDimitry Andric   TRI = ST.getRegisterInfo();
5930b57cec5SDimitry Andric   MRI = &MFn.getRegInfo();
5940b57cec5SDimitry Andric   AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric   bool Changed = false;
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric   for (auto &B : MFn)
5990b57cec5SDimitry Andric     Changed |= processBasicBlock(B);
6000b57cec5SDimitry Andric 
6010b57cec5SDimitry Andric   return Changed;
6020b57cec5SDimitry Andric }
6030b57cec5SDimitry Andric 
createHexagonStoreWidening()6040b57cec5SDimitry Andric FunctionPass *llvm::createHexagonStoreWidening() {
6050b57cec5SDimitry Andric   return new HexagonStoreWidening();
6060b57cec5SDimitry Andric }
607