[NFC][Alignment] Use MaybeAlign in CGCleanup/CGExpr

This commit is contained in:
Guillaume Chatelet 2022-06-14 09:55:27 +00:00
parent 77bba68de6
commit d9b8d13f8b
3 changed files with 14 additions and 11 deletions
clang
include/clang/AST
lib/CodeGen

View File

@ -182,6 +182,12 @@ namespace clang {
/// Beware llvm::Align assumes power of two 8-bit bytes. /// Beware llvm::Align assumes power of two 8-bit bytes.
llvm::Align getAsAlign() const { return llvm::Align(Quantity); } llvm::Align getAsAlign() const { return llvm::Align(Quantity); }
/// getAsMaybeAlign - Returns Quantity as a valid llvm::Align or
/// llvm::None, Beware llvm::MaybeAlign assumes power of two 8-bit bytes.
llvm::MaybeAlign getAsMaybeAlign() const {
return llvm::MaybeAlign(Quantity);
}
/// alignTo - Returns the next integer (mod 2**64) that is /// alignTo - Returns the next integer (mod 2**64) that is
/// greater than or equal to this quantity and is a multiple of \p Align. /// greater than or equal to this quantity and is a multiple of \p Align.
/// Align must be non-zero. /// Align must be non-zero.

View File

@ -77,7 +77,7 @@ RValue DominatingValue<RValue>::saved_type::restore(CodeGenFunction &CGF) {
auto getSavingAddress = [&](llvm::Value *value) { auto getSavingAddress = [&](llvm::Value *value) {
auto *AI = cast<llvm::AllocaInst>(value); auto *AI = cast<llvm::AllocaInst>(value);
return Address(value, AI->getAllocatedType(), return Address(value, AI->getAllocatedType(),
CharUnits::fromQuantity(AI->getAlignment())); CharUnits::fromQuantity(AI->getAlign().value()));
}; };
switch (K) { switch (K) {
case ScalarLiteral: case ScalarLiteral:

View File

@ -757,23 +757,23 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
} }
} }
uint64_t AlignVal = 0; llvm::MaybeAlign AlignVal;
llvm::Value *PtrAsInt = nullptr; llvm::Value *PtrAsInt = nullptr;
if (SanOpts.has(SanitizerKind::Alignment) && if (SanOpts.has(SanitizerKind::Alignment) &&
!SkippedChecks.has(SanitizerKind::Alignment)) { !SkippedChecks.has(SanitizerKind::Alignment)) {
AlignVal = Alignment.getQuantity(); AlignVal = Alignment.getAsMaybeAlign();
if (!Ty->isIncompleteType() && !AlignVal) if (!Ty->isIncompleteType() && !AlignVal)
AlignVal = CGM.getNaturalTypeAlignment(Ty, nullptr, nullptr, AlignVal = CGM.getNaturalTypeAlignment(Ty, nullptr, nullptr,
/*ForPointeeType=*/true) /*ForPointeeType=*/true)
.getQuantity(); .getAsMaybeAlign();
// The glvalue must be suitably aligned. // The glvalue must be suitably aligned.
if (AlignVal > 1 && if (AlignVal && *AlignVal > llvm::Align(1) &&
(!PtrToAlloca || PtrToAlloca->getAlignment() < AlignVal)) { (!PtrToAlloca || PtrToAlloca->getAlign() < *AlignVal)) {
PtrAsInt = Builder.CreatePtrToInt(Ptr, IntPtrTy); PtrAsInt = Builder.CreatePtrToInt(Ptr, IntPtrTy);
llvm::Value *Align = Builder.CreateAnd( llvm::Value *Align = Builder.CreateAnd(
PtrAsInt, llvm::ConstantInt::get(IntPtrTy, AlignVal - 1)); PtrAsInt, llvm::ConstantInt::get(IntPtrTy, AlignVal->value() - 1));
llvm::Value *Aligned = llvm::Value *Aligned =
Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0)); Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0));
if (Aligned != True) if (Aligned != True)
@ -782,12 +782,9 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
} }
if (Checks.size() > 0) { if (Checks.size() > 0) {
// Make sure we're not losing information. Alignment needs to be a power of
// 2
assert(!AlignVal || (uint64_t)1 << llvm::Log2_64(AlignVal) == AlignVal);
llvm::Constant *StaticData[] = { llvm::Constant *StaticData[] = {
EmitCheckSourceLocation(Loc), EmitCheckTypeDescriptor(Ty), EmitCheckSourceLocation(Loc), EmitCheckTypeDescriptor(Ty),
llvm::ConstantInt::get(Int8Ty, AlignVal ? llvm::Log2_64(AlignVal) : 1), llvm::ConstantInt::get(Int8Ty, AlignVal ? llvm::Log2(*AlignVal) : 1),
llvm::ConstantInt::get(Int8Ty, TCK)}; llvm::ConstantInt::get(Int8Ty, TCK)};
EmitCheck(Checks, SanitizerHandler::TypeMismatch, StaticData, EmitCheck(Checks, SanitizerHandler::TypeMismatch, StaticData,
PtrAsInt ? PtrAsInt : Ptr); PtrAsInt ? PtrAsInt : Ptr);