MustAnalysis.cpp 8.6 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
/*
 *	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
 */

Valentin Touzeau's avatar
Valentin Touzeau committed
23
#include <chrono>
Valentin Touzeau's avatar
Valentin Touzeau committed
24
25
26
27
28
29
#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/MustAnalysis/MustManager.h>
32
#include <lruexact/features.h>
Valentin Touzeau's avatar
WIP    
Valentin Touzeau committed
33
34
35
36
#include "../ZDD/MustAbstractValuePolicy.h"
#include "../ZDD/ZDDMustDomainPolicy.h"
#include "../GeneratorsSet/GSMustAbstractValuePolicy.h"
#include "../GeneratorsSet/GSMustDomainPolicy.h"
Valentin Touzeau's avatar
Valentin Touzeau committed
37

38
#include "../Domain.h"
Valentin Touzeau's avatar
WIP    
Valentin Touzeau committed
39

Valentin Touzeau's avatar
Valentin Touzeau committed
40
41
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
{

class MustAdapter
{
public:
Valentin Touzeau's avatar
WIP    
Valentin Touzeau committed
48
49
	typedef Domain<ZDDMustDomainPolicy> domain_t;
	//typedef Domain<GSMustDomainPolicy> domain_t;
Valentin Touzeau's avatar
Valentin Touzeau committed
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
	typedef typename domain_t::t t;
	typedef CompositeCFG graph_t;
	typedef ai::ArrayStore<domain_t, graph_t> store_t;

	MustAdapter(const otawa::icat3::LBlock* focus,
	            int set,
	            const t* init,
	            const icat3::LBlockCollection& coll,
	            const CFGCollection& cfgs):
		m_domain(focus, coll, set, init),
		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 MustAnalysis : public Processor
{
public:
Valentin Touzeau's avatar
WIP    
Valentin Touzeau committed
111
	//using domain_t = Domain<ZDDMustDomainPolicy>;
Valentin Touzeau's avatar
WIP    
Valentin Touzeau committed
112
113
	//using domain_t = Domain<GSMustDomainPolicy>;
	using domain_t = MustAdapter::domain_t;
Valentin Touzeau's avatar
WIP    
Valentin Touzeau committed
114

Valentin Touzeau's avatar
Valentin Touzeau committed
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
	static p::declare reg;
	MustAnalysis(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*) override
	{
Valentin Touzeau's avatar
Valentin Touzeau committed
145
146
		auto start = std::chrono::system_clock::now();

Valentin Touzeau's avatar
WIP    
Valentin Touzeau committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
		std::set<const icat3::LBlock*> toRefine;
		for(CFGCollection::BlockIter b(m_cfgs); b; b++) {
			if(!b->isBasic())
				continue;

			BasicBlock* bb = b->toBasic();

			for(Block::EdgeIter edgeIter(bb->ins()); edgeIter; ++edgeIter) {
				Edge* e = *edgeIter;
				Bag<icache::Access>& bag = icache::ACCESSES(e).ref();
				processBag(toRefine, bag);
			}
			Bag<icache::Access>& bag = icache::ACCESSES(bb).ref();
			processBag(toRefine, bag);
Valentin Touzeau's avatar
Valentin Touzeau committed
161
		}
Valentin Touzeau's avatar
WIP    
Valentin Touzeau committed
162
163
164
165

		for(const icat3::LBlock* lb : toRefine)
			processLBlock(lb);

Valentin Touzeau's avatar
Valentin Touzeau committed
166
167
168
169
170
		auto end = std::chrono::system_clock::now();
		auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(end - start);
		if(logFor(LOG_FUN))
			log << "\tExact Must Analysis running time: " << elapsed.count() << " s" << io::endl;

Valentin Touzeau's avatar
WIP    
Valentin Touzeau committed
171
172
173
174
175
//		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]);
//		}
Valentin Touzeau's avatar
Valentin Touzeau committed
176
177
178
179
180
181
182
183
184
	}

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

private:
Valentin Touzeau's avatar
WIP    
Valentin Touzeau committed
185
186
187
	void processBag(std::set<const icat3::LBlock*>& set, Bag<icache::Access>& bag)
	{
		for(int i = 0; i < bag.size(); ++i) {
188
			lruexact::RefinementCategory refCat = lruexact::REFINEMENT_CATEGORY(bag[i]);
Valentin Touzeau's avatar
WIP    
Valentin Touzeau committed
189
190
			HitCategory e = HitCategory::NC;

191
			if(refCat == lruexact::RefinementCategory::CLASSIFIED &&
Valentin Touzeau's avatar
WIP    
Valentin Touzeau committed
192
193
194
195
196
			   icat3::CATEGORY(bag[i]) == icat3::AH)
					e = HitCategory::AH;

			HIT_CATEGORY(bag[i]) = e;

197
198
			if(refCat == lruexact::RefinementCategory::AH_CANDIDATE ||
			   refCat == lruexact::RefinementCategory::AH_AM_CANDIDATE)
Valentin Touzeau's avatar
WIP    
Valentin Touzeau committed
199
200
201
			set.insert(icat3::LBLOCK(&bag[i]));
		}
	}
Valentin Touzeau's avatar
Valentin Touzeau committed
202
203
204
205
206
207
208
209
210
211
212
213

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

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

		for(CFGCollection::BlockIter b(m_cfgs); b; b++)
			classifyBlock(lb, ada.domain(), b, ada.store().get(b));
Valentin Touzeau's avatar
Valentin Touzeau committed
214
215

		ada.store().clear();
Valentin Touzeau's avatar
Valentin Touzeau committed
216
217
	}

Valentin Touzeau's avatar
WIP    
Valentin Touzeau committed
218
219
220
221
	void classifyBlock(const icat3::LBlock* focus,
	                   MustAdapter::domain_t& d,
	                   Block* b,
	                   const MustAdapter::t& v)
Valentin Touzeau's avatar
Valentin Touzeau committed
222
	{
Valentin Touzeau's avatar
WIP    
Valentin Touzeau committed
223
		MustManager<domain_t> man(d, v);
Valentin Touzeau's avatar
Valentin Touzeau committed
224
225
226
227
228
229
230
231
232
233

		Bag<icache::Access>& bAccs = icache::ACCESSES(b).ref();
		MustAdapter::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);
		}
	}

