Skip to content

Commit b120d8d

Browse files
committed
[Clang][CodeGen] Add metadata for load from reference
1 parent 3a84a4e commit b120d8d

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
@@ -61,6 +61,8 @@ C++ Specific Potentially Breaking Changes
6161
- A workaround for libstdc++4.7 has been removed. Note that 4.8.3 remains the oldest
6262
supported libstdc++ version.
6363

64+
- Added ``!nonnull/!align`` metadata to load of references for better codegen.
65+
6466
ABI Changes in This Version
6567
---------------------------
6668

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2938,9 +2938,30 @@ CodeGenFunction::EmitLoadOfReference(LValue RefLVal,
29382938
llvm::LoadInst *Load =
29392939
Builder.CreateLoad(RefLVal.getAddress(), RefLVal.isVolatile());
29402940
CGM.DecorateInstructionWithTBAA(Load, RefLVal.getTBAAInfo());
2941-
return makeNaturalAddressForPointer(Load, RefLVal.getType()->getPointeeType(),
2942-
CharUnits(), /*ForPointeeType=*/true,
2943-
PointeeBaseInfo, PointeeTBAAInfo);
2941+
QualType PTy = RefLVal.getType()->getPointeeType();
2942+
if (!PTy->isIncompleteType()) {
2943+
llvm::LLVMContext &Ctx = getLLVMContext();
2944+
llvm::MDBuilder MDB(Ctx);
2945+
// Emit !nonnull metadata
2946+
if (CGM.getTypes().getTargetAddressSpace(PTy) == 0 &&
2947+
!CGM.getCodeGenOpts().NullPointerIsValid)
2948+
Load->setMetadata(llvm::LLVMContext::MD_nonnull,
2949+
llvm::MDNode::get(Ctx, {}));
2950+
// Emit !align metadata
2951+
if (PTy->isObjectType()) {
2952+
auto Align =
2953+
CGM.getNaturalPointeeTypeAlignment(RefLVal.getType()).getQuantity();
2954+
if (Align > 1) {
2955+
Load->setMetadata(
2956+
llvm::LLVMContext::MD_align,
2957+
llvm::MDNode::get(Ctx, MDB.createConstant(llvm::ConstantInt::get(
2958+
Builder.getInt64Ty(), Align))));
2959+
}
2960+
}
2961+
}
2962+
return makeNaturalAddressForPointer(Load, PTy, CharUnits(),
2963+
/*ForPointeeType=*/true, PointeeBaseInfo,
2964+
PointeeTBAAInfo);
29442965
}
29452966

29462967
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)