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