@@ -95,7 +95,7 @@ void IGCLivenessAnalysis::combineOut(llvm::BasicBlock *BB, ValueSet *Set) {
9595 }
9696}
9797
98- void addToSet (llvm::Instruction *Inst, ValueSet &Set) {
98+ void IGCLivenessAnalysis::addOperandsToSet (llvm::Instruction *Inst, ValueSet &Set) {
9999 for (auto &Op : Inst->operands ()) {
100100 llvm::Value *V = Op.get ();
101101 // We are counting only instructions right now
@@ -110,6 +110,26 @@ void addToSet(llvm::Instruction *Inst, ValueSet &Set) {
110110 }
111111}
112112
113+ void IGCLivenessAnalysis::addNonLocalOperandsToSet (llvm::Instruction *Inst, ValueSet &Set) {
114+ for (auto &Op : Inst->operands ()) {
115+ llvm::Value *V = Op.get ();
116+ // We are counting only instructions right now
117+ // potetntially we should also count globals, but
118+ // we defintely shouldn't count:
119+ // br label %bb1 (basic block names)
120+ // call %functionName (function names)
121+ // add %a, 1 (constants)
122+ Instruction *I = dyn_cast<Instruction>(V);
123+ bool IsInstruction = I != nullptr ;
124+ bool OperandInDifferentBB = IsInstruction && (I->getParent () != Inst->getParent ());
125+ bool IsArgument = !IsInstruction && llvm::isa<llvm::Argument>(V);
126+ if (OperandInDifferentBB || IsArgument)
127+ {
128+ Set.insert (V);
129+ }
130+ }
131+ }
132+
113133// idea is simple, each predecessor block that converges into our block
114134// has its own set of PHI values, that it has to deliver
115135// so we take values that are coming from each block
@@ -126,6 +146,7 @@ void IGCLivenessAnalysis::addToPhiSet(llvm::PHINode *Phi, PhiSet *InPhiSet) {
126146 (*InPhiSet)[BB].insert (ValueFromOurBlock);
127147 }
128148}
149+
129150// scan through block in reversed order and add each operand
130151// into IN block while deleting defined values
131152void IGCLivenessAnalysis::processBlock (llvm::BasicBlock *BB, ValueSet &Set,
@@ -139,14 +160,27 @@ void IGCLivenessAnalysis::processBlock(llvm::BasicBlock *BB, ValueSet &Set,
139160 addToPhiSet (Phi, PhiSet);
140161 continue ;
141162 }
142- addToSet (Inst, Set);
163+ addNonLocalOperandsToSet (Inst, Set);
143164 }
144165}
145166
146- void IGCLivenessAnalysis::livenessAnalysis (llvm::Function &F) {
167+ void IGCLivenessAnalysis::livenessAnalysis (llvm::Function &F, BBSet *StartBBs ) {
147168 std::queue<llvm::BasicBlock *> Worklist;
148- for (BasicBlock &BB : F)
149- Worklist.push (&BB);
169+
170+ if (StartBBs != nullptr )
171+ {
172+ // If StartBBs are provided we know that only these BBs could be changed
173+ // Add only them to the initial Worklist
174+ for (BasicBlock *BB : *StartBBs)
175+ Worklist.push (BB);
176+ }
177+ else
178+ {
179+ // Start with adding all BBs to the Worklist
180+ // to make sure In set is populated for every BB
181+ for (BasicBlock &BB : F)
182+ Worklist.push (&BB);
183+ }
150184
151185 while (!Worklist.empty ()) {
152186
@@ -214,7 +248,7 @@ void IGCLivenessAnalysis::collectPressureForBB(
214248
215249 auto Phi = llvm::dyn_cast<llvm::PHINode>(Inst);
216250 if (!Phi) {
217- addToSet (Inst, BBSet);
251+ addOperandsToSet (Inst, BBSet);
218252 }
219253
220254 BBListing[Inst] = Size;
@@ -225,7 +259,7 @@ void IGCLivenessAnalysis::collectPressureForBB(
225259bool IGCLivenessAnalysis::runOnFunction (llvm::Function &F) {
226260
227261 CGCtx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext ();
228- livenessAnalysis (F);
262+ livenessAnalysis (F, nullptr );
229263
230264 return true ;
231265}
0 commit comments