37#define DEBUG_TYPE "vec-merger"
43 return MI &&
MI->isImplicitDef();
51 DenseMap<Register, unsigned> RegToChan;
52 std::vector<Register> UndefReg;
54 RegSeqInfo(MachineRegisterInfo &MRI, MachineInstr *
MI) : Instr(
MI) {
55 assert(
MI->getOpcode() == R600::REG_SEQUENCE);
56 for (
unsigned i = 1, e = Instr->getNumOperands(); i < e; i+=2) {
57 MachineOperand &MO = Instr->getOperand(i);
58 unsigned Chan = Instr->getOperand(i + 1).getImm();
60 UndefReg.emplace_back(Chan);
62 RegToChan[MO.
getReg()] = Chan;
66 RegSeqInfo() =
default;
69 return RSI.Instr == Instr;
75 using InstructionSetMap = DenseMap<unsigned, std::vector<MachineInstr *>>;
77 MachineRegisterInfo *MRI;
78 const R600InstrInfo *TII =
nullptr;
79 DenseMap<MachineInstr *, RegSeqInfo> PreviousRegSeq;
80 InstructionSetMap PreviousRegSeqByReg;
81 InstructionSetMap PreviousRegSeqByUndefCount;
83 bool canSwizzle(
const MachineInstr &
MI)
const;
85 void SwizzleInput(MachineInstr &,
86 const std::vector<std::pair<unsigned, unsigned>> &RemapChan)
const;
87 bool tryMergeVector(
const RegSeqInfo *Untouched, RegSeqInfo *ToMerge,
88 std::vector<std::pair<unsigned, unsigned>> &Remap)
const;
89 bool tryMergeUsingCommonSlot(RegSeqInfo &RSI, RegSeqInfo &CompatibleRSI,
90 std::vector<std::pair<unsigned, unsigned>> &RemapChan);
91 bool tryMergeUsingFreeSlot(RegSeqInfo &RSI, RegSeqInfo &CompatibleRSI,
92 std::vector<std::pair<unsigned, unsigned>> &RemapChan);
93 MachineInstr *RebuildVector(RegSeqInfo *
MI,
const RegSeqInfo *BaseVec,
94 const std::vector<std::pair<unsigned, unsigned>> &RemapChan)
const;
95 void RemoveMI(MachineInstr *);
96 void trackRSI(
const RegSeqInfo &RSI);
101 R600VectorRegMerger() : MachineFunctionPass(ID) {}
103 void getAnalysisUsage(AnalysisUsage &AU)
const override {
108 MachineFunctionProperties getRequiredProperties()
const override {
109 return MachineFunctionProperties().setIsSSA();
112 StringRef getPassName()
const override {
113 return "R600 Vector Registers Merge Pass";
122 "R600 Vector Reg Merger",
false,
false)
126char R600VectorRegMerger::ID = 0;
134 switch (
MI.getOpcode()) {
135 case R600::R600_ExportSwz:
136 case R600::EG_ExportSwz:
143bool R600VectorRegMerger::tryMergeVector(
const RegSeqInfo *Untouched,
144 RegSeqInfo *ToMerge, std::vector< std::pair<unsigned, unsigned>> &Remap)
146 unsigned CurrentUndexIdx = 0;
147 for (
auto &It : ToMerge->RegToChan) {
148 auto PosInUntouched = Untouched->RegToChan.
find(It.first);
149 if (PosInUntouched != Untouched->RegToChan.
end()) {
150 Remap.emplace_back(It.second, (*PosInUntouched).second);
153 if (CurrentUndexIdx >= Untouched->UndefReg.size())
155 Remap.emplace_back(It.second, Untouched->UndefReg[CurrentUndexIdx++]);
163 const std::vector<std::pair<unsigned, unsigned>> &RemapChan,
165 for (
const auto &J : RemapChan) {
172MachineInstr *R600VectorRegMerger::RebuildVector(
173 RegSeqInfo *RSI,
const RegSeqInfo *BaseRSI,
174 const std::vector<std::pair<unsigned, unsigned>> &RemapChan)
const {
177 MachineBasicBlock &
MBB = *Pos->getParent();
181 DenseMap<Register, unsigned> UpdatedRegToChan = BaseRSI->RegToChan;
182 std::vector<Register> UpdatedUndef = BaseRSI->UndefReg;
183 for (
const auto &It : RSI->RegToChan) {
185 unsigned SubReg = It.first;
189 MachineInstr *Tmp =
BuildMI(
MBB, Pos,
DL,
TII->get(R600::INSERT_SUBREG),
194 UpdatedRegToChan[SubReg] = Chan;
195 std::vector<Register>::iterator ChanPos =
llvm::find(UpdatedUndef, Chan);
196 if (ChanPos != UpdatedUndef.end())
197 UpdatedUndef.erase(ChanPos);
199 "UpdatedUndef shouldn't contain Chan more than once!");
204 MachineInstr *NewMI =
212 SwizzleInput(*It, RemapChan);
219 RSI->RegToChan = std::move(UpdatedRegToChan);
220 RSI->UndefReg = std::move(UpdatedUndef);
225void R600VectorRegMerger::RemoveMI(MachineInstr *
MI) {
226 for (
auto &It : PreviousRegSeqByReg) {
227 std::vector<MachineInstr *> &MIs = It.second;
230 for (
auto &It : PreviousRegSeqByUndefCount) {
231 std::vector<MachineInstr *> &MIs = It.second;
236void R600VectorRegMerger::SwizzleInput(MachineInstr &
MI,
237 const std::vector<std::pair<unsigned, unsigned>> &RemapChan)
const {
243 for (
unsigned i = 0; i < 4; i++) {
245 for (
const auto &J : RemapChan) {
247 MI.getOperand(i +
Offset).setImm(J.second - 1);
254bool R600VectorRegMerger::areAllUsesSwizzeable(
Register Reg)
const {
256 [&](
const MachineInstr &
MI) { return canSwizzle(MI); });
259bool R600VectorRegMerger::tryMergeUsingCommonSlot(RegSeqInfo &RSI,
260 RegSeqInfo &CompatibleRSI,
261 std::vector<std::pair<unsigned, unsigned>> &RemapChan) {
266 auto &Insts = PreviousRegSeqByReg[MOp->getReg()];
269 for (MachineInstr *
MI : Insts) {
270 CompatibleRSI = PreviousRegSeq[
MI];
271 if (RSI == CompatibleRSI)
273 if (tryMergeVector(&CompatibleRSI, &RSI, RemapChan))
280bool R600VectorRegMerger::tryMergeUsingFreeSlot(RegSeqInfo &RSI,
281 RegSeqInfo &CompatibleRSI,
282 std::vector<std::pair<unsigned, unsigned>> &RemapChan) {
283 unsigned NeededUndefs = 4 - RSI.UndefReg.size();
284 std::vector<MachineInstr *> &MIs =
285 PreviousRegSeqByUndefCount[NeededUndefs];
288 CompatibleRSI = PreviousRegSeq[MIs.back()];
289 tryMergeVector(&CompatibleRSI, &RSI, RemapChan);
293void R600VectorRegMerger::trackRSI(
const RegSeqInfo &RSI) {
294 for (DenseMap<Register, unsigned>::const_iterator
295 It = RSI.RegToChan.
begin(),
E = RSI.RegToChan.
end(); It !=
E; ++It) {
296 PreviousRegSeqByReg[(*It).first].push_back(RSI.Instr);
298 PreviousRegSeqByUndefCount[RSI.UndefReg.size()].push_back(RSI.Instr);
299 PreviousRegSeq[RSI.Instr] = RSI;
307 TII =
ST.getInstrInfo();
310 for (MachineBasicBlock &MB : Fn) {
311 PreviousRegSeq.
clear();
312 PreviousRegSeqByReg.clear();
313 PreviousRegSeqByUndefCount.clear();
316 MII != MIIE; ++MII) {
317 MachineInstr &
MI = *MII;
318 if (
MI.getOpcode() != R600::REG_SEQUENCE) {
327 RegSeqInfo RSI(*MRI, &
MI);
331 if (!areAllUsesSwizzeable(
Reg))
335 dbgs() <<
"Trying to optimize ";
339 RegSeqInfo CandidateRSI;
340 std::vector<std::pair<unsigned, unsigned>> RemapChan;
342 if (tryMergeUsingCommonSlot(RSI, CandidateRSI, RemapChan)) {
344 RemoveMI(CandidateRSI.Instr);
345 MII = RebuildVector(&RSI, &CandidateRSI, RemapChan);
351 if (tryMergeUsingFreeSlot(RSI, CandidateRSI, RemapChan)) {
352 RemoveMI(CandidateRSI.Instr);
353 MII = RebuildVector(&RSI, &CandidateRSI, RemapChan);
365 return new R600VectorRegMerger();
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
Promote Memory to Register
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
static std::vector< std::pair< int, unsigned > > Swizzle(std::vector< std::pair< int, unsigned > > Src, R600InstrInfo::BankSwizzle Swz)
Provides R600 specific target descriptions.
static unsigned getReassignedChan(const std::vector< std::pair< unsigned, unsigned > > &RemapChan, unsigned Chan)
static bool isImplicitlyDef(MachineRegisterInfo &MRI, Register Reg)
AMDGPU R600 specific subclass of TargetSubtarget.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
iterator find(const_arg_type_t< KeyT > Val)
FunctionPass class - This class is used to implement most global optimizations.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
mop_iterator operands_begin()
mop_iterator operands_end()
MachineOperand * mop_iterator
iterator/begin/end - Iterate over all operands of a machine instruction.
LLVM_ABI void dump() const
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
defusechain_instr_iterator< true, false, false, true > use_instr_iterator
use_instr_iterator/use_instr_begin/use_instr_end - Walk all uses of the specified register,...
iterator_range< def_instr_iterator > def_instructions(Register Reg) const
use_instr_iterator use_instr_begin(Register RegNo) const
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
static use_instr_iterator use_instr_end()
iterator_range< use_instr_iterator > use_instructions(Register Reg) const
LLVM_ABI LLVM_READONLY MachineInstr * getUniqueVRegDef(Register Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
Wrapper class representing virtual and physical registers.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
FunctionPass * createR600VectorRegMerger()
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
char & R600VectorRegMergerID