Skip to content

Commit b9b2c03

Browse files
elvinw-inteligcbot
authored andcommitted
Refactor constant folding pass
Run the pass until nothing can be folded
1 parent 8a032d6 commit b9b2c03

File tree

1 file changed

+56
-50
lines changed

1 file changed

+56
-50
lines changed

IGC/Compiler/CustomSafeOptPass.cpp

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4258,72 +4258,78 @@ bool IGCConstProp::runOnFunction(Function& F)
42584258
module = F.getParent();
42594259
// Initialize the worklist to all of the instructions ready to process...
42604260
llvm::SetVector<Instruction*> WorkList;
4261-
for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
4262-
{
4263-
WorkList.insert(&*i);
4264-
}
42654261
bool Changed = false;
4266-
m_TD = &F.getParent()->getDataLayout();
4267-
m_TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
4268-
while (!WorkList.empty())
4262+
bool NotClosed;
4263+
do // Fold constants until closure
42694264
{
4270-
Instruction* I = WorkList.pop_back_val(); // Get an element from the worklist...
4271-
if (I->use_empty()) // Don't muck with dead instructions...
4265+
NotClosed = false;
4266+
for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
42724267
{
4273-
continue;
4268+
WorkList.insert(&*i);
42744269
}
4275-
Constant* C = nullptr;
4276-
C = ConstantFoldInstruction(I, *m_TD, m_TLI);
4277-
4278-
if (!C && isa<CallInst>(I))
4270+
m_TD = &F.getParent()->getDataLayout();
4271+
m_TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
4272+
while (!WorkList.empty())
42794273
{
4280-
C = ConstantFoldCallInstruction(cast<CallInst>(I));
4281-
}
4274+
Instruction* I = WorkList.pop_back_val(); // Get an element from the worklist...
4275+
if (I->use_empty()) // Don't muck with dead instructions...
4276+
{
4277+
continue;
4278+
}
4279+
Constant* C = nullptr;
4280+
C = ConstantFoldInstruction(I, *m_TD, m_TLI);
42824281

4283-
// replace shader-constant load with the known value
4284-
if (!C && isa<LoadInst>(I))
4285-
{
4286-
C = replaceShaderConstant(cast<LoadInst>(I));
4287-
}
4288-
if (!C && isa<CmpInst>(I))
4289-
{
4290-
C = ConstantFoldCmpInst(cast<CmpInst>(I));
4291-
}
4292-
if (!C && isa<ExtractElementInst>(I))
4293-
{
4294-
C = ConstantFoldExtractElement(cast<ExtractElementInst>(I));
4295-
}
4296-
if (C)
4297-
{
4298-
// Add all of the users of this instruction to the worklist, they might
4299-
// be constant propagatable now...
4300-
for (Value::user_iterator UI = I->user_begin(), UE = I->user_end();
4301-
UI != UE; ++UI)
4282+
if (!C && isa<CallInst>(I))
43024283
{
4303-
WorkList.insert(cast<Instruction>(*UI));
4284+
C = ConstantFoldCallInstruction(cast<CallInst>(I));
43044285
}
43054286

4306-
// Replace all of the uses of a variable with uses of the constant.
4307-
I->replaceAllUsesWith(C);
4287+
// replace shader-constant load with the known value
4288+
if (!C && isa<LoadInst>(I))
4289+
{
4290+
C = replaceShaderConstant(cast<LoadInst>(I));
4291+
}
4292+
if (!C && isa<CmpInst>(I))
4293+
{
4294+
C = ConstantFoldCmpInst(cast<CmpInst>(I));
4295+
}
4296+
if (!C && isa<ExtractElementInst>(I))
4297+
{
4298+
C = ConstantFoldExtractElement(cast<ExtractElementInst>(I));
4299+
}
43084300

4309-
// Remove the dead instruction.
4310-
I->eraseFromParent();
4301+
if (C)
4302+
{
4303+
// Add all of the users of this instruction to the worklist, they might
4304+
// be constant propagatable now...
4305+
for (Value::user_iterator UI = I->user_begin(), UE = I->user_end();
4306+
UI != UE; ++UI)
4307+
{
4308+
WorkList.insert(cast<Instruction>(*UI));
4309+
}
43114310

4312-
// We made a change to the function...
4313-
Changed = true;
4311+
// Replace all of the uses of a variable with uses of the constant.
4312+
I->replaceAllUsesWith(C);
43144313

4315-
// I is erased, continue to the next one.
4316-
continue;
4317-
}
4314+
// Remove the dead instruction.
4315+
I->eraseFromParent();
43184316

4319-
if (GetElementPtrInst * GEP = dyn_cast<GetElementPtrInst>(I))
4320-
{
4321-
if ((m_enableSimplifyGEP || overrideEnableSimplifyGEP) && simplifyGEP(GEP))
4317+
// We made a change to the function...
4318+
Changed = NotClosed = true;
4319+
4320+
// I is erased, continue to the next one.
4321+
continue;
4322+
}
4323+
4324+
if (GetElementPtrInst * GEP = dyn_cast<GetElementPtrInst>(I))
43224325
{
4323-
Changed = true;
4326+
if ((m_enableSimplifyGEP || overrideEnableSimplifyGEP) && simplifyGEP(GEP))
4327+
{
4328+
Changed = NotClosed = true;
4329+
}
43244330
}
43254331
}
4326-
}
4332+
} while (NotClosed);
43274333
return Changed;
43284334
}
43294335

0 commit comments

Comments
 (0)