From 71af0441004cad3f35b4eefb369884253dae684b Mon Sep 17 00:00:00 2001
From: clc5q <clc5q@git.zephyr-software.com>
Date: Sat, 13 Aug 2016 02:59:07 +0000
Subject: [PATCH] Fixes for induction var analysis; code cleanup for cGC.

Former-commit-id: 0fb0ae8c5cc0841458a435d70e534a1dcd00a9b0
---
 src/base/SMPFunction.cpp | 28 +++++++++-------------------
 src/base/SMPInstr.cpp    | 23 ++++++++++++++++-------
 2 files changed, 25 insertions(+), 26 deletions(-)

diff --git a/src/base/SMPFunction.cpp b/src/base/SMPFunction.cpp
index 4e8e2277..3f0b9060 100644
--- a/src/base/SMPFunction.cpp
+++ b/src/base/SMPFunction.cpp
@@ -3978,13 +3978,13 @@ void SMPFunction::AnalyzeFunc(void) {
 			CurrInst->SetBlock(CurrBlock->GetThisBlock());
 
 
-// jdh hack:  redo syscall analysis as block may not be set 
-// when syscall analysis was done first time.
-if (CurrInst->GetDataFlowType() == INDIR_CALL) {
-	if (CurrInst->MDIsSystemCall()) {
-		this->SetHasSystemCalls();
-	}
-}
+			// jdh hack:  redo syscall analysis as block may not be set 
+			// when syscall analysis was done first time.
+			if (CurrInst->GetDataFlowType() == INDIR_CALL) {
+				if (CurrInst->MDIsSystemCall()) {
+					this->SetHasSystemCalls();
+				}
+			}
 
 			SMPitype InstDataFlowType = CurrInst->GetDataFlowType();
 			if ((CALL == InstDataFlowType) || (INDIR_CALL == InstDataFlowType)) {
@@ -5992,6 +5992,7 @@ void SMPFunction::DetectLoopInductionVars(void) {
 					int SecondOpSSANum = SMP_SSA_UNINIT;
 					if (!BIVarFound && MDIsDataFlowOpnd(SecondOp, UseFP) && this->IsGlobalName(SecondOp)) {
 						STARSOpndTypePtr SearchOp = CloneIfNecessary(SecondOp, UseFP);
+						CanonicalizeOpnd(SearchOp);
 						STARSDefUseIter SecondOpUseIter = InsideDefInst->FindUse(SearchOp);
 						assert(SecondOpUseIter != InsideDefInst->GetLastUse());
 						SecondOpSSANum = SecondOpUseIter->GetSSANum();
@@ -6000,7 +6001,7 @@ void SMPFunction::DetectLoopInductionVars(void) {
 						//  Note that block-local names are loop-variant and are already excluded.
 						SMPBasicBlock *InsideDefBlock = this->GetBlockByNum((size_t) PhiUseDefBlockNum1);
 						assert(NULL != InsideDefBlock);
-						STARS_ea_t SecondOpDefAddr = InsideDefBlock->GetDefAddrFromUseAddr(SecondOp, InsideDefAddr, UseIter->GetSSANum(), false);
+						STARS_ea_t SecondOpDefAddr = InsideDefBlock->GetDefAddrFromUseAddr(SearchOp, InsideDefAddr, SecondOpUseIter->GetSSANum(), false);
 						assert(STARS_BADADDR != SecondOpDefAddr);
 						int SecondOpDefBlockNum = SMP_BLOCKNUM_UNINIT;
 						bool SecondOpDEFIsInst = false;
@@ -8702,16 +8703,6 @@ void SMPFunction::AnalyzeSystemCalls(void) {
 						if (!UnresolvedCallee) {
 							// We have an instruction addr in EAXDefAddr.
 							SMPInstr *EAXDefInst = this->GetInstFromAddr(EAXDefAddr);
-#if 0
-							STARSDefUseIter DefIter = EAXDefInst->FindDef(EAXOpnd);
-							assert(DefIter != EAXDefInst->GetLastDef());
-							STARSOpndTypePtr DefOp = DefIter->GetOp();
-							UnresolvedCallee = (!DefOp->IsImmedOp());
-							if (!UnresolvedCallee) {
-								SysCallNumber = DefOp->GetImmedValue();
-								SMP_msg("INFO: System call number %u found at %llx\n", (unsigned int)SysCallNumber, (unsigned long long) EAXDefAddr);
-							}
-#else
 							if (EAXDefInst == NULL)
 								UnresolvedCallee = true;
 
@@ -8745,7 +8736,6 @@ void SMPFunction::AnalyzeSystemCalls(void) {
 								SMP_msg("INFO: System call set of EAX determined solid. number: %u at: %llx\n", 
 									(unsigned int) SysCallNumber, (unsigned long long) EAXDefAddr);
 							}
-#endif
 						}
 						if (!UnresolvedCallee) { // we have a good SysCallNumber
 #define CGC_RECEIVE_SYSCALL 3
diff --git a/src/base/SMPInstr.cpp b/src/base/SMPInstr.cpp
index 3568ec28..9ea2c8da 100644
--- a/src/base/SMPInstr.cpp
+++ b/src/base/SMPInstr.cpp
@@ -3969,13 +3969,22 @@ bool SMPInstr::IsDependentInductionVarArithmetic(STARSOpndTypePtr &Mult1, STARSO
 		STARS_ea_t offset;
 		LeaOpnd->MDExtractAddressFields(BaseReg, IndexReg, Scale, offset);
 		if (STARS_x86_R_none == IndexReg) {
-			// Simple case: Lhs := BaseReg + offset
-			STARS_uval_t ImmedValue = (STARS_uval_t) offset;
-			Add1 = this->MakeRegOpnd((STARS_regnum_t) BaseReg);
-			Add2 = this->MakeImmediateOpnd(ImmedValue);
-			Mult1 = nullptr;
-			Mult2 = nullptr;
-			DIVOperation = true;
+			if (STARS_x86_R_none != BaseReg) {
+				// Simple case: Lhs := BaseReg + offset
+				STARS_uval_t ImmedValue = (STARS_uval_t)offset;
+				Add1 = this->MakeRegOpnd((STARS_regnum_t)BaseReg);
+				Add2 = this->MakeImmediateOpnd(ImmedValue);
+				Mult1 = nullptr;
+				Mult2 = nullptr;
+				DIVOperation = true;
+			}
+			else { // both regs are null, e.g. PC-relative expression
+				Add1 = nullptr;
+				Add2 = nullptr;
+				Mult1 = nullptr;
+				Mult2 = nullptr;
+				DIVOperation = false;
+			}
 		}
 		else {
 			STARS_uval_t ImmedValue = (STARS_uval_t) offset;
-- 
GitLab