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