@@ -107,16 +107,15 @@ class IRPromoter {
107
107
LLVMContext &Ctx;
108
108
IntegerType *OrigTy = nullptr ;
109
109
unsigned PromotedWidth = 0 ;
110
+ SetVector<Value*> &Visited;
111
+ SetVector<Value*> &Sources;
112
+ SetVector<Instruction*> &Sinks;
113
+ SmallVectorImpl<Instruction*> &SafeWrap;
110
114
IntegerType *ExtTy = nullptr ;
111
115
SmallPtrSet<Value*, 8 > NewInsts;
112
116
SmallPtrSet<Instruction*, 4 > InstsToRemove;
113
117
DenseMap<Value*, SmallVector<Type*, 4 >> TruncTysMap;
114
118
SmallPtrSet<Value*, 8 > Promoted;
115
- SetVector<Value*> *Visited;
116
- SmallPtrSetImpl<Value*> *Sources;
117
- SmallPtrSetImpl<Instruction*> *Sinks;
118
- SmallPtrSetImpl<Instruction*> *SafeToPromote;
119
- SmallPtrSetImpl<Instruction*> *SafeWrap;
120
119
121
120
void ReplaceAllUsersOfWith (Value *From, Value *To);
122
121
void PrepareWrappingAdds (void );
@@ -127,18 +126,18 @@ class IRPromoter {
127
126
void Cleanup (void );
128
127
129
128
public:
130
- IRPromoter (LLVMContext &C, IntegerType *Ty, unsigned Width) :
131
- Ctx (C), OrigTy(Ty), PromotedWidth(Width) {
129
+ IRPromoter (LLVMContext &C, IntegerType *Ty, unsigned Width,
130
+ SetVector<Value*> &visited, SetVector<Value*> &sources,
131
+ SetVector<Instruction*> &sinks,
132
+ SmallVectorImpl<Instruction*> &wrap) :
133
+ Ctx (C), OrigTy(Ty), PromotedWidth(Width), Visited(visited),
134
+ Sources (sources), Sinks(sinks), SafeWrap(wrap) {
132
135
ExtTy = IntegerType::get (Ctx, PromotedWidth);
133
136
assert (OrigTy->getPrimitiveSizeInBits () < ExtTy->getPrimitiveSizeInBits ()
134
137
&& " Original type not smaller than extended type" );
135
138
}
136
139
137
- void Mutate (SetVector<Value*> &Visited,
138
- SmallPtrSetImpl<Value*> &Sources,
139
- SmallPtrSetImpl<Instruction*> &Sinks,
140
- SmallPtrSetImpl<Instruction*> &SafeToPromote,
141
- SmallPtrSetImpl<Instruction*> &SafeWrap);
140
+ void Mutate ();
142
141
};
143
142
144
143
class TypePromotion : public FunctionPass {
@@ -147,7 +146,7 @@ class TypePromotion : public FunctionPass {
147
146
unsigned RegisterBitWidth = 0 ;
148
147
SmallPtrSet<Value*, 16 > AllVisited;
149
148
SmallPtrSet<Instruction*, 8 > SafeToPromote;
150
- SmallPtrSet <Instruction*, 4 > SafeWrap;
149
+ SmallVector <Instruction*, 4 > SafeWrap;
151
150
152
151
// Does V have the same size result type as TypeSize.
153
152
bool EqualTypeSize (Value *V);
@@ -382,7 +381,7 @@ bool TypePromotion::isSafeWrap(Instruction *I) {
382
381
383
382
LLVM_DEBUG (dbgs () << " IR Promotion: Allowing safe overflow for "
384
383
<< *I << " \n " );
385
- SafeWrap.insert (I);
384
+ SafeWrap.push_back (I);
386
385
return true ;
387
386
}
388
387
@@ -451,7 +450,7 @@ void IRPromoter::PrepareWrappingAdds() {
451
450
// create an equivalent instruction using a positive immediate.
452
451
// That positive immediate can then be zext along with all the other
453
452
// immediates later.
454
- for (auto *I : * SafeWrap) {
453
+ for (auto *I : SafeWrap) {
455
454
if (I->getOpcode () != Instruction::Add)
456
455
continue ;
457
456
@@ -473,7 +472,7 @@ void IRPromoter::PrepareWrappingAdds() {
473
472
LLVM_DEBUG (dbgs () << " IR Promotion: New equivalent: " << *NewVal << " \n " );
474
473
}
475
474
for (auto *I : NewInsts)
476
- Visited-> insert (I);
475
+ Visited. insert (I);
477
476
}
478
477
479
478
void IRPromoter::ExtendSources () {
@@ -500,7 +499,7 @@ void IRPromoter::ExtendSources() {
500
499
501
500
// Now, insert extending instructions between the sources and their users.
502
501
LLVM_DEBUG (dbgs () << " IR Promotion: Promoting sources:\n " );
503
- for (auto V : * Sources) {
502
+ for (auto V : Sources) {
504
503
LLVM_DEBUG (dbgs () << " - " << *V << " \n " );
505
504
if (auto *I = dyn_cast<Instruction>(V))
506
505
InsertZExt (I, I);
@@ -521,12 +520,12 @@ void IRPromoter::PromoteTree() {
521
520
522
521
// Mutate the types of the instructions within the tree. Here we handle
523
522
// constant operands.
524
- for (auto *V : * Visited) {
525
- if (Sources-> count (V))
523
+ for (auto *V : Visited) {
524
+ if (Sources. count (V))
526
525
continue ;
527
526
528
527
auto *I = cast<Instruction>(V);
529
- if (Sinks-> count (I))
528
+ if (Sinks. count (I))
530
529
continue ;
531
530
532
531
for (unsigned i = 0 , e = I->getNumOperands (); i < e; ++i) {
@@ -558,7 +557,7 @@ void IRPromoter::TruncateSinks() {
558
557
if (!isa<Instruction>(V) || !isa<IntegerType>(V->getType ()))
559
558
return nullptr ;
560
559
561
- if ((!Promoted.count (V) && !NewInsts.count (V)) || Sources-> count (V))
560
+ if ((!Promoted.count (V) && !NewInsts.count (V)) || Sources. count (V))
562
561
return nullptr ;
563
562
564
563
LLVM_DEBUG (dbgs () << " IR Promotion: Creating " << *TruncTy << " Trunc for "
@@ -572,7 +571,7 @@ void IRPromoter::TruncateSinks() {
572
571
573
572
// Fix up any stores or returns that use the results of the promoted
574
573
// chain.
575
- for (auto I : * Sinks) {
574
+ for (auto I : Sinks) {
576
575
LLVM_DEBUG (dbgs () << " IR Promotion: For Sink: " << *I << " \n " );
577
576
578
577
// Handle calls separately as we need to iterate over arg operands.
@@ -613,7 +612,7 @@ void IRPromoter::Cleanup() {
613
612
LLVM_DEBUG (dbgs () << " IR Promotion: Cleanup..\n " );
614
613
// Some zexts will now have become redundant, along with their trunc
615
614
// operands, so remove them
616
- for (auto V : * Visited) {
615
+ for (auto V : Visited) {
617
616
if (!isa<ZExtInst>(V))
618
617
continue ;
619
618
@@ -652,8 +651,8 @@ void IRPromoter::ConvertTruncs() {
652
651
LLVM_DEBUG (dbgs () << " IR Promotion: Converting truncs..\n " );
653
652
IRBuilder<> Builder{Ctx};
654
653
655
- for (auto *V : * Visited) {
656
- if (!isa<TruncInst>(V) || Sources-> count (V))
654
+ for (auto *V : Visited) {
655
+ if (!isa<TruncInst>(V) || Sources. count (V))
657
656
continue ;
658
657
659
658
auto *Trunc = cast<TruncInst>(V);
@@ -673,20 +672,10 @@ void IRPromoter::ConvertTruncs() {
673
672
}
674
673
}
675
674
676
- void IRPromoter::Mutate (SetVector<Value*> &Visited,
677
- SmallPtrSetImpl<Value*> &Sources,
678
- SmallPtrSetImpl<Instruction*> &Sinks,
679
- SmallPtrSetImpl<Instruction*> &SafeToPromote,
680
- SmallPtrSetImpl<Instruction*> &SafeWrap) {
675
+ void IRPromoter::Mutate () {
681
676
LLVM_DEBUG (dbgs () << " IR Promotion: Promoting use-def chains from "
682
677
<< OrigTy->getBitWidth () << " to " << PromotedWidth << " -bits\n " );
683
678
684
- this ->Visited = &Visited;
685
- this ->Sources = &Sources;
686
- this ->Sinks = &Sinks;
687
- this ->SafeToPromote = &SafeToPromote;
688
- this ->SafeWrap = &SafeWrap;
689
-
690
679
// Cache original types of the values that will likely need truncating
691
680
for (auto *I : Sinks) {
692
681
if (auto *Call = dyn_cast<CallInst>(I)) {
@@ -830,8 +819,8 @@ bool TypePromotion::TryToPromote(Value *V, unsigned PromotedWidth) {
830
819
<< TypeSize << " bits to " << PromotedWidth << " \n " );
831
820
832
821
SetVector<Value*> WorkList;
833
- SmallPtrSet <Value*, 8 > Sources;
834
- SmallPtrSet <Instruction*, 4 > Sinks;
822
+ SetVector <Value*> Sources;
823
+ SetVector <Instruction*> Sinks;
835
824
SetVector<Value*> CurrentVisited;
836
825
WorkList.insert (V);
837
826
@@ -936,8 +925,9 @@ bool TypePromotion::TryToPromote(Value *V, unsigned PromotedWidth) {
936
925
if (ToPromote < 2 )
937
926
return false ;
938
927
939
- IRPromoter Promoter (*Ctx, cast<IntegerType>(OrigTy), PromotedWidth);
940
- Promoter.Mutate (CurrentVisited, Sources, Sinks, SafeToPromote, SafeWrap);
928
+ IRPromoter Promoter (*Ctx, cast<IntegerType>(OrigTy), PromotedWidth,
929
+ CurrentVisited, Sources, Sinks, SafeWrap);
930
+ Promoter.Mutate ();
941
931
return true ;
942
932
}
943
933
0 commit comments