comparison lib/Target/PowerPC/PPCFastISel.cpp @ 95:afa8332a0e37

LLVM 3.8
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Tue, 13 Oct 2015 17:48:58 +0900
parents 60c9769439b8
children 7d135dc70f03
comparison
equal deleted inserted replaced
84:f3e34b893a5f 95:afa8332a0e37
142 142
143 // Utility routines. 143 // Utility routines.
144 private: 144 private:
145 bool isTypeLegal(Type *Ty, MVT &VT); 145 bool isTypeLegal(Type *Ty, MVT &VT);
146 bool isLoadTypeLegal(Type *Ty, MVT &VT); 146 bool isLoadTypeLegal(Type *Ty, MVT &VT);
147 bool isValueAvailable(const Value *V) const;
147 bool isVSFRCRegister(unsigned Register) const { 148 bool isVSFRCRegister(unsigned Register) const {
148 return MRI.getRegClass(Register)->getID() == PPC::VSFRCRegClassID; 149 return MRI.getRegClass(Register)->getID() == PPC::VSFRCRegClassID;
150 }
151 bool isVSSRCRegister(unsigned Register) const {
152 return MRI.getRegClass(Register)->getID() == PPC::VSSRCRegClassID;
149 } 153 }
150 bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value, 154 bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
151 bool isZExt, unsigned DestReg); 155 bool isZExt, unsigned DestReg);
152 bool PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr, 156 bool PPCEmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
153 const TargetRegisterClass *RC, bool IsZExt = true, 157 const TargetRegisterClass *RC, bool IsZExt = true,
158 unsigned &IndexReg); 162 unsigned &IndexReg);
159 bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, 163 bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
160 unsigned DestReg, bool IsZExt); 164 unsigned DestReg, bool IsZExt);
161 unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT); 165 unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
162 unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT); 166 unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
163 unsigned PPCMaterializeInt(const Constant *C, MVT VT, bool UseSExt = true); 167 unsigned PPCMaterializeInt(const ConstantInt *CI, MVT VT,
168 bool UseSExt = true);
164 unsigned PPCMaterialize32BitInt(int64_t Imm, 169 unsigned PPCMaterialize32BitInt(int64_t Imm,
165 const TargetRegisterClass *RC); 170 const TargetRegisterClass *RC);
166 unsigned PPCMaterialize64BitInt(int64_t Imm, 171 unsigned PPCMaterialize64BitInt(int64_t Imm,
167 const TargetRegisterClass *RC); 172 const TargetRegisterClass *RC);
168 unsigned PPCMoveToIntReg(const Instruction *I, MVT VT, 173 unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
256 261
257 // Determine whether the type Ty is simple enough to be handled by 262 // Determine whether the type Ty is simple enough to be handled by
258 // fast-isel, and return its equivalent machine type in VT. 263 // fast-isel, and return its equivalent machine type in VT.
259 // FIXME: Copied directly from ARM -- factor into base class? 264 // FIXME: Copied directly from ARM -- factor into base class?
260 bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) { 265 bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
261 EVT Evt = TLI.getValueType(Ty, true); 266 EVT Evt = TLI.getValueType(DL, Ty, true);
262 267
263 // Only handle simple types. 268 // Only handle simple types.
264 if (Evt == MVT::Other || !Evt.isSimple()) return false; 269 if (Evt == MVT::Other || !Evt.isSimple()) return false;
265 VT = Evt.getSimpleVT(); 270 VT = Evt.getSimpleVT();
266 271
277 // If this is a type than can be sign or zero-extended to a basic operation 282 // If this is a type than can be sign or zero-extended to a basic operation
278 // go ahead and accept it now. 283 // go ahead and accept it now.
279 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) { 284 if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
280 return true; 285 return true;
281 } 286 }
287
288 return false;
289 }
290
291 bool PPCFastISel::isValueAvailable(const Value *V) const {
292 if (!isa<Instruction>(V))
293 return true;
294
295 const auto *I = cast<Instruction>(V);
296 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB)
297 return true;
282 298
283 return false; 299 return false;
284 } 300 }
285 301
286 // Given a value Obj, create an Address object Addr that represents its 302 // Given a value Obj, create an Address object Addr that represents its
307 case Instruction::BitCast: 323 case Instruction::BitCast:
308 // Look through bitcasts. 324 // Look through bitcasts.
309 return PPCComputeAddress(U->getOperand(0), Addr); 325 return PPCComputeAddress(U->getOperand(0), Addr);
310 case Instruction::IntToPtr: 326 case Instruction::IntToPtr:
311 // Look past no-op inttoptrs. 327 // Look past no-op inttoptrs.
312 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy()) 328 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
329 TLI.getPointerTy(DL))
313 return PPCComputeAddress(U->getOperand(0), Addr); 330 return PPCComputeAddress(U->getOperand(0), Addr);
314 break; 331 break;
315 case Instruction::PtrToInt: 332 case Instruction::PtrToInt:
316 // Look past no-op ptrtoints. 333 // Look past no-op ptrtoints.
317 if (TLI.getValueType(U->getType()) == TLI.getPointerTy()) 334 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
318 return PPCComputeAddress(U->getOperand(0), Addr); 335 return PPCComputeAddress(U->getOperand(0), Addr);
319 break; 336 break;
320 case Instruction::GetElementPtr: { 337 case Instruction::GetElementPtr: {
321 Address SavedAddr = Addr; 338 Address SavedAddr = Addr;
322 long TmpOffset = Addr.Offset; 339 long TmpOffset = Addr.Offset;
489 unsigned IndexReg = 0; 506 unsigned IndexReg = 0;
490 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg); 507 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
491 508
492 // If this is a potential VSX load with an offset of 0, a VSX indexed load can 509 // If this is a potential VSX load with an offset of 0, a VSX indexed load can
493 // be used. 510 // be used.
511 bool IsVSSRC = (ResultReg != 0) && isVSSRCRegister(ResultReg);
494 bool IsVSFRC = (ResultReg != 0) && isVSFRCRegister(ResultReg); 512 bool IsVSFRC = (ResultReg != 0) && isVSFRCRegister(ResultReg);
495 if (IsVSFRC && (Opc == PPC::LFD) && 513 bool Is32VSXLoad = IsVSSRC && Opc == PPC::LFS;
514 bool Is64VSXLoad = IsVSSRC && Opc == PPC::LFD;
515 if ((Is32VSXLoad || Is64VSXLoad) &&
496 (Addr.BaseType != Address::FrameIndexBase) && UseOffset && 516 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
497 (Addr.Offset == 0)) { 517 (Addr.Offset == 0)) {
498 UseOffset = false; 518 UseOffset = false;
499 } 519 }
500 520
504 // Note: If we still have a frame index here, we know the offset is 524 // Note: If we still have a frame index here, we know the offset is
505 // in range, as otherwise PPCSimplifyAddress would have converted it 525 // in range, as otherwise PPCSimplifyAddress would have converted it
506 // into a RegBase. 526 // into a RegBase.
507 if (Addr.BaseType == Address::FrameIndexBase) { 527 if (Addr.BaseType == Address::FrameIndexBase) {
508 // VSX only provides an indexed load. 528 // VSX only provides an indexed load.
509 if (IsVSFRC && Opc == PPC::LFD) return false; 529 if (Is32VSXLoad || Is64VSXLoad) return false;
510 530
511 MachineMemOperand *MMO = 531 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
512 FuncInfo.MF->getMachineMemOperand( 532 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
513 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset), 533 Addr.Offset),
514 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI), 534 MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
515 MFI.getObjectAlignment(Addr.Base.FI)); 535 MFI.getObjectAlignment(Addr.Base.FI));
516 536
517 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) 537 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
518 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO); 538 .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
519 539
520 // Base reg with offset in range. 540 // Base reg with offset in range.
521 } else if (UseOffset) { 541 } else if (UseOffset) {
522 // VSX only provides an indexed load. 542 // VSX only provides an indexed load.
523 if (IsVSFRC && Opc == PPC::LFD) return false; 543 if (Is32VSXLoad || Is64VSXLoad) return false;
524 544
525 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) 545 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
526 .addImm(Addr.Offset).addReg(Addr.Base.Reg); 546 .addImm(Addr.Offset).addReg(Addr.Base.Reg);
527 547
528 // Indexed form. 548 // Indexed form.
541 case PPC::LWZ: Opc = PPC::LWZX; break; 561 case PPC::LWZ: Opc = PPC::LWZX; break;
542 case PPC::LWZ8: Opc = PPC::LWZX8; break; 562 case PPC::LWZ8: Opc = PPC::LWZX8; break;
543 case PPC::LWA: Opc = PPC::LWAX; break; 563 case PPC::LWA: Opc = PPC::LWAX; break;
544 case PPC::LWA_32: Opc = PPC::LWAX_32; break; 564 case PPC::LWA_32: Opc = PPC::LWAX_32; break;
545 case PPC::LD: Opc = PPC::LDX; break; 565 case PPC::LD: Opc = PPC::LDX; break;
546 case PPC::LFS: Opc = PPC::LFSX; break; 566 case PPC::LFS: Opc = IsVSSRC ? PPC::LXSSPX : PPC::LFSX; break;
547 case PPC::LFD: Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break; 567 case PPC::LFD: Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break;
548 } 568 }
549 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) 569 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
550 .addReg(Addr.Base.Reg).addReg(IndexReg); 570 .addReg(Addr.Base.Reg).addReg(IndexReg);
551 } 571 }
622 unsigned IndexReg = 0; 642 unsigned IndexReg = 0;
623 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg); 643 PPCSimplifyAddress(Addr, VT, UseOffset, IndexReg);
624 644
625 // If this is a potential VSX store with an offset of 0, a VSX indexed store 645 // If this is a potential VSX store with an offset of 0, a VSX indexed store
626 // can be used. 646 // can be used.
647 bool IsVSSRC = isVSSRCRegister(SrcReg);
627 bool IsVSFRC = isVSFRCRegister(SrcReg); 648 bool IsVSFRC = isVSFRCRegister(SrcReg);
628 if (IsVSFRC && (Opc == PPC::STFD) && 649 bool Is32VSXStore = IsVSSRC && Opc == PPC::STFS;
629 (Addr.BaseType != Address::FrameIndexBase) && UseOffset && 650 bool Is64VSXStore = IsVSFRC && Opc == PPC::STFD;
651 if ((Is32VSXStore || Is64VSXStore) &&
652 (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
630 (Addr.Offset == 0)) { 653 (Addr.Offset == 0)) {
631 UseOffset = false; 654 UseOffset = false;
632 } 655 }
633 656
634 // Note: If we still have a frame index here, we know the offset is 657 // Note: If we still have a frame index here, we know the offset is
635 // in range, as otherwise PPCSimplifyAddress would have converted it 658 // in range, as otherwise PPCSimplifyAddress would have converted it
636 // into a RegBase. 659 // into a RegBase.
637 if (Addr.BaseType == Address::FrameIndexBase) { 660 if (Addr.BaseType == Address::FrameIndexBase) {
638 // VSX only provides an indexed store. 661 // VSX only provides an indexed store.
639 if (IsVSFRC && Opc == PPC::STFD) return false; 662 if (Is32VSXStore || Is64VSXStore) return false;
640 663
641 MachineMemOperand *MMO = 664 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
642 FuncInfo.MF->getMachineMemOperand( 665 MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
643 MachinePointerInfo::getFixedStack(Addr.Base.FI, Addr.Offset), 666 Addr.Offset),
644 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI), 667 MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
645 MFI.getObjectAlignment(Addr.Base.FI)); 668 MFI.getObjectAlignment(Addr.Base.FI));
646 669
647 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc)) 670 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
648 .addReg(SrcReg) 671 .addReg(SrcReg)
651 .addMemOperand(MMO); 674 .addMemOperand(MMO);
652 675
653 // Base reg with offset in range. 676 // Base reg with offset in range.
654 } else if (UseOffset) { 677 } else if (UseOffset) {
655 // VSX only provides an indexed store. 678 // VSX only provides an indexed store.
656 if (IsVSFRC && Opc == PPC::STFD) return false; 679 if (Is32VSXStore || Is64VSXStore) return false;
657 680
658 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc)) 681 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
659 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg); 682 .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
660 683
661 // Indexed form. 684 // Indexed form.
670 case PPC::STW : Opc = PPC::STWX; break; 693 case PPC::STW : Opc = PPC::STWX; break;
671 case PPC::STB8: Opc = PPC::STBX8; break; 694 case PPC::STB8: Opc = PPC::STBX8; break;
672 case PPC::STH8: Opc = PPC::STHX8; break; 695 case PPC::STH8: Opc = PPC::STHX8; break;
673 case PPC::STW8: Opc = PPC::STWX8; break; 696 case PPC::STW8: Opc = PPC::STWX8; break;
674 case PPC::STD: Opc = PPC::STDX; break; 697 case PPC::STD: Opc = PPC::STDX; break;
675 case PPC::STFS: Opc = PPC::STFSX; break; 698 case PPC::STFS: Opc = IsVSSRC ? PPC::STXSSPX : PPC::STFSX; break;
676 case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break; 699 case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break;
677 } 700 }
678 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc)) 701
679 .addReg(SrcReg).addReg(Addr.Base.Reg).addReg(IndexReg); 702 auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
703 .addReg(SrcReg);
704
705 // If we have an index register defined we use it in the store inst,
706 // otherwise we use X0 as base as it makes the vector instructions to
707 // use zero in the computation of the effective address regardless the
708 // content of the register.
709 if (IndexReg)
710 MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
711 else
712 MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
680 } 713 }
681 714
682 return true; 715 return true;
683 } 716 }
684 717
719 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)]; 752 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
720 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)]; 753 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
721 754
722 // For now, just try the simplest case where it's fed by a compare. 755 // For now, just try the simplest case where it's fed by a compare.
723 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) { 756 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
724 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate()); 757 if (isValueAvailable(CI)) {
725 if (!OptPPCPred) 758 Optional<PPC::Predicate> OptPPCPred = getComparePred(CI->getPredicate());
726 return false; 759 if (!OptPPCPred)
727 760 return false;
728 PPC::Predicate PPCPred = OptPPCPred.getValue(); 761
729 762 PPC::Predicate PPCPred = OptPPCPred.getValue();
730 // Take advantage of fall-through opportunities. 763
731 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) { 764 // Take advantage of fall-through opportunities.
732 std::swap(TBB, FBB); 765 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
733 PPCPred = PPC::InvertPredicate(PPCPred); 766 std::swap(TBB, FBB);
767 PPCPred = PPC::InvertPredicate(PPCPred);
768 }
769
770 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
771
772 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
773 CondReg))
774 return false;
775
776 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
777 .addImm(PPCPred).addReg(CondReg).addMBB(TBB);
778 finishCondBranch(BI->getParent(), TBB, FBB);
779 return true;
734 } 780 }
735
736 unsigned CondReg = createResultReg(&PPC::CRRCRegClass);
737
738 if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
739 CondReg))
740 return false;
741
742 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCC))
743 .addImm(PPCPred).addReg(CondReg).addMBB(TBB);
744 fastEmitBranch(FBB, DbgLoc);
745 FuncInfo.MBB->addSuccessor(TBB);
746 return true;
747
748 } else if (const ConstantInt *CI = 781 } else if (const ConstantInt *CI =
749 dyn_cast<ConstantInt>(BI->getCondition())) { 782 dyn_cast<ConstantInt>(BI->getCondition())) {
750 uint64_t Imm = CI->getZExtValue(); 783 uint64_t Imm = CI->getZExtValue();
751 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB; 784 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
752 fastEmitBranch(Target, DbgLoc); 785 fastEmitBranch(Target, DbgLoc);
765 // Attempt to emit a compare of the two source values. Signed and unsigned 798 // Attempt to emit a compare of the two source values. Signed and unsigned
766 // comparisons are supported. Return false if we can't handle it. 799 // comparisons are supported. Return false if we can't handle it.
767 bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2, 800 bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
768 bool IsZExt, unsigned DestReg) { 801 bool IsZExt, unsigned DestReg) {
769 Type *Ty = SrcValue1->getType(); 802 Type *Ty = SrcValue1->getType();
770 EVT SrcEVT = TLI.getValueType(Ty, true); 803 EVT SrcEVT = TLI.getValueType(DL, Ty, true);
771 if (!SrcEVT.isSimple()) 804 if (!SrcEVT.isSimple())
772 return false; 805 return false;
773 MVT SrcVT = SrcEVT.getSimpleVT(); 806 MVT SrcVT = SrcEVT.getSimpleVT();
774 807
775 if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits()) 808 if (SrcVT == MVT::i1 && PPCSubTarget->useCRBits())
859 } 892 }
860 893
861 // Attempt to fast-select a floating-point extend instruction. 894 // Attempt to fast-select a floating-point extend instruction.
862 bool PPCFastISel::SelectFPExt(const Instruction *I) { 895 bool PPCFastISel::SelectFPExt(const Instruction *I) {
863 Value *Src = I->getOperand(0); 896 Value *Src = I->getOperand(0);
864 EVT SrcVT = TLI.getValueType(Src->getType(), true); 897 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
865 EVT DestVT = TLI.getValueType(I->getType(), true); 898 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
866 899
867 if (SrcVT != MVT::f32 || DestVT != MVT::f64) 900 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
868 return false; 901 return false;
869 902
870 unsigned SrcReg = getRegForValue(Src); 903 unsigned SrcReg = getRegForValue(Src);
877 } 910 }
878 911
879 // Attempt to fast-select a floating-point truncate instruction. 912 // Attempt to fast-select a floating-point truncate instruction.
880 bool PPCFastISel::SelectFPTrunc(const Instruction *I) { 913 bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
881 Value *Src = I->getOperand(0); 914 Value *Src = I->getOperand(0);
882 EVT SrcVT = TLI.getValueType(Src->getType(), true); 915 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
883 EVT DestVT = TLI.getValueType(I->getType(), true); 916 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
884 917
885 if (SrcVT != MVT::f64 || DestVT != MVT::f32) 918 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
886 return false; 919 return false;
887 920
888 unsigned SrcReg = getRegForValue(Src); 921 unsigned SrcReg = getRegForValue(Src);
946 979
947 return ResultReg; 980 return ResultReg;
948 } 981 }
949 982
950 // Attempt to fast-select an integer-to-floating-point conversion. 983 // Attempt to fast-select an integer-to-floating-point conversion.
984 // FIXME: Once fast-isel has better support for VSX, conversions using
985 // direct moves should be implemented.
951 bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) { 986 bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
952 MVT DstVT; 987 MVT DstVT;
953 Type *DstTy = I->getType(); 988 Type *DstTy = I->getType();
954 if (!isTypeLegal(DstTy, DstVT)) 989 if (!isTypeLegal(DstTy, DstVT))
955 return false; 990 return false;
956 991
957 if (DstVT != MVT::f32 && DstVT != MVT::f64) 992 if (DstVT != MVT::f32 && DstVT != MVT::f64)
958 return false; 993 return false;
959 994
960 Value *Src = I->getOperand(0); 995 Value *Src = I->getOperand(0);
961 EVT SrcEVT = TLI.getValueType(Src->getType(), true); 996 EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
962 if (!SrcEVT.isSimple()) 997 if (!SrcEVT.isSimple())
963 return false; 998 return false;
964 999
965 MVT SrcVT = SrcEVT.getSimpleVT(); 1000 MVT SrcVT = SrcEVT.getSimpleVT();
966 1001
1053 1088
1054 return ResultReg; 1089 return ResultReg;
1055 } 1090 }
1056 1091
1057 // Attempt to fast-select a floating-point-to-integer conversion. 1092 // Attempt to fast-select a floating-point-to-integer conversion.
1093 // FIXME: Once fast-isel has better support for VSX, conversions using
1094 // direct moves should be implemented.
1058 bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) { 1095 bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
1059 MVT DstVT, SrcVT; 1096 MVT DstVT, SrcVT;
1060 Type *DstTy = I->getType(); 1097 Type *DstTy = I->getType();
1061 if (!isTypeLegal(DstTy, DstVT)) 1098 if (!isTypeLegal(DstTy, DstVT))
1062 return false; 1099 return false;
1119 } 1156 }
1120 1157
1121 // Attempt to fast-select a binary integer operation that isn't already 1158 // Attempt to fast-select a binary integer operation that isn't already
1122 // handled automatically. 1159 // handled automatically.
1123 bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) { 1160 bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
1124 EVT DestVT = TLI.getValueType(I->getType(), true); 1161 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
1125 1162
1126 // We can get here in the case when we have a binary operation on a non-legal 1163 // We can get here in the case when we have a binary operation on a non-legal
1127 // type and the target independent selector doesn't know how to handle it. 1164 // type and the target independent selector doesn't know how to handle it.
1128 if (DestVT != MVT::i16 && DestVT != MVT::i8) 1165 if (DestVT != MVT::i16 && DestVT != MVT::i8)
1129 return false; 1166 return false;
1410 bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) { 1447 bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1411 CallingConv::ID CC = CLI.CallConv; 1448 CallingConv::ID CC = CLI.CallConv;
1412 bool IsTailCall = CLI.IsTailCall; 1449 bool IsTailCall = CLI.IsTailCall;
1413 bool IsVarArg = CLI.IsVarArg; 1450 bool IsVarArg = CLI.IsVarArg;
1414 const Value *Callee = CLI.Callee; 1451 const Value *Callee = CLI.Callee;
1415 const char *SymName = CLI.SymName; 1452 const MCSymbol *Symbol = CLI.Symbol;
1416 1453
1417 if (!Callee && !SymName) 1454 if (!Callee && !Symbol)
1418 return false; 1455 return false;
1419 1456
1420 // Allow SelectionDAG isel to handle tail calls. 1457 // Allow SelectionDAG isel to handle tail calls.
1421 if (IsTailCall) 1458 if (IsTailCall)
1422 return false; 1459 return false;
1431 MVT RetVT; 1468 MVT RetVT;
1432 if (RetTy->isVoidTy()) 1469 if (RetTy->isVoidTy())
1433 RetVT = MVT::isVoid; 1470 RetVT = MVT::isVoid;
1434 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 && 1471 else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
1435 RetVT != MVT::i8) 1472 RetVT != MVT::i8)
1473 return false;
1474 else if (RetVT == MVT::i1 && PPCSubTarget->useCRBits())
1475 // We can't handle boolean returns when CR bits are in use.
1436 return false; 1476 return false;
1437 1477
1438 // FIXME: No multi-register return values yet. 1478 // FIXME: No multi-register return values yet.
1439 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 && 1479 if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
1440 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 && 1480 RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
1530 PPCFuncInfo->setUsesTOCBasePtr(); 1570 PPCFuncInfo->setUsesTOCBasePtr();
1531 MIB.addReg(PPC::X2, RegState::Implicit); 1571 MIB.addReg(PPC::X2, RegState::Implicit);
1532 1572
1533 // Add a register mask with the call-preserved registers. Proper 1573 // Add a register mask with the call-preserved registers. Proper
1534 // defs for return values will be added by setPhysRegsDeadExcept(). 1574 // defs for return values will be added by setPhysRegsDeadExcept().
1535 MIB.addRegMask(TRI.getCallPreservedMask(CC)); 1575 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
1536 1576
1537 CLI.Call = MIB; 1577 CLI.Call = MIB;
1538 1578
1539 // Finish off the call including any return values. 1579 // Finish off the call including any return values.
1540 return finishCall(RetVT, CLI, NumBytes); 1580 return finishCall(RetVT, CLI, NumBytes);
1553 SmallVector<unsigned, 4> RetRegs; 1593 SmallVector<unsigned, 4> RetRegs;
1554 CallingConv::ID CC = F.getCallingConv(); 1594 CallingConv::ID CC = F.getCallingConv();
1555 1595
1556 if (Ret->getNumOperands() > 0) { 1596 if (Ret->getNumOperands() > 0) {
1557 SmallVector<ISD::OutputArg, 4> Outs; 1597 SmallVector<ISD::OutputArg, 4> Outs;
1558 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI); 1598 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
1559 1599
1560 // Analyze operands of the call, assigning locations to each operand. 1600 // Analyze operands of the call, assigning locations to each operand.
1561 SmallVector<CCValAssign, 16> ValLocs; 1601 SmallVector<CCValAssign, 16> ValLocs;
1562 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context); 1602 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);
1563 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS); 1603 CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
1565 1605
1566 // FIXME: Only one output register for now. 1606 // FIXME: Only one output register for now.
1567 if (ValLocs.size() > 1) 1607 if (ValLocs.size() > 1)
1568 return false; 1608 return false;
1569 1609
1570 // Special case for returning a constant integer of any size. 1610 // Special case for returning a constant integer of any size - materialize
1571 // Materialize the constant as an i64 and copy it to the return 1611 // the constant as an i64 and copy it to the return register.
1572 // register. We still need to worry about properly extending the sign. E.g: 1612 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RV)) {
1573 // If the constant has only one bit, it means it is a boolean. Therefore
1574 // we can't use PPCMaterializeInt because it extends the sign which will
1575 // cause negations of the returned value to be incorrect as they are
1576 // implemented as the flip of the least significant bit.
1577 if (isa<ConstantInt>(*RV)) {
1578 const Constant *C = cast<Constant>(RV);
1579
1580 CCValAssign &VA = ValLocs[0]; 1613 CCValAssign &VA = ValLocs[0];
1581 1614
1582 unsigned RetReg = VA.getLocReg(); 1615 unsigned RetReg = VA.getLocReg();
1583 unsigned SrcReg = PPCMaterializeInt(C, MVT::i64, 1616 // We still need to worry about properly extending the sign. For example,
1584 VA.getLocInfo() == CCValAssign::SExt); 1617 // we could have only a single bit or a constant that needs zero
1618 // extension rather than sign extension. Make sure we pass the return
1619 // value extension property to integer materialization.
1620 unsigned SrcReg =
1621 PPCMaterializeInt(CI, MVT::i64, VA.getLocInfo() == CCValAssign::SExt);
1585 1622
1586 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 1623 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1587 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg); 1624 TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
1588 1625
1589 RetRegs.push_back(RetReg); 1626 RetRegs.push_back(RetReg);
1600 CCValAssign &VA = ValLocs[i]; 1637 CCValAssign &VA = ValLocs[i];
1601 assert(VA.isRegLoc() && "Can only return in registers!"); 1638 assert(VA.isRegLoc() && "Can only return in registers!");
1602 RetRegs.push_back(VA.getLocReg()); 1639 RetRegs.push_back(VA.getLocReg());
1603 unsigned SrcReg = Reg + VA.getValNo(); 1640 unsigned SrcReg = Reg + VA.getValNo();
1604 1641
1605 EVT RVEVT = TLI.getValueType(RV->getType()); 1642 EVT RVEVT = TLI.getValueType(DL, RV->getType());
1606 if (!RVEVT.isSimple()) 1643 if (!RVEVT.isSimple())
1607 return false; 1644 return false;
1608 MVT RVVT = RVEVT.getSimpleVT(); 1645 MVT RVVT = RVEVT.getSimpleVT();
1609 MVT DestVT = VA.getLocVT(); 1646 MVT DestVT = VA.getLocVT();
1610 1647
1719 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8)) 1756 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::MTCTR8))
1720 .addReg(AddrReg); 1757 .addReg(AddrReg);
1721 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8)); 1758 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::BCTR8));
1722 1759
1723 const IndirectBrInst *IB = cast<IndirectBrInst>(I); 1760 const IndirectBrInst *IB = cast<IndirectBrInst>(I);
1724 for (unsigned i = 0, e = IB->getNumSuccessors(); i != e; ++i) 1761 for (const BasicBlock *SuccBB : IB->successors())
1725 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[IB->getSuccessor(i)]); 1762 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
1726 1763
1727 return true; 1764 return true;
1728 } 1765 }
1729 1766
1730 // Attempt to fast-select an integer truncate instruction. 1767 // Attempt to fast-select an integer truncate instruction.
1731 bool PPCFastISel::SelectTrunc(const Instruction *I) { 1768 bool PPCFastISel::SelectTrunc(const Instruction *I) {
1732 Value *Src = I->getOperand(0); 1769 Value *Src = I->getOperand(0);
1733 EVT SrcVT = TLI.getValueType(Src->getType(), true); 1770 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1734 EVT DestVT = TLI.getValueType(I->getType(), true); 1771 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
1735 1772
1736 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16) 1773 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
1737 return false; 1774 return false;
1738 1775
1739 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8) 1776 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
1765 bool IsZExt = isa<ZExtInst>(I); 1802 bool IsZExt = isa<ZExtInst>(I);
1766 unsigned SrcReg = getRegForValue(Src); 1803 unsigned SrcReg = getRegForValue(Src);
1767 if (!SrcReg) return false; 1804 if (!SrcReg) return false;
1768 1805
1769 EVT SrcEVT, DestEVT; 1806 EVT SrcEVT, DestEVT;
1770 SrcEVT = TLI.getValueType(SrcTy, true); 1807 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1771 DestEVT = TLI.getValueType(DestTy, true); 1808 DestEVT = TLI.getValueType(DL, DestTy, true);
1772 if (!SrcEVT.isSimple()) 1809 if (!SrcEVT.isSimple())
1773 return false; 1810 return false;
1774 if (!DestEVT.isSimple()) 1811 if (!DestEVT.isSimple())
1775 return false; 1812 return false;
1776 1813
1856 assert(Align > 0 && "Unexpectedly missing alignment information!"); 1893 assert(Align > 0 && "Unexpectedly missing alignment information!");
1857 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align); 1894 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
1858 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT)); 1895 unsigned DestReg = createResultReg(TLI.getRegClassFor(VT));
1859 CodeModel::Model CModel = TM.getCodeModel(); 1896 CodeModel::Model CModel = TM.getCodeModel();
1860 1897
1861 MachineMemOperand *MMO = 1898 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
1862 FuncInfo.MF->getMachineMemOperand( 1899 MachinePointerInfo::getConstantPool(*FuncInfo.MF),
1863 MachinePointerInfo::getConstantPool(), MachineMemOperand::MOLoad, 1900 MachineMemOperand::MOLoad, (VT == MVT::f32) ? 4 : 8, Align);
1864 (VT == MVT::f32) ? 4 : 8, Align);
1865 1901
1866 unsigned Opc = (VT == MVT::f32) ? PPC::LFS : PPC::LFD; 1902 unsigned Opc = (VT == MVT::f32) ? PPC::LFS : PPC::LFD;
1867 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass); 1903 unsigned TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
1868 1904
1869 PPCFuncInfo->setUsesTOCBasePtr(); 1905 PPCFuncInfo->setUsesTOCBasePtr();
1938 1974
1939 // If/when switches are implemented, jump tables should be handled 1975 // If/when switches are implemented, jump tables should be handled
1940 // on the "if" path here. 1976 // on the "if" path here.
1941 if (CModel == CodeModel::Large || 1977 if (CModel == CodeModel::Large ||
1942 (GV->getType()->getElementType()->isFunctionTy() && 1978 (GV->getType()->getElementType()->isFunctionTy() &&
1943 (GV->isDeclaration() || GV->isWeakForLinker())) || 1979 !GV->isStrongDefinitionForLinker()) ||
1944 GV->isDeclaration() || GV->hasCommonLinkage() || 1980 GV->isDeclaration() || GV->hasCommonLinkage() ||
1945 GV->hasAvailableExternallyLinkage()) 1981 GV->hasAvailableExternallyLinkage())
1946 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL), 1982 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(PPC::LDtocL),
1947 DestReg).addGlobalAddress(GV).addReg(HighPartReg); 1983 DestReg).addGlobalAddress(GV).addReg(HighPartReg);
1948 else 1984 else
2043 } 2079 }
2044 2080
2045 2081
2046 // Materialize an integer constant into a register, and return 2082 // Materialize an integer constant into a register, and return
2047 // the register number (or zero if we failed to handle it). 2083 // the register number (or zero if we failed to handle it).
2048 unsigned PPCFastISel::PPCMaterializeInt(const Constant *C, MVT VT, 2084 unsigned PPCFastISel::PPCMaterializeInt(const ConstantInt *CI, MVT VT,
2049 bool UseSExt) { 2085 bool UseSExt) {
2050 // If we're using CR bit registers for i1 values, handle that as a special 2086 // If we're using CR bit registers for i1 values, handle that as a special
2051 // case first. 2087 // case first.
2052 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) { 2088 if (VT == MVT::i1 && PPCSubTarget->useCRBits()) {
2053 const ConstantInt *CI = cast<ConstantInt>(C);
2054 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass); 2089 unsigned ImmReg = createResultReg(&PPC::CRBITRCRegClass);
2055 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, 2090 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2056 TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg); 2091 TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
2057 return ImmReg; 2092 return ImmReg;
2058 } 2093 }
2063 2098
2064 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass : 2099 const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
2065 &PPC::GPRCRegClass); 2100 &PPC::GPRCRegClass);
2066 2101
2067 // If the constant is in range, use a load-immediate. 2102 // If the constant is in range, use a load-immediate.
2068 const ConstantInt *CI = cast<ConstantInt>(C); 2103 if (UseSExt && isInt<16>(CI->getSExtValue())) {
2069 if (isInt<16>(CI->getSExtValue())) {
2070 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI; 2104 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2071 unsigned ImmReg = createResultReg(RC); 2105 unsigned ImmReg = createResultReg(RC);
2072 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg) 2106 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
2073 .addImm( (UseSExt) ? CI->getSExtValue() : CI->getZExtValue() ); 2107 .addImm(CI->getSExtValue());
2108 return ImmReg;
2109 } else if (!UseSExt && isUInt<16>(CI->getZExtValue())) {
2110 unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
2111 unsigned ImmReg = createResultReg(RC);
2112 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ImmReg)
2113 .addImm(CI->getZExtValue());
2074 return ImmReg; 2114 return ImmReg;
2075 } 2115 }
2076 2116
2077 // Construct the constant piecewise. 2117 // Construct the constant piecewise.
2078 int64_t Imm = CI->getZExtValue(); 2118 int64_t Imm = CI->getZExtValue();
2086 } 2126 }
2087 2127
2088 // Materialize a constant into a register, and return the register 2128 // Materialize a constant into a register, and return the register
2089 // number (or zero if we failed to handle it). 2129 // number (or zero if we failed to handle it).
2090 unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) { 2130 unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
2091 EVT CEVT = TLI.getValueType(C->getType(), true); 2131 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
2092 2132
2093 // Only handle simple types. 2133 // Only handle simple types.
2094 if (!CEVT.isSimple()) return 0; 2134 if (!CEVT.isSimple()) return 0;
2095 MVT VT = CEVT.getSimpleVT(); 2135 MVT VT = CEVT.getSimpleVT();
2096 2136
2097 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) 2137 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
2098 return PPCMaterializeFP(CFP, VT); 2138 return PPCMaterializeFP(CFP, VT);
2099 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) 2139 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
2100 return PPCMaterializeGV(GV, VT); 2140 return PPCMaterializeGV(GV, VT);
2101 else if (isa<ConstantInt>(C)) 2141 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
2102 return PPCMaterializeInt(C, VT, VT != MVT::i1); 2142 return PPCMaterializeInt(CI, VT, VT != MVT::i1);
2103 2143
2104 return 0; 2144 return 0;
2105 } 2145 }
2106 2146
2107 // Materialize the address created by an alloca into a register, and 2147 // Materialize the address created by an alloca into a register, and