#include "HexagonInstrInfo.h"
#include "HexagonRegisterInfo.h"
#include "HexagonSubtarget.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/MemoryLocation.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineMemOperand.h"
#include "llvm/CodeGen/MachineOperand.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/InitializePasses.h"
#include "llvm/MC/MCInstrDesc.h"
#include "llvm/Pass.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <iterator>
#include <vector>
#define DEBUG_TYPE "hexagon-widen-stores"
using namespace llvm;
namespace llvm {
FunctionPass *createHexagonStoreWidening();
void initializeHexagonStoreWideningPass(PassRegistry&);
}
namespace {
struct HexagonStoreWidening : public MachineFunctionPass {
const HexagonInstrInfo *TII;
const HexagonRegisterInfo *TRI;
const MachineRegisterInfo *MRI;
AliasAnalysis *AA;
MachineFunction *MF;
public:
static char ID;
HexagonStoreWidening() : MachineFunctionPass(ID) {
initializeHexagonStoreWideningPass(*PassRegistry::getPassRegistry());
}
bool runOnMachineFunction(MachineFunction &MF) override;
StringRef getPassName() const override { return "Hexagon Store Widening"; }
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<AAResultsWrapperPass>();
AU.addPreserved<AAResultsWrapperPass>();
MachineFunctionPass::getAnalysisUsage(AU);
}
static bool handledStoreType(const MachineInstr *MI);
private:
static const int MaxWideSize = 4;
using InstrGroup = std::vector<MachineInstr *>;
using InstrGroupList = std::vector<InstrGroup>;
bool instrAliased(InstrGroup &Stores, const MachineMemOperand &MMO);
bool instrAliased(InstrGroup &Stores, const MachineInstr *MI);
void createStoreGroup(MachineInstr *BaseStore, InstrGroup::iterator Begin,
InstrGroup::iterator End, InstrGroup &Group);
void createStoreGroups(MachineBasicBlock &MBB,
InstrGroupList &StoreGroups);
bool processBasicBlock(MachineBasicBlock &MBB);
bool processStoreGroup(InstrGroup &Group);
bool selectStores(InstrGroup::iterator Begin, InstrGroup::iterator End,
InstrGroup &OG, unsigned &TotalSize, unsigned MaxSize);
bool createWideStores(InstrGroup &OG, InstrGroup &NG, unsigned TotalSize);
bool replaceStores(InstrGroup &OG, InstrGroup &NG);
bool storesAreAdjacent(const MachineInstr *S1, const MachineInstr *S2);
};
}
char HexagonStoreWidening::ID = 0;
INITIALIZE_PASS_BEGIN(HexagonStoreWidening, "hexagon-widen-stores",
"Hexason Store Widening", false, false)
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
INITIALIZE_PASS_END(HexagonStoreWidening, "hexagon-widen-stores",
"Hexagon Store Widening", false, false)
static unsigned getBaseAddressRegister(const MachineInstr *MI) {
const MachineOperand &MO = MI->getOperand(0);
assert(MO.isReg() && "Expecting register operand");
return MO.getReg();
}
static int64_t getStoreOffset(const MachineInstr *MI) {
unsigned OpC = MI->getOpcode();
assert(HexagonStoreWidening::handledStoreType(MI) && "Unhandled opcode");
switch (OpC) {
case Hexagon::S4_storeirb_io:
case Hexagon::S4_storeirh_io:
case Hexagon::S4_storeiri_io: {
const MachineOperand &MO = MI->getOperand(1);
assert(MO.isImm() && "Expecting immediate offset");
return MO.getImm();
}
}
dbgs() << *MI;
llvm_unreachable("Store offset calculation missing for a handled opcode");
return 0;
}
static const MachineMemOperand &getStoreTarget(const MachineInstr *MI) {
assert(!MI->memoperands_empty() && "Expecting memory operands");
return **MI->memoperands_begin();
}
inline bool HexagonStoreWidening::handledStoreType(const MachineInstr *MI) {
unsigned Opc = MI->getOpcode();
switch (Opc) {
case Hexagon::S4_storeirb_io:
case Hexagon::S4_storeirh_io:
case Hexagon::S4_storeiri_io:
return MI->getOperand(0).isReg();
default:
return false;
}
}
bool HexagonStoreWidening::instrAliased(InstrGroup &Stores,
const MachineMemOperand &MMO) {
if (!MMO.getValue())
return true;
MemoryLocation L(MMO.getValue(), MMO.getSize(), MMO.getAAInfo());
for (auto SI : Stores) {
const MachineMemOperand &SMO = getStoreTarget(SI);
if (!SMO.getValue())
return true;
MemoryLocation SL(SMO.getValue(), SMO.getSize(), SMO.getAAInfo());
if (!AA->isNoAlias(L, SL))
return true;
}
return false;
}
bool HexagonStoreWidening::instrAliased(InstrGroup &Stores,
const MachineInstr *MI) {
for (auto &I : MI->memoperands())
if (instrAliased(Stores, *I))
return true;
return false;
}
void HexagonStoreWidening::createStoreGroups(MachineBasicBlock &MBB,
InstrGroupList &StoreGroups) {
InstrGroup AllInsns;
for (auto &I : MBB)
AllInsns.push_back(&I);
for (auto I = AllInsns.begin(), E = AllInsns.end(); I != E; ++I) {
MachineInstr *MI = *I;
if (!MI || !handledStoreType(MI))
continue;
InstrGroup G;
createStoreGroup(MI, I+1, E, G);
if (G.size() > 1)
StoreGroups.push_back(G);
}
}
void HexagonStoreWidening::createStoreGroup(MachineInstr *BaseStore,
InstrGroup::iterator Begin, InstrGroup::iterator End, InstrGroup &Group) {
assert(handledStoreType(BaseStore) && "Unexpected instruction");
unsigned BaseReg = getBaseAddressRegister(BaseStore);
InstrGroup Other;
Group.push_back(BaseStore);
for (auto I = Begin; I != End; ++I) {
MachineInstr *MI = *I;
if (!MI)
continue;
if (handledStoreType(MI)) {
if (instrAliased(Group, getStoreTarget(MI)))
return;
if (instrAliased(Other, getStoreTarget(MI)))
return;
unsigned BR = getBaseAddressRegister(MI);
if (BR == BaseReg) {
Group.push_back(MI);
*I = nullptr;
continue;
}
}
if (MI->isCall() || MI->hasUnmodeledSideEffects())
return;
if (MI->mayLoadOrStore()) {
if (MI->hasOrderedMemoryRef() || instrAliased(Group, MI))
return;
Other.push_back(MI);
}
} }
bool HexagonStoreWidening::storesAreAdjacent(const MachineInstr *S1,
const MachineInstr *S2) {
if (!handledStoreType(S1) || !handledStoreType(S2))
return false;
const MachineMemOperand &S1MO = getStoreTarget(S1);
int Off1 = S1->getOperand(1).getImm();
int Off2 = S2->getOperand(1).getImm();
return (Off1 >= 0) ? Off1+S1MO.getSize() == unsigned(Off2)
: int(Off1+S1MO.getSize()) == Off2;
}
bool HexagonStoreWidening::selectStores(InstrGroup::iterator Begin,
InstrGroup::iterator End, InstrGroup &OG, unsigned &TotalSize,
unsigned MaxSize) {
assert(Begin != End && "No instructions to analyze");
assert(OG.empty() && "Old group not empty on entry");
if (std::distance(Begin, End) <= 1)
return false;
MachineInstr *FirstMI = *Begin;
assert(!FirstMI->memoperands_empty() && "Expecting some memory operands");
const MachineMemOperand &FirstMMO = getStoreTarget(FirstMI);
unsigned Alignment = FirstMMO.getAlign().value();
unsigned SizeAccum = FirstMMO.getSize();
unsigned FirstOffset = getStoreOffset(FirstMI);
assert(isPowerOf2_32(SizeAccum) && "First store size not a power of 2");
if (SizeAccum >= MaxSize)
return false;
if (SizeAccum >= Alignment)
return false;
if ((2*SizeAccum-1) & FirstOffset)
return false;
OG.push_back(FirstMI);
MachineInstr *S1 = FirstMI;
unsigned Pow2Num = 1;
unsigned Pow2Size = SizeAccum;
for (InstrGroup::iterator I = Begin + 1; I != End; ++I) {
MachineInstr *S2 = *I;
if (!storesAreAdjacent(S1, S2))
break;
unsigned S2Size = getStoreTarget(S2).getSize();
if (SizeAccum + S2Size > std::min(MaxSize, Alignment))
break;
OG.push_back(S2);
SizeAccum += S2Size;
if (isPowerOf2_32(SizeAccum)) {
Pow2Num = OG.size();
Pow2Size = SizeAccum;
}
if ((2*Pow2Size-1) & FirstOffset)
break;
S1 = S2;
}
if (Pow2Num <= 1) {
OG.clear();
return false;
}
OG.resize(Pow2Num);
TotalSize = Pow2Size;
return true;
}
bool HexagonStoreWidening::createWideStores(InstrGroup &OG, InstrGroup &NG,
unsigned TotalSize) {
if (TotalSize > 4)
return false;
unsigned Acc = 0; unsigned Shift = 0;
for (MachineInstr *MI : OG) {
const MachineMemOperand &MMO = getStoreTarget(MI);
MachineOperand &SO = MI->getOperand(2); assert(SO.isImm() && "Expecting an immediate operand");
unsigned NBits = MMO.getSize()*8;
unsigned Mask = (0xFFFFFFFFU >> (32-NBits));
unsigned Val = (SO.getImm() & Mask) << Shift;
Acc |= Val;
Shift += NBits;
}
MachineInstr *FirstSt = OG.front();
DebugLoc DL = OG.back()->getDebugLoc();
const MachineMemOperand &OldM = getStoreTarget(FirstSt);
MachineMemOperand *NewM =
MF->getMachineMemOperand(OldM.getPointerInfo(), OldM.getFlags(),
TotalSize, OldM.getAlign(), OldM.getAAInfo());
if (Acc < 0x10000) {
unsigned WOpc = (TotalSize == 2) ? Hexagon::S4_storeirh_io :
(TotalSize == 4) ? Hexagon::S4_storeiri_io : 0;
assert(WOpc && "Unexpected size");
int Val = (TotalSize == 2) ? int16_t(Acc) : int(Acc);
const MCInstrDesc &StD = TII->get(WOpc);
MachineOperand &MR = FirstSt->getOperand(0);
int64_t Off = FirstSt->getOperand(1).getImm();
MachineInstr *StI =
BuildMI(*MF, DL, StD)
.addReg(MR.getReg(), getKillRegState(MR.isKill()), MR.getSubReg())
.addImm(Off)
.addImm(Val);
StI->addMemOperand(*MF, NewM);
NG.push_back(StI);
} else {
const MCInstrDesc &TfrD = TII->get(Hexagon::A2_tfrsi);
const TargetRegisterClass *RC = TII->getRegClass(TfrD, 0, TRI, *MF);
Register VReg = MF->getRegInfo().createVirtualRegister(RC);
MachineInstr *TfrI = BuildMI(*MF, DL, TfrD, VReg)
.addImm(int(Acc));
NG.push_back(TfrI);
unsigned WOpc = (TotalSize == 2) ? Hexagon::S2_storerh_io :
(TotalSize == 4) ? Hexagon::S2_storeri_io : 0;
assert(WOpc && "Unexpected size");
const MCInstrDesc &StD = TII->get(WOpc);
MachineOperand &MR = FirstSt->getOperand(0);
int64_t Off = FirstSt->getOperand(1).getImm();
MachineInstr *StI =
BuildMI(*MF, DL, StD)
.addReg(MR.getReg(), getKillRegState(MR.isKill()), MR.getSubReg())
.addImm(Off)
.addReg(VReg, RegState::Kill);
StI->addMemOperand(*MF, NewM);
NG.push_back(StI);
}
return true;
}
bool HexagonStoreWidening::replaceStores(InstrGroup &OG, InstrGroup &NG) {
LLVM_DEBUG({
dbgs() << "Replacing:\n";
for (auto I : OG)
dbgs() << " " << *I;
dbgs() << "with\n";
for (auto I : NG)
dbgs() << " " << *I;
});
MachineBasicBlock *MBB = OG.back()->getParent();
MachineBasicBlock::iterator InsertAt = MBB->end();
SmallPtrSet<MachineInstr*, 4> InstrSet;
for (auto I : OG)
InstrSet.insert(I);
for (auto &I : *MBB) {
if (InstrSet.count(&I)) {
InsertAt = I;
break;
}
}
assert((InsertAt != MBB->end()) && "Cannot locate any store from the group");
bool AtBBStart = false;
if (InsertAt != MBB->begin())
--InsertAt;
else
AtBBStart = true;
for (auto I : OG)
I->eraseFromParent();
if (!AtBBStart)
++InsertAt;
else
InsertAt = MBB->begin();
for (auto I : NG)
MBB->insert(InsertAt, I);
return true;
}
bool HexagonStoreWidening::processStoreGroup(InstrGroup &Group) {
bool Changed = false;
InstrGroup::iterator I = Group.begin(), E = Group.end();
InstrGroup OG, NG; unsigned CollectedSize;
while (I != E) {
OG.clear();
NG.clear();
bool Succ = selectStores(I++, E, OG, CollectedSize, MaxWideSize) &&
createWideStores(OG, NG, CollectedSize) &&
replaceStores(OG, NG);
if (!Succ)
continue;
assert(OG.size() > 1 && "Created invalid group");
assert(distance(I, E)+1 >= int(OG.size()) && "Too many elements");
I += OG.size()-1;
Changed = true;
}
return Changed;
}
bool HexagonStoreWidening::processBasicBlock(MachineBasicBlock &MBB) {
InstrGroupList SGs;
bool Changed = false;
createStoreGroups(MBB, SGs);
auto Less = [] (const MachineInstr *A, const MachineInstr *B) -> bool {
return getStoreOffset(A) < getStoreOffset(B);
};
for (auto &G : SGs) {
assert(G.size() > 1 && "Store group with fewer than 2 elements");
llvm::sort(G, Less);
Changed |= processStoreGroup(G);
}
return Changed;
}
bool HexagonStoreWidening::runOnMachineFunction(MachineFunction &MFn) {
if (skipFunction(MFn.getFunction()))
return false;
MF = &MFn;
auto &ST = MFn.getSubtarget<HexagonSubtarget>();
TII = ST.getInstrInfo();
TRI = ST.getRegisterInfo();
MRI = &MFn.getRegInfo();
AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
bool Changed = false;
for (auto &B : MFn)
Changed |= processBasicBlock(B);
return Changed;
}
FunctionPass *llvm::createHexagonStoreWidening() {
return new HexagonStoreWidening();
}