Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions clang/test/CIR/CodeGen/builtins.cpp
Original file line number Diff line number Diff line change
@@ -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<!s32i>
// CIR: cir.store [[ADDR]], [[SAVE:%.*]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
// CIR: [[RES:%.*]] = cir.load [[SAVE]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
// CIR: cir.return [[RES]] : !cir.ptr<!s32i>

// 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<typename T> T *addressof(T &); }
int *test_std_addressof() {
return std::addressof(s);

// CIR-LABEL: test_std_addressof
// CIR: [[ADDR:%.*]] = cir.get_global @s : !cir.ptr<!s32i>
// CIR: cir.store [[ADDR]], [[SAVE:%.*]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
// CIR: [[RES:%.*]] = cir.load [[SAVE]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
// CIR: cir.return [[RES]] : !cir.ptr<!s32i>

// 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<typename T> T *__addressof(T &); }
int *test_std_addressof2() {
return std::__addressof(s);

// CIR-LABEL: test_std_addressof2
// CIR: [[ADDR:%.*]] = cir.get_global @s : !cir.ptr<!s32i>
// CIR: cir.store [[ADDR]], [[SAVE:%.*]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
// CIR: [[RES:%.*]] = cir.load [[SAVE]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
// CIR: cir.return [[RES]] : !cir.ptr<!s32i>

/// 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]]
}