Valentin Touzeau's avatar
WIP    
Valentin Touzeau committed
234
235
236
237
	MustAdapter::t classifyAccesses(const Block* b,
	                                const icat3::LBlock* focus,
	                                MustManager<domain_t>& man,
	                                Bag<icache::Access>& accs)
Valentin Touzeau's avatar
Valentin Touzeau committed
238
239
240
241
	{
		for(int i = 0; i < accs.size(); ++i) {
			icat3::LBlock* lb = icat3::LBLOCK(accs[i]);
			if(lb == focus)  {
Valentin Touzeau's avatar
Valentin Touzeau committed
242
243
244
245
246
//				if(logFor(LOG_BLOCK)) {
//					log << "\t\tFunction " << b->cfg()->label() << ", ";
//					log << const_cast<Block*>(b) << ": " << io::endl;
//					log << "\t\t\tAccess (" << accs[i] << ") is ";
//				}
Valentin Touzeau's avatar
Valentin Touzeau committed
247
248
				if(man.alwaysHit()) {
					HIT_CATEGORY(accs[i]) = HitCategory::AH;
Valentin Touzeau's avatar
Valentin Touzeau committed
249
250
//					if(logFor(LOG_BLOCK))
//						log << "AH" << elm::io::endl;
Valentin Touzeau's avatar
Valentin Touzeau committed
251
252
253
				}
				else {
					HIT_CATEGORY(accs[i]) = HitCategory::NC;
Valentin Touzeau's avatar
Valentin Touzeau committed
254
255
//					if(logFor(LOG_BLOCK))
//						log << "NC" << elm::io::endl;
Valentin Touzeau's avatar
Valentin Touzeau committed
256
257
258
259
260
261
262
263
264
265
266
267
				}
			}
			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
268
p::declare MustAnalysis::reg = p::init("lruzdd::MustAnalysis", Version(1, 0, 0))
Valentin Touzeau's avatar
Valentin Touzeau committed
269
	.require(icat3::LBLOCKS_FEATURE)
270
	.require(lruexact::REFINEMENT_CATEGORY_FEATURE)
Valentin Touzeau's avatar
Valentin Touzeau committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
	.require(COLLECTED_CFG_FEATURE)
	.provide(EXACT_MUST_ANALYSIS_FEATURE)
	.make<MustAnalysis>();


/**
 * Perform the ACS analysis for the Exact-May domain, that is, computes for each cache
 * block the highest age it may have considering all execution paths.
 *
 * @par Properties
 * @li @ref EXACT_MAY_IN
 *
 * @par Configuraiton
 * @li @ref EXACT_MAY_INIT
 *
 * @par Implementation
 * @li @ref ExactMayAnalysis
 *
EXT Valentin Touzeau's avatar
EXT Valentin Touzeau committed
289
 * @ingroup lruzdd
Valentin Touzeau's avatar
Valentin Touzeau committed
290
 */
EXT Valentin Touzeau's avatar
EXT Valentin Touzeau committed
291
p::feature EXACT_MUST_ANALYSIS_FEATURE("lruzdd::EXACT_MUST_ANALYSIS_FEATURE", p::make<MustAnalysis>());
Valentin Touzeau's avatar
Valentin Touzeau committed
292
293
294
295
296
297
298
299
300
301
302
303


/**
 * ACS for the Exact-May analysis at the entry of the corresponding block or edge.
 *
 * @par Feature
 * @li @ref EXACT_MAY_ANALYSIS_FEATURE
 *
 * @par Hooks
 * @li @ref Block
 * @li @ref Edge
 *
EXT Valentin Touzeau's avatar
EXT Valentin Touzeau committed
304
 * @ingroup lruzdd
Valentin Touzeau's avatar
Valentin Touzeau committed
305
 */
EXT Valentin Touzeau's avatar
EXT Valentin Touzeau committed
306
//p::id<Container<YoungerSetAntichain<AntichainType::MAY> > > EXACT_MAY_IN("lruzdd::EXACT_MAY_IN");
Valentin Touzeau's avatar
Valentin Touzeau committed
307

EXT Valentin Touzeau's avatar
EXT Valentin Touzeau committed
308
p::id<HitCategory> HIT_CATEGORY("lruzdd::HIT_CATEGORY");
Valentin Touzeau's avatar
Valentin Touzeau committed
309
310
311
312
313
314
315
316
317
318

/**
 * Initial state for Exact-May instruction cache analysis.
 *
 * @par Hook
 * @li Feature configuration.
 *
 * @par Feature
 * @li @ref EXACT_MAY_ANALYSIS_FEATURE
 *
EXT Valentin Touzeau's avatar
EXT Valentin Touzeau committed
319
 * @ingroup lruzdd
Valentin Touzeau's avatar
Valentin Touzeau committed
320
 */
EXT Valentin Touzeau's avatar
EXT Valentin Touzeau committed
321
//p::id<Container<YoungerSetAntichain<AntichainType::MAY> > > EXACT_MAY_INIT("lruzdd::EXACT_MAY_INIT");
Valentin Touzeau's avatar
Valentin Touzeau committed
322

EXT Valentin Touzeau's avatar
EXT Valentin Touzeau committed
323
} // namespace lruzdd