GlobalMayAnalysis.cpp 7.05 KB
Newer Older
Valentin Touzeau's avatar
Valentin Touzeau committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 *	icat3::MayAnalysis class implementation
 *	Copyright (c) 2017, IRIT UPS.
 *
 *	This file is part of OTAWA
 *
 *	OTAWA is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	OTAWA is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with OTAWA; if not, write to the Free Software
 *	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 *	02110-1301  USA
 */

#include <chrono>
#include <otawa/ai/ArrayStore.h>
#include <otawa/ai/SimpleAI.h>
#include <otawa/cfg/CompositeCFG.h>
#include <otawa/cfg/features.h>
#include <otawa/icache/features.h>
#include <otawa/icat3/features.h>
EXT Valentin Touzeau's avatar
EXT Valentin Touzeau committed
30
31
#include <lruzdd/features.h>
#include <lruzdd/MayAnalysis/MayManager.h>
Valentin Touzeau's avatar
Valentin Touzeau committed
32
33
34
35
36
37
38
39
40
41
#include "../ZDD/ZDDManager.h"
#include "../ZDD/MayAbstractValuePolicy.h"
#include "../ZDD/ZDDMayDomainPolicy.h"
#include "../GeneratorsSet/GSMayAbstractValuePolicy.h"
#include "../GeneratorsSet/GSMayDomainPolicy.h"

#include "../Domain.h"

using namespace otawa;

