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 emitInst(CondMovOpc, ResultReg) 696 .addReg(RegWithOne) 697 .addReg(Mips::FCC0) 698 .addReg(RegWithZero); 699 break; 700 } 701 } 702 return true; 703 } 704 bool MipsFastISel::emitLoad(MVT VT, unsigned &ResultReg, Address &Addr, 705 unsigned Alignment) { 706 // 707 // more cases will be handled here in following patches. 708 // 709 unsigned Opc; 710 switch (VT.SimpleTy) { 711 case MVT::i32: { 712 ResultReg = createResultReg(&Mips::GPR32RegClass); 713 Opc = Mips::LW; 714 break; 715 } 716 case MVT::i16: { 717 ResultReg = createResultReg(&Mips::GPR32RegClass); 718 Opc = Mips::LHu; 719 break; 720 } 721 case MVT::i8: { 722 ResultReg = createResultReg(&Mips::GPR32RegClass); 723 Opc = Mips::LBu; 724 break; 725 } 726 case MVT::f32: { 727 if (UnsupportedFPMode) 728 return false; 729 ResultReg = createResultReg(&Mips::FGR32RegClass); 730 Opc = Mips::LWC1; 731 break; 732 } 733 case MVT::f64: { 734 if (UnsupportedFPMode) 735 return false; 736 ResultReg = createResultReg(&Mips::AFGR64RegClass); 737 Opc = Mips::LDC1; 738 break; 739 } 740 default: 741 return false; 742 } 743 if (Addr.isRegBase()) { 744 simplifyAddress(Addr); 745 emitInstLoad(Opc, ResultReg, Addr.getReg(), Addr.getOffset()); 746 return true; 747 } 748 if (Addr.isFIBase()) { 749 unsigned FI = Addr.getFI(); 750 unsigned Align = 4; 751 unsigned Offset = Addr.getOffset(); 752 MachineFrameInfo &MFI = *MF->getFrameInfo(); 753 MachineMemOperand *MMO = MF->getMachineMemOperand( 754 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad, 755 MFI.getObjectSize(FI), Align); 756 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) 757 .addFrameIndex(FI) 758 .addImm(Offset) 759 .addMemOperand(MMO); 760 return true; 761 } 762 return false; 763 } 764 765 bool MipsFastISel::emitStore(MVT VT, unsigned SrcReg, Address &Addr, 766 unsigned Alignment) { 767 // 768 // more cases will be handled here in following patches. 769 // 770 unsigned Opc; 771 switch (VT.SimpleTy) { 772 case MVT::i8: 773 Opc = Mips::SB; 774 break; 775 case MVT::i16: 776 Opc = Mips::SH; 777 break; 778 case MVT::i32: 779 Opc = Mips::SW; 780 break; 781 case MVT::f32: 782 if (UnsupportedFPMode) 783 return false; 784 Opc = Mips::SWC1; 785 break; 786 case MVT::f64: 787 if (UnsupportedFPMode) 788 return false; 789 Opc = Mips::SDC1; 790 break; 791 default: 792 return false; 793 } 794 if (Addr.isRegBase()) { 795 simplifyAddress(Addr); 796 emitInstStore(Opc, SrcReg, Addr.getReg(), Addr.getOffset()); 797 return true; 798 } 799 if (Addr.isFIBase()) { 800 unsigned FI = Addr.getFI(); 801 unsigned Align = 4; 802 unsigned Offset = Addr.getOffset(); 803 MachineFrameInfo &MFI = *MF->getFrameInfo(); 804 MachineMemOperand *MMO = MF->getMachineMemOperand( 805 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOStore, 806 MFI.getObjectSize(FI), Align); 807 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc)) 808 .addReg(SrcReg) 809 .addFrameIndex(FI) 810 .addImm(Offset) 811 .addMemOperand(MMO); 812 return true; 813 } 814 return false; 815 } 816 817 bool MipsFastISel::selectLogicalOp(const Instruction *I) { 818 MVT VT; 819 if (!isTypeSupported(I->getType(), VT)) 820 return false; 821 822 unsigned ResultReg; 823 switch (I->getOpcode()) { 824 default: 825 llvm_unreachable("Unexpected instruction."); 826 case Instruction::And: 827 ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1)); 828 break; 829 case Instruction::Or: 830 ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1)); 831 break; 832 case Instruction::Xor: 833 ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1)); 834 break; 835 } 836 837 if (!ResultReg) 838 return false; 839 840 updateValueMap(I, ResultReg); 841 return true; 842 } 843 844 bool MipsFastISel::selectLoad(const Instruction *I) { 845 // Atomic loads need special handling. 846 if (cast<LoadInst>(I)->isAtomic()) 847 return false; 848 849 // Verify we have a legal type before going any further. 850 MVT VT; 851 if (!isLoadTypeLegal(I->getType(), VT)) 852 return false; 853 854 // See if we can handle this address. 855 Address Addr; 856 if (!computeAddress(I->getOperand(0), Addr)) 857 return false; 858 859 unsigned ResultReg; 860 if (!emitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment())) 861 return false; 862 updateValueMap(I, ResultReg); 863 return true; 864 } 865 866 bool MipsFastISel::selectStore(const Instruction *I) { 867 Value *Op0 = I->getOperand(0); 868 unsigned SrcReg = 0; 869 870 // Atomic stores need special handling. 871 if (cast<StoreInst>(I)->isAtomic()) 872 return false; 873 874 // Verify we have a legal type before going any further. 875 MVT VT; 876 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT)) 877 return false; 878 879 // Get the value to be stored into a register. 880 SrcReg = getRegForValue(Op0); 881 if (SrcReg == 0) 882 return false; 883 884 // See if we can handle this address. 885 Address Addr; 886 if (!computeAddress(I->getOperand(1), Addr)) 887 return false; 888 889 if (!emitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment())) 890 return false; 891 return true; 892 } 893 894 // 895 // This can cause a redundant sltiu to be generated. 896 // FIXME: try and eliminate this in a future patch. 897 // 898 bool MipsFastISel::selectBranch(const Instruction *I) { 899 const BranchInst *BI = cast<BranchInst>(I); 900 MachineBasicBlock *BrBB = FuncInfo.MBB; 901 // 902 // TBB is the basic block for the case where the comparison is true. 903 // FBB is the basic block for the case where the comparison is false. 904 // if (cond) goto TBB 905 // goto FBB 906 // TBB: 907 // 908 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)]; 909 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)]; 910 BI->getCondition(); 911 // For now, just try the simplest case where it's fed by a compare. 912 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) { 913 unsigned CondReg = createResultReg(&Mips::GPR32RegClass); 914 if (!emitCmp(CondReg, CI)) 915 return false; 916 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::BGTZ)) 917 .addReg(CondReg) 918 .addMBB(TBB); 919 finishCondBranch(BI->getParent(), TBB, FBB); 920 return true; 921 } 922 return false; 923 } 924 925 bool MipsFastISel::selectCmp(const Instruction *I) { 926 const CmpInst *CI = cast<CmpInst>(I); 927 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass); 928 if (!emitCmp(ResultReg, CI)) 929 return false; 930 updateValueMap(I, ResultReg); 931 return true; 932 } 933 934 // Attempt to fast-select a floating-point extend instruction. 935 bool MipsFastISel::selectFPExt(const Instruction *I) { 936 if (UnsupportedFPMode) 937 return false; 938 Value *Src = I->getOperand(0); 939 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true); 940 EVT DestVT = TLI.getValueType(DL, I->getType(), true); 941 942 if (SrcVT != MVT::f32 || DestVT != MVT::f64) 943 return false; 944 945 unsigned SrcReg = 946 getRegForValue(Src); // his must be a 32 bit floating point register class 947 // maybe we should handle this differently 948 if (!SrcReg) 949 return false; 950 951 unsigned DestReg = createResultReg(&Mips::AFGR64RegClass); 952 emitInst(Mips::CVT_D32_S, DestReg).addReg(SrcReg); 953 updateValueMap(I, DestReg); 954 return true; 955 } 956 957 bool MipsFastISel::selectSelect(const Instruction *I) { 958 assert(isa<SelectInst>(I) && "Expected a select instruction."); 959 960 MVT VT; 961 if (!isTypeSupported(I->getType(), VT)) 962 return false; 963 964 unsigned CondMovOpc; 965 const TargetRegisterClass *RC; 966 967 if (VT.isInteger() && !VT.isVector() && VT.getSizeInBits() <= 32) { 968 CondMovOpc = Mips::MOVN_I_I; 969 RC = &Mips::GPR32RegClass; 970 } else if (VT == MVT::f32) { 971 CondMovOpc = Mips::MOVN_I_S; 972 RC = &Mips::FGR32RegClass; 973 } else if (VT == MVT::f64) { 974 CondMovOpc = Mips::MOVN_I_D32; 975 RC = &Mips::AFGR64RegClass; 976 } else 977 return false; 978 979 const SelectInst *SI = cast<SelectInst>(I); 980 const Value *Cond = SI->getCondition(); 981 unsigned Src1Reg = getRegForValue(SI->getTrueValue()); 982 unsigned Src2Reg = getRegForValue(SI->getFalseValue()); 983 unsigned CondReg = getRegForValue(Cond); 984 985 if (!Src1Reg || !Src2Reg || !CondReg) 986 return false; 987 988 unsigned ZExtCondReg = createResultReg(&Mips::GPR32RegClass); 989 if (!ZExtCondReg) 990 return false; 991 992 if (!emitIntExt(MVT::i1, CondReg, MVT::i32, ZExtCondReg, true)) 993 return false; 994 995 unsigned ResultReg = createResultReg(RC); 996 unsigned TempReg = createResultReg(RC); 997 998 if (!ResultReg || !TempReg) 999 return false; 1000 1001 emitInst(TargetOpcode::COPY, TempReg).addReg(Src2Reg); 1002 emitInst(CondMovOpc, ResultReg) 1003 .addReg(Src1Reg).addReg(ZExtCondReg).addReg(TempReg); 1004 updateValueMap(I, ResultReg); 1005 return true; 1006 } 1007 1008 // Attempt to fast-select a floating-point truncate instruction. 1009 bool MipsFastISel::selectFPTrunc(const Instruction *I) { 1010 if (UnsupportedFPMode) 1011 return false; 1012 Value *Src = I->getOperand(0); 1013 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true); 1014 EVT DestVT = TLI.getValueType(DL, I->getType(), true); 1015 1016 if (SrcVT != MVT::f64 || DestVT != MVT::f32) 1017 return false; 1018 1019 unsigned SrcReg = getRegForValue(Src); 1020 if (!SrcReg) 1021 return false; 1022 1023 unsigned DestReg = createResultReg(&Mips::FGR32RegClass); 1024 if (!DestReg) 1025 return false; 1026 1027 emitInst(Mips::CVT_S_D32, DestReg).addReg(SrcReg); 1028 updateValueMap(I, DestReg); 1029 return true; 1030 } 1031 1032 // Attempt to fast-select a floating-point-to-integer conversion. 1033 bool MipsFastISel::selectFPToInt(const Instruction *I, bool IsSigned) { 1034 if (UnsupportedFPMode) 1035 return false; 1036 MVT DstVT, SrcVT; 1037 if (!IsSigned) 1038 return false; // We don't handle this case yet. There is no native 1039 // instruction for this but it can be synthesized. 1040 Type *DstTy = I->getType(); 1041 if (!isTypeLegal(DstTy, DstVT)) 1042 return false; 1043 1044 if (DstVT != MVT::i32) 1045 return false; 1046 1047 Value *Src = I->getOperand(0); 1048 Type *SrcTy = Src->getType(); 1049 if (!isTypeLegal(SrcTy, SrcVT)) 1050 return false; 1051 1052 if (SrcVT != MVT::f32 && SrcVT != MVT::f64) 1053 return false; 1054 1055 unsigned SrcReg = getRegForValue(Src); 1056 if (SrcReg == 0) 1057 return false; 1058 1059 // Determine the opcode for the conversion, which takes place 1060 // entirely within FPRs. 1061 unsigned DestReg = createResultReg(&Mips::GPR32RegClass); 1062 unsigned TempReg = createResultReg(&Mips::FGR32RegClass); 1063 unsigned Opc = (SrcVT == MVT::f32) ? Mips::TRUNC_W_S : Mips::TRUNC_W_D32; 1064 1065 // Generate the convert. 1066 emitInst(Opc, TempReg).addReg(SrcReg); 1067 emitInst(Mips::MFC1, DestReg).addReg(TempReg); 1068 1069 updateValueMap(I, DestReg); 1070 return true; 1071 } 1072 1073 bool MipsFastISel::processCallArgs(CallLoweringInfo &CLI, 1074 SmallVectorImpl<MVT> &OutVTs, 1075 unsigned &NumBytes) { 1076 CallingConv::ID CC = CLI.CallConv; 1077 SmallVector<CCValAssign, 16> ArgLocs; 1078 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context); 1079 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC)); 1080 // Get a count of how many bytes are to be pushed on the stack. 1081 NumBytes = CCInfo.getNextStackOffset(); 1082 // This is the minimum argument area used for A0-A3. 1083 if (NumBytes < 16) 1084 NumBytes = 16; 1085 1086 emitInst(Mips::ADJCALLSTACKDOWN).addImm(16); 1087 // Process the args. 1088 MVT firstMVT; 1089 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 1090 CCValAssign &VA = ArgLocs[i]; 1091 const Value *ArgVal = CLI.OutVals[VA.getValNo()]; 1092 MVT ArgVT = OutVTs[VA.getValNo()]; 1093 1094 if (i == 0) { 1095 firstMVT = ArgVT; 1096 if (ArgVT == MVT::f32) { 1097 VA.convertToReg(Mips::F12); 1098 } else if (ArgVT == MVT::f64) { 1099 VA.convertToReg(Mips::D6); 1100 } 1101 } else if (i == 1) { 1102 if ((firstMVT == MVT::f32) || (firstMVT == MVT::f64)) { 1103 if (ArgVT == MVT::f32) { 1104 VA.convertToReg(Mips::F14); 1105 } else if (ArgVT == MVT::f64) { 1106 VA.convertToReg(Mips::D7); 1107 } 1108 } 1109 } 1110 if (((ArgVT == MVT::i32) || (ArgVT == MVT::f32) || (ArgVT == MVT::i16) || 1111 (ArgVT == MVT::i8)) && 1112 VA.isMemLoc()) { 1113 switch (VA.getLocMemOffset()) { 1114 case 0: 1115 VA.convertToReg(Mips::A0); 1116 break; 1117 case 4: 1118 VA.convertToReg(Mips::A1); 1119 break; 1120 case 8: 1121 VA.convertToReg(Mips::A2); 1122 break; 1123 case 12: 1124 VA.convertToReg(Mips::A3); 1125 break; 1126 default: 1127 break; 1128 } 1129 } 1130 unsigned ArgReg = getRegForValue(ArgVal); 1131 if (!ArgReg) 1132 return false; 1133 1134 // Handle arg promotion: SExt, ZExt, AExt. 1135 switch (VA.getLocInfo()) { 1136 case CCValAssign::Full: 1137 break; 1138 case CCValAssign::AExt: 1139 case CCValAssign::SExt: { 1140 MVT DestVT = VA.getLocVT(); 1141 MVT SrcVT = ArgVT; 1142 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false); 1143 if (!ArgReg) 1144 return false; 1145 break; 1146 } 1147 case CCValAssign::ZExt: { 1148 MVT DestVT = VA.getLocVT(); 1149 MVT SrcVT = ArgVT; 1150 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true); 1151 if (!ArgReg) 1152 return false; 1153 break; 1154 } 1155 default: 1156 llvm_unreachable("Unknown arg promotion!"); 1157 } 1158 1159 // Now copy/store arg to correct locations. 1160 if (VA.isRegLoc() && !VA.needsCustom()) { 1161 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1162 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg); 1163 CLI.OutRegs.push_back(VA.getLocReg()); 1164 } else if (VA.needsCustom()) { 1165 llvm_unreachable("Mips does not use custom args."); 1166 return false; 1167 } else { 1168 // 1169 // FIXME: This path will currently return false. It was copied 1170 // from the AArch64 port and should be essentially fine for Mips too. 1171 // The work to finish up this path will be done in a follow-on patch. 1172 // 1173 assert(VA.isMemLoc() && "Assuming store on stack."); 1174 // Don't emit stores for undef values. 1175 if (isa<UndefValue>(ArgVal)) 1176 continue; 1177 1178 // Need to store on the stack. 1179 // FIXME: This alignment is incorrect but this path is disabled 1180 // for now (will return false). We need to determine the right alignment 1181 // based on the normal alignment for the underlying machine type. 1182 // 1183 unsigned ArgSize = alignTo(ArgVT.getSizeInBits(), 4); 1184 1185 unsigned BEAlign = 0; 1186 if (ArgSize < 8 && !Subtarget->isLittle()) 1187 BEAlign = 8 - ArgSize; 1188 1189 Address Addr; 1190 Addr.setKind(Address::RegBase); 1191 Addr.setReg(Mips::SP); 1192 Addr.setOffset(VA.getLocMemOffset() + BEAlign); 1193 1194 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType()); 1195 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand( 1196 MachinePointerInfo::getStack(*FuncInfo.MF, Addr.getOffset()), 1197 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment); 1198 (void)(MMO); 1199 // if (!emitStore(ArgVT, ArgReg, Addr, MMO)) 1200 return false; // can't store on the stack yet. 1201 } 1202 } 1203 1204 return true; 1205 } 1206 1207 bool MipsFastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT, 1208 unsigned NumBytes) { 1209 CallingConv::ID CC = CLI.CallConv; 1210 emitInst(Mips::ADJCALLSTACKUP).addImm(16).addImm(0); 1211 if (RetVT != MVT::isVoid) { 1212 SmallVector<CCValAssign, 16> RVLocs; 1213 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context); 1214 CCInfo.AnalyzeCallResult(RetVT, RetCC_Mips); 1215 1216 // Only handle a single return value. 1217 if (RVLocs.size() != 1) 1218 return false; 1219 // Copy all of the result registers out of their specified physreg. 1220 MVT CopyVT = RVLocs[0].getValVT(); 1221 // Special handling for extended integers. 1222 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16) 1223 CopyVT = MVT::i32; 1224 1225 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT)); 1226 if (!ResultReg) 1227 return false; 1228 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1229 TII.get(TargetOpcode::COPY), 1230 ResultReg).addReg(RVLocs[0].getLocReg()); 1231 CLI.InRegs.push_back(RVLocs[0].getLocReg()); 1232 1233 CLI.ResultReg = ResultReg; 1234 CLI.NumResultRegs = 1; 1235 } 1236 return true; 1237 } 1238 1239 bool MipsFastISel::fastLowerCall(CallLoweringInfo &CLI) { 1240 if (!TargetSupported) 1241 return false; 1242 1243 CallingConv::ID CC = CLI.CallConv; 1244 bool IsTailCall = CLI.IsTailCall; 1245 bool IsVarArg = CLI.IsVarArg; 1246 const Value *Callee = CLI.Callee; 1247 MCSymbol *Symbol = CLI.Symbol; 1248 1249 // Do not handle FastCC. 1250 if (CC == CallingConv::Fast) 1251 return false; 1252 1253 // Allow SelectionDAG isel to handle tail calls. 1254 if (IsTailCall) 1255 return false; 1256 1257 // Let SDISel handle vararg functions. 1258 if (IsVarArg) 1259 return false; 1260 1261 // FIXME: Only handle *simple* calls for now. 1262 MVT RetVT; 1263 if (CLI.RetTy->isVoidTy()) 1264 RetVT = MVT::isVoid; 1265 else if (!isTypeSupported(CLI.RetTy, RetVT)) 1266 return false; 1267 1268 for (auto Flag : CLI.OutFlags) 1269 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal()) 1270 return false; 1271 1272 // Set up the argument vectors. 1273 SmallVector<MVT, 16> OutVTs; 1274 OutVTs.reserve(CLI.OutVals.size()); 1275 1276 for (auto *Val : CLI.OutVals) { 1277 MVT VT; 1278 if (!isTypeLegal(Val->getType(), VT) && 1279 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)) 1280 return false; 1281 1282 // We don't handle vector parameters yet. 1283 if (VT.isVector() || VT.getSizeInBits() > 64) 1284 return false; 1285 1286 OutVTs.push_back(VT); 1287 } 1288 1289 Address Addr; 1290 if (!computeCallAddress(Callee, Addr)) 1291 return false; 1292 1293 // Handle the arguments now that we've gotten them. 1294 unsigned NumBytes; 1295 if (!processCallArgs(CLI, OutVTs, NumBytes)) 1296 return false; 1297 1298 if (!Addr.getGlobalValue()) 1299 return false; 1300 1301 // Issue the call. 1302 unsigned DestAddress; 1303 if (Symbol) 1304 DestAddress = materializeExternalCallSym(Symbol); 1305 else 1306 DestAddress = materializeGV(Addr.getGlobalValue(), MVT::i32); 1307 emitInst(TargetOpcode::COPY, Mips::T9).addReg(DestAddress); 1308 MachineInstrBuilder MIB = 1309 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::JALR), 1310 Mips::RA).addReg(Mips::T9); 1311 1312 // Add implicit physical register uses to the call. 1313 for (auto Reg : CLI.OutRegs) 1314 MIB.addReg(Reg, RegState::Implicit); 1315 1316 // Add a register mask with the call-preserved registers. 1317 // Proper defs for return values will be added by setPhysRegsDeadExcept(). 1318 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC)); 1319 1320 CLI.Call = MIB; 1321 1322 // Finish off the call including any return values. 1323 return finishCall(CLI, RetVT, NumBytes); 1324 } 1325 1326 bool MipsFastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) { 1327 if (!TargetSupported) 1328 return false; 1329 1330 switch (II->getIntrinsicID()) { 1331 default: 1332 return false; 1333 case Intrinsic::bswap: { 1334 Type *RetTy = II->getCalledFunction()->getReturnType(); 1335 1336 MVT VT; 1337 if (!isTypeSupported(RetTy, VT)) 1338 return false; 1339 1340 unsigned SrcReg = getRegForValue(II->getOperand(0)); 1341 if (SrcReg == 0) 1342 return false; 1343 unsigned DestReg = createResultReg(&Mips::GPR32RegClass); 1344 if (DestReg == 0) 1345 return false; 1346 if (VT == MVT::i16) { 1347 if (Subtarget->hasMips32r2()) { 1348 emitInst(Mips::WSBH, DestReg).addReg(SrcReg); 1349 updateValueMap(II, DestReg); 1350 return true; 1351 } else { 1352 unsigned TempReg[3]; 1353 for (int i = 0; i < 3; i++) { 1354 TempReg[i] = createResultReg(&Mips::GPR32RegClass); 1355 if (TempReg[i] == 0) 1356 return false; 1357 } 1358 emitInst(Mips::SLL, TempReg[0]).addReg(SrcReg).addImm(8); 1359 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(8); 1360 emitInst(Mips::OR, TempReg[2]).addReg(TempReg[0]).addReg(TempReg[1]); 1361 emitInst(Mips::ANDi, DestReg).addReg(TempReg[2]).addImm(0xFFFF); 1362 updateValueMap(II, DestReg); 1363 return true; 1364 } 1365 } else if (VT == MVT::i32) { 1366 if (Subtarget->hasMips32r2()) { 1367 unsigned TempReg = createResultReg(&Mips::GPR32RegClass); 1368 emitInst(Mips::WSBH, TempReg).addReg(SrcReg); 1369 emitInst(Mips::ROTR, DestReg).addReg(TempReg).addImm(16); 1370 updateValueMap(II, DestReg); 1371 return true; 1372 } else { 1373 unsigned TempReg[8]; 1374 for (int i = 0; i < 8; i++) { 1375 TempReg[i] = createResultReg(&Mips::GPR32RegClass); 1376 if (TempReg[i] == 0) 1377 return false; 1378 } 1379 1380 emitInst(Mips::SRL, TempReg[0]).addReg(SrcReg).addImm(8); 1381 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(24); 1382 emitInst(Mips::ANDi, TempReg[2]).addReg(TempReg[0]).addImm(0xFF00); 1383 emitInst(Mips::OR, TempReg[3]).addReg(TempReg[1]).addReg(TempReg[2]); 1384 1385 emitInst(Mips::ANDi, TempReg[4]).addReg(SrcReg).addImm(0xFF00); 1386 emitInst(Mips::SLL, TempReg[5]).addReg(TempReg[4]).addImm(8); 1387 1388 emitInst(Mips::SLL, TempReg[6]).addReg(SrcReg).addImm(24); 1389 emitInst(Mips::OR, TempReg[7]).addReg(TempReg[3]).addReg(TempReg[5]); 1390 emitInst(Mips::OR, DestReg).addReg(TempReg[6]).addReg(TempReg[7]); 1391 updateValueMap(II, DestReg); 1392 return true; 1393 } 1394 } 1395 return false; 1396 } 1397 case Intrinsic::memcpy: 1398 case Intrinsic::memmove: { 1399 const auto *MTI = cast<MemTransferInst>(II); 1400 // Don't handle volatile. 1401 if (MTI->isVolatile()) 1402 return false; 1403 if (!MTI->getLength()->getType()->isIntegerTy(32)) 1404 return false; 1405 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove"; 1406 return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2); 1407 } 1408 case Intrinsic::memset: { 1409 const MemSetInst *MSI = cast<MemSetInst>(II); 1410 // Don't handle volatile. 1411 if (MSI->isVolatile()) 1412 return false; 1413 if (!MSI->getLength()->getType()->isIntegerTy(32)) 1414 return false; 1415 return lowerCallTo(II, "memset", II->getNumArgOperands() - 2); 1416 } 1417 } 1418 return false; 1419 } 1420 1421 bool MipsFastISel::selectRet(const Instruction *I) { 1422 const Function &F = *I->getParent()->getParent(); 1423 const ReturnInst *Ret = cast<ReturnInst>(I); 1424 1425 if (!FuncInfo.CanLowerReturn) 1426 return false; 1427 1428 // Build a list of return value registers. 1429 SmallVector<unsigned, 4> RetRegs; 1430 1431 if (Ret->getNumOperands() > 0) { 1432 CallingConv::ID CC = F.getCallingConv(); 1433 1434 // Do not handle FastCC. 1435 if (CC == CallingConv::Fast) 1436 return false; 1437 1438 SmallVector<ISD::OutputArg, 4> Outs; 1439 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL); 1440 1441 // Analyze operands of the call, assigning locations to each operand. 1442 SmallVector<CCValAssign, 16> ValLocs; 1443 MipsCCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, 1444 I->getContext()); 1445 CCAssignFn *RetCC = RetCC_Mips; 1446 CCInfo.AnalyzeReturn(Outs, RetCC); 1447 1448 // Only handle a single return value for now. 1449 if (ValLocs.size() != 1) 1450 return false; 1451 1452 CCValAssign &VA = ValLocs[0]; 1453 const Value *RV = Ret->getOperand(0); 1454 1455 // Don't bother handling odd stuff for now. 1456 if ((VA.getLocInfo() != CCValAssign::Full) && 1457 (VA.getLocInfo() != CCValAssign::BCvt)) 1458 return false; 1459 1460 // Only handle register returns for now. 1461 if (!VA.isRegLoc()) 1462 return false; 1463 1464 unsigned Reg = getRegForValue(RV); 1465 if (Reg == 0) 1466 return false; 1467 1468 unsigned SrcReg = Reg + VA.getValNo(); 1469 unsigned DestReg = VA.getLocReg(); 1470 // Avoid a cross-class copy. This is very unlikely. 1471 if (!MRI.getRegClass(SrcReg)->contains(DestReg)) 1472 return false; 1473 1474 EVT RVEVT = TLI.getValueType(DL, RV->getType()); 1475 if (!RVEVT.isSimple()) 1476 return false; 1477 1478 if (RVEVT.isVector()) 1479 return false; 1480 1481 MVT RVVT = RVEVT.getSimpleVT(); 1482 if (RVVT == MVT::f128) 1483 return false; 1484 1485 MVT DestVT = VA.getValVT(); 1486 // Special handling for extended integers. 1487 if (RVVT != DestVT) { 1488 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16) 1489 return false; 1490 1491 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) { 1492 bool IsZExt = Outs[0].Flags.isZExt(); 1493 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt); 1494 if (SrcReg == 0) 1495 return false; 1496 } 1497 } 1498 1499 // Make the copy. 1500 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1501 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg); 1502 1503 // Add register to return instruction. 1504 RetRegs.push_back(VA.getLocReg()); 1505 } 1506 MachineInstrBuilder MIB = emitInst(Mips::RetRA); 1507 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i) 1508 MIB.addReg(RetRegs[i], RegState::Implicit); 1509 return true; 1510 } 1511 1512 bool MipsFastISel::selectTrunc(const Instruction *I) { 1513 // The high bits for a type smaller than the register size are assumed to be 1514 // undefined. 1515 Value *Op = I->getOperand(0); 1516 1517 EVT SrcVT, DestVT; 1518 SrcVT = TLI.getValueType(DL, Op->getType(), true); 1519 DestVT = TLI.getValueType(DL, I->getType(), true); 1520 1521 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8) 1522 return false; 1523 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1) 1524 return false; 1525 1526 unsigned SrcReg = getRegForValue(Op); 1527 if (!SrcReg) 1528 return false; 1529 1530 // Because the high bits are undefined, a truncate doesn't generate 1531 // any code. 1532 updateValueMap(I, SrcReg); 1533 return true; 1534 } 1535 bool MipsFastISel::selectIntExt(const Instruction *I) { 1536 Type *DestTy = I->getType(); 1537 Value *Src = I->getOperand(0); 1538 Type *SrcTy = Src->getType(); 1539 1540 bool isZExt = isa<ZExtInst>(I); 1541 unsigned SrcReg = getRegForValue(Src); 1542 if (!SrcReg) 1543 return false; 1544 1545 EVT SrcEVT, DestEVT; 1546 SrcEVT = TLI.getValueType(DL, SrcTy, true); 1547 DestEVT = TLI.getValueType(DL, DestTy, true); 1548 if (!SrcEVT.isSimple()) 1549 return false; 1550 if (!DestEVT.isSimple()) 1551 return false; 1552 1553 MVT SrcVT = SrcEVT.getSimpleVT(); 1554 MVT DestVT = DestEVT.getSimpleVT(); 1555 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass); 1556 1557 if (!emitIntExt(SrcVT, SrcReg, DestVT, ResultReg, isZExt)) 1558 return false; 1559 updateValueMap(I, ResultReg); 1560 return true; 1561 } 1562 bool MipsFastISel::emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT, 1563 unsigned DestReg) { 1564 unsigned ShiftAmt; 1565 switch (SrcVT.SimpleTy) { 1566 default: 1567 return false; 1568 case MVT::i8: 1569 ShiftAmt = 24; 1570 break; 1571 case MVT::i16: 1572 ShiftAmt = 16; 1573 break; 1574 } 1575 unsigned TempReg = createResultReg(&Mips::GPR32RegClass); 1576 emitInst(Mips::SLL, TempReg).addReg(SrcReg).addImm(ShiftAmt); 1577 emitInst(Mips::SRA, DestReg).addReg(TempReg).addImm(ShiftAmt); 1578 return true; 1579 } 1580 1581 bool MipsFastISel::emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT, 1582 unsigned DestReg) { 1583 switch (SrcVT.SimpleTy) { 1584 default: 1585 return false; 1586 case MVT::i8: 1587 emitInst(Mips::SEB, DestReg).addReg(SrcReg); 1588 break; 1589 case MVT::i16: 1590 emitInst(Mips::SEH, DestReg).addReg(SrcReg); 1591 break; 1592 } 1593 return true; 1594 } 1595 1596 bool MipsFastISel::emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, 1597 unsigned DestReg) { 1598 if ((DestVT != MVT::i32) && (DestVT != MVT::i16)) 1599 return false; 1600 if (Subtarget->hasMips32r2()) 1601 return emitIntSExt32r2(SrcVT, SrcReg, DestVT, DestReg); 1602 return emitIntSExt32r1(SrcVT, SrcReg, DestVT, DestReg); 1603 } 1604 1605 bool MipsFastISel::emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, 1606 unsigned DestReg) { 1607 int64_t Imm; 1608 1609 switch (SrcVT.SimpleTy) { 1610 default: 1611 return false; 1612 case MVT::i1: 1613 Imm = 1; 1614 break; 1615 case MVT::i8: 1616 Imm = 0xff; 1617 break; 1618 case MVT::i16: 1619 Imm = 0xffff; 1620 break; 1621 } 1622 1623 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(Imm); 1624 return true; 1625 } 1626 1627 bool MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, 1628 unsigned DestReg, bool IsZExt) { 1629 // FastISel does not have plumbing to deal with extensions where the SrcVT or 1630 // DestVT are odd things, so test to make sure that they are both types we can 1631 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise 1632 // bail out to SelectionDAG. 1633 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) && (DestVT != MVT::i32)) || 1634 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) && (SrcVT != MVT::i16))) 1635 return false; 1636 if (IsZExt) 1637 return emitIntZExt(SrcVT, SrcReg, DestVT, DestReg); 1638 return emitIntSExt(SrcVT, SrcReg, DestVT, DestReg); 1639 } 1640 1641 unsigned MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, 1642 bool isZExt) { 1643 unsigned DestReg = createResultReg(&Mips::GPR32RegClass); 1644 bool Success = emitIntExt(SrcVT, SrcReg, DestVT, DestReg, isZExt); 1645 return Success ? DestReg : 0; 1646 } 1647 1648 bool MipsFastISel::selectDivRem(const Instruction *I, unsigned ISDOpcode) { 1649 EVT DestEVT = TLI.getValueType(DL, I->getType(), true); 1650 if (!DestEVT.isSimple()) 1651 return false; 1652 1653 MVT DestVT = DestEVT.getSimpleVT(); 1654 if (DestVT != MVT::i32) 1655 return false; 1656 1657 unsigned DivOpc; 1658 switch (ISDOpcode) { 1659 default: 1660 return false; 1661 case ISD::SDIV: 1662 case ISD::SREM: 1663 DivOpc = Mips::SDIV; 1664 break; 1665 case ISD::UDIV: 1666 case ISD::UREM: 1667 DivOpc = Mips::UDIV; 1668 break; 1669 } 1670 1671 unsigned Src0Reg = getRegForValue(I->getOperand(0)); 1672 unsigned Src1Reg = getRegForValue(I->getOperand(1)); 1673 if (!Src0Reg || !Src1Reg) 1674 return false; 1675 1676 emitInst(DivOpc).addReg(Src0Reg).addReg(Src1Reg); 1677 emitInst(Mips::TEQ).addReg(Src1Reg).addReg(Mips::ZERO).addImm(7); 1678 1679 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass); 1680 if (!ResultReg) 1681 return false; 1682 1683 unsigned MFOpc = (ISDOpcode == ISD::SREM || ISDOpcode == ISD::UREM) 1684 ? Mips::MFHI 1685 : Mips::MFLO; 1686 emitInst(MFOpc, ResultReg); 1687 1688 updateValueMap(I, ResultReg); 1689 return true; 1690 } 1691 1692 bool MipsFastISel::selectShift(const Instruction *I) { 1693 MVT RetVT; 1694 1695 if (!isTypeSupported(I->getType(), RetVT)) 1696 return false; 1697 1698 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass); 1699 if (!ResultReg) 1700 return false; 1701 1702 unsigned Opcode = I->getOpcode(); 1703 const Value *Op0 = I->getOperand(0); 1704 unsigned Op0Reg = getRegForValue(Op0); 1705 if (!Op0Reg) 1706 return false; 1707 1708 // If AShr or LShr, then we need to make sure the operand0 is sign extended. 1709 if (Opcode == Instruction::AShr || Opcode == Instruction::LShr) { 1710 unsigned TempReg = createResultReg(&Mips::GPR32RegClass); 1711 if (!TempReg) 1712 return false; 1713 1714 MVT Op0MVT = TLI.getValueType(DL, Op0->getType(), true).getSimpleVT(); 1715 bool IsZExt = Opcode == Instruction::LShr; 1716 if (!emitIntExt(Op0MVT, Op0Reg, MVT::i32, TempReg, IsZExt)) 1717 return false; 1718 1719 Op0Reg = TempReg; 1720 } 1721 1722 if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) { 1723 uint64_t ShiftVal = C->getZExtValue(); 1724 1725 switch (Opcode) { 1726 default: 1727 llvm_unreachable("Unexpected instruction."); 1728 case Instruction::Shl: 1729 Opcode = Mips::SLL; 1730 break; 1731 case Instruction::AShr: 1732 Opcode = Mips::SRA; 1733 break; 1734 case Instruction::LShr: 1735 Opcode = Mips::SRL; 1736 break; 1737 } 1738 1739 emitInst(Opcode, ResultReg).addReg(Op0Reg).addImm(ShiftVal); 1740 updateValueMap(I, ResultReg); 1741 return true; 1742 } 1743 1744 unsigned Op1Reg = getRegForValue(I->getOperand(1)); 1745 if (!Op1Reg) 1746 return false; 1747 1748 switch (Opcode) { 1749 default: 1750 llvm_unreachable("Unexpected instruction."); 1751 case Instruction::Shl: 1752 Opcode = Mips::SLLV; 1753 break; 1754 case Instruction::AShr: 1755 Opcode = Mips::SRAV; 1756 break; 1757 case Instruction::LShr: 1758 Opcode = Mips::SRLV; 1759 break; 1760 } 1761 1762 emitInst(Opcode, ResultReg).addReg(Op0Reg).addReg(Op1Reg); 1763 updateValueMap(I, ResultReg); 1764 return true; 1765 } 1766 1767 bool MipsFastISel::fastSelectInstruction(const Instruction *I) { 1768 if (!TargetSupported) 1769 return false; 1770 switch (I->getOpcode()) { 1771 default: 1772 break; 1773 case Instruction::Load: 1774 return selectLoad(I); 1775 case Instruction::Store: 1776 return selectStore(I); 1777 case Instruction::SDiv: 1778 if (!selectBinaryOp(I, ISD::SDIV)) 1779 return selectDivRem(I, ISD::SDIV); 1780 return true; 1781 case Instruction::UDiv: 1782 if (!selectBinaryOp(I, ISD::UDIV)) 1783 return selectDivRem(I, ISD::UDIV); 1784 return true; 1785 case Instruction::SRem: 1786 if (!selectBinaryOp(I, ISD::SREM)) 1787 return selectDivRem(I, ISD::SREM); 1788 return true; 1789 case Instruction::URem: 1790 if (!selectBinaryOp(I, ISD::UREM)) 1791 return selectDivRem(I, ISD::UREM); 1792 return true; 1793 case Instruction::Shl: 1794 case Instruction::LShr: 1795 case Instruction::AShr: 1796 return selectShift(I); 1797 case Instruction::And: 1798 case Instruction::Or: 1799 case Instruction::Xor: 1800 return selectLogicalOp(I); 1801 case Instruction::Br: 1802 return selectBranch(I); 1803 case Instruction::Ret: 1804 return selectRet(I); 1805 case Instruction::Trunc: 1806 return selectTrunc(I); 1807 case Instruction::ZExt: 1808 case Instruction::SExt: 1809 return selectIntExt(I); 1810 case Instruction::FPTrunc: 1811 return selectFPTrunc(I); 1812 case Instruction::FPExt: 1813 return selectFPExt(I); 1814 case Instruction::FPToSI: 1815 return selectFPToInt(I, /*isSigned*/ true); 1816 case Instruction::FPToUI: 1817 return selectFPToInt(I, /*isSigned*/ false); 1818 case Instruction::ICmp: 1819 case Instruction::FCmp: 1820 return selectCmp(I); 1821 case Instruction::Select: 1822 return selectSelect(I); 1823 } 1824 return false; 1825 } 1826 1827 unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value *V, 1828 bool IsUnsigned) { 1829 unsigned VReg = getRegForValue(V); 1830 if (VReg == 0) 1831 return 0; 1832 MVT VMVT = TLI.getValueType(DL, V->getType(), true).getSimpleVT(); 1833 if ((VMVT == MVT::i8) || (VMVT == MVT::i16)) { 1834 unsigned TempReg = createResultReg(&Mips::GPR32RegClass); 1835 if (!emitIntExt(VMVT, VReg, MVT::i32, TempReg, IsUnsigned)) 1836 return 0; 1837 VReg = TempReg; 1838 } 1839 return VReg; 1840 } 1841 1842 void MipsFastISel::simplifyAddress(Address &Addr) { 1843 if (!isInt<16>(Addr.getOffset())) { 1844 unsigned TempReg = 1845 materialize32BitInt(Addr.getOffset(), &Mips::GPR32RegClass); 1846 unsigned DestReg = createResultReg(&Mips::GPR32RegClass); 1847 emitInst(Mips::ADDu, DestReg).addReg(TempReg).addReg(Addr.getReg()); 1848 Addr.setReg(DestReg); 1849 Addr.setOffset(0); 1850 } 1851 } 1852 1853 unsigned MipsFastISel::fastEmitInst_rr(unsigned MachineInstOpcode, 1854 const TargetRegisterClass *RC, 1855 unsigned Op0, bool Op0IsKill, 1856 unsigned Op1, bool Op1IsKill) { 1857 // We treat the MUL instruction in a special way because it clobbers 1858 // the HI0 & LO0 registers. The TableGen definition of this instruction can 1859 // mark these registers only as implicitly defined. As a result, the 1860 // register allocator runs out of registers when this instruction is 1861 // followed by another instruction that defines the same registers too. 1862 // We can fix this by explicitly marking those registers as dead. 1863 if (MachineInstOpcode == Mips::MUL) { 1864 unsigned ResultReg = createResultReg(RC); 1865 const MCInstrDesc &II = TII.get(MachineInstOpcode); 1866 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); 1867 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1); 1868 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) 1869 .addReg(Op0, getKillRegState(Op0IsKill)) 1870 .addReg(Op1, getKillRegState(Op1IsKill)) 1871 .addReg(Mips::HI0, RegState::ImplicitDefine | RegState::Dead) 1872 .addReg(Mips::LO0, RegState::ImplicitDefine | RegState::Dead); 1873 return ResultReg; 1874 } 1875 1876 return FastISel::fastEmitInst_rr(MachineInstOpcode, RC, Op0, Op0IsKill, Op1, 1877 Op1IsKill); 1878 } 1879 1880 namespace llvm { 1881 FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo, 1882 const TargetLibraryInfo *libInfo) { 1883 return new MipsFastISel(funcInfo, libInfo); 1884 } 1885 } 1886