diff options
Diffstat (limited to 'llvm/lib/Transforms/Scalar/SROA.cpp')
-rw-r--r-- | llvm/lib/Transforms/Scalar/SROA.cpp | 84 |
1 files changed, 46 insertions, 38 deletions
diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp index 991e9c7c52f..6be3f7903c9 100644 --- a/llvm/lib/Transforms/Scalar/SROA.cpp +++ b/llvm/lib/Transforms/Scalar/SROA.cpp @@ -1231,15 +1231,14 @@ static bool isSafePHIToSpeculate(PHINode &PN) { static void speculatePHINodeLoads(PHINode &PN) { LLVM_DEBUG(dbgs() << " original: " << PN << "\n"); - Type *LoadTy = cast<PointerType>(PN.getType())->getElementType(); + LoadInst *SomeLoad = cast<LoadInst>(PN.user_back()); + Type *LoadTy = SomeLoad->getType(); IRBuilderTy PHIBuilder(&PN); PHINode *NewPN = PHIBuilder.CreatePHI(LoadTy, PN.getNumIncomingValues(), PN.getName() + ".sroa.speculated"); // Get the AA tags and alignment to use from one of the loads. It doesn't // matter which one we get and if any differ. - LoadInst *SomeLoad = cast<LoadInst>(PN.user_back()); - AAMDNodes AATags; SomeLoad->getAAMetadata(AATags); unsigned Align = SomeLoad->getAlignment(); @@ -1270,7 +1269,8 @@ static void speculatePHINodeLoads(PHINode &PN) { IRBuilderTy PredBuilder(TI); LoadInst *Load = PredBuilder.CreateLoad( - InVal, (PN.getName() + ".sroa.speculate.load." + Pred->getName())); + LoadTy, InVal, + (PN.getName() + ".sroa.speculate.load." + Pred->getName())); ++NumLoadsSpeculated; Load->setAlignment(Align); if (AATags) @@ -1330,10 +1330,10 @@ static void speculateSelectInstLoads(SelectInst &SI) { assert(LI->isSimple() && "We only speculate simple loads"); IRB.SetInsertPoint(LI); - LoadInst *TL = - IRB.CreateLoad(TV, LI->getName() + ".sroa.speculate.load.true"); - LoadInst *FL = - IRB.CreateLoad(FV, LI->getName() + ".sroa.speculate.load.false"); + LoadInst *TL = IRB.CreateLoad(LI->getType(), TV, + LI->getName() + ".sroa.speculate.load.true"); + LoadInst *FL = IRB.CreateLoad(LI->getType(), FV, + LI->getName() + ".sroa.speculate.load.false"); NumLoadsSpeculated += 2; // Transfer alignment and AA info if present. @@ -2410,14 +2410,16 @@ private: unsigned EndIndex = getIndex(NewEndOffset); assert(EndIndex > BeginIndex && "Empty vector!"); - Value *V = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "load"); + Value *V = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, + NewAI.getAlignment(), "load"); return extractVector(IRB, V, BeginIndex, EndIndex, "vec"); } Value *rewriteIntegerLoad(LoadInst &LI) { assert(IntTy && "We cannot insert an integer to the alloca"); assert(!LI.isVolatile()); - Value *V = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "load"); + Value *V = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, + NewAI.getAlignment(), "load"); V = convertValue(DL, IRB, V, IntTy); assert(NewBeginOffset >= NewAllocaBeginOffset && "Out of bounds offset"); uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; @@ -2461,7 +2463,8 @@ private: (canConvertValue(DL, NewAllocaTy, TargetTy) || (IsLoadPastEnd && NewAllocaTy->isIntegerTy() && TargetTy->isIntegerTy()))) { - LoadInst *NewLI = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), + LoadInst *NewLI = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, + NewAI.getAlignment(), LI.isVolatile(), LI.getName()); if (AATags) NewLI->setAAMetadata(AATags); @@ -2497,9 +2500,9 @@ private: } } else { Type *LTy = TargetTy->getPointerTo(AS); - LoadInst *NewLI = IRB.CreateAlignedLoad(getNewAllocaSlicePtr(IRB, LTy), - getSliceAlign(TargetTy), - LI.isVolatile(), LI.getName()); + LoadInst *NewLI = IRB.CreateAlignedLoad( + TargetTy, getNewAllocaSlicePtr(IRB, LTy), getSliceAlign(TargetTy), + LI.isVolatile(), LI.getName()); if (AATags) NewLI->setAAMetadata(AATags); if (LI.isVolatile()) @@ -2525,8 +2528,8 @@ private: // basis for the new value. This allows us to replace the uses of LI with // the computed value, and then replace the placeholder with LI, leaving // LI only used for this computation. - Value *Placeholder = - new LoadInst(UndefValue::get(LI.getType()->getPointerTo(AS))); + Value *Placeholder = new LoadInst( + LI.getType(), UndefValue::get(LI.getType()->getPointerTo(AS))); V = insertInteger(DL, IRB, Placeholder, V, NewBeginOffset - BeginOffset, "insert"); LI.replaceAllUsesWith(V); @@ -2557,7 +2560,8 @@ private: V = convertValue(DL, IRB, V, SliceTy); // Mix in the existing elements. - Value *Old = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "load"); + Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, + NewAI.getAlignment(), "load"); V = insertVector(IRB, Old, V, BeginIndex, "vec"); } StoreInst *Store = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment()); @@ -2573,8 +2577,8 @@ private: assert(IntTy && "We cannot extract an integer from the alloca"); assert(!SI.isVolatile()); if (DL.getTypeSizeInBits(V->getType()) != IntTy->getBitWidth()) { - Value *Old = - IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "oldload"); + Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, + NewAI.getAlignment(), "oldload"); Old = convertValue(DL, IRB, Old, IntTy); assert(BeginOffset >= NewAllocaBeginOffset && "Out of bounds offset"); uint64_t Offset = BeginOffset - NewAllocaBeginOffset; @@ -2766,8 +2770,8 @@ private: if (NumElements > 1) Splat = getVectorSplat(Splat, NumElements); - Value *Old = - IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "oldload"); + Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, + NewAI.getAlignment(), "oldload"); V = insertVector(IRB, Old, Splat, BeginIndex, "vec"); } else if (IntTy) { // If this is a memset on an alloca where we can widen stores, insert the @@ -2779,8 +2783,8 @@ private: if (IntTy && (BeginOffset != NewAllocaBeginOffset || EndOffset != NewAllocaBeginOffset)) { - Value *Old = - IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "oldload"); + Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, + NewAI.getAlignment(), "oldload"); Old = convertValue(DL, IRB, Old, IntTy); uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; V = insertInteger(DL, IRB, Old, V, Offset, "insert"); @@ -2940,18 +2944,18 @@ private: // Reset the other pointer type to match the register type we're going to // use, but using the address space of the original other pointer. + Type *OtherTy; if (VecTy && !IsWholeAlloca) { if (NumElements == 1) - OtherPtrTy = VecTy->getElementType(); + OtherTy = VecTy->getElementType(); else - OtherPtrTy = VectorType::get(VecTy->getElementType(), NumElements); - - OtherPtrTy = OtherPtrTy->getPointerTo(OtherAS); + OtherTy = VectorType::get(VecTy->getElementType(), NumElements); } else if (IntTy && !IsWholeAlloca) { - OtherPtrTy = SubIntTy->getPointerTo(OtherAS); + OtherTy = SubIntTy; } else { - OtherPtrTy = NewAllocaTy->getPointerTo(OtherAS); + OtherTy = NewAllocaTy; } + OtherPtrTy = OtherTy->getPointerTo(OtherAS); Value *SrcPtr = getAdjustedPtr(IRB, DL, OtherPtr, OtherOffset, OtherPtrTy, OtherPtr->getName() + "."); @@ -2965,28 +2969,30 @@ private: Value *Src; if (VecTy && !IsWholeAlloca && !IsDest) { - Src = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "load"); + Src = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, + NewAI.getAlignment(), "load"); Src = extractVector(IRB, Src, BeginIndex, EndIndex, "vec"); } else if (IntTy && !IsWholeAlloca && !IsDest) { - Src = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "load"); + Src = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, + NewAI.getAlignment(), "load"); Src = convertValue(DL, IRB, Src, IntTy); uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; Src = extractInteger(DL, IRB, Src, SubIntTy, Offset, "extract"); } else { - LoadInst *Load = IRB.CreateAlignedLoad(SrcPtr, SrcAlign, II.isVolatile(), - "copyload"); + LoadInst *Load = IRB.CreateAlignedLoad(OtherTy, SrcPtr, SrcAlign, + II.isVolatile(), "copyload"); if (AATags) Load->setAAMetadata(AATags); Src = Load; } if (VecTy && !IsWholeAlloca && IsDest) { - Value *Old = - IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "oldload"); + Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, + NewAI.getAlignment(), "oldload"); Src = insertVector(IRB, Old, Src, BeginIndex, "vec"); } else if (IntTy && !IsWholeAlloca && IsDest) { - Value *Old = - IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "oldload"); + Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, + NewAI.getAlignment(), "oldload"); Old = convertValue(DL, IRB, Old, IntTy); uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; Src = insertInteger(DL, IRB, Old, Src, Offset, "insert"); @@ -3293,7 +3299,7 @@ private: // Load the single value and insert it using the indices. Value *GEP = IRB.CreateInBoundsGEP(nullptr, Ptr, GEPIndices, Name + ".gep"); - LoadInst *Load = IRB.CreateAlignedLoad(GEP, Align, Name + ".load"); + LoadInst *Load = IRB.CreateAlignedLoad(Ty, GEP, Align, Name + ".load"); if (AATags) Load->setAAMetadata(AATags); Agg = IRB.CreateInsertValue(Agg, Load, Indices, Name + ".insert"); @@ -3787,6 +3793,7 @@ bool SROA::presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS) { auto AS = LI->getPointerAddressSpace(); auto *PartPtrTy = PartTy->getPointerTo(AS); LoadInst *PLoad = IRB.CreateAlignedLoad( + PartTy, getAdjustedPtr(IRB, DL, BasePtr, APInt(DL.getIndexSizeInBits(AS), PartOffset), PartPtrTy, BasePtr->getName() + "."), @@ -3928,6 +3935,7 @@ bool SROA::presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS) { IRB.SetInsertPoint(LI); auto AS = LI->getPointerAddressSpace(); PLoad = IRB.CreateAlignedLoad( + PartTy, getAdjustedPtr(IRB, DL, LoadBasePtr, APInt(DL.getIndexSizeInBits(AS), PartOffset), LoadPartPtrTy, LoadBasePtr->getName() + "."), |