Skip to content

Commit 276c8dd

Browse files
committed
[clang][codegen] Refactor argument loading in function prolog. NFC.
Summary: - Skip copying function arguments and unnecessary casting by using them directly. Reviewers: rjmccall, kerbowa, yaxunl Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D79394
1 parent 86e50af commit 276c8dd

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,8 +1016,8 @@ static void forConstantArrayExpansion(CodeGenFunction &CGF,
10161016
}
10171017
}
10181018

1019-
void CodeGenFunction::ExpandTypeFromArgs(
1020-
QualType Ty, LValue LV, SmallVectorImpl<llvm::Value *>::iterator &AI) {
1019+
void CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1020+
llvm::Function::arg_iterator &AI) {
10211021
assert(LV.isSimple() &&
10221022
"Unexpected non-simple lvalue during struct expansion.");
10231023

@@ -1046,17 +1046,17 @@ void CodeGenFunction::ExpandTypeFromArgs(
10461046
ExpandTypeFromArgs(FD->getType(), SubLV, AI);
10471047
}
10481048
} else if (isa<ComplexExpansion>(Exp.get())) {
1049-
auto realValue = *AI++;
1050-
auto imagValue = *AI++;
1049+
auto realValue = &*AI++;
1050+
auto imagValue = &*AI++;
10511051
EmitStoreOfComplex(ComplexPairTy(realValue, imagValue), LV, /*init*/ true);
10521052
} else {
10531053
// Call EmitStoreOfScalar except when the lvalue is a bitfield to emit a
10541054
// primitive store.
10551055
assert(isa<NoExpansion>(Exp.get()));
10561056
if (LV.isBitField())
1057-
EmitStoreThroughLValue(RValue::get(*AI++), LV);
1057+
EmitStoreThroughLValue(RValue::get(&*AI++), LV);
10581058
else
1059-
EmitStoreOfScalar(*AI++, LV);
1059+
EmitStoreOfScalar(&*AI++, LV);
10601060
}
10611061
}
10621062

@@ -2323,27 +2323,21 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
23232323
// simplify.
23242324

23252325
ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), FI);
2326-
// Flattened function arguments.
2327-
SmallVector<llvm::Value *, 16> FnArgs;
2328-
FnArgs.reserve(IRFunctionArgs.totalIRArgs());
2329-
for (auto &Arg : Fn->args()) {
2330-
FnArgs.push_back(&Arg);
2331-
}
2332-
assert(FnArgs.size() == IRFunctionArgs.totalIRArgs());
2326+
assert(Fn->arg_size() == IRFunctionArgs.totalIRArgs());
23332327

