51 #define DEBUG_TYPE "hexagon-widen-stores" 80 StringRef getPassName()
const override {
return "Hexagon Store Widening"; }
91 static const int MaxWideSize = 4;
93 using InstrGroup = std::vector<MachineInstr *>;
94 using InstrGroupList = std::vector<InstrGroup>;
97 bool instrAliased(InstrGroup &Stores,
const MachineInstr *MI);
98 void createStoreGroup(
MachineInstr *BaseStore, InstrGroup::iterator Begin,
99 InstrGroup::iterator End, InstrGroup &Group);
101 InstrGroupList &StoreGroups);
103 bool processStoreGroup(InstrGroup &Group);
104 bool selectStores(InstrGroup::iterator Begin, InstrGroup::iterator End,
105 InstrGroup &OG,
unsigned &TotalSize,
unsigned MaxSize);
106 bool createWideStores(InstrGroup &OG, InstrGroup &NG,
unsigned TotalSize);
107 bool replaceStores(InstrGroup &OG, InstrGroup &NG);
116 "Hexason Store Widening",
false,
false)
124 assert(MO.
isReg() &&
"Expecting register operand");
130 assert(HexagonStoreWidening::handledStoreType(MI) &&
"Unhandled opcode");
133 case Hexagon::S4_storeirb_io:
134 case Hexagon::S4_storeirh_io:
135 case Hexagon::S4_storeiri_io: {
137 assert(MO.
isImm() &&
"Expecting immediate offset");
153 inline bool HexagonStoreWidening::handledStoreType(
const MachineInstr *
MI) {
158 case Hexagon::S4_storeirb_io:
159 case Hexagon::S4_storeirh_io:
160 case Hexagon::S4_storeiri_io:
170 bool HexagonStoreWidening::instrAliased(InstrGroup &Stores,
177 for (
auto SI : Stores) {
183 if (AA->alias(L, SL))
192 bool HexagonStoreWidening::instrAliased(InstrGroup &Stores,
195 if (instrAliased(Stores, *
I))
210 InstrGroupList &StoreGroups) {
217 AllInsns.push_back(&
I);
222 for (
auto I = AllInsns.begin(),
E = AllInsns.end();
I !=
E; ++
I) {
225 if (!MI || !handledStoreType(MI))
230 createStoreGroup(MI,
I+1,
E, G);
232 StoreGroups.push_back(G);
239 void HexagonStoreWidening::createStoreGroup(
MachineInstr *BaseStore,
240 InstrGroup::iterator Begin, InstrGroup::iterator End, InstrGroup &Group) {
241 assert(handledStoreType(BaseStore) &&
"Unexpected instruction");
245 Group.push_back(BaseStore);
247 for (
auto I = Begin;
I != End; ++
I) {
252 if (handledStoreType(MI)) {
284 bool HexagonStoreWidening::storesAreAdjacent(
const MachineInstr *S1,
286 if (!handledStoreType(S1) || !handledStoreType(S2))
296 : int(Off1+S1MO.
getSize()) == Off2;
305 bool HexagonStoreWidening::selectStores(InstrGroup::iterator Begin,
306 InstrGroup::iterator End, InstrGroup &OG,
unsigned &TotalSize,
308 assert(Begin != End &&
"No instructions to analyze");
309 assert(OG.empty() &&
"Old group not empty on entry");
311 if (std::distance(Begin, End) <= 1)
317 unsigned Alignment = FirstMMO.getAlignment();
318 unsigned SizeAccum = FirstMMO.getSize();
325 if (SizeAccum >= MaxSize)
330 if (SizeAccum >= Alignment)
337 if ((2*SizeAccum-1) & FirstOffset)
340 OG.push_back(FirstMI);
345 unsigned Pow2Num = 1;
346 unsigned Pow2Size = SizeAccum;
353 for (InstrGroup::iterator
I = Begin + 1;
I != End; ++
I) {
357 if (!storesAreAdjacent(S1, S2))
361 if (SizeAccum + S2Size > std::min(MaxSize, Alignment))
368 Pow2Size = SizeAccum;
370 if ((2*Pow2Size-1) & FirstOffset)
384 TotalSize = Pow2Size;
391 bool HexagonStoreWidening::createWideStores(InstrGroup &OG, InstrGroup &NG,
392 unsigned TotalSize) {
403 for (InstrGroup::iterator
I = OG.begin(),
E = OG.end();
I !=
E; ++
I) {
407 assert(SO.
isImm() &&
"Expecting an immediate operand");
409 unsigned NBits = MMO.
getSize()*8;
410 unsigned Mask = (0xFFFFFFFFU >> (32-NBits));
417 DebugLoc DL = OG.back()->getDebugLoc();
426 unsigned WOpc = (TotalSize == 2) ? Hexagon::S4_storeirh_io :
427 (TotalSize == 4) ? Hexagon::S4_storeiri_io : 0;
428 assert(WOpc &&
"Unexpected size");
430 int Val = (TotalSize == 2) ? int16_t(Acc) : int(Acc);
445 Register VReg = MF->getRegInfo().createVirtualRegister(RC);
450 unsigned WOpc = (TotalSize == 2) ? Hexagon::S2_storerh_io :
451 (TotalSize == 4) ? Hexagon::S2_storeri_io : 0;
452 assert(WOpc &&
"Unexpected size");
474 bool HexagonStoreWidening::replaceStores(InstrGroup &OG, InstrGroup &NG) {
476 dbgs() <<
"Replacing:\n";
499 for (
auto &
I : *MBB) {
506 assert((InsertAt != MBB->end()) &&
"Cannot locate any store from the group");
508 bool AtBBStart =
false;
514 if (InsertAt != MBB->begin())
520 I->eraseFromParent();
525 InsertAt = MBB->begin();
528 MBB->insert(InsertAt,
I);
536 bool HexagonStoreWidening::processStoreGroup(InstrGroup &Group) {
537 bool Changed =
false;
538 InstrGroup::iterator
I = Group.begin(),
E = Group.end();
540 unsigned CollectedSize;
546 bool Succ = selectStores(I++,
E, OG, CollectedSize, MaxWideSize) &&
547 createWideStores(OG, NG, CollectedSize) &&
548 replaceStores(OG, NG);
552 assert(OG.size() > 1 &&
"Created invalid group");
553 assert(distance(I,
E)+1 >=
int(OG.size()) &&
"Too many elements");
570 bool Changed =
false;
572 createStoreGroups(MBB, SGs);
577 for (
auto &
G : SGs) {
578 assert(
G.size() > 1 &&
"Store group with fewer than 2 elements");
581 Changed |= processStoreGroup(
G);
587 bool HexagonStoreWidening::runOnMachineFunction(
MachineFunction &MFn) {
593 TII =
ST.getInstrInfo();
594 TRI =
ST.getRegisterInfo();
596 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
598 bool Changed =
false;
601 Changed |= processBasicBlock(
B);
607 return new HexagonStoreWidening();
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
bool isCall(QueryType Type=AnyInBundle) const
This class represents lattice values for constants.
static const MachineMemOperand & getStoreTarget(const MachineInstr *MI)
Describe properties that are true of each instruction in the target description file.
unsigned getSubReg() const
uint64_t getSize() const
Return the size in bytes of the memory reference.
bool mayLoadOrStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read or modify memory.
unsigned const TargetRegisterInfo * TRI
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
AAMDNodes getAAInfo() const
Return the AA tags for the memory reference.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
A description of a memory reference used in the backend.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
void addMemOperand(MachineFunction &MF, MachineMemOperand *MO)
Add a MachineMemOperand to the machine instruction.
hexagon widen Hexagon Store Widening
unsigned getKillRegState(bool B)
const Value * getValue() const
Return the base address of the memory access.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Control flow instructions. These all have token chains.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
hexagon widen Hexagon Store static false unsigned getBaseAddressRegister(const MachineInstr *MI)
unsigned const MachineRegisterInfo * MRI
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
ArrayRef< MachineMemOperand * > memoperands() const
Access to memory operands of the instruction.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static int64_t getStoreOffset(const MachineInstr *MI)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
FunctionPass * createHexagonStoreWidening()
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
uint64_t getAlignment() const
Return the minimum known alignment in bytes of the actual memory reference.
void sort(IteratorTy Start, IteratorTy End)
Representation for a specific memory location.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
MachineOperand class - Representation of each machine instruction operand.
void initializeHexagonStoreWideningPass(PassRegistry &)
bool hasOrderedMemoryRef() const
Return true if this instruction may have an ordered or volatile memory reference, or if the informati...
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
const MachinePointerInfo & getPointerInfo() const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
This file provides utility analysis objects describing memory locations.
Flags getFlags() const
Return the raw flags of the source value,.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool memoperands_empty() const
Return true if we don't have any memory operands which described the memory access done by this instr...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
bool hasUnmodeledSideEffects() const
Return true if this instruction has side effects that are not modeled by mayLoad / mayStore...
StringRef - Represent a constant reference to a string, i.e.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
INITIALIZE_PASS_BEGIN(HexagonStoreWidening, "hexagon-widen-stores", "Hexason Store Widening", false, false) INITIALIZE_PASS_END(HexagonStoreWidening
Register getReg() const
getReg - Returns the register number.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
const MachineOperand & getOperand(unsigned i) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Wrapper class representing virtual and physical registers.