OSDN Git Service

Memory leak fix
[android-x86/external-swiftshader.git] / src / Reactor / Optimizer.cpp
index 83ea5f2..aac8c04 100644 (file)
@@ -17,7 +17,6 @@
 #include "src/IceCfg.h"
 #include "src/IceCfgNode.h"
 
-#include <unordered_map>
 #include <vector>
 
 namespace
@@ -61,9 +60,35 @@ namespace
                        std::vector<Ice::Inst*> stores;
                };
 
-               std::unordered_map<Ice::Operand*, Uses> uses;
-               std::unordered_map<Ice::Inst*, Ice::CfgNode*> node;
-               std::unordered_map<Ice::Variable*, Ice::Inst*> definition;
+               struct LoadStoreInst
+               {
+                       LoadStoreInst(Ice::Inst* inst, bool isStore)
+                         : inst(inst),
+                           address(isStore ? storeAddress(inst) : loadAddress(inst)),
+                           isStore(isStore)
+                       {
+                       }
+
+                       Ice::Inst* inst;
+                       Ice::Operand *address;
+                       bool isStore;
+               };
+
+               Optimizer::Uses* getUses(Ice::Operand*);
+               void setUses(Ice::Operand*, Optimizer::Uses*);
+               bool hasUses(Ice::Operand*) const;
+
+               Ice::CfgNode* getNode(Ice::Inst*);
+               void setNode(Ice::Inst*, Ice::CfgNode*);
+
+               Ice::Inst* getDefinition(Ice::Variable*);
+               void setDefinition(Ice::Variable*, Ice::Inst*);
+
+               const std::vector<LoadStoreInst>& getLoadStoreInsts(Ice::CfgNode*);
+               void setLoadStoreInsts(Ice::CfgNode*, std::vector<LoadStoreInst>*);
+               bool hasLoadStoreInsts(Ice::CfgNode* node) const;
+
+               std::vector<Optimizer::Uses*> allocatedUses;
        };
 
        void Optimizer::run(Ice::Cfg *function)
@@ -78,6 +103,12 @@ namespace
                eliminateLoadsFollowingSingleStore();
                optimizeStoresInSingleBasicBlock();
                eliminateDeadCode();
+
+               for(auto uses : allocatedUses)
+               {
+                       delete uses;
+               }
+               allocatedUses.clear();
        }
 
        void Optimizer::eliminateDeadCode()
@@ -119,12 +150,17 @@ namespace
 
                        if(!llvm::isa<Ice::InstAlloca>(alloca))
                        {
-                               return;   // Allocas are all at the top
+                               break;   // Allocas are all at the top
                        }
 
                        Ice::Operand *address = alloca.getDest();
-                       const auto &addressEntry = uses.find(address);
-                       const auto &addressUses = addressEntry->second;
+
+                       if(!hasUses(address))
+                       {
+                               continue;
+                       }
+
+                       const auto &addressUses = *getUses(address);
 
                        if(!addressUses.areOnlyLoadStore())
                        {
@@ -137,26 +173,29 @@ namespace
                                {
                                        Ice::Variable *loadData = load->getDest();
 
-                                       for(Ice::Inst *use : uses[loadData])
+                                       if(hasUses(loadData))
                                        {
-                                               for(Ice::SizeT i = 0; i < use->getSrcSize(); i++)
+                                               for(Ice::Inst *use : *getUses(loadData))
                                                {
-                                                       if(use->getSrc(i) == loadData)
+                                                       for(Ice::SizeT i = 0; i < use->getSrcSize(); i++)
                                                        {
-                                                               auto *undef = context->getConstantUndef(loadData->getType());
+                                                               if(use->getSrc(i) == loadData)
+                                                               {
+                                                                       auto *undef = context->getConstantUndef(loadData->getType());
 
-                                                               use->replaceSource(i, undef);
+                                                                       use->replaceSource(i, undef);
+                                                               }
                                                        }
                                                }
-                                       }
 
-                                       uses.erase(loadData);
+                                               setUses(loadData, nullptr);
+                                       }
 
                                        load->setDeleted();
                                }
 
                                alloca.setDeleted();
