2017-02-02 06:56:06 +08:00
|
|
|
//===--- AArch64CallLowering.cpp - Call lowering --------------------------===//
|
2016-02-17 03:26:02 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// This file implements the lowering of LLVM calls to machine code calls for
|
|
|
|
/// GlobalISel.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AArch64CallLowering.h"
|
|
|
|
#include "AArch64ISelLowering.h"
|
2017-02-09 01:57:27 +08:00
|
|
|
#include "AArch64MachineFunctionInfo.h"
|
|
|
|
#include "AArch64Subtarget.h"
|
2017-02-02 06:56:06 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2016-09-20 23:20:36 +08:00
|
|
|
#include "llvm/CodeGen/Analysis.h"
|
2017-02-02 06:56:06 +08:00
|
|
|
#include "llvm/CodeGen/CallingConvLower.h"
|
2016-12-23 05:56:31 +08:00
|
|
|
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
|
|
|
|
#include "llvm/CodeGen/GlobalISel/Utils.h"
|
2017-02-02 06:56:06 +08:00
|
|
|
#include "llvm/CodeGen/LowLevelType.h"
|
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2016-02-17 03:26:02 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2017-02-02 06:56:06 +08:00
|
|
|
#include "llvm/CodeGen/MachineMemOperand.h"
|
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2016-09-20 23:20:36 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2018-03-30 01:21:10 +08:00
|
|
|
#include "llvm/CodeGen/ValueTypes.h"
|
2017-02-02 06:56:06 +08:00
|
|
|
#include "llvm/IR/Argument.h"
|
|
|
|
#include "llvm/IR/Attributes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
2018-03-24 07:58:25 +08:00
|
|
|
#include "llvm/Support/MachineValueType.h"
|
2017-02-02 06:56:06 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iterator>
|
|
|
|
|
2016-02-17 03:26:02 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
AArch64CallLowering::AArch64CallLowering(const AArch64TargetLowering &TLI)
|
2017-02-02 06:56:06 +08:00
|
|
|
: CallLowering(&TLI) {}
|
2016-02-17 03:26:02 +08:00
|
|
|
|
2017-08-20 21:03:48 +08:00
|
|
|
namespace {
|
2016-12-05 18:40:33 +08:00
|
|
|
struct IncomingArgHandler : public CallLowering::ValueHandler {
|
2017-01-18 06:30:10 +08:00
|
|
|
IncomingArgHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
|
|
|
|
CCAssignFn *AssignFn)
|
2017-02-09 01:57:27 +08:00
|
|
|
: ValueHandler(MIRBuilder, MRI, AssignFn), StackUsed(0) {}
|
2016-09-22 21:49:25 +08:00
|
|
|
|
|
|
|
unsigned getStackAddress(uint64_t Size, int64_t Offset,
|
|
|
|
MachinePointerInfo &MPO) override {
|
|
|
|
auto &MFI = MIRBuilder.getMF().getFrameInfo();
|
|
|
|
int FI = MFI.CreateFixedObject(Size, Offset, true);
|
|
|
|
MPO = MachinePointerInfo::getFixedStack(MIRBuilder.getMF(), FI);
|
|
|
|
unsigned AddrReg = MRI.createGenericVirtualRegister(LLT::pointer(0, 64));
|
|
|
|
MIRBuilder.buildFrameIndex(AddrReg, FI);
|
2017-02-09 01:57:27 +08:00
|
|
|
StackUsed = std::max(StackUsed, Size + Offset);
|
2016-09-22 21:49:25 +08:00
|
|
|
return AddrReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void assignValueToReg(unsigned ValVReg, unsigned PhysReg,
|
|
|
|
CCValAssign &VA) override {
|
|
|
|
markPhysRegUsed(PhysReg);
|
2017-10-10 04:07:43 +08:00
|
|
|
switch (VA.getLocInfo()) {
|
|
|
|
default:
|
|
|
|
MIRBuilder.buildCopy(ValVReg, PhysReg);
|
|
|
|
break;
|
|
|
|
case CCValAssign::LocInfo::SExt:
|
|
|
|
case CCValAssign::LocInfo::ZExt:
|
|
|
|
case CCValAssign::LocInfo::AExt: {
|
|
|
|
auto Copy = MIRBuilder.buildCopy(LLT{VA.getLocVT()}, PhysReg);
|
|
|
|
MIRBuilder.buildTrunc(ValVReg, Copy);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-09-22 21:49:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void assignValueToAddress(unsigned ValVReg, unsigned Addr, uint64_t Size,
|
|
|
|
MachinePointerInfo &MPO, CCValAssign &VA) override {
|
|
|
|
auto MMO = MIRBuilder.getMF().getMachineMemOperand(
|
|
|
|
MPO, MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant, Size,
|
|
|
|
0);
|
|
|
|
MIRBuilder.buildLoad(ValVReg, Addr, *MMO);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// How the physical register gets marked varies between formal
|
|
|
|
/// parameters (it's a basic-block live-in), and a call instruction
|
|
|
|
/// (it's an implicit-def of the BL).
|
|
|
|
virtual void markPhysRegUsed(unsigned PhysReg) = 0;
|
2017-02-09 01:57:27 +08:00
|
|
|
|
|
|
|
uint64_t StackUsed;
|
2016-09-22 21:49:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct FormalArgHandler : public IncomingArgHandler {
|
2017-01-18 06:30:10 +08:00
|
|
|
FormalArgHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
|
|
|
|
CCAssignFn *AssignFn)
|
|
|
|
: IncomingArgHandler(MIRBuilder, MRI, AssignFn) {}
|
2016-09-22 21:49:25 +08:00
|
|
|
|
|
|
|
void markPhysRegUsed(unsigned PhysReg) override {
|
|
|
|
MIRBuilder.getMBB().addLiveIn(PhysReg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CallReturnHandler : public IncomingArgHandler {
|
|
|
|
CallReturnHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
|
2017-01-18 06:30:10 +08:00
|
|
|
MachineInstrBuilder MIB, CCAssignFn *AssignFn)
|
|
|
|
: IncomingArgHandler(MIRBuilder, MRI, AssignFn), MIB(MIB) {}
|
2016-09-22 21:49:25 +08:00
|
|
|
|
|
|
|
void markPhysRegUsed(unsigned PhysReg) override {
|
|
|
|
MIB.addDef(PhysReg, RegState::Implicit);
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstrBuilder MIB;
|
|
|
|
};
|
|
|
|
|
2016-12-05 18:40:33 +08:00
|
|
|
struct OutgoingArgHandler : public CallLowering::ValueHandler {
|
2016-09-22 21:49:25 +08:00
|
|
|
OutgoingArgHandler(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
|
2017-01-18 06:30:10 +08:00
|
|
|
MachineInstrBuilder MIB, CCAssignFn *AssignFn,
|
|
|
|
CCAssignFn *AssignFnVarArg)
|
|
|
|
: ValueHandler(MIRBuilder, MRI, AssignFn), MIB(MIB),
|
2017-01-18 06:43:34 +08:00
|
|
|
AssignFnVarArg(AssignFnVarArg), StackSize(0) {}
|
2016-09-22 21:49:25 +08:00
|
|
|
|
|
|
|
unsigned getStackAddress(uint64_t Size, int64_t Offset,
|
|
|
|
MachinePointerInfo &MPO) override {
|
|
|
|
LLT p0 = LLT::pointer(0, 64);
|
|
|
|
LLT s64 = LLT::scalar(64);
|
|
|
|
unsigned SPReg = MRI.createGenericVirtualRegister(p0);
|
|
|
|
MIRBuilder.buildCopy(SPReg, AArch64::SP);
|
|
|
|
|
|
|
|
unsigned OffsetReg = MRI.createGenericVirtualRegister(s64);
|
|
|
|
MIRBuilder.buildConstant(OffsetReg, Offset);
|
|
|
|
|
|
|
|
unsigned AddrReg = MRI.createGenericVirtualRegister(p0);
|
|
|
|
MIRBuilder.buildGEP(AddrReg, SPReg, OffsetReg);
|
|
|
|
|
|
|
|
MPO = MachinePointerInfo::getStack(MIRBuilder.getMF(), Offset);
|
|
|
|
return AddrReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void assignValueToReg(unsigned ValVReg, unsigned PhysReg,
|
|
|
|
CCValAssign &VA) override {
|
|
|
|
MIB.addUse(PhysReg, RegState::Implicit);
|
|
|
|
unsigned ExtReg = extendRegister(ValVReg, VA);
|
|
|
|
MIRBuilder.buildCopy(PhysReg, ExtReg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void assignValueToAddress(unsigned ValVReg, unsigned Addr, uint64_t Size,
|
|
|
|
MachinePointerInfo &MPO, CCValAssign &VA) override {
|
|
|
|
auto MMO = MIRBuilder.getMF().getMachineMemOperand(
|
|
|
|
MPO, MachineMemOperand::MOStore, Size, 0);
|
|
|
|
MIRBuilder.buildStore(ValVReg, Addr, *MMO);
|
|
|
|
}
|
|
|
|
|
2017-02-02 06:56:06 +08:00
|
|
|
bool assignArg(unsigned ValNo, MVT ValVT, MVT LocVT,
|
|
|
|
CCValAssign::LocInfo LocInfo,
|
|
|
|
const CallLowering::ArgInfo &Info,
|
|
|
|
CCState &State) override {
|
2017-03-02 23:34:18 +08:00
|
|
|
bool Res;
|
2017-01-18 06:30:10 +08:00
|
|
|
if (Info.IsFixed)
|
2017-03-02 23:34:18 +08:00
|
|
|
Res = AssignFn(ValNo, ValVT, LocVT, LocInfo, Info.Flags, State);
|
|
|
|
else
|
|
|
|
Res = AssignFnVarArg(ValNo, ValVT, LocVT, LocInfo, Info.Flags, State);
|
|
|
|
|
|
|
|
StackSize = State.getNextStackOffset();
|
|
|
|
return Res;
|
2017-01-18 06:30:10 +08:00
|
|
|
}
|
|
|
|
|
2016-09-22 21:49:25 +08:00
|
|
|
MachineInstrBuilder MIB;
|
2017-01-18 06:30:10 +08:00
|
|
|
CCAssignFn *AssignFnVarArg;
|
2017-01-18 06:43:34 +08:00
|
|
|
uint64_t StackSize;
|
2016-09-22 21:49:25 +08:00
|
|
|
};
|
2017-08-20 21:03:48 +08:00
|
|
|
} // namespace
|
2016-09-22 21:49:25 +08:00
|
|
|
|
2017-01-13 22:39:03 +08:00
|
|
|
void AArch64CallLowering::splitToValueTypes(
|
|
|
|
const ArgInfo &OrigArg, SmallVectorImpl<ArgInfo> &SplitArgs,
|
2017-08-22 05:56:11 +08:00
|
|
|
const DataLayout &DL, MachineRegisterInfo &MRI, CallingConv::ID CallConv,
|
2017-01-13 22:39:03 +08:00
|
|
|
const SplitArgTy &PerformArgSplit) const {
|
2016-09-20 23:20:36 +08:00
|
|
|
const AArch64TargetLowering &TLI = *getTLI<AArch64TargetLowering>();
|
2016-09-21 20:57:45 +08:00
|
|
|
LLVMContext &Ctx = OrigArg.Ty->getContext();
|
2016-09-20 23:20:36 +08:00
|
|
|
|
|
|
|
SmallVector<EVT, 4> SplitVTs;
|
|
|
|
SmallVector<uint64_t, 4> Offsets;
|
2016-09-21 20:57:45 +08:00
|
|
|
ComputeValueVTs(TLI, DL, OrigArg.Ty, SplitVTs, &Offsets, 0);
|
2016-09-20 23:20:36 +08:00
|
|
|
|
|
|
|
if (SplitVTs.size() == 1) {
|
2016-12-06 05:25:33 +08:00
|
|
|
// No splitting to do, but we want to replace the original type (e.g. [1 x
|
|
|
|
// double] -> double).
|
|
|
|
SplitArgs.emplace_back(OrigArg.Reg, SplitVTs[0].getTypeForEVT(Ctx),
|
2017-01-18 06:30:10 +08:00
|
|
|
OrigArg.Flags, OrigArg.IsFixed);
|
2016-09-20 23:20:36 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-21 20:57:45 +08:00
|
|
|
unsigned FirstRegIdx = SplitArgs.size();
|
2017-08-22 05:56:11 +08:00
|
|
|
bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters(
|
|
|
|
OrigArg.Ty, CallConv, false);
|
2016-09-20 23:20:36 +08:00
|
|
|
for (auto SplitVT : SplitVTs) {
|
|
|
|
Type *SplitTy = SplitVT.getTypeForEVT(Ctx);
|
2016-09-21 20:57:45 +08:00
|
|
|
SplitArgs.push_back(
|
Recommit: [globalisel] Change LLT constructor string into an LLT-based object that knows how to generate it.
Summary:
This will allow future patches to inspect the details of the LLT. The implementation is now split between
the Support and CodeGen libraries to allow TableGen to use this class without introducing layering concerns.
Thanks to Ahmed Bougacha for finding a reasonable way to avoid the layering issue and providing the version of this patch without that problem.
The problem with the previous commit appears to have been that TableGen was including CodeGen/LowLevelType.h instead of Support/LowLevelTypeImpl.h.
Reviewers: t.p.northover, qcolombet, rovka, aditya_nandakumar, ab, javed.absar
Subscribers: arsenm, nhaehnle, mgorny, dberris, llvm-commits, kristof.beyls
Differential Revision: https://reviews.llvm.org/D30046
llvm-svn: 297241
2017-03-08 07:20:35 +08:00
|
|
|
ArgInfo{MRI.createGenericVirtualRegister(getLLTForType(*SplitTy, DL)),
|
|
|
|
SplitTy, OrigArg.Flags, OrigArg.IsFixed});
|
2017-08-22 05:56:11 +08:00
|
|
|
if (NeedsRegBlock)
|
|
|
|
SplitArgs.back().Flags.setInConsecutiveRegs();
|
2016-09-20 23:20:36 +08:00
|
|
|
}
|
|
|
|
|
2017-08-22 05:56:11 +08:00
|
|
|
SplitArgs.back().Flags.setInConsecutiveRegsLast();
|
|
|
|
|
2017-03-07 07:50:28 +08:00
|
|
|
for (unsigned i = 0; i < Offsets.size(); ++i)
|
|
|
|
PerformArgSplit(SplitArgs[FirstRegIdx + i].Reg, Offsets[i] * 8);
|
2016-09-21 20:57:45 +08:00
|
|
|
}
|
|
|
|
|
2016-09-20 23:20:36 +08:00
|
|
|
bool AArch64CallLowering::lowerReturn(MachineIRBuilder &MIRBuilder,
|
|
|
|
const Value *Val, unsigned VReg) const {
|
|
|
|
MachineFunction &MF = MIRBuilder.getMF();
|
2017-12-16 06:22:58 +08:00
|
|
|
const Function &F = MF.getFunction();
|
2016-09-20 23:20:36 +08:00
|
|
|
|
2016-12-08 05:05:38 +08:00
|
|
|
auto MIB = MIRBuilder.buildInstrNoInsert(AArch64::RET_ReallyLR);
|
2016-09-20 23:20:36 +08:00
|
|
|
assert(((Val && VReg) || (!Val && !VReg)) && "Return value without a vreg");
|
2016-12-08 05:05:38 +08:00
|
|
|
bool Success = true;
|
2016-09-20 23:20:36 +08:00
|
|
|
if (VReg) {
|
|
|
|
const AArch64TargetLowering &TLI = *getTLI<AArch64TargetLowering>();
|
|
|
|
CCAssignFn *AssignFn = TLI.CCAssignFnForReturn(F.getCallingConv());
|
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
auto &DL = F.getParent()->getDataLayout();
|
|
|
|
|
2016-09-21 20:57:45 +08:00
|
|
|
ArgInfo OrigArg{VReg, Val->getType()};
|
Rename AttributeSet to AttributeList
Summary:
This class is a list of AttributeSetNodes corresponding the function
prototype of a call or function declaration. This class used to be
called ParamAttrListPtr, then AttrListPtr, then AttributeSet. It is
typically accessed by parameter and return value index, so
"AttributeList" seems like a more intuitive name.
Rename AttributeSetImpl to AttributeListImpl to follow suit.
It's useful to rename this class so that we can rename AttributeSetNode
to AttributeSet later. AttributeSet is the set of attributes that apply
to a single function, argument, or return value.
Reviewers: sanjoy, javed.absar, chandlerc, pete
Reviewed By: pete
Subscribers: pete, jholewinski, arsenm, dschuff, mehdi_amini, jfb, nhaehnle, sbc100, void, llvm-commits
Differential Revision: https://reviews.llvm.org/D31102
llvm-svn: 298393
2017-03-22 00:57:19 +08:00
|
|
|
setArgFlags(OrigArg, AttributeList::ReturnIndex, DL, F);
|
2016-09-21 20:57:45 +08:00
|
|
|
|
|
|
|
SmallVector<ArgInfo, 8> SplitArgs;
|
2017-08-22 05:56:11 +08:00
|
|
|
splitToValueTypes(OrigArg, SplitArgs, DL, MRI, F.getCallingConv(),
|
2017-03-07 07:50:28 +08:00
|
|
|
[&](unsigned Reg, uint64_t Offset) {
|
|
|
|
MIRBuilder.buildExtract(Reg, VReg, Offset);
|
2016-09-20 23:20:36 +08:00
|
|
|
});
|
|
|
|
|
2017-01-18 06:30:10 +08:00
|
|
|
OutgoingArgHandler Handler(MIRBuilder, MRI, MIB, AssignFn, AssignFn);
|
|
|
|
Success = handleAssignments(MIRBuilder, SplitArgs, Handler);
|
2016-09-20 23:20:36 +08:00
|
|
|
}
|
2016-12-08 05:05:38 +08:00
|
|
|
|
|
|
|
MIRBuilder.insertInstr(MIB);
|
|
|
|
return Success;
|
2016-09-20 23:20:36 +08:00
|
|
|
}
|
|
|
|
|
2016-09-21 20:57:35 +08:00
|
|
|
bool AArch64CallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder,
|
|
|
|
const Function &F,
|
|
|
|
ArrayRef<unsigned> VRegs) const {
|
2016-08-11 05:44:01 +08:00
|
|
|
MachineFunction &MF = MIRBuilder.getMF();
|
2016-09-20 23:20:36 +08:00
|
|
|
MachineBasicBlock &MBB = MIRBuilder.getMBB();
|
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
auto &DL = F.getParent()->getDataLayout();
|
|
|
|
|
2016-09-21 20:57:45 +08:00
|
|
|
SmallVector<ArgInfo, 8> SplitArgs;
|
2016-09-20 23:20:36 +08:00
|
|
|
unsigned i = 0;
|
Remove getArgumentList() in favor of arg_begin(), args(), etc
Users often call getArgumentList().size(), which is a linear way to get
the number of function arguments. arg_size(), on the other hand, is
constant time.
In general, the fact that arguments are stored in an iplist is an
implementation detail, so I've removed it from the Function interface
and moved all other users to the argument container APIs (arg_begin(),
arg_end(), args(), arg_size()).
Reviewed By: chandlerc
Differential Revision: https://reviews.llvm.org/D31052
llvm-svn: 298010
2017-03-17 06:59:15 +08:00
|
|
|
for (auto &Arg : F.args()) {
|
2017-12-01 04:06:02 +08:00
|
|
|
if (DL.getTypeStoreSize(Arg.getType()) == 0)
|
|
|
|
continue;
|
2016-09-21 20:57:45 +08:00
|
|
|
ArgInfo OrigArg{VRegs[i], Arg.getType()};
|
2017-05-04 02:17:31 +08:00
|
|
|
setArgFlags(OrigArg, i + AttributeList::FirstArgIndex, DL, F);
|
2017-03-07 07:50:28 +08:00
|
|
|
bool Split = false;
|
|
|
|
LLT Ty = MRI.getType(VRegs[i]);
|
|
|
|
unsigned Dst = VRegs[i];
|
|
|
|
|
2017-08-22 05:56:11 +08:00
|
|
|
splitToValueTypes(OrigArg, SplitArgs, DL, MRI, F.getCallingConv(),
|
2017-03-07 07:50:28 +08:00
|
|
|
[&](unsigned Reg, uint64_t Offset) {
|
|
|
|
if (!Split) {
|
|
|
|
Split = true;
|
|
|
|
Dst = MRI.createGenericVirtualRegister(Ty);
|
|
|
|
MIRBuilder.buildUndef(Dst);
|
|
|
|
}
|
|
|
|
unsigned Tmp = MRI.createGenericVirtualRegister(Ty);
|
|
|
|
MIRBuilder.buildInsert(Tmp, Dst, Reg, Offset);
|
|
|
|
Dst = Tmp;
|
2016-09-20 23:20:36 +08:00
|
|
|
});
|
2017-03-07 07:50:28 +08:00
|
|
|
|
|
|
|
if (Dst != VRegs[i])
|
|
|
|
MIRBuilder.buildCopy(VRegs[i], Dst);
|
2016-09-20 23:20:36 +08:00
|
|
|
++i;
|
|
|
|
}
|
2016-08-11 05:44:01 +08:00
|
|
|
|
2016-09-20 23:20:36 +08:00
|
|
|
if (!MBB.empty())
|
|
|
|
MIRBuilder.setInstr(*MBB.begin());
|
2016-08-11 05:44:01 +08:00
|
|
|
|
|
|
|
const AArch64TargetLowering &TLI = *getTLI<AArch64TargetLowering>();
|
|
|
|
CCAssignFn *AssignFn =
|
|
|
|
TLI.CCAssignFnForCall(F.getCallingConv(), /*IsVarArg=*/false);
|
|
|
|
|
2017-01-18 06:30:10 +08:00
|
|
|
FormalArgHandler Handler(MIRBuilder, MRI, AssignFn);
|
|
|
|
if (!handleAssignments(MIRBuilder, SplitArgs, Handler))
|
2016-09-21 20:57:45 +08:00
|
|
|
return false;
|
2016-09-20 23:20:36 +08:00
|
|
|
|
2017-02-09 01:57:27 +08:00
|
|
|
if (F.isVarArg()) {
|
|
|
|
if (!MF.getSubtarget<AArch64Subtarget>().isTargetDarwin()) {
|
|
|
|
// FIXME: we need to reimplement saveVarArgsRegisters from
|
|
|
|
// AArch64ISelLowering.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We currently pass all varargs at 8-byte alignment.
|
|
|
|
uint64_t StackOffset = alignTo(Handler.StackUsed, 8);
|
|
|
|
|
|
|
|
auto &MFI = MIRBuilder.getMF().getFrameInfo();
|
|
|
|
AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
|
|
|
|
FuncInfo->setVarArgsStackIndex(MFI.CreateFixedObject(4, StackOffset, true));
|
|
|
|
}
|
|
|
|
|
2016-09-20 23:20:36 +08:00
|
|
|
// Move back to the end of the basic block.
|
|
|
|
MIRBuilder.setMBB(MBB);
|
|
|
|
|
2016-09-21 20:57:45 +08:00
|
|
|
return true;
|
2016-08-11 05:44:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AArch64CallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
|
2017-03-20 22:40:18 +08:00
|
|
|
CallingConv::ID CallConv,
|
2016-09-21 20:57:45 +08:00
|
|
|
const MachineOperand &Callee,
|
|
|
|
const ArgInfo &OrigRet,
|
|
|
|
ArrayRef<ArgInfo> OrigArgs) const {
|
2016-08-11 05:44:01 +08:00
|
|
|
MachineFunction &MF = MIRBuilder.getMF();
|
2017-12-16 06:22:58 +08:00
|
|
|
const Function &F = MF.getFunction();
|
2016-09-20 23:20:36 +08:00
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
auto &DL = F.getParent()->getDataLayout();
|
|
|
|
|
2016-09-21 20:57:45 +08:00
|
|
|
SmallVector<ArgInfo, 8> SplitArgs;
|
|
|
|
for (auto &OrigArg : OrigArgs) {
|
2017-08-22 05:56:11 +08:00
|
|
|
splitToValueTypes(OrigArg, SplitArgs, DL, MRI, CallConv,
|
2017-03-07 07:50:28 +08:00
|
|
|
[&](unsigned Reg, uint64_t Offset) {
|
|
|
|
MIRBuilder.buildExtract(Reg, OrigArg.Reg, Offset);
|
2016-09-20 23:20:36 +08:00
|
|
|
});
|
|
|
|
}
|
2016-08-11 05:44:01 +08:00
|
|
|
|
|
|
|
// Find out which ABI gets to decide where things go.
|
|
|
|
const AArch64TargetLowering &TLI = *getTLI<AArch64TargetLowering>();
|
2017-01-18 06:30:10 +08:00
|
|
|
CCAssignFn *AssignFnFixed =
|
2017-03-20 22:40:18 +08:00
|
|
|
TLI.CCAssignFnForCall(CallConv, /*IsVarArg=*/false);
|
2017-01-18 06:30:10 +08:00
|
|
|
CCAssignFn *AssignFnVarArg =
|
2017-03-20 22:40:18 +08:00
|
|
|
TLI.CCAssignFnForCall(CallConv, /*IsVarArg=*/true);
|
2016-08-11 05:44:01 +08:00
|
|
|
|
2017-01-18 06:43:34 +08:00
|
|
|
auto CallSeqStart = MIRBuilder.buildInstr(AArch64::ADJCALLSTACKDOWN);
|
|
|
|
|
2016-09-22 21:49:25 +08:00
|
|
|
// Create a temporarily-floating call instruction so we can add the implicit
|
|
|
|
// uses of arg registers.
|
|
|
|
auto MIB = MIRBuilder.buildInstrNoInsert(Callee.isReg() ? AArch64::BLR
|
|
|
|
: AArch64::BL);
|
2017-01-13 17:58:52 +08:00
|
|
|
MIB.add(Callee);
|
2016-08-11 05:44:01 +08:00
|
|
|
|
|
|
|
// Tell the call which registers are clobbered.
|
|
|
|
auto TRI = MF.getSubtarget().getRegisterInfo();
|
|
|
|
MIB.addRegMask(TRI->getCallPreservedMask(MF, F.getCallingConv()));
|
|
|
|
|
2016-09-22 21:49:25 +08:00
|
|
|
// Do the actual argument marshalling.
|
|
|
|
SmallVector<unsigned, 8> PhysRegs;
|
2017-01-18 06:30:10 +08:00
|
|
|
OutgoingArgHandler Handler(MIRBuilder, MRI, MIB, AssignFnFixed,
|
|
|
|
AssignFnVarArg);
|
|
|
|
if (!handleAssignments(MIRBuilder, SplitArgs, Handler))
|
2016-09-22 21:49:25 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Now we can add the actual call instruction to the correct basic block.
|
|
|
|
MIRBuilder.insertInstr(MIB);
|
2016-08-11 05:44:01 +08:00
|
|
|
|
2016-12-23 05:56:31 +08:00
|
|
|
// If Callee is a reg, since it is used by a target specific
|
|
|
|
// instruction, it must have a register class matching the
|
|
|
|
// constraint of that instruction.
|
|
|
|
if (Callee.isReg())
|
|
|
|
MIB->getOperand(0).setReg(constrainOperandRegClass(
|
|
|
|
MF, *TRI, MRI, *MF.getSubtarget().getInstrInfo(),
|
2018-02-27 06:56:21 +08:00
|
|
|
*MF.getSubtarget().getRegBankInfo(), *MIB, MIB->getDesc(), Callee, 0));
|
2016-12-23 05:56:31 +08:00
|
|
|
|
2016-08-11 05:44:01 +08:00
|
|
|
// Finally we can copy the returned value back into its virtual-register. In
|
|
|
|
// symmetry with the arugments, the physical register must be an
|
|
|
|
// implicit-define of the call instruction.
|
|
|
|
CCAssignFn *RetAssignFn = TLI.CCAssignFnForReturn(F.getCallingConv());
|
2016-09-21 20:57:45 +08:00
|
|
|
if (OrigRet.Reg) {
|
|
|
|
SplitArgs.clear();
|
2016-09-20 23:20:36 +08:00
|
|
|
|
|
|
|
SmallVector<uint64_t, 8> RegOffsets;
|
2016-09-21 20:57:45 +08:00
|
|
|
SmallVector<unsigned, 8> SplitRegs;
|
2017-08-22 05:56:11 +08:00
|
|
|
splitToValueTypes(OrigRet, SplitArgs, DL, MRI, F.getCallingConv(),
|
2017-03-07 07:50:28 +08:00
|
|
|
[&](unsigned Reg, uint64_t Offset) {
|
|
|
|
RegOffsets.push_back(Offset);
|
|
|
|
SplitRegs.push_back(Reg);
|
2016-09-20 23:20:36 +08:00
|
|
|
});
|
|
|
|
|
2017-01-18 06:30:10 +08:00
|
|
|
CallReturnHandler Handler(MIRBuilder, MRI, MIB, RetAssignFn);
|
|
|
|
if (!handleAssignments(MIRBuilder, SplitArgs, Handler))
|
2016-09-21 20:57:45 +08:00
|
|
|
return false;
|
2016-08-11 05:44:01 +08:00
|
|
|
|
2016-09-20 23:20:36 +08:00
|
|
|
if (!RegOffsets.empty())
|
2016-09-21 20:57:45 +08:00
|
|
|
MIRBuilder.buildSequence(OrigRet.Reg, SplitRegs, RegOffsets);
|
2016-09-20 23:20:36 +08:00
|
|
|
}
|
|
|
|
|
2017-05-09 21:35:13 +08:00
|
|
|
CallSeqStart.addImm(Handler.StackSize).addImm(0);
|
2017-01-18 06:43:34 +08:00
|
|
|
MIRBuilder.buildInstr(AArch64::ADJCALLSTACKUP)
|
|
|
|
.addImm(Handler.StackSize)
|
|
|
|
.addImm(0);
|
|
|
|
|
2016-08-11 05:44:01 +08:00
|
|
|
return true;
|
|
|
|
}
|