Skip to content

Commit 6d973b4

Browse files
authored
[clang][CodeGen] Return RValue from EmitVAArg (#94635)
This should simplify handling of resulting value by the callers.
1 parent 52d87de commit 6d973b4

36 files changed

+283
-281
lines changed

clang/lib/CodeGen/ABIInfo.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ bool ABIInfo::isOHOSFamily() const {
3939
return getTarget().getTriple().isOHOSFamily();
4040
}
4141

42-
Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
43-
QualType Ty) const {
44-
return Address::invalid();
42+
RValue ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
43+
QualType Ty, AggValueSlot Slot) const {
44+
return RValue::getIgnored();
4545
}
4646

4747
bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {

clang/lib/CodeGen/ABIInfo.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class CGCXXABI;
3434
class CGFunctionInfo;
3535
class CodeGenFunction;
3636
class CodeGenTypes;
37+
class RValue;
38+
class AggValueSlot;
3739

3840
// FIXME: All of this stuff should be part of the target interface
3941
// somehow. It is currently here because it is not clear how to factor
@@ -75,18 +77,18 @@ class ABIInfo {
7577
// the ABI information any lower than CodeGen. Of course, for
7678
// VAArg handling it has to be at this level; there is no way to
7779
// abstract this out.
78-
virtual CodeGen::Address EmitVAArg(CodeGen::CodeGenFunction &CGF,
79-
CodeGen::Address VAListAddr,
80-
QualType Ty) const = 0;
80+
virtual RValue EmitVAArg(CodeGen::CodeGenFunction &CGF,
81+
CodeGen::Address VAListAddr, QualType Ty,
82+
AggValueSlot Slot) const = 0;
8183

8284
bool isAndroid() const;
8385
bool isOHOSFamily() const;
8486

8587
/// Emit the target dependent code to load a value of
8688
/// \arg Ty from the \c __builtin_ms_va_list pointed to by \arg VAListAddr.
87-
virtual CodeGen::Address EmitMSVAArg(CodeGen::CodeGenFunction &CGF,
88-
CodeGen::Address VAListAddr,
89-
QualType Ty) const;
89+
virtual RValue EmitMSVAArg(CodeGen::CodeGenFunction &CGF,
90+
CodeGen::Address VAListAddr, QualType Ty,
91+
AggValueSlot Slot) const;
9092

9193
virtual bool isHomogeneousAggregateBaseType(QualType Ty) const;
9294

clang/lib/CodeGen/ABIInfoImpl.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,12 @@ void DefaultABIInfo::computeInfo(CGFunctionInfo &FI) const {
7171
I.info = classifyArgumentType(I.type);
7272
}
7373

74-
Address DefaultABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
75-
QualType Ty) const {
76-
return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
74+
RValue DefaultABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
75+
QualType Ty, AggValueSlot Slot) const {
76+
return CGF.EmitLoadOfAnyValue(
77+
CGF.MakeAddrLValue(
78+
EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)), Ty),
79+
Slot);
7780
}
7881

7982
ABIArgInfo CodeGen::coerceToIntArray(QualType Ty, ASTContext &Context,
@@ -199,12 +202,12 @@ CodeGen::emitVoidPtrDirectVAArg(CodeGenFunction &CGF, Address VAListAddr,
199202
return Addr.withElementType(DirectTy);
200203
}
201204

202-
Address CodeGen::emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
203-
QualType ValueTy, bool IsIndirect,
204-
TypeInfoChars ValueInfo,
205-
CharUnits SlotSizeAndAlign,
206-
bool AllowHigherAlign,
207-
bool ForceRightAdjust) {
205+
RValue CodeGen::emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
206+
QualType ValueTy, bool IsIndirect,
207+
TypeInfoChars ValueInfo,
208+
CharUnits SlotSizeAndAlign,
209+
bool AllowHigherAlign, AggValueSlot Slot,
210+
bool ForceRightAdjust) {
208211
// The size and alignment of the value that was passed directly.
209212
CharUnits DirectSize, DirectAlign;
210213
if (IsIndirect) {
@@ -230,7 +233,7 @@ Address CodeGen::emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
230233
Addr = Address(CGF.Builder.CreateLoad(Addr), ElementTy, ValueInfo.Align);
231234
}
232235

233-
return Addr;
236+
return CGF.EmitLoadOfAnyValue(CGF.MakeAddrLValue(Addr, ValueTy), Slot);
234237
}
235238

