Index: include/llvm/InstrTypes.h =================================================================== --- include/llvm/InstrTypes.h (revision 166296) +++ include/llvm/InstrTypes.h (working copy) @@ -17,6 +17,7 @@ #define LLVM_INSTRUCTION_TYPES_H #include "llvm/Instruction.h" +#include "llvm/DataLayout.h" #include "llvm/OperandTraits.h" #include "llvm/DerivedTypes.h" #include "llvm/ADT/Twine.h" @@ -576,6 +577,11 @@ Type *IntPtrTy ///< Integer type corresponding to pointer ) const; + /// @brief Determine if this cast is a no-op cast. + bool isNoopCast( + DataLayout &DL ///< DataLayout to get the Int Ptr type from. + ) const; + /// Determine how a pair of casts can be eliminated, if they can be at all. /// This is a helper function for both CastInst and ConstantExpr. /// @returns 0 if the CastInst pair can't be eliminated, otherwise Index: include/llvm/DataLayout.h =================================================================== --- include/llvm/DataLayout.h (revision 166296) +++ include/llvm/DataLayout.h (working copy) @@ -338,10 +338,12 @@ unsigned getPreferredTypeAlignmentShift(Type *Ty) const; /// getIntPtrType - Return an unsigned integer type that is the same size or - /// greater to the host pointer size. - /// FIXME: Need to remove the default argument when the rest of the LLVM code - /// base has been updated. - IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const; + /// greater to the pointer size based on the address space. + IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace) const; + + /// getIntPtrType - Return an unsigned integer type that is the same size or + /// greater to the pointer size based on the Type. + IntegerType *getIntPtrType(Type *) const; /// getIndexedOffset - return the offset from the beginning of the type for /// the specified indices. This is used to implement getelementptr. Index: include/llvm/Transforms/Utils/Local.h =================================================================== --- include/llvm/Transforms/Utils/Local.h (revision 166296) +++ include/llvm/Transforms/Utils/Local.h (working copy) @@ -177,16 +177,16 @@ template Value *EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &TD, User *GEP, bool NoAssumptions = false) { + // Build a mask for high order bits. + unsigned AS = cast(GEP)->getPointerAddressSpace(); gep_type_iterator GTI = gep_type_begin(GEP); - Type *IntPtrTy = TD.getIntPtrType(GEP->getContext()); + Type *IntPtrTy = TD.getIntPtrType(GEP->getContext(), AS); Value *Result = Constant::getNullValue(IntPtrTy); // If the GEP is inbounds, we know that none of the addressing operations will // overflow in an unsigned sense. bool isInBounds = cast(GEP)->isInBounds() && !NoAssumptions; - // Build a mask for high order bits. - unsigned AS = cast(GEP)->getPointerAddressSpace(); unsigned IntPtrWidth = TD.getPointerSizeInBits(AS); uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); Index: include/llvm/Analysis/MemoryBuiltins.h =================================================================== --- include/llvm/Analysis/MemoryBuiltins.h (revision 166296) +++ include/llvm/Analysis/MemoryBuiltins.h (working copy) @@ -168,7 +168,8 @@ public: ObjectSizeOffsetVisitor(const DataLayout *TD, const TargetLibraryInfo *TLI, - LLVMContext &Context, bool RoundToAlign = false); + LLVMContext &Context, bool RoundToAlign = false, + unsigned AS = 0); SizeOffsetType compute(Value *V); @@ -229,7 +230,7 @@ public: ObjectSizeOffsetEvaluator(const DataLayout *TD, const TargetLibraryInfo *TLI, - LLVMContext &Context); + LLVMContext &Context, unsigned AS = 0); SizeOffsetEvalType compute(Value *V); bool knownSize(SizeOffsetEvalType SizeOffset) { Index: include/llvm/Analysis/ScalarEvolution.h =================================================================== --- include/llvm/Analysis/ScalarEvolution.h (revision 166296) +++ include/llvm/Analysis/ScalarEvolution.h (working copy) @@ -628,7 +628,7 @@ /// getSizeOfExpr - Return an expression for sizeof on the given type. /// - const SCEV *getSizeOfExpr(Type *AllocTy); + const SCEV *getSizeOfExpr(Type *AllocTy, Type *IntPtrTy); /// getAlignOfExpr - Return an expression for alignof on the given type. /// @@ -636,7 +636,8 @@ /// getOffsetOfExpr - Return an expression for offsetof on the given field. /// - const SCEV *getOffsetOfExpr(StructType *STy, unsigned FieldNo); + const SCEV *getOffsetOfExpr(StructType *STy, Type *IntPtrTy, + unsigned FieldNo); /// getOffsetOfExpr - Return an expression for offsetof on the given field. /// Index: lib/VMCore/Instructions.cpp =================================================================== --- lib/VMCore/Instructions.cpp (revision 166296) +++ lib/VMCore/Instructions.cpp (working copy) @@ -2120,6 +2120,17 @@ return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy); } +/// @brief Determine if a cast is a no-op +bool CastInst::isNoopCast(DataLayout &DL) const { + unsigned AS = 0; + if (Opcode == Instruction::PtrToInt) + AS = getOperand(0)->getType()->getPointerAddressSpace(); + else if (Opcode == Instruction::IntToPtr) + AS = getType()->getPointerAddressSpace(); + Type *IntPtrTy = DL.getIntPtrType(getContext(), AS); + return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy); +} + /// This function determines if a pair of casts can be eliminated and what /// opcode should be used in the elimination. This assumes that there are two /// instructions like this: Index: lib/VMCore/DataLayout.cpp =================================================================== --- lib/VMCore/DataLayout.cpp (revision 166296) +++ lib/VMCore/DataLayout.cpp (working copy) @@ -659,12 +659,27 @@ } /// getIntPtrType - Return an unsigned integer type that is the same size or -/// greater to the host pointer size. +/// greater to the pointer size for the address space. IntegerType *DataLayout::getIntPtrType(LLVMContext &C, unsigned AddressSpace) const { return IntegerType::get(C, getPointerSizeInBits(AddressSpace)); } +/// getIntPtrType - Return an unsigned integer type that is the same size or +/// greater to the pointer size of the specific PointerType. +IntegerType *DataLayout::getIntPtrType(Type *Ty) const { + LLVMContext &C = Ty->getContext(); + // For pointers, we return the size for the specific address space. + if (Ty->isPointerTy()) return IntegerType::get(C, getTypeSizeInBits(Ty)); + // For vector of pointers, we return the size of the address space + // of the pointer type. + if (Ty->isVectorTy() && cast(Ty)->getElementType()->isPointerTy()) + return IntegerType::get(C, + getTypeSizeInBits(cast(Ty)->getElementType())); + // Otherwise return the address space for the default address space. + return getIntPtrType(C, 0); +} + uint64_t DataLayout::getIndexedOffset(Type *ptrTy, ArrayRef Indices) const { Index: lib/VMCore/Type.cpp =================================================================== --- lib/VMCore/Type.cpp (revision 166296) +++ lib/VMCore/Type.cpp (working copy) @@ -233,7 +233,12 @@ } unsigned Type::getPointerAddressSpace() const { + if (isPointerTy()) return cast(this)->getAddressSpace(); + if (isVectorTy()) + return getSequentialElementType()->getPointerAddressSpace(); + llvm_unreachable("Should never reach here!"); + return 0; } Index: lib/CodeGen/IntrinsicLowering.cpp =================================================================== --- lib/CodeGen/IntrinsicLowering.cpp (revision 166296) +++ lib/CodeGen/IntrinsicLowering.cpp (working copy) @@ -115,21 +115,21 @@ Type::getInt8PtrTy(Context), Type::getInt8PtrTy(Context), Type::getInt8PtrTy(Context), - TD.getIntPtrType(Context), (Type *)0); + TD.getIntPtrType(Context, 0), (Type *)0); break; case Intrinsic::memmove: M.getOrInsertFunction("memmove", Type::getInt8PtrTy(Context), Type::getInt8PtrTy(Context), Type::getInt8PtrTy(Context), - TD.getIntPtrType(Context), (Type *)0); + TD.getIntPtrType(Context, 0), (Type *)0); break; case Intrinsic::memset: M.getOrInsertFunction("memset", Type::getInt8PtrTy(Context), Type::getInt8PtrTy(Context), Type::getInt32Ty(M.getContext()), - TD.getIntPtrType(Context), (Type *)0); + TD.getIntPtrType(Context, 0), (Type *)0); break; case Intrinsic::sqrt: EnsureFPIntrinsicsExist(M, I, "sqrtf", "sqrt", "sqrtl"); @@ -457,7 +457,7 @@ break; // Strip out annotate intrinsic case Intrinsic::memcpy: { - IntegerType *IntPtr = TD.getIntPtrType(Context); + IntegerType *IntPtr = TD.getIntPtrType(CI->getArgOperand(0)->getType()); Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr, /* isSigned */ false); Value *Ops[3]; @@ -468,7 +468,7 @@ break; } case Intrinsic::memmove: { - IntegerType *IntPtr = TD.getIntPtrType(Context); + IntegerType *IntPtr = TD.getIntPtrType(CI->getArgOperand(0)->getType()); Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr, /* isSigned */ false); Value *Ops[3]; @@ -479,7 +479,7 @@ break; } case Intrinsic::memset: { - IntegerType *IntPtr = TD.getIntPtrType(Context); + IntegerType *IntPtr = TD.getIntPtrType(CI->getArgOperand(0)->getType()); Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr, /* isSigned */ false); Value *Ops[3]; Index: lib/CodeGen/AsmPrinter/AsmPrinter.cpp =================================================================== --- lib/CodeGen/AsmPrinter/AsmPrinter.cpp (revision 166296) +++ lib/CodeGen/AsmPrinter/AsmPrinter.cpp (working copy) @@ -1505,7 +1505,7 @@ // Handle casts to pointers by changing them into casts to the appropriate // integer type. This promotes constant folding and simplifies this code. Constant *Op = CE->getOperand(0); - Op = ConstantExpr::getIntegerCast(Op, TD.getIntPtrType(CV->getContext()), + Op = ConstantExpr::getIntegerCast(Op, TD.getIntPtrType(CE->getType()), false/*ZExt*/); return lowerConstant(Op, AP); } Index: lib/CodeGen/SelectionDAG/SelectionDAG.cpp =================================================================== --- lib/CodeGen/SelectionDAG/SelectionDAG.cpp (revision 166296) +++ lib/CodeGen/SelectionDAG/SelectionDAG.cpp (working copy) @@ -3804,7 +3804,8 @@ // Emit a library call. TargetLowering::ArgListTy Args; TargetLowering::ArgListEntry Entry; - Entry.Ty = TLI.getDataLayout()->getIntPtrType(*getContext()); + unsigned AS = SrcPtrInfo.getAddrSpace(); + Entry.Ty = TLI.getDataLayout()->getIntPtrType(*getContext(), AS); Entry.Node = Dst; Args.push_back(Entry); Entry.Node = Src; Args.push_back(Entry); Entry.Node = Size; Args.push_back(Entry); @@ -3859,7 +3860,8 @@ // Emit a library call. TargetLowering::ArgListTy Args; TargetLowering::ArgListEntry Entry; - Entry.Ty = TLI.getDataLayout()->getIntPtrType(*getContext()); + unsigned AS = SrcPtrInfo.getAddrSpace(); + Entry.Ty = TLI.getDataLayout()->getIntPtrType(*getContext(), AS); Entry.Node = Dst; Args.push_back(Entry); Entry.Node = Src; Args.push_back(Entry); Entry.Node = Size; Args.push_back(Entry); @@ -3908,7 +3910,8 @@ return Result; // Emit a library call. - Type *IntPtrTy = TLI.getDataLayout()->getIntPtrType(*getContext()); + unsigned AS = DstPtrInfo.getAddrSpace(); + Type *IntPtrTy = TLI.getDataLayout()->getIntPtrType(*getContext(), AS); TargetLowering::ArgListTy Args; TargetLowering::ArgListEntry Entry; Entry.Node = Dst; Entry.Ty = IntPtrTy; Index: lib/CodeGen/SelectionDAG/FastISel.cpp =================================================================== --- lib/CodeGen/SelectionDAG/FastISel.cpp (revision 166296) +++ lib/CodeGen/SelectionDAG/FastISel.cpp (working copy) @@ -100,10 +100,11 @@ return false; // No-op casts are trivially coalesced by fast-isel. - if (const CastInst *Cast = dyn_cast(I)) - if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) && + if (const CastInst *Cast = dyn_cast(I)) { + if (Cast->isNoopCast(TD) && !hasTrivialKill(Cast->getOperand(0))) return false; + } // GEPs with all zero indices are trivially coalesced by fast-isel. if (const GetElementPtrInst *GEP = dyn_cast(I)) @@ -175,7 +176,8 @@ // Translate this as an integer zero so that it can be // local-CSE'd with actual integer zeros. Reg = - getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext()))); + getRegForValue(Constant::getNullValue( + TD.getIntPtrType(V->getType()))); } else if (const ConstantFP *CF = dyn_cast(V)) { if (CF->isNullValue()) { Reg = TargetMaterializeFloatZero(CF); Index: lib/Transforms/IPO/MergeFunctions.cpp =================================================================== --- lib/Transforms/IPO/MergeFunctions.cpp (revision 166296) +++ lib/Transforms/IPO/MergeFunctions.cpp (working copy) @@ -207,8 +207,8 @@ if (Ty1->getTypeID() != Ty2->getTypeID()) { if (TD) { LLVMContext &Ctx = Ty1->getContext(); - if (isa(Ty1) && Ty2 == TD->getIntPtrType(Ctx)) return true; - if (isa(Ty2) && Ty1 == TD->getIntPtrType(Ctx)) return true; + if (isa(Ty1) && Ty2 == TD->getIntPtrType(Ty1)) return true; + if (isa(Ty2) && Ty1 == TD->getIntPtrType(Ty2)) return true; } return false; } Index: lib/Transforms/IPO/GlobalOpt.cpp =================================================================== --- lib/Transforms/IPO/GlobalOpt.cpp (revision 166296) +++ lib/Transforms/IPO/GlobalOpt.cpp (working copy) @@ -1500,7 +1500,7 @@ unsigned TypeSize = TD->getTypeAllocSize(FieldTy); if (StructType *ST = dyn_cast(FieldTy)) TypeSize = TD->getStructLayout(ST)->getSizeInBytes(); - Type *IntPtrTy = TD->getIntPtrType(CI->getContext()); + Type *IntPtrTy = TD->getIntPtrType(GV->getType()); Value *NMI = CallInst::CreateMalloc(CI, IntPtrTy, FieldTy, ConstantInt::get(IntPtrTy, TypeSize), NElems, 0, @@ -1730,7 +1730,7 @@ // If this is a fixed size array, transform the Malloc to be an alloc of // structs. malloc [100 x struct],1 -> malloc struct, 100 if (ArrayType *AT = dyn_cast(getMallocAllocatedType(CI, TLI))) { - Type *IntPtrTy = TD->getIntPtrType(CI->getContext()); + Type *IntPtrTy = TD->getIntPtrType(GV->getType()); unsigned TypeSize = TD->getStructLayout(AllocSTy)->getSizeInBytes(); Value *AllocSize = ConstantInt::get(IntPtrTy, TypeSize); Value *NumElements = ConstantInt::get(IntPtrTy, AT->getNumElements()); Index: lib/Transforms/Utils/SimplifyLibCalls.cpp =================================================================== --- lib/Transforms/Utils/SimplifyLibCalls.cpp (revision 166296) +++ lib/Transforms/Utils/SimplifyLibCalls.cpp (working copy) @@ -107,9 +107,13 @@ // Check if this has the right signature. if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || !FT->getParamType(0)->isPointerTy() || - !FT->getParamType(1)->isPointerTy() || - FT->getParamType(2) != TD->getIntPtrType(Context) || - FT->getParamType(3) != TD->getIntPtrType(Context)) + !FT->getParamType(1)->isPointerTy()) + return 0; + + Type *PT0 = FT->getParamType(0); + Type *PT1 = FT->getParamType(1); + if (FT->getParamType(2) != TD->getIntPtrType(PT0) || + FT->getParamType(3) != TD->getIntPtrType(PT1)) return 0; if (isFoldable(3, 2, false)) { @@ -130,9 +134,13 @@ // Check if this has the right signature. if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || !FT->getParamType(0)->isPointerTy() || - !FT->getParamType(1)->isPointerTy() || - FT->getParamType(2) != TD->getIntPtrType(Context) || - FT->getParamType(3) != TD->getIntPtrType(Context)) + !FT->getParamType(1)->isPointerTy()) + return 0; + + Type *PT0 = FT->getParamType(0); + Type *PT1 = FT->getParamType(1); + if (FT->getParamType(2) != TD->getIntPtrType(PT0) || + FT->getParamType(3) != TD->getIntPtrType(PT1)) return 0; if (isFoldable(3, 2, false)) { @@ -153,9 +161,12 @@ // Check if this has the right signature. if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || !FT->getParamType(0)->isPointerTy() || - !FT->getParamType(1)->isIntegerTy() || - FT->getParamType(2) != TD->getIntPtrType(Context) || - FT->getParamType(3) != TD->getIntPtrType(Context)) + !FT->getParamType(1)->isIntegerTy()) + return 0; + + Type *PT = FT->getParamType(0); + if (FT->getParamType(2) != TD->getIntPtrType(PT) || + FT->getParamType(3) != TD->getIntPtrType(PT)) return 0; if (isFoldable(3, 2, false)) { @@ -178,9 +189,14 @@ // Check if this has the right signature. if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) || - FT->getParamType(0) != FT->getParamType(1) || - FT->getParamType(0) != Type::getInt8PtrTy(Context) || - FT->getParamType(2) != TD->getIntPtrType(Context)) + FT->getParamType(0) != FT->getParamType(1)) + return 0; + + assert(FT->getParamType(0)->isPointerTy() && + "Must be a pointer type."); + Type *PT = FT->getParamType(0); + if (FT->getParamType(0) != Type::getInt8PtrTy(Context) || + FT->getParamType(2) != TD->getIntPtrType(PT)) return 0; Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1); @@ -205,8 +221,8 @@ Value *Ret = EmitMemCpyChk(Dst, Src, - ConstantInt::get(TD->getIntPtrType(Context), Len), - CI->getArgOperand(2), B, TD, TLI); + ConstantInt::get(TD->getIntPtrType(Dst->getType()), + Len), CI->getArgOperand(2), B, TD, TLI); return Ret; } return 0; @@ -224,8 +240,11 @@ if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || FT->getParamType(0) != FT->getParamType(1) || FT->getParamType(0) != Type::getInt8PtrTy(Context) || - !FT->getParamType(2)->isIntegerTy() || - FT->getParamType(3) != TD->getIntPtrType(Context)) + !FT->getParamType(2)->isIntegerTy()) + return 0; + + Type *PT = FT->getParamType(0); + if (FT->getParamType(3) != TD->getIntPtrType(PT)) return 0; if (isFoldable(3, 2, false)) { @@ -287,7 +306,8 @@ // We have enough information to now generate the memcpy call to do the // concatenation for us. Make a memcpy to copy the nul byte with align = 1. B.CreateMemCpy(CpyDst, Src, - ConstantInt::get(TD->getIntPtrType(*Context), Len + 1), 1); + ConstantInt::get(TD->getIntPtrType(Src->getType()), + Len + 1), 1); return Dst; } }; @@ -359,8 +379,9 @@ if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32. return 0; + Type *PT = FT->getParamType(0); return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul. - ConstantInt::get(TD->getIntPtrType(*Context), Len), + ConstantInt::get(TD->getIntPtrType(PT), Len), B, TD, TLI); } @@ -454,8 +475,9 @@ // These optimizations require DataLayout. if (!TD) return 0; + Type *PT = FT->getParamType(0); return EmitMemCmp(Str1P, Str2P, - ConstantInt::get(TD->getIntPtrType(*Context), + ConstantInt::get(TD->getIntPtrType(PT), std::min(Len1, Len2)), B, TD, TLI); } @@ -537,7 +559,7 @@ // We have enough information to now generate the memcpy call to do the // copy for us. Make a memcpy to copy the nul byte with align = 1. B.CreateMemCpy(Dst, Src, - ConstantInt::get(TD->getIntPtrType(*Context), Len), 1); + ConstantInt::get(TD->getIntPtrType(Dst->getType()), Len), 1); return Dst; } }; Index: lib/Transforms/Utils/BuildLibCalls.cpp =================================================================== --- lib/Transforms/Utils/BuildLibCalls.cpp (revision 166296) +++ lib/Transforms/Utils/BuildLibCalls.cpp (working copy) @@ -48,7 +48,7 @@ LLVMContext &Context = B.GetInsertBlock()->getContext(); Constant *StrLen = M->getOrInsertFunction("strlen", AttrListPtr::get(AWI), - TD->getIntPtrType(Context), + TD->getIntPtrType(Ptr->getType()), B.getInt8PtrTy(), NULL); CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen"); @@ -75,9 +75,9 @@ LLVMContext &Context = B.GetInsertBlock()->getContext(); Constant *StrNLen = M->getOrInsertFunction("strnlen", AttrListPtr::get(AWI), - TD->getIntPtrType(Context), + TD->getIntPtrType(Ptr->getType()), B.getInt8PtrTy(), - TD->getIntPtrType(Context), + TD->getIntPtrType(Ptr->getType()), NULL); CallInst *CI = B.CreateCall2(StrNLen, CastToCStr(Ptr, B), MaxLen, "strnlen"); if (const Function *F = dyn_cast(StrNLen->stripPointerCasts())) @@ -131,7 +131,8 @@ B.getInt32Ty(), B.getInt8PtrTy(), B.getInt8PtrTy(), - TD->getIntPtrType(Context), NULL); + TD->getIntPtrType(Ptr1->getType()), + NULL); CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len, "strncmp"); @@ -207,8 +208,9 @@ B.getInt8PtrTy(), B.getInt8PtrTy(), B.getInt8PtrTy(), - TD->getIntPtrType(Context), - TD->getIntPtrType(Context), NULL); + TD->getIntPtrType(Dst->getType()), + TD->getIntPtrType(Src->getType()), + NULL); Dst = CastToCStr(Dst, B); Src = CastToCStr(Src, B); CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize); @@ -235,7 +237,7 @@ B.getInt8PtrTy(), B.getInt8PtrTy(), B.getInt32Ty(), - TD->getIntPtrType(Context), + TD->getIntPtrType(Ptr->getType()), NULL); CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr"); @@ -265,7 +267,8 @@ B.getInt32Ty(), B.getInt8PtrTy(), B.getInt8PtrTy(), - TD->getIntPtrType(Context), NULL); + TD->getIntPtrType(Ptr1->getType()), + NULL); CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len, "memcmp"); @@ -428,21 +431,22 @@ LLVMContext &Context = B.GetInsertBlock()->getContext(); StringRef FWriteName = TLI->getName(LibFunc::fwrite); Constant *F; + Type *PtrTy = Ptr->getType(); if (File->getType()->isPointerTy()) F = M->getOrInsertFunction(FWriteName, AttrListPtr::get(AWI), - TD->getIntPtrType(Context), + TD->getIntPtrType(PtrTy), B.getInt8PtrTy(), - TD->getIntPtrType(Context), - TD->getIntPtrType(Context), + TD->getIntPtrType(PtrTy), + TD->getIntPtrType(PtrTy), File->getType(), NULL); else - F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context), + F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(PtrTy), B.getInt8PtrTy(), - TD->getIntPtrType(Context), - TD->getIntPtrType(Context), + TD->getIntPtrType(PtrTy), + TD->getIntPtrType(PtrTy), File->getType(), NULL); CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size, - ConstantInt::get(TD->getIntPtrType(Context), 1), File); + ConstantInt::get(TD->getIntPtrType(PtrTy), 1), File); if (const Function *Fn = dyn_cast(F->stripPointerCasts())) CI->setCallingConv(Fn->getCallingConv()); @@ -464,12 +468,13 @@ IRBuilder<> B(CI); if (Name == "__memcpy_chk") { + Type *PT = FT->getParamType(0); // Check if this has the right signature. if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || !FT->getParamType(0)->isPointerTy() || !FT->getParamType(1)->isPointerTy() || - FT->getParamType(2) != TD->getIntPtrType(Context) || - FT->getParamType(3) != TD->getIntPtrType(Context)) + FT->getParamType(2) != TD->getIntPtrType(PT) || + FT->getParamType(3) != TD->getIntPtrType(PT)) return false; if (isFoldable(3, 2, false)) { @@ -488,11 +493,12 @@ if (Name == "__memmove_chk") { // Check if this has the right signature. + Type *PT = FT->getParamType(0); if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || !FT->getParamType(0)->isPointerTy() || !FT->getParamType(1)->isPointerTy() || - FT->getParamType(2) != TD->getIntPtrType(Context) || - FT->getParamType(3) != TD->getIntPtrType(Context)) + FT->getParamType(2) != TD->getIntPtrType(PT) || + FT->getParamType(3) != TD->getIntPtrType(PT)) return false; if (isFoldable(3, 2, false)) { @@ -506,11 +512,12 @@ if (Name == "__memset_chk") { // Check if this has the right signature. + Type *PT = FT->getParamType(0); if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || !FT->getParamType(0)->isPointerTy() || !FT->getParamType(1)->isIntegerTy() || - FT->getParamType(2) != TD->getIntPtrType(Context) || - FT->getParamType(3) != TD->getIntPtrType(Context)) + FT->getParamType(2) != TD->getIntPtrType(PT) || + FT->getParamType(3) != TD->getIntPtrType(PT)) return false; if (isFoldable(3, 2, false)) { @@ -525,11 +532,12 @@ if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") { // Check if this has the right signature. + Type *PT = FT->getParamType(0); if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) || FT->getParamType(0) != FT->getParamType(1) || FT->getParamType(0) != Type::getInt8PtrTy(Context) || - FT->getParamType(2) != TD->getIntPtrType(Context)) + FT->getParamType(2) != TD->getIntPtrType(PT)) return 0; @@ -551,11 +559,12 @@ if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") { // Check if this has the right signature. + Type *PT = FT->getParamType(0); if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || FT->getParamType(0) != FT->getParamType(1) || FT->getParamType(0) != Type::getInt8PtrTy(Context) || !FT->getParamType(2)->isIntegerTy() || - FT->getParamType(3) != TD->getIntPtrType(Context)) + FT->getParamType(3) != TD->getIntPtrType(PT)) return false; if (isFoldable(3, 2, false)) { Index: lib/Transforms/Utils/SimplifyCFG.cpp =================================================================== --- lib/Transforms/Utils/SimplifyCFG.cpp (revision 166296) +++ lib/Transforms/Utils/SimplifyCFG.cpp (working copy) @@ -392,7 +392,7 @@ // This is some kind of pointer constant. Turn it into a pointer-sized // ConstantInt if possible. - IntegerType *PtrTy = TD->getIntPtrType(V->getContext()); + IntegerType *PtrTy = TD->getIntPtrType(V->getType()); // Null pointer means 0, see SelectionDAGBuilder::getValue(const Value*). if (isa(V)) @@ -532,9 +532,13 @@ CV = ICI->getOperand(0); // Unwrap any lossless ptrtoint cast. - if (TD && CV && CV->getType() == TD->getIntPtrType(CV->getContext())) - if (PtrToIntInst *PTII = dyn_cast(CV)) + if (TD && CV) { + PtrToIntInst *PTII = NULL; + if ((PTII = dyn_cast(CV)) && + CV->getType() == TD->getIntPtrType(CV->getContext(), + PTII->getPointerAddressSpace())) CV = PTII->getOperand(0); + } return CV; } @@ -981,7 +985,7 @@ // Convert pointer to int before we switch. if (CV->getType()->isPointerTy()) { assert(TD && "Cannot switch on pointer without DataLayout"); - CV = Builder.CreatePtrToInt(CV, TD->getIntPtrType(CV->getContext()), + CV = Builder.CreatePtrToInt(CV, TD->getIntPtrType(CV->getType()), "magicptr"); } @@ -2709,7 +2713,7 @@ if (CompVal->getType()->isPointerTy()) { assert(TD && "Cannot switch on pointer without DataLayout"); CompVal = Builder.CreatePtrToInt(CompVal, - TD->getIntPtrType(CompVal->getContext()), + TD->getIntPtrType(CompVal->getType()), "magicptr"); } Index: lib/Transforms/Instrumentation/BoundsChecking.cpp =================================================================== --- lib/Transforms/Instrumentation/BoundsChecking.cpp (revision 166296) +++ lib/Transforms/Instrumentation/BoundsChecking.cpp (working copy) @@ -143,7 +143,7 @@ Value *Offset = SizeOffset.second; ConstantInt *SizeCI = dyn_cast(Size); - IntegerType *IntTy = TD->getIntPtrType(Inst->getContext()); + IntegerType *IntTy = TD->getIntPtrType(Ptr->getType()); Value *NeededSizeVal = ConstantInt::get(IntTy, NeededSize); // three checks are required to ensure safety: Index: lib/Transforms/Scalar/IndVarSimplify.cpp =================================================================== --- lib/Transforms/Scalar/IndVarSimplify.cpp (revision 166296) +++ lib/Transforms/Scalar/IndVarSimplify.cpp (working copy) @@ -1430,7 +1430,8 @@ /// genLoopLimit - Help LinearFunctionTestReplace by generating a value that /// holds the RHS of the new loop test. static Value *genLoopLimit(PHINode *IndVar, const SCEV *IVCount, Loop *L, - SCEVExpander &Rewriter, ScalarEvolution *SE) { + SCEVExpander &Rewriter, ScalarEvolution *SE, + Type *IntPtrTy) { const SCEVAddRecExpr *AR = dyn_cast(SE->getSCEV(IndVar)); assert(AR && AR->getLoop() == L && AR->isAffine() && "bad loop counter"); const SCEV *IVInit = AR->getStart(); @@ -1456,7 +1457,8 @@ // We could handle pointer IVs other than i8*, but we need to compensate for // gep index scaling. See canExpandBackedgeTakenCount comments. assert(SE->getSizeOfExpr( - cast(GEPBase->getType())->getElementType())->isOne() + cast(GEPBase->getType())->getElementType(), + IntPtrTy)->isOne() && "unit stride pointer IV must be i8*"); IRBuilder<> Builder(L->getLoopPreheader()->getTerminator()); @@ -1555,7 +1557,9 @@ CmpIndVar = IndVar; } - Value *ExitCnt = genLoopLimit(IndVar, IVCount, L, Rewriter, SE); + Type *IntPtrTy = TD ? TD->getIntPtrType(IndVar->getType()) : + IntegerType::getInt64Ty(IndVar->getContext()); + Value *ExitCnt = genLoopLimit(IndVar, IVCount, L, Rewriter, SE, IntPtrTy); assert(ExitCnt->getType()->isPointerTy() == IndVar->getType()->isPointerTy() && "genLoopLimit missed a cast"); Index: lib/Transforms/Scalar/ScalarReplAggregates.cpp =================================================================== --- lib/Transforms/Scalar/ScalarReplAggregates.cpp (revision 166296) +++ lib/Transforms/Scalar/ScalarReplAggregates.cpp (working copy) @@ -963,7 +963,7 @@ if (SV->getType()->isFloatingPointTy() || SV->getType()->isVectorTy()) SV = Builder.CreateBitCast(SV, IntegerType::get(SV->getContext(),SrcWidth)); else if (SV->getType()->isPointerTy()) - SV = Builder.CreatePtrToInt(SV, TD.getIntPtrType(SV->getContext())); + SV = Builder.CreatePtrToInt(SV, TD.getIntPtrType(SV->getType())); // Zero extend or truncate the value if needed. if (SV->getType() != AllocaType) { Index: lib/Transforms/Scalar/CodeGenPrepare.cpp =================================================================== --- lib/Transforms/Scalar/CodeGenPrepare.cpp (revision 166296) +++ lib/Transforms/Scalar/CodeGenPrepare.cpp (working copy) @@ -934,7 +934,7 @@ DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for " << *MemoryInst); Type *IntPtrTy = - TLI->getDataLayout()->getIntPtrType(AccessTy->getContext()); + TLI->getDataLayout()->getIntPtrType(Addr->getType()); Value *Result = 0; Index: lib/Transforms/Scalar/LoopIdiomRecognize.cpp =================================================================== --- lib/Transforms/Scalar/LoopIdiomRecognize.cpp (revision 166296) +++ lib/Transforms/Scalar/LoopIdiomRecognize.cpp (working copy) @@ -486,7 +486,9 @@ // would be unsafe to do if there is anything else in the loop that may read // or write to the aliased location. Check for any overlap by generating the // base pointer and checking the region. - unsigned AddrSpace = cast(DestPtr->getType())->getAddressSpace(); + assert(DestPtr->getType()->isPointerTy() + && "Must be a pointer type."); + unsigned AddrSpace = DestPtr->getType()->getPointerAddressSpace(); Value *BasePtr = Expander.expandCodeFor(Ev->getStart(), Builder.getInt8PtrTy(AddrSpace), Preheader->getTerminator()); @@ -505,7 +507,7 @@ // The # stored bytes is (BECount+1)*Size. Expand the trip count out to // pointer size if it isn't already. - Type *IntPtr = TD->getIntPtrType(DestPtr->getContext()); + Type *IntPtr = TD->getIntPtrType(DestPtr->getType()); BECount = SE->getTruncateOrZeroExtend(BECount, IntPtr); const SCEV *NumBytesS = SE->getAddExpr(BECount, SE->getConstant(IntPtr, 1), @@ -611,7 +613,7 @@ // The # stored bytes is (BECount+1)*Size. Expand the trip count out to // pointer size if it isn't already. - Type *IntPtr = TD->getIntPtrType(SI->getContext()); + Type *IntPtr = TD->getIntPtrType(SI->getType()); BECount = SE->getTruncateOrZeroExtend(BECount, IntPtr); const SCEV *NumBytesS = SE->getAddExpr(BECount, SE->getConstant(IntPtr, 1), Index: lib/Transforms/Scalar/GVN.cpp =================================================================== --- lib/Transforms/Scalar/GVN.cpp (revision 166296) +++ lib/Transforms/Scalar/GVN.cpp (working copy) @@ -774,13 +774,13 @@ // Convert source pointers to integers, which can be bitcast. if (StoredValTy->isPointerTy()) { - StoredValTy = TD.getIntPtrType(StoredValTy->getContext()); + StoredValTy = TD.getIntPtrType(StoredValTy); StoredVal = new PtrToIntInst(StoredVal, StoredValTy, "", InsertPt); } Type *TypeToCastTo = LoadedTy; if (TypeToCastTo->isPointerTy()) - TypeToCastTo = TD.getIntPtrType(StoredValTy->getContext()); + TypeToCastTo = TD.getIntPtrType(StoredValTy); if (StoredValTy != TypeToCastTo) StoredVal = new BitCastInst(StoredVal, TypeToCastTo, "", InsertPt); @@ -799,7 +799,7 @@ // Convert source pointers to integers, which can be manipulated. if (StoredValTy->isPointerTy()) { - StoredValTy = TD.getIntPtrType(StoredValTy->getContext()); + StoredValTy = TD.getIntPtrType(StoredValTy); StoredVal = new PtrToIntInst(StoredVal, StoredValTy, "", InsertPt); } @@ -1020,7 +1020,8 @@ // Compute which bits of the stored value are being used by the load. Convert // to an integer type to start with. if (SrcVal->getType()->isPointerTy()) - SrcVal = Builder.CreatePtrToInt(SrcVal, TD.getIntPtrType(Ctx)); + SrcVal = Builder.CreatePtrToInt(SrcVal, + TD.getIntPtrType(SrcVal->getType())); if (!SrcVal->getType()->isIntegerTy()) SrcVal = Builder.CreateBitCast(SrcVal, IntegerType::get(Ctx, StoreSize*8)); Index: lib/Transforms/Scalar/SimplifyLibCalls.cpp =================================================================== --- lib/Transforms/Scalar/SimplifyLibCalls.cpp (revision 166296) +++ lib/Transforms/Scalar/SimplifyLibCalls.cpp (working copy) @@ -135,7 +135,7 @@ namespace { //===---------------------------------------===// -// 'stpcpy' Optimizations +// 'strcpy' Optimizations struct StpCpyOpt: public LibCallOptimization { bool OptChkCall; // True if it's optimizing a __stpcpy_chk libcall. @@ -165,9 +165,10 @@ uint64_t Len = GetStringLength(Src); if (Len == 0) return 0; - Value *LenV = ConstantInt::get(TD->getIntPtrType(*Context), Len); + Type *PT = FT->getParamType(0); + Value *LenV = ConstantInt::get(TD->getIntPtrType(PT), Len); Value *DstEnd = B.CreateGEP(Dst, - ConstantInt::get(TD->getIntPtrType(*Context), + ConstantInt::get(TD->getIntPtrType(PT), Len - 1)); // We have enough information to now generate the memcpy call to do the @@ -220,9 +221,10 @@ // Let strncpy handle the zero padding if (Len > SrcLen+1) return 0; + Type *PT = FT->getParamType(0); // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant] B.CreateMemCpy(Dst, Src, - ConstantInt::get(TD->getIntPtrType(*Context), Len), 1); + ConstantInt::get(TD->getIntPtrType(PT), Len), 1); return Dst; } @@ -508,10 +510,11 @@ if (!TD) return 0; FunctionType *FT = Callee->getFunctionType(); + Type *PT = FT->getParamType(0); if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) || !FT->getParamType(0)->isPointerTy() || !FT->getParamType(1)->isPointerTy() || - FT->getParamType(2) != TD->getIntPtrType(*Context)) + FT->getParamType(2) != TD->getIntPtrType(PT)) return 0; // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1) @@ -530,10 +533,11 @@ if (!TD) return 0; FunctionType *FT = Callee->getFunctionType(); + Type *PT = FT->getParamType(0); if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) || !FT->getParamType(0)->isPointerTy() || !FT->getParamType(1)->isPointerTy() || - FT->getParamType(2) != TD->getIntPtrType(*Context)) + FT->getParamType(2) != TD->getIntPtrType(PT)) return 0; // memmove(x, y, n) -> llvm.memmove(x, y, n, 1) @@ -552,10 +556,11 @@ if (!TD) return 0; FunctionType *FT = Callee->getFunctionType(); + Type *PT = FT->getParamType(0); if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) || !FT->getParamType(0)->isPointerTy() || !FT->getParamType(1)->isIntegerTy() || - FT->getParamType(2) != TD->getIntPtrType(*Context)) + FT->getParamType(2) != TD->getIntPtrType(PT)) return 0; // memset(p, v, n) -> llvm.memset(p, v, n, 1) @@ -980,8 +985,9 @@ if (!TD) return 0; // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1) + Type *AT = CI->getArgOperand(0)->getType(); B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1), - ConstantInt::get(TD->getIntPtrType(*Context), // Copy the + ConstantInt::get(TD->getIntPtrType(AT), // Copy the FormatStr.size() + 1), 1); // nul byte. return ConstantInt::get(CI->getType(), FormatStr.size()); } @@ -1108,8 +1114,9 @@ uint64_t Len = GetStringLength(CI->getArgOperand(0)); if (!Len) return 0; // Known to have no uses (see above). + Type *PT = FT->getParamType(0); return EmitFWrite(CI->getArgOperand(0), - ConstantInt::get(TD->getIntPtrType(*Context), Len-1), + ConstantInt::get(TD->getIntPtrType(PT), Len-1), CI->getArgOperand(1), B, TD, TLI); } }; @@ -1134,8 +1141,9 @@ // These optimizations require DataLayout. if (!TD) return 0; + Type *AT = CI->getArgOperand(1)->getType(); Value *NewCI = EmitFWrite(CI->getArgOperand(1), - ConstantInt::get(TD->getIntPtrType(*Context), + ConstantInt::get(TD->getIntPtrType(AT), FormatStr.size()), CI->getArgOperand(0), B, TD, TLI); return NewCI ? ConstantInt::get(CI->getType(), FormatStr.size()) : 0; Index: lib/Transforms/InstCombine/InstCombineCalls.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCalls.cpp (revision 166296) +++ lib/Transforms/InstCombine/InstCombineCalls.cpp (working copy) @@ -996,9 +996,9 @@ // Conversion is ok if changing from one pointer type to another or from // a pointer to an integer of the same size. !((OldRetTy->isPointerTy() || !TD || - OldRetTy == TD->getIntPtrType(Caller->getContext())) && + OldRetTy == TD->getIntPtrType(NewRetTy)) && (NewRetTy->isPointerTy() || !TD || - NewRetTy == TD->getIntPtrType(Caller->getContext())))) + NewRetTy == TD->getIntPtrType(OldRetTy)))) return false; // Cannot transform this return value. if (!Caller->use_empty() && @@ -1057,11 +1057,13 @@ // Converting from one pointer type to another or between a pointer and an // integer of the same size is safe even if we do not have a body. + // FIXME: Not sure what to do here, so setting AS to 0. + // How can the AS for a function call be outside the default? bool isConvertible = ActTy == ParamTy || (TD && ((ParamTy->isPointerTy() || - ParamTy == TD->getIntPtrType(Caller->getContext())) && + ParamTy == TD->getIntPtrType(ActTy)) && (ActTy->isPointerTy() || - ActTy == TD->getIntPtrType(Caller->getContext())))); + ActTy == TD->getIntPtrType(ParamTy)))); if (Callee->isDeclaration() && !isConvertible) return false; } Index: lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCompares.cpp (revision 166296) +++ lib/Transforms/InstCombine/InstCombineCompares.cpp (working copy) @@ -371,7 +371,7 @@ // an inbounds GEP because the index can't be out of range. if (!GEP->isInBounds() && Idx->getType()->getPrimitiveSizeInBits() > TD->getPointerSizeInBits(AS)) - Idx = Builder->CreateTrunc(Idx, TD->getIntPtrType(Idx->getContext())); + Idx = Builder->CreateTrunc(Idx, TD->getIntPtrType(Idx->getContext(), AS)); // If the comparison is only true for one or two elements, emit direct // comparisons. @@ -539,7 +539,7 @@ // we don't need to bother extending: the extension won't affect where the // computation crosses zero. if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) { - Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext()); + Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext(), AS); VariableIdx = IC.Builder->CreateTrunc(VariableIdx, IntPtrTy); } return VariableIdx; @@ -561,7 +561,7 @@ return 0; // Okay, we can do this evaluation. Start by converting the index to intptr. - Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext()); + Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext(), AS); if (VariableIdx->getType() != IntPtrTy) VariableIdx = IC.Builder->CreateIntCast(VariableIdx, IntPtrTy, true /*Signed*/); @@ -2251,7 +2251,7 @@ case Instruction::IntToPtr: // icmp pred inttoptr(X), null -> icmp pred X, 0 if (RHSC->isNullValue() && TD && - TD->getIntPtrType(RHSC->getContext()) == + TD->getIntPtrType(LHSI->getType()) == LHSI->getOperand(0)->getType()) return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), Constant::getNullValue(LHSI->getOperand(0)->getType())); Index: lib/Transforms/InstCombine/InstCombineCasts.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCasts.cpp (revision 166296) +++ lib/Transforms/InstCombine/InstCombineCasts.cpp (working copy) @@ -238,17 +238,16 @@ // Get the opcodes of the two Cast instructions Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode()); Instruction::CastOps secondOp = Instruction::CastOps(opcode); - unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy, - TD ? TD->getIntPtrType(CI->getContext()) : 0); + TD ? TD->getIntPtrType(DstTy) : 0); // We don't want to form an inttoptr or ptrtoint that converts to an integer // type that differs from the pointer size. if ((Res == Instruction::IntToPtr && - (!TD || SrcTy != TD->getIntPtrType(CI->getContext()))) || + (!TD || SrcTy != TD->getIntPtrType(DstTy))) || (Res == Instruction::PtrToInt && - (!TD || DstTy != TD->getIntPtrType(CI->getContext())))) + (!TD || DstTy != TD->getIntPtrType(SrcTy)))) Res = 0; return Instruction::CastOps(Res); @@ -1298,13 +1297,13 @@ if (CI.getOperand(0)->getType()->getScalarSizeInBits() > TD->getPointerSizeInBits(AS)) { Value *P = Builder->CreateTrunc(CI.getOperand(0), - TD->getIntPtrType(CI.getContext())); + TD->getIntPtrType(CI.getType())); return new IntToPtrInst(P, CI.getType()); } if (CI.getOperand(0)->getType()->getScalarSizeInBits() < TD->getPointerSizeInBits(AS)) { Value *P = Builder->CreateZExt(CI.getOperand(0), - TD->getIntPtrType(CI.getContext())); + TD->getIntPtrType(CI.getType())); return new IntToPtrInst(P, CI.getType()); } } @@ -1345,7 +1344,8 @@ Type *GEPIdxTy = cast(OrigBase->getType())->getElementType(); SmallVector NewIndices; - if (FindElementAtOffset(GEPIdxTy, Offset, NewIndices)) { + Type *IntPtrTy = TD->getIntPtrType(OrigBase->getType()); + if (FindElementAtOffset(GEPIdxTy, Offset, IntPtrTy, NewIndices)) { // If we were able to index down into an element, create the GEP // and bitcast the result. This eliminates one bitcast, potentially // two. @@ -1373,12 +1373,12 @@ if (TD) { if (CI.getType()->getScalarSizeInBits() < TD->getPointerSizeInBits(AS)) { Value *P = Builder->CreatePtrToInt(CI.getOperand(0), - TD->getIntPtrType(CI.getContext())); + TD->getIntPtrType(CI.getContext(), AS)); return new TruncInst(P, CI.getType()); } if (CI.getType()->getScalarSizeInBits() > TD->getPointerSizeInBits(AS)) { Value *P = Builder->CreatePtrToInt(CI.getOperand(0), - TD->getIntPtrType(CI.getContext())); + TD->getIntPtrType(CI.getContext(), AS)); return new ZExtInst(P, CI.getType()); } } Index: lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp (revision 166296) +++ lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp (working copy) @@ -173,7 +173,7 @@ // Ensure that the alloca array size argument has type intptr_t, so that // any casting is exposed early. if (TD) { - Type *IntPtrTy = TD->getIntPtrType(AI.getContext()); + Type *IntPtrTy = TD->getIntPtrType(AI.getContext(), 0); if (AI.getArraySize()->getType() != IntPtrTy) { Value *V = Builder->CreateIntCast(AI.getArraySize(), IntPtrTy, false); @@ -513,8 +513,7 @@ // If the pointers point into different address spaces or if they point to // values with different sizes, we can't do the transformation. if (!IC.getDataLayout() || - SrcTy->getAddressSpace() != - cast(CI->getType())->getAddressSpace() || + SrcTy->getAddressSpace() != CI->getType()->getPointerAddressSpace() || IC.getDataLayout()->getTypeSizeInBits(SrcPTy) != IC.getDataLayout()->getTypeSizeInBits(DestPTy)) return 0; Index: lib/Transforms/InstCombine/InstructionCombining.cpp =================================================================== --- lib/Transforms/InstCombine/InstructionCombining.cpp (revision 166296) +++ lib/Transforms/InstCombine/InstructionCombining.cpp (working copy) @@ -738,7 +738,7 @@ /// or not there is a sequence of GEP indices into the type that will land us at /// the specified offset. If so, fill them into NewIndices and return the /// resultant element type, otherwise return null. -Type *InstCombiner::FindElementAtOffset(Type *Ty, int64_t Offset, +Type *InstCombiner::FindElementAtOffset(Type *Ty, int64_t Offset, Type *IntPtrTy, SmallVectorImpl &NewIndices) { if (!TD) return 0; if (!Ty->isSized()) return 0; @@ -746,7 +746,6 @@ // Start with the index over the outer type. Note that the type size // might be zero (even if the offset isn't zero) if the indexed type // is something like [0 x {int, int}] - Type *IntPtrTy = TD->getIntPtrType(Ty->getContext()); int64_t FirstIdx = 0; if (int64_t TySize = TD->getTypeAllocSize(Ty)) { FirstIdx = Offset/TySize; @@ -817,7 +816,7 @@ // by multiples of a zero size type with zero. if (TD) { bool MadeChange = false; - Type *IntPtrTy = TD->getIntPtrType(GEP.getContext()); + Type *IntPtrTy = TD->getIntPtrType(PtrOp->getType()); gep_type_iterator GTI = gep_type_begin(GEP); for (User::op_iterator I = GEP.op_begin() + 1, E = GEP.op_end(); @@ -1088,7 +1087,8 @@ SmallVector NewIndices; Type *InTy = cast(BCI->getOperand(0)->getType())->getElementType(); - if (FindElementAtOffset(InTy, Offset, NewIndices)) { + Type *IntPtrTy = TD->getIntPtrType(BCI->getOperand(0)->getType()); + if (FindElementAtOffset(InTy, Offset, IntPtrTy, NewIndices)) { Value *NGEP = GEP.isInBounds() ? Builder->CreateInBoundsGEP(BCI->getOperand(0), NewIndices) : Builder->CreateGEP(BCI->getOperand(0), NewIndices); Index: lib/Transforms/InstCombine/InstCombine.h =================================================================== --- lib/Transforms/InstCombine/InstCombine.h (revision 166296) +++ lib/Transforms/InstCombine/InstCombine.h (working copy) @@ -208,7 +208,7 @@ bool ShouldChangeType(Type *From, Type *To) const; Value *dyn_castNegVal(Value *V) const; Value *dyn_castFNegVal(Value *V) const; - Type *FindElementAtOffset(Type *Ty, int64_t Offset, + Type *FindElementAtOffset(Type *Ty, int64_t Offset, Type *IntPtrTy, SmallVectorImpl &NewIndices); Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI); Index: lib/Analysis/InlineCost.cpp =================================================================== --- lib/Analysis/InlineCost.cpp (revision 166296) +++ lib/Analysis/InlineCost.cpp (working copy) @@ -788,7 +788,7 @@ assert(V->getType()->isPointerTy() && "Unexpected operand type!"); } while (Visited.insert(V)); - Type *IntPtrTy = TD->getIntPtrType(V->getContext()); + Type *IntPtrTy = TD->getIntPtrType(V->getType()); return cast(ConstantInt::get(IntPtrTy, Offset)); } Index: lib/Analysis/InstructionSimplify.cpp =================================================================== --- lib/Analysis/InstructionSimplify.cpp (revision 166296) +++ lib/Analysis/InstructionSimplify.cpp (working copy) @@ -728,7 +728,7 @@ assert(V->getType()->isPointerTy() && "Unexpected operand type!"); } while (Visited.insert(V)); - Type *IntPtrTy = TD.getIntPtrType(V->getContext()); + Type *IntPtrTy = TD.getIntPtrType(V->getContext(), AS); return ConstantInt::get(IntPtrTy, Offset); } Index: lib/Analysis/ScalarEvolutionExpander.cpp =================================================================== --- lib/Analysis/ScalarEvolutionExpander.cpp (revision 166296) +++ lib/Analysis/ScalarEvolutionExpander.cpp (working copy) @@ -417,7 +417,9 @@ // array indexing. SmallVector ScaledOps; if (ElTy->isSized()) { - const SCEV *ElSize = SE.getSizeOfExpr(ElTy); + Type *IntPtrTy = SE.TD ? SE.TD->getIntPtrType(PTy) : + IntegerType::getInt64Ty(PTy->getContext()); + const SCEV *ElSize = SE.getSizeOfExpr(ElTy, IntPtrTy); if (!ElSize->isZero()) { SmallVector NewOps; for (unsigned i = 0, e = Ops.size(); i != e; ++i) { Index: lib/Analysis/Lint.cpp =================================================================== --- lib/Analysis/Lint.cpp (revision 166296) +++ lib/Analysis/Lint.cpp (working copy) @@ -606,6 +606,9 @@ // TODO: Look through eliminable cast pairs. // TODO: Look through calls with unique return values. // TODO: Look through vector insert/extract/shuffle. + // FIXME: Not sure how to get the correct address space here, + // so will just set it to zero for now. + unsigned AS = 0; V = OffsetOk ? GetUnderlyingObject(V, TD) : V->stripPointerCasts(); if (LoadInst *L = dyn_cast(V)) { BasicBlock::iterator BBI = L; @@ -626,7 +629,7 @@ if (W != V) return findValueImpl(W, OffsetOk, Visited); } else if (CastInst *CI = dyn_cast(V)) { - if (CI->isNoopCast(TD ? TD->getIntPtrType(V->getContext()) : + if (CI->isNoopCast(TD ? TD->getIntPtrType(V->getContext(), AS) : Type::getInt64Ty(V->getContext()))) return findValueImpl(CI->getOperand(0), OffsetOk, Visited); } else if (ExtractValueInst *Ex = dyn_cast(V)) { @@ -640,7 +643,7 @@ if (CastInst::isNoopCast(Instruction::CastOps(CE->getOpcode()), CE->getOperand(0)->getType(), CE->getType(), - TD ? TD->getIntPtrType(V->getContext()) : + TD ? TD->getIntPtrType(V->getContext(), AS) : Type::getInt64Ty(V->getContext()))) return findValueImpl(CE->getOperand(0), OffsetOk, Visited); } else if (CE->getOpcode() == Instruction::ExtractValue) { Index: lib/Analysis/MemoryBuiltins.cpp =================================================================== --- lib/Analysis/MemoryBuiltins.cpp (revision 166296) +++ lib/Analysis/MemoryBuiltins.cpp (working copy) @@ -376,9 +376,10 @@ ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor(const DataLayout *TD, const TargetLibraryInfo *TLI, LLVMContext &Context, - bool RoundToAlign) + bool RoundToAlign, + unsigned AS) : TD(TD), TLI(TLI), RoundToAlign(RoundToAlign) { - IntegerType *IntTy = TD->getIntPtrType(Context); + IntegerType *IntTy = TD->getIntPtrType(Context, AS); IntTyBits = IntTy->getBitWidth(); Zero = APInt::getNullValue(IntTyBits); } @@ -561,9 +562,10 @@ ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator(const DataLayout *TD, const TargetLibraryInfo *TLI, - LLVMContext &Context) + LLVMContext &Context, + unsigned AS) : TD(TD), TLI(TLI), Context(Context), Builder(Context, TargetFolder(TD)) { - IntTy = TD->getIntPtrType(Context); + IntTy = TD->getIntPtrType(Context, AS); Zero = ConstantInt::get(IntTy, 0); } Index: lib/Analysis/ConstantFolding.cpp =================================================================== --- lib/Analysis/ConstantFolding.cpp (revision 166296) +++ lib/Analysis/ConstantFolding.cpp (working copy) @@ -377,11 +377,12 @@ } if (ConstantExpr *CE = dyn_cast(C)) { - if (CE->getOpcode() == Instruction::IntToPtr && - CE->getOperand(0)->getType() == TD.getIntPtrType(CE->getContext())) + if (CE->getOpcode() == Instruction::IntToPtr) { + if (CE->getOperand(0)->getType() == TD.getIntPtrType(CE->getType())) return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr, BytesLeft, TD); } + } // Otherwise, unknown initializer type. return false; @@ -575,7 +576,7 @@ Type *ResultTy, const DataLayout *TD, const TargetLibraryInfo *TLI) { if (!TD) return 0; - Type *IntPtrTy = TD->getIntPtrType(ResultTy->getContext()); + Type *IntPtrTy = TD->getIntPtrType(ResultTy); bool Any = false; SmallVector NewIdxs; @@ -629,7 +630,8 @@ !Ptr->getType()->isPointerTy()) return 0; - Type *IntPtrTy = TD->getIntPtrType(Ptr->getContext()); + unsigned AS = cast(Ptr->getType())->getAddressSpace(); + Type *IntPtrTy = TD->getIntPtrType(Ptr->getContext(), AS); // If this is a constant expr gep that is effectively computing an // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12' @@ -702,6 +704,8 @@ // Also, this helps GlobalOpt do SROA on GlobalVariables. Type *Ty = Ptr->getType(); assert(Ty->isPointerTy() && "Forming regular GEP of non-pointer type"); + assert(Ty->getPointerAddressSpace() == AS + && "Operand and result of GEP should be in the same address space."); SmallVector NewIdxs; do { if (SequentialType *ATy = dyn_cast(Ty)) { @@ -717,7 +721,7 @@ // Determine which element of the array the offset points into. APInt ElemSize(BitWidth, TD->getTypeAllocSize(ATy->getElementType())); - IntegerType *IntPtrTy = TD->getIntPtrType(Ty->getContext()); + IntegerType *IntPtrTy = TD->getIntPtrType(Ty->getContext(), AS); if (ElemSize == 0) // The element size is 0. This may be [0 x Ty]*, so just use a zero // index for this level and proceed to the next level to see if it can @@ -988,9 +992,12 @@ // ConstantExpr::getCompare cannot do this, because it doesn't have TD // around to know if bit truncation is happening. if (ConstantExpr *CE0 = dyn_cast(Ops0)) { + Type *IntPtrTy = NULL; + unsigned AS = 0; if (TD && Ops1->isNullValue()) { - Type *IntPtrTy = TD->getIntPtrType(CE0->getContext()); if (CE0->getOpcode() == Instruction::IntToPtr) { + AS = CE0->getType()->getPointerAddressSpace(); + IntPtrTy = TD->getIntPtrType(CE0->getContext(), AS); // Convert the integer value to the right size to ensure we get the // proper extension or truncation. Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0), @@ -1001,19 +1008,23 @@ // Only do this transformation if the int is intptrty in size, otherwise // there is a truncation or extension that we aren't modeling. - if (CE0->getOpcode() == Instruction::PtrToInt && - CE0->getType() == IntPtrTy) { + if (CE0->getOpcode() == Instruction::PtrToInt) { + AS = CE0->getOperand(0)->getType()->getPointerAddressSpace(); + IntPtrTy = TD->getIntPtrType(CE0->getContext(), AS); + if (CE0->getType() == IntPtrTy) { Constant *C = CE0->getOperand(0); Constant *Null = Constant::getNullValue(C->getType()); return ConstantFoldCompareInstOperands(Predicate, C, Null, TD, TLI); } } + } if (ConstantExpr *CE1 = dyn_cast(Ops1)) { if (TD && CE0->getOpcode() == CE1->getOpcode()) { - Type *IntPtrTy = TD->getIntPtrType(CE0->getContext()); if (CE0->getOpcode() == Instruction::IntToPtr) { + AS = CE0->getType()->getPointerAddressSpace(); + Type *IntPtrTy = TD->getIntPtrType(CE0->getContext(), AS); // Convert the integer value to the right size to ensure we get the // proper extension or truncation. Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0), @@ -1022,12 +1033,15 @@ IntPtrTy, false); return ConstantFoldCompareInstOperands(Predicate, C0, C1, TD, TLI); } + } // Only do this transformation if the int is intptrty in size, otherwise // there is a truncation or extension that we aren't modeling. - if ((CE0->getOpcode() == Instruction::PtrToInt && - CE0->getType() == IntPtrTy && - CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType())) + if (CE0->getOpcode() == Instruction::PtrToInt) { + AS = CE0->getOperand(0)->getType()->getPointerAddressSpace(); + IntPtrTy = TD->getIntPtrType(CE0->getContext(), AS); + if (CE0->getType() == IntPtrTy && + CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) return ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(0), CE1->getOperand(0), TD, TLI); } Index: lib/Analysis/ScalarEvolution.cpp =================================================================== --- lib/Analysis/ScalarEvolution.cpp (revision 166296) +++ lib/Analysis/ScalarEvolution.cpp (working copy) @@ -2581,13 +2581,12 @@ return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); } -const SCEV *ScalarEvolution::getSizeOfExpr(Type *AllocTy) { +const SCEV *ScalarEvolution::getSizeOfExpr(Type *AllocTy, Type *IntPtrTy) { // If we have DataLayout, we can bypass creating a target-independent // constant expression and then folding it back into a ConstantInt. // This is just a compile-time optimization. if (TD) - return getConstant(TD->getIntPtrType(getContext()), - TD->getTypeAllocSize(AllocTy)); + return getConstant(IntPtrTy, TD->getTypeAllocSize(AllocTy)); Constant *C = ConstantExpr::getSizeOf(AllocTy); if (ConstantExpr *CE = dyn_cast(C)) @@ -2606,13 +2605,13 @@ return getTruncateOrZeroExtend(getSCEV(C), Ty); } -const SCEV *ScalarEvolution::getOffsetOfExpr(StructType *STy, +const SCEV *ScalarEvolution::getOffsetOfExpr(StructType *STy, Type *IntPtrTy, unsigned FieldNo) { // If we have DataLayout, we can bypass creating a target-independent // constant expression and then folding it back into a ConstantInt. // This is just a compile-time optimization. if (TD) - return getConstant(TD->getIntPtrType(getContext()), + return getConstant(IntPtrTy, TD->getStructLayout(STy)->getElementOffset(FieldNo)); Constant *C = ConstantExpr::getOffsetOf(STy, FieldNo); @@ -2699,7 +2698,7 @@ // The only other support type is pointer. assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!"); - if (TD) return TD->getIntPtrType(getContext()); + if (TD) return TD->getIntPtrType(Ty); // Without DataLayout, conservatively assume pointers are 64-bit. return Type::getInt64Ty(getContext()); @@ -3152,13 +3151,13 @@ if (StructType *STy = dyn_cast(*GTI++)) { // For a struct, add the member offset. unsigned FieldNo = cast(Index)->getZExtValue(); - const SCEV *FieldOffset = getOffsetOfExpr(STy, FieldNo); + const SCEV *FieldOffset = getOffsetOfExpr(STy, IntPtrTy, FieldNo); // Add the field offset to the running total offset. TotalOffset = getAddExpr(TotalOffset, FieldOffset); } else { // For an array, add the element offset, explicitly scaled. - const SCEV *ElementSize = getSizeOfExpr(*GTI); + const SCEV *ElementSize = getSizeOfExpr(*GTI, IntPtrTy); const SCEV *IndexS = getSCEV(Index); // Getelementptr indices are signed. IndexS = getTruncateOrSignExtend(IndexS, IntPtrTy); Index: lib/Target/NVPTX/NVPTXAsmPrinter.cpp =================================================================== --- lib/Target/NVPTX/NVPTXAsmPrinter.cpp (revision 166296) +++ lib/Target/NVPTX/NVPTXAsmPrinter.cpp (working copy) @@ -151,7 +151,7 @@ // Handle casts to pointers by changing them into casts to the appropriate // integer type. This promotes constant folding and simplifies this code. Constant *Op = CE->getOperand(0); - Op = ConstantExpr::getIntegerCast(Op, TD.getIntPtrType(CV->getContext()), + Op = ConstantExpr::getIntegerCast(Op, TD.getIntPtrType(CE->getType()), false/*ZExt*/); return LowerConstant(Op, AP); } Index: lib/Target/PowerPC/PPCISelLowering.cpp =================================================================== --- lib/Target/PowerPC/PPCISelLowering.cpp (revision 166296) +++ lib/Target/PowerPC/PPCISelLowering.cpp (working copy) @@ -1498,9 +1498,10 @@ EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); bool isPPC64 = (PtrVT == MVT::i64); + unsigned AS = 0; Type *IntPtrTy = DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType( - *DAG.getContext()); + *DAG.getContext(), AS); TargetLowering::ArgListTy Args; TargetLowering::ArgListEntry Entry; Index: lib/Target/ARM/ARMSelectionDAGInfo.cpp =================================================================== --- lib/Target/ARM/ARMSelectionDAGInfo.cpp (revision 166296) +++ lib/Target/ARM/ARMSelectionDAGInfo.cpp (working copy) @@ -155,7 +155,8 @@ TargetLowering::ArgListEntry Entry; // First argument: data pointer - Type *IntPtrTy = TLI.getDataLayout()->getIntPtrType(*DAG.getContext()); + unsigned AS = DstPtrInfo.getAddrSpace(); + Type *IntPtrTy = TLI.getDataLayout()->getIntPtrType(*DAG.getContext(), AS); Entry.Node = Dst; Entry.Ty = IntPtrTy; Args.push_back(Entry); Index: lib/Target/Target.cpp =================================================================== --- lib/Target/Target.cpp (revision 166296) +++ lib/Target/Target.cpp (working copy) @@ -64,7 +64,7 @@ } LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD) { - return wrap(unwrap(TD)->getIntPtrType(getGlobalContext())); + return wrap(unwrap(TD)->getIntPtrType(getGlobalContext(), 0)); } LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS) { Index: lib/Target/XCore/XCoreISelLowering.cpp =================================================================== --- lib/Target/XCore/XCoreISelLowering.cpp (revision 166296) +++ lib/Target/XCore/XCoreISelLowering.cpp (working copy) @@ -477,7 +477,8 @@ } // Lower to a call to __misaligned_load(BasePtr). - Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext()); + unsigned AS = LD->getAddressSpace(); + Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext(), AS); TargetLowering::ArgListTy Args; TargetLowering::ArgListEntry Entry; @@ -536,7 +537,8 @@ } // Lower to a call to __misaligned_store(BasePtr, Value). - Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext()); + unsigned AS = ST->getAddressSpace(); + Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext(), AS); TargetLowering::ArgListTy Args; TargetLowering::ArgListEntry Entry; Index: lib/Target/X86/X86FastISel.cpp =================================================================== --- lib/Target/X86/X86FastISel.cpp (revision 166296) +++ lib/Target/X86/X86FastISel.cpp (working copy) @@ -282,8 +282,9 @@ bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM) { // Handle 'null' like i32/i64 0. - if (isa(Val)) - Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext())); + if (isa(Val)) { + Val = Constant::getNullValue(TD.getIntPtrType(Val->getType())); + } // If this is a store of a simple constant, fold the constant into the store. if (const ConstantInt *CI = dyn_cast(Val)) { @@ -894,8 +895,9 @@ if (Op0Reg == 0) return false; // Handle 'null' like i32/i64 0. - if (isa(Op1)) - Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext())); + if (isa(Op1)) { + Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getType())); + } // We have two options: compare with register or immediate. If the RHS of // the compare is an immediate that we can fold into this compare, use Index: lib/Target/X86/X86SelectionDAGInfo.cpp =================================================================== --- lib/Target/X86/X86SelectionDAGInfo.cpp (revision 166296) +++ lib/Target/X86/X86SelectionDAGInfo.cpp (working copy) @@ -54,7 +54,8 @@ if (const char *bzeroEntry = V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) { EVT IntPtr = TLI.getPointerTy(); - Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext()); + unsigned AS = DstPtrInfo.getAddrSpace(); + Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext(), AS); TargetLowering::ArgListTy Args; TargetLowering::ArgListEntry Entry; Entry.Node = Dst; Index: tools/polly/include/polly/CodeGen/LoopGenerators.h =================================================================== --- tools/polly/include/polly/CodeGen/LoopGenerators.h (revision 166296) +++ tools/polly/include/polly/CodeGen/LoopGenerators.h (working copy) @@ -75,7 +75,7 @@ IRBuilder<> &Builder; Pass *P; - IntegerType *getIntPtrTy(); + IntegerType *getIntPtrTy(unsigned AS = 0); Module *getModule(); void createCallParallelLoopStart(Value *SubFunction, Value *SubfunctionParam, Index: tools/polly/lib/CodeGen/CodeGeneration.cpp =================================================================== --- tools/polly/lib/CodeGen/CodeGeneration.cpp (revision 166296) +++ tools/polly/lib/CodeGen/CodeGeneration.cpp (working copy) @@ -348,7 +348,7 @@ void addParameters(const CloogNames *names); - IntegerType *getIntPtrTy(); + IntegerType *getIntPtrTy(unsigned AS = 0); public: void codegen(const clast_root *r); @@ -357,8 +357,8 @@ }; } -IntegerType *ClastStmtCodeGen::getIntPtrTy() { - return P->getAnalysis().getIntPtrType(Builder.getContext()); +IntegerType *ClastStmtCodeGen::getIntPtrTy(unsigned AS) { + return P->getAnalysis().getIntPtrType(Builder.getContext(), AS); } const std::vector &ClastStmtCodeGen::getParallelLoops() { Index: tools/polly/lib/CodeGen/LoopGenerators.cpp =================================================================== --- tools/polly/lib/CodeGen/LoopGenerators.cpp (revision 166296) +++ tools/polly/lib/CodeGen/LoopGenerators.cpp (working copy) @@ -179,8 +179,8 @@ Builder.CreateCall(F); } -IntegerType *OMPGenerator::getIntPtrTy() { - return P->getAnalysis().getIntPtrType(Builder.getContext()); +IntegerType *OMPGenerator::getIntPtrTy(unsigned AS) { + return P->getAnalysis().getIntPtrType(Builder.getContext(), AS); } Module *OMPGenerator::getModule() { Index: tools/dragonegg/src/Convert.cpp =================================================================== --- tools/dragonegg/src/Convert.cpp (revision 166296) +++ tools/dragonegg/src/Convert.cpp (working copy) @@ -461,7 +461,7 @@ Value *Res = UndefValue::get(RegTy); bool isVectorOfPointers = isa(EltRegTy); unsigned Stride = GET_MODE_SIZE(TYPE_MODE(elt_type)); - IntegerType *IntPtrTy = getDataLayout().getIntPtrType(Context); + IntegerType *IntPtrTy = getDataLayout().getIntPtrType(EltRegTy); for (unsigned i = 0; i != NumElts; ++i) { Value *Idx = Builder.getInt32(i); Value *Elt = LoadRegisterFromMemory(Loc, elt_type, AliasTag, Builder); @@ -840,8 +840,10 @@ // bytes, but only 10 are copied. If the object is really a union // we might need the other bytes. We must also be careful to use // the smaller alignment. + // FIXME: Where do we get the address space? + unsigned AS = 0; Type *SBP = Type::getInt8PtrTy(Context); - Type *IntPtr = getDataLayout().getIntPtrType(Context); + Type *IntPtr = getDataLayout().getIntPtrType(Context, AS); Value *Ops[5] = { Builder.CreateCast(Instruction::BitCast, Loc, SBP), Builder.CreateCast(Instruction::BitCast, AI, SBP), @@ -1355,7 +1357,7 @@ Builder.CreateBitCast(ResultLV.Ptr, Type::getInt8PtrTy(Context)); ResultLV.Ptr = Builder.CreateGEP(ResultLV.Ptr, - ConstantInt::get(TD.getIntPtrType(Context), + ConstantInt::get(TD.getIntPtrType(ResultLV.Ptr->getType()), ReturnOffset), flag_verbose_asm ? "rtvl" : ""); ResultLV.setAlignment(MinAlign(ResultLV.getAlignment(), ReturnOffset)); @@ -1861,7 +1863,7 @@ cast(OrigTy)->getNumElements() : 0; if (OrigEltTy->isPointerTy()) { // A pointer/vector of pointer - form a (vector of) pointer sized integers. - Type *NewEltTy = TD.getIntPtrType(Context); + Type *NewEltTy = TD.getIntPtrType(OrigEltTy); Type *NewTy = VecElts ? VectorType::get(NewEltTy, VecElts) : NewEltTy; return Builder.CreatePtrToInt(V, NewTy); } @@ -2219,8 +2221,9 @@ Value *TreeToLLVM::EmitMemCpy(Value *DestPtr, Value *SrcPtr, Value *Size, unsigned Align) { + Type *SBP = Type::getInt8PtrTy(Context); - Type *IntPtr = TD.getIntPtrType(Context); + Type *IntPtr = TD.getIntPtrType(DestPtr->getType()); Value *Ops[5] = { Builder.CreateBitCast(DestPtr, SBP), Builder.CreateBitCast(SrcPtr, SBP), @@ -2238,7 +2241,7 @@ Value *TreeToLLVM::EmitMemMove(Value *DestPtr, Value *SrcPtr, Value *Size, unsigned Align) { Type *SBP = Type::getInt8PtrTy(Context); - Type *IntPtr = TD.getIntPtrType(Context); + Type *IntPtr = TD.getIntPtrType(DestPtr->getType()); Value *Ops[5] = { Builder.CreateBitCast(DestPtr, SBP), Builder.CreateBitCast(SrcPtr, SBP), @@ -2256,7 +2259,7 @@ Value *TreeToLLVM::EmitMemSet(Value *DestPtr, Value *SrcVal, Value *Size, unsigned Align) { Type *SBP = Type::getInt8PtrTy(Context); - Type *IntPtr = TD.getIntPtrType(Context); + Type *IntPtr = TD.getIntPtrType(DestPtr->getType()); Value *Ops[5] = { Builder.CreateBitCast(DestPtr, SBP), Builder.CreateIntCast(SrcVal, Type::getInt8Ty(Context), /*isSigned*/true), @@ -2904,8 +2907,9 @@ } else { // LLVM does not support vectors of pointers, so turn any pointers into // integers. - if (isa(Elt->getType())) - Elt = Builder.CreatePtrToInt(Elt, TD.getIntPtrType(Context)); + if (isa(Elt->getType())) { + Elt = Builder.CreatePtrToInt(Elt, TD.getIntPtrType(Elt->getType())); + } assert(Elt->getType() == VTy->getElementType() && "Unexpected type for vector constructor!"); BuildVecOps.push_back(Elt); @@ -3477,7 +3481,7 @@ if (Client.Offset) { Ptr = Builder.CreateBitCast(Ptr, Type::getInt8PtrTy(Context)); Ptr = Builder.CreateGEP(Ptr, - ConstantInt::get(TD.getIntPtrType(Context), Client.Offset), + ConstantInt::get(TD.getIntPtrType(Ptr->getType()), Client.Offset), flag_verbose_asm ? "ro" : ""); Align = MinAlign(Align, Client.Offset); MaxStoreSize -= Client.Offset; @@ -5674,7 +5678,9 @@ if (!validate_gimple_arglist(stmt, INTEGER_TYPE, POINTER_TYPE, VOID_TYPE)) return false; - Type *IntPtr = TD.getIntPtrType(Context); + // FIXME: Where do I get the address space from here? + unsigned AS = 0; + Type *IntPtr = TD.getIntPtrType(Context, AS); Value *Offset = EmitMemory(gimple_call_arg(stmt, 0)); Value *Handler = EmitMemory(gimple_call_arg(stmt, 1)); @@ -6023,7 +6029,7 @@ ArrayAddr = ArrayAddrLV.Ptr; ArrayAlign = ArrayAddrLV.getAlignment(); - Type *IntPtrTy = getDataLayout().getIntPtrType(Context); + Type *IntPtrTy = getDataLayout().getIntPtrType(ArrayAddr->getType()); IndexVal = Builder.CreateIntCast(IndexVal, IntPtrTy, /*isSigned*/!TYPE_UNSIGNED(IndexType)); @@ -6599,7 +6605,9 @@ // Convert the elements. SmallVector Elts; - IntegerType *IntTy = getDataLayout().getIntPtrType(Context); + // FIXME: Where do I get the address space from? + unsigned AS = 0; + IntegerType *IntTy = getDataLayout().getIntPtrType(Context, AS); for (tree elt = TREE_VECTOR_CST_ELTS(reg); elt; elt = TREE_CHAIN(elt)) { Constant *Elt = EmitRegisterConstant(TREE_VALUE(elt)); // LLVM does not support vectors of pointers, so turn any pointers into Index: tools/dragonegg/src/TypeConversion.cpp =================================================================== --- tools/dragonegg/src/TypeConversion.cpp (revision 166296) +++ tools/dragonegg/src/TypeConversion.cpp (working copy) @@ -457,7 +457,9 @@ } case OFFSET_TYPE: - return getDataLayout().getIntPtrType(Context); + // FIXME: Need to get the Address space here. + unsigned AS = 0; + return getDataLayout().getIntPtrType(Context, AS); case POINTER_TYPE: case REFERENCE_TYPE: @@ -484,9 +486,11 @@ case VECTOR_TYPE: { // LLVM does not support vectors of pointers, so turn any pointers into - // integers. + // integers. <-- This isn't true since at least 3.1 as far as I know - MicahV + // FIXME: Need to get the Address space here. + unsigned AS = 0; Type *EltTy = isa(TREE_TYPE(type)) ? - getDataLayout().getIntPtrType(Context) : getRegType(TREE_TYPE(type)); + getDataLayout().getIntPtrType(Context, AS) : getRegType(TREE_TYPE(type)); return VectorType::get(EltTy, TYPE_VECTOR_SUBPARTS(type)); } @@ -1419,7 +1423,9 @@ // which are really just integer offsets. Return the appropriate integer // type directly. // Caching the type conversion is not worth it. - return CheckTypeConversion(type, getDataLayout().getIntPtrType(Context)); + // FIXME: Need to get the Address space here. + unsigned AS = 0; + return CheckTypeConversion(type, getDataLayout().getIntPtrType(Context, AS)); case REAL_TYPE: // Caching the type conversion is not worth it. @@ -1458,7 +1464,9 @@ // LLVM does not support vectors of pointers, so turn any pointers into // integers. if (isa(TREE_TYPE(type))) - Ty = getDataLayout().getIntPtrType(Context); + // FIXME: Need to get the Address space here. + unsigned AS = 0; + Ty = getDataLayout().getIntPtrType(Context, AS); else Ty = ConvertTypeNonRecursive(main_type(type)); Ty = VectorType::get(Ty, TYPE_VECTOR_SUBPARTS(type)); Index: tools/dragonegg/src/ConstantConversion.cpp =================================================================== --- tools/dragonegg/src/ConstantConversion.cpp (revision 166296) +++ tools/dragonegg/src/ConstantConversion.cpp (working copy) @@ -295,7 +295,7 @@ llvm_unreachable("Unsupported type!"); case Type::PointerTyID: { // Cast to an integer with the same number of bits and return that. - IntegerType *IntTy = getDataLayout().getIntPtrType(Context); + IntegerType *IntTy = getDataLayout().getIntPtrType(Ty); return BitSlice(0, StoreSize, Folder.CreatePtrToInt(C, IntTy)); } case Type::DoubleTyID: @@ -445,7 +445,7 @@ case Type::PointerTyID: { // Interpret as an integer with the same number of bits then cast back to // the original type. - IntegerType *IntTy = getDataLayout().getIntPtrType(Context); + IntegerType *IntTy = getDataLayout().getIntPtrType(Ty); C = InterpretAsType(C, IntTy, StartingBit, Folder); return Folder.CreateIntToPtr(C, Ty); } @@ -559,7 +559,9 @@ unsigned NumElts = TYPE_VECTOR_SUBPARTS(type); unsigned Stride = GET_MODE_BITSIZE(TYPE_MODE(elt_type)); SmallVector Vals(NumElts); - IntegerType *IntPtrTy = getDataLayout().getIntPtrType(Context); + // FIXME: what is the address space here? + unsigned AS = 0; + IntegerType *IntPtrTy = getDataLayout().getIntPtrType(Context, AS); for (unsigned i = 0; i != NumElts; ++i) { Vals[i] = ExtractRegisterFromConstantImpl(C, elt_type, StartingBit+i*Stride, Folder); @@ -983,7 +985,7 @@ if (isa(init_type) && ActualEltTy == EltTy) { // If this is a vector of pointers, convert it to a vector of integers. if (isa(EltTy)) { - IntegerType *IntPtrTy = getDataLayout().getIntPtrType(Context); + IntegerType *IntPtrTy = getDataLayout().getIntPtrType(EltTy); for (unsigned i = 0, e = Elts.size(); i != e; ++i) Elts[i] = Folder.CreatePtrToInt(Elts[i], IntPtrTy); } @@ -1351,7 +1353,7 @@ Constant *LHS = getAsRegister(TREE_OPERAND(exp, 0), Folder); Constant *RHS = getAsRegister(TREE_OPERAND(exp, 1), Folder); if (LHS->getType()->getScalarType()->isPointerTy()) { - Type *PtrIntTy = getDataLayout().getIntPtrType(Context); + Type *PtrIntTy = getDataLayout().getIntPtrType(LHS->getType()); LHS = Folder.CreatePtrToInt(LHS, PtrIntTy); RHS = Folder.CreatePtrToInt(RHS, PtrIntTy); }