EXT Valentin Touzeau's avatar
EXT Valentin Touzeau committed
42
namespace lruzdd
Valentin Touzeau's avatar
Valentin Touzeau committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
{

class GlobalMayAdapter
{
public:
	typedef Domain<ZDDMayDomainPolicy> domain_t;
	//typedef Domain<GSMayDomainPolicy> domain_t;
	typedef typename domain_t::t t;
	typedef CompositeCFG graph_t;
	typedef ai::ArrayStore<domain_t, graph_t> store_t;

	GlobalMayAdapter(const otawa::icat3::LBlock* focus,
	                 int set,
	                 const t* init,
	                 const icat3::LBlockCollection& coll,
	                 const CFGCollection& cfgs,
	                 std::shared_ptr<ZDDManager> manager):
		m_domain(focus, coll, set, init, manager),
		m_graph(cfgs),
		m_store(m_domain, m_graph) { }

	inline domain_t& domain(void) { return m_domain; }
	inline graph_t& graph(void) { return m_graph; }
	inline store_t& store(void) { return m_store; }

	void update(const Bag<icache::Access>& accs, t& d) {
		for(auto acc = *accs; acc; acc++)
			m_domain.update(acc, d);
	}

	void update(Block* v, t& d) {
		// ∀v ∈ V \ {ν}, IN(v) = ⊔{(w, v) ∈ E} 𝕀*(β(w, v), (v, w), IN(w))
		m_domain.copy(d, m_domain.bot());
		t s;

		// update and join along edges
		for(auto e = m_graph.preds(v); e; e++) {
			Block *w = e->source();
			m_domain.copy(s, m_store.get(w));

			// apply block
			{
				const Bag<icache::Access>& accs = icache::ACCESSES(w);
				if(accs.count() > 0)
					update(accs, s);
			}

			// apply edge
			{
				const Bag<icache::Access>& accs = icache::ACCESSES(e);
				if(accs.count() > 0)
					update(accs, s);
			}

			// merge result
			m_domain.join(d, s);
		}
	}

private:
	domain_t m_domain;
	graph_t m_graph;
	store_t m_store;
};


class GlobalMayAnalysis : public Processor
{
public:
	//using domain_t = Domain<ZDDMayDomainPolicy>;
	//using domain_t = Domain<GSMayDomainPolicy>;
	using domain_t = GlobalMayAdapter::domain_t;

	static p::declare reg;
	GlobalMayAnalysis(p::declare& r = reg) :
		Processor(r),
		//m_init_exact_may(nullptr),
		m_coll(nullptr),
		m_cfgs(nullptr)
	{
	}

protected:

	void configure(const PropList& props) override
	{
		Processor::configure(props);
//		if(props.hasProp(EXACT_MAY_INIT))
//			m_init_exact_may = &EXACT_MAY_INIT(props);
	}

	void setup(WorkSpace *ws) override
	{
		m_coll = icat3::LBLOCKS(ws);
		ASSERT(m_coll != nullptr);
		m_cfgs = otawa::INVOLVED_CFGS(ws);
		ASSERT(m_cfgs != nullptr);
//		for(CFGCollection::BlockIter b(m_cfgs); b; b++)
//			EXACT_MAY_IN(b) = Container<YoungerSetAntichain<AntichainType::MAY> >(*m_coll);
	}

	void processWorkSpace(WorkSpace* ws) override
	{
		auto start = std::chrono::system_clock::now();

		for(int i = 0; i < m_coll->cache()->setCount(); i++) {
			if((*m_coll)[i].count()) {
				if(logFor(LOG_FUN))
					log << "\tanalyzing set " << i << io::endl;
				processSet(i, ws);
			}
		}

		auto end = std::chrono::system_clock::now();
Valentin Touzeau's avatar
Valentin Touzeau committed
157
		auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
Valentin Touzeau's avatar
Valentin Touzeau committed
158
		if(logFor(LOG_FUN))
Valentin Touzeau's avatar
Valentin Touzeau committed
159
			log << "\tExact Global May Analysis running time: " << elapsed.count() << " ms" << io::endl;
Valentin Touzeau's avatar
Valentin Touzeau committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250

//		for(int i = 0; i < m_coll->size(); ++i) {
//			const icat3::LBlockSet& s = (*m_coll)[i];
//			for(int j = 0; j < s.size(); ++j)
//				processLBlock(s[j]);
//		}
	}

	void destroy(WorkSpace*) override
	{
//		for(CFGCollection::BlockIter b(m_cfgs); b; b++)
//			EXACT_MAY_IN(b).remove();
	}

private:

	void processSet(int set, WorkSpace* ws)
	{
		const icat3::LBlockSet& lbSet = (*m_coll)[set];
		std::shared_ptr<ZDDManager> manager = std::make_shared<ZDDManager>(lbSet);
		for(int i = 0; i < lbSet.size(); ++i) {
			const icat3::LBlock* lb = lbSet[i];
			processLBlock(lb, manager);
		}
	}

	void processLBlock(const icat3::LBlock* lb, std::shared_ptr<ZDDManager> manager)
	{
		if(logFor(LOG_BLOCK))
			log << "\tAnalyzing Block " << lb->index() << " (" << lb->address() << ")" << io::endl;

		GlobalMayAdapter ada(lb, lb->set(), nullptr, *m_coll, *m_cfgs, manager);
		ai::SimpleAI<GlobalMayAdapter> ana(ada);
		ana.run();

		for(CFGCollection::BlockIter b(m_cfgs); b; b++)
			classifyBlock(lb, ada.domain(), b, ada.store().get(b));

		//ada.store().clear();
	}

	void classifyBlock(const icat3::LBlock* focus,
	                   GlobalMayAdapter::domain_t& d,
	                   Block* b,
	                   const GlobalMayAdapter::t& v)
	{
		MayManager<domain_t> man(d, v);

		Bag<icache::Access>& bAccs = icache::ACCESSES(b).ref();
		GlobalMayAdapter::t save = classifyAccesses(b, focus, man, bAccs);
		for(Block::EdgeIter e = b->outs(); e; e++) {
			Bag<icache::Access>& eAccs = icache::ACCESSES(e).ref();
			man.restart(save);
			classifyAccesses(b, focus, man, eAccs);
		}
	}

	GlobalMayAdapter::t classifyAccesses(const Block* b,
	                               const icat3::LBlock* focus,
	                               MayManager<domain_t>& man,
	                               Bag<icache::Access>& accs)
	{
		for(int i = 0; i < accs.size(); ++i) {
			icat3::LBlock* lb = icat3::LBLOCK(accs[i]);
			if(lb == focus)  {
				if(logFor(LOG_BLOCK)) {
					log << "\t\tFunction " << b->cfg()->label() << ", ";
					log << const_cast<Block*>(b) << ": " << io::endl;
					log << "\t\t\tAccess (" << accs[i] << ") is ";
				}
				if(man.alwaysMiss()) {
					MISS_CATEGORY(accs[i]) = MissCategory::AM;
					if(logFor(LOG_BLOCK))
						log << "AM" << elm::io::endl;
				}
				else {
					MISS_CATEGORY(accs[i]) = MissCategory::NC;
					if(logFor(LOG_BLOCK))
						log << "NC" << elm::io::endl;
				}
			}
			man.update(accs[i]);
		}
		return man.current();
	}

	//const Container<YoungerSetAntichain<AntichainType::MAY> >* m_init_exact_may;
	const icat3::LBlockCollection* m_coll;
	const CFGCollection* m_cfgs;
};

EXT Valentin Touzeau's avatar
EXT Valentin Touzeau committed
251
p::declare GlobalMayAnalysis::reg = p::init("lruzdd::GlobalMayAnalysis", Version(1, 0, 0))
Valentin Touzeau's avatar
Valentin Touzeau committed
252
253
254
255
256
	.require(icat3::LBLOCKS_FEATURE)
	.require(COLLECTED_CFG_FEATURE)
	.provide(EXACT_MAY_ANALYSIS_FEATURE)
	.make<GlobalMayAnalysis>();

EXT Valentin Touzeau's avatar
EXT Valentin Touzeau committed
257
} // namespace lruzdd
Valentin Touzeau's avatar
Valentin Touzeau committed
258