Skip to content

Commit b7a6299

Browse files
committed
Address review feedback
1 parent ae1fd50 commit b7a6299

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1178,11 +1178,18 @@ static mlir::Value emitPointerArithmetic(CIRGenFunction &cgf,
11781178
mlir::Value index = op.rhs;
11791179
Expr *indexOperand = expr->getRHS();
11801180

1181-
// In a subtraction, the LHS is always the pointer.
1181+
// In the case of subtraction, the FE has ensured that the LHS is always the
1182+
// pointer. However, addition can have the pointer on either side. We will
1183+
// always have a pointer operand and an integer operand, so if the LHS wasn't
1184+
// a pointer, we need to swap our values.
11821185
if (!isSubtraction && !mlir::isa<cir::PointerType>(pointer.getType())) {
11831186
std::swap(pointer, index);
11841187
std::swap(pointerOperand, indexOperand);
11851188
}
1189+
assert(mlir::isa<cir::PointerType>(pointer.getType()) &&
1190+
"Need a pointer operand");
1191+
assert(mlir::isa<cir::IntType>(index.getType()) &&
1192+
"Need an integer operand");
11861193

11871194
// Some versions of glibc and gcc use idioms (particularly in their malloc
11881195
// routines) that add a pointer-sized integer (known to be a pointer value)

clang/test/CIR/CodeGen/pointers.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ void foo(int *iptr, char *cptr, unsigned ustride) {
2828
// CHECK: %[[#NEGSTRIDE:]] = cir.unary(minus, %[[#SIGNSTRIDE]]) : !s32i, !s32i
2929
// CHECK: cir.ptr_stride(%{{.+}} : !cir.ptr<!s32i>, %[[#NEGSTRIDE]] : !s32i), !cir.ptr<!s32i>
3030

31+
4 + iptr;
32+
// CHECK: %[[#STRIDE:]] = cir.const #cir.int<4> : !s32i
33+
// CHECK: cir.ptr_stride(%{{.+}} : !cir.ptr<!s32i>, %[[#STRIDE]] : !s32i), !cir.ptr<!s32i>
34+
3135
iptr++;
3236
// CHECK: %[[#STRIDE:]] = cir.const #cir.int<1> : !s32i
3337
// CHECK: cir.ptr_stride(%{{.+}} : !cir.ptr<!s32i>, %[[#STRIDE]] : !s32i), !cir.ptr<!s32i>

0 commit comments

Comments
 (0)