-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathOMRCodeGenerator.cpp
402 lines (351 loc) · 14.1 KB
/
OMRCodeGenerator.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
/*******************************************************************************
* Copyright (c) 2000, 2020 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
*******************************************************************************/
#include "codegen/CodeGenerator.hpp"
#include "codegen/CodeGenerator_inlines.hpp"
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include "codegen/CodeGenerator.hpp"
#include "env/FrontEnd.hpp"
#include "codegen/Machine.hpp"
#include "codegen/RealRegister.hpp"
#include "codegen/RecognizedMethods.hpp"
#include "codegen/Register.hpp"
#include "codegen/RegisterConstants.hpp"
#include "codegen/RegisterPair.hpp"
#include "codegen/TreeEvaluator.hpp"
#include "compile/Compilation.hpp"
#include "control/Options.hpp"
#include "control/Options_inlines.hpp"
#include "env/TRMemory.hpp"
#include "il/Block.hpp"
#include "il/DataTypes.hpp"
#include "il/ILOpCodes.hpp"
#include "il/ILOps.hpp"
#include "il/Node.hpp"
#include "il/Node_inlines.hpp"
#include "il/ResolvedMethodSymbol.hpp"
#include "il/Symbol.hpp"
#include "il/SymbolReference.hpp"
#include "infra/Assert.hpp"
#include "infra/BitVector.hpp"
#include "infra/Link.hpp"
#include "optimizer/RegisterCandidate.hpp"
#include "optimizer/Structure.hpp"
#include "x/codegen/IntegerMultiplyDecomposer.hpp"
#include "x/codegen/X86Evaluator.hpp"
#include "x/codegen/X86Instruction.hpp"
#include "x/codegen/X86Ops.hpp"
OMR::X86::I386::CodeGenerator::CodeGenerator() :
OMR::X86::CodeGenerator()
{
// Common X86 initialization
//
self()->initializeX86(self()->comp());
self()->setUsesRegisterPairsForLongs();
if (debug("supportsArrayTranslateAndTest"))
self()->setSupportsArrayTranslateAndTest();
if (debug("supportsArrayCmp"))
self()->setSupportsArrayCmp();
self()->setSupportsDoubleWordCAS();
self()->setSupportsDoubleWordSet();
if (self()->comp()->target().isWindows())
{
if (self()->comp()->getOption(TR_DisableTraps))
{
_numberBytesReadInaccessible = 0;
_numberBytesWriteInaccessible = 0;
}
else
{
_numberBytesReadInaccessible = 4096;
_numberBytesWriteInaccessible = 4096;
self()->setHasResumableTrapHandler();
self()->setEnableImplicitDivideCheck();
}
self()->setSupportsDivCheck();
self()->setJNILinkageCalleeCleanup();
self()->setRealVMThreadRegister(self()->machine()->getRealRegister(TR::RealRegister::ebp));
}
else if (self()->comp()->target().isLinux())
{
if (self()->comp()->getOption(TR_DisableTraps))
{
_numberBytesReadInaccessible = 0;
_numberBytesWriteInaccessible = 0;
}
else
{
_numberBytesReadInaccessible = 4096;
_numberBytesWriteInaccessible = 4096;
self()->setHasResumableTrapHandler();
self()->setEnableImplicitDivideCheck();
}
self()->setRealVMThreadRegister(self()->machine()->getRealRegister(TR::RealRegister::ebp));
self()->setSupportsDivCheck();
}
else
{
TR_ASSERT(0, "unknown target");
}
self()->setSupportsInlinedAtomicLongVolatiles();
static char *dontConsiderAllAutosForGRA = feGetEnv("TR_dontConsiderAllAutosForGRA");
if (!dontConsiderAllAutosForGRA)
self()->setConsiderAllAutosAsTacticalGlobalRegisterCandidates();
}
TR::Register *
OMR::X86::I386::CodeGenerator::longClobberEvaluate(TR::Node *node)
{
TR_ASSERT(node->getOpCode().is8Byte(), "assertion failure");
if (node->getReferenceCount() > 1)
{
TR::Register *temp = self()->evaluate(node);
TR::Register *lowReg = self()->allocateRegister();
TR::Register *highReg = self()->allocateRegister();
TR::RegisterPair *longReg = self()->allocateRegisterPair(lowReg, highReg);
generateRegRegInstruction(MOV4RegReg, node, lowReg, temp->getLowOrder(), self());
generateRegRegInstruction(MOV4RegReg, node, highReg, temp->getHighOrder(), self());
return longReg;
}
else
{
return self()->evaluate(node);
}
}
TR_GlobalRegisterNumber
OMR::X86::I386::CodeGenerator::pickRegister(
TR_RegisterCandidate *rc,
TR::Block **allBlocks,
TR_BitVector &availableRegisters,
TR_GlobalRegisterNumber &highRegisterNumber,
TR_LinkHead<TR_RegisterCandidate> *candidates)
{
if (!self()->comp()->getOption(TR_DisableRegisterPressureSimulation))
{
if (self()->comp()->getOption(TR_AssignEveryGlobalRegister))
{
// This is not really necessary except for testing purposes.
// Conceptually, the common pickRegister code should be free to make
// its choices based only on performance considerations, and shouldn't
// need to worry about correctness. When SupportsVMThreadGRA is not set,
// it is incorrect to choose the VMThread register. Therefore we mask
// it out here.
//
// Having said that, the common code *does* already mask out the
// VMThread register for convenience, so under normal circumstances,
// this code is redundant. It is only necessary when
// TR_AssignEveryGlobalRegister is set.
//
availableRegisters -= *self()->getGlobalRegisters(TR_vmThreadSpill, self()->comp()->getMethodSymbol()->getLinkageConvention());
}
return OMR::CodeGenerator::pickRegister(rc, allBlocks, availableRegisters, highRegisterNumber, candidates);
}
if ((rc->getSymbol()->getDataType() == TR::Float) ||
(rc->getSymbol()->getDataType() == TR::Double))
{
if (availableRegisters.get(7))
return 7;
if (availableRegisters.get(8))
return 8;
if (availableRegisters.get(9))
return 9;
if (availableRegisters.get(10))
return 10;
if (availableRegisters.get(11))
return 11;
if (availableRegisters.get(12))
return 12;
return -1;
}
if (!_assignedGlobalRegisters)
_assignedGlobalRegisters = new (self()->trStackMemory()) TR_BitVector(self()->comp()->getSymRefCount(), self()->trMemory(), stackAlloc, growable);
if (availableRegisters.get(5))
return 5; // esi
if (availableRegisters.get(2))
return 2; // ecx
static char *dontUseEBXasGPR = feGetEnv("dontUseEBXasGPR");
if (!dontUseEBXasGPR && availableRegisters.get(1))
return 1;
#ifdef J9_PROJECT_SPECIFIC
TR::RecognizedMethod rm = self()->comp()->getMethodSymbol()->getRecognizedMethod();
if (rm == TR::java_util_HashtableHashEnumerator_hasMoreElements)
{
if (availableRegisters.get(4))
return 4; // edi
if (availableRegisters.get(3))
return 3; // edx
}
else
#endif
{
int32_t numExtraRegs = 0;
int32_t maxRegisterPressure = 0;
vcount_t visitCount = self()->comp()->incVisitCount();
TR_BitVectorIterator bvi(rc->getBlocksLiveOnEntry());
int32_t maxFrequency = 0;
while (bvi.hasMoreElements())
{
int32_t liveBlockNum = bvi.getNextElement();
TR::Block *block = allBlocks[liveBlockNum];
if (block->getFrequency() > maxFrequency)
maxFrequency = block->getFrequency();
}
int32_t maxStaticFrequency = 0;
if (maxFrequency == 0)
{
bvi.setBitVector(rc->getBlocksLiveOnEntry());
while (bvi.hasMoreElements())
{
int32_t liveBlockNum = bvi.getNextElement();
TR::Block *block = allBlocks[liveBlockNum];
TR_BlockStructure *blockStructure = block->getStructureOf();
int32_t blockWeight = 1;
if (blockStructure &&
!block->isCold())
{
blockStructure->calculateFrequencyOfExecution(&blockWeight);
if (blockWeight > maxStaticFrequency)
maxStaticFrequency = blockWeight;
}
}
}
bool assigningEDX = false;
if (!availableRegisters.get(4) &&
availableRegisters.get(3))
assigningEDX = true;
bool vmThreadUsed = false;
bvi.setBitVector(rc->getBlocksLiveOnEntry());
while (bvi.hasMoreElements())
{
int32_t liveBlockNum = bvi.getNextElement();
TR::Block *block = allBlocks[liveBlockNum];
_assignedGlobalRegisters->empty();
int32_t numAssignedGlobalRegs = 0;
TR_RegisterCandidate *prev;
for (prev = candidates->getFirst(); prev; prev = prev->getNext())
{
bool gprCandidate = true;
if ((prev->getSymbol()->getDataType() == TR::Float) ||
(prev->getSymbol()->getDataType() == TR::Double))
gprCandidate = false;
if (gprCandidate && prev->getBlocksLiveOnEntry().get(liveBlockNum))
{
numAssignedGlobalRegs++;
if (prev->getDataType() == TR::Int64)
numAssignedGlobalRegs++;
_assignedGlobalRegisters->set(prev->getSymbolReference()->getReferenceNumber());
}
}
maxRegisterPressure = self()->estimateRegisterPressure(block, visitCount, maxStaticFrequency, maxFrequency, vmThreadUsed, numAssignedGlobalRegs, _assignedGlobalRegisters, rc->getSymbolReference(), assigningEDX);
if (maxRegisterPressure >= self()->getMaximumNumbersOfAssignableGPRs())
break;
}
// Determine if we can spare any extra registers for this candidate without spilling
// in any hot (critical) blocks
//
if (maxRegisterPressure < self()->getMaximumNumbersOfAssignableGPRs())
numExtraRegs = self()->getMaximumNumbersOfAssignableGPRs() - maxRegisterPressure;
//dumpOptDetails("For global register candidate %d reg pressure is %d maxRegs %d numExtraRegs %d\n", rc->getSymbolReference()->getReferenceNumber(), maxRegisterPressure, comp()->cg()->getMaximumNumbersOfAssignableGPRs(), numExtraRegs);
if (numExtraRegs > 0)
{
if (availableRegisters.get(4))
return 4; // edi
if (availableRegisters.get(3))
return 3; // edx
}
}
return -1; // -1 ==> don't use a global register
}
int32_t
OMR::X86::I386::CodeGenerator::getMaximumNumberOfGPRsAllowedAcrossEdge(TR::Node *node)
{
// TODO: Currently, lookupEvaluator doesn't deal properly with different
// glRegDeps on different cases of a lookupswitch.
//
static const char *enableLookupswitch = feGetEnv("TR_enableGRAAcrossLookupSwitch");
if (!enableLookupswitch && node->getOpCode().getOpCodeValue()==TR::lookup)
return 1;
if (node->getOpCode().getOpCodeValue()==TR::table)
{
// 1 for jump table base reg, which is not apparent in the trees
// 1 for ebp when it is needed for the VMThread
//
return self()->getNumberOfGlobalGPRs() - 2;
}
if (node->getOpCode().isIf())
{
// we run out of all but one/two registers in these cases
//
if (node->getFirstChild()->getType().isInt64())
{
if (node->getOpCode().isBranch())
{
TR::Node *firstChild = node->getFirstChild();
TR::Node *secondChild = node->getSecondChild();
int extraRegsAvailable = 0;
if(firstChild->getOpCodeValue() == TR::d2l ||
secondChild->getOpCodeValue() == TR::d2l)
{
return 1;
}
if ((firstChild->getReferenceCount() == 1 &&
firstChild->getOpCode().isLoadVarDirect()) ||
(secondChild->getReferenceCount() == 1 &&
firstChild->getOpCode().isLoadVarDirect()))
extraRegsAvailable += 0; // TODO: put it back to 2 when looking at GRA, GRA pushes allocation of 8 registers
return 2 + extraRegsAvailable;
}
else
{
// TR_lcmpXX opcodes take up 5 regs
//
return 1;
}
}
// we run out of all but one register in these cases....last time I tried....
//
if (node->getFirstChild()->getOpCodeValue() == TR::instanceof)
{
if (!TR::TreeEvaluator::instanceOfOrCheckCastNeedSuperTest(node->getFirstChild(), self()) &&
TR::TreeEvaluator::instanceOfOrCheckCastNeedEqualityTest(node->getFirstChild(), self()))
return self()->getNumberOfGlobalGPRs() - 4; // ebp plus three other regs if vft masking is enabled
else
return 0;
}
// All other conditional branches, we usually need one reg for the compare and possibly one for the vmthread
//return getNumberOfGlobalGPRs() - 1 - (node->isVMThreadRequired()? 1 : 0);
// vmThread required might be set on a node after GRA has ran
return self()->getNumberOfGlobalGPRs() - 2;
}
return INT_MAX;
}
bool
OMR::X86::I386::CodeGenerator::codegenMulDecomposition(int64_t multiplier)
{
bool iMulDecomposeReport = self()->comp()->getOptions()->trace(OMR::treeSimplification);
bool answer = false;
if (iMulDecomposeReport)
diagnostic("\nCodegen was queried for the value of %d, ", (int32_t)multiplier);
answer = TR_X86IntegerMultiplyDecomposer::hasDecomposition (multiplier);
if (iMulDecomposeReport)
diagnostic("codegen said [%c]\n", (answer)?'Y':'N');
return answer;
}