Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 1 | //===---- Mips16HardFloat.cpp for Mips16 Hard Float --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a pass needed for Mips16 Hard Float |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 14 | #include "Mips16HardFloat.h" |
Benjamin Kramer | a52f696 | 2015-03-09 15:50:58 +0000 | [diff] [blame^] | 15 | #include "MipsTargetMachine.h" |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Module.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Value.h" |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
Reed Kotler | d265e88 | 2013-08-11 21:30:27 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 21 | #include <string> |
Benjamin Kramer | a52f696 | 2015-03-09 15:50:58 +0000 | [diff] [blame^] | 22 | using namespace llvm; |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 23 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 24 | #define DEBUG_TYPE "mips16-hard-float" |
| 25 | |
Reed Kotler | 2c4657d | 2013-05-14 02:00:24 +0000 | [diff] [blame] | 26 | static void inlineAsmOut |
| 27 | (LLVMContext &C, StringRef AsmString, BasicBlock *BB ) { |
| 28 | std::vector<llvm::Type *> AsmArgTypes; |
| 29 | std::vector<llvm::Value*> AsmArgs; |
| 30 | llvm::FunctionType *AsmFTy = |
| 31 | llvm::FunctionType::get(Type::getVoidTy(C), |
| 32 | AsmArgTypes, false); |
| 33 | llvm::InlineAsm *IA = |
| 34 | llvm::InlineAsm::get(AsmFTy, AsmString, "", true, |
| 35 | /* IsAlignStack */ false, |
| 36 | llvm::InlineAsm::AD_ATT); |
| 37 | CallInst::Create(IA, AsmArgs, "", BB); |
| 38 | } |
| 39 | |
| 40 | namespace { |
| 41 | |
| 42 | class InlineAsmHelper { |
| 43 | LLVMContext &C; |
| 44 | BasicBlock *BB; |
| 45 | public: |
| 46 | InlineAsmHelper(LLVMContext &C_, BasicBlock *BB_) : |
| 47 | C(C_), BB(BB_) { |
| 48 | } |
| 49 | |
| 50 | void Out(StringRef AsmString) { |
| 51 | inlineAsmOut(C, AsmString, BB); |
| 52 | } |
| 53 | |
| 54 | }; |
| 55 | } |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 56 | // |
| 57 | // Return types that matter for hard float are: |
| 58 | // float, double, complex float, and complex double |
| 59 | // |
| 60 | enum FPReturnVariant { |
| 61 | FRet, DRet, CFRet, CDRet, NoFPRet |
| 62 | }; |
| 63 | |
| 64 | // |
| 65 | // Determine which FP return type this function has |
| 66 | // |
| 67 | static FPReturnVariant whichFPReturnVariant(Type *T) { |
| 68 | switch (T->getTypeID()) { |
| 69 | case Type::FloatTyID: |
| 70 | return FRet; |
| 71 | case Type::DoubleTyID: |
| 72 | return DRet; |
| 73 | case Type::StructTyID: |
| 74 | if (T->getStructNumElements() != 2) |
| 75 | break; |
| 76 | if ((T->getContainedType(0)->isFloatTy()) && |
| 77 | (T->getContainedType(1)->isFloatTy())) |
| 78 | return CFRet; |
| 79 | if ((T->getContainedType(0)->isDoubleTy()) && |
| 80 | (T->getContainedType(1)->isDoubleTy())) |
| 81 | return CDRet; |
| 82 | break; |
| 83 | default: |
| 84 | break; |
| 85 | } |
| 86 | return NoFPRet; |
| 87 | } |
| 88 | |
| 89 | // |
Reed Kotler | 2c4657d | 2013-05-14 02:00:24 +0000 | [diff] [blame] | 90 | // Parameter type that matter are float, (float, float), (float, double), |
| 91 | // double, (double, double), (double, float) |
| 92 | // |
| 93 | enum FPParamVariant { |
| 94 | FSig, FFSig, FDSig, |
| 95 | DSig, DDSig, DFSig, NoSig |
| 96 | }; |
| 97 | |
| 98 | // which floating point parameter signature variant we are dealing with |
| 99 | // |
| 100 | typedef Type::TypeID TypeID; |
| 101 | const Type::TypeID FloatTyID = Type::FloatTyID; |
| 102 | const Type::TypeID DoubleTyID = Type::DoubleTyID; |
| 103 | |
| 104 | static FPParamVariant whichFPParamVariantNeeded(Function &F) { |
| 105 | switch (F.arg_size()) { |
| 106 | case 0: |
| 107 | return NoSig; |
| 108 | case 1:{ |
| 109 | TypeID ArgTypeID = F.getFunctionType()->getParamType(0)->getTypeID(); |
| 110 | switch (ArgTypeID) { |
| 111 | case FloatTyID: |
| 112 | return FSig; |
| 113 | case DoubleTyID: |
| 114 | return DSig; |
| 115 | default: |
| 116 | return NoSig; |
| 117 | } |
| 118 | } |
| 119 | default: { |
| 120 | TypeID ArgTypeID0 = F.getFunctionType()->getParamType(0)->getTypeID(); |
| 121 | TypeID ArgTypeID1 = F.getFunctionType()->getParamType(1)->getTypeID(); |
| 122 | switch(ArgTypeID0) { |
| 123 | case FloatTyID: { |
| 124 | switch (ArgTypeID1) { |
| 125 | case FloatTyID: |
| 126 | return FFSig; |
| 127 | case DoubleTyID: |
| 128 | return FDSig; |
| 129 | default: |
| 130 | return FSig; |
| 131 | } |
| 132 | } |
| 133 | case DoubleTyID: { |
| 134 | switch (ArgTypeID1) { |
| 135 | case FloatTyID: |
| 136 | return DFSig; |
| 137 | case DoubleTyID: |
| 138 | return DDSig; |
| 139 | default: |
| 140 | return DSig; |
| 141 | } |
| 142 | } |
| 143 | default: |
| 144 | return NoSig; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | llvm_unreachable("can't get here"); |
| 149 | } |
| 150 | |
| 151 | // Figure out if we need float point based on the function parameters. |
| 152 | // We need to move variables in and/or out of floating point |
| 153 | // registers because of the ABI |
| 154 | // |
| 155 | static bool needsFPStubFromParams(Function &F) { |
| 156 | if (F.arg_size() >=1) { |
| 157 | Type *ArgType = F.getFunctionType()->getParamType(0); |
| 158 | switch (ArgType->getTypeID()) { |
| 159 | case Type::FloatTyID: |
| 160 | case Type::DoubleTyID: |
| 161 | return true; |
| 162 | default: |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | static bool needsFPReturnHelper(Function &F) { |
| 170 | Type* RetType = F.getReturnType(); |
| 171 | return whichFPReturnVariant(RetType) != NoFPRet; |
| 172 | } |
| 173 | |
Reed Kotler | 2500bd6 | 2013-12-18 23:57:48 +0000 | [diff] [blame] | 174 | static bool needsFPReturnHelper(const FunctionType &FT) { |
| 175 | Type* RetType = FT.getReturnType(); |
| 176 | return whichFPReturnVariant(RetType) != NoFPRet; |
| 177 | } |
| 178 | |
Reed Kotler | 2c4657d | 2013-05-14 02:00:24 +0000 | [diff] [blame] | 179 | static bool needsFPHelperFromSig(Function &F) { |
| 180 | return needsFPStubFromParams(F) || needsFPReturnHelper(F); |
| 181 | } |
| 182 | |
| 183 | // |
| 184 | // We swap between FP and Integer registers to allow Mips16 and Mips32 to |
| 185 | // interoperate |
| 186 | // |
| 187 | |
Reed Kotler | cad47f0 | 2013-05-14 02:13:45 +0000 | [diff] [blame] | 188 | static void swapFPIntParams |
| 189 | (FPParamVariant PV, Module *M, InlineAsmHelper &IAH, |
| 190 | bool LE, bool ToFP) { |
Reed Kotler | 2c4657d | 2013-05-14 02:00:24 +0000 | [diff] [blame] | 191 | //LLVMContext &Context = M->getContext(); |
| 192 | std::string MI = ToFP? "mtc1 ": "mfc1 "; |
| 193 | switch (PV) { |
| 194 | case FSig: |
| 195 | IAH.Out(MI + "$$4,$$f12"); |
| 196 | break; |
| 197 | case FFSig: |
| 198 | IAH.Out(MI +"$$4,$$f12"); |
| 199 | IAH.Out(MI + "$$5,$$f14"); |
| 200 | break; |
| 201 | case FDSig: |
| 202 | IAH.Out(MI + "$$4,$$f12"); |
| 203 | if (LE) { |
| 204 | IAH.Out(MI + "$$6,$$f14"); |
| 205 | IAH.Out(MI + "$$7,$$f15"); |
| 206 | } else { |
| 207 | IAH.Out(MI + "$$7,$$f14"); |
| 208 | IAH.Out(MI + "$$6,$$f15"); |
| 209 | } |
| 210 | break; |
| 211 | case DSig: |
| 212 | if (LE) { |
| 213 | IAH.Out(MI + "$$4,$$f12"); |
| 214 | IAH.Out(MI + "$$5,$$f13"); |
| 215 | } else { |
| 216 | IAH.Out(MI + "$$5,$$f12"); |
| 217 | IAH.Out(MI + "$$4,$$f13"); |
| 218 | } |
| 219 | break; |
| 220 | case DDSig: |
| 221 | if (LE) { |
| 222 | IAH.Out(MI + "$$4,$$f12"); |
| 223 | IAH.Out(MI + "$$5,$$f13"); |
| 224 | IAH.Out(MI + "$$6,$$f14"); |
| 225 | IAH.Out(MI + "$$7,$$f15"); |
| 226 | } else { |
| 227 | IAH.Out(MI + "$$5,$$f12"); |
| 228 | IAH.Out(MI + "$$4,$$f13"); |
| 229 | IAH.Out(MI + "$$7,$$f14"); |
| 230 | IAH.Out(MI + "$$6,$$f15"); |
| 231 | } |
| 232 | break; |
| 233 | case DFSig: |
| 234 | if (LE) { |
| 235 | IAH.Out(MI + "$$4,$$f12"); |
| 236 | IAH.Out(MI + "$$5,$$f13"); |
| 237 | } else { |
| 238 | IAH.Out(MI + "$$5,$$f12"); |
| 239 | IAH.Out(MI + "$$4,$$f13"); |
| 240 | } |
| 241 | IAH.Out(MI + "$$6,$$f14"); |
| 242 | break; |
| 243 | case NoSig: |
| 244 | return; |
| 245 | } |
| 246 | } |
| 247 | // |
| 248 | // Make sure that we know we already need a stub for this function. |
| 249 | // Having called needsFPHelperFromSig |
| 250 | // |
Reed Kotler | 4cdaa7d | 2014-02-14 19:16:39 +0000 | [diff] [blame] | 251 | static void assureFPCallStub(Function &F, Module *M, |
Eric Christopher | d20ee0a | 2015-01-06 01:12:30 +0000 | [diff] [blame] | 252 | const MipsTargetMachine &TM) { |
Reed Kotler | 2c4657d | 2013-05-14 02:00:24 +0000 | [diff] [blame] | 253 | // for now we only need them for static relocation |
Eric Christopher | d20ee0a | 2015-01-06 01:12:30 +0000 | [diff] [blame] | 254 | if (TM.getRelocationModel() == Reloc::PIC_) |
Reed Kotler | 2c4657d | 2013-05-14 02:00:24 +0000 | [diff] [blame] | 255 | return; |
| 256 | LLVMContext &Context = M->getContext(); |
Eric Christopher | d20ee0a | 2015-01-06 01:12:30 +0000 | [diff] [blame] | 257 | bool LE = TM.isLittleEndian(); |
Reed Kotler | 2c4657d | 2013-05-14 02:00:24 +0000 | [diff] [blame] | 258 | std::string Name = F.getName(); |
| 259 | std::string SectionName = ".mips16.call.fp." + Name; |
Reed Kotler | 302ae6b | 2013-08-01 02:26:31 +0000 | [diff] [blame] | 260 | std::string StubName = "__call_stub_fp_" + Name; |
Reed Kotler | 2c4657d | 2013-05-14 02:00:24 +0000 | [diff] [blame] | 261 | // |
| 262 | // see if we already have the stub |
| 263 | // |
| 264 | Function *FStub = M->getFunction(StubName); |
| 265 | if (FStub && !FStub->isDeclaration()) return; |
| 266 | FStub = Function::Create(F.getFunctionType(), |
| 267 | Function::InternalLinkage, StubName, M); |
| 268 | FStub->addFnAttr("mips16_fp_stub"); |
| 269 | FStub->addFnAttr(llvm::Attribute::Naked); |
Reed Kotler | 302ae6b | 2013-08-01 02:26:31 +0000 | [diff] [blame] | 270 | FStub->addFnAttr(llvm::Attribute::NoInline); |
Reed Kotler | 2c4657d | 2013-05-14 02:00:24 +0000 | [diff] [blame] | 271 | FStub->addFnAttr(llvm::Attribute::NoUnwind); |
| 272 | FStub->addFnAttr("nomips16"); |
| 273 | FStub->setSection(SectionName); |
| 274 | BasicBlock *BB = BasicBlock::Create(Context, "entry", FStub); |
| 275 | InlineAsmHelper IAH(Context, BB); |
Reed Kotler | 302ae6b | 2013-08-01 02:26:31 +0000 | [diff] [blame] | 276 | IAH.Out(".set reorder"); |
Reed Kotler | 2c4657d | 2013-05-14 02:00:24 +0000 | [diff] [blame] | 277 | FPReturnVariant RV = whichFPReturnVariant(FStub->getReturnType()); |
| 278 | FPParamVariant PV = whichFPParamVariantNeeded(F); |
| 279 | swapFPIntParams(PV, M, IAH, LE, true); |
| 280 | if (RV != NoFPRet) { |
| 281 | IAH.Out("move $$18, $$31"); |
| 282 | IAH.Out("jal " + Name); |
| 283 | } else { |
| 284 | IAH.Out("lui $$25,%hi(" + Name + ")"); |
| 285 | IAH.Out("addiu $$25,$$25,%lo(" + Name + ")" ); |
| 286 | } |
| 287 | switch (RV) { |
| 288 | case FRet: |
| 289 | IAH.Out("mfc1 $$2,$$f0"); |
| 290 | break; |
| 291 | case DRet: |
| 292 | if (LE) { |
| 293 | IAH.Out("mfc1 $$2,$$f0"); |
| 294 | IAH.Out("mfc1 $$3,$$f1"); |
| 295 | } else { |
| 296 | IAH.Out("mfc1 $$3,$$f0"); |
| 297 | IAH.Out("mfc1 $$2,$$f1"); |
| 298 | } |
| 299 | break; |
| 300 | case CFRet: |
| 301 | if (LE) { |
| 302 | IAH.Out("mfc1 $$2,$$f0"); |
| 303 | IAH.Out("mfc1 $$3,$$f2"); |
| 304 | } else { |
| 305 | IAH.Out("mfc1 $$3,$$f0"); |
| 306 | IAH.Out("mfc1 $$3,$$f2"); |
| 307 | } |
| 308 | break; |
| 309 | case CDRet: |
| 310 | if (LE) { |
| 311 | IAH.Out("mfc1 $$4,$$f2"); |
| 312 | IAH.Out("mfc1 $$5,$$f3"); |
| 313 | IAH.Out("mfc1 $$2,$$f0"); |
| 314 | IAH.Out("mfc1 $$3,$$f1"); |
| 315 | |
| 316 | } else { |
| 317 | IAH.Out("mfc1 $$5,$$f2"); |
| 318 | IAH.Out("mfc1 $$4,$$f3"); |
| 319 | IAH.Out("mfc1 $$3,$$f0"); |
| 320 | IAH.Out("mfc1 $$2,$$f1"); |
| 321 | } |
| 322 | break; |
| 323 | case NoFPRet: |
| 324 | break; |
| 325 | } |
| 326 | if (RV != NoFPRet) |
| 327 | IAH.Out("jr $$18"); |
| 328 | else |
| 329 | IAH.Out("jr $$25"); |
| 330 | new UnreachableInst(Context, BB); |
| 331 | } |
| 332 | |
| 333 | // |
Reed Kotler | 5fdadce | 2013-09-01 04:12:59 +0000 | [diff] [blame] | 334 | // Functions that are llvm intrinsics and don't need helpers. |
Reed Kotler | d265e88 | 2013-08-11 21:30:27 +0000 | [diff] [blame] | 335 | // |
Benjamin Kramer | c9b7d47 | 2013-08-12 09:37:29 +0000 | [diff] [blame] | 336 | static const char *IntrinsicInline[] = |
Reed Kotler | 5fdadce | 2013-09-01 04:12:59 +0000 | [diff] [blame] | 337 | {"fabs", |
Reed Kotler | 339c741 | 2013-10-08 19:55:01 +0000 | [diff] [blame] | 338 | "fabsf", |
Reed Kotler | 5fdadce | 2013-09-01 04:12:59 +0000 | [diff] [blame] | 339 | "llvm.ceil.f32", "llvm.ceil.f64", |
| 340 | "llvm.copysign.f32", "llvm.copysign.f64", |
| 341 | "llvm.cos.f32", "llvm.cos.f64", |
| 342 | "llvm.exp.f32", "llvm.exp.f64", |
| 343 | "llvm.exp2.f32", "llvm.exp2.f64", |
| 344 | "llvm.fabs.f32", "llvm.fabs.f64", |
| 345 | "llvm.floor.f32", "llvm.floor.f64", |
| 346 | "llvm.fma.f32", "llvm.fma.f64", |
| 347 | "llvm.log.f32", "llvm.log.f64", |
| 348 | "llvm.log10.f32", "llvm.log10.f64", |
| 349 | "llvm.nearbyint.f32", "llvm.nearbyint.f64", |
| 350 | "llvm.pow.f32", "llvm.pow.f64", |
| 351 | "llvm.powi.f32", "llvm.powi.f64", |
| 352 | "llvm.rint.f32", "llvm.rint.f64", |
| 353 | "llvm.round.f32", "llvm.round.f64", |
| 354 | "llvm.sin.f32", "llvm.sin.f64", |
| 355 | "llvm.sqrt.f32", "llvm.sqrt.f64", |
| 356 | "llvm.trunc.f32", "llvm.trunc.f64", |
| 357 | }; |
Reed Kotler | d265e88 | 2013-08-11 21:30:27 +0000 | [diff] [blame] | 358 | |
Benjamin Kramer | c9b7d47 | 2013-08-12 09:37:29 +0000 | [diff] [blame] | 359 | static bool isIntrinsicInline(Function *F) { |
Benjamin Kramer | 502b9e1 | 2014-04-12 16:15:53 +0000 | [diff] [blame] | 360 | return std::binary_search(std::begin(IntrinsicInline), |
| 361 | std::end(IntrinsicInline), F->getName()); |
Reed Kotler | d265e88 | 2013-08-11 21:30:27 +0000 | [diff] [blame] | 362 | } |
| 363 | // |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 364 | // Returns of float, double and complex need to be handled with a helper |
Reed Kotler | 515e937 | 2013-05-16 02:17:42 +0000 | [diff] [blame] | 365 | // function. |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 366 | // |
Eric Christopher | d20ee0a | 2015-01-06 01:12:30 +0000 | [diff] [blame] | 367 | static bool fixupFPReturnAndCall(Function &F, Module *M, |
| 368 | const MipsTargetMachine &TM) { |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 369 | bool Modified = false; |
| 370 | LLVMContext &C = M->getContext(); |
| 371 | Type *MyVoid = Type::getVoidTy(C); |
| 372 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 373 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); |
| 374 | I != E; ++I) { |
| 375 | Instruction &Inst = *I; |
| 376 | if (const ReturnInst *RI = dyn_cast<ReturnInst>(I)) { |
| 377 | Value *RVal = RI->getReturnValue(); |
| 378 | if (!RVal) continue; |
| 379 | // |
| 380 | // If there is a return value and it needs a helper function, |
| 381 | // figure out which one and add a call before the actual |
| 382 | // return to this helper. The purpose of the helper is to move |
| 383 | // floating point values from their soft float return mapping to |
| 384 | // where they would have been mapped to in floating point registers. |
| 385 | // |
| 386 | Type *T = RVal->getType(); |
| 387 | FPReturnVariant RV = whichFPReturnVariant(T); |
| 388 | if (RV == NoFPRet) continue; |
| 389 | static const char* Helper[NoFPRet] = |
| 390 | {"__mips16_ret_sf", "__mips16_ret_df", "__mips16_ret_sc", |
| 391 | "__mips16_ret_dc"}; |
| 392 | const char *Name = Helper[RV]; |
| 393 | AttributeSet A; |
| 394 | Value *Params[] = {RVal}; |
| 395 | Modified = true; |
| 396 | // |
| 397 | // These helper functions have a different calling ABI so |
| 398 | // this __Mips16RetHelper indicates that so that later |
| 399 | // during call setup, the proper call lowering to the helper |
| 400 | // functions will take place. |
| 401 | // |
| 402 | A = A.addAttribute(C, AttributeSet::FunctionIndex, |
| 403 | "__Mips16RetHelper"); |
| 404 | A = A.addAttribute(C, AttributeSet::FunctionIndex, |
| 405 | Attribute::ReadNone); |
Reed Kotler | 302ae6b | 2013-08-01 02:26:31 +0000 | [diff] [blame] | 406 | A = A.addAttribute(C, AttributeSet::FunctionIndex, |
| 407 | Attribute::NoInline); |
Reid Kleckner | 343c395 | 2014-11-20 23:51:47 +0000 | [diff] [blame] | 408 | Value *F = (M->getOrInsertFunction(Name, A, MyVoid, T, nullptr)); |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 409 | CallInst::Create(F, Params, "", &Inst ); |
Reed Kotler | 2c4657d | 2013-05-14 02:00:24 +0000 | [diff] [blame] | 410 | } else if (const CallInst *CI = dyn_cast<CallInst>(I)) { |
Reed Kotler | 2500bd6 | 2013-12-18 23:57:48 +0000 | [diff] [blame] | 411 | const Value* V = CI->getCalledValue(); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 412 | const Type* T = nullptr; |
Reed Kotler | 2500bd6 | 2013-12-18 23:57:48 +0000 | [diff] [blame] | 413 | if (V) T = V->getType(); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 414 | const PointerType *PFT=nullptr; |
Reed Kotler | 2500bd6 | 2013-12-18 23:57:48 +0000 | [diff] [blame] | 415 | if (T) PFT = dyn_cast<PointerType>(T); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 416 | const FunctionType *FT=nullptr; |
Reed Kotler | 2500bd6 | 2013-12-18 23:57:48 +0000 | [diff] [blame] | 417 | if (PFT) FT = dyn_cast<FunctionType>(PFT->getElementType()); |
Reed Kotler | 0ff4001 | 2013-12-10 14:29:38 +0000 | [diff] [blame] | 418 | Function *F_ = CI->getCalledFunction(); |
Reed Kotler | 2500bd6 | 2013-12-18 23:57:48 +0000 | [diff] [blame] | 419 | if (FT && needsFPReturnHelper(*FT) && |
| 420 | !(F_ && isIntrinsicInline(F_))) { |
| 421 | Modified=true; |
| 422 | F.addFnAttr("saveS2"); |
| 423 | } |
Reed Kotler | 0ff4001 | 2013-12-10 14:29:38 +0000 | [diff] [blame] | 424 | if (F_ && !isIntrinsicInline(F_)) { |
Reed Kotler | 2c4657d | 2013-05-14 02:00:24 +0000 | [diff] [blame] | 425 | // pic mode calls are handled by already defined |
| 426 | // helper functions |
Reed Kotler | 0ff4001 | 2013-12-10 14:29:38 +0000 | [diff] [blame] | 427 | if (needsFPReturnHelper(*F_)) { |
Reed Kotler | 2c4657d | 2013-05-14 02:00:24 +0000 | [diff] [blame] | 428 | Modified=true; |
Reed Kotler | 0ff4001 | 2013-12-10 14:29:38 +0000 | [diff] [blame] | 429 | F.addFnAttr("saveS2"); |
| 430 | } |
Eric Christopher | d20ee0a | 2015-01-06 01:12:30 +0000 | [diff] [blame] | 431 | if (TM.getRelocationModel() != Reloc::PIC_ ) { |
Reed Kotler | 0ff4001 | 2013-12-10 14:29:38 +0000 | [diff] [blame] | 432 | if (needsFPHelperFromSig(*F_)) { |
Eric Christopher | d20ee0a | 2015-01-06 01:12:30 +0000 | [diff] [blame] | 433 | assureFPCallStub(*F_, M, TM); |
Reed Kotler | 0ff4001 | 2013-12-10 14:29:38 +0000 | [diff] [blame] | 434 | Modified=true; |
| 435 | } |
Reed Kotler | 2c4657d | 2013-05-14 02:00:24 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | return Modified; |
| 441 | } |
| 442 | |
Reed Kotler | 515e937 | 2013-05-16 02:17:42 +0000 | [diff] [blame] | 443 | static void createFPFnStub(Function *F, Module *M, FPParamVariant PV, |
Eric Christopher | d20ee0a | 2015-01-06 01:12:30 +0000 | [diff] [blame] | 444 | const MipsTargetMachine &TM) { |
| 445 | bool PicMode = TM.getRelocationModel() == Reloc::PIC_; |
| 446 | bool LE = TM.isLittleEndian(); |
Reed Kotler | 515e937 | 2013-05-16 02:17:42 +0000 | [diff] [blame] | 447 | LLVMContext &Context = M->getContext(); |
| 448 | std::string Name = F->getName(); |
| 449 | std::string SectionName = ".mips16.fn." + Name; |
| 450 | std::string StubName = "__fn_stub_" + Name; |
Reed Kotler | a6ce797 | 2013-09-25 20:58:50 +0000 | [diff] [blame] | 451 | std::string LocalName = "$$__fn_local_" + Name; |
Reed Kotler | 515e937 | 2013-05-16 02:17:42 +0000 | [diff] [blame] | 452 | Function *FStub = Function::Create |
| 453 | (F->getFunctionType(), |
Reed Kotler | 302ae6b | 2013-08-01 02:26:31 +0000 | [diff] [blame] | 454 | Function::InternalLinkage, StubName, M); |
Reed Kotler | 515e937 | 2013-05-16 02:17:42 +0000 | [diff] [blame] | 455 | FStub->addFnAttr("mips16_fp_stub"); |
| 456 | FStub->addFnAttr(llvm::Attribute::Naked); |
| 457 | FStub->addFnAttr(llvm::Attribute::NoUnwind); |
Reed Kotler | 302ae6b | 2013-08-01 02:26:31 +0000 | [diff] [blame] | 458 | FStub->addFnAttr(llvm::Attribute::NoInline); |
Reed Kotler | 515e937 | 2013-05-16 02:17:42 +0000 | [diff] [blame] | 459 | FStub->addFnAttr("nomips16"); |
| 460 | FStub->setSection(SectionName); |
| 461 | BasicBlock *BB = BasicBlock::Create(Context, "entry", FStub); |
| 462 | InlineAsmHelper IAH(Context, BB); |
Reed Kotler | 515e937 | 2013-05-16 02:17:42 +0000 | [diff] [blame] | 463 | if (PicMode) { |
| 464 | IAH.Out(".set noreorder"); |
Reed Kotler | a6ce797 | 2013-09-25 20:58:50 +0000 | [diff] [blame] | 465 | IAH.Out(".cpload $$25"); |
Reed Kotler | 515e937 | 2013-05-16 02:17:42 +0000 | [diff] [blame] | 466 | IAH.Out(".set reorder"); |
| 467 | IAH.Out(".reloc 0,R_MIPS_NONE," + Name); |
| 468 | IAH.Out("la $$25," + LocalName); |
| 469 | } |
Reed Kotler | 78fb291 | 2013-09-21 01:37:52 +0000 | [diff] [blame] | 470 | else { |
Reed Kotler | a6ce797 | 2013-09-25 20:58:50 +0000 | [diff] [blame] | 471 | IAH.Out("la $$25," + Name); |
Reed Kotler | 78fb291 | 2013-09-21 01:37:52 +0000 | [diff] [blame] | 472 | } |
Reed Kotler | 515e937 | 2013-05-16 02:17:42 +0000 | [diff] [blame] | 473 | swapFPIntParams(PV, M, IAH, LE, false); |
| 474 | IAH.Out("jr $$25"); |
| 475 | IAH.Out(LocalName + " = " + Name); |
| 476 | new UnreachableInst(FStub->getContext(), BB); |
| 477 | } |
| 478 | |
Reed Kotler | c03807a | 2013-08-30 19:40:56 +0000 | [diff] [blame] | 479 | // |
| 480 | // remove the use-soft-float attribute |
| 481 | // |
| 482 | static void removeUseSoftFloat(Function &F) { |
| 483 | AttributeSet A; |
| 484 | DEBUG(errs() << "removing -use-soft-float\n"); |
| 485 | A = A.addAttribute(F.getContext(), AttributeSet::FunctionIndex, |
| 486 | "use-soft-float", "false"); |
| 487 | F.removeAttributes(AttributeSet::FunctionIndex, A); |
| 488 | if (F.hasFnAttribute("use-soft-float")) { |
| 489 | DEBUG(errs() << "still has -use-soft-float\n"); |
| 490 | } |
| 491 | F.addAttributes(AttributeSet::FunctionIndex, A); |
| 492 | } |
| 493 | |
Benjamin Kramer | a52f696 | 2015-03-09 15:50:58 +0000 | [diff] [blame^] | 494 | namespace { |
| 495 | class Mips16HardFloat : public ModulePass { |
| 496 | public: |
| 497 | static char ID; |
| 498 | |
| 499 | Mips16HardFloat(MipsTargetMachine &TM_) : ModulePass(ID), TM(TM_) {} |
| 500 | |
| 501 | const char *getPassName() const override { return "MIPS16 Hard Float Pass"; } |
| 502 | bool runOnModule(Module &M) override; |
| 503 | |
| 504 | protected: |
| 505 | const MipsTargetMachine &TM; |
| 506 | }; |
| 507 | } // namespace |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 508 | |
| 509 | // |
| 510 | // This pass only makes sense when the underlying chip has floating point but |
| 511 | // we are compiling as mips16. |
| 512 | // For all mips16 functions (that are not stubs we have already generated), or |
| 513 | // declared via attributes as nomips16, we must: |
| 514 | // 1) fixup all returns of float, double, single and double complex |
| 515 | // by calling a helper function before the actual return. |
Reed Kotler | 4cdaa7d | 2014-02-14 19:16:39 +0000 | [diff] [blame] | 516 | // 2) generate helper functions (stubs) that can be called by mips32 |
| 517 | // functions that will move parameters passed normally passed in |
| 518 | // floating point |
Reed Kotler | 515e937 | 2013-05-16 02:17:42 +0000 | [diff] [blame] | 519 | // registers the soft float equivalents. |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 520 | // 3) in the case of static relocation, generate helper functions so that |
| 521 | // mips16 functions can call extern functions of unknown type (mips16 or |
Reed Kotler | 515e937 | 2013-05-16 02:17:42 +0000 | [diff] [blame] | 522 | // mips32). |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 523 | // 4) TBD. For pic, calls to extern functions of unknown type are handled by |
| 524 | // predefined helper functions in libc but this work is currently done |
| 525 | // during call lowering but it should be moved here in the future. |
| 526 | // |
| 527 | bool Mips16HardFloat::runOnModule(Module &M) { |
| 528 | DEBUG(errs() << "Run on Module Mips16HardFloat\n"); |
| 529 | bool Modified = false; |
| 530 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
Reed Kotler | c03807a | 2013-08-30 19:40:56 +0000 | [diff] [blame] | 531 | if (F->hasFnAttribute("nomips16") && |
| 532 | F->hasFnAttribute("use-soft-float")) { |
| 533 | removeUseSoftFloat(*F); |
| 534 | continue; |
| 535 | } |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 536 | if (F->isDeclaration() || F->hasFnAttribute("mips16_fp_stub") || |
| 537 | F->hasFnAttribute("nomips16")) continue; |
Eric Christopher | d20ee0a | 2015-01-06 01:12:30 +0000 | [diff] [blame] | 538 | Modified |= fixupFPReturnAndCall(*F, &M, TM); |
Reed Kotler | 515e937 | 2013-05-16 02:17:42 +0000 | [diff] [blame] | 539 | FPParamVariant V = whichFPParamVariantNeeded(*F); |
| 540 | if (V != NoSig) { |
| 541 | Modified = true; |
Eric Christopher | d20ee0a | 2015-01-06 01:12:30 +0000 | [diff] [blame] | 542 | createFPFnStub(F, &M, V, TM); |
Reed Kotler | 515e937 | 2013-05-16 02:17:42 +0000 | [diff] [blame] | 543 | } |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 544 | } |
| 545 | return Modified; |
| 546 | } |
| 547 | |
| 548 | char Mips16HardFloat::ID = 0; |
| 549 | |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 550 | ModulePass *llvm::createMips16HardFloat(MipsTargetMachine &TM) { |
| 551 | return new Mips16HardFloat(TM); |
| 552 | } |
| 553 | |