2022-04-19 10:56:42 +02:00
|
|
|
|
|
|
|
#ifndef ABSTRACHTCACHESTATE_H
|
|
|
|
#define ABSTRACHTCACHESTATE_H
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
|
|
|
#include <ostream>
|
|
|
|
#include <utility>
|
|
|
|
|
2022-04-22 10:35:30 +02:00
|
|
|
#include <llvm/IR/BasicBlock.h>
|
|
|
|
#include <llvm/Support/raw_ostream.h>
|
|
|
|
|
2022-04-19 10:56:42 +02:00
|
|
|
#include "AbstractState.h"
|
|
|
|
#include "Address.h"
|
|
|
|
#include "ConcreteState.h"
|
|
|
|
|
|
|
|
// Forward declarations
|
|
|
|
|
|
|
|
namespace cacheAnaPass {
|
|
|
|
class AbstractCache;
|
|
|
|
|
|
|
|
} // namespace cacheAnaPass
|
|
|
|
|
|
|
|
class AbstractCache {
|
|
|
|
public: // everything is public, because IDGAF
|
|
|
|
// map keys are instruction Addresses.
|
|
|
|
std::map<unsigned int, std::list<unsigned int>> Edges;
|
|
|
|
std::map<unsigned int, AbstractState> Nodes;
|
|
|
|
|
|
|
|
AbstractCache() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Add an Edge to the Abstract Cache
|
|
|
|
*
|
|
|
|
* @param Pre Predecessor Address
|
|
|
|
* @param Suc Successor Address
|
|
|
|
*/
|
|
|
|
void addEdge(unsigned int Pre, unsigned int Suc) {
|
|
|
|
Edges[Pre].push_back(Suc);
|
|
|
|
Nodes[Pre].Successors.push_back(Suc);
|
|
|
|
Nodes[Suc].Predecessors.push_back(Pre);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addEmptyNode(unsigned int NodeAddr) {
|
|
|
|
Nodes[NodeAddr] = AbstractState(NodeAddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fillAbstractCache(unsigned int NodeNr) {
|
2022-04-22 10:35:30 +02:00
|
|
|
Nodes[NodeNr].Computed = true;
|
2022-04-19 10:56:42 +02:00
|
|
|
for (unsigned int SuccNr : Nodes[NodeNr].Successors) {
|
|
|
|
Nodes[SuccNr];
|
2022-04-22 10:35:30 +02:00
|
|
|
if (Nodes[SuccNr].Computed) {
|
2022-04-19 10:56:42 +02:00
|
|
|
// Join don't call
|
|
|
|
Nodes[SuccNr].mustJoin(Nodes[NodeNr]);
|
|
|
|
Nodes[SuccNr].mustJoin(AbstractState(NodeNr));
|
|
|
|
} else {
|
|
|
|
// Update and fill Succ
|
|
|
|
Nodes[SuccNr].fill(Nodes[NodeNr], NodeNr);
|
|
|
|
fillAbstractCache(SuccNr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int collectHits() {
|
|
|
|
unsigned int Hits = 0;
|
|
|
|
for (auto const &E : Edges) {
|
2022-04-22 10:35:30 +02:00
|
|
|
auto Predecessor = Nodes[E.first];
|
2022-04-19 10:56:42 +02:00
|
|
|
for (unsigned int SuccessorAddr : E.second) {
|
|
|
|
// When successors Address is in predecessor, we have a Hit.
|
2022-04-22 10:35:30 +02:00
|
|
|
Hits += Predecessor.isHit(Address(SuccessorAddr)) ? 1 : 0;
|
2022-04-19 10:56:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Hits;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int collectMisses() {
|
|
|
|
unsigned int Misses = 0;
|
|
|
|
for (auto const &E : Edges) {
|
2022-04-22 10:35:30 +02:00
|
|
|
auto Predecessor = Nodes[E.first];
|
2022-04-19 10:56:42 +02:00
|
|
|
for (unsigned int SuccessorAddr : E.second) {
|
|
|
|
// When successors Address is in predecessor, we have a Hit.
|
2022-04-22 10:35:30 +02:00
|
|
|
Misses += Predecessor.isHit(Address(SuccessorAddr)) ? 0 : 1;
|
2022-04-19 10:56:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Misses;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dumpEdges() {
|
|
|
|
llvm::outs() << "Dumping Edges:\n";
|
|
|
|
for (auto const &E : Edges) {
|
|
|
|
llvm::outs() << E.first;
|
|
|
|
bool FirstPrint = true;
|
|
|
|
for (unsigned int To : E.second) {
|
|
|
|
if (FirstPrint) {
|
|
|
|
llvm::outs() << " -> " << To;
|
|
|
|
FirstPrint = false;
|
|
|
|
} else {
|
|
|
|
llvm::outs() << ", " << To;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm::outs() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void dumpDotFile() {
|
|
|
|
std::ofstream DotFile;
|
|
|
|
DotFile.open("out.dot");
|
|
|
|
DotFile << "digraph g {"
|
|
|
|
<< "\n";
|
|
|
|
for (auto const &E : Edges) {
|
|
|
|
for (unsigned int To : E.second) {
|
|
|
|
DotFile << E.first << " -> " << To << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DotFile << "}\n";
|
|
|
|
DotFile.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void dumpNodes() {
|
|
|
|
for (auto const &E : Edges) {
|
|
|
|
Nodes[E.first].dump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}; // namespace
|
|
|
|
#endif // ABSTRACHTCACHESTATE_H
|