aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen
diff options
context:
space:
mode:
Diffstat (limited to 'lib/CodeGen')
-rw-r--r--lib/CodeGen/CGExpr.cpp22
-rw-r--r--lib/CodeGen/CGExprScalar.cpp31
2 files changed, 48 insertions, 5 deletions
diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp
index f155139e51..c49592eae8 100644
--- a/lib/CodeGen/CGExpr.cpp
+++ b/lib/CodeGen/CGExpr.cpp
@@ -809,8 +809,9 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
IdxSigned, "idxprom");
- // We know that the pointer points to a type of the correct size, unless the
- // size is a VLA.
+ // We know that the pointer points to a type of the correct size,
+ // unless the size is a VLA or Objective-C interface.
+ llvm::Value *Address = 0;
if (const VariableArrayType *VAT =
getContext().getAsVariableArrayType(E->getType())) {
llvm::Value *VLASize = VLASizeMap[VAT];
@@ -823,10 +824,25 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Idx = Builder.CreateUDiv(Idx,
llvm::ConstantInt::get(Idx->getType(),
BaseTypeSize));
+ Address = Builder.CreateGEP(Base, Idx, "arrayidx");
+ } else if (const ObjCInterfaceType *OIT =
+ dyn_cast<ObjCInterfaceType>(E->getType())) {
+ llvm::Value *InterfaceSize =
+ llvm::ConstantInt::get(Idx->getType(),
+ getContext().getTypeSize(OIT) / 8);
+
+ Idx = Builder.CreateMul(Idx, InterfaceSize);
+
+ llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
+ Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
+ Idx, "arrayidx");
+ Address = Builder.CreateBitCast(Address, Base->getType());
+ } else {
+ Address = Builder.CreateGEP(Base, Idx, "arrayidx");
}
QualType T = E->getBase()->getType()->getAsPointerType()->getPointeeType();
- LValue LV = LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx"),
+ LValue LV = LValue::MakeAddr(Address,
T.getCVRQualifiers(),
getContext().getObjCGCAttrKind(T));
if (getContext().getLangOptions().ObjC1 &&
diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp
index 308a45e433..5e0159e597 100644
--- a/lib/CodeGen/CGExprScalar.cpp
+++ b/lib/CodeGen/CGExprScalar.cpp
@@ -968,14 +968,27 @@ Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
}
+ const QualType ElementType = PT->getPointeeType();
+ // Handle interface types, which are not represented with a concrete
+ // type.
+ if (const ObjCInterfaceType *OIT = dyn_cast<ObjCInterfaceType>(ElementType)) {
+ llvm::Value *InterfaceSize =
+ llvm::ConstantInt::get(Idx->getType(),
+ CGF.getContext().getTypeSize(OIT) / 8);
+ Idx = Builder.CreateMul(Idx, InterfaceSize);
+ const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
+ Value *Casted = Builder.CreateBitCast(Ptr, i8Ty);
+ Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr");
+ return Builder.CreateBitCast(Res, Ptr->getType());
+ }
+
// Explicitly handle GNU void* and function pointer arithmetic
// extensions. The GNU void* casts amount to no-ops since our void*
// type is i8*, but this is future proof.
- const QualType ElementType = PT->getPointeeType();
if (ElementType->isVoidType() || ElementType->isFunctionType()) {
const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Value *Casted = Builder.CreateBitCast(Ptr, i8Ty);
- Value *Res = Builder.CreateGEP(Casted, Idx, "sub.ptr");
+ Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr");
return Builder.CreateBitCast(Res, Ptr->getType());
}
@@ -1015,6 +1028,20 @@ Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
}
Idx = Builder.CreateNeg(Idx, "sub.ptr.neg");
+ // Handle interface types, which are not represented with a concrete
+ // type.
+ if (const ObjCInterfaceType *OIT =
+ dyn_cast<ObjCInterfaceType>(LHSElementType)) {
+ llvm::Value *InterfaceSize =
+ llvm::ConstantInt::get(Idx->getType(),
+ CGF.getContext().getTypeSize(OIT) / 8);
+ Idx = Builder.CreateMul(Idx, InterfaceSize);
+ const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
+ Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty);
+ Value *Res = Builder.CreateGEP(LHSCasted, Idx, "add.ptr");
+ return Builder.CreateBitCast(Res, Ops.LHS->getType());
+ }
+
// Explicitly handle GNU void* and function pointer arithmetic
// extensions. The GNU void* casts amount to no-ops since our
// void* type is i8*, but this is future proof.