diff --git a/llvm/lib/CodeGen/TypePromotion.cpp b/llvm/lib/CodeGen/TypePromotion.cpp --- a/llvm/lib/CodeGen/TypePromotion.cpp +++ b/llvm/lib/CodeGen/TypePromotion.cpp @@ -107,16 +107,15 @@ LLVMContext &Ctx; IntegerType *OrigTy = nullptr; unsigned PromotedWidth = 0; + SetVector &Visited; + SetVector &Sources; + SetVector &Sinks; + SmallVectorImpl &SafeWrap; IntegerType *ExtTy = nullptr; SmallPtrSet NewInsts; SmallPtrSet InstsToRemove; DenseMap> TruncTysMap; SmallPtrSet Promoted; - SetVector *Visited; - SmallPtrSetImpl *Sources; - SmallPtrSetImpl *Sinks; - SmallPtrSetImpl *SafeToPromote; - SmallPtrSetImpl *SafeWrap; void ReplaceAllUsersOfWith(Value *From, Value *To); void PrepareWrappingAdds(void); @@ -127,18 +126,18 @@ void Cleanup(void); public: - IRPromoter(LLVMContext &C, IntegerType *Ty, unsigned Width) : - Ctx(C), OrigTy(Ty), PromotedWidth(Width) { + IRPromoter(LLVMContext &C, IntegerType *Ty, unsigned Width, + SetVector &visited, SetVector &sources, + SetVector &sinks, + SmallVectorImpl &wrap) : + Ctx(C), OrigTy(Ty), PromotedWidth(Width), Visited(visited), + Sources(sources), Sinks(sinks), SafeWrap(wrap) { ExtTy = IntegerType::get(Ctx, PromotedWidth); assert(OrigTy->getPrimitiveSizeInBits() < ExtTy->getPrimitiveSizeInBits() && "Original type not smaller than extended type"); } - void Mutate(SetVector &Visited, - SmallPtrSetImpl &Sources, - SmallPtrSetImpl &Sinks, - SmallPtrSetImpl &SafeToPromote, - SmallPtrSetImpl &SafeWrap); + void Mutate(); }; class TypePromotion : public FunctionPass { @@ -147,7 +146,7 @@ unsigned RegisterBitWidth = 0; SmallPtrSet AllVisited; SmallPtrSet SafeToPromote; - SmallPtrSet SafeWrap; + SmallVector SafeWrap; // Does V have the same size result type as TypeSize. bool EqualTypeSize(Value *V); @@ -382,7 +381,7 @@ LLVM_DEBUG(dbgs() << "IR Promotion: Allowing safe overflow for " << *I << "\n"); - SafeWrap.insert(I); + SafeWrap.push_back(I); return true; } @@ -451,7 +450,7 @@ // create an equivalent instruction using a positive immediate. // That positive immediate can then be zext along with all the other // immediates later. - for (auto *I : *SafeWrap) { + for (auto *I : SafeWrap) { if (I->getOpcode() != Instruction::Add) continue; @@ -473,7 +472,7 @@ LLVM_DEBUG(dbgs() << "IR Promotion: New equivalent: " << *NewVal << "\n"); } for (auto *I : NewInsts) - Visited->insert(I); + Visited.insert(I); } void IRPromoter::ExtendSources() { @@ -500,7 +499,7 @@ // Now, insert extending instructions between the sources and their users. LLVM_DEBUG(dbgs() << "IR Promotion: Promoting sources:\n"); - for (auto V : *Sources) { + for (auto V : Sources) { LLVM_DEBUG(dbgs() << " - " << *V << "\n"); if (auto *I = dyn_cast(V)) InsertZExt(I, I); @@ -521,12 +520,12 @@ // Mutate the types of the instructions within the tree. Here we handle // constant operands. - for (auto *V : *Visited) { - if (Sources->count(V)) + for (auto *V : Visited) { + if (Sources.count(V)) continue; auto *I = cast(V); - if (Sinks->count(I)) + if (Sinks.count(I)) continue; for (unsigned i = 0, e = I->getNumOperands(); i < e; ++i) { @@ -558,7 +557,7 @@ if (!isa(V) || !isa(V->getType())) return nullptr; - if ((!Promoted.count(V) && !NewInsts.count(V)) || Sources->count(V)) + if ((!Promoted.count(V) && !NewInsts.count(V)) || Sources.count(V)) return nullptr; LLVM_DEBUG(dbgs() << "IR Promotion: Creating " << *TruncTy << " Trunc for " @@ -572,7 +571,7 @@ // Fix up any stores or returns that use the results of the promoted // chain. - for (auto I : *Sinks) { + for (auto I : Sinks) { LLVM_DEBUG(dbgs() << "IR Promotion: For Sink: " << *I << "\n"); // Handle calls separately as we need to iterate over arg operands. @@ -613,7 +612,7 @@ LLVM_DEBUG(dbgs() << "IR Promotion: Cleanup..\n"); // Some zexts will now have become redundant, along with their trunc // operands, so remove them - for (auto V : *Visited) { + for (auto V : Visited) { if (!isa(V)) continue; @@ -652,8 +651,8 @@ LLVM_DEBUG(dbgs() << "IR Promotion: Converting truncs..\n"); IRBuilder<> Builder{Ctx}; - for (auto *V : *Visited) { - if (!isa(V) || Sources->count(V)) + for (auto *V : Visited) { + if (!isa(V) || Sources.count(V)) continue; auto *Trunc = cast(V); @@ -673,20 +672,10 @@ } } -void IRPromoter::Mutate(SetVector &Visited, - SmallPtrSetImpl &Sources, - SmallPtrSetImpl &Sinks, - SmallPtrSetImpl &SafeToPromote, - SmallPtrSetImpl &SafeWrap) { +void IRPromoter::Mutate() { LLVM_DEBUG(dbgs() << "IR Promotion: Promoting use-def chains from " << OrigTy->getBitWidth() << " to " << PromotedWidth << "-bits\n"); - this->Visited = &Visited; - this->Sources = &Sources; - this->Sinks = &Sinks; - this->SafeToPromote = &SafeToPromote; - this->SafeWrap = &SafeWrap; - // Cache original types of the values that will likely need truncating for (auto *I : Sinks) { if (auto *Call = dyn_cast(I)) { @@ -830,8 +819,8 @@ << TypeSize << " bits to " << PromotedWidth << "\n"); SetVector WorkList; - SmallPtrSet Sources; - SmallPtrSet Sinks; + SetVector Sources; + SetVector Sinks; SetVector CurrentVisited; WorkList.insert(V); @@ -936,8 +925,9 @@ if (ToPromote < 2) return false; - IRPromoter Promoter(*Ctx, cast(OrigTy), PromotedWidth); - Promoter.Mutate(CurrentVisited, Sources, Sinks, SafeToPromote, SafeWrap); + IRPromoter Promoter(*Ctx, cast(OrigTy), PromotedWidth, + CurrentVisited, Sources, Sinks, SafeWrap); + Promoter.Mutate(); return true; }