23342328
// If we're using inalloca, all the memory arguments are GEPs off of the last
23352329
// parameter, which is a pointer to the complete memory area.
23362330
Address ArgStruct = Address::invalid();
23372331
if (IRFunctionArgs.hasInallocaArg()) {
2338-
ArgStruct = Address(FnArgs[IRFunctionArgs.getInallocaArgNo()],
2332+
ArgStruct = Address(Fn->getArg(IRFunctionArgs.getInallocaArgNo()),
23392333
FI.getArgStructAlignment());
23402334

23412335
assert(ArgStruct.getType() == FI.getArgStruct()->getPointerTo());
23422336
}
23432337

23442338
// Name the struct return parameter.
23452339
if (IRFunctionArgs.hasSRetArg()) {
2346-
auto AI = cast<llvm::Argument>(FnArgs[IRFunctionArgs.getSRetArgNo()]);
2340+
auto AI = Fn->getArg(IRFunctionArgs.getSRetArgNo());
23472341
AI->setName("agg.result");
23482342
AI->addAttr(llvm::Attribute::NoAlias);
23492343
}
@@ -2394,7 +2388,8 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
23942388

23952389
case ABIArgInfo::Indirect: {
23962390
assert(NumIRArgs == 1);
2397-
Address ParamAddr = Address(FnArgs[FirstIRArg], ArgI.getIndirectAlign());
2391+
Address ParamAddr =
2392+
Address(Fn->getArg(FirstIRArg), ArgI.getIndirectAlign());
23982393

23992394
if (!hasScalarEvaluationKind(Ty)) {
24002395
// Aggregates and complex variables are accessed by reference. All we
@@ -2436,8 +2431,7 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
24362431
ArgI.getCoerceToType() == ConvertType(Ty) &&
24372432
ArgI.getDirectOffset() == 0) {
24382433
assert(NumIRArgs == 1);
2439-
llvm::Value *V = FnArgs[FirstIRArg];
2440-
auto AI = cast<llvm::Argument>(V);
2434+
auto AI = Fn->getArg(FirstIRArg);
24412435

24422436
if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(Arg)) {
24432437
if (getNonNullAttr(CurCodeDecl, PVD, PVD->getType(),
@@ -2499,6 +2493,7 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
24992493

25002494
// LLVM expects swifterror parameters to be used in very restricted
25012495
// ways. Copy the value into a less-restricted temporary.
2496+
llvm::Value *V = AI;
25022497
if (FI.getExtParameterInfo(ArgNo).getABI()
25032498
== ParameterABI::SwiftErrorResult) {
25042499
QualType pointeeTy = Ty->getPointeeType();
@@ -2560,7 +2555,7 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
25602555

25612556
assert(STy->getNumElements() == NumIRArgs);
25622557
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
2563-
auto AI = FnArgs[FirstIRArg + i];
2558+
auto AI = Fn->getArg(FirstIRArg + i);
25642559
AI->setName(Arg->getName() + ".coerce" + Twine(i));
25652560
Address EltPtr = Builder.CreateStructGEP(AddrToStoreInto, i);
25662561
Builder.CreateStore(AI, EltPtr);
@@ -2573,7 +2568,7 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
25732568
} else {
25742569
// Simple case, just do a coerced store of the argument into the alloca.
25752570
assert(NumIRArgs == 1);
2576-
auto AI = FnArgs[FirstIRArg];
2571+
auto AI = Fn->getArg(FirstIRArg);
25772572
AI->setName(Arg->getName() + ".coerce");
25782573
CreateCoercedStore(AI, Ptr, /*DstIsVolatile=*/false, *this);
25792574
}
@@ -2606,7 +2601,7 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
26062601
continue;
26072602

26082603
auto eltAddr = Builder.CreateStructGEP(alloca, i);
2609-
auto elt = FnArgs[argIndex++];
2604+
auto elt = Fn->getArg(argIndex++);
26102605
Builder.CreateStore(elt, eltAddr);
26112606
}
26122607
assert(argIndex == FirstIRArg + NumIRArgs);
@@ -2621,11 +2616,11 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
26212616
LValue LV = MakeAddrLValue(Alloca, Ty);
26222617
ArgVals.push_back(ParamValue::forIndirect(Alloca));
26232618

2624-
auto FnArgIter = FnArgs.begin() + FirstIRArg;
2619+
auto FnArgIter = Fn->arg_begin() + FirstIRArg;
26252620
ExpandTypeFromArgs(Ty, LV, FnArgIter);
2626-
assert(FnArgIter == FnArgs.begin() + FirstIRArg + NumIRArgs);
2621+
assert(FnArgIter == Fn->arg_begin() + FirstIRArg + NumIRArgs);
26272622
for (unsigned i = 0, e = NumIRArgs; i != e; ++i) {
2628-
auto AI = FnArgs[FirstIRArg + i];
2623+
auto AI = Fn->getArg(FirstIRArg + i);
26292624
AI->setName(Arg->getName() + "." + Twine(i));
26302625
}
26312626
break;

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4356,7 +4356,7 @@ class CodeGenFunction : public CodeGenTypeCache {
43564356
///
43574357
/// \param AI - The first function argument of the expansion.
43584358
void ExpandTypeFromArgs(QualType Ty, LValue Dst,
4359-
SmallVectorImpl<llvm::Value *>::iterator &AI);
4359+
llvm::Function::arg_iterator &AI);
43604360

43614361
/// ExpandTypeToArgs - Expand an CallArg \arg Arg, with the LLVM type for \arg
43624362
/// Ty, into individual arguments on the provided vector \arg IRCallArgs,

0 commit comments

Comments
 (0)