-
Notifications
You must be signed in to change notification settings - Fork 747
/
Copy pathIdiomRecognition.hpp
1521 lines (1340 loc) · 62 KB
/
IdiomRecognition.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
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* 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
*******************************************************************************/
#ifndef IDIOMRECOGNITION_INCL
#define IDIOMRECOGNITION_INCL
#include <stdint.h>
#include <string.h>
#include "compile/Compilation.hpp"
#include "compile/CompilationTypes.hpp"
#include "env/TRMemory.hpp"
#include "il/DataTypes.hpp"
#include "il/ILOpCodes.hpp"
#include "il/ILOps.hpp"
#include "il/ILProps.hpp"
#include "infra/Assert.hpp"
#include "infra/BitVector.hpp"
#include "infra/Flags.hpp"
#include "infra/HashTab.hpp"
#include "infra/Link.hpp"
#include "infra/List.hpp"
#include "infra/String.hpp"
#include "infra/TRlist.hpp"
#include "optimizer/LoopCanonicalizer.hpp"
#include "optimizer/OptimizationManager.hpp"
class TR_CISCTransformer;
class TR_RegionStructure;
class TR_UseDefInfo;
namespace TR { class Block; }
namespace TR { class CFG; }
namespace TR { class CFGEdge; }
namespace TR { class CFGNode; }
namespace TR { class Node; }
namespace TR { class Optimization; }
namespace TR { class Optimizer; }
namespace TR { class SymbolReference; }
namespace TR { class TreeTop; }
typedef enum
{
TR_variable = TR::NumAllIlOps,
TR_booltable,
TR_entrynode,
TR_exitnode,
TR_allconst,
TR_ahconst, // constant for array header
TR_variableORconst,
TR_quasiConst, // Currently, variable or constant or arraylength
TR_quasiConst2, // Currently, variable or constant, arraylength, or *iloadi* (for non-array)
// We shouldn't use it in an idiom including istorei to *non-array* variable.
TR_iaddORisub, // addition or subtraction
TR_conversion, // all data conversions, such as b2i, c2i, i2l, ...
TR_ifcmpall, // all if instructions
TR_ishrall, // ishr and iushr
TR_bitop1, // AND, OR, and XOR
TR_arrayindex, // variable or addition
TR_arraybase, // variable or aloadi
TR_inbload, // indirect non-byte load
TR_inbstore, // indirect non-byte store
TR_indload, // indirect load
TR_indstore, // indirect store
TR_ibcload, // indirect byte or char load
TR_ibcstore // indirect byte or char store
} TR_CISCOps;
struct TrNodeInfo
{
TR::Block *_block;
TR::Node *_node;
TR::TreeTop *_treeTop;
};
class TR_CISCNode
{
public:
TR_ALLOC(TR_Memory::LoopTransformer);
TR_HeapMemory trHeapMemory() { return _trMemory; }
bool isEqualOpc(TR_CISCNode *t);
TR_CISCNode(TR_Memory * m, uint32_t opc, TR::DataType dt, uint16_t id, int16_t dagId, uint16_t ncfgs, uint16_t nchildren, TR_AllocationKind allocKind = heapAlloc)
: _allocKind(allocKind), _trMemory(m), _preds(m), _parents(m), _dest(m), _chains(m), _hintChildren(m), _trNodeInfo(m), _dt(dt)
{
initialize(opc, id, dagId, ncfgs, nchildren);
}
TR_CISCNode(TR_Memory * m, uint32_t opc, TR::DataType dt, uint16_t id, int16_t dagId, uint16_t ncfgs, uint16_t nchildren, TR_CISCNode *pred, TR_AllocationKind allocKind = heapAlloc)
: _allocKind(allocKind), _trMemory(m), _preds(m), _parents(m), _dest(m), _chains(m), _hintChildren(m), _trNodeInfo(m), _dt(dt)
{
initialize(opc, id, dagId, ncfgs, nchildren);
pred->setSucc(0, this);
}
TR_CISCNode(TR_Memory * m, uint32_t opc, TR::DataType dt, uint16_t id, int16_t dagId, uint16_t ncfgs, uint16_t nchildren, TR_CISCNode *pred, TR_CISCNode *child0, TR_AllocationKind allocKind = heapAlloc)
: _allocKind(allocKind), _trMemory(m), _preds(m), _parents(m), _dest(m), _chains(m), _hintChildren(m), _trNodeInfo(m), _dt(dt)
{
initialize(opc, id, dagId, ncfgs, nchildren);
pred->setSucc(0, this);
setChild(0, child0);
}
TR_CISCNode(TR_Memory * m, uint32_t opc, TR::DataType dt, uint16_t id, int16_t dagId, uint16_t ncfgs, uint16_t nchildren, TR_CISCNode *pred, TR_CISCNode *child0, TR_CISCNode *child1, TR_AllocationKind allocKind = heapAlloc)
: _allocKind(allocKind), _trMemory(m), _preds(m), _parents(m), _dest(m), _chains(m), _hintChildren(m), _trNodeInfo(m), _dt(dt)
{
initialize(opc, id, dagId, ncfgs, nchildren);
pred->setSucc(0, this);
setChildren(child0, child1);
}
TR_CISCNode(TR_Memory * m, uint32_t opc, TR::DataType dt, uint16_t id, int16_t dagId, uint16_t ncfgs, uint16_t nchildren, int32_t otherInfo, TR_AllocationKind allocKind = heapAlloc)
: _allocKind(allocKind), _trMemory(m), _preds(m), _parents(m), _dest(m), _chains(m), _hintChildren(m), _trNodeInfo(m), _dt(dt)
{
initialize(opc, id, dagId, ncfgs, nchildren, otherInfo);
}
TR_CISCNode(TR_Memory * m, uint32_t opc, TR::DataType dt, uint16_t id, int16_t dagId, uint16_t ncfgs, uint16_t nchildren, int32_t otherInfo, TR_CISCNode *pred, TR_AllocationKind allocKind = heapAlloc)
: _allocKind(allocKind), _trMemory(m), _preds(m), _parents(m), _dest(m), _chains(m), _hintChildren(m), _trNodeInfo(m), _dt(dt)
{
initialize(opc, id, dagId, ncfgs, nchildren, otherInfo);
pred->setSucc(0, this);
}
void initialize(uint32_t opc, uint16_t id, int16_t dagId, uint16_t ncfgs, uint16_t nchildren)
{
initializeMembers(opc, id, dagId, ncfgs, nchildren);
allocArrays(ncfgs, nchildren);
}
void initializeMembers(uint32_t opc, uint16_t id, int16_t dagId, uint16_t ncfgs, uint16_t nchildren);
void initialize(uint32_t opc, uint16_t id, int16_t dagId, uint16_t ncfgs, uint16_t nchildren, int32_t otherInfo)
{
initialize(opc, id, dagId, ncfgs, nchildren);
setOtherInfo(otherInfo);
}
TR_Memory * trMemory() { return _trMemory; }
void setOtherInfo(int32_t otherInfo)
{
_flags.set(_isValidOtherInfo);
_otherInfo = otherInfo;
checkFlags();
}
void checkFlags()
{
if (isValidOtherInfo())
{
switch(_opcode)
{
case TR::iconst:
case TR::sconst:
case TR::bconst:
case TR::lconst:
setIsInterestingConstant();
break;
}
}
}
void replaceSucc(uint32_t index, TR_CISCNode *to);
inline void setSucc(uint32_t index, TR_CISCNode *ch)
{
TR_ASSERT(index < _numSuccs, "TR_CISCNode::setSucc index out of range");
_succs[index] = ch;
ch->addPred(this);
}
void setSucc(TR_CISCNode *ch)
{
setSucc(0, ch);
}
void setSuccs(TR_CISCNode *ch1, TR_CISCNode *ch2)
{
setSucc(0, ch1);
setSucc(1, ch2);
}
void replaceChild(uint32_t index, TR_CISCNode *ch);
inline void setChild(uint32_t index, TR_CISCNode *ch)
{
TR_ASSERT(index < _numChildren, "TR_CISCNode::setChild index out of range");
_children[index] = ch;
ch->addParent(this);
}
void setChild(TR_CISCNode *ch)
{
setChild(0, ch);
}
void setChildren(TR_CISCNode *ch1, TR_CISCNode *ch2)
{
setChild(0, ch1);
setChild(1, ch2);
}
inline TR_CISCNode *getSucc(uint32_t index)
{
TR_ASSERT(index < _numSuccs, "TR_CISCNode::getSucc index out of range");
return _succs[index];
}
inline TR_CISCNode *getChild(uint32_t index)
{
TR_ASSERT(index < _numChildren, "TR_CISCNode::getChild index out of range");
return _children[index];
}
TR_CISCNode *getNodeIfSingleChain() { return _chains.isSingleton() ? _chains.getListHead()->getData() : 0; }
inline uint16_t getChildID(uint32_t index) { return getChild(index)->_id; }
inline uint32_t getOpcode() { return _opcode; }
inline const TR::ILOpCode& getIlOpCode() { return _ilOpCode; }
inline TR::DataType getDataType() { return _dt; }
inline uint16_t getNumChildren() { return _numChildren; }
inline uint16_t getNumSuccs() { return _numSuccs; }
inline uint16_t getID() { return _id; }
inline uint16_t getDagID() { return _dagId; }
inline void setDagID(int16_t d) { _dagId = d; }
inline int32_t getOtherInfo() { return _otherInfo; }
void initChains() { _chains.init(); }
// flags
bool isValidOtherInfo() { return _flags.testAny(_isValidOtherInfo); }
void setIsStoreDirect(bool v = true) { _flags.set(_isStoreDirect, v); }
bool isStoreDirect() { return _flags.testAny(_isStoreDirect); }
void setIsLoadVarDirect(bool v = true) { _flags.set(_isLoadVarDirect, v); }
bool isLoadVarDirect() { return _flags.testAny(_isLoadVarDirect); }
void setIsNegligible(bool v = true) { _flags.set(_isNegligible, v); }
bool isNegligible() { return _flags.testAny(_isNegligible); }
void setIsSuccSimplyConnected(bool v = true) { _flags.set(_isSuccSimplyConnected, v); }
bool isSuccSimplyConnected() { return _flags.testAny(_isSuccSimplyConnected); }
void setIsPredSimplyConnected(bool v = true) { _flags.set(_isPredSimplyConnected, v); }
bool isPredSimplyConnected() { return _flags.testAny(_isPredSimplyConnected); }
void setIsChildSimplyConnected(bool v = true) { _flags.set(_isChildSimplyConnected, v); }
bool isChildSimplyConnected() { return _flags.testAny(_isChildSimplyConnected); }
void setIsParentSimplyConnected(bool v = true) { _flags.set(_isParentSimplyConnected, v); }
bool isParentSimplyConnected() { return _flags.testAny(_isParentSimplyConnected); }
void setIsEssentialNode(bool v = true) { _flags.set(_isEssentialNode, v); }
bool isEssentialNode() { return _flags.testAny(_isEssentialNode); }
void setIsOptionalNode(bool v = true) { _flags.set(_isOptionalNode, v); }
bool isOptionalNode() { return _flags.testAny(_isOptionalNode); }
void setIsChildDirectlyConnected(bool v = true) { _flags.set(_isChildDirectlyConnected, v); }
bool isChildDirectlyConnected() { return _flags.testAny(_isChildDirectlyConnected); }
void setIsSuccDirectlyConnected(bool v = true) { _flags.set(_isSuccDirectlyConnected, v); }
bool isSuccDirectlyConnected() { return _flags.testAny(_isSuccDirectlyConnected); }
void setIsInterestingConstant(bool v = true) { _flags.set(_isInterestingConstant, v); }
bool isInterestingConstant() { return _flags.testAny(_isInterestingConstant); }
void setIsNecessaryScreening(bool v = true) { _flags.set(_isNecessaryScreening, v); }
bool isNecessaryScreening() { return _flags.testAny(_isNecessaryScreening); }
void setIsLightScreening(bool v = true) { _flags.set(_isLightScreening, v); }
bool isLightScreening() { return _flags.testAny(_isLightScreening); }
bool isDataConnected() { return _flags.testAll(_isChildSimplyConnected |
_isParentSimplyConnected); }
bool isCFGConnected() { return _flags.testAll(_isSuccSimplyConnected |
_isPredSimplyConnected); }
bool isCFGDataConected() { return _flags.testAll(_isSuccSimplyConnected |
_isPredSimplyConnected |
_isChildSimplyConnected |
_isParentSimplyConnected); }
void setOutsideOfLoop(bool v = true) { _flags.set(_isOutsideOfLoop, v); }
bool isOutsideOfLoop() { return _flags.testAny(_isOutsideOfLoop); }
void setCISCNodeModified(bool v = true) { _flags.set(_isCISCNodeModified, v); }
bool isCISCNodeModified() { return _flags.testAny(_isCISCNodeModified); }
void setNewCISCNode(bool v = true) { _flags.set(_isNewCISCNode, v); }
bool isNewCISCNode() { return _flags.testAny(_isNewCISCNode); }
void setSkipParentsCheck(bool v = true) { _flags.set(_isSkipParentsCheck, v); }
bool isSkipParentsCheck() { return _flags.testAny(_isSkipParentsCheck); }
// short cut
bool isCommutative() { return _ilOpCode.isCommutative(); }
TR_CISCNode *getLatestDest()
{
TR_ASSERT( _opcode == TR_variable, "TR_CISCNode::getLatestDest()");
return _latestDest;
}
//bool checkParents(TR_CISCNode *, const int8_t level);
static bool checkParentsNonRec(TR_CISCNode *, TR_CISCNode *, int8_t level, TR::Compilation *);
void reverseBranchOpCodes();
static const char *getName(TR_CISCOps, TR::Compilation *);
void dump(TR::FILE *pOutFile, TR::Compilation *);
void printStdout();
void addChain(TR_CISCNode *tgt, bool alsoAddChainsTgt = false)
{
_chains.add(tgt);
if (alsoAddChainsTgt) tgt->_chains.add(this);
}
enum // flag bits
{
_isValidOtherInfo = 0x00000001,
_isStoreDirect = 0x00000002,
_isNegligible = 0x00000004,
_isSuccSimplyConnected = 0x00000008,
_isPredSimplyConnected = 0x00000010,
_isChildSimplyConnected = 0x00000020,
_isParentSimplyConnected = 0x00000040,
_isLoadVarDirect = 0x00000080,
_isEssentialNode = 0x00000100,
_isOptionalNode = 0x00000200,
_isChildDirectlyConnected = 0x00000400,
_isSuccDirectlyConnected = 0x00000800,
_isInterestingConstant = 0x00001000,
_isNecessaryScreening = 0x00002000,
_isLightScreening = 0x00004000,
_isOutsideOfLoop = 0x00008000,
_isCISCNodeModified = 0x00010000,
_isNewCISCNode = 0x00020000,
_isSkipParentsCheck = 0x00040000,
_dummyEnum
};
void initializeLists()
{
_preds.init();
_parents.init();
_dest.init();
_hintChildren.init();
_trNodeInfo.init();
initChains();
}
virtual void allocArrays(uint16_t ncfgs, uint16_t nchildren)
{
_succs = (TR_CISCNode **)(ncfgs ? trMemory()->allocateMemory(ncfgs * sizeof(*_succs), _allocKind) : 0);
_children = (TR_CISCNode **)(nchildren ? trMemory()->allocateMemory(nchildren * sizeof(*_children), _allocKind) : 0);
}
virtual void addPred(TR_CISCNode *t) { _preds.add(t); }
virtual void addParent(TR_CISCNode *t) { _parents.add(t); }
List<TR_CISCNode> *getPreds() { return &_preds; }
TR_CISCNode *getHeadOfPredecessors() { return _preds.getListHead()->getData(); }
List<TR_CISCNode> *getParents() { return &_parents; }
TR_CISCNode *getHeadOfParents() { return _parents.getListHead()->getData(); }
void addTrNode(TR::Block *b, TR::TreeTop *t, TR::Node *n)
{
TrNodeInfo *newInfo = (TrNodeInfo *)trMemory()->allocateMemory(sizeof(*newInfo), _allocKind);
newInfo->_block = b;
newInfo->_treeTop = t;
newInfo->_node = n;
_trNodeInfo.add(newInfo);
}
List<TrNodeInfo> *getTrNodeInfo() { return &_trNodeInfo; }
inline TrNodeInfo *getHeadOfTrNodeInfo() { return _trNodeInfo.getListHead()->getData(); }
inline TR::Node *getHeadOfTrNode() { return _trNodeInfo.getListHead()->getData()->_node; }
inline TR::TreeTop *getHeadOfTreeTop() { return _trNodeInfo.getListHead()->getData()->_treeTop; }
inline TR::Block *getHeadOfBlock() { return _trNodeInfo.getListHead()->getData()->_block; }
void setDest(TR_CISCNode *v)
{
TR_ASSERT(v != 0 && v->_opcode == TR_variable, "TR_CISCNode::setDest(), input error");
if (v->_latestDest)
{
TR_ASSERT(v->_latestDest->_dest.find(v), "TR_CISCNode::setDest(), input variable is not found in _latestDest");
v->_latestDest->_dest.remove(v);
}
ListAppender<TR_CISCNode> appender(&_dest);
appender.add(v);
v->_latestDest = this;
}
TR_CISCNode *getFirstDest()
{
return _dest.isEmpty() ? 0 : _dest.getListHead()->getData();
}
List<TR_CISCNode> *getHintChildren() { return &_hintChildren; }
void addHint(TR_CISCNode *n) { _hintChildren.add(n); }
bool isEmptyHint() { return _hintChildren.isEmpty(); }
List<TR_CISCNode> *getChains() {return &_chains; }
bool checkDagIdInChains();
TR::TreeTop *getDestination(bool isFallThrough = false);
void deadAllChildren();
TR_CISCNode *duplicate()
{
TR_CISCNode *ret = new (trHeapMemory()) TR_CISCNode(_trMemory, _opcode, _dt, _id, _dagId, _numSuccs, _numChildren);
ret->_otherInfo = _otherInfo;
ret->_flags = _flags;
// duplicate _trNodeIndo
ListIterator<TrNodeInfo> li(&_trNodeInfo);
ListAppender<TrNodeInfo> appender(&ret->_trNodeInfo);
for (TrNodeInfo *t = li.getFirst(); t; t = li.getNext()) appender.add(t);
// Duplicating other lists will be performed in the caller.
return ret;
}
// Variables
protected:
void setOpcode(uint32_t opc)
{
_opcode = opc;
_ilOpCode.setOpCodeValue(opc < TR::NumAllIlOps ? (TR::ILOpCodes)opc : TR::BadILOp);
}
uint32_t _opcode; // TR::ILOpCodes enum
TR::ILOpCode _ilOpCode;
TR::DataType _dt;
TR_CISCNode **_succs;
TR_CISCNode **_children;
TR_CISCNode *_latestDest;
int32_t _otherInfo;
uint16_t _numSuccs;
uint16_t _numChildren;
uint16_t _id;
uint16_t _dagId;
flags32_t _flags;
TR_AllocationKind _allocKind;
TR_Memory * _trMemory;
List<TR_CISCNode> _preds;
List<TR_CISCNode> _parents;
List<TR_CISCNode> _dest; // destination operands
List<TR_CISCNode> _chains;
List<TR_CISCNode> _hintChildren;
List<TrNodeInfo> _trNodeInfo;
};
// Use persistent allocation
class TR_PCISCNode : public TR_CISCNode
{
public:
TR_PCISCNode(TR_Memory * m, uint32_t opc, TR::DataType dt, uint16_t id, int16_t dagId, uint16_t ncfgs, uint16_t nchildren)
: TR_CISCNode(m, opc, dt, id, dagId, ncfgs, nchildren, persistentAlloc)
{
};
TR_PCISCNode(TR_Memory * m, uint32_t opc, TR::DataType dt, uint16_t id, int16_t dagId, uint16_t ncfgs, uint16_t nchildren, TR_CISCNode *pred)
: TR_CISCNode(m, opc, dt, id, dagId, ncfgs, nchildren, pred, persistentAlloc)
{
}
TR_PCISCNode(TR_Memory * m, uint32_t opc, TR::DataType dt, uint16_t id, int16_t dagId, uint16_t ncfgs, uint16_t nchildren, TR_CISCNode *pred, TR_CISCNode *child0)
: TR_CISCNode(m, opc, dt, id, dagId, ncfgs, nchildren, pred, child0, persistentAlloc)
{
}
TR_PCISCNode(TR_Memory * m, uint32_t opc, TR::DataType dt, uint16_t id, int16_t dagId, uint16_t ncfgs, uint16_t nchildren, TR_CISCNode *pred, TR_CISCNode *child0, TR_CISCNode *child1)
: TR_CISCNode(m, opc, dt, id, dagId, ncfgs, nchildren, pred, child0, child1, persistentAlloc)
{
}
TR_PCISCNode(TR_Memory * m, uint32_t opc, TR::DataType dt, uint16_t id, int16_t dagId, uint16_t ncfgs, uint16_t nchildren, int32_t otherInfo)
: TR_CISCNode(m, opc, dt, id, dagId, ncfgs, nchildren, otherInfo, persistentAlloc)
{
}
TR_PCISCNode(TR_Memory * m, uint32_t opc, TR::DataType dt, uint16_t id, int16_t dagId, uint16_t ncfgs, uint16_t nchildren, int32_t otherInfo, TR_CISCNode *pred)
: TR_CISCNode(m, opc, dt, id, dagId, ncfgs, nchildren, otherInfo, pred, persistentAlloc)
{
}
TR_PCISCNode *getSucc(uint32_t index)
{
return (TR_PCISCNode *) TR_CISCNode::getSucc(index);
}
TR_PCISCNode *getChild(uint32_t index)
{
return (TR_PCISCNode *) TR_CISCNode::getChild(index);
}
virtual void addPred(TR_CISCNode *t)
{
TR_PersistentList<TR_CISCNode> *p = (TR_PersistentList<TR_CISCNode> *)&_preds;
p->add(t);
}
virtual void addParent(TR_CISCNode *t)
{
TR_PersistentList<TR_CISCNode> *p = (TR_PersistentList<TR_CISCNode> *)&_parents;
p->add(t);
}
virtual void allocArrays(uint16_t ncfgs, uint16_t nchildren)
{
_succs = (TR_CISCNode **)(ncfgs ? jitPersistentAlloc(ncfgs * sizeof(*_succs)) : 0);
_children = (TR_CISCNode **)(nchildren ? jitPersistentAlloc(nchildren * sizeof(*_children)) : 0);
}
};
class TR_CISCHash
{
public:
TR_ALLOC(TR_Memory::LoopTransformer);
TR_CISCHash(TR_Memory * m, uint32_t num = 0, TR_AllocationKind allocKind = heapAlloc)
:_trMemory(m)
{
if (num > 0)
setNumBuckets(num, allocKind);
else
{
_numBuckets = 0;
_buckets = 0;
_allocationKind = allocKind;
}
}
TR_Memory * trMemory() { return _trMemory; }
void setNumBuckets(uint32_t num, TR_AllocationKind allocKind)
{
_allocationKind = allocKind;
setNumBuckets(num);
}
void setNumBuckets(uint32_t num)
{
TR_ASSERT(num > 0, "error: num == 0!");
_numBuckets = num;
uint32_t size = num * sizeof(*_buckets);
_buckets = (HashTableEntry **)trMemory()->allocateMemory(size, _allocationKind);
memset(_buckets, 0, size);
}
struct HashTableEntry
{
HashTableEntry *_next;
uint64_t _key;
TR_CISCNode *_node;
};
TR_CISCNode *find(uint64_t key);
bool add(uint64_t key, TR_CISCNode *, bool checkExist = false);
uint32_t getNumBuckets() { return _numBuckets; }
private:
uint32_t _numBuckets;
HashTableEntry **_buckets;
TR_Memory * _trMemory;
TR_AllocationKind _allocationKind;
};
enum TR_GraphAspects // flag bits
{
storeShiftCount = 12,
nbValue = (0xFF & ~ILTypeProp::Size_1),
existAccess = 0x00000100,
loadMasks = 0x000001FF,
storeMasks = 0x001FF000,
sameTypeLoadStore = 0x00200000,
bitop1 = 0x00800000,
iadd = 0x01000000,
isub = 0x02000000,
call = 0x04000000,
shr = 0x08000000,
bndchk = 0x10000000,
reminder = 0x20000000,
division = 0x40000000,
mul = 0x80000000
};
class TR_CISCGraphAspects : public flags32_t
{
public:
TR_CISCGraphAspects() {init();}
void init() {clear();}
void setLoadAspects(uint32_t val, bool orExistAccess = true); // assuming val as a combination of ILTypeProp::*
void setStoreAspects(uint32_t val, bool orExistAccess = true); // assuming val as a combination of ILTypeProp::*
void modifyAspects();
uint32_t getLoadAspects() { return getValue(loadMasks); }
uint32_t getStoreAspects() { return getValue(storeMasks) >> storeShiftCount; }
virtual void print(TR::Compilation *, bool noaspects);
};
class TR_CISCGraphAspectsWithCounts : public TR_CISCGraphAspects
{
public:
TR_CISCGraphAspectsWithCounts() {init();}
void init() {TR_CISCGraphAspects::init(); setMinCounts(0,0,0);}
void setAspectsByOpcode(int opc);
void setAspectsByOpcode(TR_CISCNode *n) { setAspectsByOpcode(n->getOpcode()); }
void print(TR::Compilation *, bool noaspects);
void setMinCounts(uint8_t ifCount, uint8_t iloadCount, uint8_t istoreCount)
{ _ifCount = ifCount; _indirectLoadCount = iloadCount; _indirectStoreCount = istoreCount; }
uint8_t incIfCount() { return ++_ifCount; }
uint8_t incIndirectLoadCount() { return ++_indirectLoadCount; }
uint8_t incIndirectStoreCount() { return ++_indirectStoreCount; }
uint8_t getIfCount() { return _ifCount; }
uint8_t getIndirectLoadCount() { return _indirectLoadCount; }
uint8_t getIndirectStoreCount() { return _indirectStoreCount; }
bool meetMinCounts(TR_CISCGraphAspectsWithCounts *p) // p is for the pattern
{
return (_ifCount >= p->_ifCount &&
_indirectLoadCount >= p->_indirectLoadCount &&
_indirectStoreCount >= p->_indirectStoreCount);
}
protected:
uint8_t _ifCount;
uint8_t _indirectLoadCount;
uint8_t _indirectStoreCount;
};
template <class T> class TR_ListDuplicator
{
public:
TR_ALLOC(TR_Memory::LoopTransformer);
TR_HeapMemory trHeapMemory() { return _trMemory; }
TR_ListDuplicator<T>(TR_Memory * m)
: _trMemory(m), _list(0)
{
_flags.clear();
}
virtual void setList(List<T>* l) { setRegistered(); setDuplicated(false); _list = l; }
List<T>* getList() { TR_ASSERT(isRegistered(), "error"); return _list; }
virtual List<T>* duplicateList(bool ifNecessary = true)
{
if (ifNecessary && isDuplicated()) return _list;
setDuplicated();
List<T>* ret = new (trHeapMemory()) List<T>(_trMemory);
ListAppender<T> appender(ret);
ListIterator<T> li(_list);
T *t;
for (t = li.getFirst(); t; t = li.getNext()) appender.add(t);
_list = ret;
return ret;
}
enum // flag bits
{
_isRegistered = 0x0001,
_isDuplicated = 0x0002,
_isDuplicateElement = 0x0004,
_dummyEnum
};
void setRegistered(bool v = true) { _flags.set(_isRegistered, v); }
bool isRegistered() { return _flags.testAny(_isRegistered); }
void setDuplicated(bool v = true) { _flags.set(_isDuplicated, v); }
bool isDuplicated() { return _flags.testAny(_isDuplicated); }
void setDuplicateElement(bool v = true) { _flags.set(_isDuplicateElement, v); }
bool isDuplicateElement() { return _flags.testAny(_isDuplicateElement); }
protected:
List<T> *_list;
flags8_t _flags;
TR_Memory * _trMemory;
};
class ListOfCISCNodeDuplicator : public TR_ListDuplicator<TR_CISCNode>
{
public:
ListOfCISCNodeDuplicator(TR_Memory * m) : TR_ListDuplicator<TR_CISCNode>(m), _mapping(m) {};
virtual void setList(List<TR_CISCNode>* l)
{
TR_ListDuplicator<TR_CISCNode>::setList(l);
_mapping.init();
}
virtual List<TR_CISCNode> *duplicateList(bool ifNecessary = true)
{
if (ifNecessary && isDuplicated()) return _list;
setDuplicated();
List<TR_CISCNode>* ret = new (trHeapMemory()) List<TR_CISCNode>(_trMemory);
ListAppender<TR_CISCNode> appender(ret);
ListIterator<TR_CISCNode> li(_list);
TR_CISCNode *t;
_list = ret;
if (isDuplicateElement())
{
TR_CISCNode *newCISC;
for (t = li.getFirst(); t; t = li.getNext())
{
newCISC = t->duplicate();
appender.add(newCISC);
TR_Pair<TR_CISCNode,TR_CISCNode> *pair = new (trHeapMemory()) TR_Pair<TR_CISCNode,TR_CISCNode>(t, newCISC);
_mapping.add(pair);
}
ListIterator<TR_CISCNode> liNew(ret);
for (t = li.getFirst(), newCISC = liNew.getFirst(); t; t = li.getNext(), newCISC = liNew.getNext())
{
int32_t i;
for (i = 0; i < t->getNumChildren(); i++)
newCISC->setChild(i, findCorrespondingNode(t->getChild(i)));
for (i = 0; i < t->getNumSuccs(); i++)
newCISC->setSucc(i, findCorrespondingNode(t->getSucc(i)));
restructureList(t->getChains(), newCISC->getChains());
restructureList(t->getHintChildren(), newCISC->getHintChildren());
}
}
else
{
for (t = li.getFirst(); t; t = li.getNext()) { appender.add(t); }
}
return ret;
}
TR_CISCNode *findCorrespondingNode(TR_CISCNode *old)
{
TR_Pair<TR_CISCNode,TR_CISCNode> *pair;
ListElement<TR_Pair<TR_CISCNode,TR_CISCNode> > *le;
// Search for old
for (le = _mapping.getListHead(); le; le = le->getNextElement())
{
pair = le->getData();
if (pair->getKey() == old) return pair->getValue();
}
TR_ASSERT(false, "error");
return NULL;
}
void restructureList(List<TR_CISCNode> *oldList, List<TR_CISCNode> *newList)
{
TR_ASSERT(newList->isEmpty(), "error");
ListAppender<TR_CISCNode> appender(newList);
ListIterator<TR_CISCNode> li(oldList);
TR_CISCNode *t;
for (t = li.getFirst(); t; t = li.getNext()) appender.add(findCorrespondingNode(t));
}
protected:
List<TR_Pair<TR_CISCNode,TR_CISCNode> > _mapping;
};
#define MAX_IMPORTANT_NODES 8
#define MAX_SPECIALCARE_NODES 4
typedef bool (* TransformerPtr)(TR_CISCTransformer *);
typedef bool (* SpecialNodeTransformerPtr)(TR_CISCTransformer *);
class TR_CISCGraph
{
public:
TR_ALLOC(TR_Memory::LoopTransformer);
static void setEssentialNodes(TR_CISCGraph*);
static void makePreparedCISCGraphs(TR::Compilation *c);
static void initializeGraphs(TR::Compilation *c);
TR_CISCGraph(TR_Memory * m, const char *title = 0, int32_t numHashTrNode = 31, int32_t numHashOpc = 17)
: _titleOfCISC(title), _entryNode(0), _exitNode(0), _numNodes(0), _numDagIds(0),
_dagId2Nodes(0), _transformer(0), _specialNodeTransformer(0), _hotness(noOpt),
_javaSource(0), _versionLength(0), _trMemory(m),
_trNode2CISCNode(m), _opc2CISCNode(m), _nodes(m), _orderByData(m),
_duplicatorNodes(m), _patternType(-1)
{
initializeLists();
_flags.clear();
_aspects.init();
if (numHashTrNode > 0) _trNode2CISCNode.setNumBuckets(numHashTrNode);
if (numHashOpc > 0) _opc2CISCNode.setNumBuckets(numHashOpc);
int32_t i;
for (i = MAX_IMPORTANT_NODES; --i >= 0; ) _importantNode[i] = 0;
for (i = MAX_SPECIALCARE_NODES; --i >= 0; ) _specialCareNode[i] = 0;
}
TR_CISCNode *getCISCNode(TR::Node *n) { return _trNode2CISCNode.find(getHashKeyTrNode(n)); }
TR_CISCNode *getCISCNode(uint32_t opc, bool validOther, int32_t other)
{
return _opc2CISCNode.find(getHashKeyOpcs(opc, validOther, other));
}
void setEntryNode(TR_CISCNode *n) {_entryNode = n;}
TR_CISCNode *getEntryNode() {return _entryNode;}
void setExitNode(TR_CISCNode *n) {_exitNode = n;}
TR_CISCNode *getExitNode() {return _exitNode;}
void setImportantNode(uint32_t idx, TR_CISCNode *n)
{
TR_ASSERT(idx < MAX_IMPORTANT_NODES, "TR_CISCGraph::setImportantNode index out of range");
_importantNode[idx] = n;
}
void setImportantNodes(TR_CISCNode *n1, TR_CISCNode *n2)
{
setImportantNode(0, n1);
setImportantNode(1, n2);
}
void setImportantNodes(TR_CISCNode *n1, TR_CISCNode *n2, TR_CISCNode *n3)
{
setImportantNode(0, n1);
setImportantNode(1, n2);
setImportantNode(2, n3);
}
void setImportantNodes(TR_CISCNode *n1, TR_CISCNode *n2, TR_CISCNode *n3, TR_CISCNode *n4)
{
setImportantNode(0, n1);
setImportantNode(1, n2);
setImportantNode(2, n3);
setImportantNode(3, n4);
}
void setImportantNodes(TR_CISCNode *n1, TR_CISCNode *n2, TR_CISCNode *n3, TR_CISCNode *n4, TR_CISCNode *n5)
{
setImportantNode(0, n1);
setImportantNode(1, n2);
setImportantNode(2, n3);
setImportantNode(3, n4);
setImportantNode(4, n5);
}
void setImportantNodes(TR_CISCNode *n1, TR_CISCNode *n2, TR_CISCNode *n3, TR_CISCNode *n4, TR_CISCNode *n5, TR_CISCNode *n6)
{
setImportantNode(0, n1);
setImportantNode(1, n2);
setImportantNode(2, n3);
setImportantNode(3, n4);
setImportantNode(4, n5);
setImportantNode(5, n6);
}
void setImportantNodes(TR_CISCNode *n1, TR_CISCNode *n2, TR_CISCNode *n3, TR_CISCNode *n4, TR_CISCNode *n5, TR_CISCNode *n6, TR_CISCNode *n7)
{
setImportantNode(0, n1);
setImportantNode(1, n2);
setImportantNode(2, n3);
setImportantNode(3, n4);
setImportantNode(4, n5);
setImportantNode(5, n6);
setImportantNode(6, n7);
}
void setImportantNodes(TR_CISCNode *n1, TR_CISCNode *n2, TR_CISCNode *n3, TR_CISCNode *n4, TR_CISCNode *n5, TR_CISCNode *n6, TR_CISCNode *n7, TR_CISCNode *n8)
{
setImportantNode(0, n1);
setImportantNode(1, n2);
setImportantNode(2, n3);
setImportantNode(3, n4);
setImportantNode(4, n5);
setImportantNode(5, n6);
setImportantNode(6, n7);
setImportantNode(7, n8);
}
TR_CISCNode *getImportantNode(uint32_t idx)
{
TR_ASSERT(idx < MAX_IMPORTANT_NODES, "TR_CISCGraph::getImportantNode index out of range");
return _importantNode[idx];
}
void setSpecialCareNode(uint32_t idx, TR_CISCNode *n)
{
TR_ASSERT(idx < MAX_SPECIALCARE_NODES, "TR_CISCGraph::setSpecialCareNode index out of range");
_specialCareNode[idx] = n;
}
TR_CISCNode *getSpecialCareNode(uint32_t idx)
{
TR_ASSERT(idx < MAX_SPECIALCARE_NODES, "TR_CISCGraph::getSpecialCareNode index out of range");
return _specialCareNode[idx];
}
TR_Memory * trMemory() { return _trMemory; }
uint16_t getNumNodes() {return _numNodes;}
uint16_t incNumNodes() {return _numNodes++;}
uint16_t getNumDagIds() {return _numDagIds;}
void setNumDagIds(uint16_t n) {_numDagIds = n;}
TR_CISCNode *getHeadOfNodes()
{
return _nodes.isEmpty() ? 0 : _nodes.getListHead()->getData();
}
void addTrNode(TR_CISCNode *n, TR::Block *block, TR::TreeTop *top, TR::Node *trNode);
virtual void addNode(TR_CISCNode *n, TR::Block *block = 0, TR::TreeTop *top = 0, TR::Node *trNode = 0);
void createInternalData(int32_t loopBodyDagId)
{
createDagId2NodesTable();
createOrderByData();
setOutsideOfLoopFlag(loopBodyDagId);
}
bool isDagIdCycle(uint16_t dagId) { TR_ASSERT(dagId < _numDagIds, "error"); return getDagId2Nodes()[dagId].isMultipleEntry(); }
bool isDagIdDag(uint16_t dagId) { TR_ASSERT(dagId < _numDagIds, "error"); return getDagId2Nodes()[dagId].isSingleton(); }
void dump(TR::FILE *pOutFile, TR::Compilation *);
void importUDchains(TR::Compilation *comp, TR_UseDefInfo *useDefInfo, bool reinitialize = false);
void setTransformer(TransformerPtr t) { _transformer = t; }
TransformerPtr getTransformer() { return _transformer; }
bool isPatternGraph() { return _transformer != 0; }
void setSpecialNodeTransformer(SpecialNodeTransformerPtr t) { _specialNodeTransformer = t; }
SpecialNodeTransformerPtr getSpecialNodeTransformer() { return _specialNodeTransformer; }
// flags
bool isSetUDDUchains() { return _flags.testAny(_isSetUDDUchains); }
void setIsSetUDDUchains(bool v = true) { _flags.set(_isSetUDDUchains, v); }
bool isInhibitAfterVersioning() { TR_ASSERT(isPatternGraph(), "error"); return _flags.testAny(_inhibitAfterVersioning); }
void setInhibitAfterVersioning(bool v = true) { TR_ASSERT(isPatternGraph(), "error"); _flags.set(_inhibitAfterVersioning, v); }
bool isInhibitBeforeVersioning() { TR_ASSERT(isPatternGraph(), "error"); return _flags.testAny(_inhibitBeforeVersioning); }
void setInhibitBeforeVersioning(bool v = true) { TR_ASSERT(isPatternGraph(), "error"); _flags.set(_inhibitBeforeVersioning, v); }
bool isInsideOfFastVersioned() { TR_ASSERT(!isPatternGraph(), "error"); return _flags.testAny(_insideOfFastVersioned); }
void setInsideOfFastVersioned(bool v = true) { TR_ASSERT(!isPatternGraph(), "error"); _flags.set(_insideOfFastVersioned, v); }
bool isRequireAHconst() { TR_ASSERT(isPatternGraph(), "error"); return _flags.testAny(_requireAHconst); }
void setRequireAHconst(bool v = true) { TR_ASSERT(isPatternGraph(), "error"); _flags.set(_requireAHconst, v); }
bool isHighFrequency() { return _flags.testAny(_highFrequency); }
void setHighFrequency(bool v = true) { _flags.set(_highFrequency, v); }
bool isNoFragmentDagId() { return _flags.testAny(_noFragmentDagId); }
void setNoFragmentDagId(bool v = true) { _flags.set(_noFragmentDagId, v); }
bool isRecordingAspectsByOpcode() { return _flags.testAny(_recordingAspectsByOpcode); }
void setRecordingAspectsByOpcode(bool v = true) { _flags.set(_recordingAspectsByOpcode, v); }
bool needsInductionVariableInit() { return _flags.testAny(_needsInductionVariableInit); }
void setNeedsInductionVariableInit(bool v = false) { _flags.set(_needsInductionVariableInit, v); }
enum // flag bits
{
_isSetUDDUchains = 0x0001,
_inhibitAfterVersioning = 0x0002, // for compilation time reduction
_inhibitBeforeVersioning = 0x0004, // for compilation time reduction
_insideOfFastVersioned = _inhibitAfterVersioning,
_highFrequency = 0x0008,
_noFragmentDagId = 0x0010,
_recordingAspectsByOpcode = 0x0020,
_requireAHconst = 0x0040,
_needsInductionVariableInit = 0x0080,
_dummyEnum
};
void initializeLists()
{
_nodes.init();
_orderByData.init();
}
List<TR_CISCNode> *getNodes() { return &_nodes; }
List<TR_CISCNode> *getDagId2Nodes() { return _dagId2Nodes; }
List<TR_CISCNode> *getOrderByData() { return &_orderByData; }
ListOfCISCNodeDuplicator *getDuplicatorNodes() { return &_duplicatorNodes; }
virtual void createDagId2NodesTable();
virtual void createOrderByData();
const char *getTitle() { return _titleOfCISC; }
void setMemAspects(uint32_t load, uint32_t store)
{
_aspects.setLoadAspects(load);
_aspects.setStoreAspects(store);
}
void setAspects(uint32_t val) { _aspects.set(val); }
void setAspects(uint32_t val, uint32_t load, uint32_t store)
{
setAspects(val);
setMemAspects(load, store);
}
uint32_t getAspectsValue() { return _aspects.getValue(); }
bool testAllAspects(TR_CISCGraph *P) { return _aspects.testAll(P->getAspectsValue()); }
void modifyTargetGraphAspects() { _aspects.modifyAspects(); }
void setNoMemAspects(uint32_t load, uint32_t store)
{
_noaspects.setLoadAspects(load, false);
_noaspects.setStoreAspects(store, false);
}
void setNoAspects(uint32_t val) { _noaspects.set(val); }
void setNoAspects(uint32_t val, uint32_t load, uint32_t store)
{
setNoAspects(val);
setNoMemAspects(load, store);
}
uint32_t getNoAspectsValue() { return _noaspects.getValue(); }
bool testAnyNoAspects(TR_CISCGraph *P) { return _aspects.testAny(P->getNoAspectsValue()); }
TR_Hotness getHotness() { return _hotness; }
void setHotness(TR_Hotness hotness) { _hotness = hotness; }
void setHotness(TR_Hotness hotness, bool highFreq) { setHotness(hotness); setHighFrequency(highFreq); }
int32_t defragDagId();
void setJavaSource(char *s) { _javaSource = s; }
char *getJavaSource() { return _javaSource; }
TR_CISCGraphAspectsWithCounts * getAspects() { return &_aspects; }
void setMinCounts(uint8_t ifCount, uint8_t iloadCount, uint8_t istoreCount) { _aspects.setMinCounts(ifCount,iloadCount,istoreCount);}
bool meetMinCounts(TR_CISCGraph *p) { return _aspects.meetMinCounts(&p->_aspects); }
uint16_t getVersionLength() { return _versionLength; }
void setVersionLength(uint16_t len) { _versionLength = len; }