1 //===-- MipsFastISel.cpp - Mips FastISel implementation --------------------===// 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 /// \file 11 /// \brief This file defines the MIPS-specific support for the FastISel class. 12 /// Some of the target-specific code is generated by tablegen in the file 13 /// MipsGenFastISel.inc, which is #included here. 14 /// 15 //===----------------------------------------------------------------------===// 16 17 #include "MipsCCState.h" 18 #include "MipsInstrInfo.h" 19 #include "MipsISelLowering.h" 20 #include "MipsMachineFunction.h" 21 #include "MipsRegisterInfo.h" 22 #include "MipsSubtarget.h" 23 #include "MipsTargetMachine.h" 24 #include "llvm/Analysis/TargetLibraryInfo.h" 25 #include "llvm/CodeGen/FastISel.h" 26 #include "llvm/CodeGen/FunctionLoweringInfo.h" 27 #include "llvm/CodeGen/MachineInstrBuilder.h" 28 #include "llvm/CodeGen/MachineRegisterInfo.h" 29 #include "llvm/IR/GetElementPtrTypeIterator.h" 30 #include "llvm/IR/GlobalAlias.h" 31 #include "llvm/IR/GlobalVariable.h" 32 #include "llvm/MC/MCSymbol.h" 33 #include "llvm/Target/TargetInstrInfo.h" 34 #include "llvm/Support/Debug.h" 35 36 #define DEBUG_TYPE "mips-fastisel" 37 38 using namespace llvm; 39 40 namespace { 41 42 class MipsFastISel final : public FastISel { 43 44 // All possible address modes. 45 class Address { 46 public: 47 typedef enum { RegBase, FrameIndexBase } BaseKind; 48 49 private: 50 BaseKind Kind; 51 union { 52 unsigned Reg; 53 int FI; 54 } Base; 55 56 int64_t Offset; 57 58 const GlobalValue *GV; 59 60 public: 61 // Innocuous defaults for our address. 62 Address() : Kind(RegBase), Offset(0), GV(0) { Base.Reg = 0; } 63 void setKind(BaseKind K) { Kind = K; } 64 BaseKind getKind() const { return Kind; } 65 bool isRegBase() const { return Kind == RegBase; } 66 bool isFIBase() const { return Kind == FrameIndexBase; } 67 void setReg(unsigned Reg) { 68 assert(isRegBase() && "Invalid base register access!"); 69 Base.Reg = Reg; 70 } 71 unsigned getReg() const { 72 assert(isRegBase() && "Invalid base register access!"); 73 return Base.Reg; 74 } 75 void setFI(unsigned FI) { 76 assert(isFIBase() && "Invalid base frame index access!"); 77 Base.FI = FI; 78 } 79 unsigned getFI() const { 80 assert(isFIBase() && "Invalid base frame index access!"); 81 return Base.FI; 82 } 83 84 void setOffset(int64_t Offset_) { Offset = Offset_; } 85 int64_t getOffset() const { return Offset; } 86 void setGlobalValue(const GlobalValue *G) { GV = G; } 87 const GlobalValue *getGlobalValue() { return GV; } 88 }; 89 90 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can 91 /// make the right decision when generating code for different targets. 92 const TargetMachine &TM; 93 const MipsSubtarget *Subtarget; 94 const TargetInstrInfo &TII; 95 const TargetLowering &TLI; 96 MipsFunctionInfo *MFI; 97 98 // Convenience variables to avoid some queries. 99 LLVMContext *Context; 100 101 bool fastLowerArguments() override; 102 bool fastLowerCall(CallLoweringInfo &CLI) override; 103 bool fastLowerIntrinsicCall(const IntrinsicInst *II) override; 104 105 bool TargetSupported; 106 bool UnsupportedFPMode; // To allow fast-isel to proceed and just not handle 107 // floating point but not reject doing fast-isel in other 108 // situations 109 110 private: 111 // Selection routines. 112 bool selectLogicalOp(const Instruction *I); 113 bool selectLoad(const Instruction *I); 114 bool selectStore(const Instruction *I); 115 bool selectBranch(const Instruction *I); 116 bool selectSelect(const Instruction *I); 117 bool selectCmp(const Instruction *I); 118 bool selectFPExt(const Instruction *I); 119 bool selectFPTrunc(const Instruction *I); 120 bool selectFPToInt(const Instruction *I, bool IsSigned); 121 bool selectRet(const Instruction *I); 122 bool selectTrunc(const Instruction *I); 123 bool selectIntExt(const Instruction *I); 124 bool selectShift(const Instruction *I); 125 bool selectDivRem(const Instruction *I, unsigned ISDOpcode); 126 127 // Utility helper routines. 128 bool isTypeLegal(Type *Ty, MVT &VT); 129 bool isTypeSupported(Type *Ty, MVT &VT); 130 bool isLoadTypeLegal(Type *Ty, MVT &VT); 131 bool computeAddress(const Value *Obj, Address &Addr); 132 bool computeCallAddress(const Value *V, Address &Addr); 133 void simplifyAddress(Address &Addr); 134 135 // Emit helper routines. 136 bool emitCmp(unsigned DestReg, const CmpInst *CI); 137 bool emitLoad(MVT VT, unsigned &ResultReg, Address &Addr, 138 unsigned Alignment = 0); 139 bool emitStore(MVT VT, unsigned SrcReg, Address Addr, 140 MachineMemOperand *MMO = nullptr); 141 bool emitStore(MVT VT, unsigned SrcReg, Address &Addr, 142 unsigned Alignment = 0); 143 unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt); 144 bool emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg, 145 146 bool IsZExt); 147 bool emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg); 148 149 bool emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg); 150 bool emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT, 151 unsigned DestReg); 152 bool emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT, 153 unsigned DestReg); 154 155 unsigned getRegEnsuringSimpleIntegerWidening(const Value *, bool IsUnsigned); 156 157 unsigned emitLogicalOp(unsigned ISDOpc, MVT RetVT, const Value *LHS, 158 const Value *RHS); 159 160 unsigned materializeFP(const ConstantFP *CFP, MVT VT); 161 unsigned materializeGV(const GlobalValue *GV, MVT VT); 162 unsigned materializeInt(const Constant *C, MVT VT); 163 unsigned materialize32BitInt(int64_t Imm, const TargetRegisterClass *RC); 164 unsigned materializeExternalCallSym(MCSymbol *Syn); 165 166 MachineInstrBuilder emitInst(unsigned Opc) { 167 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc)); 168 } 169 MachineInstrBuilder emitInst(unsigned Opc, unsigned DstReg) { 170 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), 171 DstReg); 172 } 173 MachineInstrBuilder emitInstStore(unsigned Opc, unsigned SrcReg, 174 unsigned MemReg, int64_t MemOffset) { 175 return emitInst(Opc).addReg(SrcReg).addReg(MemReg).addImm(MemOffset); 176 } 177 MachineInstrBuilder emitInstLoad(unsigned Opc, unsigned DstReg, 178 unsigned MemReg, int64_t MemOffset) { 179 return emitInst(Opc, DstReg).addReg(MemReg).addImm(MemOffset); 180 } 181 182 unsigned fastEmitInst_rr(unsigned MachineInstOpcode, 183 const TargetRegisterClass *RC, 184 unsigned Op0, bool Op0IsKill, 185 unsigned Op1, bool Op1IsKill); 186 187 // for some reason, this default is not generated by tablegen 188 // so we explicitly generate it here. 189 // 190 unsigned fastEmitInst_riir(uint64_t inst, const TargetRegisterClass *RC, 191 unsigned Op0, bool Op0IsKill, uint64_t imm1, 192 uint64_t imm2, unsigned Op3, bool Op3IsKill) { 193 return 0; 194 } 195 196 // Call handling routines. 197 private: 198 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const; 199 bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs, 200 unsigned &NumBytes); 201 bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes); 202 const MipsABIInfo &getABI() const { 203 return static_cast<const MipsTargetMachine &>(TM).getABI(); 204 } 205 206 public: 207 // Backend specific FastISel code. 208 explicit MipsFastISel(FunctionLoweringInfo &funcInfo, 209 const TargetLibraryInfo *libInfo) 210 : FastISel(funcInfo, libInfo), TM(funcInfo.MF->getTarget()), 211 Subtarget(&funcInfo.MF->getSubtarget<MipsSubtarget>()), 212 TII(*Subtarget->getInstrInfo()), TLI(*Subtarget->getTargetLowering()) { 213 MFI = funcInfo.MF->getInfo<MipsFunctionInfo>(); 214 Context = &funcInfo.Fn->getContext(); 215 bool ISASupported = !Subtarget->hasMips32r6() && 216 !Subtarget->inMicroMipsMode() && Subtarget->hasMips32(); 217 TargetSupported = 218 ISASupported && TM.isPositionIndependent() && getABI().IsO32(); 219 UnsupportedFPMode = Subtarget->isFP64bit() || Subtarget->useSoftFloat(); 220 } 221 222 unsigned fastMaterializeAlloca(const AllocaInst *AI) override; 223 unsigned fastMaterializeConstant(const Constant *C) override; 224 bool fastSelectInstruction(const Instruction *I) override; 225 226 #include "MipsGenFastISel.inc" 227 }; 228 } // end anonymous namespace. 229 230 static bool CC_Mips(unsigned ValNo, MVT ValVT, MVT LocVT, 231 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, 232 CCState &State) LLVM_ATTRIBUTE_UNUSED; 233 234 static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, MVT LocVT, 235 CCValAssign::LocInfo LocInfo, 236 ISD::ArgFlagsTy ArgFlags, CCState &State) { 237 llvm_unreachable("should not be called"); 238 } 239 240 static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, MVT LocVT, 241 CCValAssign::LocInfo LocInfo, 242 ISD::ArgFlagsTy ArgFlags, CCState &State) { 243 llvm_unreachable("should not be called"); 244 } 245 246 #include "MipsGenCallingConv.inc" 247 248 CCAssignFn *MipsFastISel::CCAssignFnForCall(CallingConv::ID CC) const { 249 return CC_MipsO32; 250 } 251 252 unsigned MipsFastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT, 253 const Value *LHS, const Value *RHS) { 254 // Canonicalize immediates to the RHS first. 255 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS)) 256 std::swap(LHS, RHS); 257 258 unsigned Opc; 259 switch (ISDOpc) { 260 case ISD::AND: 261 Opc = Mips::AND; 262 break; 263 case ISD::OR: 264 Opc = Mips::OR; 265 break; 266 case ISD::XOR: 267 Opc = Mips::XOR; 268 break; 269 default: 270 llvm_unreachable("unexpected opcode"); 271 } 272 273 unsigned LHSReg = getRegForValue(LHS); 274 if (!LHSReg) 275 return 0; 276 277 unsigned RHSReg; 278 if (const auto *C = dyn_cast<ConstantInt>(RHS)) 279 RHSReg = materializeInt(C, MVT::i32); 280 else 281 RHSReg = getRegForValue(RHS); 282 if (!RHSReg) 283 return 0; 284 285 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass); 286 if (!ResultReg) 287 return 0; 288 289 emitInst(Opc, ResultReg).addReg(LHSReg).addReg(RHSReg); 290 return ResultReg; 291 } 292 293 unsigned MipsFastISel::fastMaterializeAlloca(const AllocaInst *AI) { 294 if (!TargetSupported) 295 return 0; 296 297 assert(TLI.getValueType(DL, AI->getType(), true) == MVT::i32 && 298 "Alloca should always return a pointer."); 299 300 DenseMap<const AllocaInst *, int>::iterator SI = 301 FuncInfo.StaticAllocaMap.find(AI); 302 303 if (SI != FuncInfo.StaticAllocaMap.end()) { 304 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass); 305 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::LEA_ADDiu), 306 ResultReg) 307 .addFrameIndex(SI->second) 308 .addImm(0); 309 return ResultReg; 310 } 311 312 return 0; 313 } 314 315 unsigned MipsFastISel::materializeInt(const Constant *C, MVT VT) { 316 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1) 317 return 0; 318 const TargetRegisterClass *RC = &Mips::GPR32RegClass; 319 const ConstantInt *CI = cast<ConstantInt>(C); 320 return materialize32BitInt(CI->getZExtValue(), RC); 321 } 322 323 unsigned MipsFastISel::materialize32BitInt(int64_t Imm, 324 const TargetRegisterClass *RC) { 325 unsigned ResultReg = createResultReg(RC); 326 327 if (isInt<16>(Imm)) { 328 unsigned Opc = Mips::ADDiu; 329 emitInst(Opc, ResultReg).addReg(Mips::ZERO).addImm(Imm); 330 return ResultReg; 331 } else if (isUInt<16>(Imm)) { 332 emitInst(Mips::ORi, ResultReg).addReg(Mips::ZERO).addImm(Imm); 333 return ResultReg; 334 } 335 unsigned Lo = Imm & 0xFFFF; 336 unsigned Hi = (Imm >> 16) & 0xFFFF; 337 if (Lo) { 338 // Both Lo and Hi have nonzero bits. 339 unsigned TmpReg = createResultReg(RC); 340 emitInst(Mips::LUi, TmpReg).addImm(Hi); 341 emitInst(Mips::ORi, ResultReg).addReg(TmpReg).addImm(Lo); 342 } else { 343 emitInst(Mips::LUi, ResultReg).addImm(Hi); 344 } 345 return ResultReg; 346 } 347 348 unsigned MipsFastISel::materializeFP(const ConstantFP *CFP, MVT VT) { 349 if (UnsupportedFPMode) 350 return 0; 351 int64_t Imm = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); 352 if (VT == MVT::f32) { 353 const TargetRegisterClass *RC = &Mips::FGR32RegClass; 354 unsigned DestReg = createResultReg(RC); 355 unsigned TempReg = materialize32BitInt(Imm, &Mips::GPR32RegClass); 356 emitInst(Mips::MTC1, DestReg).addReg(TempReg); 357 return DestReg; 358 } else if (VT == MVT::f64) { 359 const TargetRegisterClass *RC = &Mips::AFGR64RegClass; 360 unsigned DestReg = createResultReg(RC); 361 unsigned TempReg1 = materialize32BitInt(Imm >> 32, &Mips::GPR32RegClass); 362 unsigned TempReg2 = 363 materialize32BitInt(Imm & 0xFFFFFFFF, &Mips::GPR32RegClass); 364 emitInst(Mips::BuildPairF64, DestReg).addReg(TempReg2).addReg(TempReg1); 365 return DestReg; 366 } 367 return 0; 368 } 369 370 unsigned MipsFastISel::materializeGV(const GlobalValue *GV, MVT VT) { 371 // For now 32-bit only. 372 if (VT != MVT::i32) 373 return 0; 374 const TargetRegisterClass *RC = &Mips::GPR32RegClass; 375 unsigned DestReg = createResultReg(RC); 376 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 377 bool IsThreadLocal = GVar && GVar->isThreadLocal(); 378 // TLS not supported at this time. 379 if (IsThreadLocal) 380 return 0; 381 emitInst(Mips::LW, DestReg) 382 .addReg(MFI->getGlobalBaseReg()) 383 .addGlobalAddress(GV, 0, MipsII::MO_GOT); 384 if ((GV->hasInternalLinkage() || 385 (GV->hasLocalLinkage() && !isa<Function>(GV)))) { 386 unsigned TempReg = createResultReg(RC); 387 emitInst(Mips::ADDiu, TempReg) 388 .addReg(DestReg) 389 .addGlobalAddress(GV, 0, MipsII::MO_ABS_LO); 390 DestReg = TempReg; 391 } 392 return DestReg; 393 } 394 395 unsigned MipsFastISel::materializeExternalCallSym(MCSymbol *Sym) { 396 const TargetRegisterClass *RC = &Mips::GPR32RegClass; 397 unsigned DestReg = createResultReg(RC); 398 emitInst(Mips::LW, DestReg) 399 .addReg(MFI->getGlobalBaseReg()) 400 .addSym(Sym, MipsII::MO_GOT); 401 return DestReg; 402 } 403 404 // Materialize a constant into a register, and return the register 405 // number (or zero if we failed to handle it). 406 unsigned MipsFastISel::fastMaterializeConstant(const Constant *C) { 407 if (!TargetSupported) 408 return 0; 409 410 EVT CEVT = TLI.getValueType(DL, C->getType(), true); 411 412 // Only handle simple types. 413 if (!CEVT.isSimple()) 414 return 0; 415 MVT VT = CEVT.getSimpleVT(); 416 417 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) 418 return (UnsupportedFPMode) ? 0 : materializeFP(CFP, VT); 419 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) 420 return materializeGV(GV, VT); 421 else if (isa<ConstantInt>(C)) 422 return materializeInt(C, VT); 423 424 return 0; 425 } 426 427 bool MipsFastISel::computeAddress(const Value *Obj, Address &Addr) { 428 429 const User *U = nullptr; 430 unsigned Opcode = Instruction::UserOp1; 431 if (const Instruction *I = dyn_cast<Instruction>(Obj)) { 432 // Don't walk into other basic blocks unless the object is an alloca from 433 // another block, otherwise it may not have a virtual register assigned. 434 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) || 435 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) { 436 Opcode = I->getOpcode(); 437 U = I; 438 } 439 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) { 440 Opcode = C->getOpcode(); 441 U = C; 442 } 443 switch (Opcode) { 444 default: 445 break; 446 case Instruction::BitCast: { 447 // Look through bitcasts. 448 return computeAddress(U->getOperand(0), Addr); 449 } 450 case Instruction::GetElementPtr: { 451 Address SavedAddr = Addr; 452 uint64_t TmpOffset = Addr.getOffset(); 453 // Iterate through the GEP folding the constants into offsets where 454 // we can. 455 gep_type_iterator GTI = gep_type_begin(U); 456 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e; 457 ++i, ++GTI) { 458 const Value *Op = *i; 459 if (StructType *STy = dyn_cast<StructType>(*GTI)) { 460 const StructLayout *SL = DL.getStructLayout(STy); 461 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue(); 462 TmpOffset += SL->getElementOffset(Idx); 463 } else { 464 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType()); 465 for (;;) { 466 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) { 467 // Constant-offset addressing. 468 TmpOffset += CI->getSExtValue() * S; 469 break; 470 } 471 if (canFoldAddIntoGEP(U, Op)) { 472 // A compatible add with a constant operand. Fold the constant. 473 ConstantInt *CI = 474 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1)); 475 TmpOffset += CI->getSExtValue() * S; 476 // Iterate on the other operand. 477 Op = cast<AddOperator>(Op)->getOperand(0); 478 continue; 479 } 480 // Unsupported 481 goto unsupported_gep; 482 } 483 } 484 } 485 // Try to grab the base operand now. 486 Addr.setOffset(TmpOffset); 487 if (computeAddress(U->getOperand(0), Addr)) 488 return true; 489 // We failed, restore everything and try the other options. 490 Addr = SavedAddr; 491 unsupported_gep: 492 break; 493 } 494 case Instruction::Alloca: { 495 const AllocaInst *AI = cast<AllocaInst>(Obj); 496 DenseMap<const AllocaInst *, int>::iterator SI = 497 FuncInfo.StaticAllocaMap.find(AI); 498 if (SI != FuncInfo.StaticAllocaMap.end()) { 499 Addr.setKind(Address::FrameIndexBase); 500 Addr.setFI(SI->second); 501 return true; 502 } 503 break; 504 } 505 } 506 Addr.setReg(getRegForValue(Obj)); 507 return Addr.getReg() != 0; 508 } 509 510 bool MipsFastISel::computeCallAddress(const Value *V, Address &Addr) { 511 const User *U = nullptr; 512 unsigned Opcode = Instruction::UserOp1; 513 514 if (const auto *I = dyn_cast<Instruction>(V)) { 515 // Check if the value is defined in the same basic block. This information 516 // is crucial to know whether or not folding an operand is valid. 517 if (I->getParent() == FuncInfo.MBB->getBasicBlock()) { 518 Opcode = I->getOpcode(); 519 U = I; 520 } 521 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) { 522 Opcode = C->getOpcode(); 523 U = C; 524 } 525 526 switch (Opcode) { 527 default: 528 break; 529 case Instruction::BitCast: 530 // Look past bitcasts if its operand is in the same BB. 531 return computeCallAddress(U->getOperand(0), Addr); 532 break; 533 case Instruction::IntToPtr: 534 // Look past no-op inttoptrs if its operand is in the same BB. 535 if (TLI.getValueType(DL, U->getOperand(0)->getType()) == 536 TLI.getPointerTy(DL)) 537 return computeCallAddress(U->getOperand(0), Addr); 538 break; 539 case Instruction::PtrToInt: 540 // Look past no-op ptrtoints if its operand is in the same BB. 541 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL)) 542 return computeCallAddress(U->getOperand(0), Addr); 543 break; 544 } 545 546 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 547 Addr.setGlobalValue(GV); 548 return true; 549 } 550 551 // If all else fails, try to materialize the value in a register. 552 if (!Addr.getGlobalValue()) { 553 Addr.setReg(getRegForValue(V)); 554 return Addr.getReg() != 0; 555 } 556 557 return false; 558 } 559 560 bool MipsFastISel::isTypeLegal(Type *Ty, MVT &VT) { 561 EVT evt = TLI.getValueType(DL, Ty, true); 562 // Only handle simple types. 563 if (evt == MVT::Other || !evt.isSimple()) 564 return false; 565 VT = evt.getSimpleVT(); 566 567 // Handle all legal types, i.e. a register that will directly hold this 568 // value. 569 return TLI.isTypeLegal(VT); 570 } 571 572 bool MipsFastISel::isTypeSupported(Type *Ty, MVT &VT) { 573 if (Ty->isVectorTy()) 574 return false; 575 576 if (isTypeLegal(Ty, VT)) 577 return true; 578 579 // If this is a type than can be sign or zero-extended to a basic operation 580 // go ahead and accept it now. 581 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16) 582 return true; 583 584 return false; 585 } 586 587 bool MipsFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) { 588 if (isTypeLegal(Ty, VT)) 589 return true; 590 // We will extend this in a later patch: 591 // If this is a type than can be sign or zero-extended to a basic operation 592 // go ahead and accept it now. 593 if (VT == MVT::i8 || VT == MVT::i16) 594 return true; 595 return false; 596 } 597 // Because of how EmitCmp is called with fast-isel, you can 598 // end up with redundant "andi" instructions after the sequences emitted below. 599 // We should try and solve this issue in the future. 600 // 601 bool MipsFastISel::emitCmp(unsigned ResultReg, const CmpInst *CI) { 602 const Value *Left = CI->getOperand(0), *Right = CI->getOperand(1); 603 bool IsUnsigned = CI->isUnsigned(); 604 unsigned LeftReg = getRegEnsuringSimpleIntegerWidening(Left, IsUnsigned); 605 if (LeftReg == 0) 606 return false; 607 unsigned RightReg = getRegEnsuringSimpleIntegerWidening(Right, IsUnsigned); 608 if (RightReg == 0) 609 return false; 610 CmpInst::Predicate P = CI->getPredicate(); 611 612 switch (P) { 613 default: 614 return false; 615 case CmpInst::ICMP_EQ: { 616 unsigned TempReg = createResultReg(&Mips::GPR32RegClass); 617 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg); 618 emitInst(Mips::SLTiu, ResultReg).addReg(TempReg).addImm(1); 619 break; 620 } 621 case CmpInst::ICMP_NE: { 622 unsigned TempReg = createResultReg(&Mips::GPR32RegClass); 623 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg); 624 emitInst(Mips::SLTu, ResultReg).addReg(Mips::ZERO).addReg(TempReg); 625 break; 626 } 627 case CmpInst::ICMP_UGT: { 628 emitInst(Mips::SLTu, ResultReg).addReg(RightReg).addReg(LeftReg); 629 break; 630 } 631 case CmpInst::ICMP_ULT: { 632 emitInst(Mips::SLTu, ResultReg).addReg(LeftReg).addReg(RightReg); 633 break; 634 } 635 case CmpInst::ICMP_UGE: { 636 unsigned TempReg = createResultReg(&Mips::GPR32RegClass); 637 emitInst(Mips::SLTu, TempReg).addReg(LeftReg).addReg(RightReg); 638 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1); 639 break; 640 } 641 case CmpInst::ICMP_ULE: { 642 unsigned TempReg = createResultReg(&Mips::GPR32RegClass); 643 emitInst(Mips::SLTu, TempReg).addReg(RightReg).addReg(LeftReg); 644 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1); 645 break; 646 } 647 case CmpInst::ICMP_SGT: { 648 emitInst(Mips::SLT, ResultReg).addReg(RightReg).addReg(LeftReg); 649 break; 650 } 651 case CmpInst::ICMP_SLT: { 652 emitInst(Mips::SLT, ResultReg).addReg(LeftReg).addReg(RightReg); 653 break; 654 } 655 case CmpInst::ICMP_SGE: { 656 unsigned TempReg = createResultReg(&Mips::GPR32RegClass); 657 emitInst(Mips::SLT, TempReg).addReg(LeftReg).addReg(RightReg); 658 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1); 659 break; 660 } 661 case CmpInst::ICMP_SLE: { 662 unsigned TempReg = createResultReg(&Mips::GPR32RegClass); 663 emitInst(Mips::SLT, TempReg).addReg(RightReg).addReg(LeftReg); 664 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1); 665 break; 666 } 667 case CmpInst::FCMP_OEQ: 668 case CmpInst::FCMP_UNE: 669 case CmpInst::FCMP_OLT: 670 case CmpInst::FCMP_OLE: 671 case CmpInst::FCMP_OGT: 672 case CmpInst::FCMP_OGE: { 673 if (UnsupportedFPMode) 674 return false; 675 bool IsFloat = Left->getType()->isFloatTy(); 676 bool IsDouble = Left->getType()->isDoubleTy(); 677 if (!IsFloat && !IsDouble) 678 return false; 679 unsigned Opc, CondMovOpc; 680 switch (P) { 681 case CmpInst::FCMP_OEQ: 682 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32; 683 CondMovOpc = Mips::MOVT_I; 684 break; 685 case CmpInst::FCMP_UNE: 686 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32; 687 CondMovOpc = Mips::MOVF_I; 688 break; 689 case CmpInst::FCMP_OLT: 690 Opc = IsFloat ? Mips::C_OLT_S : Mips::C_OLT_D32; 691 CondMovOpc = Mips::MOVT_I; 692 break; 693 case CmpInst::FCMP_OLE: 694 Opc = IsFloat ? Mips::C_OLE_S : Mips::C_OLE_D32; 695 CondMovOpc = Mips::MOVT_I; 696 break; 697 case CmpInst::FCMP_OGT: 698 Opc = IsFloat ? Mips::C_ULE_S : Mips::C_ULE_D32; 699 CondMovOpc = Mips::MOVF_I; 700 break; 701 case CmpInst::FCMP_OGE: 702 Opc = IsFloat ? Mips::C_ULT_S : Mips::C_ULT_D32; 703 CondMovOpc = Mips::MOVF_I; 704 break; 705 default: 706 llvm_unreachable("Only switching of a subset of CCs."); 707 } 708 unsigned RegWithZero = createResultReg(&Mips::GPR32RegClass); 709 unsigned RegWithOne = createResultReg(&Mips::GPR32RegClass); 710 emitInst(Mips::ADDiu, RegWithZero).addReg(Mips::ZERO).addImm(0); 711 emitInst(Mips::ADDiu, RegWithOne).addReg(Mips::ZERO).addImm(1); 712 emitInst(Opc).addReg(LeftReg).addReg(RightReg).addReg( 713 Mips::FCC0, RegState::ImplicitDefine); 714 emitInst(CondMovOpc, ResultReg) 715 .addReg(RegWithOne) 716 .addReg(Mips::FCC0) 717 .addReg(RegWithZero); 718 break; 719 } 720 } 721 return true; 722 } 723 bool MipsFastISel::emitLoad(MVT VT, unsigned &ResultReg, Address &Addr, 724 unsigned Alignment) { 725 // 726 // more cases will be handled here in following patches. 727 // 728 unsigned Opc; 729 switch (VT.SimpleTy) { 730 case MVT::i32: { 731 ResultReg = createResultReg(&Mips::GPR32RegClass); 732 Opc = Mips::LW; 733 break; 734 } 735 case MVT::i16: { 736 ResultReg = createResultReg(&Mips::GPR32RegClass); 737 Opc = Mips::LHu; 738 break; 739 } 740 case MVT::i8: { 741 ResultReg = createResultReg(&Mips::GPR32RegClass); 742 Opc = Mips::LBu; 743 break; 744 } 745 case MVT::f32: { 746 if (UnsupportedFPMode) 747 return false; 748 ResultReg = createResultReg(&Mips::FGR32RegClass); 749 Opc = Mips::LWC1; 750 break; 751 } 752 case MVT::f64: { 753 if (UnsupportedFPMode) 754 return false; 755 ResultReg = createResultReg(&Mips::AFGR64RegClass); 756 Opc = Mips::LDC1; 757 break; 758 } 759 default: 760 return false; 761 } 762 if (Addr.isRegBase()) { 763 simplifyAddress(Addr); 764 emitInstLoad(Opc, ResultReg, Addr.getReg(), Addr.getOffset()); 765 return true; 766 } 767 if (Addr.isFIBase()) { 768 unsigned FI = Addr.getFI(); 769 unsigned Align = 4; 770 unsigned Offset = Addr.getOffset(); 771 MachineFrameInfo &MFI = MF->getFrameInfo(); 772 MachineMemOperand *MMO = MF->getMachineMemOperand( 773 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad, 774 MFI.getObjectSize(FI), Align); 775 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) 776 .addFrameIndex(FI) 777 .addImm(Offset) 778 .addMemOperand(MMO); 779 return true; 780 } 781 return false; 782 } 783 784 bool MipsFastISel::emitStore(MVT VT, unsigned SrcReg, Address &Addr, 785 unsigned Alignment) { 786 // 787 // more cases will be handled here in following patches. 788 // 789 unsigned Opc; 790 switch (VT.SimpleTy) { 791 case MVT::i8: 792 Opc = Mips::SB; 793 break; 794 case MVT::i16: 795 Opc = Mips::SH; 796 break; 797 case MVT::i32: 798 Opc = Mips::SW; 799 break; 800 case MVT::f32: 801 if (UnsupportedFPMode) 802 return false; 803 Opc = Mips::SWC1; 804 break; 805 case MVT::f64: 806 if (UnsupportedFPMode) 807 return false; 808 Opc = Mips::SDC1; 809 break; 810 default: 811 return false; 812 } 813 if (Addr.isRegBase()) { 814 simplifyAddress(Addr); 815 emitInstStore(Opc, SrcReg, Addr.getReg(), Addr.getOffset()); 816 return true; 817 } 818 if (Addr.isFIBase()) { 819 unsigned FI = Addr.getFI(); 820 unsigned Align = 4; 821 unsigned Offset = Addr.getOffset(); 822 MachineFrameInfo &MFI = MF->getFrameInfo(); 823 MachineMemOperand *MMO = MF->getMachineMemOperand( 824 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOStore, 825 MFI.getObjectSize(FI), Align); 826 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc)) 827 .addReg(SrcReg) 828 .addFrameIndex(FI) 829 .addImm(Offset) 830 .addMemOperand(MMO); 831 return true; 832 } 833 return false; 834 } 835 836 bool MipsFastISel::selectLogicalOp(const Instruction *I) { 837 MVT VT; 838 if (!isTypeSupported(I->getType(), VT)) 839 return false; 840 841 unsigned ResultReg; 842 switch (I->getOpcode()) { 843 default: 844 llvm_unreachable("Unexpected instruction."); 845 case Instruction::And: 846 ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1)); 847 break; 848 case Instruction::Or: 849 ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1)); 850 break; 851 case Instruction::Xor: 852 ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1)); 853 break; 854 } 855 856 if (!ResultReg) 857 return false; 858 859 updateValueMap(I, ResultReg); 860 return true; 861 } 862 863 bool MipsFastISel::selectLoad(const Instruction *I) { 864 // Atomic loads need special handling. 865 if (cast<LoadInst>(I)->isAtomic()) 866 return false; 867 868 // Verify we have a legal type before going any further. 869 MVT VT; 870 if (!isLoadTypeLegal(I->getType(), VT)) 871 return false; 872 873 // See if we can handle this address. 874 Address Addr; 875 if (!computeAddress(I->getOperand(0), Addr)) 876 return false; 877 878 unsigned ResultReg; 879 if (!emitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment())) 880 return false; 881 updateValueMap(I, ResultReg); 882 return true; 883 } 884 885 bool MipsFastISel::selectStore(const Instruction *I) { 886 Value *Op0 = I->getOperand(0); 887 unsigned SrcReg = 0; 888 889 // Atomic stores need special handling. 890 if (cast<StoreInst>(I)->isAtomic()) 891 return false; 892 893 // Verify we have a legal type before going any further. 894 MVT VT; 895 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT)) 896 return false; 897 898 // Get the value to be stored into a register. 899 SrcReg = getRegForValue(Op0); 900 if (SrcReg == 0) 901 return false; 902 903 // See if we can handle this address. 904 Address Addr; 905 if (!computeAddress(I->getOperand(1), Addr)) 906 return false; 907 908 if (!emitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment())) 909 return false; 910 return true; 911 } 912 913 // 914 // This can cause a redundant sltiu to be generated. 915 // FIXME: try and eliminate this in a future patch. 916 // 917 bool MipsFastISel::selectBranch(const Instruction *I) { 918 const BranchInst *BI = cast<BranchInst>(I); 919 MachineBasicBlock *BrBB = FuncInfo.MBB; 920 // 921 // TBB is the basic block for the case where the comparison is true. 922 // FBB is the basic block for the case where the comparison is false. 923 // if (cond) goto TBB 924 // goto FBB 925 // TBB: 926 // 927 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)]; 928 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)]; 929 BI->getCondition(); 930 // For now, just try the simplest case where it's fed by a compare. 931 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) { 932 unsigned CondReg = createResultReg(&Mips::GPR32RegClass); 933 if (!emitCmp(CondReg, CI)) 934 return false; 935 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::BGTZ)) 936 .addReg(CondReg) 937 .addMBB(TBB); 938 finishCondBranch(BI->getParent(), TBB, FBB); 939 return true; 940 } 941 return false; 942 } 943 944 bool MipsFastISel::selectCmp(const Instruction *I) { 945 const CmpInst *CI = cast<CmpInst>(I); 946 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass); 947 if (!emitCmp(ResultReg, CI)) 948 return false; 949 updateValueMap(I, ResultReg); 950 return true; 951 } 952 953 // Attempt to fast-select a floating-point extend instruction. 954 bool MipsFastISel::selectFPExt(const Instruction *I) { 955 if (UnsupportedFPMode) 956 return false; 957 Value *Src = I->getOperand(0); 958 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true); 959 EVT DestVT = TLI.getValueType(DL, I->getType(), true); 960 961 if (SrcVT != MVT::f32 || DestVT != MVT::f64) 962 return false; 963 964 unsigned SrcReg = 965 getRegForValue(Src); // this must be a 32bit floating point register class 966 // maybe we should handle this differently 967 if (!SrcReg) 968 return false; 969 970 unsigned DestReg = createResultReg(&Mips::AFGR64RegClass); 971 emitInst(Mips::CVT_D32_S, DestReg).addReg(SrcReg); 972 updateValueMap(I, DestReg); 973 return true; 974 } 975 976 bool MipsFastISel::selectSelect(const Instruction *I) { 977 assert(isa<SelectInst>(I) && "Expected a select instruction."); 978 979 DEBUG(dbgs() << "selectSelect\n"); 980 981 MVT VT; 982 if (!isTypeSupported(I->getType(), VT) || UnsupportedFPMode) { 983 DEBUG(dbgs() << ".. .. gave up (!isTypeSupported || UnsupportedFPMode)\n"); 984 return false; 985 } 986 987 unsigned CondMovOpc; 988 const TargetRegisterClass *RC; 989 990 if (VT.isInteger() && !VT.isVector() && VT.getSizeInBits() <= 32) { 991 CondMovOpc = Mips::MOVN_I_I; 992 RC = &Mips::GPR32RegClass; 993 } else if (VT == MVT::f32) { 994 CondMovOpc = Mips::MOVN_I_S; 995 RC = &Mips::FGR32RegClass; 996 } else if (VT == MVT::f64) { 997 CondMovOpc = Mips::MOVN_I_D32; 998 RC = &Mips::AFGR64RegClass; 999 } else 1000 return false; 1001 1002 const SelectInst *SI = cast<SelectInst>(I); 1003 const Value *Cond = SI->getCondition(); 1004 unsigned Src1Reg = getRegForValue(SI->getTrueValue()); 1005 unsigned Src2Reg = getRegForValue(SI->getFalseValue()); 1006 unsigned CondReg = getRegForValue(Cond); 1007 1008 if (!Src1Reg || !Src2Reg || !CondReg) 1009 return false; 1010 1011 unsigned ZExtCondReg = createResultReg(&Mips::GPR32RegClass); 1012 if (!ZExtCondReg) 1013 return false; 1014 1015 if (!emitIntExt(MVT::i1, CondReg, MVT::i32, ZExtCondReg, true)) 1016 return false; 1017 1018 unsigned ResultReg = createResultReg(RC); 1019 unsigned TempReg = createResultReg(RC); 1020 1021 if (!ResultReg || !TempReg) 1022 return false; 1023 1024 emitInst(TargetOpcode::COPY, TempReg).addReg(Src2Reg); 1025 emitInst(CondMovOpc, ResultReg) 1026 .addReg(Src1Reg).addReg(ZExtCondReg).addReg(TempReg); 1027 updateValueMap(I, ResultReg); 1028 return true; 1029 } 1030 1031 // Attempt to fast-select a floating-point truncate instruction. 1032 bool MipsFastISel::selectFPTrunc(const Instruction *I) { 1033 if (UnsupportedFPMode) 1034 return false; 1035 Value *Src = I->getOperand(0); 1036 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true); 1037 EVT DestVT = TLI.getValueType(DL, I->getType(), true); 1038 1039 if (SrcVT != MVT::f64 || DestVT != MVT::f32) 1040 return false; 1041 1042 unsigned SrcReg = getRegForValue(Src); 1043 if (!SrcReg) 1044 return false; 1045 1046 unsigned DestReg = createResultReg(&Mips::FGR32RegClass); 1047 if (!DestReg) 1048 return false; 1049 1050 emitInst(Mips::CVT_S_D32, DestReg).addReg(SrcReg); 1051 updateValueMap(I, DestReg); 1052 return true; 1053 } 1054 1055 // Attempt to fast-select a floating-point-to-integer conversion. 1056 bool MipsFastISel::selectFPToInt(const Instruction *I, bool IsSigned) { 1057 if (UnsupportedFPMode) 1058 return false; 1059 MVT DstVT, SrcVT; 1060 if (!IsSigned) 1061 return false; // We don't handle this case yet. There is no native 1062 // instruction for this but it can be synthesized. 1063 Type *DstTy = I->getType(); 1064 if (!isTypeLegal(DstTy, DstVT)) 1065 return false; 1066 1067 if (DstVT != MVT::i32) 1068 return false; 1069 1070 Value *Src = I->getOperand(0); 1071 Type *SrcTy = Src->getType(); 1072 if (!isTypeLegal(SrcTy, SrcVT)) 1073 return false; 1074 1075 if (SrcVT != MVT::f32 && SrcVT != MVT::f64) 1076 return false; 1077 1078 unsigned SrcReg = getRegForValue(Src); 1079 if (SrcReg == 0) 1080 return false; 1081 1082 // Determine the opcode for the conversion, which takes place 1083 // entirely within FPRs. 1084 unsigned DestReg = createResultReg(&Mips::GPR32RegClass); 1085 unsigned TempReg = createResultReg(&Mips::FGR32RegClass); 1086 unsigned Opc = (SrcVT == MVT::f32) ? Mips::TRUNC_W_S : Mips::TRUNC_W_D32; 1087 1088 // Generate the convert. 1089 emitInst(Opc, TempReg).addReg(SrcReg); 1090 emitInst(Mips::MFC1, DestReg).addReg(TempReg); 1091 1092 updateValueMap(I, DestReg); 1093 return true; 1094 } 1095 1096 bool MipsFastISel::processCallArgs(CallLoweringInfo &CLI, 1097 SmallVectorImpl<MVT> &OutVTs, 1098 unsigned &NumBytes) { 1099 CallingConv::ID CC = CLI.CallConv; 1100 SmallVector<CCValAssign, 16> ArgLocs; 1101 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context); 1102 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC)); 1103 // Get a count of how many bytes are to be pushed on the stack. 1104 NumBytes = CCInfo.getNextStackOffset(); 1105 // This is the minimum argument area used for A0-A3. 1106 if (NumBytes < 16) 1107 NumBytes = 16; 1108 1109 emitInst(Mips::ADJCALLSTACKDOWN).addImm(16); 1110 // Process the args. 1111 MVT firstMVT; 1112 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 1113 CCValAssign &VA = ArgLocs[i]; 1114 const Value *ArgVal = CLI.OutVals[VA.getValNo()]; 1115 MVT ArgVT = OutVTs[VA.getValNo()]; 1116 1117 if (i == 0) { 1118 firstMVT = ArgVT; 1119 if (ArgVT == MVT::f32) { 1120 VA.convertToReg(Mips::F12); 1121 } else if (ArgVT == MVT::f64) { 1122 VA.convertToReg(Mips::D6); 1123 } 1124 } else if (i == 1) { 1125 if ((firstMVT == MVT::f32) || (firstMVT == MVT::f64)) { 1126 if (ArgVT == MVT::f32) { 1127 VA.convertToReg(Mips::F14); 1128 } else if (ArgVT == MVT::f64) { 1129 VA.convertToReg(Mips::D7); 1130 } 1131 } 1132 } 1133 if (((ArgVT == MVT::i32) || (ArgVT == MVT::f32) || (ArgVT == MVT::i16) || 1134 (ArgVT == MVT::i8)) && 1135 VA.isMemLoc()) { 1136 switch (VA.getLocMemOffset()) { 1137 case 0: 1138 VA.convertToReg(Mips::A0); 1139 break; 1140 case 4: 1141 VA.convertToReg(Mips::A1); 1142 break; 1143 case 8: 1144 VA.convertToReg(Mips::A2); 1145 break; 1146 case 12: 1147 VA.convertToReg(Mips::A3); 1148 break; 1149 default: 1150 break; 1151 } 1152 } 1153 unsigned ArgReg = getRegForValue(ArgVal); 1154 if (!ArgReg) 1155 return false; 1156 1157 // Handle arg promotion: SExt, ZExt, AExt. 1158 switch (VA.getLocInfo()) { 1159 case CCValAssign::Full: 1160 break; 1161 case CCValAssign::AExt: 1162 case CCValAssign::SExt: { 1163 MVT DestVT = VA.getLocVT(); 1164 MVT SrcVT = ArgVT; 1165 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false); 1166 if (!ArgReg) 1167 return false; 1168 break; 1169 } 1170 case CCValAssign::ZExt: { 1171 MVT DestVT = VA.getLocVT(); 1172 MVT SrcVT = ArgVT; 1173 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true); 1174 if (!ArgReg) 1175 return false; 1176 break; 1177 } 1178 default: 1179 llvm_unreachable("Unknown arg promotion!"); 1180 } 1181 1182 // Now copy/store arg to correct locations. 1183 if (VA.isRegLoc() && !VA.needsCustom()) { 1184 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1185 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg); 1186 CLI.OutRegs.push_back(VA.getLocReg()); 1187 } else if (VA.needsCustom()) { 1188 llvm_unreachable("Mips does not use custom args."); 1189 return false; 1190 } else { 1191 // 1192 // FIXME: This path will currently return false. It was copied 1193 // from the AArch64 port and should be essentially fine for Mips too. 1194 // The work to finish up this path will be done in a follow-on patch. 1195 // 1196 assert(VA.isMemLoc() && "Assuming store on stack."); 1197 // Don't emit stores for undef values. 1198 if (isa<UndefValue>(ArgVal)) 1199 continue; 1200 1201 // Need to store on the stack. 1202 // FIXME: This alignment is incorrect but this path is disabled 1203 // for now (will return false). We need to determine the right alignment 1204 // based on the normal alignment for the underlying machine type. 1205 // 1206 unsigned ArgSize = alignTo(ArgVT.getSizeInBits(), 4); 1207 1208 unsigned BEAlign = 0; 1209 if (ArgSize < 8 && !Subtarget->isLittle()) 1210 BEAlign = 8 - ArgSize; 1211 1212 Address Addr; 1213 Addr.setKind(Address::RegBase); 1214 Addr.setReg(Mips::SP); 1215 Addr.setOffset(VA.getLocMemOffset() + BEAlign); 1216 1217 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType()); 1218 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand( 1219 MachinePointerInfo::getStack(*FuncInfo.MF, Addr.getOffset()), 1220 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment); 1221 (void)(MMO); 1222 // if (!emitStore(ArgVT, ArgReg, Addr, MMO)) 1223 return false; // can't store on the stack yet. 1224 } 1225 } 1226 1227 return true; 1228 } 1229 1230 bool MipsFastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT, 1231 unsigned NumBytes) { 1232 CallingConv::ID CC = CLI.CallConv; 1233 emitInst(Mips::ADJCALLSTACKUP).addImm(16).addImm(0); 1234 if (RetVT != MVT::isVoid) { 1235 SmallVector<CCValAssign, 16> RVLocs; 1236 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context); 1237 CCInfo.AnalyzeCallResult(RetVT, RetCC_Mips); 1238 1239 // Only handle a single return value. 1240 if (RVLocs.size() != 1) 1241 return false; 1242 // Copy all of the result registers out of their specified physreg. 1243 MVT CopyVT = RVLocs[0].getValVT(); 1244 // Special handling for extended integers. 1245 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16) 1246 CopyVT = MVT::i32; 1247 1248 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT)); 1249 if (!ResultReg) 1250 return false; 1251 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1252 TII.get(TargetOpcode::COPY), 1253 ResultReg).addReg(RVLocs[0].getLocReg()); 1254 CLI.InRegs.push_back(RVLocs[0].getLocReg()); 1255 1256 CLI.ResultReg = ResultReg; 1257 CLI.NumResultRegs = 1; 1258 } 1259 return true; 1260 } 1261 1262 bool MipsFastISel::fastLowerArguments() { 1263 DEBUG(dbgs() << "fastLowerArguments\n"); 1264 1265 if (!FuncInfo.CanLowerReturn) { 1266 DEBUG(dbgs() << ".. gave up (!CanLowerReturn)\n"); 1267 return false; 1268 } 1269 1270 const Function *F = FuncInfo.Fn; 1271 if (F->isVarArg()) { 1272 DEBUG(dbgs() << ".. gave up (varargs)\n"); 1273 return false; 1274 } 1275 1276 CallingConv::ID CC = F->getCallingConv(); 1277 if (CC != CallingConv::C) { 1278 DEBUG(dbgs() << ".. gave up (calling convention is not C)\n"); 1279 return false; 1280 } 1281 1282 const ArrayRef<MCPhysReg> GPR32ArgRegs = {Mips::A0, Mips::A1, Mips::A2, 1283 Mips::A3}; 1284 const ArrayRef<MCPhysReg> FGR32ArgRegs = {Mips::F12, Mips::F14}; 1285 const ArrayRef<MCPhysReg> AFGR64ArgRegs = {Mips::D6, Mips::D7}; 1286 ArrayRef<MCPhysReg>::iterator NextGPR32 = GPR32ArgRegs.begin(); 1287 ArrayRef<MCPhysReg>::iterator NextFGR32 = FGR32ArgRegs.begin(); 1288 ArrayRef<MCPhysReg>::iterator NextAFGR64 = AFGR64ArgRegs.begin(); 1289 1290 struct AllocatedReg { 1291 const TargetRegisterClass *RC; 1292 unsigned Reg; 1293 AllocatedReg(const TargetRegisterClass *RC, unsigned Reg) 1294 : RC(RC), Reg(Reg) {} 1295 }; 1296 1297 // Only handle simple cases. i.e. All arguments are directly mapped to 1298 // registers of the appropriate type. 1299 SmallVector<AllocatedReg, 4> Allocation; 1300 unsigned Idx = 1; 1301 for (const auto &FormalArg : F->args()) { 1302 if (F->getAttributes().hasAttribute(Idx, Attribute::InReg) || 1303 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) || 1304 F->getAttributes().hasAttribute(Idx, Attribute::ByVal)) { 1305 DEBUG(dbgs() << ".. gave up (inreg, structret, byval)\n"); 1306 return false; 1307 } 1308 1309 Type *ArgTy = FormalArg.getType(); 1310 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy()) { 1311 DEBUG(dbgs() << ".. gave up (struct, array, or vector)\n"); 1312 return false; 1313 } 1314 1315 EVT ArgVT = TLI.getValueType(DL, ArgTy); 1316 DEBUG(dbgs() << ".. " << (Idx - 1) << ": " << ArgVT.getEVTString() << "\n"); 1317 if (!ArgVT.isSimple()) { 1318 DEBUG(dbgs() << ".. .. gave up (not a simple type)\n"); 1319 return false; 1320 } 1321 1322 switch (ArgVT.getSimpleVT().SimpleTy) { 1323 case MVT::i1: 1324 case MVT::i8: 1325 case MVT::i16: 1326 if (!F->getAttributes().hasAttribute(Idx, Attribute::SExt) && 1327 !F->getAttributes().hasAttribute(Idx, Attribute::ZExt)) { 1328 // It must be any extend, this shouldn't happen for clang-generated IR 1329 // so just fall back on SelectionDAG. 1330 DEBUG(dbgs() << ".. .. gave up (i8/i16 arg is not extended)\n"); 1331 return false; 1332 } 1333 1334 if (NextGPR32 == GPR32ArgRegs.end()) { 1335 DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n"); 1336 return false; 1337 } 1338 1339 DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32 << ")\n"); 1340 Allocation.emplace_back(&Mips::GPR32RegClass, *NextGPR32++); 1341 1342 // Allocating any GPR32 prohibits further use of floating point arguments. 1343 NextFGR32 = FGR32ArgRegs.end(); 1344 NextAFGR64 = AFGR64ArgRegs.end(); 1345 break; 1346 1347 case MVT::i32: 1348 if (F->getAttributes().hasAttribute(Idx, Attribute::ZExt)) { 1349 // The O32 ABI does not permit a zero-extended i32. 1350 DEBUG(dbgs() << ".. .. gave up (i32 arg is zero extended)\n"); 1351 return false; 1352 } 1353 1354 if (NextGPR32 == GPR32ArgRegs.end()) { 1355 DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n"); 1356 return false; 1357 } 1358 1359 DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32 << ")\n"); 1360 Allocation.emplace_back(&Mips::GPR32RegClass, *NextGPR32++); 1361 1362 // Allocating any GPR32 prohibits further use of floating point arguments. 1363 NextFGR32 = FGR32ArgRegs.end(); 1364 NextAFGR64 = AFGR64ArgRegs.end(); 1365 break; 1366 1367 case MVT::f32: 1368 if (UnsupportedFPMode) { 1369 DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n"); 1370 return false; 1371 } 1372 if (NextFGR32 == FGR32ArgRegs.end()) { 1373 DEBUG(dbgs() << ".. .. gave up (ran out of FGR32 arguments)\n"); 1374 return false; 1375 } 1376 DEBUG(dbgs() << ".. .. FGR32(" << *NextFGR32 << ")\n"); 1377 Allocation.emplace_back(&Mips::FGR32RegClass, *NextFGR32++); 1378 // Allocating an FGR32 also allocates the super-register AFGR64, and 1379 // ABI rules require us to skip the corresponding GPR32. 1380 if (NextGPR32 != GPR32ArgRegs.end()) 1381 NextGPR32++; 1382 if (NextAFGR64 != AFGR64ArgRegs.end()) 1383 NextAFGR64++; 1384 break; 1385 1386 case MVT::f64: 1387 if (UnsupportedFPMode) { 1388 DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n"); 1389 return false; 1390 } 1391 if (NextAFGR64 == AFGR64ArgRegs.end()) { 1392 DEBUG(dbgs() << ".. .. gave up (ran out of AFGR64 arguments)\n"); 1393 return false; 1394 } 1395 DEBUG(dbgs() << ".. .. AFGR64(" << *NextAFGR64 << ")\n"); 1396 Allocation.emplace_back(&Mips::AFGR64RegClass, *NextAFGR64++); 1397 // Allocating an FGR32 also allocates the super-register AFGR64, and 1398 // ABI rules require us to skip the corresponding GPR32 pair. 1399 if (NextGPR32 != GPR32ArgRegs.end()) 1400 NextGPR32++; 1401 if (NextGPR32 != GPR32ArgRegs.end()) 1402 NextGPR32++; 1403 if (NextFGR32 != FGR32ArgRegs.end()) 1404 NextFGR32++; 1405 break; 1406 1407 default: 1408 DEBUG(dbgs() << ".. .. gave up (unknown type)\n"); 1409 return false; 1410 } 1411 1412 ++Idx; 1413 } 1414 1415 Idx = 0; 1416 for (const auto &FormalArg : F->args()) { 1417 unsigned SrcReg = Allocation[Idx].Reg; 1418 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, Allocation[Idx].RC); 1419 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy. 1420 // Without this, EmitLiveInCopies may eliminate the livein if its only 1421 // use is a bitcast (which isn't turned into an instruction). 1422 unsigned ResultReg = createResultReg(Allocation[Idx].RC); 1423 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1424 TII.get(TargetOpcode::COPY), ResultReg) 1425 .addReg(DstReg, getKillRegState(true)); 1426 updateValueMap(&FormalArg, ResultReg); 1427 ++Idx; 1428 } 1429 1430 // Calculate the size of the incoming arguments area. 1431 // We currently reject all the cases where this would be non-zero. 1432 unsigned IncomingArgSizeInBytes = 0; 1433 1434 // Account for the reserved argument area on ABI's that have one (O32). 1435 // It seems strange to do this on the caller side but it's necessary in 1436 // SelectionDAG's implementation. 1437 IncomingArgSizeInBytes = std::min(getABI().GetCalleeAllocdArgSizeInBytes(CC), 1438 IncomingArgSizeInBytes); 1439 1440 MF->getInfo<MipsFunctionInfo>()->setFormalArgInfo(IncomingArgSizeInBytes, 1441 false); 1442 1443 return true; 1444 } 1445 1446 bool MipsFastISel::fastLowerCall(CallLoweringInfo &CLI) { 1447 if (!TargetSupported) 1448 return false; 1449 1450 CallingConv::ID CC = CLI.CallConv; 1451 bool IsTailCall = CLI.IsTailCall; 1452 bool IsVarArg = CLI.IsVarArg; 1453 const Value *Callee = CLI.Callee; 1454 MCSymbol *Symbol = CLI.Symbol; 1455 1456 // Do not handle FastCC. 1457 if (CC == CallingConv::Fast) 1458 return false; 1459 1460 // Allow SelectionDAG isel to handle tail calls. 1461 if (IsTailCall) 1462 return false; 1463 1464 // Let SDISel handle vararg functions. 1465 if (IsVarArg) 1466 return false; 1467 1468 // FIXME: Only handle *simple* calls for now. 1469 MVT RetVT; 1470 if (CLI.RetTy->isVoidTy()) 1471 RetVT = MVT::isVoid; 1472 else if (!isTypeSupported(CLI.RetTy, RetVT)) 1473 return false; 1474 1475 for (auto Flag : CLI.OutFlags) 1476 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal()) 1477 return false; 1478 1479 // Set up the argument vectors. 1480 SmallVector<MVT, 16> OutVTs; 1481 OutVTs.reserve(CLI.OutVals.size()); 1482 1483 for (auto *Val : CLI.OutVals) { 1484 MVT VT; 1485 if (!isTypeLegal(Val->getType(), VT) && 1486 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)) 1487 return false; 1488 1489 // We don't handle vector parameters yet. 1490 if (VT.isVector() || VT.getSizeInBits() > 64) 1491 return false; 1492 1493 OutVTs.push_back(VT); 1494 } 1495 1496 Address Addr; 1497 if (!computeCallAddress(Callee, Addr)) 1498 return false; 1499 1500 // Handle the arguments now that we've gotten them. 1501 unsigned NumBytes; 1502 if (!processCallArgs(CLI, OutVTs, NumBytes)) 1503 return false; 1504 1505 if (!Addr.getGlobalValue()) 1506 return false; 1507 1508 // Issue the call. 1509 unsigned DestAddress; 1510 if (Symbol) 1511 DestAddress = materializeExternalCallSym(Symbol); 1512 else 1513 DestAddress = materializeGV(Addr.getGlobalValue(), MVT::i32); 1514 emitInst(TargetOpcode::COPY, Mips::T9).addReg(DestAddress); 1515 MachineInstrBuilder MIB = 1516 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::JALR), 1517 Mips::RA).addReg(Mips::T9); 1518 1519 // Add implicit physical register uses to the call. 1520 for (auto Reg : CLI.OutRegs) 1521 MIB.addReg(Reg, RegState::Implicit); 1522 1523 // Add a register mask with the call-preserved registers. 1524 // Proper defs for return values will be added by setPhysRegsDeadExcept(). 1525 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC)); 1526 1527 CLI.Call = MIB; 1528 1529 // Finish off the call including any return values. 1530 return finishCall(CLI, RetVT, NumBytes); 1531 } 1532 1533 bool MipsFastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) { 1534 if (!TargetSupported) 1535 return false; 1536 1537 switch (II->getIntrinsicID()) { 1538 default: 1539 return false; 1540 case Intrinsic::bswap: { 1541 Type *RetTy = II->getCalledFunction()->getReturnType(); 1542 1543 MVT VT; 1544 if (!isTypeSupported(RetTy, VT)) 1545 return false; 1546 1547 unsigned SrcReg = getRegForValue(II->getOperand(0)); 1548 if (SrcReg == 0) 1549 return false; 1550 unsigned DestReg = createResultReg(&Mips::GPR32RegClass); 1551 if (DestReg == 0) 1552 return false; 1553 if (VT == MVT::i16) { 1554 if (Subtarget->hasMips32r2()) { 1555 emitInst(Mips::WSBH, DestReg).addReg(SrcReg); 1556 updateValueMap(II, DestReg); 1557 return true; 1558 } else { 1559 unsigned TempReg[3]; 1560 for (int i = 0; i < 3; i++) { 1561 TempReg[i] = createResultReg(&Mips::GPR32RegClass); 1562 if (TempReg[i] == 0) 1563 return false; 1564 } 1565 emitInst(Mips::SLL, TempReg[0]).addReg(SrcReg).addImm(8); 1566 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(8); 1567 emitInst(Mips::OR, TempReg[2]).addReg(TempReg[0]).addReg(TempReg[1]); 1568 emitInst(Mips::ANDi, DestReg).addReg(TempReg[2]).addImm(0xFFFF); 1569 updateValueMap(II, DestReg); 1570 return true; 1571 } 1572 } else if (VT == MVT::i32) { 1573 if (Subtarget->hasMips32r2()) { 1574 unsigned TempReg = createResultReg(&Mips::GPR32RegClass); 1575 emitInst(Mips::WSBH, TempReg).addReg(SrcReg); 1576 emitInst(Mips::ROTR, DestReg).addReg(TempReg).addImm(16); 1577 updateValueMap(II, DestReg); 1578 return true; 1579 } else { 1580 unsigned TempReg[8]; 1581 for (int i = 0; i < 8; i++) { 1582 TempReg[i] = createResultReg(&Mips::GPR32RegClass); 1583 if (TempReg[i] == 0) 1584 return false; 1585 } 1586 1587 emitInst(Mips::SRL, TempReg[0]).addReg(SrcReg).addImm(8); 1588 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(24); 1589 emitInst(Mips::ANDi, TempReg[2]).addReg(TempReg[0]).addImm(0xFF00); 1590 emitInst(Mips::OR, TempReg[3]).addReg(TempReg[1]).addReg(TempReg[2]); 1591 1592 emitInst(Mips::ANDi, TempReg[4]).addReg(SrcReg).addImm(0xFF00); 1593 emitInst(Mips::SLL, TempReg[5]).addReg(TempReg[4]).addImm(8); 1594 1595 emitInst(Mips::SLL, TempReg[6]).addReg(SrcReg).addImm(24); 1596 emitInst(Mips::OR, TempReg[7]).addReg(TempReg[3]).addReg(TempReg[5]); 1597 emitInst(Mips::OR, DestReg).addReg(TempReg[6]).addReg(TempReg[7]); 1598 updateValueMap(II, DestReg); 1599 return true; 1600 } 1601 } 1602 return false; 1603 } 1604 case Intrinsic::memcpy: 1605 case Intrinsic::memmove: { 1606 const auto *MTI = cast<MemTransferInst>(II); 1607 // Don't handle volatile. 1608 if (MTI->isVolatile()) 1609 return false; 1610 if (!MTI->getLength()->getType()->isIntegerTy(32)) 1611 return false; 1612 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove"; 1613 return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2); 1614 } 1615 case Intrinsic::memset: { 1616 const MemSetInst *MSI = cast<MemSetInst>(II); 1617 // Don't handle volatile. 1618 if (MSI->isVolatile()) 1619 return false; 1620 if (!MSI->getLength()->getType()->isIntegerTy(32)) 1621 return false; 1622 return lowerCallTo(II, "memset", II->getNumArgOperands() - 2); 1623 } 1624 } 1625 return false; 1626 } 1627 1628 bool MipsFastISel::selectRet(const Instruction *I) { 1629 const Function &F = *I->getParent()->getParent(); 1630 const ReturnInst *Ret = cast<ReturnInst>(I); 1631 1632 DEBUG(dbgs() << "selectRet\n"); 1633 1634 if (!FuncInfo.CanLowerReturn) 1635 return false; 1636 1637 // Build a list of return value registers. 1638 SmallVector<unsigned, 4> RetRegs; 1639 1640 if (Ret->getNumOperands() > 0) { 1641 CallingConv::ID CC = F.getCallingConv(); 1642 1643 // Do not handle FastCC. 1644 if (CC == CallingConv::Fast) 1645 return false; 1646 1647 SmallVector<ISD::OutputArg, 4> Outs; 1648 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL); 1649 1650 // Analyze operands of the call, assigning locations to each operand. 1651 SmallVector<CCValAssign, 16> ValLocs; 1652 MipsCCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, 1653 I->getContext()); 1654 CCAssignFn *RetCC = RetCC_Mips; 1655 CCInfo.AnalyzeReturn(Outs, RetCC); 1656 1657 // Only handle a single return value for now. 1658 if (ValLocs.size() != 1) 1659 return false; 1660 1661 CCValAssign &VA = ValLocs[0]; 1662 const Value *RV = Ret->getOperand(0); 1663 1664 // Don't bother handling odd stuff for now. 1665 if ((VA.getLocInfo() != CCValAssign::Full) && 1666 (VA.getLocInfo() != CCValAssign::BCvt)) 1667 return false; 1668 1669 // Only handle register returns for now. 1670 if (!VA.isRegLoc()) 1671 return false; 1672 1673 unsigned Reg = getRegForValue(RV); 1674 if (Reg == 0) 1675 return false; 1676 1677 unsigned SrcReg = Reg + VA.getValNo(); 1678 unsigned DestReg = VA.getLocReg(); 1679 // Avoid a cross-class copy. This is very unlikely. 1680 if (!MRI.getRegClass(SrcReg)->contains(DestReg)) 1681 return false; 1682 1683 EVT RVEVT = TLI.getValueType(DL, RV->getType()); 1684 if (!RVEVT.isSimple()) 1685 return false; 1686 1687 if (RVEVT.isVector()) 1688 return false; 1689 1690 MVT RVVT = RVEVT.getSimpleVT(); 1691 if (RVVT == MVT::f128) 1692 return false; 1693 1694 // Do not handle FGR64 returns for now. 1695 if (RVVT == MVT::f64 && UnsupportedFPMode) { 1696 DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode\n"); 1697 return false; 1698 } 1699 1700 MVT DestVT = VA.getValVT(); 1701 // Special handling for extended integers. 1702 if (RVVT != DestVT) { 1703 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16) 1704 return false; 1705 1706 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) { 1707 bool IsZExt = Outs[0].Flags.isZExt(); 1708 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt); 1709 if (SrcReg == 0) 1710 return false; 1711 } 1712 } 1713 1714 // Make the copy. 1715 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1716 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg); 1717 1718 // Add register to return instruction. 1719 RetRegs.push_back(VA.getLocReg()); 1720 } 1721 MachineInstrBuilder MIB = emitInst(Mips::RetRA); 1722 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i) 1723 MIB.addReg(RetRegs[i], RegState::Implicit); 1724 return true; 1725 } 1726 1727 bool MipsFastISel::selectTrunc(const Instruction *I) { 1728 // The high bits for a type smaller than the register size are assumed to be 1729 // undefined. 1730 Value *Op = I->getOperand(0); 1731 1732 EVT SrcVT, DestVT; 1733 SrcVT = TLI.getValueType(DL, Op->getType(), true); 1734 DestVT = TLI.getValueType(DL, I->getType(), true); 1735 1736 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8) 1737 return false; 1738 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1) 1739 return false; 1740 1741 unsigned SrcReg = getRegForValue(Op); 1742 if (!SrcReg) 1743 return false; 1744 1745 // Because the high bits are undefined, a truncate doesn't generate 1746 // any code. 1747 updateValueMap(I, SrcReg); 1748 return true; 1749 } 1750 bool MipsFastISel::selectIntExt(const Instruction *I) { 1751 Type *DestTy = I->getType(); 1752 Value *Src = I->getOperand(0); 1753 Type *SrcTy = Src->getType(); 1754 1755 bool isZExt = isa<ZExtInst>(I); 1756 unsigned SrcReg = getRegForValue(Src); 1757 if (!SrcReg) 1758 return false; 1759 1760 EVT SrcEVT, DestEVT; 1761 SrcEVT = TLI.getValueType(DL, SrcTy, true); 1762 DestEVT = TLI.getValueType(DL, DestTy, true); 1763 if (!SrcEVT.isSimple()) 1764 return false; 1765 if (!DestEVT.isSimple()) 1766 return false; 1767 1768 MVT SrcVT = SrcEVT.getSimpleVT(); 1769 MVT DestVT = DestEVT.getSimpleVT(); 1770 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass); 1771 1772 if (!emitIntExt(SrcVT, SrcReg, DestVT, ResultReg, isZExt)) 1773 return false; 1774 updateValueMap(I, ResultReg); 1775 return true; 1776 } 1777 bool MipsFastISel::emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT, 1778 unsigned DestReg) { 1779 unsigned ShiftAmt; 1780 switch (SrcVT.SimpleTy) { 1781 default: 1782 return false; 1783 case MVT::i8: 1784 ShiftAmt = 24; 1785 break; 1786 case MVT::i16: 1787 ShiftAmt = 16; 1788 break; 1789 } 1790 unsigned TempReg = createResultReg(&Mips::GPR32RegClass); 1791 emitInst(Mips::SLL, TempReg).addReg(SrcReg).addImm(ShiftAmt); 1792 emitInst(Mips::SRA, DestReg).addReg(TempReg).addImm(ShiftAmt); 1793 return true; 1794 } 1795 1796 bool MipsFastISel::emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT, 1797 unsigned DestReg) { 1798 switch (SrcVT.SimpleTy) { 1799 default: 1800 return false; 1801 case MVT::i8: 1802 emitInst(Mips::SEB, DestReg).addReg(SrcReg); 1803 break; 1804 case MVT::i16: 1805 emitInst(Mips::SEH, DestReg).addReg(SrcReg); 1806 break; 1807 } 1808 return true; 1809 } 1810 1811 bool MipsFastISel::emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, 1812 unsigned DestReg) { 1813 if ((DestVT != MVT::i32) && (DestVT != MVT::i16)) 1814 return false; 1815 if (Subtarget->hasMips32r2()) 1816 return emitIntSExt32r2(SrcVT, SrcReg, DestVT, DestReg); 1817 return emitIntSExt32r1(SrcVT, SrcReg, DestVT, DestReg); 1818 } 1819 1820 bool MipsFastISel::emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, 1821 unsigned DestReg) { 1822 int64_t Imm; 1823 1824 switch (SrcVT.SimpleTy) { 1825 default: 1826 return false; 1827 case MVT::i1: 1828 Imm = 1; 1829 break; 1830 case MVT::i8: 1831 Imm = 0xff; 1832 break; 1833 case MVT::i16: 1834 Imm = 0xffff; 1835 break; 1836 } 1837 1838 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(Imm); 1839 return true; 1840 } 1841 1842 bool MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, 1843 unsigned DestReg, bool IsZExt) { 1844 // FastISel does not have plumbing to deal with extensions where the SrcVT or 1845 // DestVT are odd things, so test to make sure that they are both types we can 1846 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise 1847 // bail out to SelectionDAG. 1848 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) && (DestVT != MVT::i32)) || 1849 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) && (SrcVT != MVT::i16))) 1850 return false; 1851 if (IsZExt) 1852 return emitIntZExt(SrcVT, SrcReg, DestVT, DestReg); 1853 return emitIntSExt(SrcVT, SrcReg, DestVT, DestReg); 1854 } 1855 1856 unsigned MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, 1857 bool isZExt) { 1858 unsigned DestReg = createResultReg(&Mips::GPR32RegClass); 1859 bool Success = emitIntExt(SrcVT, SrcReg, DestVT, DestReg, isZExt); 1860 return Success ? DestReg : 0; 1861 } 1862 1863 bool MipsFastISel::selectDivRem(const Instruction *I, unsigned ISDOpcode) { 1864 EVT DestEVT = TLI.getValueType(DL, I->getType(), true); 1865 if (!DestEVT.isSimple()) 1866 return false; 1867 1868 MVT DestVT = DestEVT.getSimpleVT(); 1869 if (DestVT != MVT::i32) 1870 return false; 1871 1872 unsigned DivOpc; 1873 switch (ISDOpcode) { 1874 default: 1875 return false; 1876 case ISD::SDIV: 1877 case ISD::SREM: 1878 DivOpc = Mips::SDIV; 1879 break; 1880 case ISD::UDIV: 1881 case ISD::UREM: 1882 DivOpc = Mips::UDIV; 1883 break; 1884 } 1885 1886 unsigned Src0Reg = getRegForValue(I->getOperand(0)); 1887 unsigned Src1Reg = getRegForValue(I->getOperand(1)); 1888 if (!Src0Reg || !Src1Reg) 1889 return false; 1890 1891 emitInst(DivOpc).addReg(Src0Reg).addReg(Src1Reg); 1892 emitInst(Mips::TEQ).addReg(Src1Reg).addReg(Mips::ZERO).addImm(7); 1893 1894 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass); 1895 if (!ResultReg) 1896 return false; 1897 1898 unsigned MFOpc = (ISDOpcode == ISD::SREM || ISDOpcode == ISD::UREM) 1899 ? Mips::MFHI 1900 : Mips::MFLO; 1901 emitInst(MFOpc, ResultReg); 1902 1903 updateValueMap(I, ResultReg); 1904 return true; 1905 } 1906 1907 bool MipsFastISel::selectShift(const Instruction *I) { 1908 MVT RetVT; 1909 1910 if (!isTypeSupported(I->getType(), RetVT)) 1911 return false; 1912 1913 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass); 1914 if (!ResultReg) 1915 return false; 1916 1917 unsigned Opcode = I->getOpcode(); 1918 const Value *Op0 = I->getOperand(0); 1919 unsigned Op0Reg = getRegForValue(Op0); 1920 if (!Op0Reg) 1921 return false; 1922 1923 // If AShr or LShr, then we need to make sure the operand0 is sign extended. 1924 if (Opcode == Instruction::AShr || Opcode == Instruction::LShr) { 1925 unsigned TempReg = createResultReg(&Mips::GPR32RegClass); 1926 if (!TempReg) 1927 return false; 1928 1929 MVT Op0MVT = TLI.getValueType(DL, Op0->getType(), true).getSimpleVT(); 1930 bool IsZExt = Opcode == Instruction::LShr; 1931 if (!emitIntExt(Op0MVT, Op0Reg, MVT::i32, TempReg, IsZExt)) 1932 return false; 1933 1934 Op0Reg = TempReg; 1935 } 1936 1937 if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) { 1938 uint64_t ShiftVal = C->getZExtValue(); 1939 1940 switch (Opcode) { 1941 default: 1942 llvm_unreachable("Unexpected instruction."); 1943 case Instruction::Shl: 1944 Opcode = Mips::SLL; 1945 break; 1946 case Instruction::AShr: 1947 Opcode = Mips::SRA; 1948 break; 1949 case Instruction::LShr: 1950 Opcode = Mips::SRL; 1951 break; 1952 } 1953 1954 emitInst(Opcode, ResultReg).addReg(Op0Reg).addImm(ShiftVal); 1955 updateValueMap(I, ResultReg); 1956 return true; 1957 } 1958 1959 unsigned Op1Reg = getRegForValue(I->getOperand(1)); 1960 if (!Op1Reg) 1961 return false; 1962 1963 switch (Opcode) { 1964 default: 1965 llvm_unreachable("Unexpected instruction."); 1966 case Instruction::Shl: 1967 Opcode = Mips::SLLV; 1968 break; 1969 case Instruction::AShr: 1970 Opcode = Mips::SRAV; 1971 break; 1972 case Instruction::LShr: 1973 Opcode = Mips::SRLV; 1974 break; 1975 } 1976 1977 emitInst(Opcode, ResultReg).addReg(Op0Reg).addReg(Op1Reg); 1978 updateValueMap(I, ResultReg); 1979 return true; 1980 } 1981 1982 bool MipsFastISel::fastSelectInstruction(const Instruction *I) { 1983 if (!TargetSupported) 1984 return false; 1985 switch (I->getOpcode()) { 1986 default: 1987 break; 1988 case Instruction::Load: 1989 return selectLoad(I); 1990 case Instruction::Store: 1991 return selectStore(I); 1992 case Instruction::SDiv: 1993 if (!selectBinaryOp(I, ISD::SDIV)) 1994 return selectDivRem(I, ISD::SDIV); 1995 return true; 1996 case Instruction::UDiv: 1997 if (!selectBinaryOp(I, ISD::UDIV)) 1998 return selectDivRem(I, ISD::UDIV); 1999 return true; 2000 case Instruction::SRem: 2001 if (!selectBinaryOp(I, ISD::SREM)) 2002 return selectDivRem(I, ISD::SREM); 2003 return true; 2004 case Instruction::URem: 2005 if (!selectBinaryOp(I, ISD::UREM)) 2006 return selectDivRem(I, ISD::UREM); 2007 return true; 2008 case Instruction::Shl: 2009 case Instruction::LShr: 2010 case Instruction::AShr: 2011 return selectShift(I); 2012 case Instruction::And: 2013 case Instruction::Or: 2014 case Instruction::Xor: 2015 return selectLogicalOp(I); 2016 case Instruction::Br: 2017 return selectBranch(I); 2018 case Instruction::Ret: 2019 return selectRet(I); 2020 case Instruction::Trunc: 2021 return selectTrunc(I); 2022 case Instruction::ZExt: 2023 case Instruction::SExt: 2024 return selectIntExt(I); 2025 case Instruction::FPTrunc: 2026 return selectFPTrunc(I); 2027 case Instruction::FPExt: 2028 return selectFPExt(I); 2029 case Instruction::FPToSI: 2030 return selectFPToInt(I, /*isSigned*/ true); 2031 case Instruction::FPToUI: 2032 return selectFPToInt(I, /*isSigned*/ false); 2033 case Instruction::ICmp: 2034 case Instruction::FCmp: 2035 return selectCmp(I); 2036 case Instruction::Select: 2037 return selectSelect(I); 2038 } 2039 return false; 2040 } 2041 2042 unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value *V, 2043 bool IsUnsigned) { 2044 unsigned VReg = getRegForValue(V); 2045 if (VReg == 0) 2046 return 0; 2047 MVT VMVT = TLI.getValueType(DL, V->getType(), true).getSimpleVT(); 2048 if ((VMVT == MVT::i8) || (VMVT == MVT::i16)) { 2049 unsigned TempReg = createResultReg(&Mips::GPR32RegClass); 2050 if (!emitIntExt(VMVT, VReg, MVT::i32, TempReg, IsUnsigned)) 2051 return 0; 2052 VReg = TempReg; 2053 } 2054 return VReg; 2055 } 2056 2057 void MipsFastISel::simplifyAddress(Address &Addr) { 2058 if (!isInt<16>(Addr.getOffset())) { 2059 unsigned TempReg = 2060 materialize32BitInt(Addr.getOffset(), &Mips::GPR32RegClass); 2061 unsigned DestReg = createResultReg(&Mips::GPR32RegClass); 2062 emitInst(Mips::ADDu, DestReg).addReg(TempReg).addReg(Addr.getReg()); 2063 Addr.setReg(DestReg); 2064 Addr.setOffset(0); 2065 } 2066 } 2067 2068 unsigned MipsFastISel::fastEmitInst_rr(unsigned MachineInstOpcode, 2069 const TargetRegisterClass *RC, 2070 unsigned Op0, bool Op0IsKill, 2071 unsigned Op1, bool Op1IsKill) { 2072 // We treat the MUL instruction in a special way because it clobbers 2073 // the HI0 & LO0 registers. The TableGen definition of this instruction can 2074 // mark these registers only as implicitly defined. As a result, the 2075 // register allocator runs out of registers when this instruction is 2076 // followed by another instruction that defines the same registers too. 2077 // We can fix this by explicitly marking those registers as dead. 2078 if (MachineInstOpcode == Mips::MUL) { 2079 unsigned ResultReg = createResultReg(RC); 2080 const MCInstrDesc &II = TII.get(MachineInstOpcode); 2081 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); 2082 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1); 2083 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 2084 .addReg(Op0, getKillRegState(Op0IsKill)) 2085 .addReg(Op1, getKillRegState(Op1IsKill)) 2086 .addReg(Mips::HI0, RegState::ImplicitDefine | RegState::Dead) 2087 .addReg(Mips::LO0, RegState::ImplicitDefine | RegState::Dead); 2088 return ResultReg; 2089 } 2090 2091 return FastISel::fastEmitInst_rr(MachineInstOpcode, RC, Op0, Op0IsKill, Op1, 2092 Op1IsKill); 2093 } 2094 2095 namespace llvm { 2096 FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo, 2097 const TargetLibraryInfo *libInfo) { 2098 return new MipsFastISel(funcInfo, libInfo); 2099 } 2100 } 2101