57 #define DEBUG_TYPE "vec-merger" 73 std::vector<unsigned> UndefReg;
81 UndefReg.push_back(Chan);
83 RegToChan[MO.
getReg()] = Chan;
87 RegSeqInfo() =
default;
90 return RSI.Instr == Instr;
101 InstructionSetMap PreviousRegSeqByReg;
102 InstructionSetMap PreviousRegSeqByUndefCount;
105 bool areAllUsesSwizzeable(
unsigned Reg)
const;
107 const std::vector<std::pair<unsigned, unsigned>> &RemapChan)
const;
108 bool tryMergeVector(
const RegSeqInfo *Untouched, RegSeqInfo *ToMerge,
109 std::vector<std::pair<unsigned, unsigned>> &Remap)
const;
110 bool tryMergeUsingCommonSlot(RegSeqInfo &RSI, RegSeqInfo &CompatibleRSI,
111 std::vector<std::pair<unsigned, unsigned>> &RemapChan);
112 bool tryMergeUsingFreeSlot(RegSeqInfo &RSI, RegSeqInfo &CompatibleRSI,
113 std::vector<std::pair<unsigned, unsigned>> &RemapChan);
114 MachineInstr *RebuildVector(RegSeqInfo *MI,
const RegSeqInfo *BaseVec,
115 const std::vector<std::pair<unsigned, unsigned>> &RemapChan)
const;
117 void trackRSI(
const RegSeqInfo &RSI);
134 return "R600 Vector Registers Merge Pass";
143 "R600 Vector Reg Merger",
false,
false)
147 char R600VectorRegMerger::
ID = 0;
155 switch (
MI.getOpcode()) {
156 case R600::R600_ExportSwz:
157 case R600::EG_ExportSwz:
164 bool R600VectorRegMerger::tryMergeVector(
const RegSeqInfo *Untouched,
165 RegSeqInfo *ToMerge, std::vector< std::pair<unsigned, unsigned>> &Remap)
167 unsigned CurrentUndexIdx = 0;
169 E = ToMerge->RegToChan.end(); It !=
E; ++It) {
171 Untouched->RegToChan.find((*It).first);
172 if (PosInUntouched != Untouched->RegToChan.end()) {
173 Remap.push_back(std::pair<unsigned, unsigned>
174 ((*It).second, (*PosInUntouched).second));
177 if (CurrentUndexIdx >= Untouched->UndefReg.size())
179 Remap.push_back(std::pair<unsigned, unsigned>
180 ((*It).second, Untouched->UndefReg[CurrentUndexIdx++]));
188 const std::vector<std::pair<unsigned, unsigned>> &RemapChan,
190 for (
unsigned j = 0, je = RemapChan.size(); j < je; j++) {
191 if (RemapChan[j].
first == Chan)
192 return RemapChan[j].second;
198 RegSeqInfo *RSI,
const RegSeqInfo *BaseRSI,
199 const std::vector<std::pair<unsigned, unsigned>> &RemapChan)
const {
205 Register SrcVec = BaseRSI->Instr->getOperand(0).getReg();
207 std::vector<unsigned> UpdatedUndef = BaseRSI->UndefReg;
209 E = RSI->RegToChan.end(); It !=
E; ++It) {
210 Register DstReg =
MRI->createVirtualRegister(&R600::R600_Reg128RegClass);
211 unsigned SubReg = (*It).first;
212 unsigned Swizzle = (*It).second;
220 UpdatedRegToChan[
SubReg] = Chan;
221 std::vector<unsigned>::iterator ChanPos =
llvm::find(UpdatedUndef, Chan);
222 if (ChanPos != UpdatedUndef.end())
223 UpdatedUndef.erase(ChanPos);
225 "UpdatedUndef shouldn't contain Chan more than once!");
231 BuildMI(MBB, Pos, DL,
TII->get(R600::COPY),
Reg).addReg(SrcVec);
236 E =
MRI->use_instr_end(); It !=
E; ++It) {
238 SwizzleInput(*It, RemapChan);
241 RSI->Instr->eraseFromParent();
245 RSI->RegToChan = UpdatedRegToChan;
246 RSI->UndefReg = UpdatedUndef;
252 for (InstructionSetMap::iterator It = PreviousRegSeqByReg.begin(),
253 E = PreviousRegSeqByReg.end(); It !=
E; ++It) {
254 std::vector<MachineInstr *> &MIs = (*It).second;
257 for (InstructionSetMap::iterator It = PreviousRegSeqByUndefCount.begin(),
258 E = PreviousRegSeqByUndefCount.end(); It !=
E; ++It) {
259 std::vector<MachineInstr *> &MIs = (*It).second;
264 void R600VectorRegMerger::SwizzleInput(
MachineInstr &MI,
265 const std::vector<std::pair<unsigned, unsigned>> &RemapChan)
const {
271 for (
unsigned i = 0; i < 4; i++) {
273 for (
unsigned j = 0,
e = RemapChan.size(); j <
e; j++) {
274 if (RemapChan[j].
first == Swizzle) {
282 bool R600VectorRegMerger::areAllUsesSwizzeable(
unsigned Reg)
const {
284 E =
MRI->use_instr_end(); It !=
E; ++It) {
285 if (!canSwizzle(*It))
291 bool R600VectorRegMerger::tryMergeUsingCommonSlot(RegSeqInfo &RSI,
292 RegSeqInfo &CompatibleRSI,
293 std::vector<std::pair<unsigned, unsigned>> &RemapChan) {
295 MOE = RSI.Instr->operands_end(); MOp != MOE; ++MOp) {
298 if (PreviousRegSeqByReg[MOp->getReg()].empty())
300 for (
MachineInstr *MI : PreviousRegSeqByReg[MOp->getReg()]) {
301 CompatibleRSI = PreviousRegSeq[
MI];
302 if (RSI == CompatibleRSI)
304 if (tryMergeVector(&CompatibleRSI, &RSI, RemapChan))
311 bool R600VectorRegMerger::tryMergeUsingFreeSlot(RegSeqInfo &RSI,
312 RegSeqInfo &CompatibleRSI,
313 std::vector<std::pair<unsigned, unsigned>> &RemapChan) {
314 unsigned NeededUndefs = 4 - RSI.UndefReg.size();
315 if (PreviousRegSeqByUndefCount[NeededUndefs].
empty())
317 std::vector<MachineInstr *> &MIs =
318 PreviousRegSeqByUndefCount[NeededUndefs];
319 CompatibleRSI = PreviousRegSeq[MIs.back()];
320 tryMergeVector(&CompatibleRSI, &RSI, RemapChan);
324 void R600VectorRegMerger::trackRSI(
const RegSeqInfo &RSI) {
326 It = RSI.RegToChan.begin(),
E = RSI.RegToChan.end(); It !=
E; ++It) {
327 PreviousRegSeqByReg[(*It).first].push_back(RSI.Instr);
329 PreviousRegSeqByUndefCount[RSI.UndefReg.size()].push_back(RSI.Instr);
330 PreviousRegSeq[RSI.Instr] = RSI;
342 MBB != MBBe; ++MBB) {
344 PreviousRegSeq.
clear();
345 PreviousRegSeqByReg.clear();
346 PreviousRegSeqByUndefCount.clear();
349 MII != MIIE; ++MII) {
351 if (MI.
getOpcode() != R600::REG_SEQUENCE) {
355 It =
MRI->def_instr_begin(Reg),
E =
MRI->def_instr_end();
363 RegSeqInfo RSI(*
MRI, &MI);
367 if (!areAllUsesSwizzeable(Reg))
371 dbgs() <<
"Trying to optimize ";
375 RegSeqInfo CandidateRSI;
376 std::vector<std::pair<unsigned, unsigned>> RemapChan;
378 if (tryMergeUsingCommonSlot(RSI, CandidateRSI, RemapChan)) {
380 RemoveMI(CandidateRSI.Instr);
381 MII = RebuildVector(&RSI, &CandidateRSI, RemapChan);
387 if (tryMergeUsingFreeSlot(RSI, CandidateRSI, RemapChan)) {
388 RemoveMI(CandidateRSI.Instr);
389 MII = RebuildVector(&RSI, &CandidateRSI, RemapChan);
401 return new R600VectorRegMerger();
INITIALIZE_PASS_BEGIN(R600VectorRegMerger, DEBUG_TYPE, "R600 Vector Reg Merger", false, false) INITIALIZE_PASS_END(R600VectorRegMerger
static bool isImplicitlyDef(MachineRegisterInfo &MRI, unsigned Reg)
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
AMDGPU specific subclass of TargetSubtarget.
This class represents lattice values for constants.
Interface definition for R600InstrInfo.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
static std::vector< std::pair< int, unsigned > > Swizzle(std::vector< std::pair< int, unsigned >> Src, R600InstrInfo::BankSwizzle Swz)
static const AMDGPUSubtarget & get(const MachineFunction &MF)
AnalysisUsage & addRequired()
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
unsigned getNumOperands() const
Retuns the total number of operands.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
defusechain_iterator - This class provides iterator support for machine operands in the function that...
char & R600VectorRegMergerID
FunctionPass * createR600VectorRegMerger()
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
unsigned const MachineRegisterInfo * MRI
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")
Represent the analysis usage information of a pass.
const R600InstrInfo * getInstrInfo() const override
void setImm(int64_t immVal)
FunctionPass class - This class is used to implement most global optimizations.
static unsigned getReassignedChan(const std::vector< std::pair< unsigned, unsigned >> &RemapChan, unsigned Chan)
bool isImplicitDef() const
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
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.
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
Iterator for intrusive lists based on ilist_node.
MachineOperand class - Representation of each machine instruction operand.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
MachineInstr * getUniqueVRegDef(unsigned Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
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.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Provides AMDGPU specific target descriptions.
Representation of each machine instruction.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
bool operator==(uint64_t V1, const APInt &V2)
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
Wrapper class representing virtual and physical registers.
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.