Skip to content

[ObjC][CodeGen] Assume a for-in loop is in bounds and cannot overflow #94885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1952,7 +1952,7 @@ void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
Builder.CreateLoad(StateItemsPtr, "stateitems");

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

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

// If we haven't overrun the buffer yet, we can continue.
// Set the branch weights based on the simplifying assumption that this is
Expand Down
4 changes: 2 additions & 2 deletions clang/test/CodeGenObjC/arc-foreach.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void test0(NSArray *array) {

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

Expand Down Expand Up @@ -100,7 +100,7 @@ void test1(NSArray *array) {

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

Expand Down
Loading