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