#ifndef LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZCALLINGCONV_H
#define LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZCALLINGCONV_H
#include "SystemZSubtarget.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/CodeGen/CallingConvLower.h"
#include "llvm/MC/MCRegisterInfo.h"
namespace llvm {
namespace SystemZ {
const unsigned ELFNumArgGPRs = 5;
extern const MCPhysReg ELFArgGPRs[ELFNumArgGPRs];
const unsigned ELFNumArgFPRs = 4;
extern const MCPhysReg ELFArgFPRs[ELFNumArgFPRs];
const unsigned XPLINK64NumArgGPRs = 3;
extern const MCPhysReg XPLINK64ArgGPRs[XPLINK64NumArgGPRs];
const unsigned XPLINK64NumArgFPRs = 4;
extern const MCPhysReg XPLINK64ArgFPRs[XPLINK64NumArgFPRs];
}
class SystemZCCState : public CCState {
private:
SmallVector<bool, 4> ArgIsFixed;
SmallVector<bool, 4> ArgIsShortVector;
bool IsShortVectorType(EVT ArgVT) {
return ArgVT.isVector() && ArgVT.getStoreSize() <= 8;
}
public:
SystemZCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
SmallVectorImpl<CCValAssign> &locs, LLVMContext &C)
: CCState(CC, isVarArg, MF, locs, C) {}
void AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
CCAssignFn Fn) {
ArgIsFixed.clear();
for (unsigned i = 0; i < Ins.size(); ++i)
ArgIsFixed.push_back(true);
ArgIsShortVector.clear();
for (unsigned i = 0; i < Ins.size(); ++i)
ArgIsShortVector.push_back(IsShortVectorType(Ins[i].ArgVT));
CCState::AnalyzeFormalArguments(Ins, Fn);
}
void AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
CCAssignFn Fn) {
ArgIsFixed.clear();
for (unsigned i = 0; i < Outs.size(); ++i)
ArgIsFixed.push_back(Outs[i].IsFixed);
ArgIsShortVector.clear();
for (unsigned i = 0; i < Outs.size(); ++i)
ArgIsShortVector.push_back(IsShortVectorType(Outs[i].ArgVT));
CCState::AnalyzeCallOperands(Outs, Fn);
}
void AnalyzeCallOperands(const SmallVectorImpl<MVT> &Outs,
SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
CCAssignFn Fn) = delete;
bool IsFixed(unsigned ValNo) { return ArgIsFixed[ValNo]; }
bool IsShortVector(unsigned ValNo) { return ArgIsShortVector[ValNo]; }
};
inline bool CC_SystemZ_I128Indirect(unsigned &ValNo, MVT &ValVT,
MVT &LocVT,
CCValAssign::LocInfo &LocInfo,
ISD::ArgFlagsTy &ArgFlags,
CCState &State) {
SmallVectorImpl<CCValAssign> &PendingMembers = State.getPendingLocs();
if (!ArgFlags.isSplit() && PendingMembers.empty())
return false;
LocVT = MVT::i64;
LocInfo = CCValAssign::Indirect;
PendingMembers.push_back(CCValAssign::getPending(ValNo, ValVT,
LocVT, LocInfo));
if (!ArgFlags.isSplitEnd())
return true;
unsigned Reg;
const SystemZSubtarget &Subtarget =
State.getMachineFunction().getSubtarget<SystemZSubtarget>();
if (Subtarget.isTargetELF())
Reg = State.AllocateReg(SystemZ::ELFArgGPRs);
else if (Subtarget.isTargetXPLINK64())
Reg = State.AllocateReg(SystemZ::XPLINK64ArgGPRs);
else
llvm_unreachable("Unknown Calling Convention!");
unsigned Offset = Reg && !Subtarget.isTargetXPLINK64()
? 0
: State.AllocateStack(8, Align(8));
for (auto &It : PendingMembers) {
if (Reg)
It.convertToReg(Reg);
else
It.convertToMem(Offset);
State.addLoc(It);
}
PendingMembers.clear();
return true;
}
inline bool CC_XPLINK64_Shadow_Reg(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
CCValAssign::LocInfo &LocInfo,
ISD::ArgFlagsTy &ArgFlags, CCState &State) {
if (LocVT == MVT::f32 || LocVT == MVT::f64) {
State.AllocateReg(SystemZ::XPLINK64ArgGPRs);
}
if (LocVT == MVT::f128 || LocVT.is128BitVector()) {
State.AllocateReg(SystemZ::XPLINK64ArgGPRs);
State.AllocateReg(SystemZ::XPLINK64ArgGPRs);
if (LocVT == MVT::f128) {
for (unsigned I = 0; I < SystemZ::XPLINK64NumArgFPRs; I += 2)
if (State.isAllocated(SystemZ::XPLINK64ArgFPRs[I]))
State.AllocateReg(SystemZ::XPLINK64ArgFPRs[I + 1]);
}
}
return false;
}
inline bool CC_XPLINK64_Allocate128BitVararg(unsigned &ValNo, MVT &ValVT,
MVT &LocVT,
CCValAssign::LocInfo &LocInfo,
ISD::ArgFlagsTy &ArgFlags,
CCState &State) {
State.AllocateReg(SystemZ::R1D);
bool AllocGPR2 = State.AllocateReg(SystemZ::R2D);
bool AllocGPR3 = State.AllocateReg(SystemZ::R3D);
if (AllocGPR3) {
LocVT = MVT::i128;
LocInfo = CCValAssign::BCvt;
auto Offset = State.AllocateStack(16, Align(8));
if (AllocGPR2)
State.addLoc(
CCValAssign::getReg(ValNo, ValVT, SystemZ::R2Q, LocVT, LocInfo));
else
State.addLoc(
CCValAssign::getCustomMem(ValNo, ValVT, Offset, LocVT, LocInfo));
return true;
}
return false;
}
inline bool RetCC_SystemZ_Error(unsigned &, MVT &, MVT &,
CCValAssign::LocInfo &, ISD::ArgFlagsTy &,
CCState &) {
llvm_unreachable("Return value calling convention currently unsupported.");
}
inline bool CC_SystemZ_Error(unsigned &, MVT &, MVT &, CCValAssign::LocInfo &,
ISD::ArgFlagsTy &, CCState &) {
llvm_unreachable("Argument calling convention currently unsupported.");
}
inline bool CC_SystemZ_GHC_Error(unsigned &, MVT &, MVT &,
CCValAssign::LocInfo &, ISD::ArgFlagsTy &,
CCState &) {
report_fatal_error("No registers left in GHC calling convention");
return false;
}
}
#endif