From: Sanjiv Gupta Date: Wed, 8 Apr 2009 04:16:39 +0000 (+0000) Subject: Pointer width on targets like PIC16 is 16-bit, while the valid index size to GEP... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=75c47a5cdf480571a71935cf80d49f802fec15a3;p=clang Pointer width on targets like PIC16 is 16-bit, while the valid index size to GEP is only 32 or 64. So promote index to 32 in such cases. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68590 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index 9067787f32..4a7171ebd8 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -799,8 +799,11 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { QualType IdxTy = E->getIdx()->getType(); bool IdxSigned = IdxTy->isSignedIntegerType(); unsigned IdxBitwidth = cast(Idx->getType())->getBitWidth(); - if (IdxBitwidth != LLVMPointerWidth) - Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth), + + // If Pointer width is less than 32 than extend to 32. + unsigned IdxValidWidth = (LLVMPointerWidth < 32 ) ? 32 : LLVMPointerWidth; + if (IdxBitwidth != IdxValidWidth) + Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(IdxValidWidth), IdxSigned, "idxprom"); // We know that the pointer points to a type of the correct size, unless the diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp index 225b70882e..3a24af4953 100644 --- a/lib/CodeGen/CGExprScalar.cpp +++ b/lib/CodeGen/CGExprScalar.cpp @@ -944,10 +944,14 @@ Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) { } unsigned Width = cast(Idx->getType())->getBitWidth(); - if (Width < CGF.LLVMPointerWidth) { + // Only 32 and 64 are valid index widths. So if a target has shorter + // pointe width, extend to 32 at least. + unsigned IdxValidWidth + = (CGF.LLVMPointerWidth < 32) ? 32 : CGF.LLVMPointerWidth; + if (Width < IdxValidWidth) { // Zero or sign extend the pointer value based on whether the index is // signed or not. - const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth); + const llvm::Type *IdxType = llvm::IntegerType::get(IdxValidWidth); if (IdxExp->getType()->isSignedIntegerType()) Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); else @@ -990,10 +994,12 @@ Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) { // pointer - int Value *Idx = Ops.RHS; unsigned Width = cast(Idx->getType())->getBitWidth(); - if (Width < CGF.LLVMPointerWidth) { + unsigned IdxValidWidth + = (CGF.LLVMPointerWidth < 32) ? 32 : CGF.LLVMPointerWidth; + if (Width < IdxValidWidth) { // Zero or sign extend the pointer value based on whether the index is // signed or not. - const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth); + const llvm::Type *IdxType = llvm::IntegerType::get(IdxValidWidth); if (Ops.E->getRHS()->getType()->isSignedIntegerType()) Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); else