Skip to content

Commit

Permalink
[CodeGen] Avoid more pointer element type accesses
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Dec 16, 2021
1 parent 8c7f2a4 commit 58c8c53
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 21 deletions.
3 changes: 1 addition & 2 deletions clang/lib/CodeGen/CGCall.cpp
Expand Up @@ -4964,8 +4964,7 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Builder.CreateMemCpy(TempAlloca, Src, SrcSize);
Src = TempAlloca;
} else {
Src = Builder.CreateBitCast(Src,
STy->getPointerTo(Src.getAddressSpace()));
Src = Builder.CreateElementBitCast(Src, STy);
}

assert(NumIRArgs == STy->getNumElements());
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/CodeGen/CGDecl.cpp
Expand Up @@ -405,7 +405,8 @@ void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D,

// Store into LocalDeclMap before generating initializer to handle
// circular references.
setAddrOfLocalVar(&D, Address(addr, alignment));
setAddrOfLocalVar(
&D, Address(addr, ConvertTypeForMem(D.getType()), alignment));

// We can't have a VLA here, but we can have a pointer to a VLA,
// even though that doesn't really make any sense.
Expand Down
23 changes: 13 additions & 10 deletions clang/lib/CodeGen/CGExpr.cpp
Expand Up @@ -1099,7 +1099,7 @@ Address CodeGenFunction::EmitPointerWithAlignment(const Expr *E,
if (InnerBaseInfo.getAlignmentSource() != AlignmentSource::Decl) {
if (BaseInfo)
BaseInfo->mergeForCast(TargetTypeBaseInfo);
Addr = Address(Addr.getPointer(), Align);
Addr = Address(Addr.getPointer(), Addr.getElementType(), Align);
}
}

Expand All @@ -1111,10 +1111,12 @@ Address CodeGenFunction::EmitPointerWithAlignment(const Expr *E,
CodeGenFunction::CFITCK_UnrelatedCast,
CE->getBeginLoc());
}
return CE->getCastKind() != CK_AddressSpaceConversion
? Builder.CreateBitCast(Addr, ConvertType(E->getType()))
: Builder.CreateAddrSpaceCast(Addr,
ConvertType(E->getType()));

if (CE->getCastKind() == CK_AddressSpaceConversion)
return Builder.CreateAddrSpaceCast(Addr, ConvertType(E->getType()));

llvm::Type *ElemTy = ConvertTypeForMem(E->getType()->getPointeeType());
return Builder.CreateElementBitCast(Addr, ElemTy);
}
break;

Expand Down Expand Up @@ -2527,7 +2529,7 @@ static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(VD->getType());
V = EmitBitCastOfLValueToProperType(CGF, V, RealVarTy);
CharUnits Alignment = CGF.getContext().getDeclAlign(VD);
Address Addr(V, Alignment);
Address Addr(V, RealVarTy, Alignment);
// Emit reference to the private copy of the variable if it is an OpenMP
// threadprivate variable.
if (CGF.getLangOpts().OpenMP && !CGF.getLangOpts().OpenMPSimd &&
Expand Down Expand Up @@ -2705,7 +2707,7 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
/* BaseInfo= */ nullptr,
/* TBAAInfo= */ nullptr,
/* forPointeeType= */ true);
Addr = Address(Val, Alignment);
Addr = Address(Val, ConvertTypeForMem(E->getType()), Alignment);
}
return MakeAddrLValue(Addr, T, AlignmentSource::Decl);
}
Expand Down Expand Up @@ -2782,9 +2784,10 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
// Otherwise, it might be static local we haven't emitted yet for
// some reason; most likely, because it's in an outer function.
} else if (VD->isStaticLocal()) {
addr = Address(CGM.getOrCreateStaticVarDecl(
*VD, CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false)),
getContext().getDeclAlign(VD));
llvm::Constant *var = CGM.getOrCreateStaticVarDecl(
*VD, CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false));
addr = Address(
var, ConvertTypeForMem(VD->getType()), getContext().getDeclAlign(VD));

// No other cases for now.
} else {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGExprScalar.cpp
Expand Up @@ -2667,7 +2667,7 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
// For everything else, we can just do a simple increment.
} else {
llvm::Value *amt = Builder.getInt32(amount);
llvm::Type *elemTy = value->getType()->getPointerElementType();
llvm::Type *elemTy = CGF.ConvertTypeForMem(type);
if (CGF.getLangOpts().isSignedOverflowDefined())
value = Builder.CreateGEP(elemTy, value, amt, "incdec.ptr");
else
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/CodeGen/CodeGenFunction.h
Expand Up @@ -2494,8 +2494,9 @@ class CodeGenFunction : public CodeGenTypeCache {

LValue MakeAddrLValue(llvm::Value *V, QualType T, CharUnits Alignment,
AlignmentSource Source = AlignmentSource::Type) {
return LValue::MakeAddr(Address(V, Alignment), T, getContext(),
LValueBaseInfo(Source), CGM.getTBAAAccessInfo(T));
Address Addr(V, ConvertTypeForMem(T), Alignment);
return LValue::MakeAddr(Addr, T, getContext(), LValueBaseInfo(Source),
CGM.getTBAAAccessInfo(T));
}

LValue
Expand Down
10 changes: 5 additions & 5 deletions clang/lib/CodeGen/TargetInfo.cpp
Expand Up @@ -431,7 +431,7 @@ static Address emitMergePHI(CodeGenFunction &CGF,
PHI->addIncoming(Addr1.getPointer(), Block1);
PHI->addIncoming(Addr2.getPointer(), Block2);
CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment());
return Address(PHI, Align);
return Address(PHI, Addr1.getElementType(), Align);
}

TargetCodeGenInfo::~TargetCodeGenInfo() = default;
Expand Down Expand Up @@ -4034,7 +4034,7 @@ static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);

// AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
return Address(Res, Align);
return Address(Res, LTy, Align);
}

Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
Expand Down Expand Up @@ -4147,7 +4147,7 @@ Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
} else if (neededInt) {
RegAddr = Address(CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, gp_offset),
CharUnits::fromQuantity(8));
CGF.Int8Ty, CharUnits::fromQuantity(8));
RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);

// Copy to a temporary if necessary to ensure the appropriate alignment.
Expand All @@ -4165,7 +4165,7 @@ Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,

} else if (neededSSE == 1) {
RegAddr = Address(CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, fp_offset),
CharUnits::fromQuantity(16));
CGF.Int8Ty, CharUnits::fromQuantity(16));
RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
} else {
assert(neededSSE == 2 && "Invalid number of needed registers!");
Expand All @@ -4177,7 +4177,7 @@ Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
// all the SSE registers to the RSA.
Address RegAddrLo = Address(CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea,
fp_offset),
CharUnits::fromQuantity(16));
CGF.Int8Ty, CharUnits::fromQuantity(16));
Address RegAddrHi =
CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
CharUnits::fromQuantity(16));
Expand Down

0 comments on commit 58c8c53

Please sign in to comment.