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 #define DEBUG_TYPE "mips16-hard-float"
15 #include "Mips16HardFloat.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/raw_ostream.h"
22 static void inlineAsmOut
23 (LLVMContext &C, StringRef AsmString, BasicBlock *BB ) {
24 std::vector<llvm::Type *> AsmArgTypes;
25 std::vector<llvm::Value*> AsmArgs;
26 llvm::FunctionType *AsmFTy =
27 llvm::FunctionType::get(Type::getVoidTy(C),
30 llvm::InlineAsm::get(AsmFTy, AsmString, "", true,
31 /* IsAlignStack */ false,
32 llvm::InlineAsm::AD_ATT);
33 CallInst::Create(IA, AsmArgs, "", BB);
38 class InlineAsmHelper {
42 InlineAsmHelper(LLVMContext &C_, BasicBlock *BB_) :
46 void Out(StringRef AsmString) {
47 inlineAsmOut(C, AsmString, BB);
53 // Return types that matter for hard float are:
54 // float, double, complex float, and complex double
56 enum FPReturnVariant {
57 FRet, DRet, CFRet, CDRet, NoFPRet
61 // Determine which FP return type this function has
63 static FPReturnVariant whichFPReturnVariant(Type *T) {
64 switch (T->getTypeID()) {
67 case Type::DoubleTyID:
69 case Type::StructTyID:
70 if (T->getStructNumElements() != 2)
72 if ((T->getContainedType(0)->isFloatTy()) &&
73 (T->getContainedType(1)->isFloatTy()))
75 if ((T->getContainedType(0)->isDoubleTy()) &&
76 (T->getContainedType(1)->isDoubleTy()))
86 // Parameter type that matter are float, (float, float), (float, double),
87 // double, (double, double), (double, float)
91 DSig, DDSig, DFSig, NoSig
94 // which floating point parameter signature variant we are dealing with
96 typedef Type::TypeID TypeID;
97 const Type::TypeID FloatTyID = Type::FloatTyID;
98 const Type::TypeID DoubleTyID = Type::DoubleTyID;
100 static FPParamVariant whichFPParamVariantNeeded(Function &F) {
101 switch (F.arg_size()) {
105 TypeID ArgTypeID = F.getFunctionType()->getParamType(0)->getTypeID();
116 TypeID ArgTypeID0 = F.getFunctionType()->getParamType(0)->getTypeID();
117 TypeID ArgTypeID1 = F.getFunctionType()->getParamType(1)->getTypeID();
120 switch (ArgTypeID1) {
130 switch (ArgTypeID1) {
144 llvm_unreachable("can't get here");
147 // Figure out if we need float point based on the function parameters.
148 // We need to move variables in and/or out of floating point
149 // registers because of the ABI
151 static bool needsFPStubFromParams(Function &F) {
152 if (F.arg_size() >=1) {
153 Type *ArgType = F.getFunctionType()->getParamType(0);
154 switch (ArgType->getTypeID()) {
155 case Type::FloatTyID:
156 case Type::DoubleTyID:
165 static bool needsFPReturnHelper(Function &F) {
166 Type* RetType = F.getReturnType();
167 return whichFPReturnVariant(RetType) != NoFPRet;
170 static bool needsFPHelperFromSig(Function &F) {
171 return needsFPStubFromParams(F) || needsFPReturnHelper(F);
175 // We swap between FP and Integer registers to allow Mips16 and Mips32 to
179 static void swapFPIntParams
180 (FPParamVariant PV, Module *M, InlineAsmHelper &IAH,
181 bool LE, bool ToFP) {
182 //LLVMContext &Context = M->getContext();
183 std::string MI = ToFP? "mtc1 ": "mfc1 ";
186 IAH.Out(MI + "$$4,$$f12");
189 IAH.Out(MI +"$$4,$$f12");
190 IAH.Out(MI + "$$5,$$f14");
193 IAH.Out(MI + "$$4,$$f12");
195 IAH.Out(MI + "$$6,$$f14");
196 IAH.Out(MI + "$$7,$$f15");
198 IAH.Out(MI + "$$7,$$f14");
199 IAH.Out(MI + "$$6,$$f15");
204 IAH.Out(MI + "$$4,$$f12");
205 IAH.Out(MI + "$$5,$$f13");
207 IAH.Out(MI + "$$5,$$f12");
208 IAH.Out(MI + "$$4,$$f13");
213 IAH.Out(MI + "$$4,$$f12");
214 IAH.Out(MI + "$$5,$$f13");
215 IAH.Out(MI + "$$6,$$f14");
216 IAH.Out(MI + "$$7,$$f15");
218 IAH.Out(MI + "$$5,$$f12");
219 IAH.Out(MI + "$$4,$$f13");
220 IAH.Out(MI + "$$7,$$f14");
221 IAH.Out(MI + "$$6,$$f15");
226 IAH.Out(MI + "$$4,$$f12");
227 IAH.Out(MI + "$$5,$$f13");
229 IAH.Out(MI + "$$5,$$f12");
230 IAH.Out(MI + "$$4,$$f13");
232 IAH.Out(MI + "$$6,$$f14");
239 // Make sure that we know we already need a stub for this function.
240 // Having called needsFPHelperFromSig
242 static void assureFPCallStub(Function &F, Module *M,
243 const MipsSubtarget &Subtarget){
244 // for now we only need them for static relocation
245 if (Subtarget.getRelocationModel() == Reloc::PIC_)
247 LLVMContext &Context = M->getContext();
248 bool LE = Subtarget.isLittle();
249 std::string Name = F.getName();
250 std::string SectionName = ".mips16.call.fp." + Name;
251 std::string StubName = "__call_stub_fp_" + Name;
253 // see if we already have the stub
255 Function *FStub = M->getFunction(StubName);
256 if (FStub && !FStub->isDeclaration()) return;
257 FStub = Function::Create(F.getFunctionType(),
258 Function::InternalLinkage, StubName, M);
259 FStub->addFnAttr("mips16_fp_stub");
260 FStub->addFnAttr(llvm::Attribute::Naked);
261 FStub->addFnAttr(llvm::Attribute::NoInline);
262 FStub->addFnAttr(llvm::Attribute::NoUnwind);
263 FStub->addFnAttr("nomips16");
264 FStub->setSection(SectionName);
265 BasicBlock *BB = BasicBlock::Create(Context, "entry", FStub);
266 InlineAsmHelper IAH(Context, BB);
267 IAH.Out(".set reorder");
268 FPReturnVariant RV = whichFPReturnVariant(FStub->getReturnType());
269 FPParamVariant PV = whichFPParamVariantNeeded(F);
270 swapFPIntParams(PV, M, IAH, LE, true);
272 IAH.Out("move $$18, $$31");
273 IAH.Out("jal " + Name);
275 IAH.Out("lui $$25,%hi(" + Name + ")");
276 IAH.Out("addiu $$25,$$25,%lo(" + Name + ")" );
280 IAH.Out("mfc1 $$2,$$f0");
284 IAH.Out("mfc1 $$2,$$f0");
285 IAH.Out("mfc1 $$3,$$f1");
287 IAH.Out("mfc1 $$3,$$f0");
288 IAH.Out("mfc1 $$2,$$f1");
293 IAH.Out("mfc1 $$2,$$f0");
294 IAH.Out("mfc1 $$3,$$f2");
296 IAH.Out("mfc1 $$3,$$f0");
297 IAH.Out("mfc1 $$3,$$f2");
302 IAH.Out("mfc1 $$4,$$f2");
303 IAH.Out("mfc1 $$5,$$f3");
304 IAH.Out("mfc1 $$2,$$f0");
305 IAH.Out("mfc1 $$3,$$f1");
308 IAH.Out("mfc1 $$5,$$f2");
309 IAH.Out("mfc1 $$4,$$f3");
310 IAH.Out("mfc1 $$3,$$f0");
311 IAH.Out("mfc1 $$2,$$f1");
321 new UnreachableInst(Context, BB);
325 // Functions that are inline intrinsics don't need helpers.
327 static const char *IntrinsicInline[] =
330 static bool isIntrinsicInline(Function *F) {
331 return std::binary_search(
332 IntrinsicInline, array_endof(IntrinsicInline),
336 // Returns of float, double and complex need to be handled with a helper
339 static bool fixupFPReturnAndCall
340 (Function &F, Module *M, const MipsSubtarget &Subtarget) {
341 bool Modified = false;
342 LLVMContext &C = M->getContext();
343 Type *MyVoid = Type::getVoidTy(C);
344 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
345 for (BasicBlock::iterator I = BB->begin(), E = BB->end();
347 Instruction &Inst = *I;
348 if (const ReturnInst *RI = dyn_cast<ReturnInst>(I)) {
349 Value *RVal = RI->getReturnValue();
352 // If there is a return value and it needs a helper function,
353 // figure out which one and add a call before the actual
354 // return to this helper. The purpose of the helper is to move
355 // floating point values from their soft float return mapping to
356 // where they would have been mapped to in floating point registers.
358 Type *T = RVal->getType();
359 FPReturnVariant RV = whichFPReturnVariant(T);
360 if (RV == NoFPRet) continue;
361 static const char* Helper[NoFPRet] =
362 {"__mips16_ret_sf", "__mips16_ret_df", "__mips16_ret_sc",
364 const char *Name = Helper[RV];
366 Value *Params[] = {RVal};
369 // These helper functions have a different calling ABI so
370 // this __Mips16RetHelper indicates that so that later
371 // during call setup, the proper call lowering to the helper
372 // functions will take place.
374 A = A.addAttribute(C, AttributeSet::FunctionIndex,
375 "__Mips16RetHelper");
376 A = A.addAttribute(C, AttributeSet::FunctionIndex,
377 Attribute::ReadNone);
378 A = A.addAttribute(C, AttributeSet::FunctionIndex,
379 Attribute::NoInline);
380 Value *F = (M->getOrInsertFunction(Name, A, MyVoid, T, NULL));
381 CallInst::Create(F, Params, "", &Inst );
382 } else if (const CallInst *CI = dyn_cast<CallInst>(I)) {
383 // pic mode calls are handled by already defined
385 if (Subtarget.getRelocationModel() != Reloc::PIC_ ) {
386 Function *F_ = CI->getCalledFunction();
387 if (F_ && !isIntrinsicInline(F_) && needsFPHelperFromSig(*F_)) {
388 assureFPCallStub(*F_, M, Subtarget);
397 static void createFPFnStub(Function *F, Module *M, FPParamVariant PV,
398 const MipsSubtarget &Subtarget ) {
399 bool PicMode = Subtarget.getRelocationModel() == Reloc::PIC_;
400 bool LE = Subtarget.isLittle();
401 LLVMContext &Context = M->getContext();
402 std::string Name = F->getName();
403 std::string SectionName = ".mips16.fn." + Name;
404 std::string StubName = "__fn_stub_" + Name;
405 std::string LocalName = "__fn_local_" + Name;
406 Function *FStub = Function::Create
407 (F->getFunctionType(),
408 Function::InternalLinkage, StubName, M);
409 FStub->addFnAttr("mips16_fp_stub");
410 FStub->addFnAttr(llvm::Attribute::Naked);
411 FStub->addFnAttr(llvm::Attribute::NoUnwind);
412 FStub->addFnAttr(llvm::Attribute::NoInline);
413 FStub->addFnAttr("nomips16");
414 FStub->setSection(SectionName);
415 BasicBlock *BB = BasicBlock::Create(Context, "entry", FStub);
416 InlineAsmHelper IAH(Context, BB);
417 IAH.Out(" .set macro");
419 IAH.Out(".set noreorder");
420 IAH.Out(".cpload $$2");
421 IAH.Out(".set reorder");
422 IAH.Out(".reloc 0,R_MIPS_NONE," + Name);
423 IAH.Out("la $$25," + LocalName);
426 IAH.Out("la $$25, " + Name);
427 swapFPIntParams(PV, M, IAH, LE, false);
429 IAH.Out(LocalName + " = " + Name);
430 new UnreachableInst(FStub->getContext(), BB);
436 // This pass only makes sense when the underlying chip has floating point but
437 // we are compiling as mips16.
438 // For all mips16 functions (that are not stubs we have already generated), or
439 // declared via attributes as nomips16, we must:
440 // 1) fixup all returns of float, double, single and double complex
441 // by calling a helper function before the actual return.
442 // 2) generate helper functions (stubs) that can be called by mips32 functions
443 // that will move parameters passed normally passed in floating point
444 // registers the soft float equivalents.
445 // 3) in the case of static relocation, generate helper functions so that
446 // mips16 functions can call extern functions of unknown type (mips16 or
448 // 4) TBD. For pic, calls to extern functions of unknown type are handled by
449 // predefined helper functions in libc but this work is currently done
450 // during call lowering but it should be moved here in the future.
452 bool Mips16HardFloat::runOnModule(Module &M) {
453 DEBUG(errs() << "Run on Module Mips16HardFloat\n");
454 bool Modified = false;
455 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
456 if (F->isDeclaration() || F->hasFnAttribute("mips16_fp_stub") ||
457 F->hasFnAttribute("nomips16")) continue;
458 Modified |= fixupFPReturnAndCall(*F, &M, Subtarget);
459 FPParamVariant V = whichFPParamVariantNeeded(*F);
462 createFPFnStub(F, &M, V, Subtarget);
468 char Mips16HardFloat::ID = 0;
472 ModulePass *llvm::createMips16HardFloat(MipsTargetMachine &TM) {
473 return new Mips16HardFloat(TM);