Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- PPCInstrInfo.cpp - PowerPC Instruction Information ----------------===// |
Misha Brukman | b440243 | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2 | // |
Misha Brukman | 116f927 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b440243 | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Misha Brukman | 116f927 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the PowerPC implementation of the TargetInstrInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 6f3b954 | 2005-10-14 23:59:06 +0000 | [diff] [blame] | 14 | #include "PPCInstrInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/PPCPredicates.h" |
Evan Cheng | c5e6d2f | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 16 | #include "PPC.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "PPCHazardRecognizers.h" |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 18 | #include "PPCInstrBuilder.h" |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 19 | #include "PPCMachineFunctionInfo.h" |
Chris Lattner | 49cadab | 2006-06-17 00:01:04 +0000 | [diff] [blame] | 20 | #include "PPCTargetMachine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
Hal Finkel | 174e590 | 2014-03-25 23:29:21 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Misha Brukman | 116f927 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineMemOperand.h" |
Jakob Stoklund Olesen | ddbf7a8 | 2010-02-26 21:09:24 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Hal Finkel | 9f9f892 | 2012-04-01 19:22:40 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/ScheduleDAG.h" |
Hal Finkel | 174e590 | 2014-03-25 23:29:21 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/SlotIndexes.h" |
Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/StackMaps.h" |
Evan Cheng | c5e6d2f | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCAsmInfo.h" |
Pete Cooper | 3de83e4 | 2015-05-15 21:58:42 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCInst.h" |
Bill Wendling | 1af20ad | 2008-03-04 23:13:51 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CommandLine.h" |
Hal Finkel | 174e590 | 2014-03-25 23:29:21 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Debug.h" |
Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 37 | #include "llvm/Support/ErrorHandling.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 38 | #include "llvm/Support/TargetRegistry.h" |
Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 39 | #include "llvm/Support/raw_ostream.h" |
Misha Brukman | 116f927 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 40 | |
Dan Gohman | 2085719 | 2010-04-15 17:20:57 +0000 | [diff] [blame] | 41 | using namespace llvm; |
Bill Wendling | 1af20ad | 2008-03-04 23:13:51 +0000 | [diff] [blame] | 42 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 43 | #define DEBUG_TYPE "ppc-instr-info" |
| 44 | |
Chandler Carruth | d174b72 | 2014-04-22 02:03:14 +0000 | [diff] [blame] | 45 | #define GET_INSTRMAP_INFO |
| 46 | #define GET_INSTRINFO_CTOR_DTOR |
| 47 | #include "PPCGenInstrInfo.inc" |
| 48 | |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 49 | static cl:: |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 50 | opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden, |
| 51 | cl::desc("Disable analysis for CTR loops")); |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 52 | |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 53 | static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt", |
Hal Finkel | b12da6b | 2013-04-18 22:54:25 +0000 | [diff] [blame] | 54 | cl::desc("Disable compare instruction optimization"), cl::Hidden); |
| 55 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 56 | static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy", |
| 57 | cl::desc("Causes the backend to crash instead of generating a nop VSX copy"), |
| 58 | cl::Hidden); |
| 59 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 60 | // Pin the vtable to this file. |
| 61 | void PPCInstrInfo::anchor() {} |
| 62 | |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 63 | PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI) |
| 64 | : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP), |
Eric Christopher | ea178cf | 2015-03-12 01:42:51 +0000 | [diff] [blame] | 65 | Subtarget(STI), RI(STI.getTargetMachine()) {} |
Chris Lattner | 49cadab | 2006-06-17 00:01:04 +0000 | [diff] [blame] | 66 | |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 67 | /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for |
| 68 | /// this target when scheduling the DAG. |
Eric Christopher | f047bfd | 2014-06-13 22:38:52 +0000 | [diff] [blame] | 69 | ScheduleHazardRecognizer * |
| 70 | PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI, |
| 71 | const ScheduleDAG *DAG) const { |
| 72 | unsigned Directive = |
| 73 | static_cast<const PPCSubtarget *>(STI)->getDarwinDirective(); |
Hal Finkel | 742b535 | 2012-08-28 16:12:39 +0000 | [diff] [blame] | 74 | if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 || |
| 75 | Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) { |
Eric Christopher | f047bfd | 2014-06-13 22:38:52 +0000 | [diff] [blame] | 76 | const InstrItineraryData *II = |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 77 | static_cast<const PPCSubtarget *>(STI)->getInstrItineraryData(); |
Hal Finkel | 563cc05 | 2013-12-02 23:52:46 +0000 | [diff] [blame] | 78 | return new ScoreboardHazardRecognizer(II, DAG); |
Hal Finkel | 6fa5697 | 2011-10-17 04:03:49 +0000 | [diff] [blame] | 79 | } |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 80 | |
Eric Christopher | f047bfd | 2014-06-13 22:38:52 +0000 | [diff] [blame] | 81 | return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG); |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 84 | /// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer |
| 85 | /// to use for this target when scheduling the DAG. |
Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 86 | ScheduleHazardRecognizer * |
| 87 | PPCInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, |
| 88 | const ScheduleDAG *DAG) const { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 89 | unsigned Directive = |
Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 90 | DAG->MF.getSubtarget<PPCSubtarget>().getDarwinDirective(); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 91 | |
Will Schmidt | 970ff64 | 2014-06-26 13:36:19 +0000 | [diff] [blame] | 92 | if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8) |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 93 | return new PPCDispatchGroupSBHazardRecognizer(II, DAG); |
| 94 | |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 95 | // Most subtargets use a PPC970 recognizer. |
Hal Finkel | 742b535 | 2012-08-28 16:12:39 +0000 | [diff] [blame] | 96 | if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 && |
| 97 | Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 98 | assert(DAG->TII && "No InstrInfo?"); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 99 | |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 100 | return new PPCHazardRecognizer970(*DAG); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Hal Finkel | 563cc05 | 2013-12-02 23:52:46 +0000 | [diff] [blame] | 103 | return new ScoreboardHazardRecognizer(II, DAG); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 104 | } |
Jakob Stoklund Olesen | 0f855e4 | 2012-06-19 21:14:34 +0000 | [diff] [blame] | 105 | |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 106 | |
| 107 | int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, |
| 108 | const MachineInstr *DefMI, unsigned DefIdx, |
| 109 | const MachineInstr *UseMI, |
| 110 | unsigned UseIdx) const { |
| 111 | int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx, |
| 112 | UseMI, UseIdx); |
| 113 | |
| 114 | const MachineOperand &DefMO = DefMI->getOperand(DefIdx); |
| 115 | unsigned Reg = DefMO.getReg(); |
| 116 | |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 117 | bool IsRegCR; |
Andrew Kaylor | 5c73e1f | 2015-03-24 23:37:10 +0000 | [diff] [blame] | 118 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 119 | const MachineRegisterInfo *MRI = |
| 120 | &DefMI->getParent()->getParent()->getRegInfo(); |
| 121 | IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) || |
| 122 | MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass); |
| 123 | } else { |
| 124 | IsRegCR = PPC::CRRCRegClass.contains(Reg) || |
| 125 | PPC::CRBITRCRegClass.contains(Reg); |
| 126 | } |
| 127 | |
| 128 | if (UseMI->isBranch() && IsRegCR) { |
| 129 | if (Latency < 0) |
| 130 | Latency = getInstrLatency(ItinData, DefMI); |
| 131 | |
| 132 | // On some cores, there is an additional delay between writing to a condition |
| 133 | // register, and using it from a branch. |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 134 | unsigned Directive = Subtarget.getDarwinDirective(); |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 135 | switch (Directive) { |
| 136 | default: break; |
| 137 | case PPC::DIR_7400: |
| 138 | case PPC::DIR_750: |
| 139 | case PPC::DIR_970: |
| 140 | case PPC::DIR_E5500: |
| 141 | case PPC::DIR_PWR4: |
| 142 | case PPC::DIR_PWR5: |
| 143 | case PPC::DIR_PWR5X: |
| 144 | case PPC::DIR_PWR6: |
| 145 | case PPC::DIR_PWR6X: |
| 146 | case PPC::DIR_PWR7: |
Will Schmidt | 970ff64 | 2014-06-26 13:36:19 +0000 | [diff] [blame] | 147 | case PPC::DIR_PWR8: |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 148 | Latency += 2; |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return Latency; |
| 154 | } |
| 155 | |
Jakob Stoklund Olesen | 0f855e4 | 2012-06-19 21:14:34 +0000 | [diff] [blame] | 156 | // Detect 32 -> 64-bit extensions where we may reuse the low sub-register. |
| 157 | bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI, |
| 158 | unsigned &SrcReg, unsigned &DstReg, |
| 159 | unsigned &SubIdx) const { |
| 160 | switch (MI.getOpcode()) { |
| 161 | default: return false; |
| 162 | case PPC::EXTSW: |
| 163 | case PPC::EXTSW_32_64: |
| 164 | SrcReg = MI.getOperand(1).getReg(); |
| 165 | DstReg = MI.getOperand(0).getReg(); |
| 166 | SubIdx = PPC::sub_32; |
| 167 | return true; |
| 168 | } |
| 169 | } |
| 170 | |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 171 | unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI, |
Chris Lattner | 91400bd | 2006-03-16 22:24:02 +0000 | [diff] [blame] | 172 | int &FrameIndex) const { |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 173 | // Note: This list must be kept consistent with LoadRegFromStackSlot. |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 174 | switch (MI->getOpcode()) { |
| 175 | default: break; |
| 176 | case PPC::LD: |
| 177 | case PPC::LWZ: |
| 178 | case PPC::LFS: |
| 179 | case PPC::LFD: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 180 | case PPC::RESTORE_CR: |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 181 | case PPC::RESTORE_CRBIT: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 182 | case PPC::LVX: |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 183 | case PPC::LXVD2X: |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 184 | case PPC::QVLFDX: |
| 185 | case PPC::QVLFSXs: |
| 186 | case PPC::QVLFDXb: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 187 | case PPC::RESTORE_VRSAVE: |
| 188 | // Check for the operands added by addFrameReference (the immediate is the |
| 189 | // offset which defaults to 0). |
Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 190 | if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() && |
| 191 | MI->getOperand(2).isFI()) { |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 192 | FrameIndex = MI->getOperand(2).getIndex(); |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 193 | return MI->getOperand(0).getReg(); |
| 194 | } |
| 195 | break; |
| 196 | } |
| 197 | return 0; |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 198 | } |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 199 | |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 200 | unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI, |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 201 | int &FrameIndex) const { |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 202 | // Note: This list must be kept consistent with StoreRegToStackSlot. |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 203 | switch (MI->getOpcode()) { |
| 204 | default: break; |
Nate Begeman | 4efb328 | 2006-02-02 21:07:50 +0000 | [diff] [blame] | 205 | case PPC::STD: |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 206 | case PPC::STW: |
| 207 | case PPC::STFS: |
| 208 | case PPC::STFD: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 209 | case PPC::SPILL_CR: |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 210 | case PPC::SPILL_CRBIT: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 211 | case PPC::STVX: |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 212 | case PPC::STXVD2X: |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 213 | case PPC::QVSTFDX: |
| 214 | case PPC::QVSTFSXs: |
| 215 | case PPC::QVSTFDXb: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 216 | case PPC::SPILL_VRSAVE: |
| 217 | // Check for the operands added by addFrameReference (the immediate is the |
| 218 | // offset which defaults to 0). |
Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 219 | if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() && |
| 220 | MI->getOperand(2).isFI()) { |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 221 | FrameIndex = MI->getOperand(2).getIndex(); |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 222 | return MI->getOperand(0).getReg(); |
| 223 | } |
| 224 | break; |
| 225 | } |
| 226 | return 0; |
| 227 | } |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 228 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 229 | // commuteInstruction - We can commute rlwimi instructions, but only if the |
| 230 | // rotate amt is zero. We also have to munge the immediates a bit. |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 231 | MachineInstr * |
| 232 | PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 233 | MachineFunction &MF = *MI->getParent()->getParent(); |
| 234 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 235 | // Normal instructions can be commuted the obvious way. |
Hal Finkel | 654d43b | 2013-04-12 02:18:09 +0000 | [diff] [blame] | 236 | if (MI->getOpcode() != PPC::RLWIMI && |
Hal Finkel | 4c6658f | 2014-12-12 23:59:36 +0000 | [diff] [blame] | 237 | MI->getOpcode() != PPC::RLWIMIo) |
Jakob Stoklund Olesen | 9de596e | 2012-11-28 02:35:17 +0000 | [diff] [blame] | 238 | return TargetInstrInfo::commuteInstruction(MI, NewMI); |
Hal Finkel | 4c6658f | 2014-12-12 23:59:36 +0000 | [diff] [blame] | 239 | // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a |
| 240 | // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because |
| 241 | // changing the relative order of the mask operands might change what happens |
| 242 | // to the high-bits of the mask (and, thus, the result). |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 243 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 244 | // Cannot commute if it has a non-zero rotate count. |
Chris Lattner | 5c46378 | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 245 | if (MI->getOperand(3).getImm() != 0) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 246 | return nullptr; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 247 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 248 | // If we have a zero rotate count, we have: |
| 249 | // M = mask(MB,ME) |
| 250 | // Op0 = (Op1 & ~M) | (Op2 & M) |
| 251 | // Change this to: |
| 252 | // M = mask((ME+1)&31, (MB-1)&31) |
| 253 | // Op0 = (Op2 & ~M) | (Op1 & M) |
| 254 | |
| 255 | // Swap op1/op2 |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 256 | unsigned Reg0 = MI->getOperand(0).getReg(); |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 257 | unsigned Reg1 = MI->getOperand(1).getReg(); |
| 258 | unsigned Reg2 = MI->getOperand(2).getReg(); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 259 | unsigned SubReg1 = MI->getOperand(1).getSubReg(); |
| 260 | unsigned SubReg2 = MI->getOperand(2).getSubReg(); |
Evan Cheng | dc2c874 | 2006-11-15 20:58:11 +0000 | [diff] [blame] | 261 | bool Reg1IsKill = MI->getOperand(1).isKill(); |
| 262 | bool Reg2IsKill = MI->getOperand(2).isKill(); |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 263 | bool ChangeReg0 = false; |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 264 | // If machine instrs are no longer in two-address forms, update |
| 265 | // destination register as well. |
| 266 | if (Reg0 == Reg1) { |
| 267 | // Must be two address instruction! |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 268 | assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) && |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 269 | "Expecting a two-address instruction!"); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 270 | assert(MI->getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch"); |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 271 | Reg2IsKill = false; |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 272 | ChangeReg0 = true; |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 273 | } |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 274 | |
| 275 | // Masks. |
| 276 | unsigned MB = MI->getOperand(4).getImm(); |
| 277 | unsigned ME = MI->getOperand(5).getImm(); |
| 278 | |
| 279 | if (NewMI) { |
| 280 | // Create a new instruction. |
| 281 | unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg(); |
| 282 | bool Reg0IsDead = MI->getOperand(0).isDead(); |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 283 | return BuildMI(MF, MI->getDebugLoc(), MI->getDesc()) |
Bill Wendling | f7b83c7 | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 284 | .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead)) |
| 285 | .addReg(Reg2, getKillRegState(Reg2IsKill)) |
| 286 | .addReg(Reg1, getKillRegState(Reg1IsKill)) |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 287 | .addImm((ME+1) & 31) |
| 288 | .addImm((MB-1) & 31); |
| 289 | } |
| 290 | |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 291 | if (ChangeReg0) { |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 292 | MI->getOperand(0).setReg(Reg2); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 293 | MI->getOperand(0).setSubReg(SubReg2); |
| 294 | } |
Chris Lattner | 10d6341 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 295 | MI->getOperand(2).setReg(Reg1); |
| 296 | MI->getOperand(1).setReg(Reg2); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 297 | MI->getOperand(2).setSubReg(SubReg1); |
| 298 | MI->getOperand(1).setSubReg(SubReg2); |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 299 | MI->getOperand(2).setIsKill(Reg1IsKill); |
| 300 | MI->getOperand(1).setIsKill(Reg2IsKill); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 301 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 302 | // Swap the mask around. |
Chris Lattner | 5c46378 | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 303 | MI->getOperand(4).setImm((ME+1) & 31); |
| 304 | MI->getOperand(5).setImm((MB-1) & 31); |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 305 | return MI; |
| 306 | } |
Chris Lattner | ea79d9fd73 | 2006-03-05 23:49:55 +0000 | [diff] [blame] | 307 | |
Hal Finkel | 6c32ff3 | 2014-03-25 19:26:43 +0000 | [diff] [blame] | 308 | bool PPCInstrInfo::findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1, |
| 309 | unsigned &SrcOpIdx2) const { |
| 310 | // For VSX A-Type FMA instructions, it is the first two operands that can be |
| 311 | // commuted, however, because the non-encoded tied input operand is listed |
| 312 | // first, the operands to swap are actually the second and third. |
| 313 | |
| 314 | int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode()); |
| 315 | if (AltOpc == -1) |
| 316 | return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2); |
| 317 | |
| 318 | SrcOpIdx1 = 2; |
| 319 | SrcOpIdx2 = 3; |
| 320 | return true; |
| 321 | } |
| 322 | |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 323 | void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB, |
Chris Lattner | ea79d9fd73 | 2006-03-05 23:49:55 +0000 | [diff] [blame] | 324 | MachineBasicBlock::iterator MI) const { |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 325 | // This function is used for scheduling, and the nop wanted here is the type |
| 326 | // that terminates dispatch groups on the POWER cores. |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 327 | unsigned Directive = Subtarget.getDarwinDirective(); |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 328 | unsigned Opcode; |
| 329 | switch (Directive) { |
| 330 | default: Opcode = PPC::NOP; break; |
| 331 | case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break; |
| 332 | case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break; |
Will Schmidt | 970ff64 | 2014-06-26 13:36:19 +0000 | [diff] [blame] | 333 | case PPC::DIR_PWR8: Opcode = PPC::NOP_GT_PWR7; break; /* FIXME: Update when P8 InstrScheduling model is ready */ |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 334 | } |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 335 | |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 336 | DebugLoc DL; |
| 337 | BuildMI(MBB, MI, DL, get(Opcode)); |
| 338 | } |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 339 | |
Joerg Sonnenberger | 7ee0f31 | 2014-08-08 19:13:23 +0000 | [diff] [blame] | 340 | /// getNoopForMachoTarget - Return the noop instruction to use for a noop. |
| 341 | void PPCInstrInfo::getNoopForMachoTarget(MCInst &NopInst) const { |
| 342 | NopInst.setOpcode(PPC::NOP); |
| 343 | } |
| 344 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 345 | // Branch analysis. |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 346 | // Note: If the condition register is set to CTR or CTR8 then this is a |
| 347 | // BDNZ (imm == 1) or BDZ (imm == 0) branch. |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 348 | bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB, |
| 349 | MachineBasicBlock *&FBB, |
Evan Cheng | 64dfcac | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 350 | SmallVectorImpl<MachineOperand> &Cond, |
| 351 | bool AllowModify) const { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 352 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 353 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 354 | // If the block has no terminators, it just falls into the block after it. |
Benjamin Kramer | 92861d7 | 2015-06-25 13:39:03 +0000 | [diff] [blame] | 355 | MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); |
| 356 | if (I == MBB.end()) |
Dale Johannesen | 4244d12 | 2010-04-02 01:38:09 +0000 | [diff] [blame] | 357 | return false; |
Benjamin Kramer | 92861d7 | 2015-06-25 13:39:03 +0000 | [diff] [blame] | 358 | |
Dale Johannesen | 4244d12 | 2010-04-02 01:38:09 +0000 | [diff] [blame] | 359 | if (!isUnpredicatedTerminator(I)) |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 360 | return false; |
| 361 | |
| 362 | // Get the last instruction in the block. |
| 363 | MachineInstr *LastInst = I; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 364 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 365 | // If there is only one terminator instruction, process it. |
Evan Cheng | 5514bbe | 2007-06-08 21:59:56 +0000 | [diff] [blame] | 366 | if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) { |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 367 | if (LastInst->getOpcode() == PPC::B) { |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 368 | if (!LastInst->getOperand(0).isMBB()) |
| 369 | return true; |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 370 | TBB = LastInst->getOperand(0).getMBB(); |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 371 | return false; |
Chris Lattner | e026379 | 2006-11-17 22:14:47 +0000 | [diff] [blame] | 372 | } else if (LastInst->getOpcode() == PPC::BCC) { |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 373 | if (!LastInst->getOperand(2).isMBB()) |
| 374 | return true; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 375 | // Block ends with fall-through condbranch. |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 376 | TBB = LastInst->getOperand(2).getMBB(); |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 377 | Cond.push_back(LastInst->getOperand(0)); |
| 378 | Cond.push_back(LastInst->getOperand(1)); |
Chris Lattner | 23f22de | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 379 | return false; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 380 | } else if (LastInst->getOpcode() == PPC::BC) { |
| 381 | if (!LastInst->getOperand(1).isMBB()) |
| 382 | return true; |
| 383 | // Block ends with fall-through condbranch. |
| 384 | TBB = LastInst->getOperand(1).getMBB(); |
| 385 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); |
| 386 | Cond.push_back(LastInst->getOperand(0)); |
| 387 | return false; |
| 388 | } else if (LastInst->getOpcode() == PPC::BCn) { |
| 389 | if (!LastInst->getOperand(1).isMBB()) |
| 390 | return true; |
| 391 | // Block ends with fall-through condbranch. |
| 392 | TBB = LastInst->getOperand(1).getMBB(); |
| 393 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); |
| 394 | Cond.push_back(LastInst->getOperand(0)); |
| 395 | return false; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 396 | } else if (LastInst->getOpcode() == PPC::BDNZ8 || |
| 397 | LastInst->getOpcode() == PPC::BDNZ) { |
| 398 | if (!LastInst->getOperand(0).isMBB()) |
| 399 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 400 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 401 | return true; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 402 | TBB = LastInst->getOperand(0).getMBB(); |
| 403 | Cond.push_back(MachineOperand::CreateImm(1)); |
| 404 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 405 | true)); |
| 406 | return false; |
| 407 | } else if (LastInst->getOpcode() == PPC::BDZ8 || |
| 408 | LastInst->getOpcode() == PPC::BDZ) { |
| 409 | if (!LastInst->getOperand(0).isMBB()) |
| 410 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 411 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 412 | return true; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 413 | TBB = LastInst->getOperand(0).getMBB(); |
| 414 | Cond.push_back(MachineOperand::CreateImm(0)); |
| 415 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 416 | true)); |
| 417 | return false; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 418 | } |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 419 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 420 | // Otherwise, don't know what this is. |
| 421 | return true; |
| 422 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 423 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 424 | // Get the instruction before it if it's a terminator. |
| 425 | MachineInstr *SecondLastInst = I; |
| 426 | |
| 427 | // If there are three terminators, we don't know what sort of block this is. |
| 428 | if (SecondLastInst && I != MBB.begin() && |
Evan Cheng | 5514bbe | 2007-06-08 21:59:56 +0000 | [diff] [blame] | 429 | isUnpredicatedTerminator(--I)) |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 430 | return true; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 431 | |
Chris Lattner | e026379 | 2006-11-17 22:14:47 +0000 | [diff] [blame] | 432 | // If the block ends with PPC::B and PPC:BCC, handle it. |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 433 | if (SecondLastInst->getOpcode() == PPC::BCC && |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 434 | LastInst->getOpcode() == PPC::B) { |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 435 | if (!SecondLastInst->getOperand(2).isMBB() || |
| 436 | !LastInst->getOperand(0).isMBB()) |
| 437 | return true; |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 438 | TBB = SecondLastInst->getOperand(2).getMBB(); |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 439 | Cond.push_back(SecondLastInst->getOperand(0)); |
| 440 | Cond.push_back(SecondLastInst->getOperand(1)); |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 441 | FBB = LastInst->getOperand(0).getMBB(); |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 442 | return false; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 443 | } else if (SecondLastInst->getOpcode() == PPC::BC && |
| 444 | LastInst->getOpcode() == PPC::B) { |
| 445 | if (!SecondLastInst->getOperand(1).isMBB() || |
| 446 | !LastInst->getOperand(0).isMBB()) |
| 447 | return true; |
| 448 | TBB = SecondLastInst->getOperand(1).getMBB(); |
| 449 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); |
| 450 | Cond.push_back(SecondLastInst->getOperand(0)); |
| 451 | FBB = LastInst->getOperand(0).getMBB(); |
| 452 | return false; |
| 453 | } else if (SecondLastInst->getOpcode() == PPC::BCn && |
| 454 | LastInst->getOpcode() == PPC::B) { |
| 455 | if (!SecondLastInst->getOperand(1).isMBB() || |
| 456 | !LastInst->getOperand(0).isMBB()) |
| 457 | return true; |
| 458 | TBB = SecondLastInst->getOperand(1).getMBB(); |
| 459 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); |
| 460 | Cond.push_back(SecondLastInst->getOperand(0)); |
| 461 | FBB = LastInst->getOperand(0).getMBB(); |
| 462 | return false; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 463 | } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 || |
| 464 | SecondLastInst->getOpcode() == PPC::BDNZ) && |
| 465 | LastInst->getOpcode() == PPC::B) { |
| 466 | if (!SecondLastInst->getOperand(0).isMBB() || |
| 467 | !LastInst->getOperand(0).isMBB()) |
| 468 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 469 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 470 | return true; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 471 | TBB = SecondLastInst->getOperand(0).getMBB(); |
| 472 | Cond.push_back(MachineOperand::CreateImm(1)); |
| 473 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 474 | true)); |
| 475 | FBB = LastInst->getOperand(0).getMBB(); |
| 476 | return false; |
| 477 | } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 || |
| 478 | SecondLastInst->getOpcode() == PPC::BDZ) && |
| 479 | LastInst->getOpcode() == PPC::B) { |
| 480 | if (!SecondLastInst->getOperand(0).isMBB() || |
| 481 | !LastInst->getOperand(0).isMBB()) |
| 482 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 483 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 484 | return true; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 485 | TBB = SecondLastInst->getOperand(0).getMBB(); |
| 486 | Cond.push_back(MachineOperand::CreateImm(0)); |
| 487 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 488 | true)); |
| 489 | FBB = LastInst->getOperand(0).getMBB(); |
| 490 | return false; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 491 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 492 | |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 493 | // If the block ends with two PPC:Bs, handle it. The second one is not |
| 494 | // executed, so remove it. |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 495 | if (SecondLastInst->getOpcode() == PPC::B && |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 496 | LastInst->getOpcode() == PPC::B) { |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 497 | if (!SecondLastInst->getOperand(0).isMBB()) |
| 498 | return true; |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 499 | TBB = SecondLastInst->getOperand(0).getMBB(); |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 500 | I = LastInst; |
Evan Cheng | 64dfcac | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 501 | if (AllowModify) |
| 502 | I->eraseFromParent(); |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 503 | return false; |
| 504 | } |
| 505 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 506 | // Otherwise, can't handle this. |
| 507 | return true; |
| 508 | } |
| 509 | |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 510 | unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const { |
Benjamin Kramer | 92861d7 | 2015-06-25 13:39:03 +0000 | [diff] [blame] | 511 | MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); |
| 512 | if (I == MBB.end()) |
| 513 | return 0; |
| 514 | |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 515 | if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC && |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 516 | I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 517 | I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && |
| 518 | I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 519 | return 0; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 520 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 521 | // Remove the branch. |
| 522 | I->eraseFromParent(); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 523 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 524 | I = MBB.end(); |
| 525 | |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 526 | if (I == MBB.begin()) return 1; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 527 | --I; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 528 | if (I->getOpcode() != PPC::BCC && |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 529 | I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 530 | I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && |
| 531 | I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 532 | return 1; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 533 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 534 | // Remove the branch. |
| 535 | I->eraseFromParent(); |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 536 | return 2; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 539 | unsigned |
| 540 | PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
| 541 | MachineBasicBlock *FBB, |
Ahmed Bougacha | c88bf54 | 2015-06-11 19:30:37 +0000 | [diff] [blame] | 542 | ArrayRef<MachineOperand> Cond, |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 543 | DebugLoc DL) const { |
Chris Lattner | a61f010 | 2006-10-17 18:06:55 +0000 | [diff] [blame] | 544 | // Shouldn't be a fall through. |
| 545 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 546 | assert((Cond.size() == 2 || Cond.size() == 0) && |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 547 | "PPC branch conditions have two components!"); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 548 | |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 549 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 550 | |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 551 | // One-way branch. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 552 | if (!FBB) { |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 553 | if (Cond.empty()) // Unconditional branch |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 554 | BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 555 | else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 556 | BuildMI(&MBB, DL, get(Cond[0].getImm() ? |
| 557 | (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : |
| 558 | (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 559 | else if (Cond[0].getImm() == PPC::PRED_BIT_SET) |
| 560 | BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB); |
| 561 | else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) |
| 562 | BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB); |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 563 | else // Conditional branch |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 564 | BuildMI(&MBB, DL, get(PPC::BCC)) |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 565 | .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB); |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 566 | return 1; |
Chris Lattner | a61f010 | 2006-10-17 18:06:55 +0000 | [diff] [blame] | 567 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 568 | |
Chris Lattner | d881660 | 2006-10-21 05:42:09 +0000 | [diff] [blame] | 569 | // Two-way Conditional Branch. |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 570 | if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 571 | BuildMI(&MBB, DL, get(Cond[0].getImm() ? |
| 572 | (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : |
| 573 | (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 574 | else if (Cond[0].getImm() == PPC::PRED_BIT_SET) |
| 575 | BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB); |
| 576 | else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) |
| 577 | BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 578 | else |
| 579 | BuildMI(&MBB, DL, get(PPC::BCC)) |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 580 | .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB); |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 581 | BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB); |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 582 | return 2; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 585 | // Select analysis. |
| 586 | bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB, |
Ahmed Bougacha | c88bf54 | 2015-06-11 19:30:37 +0000 | [diff] [blame] | 587 | ArrayRef<MachineOperand> Cond, |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 588 | unsigned TrueReg, unsigned FalseReg, |
| 589 | int &CondCycles, int &TrueCycles, int &FalseCycles) const { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 590 | if (!Subtarget.hasISEL()) |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 591 | return false; |
| 592 | |
| 593 | if (Cond.size() != 2) |
| 594 | return false; |
| 595 | |
| 596 | // If this is really a bdnz-like condition, then it cannot be turned into a |
| 597 | // select. |
| 598 | if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 599 | return false; |
| 600 | |
| 601 | // Check register classes. |
| 602 | const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 603 | const TargetRegisterClass *RC = |
| 604 | RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); |
| 605 | if (!RC) |
| 606 | return false; |
| 607 | |
| 608 | // isel is for regular integer GPRs only. |
| 609 | if (!PPC::GPRCRegClass.hasSubClassEq(RC) && |
Hal Finkel | 8e8618a | 2013-07-15 20:22:58 +0000 | [diff] [blame] | 610 | !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) && |
| 611 | !PPC::G8RCRegClass.hasSubClassEq(RC) && |
| 612 | !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 613 | return false; |
| 614 | |
| 615 | // FIXME: These numbers are for the A2, how well they work for other cores is |
| 616 | // an open question. On the A2, the isel instruction has a 2-cycle latency |
| 617 | // but single-cycle throughput. These numbers are used in combination with |
| 618 | // the MispredictPenalty setting from the active SchedMachineModel. |
| 619 | CondCycles = 1; |
| 620 | TrueCycles = 1; |
| 621 | FalseCycles = 1; |
| 622 | |
| 623 | return true; |
| 624 | } |
| 625 | |
| 626 | void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB, |
| 627 | MachineBasicBlock::iterator MI, DebugLoc dl, |
Ahmed Bougacha | c88bf54 | 2015-06-11 19:30:37 +0000 | [diff] [blame] | 628 | unsigned DestReg, ArrayRef<MachineOperand> Cond, |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 629 | unsigned TrueReg, unsigned FalseReg) const { |
| 630 | assert(Cond.size() == 2 && |
| 631 | "PPC branch conditions have two components!"); |
| 632 | |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 633 | assert(Subtarget.hasISEL() && |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 634 | "Cannot insert select on target without ISEL support"); |
| 635 | |
| 636 | // Get the register classes. |
| 637 | MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 638 | const TargetRegisterClass *RC = |
| 639 | RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); |
| 640 | assert(RC && "TrueReg and FalseReg must have overlapping register classes"); |
Hal Finkel | 8e8618a | 2013-07-15 20:22:58 +0000 | [diff] [blame] | 641 | |
| 642 | bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) || |
| 643 | PPC::G8RC_NOX0RegClass.hasSubClassEq(RC); |
| 644 | assert((Is64Bit || |
| 645 | PPC::GPRCRegClass.hasSubClassEq(RC) || |
| 646 | PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) && |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 647 | "isel is for regular integer GPRs only"); |
| 648 | |
Hal Finkel | 8e8618a | 2013-07-15 20:22:58 +0000 | [diff] [blame] | 649 | unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL; |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 650 | unsigned SelectPred = Cond[0].getImm(); |
| 651 | |
| 652 | unsigned SubIdx; |
| 653 | bool SwapOps; |
| 654 | switch (SelectPred) { |
| 655 | default: llvm_unreachable("invalid predicate for isel"); |
| 656 | case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break; |
| 657 | case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break; |
| 658 | case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break; |
| 659 | case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break; |
| 660 | case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break; |
| 661 | case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break; |
| 662 | case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break; |
| 663 | case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 664 | case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break; |
| 665 | case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break; |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | unsigned FirstReg = SwapOps ? FalseReg : TrueReg, |
| 669 | SecondReg = SwapOps ? TrueReg : FalseReg; |
| 670 | |
| 671 | // The first input register of isel cannot be r0. If it is a member |
| 672 | // of a register class that can be r0, then copy it first (the |
| 673 | // register allocator should eliminate the copy). |
| 674 | if (MRI.getRegClass(FirstReg)->contains(PPC::R0) || |
| 675 | MRI.getRegClass(FirstReg)->contains(PPC::X0)) { |
| 676 | const TargetRegisterClass *FirstRC = |
| 677 | MRI.getRegClass(FirstReg)->contains(PPC::X0) ? |
| 678 | &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass; |
| 679 | unsigned OldFirstReg = FirstReg; |
| 680 | FirstReg = MRI.createVirtualRegister(FirstRC); |
| 681 | BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg) |
| 682 | .addReg(OldFirstReg); |
| 683 | } |
| 684 | |
| 685 | BuildMI(MBB, MI, dl, get(OpCode), DestReg) |
| 686 | .addReg(FirstReg).addReg(SecondReg) |
| 687 | .addReg(Cond[1].getReg(), 0, SubIdx); |
| 688 | } |
| 689 | |
Kit Barton | 535e69d | 2015-03-25 19:36:23 +0000 | [diff] [blame] | 690 | static unsigned getCRBitValue(unsigned CRBit) { |
| 691 | unsigned Ret = 4; |
| 692 | if (CRBit == PPC::CR0LT || CRBit == PPC::CR1LT || |
| 693 | CRBit == PPC::CR2LT || CRBit == PPC::CR3LT || |
| 694 | CRBit == PPC::CR4LT || CRBit == PPC::CR5LT || |
| 695 | CRBit == PPC::CR6LT || CRBit == PPC::CR7LT) |
| 696 | Ret = 3; |
| 697 | if (CRBit == PPC::CR0GT || CRBit == PPC::CR1GT || |
| 698 | CRBit == PPC::CR2GT || CRBit == PPC::CR3GT || |
| 699 | CRBit == PPC::CR4GT || CRBit == PPC::CR5GT || |
| 700 | CRBit == PPC::CR6GT || CRBit == PPC::CR7GT) |
| 701 | Ret = 2; |
| 702 | if (CRBit == PPC::CR0EQ || CRBit == PPC::CR1EQ || |
| 703 | CRBit == PPC::CR2EQ || CRBit == PPC::CR3EQ || |
| 704 | CRBit == PPC::CR4EQ || CRBit == PPC::CR5EQ || |
| 705 | CRBit == PPC::CR6EQ || CRBit == PPC::CR7EQ) |
| 706 | Ret = 1; |
| 707 | if (CRBit == PPC::CR0UN || CRBit == PPC::CR1UN || |
| 708 | CRBit == PPC::CR2UN || CRBit == PPC::CR3UN || |
| 709 | CRBit == PPC::CR4UN || CRBit == PPC::CR5UN || |
| 710 | CRBit == PPC::CR6UN || CRBit == PPC::CR7UN) |
| 711 | Ret = 0; |
| 712 | |
| 713 | assert(Ret != 4 && "Invalid CR bit register"); |
| 714 | return Ret; |
| 715 | } |
| 716 | |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 717 | void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
| 718 | MachineBasicBlock::iterator I, DebugLoc DL, |
| 719 | unsigned DestReg, unsigned SrcReg, |
| 720 | bool KillSrc) const { |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 721 | // We can end up with self copies and similar things as a result of VSX copy |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 722 | // legalization. Promote them here. |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 723 | const TargetRegisterInfo *TRI = &getRegisterInfo(); |
| 724 | if (PPC::F8RCRegClass.contains(DestReg) && |
Hal Finkel | 5cedafb | 2015-02-16 23:46:30 +0000 | [diff] [blame] | 725 | PPC::VSRCRegClass.contains(SrcReg)) { |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 726 | unsigned SuperReg = |
| 727 | TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass); |
| 728 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 729 | if (VSXSelfCopyCrash && SrcReg == SuperReg) |
| 730 | llvm_unreachable("nop VSX copy"); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 731 | |
| 732 | DestReg = SuperReg; |
| 733 | } else if (PPC::VRRCRegClass.contains(DestReg) && |
Hal Finkel | 5cedafb | 2015-02-16 23:46:30 +0000 | [diff] [blame] | 734 | PPC::VSRCRegClass.contains(SrcReg)) { |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 735 | unsigned SuperReg = |
| 736 | TRI->getMatchingSuperReg(DestReg, PPC::sub_128, &PPC::VSRCRegClass); |
| 737 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 738 | if (VSXSelfCopyCrash && SrcReg == SuperReg) |
| 739 | llvm_unreachable("nop VSX copy"); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 740 | |
| 741 | DestReg = SuperReg; |
| 742 | } else if (PPC::F8RCRegClass.contains(SrcReg) && |
Hal Finkel | 5cedafb | 2015-02-16 23:46:30 +0000 | [diff] [blame] | 743 | PPC::VSRCRegClass.contains(DestReg)) { |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 744 | unsigned SuperReg = |
| 745 | TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass); |
| 746 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 747 | if (VSXSelfCopyCrash && DestReg == SuperReg) |
| 748 | llvm_unreachable("nop VSX copy"); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 749 | |
| 750 | SrcReg = SuperReg; |
| 751 | } else if (PPC::VRRCRegClass.contains(SrcReg) && |
Hal Finkel | 5cedafb | 2015-02-16 23:46:30 +0000 | [diff] [blame] | 752 | PPC::VSRCRegClass.contains(DestReg)) { |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 753 | unsigned SuperReg = |
| 754 | TRI->getMatchingSuperReg(SrcReg, PPC::sub_128, &PPC::VSRCRegClass); |
| 755 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 756 | if (VSXSelfCopyCrash && DestReg == SuperReg) |
| 757 | llvm_unreachable("nop VSX copy"); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 758 | |
| 759 | SrcReg = SuperReg; |
| 760 | } |
| 761 | |
Kit Barton | 535e69d | 2015-03-25 19:36:23 +0000 | [diff] [blame] | 762 | // Different class register copy |
| 763 | if (PPC::CRBITRCRegClass.contains(SrcReg) && |
| 764 | PPC::GPRCRegClass.contains(DestReg)) { |
| 765 | unsigned CRReg = getCRFromCRBit(SrcReg); |
| 766 | BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg) |
| 767 | .addReg(CRReg), getKillRegState(KillSrc); |
| 768 | // Rotate the CR bit in the CR fields to be the least significant bit and |
| 769 | // then mask with 0x1 (MB = ME = 31). |
| 770 | BuildMI(MBB, I, DL, get(PPC::RLWINM), DestReg) |
| 771 | .addReg(DestReg, RegState::Kill) |
| 772 | .addImm(TRI->getEncodingValue(CRReg) * 4 + (4 - getCRBitValue(SrcReg))) |
| 773 | .addImm(31) |
| 774 | .addImm(31); |
| 775 | return; |
| 776 | } else if (PPC::CRRCRegClass.contains(SrcReg) && |
| 777 | PPC::G8RCRegClass.contains(DestReg)) { |
| 778 | BuildMI(MBB, I, DL, get(PPC::MFOCRF8), DestReg) |
| 779 | .addReg(SrcReg), getKillRegState(KillSrc); |
| 780 | return; |
| 781 | } else if (PPC::CRRCRegClass.contains(SrcReg) && |
| 782 | PPC::GPRCRegClass.contains(DestReg)) { |
| 783 | BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg) |
| 784 | .addReg(SrcReg), getKillRegState(KillSrc); |
| 785 | return; |
| 786 | } |
| 787 | |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 788 | unsigned Opc; |
| 789 | if (PPC::GPRCRegClass.contains(DestReg, SrcReg)) |
| 790 | Opc = PPC::OR; |
| 791 | else if (PPC::G8RCRegClass.contains(DestReg, SrcReg)) |
| 792 | Opc = PPC::OR8; |
| 793 | else if (PPC::F4RCRegClass.contains(DestReg, SrcReg)) |
| 794 | Opc = PPC::FMR; |
| 795 | else if (PPC::CRRCRegClass.contains(DestReg, SrcReg)) |
| 796 | Opc = PPC::MCRF; |
| 797 | else if (PPC::VRRCRegClass.contains(DestReg, SrcReg)) |
| 798 | Opc = PPC::VOR; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 799 | else if (PPC::VSRCRegClass.contains(DestReg, SrcReg)) |
Hal Finkel | bbad233 | 2014-03-24 09:36:36 +0000 | [diff] [blame] | 800 | // There are two different ways this can be done: |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 801 | // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only |
| 802 | // issue in VSU pipeline 0. |
| 803 | // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but |
| 804 | // can go to either pipeline. |
Hal Finkel | bbad233 | 2014-03-24 09:36:36 +0000 | [diff] [blame] | 805 | // We'll always use xxlor here, because in practically all cases where |
| 806 | // copies are generated, they are close enough to some use that the |
| 807 | // lower-latency form is preferable. |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 808 | Opc = PPC::XXLOR; |
Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 809 | else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg) || |
| 810 | PPC::VSSRCRegClass.contains(DestReg, SrcReg)) |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 811 | Opc = PPC::XXLORf; |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 812 | else if (PPC::QFRCRegClass.contains(DestReg, SrcReg)) |
| 813 | Opc = PPC::QVFMR; |
| 814 | else if (PPC::QSRCRegClass.contains(DestReg, SrcReg)) |
| 815 | Opc = PPC::QVFMRs; |
| 816 | else if (PPC::QBRCRegClass.contains(DestReg, SrcReg)) |
| 817 | Opc = PPC::QVFMRb; |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 818 | else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg)) |
| 819 | Opc = PPC::CROR; |
| 820 | else |
| 821 | llvm_unreachable("Impossible reg-to-reg copy"); |
Owen Anderson | 7a73ae9 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 822 | |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 823 | const MCInstrDesc &MCID = get(Opc); |
| 824 | if (MCID.getNumOperands() == 3) |
| 825 | BuildMI(MBB, I, DL, MCID, DestReg) |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 826 | .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc)); |
| 827 | else |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 828 | BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc)); |
Owen Anderson | 7a73ae9 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Hal Finkel | 8f6834d | 2011-12-05 17:55:17 +0000 | [diff] [blame] | 831 | // This function returns true if a CR spill is necessary and false otherwise. |
Bill Wendling | c6c48fc | 2008-03-10 22:49:16 +0000 | [diff] [blame] | 832 | bool |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 833 | PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF, |
| 834 | unsigned SrcReg, bool isKill, |
Bill Wendling | c6c48fc | 2008-03-10 22:49:16 +0000 | [diff] [blame] | 835 | int FrameIdx, |
| 836 | const TargetRegisterClass *RC, |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 837 | SmallVectorImpl<MachineInstr*> &NewMIs, |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 838 | bool &NonRI, bool &SpillsVRS) const{ |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 839 | // Note: If additional store instructions are added here, |
| 840 | // update isStoreToStackSlot. |
| 841 | |
Chris Lattner | 6f306d7 | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 842 | DebugLoc DL; |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 843 | if (PPC::GPRCRegClass.hasSubClassEq(RC) || |
| 844 | PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 794e05b | 2013-03-23 17:14:27 +0000 | [diff] [blame] | 845 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW)) |
| 846 | .addReg(SrcReg, |
| 847 | getKillRegState(isKill)), |
| 848 | FrameIdx)); |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 849 | } else if (PPC::G8RCRegClass.hasSubClassEq(RC) || |
| 850 | PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 794e05b | 2013-03-23 17:14:27 +0000 | [diff] [blame] | 851 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD)) |
| 852 | .addReg(SrcReg, |
| 853 | getKillRegState(isKill)), |
| 854 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 855 | } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { |
Dale Johannesen | 6b8c76a | 2009-02-12 23:08:38 +0000 | [diff] [blame] | 856 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD)) |
Bill Wendling | f7b83c7 | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 857 | .addReg(SrcReg, |
| 858 | getKillRegState(isKill)), |
| 859 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 860 | } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { |
Dale Johannesen | 6b8c76a | 2009-02-12 23:08:38 +0000 | [diff] [blame] | 861 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS)) |
Bill Wendling | f7b83c7 | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 862 | .addReg(SrcReg, |
| 863 | getKillRegState(isKill)), |
| 864 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 865 | } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | e154c8f | 2013-03-12 14:12:16 +0000 | [diff] [blame] | 866 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR)) |
| 867 | .addReg(SrcReg, |
| 868 | getKillRegState(isKill)), |
| 869 | FrameIdx)); |
| 870 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 871 | } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 872 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT)) |
| 873 | .addReg(SrcReg, |
| 874 | getKillRegState(isKill)), |
| 875 | FrameIdx)); |
| 876 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 877 | } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 878 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX)) |
| 879 | .addReg(SrcReg, |
| 880 | getKillRegState(isKill)), |
| 881 | FrameIdx)); |
| 882 | NonRI = true; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 883 | } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { |
| 884 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXVD2X)) |
| 885 | .addReg(SrcReg, |
| 886 | getKillRegState(isKill)), |
| 887 | FrameIdx)); |
| 888 | NonRI = true; |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 889 | } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { |
| 890 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSDX)) |
| 891 | .addReg(SrcReg, |
| 892 | getKillRegState(isKill)), |
| 893 | FrameIdx)); |
| 894 | NonRI = true; |
Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 895 | } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) { |
| 896 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSSPX)) |
| 897 | .addReg(SrcReg, |
| 898 | getKillRegState(isKill)), |
| 899 | FrameIdx)); |
| 900 | NonRI = true; |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 901 | } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 902 | assert(Subtarget.isDarwin() && |
Hal Finkel | a7b0630 | 2013-03-27 00:02:20 +0000 | [diff] [blame] | 903 | "VRSAVE only needs spill/restore on Darwin"); |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 904 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE)) |
| 905 | .addReg(SrcReg, |
| 906 | getKillRegState(isKill)), |
| 907 | FrameIdx)); |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 908 | SpillsVRS = true; |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 909 | } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) { |
| 910 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDX)) |
| 911 | .addReg(SrcReg, |
| 912 | getKillRegState(isKill)), |
| 913 | FrameIdx)); |
| 914 | NonRI = true; |
| 915 | } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) { |
| 916 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFSXs)) |
| 917 | .addReg(SrcReg, |
| 918 | getKillRegState(isKill)), |
| 919 | FrameIdx)); |
| 920 | NonRI = true; |
| 921 | } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) { |
| 922 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDXb)) |
| 923 | .addReg(SrcReg, |
| 924 | getKillRegState(isKill)), |
| 925 | FrameIdx)); |
| 926 | NonRI = true; |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 927 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 928 | llvm_unreachable("Unknown regclass!"); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 929 | } |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 930 | |
| 931 | return false; |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | void |
| 935 | PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 936 | MachineBasicBlock::iterator MI, |
| 937 | unsigned SrcReg, bool isKill, int FrameIdx, |
Evan Cheng | efb126a | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 938 | const TargetRegisterClass *RC, |
| 939 | const TargetRegisterInfo *TRI) const { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 940 | MachineFunction &MF = *MBB.getParent(); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 941 | SmallVector<MachineInstr*, 4> NewMIs; |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 942 | |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 943 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 944 | FuncInfo->setHasSpills(); |
| 945 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 946 | bool NonRI = false, SpillsVRS = false; |
| 947 | if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs, |
| 948 | NonRI, SpillsVRS)) |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 949 | FuncInfo->setSpillsCR(); |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 950 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 951 | if (SpillsVRS) |
| 952 | FuncInfo->setSpillsVRSAVE(); |
| 953 | |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 954 | if (NonRI) |
| 955 | FuncInfo->setHasNonRISpills(); |
| 956 | |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 957 | for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) |
| 958 | MBB.insert(MI, NewMIs[i]); |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 959 | |
| 960 | const MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 961 | MachineMemOperand *MMO = |
Jay Foad | 465101b | 2011-11-15 07:34:52 +0000 | [diff] [blame] | 962 | MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx), |
Chris Lattner | e3d864b | 2010-09-21 04:39:43 +0000 | [diff] [blame] | 963 | MachineMemOperand::MOStore, |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 964 | MFI.getObjectSize(FrameIdx), |
| 965 | MFI.getObjectAlignment(FrameIdx)); |
| 966 | NewMIs.back()->addMemOperand(MF, MMO); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Hal Finkel | bde7f8f | 2011-12-06 20:55:36 +0000 | [diff] [blame] | 969 | bool |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 970 | PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL, |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 971 | unsigned DestReg, int FrameIdx, |
Bill Wendling | c6c48fc | 2008-03-10 22:49:16 +0000 | [diff] [blame] | 972 | const TargetRegisterClass *RC, |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 973 | SmallVectorImpl<MachineInstr*> &NewMIs, |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 974 | bool &NonRI, bool &SpillsVRS) const{ |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 975 | // Note: If additional load instructions are added here, |
| 976 | // update isLoadFromStackSlot. |
| 977 | |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 978 | if (PPC::GPRCRegClass.hasSubClassEq(RC) || |
| 979 | PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 5791f51 | 2013-03-27 19:10:40 +0000 | [diff] [blame] | 980 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), |
| 981 | DestReg), FrameIdx)); |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 982 | } else if (PPC::G8RCRegClass.hasSubClassEq(RC) || |
| 983 | PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 5791f51 | 2013-03-27 19:10:40 +0000 | [diff] [blame] | 984 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg), |
| 985 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 986 | } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 987 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg), |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 988 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 989 | } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 990 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg), |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 991 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 992 | } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | e154c8f | 2013-03-12 14:12:16 +0000 | [diff] [blame] | 993 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, |
| 994 | get(PPC::RESTORE_CR), DestReg), |
| 995 | FrameIdx)); |
| 996 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 997 | } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 998 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, |
| 999 | get(PPC::RESTORE_CRBIT), DestReg), |
| 1000 | FrameIdx)); |
| 1001 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1002 | } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1003 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg), |
| 1004 | FrameIdx)); |
| 1005 | NonRI = true; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 1006 | } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { |
| 1007 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXVD2X), DestReg), |
| 1008 | FrameIdx)); |
| 1009 | NonRI = true; |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 1010 | } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { |
| 1011 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSDX), DestReg), |
| 1012 | FrameIdx)); |
| 1013 | NonRI = true; |
Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 1014 | } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) { |
| 1015 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSSPX), DestReg), |
| 1016 | FrameIdx)); |
| 1017 | NonRI = true; |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 1018 | } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1019 | assert(Subtarget.isDarwin() && |
Hal Finkel | a7b0630 | 2013-03-27 00:02:20 +0000 | [diff] [blame] | 1020 | "VRSAVE only needs spill/restore on Darwin"); |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 1021 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, |
| 1022 | get(PPC::RESTORE_VRSAVE), |
| 1023 | DestReg), |
| 1024 | FrameIdx)); |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1025 | SpillsVRS = true; |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 1026 | } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) { |
| 1027 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDX), DestReg), |
| 1028 | FrameIdx)); |
| 1029 | NonRI = true; |
| 1030 | } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) { |
| 1031 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFSXs), DestReg), |
| 1032 | FrameIdx)); |
| 1033 | NonRI = true; |
| 1034 | } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) { |
| 1035 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDXb), DestReg), |
| 1036 | FrameIdx)); |
| 1037 | NonRI = true; |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1038 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1039 | llvm_unreachable("Unknown regclass!"); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1040 | } |
Hal Finkel | bde7f8f | 2011-12-06 20:55:36 +0000 | [diff] [blame] | 1041 | |
| 1042 | return false; |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | void |
| 1046 | PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 1047 | MachineBasicBlock::iterator MI, |
| 1048 | unsigned DestReg, int FrameIdx, |
Evan Cheng | efb126a | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 1049 | const TargetRegisterClass *RC, |
| 1050 | const TargetRegisterInfo *TRI) const { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 1051 | MachineFunction &MF = *MBB.getParent(); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1052 | SmallVector<MachineInstr*, 4> NewMIs; |
Chris Lattner | 6f306d7 | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 1053 | DebugLoc DL; |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 1054 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1055 | |
| 1056 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 1057 | FuncInfo->setHasSpills(); |
| 1058 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1059 | bool NonRI = false, SpillsVRS = false; |
| 1060 | if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs, |
| 1061 | NonRI, SpillsVRS)) |
Hal Finkel | bde7f8f | 2011-12-06 20:55:36 +0000 | [diff] [blame] | 1062 | FuncInfo->setSpillsCR(); |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1063 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1064 | if (SpillsVRS) |
| 1065 | FuncInfo->setSpillsVRSAVE(); |
| 1066 | |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1067 | if (NonRI) |
| 1068 | FuncInfo->setHasNonRISpills(); |
| 1069 | |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1070 | for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) |
| 1071 | MBB.insert(MI, NewMIs[i]); |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 1072 | |
| 1073 | const MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 1074 | MachineMemOperand *MMO = |
Jay Foad | 465101b | 2011-11-15 07:34:52 +0000 | [diff] [blame] | 1075 | MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx), |
Chris Lattner | e3d864b | 2010-09-21 04:39:43 +0000 | [diff] [blame] | 1076 | MachineMemOperand::MOLoad, |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 1077 | MFI.getObjectSize(FrameIdx), |
| 1078 | MFI.getObjectAlignment(FrameIdx)); |
| 1079 | NewMIs.back()->addMemOperand(MF, MMO); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 1082 | bool PPCInstrInfo:: |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1083 | ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { |
Chris Lattner | 23f22de | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 1084 | assert(Cond.size() == 2 && "Invalid PPC branch opcode!"); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 1085 | if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR) |
| 1086 | Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0); |
| 1087 | else |
| 1088 | // Leave the CR# the same, but invert the condition. |
| 1089 | Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm())); |
Chris Lattner | 23f22de | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 1090 | return false; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 1091 | } |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1092 | |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1093 | bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI, |
| 1094 | unsigned Reg, MachineRegisterInfo *MRI) const { |
| 1095 | // For some instructions, it is legal to fold ZERO into the RA register field. |
| 1096 | // A zero immediate should always be loaded with a single li. |
| 1097 | unsigned DefOpc = DefMI->getOpcode(); |
| 1098 | if (DefOpc != PPC::LI && DefOpc != PPC::LI8) |
| 1099 | return false; |
| 1100 | if (!DefMI->getOperand(1).isImm()) |
| 1101 | return false; |
| 1102 | if (DefMI->getOperand(1).getImm() != 0) |
| 1103 | return false; |
| 1104 | |
| 1105 | // Note that we cannot here invert the arguments of an isel in order to fold |
| 1106 | // a ZERO into what is presented as the second argument. All we have here |
| 1107 | // is the condition bit, and that might come from a CR-logical bit operation. |
| 1108 | |
| 1109 | const MCInstrDesc &UseMCID = UseMI->getDesc(); |
| 1110 | |
| 1111 | // Only fold into real machine instructions. |
| 1112 | if (UseMCID.isPseudo()) |
| 1113 | return false; |
| 1114 | |
| 1115 | unsigned UseIdx; |
| 1116 | for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx) |
| 1117 | if (UseMI->getOperand(UseIdx).isReg() && |
| 1118 | UseMI->getOperand(UseIdx).getReg() == Reg) |
| 1119 | break; |
| 1120 | |
| 1121 | assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI"); |
| 1122 | assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg"); |
| 1123 | |
| 1124 | const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx]; |
| 1125 | |
| 1126 | // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0 |
| 1127 | // register (which might also be specified as a pointer class kind). |
| 1128 | if (UseInfo->isLookupPtrRegClass()) { |
| 1129 | if (UseInfo->RegClass /* Kind */ != 1) |
| 1130 | return false; |
| 1131 | } else { |
| 1132 | if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID && |
| 1133 | UseInfo->RegClass != PPC::G8RC_NOX0RegClassID) |
| 1134 | return false; |
| 1135 | } |
| 1136 | |
| 1137 | // Make sure this is not tied to an output register (or otherwise |
| 1138 | // constrained). This is true for ST?UX registers, for example, which |
| 1139 | // are tied to their output registers. |
| 1140 | if (UseInfo->Constraints != 0) |
| 1141 | return false; |
| 1142 | |
| 1143 | unsigned ZeroReg; |
| 1144 | if (UseInfo->isLookupPtrRegClass()) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1145 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1146 | ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO; |
| 1147 | } else { |
| 1148 | ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ? |
| 1149 | PPC::ZERO8 : PPC::ZERO; |
| 1150 | } |
| 1151 | |
| 1152 | bool DeleteDef = MRI->hasOneNonDBGUse(Reg); |
| 1153 | UseMI->getOperand(UseIdx).setReg(ZeroReg); |
| 1154 | |
| 1155 | if (DeleteDef) |
| 1156 | DefMI->eraseFromParent(); |
| 1157 | |
| 1158 | return true; |
| 1159 | } |
| 1160 | |
Hal Finkel | 30ae229 | 2013-04-10 18:30:16 +0000 | [diff] [blame] | 1161 | static bool MBBDefinesCTR(MachineBasicBlock &MBB) { |
| 1162 | for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end(); |
| 1163 | I != IE; ++I) |
| 1164 | if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8)) |
| 1165 | return true; |
| 1166 | return false; |
| 1167 | } |
| 1168 | |
| 1169 | // We should make sure that, if we're going to predicate both sides of a |
| 1170 | // condition (a diamond), that both sides don't define the counter register. We |
| 1171 | // can predicate counter-decrement-based branches, but while that predicates |
| 1172 | // the branching, it does not predicate the counter decrement. If we tried to |
| 1173 | // merge the triangle into one predicated block, we'd decrement the counter |
| 1174 | // twice. |
| 1175 | bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB, |
| 1176 | unsigned NumT, unsigned ExtraT, |
| 1177 | MachineBasicBlock &FMBB, |
| 1178 | unsigned NumF, unsigned ExtraF, |
| 1179 | const BranchProbability &Probability) const { |
| 1180 | return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB)); |
| 1181 | } |
| 1182 | |
| 1183 | |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1184 | bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const { |
Hal Finkel | f29285a | 2013-04-11 01:23:34 +0000 | [diff] [blame] | 1185 | // The predicated branches are identified by their type, not really by the |
| 1186 | // explicit presence of a predicate. Furthermore, some of them can be |
| 1187 | // predicated more than once. Because if conversion won't try to predicate |
| 1188 | // any instruction which already claims to be predicated (by returning true |
| 1189 | // here), always return false. In doing so, we let isPredicable() be the |
| 1190 | // final word on whether not the instruction can be (further) predicated. |
| 1191 | |
| 1192 | return false; |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const { |
| 1196 | if (!MI->isTerminator()) |
| 1197 | return false; |
| 1198 | |
| 1199 | // Conditional branch is a special case. |
| 1200 | if (MI->isBranch() && !MI->isBarrier()) |
| 1201 | return true; |
| 1202 | |
| 1203 | return !isPredicated(MI); |
| 1204 | } |
| 1205 | |
Ahmed Bougacha | c88bf54 | 2015-06-11 19:30:37 +0000 | [diff] [blame] | 1206 | bool PPCInstrInfo::PredicateInstruction(MachineInstr *MI, |
| 1207 | ArrayRef<MachineOperand> Pred) const { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1208 | unsigned OpC = MI->getOpcode(); |
Hal Finkel | f4a22c0 | 2015-01-13 17:47:54 +0000 | [diff] [blame] | 1209 | if (OpC == PPC::BLR || OpC == PPC::BLR8) { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1210 | if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1211 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1212 | MI->setDesc(get(Pred[0].getImm() ? |
| 1213 | (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) : |
| 1214 | (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR))); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1215 | } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1216 | MI->setDesc(get(PPC::BCLR)); |
| 1217 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1218 | .addReg(Pred[1].getReg()); |
| 1219 | } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { |
| 1220 | MI->setDesc(get(PPC::BCLRn)); |
| 1221 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1222 | .addReg(Pred[1].getReg()); |
| 1223 | } else { |
| 1224 | MI->setDesc(get(PPC::BCCLR)); |
| 1225 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1226 | .addImm(Pred[0].getImm()) |
| 1227 | .addReg(Pred[1].getReg()); |
| 1228 | } |
| 1229 | |
| 1230 | return true; |
| 1231 | } else if (OpC == PPC::B) { |
| 1232 | if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1233 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1234 | MI->setDesc(get(Pred[0].getImm() ? |
| 1235 | (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : |
| 1236 | (isPPC64 ? PPC::BDZ8 : PPC::BDZ))); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1237 | } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { |
| 1238 | MachineBasicBlock *MBB = MI->getOperand(0).getMBB(); |
| 1239 | MI->RemoveOperand(0); |
| 1240 | |
| 1241 | MI->setDesc(get(PPC::BC)); |
| 1242 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1243 | .addReg(Pred[1].getReg()) |
| 1244 | .addMBB(MBB); |
| 1245 | } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { |
| 1246 | MachineBasicBlock *MBB = MI->getOperand(0).getMBB(); |
| 1247 | MI->RemoveOperand(0); |
| 1248 | |
| 1249 | MI->setDesc(get(PPC::BCn)); |
| 1250 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1251 | .addReg(Pred[1].getReg()) |
| 1252 | .addMBB(MBB); |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1253 | } else { |
| 1254 | MachineBasicBlock *MBB = MI->getOperand(0).getMBB(); |
| 1255 | MI->RemoveOperand(0); |
| 1256 | |
| 1257 | MI->setDesc(get(PPC::BCC)); |
| 1258 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1259 | .addImm(Pred[0].getImm()) |
| 1260 | .addReg(Pred[1].getReg()) |
| 1261 | .addMBB(MBB); |
| 1262 | } |
| 1263 | |
| 1264 | return true; |
Hal Finkel | 500b004 | 2013-04-10 06:42:34 +0000 | [diff] [blame] | 1265 | } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 || |
| 1266 | OpC == PPC::BCTRL || OpC == PPC::BCTRL8) { |
| 1267 | if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) |
| 1268 | llvm_unreachable("Cannot predicate bctr[l] on the ctr register"); |
| 1269 | |
| 1270 | bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8; |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1271 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1272 | |
| 1273 | if (Pred[0].getImm() == PPC::PRED_BIT_SET) { |
| 1274 | MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) : |
| 1275 | (setLR ? PPC::BCCTRL : PPC::BCCTR))); |
| 1276 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1277 | .addReg(Pred[1].getReg()); |
| 1278 | return true; |
| 1279 | } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { |
| 1280 | MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) : |
| 1281 | (setLR ? PPC::BCCTRLn : PPC::BCCTRn))); |
| 1282 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1283 | .addReg(Pred[1].getReg()); |
| 1284 | return true; |
| 1285 | } |
| 1286 | |
| 1287 | MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) : |
| 1288 | (setLR ? PPC::BCCCTRL : PPC::BCCCTR))); |
Hal Finkel | 500b004 | 2013-04-10 06:42:34 +0000 | [diff] [blame] | 1289 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1290 | .addImm(Pred[0].getImm()) |
| 1291 | .addReg(Pred[1].getReg()); |
| 1292 | return true; |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | return false; |
| 1296 | } |
| 1297 | |
Ahmed Bougacha | c88bf54 | 2015-06-11 19:30:37 +0000 | [diff] [blame] | 1298 | bool PPCInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1, |
| 1299 | ArrayRef<MachineOperand> Pred2) const { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1300 | assert(Pred1.size() == 2 && "Invalid PPC first predicate"); |
| 1301 | assert(Pred2.size() == 2 && "Invalid PPC second predicate"); |
| 1302 | |
| 1303 | if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR) |
| 1304 | return false; |
| 1305 | if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR) |
| 1306 | return false; |
| 1307 | |
Hal Finkel | 94a6f38 | 2013-12-11 23:12:25 +0000 | [diff] [blame] | 1308 | // P1 can only subsume P2 if they test the same condition register. |
| 1309 | if (Pred1[1].getReg() != Pred2[1].getReg()) |
| 1310 | return false; |
| 1311 | |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1312 | PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm(); |
| 1313 | PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm(); |
| 1314 | |
| 1315 | if (P1 == P2) |
| 1316 | return true; |
| 1317 | |
| 1318 | // Does P1 subsume P2, e.g. GE subsumes GT. |
| 1319 | if (P1 == PPC::PRED_LE && |
| 1320 | (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ)) |
| 1321 | return true; |
| 1322 | if (P1 == PPC::PRED_GE && |
| 1323 | (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ)) |
| 1324 | return true; |
| 1325 | |
| 1326 | return false; |
| 1327 | } |
| 1328 | |
| 1329 | bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI, |
| 1330 | std::vector<MachineOperand> &Pred) const { |
| 1331 | // Note: At the present time, the contents of Pred from this function is |
| 1332 | // unused by IfConversion. This implementation follows ARM by pushing the |
| 1333 | // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of |
| 1334 | // predicate, instructions defining CTR or CTR8 are also included as |
| 1335 | // predicate-defining instructions. |
| 1336 | |
| 1337 | const TargetRegisterClass *RCs[] = |
| 1338 | { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass, |
| 1339 | &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass }; |
| 1340 | |
| 1341 | bool Found = false; |
| 1342 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1343 | const MachineOperand &MO = MI->getOperand(i); |
Hal Finkel | af82201 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1344 | for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1345 | const TargetRegisterClass *RC = RCs[c]; |
Hal Finkel | af82201 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1346 | if (MO.isReg()) { |
| 1347 | if (MO.isDef() && RC->contains(MO.getReg())) { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1348 | Pred.push_back(MO); |
| 1349 | Found = true; |
| 1350 | } |
Hal Finkel | af82201 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1351 | } else if (MO.isRegMask()) { |
| 1352 | for (TargetRegisterClass::iterator I = RC->begin(), |
| 1353 | IE = RC->end(); I != IE; ++I) |
| 1354 | if (MO.clobbersPhysReg(*I)) { |
| 1355 | Pred.push_back(MO); |
| 1356 | Found = true; |
| 1357 | } |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1358 | } |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | return Found; |
| 1363 | } |
| 1364 | |
| 1365 | bool PPCInstrInfo::isPredicable(MachineInstr *MI) const { |
| 1366 | unsigned OpC = MI->getOpcode(); |
| 1367 | switch (OpC) { |
| 1368 | default: |
| 1369 | return false; |
| 1370 | case PPC::B: |
| 1371 | case PPC::BLR: |
Hal Finkel | f4a22c0 | 2015-01-13 17:47:54 +0000 | [diff] [blame] | 1372 | case PPC::BLR8: |
Hal Finkel | 500b004 | 2013-04-10 06:42:34 +0000 | [diff] [blame] | 1373 | case PPC::BCTR: |
| 1374 | case PPC::BCTR8: |
| 1375 | case PPC::BCTRL: |
| 1376 | case PPC::BCTRL8: |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1377 | return true; |
| 1378 | } |
| 1379 | } |
| 1380 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1381 | bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI, |
| 1382 | unsigned &SrcReg, unsigned &SrcReg2, |
| 1383 | int &Mask, int &Value) const { |
| 1384 | unsigned Opc = MI->getOpcode(); |
| 1385 | |
| 1386 | switch (Opc) { |
| 1387 | default: return false; |
| 1388 | case PPC::CMPWI: |
| 1389 | case PPC::CMPLWI: |
| 1390 | case PPC::CMPDI: |
| 1391 | case PPC::CMPLDI: |
| 1392 | SrcReg = MI->getOperand(1).getReg(); |
| 1393 | SrcReg2 = 0; |
| 1394 | Value = MI->getOperand(2).getImm(); |
| 1395 | Mask = 0xFFFF; |
| 1396 | return true; |
| 1397 | case PPC::CMPW: |
| 1398 | case PPC::CMPLW: |
| 1399 | case PPC::CMPD: |
| 1400 | case PPC::CMPLD: |
| 1401 | case PPC::FCMPUS: |
| 1402 | case PPC::FCMPUD: |
| 1403 | SrcReg = MI->getOperand(1).getReg(); |
| 1404 | SrcReg2 = MI->getOperand(2).getReg(); |
| 1405 | return true; |
| 1406 | } |
| 1407 | } |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1408 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1409 | bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr, |
| 1410 | unsigned SrcReg, unsigned SrcReg2, |
| 1411 | int Mask, int Value, |
| 1412 | const MachineRegisterInfo *MRI) const { |
Hal Finkel | b12da6b | 2013-04-18 22:54:25 +0000 | [diff] [blame] | 1413 | if (DisableCmpOpt) |
| 1414 | return false; |
| 1415 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1416 | int OpC = CmpInstr->getOpcode(); |
| 1417 | unsigned CRReg = CmpInstr->getOperand(0).getReg(); |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1418 | |
| 1419 | // FP record forms set CR1 based on the execption status bits, not a |
| 1420 | // comparison with zero. |
| 1421 | if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD) |
| 1422 | return false; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1423 | |
| 1424 | // The record forms set the condition register based on a signed comparison |
| 1425 | // with zero (so says the ISA manual). This is not as straightforward as it |
| 1426 | // seems, however, because this is always a 64-bit comparison on PPC64, even |
| 1427 | // for instructions that are 32-bit in nature (like slw for example). |
| 1428 | // So, on PPC32, for unsigned comparisons, we can use the record forms only |
| 1429 | // for equality checks (as those don't depend on the sign). On PPC64, |
| 1430 | // we are restricted to equality for unsigned 64-bit comparisons and for |
| 1431 | // signed 32-bit comparisons the applicability is more restricted. |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1432 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1433 | bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW; |
| 1434 | bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW; |
| 1435 | bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD; |
| 1436 | |
| 1437 | // Get the unique definition of SrcReg. |
| 1438 | MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg); |
| 1439 | if (!MI) return false; |
| 1440 | int MIOpC = MI->getOpcode(); |
| 1441 | |
| 1442 | bool equalityOnly = false; |
| 1443 | bool noSub = false; |
| 1444 | if (isPPC64) { |
| 1445 | if (is32BitSignedCompare) { |
| 1446 | // We can perform this optimization only if MI is sign-extending. |
| 1447 | if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo || |
| 1448 | MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo || |
| 1449 | MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo || |
| 1450 | MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo || |
| 1451 | MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) { |
| 1452 | noSub = true; |
| 1453 | } else |
| 1454 | return false; |
| 1455 | } else if (is32BitUnsignedCompare) { |
| 1456 | // We can perform this optimization, equality only, if MI is |
| 1457 | // zero-extending. |
| 1458 | if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo || |
| 1459 | MIOpC == PPC::SLW || MIOpC == PPC::SLWo || |
| 1460 | MIOpC == PPC::SRW || MIOpC == PPC::SRWo) { |
| 1461 | noSub = true; |
| 1462 | equalityOnly = true; |
| 1463 | } else |
| 1464 | return false; |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1465 | } else |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1466 | equalityOnly = is64BitUnsignedCompare; |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1467 | } else |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1468 | equalityOnly = is32BitUnsignedCompare; |
| 1469 | |
| 1470 | if (equalityOnly) { |
| 1471 | // We need to check the uses of the condition register in order to reject |
| 1472 | // non-equality comparisons. |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1473 | for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg), |
| 1474 | IE = MRI->use_instr_end(); I != IE; ++I) { |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1475 | MachineInstr *UseMI = &*I; |
| 1476 | if (UseMI->getOpcode() == PPC::BCC) { |
| 1477 | unsigned Pred = UseMI->getOperand(0).getImm(); |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1478 | if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE) |
| 1479 | return false; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1480 | } else if (UseMI->getOpcode() == PPC::ISEL || |
| 1481 | UseMI->getOpcode() == PPC::ISEL8) { |
| 1482 | unsigned SubIdx = UseMI->getOperand(3).getSubReg(); |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1483 | if (SubIdx != PPC::sub_eq) |
| 1484 | return false; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1485 | } else |
| 1486 | return false; |
| 1487 | } |
| 1488 | } |
| 1489 | |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1490 | MachineBasicBlock::iterator I = CmpInstr; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1491 | |
| 1492 | // Scan forward to find the first use of the compare. |
| 1493 | for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end(); |
| 1494 | I != EL; ++I) { |
| 1495 | bool FoundUse = false; |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1496 | for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg), |
| 1497 | JE = MRI->use_instr_end(); J != JE; ++J) |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1498 | if (&*J == &*I) { |
| 1499 | FoundUse = true; |
| 1500 | break; |
| 1501 | } |
| 1502 | |
| 1503 | if (FoundUse) |
| 1504 | break; |
| 1505 | } |
| 1506 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1507 | // There are two possible candidates which can be changed to set CR[01]. |
| 1508 | // One is MI, the other is a SUB instruction. |
| 1509 | // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1). |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1510 | MachineInstr *Sub = nullptr; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1511 | if (SrcReg2 != 0) |
| 1512 | // MI is not a candidate for CMPrr. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1513 | MI = nullptr; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1514 | // FIXME: Conservatively refuse to convert an instruction which isn't in the |
| 1515 | // same BB as the comparison. This is to allow the check below to avoid calls |
| 1516 | // (and other explicit clobbers); instead we should really check for these |
| 1517 | // more explicitly (in at least a few predecessors). |
| 1518 | else if (MI->getParent() != CmpInstr->getParent() || Value != 0) { |
| 1519 | // PPC does not have a record-form SUBri. |
| 1520 | return false; |
| 1521 | } |
| 1522 | |
| 1523 | // Search for Sub. |
| 1524 | const TargetRegisterInfo *TRI = &getRegisterInfo(); |
| 1525 | --I; |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1526 | |
| 1527 | // Get ready to iterate backward from CmpInstr. |
| 1528 | MachineBasicBlock::iterator E = MI, |
| 1529 | B = CmpInstr->getParent()->begin(); |
| 1530 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1531 | for (; I != E && !noSub; --I) { |
| 1532 | const MachineInstr &Instr = *I; |
| 1533 | unsigned IOpC = Instr.getOpcode(); |
| 1534 | |
| 1535 | if (&*I != CmpInstr && ( |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1536 | Instr.modifiesRegister(PPC::CR0, TRI) || |
| 1537 | Instr.readsRegister(PPC::CR0, TRI))) |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1538 | // This instruction modifies or uses the record condition register after |
| 1539 | // the one we want to change. While we could do this transformation, it |
| 1540 | // would likely not be profitable. This transformation removes one |
| 1541 | // instruction, and so even forcing RA to generate one move probably |
| 1542 | // makes it unprofitable. |
| 1543 | return false; |
| 1544 | |
| 1545 | // Check whether CmpInstr can be made redundant by the current instruction. |
| 1546 | if ((OpC == PPC::CMPW || OpC == PPC::CMPLW || |
| 1547 | OpC == PPC::CMPD || OpC == PPC::CMPLD) && |
| 1548 | (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) && |
| 1549 | ((Instr.getOperand(1).getReg() == SrcReg && |
| 1550 | Instr.getOperand(2).getReg() == SrcReg2) || |
| 1551 | (Instr.getOperand(1).getReg() == SrcReg2 && |
| 1552 | Instr.getOperand(2).getReg() == SrcReg))) { |
| 1553 | Sub = &*I; |
| 1554 | break; |
| 1555 | } |
| 1556 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1557 | if (I == B) |
| 1558 | // The 'and' is below the comparison instruction. |
| 1559 | return false; |
| 1560 | } |
| 1561 | |
| 1562 | // Return false if no candidates exist. |
| 1563 | if (!MI && !Sub) |
| 1564 | return false; |
| 1565 | |
| 1566 | // The single candidate is called MI. |
| 1567 | if (!MI) MI = Sub; |
| 1568 | |
| 1569 | int NewOpC = -1; |
| 1570 | MIOpC = MI->getOpcode(); |
| 1571 | if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8) |
| 1572 | NewOpC = MIOpC; |
| 1573 | else { |
| 1574 | NewOpC = PPC::getRecordFormOpcode(MIOpC); |
| 1575 | if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1) |
| 1576 | NewOpC = MIOpC; |
| 1577 | } |
| 1578 | |
| 1579 | // FIXME: On the non-embedded POWER architectures, only some of the record |
| 1580 | // forms are fast, and we should use only the fast ones. |
| 1581 | |
| 1582 | // The defining instruction has a record form (or is already a record |
| 1583 | // form). It is possible, however, that we'll need to reverse the condition |
| 1584 | // code of the users. |
| 1585 | if (NewOpC == -1) |
| 1586 | return false; |
| 1587 | |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1588 | SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate; |
| 1589 | SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1590 | |
| 1591 | // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP |
| 1592 | // needs to be updated to be based on SUB. Push the condition code |
| 1593 | // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the |
| 1594 | // condition code of these operands will be modified. |
| 1595 | bool ShouldSwap = false; |
| 1596 | if (Sub) { |
| 1597 | ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 && |
| 1598 | Sub->getOperand(2).getReg() == SrcReg; |
| 1599 | |
| 1600 | // The operands to subf are the opposite of sub, so only in the fixed-point |
| 1601 | // case, invert the order. |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1602 | ShouldSwap = !ShouldSwap; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | if (ShouldSwap) |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1606 | for (MachineRegisterInfo::use_instr_iterator |
| 1607 | I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end(); |
| 1608 | I != IE; ++I) { |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1609 | MachineInstr *UseMI = &*I; |
| 1610 | if (UseMI->getOpcode() == PPC::BCC) { |
| 1611 | PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm(); |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1612 | assert((!equalityOnly || |
| 1613 | Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) && |
| 1614 | "Invalid predicate for equality-only optimization"); |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1615 | PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)), |
Hal Finkel | 0f64e21 | 2013-04-20 05:16:26 +0000 | [diff] [blame] | 1616 | PPC::getSwappedPredicate(Pred))); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1617 | } else if (UseMI->getOpcode() == PPC::ISEL || |
| 1618 | UseMI->getOpcode() == PPC::ISEL8) { |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1619 | unsigned NewSubReg = UseMI->getOperand(3).getSubReg(); |
| 1620 | assert((!equalityOnly || NewSubReg == PPC::sub_eq) && |
| 1621 | "Invalid CR bit for equality-only optimization"); |
| 1622 | |
| 1623 | if (NewSubReg == PPC::sub_lt) |
| 1624 | NewSubReg = PPC::sub_gt; |
| 1625 | else if (NewSubReg == PPC::sub_gt) |
| 1626 | NewSubReg = PPC::sub_lt; |
| 1627 | |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1628 | SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)), |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1629 | NewSubReg)); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1630 | } else // We need to abort on a user we don't understand. |
| 1631 | return false; |
| 1632 | } |
| 1633 | |
| 1634 | // Create a new virtual register to hold the value of the CR set by the |
| 1635 | // record-form instruction. If the instruction was not previously in |
| 1636 | // record form, then set the kill flag on the CR. |
| 1637 | CmpInstr->eraseFromParent(); |
| 1638 | |
| 1639 | MachineBasicBlock::iterator MII = MI; |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1640 | BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(), |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1641 | get(TargetOpcode::COPY), CRReg) |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1642 | .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1643 | |
| 1644 | if (MIOpC != NewOpC) { |
| 1645 | // We need to be careful here: we're replacing one instruction with |
| 1646 | // another, and we need to make sure that we get all of the right |
| 1647 | // implicit uses and defs. On the other hand, the caller may be holding |
| 1648 | // an iterator to this instruction, and so we can't delete it (this is |
| 1649 | // specifically the case if this is the instruction directly after the |
| 1650 | // compare). |
| 1651 | |
| 1652 | const MCInstrDesc &NewDesc = get(NewOpC); |
| 1653 | MI->setDesc(NewDesc); |
| 1654 | |
| 1655 | if (NewDesc.ImplicitDefs) |
| 1656 | for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs(); |
| 1657 | *ImpDefs; ++ImpDefs) |
| 1658 | if (!MI->definesRegister(*ImpDefs)) |
| 1659 | MI->addOperand(*MI->getParent()->getParent(), |
| 1660 | MachineOperand::CreateReg(*ImpDefs, true, true)); |
| 1661 | if (NewDesc.ImplicitUses) |
| 1662 | for (const uint16_t *ImpUses = NewDesc.getImplicitUses(); |
| 1663 | *ImpUses; ++ImpUses) |
| 1664 | if (!MI->readsRegister(*ImpUses)) |
| 1665 | MI->addOperand(*MI->getParent()->getParent(), |
| 1666 | MachineOperand::CreateReg(*ImpUses, false, true)); |
| 1667 | } |
| 1668 | |
| 1669 | // Modify the condition code of operands in OperandsToUpdate. |
| 1670 | // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to |
| 1671 | // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc. |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1672 | for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++) |
| 1673 | PredsToUpdate[i].first->setImm(PredsToUpdate[i].second); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1674 | |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1675 | for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++) |
| 1676 | SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1677 | |
| 1678 | return true; |
| 1679 | } |
| 1680 | |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1681 | /// GetInstSize - Return the number of bytes of code the specified |
| 1682 | /// instruction may be. This returns the maximum number of bytes. |
| 1683 | /// |
| 1684 | unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const { |
Hal Finkel | a7bbaf6 | 2014-02-02 06:12:27 +0000 | [diff] [blame] | 1685 | unsigned Opcode = MI->getOpcode(); |
| 1686 | |
| 1687 | if (Opcode == PPC::INLINEASM) { |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1688 | const MachineFunction *MF = MI->getParent()->getParent(); |
| 1689 | const char *AsmStr = MI->getOperand(0).getSymbolName(); |
Chris Lattner | 7b26fce | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 1690 | return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); |
Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 1691 | } else if (Opcode == TargetOpcode::STACKMAP) { |
| 1692 | return MI->getOperand(1).getImm(); |
| 1693 | } else if (Opcode == TargetOpcode::PATCHPOINT) { |
| 1694 | PatchPointOpers Opers(MI); |
| 1695 | return Opers.getMetaOper(PatchPointOpers::NBytesPos).getImm(); |
Hal Finkel | a7bbaf6 | 2014-02-02 06:12:27 +0000 | [diff] [blame] | 1696 | } else { |
| 1697 | const MCInstrDesc &Desc = get(Opcode); |
| 1698 | return Desc.getSize(); |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1699 | } |
| 1700 | } |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1701 | |