Skip to content

Commit d59e990

Browse files
authored
[CIR][CIRGen][Builtin] Implement builtin addressof (#987)
1 parent 6cf9c5d commit d59e990

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,10 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
13411341

13421342
return RValue::get(ArithResult.overflow);
13431343
}
1344+
case Builtin::BIaddressof:
1345+
case Builtin::BI__addressof:
1346+
case Builtin::BI__builtin_addressof:
1347+
return RValue::get(buildLValue(E->getArg(0)).getPointer());
13441348
}
13451349

13461350
// If this is an alias for a lib function (e.g. __builtin_sin), emit

clang/test/CIR/CodeGen/builtins.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// RUN: %clang_cc1 -triple aarch64-none-linux-android24 -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
3+
// RUN: %clang_cc1 -triple aarch64-none-linux-android24 -fclangir \
4+
// RUN: -emit-llvm -fno-clangir-call-conv-lowering -o - %s \
5+
// RUN: | opt -S -passes=instcombine,mem2reg,simplifycfg -o %t.ll
6+
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
7+
8+
// This test file is a collection of test cases for all target-independent
9+
// builtins that are related to memory operations.
10+
11+
int s;
12+
13+
int *test_addressof() {
14+
return __builtin_addressof(s);
15+
16+
// CIR-LABEL: test_addressof
17+
// CIR: [[ADDR:%.*]] = cir.get_global @s : !cir.ptr<!s32i>
18+
// CIR: cir.store [[ADDR]], [[SAVE:%.*]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
19+
// CIR: [[RES:%.*]] = cir.load [[SAVE]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
20+
// CIR: cir.return [[RES]] : !cir.ptr<!s32i>
21+
22+
// LLVM-LABEL: test_addressof
23+
// LLVM: store ptr @s, ptr [[ADDR:%.*]], align 8
24+
// LLVM: [[RES:%.*]] = load ptr, ptr [[ADDR]], align 8
25+
// LLVM: ret ptr [[RES]]
26+
}
27+
28+
namespace std { template<typename T> T *addressof(T &); }
29+
int *test_std_addressof() {
30+
return std::addressof(s);
31+
32+
// CIR-LABEL: test_std_addressof
33+
// CIR: [[ADDR:%.*]] = cir.get_global @s : !cir.ptr<!s32i>
34+
// CIR: cir.store [[ADDR]], [[SAVE:%.*]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
35+
// CIR: [[RES:%.*]] = cir.load [[SAVE]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
36+
// CIR: cir.return [[RES]] : !cir.ptr<!s32i>
37+
38+
// LLVM-LABEL: test_std_addressof
39+
// LLVM: store ptr @s, ptr [[ADDR:%.*]], align 8
40+
// LLVM: [[RES:%.*]] = load ptr, ptr [[ADDR]], align 8
41+
// LLVM: ret ptr [[RES]]
42+
}
43+
44+
namespace std { template<typename T> T *__addressof(T &); }
45+
int *test_std_addressof2() {
46+
return std::__addressof(s);
47+
48+
// CIR-LABEL: test_std_addressof2
49+
// CIR: [[ADDR:%.*]] = cir.get_global @s : !cir.ptr<!s32i>
50+
// CIR: cir.store [[ADDR]], [[SAVE:%.*]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
51+
// CIR: [[RES:%.*]] = cir.load [[SAVE]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
52+
// CIR: cir.return [[RES]] : !cir.ptr<!s32i>
53+
54+
/// LLVM-LABEL: test_std_addressof2
55+
// LLVM: store ptr @s, ptr [[ADDR:%.*]], align 8
56+
// LLVM: [[RES:%.*]] = load ptr, ptr [[ADDR]], align 8
57+
// LLVM: ret ptr [[RES]]
58+
}

0 commit comments

Comments
 (0)