aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CodeGen/CGExprScalar.cpp28
-rw-r--r--CodeGen/CodeGenModule.h1
-rw-r--r--clang.xcodeproj/project.pbxproj1
-rw-r--r--test/CodeGen/exprs.c8
4 files changed, 34 insertions, 4 deletions
diff --git a/CodeGen/CGExprScalar.cpp b/CodeGen/CGExprScalar.cpp
index 0fc1d0f8e7..639b907e02 100644
--- a/CodeGen/CGExprScalar.cpp
+++ b/CodeGen/CGExprScalar.cpp
@@ -713,10 +713,30 @@ Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
// FIXME: What about a pointer to a VLA?
- if (isa<llvm::PointerType>(Ops.LHS->getType())) // pointer + int
- return Builder.CreateGEP(Ops.LHS, Ops.RHS, "add.ptr");
- // int + pointer
- return Builder.CreateGEP(Ops.RHS, Ops.LHS, "add.ptr");
+ Value *Ptr, *Idx;
+ Expr *IdxExp;
+ if (isa<llvm::PointerType>(Ops.LHS->getType())) { // pointer + int
+ Ptr = Ops.LHS;
+ Idx = Ops.RHS;
+ IdxExp = Ops.E->getRHS();
+ } else { // int + pointer
+ Ptr = Ops.RHS;
+ Idx = Ops.LHS;
+ IdxExp = Ops.E->getLHS();
+ }
+
+ unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
+ if (Width < CGF.LLVMPointerWidth) {
+ // 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);
+ if (IdxExp->getType().getCanonicalType()->isSignedIntegerType())
+ Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
+ else
+ Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
+ }
+
+ return Builder.CreateGEP(Ptr, Idx, "add.ptr");
}
Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
diff --git a/CodeGen/CodeGenModule.h b/CodeGen/CodeGenModule.h
index acfb3349e6..6653e50881 100644
--- a/CodeGen/CodeGenModule.h
+++ b/CodeGen/CodeGenModule.h
@@ -67,6 +67,7 @@ public:
llvm::Module &getModule() const { return TheModule; }
CodeGenTypes &getTypes() { return Types; }
Diagnostic &getDiags() const { return Diags; }
+ const llvm::TargetData &getTargetData() const { return TheTargetData; }
llvm::Constant *GetAddrOfFunctionDecl(const FunctionDecl *D,
bool isDefinition);
diff --git a/clang.xcodeproj/project.pbxproj b/clang.xcodeproj/project.pbxproj
index 85781e5bde..6a4013bade 100644
--- a/clang.xcodeproj/project.pbxproj
+++ b/clang.xcodeproj/project.pbxproj
@@ -796,6 +796,7 @@
08FB7793FE84155DC02AAC07 /* Project object */ = {
isa = PBXProject;
buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */;
+ compatibilityVersion = "Xcode 2.4";
hasScannedForEncodings = 1;
mainGroup = 08FB7794FE84155DC02AAC07 /* clang */;
projectDirPath = "";
diff --git a/test/CodeGen/exprs.c b/test/CodeGen/exprs.c
index c57817d7f4..502cb83c13 100644
--- a/test/CodeGen/exprs.c
+++ b/test/CodeGen/exprs.c
@@ -6,3 +6,11 @@ int zxcv(void);
int x=sizeof(zxcv);
int y=__alignof__(zxcv);
+
+void *test(int *i) {
+ short a = 1;
+ i += a;
+ i + a;
+ a + i;
+}
+