Index: lib/Transforms/Scalar/SROA.cpp =================================================================== --- lib/Transforms/Scalar/SROA.cpp +++ lib/Transforms/Scalar/SROA.cpp @@ -2030,8 +2030,9 @@ Value *getAdjustedAllocaPtr(IRBuilderTy &IRB, uint64_t Offset, Type *PointerTy) { assert(Offset >= NewAllocaBeginOffset); - return getAdjustedPtr(IRB, DL, &NewAI, APInt(DL.getPointerSizeInBits(), - Offset - NewAllocaBeginOffset), + return getAdjustedPtr(IRB, DL, &NewAI, + APInt(DL.getPointerTypeSizeInBits(PointerTy), + Offset - NewAllocaBeginOffset), PointerTy); } @@ -2107,6 +2108,8 @@ uint64_t Size = NewEndOffset - NewBeginOffset; + unsigned AS = LI.getPointerAddressSpace(); + Type *TargetTy = IsSplit ? Type::getIntNTy(LI.getContext(), Size * 8) : LI.getType(); bool IsPtrAdjusted = false; @@ -2120,7 +2123,7 @@ V = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), LI.isVolatile(), "load"); } else { - Type *LTy = TargetTy->getPointerTo(); + Type *LTy = TargetTy->getPointerTo(AS); V = IRB.CreateAlignedLoad( getAdjustedAllocaPtr(IRB, NewBeginOffset, LTy), getOffsetTypeAlign(TargetTy, NewBeginOffset - NewAllocaBeginOffset), @@ -2145,7 +2148,7 @@ // 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())); + = new LoadInst(UndefValue::get(LI.getType()->getPointerTo(AS))); V = insertInteger(DL, IRB, Placeholder, V, NewBeginOffset, "insert"); LI.replaceAllUsesWith(V); @@ -2256,8 +2259,9 @@ NewSI = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment(), SI.isVolatile()); } else { + unsigned AS = SI.getPointerAddressSpace(); Value *NewPtr = getAdjustedAllocaPtr(IRB, NewBeginOffset, - V->getType()->getPointerTo()); + V->getType()->getPointerTo(AS)); NewSI = IRB.CreateAlignedStore( V, NewPtr, getOffsetTypeAlign( V->getType(), NewBeginOffset - NewAllocaBeginOffset),