source: CLRX/CLRadeonExtender/trunk/amdasm/AsmRegAlloc.h @ 4097

Last change on this file since 4097 was 4097, checked in by matszpk, 14 months ago

CLRadeonExtender: AsmRegAlloc?: Change LastVRegMap def: map of vectors of LastAccessBlockPos? instead sizes.

File size: 10.6 KB
Line 
1/*
2 *  CLRadeonExtender - Unofficial OpenCL Radeon Extensions Library
3 *  Copyright (C) 2014-2018 Mateusz Szpakowski
4 *
5 *  This library is free software; you can redistribute it and/or
6 *  modify it under the terms of the GNU Lesser General Public
7 *  License as published by the Free Software Foundation; either
8 *  version 2.1 of the License, or (at your option) any later version.
9 *
10 *  This library is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 *  Lesser General Public License for more details.
14 *
15 *  You should have received a copy of the GNU Lesser General Public
16 *  License along with this library; if not, write to the Free Software
17 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20#ifndef __CLRX_ASMREGALLOC_H__
21#define __CLRX_ASMREGALLOC_H__
22
23#include <CLRX/Config.h>
24#include <iostream>
25#include <vector>
26#include <utility>
27#include <unordered_set>
28#include <unordered_map>
29#include <map>
30#include <algorithm>
31#include <CLRX/utils/Utilities.h>
32#include <CLRX/utils/Containers.h>
33#include <CLRX/amdasm/Assembler.h>
34#include "AsmInternals.h"
35
36using namespace CLRX;
37
38#define ASMREGALLOC_DEBUGDUMP 0
39
40namespace CLRX
41{
42
43typedef AsmRegAllocator::CodeBlock CodeBlock;
44typedef AsmRegAllocator::NextBlock NextBlock;
45typedef AsmRegAllocator::SSAInfo SSAInfo;
46typedef std::pair<const AsmSingleVReg, SSAInfo> SSAEntry;
47
48//  BlockIndex
49
50struct CLRX_INTERNAL BlockIndex
51{
52    size_t index;
53    size_t pass;
54   
55    BlockIndex(size_t _index = 0, size_t _pass = 0)
56            : index(_index), pass(_pass)
57    { }
58   
59    bool operator==(const BlockIndex& v) const
60    { return index==v.index && pass==v.pass; }
61    bool operator!=(const BlockIndex& v) const
62    { return index!=v.index || pass!=v.pass; }
63   
64    BlockIndex operator+(size_t p) const
65    { return BlockIndex(index+p, pass); }
66};
67
68};
69
70#if ASMREGALLOC_DEBUGDUMP
71extern CLRX_INTERNAL std::ostream& operator<<(std::ostream& os, const CLRX::BlockIndex& v);
72#endif
73
74namespace std
75{
76
77/// std::hash specialization for CLRX CString
78template<>
79struct hash<BlockIndex>
80{
81    typedef BlockIndex argument_type;    ///< argument type
82    typedef std::size_t result_type;    ///< result type
83   
84    /// a calling operator
85    size_t operator()(const BlockIndex& r1) const
86    {
87        std::hash<size_t> h1;
88        return h1(r1.index) ^ h1(r1.pass);
89    }
90};
91
92}
93
94namespace CLRX
95{
96
97class CLRX_INTERNAL CBlockBitPool: public std::vector<bool>
98{
99public:
100    CBlockBitPool(size_t n = 0, bool v = false) : std::vector<bool>(n<<1, v)
101    { }
102   
103    reference operator[](BlockIndex i)
104    { return std::vector<bool>::operator[](i.index + (i.pass ? (size()>>1) : 0)); }
105    const_reference operator[](BlockIndex i) const
106    { return std::vector<bool>::operator[](i.index + (i.pass ? (size()>>1) : 0)); }
107};
108
109/** Simple cache **/
110
111// map of last SSAId for routine, key - varid, value - last SSA ids
112class CLRX_INTERNAL LastSSAIdMap: public
113            std::unordered_map<AsmSingleVReg, VectorSet<size_t> >
114{
115public:
116    LastSSAIdMap()
117    { }
118   
119    iterator insertSSAId(const AsmSingleVReg& vreg, size_t ssaId)
120    {
121        auto res = insert({ vreg, { ssaId } });
122        if (!res.second)
123            res.first->second.insertValue(ssaId);
124        return res.first;
125    }
126   
127    void eraseSSAId(const AsmSingleVReg& vreg, size_t ssaId)
128    {
129        auto it = find(vreg);
130        if (it != end())
131             it->second.eraseValue(ssaId);
132    }
133   
134    size_t weight() const
135    { return size(); }
136};
137
138typedef std::unordered_map<AsmSingleVReg, BlockIndex> SVRegBlockMap;
139
140class CLRX_INTERNAL SVRegMap: public std::unordered_map<AsmSingleVReg, size_t>
141{
142public:
143    SVRegMap()
144    { }
145   
146    size_t weight() const
147    { return size(); }
148};
149
150typedef LastSSAIdMap RBWSSAIdMap;
151typedef std::unordered_map<BlockIndex, VectorSet<BlockIndex> > SubrLoopsMap;
152
153struct CLRX_INTERNAL RetSSAEntry
154{
155    std::vector<BlockIndex> routines;
156    VectorSet<size_t> ssaIds;
157    size_t prevSSAId; // for curSSAId
158};
159
160typedef std::unordered_map<AsmSingleVReg, RetSSAEntry> RetSSAIdMap;
161
162struct CLRX_INTERNAL LoopSSAIdMap
163{
164    LastSSAIdMap ssaIdMap;
165    bool passed;
166};
167
168struct CLRX_INTERNAL RoutineData
169{
170    // rbwSSAIdMap - read before write SSAId's map
171    SVRegMap rbwSSAIdMap;
172    SVRegMap origRbwSSAIdMap;
173    LastSSAIdMap curSSAIdMap;
174    LastSSAIdMap lastSSAIdMap;
175    // key - loop block, value - last ssaId map for loop end
176    std::unordered_map<BlockIndex, LoopSSAIdMap> loopEnds;
177    bool notFirstReturn;
178    bool generated;
179    size_t weight_;
180   
181    RoutineData() : notFirstReturn(false), generated(false), weight_(0)
182    { }
183   
184    void calculateWeight()
185    {
186        weight_ = rbwSSAIdMap.size() + lastSSAIdMap.weight();
187        for (const auto& entry: loopEnds)
188            weight_ += entry.second.ssaIdMap.weight();
189    }
190   
191    size_t weight() const
192    { return weight_; }
193};
194
195struct CLRX_INTERNAL LastAccessBlockPos
196{
197    size_t blockIndex;
198    bool inSubroutines; // true if last access in some called subroutine
199   
200    bool operator==(const LastAccessBlockPos& v) const
201    { return blockIndex==v.blockIndex; }
202    bool operator!=(const LastAccessBlockPos& v) const
203    { return blockIndex!=v.blockIndex; }
204};
205
206typedef std::unordered_map<AsmSingleVReg, VectorSet<LastAccessBlockPos> > LastAccessMap;
207
208// Routine data for createLivenesses - holds svreg read before writes and
209// last access of the svregs
210struct CLRX_INTERNAL RoutineDataLv
211{
212    SVRegMap rbwSSAIdMap;
213    // holds all vreg SSA's used in routine (used while creating call point)
214    // includes subroutines called in this routines
215    std::unordered_set<size_t> allSSAs[MAX_REGTYPES_NUM];
216    // key - svreg, value - list of the last codeblocks where is svreg
217    LastAccessMap lastAccessMap;
218};
219
220// used by createSSAData
221struct CLRX_INTERNAL FlowStackEntry
222{
223    BlockIndex blockIndex;
224    size_t nextIndex;
225    bool isCall;
226    bool haveReturn;
227    RetSSAIdMap prevRetSSAIdSets;
228};
229
230// used by resolveSSAConflicts
231struct CLRX_INTERNAL FlowStackEntry2
232{
233    size_t blockIndex;
234    size_t nextIndex;
235};
236
237
238// used by createLivenesses
239struct CLRX_INTERNAL FlowStackEntry3
240{
241    size_t blockIndex;
242    size_t nextIndex;
243    bool isCall;
244};
245
246struct CLRX_INTERNAL FlowStackEntry4
247{
248    size_t blockIndex;
249    size_t nextIndex;
250    bool isCall;
251    SVRegMap prevCurSVRegMap;
252};
253
254struct CLRX_INTERNAL CallStackEntry
255{
256    BlockIndex callBlock; // index
257    size_t callNextIndex; // index of call next
258    BlockIndex routineBlock;    // routine block
259};
260
261struct CLRX_INTERNAL CallStackEntry2
262{
263    size_t callBlock; // index
264    size_t callNextIndex; // index of call next
265    size_t routineBlock;    // routine block
266};
267
268typedef std::unordered_map<BlockIndex, RoutineData> RoutineMap;
269typedef std::unordered_map<BlockIndex, RoutineDataLv> RoutineLvMap;
270
271typedef std::unordered_map<size_t, std::pair<size_t, size_t> > PrevWaysIndexMap;
272
273class CLRX_INTERNAL ResSecondPointsToCache: public CBlockBitPool
274{
275public:
276    explicit ResSecondPointsToCache(size_t n) : CBlockBitPool(n<<1, false)
277    { }
278   
279    void increase(BlockIndex ip)
280    {
281        const size_t i = ip.index + (ip.pass ? (size()>>2) : 0);
282        if ((*this)[i<<1])
283            (*this)[(i<<1)+1] = true;
284        else
285            (*this)[i<<1] = true;
286    }
287   
288    cxuint count(BlockIndex ip) const
289    {
290        const size_t i = ip.index + (ip.pass ? (size()>>2) : 0);
291        return cxuint((*this)[i<<1]) + (*this)[(i<<1)+1];
292    }
293};
294
295typedef AsmRegAllocator::SSAReplace SSAReplace; // first - orig ssaid, second - dest ssaid
296typedef AsmRegAllocator::SSAReplacesMap SSAReplacesMap;
297
298#if ASMREGALLOC_DEBUGDUMP
299#define ARDOut std::cout
300#else
301struct NoOutput
302{
303    template<typename T>
304    NoOutput& operator<<(const T& v)
305    { return *this; }
306};
307
308#define ARDOut NoOutput()
309#endif
310
311struct Liveness
312{
313    std::map<size_t, size_t> l;
314   
315    Liveness() { }
316   
317    void clear()
318    { l.clear(); }
319   
320    void join(std::map<size_t, size_t>::iterator it)
321    {
322        if (it != l.begin())
323        {
324            auto prevIt = it;
325            --prevIt;
326            if (prevIt->second >= it->first)
327            {
328                // join with previous region
329                prevIt->second = std::max(it->second, prevIt->second);
330                l.erase(it);
331                it = prevIt;
332            }
333        }
334       
335        auto nextIt = it;
336        ++nextIt;
337        if (nextIt != l.end())
338        {
339            if (nextIt->first <= it->second)
340            {
341                // join with next region
342                it->second = std::max(it->second, nextIt->second);
343                l.erase(nextIt);
344            }
345        }
346    }
347   
348    void expand(size_t k)
349    {
350        auto it = l.upper_bound(k);
351        if (it != l.begin())
352            --it;
353        else // do nothing
354            return;
355        // try expand, lower new bound, then use older
356        it->second = std::max(it->second, k+1);
357        join(it);
358    }
359    void newRegion(size_t k)
360    { join(l.insert(std::make_pair(k, k)).first); }
361   
362    void insert(size_t k, size_t k2)
363    {
364        auto res = l.insert(std::make_pair(k, k2));
365        if (!res.second)
366            res.first->second = std::max(res.first->second, k2);
367        join(res.first);
368    }
369   
370    bool contain(size_t t) const
371    {
372        auto it = l.lower_bound(t);
373        if (it==l.begin() && it->first>t)
374            return false;
375        if (it==l.end() || it->first>t)
376            --it;
377        return it->first<=t && t<it->second;
378    }
379};
380
381typedef AsmRegAllocator::VarIndexMap VarIndexMap;
382
383typedef std::deque<FlowStackEntry3>::const_iterator FlowStackCIter;
384
385// key - singlevreg, value - code block chain
386typedef std::unordered_map<AsmSingleVReg, std::vector<LastAccessBlockPos> > LastVRegMap;
387
388struct CLRX_INTERNAL LiveBlock
389{
390    size_t start;
391    size_t end;
392    size_t vidx;
393   
394    bool operator==(const LiveBlock& b) const
395    { return start==b.start && end==b.end && vidx==b.vidx; }
396   
397    bool operator<(const LiveBlock& b) const
398    { return start<b.start || (start==b.start &&
399            (end<b.end || (end==b.end && vidx<b.vidx))); }
400};
401
402typedef AsmRegAllocator::LinearDep LinearDep;
403typedef std::unordered_map<size_t, LinearDep> LinearDepMap;
404
405typedef AsmRegAllocator::InterGraph InterGraph;
406
407struct CLRX_INTERNAL SDOLDOCompare
408{
409    const InterGraph& interGraph;
410    const Array<size_t>& sdoCounts;
411   
412    SDOLDOCompare(const InterGraph& _interGraph, const Array<size_t>&_sdoCounts)
413        : interGraph(_interGraph), sdoCounts(_sdoCounts)
414    { }
415   
416    bool operator()(size_t a, size_t b) const
417    {
418        if (sdoCounts[a] > sdoCounts[b])
419            return true;
420        return interGraph[a].size() > interGraph[b].size();
421    }
422};
423
424};
425
426#endif
Note: See TracBrowser for help on using the repository browser.