Skip to content

Commit 46f89e7

Browse files
committed
[ObjC][CodeGen] Assume a for-in loop is in bounds and cannot overflow
When accessing data in the buffer, we know we won't overrun the buffer, so we know it is inbounds. In addition, we know that the addition to increase the index is also NUW because the buffer's end has to be unsigned-greater-than 0, which becomes untrue if the bounds ever has an unsigned wrap.
1 parent acf6721 commit 46f89e7

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

clang/lib/CodeGen/CGObjC.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,7 +1952,7 @@ void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
19521952
Builder.CreateLoad(StateItemsPtr, "stateitems");
19531953

19541954
// Fetch the value at the current index from the buffer.
1955-
llvm::Value *CurrentItemPtr = Builder.CreateGEP(
1955+
llvm::Value *CurrentItemPtr = Builder.CreateInBoundsGEP(
19561956
ObjCIdType, EnumStateItems, index, "currentitem.ptr");
19571957
llvm::Value *CurrentItem =
19581958
Builder.CreateAlignedLoad(ObjCIdType, CurrentItemPtr, getPointerAlign());
@@ -2028,7 +2028,7 @@ void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
20282028

20292029
// First we check in the local buffer.
20302030
llvm::Value *indexPlusOne =
2031-
Builder.CreateAdd(index, llvm::ConstantInt::get(NSUIntegerTy, 1));
2031+
Builder.CreateNUWAdd(index, llvm::ConstantInt::get(NSUIntegerTy, 1));
20322032

20332033
// If we haven't overrun the buffer yet, we can continue.
20342034
// Set the branch weights based on the simplifying assumption that this is

clang/test/CodeGenObjC/arc-foreach.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void test0(NSArray *array) {
5353

5454
// CHECK-LP64: [[T0:%.*]] = getelementptr inbounds [[STATE_T]], ptr [[STATE]], i32 0, i32 1
5555
// CHECK-LP64-NEXT: [[T1:%.*]] = load ptr, ptr [[T0]]
56-
// CHECK-LP64-NEXT: [[T2:%.*]] = getelementptr ptr, ptr [[T1]], i64
56+
// CHECK-LP64-NEXT: [[T2:%.*]] = getelementptr inbounds ptr, ptr [[T1]], i64
5757
// CHECK-LP64-NEXT: [[T3:%.*]] = load ptr, ptr [[T2]]
5858
// CHECK-LP64-NEXT: store ptr [[T3]], ptr [[X]]
5959

@@ -100,7 +100,7 @@ void test1(NSArray *array) {
100100

101101
// CHECK-LP64: [[T0:%.*]] = getelementptr inbounds [[STATE_T]], ptr [[STATE]], i32 0, i32 1
102102
// CHECK-LP64-NEXT: [[T1:%.*]] = load ptr, ptr [[T0]]
103-
// CHECK-LP64-NEXT: [[T2:%.*]] = getelementptr ptr, ptr [[T1]], i64
103+
// CHECK-LP64-NEXT: [[T2:%.*]] = getelementptr inbounds ptr, ptr [[T1]], i64
104104
// CHECK-LP64-NEXT: [[T3:%.*]] = load ptr, ptr [[T2]]
105105
// CHECK-LP64-NEXT: call ptr @llvm.objc.initWeak(ptr [[X]], ptr [[T3]])
106106

0 commit comments

Comments
 (0)