236239
Address CodeGen::emitMergePHI(CodeGenFunction &CGF, Address Addr1,

clang/lib/CodeGen/ABIInfoImpl.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class DefaultABIInfo : public ABIInfo {
2929

3030
void computeInfo(CGFunctionInfo &FI) const override;
3131

32-
Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
33-
QualType Ty) const override;
32+
RValue EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
33+
AggValueSlot Slot) const override;
3434
};
3535

3636
// Helper for coercing an aggregate argument or return value into an integer
@@ -112,10 +112,11 @@ Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF, Address VAListAddr,
112112
/// \param ForceRightAdjust - Default is false. On big-endian platform and
113113
/// if the argument is smaller than a slot, set this flag will force
114114
/// right-adjust the argument in its slot irrespective of the type.
115-
Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
116-
QualType ValueTy, bool IsIndirect,
117-
TypeInfoChars ValueInfo, CharUnits SlotSizeAndAlign,
118-
bool AllowHigherAlign, bool ForceRightAdjust = false);
115+
RValue emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
116+
QualType ValueTy, bool IsIndirect,
117+
TypeInfoChars ValueInfo, CharUnits SlotSizeAndAlign,
118+
bool AllowHigherAlign, AggValueSlot Slot,
119+
bool ForceRightAdjust = false);
119120

120121
Address emitMergePHI(CodeGenFunction &CGF, Address Addr1,
121122
llvm::BasicBlock *Block1, Address Addr2,

clang/lib/CodeGen/CGCall.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5989,12 +5989,12 @@ CGCallee CGCallee::prepareConcreteCallee(CodeGenFunction &CGF) const {
59895989

59905990
/* VarArg handling */
59915991

5992-
Address CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address &VAListAddr) {
5993-
VAListAddr = VE->isMicrosoftABI()
5994-
? EmitMSVAListRef(VE->getSubExpr())
5995-
: EmitVAListRef(VE->getSubExpr());
5992+
RValue CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address &VAListAddr,
5993+
AggValueSlot Slot) {
5994+
VAListAddr = VE->isMicrosoftABI() ? EmitMSVAListRef(VE->getSubExpr())
5995+
: EmitVAListRef(VE->getSubExpr());
59965996
QualType Ty = VE->getType();
59975997
if (VE->isMicrosoftABI())
5998-
return CGM.getTypes().getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty);
5999-
return CGM.getTypes().getABIInfo().EmitVAArg(*this, VAListAddr, Ty);
5998+
return CGM.getTypes().getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
5999+
return CGM.getTypes().getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
60006000
}

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,6 +2161,21 @@ static RValue EmitLoadOfMatrixLValue(LValue LV, SourceLocation Loc,
21612161
return RValue::get(CGF.EmitLoadOfScalar(LV, Loc));
21622162
}
21632163

2164+
RValue CodeGenFunction::EmitLoadOfAnyValue(LValue LV, AggValueSlot Slot,
2165+
SourceLocation Loc) {
2166+
QualType Ty = LV.getType();
2167+
switch (getEvaluationKind(Ty)) {
2168+
case TEK_Scalar:
2169+
return EmitLoadOfLValue(LV, Loc);
2170+
case TEK_Complex:
2171+
return RValue::getComplex(EmitLoadOfComplex(LV, Loc));
2172+
case TEK_Aggregate:
2173+
EmitAggFinalDestCopy(Ty, Slot, LV, EVK_NonRValue);
2174+
return Slot.asRValue();
2175+
}
2176+
llvm_unreachable("bad evaluation kind");
2177+
}
2178+
21642179
/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
21652180
/// method emits the address of the lvalue, then loads the result as an rvalue,
21662181
/// returning the rvalue.

clang/lib/CodeGen/CGExprAgg.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,11 @@ class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
7878
/// then loads the result into DestPtr.
7979
void EmitAggLoadOfLValue(const Expr *E);
8080

