-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathOMRTransformUtil.cpp
377 lines (330 loc) · 11.4 KB
/
OMRTransformUtil.cpp
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
/*******************************************************************************
*
* (c) Copyright IBM Corp. 2000, 2016
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0 and
* Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* Contributors:
* Multiple authors (IBM Corp.) - initial implementation and documentation
*******************************************************************************/
#include "optimizer/TransformUtil.hpp"
#include "compile/Compilation.hpp"
#include "codegen/CodeGenerator.hpp"
#include "il/Node.hpp"
#include "il/Node_inlines.hpp"
#include "il/SymbolReference.hpp"
#include "il/TreeTop.hpp"
#include "il/DataTypes.hpp"
#include "infra/Assert.hpp"
#include "env/CompilerEnv.hpp"
#define OPT_DETAILS "O^O TRANSFORMUTIL: "
TR::TransformUtil *
OMR::TransformUtil::self()
{
return static_cast<TR::TransformUtil *>(this);
}
TR::Node *
OMR::TransformUtil::scalarizeArrayCopy(
TR::Compilation *comp,
TR::Node *node,
TR::TreeTop *tt,
bool useElementType,
bool &didTransformArrayCopyNode,
TR::SymbolReference *sourceRef,
TR::SymbolReference *targetRef,
bool castToIntegral)
{
TR::CodeGenerator *cg = comp->cg();
didTransformArrayCopyNode = false;
if ((comp->getOptLevel() == noOpt) ||
!comp->getOptions()->getOption(TR_ScalarizeSSOps) ||
node->getOpCodeValue() != TR::arraycopy ||
node->getNumChildren() != 3 ||
comp->requiresSpineChecks() ||
!node->getChild(2)->getOpCode().isLoadConst() ||
cg->getOptimizationPhaseIsComplete())
return node;
int64_t byteLen = node->getChild(2)->get64bitIntegralValue();
if (byteLen == 0)
{
if (tt)
{
// Anchor the first two children
if (!node->getFirstChild()->safeToDoRecursiveDecrement())
TR::TreeTop::create(comp, tt->getPrevTreeTop(),
TR::Node::create(TR::treetop, 1, node->getFirstChild()));
if (!node->getSecondChild()->safeToDoRecursiveDecrement())
TR::TreeTop::create(comp, tt->getPrevTreeTop(),
TR::Node::create(TR::treetop, 1, node->getSecondChild()));
tt->getPrevTreeTop()->join(tt->getNextTreeTop());
tt->getNode()->recursivelyDecReferenceCount();
didTransformArrayCopyNode = true;
}
return node;
}
else if (byteLen < 0)
{
return node;
}
else if (byteLen > TR_MAX_OTYPE_SIZE)
{
return node;
}
TR::DataTypes dataType = TR::Aggregate;
// Get the element datatype from the (hidden) 4th child
TR::DataTypes elementType = node->getArrayCopyElementType();
int32_t elementSize = TR::Symbol::convertTypeToSize(elementType);
if (byteLen == elementSize)
{
dataType = elementType;
}
else if (!useElementType)
{
switch (byteLen)
{
case 1: dataType = TR::Int8; break;
case 2: dataType = TR::Int16; break;
case 4: dataType = TR::Int32; break;
case 8: dataType = TR::Int64; break;
}
}
else
{
return node;
}
// load/store double on 64-bit PPC requires offset to be word aligned
// abort if this requirement is not met.
// TODO: also need to check if the first two children are aload nodes
bool cannot_use_load_store_long = false;
if (TR::Compiler->target.cpu.isPower())
if (dataType == TR::Int64 && TR::Compiler->target.is64Bit())
{
TR::Node * firstChild = node->getFirstChild();
if (firstChild->getNumChildren() == 2)
{
TR::Node *offsetChild = firstChild->getSecondChild();
TR_ASSERT(offsetChild->getOpCodeValue() != TR::iconst, "iconst shouldn't be used for 64-bit array indexing");
if (offsetChild->getOpCodeValue() == TR::lconst)
{
if ((offsetChild->getLongInt() & 0x3) != 0)
cannot_use_load_store_long = true;
}
}
TR::Node *secondChild = node->getSecondChild();
if (secondChild->getNumChildren() == 2)
{
TR::Node *offsetChild = secondChild->getSecondChild();
TR_ASSERT(offsetChild->getOpCodeValue() != TR::iconst, "iconst shouldn't be used for 64-bit array indexing");
if (offsetChild->getOpCodeValue() == TR::lconst)
{
if ((offsetChild->getLongInt() & 0x3) != 0)
cannot_use_load_store_long = true;
}
}
}
if (cannot_use_load_store_long) return node;
TR::SymbolReference *nodeRef;
targetRef = comp->getSymRefTab()->findOrCreateGenericIntShadowSymbolReference(0);
sourceRef = targetRef;
bool trace = comp->getOption(TR_TraceScalarizeSSOps);
if (trace)
traceMsg(comp,"scalarizeArrayCopy: node %p got targetRef (#%d) and sourceRef (#%d)\n",
node,targetRef?targetRef->getReferenceNumber():-1,sourceRef?sourceRef->getReferenceNumber():-1);
if (targetRef == NULL || sourceRef == NULL)
{
if (trace)
traceMsg(comp,"do not scalarizeArrayCopy node %p : targetRef is NULL (%s) or sourceRef is NULL (%s)\n",node,targetRef?"no":"yes",sourceRef?"no":"yes");
return node;
}
#ifdef J9_PROJECT_SPECIFIC
if (isBCDType(targetRef->getSymbol()->getDataType()) ||
isBCDType(sourceRef->getSymbol()->getDataType()))
{
return node;
}
#endif
if (performTransformation(comp, "%sScalarize arraycopy 0x%p\n", OPT_DETAILS, node))
{
TR::Node *store = TR::TransformUtil::scalarizeAddressParameter(comp, node->getSecondChild(), byteLen, dataType, targetRef, true);
TR::Node *load = TR::TransformUtil::scalarizeAddressParameter(comp, node->getFirstChild(), byteLen, dataType, sourceRef, false);
if (tt)
{
// Transforming
// treetop
// arrayCopy <-- node
// into
// *store
//
node->recursivelyDecReferenceCount();
tt->setNode(node);
}
else
{
for (int16_t c = node->getNumChildren() - 1; c >= 0; c--)
cg->recursivelyDecReferenceCount(node->getChild(c));
}
TR::Node::recreateAndCopyValidProperties(node, store->getOpCodeValue());
node->setSymbolReference(store->getSymbolReference());
if (store->getOpCode().isStoreIndirect())
{
node->setChild(0, store->getFirstChild());
node->setAndIncChild(1, load);
node->setNumChildren(2);
}
else
{
node->setAndIncChild(0, load);
node->setNumChildren(1);
}
didTransformArrayCopyNode = true;
}
return node;
}
TR::Node *
OMR::TransformUtil::scalarizeAddressParameter(
TR::Compilation *comp,
TR::Node *address,
size_t byteLengthOrPrecision, // precision for BCD types and byteLength for all other types
TR::DataTypes dataType,
TR::SymbolReference *ref,
bool store)
{
TR_ASSERT(ref,"symRef should not be NULL in scalarizeAddressParameter for address node %p\n",address);
TR::Node * loadOrStore = NULL;
#ifdef J9_PROJECT_SPECIFIC
size_t byteLength = isBCDType(dataType) ? TR::DataType::getSizeFromBCDPrecision(dataType, byteLengthOrPrecision) : byteLengthOrPrecision;
#else
size_t byteLength = byteLengthOrPrecision;
#endif
bool isLengthValidForDirect = false;
if (address->getOpCodeValue() == TR::loadaddr &&
address->getOpCode().hasSymbolReference() &&
address->getSymbolReference() &&
!address->getSymbol()->isStatic())
{
if (byteLength == address->getSymbol()->getSize())
isLengthValidForDirect = true;
}
if (address->getOpCodeValue() == TR::loadaddr &&
!address->getSymbol()->isStatic() &&
isLengthValidForDirect &&
address->getSymbolReference() == ref &&
ref->getSymbol()->getDataType() == dataType)
{
if (comp->getOption(TR_TraceScalarizeSSOps))
traceMsg(comp,"\n\tscalarizeAddressParameter auto direct case: address %p, dt %d\n",address,dataType);
TR::ILOpCodes opcode = store ? comp->il.opCodeForDirectStore(dataType)
: comp->il.opCodeForDirectLoad(dataType);
loadOrStore = TR::Node::create(address, opcode, store ? 1 : 0);
loadOrStore->setSymbolReference(ref);
}
else
{
TR::ILOpCodes opcode = store ? comp->il.opCodeForIndirectArrayStore(dataType)
: comp->il.opCodeForIndirectArrayLoad(dataType);
loadOrStore = TR::Node::create(address, opcode, store ? 2 : 1);
loadOrStore->setSymbolReference(ref);
loadOrStore->setAndIncChild(0, address);
}
if (byteLength == 8)
{
comp->getJittedMethodSymbol()->setMayHaveLongOps(true);
}
#ifdef J9_PROJECT_SPECIFIC
if (loadOrStore->getType().isBCD())
{
loadOrStore->setDecimalPrecision(byteLengthOrPrecision);
}
else
#endif
if (!store && loadOrStore->getType().isIntegral() && !loadOrStore->getType().isInt64())
{
loadOrStore->setUnsigned(true);
}
return loadOrStore;
}
TR::Node *
OMR::TransformUtil::transformIndirectLoad(TR::Compilation *, TR::Node *node)
{
return NULL;
}
bool
OMR::TransformUtil::isNoopConversion(TR::Compilation *comp, TR::Node *node)
{
if (node->getOpCodeValue() == TR::i2a &&
node->getSize() == 4)
return true;
if (node->getOpCodeValue() == TR::a2i &&
node->getFirstChild()->getSize() == 4)
return true;
if (node->getOpCodeValue() == TR::l2a &&
node->getSize() == 8)
return true;
if (node->getOpCodeValue() == TR::a2l &&
node->getFirstChild()->getSize() == 8)
return true;
// address truncations
if (node->getOpCodeValue() == TR::lu2a &&
node->getSize() <= 8)
return true;
if (node->getOpCodeValue() == TR::iu2a &&
node->getSize() <= 4)
return true;
if (node->getOpCodeValue() == TR::su2a &&
node->getSize() <= 2)
return true;
return false;
}
// Set the visit count for the given node and its subtree
//
void
OMR::TransformUtil::recursivelySetNodeVisitCount(TR::Node *node, vcount_t visitCount)
{
node->decFutureUseCount();
if (node->getVisitCount() == visitCount)
return;
node->setVisitCount(visitCount);
for (int32_t i = 0; i < node->getNumChildren(); ++i)
{
TR::Node *child = node->getChild(i);
TR::TransformUtil::recursivelySetNodeVisitCount(child, visitCount);
}
}
bool
OMR::TransformUtil::transformDirectLoad(TR::Compilation *, TR::Node *node)
{
return false;
}
bool
OMR::TransformUtil::transformIndirectLoadChain(
TR::Compilation *comp,
TR::Node *node,
TR::Node *baseExpression,
TR::KnownObjectTable::Index baseKnownObject,
TR::Node **removedNode)
{
return false;
}
bool
OMR::TransformUtil::transformIndirectLoadChainAt(
TR::Compilation *comp,
TR::Node *node,
TR::Node *baseExpression,
uintptrj_t *baseReferenceLocation,
TR::Node **removedNode)
{
return false;
}
bool
OMR::TransformUtil::fieldShouldBeCompressed(TR::Node *node, TR::Compilation *comp)
{
return false;
}