Skip to content

Commit bdbe464

Browse files
bcheng0127igcbot
authored andcommitted
Do LVN to flag regsiter mov
Reduce extra flag register mov instructions
1 parent b660e06 commit bdbe464

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

visa/HWCaps.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ bool hasSmov() const { return getPlatformGeneration() < PlatformGen::XE; }
182182

183183
bool doAccSub() const { return getPlatformGeneration() >= PlatformGen::GEN11; }
184184

185+
bool doFlagLVN() const {
186+
return false;
187+
}
188+
185189
bool hasNFType() const {
186190
return getPlatform() >= GENX_ICLLP &&
187191
getPlatformGeneration() < PlatformGen::XE;

visa/Passes/LVN.cpp

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,20 @@ bool LVN::canReplaceUses(INST_LIST_ITER inst_it, UseList &uses,
150150
G4_DstRegRegion *lvnDst = lvnInst->getDst();
151151
G4_Declare *lvnDstTopDcl = lvnDst->getTopDcl();
152152

153+
if (def->getBase()->asRegVar()->getDeclare()->getRegFile() !=
154+
lvnDst->getBase()->asRegVar()->getDeclare()->getRegFile()) {
155+
return false;
156+
}
157+
158+
// The sizes of the flag variables must match
159+
if (def->getBase()->asRegVar()->getDeclare()->getRegFile() == G4_FLAG) {
160+
G4_Declare *defTopDcl = def->getTopDcl();
161+
if (defTopDcl->getNumElems() != lvnDstTopDcl->getNumElems() ||
162+
defTopDcl->getElemType() != lvnDstTopDcl->getElemType()) {
163+
return false;
164+
}
165+
}
166+
153167
#ifdef DEBUG_LVN_ON
154168
std::cerr << "Inst with same value in LVN Table:"
155169
<< "\n";
@@ -655,6 +669,32 @@ void LVN::removePhysicalVarRedefs(G4_DstRegRegion *dst) {
655669
}
656670
}
657671

672+
void LVN::removeFlagVarRedefs(G4_Declare *topdcl) {
673+
auto it = dclValueTable.find(topdcl);
674+
675+
for (auto &all : lvnTable) {
676+
for (auto it = all.second.begin(); it != all.second.end();) {
677+
auto item = (*it);
678+
bool erase = false;
679+
680+
auto dstTopDcl = item->inst->getDst()->getTopDcl();
681+
if (dstTopDcl->getRegVar()->isFlag()) {
682+
if (dstTopDcl == topdcl) {
683+
item->active = false;
684+
erase = true;
685+
}
686+
}
687+
688+
if (erase) {
689+
it = all.second.erase(it);
690+
continue;
691+
}
692+
693+
it++;
694+
}
695+
}
696+
}
697+
658698
bool LVN::checkIfInPointsTo(const G4_RegVar *addr, const G4_RegVar *var) const {
659699
// Check whether var is present in points2 of addr
660700
auto ptrToAllPointsTo = p2a.getAllInPointsTo(addr);
@@ -718,8 +758,23 @@ void LVN::removeRedefs(G4_INST *inst) {
718758
removePhysicalVarRedefs(dstRegRegion);
719759
}
720760
}
721-
}
722761

762+
G4_CondMod *condMod = inst->getCondMod();
763+
if (condMod && condMod->getBase() && condMod->getBase()->isRegVar()) {
764+
G4_Declare *condDcl = condMod->getTopDcl();
765+
if (condDcl && condDcl->getRegVar()->isFlag()) {
766+
removeFlagVarRedefs(condDcl);
767+
}
768+
}
769+
770+
G4_Predicate *predicate = inst->getPredicate();
771+
if (predicate && predicate->getBase() && predicate->getBase()->isRegVar()) {
772+
G4_Declare *predDcl = predicate->getTopDcl();
773+
if (predDcl && predDcl->getRegVar()->isFlag()) {
774+
removeFlagVarRedefs(predDcl);
775+
}
776+
}
777+
}
723778
int64_t LVN::getNegativeRepresentation(int64_t imm, G4_Type type) {
724779
union {
725780
double ddata;
@@ -1041,11 +1096,23 @@ bool LVN::addValue(G4_INST *inst) {
10411096

10421097
G4_Operand *dst = inst->getDst();
10431098
if (!dst->getBase() || !dst->getBase()->isRegVar() ||
1044-
dst->getBase()->asRegVar()->getDeclare()->getRegFile() != G4_GRF ||
1099+
(dst->getBase()->asRegVar()->getDeclare()->getRegFile() != G4_GRF &&
1100+
!(builder.doFlagLVN() &&
1101+
dst->getBase()->asRegVar()->getDeclare()->getRegFile() == G4_FLAG)) ||
10451102
dst->getTopDcl()->getAddressed()) {
10461103
return false;
10471104
}
10481105

1106+
// Only handle mov flag, imm
1107+
if (dst->getBase()->asRegVar()->getDeclare()->getRegFile() == G4_FLAG) {
1108+
if (inst->opcode() != G4_mov) {
1109+
return false;
1110+
}
1111+
if (!inst->getSrc(0)->isImm()) {
1112+
return false;
1113+
}
1114+
}
1115+
10491116
if (dst->getBase() && dst->getTopDcl()->isOutput()) {
10501117
return false;
10511118
}
@@ -1597,7 +1664,8 @@ void LVN::doLVN() {
15971664
oldValue = value;
15981665

15991666
if (isGlobal || value.isValueEmpty()) {
1600-
if (isGlobal && !value.isValueEmpty()) {
1667+
if (isGlobal && !value.isValueEmpty() &&
1668+
inst->getDst()->getTopDcl()->getRegFile() != G4_FLAG) {
16011669
// If variable is global, we want to add it to LVN table.
16021670
addGlobalValueToTable = true;
16031671
}

visa/Passes/LVN.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class LVN {
136136
bool sameGRFRef(G4_Declare *dcl1, G4_Declare *dcl2);
137137
void removeVirtualVarRedefs(G4_DstRegRegion *dst);
138138
void removePhysicalVarRedefs(G4_DstRegRegion *dst);
139+
void removeFlagVarRedefs(G4_Declare *dcl);
139140
void removeRedefs(G4_INST *inst);
140141
void replaceAllUses(G4_INST *defInst, bool negate, UseList &uses,
141142
G4_INST *lvnInst, bool keepRegion);

0 commit comments

Comments
 (0)