Checkin in of first of several patches to finish implementation of
mips16/mips32 floating point interoperability.
This patch fixes returns from mips16 functions so that if the function
was in fact called by a mips32 hard float routine, then values
that would have been returned in floating point registers are so returned.
Mips16 mode has no floating point instructions so there is no way to
load values into floating point registers.
This is needed when returning float, double, single complex, double complex
in the Mips ABI.
Helper functions in libc for mips16 are available to do this.
For efficiency purposes, these helper functions have a different calling
convention from normal Mips calls.
Registers v0,v1,a0,a1 are used to pass parameters instead of
a0,a1,a2,a3.
This is because v0,v1,a0,a1 are the natural registers used to return
floating point values in soft float. These values can then be moved
to the appropriate floating point registers with no extra cost.
The only register that is modified is ra in this call.
The helper functions make sure that the return values are in the floating
point registers that they would be in if soft float was not in effect
(which it is for mips16, though the soft float is implemented using a mips32
library that uses hard float).
llvm-svn: 181641
2013-05-11 06:25:39 +08:00
|
|
|
//===---- Mips16HardFloat.cpp for Mips16 Hard Float --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines a pass needed for Mips16 Hard Float
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Mips16HardFloat.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/IR/Value.h"
|
Checkin in of first of several patches to finish implementation of
mips16/mips32 floating point interoperability.
This patch fixes returns from mips16 functions so that if the function
was in fact called by a mips32 hard float routine, then values
that would have been returned in floating point registers are so returned.
Mips16 mode has no floating point instructions so there is no way to
load values into floating point registers.
This is needed when returning float, double, single complex, double complex
in the Mips ABI.
Helper functions in libc for mips16 are available to do this.
For efficiency purposes, these helper functions have a different calling
convention from normal Mips calls.
Registers v0,v1,a0,a1 are used to pass parameters instead of
a0,a1,a2,a3.
This is because v0,v1,a0,a1 are the natural registers used to return
floating point values in soft float. These values can then be moved
to the appropriate floating point registers with no extra cost.
The only register that is modified is ra in this call.
The helper functions make sure that the return values are in the floating
point registers that they would be in if soft float was not in effect
(which it is for mips16, though the soft float is implemented using a mips32
library that uses hard float).
llvm-svn: 181641
2013-05-11 06:25:39 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-08-12 05:30:27 +08:00
|
|
|
#include <algorithm>
|
Checkin in of first of several patches to finish implementation of
mips16/mips32 floating point interoperability.
This patch fixes returns from mips16 functions so that if the function
was in fact called by a mips32 hard float routine, then values
that would have been returned in floating point registers are so returned.
Mips16 mode has no floating point instructions so there is no way to
load values into floating point registers.
This is needed when returning float, double, single complex, double complex
in the Mips ABI.
Helper functions in libc for mips16 are available to do this.
For efficiency purposes, these helper functions have a different calling
convention from normal Mips calls.
Registers v0,v1,a0,a1 are used to pass parameters instead of
a0,a1,a2,a3.
This is because v0,v1,a0,a1 are the natural registers used to return
floating point values in soft float. These values can then be moved
to the appropriate floating point registers with no extra cost.
The only register that is modified is ra in this call.
The helper functions make sure that the return values are in the floating
point registers that they would be in if soft float was not in effect
(which it is for mips16, though the soft float is implemented using a mips32
library that uses hard float).
llvm-svn: 181641
2013-05-11 06:25:39 +08:00
|
|
|
#include <string>
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "mips16-hard-float"
|
|
|
|
|
2013-05-14 10:00:24 +08:00
|
|
|
static void inlineAsmOut
|
|
|
|
(LLVMContext &C, StringRef AsmString, BasicBlock *BB ) {
|
|
|
|
std::vector<llvm::Type *> AsmArgTypes;
|
|
|
|
std::vector<llvm::Value*> AsmArgs;
|
|
|
|
llvm::FunctionType *AsmFTy =
|
|
|
|
llvm::FunctionType::get(Type::getVoidTy(C),
|
|
|
|
AsmArgTypes, false);
|
|
|
|
llvm::InlineAsm *IA =
|
|
|
|
llvm::InlineAsm::get(AsmFTy, AsmString, "", true,
|
|
|
|
/* IsAlignStack */ false,
|
|
|
|
llvm::InlineAsm::AD_ATT);
|
|
|
|
CallInst::Create(IA, AsmArgs, "", BB);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class InlineAsmHelper {
|
|
|
|
LLVMContext &C;
|
|
|
|
BasicBlock *BB;
|
|
|
|
public:
|
|
|
|
InlineAsmHelper(LLVMContext &C_, BasicBlock *BB_) :
|
|
|
|
C(C_), BB(BB_) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Out(StringRef AsmString) {
|
|
|
|
inlineAsmOut(C, AsmString, BB);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
Checkin in of first of several patches to finish implementation of
mips16/mips32 floating point interoperability.
This patch fixes returns from mips16 functions so that if the function
was in fact called by a mips32 hard float routine, then values
that would have been returned in floating point registers are so returned.
Mips16 mode has no floating point instructions so there is no way to
load values into floating point registers.
This is needed when returning float, double, single complex, double complex
in the Mips ABI.
Helper functions in libc for mips16 are available to do this.
For efficiency purposes, these helper functions have a different calling
convention from normal Mips calls.
Registers v0,v1,a0,a1 are used to pass parameters instead of
a0,a1,a2,a3.
This is because v0,v1,a0,a1 are the natural registers used to return
floating point values in soft float. These values can then be moved
to the appropriate floating point registers with no extra cost.
The only register that is modified is ra in this call.
The helper functions make sure that the return values are in the floating
point registers that they would be in if soft float was not in effect
(which it is for mips16, though the soft float is implemented using a mips32
library that uses hard float).
llvm-svn: 181641
2013-05-11 06:25:39 +08:00
|
|
|
//
|
|
|
|
// Return types that matter for hard float are:
|
|
|
|
// float, double, complex float, and complex double
|
|
|
|
//
|
|
|
|
enum FPReturnVariant {
|
|
|
|
FRet, DRet, CFRet, CDRet, NoFPRet
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Determine which FP return type this function has
|
|
|
|
//
|
|
|
|
static FPReturnVariant whichFPReturnVariant(Type *T) {
|
|
|
|
switch (T->getTypeID()) {
|
|
|
|
case Type::FloatTyID:
|
|
|
|
return FRet;
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
return DRet;
|
|
|
|
case Type::StructTyID:
|
|
|
|
if (T->getStructNumElements() != 2)
|
|
|
|
break;
|
|
|
|
if ((T->getContainedType(0)->isFloatTy()) &&
|
|
|
|
(T->getContainedType(1)->isFloatTy()))
|
|
|
|
return CFRet;
|
|
|
|
if ((T->getContainedType(0)->isDoubleTy()) &&
|
|
|
|
(T->getContainedType(1)->isDoubleTy()))
|
|
|
|
return CDRet;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NoFPRet;
|
|
|
|
}
|
|
|
|
|
2013-05-14 10:00:24 +08:00
|
|
|
//
|
|
|
|
// Parameter type that matter are float, (float, float), (float, double),
|
|
|
|
// double, (double, double), (double, float)
|
|
|
|
//
|
|
|
|
enum FPParamVariant {
|
|
|
|
FSig, FFSig, FDSig,
|
|
|
|
DSig, DDSig, DFSig, NoSig
|
|
|
|
};
|
|
|
|
|
|
|
|
// which floating point parameter signature variant we are dealing with
|
|
|
|
//
|
|
|
|
typedef Type::TypeID TypeID;
|
|
|
|
const Type::TypeID FloatTyID = Type::FloatTyID;
|
|
|
|
const Type::TypeID DoubleTyID = Type::DoubleTyID;
|
|
|
|
|
|
|
|
static FPParamVariant whichFPParamVariantNeeded(Function &F) {
|
|
|
|
switch (F.arg_size()) {
|
|
|
|
case 0:
|
|
|
|
return NoSig;
|
|
|
|
case 1:{
|
|
|
|
TypeID ArgTypeID = F.getFunctionType()->getParamType(0)->getTypeID();
|
|
|
|
switch (ArgTypeID) {
|
|
|
|
case FloatTyID:
|
|
|
|
return FSig;
|
|
|
|
case DoubleTyID:
|
|
|
|
return DSig;
|
|
|
|
default:
|
|
|
|
return NoSig;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
TypeID ArgTypeID0 = F.getFunctionType()->getParamType(0)->getTypeID();
|
|
|
|
TypeID ArgTypeID1 = F.getFunctionType()->getParamType(1)->getTypeID();
|
|
|
|
switch(ArgTypeID0) {
|
|
|
|
case FloatTyID: {
|
|
|
|
switch (ArgTypeID1) {
|
|
|
|
case FloatTyID:
|
|
|
|
return FFSig;
|
|
|
|
case DoubleTyID:
|
|
|
|
return FDSig;
|
|
|
|
default:
|
|
|
|
return FSig;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case DoubleTyID: {
|
|
|
|
switch (ArgTypeID1) {
|
|
|
|
case FloatTyID:
|
|
|
|
return DFSig;
|
|
|
|
case DoubleTyID:
|
|
|
|
return DDSig;
|
|
|
|
default:
|
|
|
|
return DSig;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return NoSig;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm_unreachable("can't get here");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out if we need float point based on the function parameters.
|
|
|
|
// We need to move variables in and/or out of floating point
|
|
|
|
// registers because of the ABI
|
|
|
|
//
|
|
|
|
static bool needsFPStubFromParams(Function &F) {
|
|
|
|
if (F.arg_size() >=1) {
|
|
|
|
Type *ArgType = F.getFunctionType()->getParamType(0);
|
|
|
|
switch (ArgType->getTypeID()) {
|
|
|
|
case Type::FloatTyID:
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool needsFPReturnHelper(Function &F) {
|
|
|
|
Type* RetType = F.getReturnType();
|
|
|
|
return whichFPReturnVariant(RetType) != NoFPRet;
|
|
|
|
}
|
|
|
|
|
2013-12-19 07:57:48 +08:00
|
|
|
static bool needsFPReturnHelper(const FunctionType &FT) {
|
|
|
|
Type* RetType = FT.getReturnType();
|
|
|
|
return whichFPReturnVariant(RetType) != NoFPRet;
|
|
|
|
}
|
|
|
|
|
2013-05-14 10:00:24 +08:00
|
|
|
static bool needsFPHelperFromSig(Function &F) {
|
|
|
|
return needsFPStubFromParams(F) || needsFPReturnHelper(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// We swap between FP and Integer registers to allow Mips16 and Mips32 to
|
|
|
|
// interoperate
|
|
|
|
//
|
|
|
|
|
2013-05-14 10:13:45 +08:00
|
|
|
static void swapFPIntParams
|
|
|
|
(FPParamVariant PV, Module *M, InlineAsmHelper &IAH,
|
|
|
|
bool LE, bool ToFP) {
|
2013-05-14 10:00:24 +08:00
|
|
|
//LLVMContext &Context = M->getContext();
|
|
|
|
std::string MI = ToFP? "mtc1 ": "mfc1 ";
|
|
|
|
switch (PV) {
|
|
|
|
case FSig:
|
|
|
|
IAH.Out(MI + "$$4,$$f12");
|
|
|
|
break;
|
|
|
|
case FFSig:
|
|
|
|
IAH.Out(MI +"$$4,$$f12");
|
|
|
|
IAH.Out(MI + "$$5,$$f14");
|
|
|
|
break;
|
|
|
|
case FDSig:
|
|
|
|
IAH.Out(MI + "$$4,$$f12");
|
|
|
|
if (LE) {
|
|
|
|
IAH.Out(MI + "$$6,$$f14");
|
|
|
|
IAH.Out(MI + "$$7,$$f15");
|
|
|
|
} else {
|
|
|
|
IAH.Out(MI + "$$7,$$f14");
|
|
|
|
IAH.Out(MI + "$$6,$$f15");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DSig:
|
|
|
|
if (LE) {
|
|
|
|
IAH.Out(MI + "$$4,$$f12");
|
|
|
|
IAH.Out(MI + "$$5,$$f13");
|
|
|
|
} else {
|
|
|
|
IAH.Out(MI + "$$5,$$f12");
|
|
|
|
IAH.Out(MI + "$$4,$$f13");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DDSig:
|
|
|
|
if (LE) {
|
|
|
|
IAH.Out(MI + "$$4,$$f12");
|
|
|
|
IAH.Out(MI + "$$5,$$f13");
|
|
|
|
IAH.Out(MI + "$$6,$$f14");
|
|
|
|
IAH.Out(MI + "$$7,$$f15");
|
|
|
|
} else {
|
|
|
|
IAH.Out(MI + "$$5,$$f12");
|
|
|
|
IAH.Out(MI + "$$4,$$f13");
|
|
|
|
IAH.Out(MI + "$$7,$$f14");
|
|
|
|
IAH.Out(MI + "$$6,$$f15");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DFSig:
|
|
|
|
if (LE) {
|
|
|
|
IAH.Out(MI + "$$4,$$f12");
|
|
|
|
IAH.Out(MI + "$$5,$$f13");
|
|
|
|
} else {
|
|
|
|
IAH.Out(MI + "$$5,$$f12");
|
|
|
|
IAH.Out(MI + "$$4,$$f13");
|
|
|
|
}
|
|
|
|
IAH.Out(MI + "$$6,$$f14");
|
|
|
|
break;
|
|
|
|
case NoSig:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Make sure that we know we already need a stub for this function.
|
|
|
|
// Having called needsFPHelperFromSig
|
|
|
|
//
|
2014-02-15 03:16:39 +08:00
|
|
|
static void assureFPCallStub(Function &F, Module *M,
|
|
|
|
const MipsSubtarget &Subtarget) {
|
2013-05-14 10:00:24 +08:00
|
|
|
// for now we only need them for static relocation
|
2013-05-14 14:00:01 +08:00
|
|
|
if (Subtarget.getRelocationModel() == Reloc::PIC_)
|
2013-05-14 10:00:24 +08:00
|
|
|
return;
|
|
|
|
LLVMContext &Context = M->getContext();
|
|
|
|
bool LE = Subtarget.isLittle();
|
|
|
|
std::string Name = F.getName();
|
|
|
|
std::string SectionName = ".mips16.call.fp." + Name;
|
2013-08-01 10:26:31 +08:00
|
|
|
std::string StubName = "__call_stub_fp_" + Name;
|
2013-05-14 10:00:24 +08:00
|
|
|
//
|
|
|
|
// see if we already have the stub
|
|
|
|
//
|
|
|
|
Function *FStub = M->getFunction(StubName);
|
|
|
|
if (FStub && !FStub->isDeclaration()) return;
|
|
|
|
FStub = Function::Create(F.getFunctionType(),
|
|
|
|
Function::InternalLinkage, StubName, M);
|
|
|
|
FStub->addFnAttr("mips16_fp_stub");
|
|
|
|
FStub->addFnAttr(llvm::Attribute::Naked);
|
2013-08-01 10:26:31 +08:00
|
|
|
FStub->addFnAttr(llvm::Attribute::NoInline);
|
2013-05-14 10:00:24 +08:00
|
|
|
FStub->addFnAttr(llvm::Attribute::NoUnwind);
|
|
|
|
FStub->addFnAttr("nomips16");
|
|
|
|
FStub->setSection(SectionName);
|
|
|
|
BasicBlock *BB = BasicBlock::Create(Context, "entry", FStub);
|
|
|
|
InlineAsmHelper IAH(Context, BB);
|
2013-08-01 10:26:31 +08:00
|
|
|
IAH.Out(".set reorder");
|
2013-05-14 10:00:24 +08:00
|
|
|
FPReturnVariant RV = whichFPReturnVariant(FStub->getReturnType());
|
|
|
|
FPParamVariant PV = whichFPParamVariantNeeded(F);
|
|
|
|
swapFPIntParams(PV, M, IAH, LE, true);
|
|
|
|
if (RV != NoFPRet) {
|
|
|
|
IAH.Out("move $$18, $$31");
|
|
|
|
IAH.Out("jal " + Name);
|
|
|
|
} else {
|
|
|
|
IAH.Out("lui $$25,%hi(" + Name + ")");
|
|
|
|
IAH.Out("addiu $$25,$$25,%lo(" + Name + ")" );
|
|
|
|
}
|
|
|
|
switch (RV) {
|
|
|
|
case FRet:
|
|
|
|
IAH.Out("mfc1 $$2,$$f0");
|
|
|
|
break;
|
|
|
|
case DRet:
|
|
|
|
if (LE) {
|
|
|
|
IAH.Out("mfc1 $$2,$$f0");
|
|
|
|
IAH.Out("mfc1 $$3,$$f1");
|
|
|
|
} else {
|
|
|
|
IAH.Out("mfc1 $$3,$$f0");
|
|
|
|
IAH.Out("mfc1 $$2,$$f1");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CFRet:
|
|
|
|
if (LE) {
|
|
|
|
IAH.Out("mfc1 $$2,$$f0");
|
|
|
|
IAH.Out("mfc1 $$3,$$f2");
|
|
|
|
} else {
|
|
|
|
IAH.Out("mfc1 $$3,$$f0");
|
|
|
|
IAH.Out("mfc1 $$3,$$f2");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CDRet:
|
|
|
|
if (LE) {
|
|
|
|
IAH.Out("mfc1 $$4,$$f2");
|
|
|
|
IAH.Out("mfc1 $$5,$$f3");
|
|
|
|
IAH.Out("mfc1 $$2,$$f0");
|
|
|
|
IAH.Out("mfc1 $$3,$$f1");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
IAH.Out("mfc1 $$5,$$f2");
|
|
|
|
IAH.Out("mfc1 $$4,$$f3");
|
|
|
|
IAH.Out("mfc1 $$3,$$f0");
|
|
|
|
IAH.Out("mfc1 $$2,$$f1");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NoFPRet:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (RV != NoFPRet)
|
|
|
|
IAH.Out("jr $$18");
|
|
|
|
else
|
|
|
|
IAH.Out("jr $$25");
|
|
|
|
new UnreachableInst(Context, BB);
|
|
|
|
}
|
|
|
|
|
2013-08-12 05:30:27 +08:00
|
|
|
//
|
2013-09-01 12:12:59 +08:00
|
|
|
// Functions that are llvm intrinsics and don't need helpers.
|
2013-08-12 05:30:27 +08:00
|
|
|
//
|
2013-08-12 17:37:29 +08:00
|
|
|
static const char *IntrinsicInline[] =
|
2013-09-01 12:12:59 +08:00
|
|
|
{"fabs",
|
2013-10-09 03:55:01 +08:00
|
|
|
"fabsf",
|
2013-09-01 12:12:59 +08:00
|
|
|
"llvm.ceil.f32", "llvm.ceil.f64",
|
|
|
|
"llvm.copysign.f32", "llvm.copysign.f64",
|
|
|
|
"llvm.cos.f32", "llvm.cos.f64",
|
|
|
|
"llvm.exp.f32", "llvm.exp.f64",
|
|
|
|
"llvm.exp2.f32", "llvm.exp2.f64",
|
|
|
|
"llvm.fabs.f32", "llvm.fabs.f64",
|
|
|
|
"llvm.floor.f32", "llvm.floor.f64",
|
|
|
|
"llvm.fma.f32", "llvm.fma.f64",
|
|
|
|
"llvm.log.f32", "llvm.log.f64",
|
|
|
|
"llvm.log10.f32", "llvm.log10.f64",
|
|
|
|
"llvm.nearbyint.f32", "llvm.nearbyint.f64",
|
|
|
|
"llvm.pow.f32", "llvm.pow.f64",
|
|
|
|
"llvm.powi.f32", "llvm.powi.f64",
|
|
|
|
"llvm.rint.f32", "llvm.rint.f64",
|
|
|
|
"llvm.round.f32", "llvm.round.f64",
|
|
|
|
"llvm.sin.f32", "llvm.sin.f64",
|
|
|
|
"llvm.sqrt.f32", "llvm.sqrt.f64",
|
|
|
|
"llvm.trunc.f32", "llvm.trunc.f64",
|
|
|
|
};
|
2013-08-12 05:30:27 +08:00
|
|
|
|
2013-08-12 17:37:29 +08:00
|
|
|
static bool isIntrinsicInline(Function *F) {
|
2014-04-13 00:15:53 +08:00
|
|
|
return std::binary_search(std::begin(IntrinsicInline),
|
|
|
|
std::end(IntrinsicInline), F->getName());
|
2013-08-12 05:30:27 +08:00
|
|
|
}
|
Checkin in of first of several patches to finish implementation of
mips16/mips32 floating point interoperability.
This patch fixes returns from mips16 functions so that if the function
was in fact called by a mips32 hard float routine, then values
that would have been returned in floating point registers are so returned.
Mips16 mode has no floating point instructions so there is no way to
load values into floating point registers.
This is needed when returning float, double, single complex, double complex
in the Mips ABI.
Helper functions in libc for mips16 are available to do this.
For efficiency purposes, these helper functions have a different calling
convention from normal Mips calls.
Registers v0,v1,a0,a1 are used to pass parameters instead of
a0,a1,a2,a3.
This is because v0,v1,a0,a1 are the natural registers used to return
floating point values in soft float. These values can then be moved
to the appropriate floating point registers with no extra cost.
The only register that is modified is ra in this call.
The helper functions make sure that the return values are in the floating
point registers that they would be in if soft float was not in effect
(which it is for mips16, though the soft float is implemented using a mips32
library that uses hard float).
llvm-svn: 181641
2013-05-11 06:25:39 +08:00
|
|
|
//
|
|
|
|
// Returns of float, double and complex need to be handled with a helper
|
2013-05-16 10:17:42 +08:00
|
|
|
// function.
|
Checkin in of first of several patches to finish implementation of
mips16/mips32 floating point interoperability.
This patch fixes returns from mips16 functions so that if the function
was in fact called by a mips32 hard float routine, then values
that would have been returned in floating point registers are so returned.
Mips16 mode has no floating point instructions so there is no way to
load values into floating point registers.
This is needed when returning float, double, single complex, double complex
in the Mips ABI.
Helper functions in libc for mips16 are available to do this.
For efficiency purposes, these helper functions have a different calling
convention from normal Mips calls.
Registers v0,v1,a0,a1 are used to pass parameters instead of
a0,a1,a2,a3.
This is because v0,v1,a0,a1 are the natural registers used to return
floating point values in soft float. These values can then be moved
to the appropriate floating point registers with no extra cost.
The only register that is modified is ra in this call.
The helper functions make sure that the return values are in the floating
point registers that they would be in if soft float was not in effect
(which it is for mips16, though the soft float is implemented using a mips32
library that uses hard float).
llvm-svn: 181641
2013-05-11 06:25:39 +08:00
|
|
|
//
|
|
|
|
static bool fixupFPReturnAndCall
|
|
|
|
(Function &F, Module *M, const MipsSubtarget &Subtarget) {
|
|
|
|
bool Modified = false;
|
|
|
|
LLVMContext &C = M->getContext();
|
|
|
|
Type *MyVoid = Type::getVoidTy(C);
|
|
|
|
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
|
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end();
|
|
|
|
I != E; ++I) {
|
|
|
|
Instruction &Inst = *I;
|
|
|
|
if (const ReturnInst *RI = dyn_cast<ReturnInst>(I)) {
|
|
|
|
Value *RVal = RI->getReturnValue();
|
|
|
|
if (!RVal) continue;
|
|
|
|
//
|
|
|
|
// If there is a return value and it needs a helper function,
|
|
|
|
// figure out which one and add a call before the actual
|
|
|
|
// return to this helper. The purpose of the helper is to move
|
|
|
|
// floating point values from their soft float return mapping to
|
|
|
|
// where they would have been mapped to in floating point registers.
|
|
|
|
//
|
|
|
|
Type *T = RVal->getType();
|
|
|
|
FPReturnVariant RV = whichFPReturnVariant(T);
|
|
|
|
if (RV == NoFPRet) continue;
|
|
|
|
static const char* Helper[NoFPRet] =
|
|
|
|
{"__mips16_ret_sf", "__mips16_ret_df", "__mips16_ret_sc",
|
|
|
|
"__mips16_ret_dc"};
|
|
|
|
const char *Name = Helper[RV];
|
|
|
|
AttributeSet A;
|
|
|
|
Value *Params[] = {RVal};
|
|
|
|
Modified = true;
|
|
|
|
//
|
|
|
|
// These helper functions have a different calling ABI so
|
|
|
|
// this __Mips16RetHelper indicates that so that later
|
|
|
|
// during call setup, the proper call lowering to the helper
|
|
|
|
// functions will take place.
|
|
|
|
//
|
|
|
|
A = A.addAttribute(C, AttributeSet::FunctionIndex,
|
|
|
|
"__Mips16RetHelper");
|
|
|
|
A = A.addAttribute(C, AttributeSet::FunctionIndex,
|
|
|
|
Attribute::ReadNone);
|
2013-08-01 10:26:31 +08:00
|
|
|
A = A.addAttribute(C, AttributeSet::FunctionIndex,
|
|
|
|
Attribute::NoInline);
|
Checkin in of first of several patches to finish implementation of
mips16/mips32 floating point interoperability.
This patch fixes returns from mips16 functions so that if the function
was in fact called by a mips32 hard float routine, then values
that would have been returned in floating point registers are so returned.
Mips16 mode has no floating point instructions so there is no way to
load values into floating point registers.
This is needed when returning float, double, single complex, double complex
in the Mips ABI.
Helper functions in libc for mips16 are available to do this.
For efficiency purposes, these helper functions have a different calling
convention from normal Mips calls.
Registers v0,v1,a0,a1 are used to pass parameters instead of
a0,a1,a2,a3.
This is because v0,v1,a0,a1 are the natural registers used to return
floating point values in soft float. These values can then be moved
to the appropriate floating point registers with no extra cost.
The only register that is modified is ra in this call.
The helper functions make sure that the return values are in the floating
point registers that they would be in if soft float was not in effect
(which it is for mips16, though the soft float is implemented using a mips32
library that uses hard float).
llvm-svn: 181641
2013-05-11 06:25:39 +08:00
|
|
|
Value *F = (M->getOrInsertFunction(Name, A, MyVoid, T, NULL));
|
|
|
|
CallInst::Create(F, Params, "", &Inst );
|
2013-05-14 10:00:24 +08:00
|
|
|
} else if (const CallInst *CI = dyn_cast<CallInst>(I)) {
|
2013-12-19 07:57:48 +08:00
|
|
|
const Value* V = CI->getCalledValue();
|
2014-04-25 13:30:21 +08:00
|
|
|
const Type* T = nullptr;
|
2013-12-19 07:57:48 +08:00
|
|
|
if (V) T = V->getType();
|
2014-04-25 13:30:21 +08:00
|
|
|
const PointerType *PFT=nullptr;
|
2013-12-19 07:57:48 +08:00
|
|
|
if (T) PFT = dyn_cast<PointerType>(T);
|
2014-04-25 13:30:21 +08:00
|
|
|
const FunctionType *FT=nullptr;
|
2013-12-19 07:57:48 +08:00
|
|
|
if (PFT) FT = dyn_cast<FunctionType>(PFT->getElementType());
|
2013-12-10 22:29:38 +08:00
|
|
|
Function *F_ = CI->getCalledFunction();
|
2013-12-19 07:57:48 +08:00
|
|
|
if (FT && needsFPReturnHelper(*FT) &&
|
|
|
|
!(F_ && isIntrinsicInline(F_))) {
|
|
|
|
Modified=true;
|
|
|
|
F.addFnAttr("saveS2");
|
|
|
|
}
|
2013-12-10 22:29:38 +08:00
|
|
|
if (F_ && !isIntrinsicInline(F_)) {
|
2013-05-14 10:00:24 +08:00
|
|
|
// pic mode calls are handled by already defined
|
|
|
|
// helper functions
|
2013-12-10 22:29:38 +08:00
|
|
|
if (needsFPReturnHelper(*F_)) {
|
2013-05-14 10:00:24 +08:00
|
|
|
Modified=true;
|
2013-12-10 22:29:38 +08:00
|
|
|
F.addFnAttr("saveS2");
|
|
|
|
}
|
|
|
|
if (Subtarget.getRelocationModel() != Reloc::PIC_ ) {
|
|
|
|
if (needsFPHelperFromSig(*F_)) {
|
|
|
|
assureFPCallStub(*F_, M, Subtarget);
|
|
|
|
Modified=true;
|
|
|
|
}
|
2013-05-14 10:00:24 +08:00
|
|
|
}
|
|
|
|
}
|
Checkin in of first of several patches to finish implementation of
mips16/mips32 floating point interoperability.
This patch fixes returns from mips16 functions so that if the function
was in fact called by a mips32 hard float routine, then values
that would have been returned in floating point registers are so returned.
Mips16 mode has no floating point instructions so there is no way to
load values into floating point registers.
This is needed when returning float, double, single complex, double complex
in the Mips ABI.
Helper functions in libc for mips16 are available to do this.
For efficiency purposes, these helper functions have a different calling
convention from normal Mips calls.
Registers v0,v1,a0,a1 are used to pass parameters instead of
a0,a1,a2,a3.
This is because v0,v1,a0,a1 are the natural registers used to return
floating point values in soft float. These values can then be moved
to the appropriate floating point registers with no extra cost.
The only register that is modified is ra in this call.
The helper functions make sure that the return values are in the floating
point registers that they would be in if soft float was not in effect
(which it is for mips16, though the soft float is implemented using a mips32
library that uses hard float).
llvm-svn: 181641
2013-05-11 06:25:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Modified;
|
|
|
|
}
|
|
|
|
|
2013-05-16 10:17:42 +08:00
|
|
|
static void createFPFnStub(Function *F, Module *M, FPParamVariant PV,
|
|
|
|
const MipsSubtarget &Subtarget ) {
|
|
|
|
bool PicMode = Subtarget.getRelocationModel() == Reloc::PIC_;
|
|
|
|
bool LE = Subtarget.isLittle();
|
|
|
|
LLVMContext &Context = M->getContext();
|
|
|
|
std::string Name = F->getName();
|
|
|
|
std::string SectionName = ".mips16.fn." + Name;
|
|
|
|
std::string StubName = "__fn_stub_" + Name;
|
2013-09-26 04:58:50 +08:00
|
|
|
std::string LocalName = "$$__fn_local_" + Name;
|
2013-05-16 10:17:42 +08:00
|
|
|
Function *FStub = Function::Create
|
|
|
|
(F->getFunctionType(),
|
2013-08-01 10:26:31 +08:00
|
|
|
Function::InternalLinkage, StubName, M);
|
2013-05-16 10:17:42 +08:00
|
|
|
FStub->addFnAttr("mips16_fp_stub");
|
|
|
|
FStub->addFnAttr(llvm::Attribute::Naked);
|
|
|
|
FStub->addFnAttr(llvm::Attribute::NoUnwind);
|
2013-08-01 10:26:31 +08:00
|
|
|
FStub->addFnAttr(llvm::Attribute::NoInline);
|
2013-05-16 10:17:42 +08:00
|
|
|
FStub->addFnAttr("nomips16");
|
|
|
|
FStub->setSection(SectionName);
|
|
|
|
BasicBlock *BB = BasicBlock::Create(Context, "entry", FStub);
|
|
|
|
InlineAsmHelper IAH(Context, BB);
|
|
|
|
IAH.Out(" .set macro");
|
|
|
|
if (PicMode) {
|
|
|
|
IAH.Out(".set noreorder");
|
2013-09-26 04:58:50 +08:00
|
|
|
IAH.Out(".cpload $$25");
|
2013-05-16 10:17:42 +08:00
|
|
|
IAH.Out(".set reorder");
|
|
|
|
IAH.Out(".reloc 0,R_MIPS_NONE," + Name);
|
|
|
|
IAH.Out("la $$25," + LocalName);
|
|
|
|
}
|
2013-09-21 09:37:52 +08:00
|
|
|
else {
|
|
|
|
IAH.Out(".set reorder");
|
2013-09-26 04:58:50 +08:00
|
|
|
IAH.Out("la $$25," + Name);
|
2013-09-21 09:37:52 +08:00
|
|
|
}
|
2013-05-16 10:17:42 +08:00
|
|
|
swapFPIntParams(PV, M, IAH, LE, false);
|
|
|
|
IAH.Out("jr $$25");
|
|
|
|
IAH.Out(LocalName + " = " + Name);
|
|
|
|
new UnreachableInst(FStub->getContext(), BB);
|
|
|
|
}
|
|
|
|
|
2013-08-31 03:40:56 +08:00
|
|
|
//
|
|
|
|
// remove the use-soft-float attribute
|
|
|
|
//
|
|
|
|
static void removeUseSoftFloat(Function &F) {
|
|
|
|
AttributeSet A;
|
|
|
|
DEBUG(errs() << "removing -use-soft-float\n");
|
|
|
|
A = A.addAttribute(F.getContext(), AttributeSet::FunctionIndex,
|
|
|
|
"use-soft-float", "false");
|
|
|
|
F.removeAttributes(AttributeSet::FunctionIndex, A);
|
|
|
|
if (F.hasFnAttribute("use-soft-float")) {
|
|
|
|
DEBUG(errs() << "still has -use-soft-float\n");
|
|
|
|
}
|
|
|
|
F.addAttributes(AttributeSet::FunctionIndex, A);
|
|
|
|
}
|
|
|
|
|
Checkin in of first of several patches to finish implementation of
mips16/mips32 floating point interoperability.
This patch fixes returns from mips16 functions so that if the function
was in fact called by a mips32 hard float routine, then values
that would have been returned in floating point registers are so returned.
Mips16 mode has no floating point instructions so there is no way to
load values into floating point registers.
This is needed when returning float, double, single complex, double complex
in the Mips ABI.
Helper functions in libc for mips16 are available to do this.
For efficiency purposes, these helper functions have a different calling
convention from normal Mips calls.
Registers v0,v1,a0,a1 are used to pass parameters instead of
a0,a1,a2,a3.
This is because v0,v1,a0,a1 are the natural registers used to return
floating point values in soft float. These values can then be moved
to the appropriate floating point registers with no extra cost.
The only register that is modified is ra in this call.
The helper functions make sure that the return values are in the floating
point registers that they would be in if soft float was not in effect
(which it is for mips16, though the soft float is implemented using a mips32
library that uses hard float).
llvm-svn: 181641
2013-05-11 06:25:39 +08:00
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
//
|
|
|
|
// This pass only makes sense when the underlying chip has floating point but
|
|
|
|
// we are compiling as mips16.
|
|
|
|
// For all mips16 functions (that are not stubs we have already generated), or
|
|
|
|
// declared via attributes as nomips16, we must:
|
|
|
|
// 1) fixup all returns of float, double, single and double complex
|
|
|
|
// by calling a helper function before the actual return.
|
2014-02-15 03:16:39 +08:00
|
|
|
// 2) generate helper functions (stubs) that can be called by mips32
|
|
|
|
// functions that will move parameters passed normally passed in
|
|
|
|
// floating point
|
2013-05-16 10:17:42 +08:00
|
|
|
// registers the soft float equivalents.
|
Checkin in of first of several patches to finish implementation of
mips16/mips32 floating point interoperability.
This patch fixes returns from mips16 functions so that if the function
was in fact called by a mips32 hard float routine, then values
that would have been returned in floating point registers are so returned.
Mips16 mode has no floating point instructions so there is no way to
load values into floating point registers.
This is needed when returning float, double, single complex, double complex
in the Mips ABI.
Helper functions in libc for mips16 are available to do this.
For efficiency purposes, these helper functions have a different calling
convention from normal Mips calls.
Registers v0,v1,a0,a1 are used to pass parameters instead of
a0,a1,a2,a3.
This is because v0,v1,a0,a1 are the natural registers used to return
floating point values in soft float. These values can then be moved
to the appropriate floating point registers with no extra cost.
The only register that is modified is ra in this call.
The helper functions make sure that the return values are in the floating
point registers that they would be in if soft float was not in effect
(which it is for mips16, though the soft float is implemented using a mips32
library that uses hard float).
llvm-svn: 181641
2013-05-11 06:25:39 +08:00
|
|
|
// 3) in the case of static relocation, generate helper functions so that
|
|
|
|
// mips16 functions can call extern functions of unknown type (mips16 or
|
2013-05-16 10:17:42 +08:00
|
|
|
// mips32).
|
Checkin in of first of several patches to finish implementation of
mips16/mips32 floating point interoperability.
This patch fixes returns from mips16 functions so that if the function
was in fact called by a mips32 hard float routine, then values
that would have been returned in floating point registers are so returned.
Mips16 mode has no floating point instructions so there is no way to
load values into floating point registers.
This is needed when returning float, double, single complex, double complex
in the Mips ABI.
Helper functions in libc for mips16 are available to do this.
For efficiency purposes, these helper functions have a different calling
convention from normal Mips calls.
Registers v0,v1,a0,a1 are used to pass parameters instead of
a0,a1,a2,a3.
This is because v0,v1,a0,a1 are the natural registers used to return
floating point values in soft float. These values can then be moved
to the appropriate floating point registers with no extra cost.
The only register that is modified is ra in this call.
The helper functions make sure that the return values are in the floating
point registers that they would be in if soft float was not in effect
(which it is for mips16, though the soft float is implemented using a mips32
library that uses hard float).
llvm-svn: 181641
2013-05-11 06:25:39 +08:00
|
|
|
// 4) TBD. For pic, calls to extern functions of unknown type are handled by
|
|
|
|
// predefined helper functions in libc but this work is currently done
|
|
|
|
// during call lowering but it should be moved here in the future.
|
|
|
|
//
|
|
|
|
bool Mips16HardFloat::runOnModule(Module &M) {
|
|
|
|
DEBUG(errs() << "Run on Module Mips16HardFloat\n");
|
|
|
|
bool Modified = false;
|
|
|
|
for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
|
2013-08-31 03:40:56 +08:00
|
|
|
if (F->hasFnAttribute("nomips16") &&
|
|
|
|
F->hasFnAttribute("use-soft-float")) {
|
|
|
|
removeUseSoftFloat(*F);
|
|
|
|
continue;
|
|
|
|
}
|
Checkin in of first of several patches to finish implementation of
mips16/mips32 floating point interoperability.
This patch fixes returns from mips16 functions so that if the function
was in fact called by a mips32 hard float routine, then values
that would have been returned in floating point registers are so returned.
Mips16 mode has no floating point instructions so there is no way to
load values into floating point registers.
This is needed when returning float, double, single complex, double complex
in the Mips ABI.
Helper functions in libc for mips16 are available to do this.
For efficiency purposes, these helper functions have a different calling
convention from normal Mips calls.
Registers v0,v1,a0,a1 are used to pass parameters instead of
a0,a1,a2,a3.
This is because v0,v1,a0,a1 are the natural registers used to return
floating point values in soft float. These values can then be moved
to the appropriate floating point registers with no extra cost.
The only register that is modified is ra in this call.
The helper functions make sure that the return values are in the floating
point registers that they would be in if soft float was not in effect
(which it is for mips16, though the soft float is implemented using a mips32
library that uses hard float).
llvm-svn: 181641
2013-05-11 06:25:39 +08:00
|
|
|
if (F->isDeclaration() || F->hasFnAttribute("mips16_fp_stub") ||
|
|
|
|
F->hasFnAttribute("nomips16")) continue;
|
|
|
|
Modified |= fixupFPReturnAndCall(*F, &M, Subtarget);
|
2013-05-16 10:17:42 +08:00
|
|
|
FPParamVariant V = whichFPParamVariantNeeded(*F);
|
|
|
|
if (V != NoSig) {
|
|
|
|
Modified = true;
|
|
|
|
createFPFnStub(F, &M, V, Subtarget);
|
|
|
|
}
|
Checkin in of first of several patches to finish implementation of
mips16/mips32 floating point interoperability.
This patch fixes returns from mips16 functions so that if the function
was in fact called by a mips32 hard float routine, then values
that would have been returned in floating point registers are so returned.
Mips16 mode has no floating point instructions so there is no way to
load values into floating point registers.
This is needed when returning float, double, single complex, double complex
in the Mips ABI.
Helper functions in libc for mips16 are available to do this.
For efficiency purposes, these helper functions have a different calling
convention from normal Mips calls.
Registers v0,v1,a0,a1 are used to pass parameters instead of
a0,a1,a2,a3.
This is because v0,v1,a0,a1 are the natural registers used to return
floating point values in soft float. These values can then be moved
to the appropriate floating point registers with no extra cost.
The only register that is modified is ra in this call.
The helper functions make sure that the return values are in the floating
point registers that they would be in if soft float was not in effect
(which it is for mips16, though the soft float is implemented using a mips32
library that uses hard float).
llvm-svn: 181641
2013-05-11 06:25:39 +08:00
|
|
|
}
|
|
|
|
return Modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
char Mips16HardFloat::ID = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ModulePass *llvm::createMips16HardFloat(MipsTargetMachine &TM) {
|
|
|
|
return new Mips16HardFloat(TM);
|
|
|
|
}
|
|
|
|
|