Newer
Older
}
void DefOrUse::SetMetadataStatus(SMPMetadataType NewStatus) {
// See if we are just updating explanation codes.
bool OldUsed = ((this->MetadataStatus >= DEF_METADATA_USED) && (this->MetadataStatus < DEF_METADATA_REDUNDANT));
if (OldUsed) {
bool NewUsed = ((NewStatus >= DEF_METADATA_USED) && (NewStatus < DEF_METADATA_REDUNDANT));
if (NewUsed) {
// Union the explanation codes.
int TempInt = (int) this->GetMetadataStatus();
TempInt |= (int) NewStatus;
this->MetadataStatus = (SMPMetadataType) TempInt;
return;
}
}
this->MetadataStatus = NewStatus;
return;
}
// Debug printing.
void DefOrUse::Dump(void) const {
PrintListOperand(this->Operand, this->SSANumber);
if (IsEqType(this->OpType , NUMERIC))
clc5q
committed
SMP_msg("N ");
else if (IsEqType(this->OpType , CODEPTR))
clc5q
committed
SMP_msg("C ");
else if (IsEqType(this->OpType , POINTER))
clc5q
committed
SMP_msg("P ");
else if (IsEqType(this->OpType , STACKPTR))
clc5q
committed
SMP_msg("S ");
else if (IsEqType(this->OpType , GLOBALPTR))
clc5q
committed
SMP_msg("G ");
else if (IsEqType(this->OpType , HEAPPTR))
clc5q
committed
SMP_msg("H ");
else if (IsEqType(this->OpType , PTROFFSET))
clc5q
committed
SMP_msg("O ");
clc5q
committed
else if (IsEqType(this->OpType , NEGATEDPTR))
clc5q
committed
SMP_msg("NegP ");
else if (IsEqType(this->OpType , UNKNOWN))
clc5q
committed
SMP_msg("U ");
/* emit the profile bit */
clc5q
committed
SMP_msg("Pr ");
// Don't write anything for UNINIT OpType
// Emit the metadata status.
if (DEF_METADATA_UNUSED == this->MetadataStatus)
clc5q
committed
SMP_msg("Mn ");
else if (DEF_METADATA_USED == this->MetadataStatus)
clc5q
committed
SMP_msg("Mu ");
else if (DEF_METADATA_REDUNDANT == this->MetadataStatus)
clc5q
committed
SMP_msg("Mr ");
// Is the DEF possibly aliased because of an indirect write in
// the DEF-USE chain?
clc5q
committed
SMP_msg("Al* ");
return;
} // end of DefOrUse::Dump()
// *****************************************************************
// Class DefOrUseSet
// *****************************************************************
// Default constructor.
DefOrUseSet::DefOrUseSet(void) {
this->Refs.clear();
clc5q
committed
// Destructor.
DefOrUseSet::~DefOrUseSet() {
this->Refs.clear();
return;
clc5q
committed
}
// Find the reference for a given operand type.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::FindRef(const STARSOpndTypePtr &SearchOp) {
set<DefOrUse, LessDefUse>::iterator CurrRef;
DefOrUse DummyRef(SearchOp);
CurrRef = this->Refs.find(DummyRef);
return CurrRef;
}
// Insert a new DEF or USE; must be new, insert must succeed else we assert.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::InsertRef(DefOrUse Ref) {
pair<set<DefOrUse, LessDefUse>::iterator, bool> InsertResult;
InsertResult = this->Refs.insert(Ref);
assert(InsertResult.second);
return InsertResult.first;
}
// Set a Def or Use into the list, along with its type.
void DefOrUseSet::SetRef(STARSOpndTypePtr Ref, SMPOperandType Type, int SSASub) {
pair<set<DefOrUse, LessDefUse>::iterator, bool> InsertResult;
DefOrUse CurrRef(Ref, Type, SSASub);
InsertResult = this->Refs.insert(CurrRef);
if ((!(InsertResult.second)) && (! Ref->IsRegOp())) {
SMP_msg("WARNING: Inserted duplicate DEF or USE: ");
// Change the indirect write status for a reference.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::SetOp(set<DefOrUse, LessDefUse>::iterator CurrRef, STARSOpndTypePtr NewOp) {
// To change a field within a set, we must grab a copy, change the copy,
// delete the old set member, and insert the updated copy as a new member.
assert(CurrRef != this->Refs.end());
DefOrUse NewCopy = (*CurrRef);
NewCopy.SetOp(NewOp);
this->Refs.erase(CurrRef);
pair<set<DefOrUse, LessDefUse>::iterator, bool> InsertResult;
InsertResult = this->Refs.insert(NewCopy);
assert(InsertResult.second);
return InsertResult.first;
} // end of DefOrUseSet::SetOp()
// Change the SSA subscript for a reference.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::SetSSANum(const STARSOpndTypePtr &CurrOp, int NewSSASub) {
// To change a field within a set, we must grab a copy, change the copy,
// delete the old set member, and insert the updated copy as a new member.
set<DefOrUse, LessDefUse>::iterator CurrRef = this->FindRef(CurrOp);
assert(CurrRef != this->Refs.end());
set<DefOrUse, LessDefUse>::iterator NextRef = CurrRef;
++NextRef;
DefOrUse NewCopy = (*CurrRef);
NewCopy.SetSSANum(NewSSASub);
this->Refs.erase(CurrRef);
CurrRef = this->Refs.insert(NextRef, NewCopy);
return CurrRef;
} // end of DefOrUseSet::SetSSANum()
// Change the operand type for a reference.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::SetType(const STARSOpndTypePtr &CurrOp, SMPOperandType Type, const SMPInstr* Instr) {
// To change a field within a set, we must grab a copy, change the copy,
// delete the old set member, and insert the updated copy as a new member.
set<DefOrUse, LessDefUse>::iterator CurrRef = this->FindRef(CurrOp);
assert(CurrRef != this->Refs.end());
if (CurrOp->IsImmedOp()) {
if (UNINIT != CurrRef->GetType() && Type != CurrRef->GetType()) {
SMP_msg("ERROR: Changing type of immediate from %d to %d at %llx: ", CurrRef->GetType(), Type, (uint64_t) Instr->GetAddr());
clc5q
committed
SMP_msg("\n");
DefOrUse NewCopy = (*CurrRef);
NewCopy.SetType(Type,Instr);
this->Refs.erase(CurrRef);
pair<set<DefOrUse, LessDefUse>::iterator, bool> InsertResult;
InsertResult = this->Refs.insert(NewCopy);
assert(InsertResult.second);
CurrRef = InsertResult.first;
} // end of DefOrUseSet::SetType()
// Change the Metadata type for a reference.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::SetMetadata(const STARSOpndTypePtr &CurrOp, SMPMetadataType Status) {
// To change a field within a set, we must grab a copy, change the copy,
// delete the old set member, and insert the updated copy as a new member.
set<DefOrUse, LessDefUse>::iterator CurrRef = this->FindRef(CurrOp);
assert(CurrRef != this->Refs.end());
DefOrUse NewCopy = (*CurrRef);
NewCopy.SetMetadataStatus(Status);
this->Refs.erase(CurrRef);
pair<set<DefOrUse, LessDefUse>::iterator, bool> InsertResult;
InsertResult = this->Refs.insert(NewCopy);
assert(InsertResult.second);
CurrRef = InsertResult.first;
return CurrRef;
} // end of DefOrUseSet::SetMetadata()
// Change the indirect write status for a reference.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::SetIndWrite(const STARSOpndTypePtr &CurrOp, bool IndWriteFlag) {
// To change a field within a set, we must grab a copy, change the copy,
// delete the old set member, and insert the updated copy as a new member.
set<DefOrUse, LessDefUse>::iterator CurrRef = this->FindRef(CurrOp);
assert(CurrRef != this->Refs.end());
DefOrUse NewCopy = (*CurrRef);
NewCopy.SetIndWrite(IndWriteFlag);
this->Refs.erase(CurrRef);
pair<set<DefOrUse, LessDefUse>::iterator, bool> InsertResult;
InsertResult = this->Refs.insert(NewCopy);
assert(InsertResult.second);
CurrRef = InsertResult.first;
return CurrRef;
} // end of DefOrUseSet::SetIndWrite()
// Change the ignore apparent truncation flag for a reference.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::SetNoTruncation(const STARSOpndTypePtr &CurrOp, bool NoTruncFlag) {
// To change a field within a set, we must grab a copy, change the copy,
// delete the old set member, and insert the updated copy as a new member.
set<DefOrUse, LessDefUse>::iterator CurrRef = this->FindRef(CurrOp);
assert(CurrRef != this->Refs.end());
DefOrUse NewCopy = (*CurrRef);
NewCopy.SetNoTruncation(NoTruncFlag);
this->Refs.erase(CurrRef);
pair<set<DefOrUse, LessDefUse>::iterator, bool> InsertResult;
InsertResult = this->Refs.insert(NewCopy);
assert(InsertResult.second);
CurrRef = InsertResult.first;
return CurrRef;
} // end of DefOrUseSet::SetNoTruncation()
clc5q
committed
// Change the ignore apparent overflow flag for a reference.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::SetNoOverflow(const STARSOpndTypePtr &CurrOp, bool NoOverflowFlag) {
clc5q
committed
// To change a field within a set, we must grab a copy, change the copy,
// delete the old set member, and insert the updated copy as a new member.
set<DefOrUse, LessDefUse>::iterator CurrRef = this->FindRef(CurrOp);
assert(CurrRef != this->Refs.end());
DefOrUse NewCopy = (*CurrRef);
NewCopy.SetNoOverflow(NoOverflowFlag);
this->Refs.erase(CurrRef);
pair<set<DefOrUse, LessDefUse>::iterator, bool> InsertResult;
InsertResult = this->Refs.insert(NewCopy);
assert(InsertResult.second);
CurrRef = InsertResult.first;
return CurrRef;
} // end of DefOrUseSet::SetNoOverflow()
// Set a DEF as being invariant for all loops in the func.
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::SetLoopInvariant(const STARSOpndTypePtr &CurrOp) {
// To change a field within a set, we must grab a copy, change the copy,
// delete the old set member, and insert the updated copy as a new member.
set<DefOrUse, LessDefUse>::iterator CurrRef = this->FindRef(CurrOp);
assert(CurrRef != this->Refs.end());
DefOrUse NewCopy = (*CurrRef);
NewCopy.SetInvariantForAllLoops();
this->Refs.erase(CurrRef);
pair<set<DefOrUse, LessDefUse>::iterator, bool> InsertResult;
InsertResult = this->Refs.insert(NewCopy);
assert(InsertResult.second);
CurrRef = InsertResult.first;
return CurrRef;
} // end of DefOrUseSet::SetLoopInvariant()
// Set a DEF as being a safe memory write
set<DefOrUse, LessDefUse>::iterator DefOrUseSet::SetSafeMemWrite(const STARSOpndTypePtr &CurrOp) {
// To change a field within a set, we must grab a copy, change the copy,
// delete the old set member, and insert the updated copy as a new member.
set<DefOrUse, LessDefUse>::iterator CurrRef = this->FindRef(CurrOp);
assert(CurrRef != this->Refs.end());
DefOrUse NewCopy = (*CurrRef);
NewCopy.SetSafeMemWriteDef();
this->Refs.erase(CurrRef);
pair<set<DefOrUse, LessDefUse>::iterator, bool> InsertResult;
InsertResult = this->Refs.insert(NewCopy);
assert(InsertResult.second);
CurrRef = InsertResult.first;
return CurrRef;
} // end of DefOrUseSet::SetSafeMemWrite()
clc5q
committed
void DefOrUseSet::Dump(void) const {
set<DefOrUse, LessDefUse>::iterator CurrRef;
for (CurrRef = this->Refs.begin(); CurrRef != this->Refs.end(); ++CurrRef) {
CurrRef->Dump();
}
clc5q
committed
SMP_msg("\n");
// Do all types agree, ignoring any flags registers in the set? This is used
// for conditional move instructions; if all types agree, it does not matter
// whether the move happens or not.
clc5q
committed
bool DefOrUseSet::TypesAgreeNoFlags(void) {
bool FoundFirstUse = false;
set<DefOrUse, LessDefUse>::iterator CurrUse;
SMPOperandType UseType = UNINIT;
for (CurrUse = this->Refs.begin(); CurrUse != this->Refs.end(); ++CurrUse) {
if (!(CurrUse->GetOp()->MatchesReg(X86_FLAGS_REG))) { // ignore flags
clc5q
committed
if (!FoundFirstUse) {
FoundFirstUse = true;
UseType = CurrUse->GetType();
}
else {
clc5q
committed
if (IsNotEqType(CurrUse->GetType(), UseType)) {
clc5q
committed
return false; // inconsistent types
}
}
}
}
return true;
} // end of DefOrUseSet::TypesAgreeNoFlags()
// *****************************************************************
// Class DefOrUseList
// *****************************************************************
// Default constructor.
DefOrUseList::DefOrUseList(void) {
this->Refs.clear();
return;
}
// Set a Def or Use into the list, along with its type.
void DefOrUseList::SetRef(STARSOpndTypePtr Ref, SMPOperandType Type, int SSASub) {
DefOrUse CurrRef(Ref, Type, SSASub);
this->Refs.push_back(CurrRef);
DefOrUse DefOrUseList::GetRef(size_t index) const {
// Change the SSA subscript for a reference.
void DefOrUseList::SetSSANum(size_t index, int NewSSASub) {
this->Refs[index].SetSSANum(NewSSASub);
return;
}
// Change the operand type for a reference.
void DefOrUseList::SetType(size_t index, SMPOperandType Type, const SMPInstr* Instr) {
this->Refs[index].SetType(Type,Instr);
return;
}
// Debug printing.
void DefOrUseList::Dump(void) const {
for (size_t index = 0; index < this->Refs.size(); ++index) {
Refs[index].Dump();
}
clc5q
committed
SMP_msg("\n");
return;
}
// Erase duplicate entries, in case SMPInstr::MDFixupDefUseLists() adds one.
void DefOrUseList::EraseDuplicates(void) {
set<STARSOpndTypePtr, LessOp> TempRefs; // Use STL set to find duplicates
set<STARSOpndTypePtr, LessOp>::iterator TempIter;
vector<DefOrUse>::iterator RefIter;
RefIter = this->Refs.begin();
while (RefIter != this->Refs.end()) {
TempIter = TempRefs.find(RefIter->GetOp());
if (TempIter == TempRefs.end()) { // not already in set
TempRefs.insert(RefIter->GetOp());
++RefIter;
}
else { // found it in set already
RefIter = this->Refs.erase(RefIter);
}
}
return;
} // end of DefOrUseList::EraseDuplicates()
// *****************************************************************
// Class SMPPhiFunction
// *****************************************************************
// Constructor
SMPPhiFunction::SMPPhiFunction(int GlobIndex, const DefOrUse &Def) {
this->DefName = Def;
clc5q
committed
this->SubscriptedOps.clear();
DefOrUse SMPPhiFunction::GetDefCopy(void) const {
DefOrUse DefCopy(this->DefName);
return DefCopy;
}
// Add a phi item to the list
void SMPPhiFunction::PushBack(DefOrUse Ref) {
this->SubscriptedOps.SetRef(Ref.GetOp(), Ref.GetType(), Ref.GetSSANum());
return;
}
// Set the SSA number of the defined variable.
void SMPPhiFunction::SetSSADef(int NewSSASub) {
this->DefName.SetSSANum(NewSSASub);
return;
}
// Set the SSA number of the input variable.
void SMPPhiFunction::SetSSARef(size_t index, int NewSSASub) {
this->SubscriptedOps.SetSSANum(index, NewSSASub);
return;
}
// Set the type of the defined variable.
void SMPPhiFunction::SetDefType(SMPOperandType Type, const SMPInstr* Instr) {
this->DefName.SetType(Type, Instr);
return;
}
// Set the type of the input variable.
void SMPPhiFunction::SetRefType(size_t index, SMPOperandType Type, const SMPInstr* Instr) {
this->SubscriptedOps.SetType(index, Type, Instr);
// Set the metadata status of the DEF variable.
void SMPPhiFunction::SetDefMetadata(SMPMetadataType Status) {
this->DefName.SetMetadataStatus(Status);
return;
} // end of SMPPhiFunction::SetDefMetadata()
// Does at least one USE have a type other than UNINIT?
bool SMPPhiFunction::HasTypedUses(void) {
size_t index;
for (index = 0; index < this->GetPhiListSize(); ++index) {
if (UNINIT != this->GetUseType(index))
return true;
}
return false;
} // end of SMPPhiFunction::HasTypedUses()
// Return the result of applying the conditional type propagation meet operator
// over all the USE types.
SMPOperandType SMPPhiFunction::ConditionalMeetType(SMPBasicBlock *CurrBlock) const {
SMPOperandType MeetType;
SMPOperandType PtrType = UNINIT;
SMPOperandType NumericType = UNINIT; // can end up NUMERIC or CODEPTR
bool FoundUNINIT = false; // any USE type UNINIT?
bool FoundNUMERIC = false; // any USE type NUMERIC?
bool FoundZero = false; // was DEF to zero? (could be POINTER or NUMERIC
bool FoundPOINTER = false; // includes all POINTER subtypes
bool FoundUNKNOWN = false; // any USE type UNKNOWN?
clc5q
committed
bool FoundPTROFFSET = false; // any USE type PTROFFSET?
bool FoundNEGATEDPTR = false; // any USE type NEGATEDPTR?
bool ProfilerDerived = false; // was any USE type Profiler-derived?
list<size_t> ZeroConstIndices;
STARS_ea_t BlockStartAddr = CurrBlock->GetFirstAddr(); // for debugging
STARSOpndTypePtr PhiOp = this->GetAnyOp();
for (size_t index = 0; index < this->GetPhiListSize(); ++index) {
SMPOperandType UseType = this->GetUseType(index);
if (IsEqType(UseType, UNINIT))
FoundUNINIT = true;
else if (IsNumeric(UseType)) {
// Check for possibility that we aggressively declared NUMERIC when register was set to zero.
int UseSSANum = this->GetUseSSANum(index);
bool CurrentUseZeroCase = false;
if (MDIsDataFlowOpnd(PhiOp, false)) {
STARS_ea_t DefAddr = CurrBlock->GetFunc()->GetGlobalDefAddr(PhiOp, UseSSANum);
// Handle simple case: DEF is in an instruction.
if ((STARS_BADADDR != DefAddr) && (DefAddr < STARS_PSEUDO_ID_MIN)) {
SMPInstr *DefInst = CurrBlock->GetFunc()->GetInstFromAddr(DefAddr);
CurrentUseZeroCase = DefInst->IsSetToZero();
}
}
if (CurrentUseZeroCase) {
FoundZero = true;
ZeroConstIndices.push_back(index);
FoundNUMERIC = true;
if (IsEqType(NumericType, CODEPTR)) {
// Already refined. If current type agrees, leave it
// alone, else revert to generic type NUMERIC.
if (IsNotEqType(UseType, NumericType))
NumericType = NUMERIC;
}
else {
// Have not yet refined NumericType; might still be UNINIT.
if (IsEqType(UNINIT, NumericType))
NumericType = UseType;
else { // NumericType is NUMERIC; leave it as NUMERIC.
assert(IsEqType(NUMERIC, NumericType));
}
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
}
}
}
else if (IsDataPtr(UseType)) {
FoundPOINTER = true;
// Perform a meet over the pointer types.
if (IsRefinedDataPtr(PtrType)) {
// Already refined. If current type agrees, leave it
// alone, else revert to generic type POINTER.
if (IsNotEqType(UseType, PtrType))
PtrType = POINTER;
}
else {
// Have not yet refined PtrType; might still be UNINIT.
if (IsEqType(UNINIT, PtrType))
PtrType = UseType;
else { // PtrType is POINTER because we saw POINTER or
// had a conflict between pointer refinements; leave
// it as POINTER.
assert(IsEqType(POINTER, PtrType));
}
}
}
clc5q
committed
else if (IsEqType(PTROFFSET, UseType))
FoundPTROFFSET = true;
else if (IsEqType(NEGATEDPTR, UseType))
FoundNEGATEDPTR = true;
else if (IsUnknown(UseType))
FoundUNKNOWN = true;
if (IsProfDerived(UseType))
ProfilerDerived = true;
}
// Use the boolean flags to compute the meet function.
clc5q
committed
if (FoundUNKNOWN || (FoundNUMERIC && FoundPOINTER)
|| ((FoundNUMERIC || FoundPOINTER || FoundNEGATEDPTR) && FoundPTROFFSET)
|| ((FoundNUMERIC || FoundPOINTER || FoundPTROFFSET) && FoundNEGATEDPTR))
MeetType = UNKNOWN;
else if (FoundNUMERIC)
MeetType = NumericType;
else if (FoundPOINTER) {
MeetType = PtrType;
if (FoundZero) { // mixture of POINTER and const zero DEFs, i.e. ptr := NULL;
// Undo the aggressive NUMERIC inference when registers are set to zero.
// NOTE: There cannot be any alterations to the reg between the zero DEF and
// the current block on at least one path, or it would not show up in the Phi function with the
// current SSA number.
do {
size_t ZeroConstIndex = ZeroConstIndices.front();
int UseSSANum = this->GetUseSSANum(ZeroConstIndex);
STARS_ea_t DefAddr = CurrBlock->GetFunc()->GetGlobalDefAddr(PhiOp, UseSSANum);
// Handle simple case: DEF is in an instruction.
if ((STARS_BADADDR != DefAddr) && (DefAddr < STARS_PSEUDO_ID_MIN)) {
SMPInstr *DefInst = CurrBlock->GetFunc()->GetInstFromAddr(DefAddr);
set<DefOrUse, LessDefUse>::iterator DefIter = DefInst->SetDefType(PhiOp, PtrType);
SMP_msg("INFO: Converting zeroed reg from NUMERIC to POINTER at %lx for Block at %lx\n",
(unsigned long) DefAddr, (unsigned long) BlockStartAddr);
CurrBlock->GetFunc()->ResetProcessedBlocks();
SMPBasicBlock *DefBlock = CurrBlock->GetFunc()->GetBlockFromInstAddr(DefAddr);
#if 0 // Causes infinite loops, crashes; need to debug !!!!****!!!!
DefBlock->PropagateGlobalDefType(PhiOp, PtrType, UseSSANum, false, true);
#else
DefBlock->PropagateGlobalDefType(PhiOp, PtrType, UseSSANum, false, false);
#endif
}
ZeroConstIndices.pop_front();
} while (!ZeroConstIndices.empty());
}
}
clc5q
committed
else if (FoundPTROFFSET)
MeetType = PTROFFSET;
else if (FoundNEGATEDPTR)
MeetType = NEGATEDPTR;
else if (FoundZero && (!FoundUNINIT)) // nothing but zeroes
MeetType = NUMERIC;
else {
assert(FoundUNINIT);
MeetType = UNINIT;
}
if (ProfilerDerived)
MeetType = MakeProfDerived(MeetType);
return MeetType;
} // end of SMPPhiFunction::ConditionalMeetType()
// Debug printing.
void SMPPhiFunction::Dump(void) const {
clc5q
committed
SMP_msg(" DEF: ");
this->DefName.Dump();
clc5q
committed
SMP_msg(" USEs: ");
this->SubscriptedOps.Dump();
return;
}
// *****************************************************************
// Class SMPDefUseChain
// *****************************************************************
// Constructors
SMPDefUseChain::SMPDefUseChain(void) {
this->SSAName = nullptr;
clc5q
committed
this->RefInstrs.clear();
this->RefInstrs.push_back((unsigned short) STARS_BADADDR);
this->IndWrite = false;
return;
}
SMPDefUseChain::SMPDefUseChain(STARSOpndTypePtr Name, STARS_ea_t Def) {
this->SetName(Name);
this->RefInstrs.push_back(Def);
this->IndWrite = false;
return;
}
// Set the variable name.
void SMPDefUseChain::SetName(STARSOpndTypePtr Name) {
STARSOpndTypePtr Name2 = nullptr;
if (Name->IsRegOp()) {
// We want to map AH, AL, and AX to EAX, etc. throughout our data flow analysis
// and type inference systems.
Name2 = CloneIfSubwordReg(Name);
CanonicalizeOpnd(Name2);
}
else {
Name2 = Name;
this->SSAName = Name2;
return;
}
// Set the DEF instruction.
void SMPDefUseChain::SetDef(STARS_ea_t Def) {
this->RefInstrs[0] = (unsigned short) Def;
return;
}
// Push a USE onto the list
void SMPDefUseChain::PushUse(STARS_ea_t Use) {
this->RefInstrs.push_back((unsigned short) Use);
return;
}
// Set the indirect memory write flag.
void SMPDefUseChain::SetIndWrite(bool IndMemWrite) {
this->IndWrite = IndMemWrite;
return;
}
// DEBUG dump.
clc5q
committed
void SMPDefUseChain::Dump(int SSANum) const {
clc5q
committed
SMP_msg("DEF-USE chain for: ");
PrintListOperand(this->SSAName, SSANum);
if (this->RefInstrs.size() < 1) {
clc5q
committed
SMP_msg(" no references.\n");
return;
}
clc5q
committed
SMP_msg("\n DEF: %x USEs: ", this->RefInstrs.at(0));
size_t index;
for (index = 1; index < this->RefInstrs.size(); ++index)
clc5q
committed
SMP_msg("%x ", this->RefInstrs.at(index));
SMP_msg("\n");
return;
} // end of SMPDefUseChain::Dump()
// *****************************************************************
// Class SMPDUChainArray
// *****************************************************************
SMPDUChainArray::SMPDUChainArray(void) {
this->SSAName = nullptr;
this->DUChains.clear();
return;
}
SMPDUChainArray::SMPDUChainArray(STARSOpndTypePtr Name, STARS_ea_t FirstAddrMinusOne) {
STARSOpndTypePtr Name2 = nullptr;
if (Name->IsRegOp()) {
// We want to map AH, AL, and AX to EAX, etc. throughout our data flow analysis
// and type inference systems.
Name2 = CloneIfSubwordReg(Name);
CanonicalizeOpnd(Name2);
else {
Name2 = Name;
}
this->SSAName = Name2;
this->BaseAddr = FirstAddrMinusOne;
this->DUChains.clear();
return;
}
STARS_ea_t SMPDUChainArray::GetLastUse(int SSANum) const {
STARS_ea_t TempAddr = DUChains.at(SSANum).GetLastUse();
if (STARS_BADADDR != TempAddr) {
// If STARS_BADADDR, leave it as STARS_BADADDR. Otherwise, add in BaseAddr.
TempAddr += this->BaseAddr;
}
return TempAddr;
}
void SMPDUChainArray::SetName(STARSOpndTypePtr Name, STARS_ea_t FirstAddrMinusOne) {
STARSOpndTypePtr Name2 = nullptr;
if (Name->IsRegOp()) {
// We want to map AH, AL, and AX to EAX, etc. throughout our data flow analysis
// and type inference systems.
Name2 = CloneIfSubwordReg(Name);
CanonicalizeOpnd(Name2);
}
else {
Name2 = Name;
this->SSAName = Name2;
this->BaseAddr = FirstAddrMinusOne;
return;
}
// DEBUG dump.
clc5q
committed
void SMPDUChainArray::Dump(void) const {
size_t index;
for (index = 0; index < this->GetSize(); ++index) {
this->DUChains.at(index).Dump((int) index);
}
return;
}
// *****************************************************************
// Class SMPCompleteDUChains
// *****************************************************************
// DEBUG dump.
clc5q
committed
void SMPCompleteDUChains::Dump(void) const {
size_t index;
for (index = 0; index < this->ChainsByName.size(); ++index) {
this->ChainsByName.at(index).Dump();
}
return;
} // end of SMPCompleteDUChains::Dump()
// *****************************************************************
// Class STARSBitSet
// *****************************************************************
// Constructors.
STARSBitSet::STARSBitSet() {
this->BitLimit = 0;
}
// Get methods
bool STARSBitSet::GetBit(size_t BitIndex) const {
size_t ByteIndex = BitIndex / 8;
size_t BitNumber = BitIndex % 8;
clc5q
committed
assert(BitIndex <= this->BitLimit);
return (0 != (this->STARSBits.at(ByteIndex) & STARSBitMasks[BitNumber]));
}
// Set methods
void STARSBitSet::AllocateBits(size_t Size) {
size_t Bytes = Size / 8;
size_t ExtraBits = Size % 8;
clc5q
committed
this->BitLimit = Size;
if (0 != ExtraBits) {
}
else {
}
for (Bytes = 0; Bytes < this->STARSBits.size(); ++Bytes) {
this->STARSBits[Bytes] = 0;
}
}
void STARSBitSet::SetBit(size_t BitIndex) {
size_t ByteIndex = BitIndex / 8;
size_t BitNumber = BitIndex % 8;
clc5q
committed
assert(BitIndex <= this->BitLimit);
this->STARSBits[ByteIndex] |= STARSBitMasks[BitNumber];
return;
}
void STARSBitSet::ResetBit(size_t BitIndex) {
size_t ByteIndex = BitIndex / 8;
size_t BitNumber = BitIndex % 8;
clc5q
committed
assert(BitIndex <= this->BitLimit);
this->STARSBits[ByteIndex] &= (~STARSBitMasks[BitNumber]);
return;
}
// Query methods
// Returns false if all bits are zero, true otherwise.
bool STARSBitSet::IsAnyBitSet(void) const {
bool FoundSetBit = false;
size_t ByteIndex;
for (ByteIndex = 0; ByteIndex < this->STARSBits.size(); ++ByteIndex) {
if (0 != this->STARSBits[ByteIndex]) {
FoundSetBit = true;
break;
}
}
return FoundSetBit;
}
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
// Return highest index set; -1 if no bits set.
int STARSBitSet::FindHighestBitSet(void) const {
bool FoundSetBit = false;
size_t ByteIndex, BitIndex, BitSetSize = this->STARSBits.size();
assert(0 < BitSetSize);
for (ByteIndex = BitSetSize; ByteIndex > 0; --ByteIndex) {
if (0 != this->STARSBits[ByteIndex - 1]) {
FoundSetBit = true;
--ByteIndex;
BitIndex = HighestBitSet(this->STARSBits[ByteIndex]);
break;
}
}
if (FoundSetBit) {
return (int)(8 * ByteIndex + BitIndex);
}
else {
return -1;
}
}
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
// Return lowest index set; -1 if no bits set.
int STARSBitSet::FindLowestBitSet(void) const {
bool FoundSetBit = false;
size_t ByteIndex, BitIndex, BitSetSize = this->STARSBits.size();
assert(0 < BitSetSize);
for (ByteIndex = 0; ByteIndex < BitSetSize; ++ByteIndex) {
if (0 != this->STARSBits[ByteIndex]) {
FoundSetBit = true;
BitIndex = LowestBitSet(this->STARSBits[ByteIndex]);
break;
}
}
if (FoundSetBit) {
return (int)(8 * ByteIndex + BitIndex);
}
else {
return -1;
}
} // end of STARSBitSet::FindLowestBitSet()
// return count of set bits
size_t STARSBitSet::CountSetBits(void) const {
size_t ByteIndex, BitCount = 0, BitSetSize = this->STARSBits.size();
assert(0 < BitSetSize);
for (ByteIndex = 0; ByteIndex < BitSetSize; ++ByteIndex) {
unsigned char CurrByte = this->STARSBits[ByteIndex];
if (0 != CurrByte) {
BitCount += CountBitsSet((unsigned int) CurrByte);
}
}
return BitCount;
} // end of STARSBitSet::CountSetBits()
clc5q
committed
// Map system or library call name to FG info about its return value.
map<string, struct FineGrainedInfo> ReturnRegisterTypeMap;
clc5q
committed
// Map system or library call name to the annotation substring that
// guides saturating arithmetic or other continuation policies in
// the case of integer error detection of a value passed to that call.
// If we don't care about a certain call, we return an empty string.
static map<string, string> IntegerErrorCallSinkMap;
void InitIntegerErrorCallSinkMap(void) {
// in case STARS was invoked before.
IntegerErrorCallSinkMap.clear();
clc5q
committed
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
pair<string, string> MapEntry;
pair<map<string, string>::iterator, bool> InsertResult;
MapEntry.first = string("malloc");
MapEntry.second = string("SINKMALLOC");
InsertResult = IntegerErrorCallSinkMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("calloc");
MapEntry.second = string("SINKMALLOC");
InsertResult = IntegerErrorCallSinkMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("realloc");
MapEntry.second = string("SINKMALLOC");
InsertResult = IntegerErrorCallSinkMap.insert(MapEntry);
assert(InsertResult.second);
return;
}
// Return sink string for call name from the sink map.
// If we don't care find the call name, we return an empty string.
void GetSinkStringForCallName(string CalleeName, string &SinkString) {
map<string, string>::iterator MapIter;
SinkString.clear(); // empty string, append map string if found later
MapIter = IntegerErrorCallSinkMap.find(CalleeName);
if (MapIter != IntegerErrorCallSinkMap.end()) { // found it
SinkString.append(MapIter->second);
}
return;
}
// Map system or library call name to the argument number that
// should have an unsigned value and should be guarded from the
// signedness error that results from copying a signed value
// into the outgoing argument. Argument numbers are zero-based.
// We will return 0 when there is no argument to worry about
// for a particular library or system call name.
static map<string, unsigned int> UnsignedArgPositionMap;
void InitUnsignedArgPositionMap(void) {
// clear in case stars invoked multiple times.
UnsignedArgPositionMap.clear();
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
pair<string, unsigned int> MapEntry;
pair<map<string, unsigned int>::iterator, bool> InsertResult;
// <string.h>
MapEntry.first = string("memchr");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("memcmp");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("memcpy");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("memmove");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("memset");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("strncat");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("strncmp");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("strncpy");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("strxfrm");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
// <stdlib.h>
MapEntry.first = string("malloc");
MapEntry.second = STARS_ARG_POS_0;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("calloc");
MapEntry.second = (STARS_ARG_POS_0 | STARS_ARG_POS_1);
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("realloc");
MapEntry.second = STARS_ARG_POS_1;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("bsearch");
MapEntry.second = (STARS_ARG_POS_2 | STARS_ARG_POS_3);
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("qsort");
MapEntry.second = (STARS_ARG_POS_1 | STARS_ARG_POS_2);
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("mblen");
MapEntry.second = STARS_ARG_POS_1;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("mbtowc");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("mbstowcs");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
MapEntry.first = string("wcstombs");
MapEntry.second = STARS_ARG_POS_2;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
// <stdio.h>
MapEntry.first = string("setvbuf");
MapEntry.second = STARS_ARG_POS_3;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
// <time.h>
MapEntry.first = string("strftime");
MapEntry.second = STARS_ARG_POS_1;
InsertResult = UnsignedArgPositionMap.insert(MapEntry);
assert(InsertResult.second);
return;
} // end of InitUnsignedArgPositionMap()