Commit d9f0ded0 authored by Junjie Gu's avatar Junjie Gu Committed by gbsbuild

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
parent 598cb6b0
......@@ -140,8 +140,9 @@ void DeSSA::dump() const {
print(ods());
}
bool DeSSA::runOnFunction(Function &MF) {
bool DeSSA::runOnFunction(Function &MF)
{
CurrColor = 0;
MetaDataUtils *pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
if (pMdUtils->findFunctionsInfoItem(&MF) == pMdUtils->end_FunctionsInfo())
{
......@@ -299,7 +300,7 @@ bool DeSSA::runOnFunction(Function &MF) {
// Perform a depth-first traversal of the dominator tree, splitting
// interferences amongst PHI-congruence classes.
if (!RegNodeMap.empty()) {
DenseMap<Value*, Value*> CurrentDominatingParent;
DenseMap<int, Value*> CurrentDominatingParent;
DenseMap<Value*, Value*> ImmediateDominatingParent;
// first, go through the function arguments
SplitInterferencesForArgument(CurrentDominatingParent, ImmediateDominatingParent);
......@@ -320,7 +321,7 @@ bool DeSSA::runOnFunction(Function &MF) {
void DeSSA::MapAddReg(MapVector<Value*, Node*> &Map, Value *Val, e_alignment Align) {
if (Map.count(Val))
return;
Map[Val] = new (Allocator) Node(Val, Align);
Map[Val] = new (Allocator) Node(Val, ++CurrColor, Align);
}
DeSSA::Node*
......@@ -352,6 +353,19 @@ Value* DeSSA::getRegRoot(Value* Val, e_alignment *pAlign) const {
return TheLeader->value;
}
int DeSSA::getRootColor(Value* V)
{
auto RI = RegNodeMap.find(V);
if (RI == RegNodeMap.end())
return 0;
Node *TheNode = RI->second;
if (TheNode->parent.getInt() &
(Node::kRegisterIsolatedFlag | Node::kPHIIsolatedFlag))
return 0;
Node *TheLeader = TheNode->getLeader();
return TheLeader->color;
}
void DeSSA::MapUnionRegs(MapVector<Value*, Node*> &Map, Value* Val1, Value* Val2) {
Node *Node1 = Map[Val1]->getLeader();
Node *Node2 = Map[Val2]->getLeader();
......@@ -466,7 +480,7 @@ bool DeSSA::isPHIIsolated(Instruction *PHI) const {
void
DeSSA::SplitInterferencesForBasicBlock(
BasicBlock *MBB,
DenseMap<Value*, Value*> &CurrentDominatingParent,
DenseMap<int, Value*> &CurrentDominatingParent,
DenseMap<Value*, Value*> &ImmediateDominatingParent) {
// Sort defs by their order in the original basic block, as the code below
// assumes that it is processing definitions in dominance order.
......@@ -480,8 +494,8 @@ DeSSA::SplitInterferencesForBasicBlock(
// If the virtual register being defined is not used in any PHI or has
// already been isolated, then there are no more interferences to check.
Value* RootV = getRegRoot(DefMI);
if (!RootV)
int RootC = getRootColor(DefMI);
if (!RootC)
continue;
// The input to this pass sometimes is not in SSA form in every basic
......@@ -490,7 +504,7 @@ DeSSA::SplitInterferencesForBasicBlock(
// handle it here by tracking defining machine instructions rather than
// virtual registers. For now, we just handle the situation conservatively
// in a way that will possibly lead to false interferences.
Value* NewParent = CurrentDominatingParent[RootV];
Value* NewParent = CurrentDominatingParent[RootC];
if (NewParent == DefMI)
continue;
......@@ -524,12 +538,12 @@ DeSSA::SplitInterferencesForBasicBlock(
// could be improved by using a heuristic that decides which of the two
// registers to isolate.
isolateReg(DefMI);
CurrentDominatingParent[RootV] = NewParent;
CurrentDominatingParent[RootC] = NewParent;
} else {
// If there is no interference, update ImmediateDominatingParent and set
// the CurrentDominatingParent for this color to the current register.
ImmediateDominatingParent[DefMI] = NewParent;
CurrentDominatingParent[RootV] = DefMI;
CurrentDominatingParent[RootC] = DefMI;
}
}
......@@ -576,7 +590,7 @@ DeSSA::SplitInterferencesForBasicBlock(
}
// check live-out interference
Value *RootV = getRegRoot(PHI);
int RootC = getRootColor(PHI);
#if 0
for (unsigned i = 0; !RootV && i < PHI->getNumOperands(); i++) {
Value* SrcVal = PHI->getOperand(i);
......@@ -588,12 +602,12 @@ DeSSA::SplitInterferencesForBasicBlock(
}
}
#endif
if (!RootV)
if (!RootC)
continue;
// Pop registers from the stack represented by ImmediateDominatingParent
// until we find a parent that dominates the current instruction.
Value *NewParent = CurrentDominatingParent[RootV];
Value *NewParent = CurrentDominatingParent[RootC];
while (NewParent) {
if (getRegRoot(NewParent)) {
if (isa<Argument>(NewParent)) {
......@@ -604,7 +618,7 @@ DeSSA::SplitInterferencesForBasicBlock(
}
NewParent = ImmediateDominatingParent[NewParent];
}
CurrentDominatingParent[RootV] = NewParent;
CurrentDominatingParent[RootC] = NewParent;
// If there is an interference with a register, always isolate the
// register rather than the PHI. It is also possible to isolate the
......@@ -619,7 +633,7 @@ DeSSA::SplitInterferencesForBasicBlock(
void
DeSSA::SplitInterferencesForArgument(
DenseMap<Value*, Value*> &CurrentDominatingParent,
DenseMap<int, Value*> &CurrentDominatingParent,
DenseMap<Value*, Value*> &ImmediateDominatingParent) {
// No two arguments can be in the same congruent class
for (auto BBI = PHISrcArgs.begin(),
......@@ -627,14 +641,14 @@ DeSSA::SplitInterferencesForArgument(
Value *AV = *BBI;
// If the virtual register being defined is not used in any PHI or has
// already been isolated, then there are no more interferences to check.
Value* RootV = getRegRoot(AV);
if (!RootV)
int RootC = getRootColor(AV);
if (!RootC)
continue;
Value* NewParent = CurrentDominatingParent[RootV];
Value* NewParent = CurrentDominatingParent[RootC];
if (NewParent) {
isolateReg(AV);
} else {
CurrentDominatingParent[RootV] = AV;
CurrentDominatingParent[RootC] = AV;
}
}
}
......
......@@ -146,9 +146,9 @@ class DeSSA : public llvm::FunctionPass {
kRegisterIsolatedFlag = 1,
kPHIIsolatedFlag = 1
};
Node(llvm::Value *v, e_alignment align)
Node(llvm::Value *v, int c, e_alignment align)
: next(this), prev(this), value(v)
, rank(0), alignment(align)
, rank(0), alignment(align), color(c)
{
parent.setPointer(this);
}
......@@ -163,6 +163,11 @@ class DeSSA : public llvm::FunctionPass {
llvm::Value *value;
unsigned rank;
e_alignment alignment;
// Unique one for each node. Used to represent the color
// (in another word, id or label) of a congruent class.
// Start from 1
int color;
};
/// Add a register in a new congruence class containing only itself.
......@@ -186,6 +191,9 @@ class DeSSA : public llvm::FunctionPass {
/// all of its operands (before they were isolated if they were).
llvm::Value* getOrigRoot(llvm::Instruction*) const;
// Return color (>0) if V is in a congruent class; return 0 otherwise.
int getRootColor(llvm::Value* V);
// Isolate a register.
void isolateReg(llvm::Value*);
......@@ -204,11 +212,11 @@ class DeSSA : public llvm::FunctionPass {
/// of the dominator tree.
void SplitInterferencesForBasicBlock(
llvm::BasicBlock*,
llvm::DenseMap<llvm::Value*, llvm::Value*> &CurrentDominatingParent,
llvm::DenseMap<int, llvm::Value*> &CurrentDominatingParent,
llvm::DenseMap<llvm::Value*, llvm::Value*> &ImmediateDominatingParent);
void SplitInterferencesForArgument(
llvm::DenseMap<llvm::Value*, llvm::Value*> &CurrentDominatingParent,
llvm::DenseMap<int, llvm::Value*> &CurrentDominatingParent,
llvm::DenseMap<llvm::Value*, llvm::Value*> &ImmediateDominatingParent);
void SplitInterferencesForAlignment();
......@@ -221,6 +229,10 @@ class DeSSA : public llvm::FunctionPass {
const llvm::DataLayout *DL;
llvm::BumpPtrAllocator Allocator;
// Color (label) assigned to each congruent class
// start from 1. Make sure each Node has a different
// color number.
int CurrColor;
public:
LiveVars *getLiveVars() const { return LV; }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment