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