-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPartialExecuter.cpp
More file actions
1938 lines (1758 loc) · 56.3 KB
/
PartialExecuter.cpp
File metadata and controls
1938 lines (1758 loc) · 56.3 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
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
//===-- CheerpWriter/PartialExecuter.cpp - Analyze functions CFG to mark unreachable BBs --===//
//
// Cheerp: The C++ compiler for the Web
//
// This file is distributed under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE.TXT for details.
//
// Copyright 2021-2023 Leaning Technologies
//
//===----------------------------------------------------------------------===//
//
// This file implement PartialExecuter, an optimization pass that aims to do a
// guided visit of a Function CFG, propagating information from call-sites.
//
// Partial Executer uses the ExecutionEngine/Interpreter logic to compute the
// results of Instruction whose operands are known while skipping over non
// computable instructions.
//
// Main components are:
// * PartialInterpreter: used to wrap Interpreter execution and keep an updated
// map of the strongly known bits for each Instruction during a given visit
// * SCC-visitor: used to guide execution allowing to go recover and 'skip' over
// region of the CFG that are not predictable but can guarantee that execution
// will restart from a given set of BBs (see BasicBlockGroupNode for explanation)
//
// Example of partial-preexecutable function:
// printf ('known format string', ...)
//
// Can be fully partially executed proving as unreachable the parts connected to
// non-used formats (eg. printing of double could be dropped).
//
// When a function has multiple call sites, the set of known to be alive edges will
// be basically the union of over the call sites.
// When a function is externally reachable OR has indirect uses, it will be considered
// as having an special unknown call site.
//
// PartialExecuter analyze and modify a Module IR in a fully generic mode, altough
// the main advantages will likely be found while executing it during LTO (since more
// knowledge on call sites is present).
//
//===----------------------------------------------------------------------===//
#include "../ExecutionEngine/Interpreter/Interpreter.h"
#include "llvm/ADT/SCCIterator.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Cheerp/DeterministicUnorderedSet.h"
#include "llvm/Cheerp/PartialExecuter.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/ValueMap.h"
#include "llvm/InitializePasses.h"
#include <map>
#include <unordered_map>
#include <vector>
#include "llvm/Cheerp/Utility.h"
#include "llvm/Cheerp/BuiltinInstructions.h"
#define DEBUG_TYPE "PartialExecuter"
STATISTIC(NumRemovedEdges, "Number of edges in the CFG that have been removed");
STATISTIC(NumModifyiedFunctions, "Number of functions modyified by PartialExecuter");
STATISTIC(NumTimesBumbedGlobals, "Number of times a GlobalVariable alignment has been increased");
using namespace llvm;
typedef cheerp::DeterministicUnorderedSet<BasicBlock *, cheerp::RestrictionsLifted::NoErasure> DeterministicBBSet;
typedef llvm::DenseSet<std::pair<GlobalVariable*, uint32_t> > NewAlignmentData;
namespace cheerp {
const uint32_t MAX_NUMBER_OF_VISITS_PER_BB = 100u;
class FunctionData;
class ModuleData;
class PartialInterpreter : public llvm::Interpreter {
friend FunctionData;
friend ModuleData;
enum BitMask : uint32_t
{
NONE = 0,
ALIGNED2 = 0x01,
ALIGNED4 = 0x03,
ALIGNED8 = 0x07,
ALL = 0xff,
// Currently only one of those masks is ever used
// This is since getBitMask() either returns one of those values
// (either directly or indirectly by doing min(mask1, mask2))
};
std::vector<std::pair<llvm::Value*, llvm::Value*> > computedPhisValues;
std::vector<llvm::PHINode*> notComputedPhis;
// immutableLoadIntervals represents the ranges (when queried by the addresses computed by the Interpreter) that are known to be constant
// currently this correspond to constant GlobalVariable, but could be possibly generalized further (eg. if you can prove they are constant,
// and having always the same value, at every call site.
// Example: main function can assume that any GlobalVariable (even mutable one) will still have the initial state as long as it can be proven
// that no store to those GlobalVariable have been performed
std::vector<std::pair<long long,long long>> immutableLoadIntervals;
NewAlignmentData newAlignmentData;
llvm::ValueMap<llvm::Constant*, int> fullyKnownCEs;
llvm::DenseMap<const llvm::Function*, uint32_t> functionCounters;
bool isGlobalVariablePartiallyExecutable(const GlobalVariable& GVar)
{
if (!GVar.hasInitializer())
return false;
if (!GVar.hasInternalLinkage())
return false;
assert(GVar.isExternallyInitialized() == false);
return true;
}
//Compute whether a given Value is a constant & it's value can be executed beforehand
bool isValueComputedConstant(const llvm::Value* V)
{
// TODO(carlo): this can easily be memoized since there is no state
if (!isa<Constant>(V))
return false;
if (const GlobalVariable* GVar = dyn_cast_or_null<GlobalVariable>(V))
{
const bool res = isGlobalVariablePartiallyExecutable(*GVar);
if (res)
{
// We are required to bump GlobalVariables alignment only if they are used (potentially indirectly)
// by a PtrToInt, since that makes aligment observable, but here we are going with the simpler solution
// to mark them as always observable.
// Note that the only missing cases are basically Load of GEPs of constant strings, for which we may end up
// wasting some bytes by having a higher alignment
newAlignmentData.insert({const_cast<GlobalVariable*>(GVar), 4});
}
return res;
}
if (const ConstantExpr* CE = dyn_cast_or_null<ConstantExpr>(V))
{
for (auto& op : CE->operands())
{
if (!isValueComputedConstant(op))
return false;
}
}
return true;
}
public:
// While visiting PHINodes of a BasicBlock, incomingBB will hold the incoming (if uniquely identified) or nullptr
const llvm::BasicBlock* incomingBB{nullptr};
// Auxiliary map to determin whether a value can be queried to the Interpreter and how many bits are actually known
std::deque<llvm::DenseMap<const llvm::Value*, BitMask>> stronglyKnownBits;
std::deque<llvm::DenseMap<const llvm::Value*, GlobalVariable*>> pointerBases;
GenericValue getOperandValue(Value* V)
{
return Interpreter::getOperandValue(V, getTopCallFrame());
}
void assignToMaps(const Value* V, const BitMask bitMask, GlobalVariable* pointerBase)
{
stronglyKnownBits.back()[V] = bitMask;
pointerBases.back()[V] = pointerBase;
}
void assignToMaps(Value* V, const BitMask bitMask, GenericValue GV, GlobalVariable* pointerBase)
{
assignToMaps(V, bitMask, pointerBase);
getTopCallFrame().Values[V] = GV;
}
void assignToMaps(Value* V, Value* toAssign)
{
assignToMaps(V, getBitMask(toAssign), getOperandValue(toAssign), getPointerBase(toAssign));
}
void removeFromMaps(Value* V)
{
stronglyKnownBits.back().erase(V);
getTopCallFrame().Values.erase(V);
}
void addStackFrame()
{
stronglyKnownBits.push_back(llvm::DenseMap<const llvm::Value*, BitMask>());
pointerBases.push_back(llvm::DenseMap<const llvm::Value*, GlobalVariable*>());
}
void popStackFrame()
{
stronglyKnownBits.pop_back();
pointerBases.pop_back();
Interpreter::popCallFrame();
}
uint32_t getSizeStackFrame() const
{
return stronglyKnownBits.size();
}
BitMask computeStronglyKnownBits(uint32_t opcode, const llvm::User& U)
{
BitMask min = BitMask::ALL;
for (auto& op : U.operands())
{
min = BitMask(min & getBitMask(op));
}
switch (opcode)
{
case Instruction::Load:
{
return BitMask::ALL;
}
// TODO(carlo): Select can possibly be specialised further
case Instruction::Select:
case Instruction::Or:
case Instruction::Xor:
case Instruction::Add:
case Instruction::Mul:
case Instruction::PtrToInt:
case Instruction::BitCast:
case Instruction::GetElementPtr:
{
//Those all work modulo 2^k
return min;
}
case Instruction::Sub:
{
if(min == BitMask::ALL)
return BitMask::ALL;
// If both sides of the operation are based on the same global we can recover all the bits
GlobalValue* pointerBase0 = getPointerBase(U.getOperand(0));
if(pointerBase0 != nullptr && pointerBase0 == getPointerBase(U.getOperand(1)))
return BitMask::ALL;
return min;
}
case Instruction::And:
{
//If either is Constant && less than equal min -> ALL
if (const ConstantInt* CI = dyn_cast<ConstantInt>(U.getOperand(0)))
{
if (CI->getZExtValue() <= min)
return BitMask::ALL;
}
if (const ConstantInt* CI = dyn_cast<ConstantInt>(U.getOperand(1)))
{
if (CI->getZExtValue() <= min)
return BitMask::ALL;
}
[[clang::fallthrough]];
}
default:
{
//Some operands NOT fully known, means in general no information is conserved
break;
}
}
if (min == BitMask::ALL)
{
switch (opcode)
{
case Instruction::ICmp:
case Instruction::FCmp:
case Instruction::And:
case Instruction::SExt:
case Instruction::ZExt:
case Instruction::Trunc:
case Instruction::LShr:
case Instruction::AShr:
case Instruction::Shl:
case Instruction::SRem:
case Instruction::FPToSI:
case Instruction::FPTrunc:
case Instruction::SIToFP:
case Instruction::URem:
case Instruction::FMul:
case Instruction::FAdd:
case Instruction::FSub:
case Instruction::FDiv:
case Instruction::UDiv:
case Instruction::SDiv:
case Instruction::FPExt:
case Instruction::UIToFP:
{
return BitMask::ALL;
}
case Instruction::IntToPtr:
case Instruction::Alloca:
default:
{
break;
}
}
}
return BitMask::NONE;
}
GlobalVariable* computePointerBase(const Instruction& I)
{
switch(I.getOpcode())
{
case Instruction::PtrToInt:
return getPointerBase(I.getOperand(0));
default:
break;
}
return nullptr;
}
llvm::BasicBlock* visitBasicBlock(FunctionData& data, llvm::BasicBlock& BB, bool& BBProgress);
explicit PartialInterpreter(std::unique_ptr<llvm::Module> M)
: llvm::Interpreter(std::move(M), /*preExecute*/false)
{
}
GlobalVariable* getPointerBase(Value* V)
{
assert(V);
if (GlobalVariable* GV = dyn_cast<GlobalVariable>(V))
{
return GV;
}
if (ConstantExpr* CE = dyn_cast<ConstantExpr>(V))
{
if (CE->getOpcode() == Instruction::GetElementPtr)
{
GEPOperator *GEP = cast<GEPOperator>(CE);
if (GEP->hasAllZeroIndices())
return getPointerBase(CE->getOperand(0));
}
}
if (GetElementPtrInst* GEP = dyn_cast<GetElementPtrInst>(V))
{
return getPointerBase(GEP->getOperand(0));
}
auto it = pointerBases.back().find(V);
if(it != pointerBases.back().end())
return it->second;
return nullptr;
}
BitMask getBitMask(llvm::Value* V)
{
if (!V)
return BitMask::NONE;
if (isa<ConstantData>(V))
return BitMask::ALL;
if (isa<Function>(V))
return BitMask::NONE;
if (GlobalVariable* GV = dyn_cast<GlobalVariable>(V))
{
if (getDataLayout().getPreferredAlign(GV) >= 8)
return BitMask::ALIGNED8;
if (getDataLayout().getPreferredAlign(GV) == 4)
return BitMask::ALIGNED4;
// Interpreter will alrady align everything to 4, here we take note that we have to
// align GlobalVariables (at least the one it's queried on) to 4.
// This will allow further optimizations like executing loops like:
// while (addr % 4) {
// doStuff(*addr);
// addr++;
// }
newAlignmentData.insert({GV, 4});
return BitMask::ALIGNED4;
}
if (ConstantExpr* CE = dyn_cast<ConstantExpr>(V))
{
BitMask ret = computeStronglyKnownBits(CE->getOpcode(), *CE);
// TODO: Support other types
if(ret == BitMask::ALL && CE->getType()->isIntegerTy())
fullyKnownCEs.insert(std::make_pair(CE, 0));
return ret;
}
auto it = stronglyKnownBits.back().find(V);
if (it != stronglyKnownBits.back().end())
return it->second;
return BitMask::NONE;
}
bool isValueComputed(const llvm::Value* V)
{
if (isValueComputedConstant(V))
return true;
if (isa<BasicBlock>(V))
return true;
if (isa<Argument>(V) || isa<Instruction>(V))
return stronglyKnownBits.back().count(V);
return false;
}
bool areOperandsComputed(const llvm::Instruction& I)
{
if (isa<SelectInst>(I))
{
if (isValueComputed(I.getOperand(0)))
{
GenericValue V = getOperandValue(I.getOperand(0));
if (V.IntVal == 0u)
{
if (isValueComputed(I.getOperand(2)))
return true;
}
else
{
if (isValueComputed(I.getOperand(1)))
return true;
}
}
}
for (auto& op : I.operands())
{
if (!isValueComputed(op))
return false;
}
if (const SwitchInst* SI = dyn_cast <SwitchInst>(&I))
{
if ((getBitMask(SI->getCondition()) != BitMask::ALL) )
return false;
}
if (const BranchInst* BI = dyn_cast <BranchInst>(&I))
{
if (BI->isConditional())
if ((getBitMask(BI->getCondition()) == BitMask::NONE))
return false;
}
return true;
}
void addAlignmentRequirement(NewAlignmentData& moduleAlignmentData)
{
for (auto& pair : newAlignmentData)
moduleAlignmentData.insert(pair);
}
//We are going to interpret a CallInst, we need to add a stack frame and forward the known arguments
void forwardArgumentsToNextFrame(CallInst& CI)
{
//This logic should work similarly for generic CallBase when those are allowed
assert(!hasToBeSkipped(CI));
const Function& calledFunc = *cast<Function>(CI.getCalledFunction());
const uint32_t numFixedParams = calledFunc.getFunctionType()->getNumParams();
std::vector<std::pair<const llvm::Value*, BitMask>> knownArgs;
uint32_t argIndex=0;
for (auto& op : CI.args())
{
if (argIndex >= numFixedParams)
break; //This is possible when VAArgs are involved, but we are not forwarding those
if (isValueComputed(op))
knownArgs.push_back({calledFunc.getArg(argIndex), getBitMask(op)});
argIndex++;
}
addStackFrame();
for (auto& pair : knownArgs)
assignToMaps(pair.first, pair.second, nullptr);
}
bool hasToBeSkipped(llvm::Instruction& I)
{
if (!areOperandsComputed(I))
return true;
switch (I.getOpcode())
{
case Instruction::Br:
case Instruction::ICmp:
case Instruction::FCmp:
case Instruction::GetElementPtr:
case Instruction::PHI:
case Instruction::Ret:
case Instruction::Select:
case Instruction::Switch:
case Instruction::Alloca:
{
return false;
}
case Instruction::Call:
{
const Function* calledFunc = dyn_cast_or_null<Function>(cast<CallInst>(I).getCalledFunction());
if (!calledFunc)
return true;
if (calledFunc->getIntrinsicID() == Intrinsic::cheerp_typed_ptrcast)
return false;
if (isAlreadyInCallStack(calledFunc))
return true;
if (calledFunc->isDeclaration())
return true;
if (calledFunc->hasWeakLinkage())
return true;
return false;
}
case Instruction::Load:
{
LoadInst& LI = cast<LoadInst>(I);
GenericValue SRC = getOperandValue(LI.getPointerOperand());
GenericValue *Ptr = (GenericValue*)GVTORP(SRC);
const long long loadSize = getDataLayout().getTypeAllocSize(LI.getType());
for (const auto& interval : immutableLoadIntervals)
{
if ((long long)Ptr >= interval.first && ((long long)Ptr+loadSize) <= interval.second)
return false;
}
// TODO(carlo): Possibly even Loads of Alloca might be proven valid
break;
}
case Instruction::SRem:
case Instruction::URem:
case Instruction::SDiv:
case Instruction::UDiv:
{
Value *Src2 = I.getOperand(1);
if (isValueComputed(Src2) && getOperandValue(Src2).IntVal.isZero())
return true; // Division or remainder by zero
return false; // Is a binary operator
}
default:
{
if (isa<CastInst>(I) || isa<BinaryOperator>(I))
return false;
// Default case is for Instruction to be skipped
return true;
}
}
return true;
}
static bool isKnownConstant(Constant* C)
{
if(isa<GlobalValue>(C))
{
// These address will be different at runtime
return false;
}
for(uint32_t i=0;i<C->getNumOperands();i++)
{
if(!isKnownConstant(cast<Constant>(C->getOperand(i))))
return false;
}
return true;
}
void computeValidLoadIntervals(Module& _module)
{
// immutableLoadIntervals is populated as a first thing after PartialExecuter instantiation
// create a 'virtual' call frame, since it might be needed by getOperandValue
createStartingCallFrame();
assert(immutableLoadIntervals.empty());
for (auto& global : _module.globals())
{
GlobalVariable* GV = dyn_cast<GlobalVariable>(&global);
if (!GV)
continue;
if (!isGlobalVariablePartiallyExecutable(*GV))
continue;
if (!GV->isConstant())
continue;
if (!isKnownConstant(GV->getInitializer()))
continue;
// This constraint is here only given that currently the ExecutionEngines assumes GlobalVariable to be named
if (!GV->hasName())
continue;
GenericValue SRC = getOperandValue(GV);
GenericValue *Ptr = (GenericValue*)GVTORP(SRC);
immutableLoadIntervals.push_back({(long long)Ptr, ((long long)Ptr)+getDataLayout().getTypeAllocSize(GV->getInitializer()->getType())});
}
// detach the 'virtual' call frame
popCallFrame();
}
// Given a Terminator, find (if there is enough information do to so) what will be the next visited BB
// nullptr means failure to determine the next BB, and have to be handled by the caller
// (eg. falling back to visiting every successor)
BasicBlock* findNextBasicBlock(llvm::Instruction& I)
{
// Only terminators that keep the control flow inside a given Function are treated hereV
assert(I.isTerminator());
switch (I.getOpcode())
{
case Instruction::Br:
{
BranchInst& BI = cast<BranchInst>(I);
if (!BI.isConditional())
return BI.getSuccessor(0);
Value* condition = BI.getCondition();
if (isValueComputed(condition) && (getBitMask(condition) == BitMask::ALL) )
{
GenericValue V = getOperandValue(condition);
return BI.getSuccessor((V.IntVal == 0u) ? 1 : 0);
}
break;
}
case Instruction::Switch:
{
SwitchInst& SI = cast<SwitchInst>(I);
Value* condition = SI.getCondition();
if (isValueComputed(condition) && (getBitMask(condition) == BitMask::ALL) )
{
GenericValue conditionValue = getOperandValue(condition);
auto c = SI.findCaseValue(ConstantInt::get(SI.getContext(), conditionValue.IntVal ));
return c->getCaseSuccessor();
}
break;
}
default:
{
// Default handling of not doing anything is conservative
break;
}
}
return nullptr;
}
bool addToCounter(const llvm::Function* F)
{
return (++functionCounters[F] > 0x1000);
}
void visitOuter(FunctionData& data, llvm::Instruction& I, bool& BBProgress);
bool replaceKnownCEs()
{
if(fullyKnownCEs.empty())
return false;
// We need to allocate a virtual frame for the sake of resolving CEs
createStartingCallFrame();
bool changed = false;
while (!fullyKnownCEs.empty()) {
auto it = fullyKnownCEs.begin();
llvm::ConstantExpr* CE = dyn_cast<llvm::ConstantExpr>(it->first);
fullyKnownCEs.erase(it);
if(CE == nullptr)
{
// CEs might have already collapse due to previous replacements
continue;
}
GenericValue GV = getOperandValue(CE);
llvm::Constant* CI = ConstantInt::get(CE->getType(), GV.IntVal);
CE->replaceAllUsesWith(CI);
changed = true;
}
popCallFrame();
return changed;
}
/// Create a new interpreter object.
///
static ExecutionEngine* create(Module& M, std::string *ErrStr)
{
// Tell this Module to materialize everything and release the GVMaterializer.
if (Error Err = M.materializeAll()) {
std::string Msg;
handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
Msg = EIB.message();
});
if (ErrStr)
*ErrStr = Msg;
// We got an error, just return 0
return nullptr;
}
std::unique_ptr<Module> uniq(&M);
return new PartialInterpreter(std::move(uniq));
}
};
static void removeEdgeBetweenBlocks(llvm::BasicBlock* from, llvm::BasicBlock* to)
{
NumRemovedEdges++;
// Given two BasicBlocks, remove all edges between them.
// Multiple edges are legal and might happen since SwitchInst might have more that one case going to the same BB
{
auto computeCardinalityPred = [](const BasicBlock* from, const BasicBlock* to) -> uint32_t
{
uint32_t cardinality = 0;
for (const BasicBlock* bb : predecessors(to))
if (bb == from)
cardinality++;
return cardinality;
};
// First count how many edges there are betwen from and to
const uint32_t cardinalityPred = computeCardinalityPred(from, to);
// Then call removePredecessor to fix up the phis of to
for (uint32_t i=0; i<cardinalityPred; i++)
to->removePredecessor(from);
}
{
// Then fix up from's terminator
llvm::Instruction* I = from->getTerminator();
switch (I->getOpcode())
{
case Instruction::Br:
{
BranchInst* BI = cast<BranchInst>(I);
if (BI->isConditional() && BI->getSuccessor(0) != BI->getSuccessor(1))
{
//Turn to unconditional
llvm::BasicBlock* other = nullptr;
if (BI->getSuccessor(0) != to)
other = BI->getSuccessor(0);
else
other = BI->getSuccessor(1);
BI->eraseFromParent();
BranchInst::Create(other, from);
}
else
{
BI->eraseFromParent();
new UnreachableInst(from->getContext(), from);
}
return;
}
case Instruction::Switch:
{
SwitchInst* SI = cast<SwitchInst>(I);
for (SwitchInst::CaseIt i = SI->case_end(), e = SI->case_begin(); i != e;)
{
--i;
auto *Successor = i->getCaseSuccessor();
if (Successor == to) {
SI->removeCase(i);
}
}
if (SI->getDefaultDest() == to)
{
if (SI->getNumSuccessors() == 1)
{
SI->eraseFromParent();
new UnreachableInst(from->getContext(), from);
return;
}
BasicBlock* another = nullptr;
for (SwitchInst::CaseIt i = SI->case_end(), e = SI->case_begin(); i != e;)
{
--i;
auto *Successor = i->getCaseSuccessor();
another = Successor;
SI->removeCase(i);
break;
}
assert(another);
SI->setDefaultDest(another);
}
return;
}
case Instruction::Invoke:
{
// Given that we currently do not treat invokes the only way to get here would be via having the Invoke itself being unreachable
BasicBlock* BB = I->getParent();
I->replaceAllUsesWith(UndefValue::get(I->getType()));
I->eraseFromParent();
new UnreachableInst(BB->getContext(), BB);
return;
}
case Instruction::Unreachable:
{
return;
}
default:
{
llvm::errs() << "Warning: " << *I << " not hanlded in removeEdgeBetweenBlocks\n";
}
}
}
}
llvm::BasicBlock* PartialInterpreter::visitBasicBlock(FunctionData& data, llvm::BasicBlock& BB, bool& BBProgress)
{
ExecutionContext& executionContext = getTopCallFrame();
executionContext.CurBB = &BB;
executionContext.CurInst = executionContext.CurBB->begin();
while (getTopCallFrame().CurInst != BB.end())
{
// Note that here we could also execute a Call, and that implies adding a CallFrame
// executing there (possibly also in depth)
// So getTopCallFrame() has to be called since it will possibly change
visitOuter(data, *getTopCallFrame().CurInst++, BBProgress);
}
// Find (if there are enough information) the next BB to be visited
// nullptr otherwise means failure to do so, and will fall back to visit all successors
return findNextBasicBlock(*BB.getTerminator());
}
class FunctionData;
class ModuleData
{
friend class FunctionData;
// This strings is passed to PartialInterpreter factory method (and via that to Interpreter / ExecutionEngine constructors)
// that might report some recoverable error via the string.
std::string error;
llvm::Module& module;
PartialInterpreter* currentEE;
std::map<const llvm::Function*, FunctionData> functionData;
void initFunctionData();
public:
NewAlignmentData alignmentToBeBumped;
bool fail {false};
llvm::Module* getModulePtr()
{
return &module;
}
ModuleData(llvm::Module& _module)
: module(_module)
{
initFunctionData();
currentEE = (PartialInterpreter*)(PartialInterpreter::create(_module, &error));
if (currentEE == nullptr)
{
fail = true;
return;
}
assert(currentEE);
// Add 'virtual' frame, since it might be needed deep into computeValidLoadIntervals
// Then compute the valid intervals
currentEE->computeValidLoadIntervals(*getModulePtr());
assert(currentEE->getSizeStackFrame() == 0);
}
FunctionData& getFunctionData(const llvm::Function& F);
const FunctionData& getFunctionData(const llvm::Function& F) const;
PartialInterpreter* setUpPartialInterpreter(llvm::Function& F)
{
assert(currentEE->getSizeStackFrame() == 0);
currentEE->addStackFrame();
ExecutionContext& executionContext = currentEE->createStartingCallFrame();
executionContext.CurFunction = &F;
return currentEE;
}
~ModuleData()
{
if (fail)
return;
bool removed = currentEE->removeModule(getModulePtr());
(void)removed;
assert(removed);
delete currentEE;
}
bool replaceKnownCEs()
{
return currentEE->replaceKnownCEs();
}
void visitCallSitesOfAllFunctions();
};
class FunctionData
{
struct KnownValue
{
GenericValue value;
// NOTE: This is marked true also in the case non-consistent known value
// We cannot replace it anyway in such a case
bool everSkipped;
KnownValue():everSkipped(false)
{
}
};
llvm::Function& F;
std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*>> existingEdges;
typedef std::vector<Value*> VectorOfArgs;
llvm::DenseMap<const llvm::BasicBlock*, int> visitCounter;
llvm::DenseSet<std::pair<const llvm::BasicBlock*, const llvm::BasicBlock*> > visitedEdges;
llvm::DenseMap<llvm::Instruction*, KnownValue> knownValues;
ModuleData& moduleData;
std::vector<VectorOfArgs> callEquivalentQueue;
PartialInterpreter* currentEE;
VectorOfArgs getArguments(const llvm::CallBase* callBase)
{
VectorOfArgs args(F.getFunctionType()->getNumParams(), nullptr);
if (callBase)
{
for (uint32_t i=0; i<F.getFunctionType()->getNumParams(); i++)
args[i] = callBase->getArgOperand(i);
// Filter out not computed arguments
for (auto& v : args)
{
if (moduleData.currentEE->isValueComputedConstant(v))
continue;
if (isa<Argument>(v))
continue;
v = nullptr;
}
}
return args;
}
// This function try to determine whether the set of arguments a is a subset of b
// VectorOfArgs holds for a given call site the args[0], args[1], ... args[N]
// with nullptr representing any possible argument (as in unknown).
//
// Then a is a subset of b iff:
// for any index:
// either b[index] is nullptr or a[index] == b[index]
//
// Note that always returning 'false' here will only impact the time required to
// analyze call sites (since work is done in cases that are already covered).
static bool areEquivalent(const VectorOfArgs& a, const VectorOfArgs& b)
{
assert(a.size() == b.size());
const uint32_t numElements = a.size();
for (uint32_t i=0; i<numElements; i++)
{
if (b[i] == nullptr)
continue;
if (a[i] != b[i])
return false;
}
return true;
}
void enqueCallEquivalent(VectorOfArgs&& arguments)
{
// Insert the arguments in the map
// Check wether we already visited something similar
for (const auto& args : callEquivalentQueue)
{
if (areEquivalent(arguments, args))
{
return;
}
}
callEquivalentQueue.emplace_back(std::move(arguments));
}
void actualVisit();
void doneVisitCallBase()
{
assert(currentEE->getSizeStackFrame() == 1);
currentEE->popStackFrame();
currentEE->addAlignmentRequirement(moduleData.alignmentToBeBumped);
}
public:
explicit FunctionData(llvm::Function& F, ModuleData& moduleData)
: F(F), moduleData(moduleData), currentEE(nullptr)
{
}
llvm::Function* getFunction()
{
return &F;
}
uint32_t getVisitCounter(const llvm::BasicBlock* BB)
{
return visitCounter[BB];
}
void incrementVisitCounter(const llvm::BasicBlock* BB)
{
visitCounter[BB]++;
}
PartialInterpreter& getInterpreter()
{
// currentEE will be non-null while visiting a Call Equivalent
assert(currentEE);
return *currentEE;
}
bool registerEdge(const llvm::BasicBlock* from, const llvm::BasicBlock* to)
{
auto [it, is_inserted] = visitedEdges.insert({from, to});
return is_inserted;
}