-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperand.cpp
More file actions
974 lines (826 loc) · 35 KB
/
Operand.cpp
File metadata and controls
974 lines (826 loc) · 35 KB
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
#include "Operand.h"
void addFunctionPointersInConstantToValue(llvm::Value *constant,
llvm::Value *value) {
// Case 1. It is a function pointer already
if (auto *function = llvm::dyn_cast<llvm::Function>(constant)) {
OperandRepository::getOrCreateSLIMOperand(value)
->addContainingFunctionPointer(
OperandRepository::getOrCreateSLIMOperand(function));
return;
}
// Case 2. It is not, so iterate over the operands recursively
if (auto *user = llvm::dyn_cast<llvm::User>(constant)) {
for (unsigned i = 0; i < user->getNumOperands(); i++) {
auto *operand = user->getOperand(i);
addFunctionPointersInConstantToValue(operand, value);
}
}
}
OperandType SLIMOperand::getOperandTypeOfValue(llvm::Value *value) {
// Set the operand type
if (value != nullptr) {
// First check if the operand has a name, which won't be the case when
// the operand is a GetElementPtr (GEP) operand for example, because in
// this case we have to extract the relevant operand and indices from
// the GEP operand. (operand->getOperandType() ==
// OperandType::BITCAST_OPERATOR &&
// llvm::isa<llvm::Function>(operand->get_bitcast_operand()))
if (llvm::isa<llvm::GEPOperator>(value)) {
return OperandType::GEP_OPERATOR;
} else if (llvm::isa<llvm::AddrSpaceCastOperator>(value)) {
return OperandType::ADDR_SPACE_CAST_OPERATOR;
} else if (llvm::isa<llvm::BitCastOperator>(value)) {
return OperandType::BITCAST_OPERATOR;
} else if (llvm::isa<llvm::PtrToIntOperator>(value)) {
return OperandType::PTR_TO_INT_OPERATOR;
} else if (llvm::isa<llvm::ZExtOperator>(value)) {
return OperandType::ZEXT_OPERATOR;
} else if (llvm::isa<llvm::FPMathOperator>(value)) {
return OperandType::FP_MATH_OPERATOR;
} else if (llvm::isa<llvm::Constant>(value)) {
if (llvm::isa<llvm::BlockAddress>(value)) {
return OperandType::BLOCK_ADDRESS;
} else if (llvm::isa<llvm::ConstantAggregate>(value)) {
return OperandType::CONSTANT_AGGREGATE;
} else if (llvm::isa<llvm::ConstantDataSequential>(value)) {
return OperandType::CONSTANT_DATA_SEQUENTIAL;
} else if (llvm::isa<llvm::ConstantPointerNull>(value)) {
return OperandType::CONSTANT_POINTER_NULL;
} else if (llvm::isa<llvm::ConstantTokenNone>(value)) {
return OperandType::CONSTANT_TOKEN_NONE;
} else if (llvm::isa<llvm::UndefValue>(value)) {
return OperandType::UNDEF_VALUE;
} else if (llvm::isa<llvm::ConstantInt>(value)) {
return OperandType::CONSTANT_INT;
} else if (llvm::isa<llvm::ConstantFP>(value)) {
return OperandType::CONSTANT_FP;
} else if (llvm::isa<llvm::DSOLocalEquivalent>(value)) {
return OperandType::DSO_LOCAL_EQUIVALENT;
} else if (llvm::isa<llvm::GlobalValue>(value)) {
return OperandType::GLOBAL_VALUE;
} else if (llvm::isa<llvm::NoCFIValue>(value)) {
return OperandType::NO_CFI_VALUE;
} else {
return OperandType::NOT_SUPPORTED_OPERAND;
}
} else if (value->hasName()) {
return OperandType::VARIABLE;
} else {
return OperandType::NOT_SUPPORTED_OPERAND;
}
} else {
return OperandType::NULL_OPERAND;
}
}
// Operand is not a address-taken local variable
SLIMOperand::SLIMOperand(llvm::Value *value) : value{value} {
if (value != nullptr) {
/*
Since every global and address taken variable in the LLVM IR (after
mem2reg pass) is a pointer, we distinguish pointers in the source
program from the pointers in LLVM by checking whether a LLVM pointer
is double pointer or not.
But we have to be careful regarding this because there might be some
instances in which we can interpret the operand incorrectly. For
example, a GetElementPtr (GEP) operand is always a pointer but if we
try to check this by taking the operand (of GEPOperand) then we may
get invalid results because that operand may or may not be a pointer.
*/
if (value->hasName()) {
this->has_name = true;
}
if (llvm::isa<llvm::GlobalValue>(value)) {
if (llvm::isa<llvm::PointerType>(
llvm::cast<llvm::GlobalValue>(value)->getType())) {
this->is_pointer_variable = true;
}
} else if (value->getType() &&
value->getType()->getNumContainedTypes() > 0 &&
value->getType()->getContainedType(0)->isPointerTy()) {
this->is_pointer_variable = true;
}
if (llvm::isa<llvm::Argument>(this->value)) {
this->is_formal_argument = true;
}
}
// Get the operand type
this->operand_type = SLIMOperand::getOperandTypeOfValue(this->value);
// Check if the operand is a GEPOperator
// FIXME: This used to check if the operand has a name, proceeding
// only if it doesn't. It's not clear why this was done, so I'm
// removing it for now (we need indices for all GEPs right now)
if (this->operand_type == OperandType::GEP_OPERATOR) {
// Cast the operand to llvm::GEPOperator
llvm::GEPOperator *gep_operator =
llvm::cast<llvm::GEPOperator>(this->value);
// Get the variable operand (which is the first operand)
this->gep_main_operand = gep_operator->getOperand(0);
if (gep_operator->getNumOperands() > 0) {
this->has_indices = true;
}
for (int i = 1; i < gep_operator->getNumOperands(); i++) {
llvm::Value *index_val = gep_operator->getOperand(i);
// Check if the index is constant
if (llvm::isa<llvm::Constant>(index_val)) {
if (llvm::isa<llvm::ConstantInt>(index_val)) {
SLIMOperand *index_operand = new SLIMOperand(index_val);
this->indices.push_back(index_operand);
}
// If the indices are constant, they must be integers
else {
llvm_unreachable(
"[SLIMOperand error while construction of GEPOperator] "
"The index is a constant but not an integer constant!");
}
} else if (index_val->hasName()) {
SLIMOperand *index_operand = new SLIMOperand(index_val);
this->indices.push_back(index_operand);
} else {
// The index is stored in a variable or SSA register
llvm_unreachable("[SLIMOperand error while construction of "
"GEPOperator] The index is not a constant!");
}
}
} /*else if(this->operand_type == OperandType::BITCAST_OPERATOR) {
auto *the_operator = llvm::cast<llvm::BitCastOperator>(this->value);
llvm::outs() << "\n SLIM BITCAST OPERATOR";
// Get the variable operand (which is the first operand)
this->bitcast_operand = the_operator->getOperand(0);
}*/
}
// Operand may or may not be a address-taken local or global variable
SLIMOperand::SLIMOperand(llvm::Value *value, bool is_global_or_address_taken,
llvm::Function *direct_callee_function) {
this->value = value;
this->is_global_or_address_taken = is_global_or_address_taken;
this->direct_callee_function = direct_callee_function;
this->is_pointer_variable = false;
this->gep_main_operand = nullptr;
this->has_indices = false;
this->has_name = false;
this->is_ssa_version = false;
this->ssa_version_number = 0;
this->is_array_type = false;
this->is_formal_argument = false;
if (value != nullptr) {
if (llvm::isa<llvm::GlobalValue>(value)) {
if (llvm::isa<llvm::PointerType>(
llvm::cast<llvm::GlobalValue>(value)->getType())) {
this->is_pointer_variable = true;
}
}
// Same argument (described in the above constructor)
else if (value->getType()->getNumContainedTypes() > 0 &&
value->getType()->getContainedType(0)->isPointerTy()) {
this->is_pointer_variable = true;
}
if (value->hasName()) {
this->has_name = true;
}
if (llvm::isa<llvm::Argument>(this->value)) {
this->is_formal_argument = true;
}
}
// Get the operand type
this->operand_type = SLIMOperand::getOperandTypeOfValue(this->value);
// Check if the operand is a GEPOperator and the operand does not have a
// name
if (this->operand_type == OperandType::GEP_OPERATOR &&
!this->value->hasName()) {
// Cast the operand to llvm::GEPOperator
llvm::GEPOperator *gep_operator =
llvm::cast<llvm::GEPOperator>(this->value);
// Get the variable operand (which is the first operand)
this->gep_main_operand = gep_operator->getOperand(0);
if (gep_operator->getNumOperands() > 0) {
this->has_indices = true;
}
for (int i = 1; i < gep_operator->getNumOperands(); i++) {
llvm::Value *index_val = gep_operator->getOperand(i);
// Check if the index is constant
if (llvm::isa<llvm::Constant>(index_val)) {
if (llvm::isa<llvm::ConstantInt>(index_val)) {
SLIMOperand *index_operand = new SLIMOperand(index_val);
this->indices.push_back(index_operand);
}
// If the indices are constant, they must be integers
else {
llvm_unreachable(
"[SLIMOperand error while construction of GEPOperator] "
"The index is a constant but not an integer constant!");
}
} else if (index_val->hasName()) {
SLIMOperand *index_operand = new SLIMOperand(index_val);
this->indices.push_back(index_operand);
} else {
// The index is stored in a variable or SSA register
llvm_unreachable("[SLIMOperand error while construction of "
"GEPOperator] The index is not a constant!");
}
}
}
}
void SLIMOperand::addContainingFunctionPointer(SLIMOperand *fp) {
this->contains_function_pointers.insert(fp);
}
std::set<SLIMOperand *> &SLIMOperand::getContainingFunctionPointers() {
return this->contains_function_pointers;
}
// Returns the operand type
OperandType SLIMOperand::getOperandType() {
return this->operand_type;
}
void SLIMOperand::setOperandType(OperandType ty) {
this->operand_type = ty;
}
unsigned SLIMOperand::getPointerIndirectionLevel() {
unsigned pointer_indirection_level = 0;
llvm::Type *type = this->getType();
while (type->isPointerTy()) {
pointer_indirection_level++;
type = type->getContainedType(0);
}
return pointer_indirection_level;
}
// Returns true if the operand is a global variable or a address-taken local
// variable
bool SLIMOperand::isGlobalOrAddressTaken() {
return this->is_global_or_address_taken || this->isAlloca();
}
// Returns true if the operand is a formal argument of a function
bool SLIMOperand::isFormalArgument() {
return this->is_formal_argument;
}
// Returns true if the operand is a global variable or an address-taken local
// variable (considers only the struct if the operand is a GEP operator)
bool SLIMOperand::isVariableGlobal() {
/*if (this->operand_type == OperandType::GEP_OPERATOR) {
// Cast the operand to llvm::GEPOperator
llvm::GEPOperator *gep_operator =
llvm::cast<llvm::GEPOperator>(this->value);
// Get the variable operand (which is the first operand)
this->gep_main_operand = gep_operator->getOperand(0);
return llvm::isa<llvm::GlobalValue>(this->gep_main_operand);
}*/
return this->isGlobalOrAddressTaken() ||
llvm::isa<llvm::GlobalValue>(this->value);
}
// Returns true if the operand is a result of an alloca instruction
bool SLIMOperand::isAlloca() {
return this->is_alloca;
}
// Returns true if the operand is a pointer variable (with reference to the LLVM
// IR)
bool SLIMOperand::isPointerInLLVM() {
if (llvm::isa<llvm::GlobalValue>(this->value)) {
if (llvm::isa<llvm::PointerType>(
llvm::cast<llvm::GlobalValue>(this->value)->getValueType())) {
return true;
} else {
return false;
}
}
return this->value->getType()->isPointerTy();
}
// Returns true if the operand is a pointer variable
bool SLIMOperand::isPointerVariable() {
return this->is_pointer_variable;
}
// Returns true if the operand is of array type
bool SLIMOperand::isArrayElement() {
///llvm::outs() << "\n INSIDE isARRAYELEMNT..........";
if (this->is_array_type)
return true;
if (llvm::isa<llvm::GEPOperator>(
this->value)) { ///llvm::outs() << "\n ARRAY 1";
llvm::GEPOperator *gep_operand =
llvm::cast<llvm::GEPOperator>(this->value);
// Get the type of the GEP main operand
auto gep_main_operand = gep_operand->getOperand(0);
llvm::Type *type = gep_main_operand->getType()->getContainedType(0);
// If the type is an array then return true
if (type->isArrayTy())
return true;
// Otherwise the type is a structure, so we check if any of its fields
// is of array type
for (unsigned i = 2; i < gep_operand->getNumOperands();
i++) { /// llvm::outs() << "\n ARRAY 2";
// Get the ith operand
llvm::Value *operand_i = gep_operand->getOperand(i);
// Cast the value object to constant int (since it is an index)
llvm::ConstantInt *constant_int_i =
llvm::cast<llvm::ConstantInt>(operand_i);
// If the type of GEPOperator is a structure
if (llvm::isa<llvm::StructType>(
type)) { // llvm::outs() << "\n ARRAY 3";
// Assignment to type is required to check for nested structures
type =
type->getStructElementType(constant_int_i->getSExtValue());
if (type->isArrayTy())
return true;
}
}
}
if (this->isVariableGlobal() and !this->isDynamicAllocationType()){/// llvm::outs() << "\n SPECIAL ARRAY-- 333";
llvm::Type *type = this->value->getType(); // This gets the pointer type
if (type->isPointerTy()) {
llvm::Type *elementType = type->getPointerElementType(); // This gets the pointee type
if (elementType->isArrayTy()) {
return true;
}
}
}
/// llvm::outs() << "\n ARRAY 4";
return false;
}
// Sets the is_array_type to true
void SLIMOperand::setArrayType() {
this->is_array_type = true;
}
// Sets the gep_main_operand
void SLIMOperand::setGEPMainOperand(SLIMOperand *operand) {
this->gep_main_operand = operand->getValue();
}
// Sets the gep_main_operand
void SLIMOperand::setGEPMainOperand(llvm::Value *val) {
this->gep_main_operand = val;
}
// Sets the bitcast_operand
/*void SLIMOperand::setBitcastOperand(llvm::Value *val) {
this->bitcast_operand = val;
}*/
// Returns true if the operand is a GetElementPtr operand inside an instruction
bool SLIMOperand::isGEPInInstr() {
return llvm::isa<llvm::GEPOperator>(this->value) && !this->value->hasName();
}
bool SLIMOperand::isBitcastInInstr() {
return llvm::isa<llvm::BitCastOperator>(this->value) &&
!this->value->hasName();
}
// Sets the is_pointer_variable to true
void SLIMOperand::setIsPointerVariable() {
this->is_pointer_variable = true;
}
// Sets the is_pointer_variable to false
void SLIMOperand::unsetIsPointerVariable() {
this->is_pointer_variable = false;
}
// Returns the pointer to the corresponding llvm::Value object
llvm::Value *SLIMOperand::getValue() {
return this->value;
}
// Returns the type of the operand
llvm::Type *SLIMOperand::getType() {
if (this->value &&
(llvm::isa<llvm::GlobalValue>(this->value) || this->isAlloca())) {
return this->value->getType()->getContainedType(0);
}
if (this->value) {
return this->value->getType();
}
return nullptr;
}
// Returns the number of indices
unsigned SLIMOperand::getNumIndices() {
return this->indices.size();
}
// Returns if the operand has a name
bool SLIMOperand::hasName() {
return this->has_name;
}
// Returns the operand index at the specified position (0-based position)
SLIMOperand *SLIMOperand::getIndexOperand(unsigned position) {
// Check if the indices exist and the position (0-based) is in bounds or not
assert(this->has_indices && position >= 0 &&
position < this->getNumIndices());
// Return the index operand
return this->indices[position];
}
// Returns the vector of indices
const std::vector<SLIMOperand *> &SLIMOperand::getIndexVector() {
return this->indices;
}
void SLIMOperand::addIndexOperand(SLIMOperand *indOperand) {
// Add new index operand to indices
this->has_indices = true;
this->indices.push_back(indOperand);
}
// Internal function to be used only in case of print related tasks
std::string SLIMOperand::_getOperandName() {
llvm::Value *operand = this->getValue();
// This will hold the string value of the operand
std::string operand_name;
// The string in this stream will be flushed into the operand_name
llvm::raw_string_ostream stream(operand_name);
// First check if the operand has a name, which won't be the case when the
// operand is a GetElementPtr (GEP) operand for example, because in this
// case we have to extract the relevant operand and indices from the GEP
// operand
if (operand->hasName()) {
stream << operand->getName();
} else if (llvm::isa<llvm::GEPOperator>(operand)) {
// Cast the operand to llvm::GEPOperator
llvm::GEPOperator *gep_operator =
llvm::cast<llvm::GEPOperator>(operand);
// Get the variable operand (which is the first operand)
llvm::Value *gep_operand = gep_operator->getOperand(0);
// Print the variable name
stream << gep_operand->getName();
// Print the indices
for (int i = 1; i < gep_operator->getNumOperands(); i++) {
llvm::Value *index_val = gep_operator->getOperand(i);
// Check if the index is constant
if (llvm::isa<llvm::Constant>(index_val)) {
if (llvm::isa<llvm::ConstantInt>(index_val)) {
llvm::ConstantInt *constant_int =
llvm::cast<llvm::ConstantInt>(index_val);
// Print the constant integer
stream << "[" << constant_int->getSExtValue() << "]";
}
// If the indices are constant, they must be integers
else {
llvm_unreachable(
"[GetElementPtrOperator Error] The index is a constant "
"but not an integer constant!");
}
} else {
// The index is stored in a variable or SSA register
stream << "[" << index_val->getName() << "]";
}
}
} else if (llvm::isa<llvm::AddrSpaceCastOperator>(operand)) {
llvm::cast<llvm::AddrSpaceCastOperator>(operand)->print(stream);
}
// else if (llvm::isa<llvm::AddrSpaceCastInst>(operand))
// {
// llvm::cast<llvm::AddrSpaceCastInst>(operand)->print(stream);
// }
else if (llvm::isa<llvm::BitCastOperator>(operand)) {
llvm::BitCastOperator *bitcast_operator =
llvm::cast<llvm::BitCastOperator>(operand);
bitcast_operator->print(stream);
} else if (llvm::isa<llvm::PtrToIntOperator>(operand)) {
llvm::cast<llvm::PtrToIntOperator>(operand)->print(stream);
} else if (llvm::isa<llvm::ZExtOperator>(operand)) {
llvm::cast<llvm::ZExtOperator>(operand)->print(stream);
} else if (llvm::isa<llvm::FPMathOperator>(operand)) {
llvm::cast<llvm::FPMathOperator>(operand)->print(stream);
} else if (llvm::isa<llvm::Constant>(operand)) {
if (llvm::isa<llvm::BlockAddress>(operand)) {
llvm::BlockAddress *block_address =
llvm::cast<llvm::BlockAddress>(operand);
stream << block_address->getBasicBlock()->getName();
} else if (llvm::isa<llvm::ConstantAggregate>(operand)) {
// llvm_unreachable("[SLIMOperand Error] The print function does not
// "
// "support constant aggregate!");
llvm::ConstantAggregate *const_aggregate =
llvm::cast<llvm::ConstantAggregate>(operand);
if (const_aggregate)
const_aggregate->print(stream); // prints the entire RHS of
// instr
} else if (llvm::isa<llvm::ConstantDataSequential>(operand)) {
llvm_unreachable("[SLIMOperand Error] The print function does not "
"support constant data sequential!");
} else if (llvm::isa<llvm::ConstantPointerNull>(operand)) {
llvm::ConstantPointerNull *const_pointer_null =
llvm::cast<llvm::ConstantPointerNull>(operand);
if (const_pointer_null)
stream << const_pointer_null->getName() << " ";
stream << "nullptr";
} else if (llvm::isa<llvm::ConstantTokenNone>(operand)) {
llvm_unreachable("[SLIMOperand Error] The print function does not "
"support constant token none!");
} else if (llvm::isa<llvm::UndefValue>(operand)) {
stream << "undef";
} else if (llvm::isa<llvm::ConstantInt>(operand)) {
llvm::ConstantInt *constant_int =
llvm::cast<llvm::ConstantInt>(operand);
stream << constant_int->getSExtValue();
} else if (llvm::isa<llvm::ConstantFP>(operand)) {
llvm::ConstantFP *constant_float =
llvm::cast<llvm::ConstantFP>(operand);
stream << constant_float->getValueAPF().convertToFloat();
} else if (llvm::isa<llvm::DSOLocalEquivalent>(operand)) {
llvm_unreachable("[SLIMOperand Error] The print function does not "
"support DSO local equivalent!");
} else if (llvm::isa<llvm::GlobalValue>(operand)) {
stream << llvm::cast<llvm::GlobalValue>(operand)->getName();
} else if (llvm::isa<llvm::NoCFIValue>(operand)) {
llvm_unreachable("[SLIMOperand Error] The print function does not "
"support NoCFIValue!");
} else if (llvm::isa<llvm::ConstantExpr>(operand)) {
stream << llvm::cast<llvm::ConstantExpr>(operand)->getName();
} else {
llvm_unreachable("[SLIMOperand Error] Unexpected constant!");
}
} else {
// operand->print(llvm::outs());
if (operand->hasName()) {
stream << operand->getName();
} else {
stream << "noname";
// llvm_unreachable("[SLIMOperand Error] Unexpected operand!");
}
}
std::string result = std::string(stream.str());
if (this->is_ssa_version) {
result += ("_" + std::to_string(this->ssa_version_number));
}
// stream.flush();
return result;
}
// Print the SLIM operand
void SLIMOperand::printOperand(llvm::raw_ostream &stream) {
stream << llvm::StringRef(this->_getOperandName());
return;
}
// modSB
// Print the SLIM Operand to DOT file
void SLIMOperand::printOperandDot(llvm::raw_ostream *stream) {
*stream << llvm::StringRef(this->_getOperandName());
return;
}
// Sets the SSA version
void SLIMOperand::setSSAVersion(unsigned ssa_version) {
// llvm::outs() << "Setting SSA Version...\n";
this->is_ssa_version = true;
this->ssa_version_number = ssa_version;
}
void SLIMOperand::resetSSAVersion() {
this->is_ssa_version = false;
}
// Returns the "return operand" of the callee function if this operand is the
// result of a "direct" call instruction
SLIMOperand *SLIMOperand::getCalleeReturnOperand() {
if (this->direct_callee_function == nullptr) {
return nullptr;
} else {
SLIMOperand *return_operand =
OperandRepository::getFunctionReturnOperand(
this->direct_callee_function);
return return_operand;
}
}
// for indirect calls
SLIMOperand *
SLIMOperand::getCalleeReturnOperand(llvm::Function *callee_function) {
return OperandRepository::getFunctionReturnOperand(callee_function);
}
void SLIMOperand::setIsAlloca() {
this->is_alloca = true;
}
std::set<SLIMOperand *> &SLIMOperand::getRefersToSourceValues() {
return this->refers_to_source_values;
}
void SLIMOperand::addRefersToSourceValues(SLIMOperand *sourceValue) {
this->refers_to_source_values.insert(sourceValue);
}
std::vector<SLIMOperand *> SLIMOperand::getTransitiveIndices() {
if (this->getOperandType() == OperandType::GEP_OPERATOR) {
/* if (this->getName() == "mapping_writeSyntaxElement_UVLC") {
llvm::outs() << "GEP: " << this->getName() << "\n";
}*/
std::vector<SLIMOperand *> transitive_indices;
// add indices only if the gep operand has
// more than one index
if (this->indices.size() > 1) {
transitive_indices.insert(transitive_indices.begin(),
this->indices.begin(),
this->indices.end());
}
auto *slimOperand =
OperandRepository::getSLIMOperand(this->gep_main_operand);
if (slimOperand) {
auto transitive_indices_of_gep_main_operand =
slimOperand->getTransitiveIndices();
transitive_indices.insert(
// transitive_indices.begin()+ transitive_indices.size(),
transitive_indices.begin(),
transitive_indices_of_gep_main_operand.begin(),
transitive_indices_of_gep_main_operand.end());
}
// reverse the indices
return transitive_indices;
}
return std::vector<SLIMOperand *>();
}
bool SLIMOperand::hasDebugSourceVariable() {
return this->debug_source_var != nullptr;
}
void SLIMOperand::setDebugSourceVariable(
llvm::DILocalVariable *debug_source_var) {
assert(debug_source_var != nullptr &&
"Attempting to set a null dilocalvariable as debug source");
this->debug_source_var = debug_source_var;
}
llvm::DILocalVariable *SLIMOperand::getDebugSourceVariable() {
return this->debug_source_var;
}
bool SLIMOperand::isSource() {
return /*this->hasDebugSourceVariable() ||*/ this->isVariableGlobal() ||
this->isFormalArgument() || this->isAlloca();
}
// Clears the index vector
void SLIMOperand::resetIndexVector() {
this->indices.clear();
}
// Sets 'is_global_or_address_taken' to be true for this operand
void SLIMOperand::setVariableGlobal() {
this->is_global_or_address_taken = true;
}
// Sets 'is_formal_argument' to be true for this operand
void SLIMOperand::setFormalArgument() {
this->is_formal_argument = true;
}
bool SLIMOperand::isDynamicAllocationType() {
return this->is_dynamic_allocation_type;
}
void SLIMOperand::setDynamicAllocationType() {
this->is_dynamic_allocation_type = true;
}
// --------------- APIs for the Legacy SLIM ---------------
// Returns the name of the operand
llvm::StringRef SLIMOperand::getName() {
return this->getValue()->getName();
std::string ssa_info = "";
if (this->is_ssa_version) {
// llvm::outs() << "Setting SSA Info...\n";
ssa_info = "_" + std::to_string(this->ssa_version_number);
}
std::string *operand_name =
new std::string(this->_getOperandName() + ssa_info);
// llvm::outs() << "Name: " << *operand_name << "\n";
return llvm::StringRef(*operand_name);
}
// Returns only name for structures (and not indices) in string format and
// returns the same value as getName for other type of operands
llvm::StringRef SLIMOperand::getOnlyName() {
if (this->isGEPInInstr()) {
// Cast the operand to llvm::GEPOperator
llvm::GEPOperator *gep_operator =
llvm::cast<llvm::GEPOperator>(this->value);
// Get the variable operand (which is the first operand)
llvm::Value *gep_operand = gep_operator->getOperand(0);
// Return the structure variable name
return gep_operand->getName();
} else if (this->isBitcastInInstr()) {
llvm::BitCastOperator *bitcast_operator =
llvm::cast<llvm::BitCastOperator>(this->value);
// Get the variable operand (which is the first operand)
llvm::Value *bitcast_operand = bitcast_operator->getOperand(0);
// Return the structure variable name
return bitcast_operand->getName();
} else {
return this->getName();
}
}
llvm::Function *SLIMOperand::getDirectCalleeFunction() {
return this->direct_callee_function;
}
llvm::Value *SLIMOperand::getValueOfArray() {
if (!this->hasName()) { //true for GEP_OPERATORS or casts
if (get_gep_main_operand() != nullptr) {
return get_gep_main_operand();
}
}
return getValue();
}
llvm::Value *SLIMOperand::get_gep_main_operand() {
return this->gep_main_operand;
}
llvm::Function *SLIMOperand::getAsFunction() {
return llvm::dyn_cast<llvm::Function>(this->getValue());
}
llvm::Function *extractFunctionFromValue(llvm::Value *value) {
if (value == nullptr) {
return nullptr;
}
// If the operand is a function, return the function
if (llvm::isa<llvm::Function>(value)) {
return llvm::cast<llvm::Function>(value);
}
// if the operand is a bitcast operator, return the function
else if (llvm::isa<llvm::BitCastOperator>(value)) {
auto *bitcast = llvm::cast<llvm::BitCastOperator>(value)->getOperand(0);
if (auto *f = llvm::dyn_cast<llvm::Function>(bitcast)) {
return f;
}
}
// if the operand is a gep operator, return the function
else if (llvm::isa<llvm::GEPOperator>(value)) {
auto mainOperand = llvm::cast<llvm::GEPOperator>(value)->getOperand(0);
if (auto *f = llvm::dyn_cast<llvm::Function>(mainOperand)) {
return f;
}
return nullptr;
}
// if the operand is a structure, go through the structure and return
// the function
else if (llvm::isa<llvm::ConstantStruct>(value)) {
llvm::ConstantStruct *constant_struct =
llvm::cast<llvm::ConstantStruct>(value);
for (unsigned i = 0; i < constant_struct->getNumOperands(); i++) {
llvm::Value *operand = constant_struct->getAggregateElement(i);
if (llvm::isa<llvm::Function>(operand)) {
return llvm::cast<llvm::Function>(operand);
}
auto *f = extractFunctionFromValue(operand);
if (f != nullptr) {
return f;
}
}
}
return nullptr;
}
llvm::Function *SLIMOperand::extractFunction() {
auto *value = getValue();
return extractFunctionFromValue(value);
}
SLIMOperand *SLIMOperand::getOperandWithin() {
////returns the operator of bitcast unary operation
if (auto unary = llvm::dyn_cast<llvm::BitCastOperator>(this->getValue())) {
// if (auto unary = llvm::dyn_cast<llvm::BitCastInst>
// (this->getValue())) {
return OperandRepository::getOrCreateSLIMOperand(unary->getOperand(0));
}
return nullptr;
}
SLIMOperand *SLIMOperand::getOperandForFunction(llvm::Function *F) {
/// returns the SLIM Operand for the llvm::Function
if (F != nullptr)
return OperandRepository::getOrCreateSLIMOperand(F);
return nullptr;
}
/*llvm::Value* SLIMOperand::get_bitcast_operand() {
return this->bitcast_operand;
}*/
// --------------------------------------------------------
// Methods of the OperandRepository namespace
namespace OperandRepository {
// Check whether a SLIMOperand object corresponds to a global or a address-taken
// local variable or not
std::map<llvm::Value *, SLIMOperand *> value_to_slim_operand;
// Contains the value objects that are a result of alloca instruction
std::set<llvm::Value *> alloca_operand;
// Contains the return operand of every function
std::map<llvm::Function *, SLIMOperand *> function_return_operand;
}; // namespace OperandRepository
SLIMOperand *OperandRepository::getSLIMOperand(llvm::Value *value) {
if (OperandRepository::isSLIMOperandAvailable(value)) {
return OperandRepository::value_to_slim_operand[value];
}
return nullptr;
}
SLIMOperand *OperandRepository::getOrCreateSLIMOperand(llvm::Value *value) {
SLIMOperand *slimOperand = getSLIMOperand(value);
if (slimOperand == nullptr) {
slimOperand = new SLIMOperand(value);
setSLIMOperand(value, slimOperand);
return slimOperand;
}
return slimOperand;
}
SLIMOperand *OperandRepository::getOrCreateSLIMOperand(
llvm::Value *value, bool is_global_or_address_taken,
llvm::Function *direct_callee_function) {
SLIMOperand *slimOperand = getSLIMOperand(value);
if (slimOperand) {
if (slimOperand->isGlobalOrAddressTaken() ==
is_global_or_address_taken &&
slimOperand->getDirectCalleeFunction() == direct_callee_function) {
return slimOperand;
}
// it is not equal, so delete the old and create a new one
// below
// delete slimOperand;
}
slimOperand = new SLIMOperand(value, is_global_or_address_taken,
direct_callee_function);
setSLIMOperand(value, slimOperand);
return slimOperand;
}
bool OperandRepository::isSLIMOperandAvailable(llvm::Value *value) {
return OperandRepository::value_to_slim_operand.find(value) !=
OperandRepository::value_to_slim_operand.end();
}
void OperandRepository::setSLIMOperand(llvm::Value *value,
SLIMOperand *slim_operand) {
OperandRepository::value_to_slim_operand[value] = slim_operand;
}
// Returns the return operand of a function
SLIMOperand *
OperandRepository::getFunctionReturnOperand(llvm::Function *function) {
return OperandRepository::function_return_operand[function];
}
// Sets the return operand of a function
void OperandRepository::setFunctionReturnOperand(llvm::Function *function,
SLIMOperand *return_operand) {
OperandRepository::function_return_operand[function] = return_operand;
}
std::set<std::pair<SLIMOperand *, int>*> &SLIMOperand::getRefersToSourceValuesIndirection(){
return this->refers_to_source_values_with_indirection;
}
void SLIMOperand::addRefersToSourceValuesIndirection(std::pair<SLIMOperand *, int>* sourceValue) {
this->refers_to_source_values_with_indirection.insert(sourceValue);
}