comparison lib/Target/PowerPC/PPCInstrInfo.cpp @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents 1172e4bd9c6f
children
comparison
equal deleted inserted replaced
120:1172e4bd9c6f 121:803732b1fca8
44 44
45 #define GET_INSTRMAP_INFO 45 #define GET_INSTRMAP_INFO
46 #define GET_INSTRINFO_CTOR_DTOR 46 #define GET_INSTRINFO_CTOR_DTOR
47 #include "PPCGenInstrInfo.inc" 47 #include "PPCGenInstrInfo.inc"
48 48
49 STATISTIC(NumStoreSPILLVSRRCAsVec,
50 "Number of spillvsrrc spilled to stack as vec");
51 STATISTIC(NumStoreSPILLVSRRCAsGpr,
52 "Number of spillvsrrc spilled to stack as gpr");
53 STATISTIC(NumGPRtoVSRSpill, "Number of gpr spills to spillvsrrc");
54
49 static cl:: 55 static cl::
50 opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden, 56 opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
51 cl::desc("Disable analysis for CTR loops")); 57 cl::desc("Disable analysis for CTR loops"));
52 58
53 static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt", 59 static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt",
63 69
64 // Pin the vtable to this file. 70 // Pin the vtable to this file.
65 void PPCInstrInfo::anchor() {} 71 void PPCInstrInfo::anchor() {}
66 72
67 PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI) 73 PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI)
68 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP), 74 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP,
75 /* CatchRetOpcode */ -1,
76 STI.isPPC64() ? PPC::BLR8 : PPC::BLR),
69 Subtarget(STI), RI(STI.getTargetMachine()) {} 77 Subtarget(STI), RI(STI.getTargetMachine()) {}
70 78
71 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for 79 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
72 /// this target when scheduling the DAG. 80 /// this target when scheduling the DAG.
73 ScheduleHazardRecognizer * 81 ScheduleHazardRecognizer *
250 unsigned &SrcReg, unsigned &DstReg, 258 unsigned &SrcReg, unsigned &DstReg,
251 unsigned &SubIdx) const { 259 unsigned &SubIdx) const {
252 switch (MI.getOpcode()) { 260 switch (MI.getOpcode()) {
253 default: return false; 261 default: return false;
254 case PPC::EXTSW: 262 case PPC::EXTSW:
263 case PPC::EXTSW_32:
255 case PPC::EXTSW_32_64: 264 case PPC::EXTSW_32_64:
256 SrcReg = MI.getOperand(1).getReg(); 265 SrcReg = MI.getOperand(1).getReg();
257 DstReg = MI.getOperand(0).getReg(); 266 DstReg = MI.getOperand(0).getReg();
258 SubIdx = PPC::sub_32; 267 SubIdx = PPC::sub_32;
259 return true; 268 return true;
271 case PPC::LFD: 280 case PPC::LFD:
272 case PPC::RESTORE_CR: 281 case PPC::RESTORE_CR:
273 case PPC::RESTORE_CRBIT: 282 case PPC::RESTORE_CRBIT:
274 case PPC::LVX: 283 case PPC::LVX:
275 case PPC::LXVD2X: 284 case PPC::LXVD2X:
276 case PPC::LXVX: 285 case PPC::LXV:
277 case PPC::QVLFDX: 286 case PPC::QVLFDX:
278 case PPC::QVLFSXs: 287 case PPC::QVLFSXs:
279 case PPC::QVLFDXb: 288 case PPC::QVLFDXb:
280 case PPC::RESTORE_VRSAVE: 289 case PPC::RESTORE_VRSAVE:
290 case PPC::SPILLTOVSR_LD:
281 // Check for the operands added by addFrameReference (the immediate is the 291 // Check for the operands added by addFrameReference (the immediate is the
282 // offset which defaults to 0). 292 // offset which defaults to 0).
283 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() && 293 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() &&
284 MI.getOperand(2).isFI()) { 294 MI.getOperand(2).isFI()) {
285 FrameIndex = MI.getOperand(2).getIndex(); 295 FrameIndex = MI.getOperand(2).getIndex();
286 return MI.getOperand(0).getReg(); 296 return MI.getOperand(0).getReg();
287 } 297 }
288 break; 298 break;
289 } 299 }
290 return 0; 300 return 0;
301 }
302
303 // For opcodes with the ReMaterializable flag set, this function is called to
304 // verify the instruction is really rematable.
305 bool PPCInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr &MI,
306 AliasAnalysis *AA) const {
307 switch (MI.getOpcode()) {
308 default:
309 // This function should only be called for opcodes with the ReMaterializable
310 // flag set.
311 llvm_unreachable("Unknown rematerializable operation!");
312 break;
313 case PPC::LI:
314 case PPC::LI8:
315 case PPC::LIS:
316 case PPC::LIS8:
317 case PPC::QVGPCI:
318 case PPC::ADDIStocHA:
319 case PPC::ADDItocL:
320 case PPC::LOAD_STACK_GUARD:
321 return true;
322 }
323 return false;
291 } 324 }
292 325
293 unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr &MI, 326 unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
294 int &FrameIndex) const { 327 int &FrameIndex) const {
295 // Note: This list must be kept consistent with StoreRegToStackSlot. 328 // Note: This list must be kept consistent with StoreRegToStackSlot.
301 case PPC::STFD: 334 case PPC::STFD:
302 case PPC::SPILL_CR: 335 case PPC::SPILL_CR:
303 case PPC::SPILL_CRBIT: 336 case PPC::SPILL_CRBIT:
304 case PPC::STVX: 337 case PPC::STVX:
305 case PPC::STXVD2X: 338 case PPC::STXVD2X:
306 case PPC::STXVX: 339 case PPC::STXV:
307 case PPC::QVSTFDX: 340 case PPC::QVSTFDX:
308 case PPC::QVSTFSXs: 341 case PPC::QVSTFSXs:
309 case PPC::QVSTFDXb: 342 case PPC::QVSTFDXb:
310 case PPC::SPILL_VRSAVE: 343 case PPC::SPILL_VRSAVE:
344 case PPC::SPILLTOVSR_ST:
311 // Check for the operands added by addFrameReference (the immediate is the 345 // Check for the operands added by addFrameReference (the immediate is the
312 // offset which defaults to 0). 346 // offset which defaults to 0).
313 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() && 347 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() &&
314 MI.getOperand(2).isFI()) { 348 MI.getOperand(2).isFI()) {
315 FrameIndex = MI.getOperand(2).getIndex(); 349 FrameIndex = MI.getOperand(2).getIndex();
436 470
437 DebugLoc DL; 471 DebugLoc DL;
438 BuildMI(MBB, MI, DL, get(Opcode)); 472 BuildMI(MBB, MI, DL, get(Opcode));
439 } 473 }
440 474
441 /// getNoopForMachoTarget - Return the noop instruction to use for a noop. 475 /// Return the noop instruction to use for a noop.
442 void PPCInstrInfo::getNoopForMachoTarget(MCInst &NopInst) const { 476 void PPCInstrInfo::getNoop(MCInst &NopInst) const {
443 NopInst.setOpcode(PPC::NOP); 477 NopInst.setOpcode(PPC::NOP);
444 } 478 }
445 479
446 // Branch analysis. 480 // Branch analysis.
447 // Note: If the condition register is set to CTR or CTR8 then this is a 481 // Note: If the condition register is set to CTR or CTR8 then this is a
458 if (I == MBB.end()) 492 if (I == MBB.end())
459 return false; 493 return false;
460 494
461 if (!isUnpredicatedTerminator(*I)) 495 if (!isUnpredicatedTerminator(*I))
462 return false; 496 return false;
497
498 if (AllowModify) {
499 // If the BB ends with an unconditional branch to the fallthrough BB,
500 // we eliminate the branch instruction.
501 if (I->getOpcode() == PPC::B &&
502 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
503 I->eraseFromParent();
504
505 // We update iterator after deleting the last branch.
506 I = MBB.getLastNonDebugInstr();
507 if (I == MBB.end() || !isUnpredicatedTerminator(*I))
508 return false;
509 }
510 }
463 511
464 // Get the last instruction in the block. 512 // Get the last instruction in the block.
465 MachineInstr &LastInst = *I; 513 MachineInstr &LastInst = *I;
466 514
467 // If there is only one terminator instruction, process it. 515 // If there is only one terminator instruction, process it.
660 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) 708 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
661 BuildMI(&MBB, DL, get(Cond[0].getImm() ? 709 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
662 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : 710 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
663 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); 711 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
664 else if (Cond[0].getImm() == PPC::PRED_BIT_SET) 712 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
665 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB); 713 BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB);
666 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) 714 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
667 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB); 715 BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB);
668 else // Conditional branch 716 else // Conditional branch
669 BuildMI(&MBB, DL, get(PPC::BCC)) 717 BuildMI(&MBB, DL, get(PPC::BCC))
670 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB); 718 .addImm(Cond[0].getImm())
719 .add(Cond[1])
720 .addMBB(TBB);
671 return 1; 721 return 1;
672 } 722 }
673 723
674 // Two-way Conditional Branch. 724 // Two-way Conditional Branch.
675 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) 725 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
676 BuildMI(&MBB, DL, get(Cond[0].getImm() ? 726 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
677 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : 727 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
678 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); 728 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
679 else if (Cond[0].getImm() == PPC::PRED_BIT_SET) 729 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
680 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB); 730 BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB);
681 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) 731 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
682 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB); 732 BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB);
683 else 733 else
684 BuildMI(&MBB, DL, get(PPC::BCC)) 734 BuildMI(&MBB, DL, get(PPC::BCC))
685 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB); 735 .addImm(Cond[0].getImm())
736 .add(Cond[1])
737 .addMBB(TBB);
686 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB); 738 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
687 return 2; 739 return 2;
688 } 740 }
689 741
690 // Select analysis. 742 // Select analysis.
691 bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB, 743 bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
692 ArrayRef<MachineOperand> Cond, 744 ArrayRef<MachineOperand> Cond,
693 unsigned TrueReg, unsigned FalseReg, 745 unsigned TrueReg, unsigned FalseReg,
694 int &CondCycles, int &TrueCycles, int &FalseCycles) const { 746 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
695 if (!Subtarget.hasISEL())
696 return false;
697
698 if (Cond.size() != 2) 747 if (Cond.size() != 2)
699 return false; 748 return false;
700 749
701 // If this is really a bdnz-like condition, then it cannot be turned into a 750 // If this is really a bdnz-like condition, then it cannot be turned into a
702 // select. 751 // select.
733 const DebugLoc &dl, unsigned DestReg, 782 const DebugLoc &dl, unsigned DestReg,
734 ArrayRef<MachineOperand> Cond, unsigned TrueReg, 783 ArrayRef<MachineOperand> Cond, unsigned TrueReg,
735 unsigned FalseReg) const { 784 unsigned FalseReg) const {
736 assert(Cond.size() == 2 && 785 assert(Cond.size() == 2 &&
737 "PPC branch conditions have two components!"); 786 "PPC branch conditions have two components!");
738
739 assert(Subtarget.hasISEL() &&
740 "Cannot insert select on target without ISEL support");
741 787
742 // Get the register classes. 788 // Get the register classes.
743 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 789 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
744 const TargetRegisterClass *RC = 790 const TargetRegisterClass *RC =
745 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); 791 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
892 } else if (PPC::CRRCRegClass.contains(SrcReg) && 938 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
893 PPC::GPRCRegClass.contains(DestReg)) { 939 PPC::GPRCRegClass.contains(DestReg)) {
894 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(SrcReg); 940 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(SrcReg);
895 getKillRegState(KillSrc); 941 getKillRegState(KillSrc);
896 return; 942 return;
897 } 943 } else if (PPC::G8RCRegClass.contains(SrcReg) &&
944 PPC::VSFRCRegClass.contains(DestReg)) {
945 BuildMI(MBB, I, DL, get(PPC::MTVSRD), DestReg).addReg(SrcReg);
946 NumGPRtoVSRSpill++;
947 getKillRegState(KillSrc);
948 return;
949 } else if (PPC::VSFRCRegClass.contains(SrcReg) &&
950 PPC::G8RCRegClass.contains(DestReg)) {
951 BuildMI(MBB, I, DL, get(PPC::MFVSRD), DestReg).addReg(SrcReg);
952 getKillRegState(KillSrc);
953 return;
954 }
898 955
899 unsigned Opc; 956 unsigned Opc;
900 if (PPC::GPRCRegClass.contains(DestReg, SrcReg)) 957 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
901 Opc = PPC::OR; 958 Opc = PPC::OR;
902 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg)) 959 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
990 .addReg(SrcReg, 1047 .addReg(SrcReg,
991 getKillRegState(isKill)), 1048 getKillRegState(isKill)),
992 FrameIdx)); 1049 FrameIdx));
993 NonRI = true; 1050 NonRI = true;
994 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { 1051 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
995 unsigned Op = Subtarget.hasP9Vector() ? PPC::STXVX : PPC::STXVD2X; 1052 unsigned Op = Subtarget.hasP9Vector() ? PPC::STXV : PPC::STXVD2X;
996 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op)) 1053 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op))
997 .addReg(SrcReg, 1054 .addReg(SrcReg,
998 getKillRegState(isKill)), 1055 getKillRegState(isKill)),
999 FrameIdx)); 1056 FrameIdx));
1000 NonRI = true; 1057 NonRI = true;
1036 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDXb)) 1093 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDXb))
1037 .addReg(SrcReg, 1094 .addReg(SrcReg,
1038 getKillRegState(isKill)), 1095 getKillRegState(isKill)),
1039 FrameIdx)); 1096 FrameIdx));
1040 NonRI = true; 1097 NonRI = true;
1098 } else if (PPC::SPILLTOVSRRCRegClass.hasSubClassEq(RC)) {
1099 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILLTOVSR_ST))
1100 .addReg(SrcReg,
1101 getKillRegState(isKill)),
1102 FrameIdx));
1041 } else { 1103 } else {
1042 llvm_unreachable("Unknown regclass!"); 1104 llvm_unreachable("Unknown regclass!");
1043 } 1105 }
1044 1106
1045 return false; 1107 return false;
1123 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { 1185 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
1124 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg), 1186 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
1125 FrameIdx)); 1187 FrameIdx));
1126 NonRI = true; 1188 NonRI = true;
1127 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { 1189 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
1128 unsigned Op = Subtarget.hasP9Vector() ? PPC::LXVX : PPC::LXVD2X; 1190 unsigned Op = Subtarget.hasP9Vector() ? PPC::LXV : PPC::LXVD2X;
1129 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op), DestReg), 1191 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op), DestReg),
1130 FrameIdx)); 1192 FrameIdx));
1131 NonRI = true; 1193 NonRI = true;
1132 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { 1194 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
1133 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFLOADf64 : PPC::LXSDX; 1195 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFLOADf64 : PPC::LXSDX;
1157 NonRI = true; 1219 NonRI = true;
1158 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) { 1220 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
1159 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDXb), DestReg), 1221 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDXb), DestReg),
1160 FrameIdx)); 1222 FrameIdx));
1161 NonRI = true; 1223 NonRI = true;
1224 } else if (PPC::SPILLTOVSRRCRegClass.hasSubClassEq(RC)) {
1225 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILLTOVSR_LD),
1226 DestReg), FrameIdx));
1162 } else { 1227 } else {
1163 llvm_unreachable("Unknown regclass!"); 1228 llvm_unreachable("Unknown regclass!");
1164 } 1229 }
1165 1230
1166 return false; 1231 return false;
1491 } 1556 }
1492 1557
1493 return Found; 1558 return Found;
1494 } 1559 }
1495 1560
1496 bool PPCInstrInfo::isPredicable(MachineInstr &MI) const { 1561 bool PPCInstrInfo::isPredicable(const MachineInstr &MI) const {
1497 unsigned OpC = MI.getOpcode(); 1562 unsigned OpC = MI.getOpcode();
1498 switch (OpC) { 1563 switch (OpC) {
1499 default: 1564 default:
1500 return false; 1565 return false;
1501 case PPC::B: 1566 case PPC::B:
1531 case PPC::CMPLD: 1596 case PPC::CMPLD:
1532 case PPC::FCMPUS: 1597 case PPC::FCMPUS:
1533 case PPC::FCMPUD: 1598 case PPC::FCMPUD:
1534 SrcReg = MI.getOperand(1).getReg(); 1599 SrcReg = MI.getOperand(1).getReg();
1535 SrcReg2 = MI.getOperand(2).getReg(); 1600 SrcReg2 = MI.getOperand(2).getReg();
1601 Value = 0;
1602 Mask = 0;
1536 return true; 1603 return true;
1537 } 1604 }
1538 } 1605 }
1539 1606
1540 bool PPCInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, unsigned SrcReg, 1607 bool PPCInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, unsigned SrcReg,
1565 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD; 1632 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1566 1633
1567 // Get the unique definition of SrcReg. 1634 // Get the unique definition of SrcReg.
1568 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg); 1635 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1569 if (!MI) return false; 1636 if (!MI) return false;
1570 int MIOpC = MI->getOpcode();
1571 1637
1572 bool equalityOnly = false; 1638 bool equalityOnly = false;
1573 bool noSub = false; 1639 bool noSub = false;
1574 if (isPPC64) { 1640 if (isPPC64) {
1575 if (is32BitSignedCompare) { 1641 if (is32BitSignedCompare) {
1576 // We can perform this optimization only if MI is sign-extending. 1642 // We can perform this optimization only if MI is sign-extending.
1577 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo || 1643 if (isSignExtended(*MI))
1578 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1579 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1580 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1581 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1582 noSub = true; 1644 noSub = true;
1583 } else 1645 else
1584 return false; 1646 return false;
1585 } else if (is32BitUnsignedCompare) { 1647 } else if (is32BitUnsignedCompare) {
1586 // 32-bit rotate and mask instructions are zero extending only if MB <= ME
1587 bool isZeroExtendingRotate =
1588 (MIOpC == PPC::RLWINM || MIOpC == PPC::RLWINMo ||
1589 MIOpC == PPC::RLWNM || MIOpC == PPC::RLWNMo)
1590 && MI->getOperand(3).getImm() <= MI->getOperand(4).getImm();
1591
1592 // We can perform this optimization, equality only, if MI is 1648 // We can perform this optimization, equality only, if MI is
1593 // zero-extending. 1649 // zero-extending.
1594 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo || 1650 if (isZeroExtended(*MI)) {
1595 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
1596 MIOpC == PPC::SRW || MIOpC == PPC::SRWo ||
1597 isZeroExtendingRotate) {
1598 noSub = true; 1651 noSub = true;
1599 equalityOnly = true; 1652 equalityOnly = true;
1600 } else 1653 } else
1601 return false; 1654 return false;
1602 } else 1655 } else
1605 equalityOnly = is32BitUnsignedCompare; 1658 equalityOnly = is32BitUnsignedCompare;
1606 1659
1607 if (equalityOnly) { 1660 if (equalityOnly) {
1608 // We need to check the uses of the condition register in order to reject 1661 // We need to check the uses of the condition register in order to reject
1609 // non-equality comparisons. 1662 // non-equality comparisons.
1610 for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg), 1663 for (MachineRegisterInfo::use_instr_iterator
1611 IE = MRI->use_instr_end(); I != IE; ++I) { 1664 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1665 I != IE; ++I) {
1612 MachineInstr *UseMI = &*I; 1666 MachineInstr *UseMI = &*I;
1613 if (UseMI->getOpcode() == PPC::BCC) { 1667 if (UseMI->getOpcode() == PPC::BCC) {
1614 unsigned Pred = UseMI->getOperand(0).getImm(); 1668 PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm();
1615 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE) 1669 unsigned PredCond = PPC::getPredicateCondition(Pred);
1670 // We ignore hint bits when checking for non-equality comparisons.
1671 if (PredCond != PPC::PRED_EQ && PredCond != PPC::PRED_NE)
1616 return false; 1672 return false;
1617 } else if (UseMI->getOpcode() == PPC::ISEL || 1673 } else if (UseMI->getOpcode() == PPC::ISEL ||
1618 UseMI->getOpcode() == PPC::ISEL8) { 1674 UseMI->getOpcode() == PPC::ISEL8) {
1619 unsigned SubIdx = UseMI->getOperand(3).getSubReg(); 1675 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
1620 if (SubIdx != PPC::sub_eq) 1676 if (SubIdx != PPC::sub_eq)
1628 1684
1629 // Scan forward to find the first use of the compare. 1685 // Scan forward to find the first use of the compare.
1630 for (MachineBasicBlock::iterator EL = CmpInstr.getParent()->end(); I != EL; 1686 for (MachineBasicBlock::iterator EL = CmpInstr.getParent()->end(); I != EL;
1631 ++I) { 1687 ++I) {
1632 bool FoundUse = false; 1688 bool FoundUse = false;
1633 for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg), 1689 for (MachineRegisterInfo::use_instr_iterator
1634 JE = MRI->use_instr_end(); J != JE; ++J) 1690 J = MRI->use_instr_begin(CRReg), JE = MRI->use_instr_end();
1691 J != JE; ++J)
1635 if (&*J == &*I) { 1692 if (&*J == &*I) {
1636 FoundUse = true; 1693 FoundUse = true;
1637 break; 1694 break;
1638 } 1695 }
1639 1696
1640 if (FoundUse) 1697 if (FoundUse)
1641 break; 1698 break;
1642 } 1699 }
1700
1701 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1702 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
1643 1703
1644 // There are two possible candidates which can be changed to set CR[01]. 1704 // There are two possible candidates which can be changed to set CR[01].
1645 // One is MI, the other is a SUB instruction. 1705 // One is MI, the other is a SUB instruction.
1646 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1). 1706 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
1647 MachineInstr *Sub = nullptr; 1707 MachineInstr *Sub = nullptr;
1650 MI = nullptr; 1710 MI = nullptr;
1651 // FIXME: Conservatively refuse to convert an instruction which isn't in the 1711 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1652 // same BB as the comparison. This is to allow the check below to avoid calls 1712 // same BB as the comparison. This is to allow the check below to avoid calls
1653 // (and other explicit clobbers); instead we should really check for these 1713 // (and other explicit clobbers); instead we should really check for these
1654 // more explicitly (in at least a few predecessors). 1714 // more explicitly (in at least a few predecessors).
1655 else if (MI->getParent() != CmpInstr.getParent() || Value != 0) { 1715 else if (MI->getParent() != CmpInstr.getParent())
1656 // PPC does not have a record-form SUBri. 1716 return false;
1657 return false; 1717 else if (Value != 0) {
1718 // The record-form instructions set CR bit based on signed comparison
1719 // against 0. We try to convert a compare against 1 or -1 into a compare
1720 // against 0 to exploit record-form instructions. For example, we change
1721 // the condition "greater than -1" into "greater than or equal to 0"
1722 // and "less than 1" into "less than or equal to 0".
1723
1724 // Since we optimize comparison based on a specific branch condition,
1725 // we don't optimize if condition code is used by more than once.
1726 if (equalityOnly || !MRI->hasOneUse(CRReg))
1727 return false;
1728
1729 MachineInstr *UseMI = &*MRI->use_instr_begin(CRReg);
1730 if (UseMI->getOpcode() != PPC::BCC)
1731 return false;
1732
1733 PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm();
1734 PPC::Predicate NewPred = Pred;
1735 unsigned PredCond = PPC::getPredicateCondition(Pred);
1736 unsigned PredHint = PPC::getPredicateHint(Pred);
1737 int16_t Immed = (int16_t)Value;
1738
1739 // When modyfing the condition in the predicate, we propagate hint bits
1740 // from the original predicate to the new one.
1741 if (Immed == -1 && PredCond == PPC::PRED_GT)
1742 // We convert "greater than -1" into "greater than or equal to 0",
1743 // since we are assuming signed comparison by !equalityOnly
1744 NewPred = PPC::getPredicate(PPC::PRED_GE, PredHint);
1745 else if (Immed == -1 && PredCond == PPC::PRED_LE)
1746 // We convert "less than or equal to -1" into "less than 0".
1747 NewPred = PPC::getPredicate(PPC::PRED_LT, PredHint);
1748 else if (Immed == 1 && PredCond == PPC::PRED_LT)
1749 // We convert "less than 1" into "less than or equal to 0".
1750 NewPred = PPC::getPredicate(PPC::PRED_LE, PredHint);
1751 else if (Immed == 1 && PredCond == PPC::PRED_GE)
1752 // We convert "greater than or equal to 1" into "greater than 0".
1753 NewPred = PPC::getPredicate(PPC::PRED_GT, PredHint);
1754 else
1755 return false;
1756
1757 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
1758 NewPred));
1658 } 1759 }
1659 1760
1660 // Search for Sub. 1761 // Search for Sub.
1661 const TargetRegisterInfo *TRI = &getRegisterInfo(); 1762 const TargetRegisterInfo *TRI = &getRegisterInfo();
1662 --I; 1763 --I;
1700 1801
1701 // The single candidate is called MI. 1802 // The single candidate is called MI.
1702 if (!MI) MI = Sub; 1803 if (!MI) MI = Sub;
1703 1804
1704 int NewOpC = -1; 1805 int NewOpC = -1;
1705 MIOpC = MI->getOpcode(); 1806 int MIOpC = MI->getOpcode();
1706 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8) 1807 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1707 NewOpC = MIOpC; 1808 NewOpC = MIOpC;
1708 else { 1809 else {
1709 NewOpC = PPC::getRecordFormOpcode(MIOpC); 1810 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1710 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1) 1811 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1718 // form). It is possible, however, that we'll need to reverse the condition 1819 // form). It is possible, however, that we'll need to reverse the condition
1719 // code of the users. 1820 // code of the users.
1720 if (NewOpC == -1) 1821 if (NewOpC == -1)
1721 return false; 1822 return false;
1722 1823
1723 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1724 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
1725
1726 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP 1824 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1727 // needs to be updated to be based on SUB. Push the condition code 1825 // needs to be updated to be based on SUB. Push the condition code
1728 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the 1826 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1729 // condition code of these operands will be modified. 1827 // condition code of these operands will be modified.
1828 // Here, Value == 0 means we haven't converted comparison against 1 or -1 to
1829 // comparison against 0, which may modify predicate.
1730 bool ShouldSwap = false; 1830 bool ShouldSwap = false;
1731 if (Sub) { 1831 if (Sub && Value == 0) {
1732 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 && 1832 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1733 Sub->getOperand(2).getReg() == SrcReg; 1833 Sub->getOperand(2).getReg() == SrcReg;
1734 1834
1735 // The operands to subf are the opposite of sub, so only in the fixed-point 1835 // The operands to subf are the opposite of sub, so only in the fixed-point
1736 // case, invert the order. 1836 // case, invert the order.
1742 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end(); 1842 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1743 I != IE; ++I) { 1843 I != IE; ++I) {
1744 MachineInstr *UseMI = &*I; 1844 MachineInstr *UseMI = &*I;
1745 if (UseMI->getOpcode() == PPC::BCC) { 1845 if (UseMI->getOpcode() == PPC::BCC) {
1746 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm(); 1846 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
1847 unsigned PredCond = PPC::getPredicateCondition(Pred);
1747 assert((!equalityOnly || 1848 assert((!equalityOnly ||
1748 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) && 1849 PredCond == PPC::PRED_EQ || PredCond == PPC::PRED_NE) &&
1749 "Invalid predicate for equality-only optimization"); 1850 "Invalid predicate for equality-only optimization");
1851 (void)PredCond; // To suppress warning in release build.
1750 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)), 1852 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
1751 PPC::getSwappedPredicate(Pred))); 1853 PPC::getSwappedPredicate(Pred)));
1752 } else if (UseMI->getOpcode() == PPC::ISEL || 1854 } else if (UseMI->getOpcode() == PPC::ISEL ||
1753 UseMI->getOpcode() == PPC::ISEL8) { 1855 UseMI->getOpcode() == PPC::ISEL8) {
1754 unsigned NewSubReg = UseMI->getOperand(3).getSubReg(); 1856 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1763 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)), 1865 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
1764 NewSubReg)); 1866 NewSubReg));
1765 } else // We need to abort on a user we don't understand. 1867 } else // We need to abort on a user we don't understand.
1766 return false; 1868 return false;
1767 } 1869 }
1870 assert(!(Value != 0 && ShouldSwap) &&
1871 "Non-zero immediate support and ShouldSwap"
1872 "may conflict in updating predicate");
1768 1873
1769 // Create a new virtual register to hold the value of the CR set by the 1874 // Create a new virtual register to hold the value of the CR set by the
1770 // record-form instruction. If the instruction was not previously in 1875 // record-form instruction. If the instruction was not previously in
1771 // record form, then set the kill flag on the CR. 1876 // record form, then set the kill flag on the CR.
1772 CmpInstr.eraseFromParent(); 1877 CmpInstr.eraseFromParent();
1834 return Opers.getNumPatchBytes(); 1939 return Opers.getNumPatchBytes();
1835 } else if (Opcode == TargetOpcode::PATCHPOINT) { 1940 } else if (Opcode == TargetOpcode::PATCHPOINT) {
1836 PatchPointOpers Opers(&MI); 1941 PatchPointOpers Opers(&MI);
1837 return Opers.getNumPatchBytes(); 1942 return Opers.getNumPatchBytes();
1838 } else { 1943 } else {
1839 const MCInstrDesc &Desc = get(Opcode); 1944 return get(Opcode).getSize();
1840 return Desc.getSize();
1841 } 1945 }
1842 } 1946 }
1843 1947
1844 std::pair<unsigned, unsigned> 1948 std::pair<unsigned, unsigned>
1845 PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const { 1949 PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
1872 {MO_NLP_HIDDEN_FLAG, "ppc-nlp-hidden"}}; 1976 {MO_NLP_HIDDEN_FLAG, "ppc-nlp-hidden"}};
1873 return makeArrayRef(TargetFlags); 1977 return makeArrayRef(TargetFlags);
1874 } 1978 }
1875 1979
1876 bool PPCInstrInfo::expandPostRAPseudo(MachineInstr &MI) const { 1980 bool PPCInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1981 auto &MBB = *MI.getParent();
1982 auto DL = MI.getDebugLoc();
1877 switch (MI.getOpcode()) { 1983 switch (MI.getOpcode()) {
1878 case TargetOpcode::LOAD_STACK_GUARD: { 1984 case TargetOpcode::LOAD_STACK_GUARD: {
1879 assert(Subtarget.isTargetLinux() && 1985 assert(Subtarget.isTargetLinux() &&
1880 "Only Linux target is expected to contain LOAD_STACK_GUARD"); 1986 "Only Linux target is expected to contain LOAD_STACK_GUARD");
1881 const int64_t Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008; 1987 const int64_t Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008;
1890 case PPC::DFLOADf64: 1996 case PPC::DFLOADf64:
1891 case PPC::DFSTOREf32: 1997 case PPC::DFSTOREf32:
1892 case PPC::DFSTOREf64: { 1998 case PPC::DFSTOREf64: {
1893 assert(Subtarget.hasP9Vector() && 1999 assert(Subtarget.hasP9Vector() &&
1894 "Invalid D-Form Pseudo-ops on non-P9 target."); 2000 "Invalid D-Form Pseudo-ops on non-P9 target.");
2001 assert(MI.getOperand(2).isReg() && MI.getOperand(1).isImm() &&
2002 "D-form op must have register and immediate operands");
1895 unsigned UpperOpcode, LowerOpcode; 2003 unsigned UpperOpcode, LowerOpcode;
1896 switch (MI.getOpcode()) { 2004 switch (MI.getOpcode()) {
1897 case PPC::DFLOADf32: 2005 case PPC::DFLOADf32:
1898 UpperOpcode = PPC::LXSSP; 2006 UpperOpcode = PPC::LXSSP;
1899 LowerOpcode = PPC::LFS; 2007 LowerOpcode = PPC::LFS;
1919 else 2027 else
1920 Opcode = UpperOpcode; 2028 Opcode = UpperOpcode;
1921 MI.setDesc(get(Opcode)); 2029 MI.setDesc(get(Opcode));
1922 return true; 2030 return true;
1923 } 2031 }
2032 case PPC::SPILLTOVSR_LD: {
2033 unsigned TargetReg = MI.getOperand(0).getReg();
2034 if (PPC::VSFRCRegClass.contains(TargetReg)) {
2035 MI.setDesc(get(PPC::DFLOADf64));
2036 return expandPostRAPseudo(MI);
2037 }
2038 else
2039 MI.setDesc(get(PPC::LD));
2040 return true;
2041 }
2042 case PPC::SPILLTOVSR_ST: {
2043 unsigned SrcReg = MI.getOperand(0).getReg();
2044 if (PPC::VSFRCRegClass.contains(SrcReg)) {
2045 NumStoreSPILLVSRRCAsVec++;
2046 MI.setDesc(get(PPC::DFSTOREf64));
2047 return expandPostRAPseudo(MI);
2048 } else {
2049 NumStoreSPILLVSRRCAsGpr++;
2050 MI.setDesc(get(PPC::STD));
2051 }
2052 return true;
2053 }
2054 case PPC::SPILLTOVSR_LDX: {
2055 unsigned TargetReg = MI.getOperand(0).getReg();
2056 if (PPC::VSFRCRegClass.contains(TargetReg))
2057 MI.setDesc(get(PPC::LXSDX));
2058 else
2059 MI.setDesc(get(PPC::LDX));
2060 return true;
2061 }
2062 case PPC::SPILLTOVSR_STX: {
2063 unsigned SrcReg = MI.getOperand(0).getReg();
2064 if (PPC::VSFRCRegClass.contains(SrcReg)) {
2065 NumStoreSPILLVSRRCAsVec++;
2066 MI.setDesc(get(PPC::STXSDX));
2067 } else {
2068 NumStoreSPILLVSRRCAsGpr++;
2069 MI.setDesc(get(PPC::STDX));
2070 }
2071 return true;
2072 }
2073
2074 case PPC::CFENCE8: {
2075 auto Val = MI.getOperand(0).getReg();
2076 BuildMI(MBB, MI, DL, get(PPC::CMPD), PPC::CR7).addReg(Val).addReg(Val);
2077 BuildMI(MBB, MI, DL, get(PPC::CTRL_DEP))
2078 .addImm(PPC::PRED_NE_MINUS)
2079 .addReg(PPC::CR7)
2080 .addImm(1);
2081 MI.setDesc(get(PPC::ISYNC));
2082 MI.RemoveOperand(0);
2083 return true;
2084 }
1924 } 2085 }
1925 return false; 2086 return false;
1926 } 2087 }
1927 2088
1928 const TargetRegisterClass * 2089 const TargetRegisterClass *
1929 PPCInstrInfo::updatedRC(const TargetRegisterClass *RC) const { 2090 PPCInstrInfo::updatedRC(const TargetRegisterClass *RC) const {
1930 if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass) 2091 if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass)
1931 return &PPC::VSRCRegClass; 2092 return &PPC::VSRCRegClass;
1932 return RC; 2093 return RC;
1933 } 2094 }
2095
2096 int PPCInstrInfo::getRecordFormOpcode(unsigned Opcode) {
2097 return PPC::getRecordFormOpcode(Opcode);
2098 }
2099
2100 // This function returns true if the machine instruction
2101 // always outputs a value by sign-extending a 32 bit value,
2102 // i.e. 0 to 31-th bits are same as 32-th bit.
2103 static bool isSignExtendingOp(const MachineInstr &MI) {
2104 int Opcode = MI.getOpcode();
2105 if (Opcode == PPC::LI || Opcode == PPC::LI8 ||
2106 Opcode == PPC::LIS || Opcode == PPC::LIS8 ||
2107 Opcode == PPC::SRAW || Opcode == PPC::SRAWo ||
2108 Opcode == PPC::SRAWI || Opcode == PPC::SRAWIo ||
2109 Opcode == PPC::LWA || Opcode == PPC::LWAX ||
2110 Opcode == PPC::LWA_32 || Opcode == PPC::LWAX_32 ||
2111 Opcode == PPC::LHA || Opcode == PPC::LHAX ||
2112 Opcode == PPC::LHA8 || Opcode == PPC::LHAX8 ||
2113 Opcode == PPC::LBZ || Opcode == PPC::LBZX ||
2114 Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 ||
2115 Opcode == PPC::LBZU || Opcode == PPC::LBZUX ||
2116 Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 ||
2117 Opcode == PPC::LHZ || Opcode == PPC::LHZX ||
2118 Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 ||
2119 Opcode == PPC::LHZU || Opcode == PPC::LHZUX ||
2120 Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8 ||
2121 Opcode == PPC::EXTSB || Opcode == PPC::EXTSBo ||
2122 Opcode == PPC::EXTSH || Opcode == PPC::EXTSHo ||
2123 Opcode == PPC::EXTSB8 || Opcode == PPC::EXTSH8 ||
2124 Opcode == PPC::EXTSW || Opcode == PPC::EXTSWo ||
2125 Opcode == PPC::EXTSH8_32_64 || Opcode == PPC::EXTSW_32_64 ||
2126 Opcode == PPC::EXTSB8_32_64)
2127 return true;
2128
2129 if (Opcode == PPC::RLDICL && MI.getOperand(3).getImm() >= 33)
2130 return true;
2131
2132 if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINMo ||
2133 Opcode == PPC::RLWNM || Opcode == PPC::RLWNMo) &&
2134 MI.getOperand(3).getImm() > 0 &&
2135 MI.getOperand(3).getImm() <= MI.getOperand(4).getImm())
2136 return true;
2137
2138 return false;
2139 }
2140
2141 // This function returns true if the machine instruction
2142 // always outputs zeros in higher 32 bits.
2143 static bool isZeroExtendingOp(const MachineInstr &MI) {
2144 int Opcode = MI.getOpcode();
2145 // The 16-bit immediate is sign-extended in li/lis.
2146 // If the most significant bit is zero, all higher bits are zero.
2147 if (Opcode == PPC::LI || Opcode == PPC::LI8 ||
2148 Opcode == PPC::LIS || Opcode == PPC::LIS8) {
2149 int64_t Imm = MI.getOperand(1).getImm();
2150 if (((uint64_t)Imm & ~0x7FFFuLL) == 0)
2151 return true;
2152 }
2153
2154 // We have some variations of rotate-and-mask instructions
2155 // that clear higher 32-bits.
2156 if ((Opcode == PPC::RLDICL || Opcode == PPC::RLDICLo ||
2157 Opcode == PPC::RLDCL || Opcode == PPC::RLDCLo ||
2158 Opcode == PPC::RLDICL_32_64) &&
2159 MI.getOperand(3).getImm() >= 32)
2160 return true;
2161
2162 if ((Opcode == PPC::RLDIC || Opcode == PPC::RLDICo) &&
2163 MI.getOperand(3).getImm() >= 32 &&
2164 MI.getOperand(3).getImm() <= 63 - MI.getOperand(2).getImm())
2165 return true;
2166
2167 if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINMo ||
2168 Opcode == PPC::RLWNM || Opcode == PPC::RLWNMo ||
2169 Opcode == PPC::RLWINM8 || Opcode == PPC::RLWNM8) &&
2170 MI.getOperand(3).getImm() <= MI.getOperand(4).getImm())
2171 return true;
2172
2173 // There are other instructions that clear higher 32-bits.
2174 if (Opcode == PPC::CNTLZW || Opcode == PPC::CNTLZWo ||
2175 Opcode == PPC::CNTTZW || Opcode == PPC::CNTTZWo ||
2176 Opcode == PPC::CNTLZW8 || Opcode == PPC::CNTTZW8 ||
2177 Opcode == PPC::CNTLZD || Opcode == PPC::CNTLZDo ||
2178 Opcode == PPC::CNTTZD || Opcode == PPC::CNTTZDo ||
2179 Opcode == PPC::POPCNTD || Opcode == PPC::POPCNTW ||
2180 Opcode == PPC::SLW || Opcode == PPC::SLWo ||
2181 Opcode == PPC::SRW || Opcode == PPC::SRWo ||
2182 Opcode == PPC::SLW8 || Opcode == PPC::SRW8 ||
2183 Opcode == PPC::SLWI || Opcode == PPC::SLWIo ||
2184 Opcode == PPC::SRWI || Opcode == PPC::SRWIo ||
2185 Opcode == PPC::LWZ || Opcode == PPC::LWZX ||
2186 Opcode == PPC::LWZU || Opcode == PPC::LWZUX ||
2187 Opcode == PPC::LWBRX || Opcode == PPC::LHBRX ||
2188 Opcode == PPC::LHZ || Opcode == PPC::LHZX ||
2189 Opcode == PPC::LHZU || Opcode == PPC::LHZUX ||
2190 Opcode == PPC::LBZ || Opcode == PPC::LBZX ||
2191 Opcode == PPC::LBZU || Opcode == PPC::LBZUX ||
2192 Opcode == PPC::LWZ8 || Opcode == PPC::LWZX8 ||
2193 Opcode == PPC::LWZU8 || Opcode == PPC::LWZUX8 ||
2194 Opcode == PPC::LWBRX8 || Opcode == PPC::LHBRX8 ||
2195 Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 ||
2196 Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8 ||
2197 Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 ||
2198 Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 ||
2199 Opcode == PPC::ANDIo || Opcode == PPC::ANDISo ||
2200 Opcode == PPC::ROTRWI || Opcode == PPC::ROTRWIo ||
2201 Opcode == PPC::EXTLWI || Opcode == PPC::EXTLWIo ||
2202 Opcode == PPC::MFVSRWZ)
2203 return true;
2204
2205 return false;
2206 }
2207
2208 // We limit the max depth to track incoming values of PHIs or binary ops
2209 // (e.g. AND) to avoid exsessive cost.
2210 const unsigned MAX_DEPTH = 1;
2211
2212 bool
2213 PPCInstrInfo::isSignOrZeroExtended(const MachineInstr &MI, bool SignExt,
2214 const unsigned Depth) const {
2215 const MachineFunction *MF = MI.getParent()->getParent();
2216 const MachineRegisterInfo *MRI = &MF->getRegInfo();
2217
2218 // If we know this instruction returns sign- or zero-extended result,
2219 // return true.
2220 if (SignExt ? isSignExtendingOp(MI):
2221 isZeroExtendingOp(MI))
2222 return true;
2223
2224 switch (MI.getOpcode()) {
2225 case PPC::COPY: {
2226 unsigned SrcReg = MI.getOperand(1).getReg();
2227
2228 // In both ELFv1 and v2 ABI, method parameters and the return value
2229 // are sign- or zero-extended.
2230 if (MF->getSubtarget<PPCSubtarget>().isSVR4ABI()) {
2231 const PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
2232 // We check the ZExt/SExt flags for a method parameter.
2233 if (MI.getParent()->getBasicBlock() ==
2234 &MF->getFunction()->getEntryBlock()) {
2235 unsigned VReg = MI.getOperand(0).getReg();
2236 if (MF->getRegInfo().isLiveIn(VReg))
2237 return SignExt ? FuncInfo->isLiveInSExt(VReg) :
2238 FuncInfo->isLiveInZExt(VReg);
2239 }
2240
2241 // For a method return value, we check the ZExt/SExt flags in attribute.
2242 // We assume the following code sequence for method call.
2243 // ADJCALLSTACKDOWN 32, %R1<imp-def,dead>, %R1<imp-use>
2244 // BL8_NOP <ga:@func>,...
2245 // ADJCALLSTACKUP 32, 0, %R1<imp-def,dead>, %R1<imp-use>
2246 // %vreg5<def> = COPY %X3; G8RC:%vreg5
2247 if (SrcReg == PPC::X3) {
2248 const MachineBasicBlock *MBB = MI.getParent();
2249 MachineBasicBlock::const_instr_iterator II =
2250 MachineBasicBlock::const_instr_iterator(&MI);
2251 if (II != MBB->instr_begin() &&
2252 (--II)->getOpcode() == PPC::ADJCALLSTACKUP) {
2253 const MachineInstr &CallMI = *(--II);
2254 if (CallMI.isCall() && CallMI.getOperand(0).isGlobal()) {
2255 const Function *CalleeFn =
2256 dyn_cast<Function>(CallMI.getOperand(0).getGlobal());
2257 if (!CalleeFn)
2258 return false;
2259 const IntegerType *IntTy =
2260 dyn_cast<IntegerType>(CalleeFn->getReturnType());
2261 const AttributeSet &Attrs =
2262 CalleeFn->getAttributes().getRetAttributes();
2263 if (IntTy && IntTy->getBitWidth() <= 32)
2264 return Attrs.hasAttribute(SignExt ? Attribute::SExt :
2265 Attribute::ZExt);
2266 }
2267 }
2268 }
2269 }
2270
2271 // If this is a copy from another register, we recursively check source.
2272 if (!TargetRegisterInfo::isVirtualRegister(SrcReg))
2273 return false;
2274 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
2275 if (SrcMI != NULL)
2276 return isSignOrZeroExtended(*SrcMI, SignExt, Depth);
2277
2278 return false;
2279 }
2280
2281 case PPC::ANDIo:
2282 case PPC::ANDISo:
2283 case PPC::ORI:
2284 case PPC::ORIS:
2285 case PPC::XORI:
2286 case PPC::XORIS:
2287 case PPC::ANDIo8:
2288 case PPC::ANDISo8:
2289 case PPC::ORI8:
2290 case PPC::ORIS8:
2291 case PPC::XORI8:
2292 case PPC::XORIS8: {
2293 // logical operation with 16-bit immediate does not change the upper bits.
2294 // So, we track the operand register as we do for register copy.
2295 unsigned SrcReg = MI.getOperand(1).getReg();
2296 if (!TargetRegisterInfo::isVirtualRegister(SrcReg))
2297 return false;
2298 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
2299 if (SrcMI != NULL)
2300 return isSignOrZeroExtended(*SrcMI, SignExt, Depth);
2301
2302 return false;
2303 }
2304
2305 // If all incoming values are sign-/zero-extended,
2306 // the output of AND, OR, ISEL or PHI is also sign-/zero-extended.
2307 case PPC::AND:
2308 case PPC::AND8:
2309 case PPC::OR:
2310 case PPC::OR8:
2311 case PPC::ISEL:
2312 case PPC::PHI: {
2313 if (Depth >= MAX_DEPTH)
2314 return false;
2315
2316 // The input registers for PHI are operand 1, 3, ...
2317 // The input registers for others are operand 1 and 2.
2318 unsigned E = 3, D = 1;
2319 if (MI.getOpcode() == PPC::PHI) {
2320 E = MI.getNumOperands();
2321 D = 2;
2322 }
2323
2324 for (unsigned I = 1; I != E; I += D) {
2325 if (MI.getOperand(I).isReg()) {
2326 unsigned SrcReg = MI.getOperand(I).getReg();
2327 if (!TargetRegisterInfo::isVirtualRegister(SrcReg))
2328 return false;
2329 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
2330 if (SrcMI == NULL || !isSignOrZeroExtended(*SrcMI, SignExt, Depth+1))
2331 return false;
2332 }
2333 else
2334 return false;
2335 }
2336 return true;
2337 }
2338
2339 default:
2340 break;
2341 }
2342 return false;
2343 }