-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructure.cpp
3553 lines (3112 loc) · 121 KB
/
Structure.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
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
#include <sstream>
#include <fstream>
#include <string>
#include <cassert>
#include <iomanip>
#include <cereal/archives/json.hpp>
#include <cereal/types/vector.hpp>
#include <cereal/types/memory.hpp>
#include <cereal/types/polymorphic.hpp>
#include <cfloat>
#include "llvm/Crellvm/Structure.h"
#include "llvm/Crellvm/ValidationUnit.h"
#include "llvm/Crellvm/Infrules.h"
#include "llvm/Crellvm/RuntimeOptions.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/StringExtras.h"
namespace cereal {
[[noreturn]] void throw_exception(std::exception const &e) { std::exit(1); }
}
namespace boost {
void throw_exception(std::exception const &e) {
std::cerr << " boost::throw_exception(e) called." << std::endl;
std::cerr << e.what() << std::endl;
std::exit(1);
}
}
namespace {
std::string toString(crellvm::TyPostpropInfo::POSTPROP_OPT opt) {
//enum POSTPROP_OPT { POSTPROP_GVN = 0, POSTPROP_NONE };
switch (opt) {
case crellvm::TyPostpropInfo::POSTPROP_GVN:
return std::string("POSTPROP_GVN");
case crellvm::TyPostpropInfo::POSTPROP_NONE:
return std::string("POSTPROP_NONE");
default:
assert(false && "POSTPROP_OPT toString");
}
return "";
}
std::string toString(crellvm::CoreHint::RETURN_CODE return_code) {
switch (return_code) {
case crellvm::CoreHint::ACTUAL:
return std::string("ACTUAL");
case crellvm::CoreHint::ADMITTED:
return std::string("ADMITTED");
case crellvm::CoreHint::FAIL:
return std::string("FAIL");
default:
assert(false && "RETURN_CODE toString");
}
return "";
}
std::string toString(crellvm::CoreHint::AUTO_OPT auto_opt) {
switch (auto_opt) {
case crellvm::CoreHint::AUTO_GVN:
return std::string("AUTO_GVN");
case crellvm::CoreHint::AUTO_SROA:
return std::string("AUTO_SROA");
case crellvm::CoreHint::AUTO_INSTCOMBINE:
return std::string("AUTO_INSTCOMBINE");
case crellvm::CoreHint::AUTO_LICM:
return std::string("AUTO_LICM");
case crellvm::CoreHint::AUTO_DEFAULT:
return std::string("AUTO_DEFAULT");
default:
assert(false && "RETURN_CODE toString");
}
return "";
}
std::string toString(crellvm::TyScope scope) {
switch (scope) {
case crellvm::Source:
return std::string("Source");
case crellvm::Target:
return std::string("Target");
default:
assert(false && "Scope toString");
}
return "";
}
std::string toString(crellvm::TyTag tag) {
switch (tag) {
case crellvm::Physical:
return std::string("Physical");
case crellvm::Previous:
return std::string("Previous");
case crellvm::Ghost:
return std::string("Ghost");
default:
assert(false && "Tag toString");
}
return "";
}
unsigned int getRawInstrIndex(const llvm::Instruction &instr) {
const llvm::BasicBlock *parent = instr.getParent();
const llvm::BasicBlock::InstListType &instList = parent->getInstList();
unsigned int idx = 0;
for (llvm::BasicBlock::const_iterator itr = instList.begin();
itr != instList.end(); ++itr) {
if (&instr == &(*itr))
return idx;
idx++;
}
return (unsigned int)-1;
}
} // anonymous
// Below code PrintEscapedString is exactly copied from lib/IR/AsmWriter.cpp
// PrintEscapedString - Print each character of the specified string, escaping
// it if it is not printable or if it is an escape char.
static void PrintEscapedString(llvm::StringRef Name, llvm::raw_ostream &Out) {
for (unsigned i = 0, e = Name.size(); i != e; ++i) {
unsigned char C = Name[i];
if (isprint(C) && C != '\\' && C != '"')
Out << C;
else
Out << '\\' << llvm::hexdigit(C >> 4) << llvm::hexdigit(C & 0x0F);
}
}
// Below code PrefixType is exactly copied from lib/IR/AsmWriter.cpp
enum PrefixType {
GlobalPrefix,
ComdatPrefix,
LabelPrefix,
LocalPrefix,
NoPrefix
};
// Below code PrintLLVMName is exactly copied from lib/IR/AsmWriter.cpp
/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
/// prefixed with % (if the string only contains simple characters) or is
/// surrounded with ""'s (if it has special chars in it). Print it out.
static void PrintLLVMName(llvm::raw_ostream &OS, llvm::StringRef Name, PrefixType Prefix) {
assert(!Name.empty() && "Cannot get empty name!");
switch (Prefix) {
case NoPrefix: break;
case GlobalPrefix: OS << '@'; break;
case ComdatPrefix: OS << '$'; break;
case LabelPrefix: break;
case LocalPrefix: OS << '%'; break;
}
// Scan the name to see if it needs quotes first.
bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
if (!NeedsQuotes) {
for (unsigned i = 0, e = Name.size(); i != e; ++i) {
// By making this unsigned, the value passed in to isalnum will always be
// in the range 0-255. This is important when building with MSVC because
// its implementation will assert. This situation can arise when dealing
// with UTF-8 multibyte characters.
unsigned char C = Name[i];
if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' &&
C != '_') {
NeedsQuotes = true;
break;
}
}
}
// If we didn't need any quotes, just write out the name in one blast.
if (!NeedsQuotes) {
OS << Name;
return;
}
// Okay, we need quotes. Output the quotes and escape any scary characters as
// needed.
OS << '"';
PrintEscapedString(Name, OS);
OS << '"';
}
// Below code PrintLLVMName is exactly copied from lib/IR/AsmWriter.cpp
/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
/// prefixed with % (if the string only contains simple characters) or is
/// surrounded with ""'s (if it has special chars in it). Print it out.
static void PrintLLVMName(llvm::raw_ostream &OS, const llvm::Value *V) {
PrintLLVMName(OS, V->getName(),
llvm::isa<llvm::GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
}
namespace crellvm {
/// @return the index of the BasicBlock w.r.t. the parent function.
std::string getBasicBlockIndex(const llvm::BasicBlock *block) {
if (!block || !(block->getParent())) {
std::stringstream retStream;
retStream << ((unsigned int)-1);
return retStream.str();
}
// If a block has its own name, just return it.
if (block->hasName()) {
std::string tempstr;
llvm::raw_string_ostream rso(tempstr);
PrintLLVMName(rso, block);
assert(rso.str()[0] == '%');
return rso.str().substr(1); //to remove %
}
// If else, calculate the index and return it.
const llvm::Function *parent = block->getParent();
const llvm::Function::BasicBlockListType &blockList =
parent->getBasicBlockList();
unsigned int idx = 0;
for (llvm::Function::const_iterator itr = blockList.begin();
itr != blockList.end(); ++itr) {
if (block == &(*itr)) {
std::stringstream retStream;
retStream << idx;
return (retStream.str());
}
idx++;
}
std::stringstream retStream;
retStream << ((unsigned int)-1);
return retStream.str();
}
std::string getVariable(const llvm::Value &value) {
if (value.hasName()) {
std::string tempstr;
llvm::raw_string_ostream rso(tempstr);
PrintLLVMName(rso, &value);
return rso.str();
}
return "";
}
llvm::Instruction *getPHIResolved(llvm::Instruction *I, llvm::BasicBlock *PB) {
llvm::Instruction *result = I->clone();
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
llvm::Value *Op = I->getOperand(i);
if (llvm::PHINode *OpPHI = llvm::dyn_cast<llvm::PHINode>(Op)) {
if (I->getParent() != OpPHI->getParent())
continue;
result->setOperand(i, OpPHI->getIncomingValueForBlock(PB));
}
}
return result;
}
std::string toString(crellvm::TyFbop bop) {
switch (bop) {
case crellvm::BopFadd:
return std::string("BopFadd");
case crellvm::BopFsub:
return std::string("BopFsub");
case crellvm::BopFmul:
return std::string("BopFmul");
case crellvm::BopFdiv:
return std::string("BopFdiv");
case crellvm::BopFrem:
return std::string("BopFrem");
default:
assert(false && "Fbop toString");
}
return "";
}
std::string toString(crellvm::TyBop bop) {
switch (bop) {
case crellvm::BopAdd:
return std::string("BopAdd");
case crellvm::BopSub:
return std::string("BopSub");
case crellvm::BopMul:
return std::string("BopMul");
case crellvm::BopUdiv:
return std::string("BopUdiv");
case crellvm::BopSdiv:
return std::string("BopSdiv");
case crellvm::BopUrem:
return std::string("BopUrem");
case crellvm::BopSrem:
return std::string("BopSrem");
case crellvm::BopShl:
return std::string("BopShl");
case crellvm::BopLshr:
return std::string("BopLshr");
case crellvm::BopAshr:
return std::string("BopAshr");
case crellvm::BopAnd:
return std::string("BopAnd");
case crellvm::BopOr:
return std::string("BopOr");
case crellvm::BopXor:
return std::string("BopXor");
default:
assert(false && "Bop toString");
}
return "";
}
std::string toString(crellvm::TyFloatType float_type) {
switch (float_type) {
case crellvm::HalfType:
return std::string("HalfType");
case crellvm::FloatType:
return std::string("FloatType");
case crellvm::DoubleType:
return std::string("DoubleType");
case crellvm::FP128Type:
return std::string("FP128Type");
case crellvm::PPC_FP128Type:
return std::string("PPC_FP128Type");
case crellvm::X86_FP80Type:
return std::string("X86_FP80Type");
default:
assert(false && "FloatType toString");
}
return "";
}
std::string toString(crellvm::TyIcmpPred cond) {
switch (cond) {
case crellvm::CondEq:
return std::string("CondEq");
case crellvm::CondNe:
return std::string("CondNe");
case crellvm::CondUgt:
return std::string("CondUgt");
case crellvm::CondUge:
return std::string("CondUge");
case crellvm::CondUlt:
return std::string("CondUlt");
case crellvm::CondUle:
return std::string("CondUle");
case crellvm::CondSgt:
return std::string("CondSgt");
case crellvm::CondSge:
return std::string("CondSge");
case crellvm::CondSlt:
return std::string("CondSlt");
case crellvm::CondSle:
return std::string("CondSle");
default:
assert(false && "Cond toString");
}
return "";
}
std::string toString(crellvm::TyFcmpPred fcond) {
switch (fcond) {
case crellvm::CondFfalse:
return std::string("CondFfalse");
case crellvm::CondFoeq:
return std::string("CondFoeq");
case crellvm::CondFogt:
return std::string("CondFogt");
case crellvm::CondFoge:
return std::string("CondFoge");
case crellvm::CondFolt:
return std::string("CondFolt");
case crellvm::CondFole:
return std::string("CondFole");
case crellvm::CondFone:
return std::string("CondFone");
case crellvm::CondFord:
return std::string("CondFord");
case crellvm::CondFuno:
return std::string("CondFuno");
case crellvm::CondFueq:
return std::string("CondFueq");
case crellvm::CondFugt:
return std::string("CondFugt");
case crellvm::CondFuge:
return std::string("CondFuge");
case crellvm::CondFult:
return std::string("CondFult");
case crellvm::CondFule:
return std::string("CondFule");
case crellvm::CondFune:
return std::string("CondFune");
case crellvm::CondFtrue:
return std::string("CondFtrue");
default:
assert("crellvm::toString(crellvm::TyFCond fcond) : unknown fcond" &&
false);
}
return "";
}
crellvm::TyFloatType getFloatType(llvm::Type *typ) {
crellvm::TyFloatType fty;
if (typ->isHalfTy())
fty = crellvm::HalfType;
else if (typ->isFloatTy())
fty = crellvm::FloatType;
else if (typ->isDoubleTy())
fty = crellvm::DoubleType;
else if (typ->isX86_FP80Ty())
fty = crellvm::X86_FP80Type;
else if (typ->isFP128Ty())
fty = crellvm::FP128Type;
else if (typ->isPPC_FP128Ty())
fty = crellvm::PPC_FP128Type;
else
assert("Unknown floating point type" && false);
return fty;
}
bool isFloatOpcode(llvm::Instruction::BinaryOps ops) {
switch (ops) {
case llvm::Instruction::FAdd:
case llvm::Instruction::FSub:
case llvm::Instruction::FMul:
case llvm::Instruction::FDiv:
case llvm::Instruction::FRem:
return true;
default:
return false;
}
}
TyFbop getFbop(llvm::Instruction::BinaryOps ops) {
TyFbop tyfbop;
switch (ops) {
case llvm::Instruction::FAdd:
tyfbop = crellvm::BopFadd;
break;
case llvm::Instruction::FSub:
tyfbop = crellvm::BopFsub;
break;
case llvm::Instruction::FMul:
tyfbop = crellvm::BopFmul;
break;
case llvm::Instruction::FDiv:
tyfbop = crellvm::BopFdiv;
break;
case llvm::Instruction::FRem:
tyfbop = crellvm::BopFrem;
break;
default:
assert(
"crellvm::getFbop(llvm::Instruction::BinaryOps) : unknown opcode" &&
false);
}
return tyfbop;
}
TyBop getBop(llvm::Instruction::BinaryOps ops) {
TyBop bop;
switch (ops) {
case llvm::Instruction::Add:
bop = crellvm::BopAdd;
break;
case llvm::Instruction::Sub:
bop = crellvm::BopSub;
break;
case llvm::Instruction::Mul:
bop = crellvm::BopMul;
break;
case llvm::Instruction::UDiv:
bop = crellvm::BopUdiv;
break;
case llvm::Instruction::SDiv:
bop = crellvm::BopSdiv;
break;
case llvm::Instruction::URem:
bop = crellvm::BopUrem;
break;
case llvm::Instruction::SRem:
bop = crellvm::BopSrem;
break;
case llvm::Instruction::Shl:
bop = crellvm::BopShl;
break;
case llvm::Instruction::LShr:
bop = crellvm::BopLshr;
break;
case llvm::Instruction::AShr:
bop = crellvm::BopAshr;
break;
case llvm::Instruction::And:
bop = crellvm::BopAnd;
break;
case llvm::Instruction::Or:
bop = crellvm::BopOr;
break;
case llvm::Instruction::Xor:
bop = crellvm::BopXor;
break;
default:
assert("crellvm::getBop(llvm::Instruction::BinaryOps) : unknown opcode" &&
false);
}
return bop;
}
TyIcmpPred getIcmpPred(llvm::ICmpInst::Predicate prd) {
TyIcmpPred predicate;
switch (prd) {
case llvm::ICmpInst::ICMP_EQ:
predicate = crellvm::CondEq;
break;
case llvm::ICmpInst::ICMP_NE:
predicate = crellvm::CondNe;
break;
case llvm::ICmpInst::ICMP_UGT:
predicate = crellvm::CondUgt;
break;
case llvm::ICmpInst::ICMP_UGE:
predicate = crellvm::CondUge;
break;
case llvm::ICmpInst::ICMP_ULT:
predicate = crellvm::CondUlt;
break;
case llvm::ICmpInst::ICMP_ULE:
predicate = crellvm::CondUle;
break;
case llvm::ICmpInst::ICMP_SGT:
predicate = crellvm::CondSgt;
break;
case llvm::ICmpInst::ICMP_SGE:
predicate = crellvm::CondSge;
break;
case llvm::ICmpInst::ICMP_SLT:
predicate = crellvm::CondSlt;
break;
case llvm::ICmpInst::ICMP_SLE:
predicate = crellvm::CondSle;
break;
default:
assert("crellvm::getIcmpPred(llvm::ICmpInst::Predicate prd) : unknown "
"predicate" &&
false);
}
return predicate;
}
TyFcmpPred getFcmpPred(llvm::FCmpInst::Predicate prd) {
TyFcmpPred predicate;
switch (prd) {
case llvm::FCmpInst::FCMP_FALSE:
predicate = crellvm::CondFfalse;
break;
case llvm::FCmpInst::FCMP_OEQ:
predicate = crellvm::CondFoeq;
break;
case llvm::FCmpInst::FCMP_OGT:
predicate = crellvm::CondFogt;
break;
case llvm::FCmpInst::FCMP_OGE:
predicate = crellvm::CondFoge;
break;
case llvm::FCmpInst::FCMP_OLT:
predicate = crellvm::CondFolt;
break;
case llvm::FCmpInst::FCMP_OLE:
predicate = crellvm::CondFole;
break;
case llvm::FCmpInst::FCMP_ONE:
predicate = crellvm::CondFone;
break;
case llvm::FCmpInst::FCMP_ORD:
predicate = crellvm::CondFord;
break;
case llvm::FCmpInst::FCMP_UNO:
predicate = crellvm::CondFuno;
break;
case llvm::FCmpInst::FCMP_UEQ:
predicate = crellvm::CondFueq;
break;
case llvm::FCmpInst::FCMP_UGT:
predicate = crellvm::CondFugt;
break;
case llvm::FCmpInst::FCMP_UGE:
predicate = crellvm::CondFuge;
break;
case llvm::FCmpInst::FCMP_ULT:
predicate = crellvm::CondFult;
break;
case llvm::FCmpInst::FCMP_ULE:
predicate = crellvm::CondFule;
break;
case llvm::FCmpInst::FCMP_UNE:
predicate = crellvm::CondFune;
break;
case llvm::FCmpInst::FCMP_TRUE:
predicate = crellvm::CondFtrue;
break;
default:
assert("crellvm::getFCmpPred(llvm::FCmpInst::Predicate pred) : unknown "
"predicate" &&
false);
}
return predicate;
}
bool name_instructions(llvm::Function &F) {
for (llvm::Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end();
AI != AE; ++AI)
if (!AI->hasName() && !AI->getType()->isVoidTy())
AI->setName("arg");
for (llvm::Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
if (!BB->hasName())
BB->setName("bb");
for (llvm::BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
if (!I->hasName() && !I->getType()->isVoidTy())
I->setName("tmp");
}
return true;
}
bool name_instruction(llvm::Instruction &I) {
llvm::BasicBlock *BB = I.getParent();
if (!BB->hasName())
BB->setName("bb");
if (!I.hasName() && !I.getType()->isVoidTy())
I.setName("tmp");
return true;
}
int getCommandIndex(const llvm::Value &V) {
if (!llvm::isa<llvm::Instruction>(V))
return -1; // not an instruction
const llvm::Instruction *instr = llvm::dyn_cast<llvm::Instruction>(&V);
if (llvm::isa<llvm::PHINode>(instr)) {
return -1; // A phinode is not a command
} else if (instr->isTerminator()) {
return -1; // A terminator is not a command
} else {
unsigned int rawIndex = getRawInstrIndex(*instr);
const llvm::BasicBlock *parent = instr->getParent();
unsigned int firstNonPhiRawIndex =
getRawInstrIndex(*parent->getFirstNonPHI());
return (int)(rawIndex - firstNonPhiRawIndex);
}
}
int getTerminatorIndex(const llvm::TerminatorInst *instr) {
if (instr->isTerminator()) {
unsigned int rawIndex = getRawInstrIndex(*instr);
const llvm::BasicBlock *parent = instr->getParent();
unsigned int firstNonPhiRawIndex =
getRawInstrIndex(*parent->getFirstNonPHI());
return (int)(rawIndex - firstNonPhiRawIndex);
} else {
return -1; // not a terminator
}
}
/* position */
TyPositionPhinode::TyPositionPhinode(std::string _prev_block_name)
: prev_block_name(_prev_block_name) {}
void TyPositionPhinode::serialize(cereal::JSONOutputArchive &archive) const {
archive(CEREAL_NVP(prev_block_name));
}
TyPositionCommand::TyPositionCommand(int _index, std::string _register_name)
: index(_index), register_name(_register_name) {}
void TyPositionCommand::serialize(cereal::JSONOutputArchive &archive) const {
archive(CEREAL_NVP(index), CEREAL_NVP(register_name));
}
ConsPhinode::ConsPhinode(std::shared_ptr<TyPositionPhinode> _position_phinode)
: position_phinode(_position_phinode) {}
void ConsPhinode::serialize(cereal::JSONOutputArchive &archive) const {
archive.makeArray();
archive.writeName();
archive.saveValue("Phinode");
archive(CEREAL_NVP(position_phinode));
}
ConsCommand::ConsCommand(std::shared_ptr<TyPositionCommand> _position_command)
: position_command(_position_command) {}
void ConsCommand::serialize(cereal::JSONOutputArchive &archive) const {
archive.makeArray();
archive.writeName();
archive.saveValue("Command");
archive(CEREAL_NVP(position_command));
}
TyPosition::TyPosition(enum TyScope _scope, std::string _block_name,
std::shared_ptr<TyInstrIndex> _instr_index)
: scope(_scope), block_name(_block_name),
instr_index(_instr_index) {}
void TyPosition::serialize(cereal::JSONOutputArchive &archive) const {
archive(cereal::make_nvp("scope", ::toString(scope)), CEREAL_NVP(block_name),
CEREAL_NVP(instr_index));
}
std::shared_ptr<TyPosition> TyPosition::make(enum TyScope _scope,
std::string _block_name,
std::string _prev_block_name) {
std::shared_ptr<TyPositionPhinode> _pos_phi(
new TyPositionPhinode(_prev_block_name));
std::shared_ptr<TyInstrIndex> _phi(new ConsPhinode(_pos_phi));
return std::shared_ptr<TyPosition>(
new TyPosition(_scope, _block_name, _phi));
}
std::shared_ptr<TyPosition>
TyPosition::make_start_of_block(enum TyScope _scope, std::string _block_name) {
return TyPosition::make(_scope, _block_name, "");
}
std::shared_ptr<TyPosition> TyPosition::make(enum TyScope _scope,
const llvm::Instruction &I) {
std::string empty_str = "";
return TyPosition::make(_scope, I, empty_str);
}
std::shared_ptr<TyPosition> TyPosition::make(enum TyScope _scope,
const llvm::Instruction &I,
std::string _prev_block_name) {
std::string _block_name = getBasicBlockIndex(I.getParent());
std::string _register_name = getVariable(I);
std::shared_ptr<TyInstrIndex> _instr_index;
if (llvm::isa<llvm::PHINode>(I)) {
std::shared_ptr<TyPositionPhinode> _pos_phi(
new TyPositionPhinode(_prev_block_name));
std::shared_ptr<TyInstrIndex> _phi(new ConsPhinode(_pos_phi));
_instr_index = _phi;
} else {
int _index;
if (llvm::isa<llvm::TerminatorInst>(I)) {
_index = getTerminatorIndex(llvm::dyn_cast<llvm::TerminatorInst>(&I));
} else {
_index = getCommandIndex(I);
}
std::shared_ptr<TyPositionCommand> _pos_cmd(
new TyPositionCommand(_index, _register_name));
std::shared_ptr<TyInstrIndex> _cmd(new ConsCommand(_pos_cmd));
_instr_index = _cmd;
}
return std::shared_ptr<TyPosition>(
new TyPosition(_scope, _block_name, _instr_index));
}
std::shared_ptr<TyPosition> TyPosition::make(enum TyScope _scope,
const llvm::Instruction &I,
int index,
std::string _prev_block_name) {
std::string _block_name = getBasicBlockIndex(I.getParent());
std::string _register_name = getVariable(I);
std::shared_ptr<TyInstrIndex> _instr_index;
if (llvm::isa<llvm::PHINode>(I)) {
std::shared_ptr<TyPositionPhinode> _pos_phi(
new TyPositionPhinode(_prev_block_name));
std::shared_ptr<TyInstrIndex> _phi(new ConsPhinode(_pos_phi));
_instr_index = _phi;
} else if (index < 0) {
// if index is less than 0, it means start of block
return TyPosition::make_start_of_block(_scope, _block_name);
} else {
std::shared_ptr<TyPositionCommand> _pos_cmd(
new TyPositionCommand(index, _register_name));
std::shared_ptr<TyInstrIndex> _cmd(new ConsCommand(_pos_cmd));
_instr_index = _cmd;
}
return std::shared_ptr<TyPosition>(
new TyPosition(_scope, _block_name, _instr_index));
}
std::shared_ptr<TyPosition> TyPosition::make(enum TyScope _scope,
const llvm::BasicBlock &BB,
int index) {
std::string _block_name = getBasicBlockIndex(&BB);
std::string _register_name = "";
std::shared_ptr<TyInstrIndex> _instr_index;
if (index < 0) {
// if index is less than 0, it means start of block
return TyPosition::make_start_of_block(_scope, _block_name);
} else {
std::shared_ptr<TyPositionCommand> _pos_cmd(
new TyPositionCommand(index, _register_name));
std::shared_ptr<TyInstrIndex> _cmd(new ConsCommand(_pos_cmd));
_instr_index = _cmd;
}
return std::shared_ptr<TyPosition>(
new TyPosition(_scope, _block_name, _instr_index));
}
std::shared_ptr<TyPosition>
TyPosition::make_end_of_block(enum TyScope _scope, const llvm::BasicBlock &BB) {
const llvm::TerminatorInst *term = BB.getTerminator();
std::string _block_name = getBasicBlockIndex(&BB);
std::string _register_name = "";
int _index = getTerminatorIndex(term);
std::shared_ptr<TyPositionCommand> _pos_cmd(
new TyPositionCommand(_index, _register_name));
std::shared_ptr<TyInstrIndex> _cmd(new ConsCommand(_pos_cmd));
return std::shared_ptr<TyPosition>(
new TyPosition(_scope, _block_name, _cmd));
}
std::shared_ptr<TyPosition>
TyPosition::make_end_of_block(enum TyScope _scope, const llvm::BasicBlock &BB,
int index) {
std::string _block_name = getBasicBlockIndex(&BB);
std::string _register_name = "";
int _index = index;
std::shared_ptr<TyPositionCommand> _pos_cmd(
new TyPositionCommand(_index, _register_name));
std::shared_ptr<TyInstrIndex> _cmd(new ConsCommand(_pos_cmd));
return std::shared_ptr<TyPosition>(
new TyPosition(_scope, _block_name, _cmd));
}
/* value */
// register
TyRegister::TyRegister(const std::string &_name, enum TyTag _tag)
: name(_name), tag(_tag) {}
void TyRegister::serialize(cereal::JSONOutputArchive &archive) const {
archive(CEREAL_NVP(name), cereal::make_nvp("tag", ::toString(tag)));
}
std::shared_ptr<TyRegister> TyRegister::make(const std::string &_name,
enum TyTag _tag) {
return std::shared_ptr<TyRegister>(new TyRegister(_name, _tag));
}
std::shared_ptr<TyRegister> TyRegister::make(const llvm::Value &_val,
enum TyTag _tag) {
return make(getVariable(_val), _tag);
}
bool TyRegister::isSame(std::shared_ptr<TyRegister> r1,
std::shared_ptr<TyRegister> r2) {
return (r1->name==r2->name && r1->tag==r2->tag);
}
std::string TyRegister::getName() {
return name;
}
// constant
ConsIntType::ConsIntType(int _value) : value(_value) {}
void ConsIntType::serialize(cereal::JSONOutputArchive &archive) const {
archive.makeArray();
archive.writeName();
archive.saveValue("IntType");
archive(CEREAL_NVP(value));
}
TyConstInt::TyConstInt(int64_t _int_value, std::shared_ptr<TyIntType> _int_type)
: int_value(_int_value), int_type(_int_type) {}
TyConstInt::TyConstInt(int64_t _int_value, int _bitwidth)
: int_value(_int_value), int_type(new ConsIntType(_bitwidth)) {}
void TyConstInt::serialize(cereal::JSONOutputArchive &archive) const {
std::string str;
std::stringstream ss;
ss << int_value;
ss >> str;
archive(cereal::make_nvp("int_value", str), CEREAL_NVP(int_type));
}
std::shared_ptr<TyConstInt> TyConstInt::make(int64_t _int_value, int _value) {
return std::shared_ptr<TyConstInt>(new TyConstInt(_int_value, _value));
}
std::shared_ptr<TyConstInt> TyConstInt::make(const llvm::ConstantInt &ci) {
return TyConstInt::make(ci.getSExtValue(), ci.getBitWidth());
}
TyConstFloat::TyConstFloat(double _float_value, enum TyFloatType _float_type)
: float_value(_float_value), float_type(_float_type) {}
void TyConstFloat::serialize(cereal::JSONOutputArchive &archive) const {
std::stringstream ss;
ss << std::setprecision(DECIMAL_DIG) // No digit loss
<< float_value;
std::string res = ss.str();
archive(cereal::make_nvp("float_value", res));
archive(cereal::make_nvp("float_type", toString(float_type)));
}
std::shared_ptr<TyConstFloat> TyConstFloat::make(double _float_value,
enum TyFloatType _float_type) {
return std::shared_ptr<TyConstFloat>(
new TyConstFloat(_float_value, _float_type));
}
TyConstGlobalVarAddr::TyConstGlobalVarAddr(
std::string _var_id, std::shared_ptr<TyValueType> _var_type)
: var_id(_var_id), var_type(_var_type) {}
void TyConstGlobalVarAddr::serialize(cereal::JSONOutputArchive &archive) const {
archive(CEREAL_NVP(var_id));
archive(CEREAL_NVP(var_type));
}
std::shared_ptr<TyConstGlobalVarAddr>
TyConstGlobalVarAddr::make(const llvm::GlobalObject &gv) {
llvm::Type *ty = gv.getType();
assert(ty->isPointerTy() &&
"Global variables must be pointers to their locations."); // jylee
ty = ty->getPointerElementType();
return std::shared_ptr<TyConstGlobalVarAddr>(new TyConstGlobalVarAddr(
std::string("@") + std::string(gv.getName().data()),
TyValueType::make(*ty)));
}
/*
* Value
*/
std::shared_ptr<TyValue> TyValue::make(const llvm::Value &value,
enum TyTag _tag) {
if (llvm::isa<llvm::Instruction>(value) || llvm::isa<llvm::Argument>(value)) {
return std::shared_ptr<TyValue>(
new ConsId(TyRegister::make(getVariable(value), _tag)));
} else if (const llvm::ConstantExpr *ce =
llvm::dyn_cast<llvm::ConstantExpr>(&value)) {
// Constant expressions have two kinds of forms :
// (1) %x = add i32 1, 2
// ^^^^^^^^^^^^^^^^^
// (2) %x = add i32 %y, add (i32 1, i32 2)
// ^^^^^^^^^^^^^^^^^^
// For the case (1), TyValue::make returns register id "%x"
// For the case (2), TyValue::make returns a constant expression "add (i32
// 1, i32 2)"
if (ce->getName().str() != "") {
return std::shared_ptr<TyValue>(
new ConsId(TyRegister::make(getVariable(*ce), _tag)));
} else {
return std::shared_ptr<TyValue>(new ConsConstVal(TyConstant::make(*ce)));
}
} else if (const llvm::Constant *c = llvm::dyn_cast<llvm::Constant>(&value)) {
return std::shared_ptr<TyValue>(new ConsConstVal(TyConstant::make(*c)));
} else {
assert("Unknown value type" && false);
}
return nullptr;