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