81-
enum ExprValueKind {
82-
EVK_RValue,
83-
EVK_NonRValue
84-
};
85-
8681
/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
8782
/// SrcIsRValue is true if source comes from an RValue.
8883
void EmitFinalDestCopy(QualType type, const LValue &src,
89-
ExprValueKind SrcValueKind = EVK_NonRValue);
84+
CodeGenFunction::ExprValueKind SrcValueKind =
85+
CodeGenFunction::EVK_NonRValue);
9086
void EmitFinalDestCopy(QualType type, RValue src);
9187
void EmitCopy(QualType type, const AggValueSlot &dest,
9288
const AggValueSlot &src);
@@ -348,12 +344,13 @@ void AggExprEmitter::withReturnValueSlot(
348344
void AggExprEmitter::EmitFinalDestCopy(QualType type, RValue src) {
349345
assert(src.isAggregate() && "value must be aggregate value!");
350346
LValue srcLV = CGF.MakeAddrLValue(src.getAggregateAddress(), type);
351-
EmitFinalDestCopy(type, srcLV, EVK_RValue);
347+
EmitFinalDestCopy(type, srcLV, CodeGenFunction::EVK_RValue);
352348
}
353349

354350
/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
355-
void AggExprEmitter::EmitFinalDestCopy(QualType type, const LValue &src,
356-
ExprValueKind SrcValueKind) {
351+
void AggExprEmitter::EmitFinalDestCopy(
352+
QualType type, const LValue &src,
353+
CodeGenFunction::ExprValueKind SrcValueKind) {
357354
// If Dest is ignored, then we're evaluating an aggregate expression
358355
// in a context that doesn't care about the result. Note that loads
359356
// from volatile l-values force the existence of a non-ignored
@@ -365,7 +362,7 @@ void AggExprEmitter::EmitFinalDestCopy(QualType type, const LValue &src,
365362
LValue DstLV = CGF.MakeAddrLValue(
366363
Dest.getAddress(), Dest.isVolatile() ? type.withVolatile() : type);
367364

368-
if (SrcValueKind == EVK_RValue) {
365+
if (SrcValueKind == CodeGenFunction::EVK_RValue) {
369366
if (type.isNonTrivialToPrimitiveDestructiveMove() == QualType::PCK_Struct) {
370367
if (Dest.isPotentiallyAliased())
371368
CGF.callCStructMoveAssignmentOperator(DstLV, src);
@@ -1317,15 +1314,13 @@ void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
13171314

13181315
void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
13191316
Address ArgValue = Address::invalid();
1320-
Address ArgPtr = CGF.EmitVAArg(VE, ArgValue);
1317+
CGF.EmitVAArg(VE, ArgValue, Dest);
13211318

13221319
// If EmitVAArg fails, emit an error.
1323-
if (!ArgPtr.isValid()) {
1320+
if (!ArgValue.isValid()) {
13241321
CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
13251322
return;
13261323
}
1327-
1328-
EmitFinalDestCopy(VE->getType(), CGF.MakeAddrLValue(ArgPtr, VE->getType()));
13291324
}
13301325

13311326
void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
@@ -2027,6 +2022,13 @@ LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) {
20272022
return LV;
20282023
}
20292024

2025+
void CodeGenFunction::EmitAggFinalDestCopy(QualType Type, AggValueSlot Dest,
2026+
const LValue &Src,
2027+
ExprValueKind SrcKind) {
2028+
return AggExprEmitter(*this, Dest, Dest.isIgnored())
2029+
.EmitFinalDestCopy(Type, Src, SrcKind);
2030+
}
2031+
20302032
AggValueSlot::Overlap_t
20312033
CodeGenFunction::getOverlapForFieldInit(const FieldDecl *FD) {
20322034
if (!FD->hasAttr<NoUniqueAddressAttr>() || !FD->getType()->isRecordType())

clang/lib/CodeGen/CGExprComplex.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,18 +1448,17 @@ ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
14481448

14491449
ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) {
14501450
Address ArgValue = Address::invalid();
1451-
Address ArgPtr = CGF.EmitVAArg(E, ArgValue);
1451+
RValue RV = CGF.EmitVAArg(E, ArgValue);
14521452

1453-
if (!ArgPtr.isValid()) {
1453+
if (!ArgValue.isValid()) {
14541454
CGF.ErrorUnsupported(E, "complex va_arg expression");
14551455
llvm::Type *EltTy =
14561456
CGF.ConvertType(E->getType()->castAs<ComplexType>()->getElementType());
14571457
llvm::Value *U = llvm::UndefValue::get(EltTy);
14581458
return ComplexPairTy(U, U);
14591459
}
14601460

1461-
return EmitLoadOfLValue(CGF.MakeAddrLValue(ArgPtr, E->getType()),
1462-
E->getExprLoc());
1461+
return RV.getComplexVal();
14631462
}
14641463

14651464
//===----------------------------------------------------------------------===//

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5357,28 +5357,9 @@ Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
53575357
CGF.EmitVariablyModifiedType(Ty);
53585358

53595359
Address ArgValue = Address::invalid();
5360-
Address ArgPtr = CGF.EmitVAArg(VE, ArgValue);
5360+
RValue ArgPtr = CGF.EmitVAArg(VE, ArgValue);
53615361

5362-
llvm::Type *ArgTy = ConvertType(VE->getType());
5363-
5364-
// If EmitVAArg fails, emit an error.
5365-
if (!ArgPtr.isValid()) {
5366-
CGF.ErrorUnsupported(VE, "va_arg expression");
5367-
return llvm::UndefValue::get(ArgTy);
5368-
}
5369-
5370-
// FIXME Volatility.
5371-
llvm::Value *Val = Builder.CreateLoad(ArgPtr);
5372-
5373-
// If EmitVAArg promoted the type, we must truncate it.
5374-
if (ArgTy != Val->getType()) {
5375-
if (ArgTy->isPointerTy() && !Val->getType()->isPointerTy())
5376-
Val = Builder.CreateIntToPtr(Val, ArgTy);
5377-
else
5378-
Val = Builder.CreateTrunc(Val, ArgTy);
5379-
}
5380-
5381-
return Val;
5362+
return ArgPtr.getScalarVal();
53825363
}
53835364

53845365
Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *block) {

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3014,7 +3014,8 @@ class CodeGenFunction : public CodeGenTypeCache {
30143014
/// \returns A pointer to the argument.
30153015
// FIXME: We should be able to get rid of this method and use the va_arg
30163016
// instruction in LLVM instead once it works well enough.
3017-
Address EmitVAArg(VAArgExpr *VE, Address &VAListAddr);
3017+
RValue EmitVAArg(VAArgExpr *VE, Address &VAListAddr,
3018+
AggValueSlot Slot = AggValueSlot::ignored());
30183019

30193020
/// emitArrayLength - Compute the length of an array, even if it's a
30203021
/// VLA, and drill down to the base element type.
@@ -4215,6 +4216,11 @@ class CodeGenFunction : public CodeGenTypeCache {
42154216
RValue EmitLoadOfBitfieldLValue(LValue LV, SourceLocation Loc);
42164217
RValue EmitLoadOfGlobalRegLValue(LValue LV);
42174218

4219+
/// Like EmitLoadOfLValue but also handles complex and aggregate types.
4220+
RValue EmitLoadOfAnyValue(LValue V,
4221+
AggValueSlot Slot = AggValueSlot::ignored(),
4222+
SourceLocation Loc = {});
4223+
42184224
/// EmitStoreThroughLValue - Store the specified rvalue into the specified
42194225
/// lvalue, where both are guaranteed to the have the same type, and that type
42204226
/// is 'Ty'.
@@ -4775,6 +4781,13 @@ class CodeGenFunction : public CodeGenTypeCache {
47754781
/// aggregate type into a temporary LValue.
47764782
LValue EmitAggExprToLValue(const Expr *E);
47774783

4784+
enum ExprValueKind { EVK_RValue, EVK_NonRValue };
4785+
4786+
/// EmitAggFinalDestCopy - Emit copy of the specified aggregate into
4787+
/// destination address.
4788+
void EmitAggFinalDestCopy(QualType Type, AggValueSlot Dest, const LValue &Src,
4789+
ExprValueKind SrcKind);
4790+
47784791
/// Build all the stores needed to initialize an aggregate at Dest with the
47794792
/// value Val.
47804793
void EmitAggregateStore(llvm::Value *Val, Address Dest, bool DestIsVolatile);

0 commit comments

Comments
 (0)