Skip to content

Commit c5de163

Browse files
committed
[Clang][CodeGen] Add metadata for load from reference
1 parent bcdafc1 commit c5de163

28 files changed

+4800
-4785
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ C++ Specific Potentially Breaking Changes
5959
very few users and all the type traits that could benefit from it in the
6060
standard library already have their own bespoke builtins.
6161

62+
- Added ``!nonnull/!align`` metadata to load of references for better codegen.
63+
6264
ABI Changes in This Version
6365
---------------------------
6466

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2916,9 +2916,30 @@ CodeGenFunction::EmitLoadOfReference(LValue RefLVal,
29162916
llvm::LoadInst *Load =
29172917
Builder.CreateLoad(RefLVal.getAddress(), RefLVal.isVolatile());
29182918
CGM.DecorateInstructionWithTBAA(Load, RefLVal.getTBAAInfo());
2919-
return makeNaturalAddressForPointer(Load, RefLVal.getType()->getPointeeType(),
2920-
CharUnits(), /*ForPointeeType=*/true,
2921-
PointeeBaseInfo, PointeeTBAAInfo);
2919+
QualType PTy = RefLVal.getType()->getPointeeType();
2920+
if (!PTy->isIncompleteType()) {
2921+
llvm::LLVMContext &Ctx = getLLVMContext();
2922+
llvm::MDBuilder MDB(Ctx);
2923+
// Emit !nonnull metadata
2924+
if (CGM.getTypes().getTargetAddressSpace(PTy) == 0 &&
2925+
!CGM.getCodeGenOpts().NullPointerIsValid)
2926+
Load->setMetadata(llvm::LLVMContext::MD_nonnull,
2927+
llvm::MDNode::get(Ctx, {}));
2928+
// Emit !align metadata
2929+
if (PTy->isObjectType()) {
2930+
auto Align =
2931+
CGM.getNaturalPointeeTypeAlignment(RefLVal.getType()).getQuantity();
2932+
if (Align > 1) {
2933+
Load->setMetadata(
2934+
llvm::LLVMContext::MD_align,
2935+
llvm::MDNode::get(Ctx, MDB.createConstant(llvm::ConstantInt::get(
2936+
Builder.getInt64Ty(), Align))));
2937+
}
2938+
}
2939+
}
2940+
return makeNaturalAddressForPointer(Load, PTy, CharUnits(),
2941+
/*ForPointeeType=*/true, PointeeBaseInfo,
2942+
PointeeTBAAInfo);
29222943
}
29232944

29242945
LValue CodeGenFunction::EmitLoadOfReferenceLValue(LValue RefLVal) {

clang/test/CodeGenCXX/matrix-type-operators.cpp

Lines changed: 82 additions & 82 deletions
Large diffs are not rendered by default.

clang/test/CodeGenCXX/reference-field.cpp

Lines changed: 0 additions & 8 deletions
This file was deleted.

clang/test/OpenMP/distribute_parallel_for_simd_firstprivate_codegen.cpp

Lines changed: 351 additions & 351 deletions
Large diffs are not rendered by default.

clang/test/OpenMP/distribute_parallel_for_simd_lastprivate_codegen.cpp

Lines changed: 331 additions & 331 deletions
Large diffs are not rendered by default.

clang/test/OpenMP/distribute_parallel_for_simd_private_codegen.cpp

Lines changed: 172 additions & 172 deletions
Large diffs are not rendered by default.

clang/test/OpenMP/distribute_simd_firstprivate_codegen.cpp

Lines changed: 243 additions & 243 deletions
Large diffs are not rendered by default.

clang/test/OpenMP/distribute_simd_lastprivate_codegen.cpp

Lines changed: 239 additions & 239 deletions
Large diffs are not rendered by default.

clang/test/OpenMP/distribute_simd_private_codegen.cpp

Lines changed: 148 additions & 148 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)