1 //===-- PPCFastISel.cpp - PowerPC FastISel implementation -----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the PowerPC-specific support for the FastISel class. Some 11 // of the target-specific code is generated by tablegen in the file 12 // PPCGenFastISel.inc, which is #included here. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "PPC.h" 17 #include "MCTargetDesc/PPCPredicates.h" 18 #include "PPCISelLowering.h" 19 #include "PPCSubtarget.h" 20 #include "PPCTargetMachine.h" 21 #include "llvm/ADT/Optional.h" 22 #include "llvm/CodeGen/CallingConvLower.h" 23 #include "llvm/CodeGen/FastISel.h" 24 #include "llvm/CodeGen/FunctionLoweringInfo.h" 25 #include "llvm/CodeGen/MachineConstantPool.h" 26 #include "llvm/CodeGen/MachineFrameInfo.h" 27 #include "llvm/CodeGen/MachineInstrBuilder.h" 28 #include "llvm/CodeGen/MachineRegisterInfo.h" 29 #include "llvm/IR/CallingConv.h" 30 #include "llvm/IR/GetElementPtrTypeIterator.h" 31 #include "llvm/IR/GlobalAlias.h" 32 #include "llvm/IR/GlobalVariable.h" 33 #include "llvm/IR/IntrinsicInst.h" 34 #include "llvm/IR/Operator.h" 35 #include "llvm/Support/Debug.h" 36 #include "llvm/Target/TargetLowering.h" 37 #include "llvm/Target/TargetMachine.h" 38 39 //===----------------------------------------------------------------------===// 40 // 41 // TBD: 42 // FastLowerArguments: Handle simple cases. 43 // PPCMaterializeGV: Handle TLS. 44 // SelectCall: Handle function pointers. 45 // SelectCall: Handle multi-register return values. 46 // SelectCall: Optimize away nops for local calls. 47 // processCallArgs: Handle bit-converted arguments. 48 // finishCall: Handle multi-register return values. 49 // PPCComputeAddress: Handle parameter references as FrameIndex's. 50 // PPCEmitCmp: Handle immediate as operand 1. 51 // SelectCall: Handle small byval arguments. 52 // SelectIntrinsicCall: Implement. 53 // SelectSelect: Implement. 54 // Consider factoring isTypeLegal into the base class. 55 // Implement switches and jump tables. 56 // 57 //===----------------------------------------------------------------------===// 58 using namespace llvm; 59 60 #define DEBUG_TYPE "ppcfastisel" 61 62 namespace { 63 64 typedef struct Address { 65 enum { 66 RegBase, 67 FrameIndexBase 68 } BaseType; 69 70 union { 71 unsigned Reg; 72 int FI; 73 } Base; 74 75 long Offset; 76 77 // Innocuous defaults for our address. 78 Address() 79 : BaseType(RegBase), Offset(0) { 80 Base.Reg = 0; 81 } 82 } Address; 83 84 class PPCFastISel final : public FastISel { 85 86 const TargetMachine &TM; 87 const TargetInstrInfo &TII; 88 const TargetLowering &TLI; 89 const PPCSubtarget *PPCSubTarget; 90 LLVMContext *Context; 91 92 public: 93 explicit PPCFastISel(FunctionLoweringInfo &FuncInfo, 94 const TargetLibraryInfo *LibInfo) 95 : FastISel(FuncInfo, LibInfo), 96 TM(FuncInfo.MF->getTarget()), 97 TII(*TM.getInstrInfo()), 98 TLI(*TM.getTargetLowering()), 99 PPCSubTarget(&TM.getSubtarget<PPCSubtarget>()), 100 Context(&FuncInfo.Fn->getContext()) { } 101 102 // Backend specific FastISel code. 103 private: 104 bool TargetSelectInstruction(const Instruction *I) override; 105 unsigned TargetMaterializeConstant(const Constant *C) override; 106 unsigned TargetMaterializeAlloca(const AllocaInst *AI) override; 107 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo, 108 const LoadInst *LI) override; 109 bool FastLowerArguments() override; 110 unsigned FastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm) override; 111 unsigned FastEmitInst_ri(unsigned MachineInstOpcode, 112 const TargetRegisterClass *RC, 113 unsigned Op0, bool Op0IsKill, 114 uint64_t Imm); 115 unsigned FastEmitInst_r(unsigned MachineInstOpcode, 116 const TargetRegisterClass *RC, 117 unsigned Op0, bool Op0IsKill); 118 unsigned FastEmitInst_rr(unsigned MachineInstOpcode, 119 const TargetRegisterClass *RC, 120 unsigned Op0, bool Op0IsKill, 121 unsigned Op1, bool Op1IsKill); 122 123 // Instruction selection routines. 124 private: 125 bool SelectLoad(const Instruction *I); 126 bool SelectStore(const Instruction *I); 127 bool SelectBranch(const Instruction *I); 128 bool SelectIndirectBr(const Instruction *I); 129 bool SelectFPExt(const Instruction *I); 130 bool SelectFPTrunc(const Instruction *I); 131 bool SelectIToFP(const Instruction *I, bool IsSigned); 132 bool SelectFPToI(const Instruction *I, bool IsSigned); 133 bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode); 134 bool SelectCall(const Instruction *I); 135 bool SelectRet(const Instruction *I); 136 bool SelectTrunc(const Instruction *I); 137 bool SelectIntExt(const Instruction *I); 138 139 // Utility routines. 140 private: 141 bool isTypeLegal(Type *Ty, MVT &VT); 142 bool isLoadTypeLegal(Type *Ty, MVT &VT); 143 bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value, 144 bool isZExt, unsigned DestReg); 145 bool PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr, 146 const TargetRegisterClass *RC, bool IsZExt = true, 147 unsigned FP64LoadOpc = PPC::LFD); 148 bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr); 149 bool PPCComputeAddress(const Value *Obj, Address &Addr); 150 void PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset, 151 unsigned &IndexReg); 152 bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, 153 unsigned DestReg, bool IsZExt); 154 unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT); 155 unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT); 156 unsigned PPCMaterializeInt(const Constant *C, MVT VT); 157 unsigned PPCMaterialize32BitInt(int64_t Imm, 158 const TargetRegisterClass *RC); 159 unsigned PPCMaterialize64BitInt(int64_t Imm, 160 const TargetRegisterClass *RC); 161 unsigned PPCMoveToIntReg(const Instruction *I, MVT VT, 162 unsigned SrcReg, bool IsSigned); 163 unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned); 164 165 // Call handling routines. 166 private: 167 bool processCallArgs(SmallVectorImpl<Value*> &Args, 168 SmallVectorImpl<unsigned> &ArgRegs, 169 SmallVectorImpl<MVT> &ArgVTs, 170 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags, 171 SmallVectorImpl<unsigned> &RegArgs, 172 CallingConv::ID CC, 173 unsigned &NumBytes, 174 bool IsVarArg); 175 void finishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs, 176 const Instruction *I, CallingConv::ID CC, 177 unsigned &NumBytes, bool IsVarArg); 178 CCAssignFn *usePPC32CCs(unsigned Flag); 179 180 private: 181 #include "PPCGenFastISel.inc" 182 183 }; 184 185 } // end anonymous namespace 186 187 #include "PPCGenCallingConv.inc" 188 189 // Function whose sole purpose is to kill compiler warnings 190 // stemming from unused functions included from PPCGenCallingConv.inc. 191 CCAssignFn *PPCFastISel::usePPC32CCs(unsigned Flag) { 192 if (Flag == 1) 193 return CC_PPC32_SVR4; 194 else if (Flag == 2) 195 return CC_PPC32_SVR4_ByVal; 196 else if (Flag == 3) 197 return CC_PPC32_SVR4_VarArg; 198 else 199 return RetCC_PPC; 200 } 201 202 static Optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) { 203 switch (Pred) { 204 // These are not representable with any single compare. 205 case CmpInst::FCMP_FALSE: 206 case CmpInst::FCMP_UEQ: 207 case CmpInst::FCMP_UGT: 208 case CmpInst::FCMP_UGE: 209 case CmpInst::FCMP_ULT: 210 case CmpInst::FCMP_ULE: 211 case CmpInst::FCMP_UNE: 212 case CmpInst::FCMP_TRUE: 213 default: 214 return Optional<PPC::Predicate>(); 215 216 case CmpInst::FCMP_OEQ: 217 case CmpInst::ICMP_EQ: 218 return PPC::PRED_EQ; 219 220 case CmpInst::FCMP_OGT: 221 case CmpInst::ICMP_UGT: 222 case CmpInst::ICMP_SGT: 223 return PPC::PRED_GT; 224 225 case CmpInst::FCMP_OGE: 226 case CmpInst::ICMP_UGE: 227 case CmpInst::ICMP_SGE: 228 return PPC::PRED_GE; 229 230 case CmpInst::FCMP_OLT: 231 case CmpInst::ICMP_ULT: 232 case CmpInst::ICMP_SLT: 233 return PPC::PRED_LT; 234 235 case CmpInst::FCMP_OLE: 236 case CmpInst::ICMP_ULE: 237 case CmpInst::ICMP_SLE: 238 return PPC::PRED_LE; 239 240 case CmpInst::FCMP_ONE: 241 case CmpInst::ICMP_NE: 242 return PPC::PRED_NE; 243 244 case CmpInst::FCMP_ORD: 245 return PPC::PRED_NU; 246 247 case CmpInst::FCMP_UNO: 248 return PPC::PRED_UN; 249 } 250 } 251 252 // Determine whether the type Ty is simple enough to be handled by 253 // fast-isel, and return its equivalent machine type in VT. 254 // FIXME: Copied directly from ARM -- factor into base class? 255 bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) { 256 EVT Evt = TLI.getValueType(Ty, true); 257 258 // Only handle simple types. 259 if (Evt == MVT::Other || !Evt.isSimple()) return false; 260 VT = Evt.getSimpleVT(); 261 262 // Handle all legal types, i.e. a register that will directly hold this 263 // value. 264 return TLI.isTypeLegal(VT); 265 } 266 267 // Determine whether the type Ty is simple enough to be handled by 268 // fast-isel as a load target, and return its equivalent machine type in VT. 269 bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) { 270 if (isTypeLegal(Ty, VT)) return true; 271 272 // If this is a type than can be sign or zero-extended to a basic operation 273 // go ahead and accept it now. 274 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) { 275 return true; 276 } 277 278 return false; 279 } 280 281 // Given a value Obj, create an Address object Addr that represents its 282 // address. Return false if we can't handle it. 283 bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) { 284 const User *U = nullptr; 285 unsigned Opcode = Instruction::UserOp1; 286 if (const Instruction *I = dyn_cast<Instruction>(Obj)) { 287 // Don't walk into other basic blocks unless the object is an alloca from 288 // another block, otherwise it may not have a virtual register assigned. 289 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) || 290 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) { 291 Opcode = I->getOpcode(); 292 U = I; 293 } 294 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) { 295 Opcode = C->getOpcode(); 296 U = C; 297 } 298 299 switch (Opcode) { 300 default: 301 break; 302 case Instruction::BitCast: 303 // Look through bitcasts. 304 return PPCComputeAddress(U->getOperand(0), Addr); 305 case Instruction::IntToPtr: 306 // Look past no-op inttoptrs. 307 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy()) 308 return PPCComputeAddress(U->getOperand(0), Addr); 309 break; 310 case Instruction::PtrToInt: 311 // Look past no-op ptrtoints. 312 if (TLI.getValueType(U->getType()) == TLI.getPointerTy()) 313 return PPCComputeAddress(U->getOperand(0), Addr); 314 break; 315 case Instruction::GetElementPtr: { 316 Address SavedAddr = Addr; 317 long TmpOffset = Addr.Offset; 318 319 // Iterate through the GEP folding the constants into offsets where 320 // we can. 321 gep_type_iterator GTI = gep_type_begin(U); 322 for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end(); 323 II != IE; ++II, ++GTI) { 324 const Value *Op = *II; 325 if (StructType *STy = dyn_cast<StructType>(*GTI)) { 326 const StructLayout *SL = DL.getStructLayout(STy); 327 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue(); 328 TmpOffset += SL->getElementOffset(Idx); 329 } else { 330 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType()); 331 for (;;) { 332 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) { 333 // Constant-offset addressing. 334 TmpOffset += CI->getSExtValue() * S; 335 break; 336 } 337 if (canFoldAddIntoGEP(U, Op)) { 338 // A compatible add with a constant operand. Fold the constant. 339 ConstantInt *CI = 340 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1)); 341 TmpOffset += CI->getSExtValue() * S; 342 // Iterate on the other operand. 343 Op = cast<AddOperator>(Op)->getOperand(0); 344 continue; 345 } 346 // Unsupported 347 goto unsupported_gep; 348 } 349 } 350 } 351 352 // Try to grab the base operand now. 353 Addr.Offset = TmpOffset; 354 if (PPCComputeAddress(U->getOperand(0), Addr)) return true; 355 356 // We failed, restore everything and try the other options. 357 Addr = SavedAddr; 358 359 unsupported_gep: 360 break; 361 } 362 case Instruction::Alloca: { 363 const AllocaInst *AI = cast<AllocaInst>(Obj); 364 DenseMap<const AllocaInst*, int>::iterator SI = 365 FuncInfo.StaticAllocaMap.find(AI); 366 if (SI != FuncInfo.StaticAllocaMap.end()) { 367 Addr.BaseType = Address::FrameIndexBase; 368 Addr.Base.FI = SI->second; 369 return true; 370 } 371 break; 372 } 373 } 374 375 // FIXME: References to parameters fall through to the behavior 376 // below. They should be able to reference a frame index since 377 // they are stored to the stack, so we can get "ld rx, offset(r1)" 378 // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will 379 // just contain the parameter. Try to handle this with a FI. 380 381 // Try to get this in a register if nothing else has worked. 382 if (Addr.Base.Reg == 0) 383 Addr.Base.Reg = getRegForValue(Obj); 384 385 // Prevent assignment of base register to X0, which is inappropriate 386 // for loads and stores alike. 387 if (Addr.Base.Reg != 0) 388 MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass); 389 390 return Addr.Base.Reg != 0; 391 } 392 393 // Fix up some addresses that can't be used directly. For example, if 394 // an offset won't fit in an instruction field, we may need to move it 395 // into an index register. 396 void PPCFastISel::PPCSimplifyAddress(Address &Addr, MVT VT, bool &UseOffset, 397 unsigned &IndexReg) { 398 399 // Check whether the offset fits in the instruction field. 400 if (!isInt<16>(Addr.Offset)) 401 UseOffset = false; 402 403 // If this is a stack pointer and the offset needs to be simplified then 404 // put the alloca address into a register, set the base type back to 405 // register and continue. This should almost never happen. 406 if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) { 407 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass); 408 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8), 409 ResultReg).addFrameIndex(Addr.Base.FI).addImm(0); 410 Addr.Base.Reg = ResultReg; 411 Addr.BaseType = Address::RegBase; 412 } 413 414 if (!UseOffset) { 415 IntegerType *OffsetTy = ((VT == MVT::i32) ? Type::getInt32Ty(*Context) 416 : Type::getInt64Ty(*Context)); 417 const ConstantInt *Offset = 418 ConstantInt::getSigned(OffsetTy, (int64_t)(Addr.Offset)); 419 IndexReg = PPCMaterializeInt(Offset, MVT::i64); 420 assert(IndexReg && "Unexpected error in PPCMaterializeInt!"); 421 } 422 } 423 424 // Emit a load instruction if possible, returning true if we succeeded, 425 // otherwise false. See commentary below for how the register class of 426 // the load is determined. 427 bool PPCFastISel::PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr, 428 const TargetRegisterClass *RC, 429 bool IsZExt, unsigned FP64LoadOpc) { 430 unsigned Opc; 431 bool UseOffset = true; 432 433 // If ResultReg is given, it determines the register class of the load. 434 // Otherwise, RC is the register class to use. If the result of the 435 // load isn't anticipated in this block, both may be zero, in which 436 // case we must make a conservative guess. In particular, don't assign 437 // R0 or X0 to the result register, as the result may be used in a load, 438 // store, add-immediate, or isel that won't permit this. (Though 439 // perhaps the spill and reload of live-exit values would handle this?) 440 const TargetRegisterClass *UseRC = 441 (ResultReg ? MRI.getRegClass(ResultReg) : 442 (RC ? RC : 443 (VT == MVT::f64 ? &PPC::F8RCRegClass : 444 (VT == MVT::f32 ? &PPC::F4RCRegClass : 445 (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass : 446 &PPC::GPRC_and_GPRC_NOR0RegClass))))); 447 448 bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass); 449 450 switch (VT.SimpleTy) { 451 default: // e.g., vector types not handled 452 return false; 453 case MVT::i8: 454 Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8; 455 break; 456 case MVT::i16: 457 Opc = (IsZExt ? 458 (Is32BitInt ? PPC::LHZ : PPC::LHZ8) : 459 (Is32BitInt ? PPC::LHA : PPC::LHA8)); 460 break; 461 case MVT::i32: 462 Opc = (IsZExt ? 463 (Is32BitInt ? PPC::LWZ : PPC::LWZ8) : 464 (Is32BitInt ? PPC::LWA_32 : PPC::LWA)); 465 if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0)) 466 UseOffset = false; 467 break; 468 case MVT::i64: 469 Opc = PPC::LD; 470 assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) && 471 "64-bit load with 32-bit target??"); 472 UseOffset = ((Addr.Offset & 3) == 0); 473 break; 474 case MVT::f32: 475 Opc = PPC::LFS; 476 break; 477 case MVT::f64: 478 Opc = FP64LoadOpc; 479 break; 480 } 481 482 // If necessary, materialize the offset into a register and use 483 // the indexed form. Also handle stack pointers with special needs. 484 unsigned IndexReg = 0; 485 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg); 486 if (ResultReg == 0) 487 ResultReg = createResultReg(UseRC); 488 489 // Note: If we still have a frame index here, we know the offset is 490 // in range, as otherwise PPCSimplifyAddress would have converted it 491 // into a RegBase. 492 if (Addr.BaseType == Address::FrameIndexBase) { 493 494 MachineMemOperand *MMO = 495 FuncInfo.MF->getMachineMemOperand( 496 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset), 497 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI), 498 MFI.getObjectAlignment(Addr.Base.FI)); 499 500 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) 501 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO); 502 503 // Base reg with offset in range. 504 } else if (UseOffset) { 505 506 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) 507 .addImm(Addr.Offset).addReg(Addr.Base.Reg); 508 509 // Indexed form. 510 } else { 511 // Get the RR opcode corresponding to the RI one. FIXME: It would be 512 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it 513 // is hard to get at. 514 switch (Opc) { 515 default: llvm_unreachable("Unexpected opcode!"); 516 case PPC::LBZ: Opc = PPC::LBZX; break; 517 case PPC::LBZ8: Opc = PPC::LBZX8; break; 518 case PPC::LHZ: Opc = PPC::LHZX; break; 519 case PPC::LHZ8: Opc = PPC::LHZX8; break; 520 case PPC::LHA: Opc = PPC::LHAX; break; 521 case PPC::LHA8: Opc = PPC::LHAX8; break; 522 case PPC::LWZ: Opc = PPC::LWZX; break; 523 case PPC::LWZ8: Opc = PPC::LWZX8; break; 524 case PPC::LWA: Opc = PPC::LWAX; break; 525 case PPC::LWA_32: Opc = PPC::LWAX_32; break; 526 case PPC::LD: Opc = PPC::LDX; break; 527 case PPC::LFS: Opc = PPC::LFSX; break; 528 case PPC::LFD: Opc = PPC::LFDX; break; 529 } 530 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) 531 .addReg(Addr.Base.Reg).addReg(IndexReg); 532 } 533 534 return true; 535 } 536 537 // Attempt to fast-select a load instruction. 538 bool PPCFastISel::SelectLoad(const Instruction *I) { 539 // FIXME: No atomic loads are supported. 540 if (cast<LoadInst>(I)->isAtomic()) 541 return false; 542 543 // Verify we have a legal type before going any further. 544 MVT VT; 545 if (!isLoadTypeLegal(I->getType(), VT)) 546 return false; 547 548 // See if we can handle this address. 549 Address Addr; 550 if (!PPCComputeAddress(I->getOperand(0), Addr)) 551 return false; 552 553 // Look at the currently assigned register for this instruction 554 // to determine the required register class. This is necessary 555 // to constrain RA from using R0/X0 when this is not legal. 556 unsigned AssignedReg = FuncInfo.ValueMap[I]; 557 const TargetRegisterClass *RC = 558 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr; 559 560 unsigned ResultReg = 0; 561 if (!PPCEmitLoad(VT, ResultReg, Addr, RC)) 562 return false; 563 UpdateValueMap(I, ResultReg); 564 return true; 565 } 566 567 // Emit a store instruction to store SrcReg at Addr. 568 bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) { 569 assert(SrcReg && "Nothing to store!"); 570 unsigned Opc; 571 bool UseOffset = true; 572 573 const TargetRegisterClass *RC = MRI.getRegClass(SrcReg); 574 bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass); 575 576 switch (VT.SimpleTy) { 577 default: // e.g., vector types not handled 578 return false; 579 case MVT::i8: 580 Opc = Is32BitInt ? PPC::STB : PPC::STB8; 581 break; 582 case MVT::i16: 583 Opc = Is32BitInt ? PPC::STH : PPC::STH8; 584 break; 585 case MVT::i32: 586 assert(Is32BitInt && "Not GPRC for i32??"); 587 Opc = PPC::STW; 588 break; 589 case MVT::i64: 590 Opc = PPC::STD; 591 UseOffset = ((Addr.Offset & 3) == 0); 592 break; 593 case MVT::f32: 594 Opc = PPC::STFS; 595 break; 596 case MVT::f64: 597 Opc = PPC::STFD; 598 break; 599 } 600 601 // If necessary, materialize the offset into a register and use 602 // the indexed form. Also handle stack pointers with special needs. 603 unsigned IndexReg = 0; 604 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg); 605 606 // Note: If we still have a frame index here, we know the offset is 607 // in range, as otherwise PPCSimplifyAddress would have converted it 608 // into a RegBase. 609 if (Addr.BaseType == Address::FrameIndexBase) { 610 MachineMemOperand *MMO = 611 FuncInfo.MF->getMachineMemOperand( 612 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset), 613 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI), 614 MFI.getObjectAlignment(Addr.Base.FI)); 615 616 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc)) 617 .addReg(SrcReg) 618 .addImm(Addr.Offset) 619 .addFrameIndex(Addr.Base.FI) 620 .addMemOperand(MMO); 621 622 // Base reg with offset in range. 623 } else if (UseOffset) 624 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc)) 625 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg); 626 627 // Indexed form. 628 else { 629 // Get the RR opcode corresponding to the RI one. FIXME: It would be 630 // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it 631 // is hard to get at. 632 switch (Opc) { 633 default: llvm_unreachable("Unexpected opcode!"); 634 case PPC::STB: Opc = PPC::STBX; break; 635 case PPC::STH : Opc = PPC::STHX; break; 636 case PPC::STW : Opc = PPC::STWX; break; 637 case PPC::STB8: Opc = PPC::STBX8; break; 638 case PPC::STH8: Opc = PPC::STHX8; break; 639 case PPC::STW8: Opc = PPC::STWX8; break; 640 case PPC::STD: Opc = PPC::STDX; break; 641 case PPC::STFS: Opc = PPC::STFSX; break; 642 case PPC::STFD: Opc = PPC::STFDX; break; 643 } 644 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc)) 645 .addReg(SrcReg).addReg(Addr.Base.Reg).addReg(IndexReg); 646 } 647 648 return true; 649 } 650 651 // Attempt to fast-select a store instruction. 652 bool PPCFastISel::SelectStore(const Instruction *I) { 653 Value *Op0 = I->getOperand(0); 654 unsigned SrcReg = 0; 655 656 // FIXME: No atomics loads are supported. 657 if (cast<StoreInst>(I)->isAtomic()) 658 return false; 659 660 // Verify we have a legal type before going any further. 661 MVT VT; 662 if (!isLoadTypeLegal(Op0->getType(), VT)) 663 return false; 664 665 // Get the value to be stored into a register. 666 SrcReg = getRegForValue(Op0); 667 if (SrcReg == 0) 668 return false; 669 670 // See if we can handle this address. 671 Address Addr; 672 if (!PPCComputeAddress(I->getOperand(1), Addr)) 673 return false; 674 675 if (!PPCEmitStore(VT, SrcReg, Addr)) 676 return false; 677 678 return true; 679 } 680 681 // Attempt to fast-select a branch instruction. 682 bool PPCFastISel::SelectBranch(const Instruction *I) { 683 const BranchInst *BI = cast<BranchInst>(I); 684 MachineBasicBlock *BrBB = FuncInfo.MBB; 685 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)]; 686 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)]; 687 688 // For now, just try the simplest case where it's fed by a compare. 689 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) { 690 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate()); 691 if (!OptPPCPred) 692 return false; 693 694 PPC::Predicate PPCPred = OptPPCPred.getValue(); 695 696 // Take advantage of fall-through opportunities. 697 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) { 698 std::swap(TBB, FBB); 699 PPCPred = PPC::InvertPredicate(PPCPred); 700 } 701 702 unsigned CondReg = createResultReg(&PPC::CRRCRegClass); 703 704 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(), 705 CondReg)) 706 return false; 707 708 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC)) 709 .addImm(PPCPred).addReg(CondReg).addMBB(TBB); 710 FastEmitBranch(FBB, DbgLoc); 711 FuncInfo.MBB->addSuccessor(TBB); 712 return true; 713 714 } else if (const ConstantInt *CI = 715 dyn_cast<ConstantInt>(BI->getCondition())) { 716 uint64_t Imm = CI->getZExtValue(); 717 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB; 718 FastEmitBranch(Target, DbgLoc); 719 return true; 720 } 721 722 // FIXME: ARM looks for a case where the block containing the compare 723 // has been split from the block containing the branch. If this happens, 724 // there is a vreg available containing the result of the compare. I'm 725 // not sure we can do much, as we've lost the predicate information with 726 // the compare instruction -- we have a 4-bit CR but don't know which bit 727 // to test here. 728 return false; 729 } 730 731 // Attempt to emit a compare of the two source values. Signed and unsigned 732 // comparisons are supported. Return false if we can't handle it. 733 bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2, 734 bool IsZExt, unsigned DestReg) { 735 Type *Ty = SrcValue1->getType(); 736 EVT SrcEVT = TLI.getValueType(Ty, true); 737 if (!SrcEVT.isSimple()) 738 return false; 739 MVT SrcVT = SrcEVT.getSimpleVT(); 740 741 if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits()) 742 return false; 743 744 // See if operand 2 is an immediate encodeable in the compare. 745 // FIXME: Operands are not in canonical order at -O0, so an immediate 746 // operand in position 1 is a lost opportunity for now. We are 747 // similar to ARM in this regard. 748 long Imm = 0; 749 bool UseImm = false; 750 751 // Only 16-bit integer constants can be represented in compares for 752 // PowerPC. Others will be materialized into a register. 753 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) { 754 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 || 755 SrcVT == MVT::i8 || SrcVT == MVT::i1) { 756 const APInt &CIVal = ConstInt->getValue(); 757 Imm = (IsZExt) ? (long)CIVal.getZExtValue() : (long)CIVal.getSExtValue(); 758 if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm))) 759 UseImm = true; 760 } 761 } 762 763 unsigned CmpOpc; 764 bool NeedsExt = false; 765 switch (SrcVT.SimpleTy) { 766 default: return false; 767 case MVT::f32: 768 CmpOpc = PPC::FCMPUS; 769 break; 770 case MVT::f64: 771 CmpOpc = PPC::FCMPUD; 772 break; 773 case MVT::i1: 774 case MVT::i8: 775 case MVT::i16: 776 NeedsExt = true; 777 // Intentional fall-through. 778 case MVT::i32: 779 if (!UseImm) 780 CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW; 781 else 782 CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI; 783 break; 784 case MVT::i64: 785 if (!UseImm) 786 CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD; 787 else 788 CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI; 789 break; 790 } 791 792 unsigned SrcReg1 = getRegForValue(SrcValue1); 793 if (SrcReg1 == 0) 794 return false; 795 796 unsigned SrcReg2 = 0; 797 if (!UseImm) { 798 SrcReg2 = getRegForValue(SrcValue2); 799 if (SrcReg2 == 0) 800 return false; 801 } 802 803 if (NeedsExt) { 804 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass); 805 if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt)) 806 return false; 807 SrcReg1 = ExtReg; 808 809 if (!UseImm) { 810 unsigned ExtReg = createResultReg(&PPC::GPRCRegClass); 811 if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt)) 812 return false; 813 SrcReg2 = ExtReg; 814 } 815 } 816 817 if (!UseImm) 818 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg) 819 .addReg(SrcReg1).addReg(SrcReg2); 820 else 821 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc), DestReg) 822 .addReg(SrcReg1).addImm(Imm); 823 824 return true; 825 } 826 827 // Attempt to fast-select a floating-point extend instruction. 828 bool PPCFastISel::SelectFPExt(const Instruction *I) { 829 Value *Src = I->getOperand(0); 830 EVT SrcVT = TLI.getValueType(Src->getType(), true); 831 EVT DestVT = TLI.getValueType(I->getType(), true); 832 833 if (SrcVT != MVT::f32 || DestVT != MVT::f64) 834 return false; 835 836 unsigned SrcReg = getRegForValue(Src); 837 if (!SrcReg) 838 return false; 839 840 // No code is generated for a FP extend. 841 UpdateValueMap(I, SrcReg); 842 return true; 843 } 844 845 // Attempt to fast-select a floating-point truncate instruction. 846 bool PPCFastISel::SelectFPTrunc(const Instruction *I) { 847 Value *Src = I->getOperand(0); 848 EVT SrcVT = TLI.getValueType(Src->getType(), true); 849 EVT DestVT = TLI.getValueType(I->getType(), true); 850 851 if (SrcVT != MVT::f64 || DestVT != MVT::f32) 852 return false; 853 854 unsigned SrcReg = getRegForValue(Src); 855 if (!SrcReg) 856 return false; 857 858 // Round the result to single precision. 859 unsigned DestReg = createResultReg(&PPC::F4RCRegClass); 860 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP), DestReg) 861 .addReg(SrcReg); 862 863 UpdateValueMap(I, DestReg); 864 return true; 865 } 866 867 // Move an i32 or i64 value in a GPR to an f64 value in an FPR. 868 // FIXME: When direct register moves are implemented (see PowerISA 2.08), 869 // those should be used instead of moving via a stack slot when the 870 // subtarget permits. 871 // FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte 872 // stack slot and 4-byte store/load sequence. Or just sext the 4-byte 873 // case to 8 bytes which produces tighter code but wastes stack space. 874 unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg, 875 bool IsSigned) { 876 877 // If necessary, extend 32-bit int to 64-bit. 878 if (SrcVT == MVT::i32) { 879 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass); 880 if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned)) 881 return 0; 882 SrcReg = TmpReg; 883 } 884 885 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary. 886 Address Addr; 887 Addr.BaseType = Address::FrameIndexBase; 888 Addr.Base.FI = MFI.CreateStackObject(8, 8, false); 889 890 // Store the value from the GPR. 891 if (!PPCEmitStore(MVT::i64, SrcReg, Addr)) 892 return 0; 893 894 // Load the integer value into an FPR. The kind of load used depends 895 // on a number of conditions. 896 unsigned LoadOpc = PPC::LFD; 897 898 if (SrcVT == MVT::i32) { 899 if (!IsSigned) { 900 LoadOpc = PPC::LFIWZX; 901 Addr.Offset = 4; 902 } else if (PPCSubTarget->hasLFIWAX()) { 903 LoadOpc = PPC::LFIWAX; 904 Addr.Offset = 4; 905 } 906 } 907 908 const TargetRegisterClass *RC = &PPC::F8RCRegClass; 909 unsigned ResultReg = 0; 910 if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc)) 911 return 0; 912 913 return ResultReg; 914 } 915 916 // Attempt to fast-select an integer-to-floating-point conversion. 917 bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) { 918 MVT DstVT; 919 Type *DstTy = I->getType(); 920 if (!isTypeLegal(DstTy, DstVT)) 921 return false; 922 923 if (DstVT != MVT::f32 && DstVT != MVT::f64) 924 return false; 925 926 Value *Src = I->getOperand(0); 927 EVT SrcEVT = TLI.getValueType(Src->getType(), true); 928 if (!SrcEVT.isSimple()) 929 return false; 930 931 MVT SrcVT = SrcEVT.getSimpleVT(); 932 933 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && 934 SrcVT != MVT::i32 && SrcVT != MVT::i64) 935 return false; 936 937 unsigned SrcReg = getRegForValue(Src); 938 if (SrcReg == 0) 939 return false; 940 941 // We can only lower an unsigned convert if we have the newer 942 // floating-point conversion operations. 943 if (!IsSigned && !PPCSubTarget->hasFPCVT()) 944 return false; 945 946 // FIXME: For now we require the newer floating-point conversion operations 947 // (which are present only on P7 and A2 server models) when converting 948 // to single-precision float. Otherwise we have to generate a lot of 949 // fiddly code to avoid double rounding. If necessary, the fiddly code 950 // can be found in PPCTargetLowering::LowerINT_TO_FP(). 951 if (DstVT == MVT::f32 && !PPCSubTarget->hasFPCVT()) 952 return false; 953 954 // Extend the input if necessary. 955 if (SrcVT == MVT::i8 || SrcVT == MVT::i16) { 956 unsigned TmpReg = createResultReg(&PPC::G8RCRegClass); 957 if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned)) 958 return false; 959 SrcVT = MVT::i64; 960 SrcReg = TmpReg; 961 } 962 963 // Move the integer value to an FPR. 964 unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned); 965 if (FPReg == 0) 966 return false; 967 968 // Determine the opcode for the conversion. 969 const TargetRegisterClass *RC = &PPC::F8RCRegClass; 970 unsigned DestReg = createResultReg(RC); 971 unsigned Opc; 972 973 if (DstVT == MVT::f32) 974 Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS; 975 else 976 Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU; 977 978 // Generate the convert. 979 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg) 980 .addReg(FPReg); 981 982 UpdateValueMap(I, DestReg); 983 return true; 984 } 985 986 // Move the floating-point value in SrcReg into an integer destination 987 // register, and return the register (or zero if we can't handle it). 988 // FIXME: When direct register moves are implemented (see PowerISA 2.08), 989 // those should be used instead of moving via a stack slot when the 990 // subtarget permits. 991 unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT, 992 unsigned SrcReg, bool IsSigned) { 993 // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary. 994 // Note that if have STFIWX available, we could use a 4-byte stack 995 // slot for i32, but this being fast-isel we'll just go with the 996 // easiest code gen possible. 997 Address Addr; 998 Addr.BaseType = Address::FrameIndexBase; 999 Addr.Base.FI = MFI.CreateStackObject(8, 8, false); 1000 1001 // Store the value from the FPR. 1002 if (!PPCEmitStore(MVT::f64, SrcReg, Addr)) 1003 return 0; 1004 1005 // Reload it into a GPR. If we want an i32, modify the address 1006 // to have a 4-byte offset so we load from the right place. 1007 if (VT == MVT::i32) 1008 Addr.Offset = 4; 1009 1010 // Look at the currently assigned register for this instruction 1011 // to determine the required register class. 1012 unsigned AssignedReg = FuncInfo.ValueMap[I]; 1013 const TargetRegisterClass *RC = 1014 AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr; 1015 1016 unsigned ResultReg = 0; 1017 if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned)) 1018 return 0; 1019 1020 return ResultReg; 1021 } 1022 1023 // Attempt to fast-select a floating-point-to-integer conversion. 1024 bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) { 1025 MVT DstVT, SrcVT; 1026 Type *DstTy = I->getType(); 1027 if (!isTypeLegal(DstTy, DstVT)) 1028 return false; 1029 1030 if (DstVT != MVT::i32 && DstVT != MVT::i64) 1031 return false; 1032 1033 Value *Src = I->getOperand(0); 1034 Type *SrcTy = Src->getType(); 1035 if (!isTypeLegal(SrcTy, SrcVT)) 1036 return false; 1037 1038 if (SrcVT != MVT::f32 && SrcVT != MVT::f64) 1039 return false; 1040 1041 unsigned SrcReg = getRegForValue(Src); 1042 if (SrcReg == 0) 1043 return false; 1044 1045 // Convert f32 to f64 if necessary. This is just a meaningless copy 1046 // to get the register class right. COPY_TO_REGCLASS is needed since 1047 // a COPY from F4RC to F8RC is converted to a F4RC-F4RC copy downstream. 1048 const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg); 1049 if (InRC == &PPC::F4RCRegClass) { 1050 unsigned TmpReg = createResultReg(&PPC::F8RCRegClass); 1051 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1052 TII.get(TargetOpcode::COPY_TO_REGCLASS), TmpReg) 1053 .addReg(SrcReg).addImm(PPC::F8RCRegClassID); 1054 SrcReg = TmpReg; 1055 } 1056 1057 // Determine the opcode for the conversion, which takes place 1058 // entirely within FPRs. 1059 unsigned DestReg = createResultReg(&PPC::F8RCRegClass); 1060 unsigned Opc; 1061 1062 if (DstVT == MVT::i32) 1063 if (IsSigned) 1064 Opc = PPC::FCTIWZ; 1065 else 1066 Opc = PPCSubTarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ; 1067 else 1068 Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ; 1069 1070 // Generate the convert. 1071 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg) 1072 .addReg(SrcReg); 1073 1074 // Now move the integer value from a float register to an integer register. 1075 unsigned IntReg = PPCMoveToIntReg(I, DstVT, DestReg, IsSigned); 1076 if (IntReg == 0) 1077 return false; 1078 1079 UpdateValueMap(I, IntReg); 1080 return true; 1081 } 1082 1083 // Attempt to fast-select a binary integer operation that isn't already 1084 // handled automatically. 1085 bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) { 1086 EVT DestVT = TLI.getValueType(I->getType(), true); 1087 1088 // We can get here in the case when we have a binary operation on a non-legal 1089 // type and the target independent selector doesn't know how to handle it. 1090 if (DestVT != MVT::i16 && DestVT != MVT::i8) 1091 return false; 1092 1093 // Look at the currently assigned register for this instruction 1094 // to determine the required register class. If there is no register, 1095 // make a conservative choice (don't assign R0). 1096 unsigned AssignedReg = FuncInfo.ValueMap[I]; 1097 const TargetRegisterClass *RC = 1098 (AssignedReg ? MRI.getRegClass(AssignedReg) : 1099 &PPC::GPRC_and_GPRC_NOR0RegClass); 1100 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass); 1101 1102 unsigned Opc; 1103 switch (ISDOpcode) { 1104 default: return false; 1105 case ISD::ADD: 1106 Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8; 1107 break; 1108 case ISD::OR: 1109 Opc = IsGPRC ? PPC::OR : PPC::OR8; 1110 break; 1111 case ISD::SUB: 1112 Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8; 1113 break; 1114 } 1115 1116 unsigned ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass); 1117 unsigned SrcReg1 = getRegForValue(I->getOperand(0)); 1118 if (SrcReg1 == 0) return false; 1119 1120 // Handle case of small immediate operand. 1121 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) { 1122 const APInt &CIVal = ConstInt->getValue(); 1123 int Imm = (int)CIVal.getSExtValue(); 1124 bool UseImm = true; 1125 if (isInt<16>(Imm)) { 1126 switch (Opc) { 1127 default: 1128 llvm_unreachable("Missing case!"); 1129 case PPC::ADD4: 1130 Opc = PPC::ADDI; 1131 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass); 1132 break; 1133 case PPC::ADD8: 1134 Opc = PPC::ADDI8; 1135 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass); 1136 break; 1137 case PPC::OR: 1138 Opc = PPC::ORI; 1139 break; 1140 case PPC::OR8: 1141 Opc = PPC::ORI8; 1142 break; 1143 case PPC::SUBF: 1144 if (Imm == -32768) 1145 UseImm = false; 1146 else { 1147 Opc = PPC::ADDI; 1148 MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass); 1149 Imm = -Imm; 1150 } 1151 break; 1152 case PPC::SUBF8: 1153 if (Imm == -32768) 1154 UseImm = false; 1155 else { 1156 Opc = PPC::ADDI8; 1157 MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass); 1158 Imm = -Imm; 1159 } 1160 break; 1161 } 1162 1163 if (UseImm) { 1164 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), 1165 ResultReg) 1166 .addReg(SrcReg1) 1167 .addImm(Imm); 1168 UpdateValueMap(I, ResultReg); 1169 return true; 1170 } 1171 } 1172 } 1173 1174 // Reg-reg case. 1175 unsigned SrcReg2 = getRegForValue(I->getOperand(1)); 1176 if (SrcReg2 == 0) return false; 1177 1178 // Reverse operands for subtract-from. 1179 if (ISDOpcode == ISD::SUB) 1180 std::swap(SrcReg1, SrcReg2); 1181 1182 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) 1183 .addReg(SrcReg1).addReg(SrcReg2); 1184 UpdateValueMap(I, ResultReg); 1185 return true; 1186 } 1187 1188 // Handle arguments to a call that we're attempting to fast-select. 1189 // Return false if the arguments are too complex for us at the moment. 1190 bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args, 1191 SmallVectorImpl<unsigned> &ArgRegs, 1192 SmallVectorImpl<MVT> &ArgVTs, 1193 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags, 1194 SmallVectorImpl<unsigned> &RegArgs, 1195 CallingConv::ID CC, 1196 unsigned &NumBytes, 1197 bool IsVarArg) { 1198 SmallVector<CCValAssign, 16> ArgLocs; 1199 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, TM, ArgLocs, *Context); 1200 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS); 1201 1202 // Bail out if we can't handle any of the arguments. 1203 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { 1204 CCValAssign &VA = ArgLocs[I]; 1205 MVT ArgVT = ArgVTs[VA.getValNo()]; 1206 1207 // Skip vector arguments for now, as well as long double and 1208 // uint128_t, and anything that isn't passed in a register. 1209 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 || 1210 !VA.isRegLoc() || VA.needsCustom()) 1211 return false; 1212 1213 // Skip bit-converted arguments for now. 1214 if (VA.getLocInfo() == CCValAssign::BCvt) 1215 return false; 1216 } 1217 1218 // Get a count of how many bytes are to be pushed onto the stack. 1219 NumBytes = CCInfo.getNextStackOffset(); 1220 1221 // Issue CALLSEQ_START. 1222 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1223 TII.get(TII.getCallFrameSetupOpcode())) 1224 .addImm(NumBytes); 1225 1226 // Prepare to assign register arguments. Every argument uses up a 1227 // GPR protocol register even if it's passed in a floating-point 1228 // register. 1229 unsigned NextGPR = PPC::X3; 1230 unsigned NextFPR = PPC::F1; 1231 1232 // Process arguments. 1233 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { 1234 CCValAssign &VA = ArgLocs[I]; 1235 unsigned Arg = ArgRegs[VA.getValNo()]; 1236 MVT ArgVT = ArgVTs[VA.getValNo()]; 1237 1238 // Handle argument promotion and bitcasts. 1239 switch (VA.getLocInfo()) { 1240 default: 1241 llvm_unreachable("Unknown loc info!"); 1242 case CCValAssign::Full: 1243 break; 1244 case CCValAssign::SExt: { 1245 MVT DestVT = VA.getLocVT(); 1246 const TargetRegisterClass *RC = 1247 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass; 1248 unsigned TmpReg = createResultReg(RC); 1249 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false)) 1250 llvm_unreachable("Failed to emit a sext!"); 1251 ArgVT = DestVT; 1252 Arg = TmpReg; 1253 break; 1254 } 1255 case CCValAssign::AExt: 1256 case CCValAssign::ZExt: { 1257 MVT DestVT = VA.getLocVT(); 1258 const TargetRegisterClass *RC = 1259 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass; 1260 unsigned TmpReg = createResultReg(RC); 1261 if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true)) 1262 llvm_unreachable("Failed to emit a zext!"); 1263 ArgVT = DestVT; 1264 Arg = TmpReg; 1265 break; 1266 } 1267 case CCValAssign::BCvt: { 1268 // FIXME: Not yet handled. 1269 llvm_unreachable("Should have bailed before getting here!"); 1270 break; 1271 } 1272 } 1273 1274 // Copy this argument to the appropriate register. 1275 unsigned ArgReg; 1276 if (ArgVT == MVT::f32 || ArgVT == MVT::f64) { 1277 ArgReg = NextFPR++; 1278 ++NextGPR; 1279 } else 1280 ArgReg = NextGPR++; 1281 1282 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1283 TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg); 1284 RegArgs.push_back(ArgReg); 1285 } 1286 1287 return true; 1288 } 1289 1290 // For a call that we've determined we can fast-select, finish the 1291 // call sequence and generate a copy to obtain the return value (if any). 1292 void PPCFastISel::finishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs, 1293 const Instruction *I, CallingConv::ID CC, 1294 unsigned &NumBytes, bool IsVarArg) { 1295 // Issue CallSEQ_END. 1296 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1297 TII.get(TII.getCallFrameDestroyOpcode())) 1298 .addImm(NumBytes).addImm(0); 1299 1300 // Next, generate a copy to obtain the return value. 1301 // FIXME: No multi-register return values yet, though I don't foresee 1302 // any real difficulties there. 1303 if (RetVT != MVT::isVoid) { 1304 SmallVector<CCValAssign, 16> RVLocs; 1305 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, TM, RVLocs, *Context); 1306 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS); 1307 CCValAssign &VA = RVLocs[0]; 1308 assert(RVLocs.size() == 1 && "No support for multi-reg return values!"); 1309 assert(VA.isRegLoc() && "Can only return in registers!"); 1310 1311 MVT DestVT = VA.getValVT(); 1312 MVT CopyVT = DestVT; 1313 1314 // Ints smaller than a register still arrive in a full 64-bit 1315 // register, so make sure we recognize this. 1316 if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) 1317 CopyVT = MVT::i64; 1318 1319 unsigned SourcePhysReg = VA.getLocReg(); 1320 unsigned ResultReg = 0; 1321 1322 if (RetVT == CopyVT) { 1323 const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT); 1324 ResultReg = createResultReg(CpyRC); 1325 1326 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1327 TII.get(TargetOpcode::COPY), ResultReg) 1328 .addReg(SourcePhysReg); 1329 1330 // If necessary, round the floating result to single precision. 1331 } else if (CopyVT == MVT::f64) { 1332 ResultReg = createResultReg(TLI.getRegClassFor(RetVT)); 1333 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::FRSP), 1334 ResultReg).addReg(SourcePhysReg); 1335 1336 // If only the low half of a general register is needed, generate 1337 // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be 1338 // used along the fast-isel path (not lowered), and downstream logic 1339 // also doesn't like a direct subreg copy on a physical reg.) 1340 } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) { 1341 ResultReg = createResultReg(&PPC::GPRCRegClass); 1342 // Convert physical register from G8RC to GPRC. 1343 SourcePhysReg -= PPC::X0 - PPC::R0; 1344 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1345 TII.get(TargetOpcode::COPY), ResultReg) 1346 .addReg(SourcePhysReg); 1347 } 1348 1349 assert(ResultReg && "ResultReg unset!"); 1350 UsedRegs.push_back(SourcePhysReg); 1351 UpdateValueMap(I, ResultReg); 1352 } 1353 } 1354 1355 // Attempt to fast-select a call instruction. 1356 bool PPCFastISel::SelectCall(const Instruction *I) { 1357 const CallInst *CI = cast<CallInst>(I); 1358 const Value *Callee = CI->getCalledValue(); 1359 1360 // Can't handle inline asm. 1361 if (isa<InlineAsm>(Callee)) 1362 return false; 1363 1364 // Allow SelectionDAG isel to handle tail calls. 1365 if (CI->isTailCall()) 1366 return false; 1367 1368 // Obtain calling convention. 1369 ImmutableCallSite CS(CI); 1370 CallingConv::ID CC = CS.getCallingConv(); 1371 1372 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); 1373 FunctionType *FTy = cast<FunctionType>(PT->getElementType()); 1374 bool IsVarArg = FTy->isVarArg(); 1375 1376 // Not ready for varargs yet. 1377 if (IsVarArg) 1378 return false; 1379 1380 // Handle simple calls for now, with legal return types and 1381 // those that can be extended. 1382 Type *RetTy = I->getType(); 1383 MVT RetVT; 1384 if (RetTy->isVoidTy()) 1385 RetVT = MVT::isVoid; 1386 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 && 1387 RetVT != MVT::i8) 1388 return false; 1389 1390 // FIXME: No multi-register return values yet. 1391 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 && 1392 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 && 1393 RetVT != MVT::f64) { 1394 SmallVector<CCValAssign, 16> RVLocs; 1395 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, TM, RVLocs, *Context); 1396 CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS); 1397 if (RVLocs.size() > 1) 1398 return false; 1399 } 1400 1401 // Bail early if more than 8 arguments, as we only currently 1402 // handle arguments passed in registers. 1403 unsigned NumArgs = CS.arg_size(); 1404 if (NumArgs > 8) 1405 return false; 1406 1407 // Set up the argument vectors. 1408 SmallVector<Value*, 8> Args; 1409 SmallVector<unsigned, 8> ArgRegs; 1410 SmallVector<MVT, 8> ArgVTs; 1411 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags; 1412 1413 Args.reserve(NumArgs); 1414 ArgRegs.reserve(NumArgs); 1415 ArgVTs.reserve(NumArgs); 1416 ArgFlags.reserve(NumArgs); 1417 1418 for (ImmutableCallSite::arg_iterator II = CS.arg_begin(), IE = CS.arg_end(); 1419 II != IE; ++II) { 1420 // FIXME: ARM does something for intrinsic calls here, check into that. 1421 1422 unsigned AttrIdx = II - CS.arg_begin() + 1; 1423 1424 // Only handle easy calls for now. It would be reasonably easy 1425 // to handle <= 8-byte structures passed ByVal in registers, but we 1426 // have to ensure they are right-justified in the register. 1427 if (CS.paramHasAttr(AttrIdx, Attribute::InReg) || 1428 CS.paramHasAttr(AttrIdx, Attribute::StructRet) || 1429 CS.paramHasAttr(AttrIdx, Attribute::Nest) || 1430 CS.paramHasAttr(AttrIdx, Attribute::ByVal)) 1431 return false; 1432 1433 ISD::ArgFlagsTy Flags; 1434 if (CS.paramHasAttr(AttrIdx, Attribute::SExt)) 1435 Flags.setSExt(); 1436 if (CS.paramHasAttr(AttrIdx, Attribute::ZExt)) 1437 Flags.setZExt(); 1438 1439 Type *ArgTy = (*II)->getType(); 1440 MVT ArgVT; 1441 if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8) 1442 return false; 1443 1444 if (ArgVT.isVector()) 1445 return false; 1446 1447 unsigned Arg = getRegForValue(*II); 1448 if (Arg == 0) 1449 return false; 1450 1451 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy); 1452 Flags.setOrigAlign(OriginalAlignment); 1453 1454 Args.push_back(*II); 1455 ArgRegs.push_back(Arg); 1456 ArgVTs.push_back(ArgVT); 1457 ArgFlags.push_back(Flags); 1458 } 1459 1460 // Process the arguments. 1461 SmallVector<unsigned, 8> RegArgs; 1462 unsigned NumBytes; 1463 1464 if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, 1465 RegArgs, CC, NumBytes, IsVarArg)) 1466 return false; 1467 1468 // FIXME: No handling for function pointers yet. This requires 1469 // implementing the function descriptor (OPD) setup. 1470 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee); 1471 if (!GV) 1472 return false; 1473 1474 // Build direct call with NOP for TOC restore. 1475 // FIXME: We can and should optimize away the NOP for local calls. 1476 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1477 TII.get(PPC::BL8_NOP)); 1478 // Add callee. 1479 MIB.addGlobalAddress(GV); 1480 1481 // Add implicit physical register uses to the call. 1482 for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II) 1483 MIB.addReg(RegArgs[II], RegState::Implicit); 1484 1485 // Add a register mask with the call-preserved registers. Proper 1486 // defs for return values will be added by setPhysRegsDeadExcept(). 1487 MIB.addRegMask(TRI.getCallPreservedMask(CC)); 1488 1489 // Finish off the call including any return values. 1490 SmallVector<unsigned, 4> UsedRegs; 1491 finishCall(RetVT, UsedRegs, I, CC, NumBytes, IsVarArg); 1492 1493 // Set all unused physregs defs as dead. 1494 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI); 1495 1496 return true; 1497 } 1498 1499 // Attempt to fast-select a return instruction. 1500 bool PPCFastISel::SelectRet(const Instruction *I) { 1501 1502 if (!FuncInfo.CanLowerReturn) 1503 return false; 1504 1505 const ReturnInst *Ret = cast<ReturnInst>(I); 1506 const Function &F = *I->getParent()->getParent(); 1507 1508 // Build a list of return value registers. 1509 SmallVector<unsigned, 4> RetRegs; 1510 CallingConv::ID CC = F.getCallingConv(); 1511 1512 if (Ret->getNumOperands() > 0) { 1513 SmallVector<ISD::OutputArg, 4> Outs; 1514 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI); 1515 1516 // Analyze operands of the call, assigning locations to each operand. 1517 SmallVector<CCValAssign, 16> ValLocs; 1518 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs, *Context); 1519 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS); 1520 const Value *RV = Ret->getOperand(0); 1521 1522 // FIXME: Only one output register for now. 1523 if (ValLocs.size() > 1) 1524 return false; 1525 1526 // Special case for returning a constant integer of any size. 1527 // Materialize the constant as an i64 and copy it to the return 1528 // register. This avoids an unnecessary extend or truncate. 1529 if (isa<ConstantInt>(*RV)) { 1530 const Constant *C = cast<Constant>(RV); 1531 unsigned SrcReg = PPCMaterializeInt(C, MVT::i64); 1532 unsigned RetReg = ValLocs[0].getLocReg(); 1533 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1534 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg); 1535 RetRegs.push_back(RetReg); 1536 1537 } else { 1538 unsigned Reg = getRegForValue(RV); 1539 1540 if (Reg == 0) 1541 return false; 1542 1543 // Copy the result values into the output registers. 1544 for (unsigned i = 0; i < ValLocs.size(); ++i) { 1545 1546 CCValAssign &VA = ValLocs[i]; 1547 assert(VA.isRegLoc() && "Can only return in registers!"); 1548 RetRegs.push_back(VA.getLocReg()); 1549 unsigned SrcReg = Reg + VA.getValNo(); 1550 1551 EVT RVEVT = TLI.getValueType(RV->getType()); 1552 if (!RVEVT.isSimple()) 1553 return false; 1554 MVT RVVT = RVEVT.getSimpleVT(); 1555 MVT DestVT = VA.getLocVT(); 1556 1557 if (RVVT != DestVT && RVVT != MVT::i8 && 1558 RVVT != MVT::i16 && RVVT != MVT::i32) 1559 return false; 1560 1561 if (RVVT != DestVT) { 1562 switch (VA.getLocInfo()) { 1563 default: 1564 llvm_unreachable("Unknown loc info!"); 1565 case CCValAssign::Full: 1566 llvm_unreachable("Full value assign but types don't match?"); 1567 case CCValAssign::AExt: 1568 case CCValAssign::ZExt: { 1569 const TargetRegisterClass *RC = 1570 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass; 1571 unsigned TmpReg = createResultReg(RC); 1572 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true)) 1573 return false; 1574 SrcReg = TmpReg; 1575 break; 1576 } 1577 case CCValAssign::SExt: { 1578 const TargetRegisterClass *RC = 1579 (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass; 1580 unsigned TmpReg = createResultReg(RC); 1581 if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false)) 1582 return false; 1583 SrcReg = TmpReg; 1584 break; 1585 } 1586 } 1587 } 1588 1589 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1590 TII.get(TargetOpcode::COPY), RetRegs[i]) 1591 .addReg(SrcReg); 1592 } 1593 } 1594 } 1595 1596 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1597 TII.get(PPC::BLR)); 1598 1599 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i) 1600 MIB.addReg(RetRegs[i], RegState::Implicit); 1601 1602 return true; 1603 } 1604 1605 // Attempt to emit an integer extend of SrcReg into DestReg. Both 1606 // signed and zero extensions are supported. Return false if we 1607 // can't handle it. 1608 bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, 1609 unsigned DestReg, bool IsZExt) { 1610 if (DestVT != MVT::i32 && DestVT != MVT::i64) 1611 return false; 1612 if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32) 1613 return false; 1614 1615 // Signed extensions use EXTSB, EXTSH, EXTSW. 1616 if (!IsZExt) { 1617 unsigned Opc; 1618 if (SrcVT == MVT::i8) 1619 Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64; 1620 else if (SrcVT == MVT::i16) 1621 Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64; 1622 else { 1623 assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??"); 1624 Opc = PPC::EXTSW_32_64; 1625 } 1626 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg) 1627 .addReg(SrcReg); 1628 1629 // Unsigned 32-bit extensions use RLWINM. 1630 } else if (DestVT == MVT::i32) { 1631 unsigned MB; 1632 if (SrcVT == MVT::i8) 1633 MB = 24; 1634 else { 1635 assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??"); 1636 MB = 16; 1637 } 1638 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLWINM), 1639 DestReg) 1640 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31); 1641 1642 // Unsigned 64-bit extensions use RLDICL (with a 32-bit source). 1643 } else { 1644 unsigned MB; 1645 if (SrcVT == MVT::i8) 1646 MB = 56; 1647 else if (SrcVT == MVT::i16) 1648 MB = 48; 1649 else 1650 MB = 32; 1651 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1652 TII.get(PPC::RLDICL_32_64), DestReg) 1653 .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB); 1654 } 1655 1656 return true; 1657 } 1658 1659 // Attempt to fast-select an indirect branch instruction. 1660 bool PPCFastISel::SelectIndirectBr(const Instruction *I) { 1661 unsigned AddrReg = getRegForValue(I->getOperand(0)); 1662 if (AddrReg == 0) 1663 return false; 1664 1665 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8)) 1666 .addReg(AddrReg); 1667 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8)); 1668 1669 const IndirectBrInst *IB = cast<IndirectBrInst>(I); 1670 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i) 1671 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]); 1672 1673 return true; 1674 } 1675 1676 // Attempt to fast-select an integer truncate instruction. 1677 bool PPCFastISel::SelectTrunc(const Instruction *I) { 1678 Value *Src = I->getOperand(0); 1679 EVT SrcVT = TLI.getValueType(Src->getType(), true); 1680 EVT DestVT = TLI.getValueType(I->getType(), true); 1681 1682 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16) 1683 return false; 1684 1685 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8) 1686 return false; 1687 1688 unsigned SrcReg = getRegForValue(Src); 1689 if (!SrcReg) 1690 return false; 1691 1692 // The only interesting case is when we need to switch register classes. 1693 if (SrcVT == MVT::i64) { 1694 unsigned ResultReg = createResultReg(&PPC::GPRCRegClass); 1695 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1696 TII.get(TargetOpcode::COPY), 1697 ResultReg).addReg(SrcReg, 0, PPC::sub_32); 1698 SrcReg = ResultReg; 1699 } 1700 1701 UpdateValueMap(I, SrcReg); 1702 return true; 1703 } 1704 1705 // Attempt to fast-select an integer extend instruction. 1706 bool PPCFastISel::SelectIntExt(const Instruction *I) { 1707 Type *DestTy = I->getType(); 1708 Value *Src = I->getOperand(0); 1709 Type *SrcTy = Src->getType(); 1710 1711 bool IsZExt = isa<ZExtInst>(I); 1712 unsigned SrcReg = getRegForValue(Src); 1713 if (!SrcReg) return false; 1714 1715 EVT SrcEVT, DestEVT; 1716 SrcEVT = TLI.getValueType(SrcTy, true); 1717 DestEVT = TLI.getValueType(DestTy, true); 1718 if (!SrcEVT.isSimple()) 1719 return false; 1720 if (!DestEVT.isSimple()) 1721 return false; 1722 1723 MVT SrcVT = SrcEVT.getSimpleVT(); 1724 MVT DestVT = DestEVT.getSimpleVT(); 1725 1726 // If we know the register class needed for the result of this 1727 // instruction, use it. Otherwise pick the register class of the 1728 // correct size that does not contain X0/R0, since we don't know 1729 // whether downstream uses permit that assignment. 1730 unsigned AssignedReg = FuncInfo.ValueMap[I]; 1731 const TargetRegisterClass *RC = 1732 (AssignedReg ? MRI.getRegClass(AssignedReg) : 1733 (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass : 1734 &PPC::GPRC_and_GPRC_NOR0RegClass)); 1735 unsigned ResultReg = createResultReg(RC); 1736 1737 if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt)) 1738 return false; 1739 1740 UpdateValueMap(I, ResultReg); 1741 return true; 1742 } 1743 1744 // Attempt to fast-select an instruction that wasn't handled by 1745 // the table-generated machinery. 1746 bool PPCFastISel::TargetSelectInstruction(const Instruction *I) { 1747 1748 switch (I->getOpcode()) { 1749 case Instruction::Load: 1750 return SelectLoad(I); 1751 case Instruction::Store: 1752 return SelectStore(I); 1753 case Instruction::Br: 1754 return SelectBranch(I); 1755 case Instruction::IndirectBr: 1756 return SelectIndirectBr(I); 1757 case Instruction::FPExt: 1758 return SelectFPExt(I); 1759 case Instruction::FPTrunc: 1760 return SelectFPTrunc(I); 1761 case Instruction::SIToFP: 1762 return SelectIToFP(I, /*IsSigned*/ true); 1763 case Instruction::UIToFP: 1764 return SelectIToFP(I, /*IsSigned*/ false); 1765 case Instruction::FPToSI: 1766 return SelectFPToI(I, /*IsSigned*/ true); 1767 case Instruction::FPToUI: 1768 return SelectFPToI(I, /*IsSigned*/ false); 1769 case Instruction::Add: 1770 return SelectBinaryIntOp(I, ISD::ADD); 1771 case Instruction::Or: 1772 return SelectBinaryIntOp(I, ISD::OR); 1773 case Instruction::Sub: 1774 return SelectBinaryIntOp(I, ISD::SUB); 1775 case Instruction::Call: 1776 if (dyn_cast<IntrinsicInst>(I)) 1777 return false; 1778 return SelectCall(I); 1779 case Instruction::Ret: 1780 return SelectRet(I); 1781 case Instruction::Trunc: 1782 return SelectTrunc(I); 1783 case Instruction::ZExt: 1784 case Instruction::SExt: 1785 return SelectIntExt(I); 1786 // Here add other flavors of Instruction::XXX that automated 1787 // cases don't catch. For example, switches are terminators 1788 // that aren't yet handled. 1789 default: 1790 break; 1791 } 1792 return false; 1793 } 1794 1795 // Materialize a floating-point constant into a register, and return 1796 // the register number (or zero if we failed to handle it). 1797 unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) { 1798 // No plans to handle long double here. 1799 if (VT != MVT::f32 && VT != MVT::f64) 1800 return 0; 1801 1802 // All FP constants are loaded from the constant pool. 1803 unsigned Align = DL.getPrefTypeAlignment(CFP->getType()); 1804 assert(Align > 0 && "Unexpectedly missing alignment information!"); 1805 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align); 1806 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); 1807 CodeModel::Model CModel = TM.getCodeModel(); 1808 1809 MachineMemOperand *MMO = 1810 FuncInfo.MF->getMachineMemOperand( 1811 MachinePointerInfo::getConstantPool(), MachineMemOperand::MOLoad, 1812 (VT == MVT::f32) ? 4 : 8, Align); 1813 1814 unsigned Opc = (VT == MVT::f32) ? PPC::LFS : PPC::LFD; 1815 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass); 1816 1817 // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)). 1818 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault) { 1819 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocCPT), 1820 TmpReg) 1821 .addConstantPoolIndex(Idx).addReg(PPC::X2); 1822 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg) 1823 .addImm(0).addReg(TmpReg).addMemOperand(MMO); 1824 } else { 1825 // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA(X2, Idx)). 1826 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA), 1827 TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx); 1828 // But for large code model, we must generate a LDtocL followed 1829 // by the LF[SD]. 1830 if (CModel == CodeModel::Large) { 1831 unsigned TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass); 1832 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL), 1833 TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg); 1834 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg) 1835 .addImm(0).addReg(TmpReg2); 1836 } else 1837 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), DestReg) 1838 .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO) 1839 .addReg(TmpReg) 1840 .addMemOperand(MMO); 1841 } 1842 1843 return DestReg; 1844 } 1845 1846 // Materialize the address of a global value into a register, and return 1847 // the register number (or zero if we failed to handle it). 1848 unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) { 1849 assert(VT == MVT::i64 && "Non-address!"); 1850 const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass; 1851 unsigned DestReg = createResultReg(RC); 1852 1853 // Global values may be plain old object addresses, TLS object 1854 // addresses, constant pool entries, or jump tables. How we generate 1855 // code for these may depend on small, medium, or large code model. 1856 CodeModel::Model CModel = TM.getCodeModel(); 1857 1858 // FIXME: Jump tables are not yet required because fast-isel doesn't 1859 // handle switches; if that changes, we need them as well. For now, 1860 // what follows assumes everything's a generic (or TLS) global address. 1861 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 1862 1863 // FIXME: We don't yet handle the complexity of TLS. 1864 if (GV->isThreadLocal()) 1865 return 0; 1866 1867 // For small code model, generate a simple TOC load. 1868 if (CModel == CodeModel::Small || CModel == CodeModel::JITDefault) 1869 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtoc), 1870 DestReg) 1871 .addGlobalAddress(GV) 1872 .addReg(PPC::X2); 1873 else { 1874 // If the address is an externally defined symbol, a symbol with 1875 // common or externally available linkage, a function address, or a 1876 // jump table address (not yet needed), or if we are generating code 1877 // for large code model, we generate: 1878 // LDtocL(GV, ADDIStocHA(%X2, GV)) 1879 // Otherwise we generate: 1880 // ADDItocL(ADDIStocHA(%X2, GV), GV) 1881 // Either way, start with the ADDIStocHA: 1882 unsigned HighPartReg = createResultReg(RC); 1883 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDIStocHA), 1884 HighPartReg).addReg(PPC::X2).addGlobalAddress(GV); 1885 1886 // !GVar implies a function address. An external variable is one 1887 // without an initializer. 1888 // If/when switches are implemented, jump tables should be handled 1889 // on the "if" path here. 1890 if (CModel == CodeModel::Large || !GVar || !GVar->hasInitializer() || 1891 GVar->hasCommonLinkage() || GVar->hasAvailableExternallyLinkage()) 1892 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL), 1893 DestReg).addGlobalAddress(GV).addReg(HighPartReg); 1894 else 1895 // Otherwise generate the ADDItocL. 1896 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDItocL), 1897 DestReg).addReg(HighPartReg).addGlobalAddress(GV); 1898 } 1899 1900 return DestReg; 1901 } 1902 1903 // Materialize a 32-bit integer constant into a register, and return 1904 // the register number (or zero if we failed to handle it). 1905 unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm, 1906 const TargetRegisterClass *RC) { 1907 unsigned Lo = Imm & 0xFFFF; 1908 unsigned Hi = (Imm >> 16) & 0xFFFF; 1909 1910 unsigned ResultReg = createResultReg(RC); 1911 bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass); 1912 1913 if (isInt<16>(Imm)) 1914 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1915 TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg) 1916 .addImm(Imm); 1917 else if (Lo) { 1918 // Both Lo and Hi have nonzero bits. 1919 unsigned TmpReg = createResultReg(RC); 1920 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1921 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg) 1922 .addImm(Hi); 1923 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1924 TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg) 1925 .addReg(TmpReg).addImm(Lo); 1926 } else 1927 // Just Hi bits. 1928 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1929 TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg) 1930 .addImm(Hi); 1931 1932 return ResultReg; 1933 } 1934 1935 // Materialize a 64-bit integer constant into a register, and return 1936 // the register number (or zero if we failed to handle it). 1937 unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm, 1938 const TargetRegisterClass *RC) { 1939 unsigned Remainder = 0; 1940 unsigned Shift = 0; 1941 1942 // If the value doesn't fit in 32 bits, see if we can shift it 1943 // so that it fits in 32 bits. 1944 if (!isInt<32>(Imm)) { 1945 Shift = countTrailingZeros<uint64_t>(Imm); 1946 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift; 1947 1948 if (isInt<32>(ImmSh)) 1949 Imm = ImmSh; 1950 else { 1951 Remainder = Imm; 1952 Shift = 32; 1953 Imm >>= 32; 1954 } 1955 } 1956 1957 // Handle the high-order 32 bits (if shifted) or the whole 32 bits 1958 // (if not shifted). 1959 unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC); 1960 if (!Shift) 1961 return TmpReg1; 1962 1963 // If upper 32 bits were not zero, we've built them and need to shift 1964 // them into place. 1965 unsigned TmpReg2; 1966 if (Imm) { 1967 TmpReg2 = createResultReg(RC); 1968 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::RLDICR), 1969 TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift); 1970 } else 1971 TmpReg2 = TmpReg1; 1972 1973 unsigned TmpReg3, Hi, Lo; 1974 if ((Hi = (Remainder >> 16) & 0xFFFF)) { 1975 TmpReg3 = createResultReg(RC); 1976 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORIS8), 1977 TmpReg3).addReg(TmpReg2).addImm(Hi); 1978 } else 1979 TmpReg3 = TmpReg2; 1980 1981 if ((Lo = Remainder & 0xFFFF)) { 1982 unsigned ResultReg = createResultReg(RC); 1983 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ORI8), 1984 ResultReg).addReg(TmpReg3).addImm(Lo); 1985 return ResultReg; 1986 } 1987 1988 return TmpReg3; 1989 } 1990 1991 1992 // Materialize an integer constant into a register, and return 1993 // the register number (or zero if we failed to handle it). 1994 unsigned PPCFastISel::PPCMaterializeInt(const Constant *C, MVT VT) { 1995 // If we're using CR bit registers for i1 values, handle that as a special 1996 // case first. 1997 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) { 1998 const ConstantInt *CI = cast<ConstantInt>(C); 1999 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass); 2000 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2001 TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg); 2002 return ImmReg; 2003 } 2004 2005 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && 2006 VT != MVT::i8 && VT != MVT::i1) 2007 return 0; 2008 2009 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass : 2010 &PPC::GPRCRegClass); 2011 2012 // If the constant is in range, use a load-immediate. 2013 const ConstantInt *CI = cast<ConstantInt>(C); 2014 if (isInt<16>(CI->getSExtValue())) { 2015 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI; 2016 unsigned ImmReg = createResultReg(RC); 2017 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg) 2018 .addImm(CI->getSExtValue()); 2019 return ImmReg; 2020 } 2021 2022 // Construct the constant piecewise. 2023 int64_t Imm = CI->getZExtValue(); 2024 2025 if (VT == MVT::i64) 2026 return PPCMaterialize64BitInt(Imm, RC); 2027 else if (VT == MVT::i32) 2028 return PPCMaterialize32BitInt(Imm, RC); 2029 2030 return 0; 2031 } 2032 2033 // Materialize a constant into a register, and return the register 2034 // number (or zero if we failed to handle it). 2035 unsigned PPCFastISel::TargetMaterializeConstant(const Constant *C) { 2036 EVT CEVT = TLI.getValueType(C->getType(), true); 2037 2038 // Only handle simple types. 2039 if (!CEVT.isSimple()) return 0; 2040 MVT VT = CEVT.getSimpleVT(); 2041 2042 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) 2043 return PPCMaterializeFP(CFP, VT); 2044 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) 2045 return PPCMaterializeGV(GV, VT); 2046 else if (isa<ConstantInt>(C)) 2047 return PPCMaterializeInt(C, VT); 2048 2049 return 0; 2050 } 2051 2052 // Materialize the address created by an alloca into a register, and 2053 // return the register number (or zero if we failed to handle it). 2054 unsigned PPCFastISel::TargetMaterializeAlloca(const AllocaInst *AI) { 2055 // Don't handle dynamic allocas. 2056 if (!FuncInfo.StaticAllocaMap.count(AI)) return 0; 2057 2058 MVT VT; 2059 if (!isLoadTypeLegal(AI->getType(), VT)) return 0; 2060 2061 DenseMap<const AllocaInst*, int>::iterator SI = 2062 FuncInfo.StaticAllocaMap.find(AI); 2063 2064 if (SI != FuncInfo.StaticAllocaMap.end()) { 2065 unsigned ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass); 2066 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::ADDI8), 2067 ResultReg).addFrameIndex(SI->second).addImm(0); 2068 return ResultReg; 2069 } 2070 2071 return 0; 2072 } 2073 2074 // Fold loads into extends when possible. 2075 // FIXME: We can have multiple redundant extend/trunc instructions 2076 // following a load. The folding only picks up one. Extend this 2077 // to check subsequent instructions for the same pattern and remove 2078 // them. Thus ResultReg should be the def reg for the last redundant 2079 // instruction in a chain, and all intervening instructions can be 2080 // removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll 2081 // to add ELF64-NOT: rldicl to the appropriate tests when this works. 2082 bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo, 2083 const LoadInst *LI) { 2084 // Verify we have a legal type before going any further. 2085 MVT VT; 2086 if (!isLoadTypeLegal(LI->getType(), VT)) 2087 return false; 2088 2089 // Combine load followed by zero- or sign-extend. 2090 bool IsZExt = false; 2091 switch(MI->getOpcode()) { 2092 default: 2093 return false; 2094 2095 case PPC::RLDICL: 2096 case PPC::RLDICL_32_64: { 2097 IsZExt = true; 2098 unsigned MB = MI->getOperand(3).getImm(); 2099 if ((VT == MVT::i8 && MB <= 56) || 2100 (VT == MVT::i16 && MB <= 48) || 2101 (VT == MVT::i32 && MB <= 32)) 2102 break; 2103 return false; 2104 } 2105 2106 case PPC::RLWINM: 2107 case PPC::RLWINM8: { 2108 IsZExt = true; 2109 unsigned MB = MI->getOperand(3).getImm(); 2110 if ((VT == MVT::i8 && MB <= 24) || 2111 (VT == MVT::i16 && MB <= 16)) 2112 break; 2113 return false; 2114 } 2115 2116 case PPC::EXTSB: 2117 case PPC::EXTSB8: 2118 case PPC::EXTSB8_32_64: 2119 /* There is no sign-extending load-byte instruction. */ 2120 return false; 2121 2122 case PPC::EXTSH: 2123 case PPC::EXTSH8: 2124 case PPC::EXTSH8_32_64: { 2125 if (VT != MVT::i16 && VT != MVT::i8) 2126 return false; 2127 break; 2128 } 2129 2130 case PPC::EXTSW: 2131 case PPC::EXTSW_32_64: { 2132 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8) 2133 return false; 2134 break; 2135 } 2136 } 2137 2138 // See if we can handle this address. 2139 Address Addr; 2140 if (!PPCComputeAddress(LI->getOperand(0), Addr)) 2141 return false; 2142 2143 unsigned ResultReg = MI->getOperand(0).getReg(); 2144 2145 if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt)) 2146 return false; 2147 2148 MI->eraseFromParent(); 2149 return true; 2150 } 2151 2152 // Attempt to lower call arguments in a faster way than done by 2153 // the selection DAG code. 2154 bool PPCFastISel::FastLowerArguments() { 2155 // Defer to normal argument lowering for now. It's reasonably 2156 // efficient. Consider doing something like ARM to handle the 2157 // case where all args fit in registers, no varargs, no float 2158 // or vector args. 2159 return false; 2160 } 2161 2162 // Handle materializing integer constants into a register. This is not 2163 // automatically generated for PowerPC, so must be explicitly created here. 2164 unsigned PPCFastISel::FastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) { 2165 2166 if (Opc != ISD::Constant) 2167 return 0; 2168 2169 // If we're using CR bit registers for i1 values, handle that as a special 2170 // case first. 2171 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) { 2172 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass); 2173 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2174 TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg); 2175 return ImmReg; 2176 } 2177 2178 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && 2179 VT != MVT::i8 && VT != MVT::i1) 2180 return 0; 2181 2182 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass : 2183 &PPC::GPRCRegClass); 2184 if (VT == MVT::i64) 2185 return PPCMaterialize64BitInt(Imm, RC); 2186 else 2187 return PPCMaterialize32BitInt(Imm, RC); 2188 } 2189 2190 // Override for ADDI and ADDI8 to set the correct register class 2191 // on RHS operand 0. The automatic infrastructure naively assumes 2192 // GPRC for i32 and G8RC for i64; the concept of "no R0" is lost 2193 // for these cases. At the moment, none of the other automatically 2194 // generated RI instructions require special treatment. However, once 2195 // SelectSelect is implemented, "isel" requires similar handling. 2196 // 2197 // Also be conservative about the output register class. Avoid 2198 // assigning R0 or X0 to the output register for GPRC and G8RC 2199 // register classes, as any such result could be used in ADDI, etc., 2200 // where those regs have another meaning. 2201 unsigned PPCFastISel::FastEmitInst_ri(unsigned MachineInstOpcode, 2202 const TargetRegisterClass *RC, 2203 unsigned Op0, bool Op0IsKill, 2204 uint64_t Imm) { 2205 if (MachineInstOpcode == PPC::ADDI) 2206 MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass); 2207 else if (MachineInstOpcode == PPC::ADDI8) 2208 MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass); 2209 2210 const TargetRegisterClass *UseRC = 2211 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass : 2212 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC)); 2213 2214 return FastISel::FastEmitInst_ri(MachineInstOpcode, UseRC, 2215 Op0, Op0IsKill, Imm); 2216 } 2217 2218 // Override for instructions with one register operand to avoid use of 2219 // R0/X0. The automatic infrastructure isn't aware of the context so 2220 // we must be conservative. 2221 unsigned PPCFastISel::FastEmitInst_r(unsigned MachineInstOpcode, 2222 const TargetRegisterClass* RC, 2223 unsigned Op0, bool Op0IsKill) { 2224 const TargetRegisterClass *UseRC = 2225 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass : 2226 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC)); 2227 2228 return FastISel::FastEmitInst_r(MachineInstOpcode, UseRC, Op0, Op0IsKill); 2229 } 2230 2231 // Override for instructions with two register operands to avoid use 2232 // of R0/X0. The automatic infrastructure isn't aware of the context 2233 // so we must be conservative. 2234 unsigned PPCFastISel::FastEmitInst_rr(unsigned MachineInstOpcode, 2235 const TargetRegisterClass* RC, 2236 unsigned Op0, bool Op0IsKill, 2237 unsigned Op1, bool Op1IsKill) { 2238 const TargetRegisterClass *UseRC = 2239 (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass : 2240 (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC)); 2241 2242 return FastISel::FastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op0IsKill, 2243 Op1, Op1IsKill); 2244 } 2245 2246 namespace llvm { 2247 // Create the fast instruction selector for PowerPC64 ELF. 2248 FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo, 2249 const TargetLibraryInfo *LibInfo) { 2250 const TargetMachine &TM = FuncInfo.MF->getTarget(); 2251 2252 // Only available on 64-bit ELF for now. 2253 const PPCSubtarget *Subtarget = &TM.getSubtarget<PPCSubtarget>(); 2254 if (Subtarget->isPPC64() && Subtarget->isSVR4ABI()) 2255 return new PPCFastISel(FuncInfo, LibInfo); 2256 2257 return nullptr; 2258 } 2259 } 2260