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