update calls to objectsize intrinsic to match LLVM r156473
add a test for -fbounds-checking code generation

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@156474 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGBuiltin.cpp b/lib/CodeGen/CGBuiltin.cpp
index bf180f6..f995614 100644
--- a/lib/CodeGen/CGBuiltin.cpp
+++ b/lib/CodeGen/CGBuiltin.cpp
@@ -346,11 +346,12 @@
     assert(CI);
     uint64_t val = CI->getZExtValue();
     CI = ConstantInt::get(Builder.getInt1Ty(), (val & 0x2) >> 1);    
+    Value *Runtime = Builder.getInt32(0);  // FIXME: use BoundsChecking here?
     
     Value *F = CGM.getIntrinsic(Intrinsic::objectsize, ResType);
-    return RValue::get(Builder.CreateCall2(F,
+    return RValue::get(Builder.CreateCall3(F,
                                            EmitScalarExpr(E->getArg(0)),
-                                           CI));
+                                           CI, Runtime));
   }
   case Builtin::BI__builtin_prefetch: {
     Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0));
diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp
index 19ee428..979454b 100644
--- a/lib/CodeGen/CGExpr.cpp
+++ b/lib/CodeGen/CGExpr.cpp
@@ -525,8 +525,9 @@
 
   llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, IntPtrTy);
 
-  llvm::Value *Arg = Builder.getFalse();
-  llvm::Value *C = Builder.CreateCall2(F, Address, Arg);
+  llvm::Value *Min = Builder.getFalse();
+  llvm::Value *Runtime = Builder.getInt32(BoundsChecking);
+  llvm::Value *C = Builder.CreateCall3(F, Address, Min, Runtime);
   llvm::BasicBlock *Cont = createBasicBlock();
   Builder.CreateCondBr(Builder.CreateICmpUGE(C,
                                         llvm::ConstantInt::get(IntPtrTy, Size)),
diff --git a/test/CodeGen/bounds-checking.c b/test/CodeGen/bounds-checking.c
new file mode 100644
index 0000000..ce7f24f
--- /dev/null
+++ b/test/CodeGen/bounds-checking.c
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 -fbounds-checking=4 -emit-llvm -triple x86_64-apple-darwin10 < %s | FileCheck %s
+
+// CHECK: @f
+double f(int b, int i) {
+  double a[b];
+  return a[i];
+  // CHECK: objectsize.i64({{.*}}, i1 false, i32 4)
+  // CHECK: icmp uge i64 {{.*}}, 8
+}
+
+// CHECK: @f2
+void f2() {
+  int a[2];
+  // CHECK: objectsize.i64({{.*}}, i1 false, i32 4)
+  // CHECK: icmp uge i64 {{.*}}, 4
+  a[1] = 42;
+  
+  short *b = malloc(64);
+  // CHECK: objectsize.i64({{.*}}, i1 false, i32 4)
+  // CHECK: icmp uge i64 {{.*}}, 4
+  // CHECK: getelementptr {{.*}}, i64 5
+  // CHECK: objectsize.i64({{.*}}, i1 false, i32 4)
+  // CHECK: icmp uge i64 {{.*}}, 2
+  b[5] = a[1]+2;
+}
diff --git a/test/CodeGen/object-size.c b/test/CodeGen/object-size.c
index 1f16d02..966d59f 100644
--- a/test/CodeGen/object-size.c
+++ b/test/CodeGen/object-size.c
@@ -40,7 +40,7 @@
 // CHECK: define void @test5
 void test5() {
   // CHECK:     = load i8** @gp
-  // CHECK-NEXT:= call i64 @llvm.objectsize.i64(i8* %{{.*}}, i1 false)
+  // CHECK-NEXT:= call i64 @llvm.objectsize.i64(i8* %{{.*}}, i1 false, i32 0)
   strcpy(gp, "Hi there");
 }
 
@@ -55,7 +55,7 @@
 // CHECK: define void @test7
 void test7() {
   int i;
-  // CHECK:     = call i64 @llvm.objectsize.i64(i8* {{.*}}@gbuf{{.*}}, i1 false)
+  // CHECK:     = call i64 @llvm.objectsize.i64(i8* {{.*}}@gbuf{{.*}}, i1 false, i32 0)
   strcpy((++i, gbuf), "Hi there");
 }