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