Skip to content

Commit d9f0ded

Browse files
jgu222gfxbot
authored andcommitted
Add color as a unique ID to represent a congruent class. This will
not use value as ID during DOM traversal. This is needed for removing isolated nodes from the congruent class. No functional change expected from this change Change-Id: I670a72c80d216f5aa253edc3f73f13e35e050ba8
1 parent 598cb6b commit d9f0ded

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

IGC/Compiler/CISACodeGen/DeSSA.cpp

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ void DeSSA::dump() const {
140140
print(ods());
141141
}
142142

143-
bool DeSSA::runOnFunction(Function &MF) {
144-
143+
bool DeSSA::runOnFunction(Function &MF)
144+
{
145+
CurrColor = 0;
145146
MetaDataUtils *pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
146147
if (pMdUtils->findFunctionsInfoItem(&MF) == pMdUtils->end_FunctionsInfo())
147148
{
@@ -299,7 +300,7 @@ bool DeSSA::runOnFunction(Function &MF) {
299300
// Perform a depth-first traversal of the dominator tree, splitting
300301
// interferences amongst PHI-congruence classes.
301302
if (!RegNodeMap.empty()) {
302-
DenseMap<Value*, Value*> CurrentDominatingParent;
303+
DenseMap<int, Value*> CurrentDominatingParent;
303304
DenseMap<Value*, Value*> ImmediateDominatingParent;
304305
// first, go through the function arguments
305306
SplitInterferencesForArgument(CurrentDominatingParent, ImmediateDominatingParent);
@@ -320,7 +321,7 @@ bool DeSSA::runOnFunction(Function &MF) {
320321
void DeSSA::MapAddReg(MapVector<Value*, Node*> &Map, Value *Val, e_alignment Align) {
321322
if (Map.count(Val))
322323
return;
323-
Map[Val] = new (Allocator) Node(Val, Align);
324+
Map[Val] = new (Allocator) Node(Val, ++CurrColor, Align);
324325
}
325326

326327
DeSSA::Node*
@@ -352,6 +353,19 @@ Value* DeSSA::getRegRoot(Value* Val, e_alignment *pAlign) const {
352353
return TheLeader->value;
353354
}
354355

356+
int DeSSA::getRootColor(Value* V)
357+
{
358+
auto RI = RegNodeMap.find(V);
359+
if (RI == RegNodeMap.end())
360+
return 0;
361+
Node *TheNode = RI->second;
362+
if (TheNode->parent.getInt() &
363+
(Node::kRegisterIsolatedFlag | Node::kPHIIsolatedFlag))
364+
return 0;
365+
Node *TheLeader = TheNode->getLeader();
366+
return TheLeader->color;
367+
}
368+
355369
void DeSSA::MapUnionRegs(MapVector<Value*, Node*> &Map, Value* Val1, Value* Val2) {
356370
Node *Node1 = Map[Val1]->getLeader();
357371
Node *Node2 = Map[Val2]->getLeader();
@@ -466,7 +480,7 @@ bool DeSSA::isPHIIsolated(Instruction *PHI) const {
466480
void
467481
DeSSA::SplitInterferencesForBasicBlock(
468482
BasicBlock *MBB,
469-
DenseMap<Value*, Value*> &CurrentDominatingParent,
483+
DenseMap<int, Value*> &CurrentDominatingParent,
470484
DenseMap<Value*, Value*> &ImmediateDominatingParent) {
471485
// Sort defs by their order in the original basic block, as the code below
472486
// assumes that it is processing definitions in dominance order.
@@ -480,8 +494,8 @@ DeSSA::SplitInterferencesForBasicBlock(
480494

481495
// If the virtual register being defined is not used in any PHI or has
482496
// already been isolated, then there are no more interferences to check.
483-
Value* RootV = getRegRoot(DefMI);
484-
if (!RootV)
497+
int RootC = getRootColor(DefMI);
498+
if (!RootC)
485499
continue;
486500

487501
// The input to this pass sometimes is not in SSA form in every basic
@@ -490,7 +504,7 @@ DeSSA::SplitInterferencesForBasicBlock(
490504
// handle it here by tracking defining machine instructions rather than
491505
// virtual registers. For now, we just handle the situation conservatively
492506
// in a way that will possibly lead to false interferences.
493-
Value* NewParent = CurrentDominatingParent[RootV];
507+
Value* NewParent = CurrentDominatingParent[RootC];
494508
if (NewParent == DefMI)
495509
continue;
496510

@@ -524,12 +538,12 @@ DeSSA::SplitInterferencesForBasicBlock(
524538
// could be improved by using a heuristic that decides which of the two
525539
// registers to isolate.
526540
isolateReg(DefMI);
527-
CurrentDominatingParent[RootV] = NewParent;
541+
CurrentDominatingParent[RootC] = NewParent;
528542
} else {
529543
// If there is no interference, update ImmediateDominatingParent and set
530544
// the CurrentDominatingParent for this color to the current register.
531545
ImmediateDominatingParent[DefMI] = NewParent;
532-
CurrentDominatingParent[RootV] = DefMI;
546+
CurrentDominatingParent[RootC] = DefMI;
533547
}
534548
}
535549

@@ -576,7 +590,7 @@ DeSSA::SplitInterferencesForBasicBlock(
576590
}
577591

578592
// check live-out interference
579-
Value *RootV = getRegRoot(PHI);
593+
int RootC = getRootColor(PHI);
580594
#if 0
581595
for (unsigned i = 0; !RootV && i < PHI->getNumOperands(); i++) {
582596
Value* SrcVal = PHI->getOperand(i);
@@ -588,12 +602,12 @@ DeSSA::SplitInterferencesForBasicBlock(
588602
}
589603
}
590604
#endif
591-
if (!RootV)
605+
if (!RootC)
592606
continue;
593607

594608
// Pop registers from the stack represented by ImmediateDominatingParent
595609
// until we find a parent that dominates the current instruction.
596-
Value *NewParent = CurrentDominatingParent[RootV];
610+
Value *NewParent = CurrentDominatingParent[RootC];
597611
while (NewParent) {
598612
if (getRegRoot(NewParent)) {
599613
if (isa<Argument>(NewParent)) {
@@ -604,7 +618,7 @@ DeSSA::SplitInterferencesForBasicBlock(
604618
}
605619
NewParent = ImmediateDominatingParent[NewParent];
606620
}
607-
CurrentDominatingParent[RootV] = NewParent;
621+
CurrentDominatingParent[RootC] = NewParent;
608622

609623
// If there is an interference with a register, always isolate the
610624
// register rather than the PHI. It is also possible to isolate the
@@ -619,22 +633,22 @@ DeSSA::SplitInterferencesForBasicBlock(
619633

620634
void
621635
DeSSA::SplitInterferencesForArgument(
622-
DenseMap<Value*, Value*> &CurrentDominatingParent,
636+
DenseMap<int, Value*> &CurrentDominatingParent,
623637
DenseMap<Value*, Value*> &ImmediateDominatingParent) {
624638
// No two arguments can be in the same congruent class
625639
for (auto BBI = PHISrcArgs.begin(),
626640
BBE = PHISrcArgs.end(); BBI != BBE; ++BBI) {
627641
Value *AV = *BBI;
628642
// If the virtual register being defined is not used in any PHI or has
629643
// already been isolated, then there are no more interferences to check.
630-
Value* RootV = getRegRoot(AV);
631-
if (!RootV)
644+
int RootC = getRootColor(AV);
645+
if (!RootC)
632646
continue;
633-
Value* NewParent = CurrentDominatingParent[RootV];
647+
Value* NewParent = CurrentDominatingParent[RootC];
634648
if (NewParent) {
635649
isolateReg(AV);
636650
} else {
637-
CurrentDominatingParent[RootV] = AV;
651+
CurrentDominatingParent[RootC] = AV;
638652
}
639653
}
640654
}

IGC/Compiler/CISACodeGen/DeSSA.hpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ class DeSSA : public llvm::FunctionPass {
146146
kRegisterIsolatedFlag = 1,
147147
kPHIIsolatedFlag = 1
148148
};
149-
Node(llvm::Value *v, e_alignment align)
149+
Node(llvm::Value *v, int c, e_alignment align)
150150
: next(this), prev(this), value(v)
151-
, rank(0), alignment(align)
151+
, rank(0), alignment(align), color(c)
152152
{
153153
parent.setPointer(this);
154154
}
@@ -163,6 +163,11 @@ class DeSSA : public llvm::FunctionPass {
163163
llvm::Value *value;
164164
unsigned rank;
165165
e_alignment alignment;
166+
167+
// Unique one for each node. Used to represent the color
168+
// (in another word, id or label) of a congruent class.
169+
// Start from 1
170+
int color;
166171
};
167172

168173
/// Add a register in a new congruence class containing only itself.
@@ -186,6 +191,9 @@ class DeSSA : public llvm::FunctionPass {
186191
/// all of its operands (before they were isolated if they were).
187192
llvm::Value* getOrigRoot(llvm::Instruction*) const;
188193

194+
// Return color (>0) if V is in a congruent class; return 0 otherwise.
195+
int getRootColor(llvm::Value* V);
196+
189197
// Isolate a register.
190198
void isolateReg(llvm::Value*);
191199

@@ -204,11 +212,11 @@ class DeSSA : public llvm::FunctionPass {
204212
/// of the dominator tree.
205213
void SplitInterferencesForBasicBlock(
206214
llvm::BasicBlock*,
207-
llvm::DenseMap<llvm::Value*, llvm::Value*> &CurrentDominatingParent,
215+
llvm::DenseMap<int, llvm::Value*> &CurrentDominatingParent,
208216
llvm::DenseMap<llvm::Value*, llvm::Value*> &ImmediateDominatingParent);
209217

210218
void SplitInterferencesForArgument(
211-
llvm::DenseMap<llvm::Value*, llvm::Value*> &CurrentDominatingParent,
219+
llvm::DenseMap<int, llvm::Value*> &CurrentDominatingParent,
212220
llvm::DenseMap<llvm::Value*, llvm::Value*> &ImmediateDominatingParent);
213221

214222
void SplitInterferencesForAlignment();
@@ -221,6 +229,10 @@ class DeSSA : public llvm::FunctionPass {
221229
const llvm::DataLayout *DL;
222230

223231
llvm::BumpPtrAllocator Allocator;
232+
// Color (label) assigned to each congruent class
233+
// start from 1. Make sure each Node has a different
234+
// color number.
235+
int CurrColor;
224236

225237
public:
226238
LiveVars *getLiveVars() const { return LV; }

0 commit comments

Comments
 (0)