From 54278cbb8c3f97a8021b338f3addaaa396210ac3 Mon Sep 17 00:00:00 2001
From: clc5q <clc5q@git.zephyr-software.com>
Date: Fri, 4 Aug 2017 00:52:46 +0000
Subject: [PATCH] Improve type inference debug output for SMPTypeMeet errors.

Former-commit-id: 006f0b47220b8e8fd97e78c8ad8e955384232c66
---
 include/base/SMPDataFlowAnalysis.h |  2 +-
 src/base/SMPDataFlowAnalysis.cpp   | 11 ++++++++---
 src/base/SMPFunction.cpp           | 21 ++++++++++++++++++---
 src/base/SMPInstr.cpp              | 14 ++++++++++++--
 4 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/include/base/SMPDataFlowAnalysis.h b/include/base/SMPDataFlowAnalysis.h
index a2cf19cf..087ce17d 100644
--- a/include/base/SMPDataFlowAnalysis.h
+++ b/include/base/SMPDataFlowAnalysis.h
@@ -540,7 +540,7 @@ enum SMPOperandType { // What type is a given register or memory operand?
 #define MakeProfDerived(OpType) ((SMPOperandType)(OpType | PROF_BASE))
 
 // Meet function over any two types in the type lattice.
-SMPOperandType SMPTypeMeet(SMPOperandType Type1, SMPOperandType Type2);
+SMPOperandType SMPTypeMeet(SMPOperandType Type1, SMPOperandType Type2, bool &ErrorFlag);
 
 // Meet function for SCCP constant propagation; updates NewConstStruct
 void STARSConstantTypeMeet(struct STARS_SCCP_Const_Struct OldConstStruct, struct STARS_SCCP_Const_Struct &NewConstStruct);
diff --git a/src/base/SMPDataFlowAnalysis.cpp b/src/base/SMPDataFlowAnalysis.cpp
index 4294fa82..b2d6ecca 100644
--- a/src/base/SMPDataFlowAnalysis.cpp
+++ b/src/base/SMPDataFlowAnalysis.cpp
@@ -2777,8 +2777,9 @@ bool MDKnownOperandType(const STARSOpndTypePtr &TempOp) {
 }
 
 // Meet function over any two types in the type lattice.
-SMPOperandType SMPTypeMeet(SMPOperandType Type1, SMPOperandType Type2) {
+SMPOperandType SMPTypeMeet(SMPOperandType Type1, SMPOperandType Type2, bool &ErrorFlag) {
 	SMPOperandType MeetType = UNKNOWN;
+	ErrorFlag = false;
 	bool ProfDerived = IsProfDerived(Type1) || IsProfDerived(Type2);
 	if (IsEqType(UNINIT, Type1))
 		MeetType = Type2;
@@ -2790,16 +2791,20 @@ SMPOperandType SMPTypeMeet(SMPOperandType Type1, SMPOperandType Type2) {
 			MeetType = CODEPTR;
 		else if (IsDataPtr(Type2) || IsUnknown(Type2))
 			MeetType = UNKNOWN;
-		else
+		else {
 			SMP_msg("ERROR #1 in SMPTypeMeet.\n");
+			ErrorFlag = true;
+		}
 	}
 	else if (IsDataPtr(Type1)) {
 		if (IsDataPtr(Type2))  // two different POINTER subtypes
 			MeetType = POINTER;
 		else if (IsNumeric(Type2) || IsUnknown(Type2))
 			MeetType = UNKNOWN;
-		else
+		else {
 			SMP_msg("ERROR #2 in SMPTypeMeet.\n");
+			ErrorFlag = true;
+		}
 	}
 	if (ProfDerived && IsNotEqType(UNINIT, MeetType))
 		MeetType = MakeProfDerived(MeetType);
diff --git a/src/base/SMPFunction.cpp b/src/base/SMPFunction.cpp
index 24606f0c..d7ae435a 100644
--- a/src/base/SMPFunction.cpp
+++ b/src/base/SMPFunction.cpp
@@ -3117,7 +3117,12 @@ bool SMPFunction::MDFindReturnTypes(void) {
 				if ((! UseOp->IsRegOp()) || (global_STARS_program->GetSTARS_MD_LAST_SAVED_REG_NUM() < UseOp->GetReg()))
 					continue;
 				SMPOperandType OldType = this->ReturnRegTypes.at(UseOp->GetReg());
-				SMPOperandType NewType = SMPTypeMeet(OldType, CurrUse->GetType());
+				bool TypeErrorFlag = false;
+				SMPOperandType NewType = SMPTypeMeet(OldType, CurrUse->GetType(), TypeErrorFlag);
+				if (TypeErrorFlag) {
+					SMP_msg("ERROR: TypeMeet error in MDFindReturnTypes() func at %llx return at %llx\n",
+						(uint64_t) this->GetFirstFuncAddr(), (uint64_t) CurrInst->GetAddr());
+				}
 				if (NewType != OldType) {
 					this->ReturnRegTypes[UseOp->GetReg()] = NewType;
 					changed = true;
@@ -3172,7 +3177,12 @@ void SMPFunction::MDFindIncomingTypes(void) {
 					// Found it. See if we have type info.
 					SMPOperandType CallSiteType = CallSiteUseIter->GetType();
 					SMPOperandType OldType = this->IncomingRegTypes[RegNum];
-					this->IncomingRegTypes[RegNum] = SMPTypeMeet(CallSiteType, OldType);
+					bool TypeErrorFlag = false;
+					this->IncomingRegTypes[RegNum] = SMPTypeMeet(CallSiteType, OldType, TypeErrorFlag);
+					if (TypeErrorFlag) {
+						SMP_msg("ERROR: TypeMeet error in MDFindIncomingTypes() at %llx\n",
+							(uint64_t) this->GetFirstFuncAddr());
+					}
 					// Note this type meet function is used in an aggressive way here, as
 					//  a mixture of UNINIT and NUMERIC at various call sites will produce NUMERIC, for example.
 					//  We are not demanding that all call sites have a consistent type with no UNINIT instances.
@@ -13201,7 +13211,12 @@ void SMPFunction::GatherIncomingArgTypes(void) {
 							}
 							else { // Not the first time to see arg; could be processing an arg one byte at a time, for example.
 								// Do not increment ArgCount for duplicate processing. MaxInArgIndex does not need updating, either.
-								this->InArgTypes[ArgIndex] = (unsigned short) SMPTypeMeet((SMPOperandType) this->InArgTypes[ArgIndex], ArgType);
+								bool TypeErrorFlag = false;
+								this->InArgTypes[ArgIndex] = (unsigned short) SMPTypeMeet((SMPOperandType) this->InArgTypes[ArgIndex], ArgType, TypeErrorFlag);
+								if (TypeErrorFlag) {
+									SMP_msg("ERROR: TypeMeet error in GatherIncomingArgType() for func at %llx\n",
+										(uint64_t) this->GetFirstFuncAddr());
+								}
 							}
 						}
 						else {
diff --git a/src/base/SMPInstr.cpp b/src/base/SMPInstr.cpp
index 4a56f203..c37bb92e 100644
--- a/src/base/SMPInstr.cpp
+++ b/src/base/SMPInstr.cpp
@@ -11393,7 +11393,12 @@ void SMPInstr::MDSetWidthSignInfo(bool UseFP) {
 			else if (FGEntry.SizeInfo & FG_MASK_DATAPOINTER) {
 				NewDefType = POINTER;
 			}
-			NewDefType = SMPTypeMeet(DefType, NewDefType);
+			bool TypeErrorFlag = false;
+			NewDefType = SMPTypeMeet(DefType, NewDefType, TypeErrorFlag);
+			if (TypeErrorFlag) {
+				SMP_msg("ERROR: TypeMeet error in MDSetWidthSignInfo() at %llx\n",
+					(uint64_t) this->GetAddr());
+			}
 			bool UpdateType = ((NewDefType != DefType) && (DefType == UNINIT));
 			if (UpdateType) {
 				DefIter = this->SetDefType(DefOp, NewDefType);
@@ -12792,7 +12797,12 @@ bool SMPInstr::InferTypes(void) {
 						STARSOpndTypePtr CallUseOp = CallUseIter->GetOp();
 						if (CalleeFunc->GetMarkerInstDefType(CallUseOp, CallUseType)) {
 							// CalleeFunc marker inst DEFed the current use with type CallUseType.
-							CallUseType = SMPTypeMeet(CallUseType, CallUseIter->GetType());
+							bool TypeErrorFlag = false;
+							CallUseType = SMPTypeMeet(CallUseType, CallUseIter->GetType(), TypeErrorFlag);
+							if (TypeErrorFlag) {
+								SMP_msg("ERROR: TypeMeet error at call addr %llx CalleeAddr %llx\n", 
+									(uint64_t) this->GetAddr(), (uint64_t) CalleeAddr);
+							}
 							CallUseIter = this->SetUseType(CallUseOp, CallUseType);
 						}
 						++CallUseIter;
-- 
GitLab