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"
21 static void inlineAsmOut
22 (LLVMContext &C, StringRef AsmString, BasicBlock *BB ) {
23 std::vector<llvm::Type *> AsmArgTypes;
24 std::vector<llvm::Value*> AsmArgs;
25 llvm::FunctionType *AsmFTy =
26 llvm::FunctionType::get(Type::getVoidTy(C),
29 llvm::InlineAsm::get(AsmFTy, AsmString, "", true,
30 /* IsAlignStack */ false,
31 llvm::InlineAsm::AD_ATT);
32 CallInst::Create(IA, AsmArgs, "", BB);
37 class InlineAsmHelper {
41 InlineAsmHelper(LLVMContext &C_, BasicBlock *BB_) :
45 void Out(StringRef AsmString) {
46 inlineAsmOut(C, AsmString, BB);
52 // Return types that matter for hard float are:
53 // float, double, complex float, and complex double
55 enum FPReturnVariant {
56 FRet, DRet, CFRet, CDRet, NoFPRet
60 // Determine which FP return type this function has
62 static FPReturnVariant whichFPReturnVariant(Type *T) {
63 switch (T->getTypeID()) {
66 case Type::DoubleTyID:
68 case Type::StructTyID:
69 if (T->getStructNumElements() != 2)
71 if ((T->getContainedType(0)->isFloatTy()) &&
72 (T->getContainedType(1)->isFloatTy()))
74 if ((T->getContainedType(0)->isDoubleTy()) &&
75 (T->getContainedType(1)->isDoubleTy()))
85 // Parameter type that matter are float, (float, float), (float, double),
86 // double, (double, double), (double, float)
90 DSig, DDSig, DFSig, NoSig
93 // which floating point parameter signature variant we are dealing with
95 typedef Type::TypeID TypeID;
96 const Type::TypeID FloatTyID = Type::FloatTyID;
97 const Type::TypeID DoubleTyID = Type::DoubleTyID;
99 static FPParamVariant whichFPParamVariantNeeded(Function &F) {
100 switch (F.arg_size()) {
104 TypeID ArgTypeID = F.getFunctionType()->getParamType(0)->getTypeID();
115 TypeID ArgTypeID0 = F.getFunctionType()->getParamType(0)->getTypeID();
116 TypeID ArgTypeID1 = F.getFunctionType()->getParamType(1)->getTypeID();
119 switch (ArgTypeID1) {
129 switch (ArgTypeID1) {
143 llvm_unreachable("can't get here");
146 // Figure out if we need float point based on the function parameters.
147 // We need to move variables in and/or out of floating point
148 // registers because of the ABI
150 static bool needsFPStubFromParams(Function &F) {
151 if (F.arg_size() >=1) {
152 Type *ArgType = F.getFunctionType()->getParamType(0);
153 switch (ArgType->getTypeID()) {
154 case Type::FloatTyID:
155 case Type::DoubleTyID:
164 static bool needsFPReturnHelper(Function &F) {
165 Type* RetType = F.getReturnType();
166 return whichFPReturnVariant(RetType) != NoFPRet;
169 static bool needsFPHelperFromSig(Function &F) {
170 return needsFPStubFromParams(F) || needsFPReturnHelper(F);
174 // We swap between FP and Integer registers to allow Mips16 and Mips32 to
178 void swapFPIntParams(FPParamVariant PV, Module *M, InlineAsmHelper &IAH,
179 bool LE, bool ToFP) {
180 //LLVMContext &Context = M->getContext();
181 std::string MI = ToFP? "mtc1 ": "mfc1 ";
184 IAH.Out(MI + "$$4,$$f12");
187 IAH.Out(MI +"$$4,$$f12");
188 IAH.Out(MI + "$$5,$$f14");
191 IAH.Out(MI + "$$4,$$f12");
193 IAH.Out(MI + "$$6,$$f14");
194 IAH.Out(MI + "$$7,$$f15");
196 IAH.Out(MI + "$$7,$$f14");
197 IAH.Out(MI + "$$6,$$f15");
202 IAH.Out(MI + "$$4,$$f12");
203 IAH.Out(MI + "$$5,$$f13");
205 IAH.Out(MI + "$$5,$$f12");
206 IAH.Out(MI + "$$4,$$f13");
211 IAH.Out(MI + "$$4,$$f12");
212 IAH.Out(MI + "$$5,$$f13");
213 IAH.Out(MI + "$$6,$$f14");
214 IAH.Out(MI + "$$7,$$f15");
216 IAH.Out(MI + "$$5,$$f12");
217 IAH.Out(MI + "$$4,$$f13");
218 IAH.Out(MI + "$$7,$$f14");
219 IAH.Out(MI + "$$6,$$f15");
224 IAH.Out(MI + "$$4,$$f12");
225 IAH.Out(MI + "$$5,$$f13");
227 IAH.Out(MI + "$$5,$$f12");
228 IAH.Out(MI + "$$4,$$f13");
230 IAH.Out(MI + "$$6,$$f14");
237 // Make sure that we know we already need a stub for this function.
238 // Having called needsFPHelperFromSig
240 void assureFPCallStub(Function &F, Module *M, const MipsSubtarget &Subtarget){
241 // for now we only need them for static relocation
242 if (!Subtarget.getRelocationModel() == Reloc::PIC_)
244 LLVMContext &Context = M->getContext();
245 bool LE = Subtarget.isLittle();
246 std::string Name = F.getName();
247 std::string SectionName = ".mips16.call.fp." + Name;
248 std::string StubName = "__call_stub_" + Name;
250 // see if we already have the stub
252 Function *FStub = M->getFunction(StubName);
253 if (FStub && !FStub->isDeclaration()) return;
254 FStub = Function::Create(F.getFunctionType(),
255 Function::InternalLinkage, StubName, M);
256 FStub->addFnAttr("mips16_fp_stub");
257 FStub->addFnAttr(llvm::Attribute::Naked);
258 FStub->addFnAttr(llvm::Attribute::NoUnwind);
259 FStub->addFnAttr("nomips16");
260 FStub->setSection(SectionName);
261 BasicBlock *BB = BasicBlock::Create(Context, "entry", FStub);
262 InlineAsmHelper IAH(Context, BB);
263 FPReturnVariant RV = whichFPReturnVariant(FStub->getReturnType());
264 FPParamVariant PV = whichFPParamVariantNeeded(F);
265 swapFPIntParams(PV, M, IAH, LE, true);
267 IAH.Out("move $$18, $$31");
268 IAH.Out("jal " + Name);
270 IAH.Out("lui $$25,%hi(" + Name + ")");
271 IAH.Out("addiu $$25,$$25,%lo(" + Name + ")" );
275 IAH.Out("mfc1 $$2,$$f0");
279 IAH.Out("mfc1 $$2,$$f0");
280 IAH.Out("mfc1 $$3,$$f1");
282 IAH.Out("mfc1 $$3,$$f0");
283 IAH.Out("mfc1 $$2,$$f1");
288 IAH.Out("mfc1 $$2,$$f0");
289 IAH.Out("mfc1 $$3,$$f2");
291 IAH.Out("mfc1 $$3,$$f0");
292 IAH.Out("mfc1 $$3,$$f2");
297 IAH.Out("mfc1 $$4,$$f2");
298 IAH.Out("mfc1 $$5,$$f3");
299 IAH.Out("mfc1 $$2,$$f0");
300 IAH.Out("mfc1 $$3,$$f1");
303 IAH.Out("mfc1 $$5,$$f2");
304 IAH.Out("mfc1 $$4,$$f3");
305 IAH.Out("mfc1 $$3,$$f0");
306 IAH.Out("mfc1 $$2,$$f1");
316 new UnreachableInst(Context, BB);
320 // Returns of float, double and complex need to be handled with a helper
321 // function. The "AndCal" part is coming in a later patch.
323 static bool fixupFPReturnAndCall
324 (Function &F, Module *M, const MipsSubtarget &Subtarget) {
325 bool Modified = false;
326 LLVMContext &C = M->getContext();
327 Type *MyVoid = Type::getVoidTy(C);
328 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
329 for (BasicBlock::iterator I = BB->begin(), E = BB->end();
331 Instruction &Inst = *I;
332 if (const ReturnInst *RI = dyn_cast<ReturnInst>(I)) {
333 Value *RVal = RI->getReturnValue();
336 // If there is a return value and it needs a helper function,
337 // figure out which one and add a call before the actual
338 // return to this helper. The purpose of the helper is to move
339 // floating point values from their soft float return mapping to
340 // where they would have been mapped to in floating point registers.
342 Type *T = RVal->getType();
343 FPReturnVariant RV = whichFPReturnVariant(T);
344 if (RV == NoFPRet) continue;
345 static const char* Helper[NoFPRet] =
346 {"__mips16_ret_sf", "__mips16_ret_df", "__mips16_ret_sc",
348 const char *Name = Helper[RV];
350 Value *Params[] = {RVal};
353 // These helper functions have a different calling ABI so
354 // this __Mips16RetHelper indicates that so that later
355 // during call setup, the proper call lowering to the helper
356 // functions will take place.
358 A = A.addAttribute(C, AttributeSet::FunctionIndex,
359 "__Mips16RetHelper");
360 A = A.addAttribute(C, AttributeSet::FunctionIndex,
361 Attribute::ReadNone);
362 Value *F = (M->getOrInsertFunction(Name, A, MyVoid, T, NULL));
363 CallInst::Create(F, Params, "", &Inst );
364 } else if (const CallInst *CI = dyn_cast<CallInst>(I)) {
365 // pic mode calls are handled by already defined
367 if (Subtarget.getRelocationModel() != Reloc::PIC_ ) {
368 Function *F_ = CI->getCalledFunction();
369 if (F_ && needsFPHelperFromSig(*F_)) {
370 assureFPCallStub(*F_, M, Subtarget);
382 // This pass only makes sense when the underlying chip has floating point but
383 // we are compiling as mips16.
384 // For all mips16 functions (that are not stubs we have already generated), or
385 // declared via attributes as nomips16, we must:
386 // 1) fixup all returns of float, double, single and double complex
387 // by calling a helper function before the actual return.
388 // 2) generate helper functions (stubs) that can be called by mips32 functions
389 // that will move parameters passed normally passed in floating point
390 // registers the soft float equivalents. (Coming in a later patch).
391 // 3) in the case of static relocation, generate helper functions so that
392 // mips16 functions can call extern functions of unknown type (mips16 or
393 // mips32). (Coming in a later patch).
394 // 4) TBD. For pic, calls to extern functions of unknown type are handled by
395 // predefined helper functions in libc but this work is currently done
396 // during call lowering but it should be moved here in the future.
398 bool Mips16HardFloat::runOnModule(Module &M) {
399 DEBUG(errs() << "Run on Module Mips16HardFloat\n");
400 bool Modified = false;
401 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
402 if (F->isDeclaration() || F->hasFnAttribute("mips16_fp_stub") ||
403 F->hasFnAttribute("nomips16")) continue;
404 Modified |= fixupFPReturnAndCall(*F, &M, Subtarget);
409 char Mips16HardFloat::ID = 0;
413 ModulePass *llvm::createMips16HardFloat(MipsTargetMachine &TM) {
414 return new Mips16HardFloat(TM);