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