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