Skip to content

Commit 1ea11e4

Browse files
authored
[CIR] Implement 'builtin-addressof' for 'getPointerWithAlignment' (llvm#185684)
The 'getPointerWithAlignment' is really only called when evaluating arguments for builtins, so the test is a touch weird as it test through bcopy. However, this shows up in some headers, so it is important that we support this. This patch just adds the implementation, which mirrors classic-codegen, except that we don't generate TBAA.
1 parent 6bc0faf commit 1ea11e4

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,11 @@ Address CIRGenFunction::emitPointerWithAlignment(const Expr *expr,
236236
case Builtin::BIaddressof:
237237
case Builtin::BI__addressof:
238238
case Builtin::BI__builtin_addressof: {
239-
cgm.errorNYI(expr->getSourceRange(),
240-
"emitPointerWithAlignment: builtin addressof");
241-
return Address::invalid();
239+
LValue lv = emitLValue(call->getArg(0));
240+
if (baseInfo)
241+
*baseInfo = lv.getBaseInfo();
242+
assert(!cir::MissingFeatures::opTBAA());
243+
return lv.getAddress();
242244
}
243245
}
244246
}

clang/test/CIR/CodeGenBuiltins/builtin-bcopy.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,21 @@ extern "C" void bcopy(const void *__src, void *__dest, size_t __n);
114114
void testbcopy(const void *src, void *dest, size_t n) {
115115
bcopy(src, dest, n);
116116
}
117+
118+
// CIR-LABEL: @testaddressof(
119+
// CIR: %[[SRC:.*]] = cir.alloca !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>>, ["src", init]
120+
// CIR: %[[DEST:.*]] = cir.alloca !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>>, ["dest", init]
121+
// CIR: %[[SRC_TO_VOIDPTR:.*]] = cir.cast bitcast %[[SRC]] : !cir.ptr<!cir.ptr<!s8i>> -> !cir.ptr<!void>
122+
// CIR: %[[DEST_TO_VOIDPTR:.*]] = cir.cast bitcast %[[DEST]] : !cir.ptr<!cir.ptr<!s8i>> -> !cir.ptr<!void>
123+
// CIR: cir.libc.memmove {{.*}} bytes from %[[SRC_TO_VOIDPTR]] to %[[DEST_TO_VOIDPTR]]
124+
// LLVM-LABEL: @testaddressof(
125+
// LLVM: %[[SRC:.*]] = alloca ptr
126+
// LLVM: %[[DEST:.*]] = alloca ptr
127+
// LLVM: call void @llvm.memmove.p0.p0.i64(ptr %[[DEST]], ptr %[[SRC]], i64 {{.*}}, i1 false)
128+
// OGCG-LABEL: @testaddressof(
129+
// OGCG: %[[SRC:.*]] = alloca ptr
130+
// OGCG: %[[DEST:.*]] = alloca ptr
131+
// OGCG: call void @llvm.memmove.p0.p0.i64(ptr {{.*}}%[[DEST]], ptr {{.*}}%[[SRC]], i64 {{.*}}, i1 false)
132+
extern "C" void testaddressof(const char *src, const char *dest, size_t n) {
133+
__builtin_bcopy(__builtin_addressof(src), __builtin_addressof(dest), n);
134+
}

0 commit comments

Comments
 (0)