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