1 //===-- SPIRVDuplicatesTracker.cpp - SPIR-V Duplicates Tracker --*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // General infrastructure for keeping track of the values that according to
10 // the SPIR-V binary layout should be global to the whole module.
12 //===----------------------------------------------------------------------===//
14 #include "SPIRVDuplicatesTracker.h"
19 void SPIRVGeneralDuplicatesTracker::prebuildReg2Entry(
20 SPIRVDuplicatesTracker
<T
> &DT
, SPIRVReg2EntryTy
&Reg2Entry
) {
21 for (auto &TPair
: DT
.getAllUses()) {
22 for (auto &RegPair
: TPair
.second
) {
23 const MachineFunction
*MF
= RegPair
.first
;
24 Register R
= RegPair
.second
;
25 MachineInstr
*MI
= MF
->getRegInfo().getUniqueVRegDef(R
);
28 Reg2Entry
[&MI
->getOperand(0)] = &TPair
.second
;
33 void SPIRVGeneralDuplicatesTracker::buildDepsGraph(
34 std::vector
<SPIRV::DTSortableEntry
*> &Graph
,
35 MachineModuleInfo
*MMI
= nullptr) {
36 SPIRVReg2EntryTy Reg2Entry
;
37 prebuildReg2Entry(TT
, Reg2Entry
);
38 prebuildReg2Entry(CT
, Reg2Entry
);
39 prebuildReg2Entry(GT
, Reg2Entry
);
40 prebuildReg2Entry(FT
, Reg2Entry
);
41 prebuildReg2Entry(AT
, Reg2Entry
);
42 prebuildReg2Entry(ST
, Reg2Entry
);
44 for (auto &Op2E
: Reg2Entry
) {
45 SPIRV::DTSortableEntry
*E
= Op2E
.second
;
48 const MachineRegisterInfo
&MRI
= U
.first
->getRegInfo();
49 MachineInstr
*MI
= MRI
.getUniqueVRegDef(U
.second
);
52 assert(MI
&& MI
->getParent() && "No MachineInstr created yet");
53 for (auto i
= MI
->getNumDefs(); i
< MI
->getNumOperands(); i
++) {
54 MachineOperand
&Op
= MI
->getOperand(i
);
57 MachineOperand
*RegOp
= &MRI
.getVRegDef(Op
.getReg())->getOperand(0);
58 assert((MI
->getOpcode() == SPIRV::OpVariable
&& i
== 3) ||
59 Reg2Entry
.count(RegOp
));
60 if (Reg2Entry
.count(RegOp
))
61 E
->addDep(Reg2Entry
[RegOp
]);
65 MachineInstr
*Next
= MI
->getNextNode();
66 if (Next
&& (Next
->getOpcode() == SPIRV::OpFunction
||
67 Next
->getOpcode() == SPIRV::OpFunctionParameter
)) {
68 E
->addDep(Reg2Entry
[&Next
->getOperand(0)]);
74 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
76 const Module
*M
= MMI
->getModule();
77 for (auto F
= M
->begin(), E
= M
->end(); F
!= E
; ++F
) {
78 const MachineFunction
*MF
= MMI
->getMachineFunction(*F
);
81 for (const MachineBasicBlock
&MBB
: *MF
) {
82 for (const MachineInstr
&CMI
: MBB
) {
83 MachineInstr
&MI
= const_cast<MachineInstr
&>(CMI
);
85 if (MI
.getNumExplicitDefs() > 0 &&
86 Reg2Entry
.count(&MI
.getOperand(0))) {
88 for (SPIRV::DTSortableEntry
*D
:
89 Reg2Entry
.lookup(&MI
.getOperand(0))->getDeps())
90 dbgs() << Register::virtReg2Index(D
->lookup(MF
)) << ", ";