Commit c9d70d54 by Nicolas Capens

Eliminate dead code.

Bug swiftshader:23 Change-Id: Ifb2862e8358141f67a7974d3fa0a11e6fe41b904 Reviewed-on: https://swiftshader-review.googlesource.com/8290Tested-by: 's avatarNicolas Capens <capn@google.com> Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com> Reviewed-by: 's avatarNicolas Capens <capn@google.com>
parent 16252ab6
...@@ -29,13 +29,14 @@ namespace ...@@ -29,13 +29,14 @@ namespace
private: private:
void analyzeUses(Ice::Cfg *function); void analyzeUses(Ice::Cfg *function);
void eliminateUnusedAllocas(); void eliminateDeadCode();
void eliminateUnitializedLoads(); void eliminateUnitializedLoads();
void eliminateLoadsFollowingSingleStore(); void eliminateLoadsFollowingSingleStore();
void optimizeStoresInSingleBasicBlock(); void optimizeStoresInSingleBasicBlock();
void replace(Ice::Inst *instruction, Ice::Operand *newValue); void replace(Ice::Inst *instruction, Ice::Operand *newValue);
void deleteInstruction(Ice::Inst *instruction); void deleteInstruction(Ice::Inst *instruction);
bool isDead(Ice::Inst *instruction);
static bool isLoad(const Ice::Inst &instruction); static bool isLoad(const Ice::Inst &instruction);
static bool isStore(const Ice::Inst &instruction); static bool isStore(const Ice::Inst &instruction);
...@@ -68,30 +69,37 @@ namespace ...@@ -68,30 +69,37 @@ namespace
analyzeUses(function); analyzeUses(function);
eliminateUnusedAllocas(); eliminateDeadCode();
eliminateUnitializedLoads(); eliminateUnitializedLoads();
eliminateLoadsFollowingSingleStore(); eliminateLoadsFollowingSingleStore();
optimizeStoresInSingleBasicBlock(); optimizeStoresInSingleBasicBlock();
eliminateDeadCode();
} }
void Optimizer::eliminateUnusedAllocas() void Optimizer::eliminateDeadCode()
{ {
Ice::CfgNode *entryBlock = function->getEntryNode(); bool modified;
do
for(Ice::Inst &alloca : entryBlock->getInsts())
{ {
if(!llvm::isa<Ice::InstAlloca>(alloca)) modified = false;
for(Ice::CfgNode *basicBlock : function->getNodes())
{ {
return; // Allocas are all at the top for(Ice::Inst &inst : Ice::reverse_range(basicBlock->getInsts()))
} {
if(inst.isDeleted())
Ice::Operand *address = alloca.getDest(); {
continue;
}
if(uses[address].empty()) if(isDead(&inst))
{ {
alloca.setDeleted(); deleteInstruction(&inst);
modified = true;
}
}
} }
} }
while(modified);
} }
void Optimizer::eliminateUnitializedLoads() void Optimizer::eliminateUnitializedLoads()
...@@ -397,7 +405,7 @@ namespace ...@@ -397,7 +405,7 @@ namespace
void Optimizer::deleteInstruction(Ice::Inst *instruction) void Optimizer::deleteInstruction(Ice::Inst *instruction)
{ {
if(instruction->isDeleted()) if(!instruction || instruction->isDeleted())
{ {
return; return;
} }
...@@ -429,6 +437,30 @@ namespace ...@@ -429,6 +437,30 @@ namespace
} }
} }
bool Optimizer::isDead(Ice::Inst *instruction)
{
Ice::Variable *dest = instruction->getDest();
if(dest)
{
return uses[dest].empty() && !instruction->hasSideEffects();
}
else if(isStore(*instruction))
{
if(Ice::Variable *address = llvm::dyn_cast<Ice::Variable>(storeAddress(instruction)))
{
Ice::Inst *def = definition[address];
if(!def || llvm::isa<Ice::InstAlloca>(def))
{
return uses[address].size() == 1; // Dead if this store is the only use
}
}
}
return false;
}
bool Optimizer::isLoad(const Ice::Inst &instruction) bool Optimizer::isLoad(const Ice::Inst &instruction)
{ {
if(llvm::isa<Ice::InstLoad>(&instruction)) if(llvm::isa<Ice::InstLoad>(&instruction))
......
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