From a829d69c51343fa497fd047e6f58325c53874e85 Mon Sep 17 00:00:00 2001
From: Sandro Grebant <sandro.grebant@univ-lille.fr>
Date: Fri, 5 Feb 2021 12:29:02 +0100
Subject: [PATCH] correct wrong loop_id for instruction cache block

---
 CFTree.cpp       | 23 ++++++++++++++---------
 cache/cache.xml  |  2 +-
 include/CFTree.h |  5 ++++-
 3 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/CFTree.cpp b/CFTree.cpp
index 22feca5..23c9a93 100644
--- a/CFTree.cpp
+++ b/CFTree.cpp
@@ -1297,7 +1297,7 @@ Retourne le noeud qui est dominé par tous les autres
 					{ // nullptr means that it's a new block, associated with cache
 						BasicCacheBlock *bcb = static_cast<BasicCacheBlock *>(b);
 						f->kind = KIND_CONST;
-						f->aw.loop_id = -1;
+						f->aw.loop_id = bcb->getLoopId();
 
 						// manage annotations
 						if (bcb->getType() == HIT)
@@ -1608,6 +1608,11 @@ Retourne le noeud qui est dominé par tous les autres
 			category_t cat = CATEGORY(lBlock);
 			//cout << "\n\tLBlock " << lBlock->id() << " : " << cat << endl;
 
+			// get the loop header
+			Block* header = CATEGORY_HEADER(lBlock);
+			int loop_id = header != nullptr ? header->id() : -1;
+			//cout << "Loop id : " << loop_id << endl;
+
 			// create a fake array of instructions
 			NullInst nullInst;
 			NullInst nullInstArray[1];
@@ -1622,7 +1627,7 @@ Retourne le noeud qui est dominé par tous les autres
 			{
 			case ALWAYS_HIT:
 			{
-				BasicCacheBlock *block = new BasicCacheBlock(instsHit, lBlock->id(), basicBlockId, HIT, cat);
+				BasicCacheBlock *block = new BasicCacheBlock(instsHit, lBlock->id(), basicBlockId, HIT, cat, loop_id);
 				CFTreeLeaf *leaf = new CFTreeLeaf(block);
 				return leaf;
 				break;
@@ -1630,7 +1635,7 @@ Retourne le noeud qui est dominé par tous les autres
 
 			case ALWAYS_MISS:
 			{
-				BasicCacheBlock *block = new BasicCacheBlock(instsMiss, lBlock->id(), basicBlockId, MISS, cat);
+				BasicCacheBlock *block = new BasicCacheBlock(instsMiss, lBlock->id(), basicBlockId, MISS, cat, loop_id);
 				CFTreeLeaf *leaf = new CFTreeLeaf(block);
 				return leaf;
 				break;
@@ -1638,10 +1643,10 @@ Retourne le noeud qui est dominé par tous les autres
 
 			case FIRST_HIT:
 			{
-				BasicCacheBlock *blockHit = new BasicCacheBlock(instsHit, lBlock->id(), basicBlockId, HIT, cat);
+				BasicCacheBlock *blockHit = new BasicCacheBlock(instsHit, lBlock->id(), basicBlockId, HIT, cat, loop_id);
 				CFTreeLeaf *leafHit = new CFTreeLeaf(blockHit);
 
-				BasicCacheBlock *blockMiss = new BasicCacheBlock(instsMiss, lBlock->id(), basicBlockId, MISS, cat);
+				BasicCacheBlock *blockMiss = new BasicCacheBlock(instsMiss, lBlock->id(), basicBlockId, MISS, cat, loop_id);
 				CFTreeLeaf *leafMiss = new CFTreeLeaf(blockMiss);
 
 				std::vector<CFTree *> *alts = new std::vector<CFTree *>();
@@ -1654,10 +1659,10 @@ Retourne le noeud qui est dominé par tous les autres
 
 			case FIRST_MISS:
 			{
-				BasicCacheBlock *blockHit = new BasicCacheBlock(instsHit, lBlock->id(), basicBlockId, HIT, cat);
+				BasicCacheBlock *blockHit = new BasicCacheBlock(instsHit, lBlock->id(), basicBlockId, HIT, cat, loop_id);
 				CFTreeLeaf *leafHit = new CFTreeLeaf(blockHit);
 
-				BasicCacheBlock *blockMiss = new BasicCacheBlock(instsMiss, lBlock->id(), basicBlockId, MISS, cat);
+				BasicCacheBlock *blockMiss = new BasicCacheBlock(instsMiss, lBlock->id(), basicBlockId, MISS, cat, loop_id);
 				CFTreeLeaf *leafMiss = new CFTreeLeaf(blockMiss);
 
 				std::vector<CFTree *> *alts = new std::vector<CFTree *>();
@@ -1671,10 +1676,10 @@ Retourne le noeud qui est dominé par tous les autres
 			case INVALID_CATEGORY:
 			case NOT_CLASSIFIED:
 			{
-				BasicCacheBlock *blockHit = new BasicCacheBlock(instsHit, lBlock->id(), basicBlockId, HIT, cat);
+				BasicCacheBlock *blockHit = new BasicCacheBlock(instsHit, lBlock->id(), basicBlockId, HIT, cat, loop_id);
 				CFTreeLeaf *leafHit = new CFTreeLeaf(blockHit);
 
-				BasicCacheBlock *blockMiss = new BasicCacheBlock(instsMiss, lBlock->id(), basicBlockId, MISS, cat);
+				BasicCacheBlock *blockMiss = new BasicCacheBlock(instsMiss, lBlock->id(), basicBlockId, MISS, cat, loop_id);
 				CFTreeLeaf *leafMiss = new CFTreeLeaf(blockMiss);
 
 				std::vector<CFTree *> *alts = new std::vector<CFTree *>();
diff --git a/cache/cache.xml b/cache/cache.xml
index 506f4dd..56c538a 100644
--- a/cache/cache.xml
+++ b/cache/cache.xml
@@ -2,7 +2,7 @@
 <!DOCTYPE processor SYSTEM "/home/casse/otawa/otawa/data/dtd/cache.dtd">
 <cache-config>
 	<icache>
-		<block_bits>7</block_bits>
+		<block_bits>4</block_bits>
 		<row_bits>1</row_bits>
 		<way_bits>2</way_bits>
 		<replace>LRU</replace>
diff --git a/include/CFTree.h b/include/CFTree.h
index 43843c7..e9a712f 100644
--- a/include/CFTree.h
+++ b/include/CFTree.h
@@ -444,14 +444,16 @@ namespace otawa
 			CacheBlocType type;
 			static int penalty;
 			category_t contextCategory;
+			int loop_id;
 
 		public:
-			BasicCacheBlock(const Array<Inst *> &insts, int lBlockId, int basicBlockId, CacheBlocType cacheType, category_t lBlockCategory) : BasicBlock(insts)
+			BasicCacheBlock(const Array<Inst *> &insts, int lBlockId, int basicBlockId, CacheBlocType cacheType, category_t lBlockCategory, int loopId) : BasicBlock(insts)
 			{
 				blockId = blockCount++;
 				lBlock = lBlockId;
 				type = cacheType;
 				contextCategory = lBlockCategory;
+				loop_id = loopId;
 				bbid = basicBlockId;
 				//cout << "\t\tCreated BasicCacheBloc with id : " << blockId << endl;
 			}
@@ -462,6 +464,7 @@ namespace otawa
 			static void setPenalty(int value) {penalty = value;}
 			category_t getContextCategory() const { return contextCategory; }
 			int getBasicBlockId() const{ return bbid;}
+			int getLoopId() const{return loop_id;}
 		};
 
 	} // namespace cftree
-- 
GitLab