From a410e68584ad10c380876ce7aae7106dc20ee1af Mon Sep 17 00:00:00 2001
From: clc5q <clc5q@git.zephyr-software.com>
Date: Thu, 4 Feb 2016 05:03:08 +0000
Subject: [PATCH] Use new stack DEF map to speed up GetGlobalDefAddr().

Former-commit-id: be29304c530d6812eb9985328d001b1d95649625
---
 include/base/SMPDataFlowAnalysis.h            |  5 ++-
 include/base/SMPFunction.h                    |  2 +-
 src/base/SMPDataFlowAnalysis.cpp              | 14 +++++---
 src/base/SMPFunction.cpp                      | 33 ++++++++++++-------
 ...save-ffmpeg.psexe.infoannot.REMOVED.git-id |  2 +-
 tests/commit/save-ls-64bit.psexe.infoannot    |  4 +--
 6 files changed, 38 insertions(+), 22 deletions(-)

diff --git a/include/base/SMPDataFlowAnalysis.h b/include/base/SMPDataFlowAnalysis.h
index a9c78302..d05ea504 100644
--- a/include/base/SMPDataFlowAnalysis.h
+++ b/include/base/SMPDataFlowAnalysis.h
@@ -230,7 +230,10 @@ bool MDLessRegOpnd(const STARSOpndTypePtr &RegOp1, const STARSOpndTypePtr &RegOp
 bool MDIsGeneralPurposeReg(const STARSOpndTypePtr &CurrOp);
 
 // Hash a global name and SSA number into an int, for use in SMPFunction.GlobalDefAddrBySSA map
-int HashGlobalNameAndSSA(const STARSOpndTypePtr &DefOp, int SSANum, bool UseFP = true);
+int HashGlobalNameAndSSA(const STARSOpndTypePtr &DefOp, int SSANum);
+
+// Hash a global name and SSA number into an int, for use in SMPFunction.GlobalDefAddrBySSA map
+int64_t HashGlobalStackNameAndSSA(const STARSOpndTypePtr &DefOp, int SSANum, bool UseFP = true);
 
 // Are operands equal?
 bool IsEqOp(const STARSOpndTypePtr &Opnd1, const STARSOpndTypePtr &Opnd2);
diff --git a/include/base/SMPFunction.h b/include/base/SMPFunction.h
index cfaf746c..77a609b9 100644
--- a/include/base/SMPFunction.h
+++ b/include/base/SMPFunction.h
@@ -580,7 +580,7 @@ private:
 		// If global DEF for that SSA is found in a Phi function, we use block number instead of inst addr
 		//  Instruction addresses should never overlap block #s, as block #s start at 0 and top out at a few hundred.
 		// NOTE: We are currently limiting this map to registers, not all global names.
-	std::map<int, STARS_ea_t> GlobalStackDefAddrBySSA; // map hash of normalized stack offset & SSANum to DEF inst addr
+	std::map<int64_t, STARS_ea_t> GlobalStackDefAddrBySSA; // map hash of normalized stack offset & SSANum to DEF inst addr
 
 	std::map<int, struct FineGrainedInfo> GlobalDefFGInfoBySSA; // map hash of global name & SSANum to DEF FG info.
 		// NOTE: We are currently limiting this map to registers, not all global names.
diff --git a/src/base/SMPDataFlowAnalysis.cpp b/src/base/SMPDataFlowAnalysis.cpp
index 7ae8de56..d92f7fb9 100644
--- a/src/base/SMPDataFlowAnalysis.cpp
+++ b/src/base/SMPDataFlowAnalysis.cpp
@@ -166,15 +166,19 @@ bool SMPDefsFlags[STARS_NN_last + 1];
 bool SMPUsesFlags[STARS_NN_last + 1];
 
 // Hash a global name and SSA number into an int, for use in SMPFunction.GlobalDefAddrBySSA map
-int HashGlobalNameAndSSA(const STARSOpndTypePtr &DefOp, int SSANum, bool UseFP) { 
+int HashGlobalNameAndSSA(const STARSOpndTypePtr &DefOp, int SSANum) { 
 	int HashValue = 0;
 	if (DefOp->IsRegOp()) {
 		HashValue = ((SSANum << 16) | ((int)(DefOp->GetReg())));
 	}
-	else {
-		assert(MDIsDirectStackAccessOpnd(DefOp, UseFP));
-		HashValue = ((SSANum << 16) | (((int)(DefOp->GetAddr())) & 0xffff));
-	}
+	return HashValue;
+}
+
+// Hash a global name and SSA number into an int, for use in SMPFunction.GlobalDefAddrBySSA map
+int64_t HashGlobalStackNameAndSSA(const STARSOpndTypePtr &DefOp, int SSANum, bool UseFP) {
+	int64_t HashValue = 0;
+	assert(MDIsDirectStackAccessOpnd(DefOp, UseFP));
+	HashValue = ((((int64_t) SSANum) << 32) | (((uint64_t)(DefOp->GetAddr())) & 0xffffffff));
 	return HashValue;
 }
 
diff --git a/src/base/SMPFunction.cpp b/src/base/SMPFunction.cpp
index ae9e7cb9..8ac8e00e 100644
--- a/src/base/SMPFunction.cpp
+++ b/src/base/SMPFunction.cpp
@@ -3198,17 +3198,23 @@ bool SMPFunction::MDUpdateFGStackLocInfo(STARS_ea_t InstAddr, const STARSOpndTyp
 
 // retrieve DEF addr from GlobalDefAddrBySSA or return STARS_BADADDR
 STARS_ea_t SMPFunction::GetGlobalDefAddr(const STARSOpndTypePtr &DefOp, int SSANum) {
-	map<int, STARS_ea_t>::iterator MapResult;
 	STARS_ea_t DefAddr = STARS_BADADDR; // STARS_BADADDR means we did not find it
 	bool RegDef = (DefOp->IsRegOp());
 
 	if (RegDef) {
 		int HashedName = HashGlobalNameAndSSA(DefOp, SSANum);
-		MapResult = this->GlobalDefAddrBySSA.find(HashedName);
+		map<int, STARS_ea_t>::iterator MapResult = this->GlobalDefAddrBySSA.find(HashedName);
 		if (MapResult != this->GlobalDefAddrBySSA.end()) { // Found it.
 			DefAddr = (STARS_ea_t) MapResult->second;
 		}
 	}
+	else if (MDIsDirectStackAccessOpnd(DefOp, this->UsesFramePointer())) {
+		int64_t HashedName = HashGlobalStackNameAndSSA(DefOp, SSANum, this->UsesFramePointer());
+		map<int64_t, STARS_ea_t>::iterator MapResult = this->GlobalStackDefAddrBySSA.find(HashedName);
+		if (MapResult != this->GlobalStackDefAddrBySSA.end()) { // Found it.
+			DefAddr = (STARS_ea_t) MapResult->second;
+		}
+	}
 	else if (MDIsStackAccessOpnd(DefOp, this->UsesFramePointer())) {
 		// Until we get stack operands into the GlobalDefAddrBySSA map,
 		//  do a linear search.
@@ -7567,16 +7573,17 @@ void SMPFunction::SSARename(int BlockNumber) {
 				SMP_msg("New EAX Phi Def SSANum: %d Block %d\n", NewSSANum, BlockNumber);
 			}
 			// Map the final SSA number to the block number.
-			int DefHashValue = HashGlobalNameAndSSA(PhiDefOp, NewSSANum);
-			pair<int, STARS_ea_t> DefMapEntry(DefHashValue, (STARS_PSEUDO_ID_MIN + CurrBlock->GetNumber()));
-			pair<map<int, STARS_ea_t>::iterator, bool> MapReturnValue;
 			if (RegOpFlag) {
-				MapReturnValue = this->GlobalDefAddrBySSA.insert(DefMapEntry);
+				int DefHashValue = HashGlobalNameAndSSA(PhiDefOp, NewSSANum);
+				pair<int, STARS_ea_t> DefMapEntry(DefHashValue, (STARS_PSEUDO_ID_MIN + CurrBlock->GetNumber()));
+				pair<map<int, STARS_ea_t>::iterator, bool> MapReturnValue = this->GlobalDefAddrBySSA.insert(DefMapEntry);
 				assert(MapReturnValue.second);
 			}
 #if STARS_TRACK_STACK_DEF_ADDRS
 			else {
-				MapReturnValue = this->GlobalStackDefAddrBySSA.insert(DefMapEntry);
+				int64_t DefHashValue = HashGlobalStackNameAndSSA(PhiDefOp, NewSSANum, this->UsesFramePointer());
+				pair<int64_t, STARS_ea_t> DefMapEntry(DefHashValue, ((int64_t)(STARS_PSEUDO_ID_MIN + CurrBlock->GetNumber())));
+				pair<map<int64_t, STARS_ea_t>::iterator, bool>  MapReturnValue = this->GlobalStackDefAddrBySSA.insert(DefMapEntry);
 				assert(MapReturnValue.second);
 			}
 #endif
@@ -7659,16 +7666,17 @@ void SMPFunction::SSARename(int BlockNumber) {
 					}
 
 					// Map the final SSA number to the DEF address.
-					int DefHashValue = HashGlobalNameAndSSA(DefOp, NewSSANum);
-					pair<int, STARS_ea_t> DefMapEntry(DefHashValue, DefAddr);
-					pair<map<int, STARS_ea_t>::iterator, bool> MapReturnValue;
 					if (RegOpFlag) {
-						MapReturnValue = this->GlobalDefAddrBySSA.insert(DefMapEntry);
+						int DefHashValue = HashGlobalNameAndSSA(DefOp, NewSSANum);
+						pair<int, STARS_ea_t> DefMapEntry(DefHashValue, DefAddr);
+						pair<map<int, STARS_ea_t>::iterator, bool> MapReturnValue = this->GlobalDefAddrBySSA.insert(DefMapEntry);
 						assert(MapReturnValue.second);
 					}
 #if STARS_TRACK_STACK_DEF_ADDRS
 					else {
-						MapReturnValue = this->GlobalStackDefAddrBySSA.insert(DefMapEntry);
+						int64_t DefHashValue = HashGlobalStackNameAndSSA(DefOp, NewSSANum, this->UsesFramePointer());
+						pair<int64_t, STARS_ea_t> DefMapEntry(DefHashValue, DefAddr);
+						pair<map<int64_t, STARS_ea_t>::iterator, bool> MapReturnValue = this->GlobalStackDefAddrBySSA.insert(DefMapEntry);
 						assert(MapReturnValue.second);
 					}
 #endif
@@ -9945,6 +9953,7 @@ bool SMPFunction::IsDefUsedInUnsafeMemWrite(STARSOpndTypePtr DefOp, int DefSSANu
 		DefSSANum = HeadItem.first.second;
 		DefAddr = HeadItem.second;
 		DefWorkList.pop_front();
+		assert(!DefOp->IsMemOp());
 
 		if (!(MDIsStackPtrReg(DefOp->GetReg(), this->UsesFramePointer()))) {
 			// Ensure that we don't have a data dependence loop that causes
diff --git a/tests/commit/save-ffmpeg.psexe.infoannot.REMOVED.git-id b/tests/commit/save-ffmpeg.psexe.infoannot.REMOVED.git-id
index 5ec59274..b4e0146e 100644
--- a/tests/commit/save-ffmpeg.psexe.infoannot.REMOVED.git-id
+++ b/tests/commit/save-ffmpeg.psexe.infoannot.REMOVED.git-id
@@ -1 +1 @@
-3ab214aa7711f69874c2a6a3c167ad7e7d13c4f1
\ No newline at end of file
+d649d83c0e7aad6512d964f66e1f84bf1aa12ec7
\ No newline at end of file
diff --git a/tests/commit/save-ls-64bit.psexe.infoannot b/tests/commit/save-ls-64bit.psexe.infoannot
index 787fc817..ff3b8038 100644
--- a/tests/commit/save-ls-64bit.psexe.infoannot
+++ b/tests/commit/save-ls-64bit.psexe.infoannot
@@ -552,8 +552,8 @@
             40d010      4 INSTR CHECK OVERFLOW UNSIGNED 64  R15 ZZ add     r15, 1 
             40d0a0      4 INSTR CHECK OVERFLOW UNSIGNED 64  R15 ZZ add     r15, 1 
             40d149      5 INSTR CHECK UNDERFLOW UNSIGNED 64  RDX ZZ sub     rdx, [rsp+68h+var_58] 
-            40d14e      4 INSTR CHECK OVERFLOW NOFLAGUNSIGNED 64 RCX*8 ZZ IDIOM 32 lea     rsi, [rbx+rcx*8]; src 
-            40d14e      4 INSTR CHECK OVERFLOW NOFLAGUNSIGNED 64 RBX+RCX*8 ZZ IDIOM 32 lea     rsi, [rbx+rcx*8]; src 
+            40d14e      4 INSTR CHECK OVERFLOW NOFLAGUNSIGNED 64 RCX*8 ZZ IDIOM 18 MEMORYSINK lea     rsi, [rbx+rcx*8]; src 
+            40d14e      4 INSTR CHECK OVERFLOW NOFLAGUNSIGNED 64 RBX+RCX*8 ZZ IDIOM 18 MEMORYSINK lea     rsi, [rbx+rcx*8]; src 
             40d1a0     15 FUNC RETURNTYPE RAX 4
             40d1a0     15 FUNC INARGS    6  ARG0 4 ARG1 0 ARG2 0 ARG3 0 ARG4 1 ARG5 0 
             40d1b0    147 FUNC RETURNTYPE RAX 4
-- 
GitLab