Skip to content

Commit

Permalink
Refactor constant folding pass
Browse files Browse the repository at this point in the history
Run the pass until nothing can be folded
  • Loading branch information
elvinw-intel authored and igcbot committed Oct 12, 2023
1 parent 8a032d6 commit b9b2c03
Showing 1 changed file with 56 additions and 50 deletions.
106 changes: 56 additions & 50 deletions IGC/Compiler/CustomSafeOptPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4258,72 +4258,78 @@ bool IGCConstProp::runOnFunction(Function& F)
module = F.getParent();
// Initialize the worklist to all of the instructions ready to process...
llvm::SetVector<Instruction*> WorkList;
for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
{
WorkList.insert(&*i);
}
bool Changed = false;
m_TD = &F.getParent()->getDataLayout();
m_TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
while (!WorkList.empty())
bool NotClosed;
do // Fold constants until closure
{
Instruction* I = WorkList.pop_back_val(); // Get an element from the worklist...
if (I->use_empty()) // Don't muck with dead instructions...
NotClosed = false;
for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
{
continue;
WorkList.insert(&*i);
}
Constant* C = nullptr;
C = ConstantFoldInstruction(I, *m_TD, m_TLI);

if (!C && isa<CallInst>(I))
m_TD = &F.getParent()->getDataLayout();
m_TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
while (!WorkList.empty())
{
C = ConstantFoldCallInstruction(cast<CallInst>(I));
}
Instruction* I = WorkList.pop_back_val(); // Get an element from the worklist...
if (I->use_empty()) // Don't muck with dead instructions...
{
continue;
}
Constant* C = nullptr;
C = ConstantFoldInstruction(I, *m_TD, m_TLI);

// replace shader-constant load with the known value
if (!C && isa<LoadInst>(I))
{
C = replaceShaderConstant(cast<LoadInst>(I));
}
if (!C && isa<CmpInst>(I))
{
C = ConstantFoldCmpInst(cast<CmpInst>(I));
}
if (!C && isa<ExtractElementInst>(I))
{
C = ConstantFoldExtractElement(cast<ExtractElementInst>(I));
}
if (C)
{
// Add all of the users of this instruction to the worklist, they might
// be constant propagatable now...
for (Value::user_iterator UI = I->user_begin(), UE = I->user_end();
UI != UE; ++UI)
if (!C && isa<CallInst>(I))
{
WorkList.insert(cast<Instruction>(*UI));
C = ConstantFoldCallInstruction(cast<CallInst>(I));
}

// Replace all of the uses of a variable with uses of the constant.
I->replaceAllUsesWith(C);
// replace shader-constant load with the known value
if (!C && isa<LoadInst>(I))
{
C = replaceShaderConstant(cast<LoadInst>(I));
}
if (!C && isa<CmpInst>(I))
{
C = ConstantFoldCmpInst(cast<CmpInst>(I));
}
if (!C && isa<ExtractElementInst>(I))
{
C = ConstantFoldExtractElement(cast<ExtractElementInst>(I));
}

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

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

// I is erased, continue to the next one.
continue;
}
// Remove the dead instruction.
I->eraseFromParent();

if (GetElementPtrInst * GEP = dyn_cast<GetElementPtrInst>(I))
{
if ((m_enableSimplifyGEP || overrideEnableSimplifyGEP) && simplifyGEP(GEP))
// We made a change to the function...
Changed = NotClosed = true;

// I is erased, continue to the next one.
continue;
}

if (GetElementPtrInst * GEP = dyn_cast<GetElementPtrInst>(I))
{
Changed = true;
if ((m_enableSimplifyGEP || overrideEnableSimplifyGEP) && simplifyGEP(GEP))
{
Changed = NotClosed = true;
}
}
}
}
} while (NotClosed);
return Changed;
}

Expand Down

0 comments on commit b9b2c03

Please sign in to comment.