24 #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);
342 InstrGroup::iterator
I = Begin+1;
346 unsigned Pow2Num = 1;
347 unsigned Pow2Size = SizeAccum;
358 if (!storesAreAdjacent(S1, S2))
362 if (SizeAccum + S2Size > std::min(MaxSize, Alignment))
369 Pow2Size = SizeAccum;
371 if ((2*Pow2Size-1) & FirstOffset)
386 TotalSize = Pow2Size;
393 bool HexagonStoreWidening::createWideStores(InstrGroup &OG, InstrGroup &NG,
394 unsigned TotalSize) {
405 for (InstrGroup::iterator
I = OG.begin(),
E = OG.end();
I !=
E; ++
I) {
409 assert(SO.
isImm() &&
"Expecting an immediate operand");
411 unsigned NBits = MMO.
getSize()*8;
412 unsigned Mask = (0xFFFFFFFFU >> (32-NBits));
419 DebugLoc DL = OG.back()->getDebugLoc();
428 unsigned WOpc = (TotalSize == 2) ? Hexagon::S4_storeirh_io :
429 (TotalSize == 4) ? Hexagon::S4_storeiri_io : 0;
430 assert(WOpc &&
"Unexpected size");
432 int Val = (TotalSize == 2) ? int16_t(Acc) : int(Acc);
447 unsigned VReg = MF->getRegInfo().createVirtualRegister(RC);
452 unsigned WOpc = (TotalSize == 2) ? Hexagon::S2_storerh_io :
453 (TotalSize == 4) ? Hexagon::S2_storeri_io : 0;
454 assert(WOpc &&
"Unexpected size");
476 bool HexagonStoreWidening::replaceStores(InstrGroup &OG, InstrGroup &NG) {
478 dbgs() <<
"Replacing:\n";
501 for (
auto &
I : *MBB) {
508 assert((InsertAt != MBB->end()) &&
"Cannot locate any store from the group");
510 bool AtBBStart =
false;
516 if (InsertAt != MBB->begin())
522 I->eraseFromParent();
527 InsertAt = MBB->begin();
530 MBB->insert(InsertAt,
I);
538 bool HexagonStoreWidening::processStoreGroup(InstrGroup &Group) {
539 bool Changed =
false;
540 InstrGroup::iterator
I = Group.begin(),
E = Group.end();
542 unsigned CollectedSize;
548 bool Succ = selectStores(I++,
E, OG, CollectedSize, MaxWideSize) &&
549 createWideStores(OG, NG, CollectedSize) &&
550 replaceStores(OG, NG);
554 assert(OG.size() > 1 &&
"Created invalid group");
555 assert(distance(I,
E)+1 >=
int(OG.size()) &&
"Too many elements");
572 bool Changed =
false;
574 createStoreGroups(MBB, SGs);
579 for (
auto &
G : SGs) {
580 assert(
G.size() > 1 &&
"Store group with fewer than 2 elements");
583 Changed |= processStoreGroup(
G);
589 bool HexagonStoreWidening::runOnMachineFunction(
MachineFunction &MFn) {
595 TII =
ST.getInstrInfo();
596 TRI =
ST.getRegisterInfo();
598 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
600 bool Changed =
false;
603 Changed |= processBasicBlock(
B);
609 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 getReg() const
getReg - Returns the register number.
unsigned getSubReg() const
uint64_t getSize() const
Return the size in bytes of the memory reference.
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.
bool mayStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly modify memory.
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,.
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
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
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
const MachineOperand & getOperand(unsigned i) const