From dd28abbf3abc21cf6cc62431c38b4357854e5ca9 Mon Sep 17 00:00:00 2001 From: clc5q <clc5q@git.zephyr-software.com> Date: Sat, 11 Feb 2012 18:58:14 +0000 Subject: [PATCH] Convert DU chains to use stored BaseAddr+offset to prepare for memory reduction. --- SMPBasicBlock.cpp | 24 ++++++++++++------------ SMPDataFlowAnalysis.cpp | 15 +++++++++++++-- SMPDataFlowAnalysis.h | 21 +++++++++++---------- SMPStaticAnalyzer.h | 1 + 4 files changed, 37 insertions(+), 24 deletions(-) diff --git a/SMPBasicBlock.cpp b/SMPBasicBlock.cpp index 21dd1bc5..0f93cd11 100644 --- a/SMPBasicBlock.cpp +++ b/SMPBasicBlock.cpp @@ -769,7 +769,7 @@ void SMPBasicBlock::CreateGlobalChains() { this->GlobalDUChains.ChainsByName.reserve(NumGlobals); op_t TempOp; TempOp.type = o_void; - SMPDUChainArray Temp(TempOp); + SMPDUChainArray Temp(TempOp, this->GetFirstAddr() - 1); if (DebugFlag) msg("Initializing GlobalChainsByName.\n"); this->GlobalDUChains.ChainsByName.assign(NumGlobals, Temp); @@ -778,10 +778,10 @@ void SMPBasicBlock::CreateGlobalChains() { LocalUseIndex.push_back(-1); // init LocalUse indices to -1; first DEF will make it 0 // Set the local name for each DU chain array. if (LocalIndex > this->GlobalDUChains.ChainsByName.size()) - msg("ERROR: LocalIndex %d out of bounds in CreateGlobalChains.\n", LocalIndex); + msg("FATAL ERROR: LocalIndex %d out of bounds in CreateGlobalChains.\n", LocalIndex); if (DebugFlag) msg("Setting name for LocalIndex = %d\n", LocalIndex); - this->GlobalDUChains.ChainsByName.at(LocalIndex).SetName(*NameIter); + this->GlobalDUChains.ChainsByName.at(LocalIndex).SetName(*NameIter, this->GetFirstAddr() - 1); } // Iterate through all instructions in the block. For each DEF of a global name, @@ -807,7 +807,7 @@ void SMPBasicBlock::CreateGlobalChains() { LocalUseIndex.at(NameIndex)++; LocalUseNum++; // Make the DEF be just before the start of the block - SMPDefUseChain TempDefChain(UseOp, this->GetFirstAddr() - 1); + SMPDefUseChain TempDefChain(UseOp, 0); this->GlobalDUChains.ChainsByName.at(NameIndex).PushChain(TempDefChain); #if SMP_COUNT_MEMORY_ALLOCATIONS SMPDefUseChainBytes += sizeof(TempDefChain); @@ -816,7 +816,7 @@ void SMPBasicBlock::CreateGlobalChains() { // Push address of USE instruction onto the DEF-USE chain for this LocalUseNum. this->GlobalDUChains.ChainsByName.at(NameIndex).PushUse(LocalUseNum, (*InstIter)->GetAddr()); #if SMP_COUNT_MEMORY_ALLOCATIONS - SMPDefUseChainBytes += sizeof(ea_t); + SMPDefUseChainBytes += SMP_DU_ADDR_SIZE; #endif } ++CurrUse; @@ -841,7 +841,7 @@ void SMPBasicBlock::CreateGlobalChains() { // Set up a new DU chain and push it onto the vector of DU chains for this // local name. Push the DEF onto the list (it will be the first reference // on the list, because the list was newly created). - SMPDefUseChain TempDefChain(DefOp, (*InstIter)->GetAddr()); + SMPDefUseChain TempDefChain(DefOp, (*InstIter)->GetAddr() - this->GetFirstAddr() + 1); this->GlobalDUChains.ChainsByName.at(NameIndex).PushChain(TempDefChain); #if SMP_COUNT_MEMORY_ALLOCATIONS SMPDefUseChainBytes += sizeof(TempDefChain); @@ -879,7 +879,7 @@ void SMPBasicBlock::SSALocalRenumber(void) { this->LocalDUChains.ChainsByName.reserve(NumLocals); op_t TempOp; TempOp.type = o_void; - SMPDUChainArray Temp(TempOp); + SMPDUChainArray Temp(TempOp, this->GetFirstAddr() - 1); if (DebugFlag) msg("Initializing ChainsByName.\n"); this->LocalDUChains.ChainsByName.assign(NumLocals, Temp); @@ -888,10 +888,10 @@ void SMPBasicBlock::SSALocalRenumber(void) { SSAIndex.push_back(-1); // init SSA indices to -1; first DEF will make it 0 // Set the local name for each DU chain array. if (LocalIndex > this->LocalDUChains.ChainsByName.size()) - msg("ERROR: LocalIndex %d out of bounds in SSALocalRenumber.\n", LocalIndex); + msg("FATAL ERROR: LocalIndex %d out of bounds in SSALocalRenumber.\n", LocalIndex); if (DebugFlag) msg("Setting name for LocalIndex = %d\n", LocalIndex); - this->LocalDUChains.ChainsByName.at(LocalIndex).SetName(*NameIter); + this->LocalDUChains.ChainsByName.at(LocalIndex).SetName(*NameIter, this->GetFirstAddr() - 1); } // Iterate through all instructions in the block. For each DEF of a local name, @@ -910,7 +910,7 @@ void SMPBasicBlock::SSALocalRenumber(void) { // Found USE of a local name. NameIndex = ExtractGlobalIndex(*UseNameIter); if (NameIndex > SSAIndex.size()) - msg("ERROR: NameIndex %d out of range in SSALocalRenumber.\n", NameIndex); + msg("FATAL ERROR: NameIndex %d out of range in SSALocalRenumber.\n", NameIndex); int SSANum = SSAIndex.at(NameIndex); // Update the SSA subscript in the DEF-USE list for the instruction. #if SMP_DEBUG_DATAFLOW @@ -924,7 +924,7 @@ void SMPBasicBlock::SSALocalRenumber(void) { CurrUse = (*InstIter)->SetUseSSA(UseOp, SSANum); if (SSANum >= 0) { // skip USE before DEF names if ((size_t) SSANum > this->LocalDUChains.ChainsByName.at(NameIndex).GetSize()) - msg("ERROR: SSANum %d out of range in SSALocalRenumber.\n", SSANum); + msg("FATAL ERROR: SSANum %d out of range in SSALocalRenumber.\n", SSANum); // Push address of USE instruction onto the DEF-USE chain for this SSANum. this->LocalDUChains.ChainsByName.at(NameIndex).PushUse(SSANum, (*InstIter)->GetAddr()); } @@ -951,7 +951,7 @@ void SMPBasicBlock::SSALocalRenumber(void) { // Set up a new DU chain and push it onto the vector of DU chains for this // local name. Push the DEF onto the list (it will be the first reference // on the list, because the list was newly created). - SMPDefUseChain TempDefChain(DefOp, (*InstIter)->GetAddr()); + SMPDefUseChain TempDefChain(DefOp, (*InstIter)->GetAddr() - this->GetFirstAddr() + 1); this->LocalDUChains.ChainsByName.at(NameIndex).PushChain(TempDefChain); } ++CurrDef; diff --git a/SMPDataFlowAnalysis.cpp b/SMPDataFlowAnalysis.cpp index c398f91f..5cd46104 100644 --- a/SMPDataFlowAnalysis.cpp +++ b/SMPDataFlowAnalysis.cpp @@ -1294,24 +1294,35 @@ SMPDUChainArray::SMPDUChainArray(void) { return; } -SMPDUChainArray::SMPDUChainArray(op_t Name) { +SMPDUChainArray::SMPDUChainArray(op_t Name, ea_t FirstAddrMinusOne) { if (o_reg == Name.type) { // We want to map AH, AL, and AX to EAX, etc. throughout our data flow analysis // and type inference systems. Name.reg = MDCanonicalizeSubReg(Name.reg); } this->SSAName = Name; + this->BaseAddr = FirstAddrMinusOne; this->DUChains.clear(); return; } -void SMPDUChainArray::SetName(op_t Name) { +ea_t SMPDUChainArray::GetLastUse(int SSANum) const { + ea_t TempAddr = DUChains.at(SSANum).GetLastUse(); + if (BADADDR != TempAddr) { + // If BADADDR, leave it as BADADDR. Otherwise, add in BaseAddr. + TempAddr += this->BaseAddr; + } + return TempAddr; +} + +void SMPDUChainArray::SetName(op_t Name, ea_t FirstAddrMinusOne) { if (o_reg == Name.type) { // We want to map AH, AL, and AX to EAX, etc. throughout our data flow analysis // and type inference systems. Name.reg = MDCanonicalizeSubReg(Name.reg); } this->SSAName = Name; + this->BaseAddr = FirstAddrMinusOne; return; } diff --git a/SMPDataFlowAnalysis.h b/SMPDataFlowAnalysis.h index c4ce7a43..91906e9d 100644 --- a/SMPDataFlowAnalysis.h +++ b/SMPDataFlowAnalysis.h @@ -509,13 +509,13 @@ public: SMPDefUseChain(op_t Name, ea_t Def = BADADDR); // Get methods inline op_t GetName(void) const { return SSAName; }; - inline ea_t GetDef(void) const { return RefInstrs.at(0); }; - inline ea_t GetUse(size_t index) const { return RefInstrs.at(index + 1); }; + inline ea_t GetDef(void) const { return (ea_t) RefInstrs.at(0); }; + inline ea_t GetUse(size_t index) const { return (ea_t) RefInstrs.at(index + 1); }; inline ea_t GetLastUse(void) const { if (RefInstrs.size() > 1) - return RefInstrs.back(); + return (ea_t) RefInstrs.back(); else - return BADADDR; + return (ea_t) BADADDR; }; inline size_t GetNumUses(void) const { return RefInstrs.size() - 1; }; @@ -531,24 +531,24 @@ public: void Dump(int SSANum = (-1)); private: op_t SSAName; // What variable is defined and used in the chain? - vector<ea_t> RefInstrs; // First is always DEF, rest are USE. bool IndWrite; // Does an indirect memory write occur within the chain? + vector<ea_t> RefInstrs; // First is always DEF, rest are USE. }; // end class SMPDefUseChain class SMPDUChainArray { public: // Constructor SMPDUChainArray(void); - SMPDUChainArray(op_t Name); + SMPDUChainArray(op_t Name, ea_t FirstAddrMinusOne); // Get methods. inline op_t GetName(void) const { return SSAName; }; - inline ea_t GetDef(int SSANum) const { return DUChains.at(SSANum).GetDef(); }; - inline ea_t GetLastUse(int SSANum) const { return DUChains.at(SSANum).GetLastUse(); }; + inline ea_t GetDef(int SSANum) const { return BaseAddr + DUChains.at(SSANum).GetDef(); }; + ea_t GetLastUse(int SSANum) const; inline size_t GetNumUses(int SSANum) const { return DUChains.at(SSANum).GetNumUses(); }; inline size_t GetSize(void) const { return DUChains.size(); }; // Set methods. - void SetName(op_t Name); - inline void PushUse(int SSANum, ea_t UseAddr) { DUChains.at(SSANum).PushUse(UseAddr); }; + void SetName(op_t Name, ea_t FirstAddrMinusOne); + inline void PushUse(int SSANum, ea_t UseAddr) { DUChains.at(SSANum).PushUse(UseAddr - BaseAddr); }; inline void PushChain(SMPDefUseChain NewChain) { DUChains.push_back(NewChain); }; inline void SetIndWrite(int SSANum, bool NewFlag) { DUChains.at(SSANum).SetIndWrite(NewFlag); }; // Query methods @@ -559,6 +559,7 @@ private: // Data vector<SMPDefUseChain> DUChains; // indexed by SSA number for local chains op_t SSAName; // What variable is used in all chains in the array? + ea_t BaseAddr; // one less than first addr in basic block }; // end class SMPDUChainArray class SMPCompleteDUChains { diff --git a/SMPStaticAnalyzer.h b/SMPStaticAnalyzer.h index b7ec2917..a7ed7978 100644 --- a/SMPStaticAnalyzer.h +++ b/SMPStaticAnalyzer.h @@ -60,6 +60,7 @@ extern unsigned long SMPGlobalVarCount; extern unsigned long SMPLocalVarCount; extern unsigned long SMPInstBytes; extern unsigned long SMPDefUseChainBytes; +#define SMP_DU_ADDR_SIZE sizeof(ea_t) #endif extern const char *DataTypes[]; -- GitLab