diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index 33b6c12962c3..441b85a417e3 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -1341,6 +1341,10 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, return RValue::get(ArithResult.overflow); } + case Builtin::BIaddressof: + case Builtin::BI__addressof: + case Builtin::BI__builtin_addressof: + return RValue::get(buildLValue(E->getArg(0)).getPointer()); } // If this is an alias for a lib function (e.g. __builtin_sin), emit diff --git a/clang/test/CIR/CodeGen/builtins.cpp b/clang/test/CIR/CodeGen/builtins.cpp new file mode 100644 index 000000000000..fa7b51e88016 --- /dev/null +++ b/clang/test/CIR/CodeGen/builtins.cpp @@ -0,0 +1,58 @@ +// RUN: %clang_cc1 -triple aarch64-none-linux-android24 -emit-cir %s -o %t.cir +// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s +// RUN: %clang_cc1 -triple aarch64-none-linux-android24 -fclangir \ +// RUN: -emit-llvm -fno-clangir-call-conv-lowering -o - %s \ +// RUN: | opt -S -passes=instcombine,mem2reg,simplifycfg -o %t.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s + +// This test file is a collection of test cases for all target-independent +// builtins that are related to memory operations. + +int s; + +int *test_addressof() { + return __builtin_addressof(s); + + // CIR-LABEL: test_addressof + // CIR: [[ADDR:%.*]] = cir.get_global @s : !cir.ptr + // CIR: cir.store [[ADDR]], [[SAVE:%.*]] : !cir.ptr, !cir.ptr> + // CIR: [[RES:%.*]] = cir.load [[SAVE]] : !cir.ptr>, !cir.ptr + // CIR: cir.return [[RES]] : !cir.ptr + + // LLVM-LABEL: test_addressof + // LLVM: store ptr @s, ptr [[ADDR:%.*]], align 8 + // LLVM: [[RES:%.*]] = load ptr, ptr [[ADDR]], align 8 + // LLVM: ret ptr [[RES]] +} + +namespace std { template T *addressof(T &); } +int *test_std_addressof() { + return std::addressof(s); + + // CIR-LABEL: test_std_addressof + // CIR: [[ADDR:%.*]] = cir.get_global @s : !cir.ptr + // CIR: cir.store [[ADDR]], [[SAVE:%.*]] : !cir.ptr, !cir.ptr> + // CIR: [[RES:%.*]] = cir.load [[SAVE]] : !cir.ptr>, !cir.ptr + // CIR: cir.return [[RES]] : !cir.ptr + + // LLVM-LABEL: test_std_addressof + // LLVM: store ptr @s, ptr [[ADDR:%.*]], align 8 + // LLVM: [[RES:%.*]] = load ptr, ptr [[ADDR]], align 8 + // LLVM: ret ptr [[RES]] +} + +namespace std { template T *__addressof(T &); } +int *test_std_addressof2() { + return std::__addressof(s); + + // CIR-LABEL: test_std_addressof2 + // CIR: [[ADDR:%.*]] = cir.get_global @s : !cir.ptr + // CIR: cir.store [[ADDR]], [[SAVE:%.*]] : !cir.ptr, !cir.ptr> + // CIR: [[RES:%.*]] = cir.load [[SAVE]] : !cir.ptr>, !cir.ptr + // CIR: cir.return [[RES]] : !cir.ptr + + /// LLVM-LABEL: test_std_addressof2 + // LLVM: store ptr @s, ptr [[ADDR:%.*]], align 8 + // LLVM: [[RES:%.*]] = load ptr, ptr [[ADDR]], align 8 + // LLVM: ret ptr [[RES]] +}