Skip to content

Commit

Permalink
[clang][codegen] Refactor argument loading in function prolog. NFC.
Browse files Browse the repository at this point in the history
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

Change-Id: I5feb669e1375c9713abd5f18c5fea847695cb4fb
  • Loading branch information
darkbuck authored and mhbliao committed May 15, 2020
1 parent 006c607 commit 9df9777
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
43 changes: 19 additions & 24 deletions clang/lib/CodeGen/CGCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,8 +1015,8 @@ static void forConstantArrayExpansion(CodeGenFunction &CGF,
}
}

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

Expand Down Expand Up @@ -1045,17 +1045,17 @@ void CodeGenFunction::ExpandTypeFromArgs(
ExpandTypeFromArgs(FD->getType(), SubLV, AI);
}
} else if (isa<ComplexExpansion>(Exp.get())) {
auto realValue = *AI++;
auto imagValue = *AI++;
auto realValue = &*AI++;
auto imagValue = &*AI++;
EmitStoreOfComplex(ComplexPairTy(realValue, imagValue), LV, /*init*/ true);
} else {
// Call EmitStoreOfScalar except when the lvalue is a bitfield to emit a
// primitive store.
assert(isa<NoExpansion>(Exp.get()));
if (LV.isBitField())
EmitStoreThroughLValue(RValue::get(*AI++), LV);
EmitStoreThroughLValue(RValue::get(&*AI++), LV);
else
EmitStoreOfScalar(*AI++, LV);
EmitStoreOfScalar(&*AI++, LV);
}
}

Expand Down Expand Up @@ -2322,27 +2322,21 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
// simplify.

ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), FI);
// Flattened function arguments.
SmallVector<llvm::Value *, 16> FnArgs;
FnArgs.reserve(IRFunctionArgs.totalIRArgs());
for (auto &Arg : Fn->args()) {
FnArgs.push_back(&Arg);
}
assert(FnArgs.size() == IRFunctionArgs.totalIRArgs());
assert(Fn->arg_size() == IRFunctionArgs.totalIRArgs());

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

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

// Name the struct return parameter.
if (IRFunctionArgs.hasSRetArg()) {
auto AI = cast<llvm::Argument>(FnArgs[IRFunctionArgs.getSRetArgNo()]);
auto AI = Fn->getArg(IRFunctionArgs.getSRetArgNo());
AI->setName("agg.result");
AI->addAttr(llvm::Attribute::NoAlias);
}
Expand Down Expand Up @@ -2393,7 +2387,8 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,

case ABIArgInfo::Indirect: {
assert(NumIRArgs == 1);
Address ParamAddr = Address(FnArgs[FirstIRArg], ArgI.getIndirectAlign());
Address ParamAddr =
Address(Fn->getArg(FirstIRArg), ArgI.getIndirectAlign());

if (!hasScalarEvaluationKind(Ty)) {
// Aggregates and complex variables are accessed by reference. All we
Expand Down Expand Up @@ -2435,8 +2430,7 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
ArgI.getCoerceToType() == ConvertType(Ty) &&
ArgI.getDirectOffset() == 0) {
assert(NumIRArgs == 1);
llvm::Value *V = FnArgs[FirstIRArg];
auto AI = cast<llvm::Argument>(V);
auto AI = Fn->getArg(FirstIRArg);

if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(Arg)) {
if (getNonNullAttr(CurCodeDecl, PVD, PVD->getType(),
Expand Down Expand Up @@ -2498,6 +2492,7 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,

// LLVM expects swifterror parameters to be used in very restricted
// ways. Copy the value into a less-restricted temporary.
llvm::Value *V = AI;
if (FI.getExtParameterInfo(ArgNo).getABI()
== ParameterABI::SwiftErrorResult) {
QualType pointeeTy = Ty->getPointeeType();
Expand Down Expand Up @@ -2559,7 +2554,7 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,

assert(STy->getNumElements() == NumIRArgs);
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
auto AI = FnArgs[FirstIRArg + i];
auto AI = Fn->getArg(FirstIRArg + i);
AI->setName(Arg->getName() + ".coerce" + Twine(i));
Address EltPtr = Builder.CreateStructGEP(AddrToStoreInto, i);
Builder.CreateStore(AI, EltPtr);
Expand All @@ -2572,7 +2567,7 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
} else {
// Simple case, just do a coerced store of the argument into the alloca.
assert(NumIRArgs == 1);
auto AI = FnArgs[FirstIRArg];
auto AI = Fn->getArg(FirstIRArg);
AI->setName(Arg->getName() + ".coerce");
CreateCoercedStore(AI, Ptr, /*DstIsVolatile=*/false, *this);
}
Expand Down Expand Up @@ -2605,7 +2600,7 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
continue;

auto eltAddr = Builder.CreateStructGEP(alloca, i);
auto elt = FnArgs[argIndex++];
auto elt = Fn->getArg(argIndex++);
Builder.CreateStore(elt, eltAddr);
}
assert(argIndex == FirstIRArg + NumIRArgs);
Expand All @@ -2620,11 +2615,11 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
LValue LV = MakeAddrLValue(Alloca, Ty);
ArgVals.push_back(ParamValue::forIndirect(Alloca));

auto FnArgIter = FnArgs.begin() + FirstIRArg;
auto FnArgIter = Fn->arg_begin() + FirstIRArg;
ExpandTypeFromArgs(Ty, LV, FnArgIter);
assert(FnArgIter == FnArgs.begin() + FirstIRArg + NumIRArgs);
assert(FnArgIter == Fn->arg_begin() + FirstIRArg + NumIRArgs);
for (unsigned i = 0, e = NumIRArgs; i != e; ++i) {
auto AI = FnArgs[FirstIRArg + i];
auto AI = Fn->getArg(FirstIRArg + i);
AI->setName(Arg->getName() + "." + Twine(i));
}
break;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CodeGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -4322,7 +4322,7 @@ class CodeGenFunction : public CodeGenTypeCache {
///
/// \param AI - The first function argument of the expansion.
void ExpandTypeFromArgs(QualType Ty, LValue Dst,
SmallVectorImpl<llvm::Value *>::iterator &AI);
llvm::Function::arg_iterator &AI);

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

0 comments on commit 9df9777

Please sign in to comment.