-
Notifications
You must be signed in to change notification settings - Fork 747
/
Copy pathDynamicLiteralPool.cpp
591 lines (521 loc) · 20.9 KB
/
DynamicLiteralPool.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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*******************************************************************************
* Copyright IBM Corp. and others 2000
*
* 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 https://www.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] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#include "optimizer/DynamicLiteralPool.hpp"
#include <limits.h>
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "codegen/CodeGenerator.hpp"
#include "env/FrontEnd.hpp"
#include "compile/Compilation.hpp"
#include "compile/Method.hpp"
#include "compile/SymbolReferenceTable.hpp"
#include "compile/VirtualGuard.hpp"
#include "control/Options.hpp"
#include "control/Options_inlines.hpp"
#include "env/CompilerEnv.hpp"
#include "env/PersistentInfo.hpp"
#include "env/StackMemoryRegion.hpp"
#include "env/TRMemory.hpp"
#include "env/jittypes.h"
#include "il/AliasSetInterface.hpp"
#include "il/Block.hpp"
#include "il/DataTypes.hpp"
#include "il/ILOpCodes.hpp"
#include "il/ILOps.hpp"
#include "il/Node.hpp"
#include "il/NodePool.hpp"
#include "il/Node_inlines.hpp"
#include "il/MethodSymbol.hpp"
#include "il/StaticSymbol.hpp"
#include "il/Symbol.hpp"
#include "il/SymbolReference.hpp"
#include "il/TreeTop.hpp"
#include "il/TreeTop_inlines.hpp"
#include "infra/Assert.hpp"
#include "infra/Cfg.hpp"
#include "infra/ILWalk.hpp"
#include "infra/Stack.hpp"
#include "infra/TRCfgEdge.hpp"
#include "infra/TRCfgNode.hpp"
#include "optimizer/Optimization.hpp"
#include "optimizer/Optimization_inlines.hpp"
#include "optimizer/OptimizationManager.hpp"
#include "optimizer/Optimizations.hpp"
#include "optimizer/Optimizer.hpp"
#include "optimizer/Structure.hpp"
#include "ras/Debug.hpp"
#include "runtime/J9Runtime.hpp"
TR_DynamicLiteralPool::TR_DynamicLiteralPool(TR::OptimizationManager *manager)
: TR::Optimization(manager)
{
_litPoolAddressSym=NULL;
setAloadFromCurrentBlock(NULL);
_changed = false;
}
int32_t TR_DynamicLiteralPool::perform()
{
if (!cg()->supportsOnDemandLiteralPool())
return 1;
{
TR::StackMemoryRegion stackMemoryRegion(*trMemory());
process(comp()->getStartTree(), NULL);
if (performTransformation(comp(), "%s free reserved literal pool register\n", optDetailString()))
{
if (cg()->supportsOnDemandLiteralPool())
{
cg()->setOnDemandLiteralPoolRun(true);
cg()->enableLiteralPoolRegisterForGRA();
}
}
postPerformOnBlocks();
} // scope of the stack memory region
if (_changed)
{
optimizer()->setUseDefInfo(NULL);
optimizer()->setValueNumberInfo(NULL);
optimizer()->setAliasSetsAreValid(false);
requestOpt(OMR::localCSE, true);
}
// need that to remove the astore if no literal pool is required
requestOpt(OMR::localDeadStoreElimination, true);
return 1;
}
int32_t TR_DynamicLiteralPool::performOnBlock(TR::Block *block)
{
if (block->getEntry())
process(block->getEntry(), block->getEntry()->getExtendedBlockExitTreeTop()->getNextTreeTop());
return 0;
}
void TR_DynamicLiteralPool::initLiteralPoolBase()
{
TR::Node *tempValue;
TR::TreeTop *storeToTempTT;
TR::Node *firstNode,*storeToTemp;
TR::Block *firstBlock;
TR::SymbolReference * tempStaticSym;
firstNode = comp()->getStartTree()->getNode();
firstBlock = firstNode->getBlock();
tempStaticSym = getSymRefTab()->createKnownStaticDataSymbolRef(0, TR::Address);
_litPoolAddressSym = getSymRefTab()->createTemporary(comp()->getMethodSymbol(), TR::Address);
tempValue = TR::Node::createWithSymRef(firstNode, TR::aload, 0, tempStaticSym);
storeToTemp = TR::Node::createWithSymRef(TR::astore, 1, 1, tempValue, _litPoolAddressSym);
tempStaticSym->setLiteralPoolAddress();
_litPoolAddressSym->setFromLiteralPool();
// make sure GC knows about the two symbols, otherwise
// GC will examine the memory pointed to by the temp/static
// and will crash because the memory (i.e. the literal pool) is not a J9Object
tempStaticSym->getSymbol()->setNotCollected();
getLitPoolAddressSym()->getSymbol()->setNotCollected();
storeToTempTT = TR::TreeTop::create(comp(), storeToTemp);
firstBlock->prepend(storeToTempTT);
_changed = true;
dumpOptDetails(comp(), "Literal pool base pointer initialized to %p \n", storeToTemp);
}
int32_t TR_DynamicLiteralPool::process(TR::TreeTop *startTree, TR::TreeTop *endTree)
{
TR::TreeTop *tt, *exitTree;
vcount_t visitCount = comp()->incVisitCount();
for (tt = startTree; (tt != endTree); tt = exitTree->getNextRealTreeTop())
{
TR::Block *block = tt->getNode()->getBlock();
_currentBlock=block;
exitTree = block->getEntry()->getExtendedBlockExitTreeTop();
processBlock(block, visitCount);
}
return 1;
}
bool TR_DynamicLiteralPool::processBlock(TR::Block *block, vcount_t visitCount)
{
TR::TreeTop *exit = block->getEntry()->getExtendedBlockExitTreeTop();
setAloadFromCurrentBlock(NULL);
for (TR::TreeTop *tt = block->getEntry(); tt != exit; tt = tt->getNextRealTreeTop())
{
setNumChild(-1);
visitTreeTop(tt, 0, 0, tt->getNode(), visitCount);
}
return true;
}
bool TR_DynamicLiteralPool::visitTreeTop(TR::TreeTop * tt, TR::Node *grandParent, TR::Node *parent, TR::Node *node, vcount_t visitCount)
{
int32_t firstChild = 0;
if (node->getVisitCount() == visitCount)
return true;
node->setVisitCount(visitCount);
TR::ILOpCode opCode = node->getOpCode();
TR::ILOpCodes opCodeValue = opCode.getOpCodeValue();
TR_OpaqueClassBlock * classInfo = 0;
if (cg()->supportsOnDemandLiteralPool())
{
// do work on this node
if (opCode.isLoadConst())
{
// reset visitcount to make sure all parents see this child
if (node->getReferenceCount()>1) node->setVisitCount(visitCount-1);
dumpOptDetails(comp(), "looking at const node %p (%s)\n", node, opCode.getName());
transformLitPoolConst(grandParent, parent,node);
}
else if (opCode.hasSymbolReference() &&
node->getSymbol()->isStatic() &&
!node->getSymbolReference()->isLiteralPoolAddress() &&
(node->getSymbolReference() != comp()->getSymRefTab()->findThisRangeExtensionSymRef()))
{
dumpOptDetails(comp(), "looking at the static symref for node %p (%s)\n", node, opCode.getName());
transformStaticSymRefToIndirectLoad(tt, parent, node);
}
// add extra aload child for CurrentTimeMaxPrecision call
if (opCode.isCall() &&
comp()->getSymRefTab()->isNonHelper(node->getSymbolReference(), TR::SymbolReferenceTable::currentTimeMaxPrecisionSymbol))
{
addNewAloadChild(node);
}
// add extra aload child for float conversions
else if ((opCodeValue==TR::fbits2i || opCodeValue==TR::dbits2l) && node->normalizeNanValues())
{
addNewAloadChild(node);
}
}
for (int32_t i = firstChild; i < node->getNumChildren(); ++i)
{
setNumChild(i);
visitTreeTop(0, parent, node, node->getChild(i), visitCount);
}
return true;
}
bool TR_DynamicLiteralPool::transformLitPoolConst(TR::Node *grandParent, TR::Node *parent, TR::Node *child)
{
switch (child->getOpCodeValue())
{
case TR::fconst:
if (performTransformation(comp(), "%s Float Constant\n", optDetailString()))
{
_changed = true;
transformConstToIndirectLoad(parent, child);
}
else
return false;
break;
case TR::dconst:
// LZDR can be used to load floating point zero
if (child->getDouble() != 0.0 && performTransformation(comp(), "%s Double Constant\n", optDetailString()))
{
_changed = true;
transformConstToIndirectLoad(parent, child);
}
else
return false;
break;
case TR::aconst:
if (child->isClassUnloadingConst())
return false;
case TR::iconst:
case TR::lconst:
case TR::bconst:
case TR::sconst:
if (transformNeeded(grandParent, parent, child))
{
if (performTransformation(comp(), "%s Large non-float Constant\n", optDetailString()))
{
_changed = true;
transformConstToIndirectLoad(parent, child);
}
else
{
return false;
}
}
break;
default:
if (child->getDataType().isBCD() || child->getDataType() == TR::Aggregate)
return false;
else
TR_ASSERT(false,"Unknown const %p (type %s)\n",child,child->getDataType().toString());
break;
}
return true;
}
bool TR_DynamicLiteralPool::transformNeeded(TR::Node *grandParent, TR::Node *parent, TR::Node *child)
{
TR::ILOpCode parentOpCode = parent->getOpCode();
TR::ILOpCodes parentOpCodeValue = parentOpCode.getOpCodeValue();
// need to ensure a constant setsign can be retrieved
if (parentOpCode.isSetSign())
return false;
if (child->getType().isIntegral() && parentOpCode.skipDynamicLitPoolOnInts())
return false;
// If the hardware supports relative fixed point loads/stores from the literal pool,
// i.e. On z10 or higher, LRL/LGRL/STRL/STGRL, then do not transform to use literal pool
// base register.
if ( (child->getType().isIntegral() || child->getType().isAddress()) &&
cg()->supportsDirectIntegralLoadStoresFromLiteralPool())
return false;
// don't modify const children of multiplication or division to
// allow strength reduction to happen. The check bellow could be
// more precise, but the logic to determine if strength reduction
// is possible is quite complex and if we have it here, it needs
// be kept in sync with the logic in evaluators.
// In the worst case, when constant actually needs to go lit pool
// the code wiil insert extra LARL pre-loading lit pool entry
// The cost of this LARL is negligible considering the cost of MULT or DIV
if (parentOpCode.isMul() || parentOpCode.isDiv()) return false;
// If this looks like a pattern that could be evaluated to a test under mask type instruction
// then do not use the lit pool as this will obfuscate the pattern in the evaluator.
if (cg()->getSupportsTestUnderMask() &&
parentOpCode.isIf() &&
parent->getNumChildren() == 2 &&
parent->getSecondChild()->getOpCode().isLoadConst() &&
isPowerOf2(parent->getSecondChild()->get64bitIntegralValue()))
{
return false;
}
if (cg()->getSupportsTestUnderMask() &&
grandParent && grandParent->getOpCode().isIf() &&
parentOpCode.isAnd() &&
parent->getNumChildren() == 2 &&
parent->getSecondChild()->getOpCode().isLoadConst() &&
isPowerOf2(parent->getSecondChild()->get64bitIntegralValue()))
{
return false;
}
// Check for arithmetic operations for add, sub, and non-guard compares.
// Guarded if-stmts might contain class pointers will may be unloaded. Such
// scenarios will be handled by constLoadNeedsLitPool below.
if (parentOpCode.isAdd() || parentOpCode.isSub() ||
(parentOpCode.isBooleanCompare() && !parent->isTheVirtualGuardForAGuardedInlinedCall()))
{
if (child->getOpCode().isLong() && (comp()->target().is32Bit()))
return false; //avasilev: to be handled better
else
{
TR::ILOpCodes oldOpCode = child->getOpCodeValue();
bool needs = (cg()->arithmeticNeedsLiteralFromPool(child));
TR::Node::recreate(child, oldOpCode);
return needs;
}
}
if (parentOpCode.isAnd() || parentOpCode.isOr() || parentOpCode.isXor() || parentOpCode.isNeg())
{
if (child->getOpCode().isLong() && (comp()->target().is32Bit()))
return false; // to be handled better
else
return (cg()->bitwiseOpNeedsLiteralFromPool(parent,child));
}
if (parentOpCode.isLeftShift() || parentOpCode.isRightShift() || parentOpCode.isShiftLogical())
return false;
// TR::arraytranslateAndTest may throw AIOB, but that is taken care of by the evaluator. Also, the
// iconst may be the character to search.
if (parentOpCode.isBndCheck() && parentOpCodeValue != TR::arraytranslateAndTest)
return false;
// TR::arrayset evaluator expects to see const child, and it's evaluated
// specially. Don't modify const children of TR::arrayset. See 74553
if (parentOpCodeValue == TR::arrayset) return false;
if (child->isClassUnloadingConst()) return false;
// TODO: This function can be simplified because the context in which it is used the following call will always result yield false.
// As such many paths through this function that return false are effectively dead and can be removed. There is only one path that
// does not return false, and this is the only path that needs to exist, though my intuition is that this path too should be verified
// as it may no longer be applicable.
return (cg()->constLoadNeedsLiteralFromPool(child));
}
bool TR_DynamicLiteralPool::transformConstToIndirectLoad(TR::Node *parent, TR::Node *child)
{
//TR::Node *loadLiteralFromThePool;
//TR::Node *loadLiteralPoolAddress;
TR::Node *constCopy, *indirectLoad;
TR::SymbolReference *shadow;
dumpOptDetails(comp(), "transforming const %p (%s)\n", child, child->getOpCode().getName());
TR::Node *addrNode;
addrNode = getAloadFromCurrentBlock(parent);
constCopy =TR::Node::copy(child);
shadow = getSymRefTab()->findOrCreateImmutableGenericIntShadowSymbolReference((intptr_t)constCopy);
shadow->setLiteralPoolAddress();
if (child->getReferenceCount() > 1) // rematerialize the const by creating new indirect load node
{
indirectLoad=TR::Node::createWithSymRef(comp()->il.opCodeForIndirectLoad(child->getDataType()), 1, 1, addrNode, shadow);
dumpOptDetails(comp(), "New node created %p, refcount of const child was %d\n",indirectLoad,child->getReferenceCount());
parent->setAndIncChild(getNumChild(),indirectLoad);
child->decReferenceCount();
}
else
{
//convert const node to indirect load node
child->setNumChildren(1);
child = TR::Node::recreateWithSymRef(child, comp()->il.opCodeForIndirectLoad(child->getDataType()), shadow);
child->setAndIncChild(0, addrNode);
}
return true;
}
bool TR_DynamicLiteralPool::transformStaticSymRefToIndirectLoad(TR::TreeTop * tt, TR::Node *parent, TR::Node * & child)
{
// Only transform direct references
if (child->getOpCode().isIndirect())
return false;
TR::SymbolReference * childSymRef = child->getSymbolReference();
//childSymRef->setFromLiteralPool();
TR::ILOpCode childOpcode=child->getOpCode();
TR::ILOpCodes childOpcodeValue=child->getOpCodeValue();
if (childOpcodeValue==TR::loadaddr)
{
return false;
}
else
{
TR::SymbolReference *intChildShadow = NULL;
if (childSymRef->isUnresolved())
{
if (cg()->supportsDirectIntegralLoadStoresFromLiteralPool())
{
return false;
}
childSymRef->setFromLiteralPool();
if (performTransformation(comp(), "%s unresolved static ref for node %p (%s)\n", optDetailString(), child, child->getOpCode().getName()))
{
_changed = true;
intChildShadow = getSymRefTab()->findOrCreateGenericIntShadowSymbolReference(0);
}
else
{
return false;
}
}
else
{
return false;
}
intChildShadow->setFromLiteralPool();
getSymRefTab()->aliasBuilder.setLitPoolGenericIntShadowHasBeenCreated();
TR::Node * loadLiteralFromThePool = TR::Node::createWithSymRef(TR::aloadi, 1, 1, getAloadFromCurrentBlock(child), childSymRef);
loadLiteralFromThePool->getSymbol()->setNotCollected();
if (childOpcodeValue==TR::awrtbar)
{
child->getFirstChild()->decReferenceCount();
child->getSecondChild()->decReferenceCount();
child = TR::Node::create(TR::awrtbari, 3, loadLiteralFromThePool, child->getFirstChild(), child->getSecondChild());
if (parent)
parent->setAndIncChild(0, child);
else
tt->setNode(child);
}
else
{
//TR::DataType type = childSymRef->getSymbol()->castToStaticSymbol()->getDataType();
TR::DataType type = child->getDataType();
if (childOpcode.isStore())
{
child->setSecond(child->getFirstChild());
TR::Node::recreate(child, comp()->il.opCodeForIndirectStore(type));
}
else if (childOpcode.isLoad())
{
TR::Node::recreate(child, comp()->il.opCodeForIndirectLoad(type));
}
else
{
TR_ASSERT(0,"not Load or Store, what is it ?\n");
}
child->setAndIncChild(0, loadLiteralFromThePool);
child->setNumChildren(child->getNumChildren()+1);
}
child->setSymbolReference(intChildShadow);
dumpOptDetails(comp(), "created TR::aloadi %p from child %p\n", loadLiteralFromThePool, child);
}
return true;
}
bool TR_DynamicLiteralPool::addNewAloadChild(TR::Node *node)
{
if (!performTransformation(comp(), "%s creating new aload child for node %p (%s)\n", optDetailString(), node, node->getOpCode().getName()))
return false;
_changed = true;
node->setAndIncChild(node->getNumChildren(),getAloadFromCurrentBlock(node));
node->setNumChildren(node->getNumChildren()+1);
return true;
}
bool TR_DynamicLiteralPool::handleNodeUsingVMThread(TR::TreeTop * tt, TR::Node *parent, TR::Node *node, vcount_t visitCount)
{
/*
if (node->getOpCode().isLoadVarOrStore())
{
TR::SymbolReference *symRef = node->getSymbolReference();
if (symRef->isUnresolved())
{
}
else
{
if (symRef->getSymbol()->isMethodMetaData())
{
TR_ASSERT(node->getOpCode().isDirect(), "Load/store from meta data should use a direct opcode\n");
TR::SymbolReference *metaDataShadow = getSymRefTab()->findOrCreateGenericIntShadowSymbolReference(symRef->getOffset());
TR::Node * loadVMThreadFromTheStack = getVMThreadAloadFromCurrentBlock(node);
TR::DataType type = node->getDataType();
if (node->getOpCode().isStore())
{
node->setSecond(node->getFirstChild());
TR::Node::recreate(node, comp()->il.opCodeForIndirectStore(type));
}
else if (node->getOpCode().isLoad())
{
TR::Node::recreate(node, comp()->il.opCodeForIndirectLoad(type));
}
else
{
TR_ASSERT(0,"not Load or Store, what is it ?\n");
}
node->setAndIncChild(0, loadVMThreadFromTheStack);
node->setNumChildren(node->getNumChildren()+1);
node->setSymbolReference(metaDataShadow);
}
if (node->getOpCode().isWrtBar())
{
}
}
}
*/
return true;
}
bool TR_DynamicLiteralPool::handleNodeUsingSystemStack(TR::TreeTop * tt, TR::Node *parent, TR::Node *node, vcount_t visitCount)
{
return true;
}
TR::SymbolReference * getVMThreadSym()
{
return NULL;
}
TR::Node *TR_DynamicLiteralPool::getVMThreadAloadFromCurrentBlock(TR::Node *parent)
{
if (_vmThreadAloadFromCurrentBlock==NULL)
{
setVMThreadAloadFromCurrentBlock(TR::Node::createWithSymRef(parent, TR::aload, 0, getVMThreadSym()));
dumpOptDetails(comp(), "New VM thread aload needed, it is: %p!\n", _vmThreadAloadFromCurrentBlock);
}
else
{
dumpOptDetails(comp(), "Can re-use VM thread aload %p!\n",_vmThreadAloadFromCurrentBlock);
}
return _vmThreadAloadFromCurrentBlock;
}
const char *
TR_DynamicLiteralPool::optDetailString() const throw()
{
return "O^O DYNAMIC LITERAL POOL: ";
}