-                               uses.erase(addressEntry);
+                               setUses(address, nullptr);
                        }
                }
        }
@@ -174,12 +213,17 @@ namespace
 
                        if(!llvm::isa<Ice::InstAlloca>(alloca))
                        {
-                               return;   // Allocas are all at the top
+                               break;   // Allocas are all at the top
                        }
 
                        Ice::Operand *address = alloca.getDest();
-                       const auto &addressEntry = uses.find(address);
-                       auto &addressUses = addressEntry->second;
+
+                       if(!hasUses(address))
+                       {
+                               continue;
+                       }
+
+                       auto &addressUses = *getUses(address);
 
                        if(!addressUses.areOnlyLoadStore())
                        {
@@ -236,23 +280,26 @@ namespace
 
                                                alloca.setDeleted();
                                                store->setDeleted();
-                                               uses.erase(address);
+                                               setUses(address, nullptr);
 
-                                               auto &valueUses = uses[storeValue];
-
-                                               for(size_t i = 0; i < valueUses.size(); i++)
+                                               if(hasUses(storeValue))
                                                {
-                                                       if(valueUses[i] == store)
+                                                       auto &valueUses = *getUses(storeValue);
+
+                                                       for(size_t i = 0; i < valueUses.size(); i++)
                                                        {
-                                                               valueUses[i] = valueUses.back();
-                                                               valueUses.pop_back();
-                                                               break;
+                                                               if(valueUses[i] == store)
+                                                               {
+                                                                       valueUses[i] = valueUses.back();
+                                                                       valueUses.pop_back();
+                                                                       break;
+                                                               }
                                                        }
-                                               }
 
-                                               if(valueUses.empty())
-                                               {
-                                                       uses.erase(storeValue);
+                                                       if(valueUses.empty())
+                                                       {
+                                                               setUses(storeValue, nullptr);
+                                                       }
                                                }
 
                                                break;
@@ -266,21 +313,7 @@ namespace
        {
                Ice::CfgNode *entryBlock = function->getEntryNode();
 
-               struct LoadStoreInst
-               {
-                       LoadStoreInst(Ice::Inst* inst, bool isStore)
-                        : inst(inst),
-                          address(isStore ? storeAddress(inst) : loadAddress(inst)),
-                          isStore(isStore)
-                       {
-                       }
-
-                       Ice::Inst* inst;
-                       Ice::Operand *address;
-                       bool isStore;
-               };
-
-               std::unordered_map<Ice::CfgNode*, std::vector<LoadStoreInst> > loadStoreMap;
+               std::vector<std::vector<LoadStoreInst>* > allocatedVectors;
 
                for(Ice::Inst &alloca : entryBlock->getInsts())
                {
@@ -291,24 +324,29 @@ namespace
 
                        if(!llvm::isa<Ice::InstAlloca>(alloca))
                        {
-                               return;   // Allocas are all at the top
+                               break;   // Allocas are all at the top
                        }
 
                        Ice::Operand *address = alloca.getDest();
-                       const auto &addressEntry = uses.find(address);
-                       const auto &addressUses = addressEntry->second;
+
+                       if(!hasUses(address))
+                       {
+                               continue;
+                       }
+
+                       const auto &addressUses = *getUses(address);
 
                        if(!addressUses.areOnlyLoadStore())
                        {
                                continue;
                        }
 
-                       Ice::CfgNode *singleBasicBlock = node[addressUses.stores[0]];
+                       Ice::CfgNode *singleBasicBlock = getNode(addressUses.stores[0]);
 
                        for(size_t i = 1; i < addressUses.stores.size(); i++)
                        {
                                Ice::Inst *store = addressUses.stores[i];
-                               if(node[store] != singleBasicBlock)
+                               if(getNode(store) != singleBasicBlock)
                                {
                                        singleBasicBlock = nullptr;
                                        break;
@@ -317,9 +355,11 @@ namespace
 
                        if(singleBasicBlock)
                        {
-                               if(loadStoreMap.find(singleBasicBlock) == loadStoreMap.end())
+                               if(!hasLoadStoreInsts(singleBasicBlock))
                                {
-                                       std::vector<LoadStoreInst> &loadStoreVector = loadStoreMap[singleBasicBlock];
+                                       std::vector<LoadStoreInst>* loadStoreInstVector = new std::vector<LoadStoreInst>();
+                                       setLoadStoreInsts(singleBasicBlock, loadStoreInstVector);
+                                       allocatedVectors.push_back(loadStoreInstVector);
                                        for(Ice::Inst &inst : singleBasicBlock->getInsts())
                                        {
                                                if(inst.isDeleted())
@@ -332,7 +372,7 @@ namespace
 
                                                if(isStoreInst || isLoadInst)
                                                {
-                                                       loadStoreVector.push_back(LoadStoreInst(&inst, isStoreInst));
+                                                       loadStoreInstVector->push_back(LoadStoreInst(&inst, isStoreInst));
                                                }
                                        }
                                }
@@ -341,7 +381,7 @@ namespace
                                Ice::Operand *storeValue = nullptr;
                                bool unmatchedLoads = false;
 
-                               for (auto& loadStoreInst : loadStoreMap[singleBasicBlock])
+                               for (auto& loadStoreInst : getLoadStoreInsts(singleBasicBlock))
                                {
                                        Ice::Inst* inst = loadStoreInst.inst;
 
@@ -380,14 +420,15 @@ namespace
                                }
                        }
                }
+
+               for(auto loadStoreInstVector : allocatedVectors)
+               {
+                       delete loadStoreInstVector;
+               }
        }
 
        void Optimizer::analyzeUses(Ice::Cfg *function)
        {
-               uses.clear();
-               node.clear();
-               definition.clear();
-
                for(Ice::CfgNode *basicBlock : function->getNodes())
                {
                        for(Ice::Inst &instruction : basicBlock->getInsts())
@@ -397,8 +438,11 @@ namespace
                                        continue;
                                }
 
-                               node[&instruction] = basicBlock;
-                               definition[instruction.getDest()] = &instruction;
+                               setNode(&instruction, basicBlock);
+                               if(instruction.getDest())
+                               {
+                                       setDefinition(instruction.getDest(), &instruction);
+                               }
 
                                for(Ice::SizeT i = 0; i < instruction.getSrcSize(); i++)
                                {
@@ -414,7 +458,7 @@ namespace
                                        if(i == unique)
                                        {
                                                Ice::Operand *src = instruction.getSrc(i);
-                                               uses[src].insert(src, &instruction);
+                                               getUses(src)->insert(src, &instruction);
                                        }
                                }
                        }
@@ -430,23 +474,26 @@ namespace
                        newValue = context->getConstantUndef(oldValue->getType());
                }
 
-               for(Ice::Inst *use : uses[oldValue])
+               if(hasUses(oldValue))
                {
-                       assert(!use->isDeleted());   // Should have been removed from uses already
-
-                       for(Ice::SizeT i = 0; i < use->getSrcSize(); i++)
+                       for(Ice::Inst *use : *getUses(oldValue))
                        {
-                               if(use->getSrc(i) == oldValue)
+                               assert(!use->isDeleted());   // Should have been removed from uses already
+
+                               for(Ice::SizeT i = 0; i < use->getSrcSize(); i++)
                                {
-                                       use->replaceSource(i, newValue);
+                                       if(use->getSrc(i) == oldValue)
+                                       {
+                                               use->replaceSource(i, newValue);
+                                       }
                                }
+
+                               getUses(newValue)->insert(newValue, use);
                        }
 
-                       uses[newValue].insert(newValue, use);
+                       setUses(oldValue, nullptr);
                }
 
-               uses.erase(oldValue);
-
                deleteInstruction(instruction);
        }
 
@@ -463,21 +510,19 @@ namespace
                {
                        Ice::Operand *src = instruction->getSrc(i);
 
-                       const auto &srcEntry = uses.find(src);
-
-                       if(srcEntry != uses.end())
+                       if(hasUses(src))
                        {
-                               auto &srcUses = srcEntry->second;
+                               auto &srcUses = *getUses(src);
 
                                srcUses.erase(instruction);
 
                                if(srcUses.empty())
                                {
-                                       uses.erase(srcEntry);
+                                       setUses(src, nullptr);
 
                                        if(Ice::Variable *var = llvm::dyn_cast<Ice::Variable>(src))
                                        {
-                                               deleteInstruction(definition[var]);
+                                               deleteInstruction(getDefinition(var));
                                        }
                                }
                        }
@@ -490,17 +535,25 @@ namespace
 
                if(dest)
                {
-                       return uses[dest].empty() && !instruction->hasSideEffects();
+                       return (!hasUses(dest) || getUses(dest)->empty()) && !instruction->hasSideEffects();
                }
                else if(isStore(*instruction))
                {
                        if(Ice::Variable *address = llvm::dyn_cast<Ice::Variable>(storeAddress(instruction)))
                        {
-                               Ice::Inst *def = definition[address];
+                               Ice::Inst *def = getDefinition(address);
 
                                if(def && llvm::isa<Ice::InstAlloca>(def))
                                {
-                                       return uses[address].size() == uses[address].stores.size();   // Dead if all uses are stores
+                                       if(hasUses(address))
+                                       {
+                                               Optimizer::Uses* uses = getUses(address);
+                                               return uses->size() == uses->stores.size();   // Dead if all uses are stores
+                                       }
+                                       else
+                                       {
+                                               return true; // No uses
+                                       }
                                }
                        }
                }
@@ -654,6 +707,63 @@ namespace
                return false;
        }
 
+       Optimizer::Uses* Optimizer::getUses(Ice::Operand* operand)
+       {
+               Optimizer::Uses* uses = (Optimizer::Uses*)operand->Ice::Operand::getExternalData();
+               if(!uses)
+               {
+                       uses = new Optimizer::Uses;
+                       setUses(operand, uses);
+                       allocatedUses.push_back(uses);
+               }
+               return uses;
+       }
+
+       void Optimizer::setUses(Ice::Operand* operand, Optimizer::Uses* uses)
+       {
+               operand->Ice::Operand::setExternalData(uses);
+       }
+
+       bool Optimizer::hasUses(Ice::Operand* operand) const
+       {
+               return operand->Ice::Operand::getExternalData() != nullptr;
+       }
+
+       Ice::CfgNode* Optimizer::getNode(Ice::Inst* inst)
+       {
+               return (Ice::CfgNode*)inst->Ice::Inst::getExternalData();
+       }
+
+       void Optimizer::setNode(Ice::Inst* inst, Ice::CfgNode* node)
+       {
+               inst->Ice::Inst::setExternalData(node);
+       }
+
+       Ice::Inst* Optimizer::getDefinition(Ice::Variable* var)
+       {
+               return (Ice::Inst*)var->Ice::Variable::getExternalData();
+       }
+
+       void Optimizer::setDefinition(Ice::Variable* var, Ice::Inst* inst)
+       {
+               var->Ice::Variable::setExternalData(inst);
+       }
+
+       const std::vector<Optimizer::LoadStoreInst>& Optimizer::getLoadStoreInsts(Ice::CfgNode* node)
+       {
+               return *((const std::vector<LoadStoreInst>*)node->Ice::CfgNode::getExternalData());
+       }
+
+       void Optimizer::setLoadStoreInsts(Ice::CfgNode* node, std::vector<LoadStoreInst>* insts)
+       {
+               node->Ice::CfgNode::setExternalData(insts);
+       }
+
+       bool Optimizer::hasLoadStoreInsts(Ice::CfgNode* node) const
+       {
+               return node->Ice::CfgNode::getExternalData() != nullptr;
+       }
+
        bool Optimizer::Uses::areOnlyLoadStore() const
        {
                return size() == (loads.size() + stores.size());