From 373860a0600628041c486aa5abb1a6df4df388bc Mon Sep 17 00:00:00 2001
From: clc5q <clc5q@git.zephyr-software.com>
Date: Thu, 1 Oct 2015 19:15:48 +0000
Subject: [PATCH] Make indirect writes safe if they come from loop-invariant
 stack copies.

Former-commit-id: 41498c234c3cdb0b9493aa331552d61100b10637
---
 src/base/SMPBasicBlock.cpp                                 | 7 ++++++-
 .../trimmed-sorted-save-busybox.psexe.annot.REMOVED.git-id | 2 +-
 .../trimmed-sorted-save-ffmpeg.psexe.annot.REMOVED.git-id  | 2 +-
 .../trimmed-sorted-save-gimp.psexe.annot.REMOVED.git-id    | 2 +-
 ...sorted-save-gnome-calculator.psexe.annot.REMOVED.git-id | 2 +-
 .../trimmed-sorted-save-nginx.psexe.annot.REMOVED.git-id   | 2 +-
 6 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/base/SMPBasicBlock.cpp b/src/base/SMPBasicBlock.cpp
index 5f36c7fc..2771b466 100644
--- a/src/base/SMPBasicBlock.cpp
+++ b/src/base/SMPBasicBlock.cpp
@@ -6010,6 +6010,7 @@ bool SMPBasicBlock::IsDefUsedInUnsafeMemWrite(list<pair<pair<STARSOpndTypePtr, i
 	return true;
 #else
 	bool FoundMemWriteUse = false;
+	bool InvariantDEF = false;
 	assert(DefOp->IsRegOp());
 	if (!(this->IsProcessed())) {
 		this->SetProcessed(true);
@@ -6025,6 +6026,10 @@ bool SMPBasicBlock::IsDefUsedInUnsafeMemWrite(list<pair<pair<STARSOpndTypePtr, i
 		}
 		else {
 			InstIter = this->GetInstIterFromAddr(DefAddr);
+			SMPInstr *DefInst = (*InstIter);
+			set<DefOrUse, LessDefUse>::iterator DefIter = DefInst->FindDef(DefOp);
+			assert(DefIter != DefInst->GetLastDef());
+			InvariantDEF = DefIter->IsInvariantForAllLoops();
 			++InstIter;
 		}
 		while (InstIter != this->GetLastInst()) {
@@ -6041,7 +6046,7 @@ bool SMPBasicBlock::IsDefUsedInUnsafeMemWrite(list<pair<pair<STARSOpndTypePtr, i
 				STARSOpndTypePtr NewDefOp = DefIter->GetOp();
 				assert(nullptr != NewDefOp);
 #if 1
-				bool DangerousWrite = CurrInst->IsUnsafeMemWrite();
+				bool DangerousWrite = (!InvariantDEF) && CurrInst->IsUnsafeMemWrite();
 #else
 				bool DangerousWrite = CurrInst->HasDestMemoryOperand();
 				if (DangerousWrite) {
diff --git a/tests/commit/trimmed-sorted-save-busybox.psexe.annot.REMOVED.git-id b/tests/commit/trimmed-sorted-save-busybox.psexe.annot.REMOVED.git-id
index 888a0456..a32081bd 100644
--- a/tests/commit/trimmed-sorted-save-busybox.psexe.annot.REMOVED.git-id
+++ b/tests/commit/trimmed-sorted-save-busybox.psexe.annot.REMOVED.git-id
@@ -1 +1 @@
-cc0231bd87f595a3cbb8158cd581363bb6e25cff
\ No newline at end of file
+c1ce9a28bae332d56f8b2632e733683802430009
\ No newline at end of file
diff --git a/tests/commit/trimmed-sorted-save-ffmpeg.psexe.annot.REMOVED.git-id b/tests/commit/trimmed-sorted-save-ffmpeg.psexe.annot.REMOVED.git-id
index 66fa65bf..317b58c7 100644
--- a/tests/commit/trimmed-sorted-save-ffmpeg.psexe.annot.REMOVED.git-id
+++ b/tests/commit/trimmed-sorted-save-ffmpeg.psexe.annot.REMOVED.git-id
@@ -1 +1 @@
-cca5a06f533d5be15cc392a41d1b6632da134bda
\ No newline at end of file
+923578564814ffb4d04bdd98c8f50ae5913099ee
\ No newline at end of file
diff --git a/tests/commit/trimmed-sorted-save-gimp.psexe.annot.REMOVED.git-id b/tests/commit/trimmed-sorted-save-gimp.psexe.annot.REMOVED.git-id
index 78d399e9..3c15ebfa 100644
--- a/tests/commit/trimmed-sorted-save-gimp.psexe.annot.REMOVED.git-id
+++ b/tests/commit/trimmed-sorted-save-gimp.psexe.annot.REMOVED.git-id
@@ -1 +1 @@
-a302870b39df70affe3181870555860f054d30bc
\ No newline at end of file
+1bbceddf6fb9b7e1e29612686a4f8dae55668624
\ No newline at end of file
diff --git a/tests/commit/trimmed-sorted-save-gnome-calculator.psexe.annot.REMOVED.git-id b/tests/commit/trimmed-sorted-save-gnome-calculator.psexe.annot.REMOVED.git-id
index c7db4ff3..a5e1673e 100644
--- a/tests/commit/trimmed-sorted-save-gnome-calculator.psexe.annot.REMOVED.git-id
+++ b/tests/commit/trimmed-sorted-save-gnome-calculator.psexe.annot.REMOVED.git-id
@@ -1 +1 @@
-b022d794ff6109eee9e2d8f9413acf0c4aeef019
\ No newline at end of file
+050d480af91f2aff5a61e00f2a8d3f58afb58c11
\ No newline at end of file
diff --git a/tests/commit/trimmed-sorted-save-nginx.psexe.annot.REMOVED.git-id b/tests/commit/trimmed-sorted-save-nginx.psexe.annot.REMOVED.git-id
index a667d8c3..09b171f2 100644
--- a/tests/commit/trimmed-sorted-save-nginx.psexe.annot.REMOVED.git-id
+++ b/tests/commit/trimmed-sorted-save-nginx.psexe.annot.REMOVED.git-id
@@ -1 +1 @@
-871af1cc2b6a60b2d15b16c8ec0eef69923836b2
\ No newline at end of file
+304040fb5138645d8f4e7694eebede27fc7a0053
\ No newline at end of file
-- 
GitLab