1 //===---- Mips16HardFloat.cpp for Mips16 Hard Float --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a pass needed for Mips16 Hard Float
12 //===----------------------------------------------------------------------===//
14 #include "MipsTargetMachine.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/IR/Value.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/raw_ostream.h"
24 #define DEBUG_TYPE "mips16-hard-float"
27 class Mips16HardFloat : public ModulePass {
31 Mips16HardFloat(MipsTargetMachine &TM_) : ModulePass(ID), TM(TM_) {}
33 const char *getPassName() const override {
34 return "MIPS16 Hard Float Pass";
37 bool runOnModule(Module &M) override;
40 const MipsTargetMachine &TM;
43 static void EmitInlineAsm(LLVMContext &C, BasicBlock *BB, StringRef AsmText) {
44 std::vector<llvm::Type *> AsmArgTypes;
45 std::vector<llvm::Value *> AsmArgs;
47 llvm::FunctionType *AsmFTy =
48 llvm::FunctionType::get(Type::getVoidTy(C), AsmArgTypes, false);
50 llvm::InlineAsm::get(AsmFTy, AsmText, "", true,
51 /* IsAlignStack */ false, llvm::InlineAsm::AD_ATT);
52 CallInst::Create(IA, AsmArgs, "", BB);
55 char Mips16HardFloat::ID = 0;
59 // Return types that matter for hard float are:
60 // float, double, complex float, and complex double
62 enum FPReturnVariant {
63 FRet, DRet, CFRet, CDRet, NoFPRet
67 // Determine which FP return type this function has
69 static FPReturnVariant whichFPReturnVariant(Type *T) {
70 switch (T->getTypeID()) {
73 case Type::DoubleTyID:
75 case Type::StructTyID:
76 if (T->getStructNumElements() != 2)
78 if ((T->getContainedType(0)->isFloatTy()) &&
79 (T->getContainedType(1)->isFloatTy()))
81 if ((T->getContainedType(0)->isDoubleTy()) &&
82 (T->getContainedType(1)->isDoubleTy()))
92 // Parameter type that matter are float, (float, float), (float, double),
93 // double, (double, double), (double, float)
97 DSig, DDSig, DFSig, NoSig
100 // which floating point parameter signature variant we are dealing with
102 typedef Type::TypeID TypeID;
103 const Type::TypeID FloatTyID = Type::FloatTyID;
104 const Type::TypeID DoubleTyID = Type::DoubleTyID;
106 static FPParamVariant whichFPParamVariantNeeded(Function &F) {
107 switch (F.arg_size()) {
111 TypeID ArgTypeID = F.getFunctionType()->getParamType(0)->getTypeID();
122 TypeID ArgTypeID0 = F.getFunctionType()->getParamType(0)->getTypeID();
123 TypeID ArgTypeID1 = F.getFunctionType()->getParamType(1)->getTypeID();
126 switch (ArgTypeID1) {
136 switch (ArgTypeID1) {
150 llvm_unreachable("can't get here");
153 // Figure out if we need float point based on the function parameters.
154 // We need to move variables in and/or out of floating point
155 // registers because of the ABI
157 static bool needsFPStubFromParams(Function &F) {
158 if (F.arg_size() >=1) {
159 Type *ArgType = F.getFunctionType()->getParamType(0);
160 switch (ArgType->getTypeID()) {
161 case Type::FloatTyID:
162 case Type::DoubleTyID:
171 static bool needsFPReturnHelper(Function &F) {
172 Type* RetType = F.getReturnType();
173 return whichFPReturnVariant(RetType) != NoFPRet;
176 static bool needsFPReturnHelper(FunctionType &FT) {
177 Type* RetType = FT.getReturnType();
178 return whichFPReturnVariant(RetType) != NoFPRet;
181 static bool needsFPHelperFromSig(Function &F) {
182 return needsFPStubFromParams(F) || needsFPReturnHelper(F);
186 // We swap between FP and Integer registers to allow Mips16 and Mips32 to
189 static std::string swapFPIntParams(FPParamVariant PV, Module *M, bool LE,
191 std::string MI = ToFP ? "mtc1 ": "mfc1 ";
196 AsmText += MI + "$$4, $$f12\n";
200 AsmText += MI + "$$4, $$f12\n";
201 AsmText += MI + "$$5, $$f14\n";
205 AsmText += MI + "$$4, $$f12\n";
207 AsmText += MI + "$$6, $$f14\n";
208 AsmText += MI + "$$7, $$f15\n";
210 AsmText += MI + "$$7, $$f14\n";
211 AsmText += MI + "$$6, $$f15\n";
217 AsmText += MI + "$$4, $$f12\n";
218 AsmText += MI + "$$5, $$f13\n";
220 AsmText += MI + "$$5, $$f12\n";
221 AsmText += MI + "$$4, $$f13\n";
227 AsmText += MI + "$$4, $$f12\n";
228 AsmText += MI + "$$5, $$f13\n";
229 AsmText += MI + "$$6, $$f14\n";
230 AsmText += MI + "$$7, $$f15\n";
232 AsmText += MI + "$$5, $$f12\n";
233 AsmText += MI + "$$4, $$f13\n";
234 AsmText += MI + "$$7, $$f14\n";
235 AsmText += MI + "$$6, $$f15\n";
241 AsmText += MI + "$$4, $$f12\n";
242 AsmText += MI + "$$5, $$f13\n";
244 AsmText += MI + "$$5, $$f12\n";
245 AsmText += MI + "$$4, $$f13\n";
247 AsmText += MI + "$$6, $$f14\n";
258 // Make sure that we know we already need a stub for this function.
259 // Having called needsFPHelperFromSig
261 static void assureFPCallStub(Function &F, Module *M,
262 const MipsTargetMachine &TM) {
263 // for now we only need them for static relocation
264 if (TM.getRelocationModel() == Reloc::PIC_)
266 LLVMContext &Context = M->getContext();
267 bool LE = TM.isLittleEndian();
268 std::string Name = F.getName();
269 std::string SectionName = ".mips16.call.fp." + Name;
270 std::string StubName = "__call_stub_fp_" + Name;
272 // see if we already have the stub
274 Function *FStub = M->getFunction(StubName);
275 if (FStub && !FStub->isDeclaration()) return;
276 FStub = Function::Create(F.getFunctionType(),
277 Function::InternalLinkage, StubName, M);
278 FStub->addFnAttr("mips16_fp_stub");
279 FStub->addFnAttr(llvm::Attribute::Naked);
280 FStub->addFnAttr(llvm::Attribute::NoInline);
281 FStub->addFnAttr(llvm::Attribute::NoUnwind);
282 FStub->addFnAttr("nomips16");
283 FStub->setSection(SectionName);
284 BasicBlock *BB = BasicBlock::Create(Context, "entry", FStub);
285 FPReturnVariant RV = whichFPReturnVariant(FStub->getReturnType());
286 FPParamVariant PV = whichFPParamVariantNeeded(F);
289 AsmText += ".set reorder\n";
290 AsmText += swapFPIntParams(PV, M, LE, true);
292 AsmText += "move $$18, $$31\n";
293 AsmText += "jal " + Name + "\n";
295 AsmText += "lui $$25, %hi(" + Name + ")\n";
296 AsmText += "addiu $$25, $$25, %lo(" + Name + ")\n";
301 AsmText += "mfc1 $$2, $$f0\n";
306 AsmText += "mfc1 $$2, $$f0\n";
307 AsmText += "mfc1 $$3, $$f1\n";
309 AsmText += "mfc1 $$3, $$f0\n";
310 AsmText += "mfc1 $$2, $$f1\n";
316 AsmText += "mfc1 $$2, $$f0\n";
317 AsmText += "mfc1 $$3, $$f2\n";
319 AsmText += "mfc1 $$3, $$f0\n";
320 AsmText += "mfc1 $$3, $$f2\n";
326 AsmText += "mfc1 $$4, $$f2\n";
327 AsmText += "mfc1 $$5, $$f3\n";
328 AsmText += "mfc1 $$2, $$f0\n";
329 AsmText += "mfc1 $$3, $$f1\n";
332 AsmText += "mfc1 $$5, $$f2\n";
333 AsmText += "mfc1 $$4, $$f3\n";
334 AsmText += "mfc1 $$3, $$f0\n";
335 AsmText += "mfc1 $$2, $$f1\n";
344 AsmText += "jr $$18\n";
346 AsmText += "jr $$25\n";
347 EmitInlineAsm(Context, BB, AsmText);
349 new UnreachableInst(Context, BB);
353 // Functions that are llvm intrinsics and don't need helpers.
355 static const char *const IntrinsicInline[] = {
357 "llvm.ceil.f32", "llvm.ceil.f64",
358 "llvm.copysign.f32", "llvm.copysign.f64",
359 "llvm.cos.f32", "llvm.cos.f64",
360 "llvm.exp.f32", "llvm.exp.f64",
361 "llvm.exp2.f32", "llvm.exp2.f64",
362 "llvm.fabs.f32", "llvm.fabs.f64",
363 "llvm.floor.f32", "llvm.floor.f64",
364 "llvm.fma.f32", "llvm.fma.f64",
365 "llvm.log.f32", "llvm.log.f64",
366 "llvm.log10.f32", "llvm.log10.f64",
367 "llvm.nearbyint.f32", "llvm.nearbyint.f64",
368 "llvm.pow.f32", "llvm.pow.f64",
369 "llvm.powi.f32", "llvm.powi.f64",
370 "llvm.rint.f32", "llvm.rint.f64",
371 "llvm.round.f32", "llvm.round.f64",
372 "llvm.sin.f32", "llvm.sin.f64",
373 "llvm.sqrt.f32", "llvm.sqrt.f64",
374 "llvm.trunc.f32", "llvm.trunc.f64",
377 static bool isIntrinsicInline(Function *F) {
378 return std::binary_search(std::begin(IntrinsicInline),
379 std::end(IntrinsicInline), F->getName());
382 // Returns of float, double and complex need to be handled with a helper
385 static bool fixupFPReturnAndCall(Function &F, Module *M,
386 const MipsTargetMachine &TM) {
387 bool Modified = false;
388 LLVMContext &C = M->getContext();
389 Type *MyVoid = Type::getVoidTy(C);
390 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
391 for (BasicBlock::iterator I = BB->begin(), E = BB->end();
393 Instruction &Inst = *I;
394 if (const ReturnInst *RI = dyn_cast<ReturnInst>(I)) {
395 Value *RVal = RI->getReturnValue();
398 // If there is a return value and it needs a helper function,
399 // figure out which one and add a call before the actual
400 // return to this helper. The purpose of the helper is to move
401 // floating point values from their soft float return mapping to
402 // where they would have been mapped to in floating point registers.
404 Type *T = RVal->getType();
405 FPReturnVariant RV = whichFPReturnVariant(T);
406 if (RV == NoFPRet) continue;
407 static const char *const Helper[NoFPRet] = {
408 "__mips16_ret_sf", "__mips16_ret_df", "__mips16_ret_sc",
411 const char *Name = Helper[RV];
413 Value *Params[] = {RVal};
416 // These helper functions have a different calling ABI so
417 // this __Mips16RetHelper indicates that so that later
418 // during call setup, the proper call lowering to the helper
419 // functions will take place.
421 A = A.addAttribute(C, AttributeSet::FunctionIndex,
422 "__Mips16RetHelper");
423 A = A.addAttribute(C, AttributeSet::FunctionIndex,
424 Attribute::ReadNone);
425 A = A.addAttribute(C, AttributeSet::FunctionIndex,
426 Attribute::NoInline);
427 Value *F = (M->getOrInsertFunction(Name, A, MyVoid, T, nullptr));
428 CallInst::Create(F, Params, "", &Inst );
429 } else if (const CallInst *CI = dyn_cast<CallInst>(I)) {
430 const Value* V = CI->getCalledValue();
432 if (V) T = V->getType();
433 PointerType *PFT = nullptr;
434 if (T) PFT = dyn_cast<PointerType>(T);
435 FunctionType *FT = nullptr;
436 if (PFT) FT = dyn_cast<FunctionType>(PFT->getElementType());
437 Function *F_ = CI->getCalledFunction();
438 if (FT && needsFPReturnHelper(*FT) &&
439 !(F_ && isIntrinsicInline(F_))) {
441 F.addFnAttr("saveS2");
443 if (F_ && !isIntrinsicInline(F_)) {
444 // pic mode calls are handled by already defined
446 if (needsFPReturnHelper(*F_)) {
448 F.addFnAttr("saveS2");
450 if (TM.getRelocationModel() != Reloc::PIC_ ) {
451 if (needsFPHelperFromSig(*F_)) {
452 assureFPCallStub(*F_, M, TM);
462 static void createFPFnStub(Function *F, Module *M, FPParamVariant PV,
463 const MipsTargetMachine &TM) {
464 bool PicMode = TM.getRelocationModel() == Reloc::PIC_;
465 bool LE = TM.isLittleEndian();
466 LLVMContext &Context = M->getContext();
467 std::string Name = F->getName();
468 std::string SectionName = ".mips16.fn." + Name;
469 std::string StubName = "__fn_stub_" + Name;
470 std::string LocalName = "$$__fn_local_" + Name;
471 Function *FStub = Function::Create
472 (F->getFunctionType(),
473 Function::InternalLinkage, StubName, M);
474 FStub->addFnAttr("mips16_fp_stub");
475 FStub->addFnAttr(llvm::Attribute::Naked);
476 FStub->addFnAttr(llvm::Attribute::NoUnwind);
477 FStub->addFnAttr(llvm::Attribute::NoInline);
478 FStub->addFnAttr("nomips16");
479 FStub->setSection(SectionName);
480 BasicBlock *BB = BasicBlock::Create(Context, "entry", FStub);
484 AsmText += ".set noreorder\n";
485 AsmText += ".cpload $$25\n";
486 AsmText += ".set reorder\n";
487 AsmText += ".reloc 0, R_MIPS_NONE, " + Name + "\n";
488 AsmText += "la $$25, " + LocalName + "\n";
490 AsmText += "la $$25, " + Name + "\n";
491 AsmText += swapFPIntParams(PV, M, LE, false);
492 AsmText += "jr $$25\n";
493 AsmText += LocalName + " = " + Name + "\n";
494 EmitInlineAsm(Context, BB, AsmText);
496 new UnreachableInst(FStub->getContext(), BB);
500 // remove the use-soft-float attribute
502 static void removeUseSoftFloat(Function &F) {
504 DEBUG(errs() << "removing -use-soft-float\n");
505 A = A.addAttribute(F.getContext(), AttributeSet::FunctionIndex,
506 "use-soft-float", "false");
507 F.removeAttributes(AttributeSet::FunctionIndex, A);
508 if (F.hasFnAttribute("use-soft-float")) {
509 DEBUG(errs() << "still has -use-soft-float\n");
511 F.addAttributes(AttributeSet::FunctionIndex, A);
516 // This pass only makes sense when the underlying chip has floating point but
517 // we are compiling as mips16.
518 // For all mips16 functions (that are not stubs we have already generated), or
519 // declared via attributes as nomips16, we must:
520 // 1) fixup all returns of float, double, single and double complex
521 // by calling a helper function before the actual return.
522 // 2) generate helper functions (stubs) that can be called by mips32
523 // functions that will move parameters passed normally passed in
525 // registers the soft float equivalents.
526 // 3) in the case of static relocation, generate helper functions so that
527 // mips16 functions can call extern functions of unknown type (mips16 or
529 // 4) TBD. For pic, calls to extern functions of unknown type are handled by
530 // predefined helper functions in libc but this work is currently done
531 // during call lowering but it should be moved here in the future.
533 bool Mips16HardFloat::runOnModule(Module &M) {
534 DEBUG(errs() << "Run on Module Mips16HardFloat\n");
535 bool Modified = false;
536 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
537 if (F->hasFnAttribute("nomips16") &&
538 F->hasFnAttribute("use-soft-float")) {
539 removeUseSoftFloat(*F);
542 if (F->isDeclaration() || F->hasFnAttribute("mips16_fp_stub") ||
543 F->hasFnAttribute("nomips16")) continue;
544 Modified |= fixupFPReturnAndCall(*F, &M, TM);
545 FPParamVariant V = whichFPParamVariantNeeded(*F);
548 createFPFnStub(&*F, &M, V, TM);
555 ModulePass *llvm::createMips16HardFloatPass(MipsTargetMachine &TM) {
556 return new Mips16HardFloat(TM);