From 9bc67da0a9982f2f7597d1d46cf18f079e4f8f98 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 3 Feb 2009 19:45:44 +0000 Subject: [PATCH] convert ConvertUsesOfLoadToScalar to use IRBuilder, no functionality change. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@63652 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/ScalarReplAggregates.cpp | 70 ++++++++++++-------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/lib/Transforms/Scalar/ScalarReplAggregates.cpp b/lib/Transforms/Scalar/ScalarReplAggregates.cpp index 5d718d45341..4e5cb682149 100644 --- a/lib/Transforms/Scalar/ScalarReplAggregates.cpp +++ b/lib/Transforms/Scalar/ScalarReplAggregates.cpp @@ -130,7 +130,7 @@ namespace { bool &SawVec, uint64_t Offset, unsigned AllocaSize); void ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, uint64_t Offset); Value *ConvertUsesOfLoadToScalar(LoadInst *LI, AllocaInst *NewAI, - uint64_t Offset); + uint64_t Offset, IRBuilder<> &Builder); Value *ConvertScalar_InsertValue(Value *StoredVal, Value *ExistingVal, uint64_t Offset, IRBuilder<> &Builder); static Instruction *isOnlyCopiedFromConstantGlobal(AllocationInst *AI); @@ -1319,24 +1319,6 @@ void SROA::ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, uint64_t Offset) { while (!Ptr->use_empty()) { Instruction *User = cast(Ptr->use_back()); - if (LoadInst *LI = dyn_cast(User)) { - LI->replaceAllUsesWith(ConvertUsesOfLoadToScalar(LI, NewAI, Offset)); - LI->eraseFromParent(); - continue; - } - - if (StoreInst *SI = dyn_cast(User)) { - assert(SI->getOperand(0) != Ptr && "Consistency error!"); - - IRBuilder<> Builder(SI->getParent(), SI); - Value *Old = Builder.CreateLoad(NewAI, (NewAI->getName()+".in").c_str()); - Value *New = ConvertScalar_InsertValue(SI->getOperand(0), Old, Offset, - Builder); - Builder.CreateStore(New, NewAI); - SI->eraseFromParent(); - continue; - } - if (BitCastInst *CI = dyn_cast(User)) { ConvertUsesToScalar(CI, NewAI, Offset); CI->eraseFromParent(); @@ -1353,6 +1335,25 @@ void SROA::ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, uint64_t Offset) { continue; } + IRBuilder<> Builder(User->getParent(), User); + + if (LoadInst *LI = dyn_cast(User)) { + Value *LoadVal = ConvertUsesOfLoadToScalar(LI, NewAI, Offset, Builder); + LI->replaceAllUsesWith(LoadVal); + LI->eraseFromParent(); + continue; + } + + if (StoreInst *SI = dyn_cast(User)) { + assert(SI->getOperand(0) != Ptr && "Consistency error!"); + Value *Old = Builder.CreateLoad(NewAI, (NewAI->getName()+".in").c_str()); + Value *New = ConvertScalar_InsertValue(SI->getOperand(0), Old, Offset, + Builder); + Builder.CreateStore(New, NewAI); + SI->eraseFromParent(); + continue; + } + // If this is a constant sized memset of a constant value (e.g. 0) we can // transform it into a store of the expanded constant value. if (MemSetInst *MSI = dyn_cast(User)) { @@ -1368,8 +1369,6 @@ void SROA::ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, uint64_t Offset) { for (unsigned i = 1; i != NumBytes; ++i) APVal |= APVal << 8; - IRBuilder<> Builder(MSI->getParent(), MSI); - Value *Old = Builder.CreateLoad(NewAI, (NewAI->getName()+".in").c_str()); Value *New = ConvertScalar_InsertValue(ConstantInt::get(APVal), Old, Offset, Builder); @@ -1393,9 +1392,9 @@ void SROA::ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, uint64_t Offset) { /// Offset is an offset from the original alloca, in bits that need to be /// shifted to the right. By the end of this, there should be no uses of Ptr. Value *SROA::ConvertUsesOfLoadToScalar(LoadInst *LI, AllocaInst *NewAI, - uint64_t Offset) { + uint64_t Offset, IRBuilder<> &Builder) { // The load is a bit extract from NewAI shifted right by Offset bits. - Value *NV = new LoadInst(NewAI, LI->getName(), LI); + Value *NV = Builder.CreateLoad(NewAI, "tmp"); // If the load is of the whole new alloca, no conversion is needed. if (NV->getType() == LI->getType() && Offset == 0) @@ -1405,7 +1404,7 @@ Value *SROA::ConvertUsesOfLoadToScalar(LoadInst *LI, AllocaInst *NewAI, // access or a bitcast to another vector type of the same size. if (const VectorType *VTy = dyn_cast(NV->getType())) { if (isa(LI->getType())) - return new BitCastInst(NV, LI->getType(), LI->getName(), LI); + return Builder.CreateBitCast(NV, LI->getType(), "tmp"); // Otherwise it must be an element access. unsigned Elt = 0; @@ -1415,10 +1414,11 @@ Value *SROA::ConvertUsesOfLoadToScalar(LoadInst *LI, AllocaInst *NewAI, assert(EltSize*Elt == Offset && "Invalid modulus in validity checking"); } // Return the element extracted out of it. - Value *V = new ExtractElementInst(NV, ConstantInt::get(Type::Int32Ty, Elt), - "tmp", LI); + Value *V = Builder.CreateExtractElement(NV, + ConstantInt::get(Type::Int32Ty,Elt), + "tmp"); if (V->getType() != LI->getType()) - V = new BitCastInst(V, LI->getType(), "tmp", LI); + V = Builder.CreateBitCast(V, LI->getType(), "tmp"); return V; } @@ -1442,20 +1442,16 @@ Value *SROA::ConvertUsesOfLoadToScalar(LoadInst *LI, AllocaInst *NewAI, // We do this to support (f.e.) loads off the end of a structure where // only some bits are used. if (ShAmt > 0 && (unsigned)ShAmt < NTy->getBitWidth()) - NV = BinaryOperator::CreateLShr(NV, - ConstantInt::get(NV->getType(), ShAmt), - LI->getName(), LI); + NV = Builder.CreateLShr(NV, ConstantInt::get(NV->getType(), ShAmt), "tmp"); else if (ShAmt < 0 && (unsigned)-ShAmt < NTy->getBitWidth()) - NV = BinaryOperator::CreateShl(NV, - ConstantInt::get(NV->getType(), -ShAmt), - LI->getName(), LI); + NV = Builder.CreateShl(NV, ConstantInt::get(NV->getType(), -ShAmt), "tmp"); // Finally, unconditionally truncate the integer to the right width. unsigned LIBitWidth = TD->getTypeSizeInBits(LI->getType()); if (LIBitWidth < NTy->getBitWidth()) - NV = new TruncInst(NV, IntegerType::get(LIBitWidth), LI->getName(), LI); + NV = Builder.CreateTrunc(NV, IntegerType::get(LIBitWidth), "tmp"); else if (LIBitWidth > NTy->getBitWidth()) - NV = new ZExtInst(NV, IntegerType::get(LIBitWidth), LI->getName(), LI); + NV = Builder.CreateZExt(NV, IntegerType::get(LIBitWidth), "tmp"); // If the result is an integer, this is a trunc or bitcast. if (isa(LI->getType())) { @@ -1463,10 +1459,10 @@ Value *SROA::ConvertUsesOfLoadToScalar(LoadInst *LI, AllocaInst *NewAI, } else if (LI->getType()->isFloatingPoint() || isa(LI->getType())) { // Just do a bitcast, we know the sizes match up. - NV = new BitCastInst(NV, LI->getType(), LI->getName(), LI); + NV = Builder.CreateBitCast(NV, LI->getType(), "tmp"); } else { // Otherwise must be a pointer. - NV = new IntToPtrInst(NV, LI->getType(), LI->getName(), LI); + NV = Builder.CreateIntToPtr(NV, LI->getType(), "tmp"); } assert(NV->getType() == LI->getType() && "Didn't convert right?"); return NV; -- 2.11.0