Skip to content

Commit ba0f0cc

Browse files
committed
[CIR] Refactored array pointer type checking to avoid nested ifs
1 parent 39b3bc8 commit ba0f0cc

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

clang/lib/CIR/CodeGen/CIRGenBuilder.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ mlir::Value CIRGenBuilderTy::promoteArrayIndex(const clang::TargetInfo &ti,
3232
mlir::Location loc,
3333
mlir::Value index) {
3434
// Get the array index type.
35-
auto arrayIndexWidth = TI.getTypeWidth(clang::TargetInfo::IntType::SignedInt);
35+
auto arrayIndexWidth = ti.getTypeWidth(clang::TargetInfo::IntType::SignedInt);
3636
mlir::Type arrayIndexType = getSIntNTy(arrayIndexWidth);
3737

3838
// If this is a boolean, zero-extend it to the array index type.
@@ -50,23 +50,23 @@ mlir::Value CIRGenBuilderTy::promoteArrayIndex(const clang::TargetInfo &ti,
5050
return index;
5151
}
5252

53-
mlir::Value CIRGenBuilderTy::getArrayElement(const clang::TargetInfo &TI,
53+
mlir::Value CIRGenBuilderTy::getArrayElement(const clang::TargetInfo &ti,
5454
mlir::Location arrayLocBegin,
5555
mlir::Location arrayLocEnd,
5656
mlir::Value arrayPtr,
5757
mlir::Type eltTy, mlir::Value idx,
5858
bool shouldDecay) {
59+
auto arrayPtrTy = dyn_cast<cir::PointerType>(arrayPtr.getType());
60+
assert(arrayPtrTy && "expected pointer type");
61+
5962
// If the array pointer is not decayed, emit a GetElementOp.
60-
if (shouldDecay)
61-
if (auto arrayPtrTy = dyn_cast<cir::PointerType>(arrayPtr.getType()))
62-
if (auto arrayTy = dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee()))
63-
if (arrayTy == eltTy) {
64-
auto eltPtrTy =
65-
getPointerTo(arrayTy.getElementType(), arrayPtrTy.getAddrSpace());
66-
return create<cir::GetElementOp>(
67-
arrayLocEnd, eltPtrTy, arrayPtr,
68-
promoteArrayIndex(TI, arrayLocBegin, idx));
69-
}
63+
auto arrayTy = dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee());
64+
if (shouldDecay && arrayTy && arrayTy == eltTy) {
65+
auto eltPtrTy =
66+
getPointerTo(arrayTy.getElementType(), arrayPtrTy.getAddrSpace());
67+
return create<cir::GetElementOp>(arrayLocEnd, eltPtrTy, arrayPtr,
68+
promoteArrayIndex(ti, arrayLocBegin, idx));
69+
}
7070

7171
// If we don't have sufficient type information, emit a PtrStrideOp.
7272
mlir::Value basePtr = arrayPtr;

0 commit comments

Comments
 (0)