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