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