llvm-6502/lib/Target/Mips/Mips16HardFloat.cpp
Reed Kotler 46090914b7 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).
 


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@181641 91177308-0d34-0410-b5e6-96231b3b80d8
2013-05-10 22:25:39 +00:00

142 lines
5.0 KiB
C++

//===---- 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
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "mips16-hard-float"
#include "Mips16HardFloat.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include <string>
//
// 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;
}
//
// Returns of float, double and complex need to be handled with a helper
// function. The "AndCal" part is coming in a later patch.
//
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);
Value *F = (M->getOrInsertFunction(Name, A, MyVoid, T, NULL));
CallInst::Create(F, Params, "", &Inst );
}
}
return Modified;
}
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.
// 2) generate helper functions (stubs) that can be called by mips32 functions
// that will move parameters passed normally passed in floating point
// registers the soft float equivalents. (Coming in a later patch).
// 3) in the case of static relocation, generate helper functions so that
// mips16 functions can call extern functions of unknown type (mips16 or
// mips32). (Coming in a later patch).
// 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) {
if (F->isDeclaration() || F->hasFnAttribute("mips16_fp_stub") ||
F->hasFnAttribute("nomips16")) continue;
Modified |= fixupFPReturnAndCall(*F, &M, Subtarget);
}
return Modified;
}
char Mips16HardFloat::ID = 0;
}
ModulePass *llvm::createMips16HardFloat(MipsTargetMachine &TM) {
return new Mips16HardFloat(TM);
}