-
Notifications
You must be signed in to change notification settings - Fork 392
/
UseDefInfo.hpp
436 lines (366 loc) · 17.8 KB
/
UseDefInfo.hpp
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
30
31
32
33
34
35
36
37
38
39
40
41
42
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
157
158
159
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*******************************************************************************
* Copyright (c) 2000, 2021 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at http://eclipse.org/legal/epl-2.0
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#ifndef USEDEFINFO_INCL
#define USEDEFINFO_INCL
#include <stddef.h>
#include <stdint.h>
#include "compile/Compilation.hpp"
#include "cs2/bitvectr.h"
#include "cs2/sparsrbit.h"
#include "env/TRMemory.hpp"
#include "il/Node.hpp"
#include "il/Symbol.hpp"
#include "il/SymbolReference.hpp"
#include "infra/Assert.hpp"
#include "infra/deque.hpp"
#include "infra/vector.hpp"
#include "infra/TRlist.hpp"
class TR_ReachingDefinitions;
class TR_ValueNumberInfo;
namespace TR { class Block; }
namespace TR { class CFG; }
namespace TR { class ILOpCode; }
namespace TR { class Optimizer; }
namespace TR { class TreeTop; }
namespace OMR { class Optimizer; }
/**
* Use/def information.
*
* Each tree node that can use or define a value is assigned an index value.
* The initial index numbers are reserved for the initial definitions of
* parameters and fields.
*
* The index values are partitioned into 3 parts:
* 0-X = nodes that can define values
* (X+1)-Y = nodes that can both define and use values
* (Y+1)-Z = nodes that can use value
*
* This means that there are a total of Z index values.
* The bit vectors that hold use information are of size (Z-X).
*/
class TR_UseDefInfo
{
TR::Region _region;
public:
static void *operator new(size_t size, TR::Allocator a)
{ return a.allocate(size); }
static void operator delete(void *ptr, TR::Allocator a)
{
// If there is an exception thrown during construction, the compilation
// will be aborted, and all memory associated with that compilation will get freed.
}
static void operator delete(void *ptr, size_t size)
{ ((TR_UseDefInfo*)ptr)->allocator().deallocate(ptr, size); } /* t->allocator() better return the same allocator as used for new */
/* Virtual destructor is necessary for the above delete operator to work
* See "Modern C++ Design" section 4.7
*/
virtual ~TR_UseDefInfo() {}
// Construct use def info for the current method's trees. This also assigns
// use/def index values to the relevant nodes.
//
#if defined(NEW_USEDEF_INFO_SPARSE_INTERFACE)
typedef TR::SparseBitVector BitVector;
#else
typedef TR::BitVector BitVector;
#endif
/**
* Data that can be freed when TR_UseDefInfo is no longer needed
* Data that used to be stack allocated
*/
class AuxiliaryData
{
private:
AuxiliaryData(int32_t numSymRefs, ncount_t nodeCount, TR::Region ®ion, TR::Allocator allocator) :
_region(region),
_onceReadSymbols(numSymRefs, static_cast<TR_BitVector*>(NULL), _region),
_onceWrittenSymbols(numSymRefs, static_cast<TR_BitVector*>(NULL), _region),
_defsForSymbol(0, static_cast<TR_BitVector*>(NULL), _region),
_neverReadSymbols(numSymRefs, _region),
_neverReferencedSymbols(numSymRefs, _region),
_neverWrittenSymbols(numSymRefs, _region),
_volatileOrAliasedToVolatileSymbols(numSymRefs, _region),
_onceWrittenSymbolsIndices(numSymRefs, TR::SparseBitVector(allocator), _region),
_onceReadSymbolsIndices(numSymRefs, TR::SparseBitVector(allocator), _region),
_expandedAtoms(0, std::make_pair<TR::Node *, TR::TreeTop *>(NULL, NULL), _region),
_sideTableToUseDefMap(_region),
_numAliases(numSymRefs, _region),
_loadsBySymRefNum(numSymRefs, _region),
_defsForOSR(0, static_cast<TR_BitVector*>(NULL), _region),
_workBitVector(_region),
_doneTrivialNode(_region),
_isTrivialNode(_region)
{}
TR::Region _region;
// BitVector for temporary work. Used in buildUseDefs.
TR_BitVector _workBitVector;
// Cache results from isTrivialNode
TR_BitVector _doneTrivialNode;
TR_BitVector _isTrivialNode;
TR::vector<TR_BitVector *, TR::Region&> _onceReadSymbols;
TR::vector<TR_BitVector *, TR::Region&> _onceWrittenSymbols;
// defsForSymbol are known definitions of the symbol
TR::vector<TR_BitVector *, TR::Region&> _defsForSymbol;
TR_BitVector _neverReadSymbols;
TR_BitVector _neverReferencedSymbols;
TR_BitVector _neverWrittenSymbols;
TR_BitVector _volatileOrAliasedToVolatileSymbols;
TR::vector<TR::SparseBitVector, TR::Region&> _onceWrittenSymbolsIndices;
TR::vector<TR::SparseBitVector, TR::Region&> _onceReadSymbolsIndices;
TR::vector<std::pair<TR::Node *, TR::TreeTop *>, TR::Region&> _expandedAtoms;
protected:
TR::deque<uint32_t, TR::Region&> _sideTableToUseDefMap;
private:
TR::deque<uint32_t, TR::Region&> _numAliases;
TR::deque<TR::Node *, TR::Region&> _loadsBySymRefNum;
protected:
// used only in TR_OSRDefInfo - should extend AuxiliaryData really:
TR::vector<TR_BitVector *, TR::Region&> _defsForOSR;
friend class TR_UseDefInfo;
friend class TR_ReachingDefinitions;
friend class TR_OSRDefInfo;
};
friend class OMR::Optimizer;
protected:
/**
* Constructs TR_UseDefInfo instance. Note that this should not be called directly.
* Instead construction is handled by OMR::Optimizer::createUseDefInfo() method.
*
* @param cfg The compilation instance
* @param requiresGlobals
* @param prefersGlobals
* @param loadsShouldBeDefs
* @param cannotOmitTrivialDefs
* @param conversionRegsOnly
* @param doCompletion
* @param callsShouldBeUses Enables inclusion of calls as uses so that the alias analysis can detect
* when local (stack) variable has been aliased by a function call.
* A value of false is fine for Java like languages where
* local (stack) variables cannot be passed by reference to function calls
* and hence cannot be aliased. However for C like languages this flag should be
* set to true.
*/
TR_UseDefInfo(TR::Compilation *, TR::CFG * cfg, TR::Optimizer *,
bool requiresGlobals = true, bool prefersGlobals = true, bool loadsShouldBeDefs = true, bool cannotOmitTrivialDefs = false,
bool conversionRegsOnly = false, bool doCompletion = true, bool callsShouldBeUses = false);
void prepareUseDefInfo(bool requiresGlobals, bool prefersGlobals, bool cannotOmitTrivialDefs, bool conversionRegsOnly);
void invalidateUseDefInfo();
virtual bool performAnalysis(AuxiliaryData &aux);
virtual void processReachingDefinition(void* vblockInfo, AuxiliaryData &aux);
private:
bool _runReachingDefinitions(TR_ReachingDefinitions &reachingDefinitions, AuxiliaryData &aux);
protected:
TR::Compilation *comp() { return _compilation; }
TR::Optimizer *optimizer() { return _optimizer; }
TR_Memory * trMemory() { TR_ASSERT(false, "trMemory is prohibited in TR_UseDefInfo. Use comp->allocator()"); return NULL; }
TR_StackMemory trStackMemory() { TR_ASSERT(false, "trMemory is prohibited in TR_UseDefInfo. Use comp->allocator()"); return NULL; }
TR_HeapMemory trHeapMemory() { TR_ASSERT(false, "trMemory is prohibited in TR_UseDefInfo. Use comp->allocator()"); return NULL; }
bool trace() {return _trace;}
public:
TR::Allocator allocator() { return comp()->allocator(); }
bool infoIsValid() {return _isUseDefInfoValid;}
TR::Node *getNode(int32_t index);
TR::TreeTop *getTreeTop(int32_t index);
void clearNode(int32_t index) { _useDefs[index] = TR_UseDef(); }
bool getUsesFromDefIsZero(int32_t defIndex, bool loadAsDef = false);
bool getUsesFromDef(BitVector &usesFromDef, int32_t defIndex, bool loadAsDef = false);
private:
const BitVector &getUsesFromDef_ref(int32_t defIndex, bool loadAsDef = false);
public:
bool getUseDefIsZero(int32_t useIndex);
bool getUseDef(BitVector &useDef, int32_t useIndex);
bool getUseDef_noExpansion(BitVector &useDef, int32_t useIndex);
private:
const BitVector &getUseDef_ref(int32_t useIndex, BitVector *defs = NULL);
const BitVector &getUseDef_ref_body(int32_t useIndex, TR_BitVector *visitedDefs, TR_UseDefInfo::BitVector *defs = NULL);
public:
void setUseDef(int32_t useIndex, int32_t defIndex);
void resetUseDef(int32_t useIndex, int32_t defIndex);
void clearUseDef(int32_t useIndex);
private:
bool isLoadAddrUse(TR::Node * node);
public:
bool getDefiningLoads(BitVector &definingLoads, TR::Node *node);
private:
const BitVector &getDefiningLoads_ref(TR::Node *node);
public:
TR::Node *getSingleDefiningLoad(TR::Node *node);
void resetDefUseInfo() {_defUseInfo.clear();}
bool skipAnalyzingForCompileTime(TR::Node *node, TR::Block *block, TR::Compilation *comp, AuxiliaryData &aux);
private:
void findTrivialSymbolsToExclude(TR::Node *node, TR::TreeTop *treeTop, AuxiliaryData &aux);
bool isTrivialUseDefNode(TR::Node *node, AuxiliaryData &aux);
bool isTrivialUseDefNodeImpl(TR::Node *node, AuxiliaryData &aux);
bool isTrivialUseDefSymRef(TR::SymbolReference *symRef, AuxiliaryData &aux);
// For Languages where an auto can alias a volatile, extra care needs to be taken when setting up use-def
// The conservative answer is to not index autos that have volatile aliases.
void setVolatileSymbolsIndexAndRecurse(TR::BitVector &volatileSymbols, int32_t symRefNum);
void findAndPopulateVolatileSymbolsIndex(TR::BitVector &volatileSymbols);
bool shouldIndexVolatileSym(TR::SymbolReference *ref, AuxiliaryData &aux);
public:
int32_t getNumIrrelevantStores() { return _numIrrelevantStores; }
int32_t getNumUseNodes() {return _numUseOnlyNodes+_numDefUseNodes;}
int32_t getNumDefNodes() {return _numDefOnlyNodes+_numDefUseNodes;}
int32_t getNumDefOnlyNodes() {return _numDefOnlyNodes;}
int32_t getNumDefsOnEntry() { return (_uniqueIndexForDefsOnEntry ? _numDefsOnEntry : 1); }
int32_t getTotalNodes() {return _numDefOnlyNodes+_numDefUseNodes+_numUseOnlyNodes;}
int32_t getFirstUseIndex() {return _numDefOnlyNodes;}
int32_t getLastUseIndex() {return getTotalNodes()-1;}
int32_t getFirstDefIndex() {return 0;}
int32_t getFirstRealDefIndex() { return getFirstDefIndex()+getNumDefsOnEntry();}
int32_t getLastDefIndex() {return getNumDefNodes()-1;}
bool isDefIndex(uint32_t index) { return index && index <= getLastDefIndex(); }
bool isUseIndex(uint32_t index) { return index >= getFirstUseIndex() && index <= getLastUseIndex(); }
bool hasGlobalsUseDefs() {return _indexFields && _indexStatics;}
bool canComputeReachingDefs();
// Information used by the reaching def analysis
//
int32_t getNumExpandedUseNodes()
{return _numExpandedUseOnlyNodes+_numExpandedDefUseNodes;}
int32_t getNumExpandedDefNodes()
{return _numExpandedDefOnlyNodes+_numExpandedDefUseNodes;}
int32_t getNumExpandedDefsOnEntry() {return _numDefsOnEntry;}
int32_t getExpandedTotalNodes()
{return _numExpandedDefOnlyNodes+_numExpandedDefUseNodes+_numExpandedUseOnlyNodes;}
uint32_t getNumAliases (TR::SymbolReference * symRef, AuxiliaryData &aux)
{return aux._numAliases[symRef->getReferenceNumber()];}
protected:
uint32_t getBitVectorSize() {return getNumExpandedDefNodes(); }
private:
bool excludedGlobals(TR::Symbol *);
bool isValidAutoOrParm(TR::SymbolReference *);
public:
bool isExpandedDefIndex(uint32_t index)
{ return index && index < getNumExpandedDefNodes(); }
bool isExpandedUseIndex(uint32_t index)
{ return index >= _numExpandedDefOnlyNodes && index < getExpandedTotalNodes(); }
bool isExpandedUseDefIndex(uint32_t index)
{ return index >= _numExpandedDefOnlyNodes && index < getNumExpandedDefNodes(); }
bool getDefsForSymbol(TR_BitVector &defs, int32_t symIndex, AuxiliaryData &aux)
{
defs |= *(aux._defsForSymbol[symIndex]);
return !defs.isEmpty();
}
bool getDefsForSymbolIsZero(int32_t symIndex, AuxiliaryData &aux)
{
return aux._defsForSymbol[symIndex]->isEmpty();
}
bool hasLoadsAsDefs() { return _hasLoadsAsDefs; }
private:
void dereferenceDefs(int32_t useIndex, BitVector &nodesLookedAt, BitVector &loadDefs);
public:
void dereferenceDef(BitVector &useDefInfo, int32_t defIndex, BitVector &nodesLookedAt);
void buildDefUseInfo(bool loadAsDef = false);
int32_t getSymRefIndexFromUseDefIndex(int32_t udIndex);
public:
int32_t getNumSymbols() { return _numSymbols; }
int32_t getMemorySymbolIndex (TR::Node *);
bool isPreciseDef(TR::Node *def, TR::Node *use);
bool isChildUse(TR::Node *node, int32_t childIndex);
public:
bool _useDefForRegs;
private:
bool _useDefForMemorySymbols;
void buildValueNumbersToMemorySymbolsMap();
void findMemorySymbols(TR::Node *node);
void fillInDataStructures(AuxiliaryData &aux);
bool indexSymbolsAndNodes(AuxiliaryData &aux);
bool findUseDefNodes(
TR::Block *block,
TR::Node *node,
TR::Node *parent,
TR::TreeTop *treeTop,
AuxiliaryData &aux,
TR::deque<uint32_t, TR::Region&> &symRefToLocalIndexMap,
bool considerImplicitStores = false
);
bool assignAdjustedNodeIndex(TR::Block *, TR::Node *node, TR::Node *parent, TR::TreeTop *treeTop, AuxiliaryData &aux, bool considerImplicitStores = false);
bool childIndexIndicatesImplicitStore(TR::Node * node, int32_t childIndex);
void insertData(TR::Block *, TR::Node *node, TR::Node *parent, TR::TreeTop *treeTop, AuxiliaryData &aux, TR::SparseBitVector &, bool considerImplicitStores = false);
void buildUseDefs(void *vblockInfo, AuxiliaryData &aux);
void buildUseDefs(TR::Node *node, void *vanalysisInfo, TR_BitVector &nodesToBeDereferenced, TR::Node *parent, AuxiliaryData &aux);
int32_t setSingleDefiningLoad(int32_t useIndex, BitVector &nodesLookedAt, BitVector &loadDefs);
protected:
TR::Compilation * _compilation;
TR::Optimizer * _optimizer;
private:
TR::vector<std::pair<TR::Node *, TR::TreeTop *>, TR::Region&> _atoms;
private:
TR::vector<BitVector, TR::Region&> _useDefInfo;
bool _isUseDefInfoValid;
TR::list<BitVector, TR::Region&> _infoCache; ///< initially empty bit vectors that are used for caching
const BitVector _EMPTY; ///< the empty bit vector
TR::vector<const BitVector *,TR::Region&> _useDerefDefInfo; ///< all load defs are dereferenced
TR::vector<BitVector, TR::Region&> _defUseInfo;
TR::vector<BitVector, TR::Region&> _loadDefUseInfo;
TR::vector<int32_t, TR::Region&> _sideTableToSymRefNumMap;
// Checklist used in getUseDef_ref
TR_BitVector *_defsChecklist;
int32_t _numDefOnlyNodes;
int32_t _numDefUseNodes;
int32_t _numUseOnlyNodes;
int32_t _numIrrelevantStores;
int32_t _numExpandedDefOnlyNodes;
int32_t _numExpandedDefUseNodes;
int32_t _numExpandedUseOnlyNodes;
int32_t _numDefsOnEntry;
int32_t _numSymbols;
int32_t _numNonTrivialSymbols;
int32_t _numStaticsAndFields;
bool _indexFields;
bool _indexStatics;
bool _tempsOnly;
bool _trace;
bool _hasLoadsAsDefs;
bool _hasCallsAsUses;
bool _uniqueIndexForDefsOnEntry;
class TR_UseDef
{
public:
TR_UseDef() :_useDef(NULL) {}
TR_UseDef(TR::TreeTop *tt) : _def((TR::TreeTop *)((intptr_t)tt|1)) {}
TR_UseDef(TR::Node *node) : _useDef(node) {}
bool isDef() { return ((intptr_t)_def & 1) != 0; }
TR::TreeTop *getDef() { TR_ASSERT( isDef(), "assertion failure"); return (TR::TreeTop*)((intptr_t)_def & ~1); }
TR::Node *getUseDef() { TR_ASSERT(!isDef(), "assertion failure"); return _useDef; }
private:
/** Tagged bottom bit means def */
union
{
TR::Node *_useDef;
TR::TreeTop *_def;
};
};
TR::vector<TR_UseDef, TR::Region &> _useDefs;
class MemorySymbol
{
uint32_t _size;
uint32_t _offset;
int32_t _localIndex;
MemorySymbol(uint32_t size, uint32_t offset, int32_t localIndex) : _size(size), _offset(offset), _localIndex(localIndex) {}
friend class TR_UseDefInfo;
};
typedef TR::list<MemorySymbol, TR::Region&> MemorySymbolList;
int32_t _numMemorySymbols;
TR::vector<MemorySymbolList *, TR::Region&> _valueNumbersToMemorySymbolsMap;
TR_ValueNumberInfo *_valueNumberInfo;
TR::CFG *_cfg;
};
#endif