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 | |
Zaara Syeda | fcd9697 | 2017-09-21 16:12:33 +0000 | [diff] [blame] | 49 | STATISTIC(NumStoreSPILLVSRRCAsVec, |
| 50 | "Number of spillvsrrc spilled to stack as vec"); |
| 51 | STATISTIC(NumStoreSPILLVSRRCAsGpr, |
| 52 | "Number of spillvsrrc spilled to stack as gpr"); |
| 53 | STATISTIC(NumGPRtoVSRSpill, "Number of gpr spills to spillvsrrc"); |
| 54 | |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 55 | static cl:: |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 56 | opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden, |
| 57 | cl::desc("Disable analysis for CTR loops")); |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 58 | |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 59 | static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt", |
Hal Finkel | b12da6b | 2013-04-18 22:54:25 +0000 | [diff] [blame] | 60 | cl::desc("Disable compare instruction optimization"), cl::Hidden); |
| 61 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 62 | static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy", |
| 63 | cl::desc("Causes the backend to crash instead of generating a nop VSX copy"), |
| 64 | cl::Hidden); |
| 65 | |
Hal Finkel | 8acae52 | 2015-07-14 20:02:02 +0000 | [diff] [blame] | 66 | static cl::opt<bool> |
| 67 | UseOldLatencyCalc("ppc-old-latency-calc", cl::Hidden, |
| 68 | cl::desc("Use the old (incorrect) instruction latency calculation")); |
| 69 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 70 | // Pin the vtable to this file. |
| 71 | void PPCInstrInfo::anchor() {} |
| 72 | |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 73 | PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI) |
Tim Shen | 918ed87 | 2017-02-10 21:03:24 +0000 | [diff] [blame] | 74 | : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP, |
| 75 | /* CatchRetOpcode */ -1, |
| 76 | STI.isPPC64() ? PPC::BLR8 : PPC::BLR), |
Eric Christopher | ea178cf | 2015-03-12 01:42:51 +0000 | [diff] [blame] | 77 | Subtarget(STI), RI(STI.getTargetMachine()) {} |
Chris Lattner | 49cadab | 2006-06-17 00:01:04 +0000 | [diff] [blame] | 78 | |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 79 | /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for |
| 80 | /// this target when scheduling the DAG. |
Eric Christopher | f047bfd | 2014-06-13 22:38:52 +0000 | [diff] [blame] | 81 | ScheduleHazardRecognizer * |
| 82 | PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI, |
| 83 | const ScheduleDAG *DAG) const { |
| 84 | unsigned Directive = |
| 85 | static_cast<const PPCSubtarget *>(STI)->getDarwinDirective(); |
Hal Finkel | 742b535 | 2012-08-28 16:12:39 +0000 | [diff] [blame] | 86 | if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 || |
| 87 | Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) { |
Eric Christopher | f047bfd | 2014-06-13 22:38:52 +0000 | [diff] [blame] | 88 | const InstrItineraryData *II = |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 89 | static_cast<const PPCSubtarget *>(STI)->getInstrItineraryData(); |
Hal Finkel | 563cc05 | 2013-12-02 23:52:46 +0000 | [diff] [blame] | 90 | return new ScoreboardHazardRecognizer(II, DAG); |
Hal Finkel | 6fa5697 | 2011-10-17 04:03:49 +0000 | [diff] [blame] | 91 | } |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 92 | |
Eric Christopher | f047bfd | 2014-06-13 22:38:52 +0000 | [diff] [blame] | 93 | return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG); |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 96 | /// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer |
| 97 | /// to use for this target when scheduling the DAG. |
Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 98 | ScheduleHazardRecognizer * |
| 99 | PPCInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, |
| 100 | const ScheduleDAG *DAG) const { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 101 | unsigned Directive = |
Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 102 | DAG->MF.getSubtarget<PPCSubtarget>().getDarwinDirective(); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 103 | |
Nemanja Ivanovic | 6e29baf | 2016-05-09 18:54:58 +0000 | [diff] [blame] | 104 | // FIXME: Leaving this as-is until we have POWER9 scheduling info |
Will Schmidt | 970ff64 | 2014-06-26 13:36:19 +0000 | [diff] [blame] | 105 | if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8) |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 106 | return new PPCDispatchGroupSBHazardRecognizer(II, DAG); |
| 107 | |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 108 | // Most subtargets use a PPC970 recognizer. |
Hal Finkel | 742b535 | 2012-08-28 16:12:39 +0000 | [diff] [blame] | 109 | if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 && |
| 110 | Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 111 | assert(DAG->TII && "No InstrInfo?"); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 112 | |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 113 | return new PPCHazardRecognizer970(*DAG); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Hal Finkel | 563cc05 | 2013-12-02 23:52:46 +0000 | [diff] [blame] | 116 | return new ScoreboardHazardRecognizer(II, DAG); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 117 | } |
Jakob Stoklund Olesen | 0f855e4 | 2012-06-19 21:14:34 +0000 | [diff] [blame] | 118 | |
Hal Finkel | 8acae52 | 2015-07-14 20:02:02 +0000 | [diff] [blame] | 119 | unsigned PPCInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 120 | const MachineInstr &MI, |
Hal Finkel | 8acae52 | 2015-07-14 20:02:02 +0000 | [diff] [blame] | 121 | unsigned *PredCost) const { |
| 122 | if (!ItinData || UseOldLatencyCalc) |
| 123 | return PPCGenInstrInfo::getInstrLatency(ItinData, MI, PredCost); |
| 124 | |
| 125 | // The default implementation of getInstrLatency calls getStageLatency, but |
| 126 | // getStageLatency does not do the right thing for us. While we have |
| 127 | // itinerary, most cores are fully pipelined, and so the itineraries only |
| 128 | // express the first part of the pipeline, not every stage. Instead, we need |
| 129 | // to use the listed output operand cycle number (using operand 0 here, which |
| 130 | // is an output). |
| 131 | |
| 132 | unsigned Latency = 1; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 133 | unsigned DefClass = MI.getDesc().getSchedClass(); |
| 134 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 135 | const MachineOperand &MO = MI.getOperand(i); |
Hal Finkel | 8acae52 | 2015-07-14 20:02:02 +0000 | [diff] [blame] | 136 | if (!MO.isReg() || !MO.isDef() || MO.isImplicit()) |
| 137 | continue; |
| 138 | |
| 139 | int Cycle = ItinData->getOperandCycle(DefClass, i); |
| 140 | if (Cycle < 0) |
| 141 | continue; |
| 142 | |
| 143 | Latency = std::max(Latency, (unsigned) Cycle); |
| 144 | } |
| 145 | |
| 146 | return Latency; |
| 147 | } |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 148 | |
| 149 | int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 150 | const MachineInstr &DefMI, unsigned DefIdx, |
| 151 | const MachineInstr &UseMI, |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 152 | unsigned UseIdx) const { |
| 153 | int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx, |
| 154 | UseMI, UseIdx); |
| 155 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 156 | if (!DefMI.getParent()) |
Hal Finkel | 5d36b23 | 2015-07-15 08:23:05 +0000 | [diff] [blame] | 157 | return Latency; |
| 158 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 159 | const MachineOperand &DefMO = DefMI.getOperand(DefIdx); |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 160 | unsigned Reg = DefMO.getReg(); |
| 161 | |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 162 | bool IsRegCR; |
Andrew Kaylor | 5c73e1f | 2015-03-24 23:37:10 +0000 | [diff] [blame] | 163 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 164 | const MachineRegisterInfo *MRI = |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 165 | &DefMI.getParent()->getParent()->getRegInfo(); |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 166 | IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) || |
| 167 | MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass); |
| 168 | } else { |
| 169 | IsRegCR = PPC::CRRCRegClass.contains(Reg) || |
| 170 | PPC::CRBITRCRegClass.contains(Reg); |
| 171 | } |
| 172 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 173 | if (UseMI.isBranch() && IsRegCR) { |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 174 | if (Latency < 0) |
| 175 | Latency = getInstrLatency(ItinData, DefMI); |
| 176 | |
| 177 | // On some cores, there is an additional delay between writing to a condition |
| 178 | // register, and using it from a branch. |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 179 | unsigned Directive = Subtarget.getDarwinDirective(); |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 180 | switch (Directive) { |
| 181 | default: break; |
| 182 | case PPC::DIR_7400: |
| 183 | case PPC::DIR_750: |
| 184 | case PPC::DIR_970: |
| 185 | case PPC::DIR_E5500: |
| 186 | case PPC::DIR_PWR4: |
| 187 | case PPC::DIR_PWR5: |
| 188 | case PPC::DIR_PWR5X: |
| 189 | case PPC::DIR_PWR6: |
| 190 | case PPC::DIR_PWR6X: |
| 191 | case PPC::DIR_PWR7: |
Will Schmidt | 970ff64 | 2014-06-26 13:36:19 +0000 | [diff] [blame] | 192 | case PPC::DIR_PWR8: |
Nemanja Ivanovic | 6e29baf | 2016-05-09 18:54:58 +0000 | [diff] [blame] | 193 | // FIXME: Is this needed for POWER9? |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 194 | Latency += 2; |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return Latency; |
| 200 | } |
| 201 | |
Hal Finkel | 5d36b23 | 2015-07-15 08:23:05 +0000 | [diff] [blame] | 202 | // This function does not list all associative and commutative operations, but |
| 203 | // only those worth feeding through the machine combiner in an attempt to |
| 204 | // reduce the critical path. Mostly, this means floating-point operations, |
| 205 | // because they have high latencies (compared to other operations, such and |
| 206 | // and/or, which are also associative and commutative, but have low latencies). |
Chad Rosier | 03a4730 | 2015-09-21 15:09:11 +0000 | [diff] [blame] | 207 | bool PPCInstrInfo::isAssociativeAndCommutative(const MachineInstr &Inst) const { |
| 208 | switch (Inst.getOpcode()) { |
Hal Finkel | 5d36b23 | 2015-07-15 08:23:05 +0000 | [diff] [blame] | 209 | // FP Add: |
| 210 | case PPC::FADD: |
| 211 | case PPC::FADDS: |
| 212 | // FP Multiply: |
| 213 | case PPC::FMUL: |
| 214 | case PPC::FMULS: |
| 215 | // Altivec Add: |
| 216 | case PPC::VADDFP: |
| 217 | // VSX Add: |
| 218 | case PPC::XSADDDP: |
| 219 | case PPC::XVADDDP: |
| 220 | case PPC::XVADDSP: |
| 221 | case PPC::XSADDSP: |
| 222 | // VSX Multiply: |
| 223 | case PPC::XSMULDP: |
| 224 | case PPC::XVMULDP: |
| 225 | case PPC::XVMULSP: |
| 226 | case PPC::XSMULSP: |
| 227 | // QPX Add: |
| 228 | case PPC::QVFADD: |
| 229 | case PPC::QVFADDS: |
| 230 | case PPC::QVFADDSs: |
| 231 | // QPX Multiply: |
| 232 | case PPC::QVFMUL: |
| 233 | case PPC::QVFMULS: |
| 234 | case PPC::QVFMULSs: |
| 235 | return true; |
| 236 | default: |
| 237 | return false; |
| 238 | } |
| 239 | } |
| 240 | |
Chad Rosier | 03a4730 | 2015-09-21 15:09:11 +0000 | [diff] [blame] | 241 | bool PPCInstrInfo::getMachineCombinerPatterns( |
| 242 | MachineInstr &Root, |
Sanjay Patel | 387e66e | 2015-11-05 19:34:57 +0000 | [diff] [blame] | 243 | SmallVectorImpl<MachineCombinerPattern> &Patterns) const { |
Hal Finkel | 5d36b23 | 2015-07-15 08:23:05 +0000 | [diff] [blame] | 244 | // Using the machine combiner in this way is potentially expensive, so |
| 245 | // restrict to when aggressive optimizations are desired. |
| 246 | if (Subtarget.getTargetMachine().getOptLevel() != CodeGenOpt::Aggressive) |
| 247 | return false; |
| 248 | |
| 249 | // FP reassociation is only legal when we don't need strict IEEE semantics. |
| 250 | if (!Root.getParent()->getParent()->getTarget().Options.UnsafeFPMath) |
| 251 | return false; |
| 252 | |
Chad Rosier | 03a4730 | 2015-09-21 15:09:11 +0000 | [diff] [blame] | 253 | return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns); |
Hal Finkel | 5d36b23 | 2015-07-15 08:23:05 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Jakob Stoklund Olesen | 0f855e4 | 2012-06-19 21:14:34 +0000 | [diff] [blame] | 256 | // Detect 32 -> 64-bit extensions where we may reuse the low sub-register. |
| 257 | bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI, |
| 258 | unsigned &SrcReg, unsigned &DstReg, |
| 259 | unsigned &SubIdx) const { |
| 260 | switch (MI.getOpcode()) { |
| 261 | default: return false; |
| 262 | case PPC::EXTSW: |
Hiroshi Inoue | e3a3e3c | 2017-10-16 04:12:57 +0000 | [diff] [blame] | 263 | case PPC::EXTSW_32: |
Jakob Stoklund Olesen | 0f855e4 | 2012-06-19 21:14:34 +0000 | [diff] [blame] | 264 | case PPC::EXTSW_32_64: |
| 265 | SrcReg = MI.getOperand(1).getReg(); |
| 266 | DstReg = MI.getOperand(0).getReg(); |
| 267 | SubIdx = PPC::sub_32; |
| 268 | return true; |
| 269 | } |
| 270 | } |
| 271 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 272 | unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, |
Chris Lattner | 91400bd | 2006-03-16 22:24:02 +0000 | [diff] [blame] | 273 | int &FrameIndex) const { |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 274 | // Note: This list must be kept consistent with LoadRegFromStackSlot. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 275 | switch (MI.getOpcode()) { |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 276 | default: break; |
| 277 | case PPC::LD: |
| 278 | case PPC::LWZ: |
| 279 | case PPC::LFS: |
| 280 | case PPC::LFD: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 281 | case PPC::RESTORE_CR: |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 282 | case PPC::RESTORE_CRBIT: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 283 | case PPC::LVX: |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 284 | case PPC::LXVD2X: |
Lei Huang | 263dc4e | 2017-10-11 20:20:58 +0000 | [diff] [blame] | 285 | case PPC::LXV: |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 286 | case PPC::QVLFDX: |
| 287 | case PPC::QVLFSXs: |
| 288 | case PPC::QVLFDXb: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 289 | case PPC::RESTORE_VRSAVE: |
Zaara Syeda | fcd9697 | 2017-09-21 16:12:33 +0000 | [diff] [blame] | 290 | case PPC::SPILLTOVSR_LD: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 291 | // Check for the operands added by addFrameReference (the immediate is the |
| 292 | // offset which defaults to 0). |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 293 | if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() && |
| 294 | MI.getOperand(2).isFI()) { |
| 295 | FrameIndex = MI.getOperand(2).getIndex(); |
| 296 | return MI.getOperand(0).getReg(); |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 297 | } |
| 298 | break; |
| 299 | } |
| 300 | return 0; |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 301 | } |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 302 | |
Lei Huang | 84dbbfd | 2017-06-21 17:17:56 +0000 | [diff] [blame] | 303 | // For opcodes with the ReMaterializable flag set, this function is called to |
| 304 | // verify the instruction is really rematable. |
| 305 | bool PPCInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr &MI, |
| 306 | AliasAnalysis *AA) const { |
| 307 | switch (MI.getOpcode()) { |
| 308 | default: |
| 309 | // This function should only be called for opcodes with the ReMaterializable |
| 310 | // flag set. |
| 311 | llvm_unreachable("Unknown rematerializable operation!"); |
| 312 | break; |
| 313 | case PPC::LI: |
| 314 | case PPC::LI8: |
| 315 | case PPC::LIS: |
| 316 | case PPC::LIS8: |
| 317 | case PPC::QVGPCI: |
| 318 | case PPC::ADDIStocHA: |
| 319 | case PPC::ADDItocL: |
| 320 | case PPC::LOAD_STACK_GUARD: |
| 321 | return true; |
| 322 | } |
| 323 | return false; |
| 324 | } |
| 325 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 326 | unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr &MI, |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 327 | int &FrameIndex) const { |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 328 | // Note: This list must be kept consistent with StoreRegToStackSlot. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 329 | switch (MI.getOpcode()) { |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 330 | default: break; |
Nate Begeman | 4efb328 | 2006-02-02 21:07:50 +0000 | [diff] [blame] | 331 | case PPC::STD: |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 332 | case PPC::STW: |
| 333 | case PPC::STFS: |
| 334 | case PPC::STFD: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 335 | case PPC::SPILL_CR: |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 336 | case PPC::SPILL_CRBIT: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 337 | case PPC::STVX: |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 338 | case PPC::STXVD2X: |
Lei Huang | 263dc4e | 2017-10-11 20:20:58 +0000 | [diff] [blame] | 339 | case PPC::STXV: |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 340 | case PPC::QVSTFDX: |
| 341 | case PPC::QVSTFSXs: |
| 342 | case PPC::QVSTFDXb: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 343 | case PPC::SPILL_VRSAVE: |
Zaara Syeda | fcd9697 | 2017-09-21 16:12:33 +0000 | [diff] [blame] | 344 | case PPC::SPILLTOVSR_ST: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 345 | // Check for the operands added by addFrameReference (the immediate is the |
| 346 | // offset which defaults to 0). |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 347 | if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() && |
| 348 | MI.getOperand(2).isFI()) { |
| 349 | FrameIndex = MI.getOperand(2).getIndex(); |
| 350 | return MI.getOperand(0).getReg(); |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 351 | } |
| 352 | break; |
| 353 | } |
| 354 | return 0; |
| 355 | } |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 356 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 357 | MachineInstr *PPCInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI, |
Andrew Kaylor | 16c4da0 | 2015-09-28 20:33:22 +0000 | [diff] [blame] | 358 | unsigned OpIdx1, |
| 359 | unsigned OpIdx2) const { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 360 | MachineFunction &MF = *MI.getParent()->getParent(); |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 361 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 362 | // Normal instructions can be commuted the obvious way. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 363 | if (MI.getOpcode() != PPC::RLWIMI && MI.getOpcode() != PPC::RLWIMIo) |
Andrew Kaylor | 16c4da0 | 2015-09-28 20:33:22 +0000 | [diff] [blame] | 364 | return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); |
Hal Finkel | 4c6658f | 2014-12-12 23:59:36 +0000 | [diff] [blame] | 365 | // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a |
| 366 | // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because |
| 367 | // changing the relative order of the mask operands might change what happens |
| 368 | // to the high-bits of the mask (and, thus, the result). |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 369 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 370 | // 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] | 371 | if (MI.getOperand(3).getImm() != 0) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 372 | return nullptr; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 373 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 374 | // If we have a zero rotate count, we have: |
| 375 | // M = mask(MB,ME) |
| 376 | // Op0 = (Op1 & ~M) | (Op2 & M) |
| 377 | // Change this to: |
| 378 | // M = mask((ME+1)&31, (MB-1)&31) |
| 379 | // Op0 = (Op2 & ~M) | (Op1 & M) |
| 380 | |
| 381 | // Swap op1/op2 |
Andrew Kaylor | 16c4da0 | 2015-09-28 20:33:22 +0000 | [diff] [blame] | 382 | assert(((OpIdx1 == 1 && OpIdx2 == 2) || (OpIdx1 == 2 && OpIdx2 == 1)) && |
| 383 | "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] | 384 | unsigned Reg0 = MI.getOperand(0).getReg(); |
| 385 | unsigned Reg1 = MI.getOperand(1).getReg(); |
| 386 | unsigned Reg2 = MI.getOperand(2).getReg(); |
| 387 | unsigned SubReg1 = MI.getOperand(1).getSubReg(); |
| 388 | unsigned SubReg2 = MI.getOperand(2).getSubReg(); |
| 389 | bool Reg1IsKill = MI.getOperand(1).isKill(); |
| 390 | bool Reg2IsKill = MI.getOperand(2).isKill(); |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 391 | bool ChangeReg0 = false; |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 392 | // If machine instrs are no longer in two-address forms, update |
| 393 | // destination register as well. |
| 394 | if (Reg0 == Reg1) { |
| 395 | // Must be two address instruction! |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 396 | assert(MI.getDesc().getOperandConstraint(0, MCOI::TIED_TO) && |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 397 | "Expecting a two-address instruction!"); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 398 | assert(MI.getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch"); |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 399 | Reg2IsKill = false; |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 400 | ChangeReg0 = true; |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 401 | } |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 402 | |
| 403 | // Masks. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 404 | unsigned MB = MI.getOperand(4).getImm(); |
| 405 | unsigned ME = MI.getOperand(5).getImm(); |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 406 | |
Hal Finkel | ccf9259 | 2015-09-06 04:17:30 +0000 | [diff] [blame] | 407 | // We can't commute a trivial mask (there is no way to represent an all-zero |
| 408 | // mask). |
| 409 | if (MB == 0 && ME == 31) |
| 410 | return nullptr; |
| 411 | |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 412 | if (NewMI) { |
| 413 | // Create a new instruction. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 414 | unsigned Reg0 = ChangeReg0 ? Reg2 : MI.getOperand(0).getReg(); |
| 415 | bool Reg0IsDead = MI.getOperand(0).isDead(); |
| 416 | return BuildMI(MF, MI.getDebugLoc(), MI.getDesc()) |
| 417 | .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead)) |
| 418 | .addReg(Reg2, getKillRegState(Reg2IsKill)) |
| 419 | .addReg(Reg1, getKillRegState(Reg1IsKill)) |
| 420 | .addImm((ME + 1) & 31) |
| 421 | .addImm((MB - 1) & 31); |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 424 | if (ChangeReg0) { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 425 | MI.getOperand(0).setReg(Reg2); |
| 426 | MI.getOperand(0).setSubReg(SubReg2); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 427 | } |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 428 | MI.getOperand(2).setReg(Reg1); |
| 429 | MI.getOperand(1).setReg(Reg2); |
| 430 | MI.getOperand(2).setSubReg(SubReg1); |
| 431 | MI.getOperand(1).setSubReg(SubReg2); |
| 432 | MI.getOperand(2).setIsKill(Reg1IsKill); |
| 433 | MI.getOperand(1).setIsKill(Reg2IsKill); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 434 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 435 | // Swap the mask around. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 436 | MI.getOperand(4).setImm((ME + 1) & 31); |
| 437 | MI.getOperand(5).setImm((MB - 1) & 31); |
| 438 | return &MI; |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 439 | } |
Chris Lattner | ea79d9fd73 | 2006-03-05 23:49:55 +0000 | [diff] [blame] | 440 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 441 | bool PPCInstrInfo::findCommutedOpIndices(MachineInstr &MI, unsigned &SrcOpIdx1, |
Hal Finkel | 6c32ff3 | 2014-03-25 19:26:43 +0000 | [diff] [blame] | 442 | unsigned &SrcOpIdx2) const { |
| 443 | // For VSX A-Type FMA instructions, it is the first two operands that can be |
| 444 | // commuted, however, because the non-encoded tied input operand is listed |
| 445 | // first, the operands to swap are actually the second and third. |
| 446 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 447 | int AltOpc = PPC::getAltVSXFMAOpcode(MI.getOpcode()); |
Hal Finkel | 6c32ff3 | 2014-03-25 19:26:43 +0000 | [diff] [blame] | 448 | if (AltOpc == -1) |
| 449 | return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2); |
| 450 | |
Andrew Kaylor | 16c4da0 | 2015-09-28 20:33:22 +0000 | [diff] [blame] | 451 | // The commutable operand indices are 2 and 3. Return them in SrcOpIdx1 |
| 452 | // and SrcOpIdx2. |
| 453 | return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2, 3); |
Hal Finkel | 6c32ff3 | 2014-03-25 19:26:43 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 456 | void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB, |
Chris Lattner | ea79d9fd73 | 2006-03-05 23:49:55 +0000 | [diff] [blame] | 457 | MachineBasicBlock::iterator MI) const { |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 458 | // This function is used for scheduling, and the nop wanted here is the type |
| 459 | // that terminates dispatch groups on the POWER cores. |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 460 | unsigned Directive = Subtarget.getDarwinDirective(); |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 461 | unsigned Opcode; |
| 462 | switch (Directive) { |
| 463 | default: Opcode = PPC::NOP; break; |
| 464 | case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break; |
| 465 | case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break; |
Will Schmidt | 970ff64 | 2014-06-26 13:36:19 +0000 | [diff] [blame] | 466 | 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] | 467 | // FIXME: Update when POWER9 scheduling model is ready. |
| 468 | case PPC::DIR_PWR9: Opcode = PPC::NOP_GT_PWR7; break; |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 469 | } |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 470 | |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 471 | DebugLoc DL; |
| 472 | BuildMI(MBB, MI, DL, get(Opcode)); |
| 473 | } |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 474 | |
Hans Wennborg | 9b9a535 | 2017-04-21 21:48:41 +0000 | [diff] [blame] | 475 | /// Return the noop instruction to use for a noop. |
| 476 | void PPCInstrInfo::getNoop(MCInst &NopInst) const { |
Joerg Sonnenberger | 7ee0f31 | 2014-08-08 19:13:23 +0000 | [diff] [blame] | 477 | NopInst.setOpcode(PPC::NOP); |
| 478 | } |
| 479 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 480 | // Branch analysis. |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 481 | // Note: If the condition register is set to CTR or CTR8 then this is a |
| 482 | // BDNZ (imm == 1) or BDZ (imm == 0) branch. |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 483 | bool PPCInstrInfo::analyzeBranch(MachineBasicBlock &MBB, |
| 484 | MachineBasicBlock *&TBB, |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 485 | MachineBasicBlock *&FBB, |
Evan Cheng | 64dfcac | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 486 | SmallVectorImpl<MachineOperand> &Cond, |
| 487 | bool AllowModify) const { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 488 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 489 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 490 | // 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] | 491 | MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); |
| 492 | if (I == MBB.end()) |
Dale Johannesen | 4244d12 | 2010-04-02 01:38:09 +0000 | [diff] [blame] | 493 | return false; |
Benjamin Kramer | 92861d7 | 2015-06-25 13:39:03 +0000 | [diff] [blame] | 494 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 495 | if (!isUnpredicatedTerminator(*I)) |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 496 | return false; |
| 497 | |
Hiroshi Inoue | ed1ffa4 | 2017-09-27 10:33:02 +0000 | [diff] [blame] | 498 | if (AllowModify) { |
| 499 | // If the BB ends with an unconditional branch to the fallthrough BB, |
| 500 | // we eliminate the branch instruction. |
| 501 | if (I->getOpcode() == PPC::B && |
| 502 | MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) { |
| 503 | I->eraseFromParent(); |
| 504 | |
| 505 | // We update iterator after deleting the last branch. |
| 506 | I = MBB.getLastNonDebugInstr(); |
| 507 | if (I == MBB.end() || !isUnpredicatedTerminator(*I)) |
| 508 | return false; |
| 509 | } |
| 510 | } |
| 511 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 512 | // Get the last instruction in the block. |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 513 | MachineInstr &LastInst = *I; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 514 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 515 | // If there is only one terminator instruction, process it. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 516 | if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) { |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 517 | if (LastInst.getOpcode() == PPC::B) { |
| 518 | if (!LastInst.getOperand(0).isMBB()) |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 519 | return true; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 520 | TBB = LastInst.getOperand(0).getMBB(); |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 521 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 522 | } else if (LastInst.getOpcode() == PPC::BCC) { |
| 523 | if (!LastInst.getOperand(2).isMBB()) |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 524 | return true; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 525 | // Block ends with fall-through condbranch. |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 526 | TBB = LastInst.getOperand(2).getMBB(); |
| 527 | Cond.push_back(LastInst.getOperand(0)); |
| 528 | Cond.push_back(LastInst.getOperand(1)); |
Chris Lattner | 23f22de | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 529 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 530 | } else if (LastInst.getOpcode() == PPC::BC) { |
| 531 | if (!LastInst.getOperand(1).isMBB()) |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 532 | return true; |
| 533 | // Block ends with fall-through condbranch. |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 534 | TBB = LastInst.getOperand(1).getMBB(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 535 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 536 | Cond.push_back(LastInst.getOperand(0)); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 537 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 538 | } else if (LastInst.getOpcode() == PPC::BCn) { |
| 539 | if (!LastInst.getOperand(1).isMBB()) |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 540 | return true; |
| 541 | // Block ends with fall-through condbranch. |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 542 | TBB = LastInst.getOperand(1).getMBB(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 543 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 544 | Cond.push_back(LastInst.getOperand(0)); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 545 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 546 | } else if (LastInst.getOpcode() == PPC::BDNZ8 || |
| 547 | LastInst.getOpcode() == PPC::BDNZ) { |
| 548 | if (!LastInst.getOperand(0).isMBB()) |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 549 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 550 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 551 | return true; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 552 | TBB = LastInst.getOperand(0).getMBB(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 553 | Cond.push_back(MachineOperand::CreateImm(1)); |
| 554 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 555 | true)); |
| 556 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 557 | } else if (LastInst.getOpcode() == PPC::BDZ8 || |
| 558 | LastInst.getOpcode() == PPC::BDZ) { |
| 559 | if (!LastInst.getOperand(0).isMBB()) |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 560 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 561 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 562 | return true; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 563 | TBB = LastInst.getOperand(0).getMBB(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 564 | Cond.push_back(MachineOperand::CreateImm(0)); |
| 565 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 566 | true)); |
| 567 | return false; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 568 | } |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 569 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 570 | // Otherwise, don't know what this is. |
| 571 | return true; |
| 572 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 573 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 574 | // 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] | 575 | MachineInstr &SecondLastInst = *I; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 576 | |
| 577 | // 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] | 578 | if (I != MBB.begin() && isUnpredicatedTerminator(*--I)) |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 579 | return true; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 580 | |
Chris Lattner | e026379 | 2006-11-17 22:14:47 +0000 | [diff] [blame] | 581 | // 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] | 582 | if (SecondLastInst.getOpcode() == PPC::BCC && |
| 583 | LastInst.getOpcode() == PPC::B) { |
| 584 | if (!SecondLastInst.getOperand(2).isMBB() || |
| 585 | !LastInst.getOperand(0).isMBB()) |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 586 | return true; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 587 | TBB = SecondLastInst.getOperand(2).getMBB(); |
| 588 | Cond.push_back(SecondLastInst.getOperand(0)); |
| 589 | Cond.push_back(SecondLastInst.getOperand(1)); |
| 590 | FBB = LastInst.getOperand(0).getMBB(); |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 591 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 592 | } else if (SecondLastInst.getOpcode() == PPC::BC && |
| 593 | LastInst.getOpcode() == PPC::B) { |
| 594 | if (!SecondLastInst.getOperand(1).isMBB() || |
| 595 | !LastInst.getOperand(0).isMBB()) |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +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(1).getMBB(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 598 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 599 | Cond.push_back(SecondLastInst.getOperand(0)); |
| 600 | FBB = LastInst.getOperand(0).getMBB(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 601 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 602 | } else if (SecondLastInst.getOpcode() == PPC::BCn && |
| 603 | LastInst.getOpcode() == PPC::B) { |
| 604 | if (!SecondLastInst.getOperand(1).isMBB() || |
| 605 | !LastInst.getOperand(0).isMBB()) |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 606 | return true; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 607 | TBB = SecondLastInst.getOperand(1).getMBB(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 608 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 609 | Cond.push_back(SecondLastInst.getOperand(0)); |
| 610 | FBB = LastInst.getOperand(0).getMBB(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 611 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 612 | } else if ((SecondLastInst.getOpcode() == PPC::BDNZ8 || |
| 613 | SecondLastInst.getOpcode() == PPC::BDNZ) && |
| 614 | LastInst.getOpcode() == PPC::B) { |
| 615 | if (!SecondLastInst.getOperand(0).isMBB() || |
| 616 | !LastInst.getOperand(0).isMBB()) |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 617 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 618 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 619 | return true; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 620 | TBB = SecondLastInst.getOperand(0).getMBB(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 621 | Cond.push_back(MachineOperand::CreateImm(1)); |
| 622 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 623 | true)); |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 624 | FBB = LastInst.getOperand(0).getMBB(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 625 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 626 | } else if ((SecondLastInst.getOpcode() == PPC::BDZ8 || |
| 627 | SecondLastInst.getOpcode() == PPC::BDZ) && |
| 628 | LastInst.getOpcode() == PPC::B) { |
| 629 | if (!SecondLastInst.getOperand(0).isMBB() || |
| 630 | !LastInst.getOperand(0).isMBB()) |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 631 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 632 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 633 | return true; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 634 | TBB = SecondLastInst.getOperand(0).getMBB(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 635 | Cond.push_back(MachineOperand::CreateImm(0)); |
| 636 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 637 | true)); |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 638 | FBB = LastInst.getOperand(0).getMBB(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 639 | return false; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 640 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 641 | |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 642 | // If the block ends with two PPC:Bs, handle it. The second one is not |
| 643 | // executed, so remove it. |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 644 | if (SecondLastInst.getOpcode() == PPC::B && LastInst.getOpcode() == PPC::B) { |
| 645 | if (!SecondLastInst.getOperand(0).isMBB()) |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 646 | return true; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 647 | TBB = SecondLastInst.getOperand(0).getMBB(); |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 648 | I = LastInst; |
Evan Cheng | 64dfcac | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 649 | if (AllowModify) |
| 650 | I->eraseFromParent(); |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 651 | return false; |
| 652 | } |
| 653 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 654 | // Otherwise, can't handle this. |
| 655 | return true; |
| 656 | } |
| 657 | |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 658 | unsigned PPCInstrInfo::removeBranch(MachineBasicBlock &MBB, |
Matt Arsenault | a2b036e | 2016-09-14 17:23:48 +0000 | [diff] [blame] | 659 | int *BytesRemoved) const { |
| 660 | assert(!BytesRemoved && "code size not handled"); |
| 661 | |
Benjamin Kramer | 92861d7 | 2015-06-25 13:39:03 +0000 | [diff] [blame] | 662 | MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); |
| 663 | if (I == MBB.end()) |
| 664 | return 0; |
| 665 | |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 666 | if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC && |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 667 | I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 668 | I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && |
| 669 | I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 670 | return 0; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 671 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 672 | // Remove the branch. |
| 673 | I->eraseFromParent(); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 674 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 675 | I = MBB.end(); |
| 676 | |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 677 | if (I == MBB.begin()) return 1; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 678 | --I; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 679 | if (I->getOpcode() != PPC::BCC && |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 680 | I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 681 | I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && |
| 682 | I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 683 | return 1; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 684 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 685 | // Remove the branch. |
| 686 | I->eraseFromParent(); |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 687 | return 2; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 690 | unsigned PPCInstrInfo::insertBranch(MachineBasicBlock &MBB, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 691 | MachineBasicBlock *TBB, |
| 692 | MachineBasicBlock *FBB, |
| 693 | ArrayRef<MachineOperand> Cond, |
Matt Arsenault | a2b036e | 2016-09-14 17:23:48 +0000 | [diff] [blame] | 694 | const DebugLoc &DL, |
| 695 | int *BytesAdded) const { |
Chris Lattner | a61f010 | 2006-10-17 18:06:55 +0000 | [diff] [blame] | 696 | // Shouldn't be a fall through. |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 697 | assert(TBB && "insertBranch must not be told to insert a fallthrough"); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 698 | assert((Cond.size() == 2 || Cond.size() == 0) && |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 699 | "PPC branch conditions have two components!"); |
Matt Arsenault | a2b036e | 2016-09-14 17:23:48 +0000 | [diff] [blame] | 700 | assert(!BytesAdded && "code size not handled"); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 701 | |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 702 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 703 | |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 704 | // One-way branch. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 705 | if (!FBB) { |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 706 | if (Cond.empty()) // Unconditional branch |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 707 | BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 708 | else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 709 | BuildMI(&MBB, DL, get(Cond[0].getImm() ? |
| 710 | (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : |
| 711 | (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 712 | else if (Cond[0].getImm() == PPC::PRED_BIT_SET) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 713 | BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 714 | else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 715 | BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB); |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 716 | else // Conditional branch |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 717 | BuildMI(&MBB, DL, get(PPC::BCC)) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 718 | .addImm(Cond[0].getImm()) |
| 719 | .add(Cond[1]) |
| 720 | .addMBB(TBB); |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 721 | return 1; |
Chris Lattner | a61f010 | 2006-10-17 18:06:55 +0000 | [diff] [blame] | 722 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 723 | |
Chris Lattner | d881660 | 2006-10-21 05:42:09 +0000 | [diff] [blame] | 724 | // Two-way Conditional Branch. |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 725 | if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 726 | BuildMI(&MBB, DL, get(Cond[0].getImm() ? |
| 727 | (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : |
| 728 | (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 729 | else if (Cond[0].getImm() == PPC::PRED_BIT_SET) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 730 | BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 731 | else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 732 | BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 733 | else |
| 734 | BuildMI(&MBB, DL, get(PPC::BCC)) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 735 | .addImm(Cond[0].getImm()) |
| 736 | .add(Cond[1]) |
| 737 | .addMBB(TBB); |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 738 | BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB); |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 739 | return 2; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 742 | // Select analysis. |
| 743 | bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB, |
Ahmed Bougacha | c88bf54 | 2015-06-11 19:30:37 +0000 | [diff] [blame] | 744 | ArrayRef<MachineOperand> Cond, |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 745 | unsigned TrueReg, unsigned FalseReg, |
| 746 | int &CondCycles, int &TrueCycles, int &FalseCycles) const { |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 747 | if (Cond.size() != 2) |
| 748 | return false; |
| 749 | |
| 750 | // If this is really a bdnz-like condition, then it cannot be turned into a |
| 751 | // select. |
| 752 | if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 753 | return false; |
| 754 | |
| 755 | // Check register classes. |
| 756 | const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 757 | const TargetRegisterClass *RC = |
| 758 | RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); |
| 759 | if (!RC) |
| 760 | return false; |
| 761 | |
| 762 | // isel is for regular integer GPRs only. |
| 763 | if (!PPC::GPRCRegClass.hasSubClassEq(RC) && |
Hal Finkel | 8e8618a | 2013-07-15 20:22:58 +0000 | [diff] [blame] | 764 | !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) && |
| 765 | !PPC::G8RCRegClass.hasSubClassEq(RC) && |
| 766 | !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 767 | return false; |
| 768 | |
| 769 | // FIXME: These numbers are for the A2, how well they work for other cores is |
| 770 | // an open question. On the A2, the isel instruction has a 2-cycle latency |
| 771 | // but single-cycle throughput. These numbers are used in combination with |
| 772 | // the MispredictPenalty setting from the active SchedMachineModel. |
| 773 | CondCycles = 1; |
| 774 | TrueCycles = 1; |
| 775 | FalseCycles = 1; |
| 776 | |
| 777 | return true; |
| 778 | } |
| 779 | |
| 780 | void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 781 | MachineBasicBlock::iterator MI, |
| 782 | const DebugLoc &dl, unsigned DestReg, |
| 783 | ArrayRef<MachineOperand> Cond, unsigned TrueReg, |
| 784 | unsigned FalseReg) const { |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 785 | assert(Cond.size() == 2 && |
| 786 | "PPC branch conditions have two components!"); |
| 787 | |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 788 | // Get the register classes. |
| 789 | MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 790 | const TargetRegisterClass *RC = |
| 791 | RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); |
| 792 | assert(RC && "TrueReg and FalseReg must have overlapping register classes"); |
Hal Finkel | 8e8618a | 2013-07-15 20:22:58 +0000 | [diff] [blame] | 793 | |
| 794 | bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) || |
| 795 | PPC::G8RC_NOX0RegClass.hasSubClassEq(RC); |
| 796 | assert((Is64Bit || |
| 797 | PPC::GPRCRegClass.hasSubClassEq(RC) || |
| 798 | PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) && |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 799 | "isel is for regular integer GPRs only"); |
| 800 | |
Hal Finkel | 8e8618a | 2013-07-15 20:22:58 +0000 | [diff] [blame] | 801 | unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL; |
Kyle Butt | cec4080 | 2016-01-12 21:00:43 +0000 | [diff] [blame] | 802 | auto SelectPred = static_cast<PPC::Predicate>(Cond[0].getImm()); |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 803 | |
Kyle Butt | 132bf36 | 2016-01-15 19:20:06 +0000 | [diff] [blame] | 804 | unsigned SubIdx = 0; |
| 805 | bool SwapOps = false; |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 806 | switch (SelectPred) { |
Kyle Butt | cec4080 | 2016-01-12 21:00:43 +0000 | [diff] [blame] | 807 | case PPC::PRED_EQ: |
| 808 | case PPC::PRED_EQ_MINUS: |
| 809 | case PPC::PRED_EQ_PLUS: |
| 810 | SubIdx = PPC::sub_eq; SwapOps = false; break; |
| 811 | case PPC::PRED_NE: |
| 812 | case PPC::PRED_NE_MINUS: |
| 813 | case PPC::PRED_NE_PLUS: |
| 814 | SubIdx = PPC::sub_eq; SwapOps = true; break; |
| 815 | case PPC::PRED_LT: |
| 816 | case PPC::PRED_LT_MINUS: |
| 817 | case PPC::PRED_LT_PLUS: |
| 818 | SubIdx = PPC::sub_lt; SwapOps = false; break; |
| 819 | case PPC::PRED_GE: |
| 820 | case PPC::PRED_GE_MINUS: |
| 821 | case PPC::PRED_GE_PLUS: |
| 822 | SubIdx = PPC::sub_lt; SwapOps = true; break; |
| 823 | case PPC::PRED_GT: |
| 824 | case PPC::PRED_GT_MINUS: |
| 825 | case PPC::PRED_GT_PLUS: |
| 826 | SubIdx = PPC::sub_gt; SwapOps = false; break; |
| 827 | case PPC::PRED_LE: |
| 828 | case PPC::PRED_LE_MINUS: |
| 829 | case PPC::PRED_LE_PLUS: |
| 830 | SubIdx = PPC::sub_gt; SwapOps = true; break; |
| 831 | case PPC::PRED_UN: |
| 832 | case PPC::PRED_UN_MINUS: |
| 833 | case PPC::PRED_UN_PLUS: |
| 834 | SubIdx = PPC::sub_un; SwapOps = false; break; |
| 835 | case PPC::PRED_NU: |
| 836 | case PPC::PRED_NU_MINUS: |
| 837 | case PPC::PRED_NU_PLUS: |
| 838 | SubIdx = PPC::sub_un; SwapOps = true; break; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 839 | case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break; |
| 840 | case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break; |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | unsigned FirstReg = SwapOps ? FalseReg : TrueReg, |
| 844 | SecondReg = SwapOps ? TrueReg : FalseReg; |
| 845 | |
| 846 | // The first input register of isel cannot be r0. If it is a member |
| 847 | // of a register class that can be r0, then copy it first (the |
| 848 | // register allocator should eliminate the copy). |
| 849 | if (MRI.getRegClass(FirstReg)->contains(PPC::R0) || |
| 850 | MRI.getRegClass(FirstReg)->contains(PPC::X0)) { |
| 851 | const TargetRegisterClass *FirstRC = |
| 852 | MRI.getRegClass(FirstReg)->contains(PPC::X0) ? |
| 853 | &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass; |
| 854 | unsigned OldFirstReg = FirstReg; |
| 855 | FirstReg = MRI.createVirtualRegister(FirstRC); |
| 856 | BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg) |
| 857 | .addReg(OldFirstReg); |
| 858 | } |
| 859 | |
| 860 | BuildMI(MBB, MI, dl, get(OpCode), DestReg) |
| 861 | .addReg(FirstReg).addReg(SecondReg) |
| 862 | .addReg(Cond[1].getReg(), 0, SubIdx); |
| 863 | } |
| 864 | |
Kit Barton | 535e69d | 2015-03-25 19:36:23 +0000 | [diff] [blame] | 865 | static unsigned getCRBitValue(unsigned CRBit) { |
| 866 | unsigned Ret = 4; |
| 867 | if (CRBit == PPC::CR0LT || CRBit == PPC::CR1LT || |
| 868 | CRBit == PPC::CR2LT || CRBit == PPC::CR3LT || |
| 869 | CRBit == PPC::CR4LT || CRBit == PPC::CR5LT || |
| 870 | CRBit == PPC::CR6LT || CRBit == PPC::CR7LT) |
| 871 | Ret = 3; |
| 872 | if (CRBit == PPC::CR0GT || CRBit == PPC::CR1GT || |
| 873 | CRBit == PPC::CR2GT || CRBit == PPC::CR3GT || |
| 874 | CRBit == PPC::CR4GT || CRBit == PPC::CR5GT || |
| 875 | CRBit == PPC::CR6GT || CRBit == PPC::CR7GT) |
| 876 | Ret = 2; |
| 877 | if (CRBit == PPC::CR0EQ || CRBit == PPC::CR1EQ || |
| 878 | CRBit == PPC::CR2EQ || CRBit == PPC::CR3EQ || |
| 879 | CRBit == PPC::CR4EQ || CRBit == PPC::CR5EQ || |
| 880 | CRBit == PPC::CR6EQ || CRBit == PPC::CR7EQ) |
| 881 | Ret = 1; |
| 882 | if (CRBit == PPC::CR0UN || CRBit == PPC::CR1UN || |
| 883 | CRBit == PPC::CR2UN || CRBit == PPC::CR3UN || |
| 884 | CRBit == PPC::CR4UN || CRBit == PPC::CR5UN || |
| 885 | CRBit == PPC::CR6UN || CRBit == PPC::CR7UN) |
| 886 | Ret = 0; |
| 887 | |
| 888 | assert(Ret != 4 && "Invalid CR bit register"); |
| 889 | return Ret; |
| 890 | } |
| 891 | |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 892 | void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 893 | MachineBasicBlock::iterator I, |
| 894 | const DebugLoc &DL, unsigned DestReg, |
| 895 | unsigned SrcReg, bool KillSrc) const { |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 896 | // 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] | 897 | // legalization. Promote them here. |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 898 | const TargetRegisterInfo *TRI = &getRegisterInfo(); |
| 899 | if (PPC::F8RCRegClass.contains(DestReg) && |
Hal Finkel | 5cedafb | 2015-02-16 23:46:30 +0000 | [diff] [blame] | 900 | PPC::VSRCRegClass.contains(SrcReg)) { |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 901 | unsigned SuperReg = |
| 902 | TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass); |
| 903 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 904 | if (VSXSelfCopyCrash && SrcReg == SuperReg) |
| 905 | llvm_unreachable("nop VSX copy"); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 906 | |
| 907 | DestReg = SuperReg; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 908 | } else if (PPC::F8RCRegClass.contains(SrcReg) && |
Hal Finkel | 5cedafb | 2015-02-16 23:46:30 +0000 | [diff] [blame] | 909 | PPC::VSRCRegClass.contains(DestReg)) { |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 910 | unsigned SuperReg = |
| 911 | TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass); |
| 912 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 913 | if (VSXSelfCopyCrash && DestReg == SuperReg) |
| 914 | llvm_unreachable("nop VSX copy"); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 915 | |
| 916 | SrcReg = SuperReg; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Kit Barton | 535e69d | 2015-03-25 19:36:23 +0000 | [diff] [blame] | 919 | // Different class register copy |
| 920 | if (PPC::CRBITRCRegClass.contains(SrcReg) && |
| 921 | PPC::GPRCRegClass.contains(DestReg)) { |
| 922 | unsigned CRReg = getCRFromCRBit(SrcReg); |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 923 | BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(CRReg); |
| 924 | getKillRegState(KillSrc); |
Kit Barton | 535e69d | 2015-03-25 19:36:23 +0000 | [diff] [blame] | 925 | // Rotate the CR bit in the CR fields to be the least significant bit and |
| 926 | // then mask with 0x1 (MB = ME = 31). |
| 927 | BuildMI(MBB, I, DL, get(PPC::RLWINM), DestReg) |
| 928 | .addReg(DestReg, RegState::Kill) |
| 929 | .addImm(TRI->getEncodingValue(CRReg) * 4 + (4 - getCRBitValue(SrcReg))) |
| 930 | .addImm(31) |
| 931 | .addImm(31); |
| 932 | return; |
| 933 | } else if (PPC::CRRCRegClass.contains(SrcReg) && |
| 934 | PPC::G8RCRegClass.contains(DestReg)) { |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 935 | BuildMI(MBB, I, DL, get(PPC::MFOCRF8), DestReg).addReg(SrcReg); |
| 936 | getKillRegState(KillSrc); |
Kit Barton | 535e69d | 2015-03-25 19:36:23 +0000 | [diff] [blame] | 937 | return; |
| 938 | } else if (PPC::CRRCRegClass.contains(SrcReg) && |
| 939 | PPC::GPRCRegClass.contains(DestReg)) { |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 940 | BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(SrcReg); |
| 941 | getKillRegState(KillSrc); |
Kit Barton | 535e69d | 2015-03-25 19:36:23 +0000 | [diff] [blame] | 942 | return; |
Zaara Syeda | fcd9697 | 2017-09-21 16:12:33 +0000 | [diff] [blame] | 943 | } else if (PPC::G8RCRegClass.contains(SrcReg) && |
| 944 | PPC::VSFRCRegClass.contains(DestReg)) { |
| 945 | BuildMI(MBB, I, DL, get(PPC::MTVSRD), DestReg).addReg(SrcReg); |
| 946 | NumGPRtoVSRSpill++; |
| 947 | getKillRegState(KillSrc); |
| 948 | return; |
| 949 | } else if (PPC::VSFRCRegClass.contains(SrcReg) && |
| 950 | PPC::G8RCRegClass.contains(DestReg)) { |
| 951 | BuildMI(MBB, I, DL, get(PPC::MFVSRD), DestReg).addReg(SrcReg); |
| 952 | getKillRegState(KillSrc); |
| 953 | return; |
| 954 | } |
Kit Barton | 535e69d | 2015-03-25 19:36:23 +0000 | [diff] [blame] | 955 | |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 956 | unsigned Opc; |
| 957 | if (PPC::GPRCRegClass.contains(DestReg, SrcReg)) |
| 958 | Opc = PPC::OR; |
| 959 | else if (PPC::G8RCRegClass.contains(DestReg, SrcReg)) |
| 960 | Opc = PPC::OR8; |
| 961 | else if (PPC::F4RCRegClass.contains(DestReg, SrcReg)) |
| 962 | Opc = PPC::FMR; |
| 963 | else if (PPC::CRRCRegClass.contains(DestReg, SrcReg)) |
| 964 | Opc = PPC::MCRF; |
| 965 | else if (PPC::VRRCRegClass.contains(DestReg, SrcReg)) |
| 966 | Opc = PPC::VOR; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 967 | else if (PPC::VSRCRegClass.contains(DestReg, SrcReg)) |
Hal Finkel | bbad233 | 2014-03-24 09:36:36 +0000 | [diff] [blame] | 968 | // There are two different ways this can be done: |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 969 | // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only |
| 970 | // issue in VSU pipeline 0. |
| 971 | // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but |
| 972 | // can go to either pipeline. |
Hal Finkel | bbad233 | 2014-03-24 09:36:36 +0000 | [diff] [blame] | 973 | // We'll always use xxlor here, because in practically all cases where |
| 974 | // copies are generated, they are close enough to some use that the |
| 975 | // lower-latency form is preferable. |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 976 | Opc = PPC::XXLOR; |
Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 977 | else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg) || |
| 978 | PPC::VSSRCRegClass.contains(DestReg, SrcReg)) |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 979 | Opc = PPC::XXLORf; |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 980 | else if (PPC::QFRCRegClass.contains(DestReg, SrcReg)) |
| 981 | Opc = PPC::QVFMR; |
| 982 | else if (PPC::QSRCRegClass.contains(DestReg, SrcReg)) |
| 983 | Opc = PPC::QVFMRs; |
| 984 | else if (PPC::QBRCRegClass.contains(DestReg, SrcReg)) |
| 985 | Opc = PPC::QVFMRb; |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 986 | else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg)) |
| 987 | Opc = PPC::CROR; |
| 988 | else |
| 989 | llvm_unreachable("Impossible reg-to-reg copy"); |
Owen Anderson | 7a73ae9 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 990 | |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 991 | const MCInstrDesc &MCID = get(Opc); |
| 992 | if (MCID.getNumOperands() == 3) |
| 993 | BuildMI(MBB, I, DL, MCID, DestReg) |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 994 | .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc)); |
| 995 | else |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 996 | BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc)); |
Owen Anderson | 7a73ae9 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Hal Finkel | 8f6834d | 2011-12-05 17:55:17 +0000 | [diff] [blame] | 999 | // 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] | 1000 | bool |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 1001 | PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF, |
| 1002 | unsigned SrcReg, bool isKill, |
Bill Wendling | c6c48fc | 2008-03-10 22:49:16 +0000 | [diff] [blame] | 1003 | int FrameIdx, |
| 1004 | const TargetRegisterClass *RC, |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1005 | SmallVectorImpl<MachineInstr*> &NewMIs, |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1006 | bool &NonRI, bool &SpillsVRS) const{ |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 1007 | // Note: If additional store instructions are added here, |
| 1008 | // update isStoreToStackSlot. |
| 1009 | |
Chris Lattner | 6f306d7 | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 1010 | DebugLoc DL; |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 1011 | if (PPC::GPRCRegClass.hasSubClassEq(RC) || |
| 1012 | PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 794e05b | 2013-03-23 17:14:27 +0000 | [diff] [blame] | 1013 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW)) |
| 1014 | .addReg(SrcReg, |
| 1015 | getKillRegState(isKill)), |
| 1016 | FrameIdx)); |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 1017 | } else if (PPC::G8RCRegClass.hasSubClassEq(RC) || |
| 1018 | PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 794e05b | 2013-03-23 17:14:27 +0000 | [diff] [blame] | 1019 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD)) |
| 1020 | .addReg(SrcReg, |
| 1021 | getKillRegState(isKill)), |
| 1022 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1023 | } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { |
Dale Johannesen | 6b8c76a | 2009-02-12 23:08:38 +0000 | [diff] [blame] | 1024 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD)) |
Bill Wendling | f7b83c7 | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 1025 | .addReg(SrcReg, |
| 1026 | getKillRegState(isKill)), |
| 1027 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1028 | } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { |
Dale Johannesen | 6b8c76a | 2009-02-12 23:08:38 +0000 | [diff] [blame] | 1029 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS)) |
Bill Wendling | f7b83c7 | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 1030 | .addReg(SrcReg, |
| 1031 | getKillRegState(isKill)), |
| 1032 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1033 | } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | e154c8f | 2013-03-12 14:12:16 +0000 | [diff] [blame] | 1034 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR)) |
| 1035 | .addReg(SrcReg, |
| 1036 | getKillRegState(isKill)), |
| 1037 | FrameIdx)); |
| 1038 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1039 | } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1040 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT)) |
| 1041 | .addReg(SrcReg, |
| 1042 | getKillRegState(isKill)), |
| 1043 | FrameIdx)); |
| 1044 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1045 | } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1046 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX)) |
| 1047 | .addReg(SrcReg, |
| 1048 | getKillRegState(isKill)), |
| 1049 | FrameIdx)); |
| 1050 | NonRI = true; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 1051 | } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { |
Lei Huang | 263dc4e | 2017-10-11 20:20:58 +0000 | [diff] [blame] | 1052 | unsigned Op = Subtarget.hasP9Vector() ? PPC::STXV : PPC::STXVD2X; |
Nemanja Ivanovic | 6e7879c | 2016-09-22 09:52:19 +0000 | [diff] [blame] | 1053 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op)) |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 1054 | .addReg(SrcReg, |
| 1055 | getKillRegState(isKill)), |
| 1056 | FrameIdx)); |
| 1057 | NonRI = true; |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 1058 | } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { |
Nemanja Ivanovic | 6354d23 | 2016-10-04 11:25:52 +0000 | [diff] [blame] | 1059 | unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFSTOREf64 : PPC::STXSDX; |
| 1060 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc)) |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 1061 | .addReg(SrcReg, |
| 1062 | getKillRegState(isKill)), |
| 1063 | FrameIdx)); |
| 1064 | NonRI = true; |
Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 1065 | } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) { |
Nemanja Ivanovic | 6354d23 | 2016-10-04 11:25:52 +0000 | [diff] [blame] | 1066 | unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFSTOREf32 : PPC::STXSSPX; |
| 1067 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc)) |
Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 1068 | .addReg(SrcReg, |
| 1069 | getKillRegState(isKill)), |
| 1070 | FrameIdx)); |
| 1071 | NonRI = true; |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 1072 | } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1073 | assert(Subtarget.isDarwin() && |
Hal Finkel | a7b0630 | 2013-03-27 00:02:20 +0000 | [diff] [blame] | 1074 | "VRSAVE only needs spill/restore on Darwin"); |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 1075 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE)) |
| 1076 | .addReg(SrcReg, |
| 1077 | getKillRegState(isKill)), |
| 1078 | FrameIdx)); |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1079 | SpillsVRS = true; |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 1080 | } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) { |
| 1081 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDX)) |
| 1082 | .addReg(SrcReg, |
| 1083 | getKillRegState(isKill)), |
| 1084 | FrameIdx)); |
| 1085 | NonRI = true; |
| 1086 | } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) { |
| 1087 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFSXs)) |
| 1088 | .addReg(SrcReg, |
| 1089 | getKillRegState(isKill)), |
| 1090 | FrameIdx)); |
| 1091 | NonRI = true; |
| 1092 | } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) { |
| 1093 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDXb)) |
| 1094 | .addReg(SrcReg, |
| 1095 | getKillRegState(isKill)), |
| 1096 | FrameIdx)); |
| 1097 | NonRI = true; |
Zaara Syeda | fcd9697 | 2017-09-21 16:12:33 +0000 | [diff] [blame] | 1098 | } else if (PPC::SPILLTOVSRRCRegClass.hasSubClassEq(RC)) { |
| 1099 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILLTOVSR_ST)) |
| 1100 | .addReg(SrcReg, |
| 1101 | getKillRegState(isKill)), |
| 1102 | FrameIdx)); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1103 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1104 | llvm_unreachable("Unknown regclass!"); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1105 | } |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 1106 | |
| 1107 | return false; |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | void |
| 1111 | PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 1112 | MachineBasicBlock::iterator MI, |
| 1113 | unsigned SrcReg, bool isKill, int FrameIdx, |
Evan Cheng | efb126a | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 1114 | const TargetRegisterClass *RC, |
| 1115 | const TargetRegisterInfo *TRI) const { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 1116 | MachineFunction &MF = *MBB.getParent(); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1117 | SmallVector<MachineInstr*, 4> NewMIs; |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 1118 | |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 1119 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 1120 | FuncInfo->setHasSpills(); |
| 1121 | |
Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 1122 | // We need to avoid a situation in which the value from a VRRC register is |
| 1123 | // spilled using an Altivec instruction and reloaded into a VSRC register |
| 1124 | // using a VSX instruction. The issue with this is that the VSX |
| 1125 | // load/store instructions swap the doublewords in the vector and the Altivec |
| 1126 | // ones don't. The register classes on the spill/reload may be different if |
| 1127 | // the register is defined using an Altivec instruction and is then used by a |
| 1128 | // VSX instruction. |
| 1129 | RC = updatedRC(RC); |
| 1130 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1131 | bool NonRI = false, SpillsVRS = false; |
| 1132 | if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs, |
| 1133 | NonRI, SpillsVRS)) |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 1134 | FuncInfo->setSpillsCR(); |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 1135 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1136 | if (SpillsVRS) |
| 1137 | FuncInfo->setSpillsVRSAVE(); |
| 1138 | |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1139 | if (NonRI) |
| 1140 | FuncInfo->setHasNonRISpills(); |
| 1141 | |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1142 | for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) |
| 1143 | MBB.insert(MI, NewMIs[i]); |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 1144 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1145 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 1146 | MachineMemOperand *MMO = MF.getMachineMemOperand( |
| 1147 | MachinePointerInfo::getFixedStack(MF, FrameIdx), |
| 1148 | MachineMemOperand::MOStore, MFI.getObjectSize(FrameIdx), |
| 1149 | MFI.getObjectAlignment(FrameIdx)); |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 1150 | NewMIs.back()->addMemOperand(MF, MMO); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 1153 | bool PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, const DebugLoc &DL, |
| 1154 | unsigned DestReg, int FrameIdx, |
| 1155 | const TargetRegisterClass *RC, |
| 1156 | SmallVectorImpl<MachineInstr *> &NewMIs, |
| 1157 | bool &NonRI, bool &SpillsVRS) const { |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 1158 | // Note: If additional load instructions are added here, |
| 1159 | // update isLoadFromStackSlot. |
| 1160 | |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 1161 | if (PPC::GPRCRegClass.hasSubClassEq(RC) || |
| 1162 | PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 5791f51 | 2013-03-27 19:10:40 +0000 | [diff] [blame] | 1163 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), |
| 1164 | DestReg), FrameIdx)); |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 1165 | } else if (PPC::G8RCRegClass.hasSubClassEq(RC) || |
| 1166 | PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 5791f51 | 2013-03-27 19:10:40 +0000 | [diff] [blame] | 1167 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg), |
| 1168 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1169 | } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 1170 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg), |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1171 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1172 | } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 1173 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg), |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1174 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1175 | } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | e154c8f | 2013-03-12 14:12:16 +0000 | [diff] [blame] | 1176 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, |
| 1177 | get(PPC::RESTORE_CR), DestReg), |
| 1178 | FrameIdx)); |
| 1179 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1180 | } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1181 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, |
| 1182 | get(PPC::RESTORE_CRBIT), DestReg), |
| 1183 | FrameIdx)); |
| 1184 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1185 | } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1186 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg), |
| 1187 | FrameIdx)); |
| 1188 | NonRI = true; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 1189 | } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { |
Lei Huang | 263dc4e | 2017-10-11 20:20:58 +0000 | [diff] [blame] | 1190 | unsigned Op = Subtarget.hasP9Vector() ? PPC::LXV : PPC::LXVD2X; |
Nemanja Ivanovic | 6e7879c | 2016-09-22 09:52:19 +0000 | [diff] [blame] | 1191 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op), DestReg), |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 1192 | FrameIdx)); |
| 1193 | NonRI = true; |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 1194 | } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { |
Nemanja Ivanovic | 6354d23 | 2016-10-04 11:25:52 +0000 | [diff] [blame] | 1195 | unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFLOADf64 : PPC::LXSDX; |
| 1196 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc), |
| 1197 | DestReg), FrameIdx)); |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 1198 | NonRI = true; |
Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 1199 | } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) { |
Nemanja Ivanovic | 6354d23 | 2016-10-04 11:25:52 +0000 | [diff] [blame] | 1200 | unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFLOADf32 : PPC::LXSSPX; |
| 1201 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc), |
| 1202 | DestReg), FrameIdx)); |
Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 1203 | NonRI = true; |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 1204 | } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1205 | assert(Subtarget.isDarwin() && |
Hal Finkel | a7b0630 | 2013-03-27 00:02:20 +0000 | [diff] [blame] | 1206 | "VRSAVE only needs spill/restore on Darwin"); |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 1207 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, |
| 1208 | get(PPC::RESTORE_VRSAVE), |
| 1209 | DestReg), |
| 1210 | FrameIdx)); |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1211 | SpillsVRS = true; |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 1212 | } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) { |
| 1213 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDX), DestReg), |
| 1214 | FrameIdx)); |
| 1215 | NonRI = true; |
| 1216 | } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) { |
| 1217 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFSXs), DestReg), |
| 1218 | FrameIdx)); |
| 1219 | NonRI = true; |
| 1220 | } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) { |
| 1221 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDXb), DestReg), |
| 1222 | FrameIdx)); |
| 1223 | NonRI = true; |
Zaara Syeda | fcd9697 | 2017-09-21 16:12:33 +0000 | [diff] [blame] | 1224 | } else if (PPC::SPILLTOVSRRCRegClass.hasSubClassEq(RC)) { |
| 1225 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILLTOVSR_LD), |
| 1226 | DestReg), FrameIdx)); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1227 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1228 | llvm_unreachable("Unknown regclass!"); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1229 | } |
Hal Finkel | bde7f8f | 2011-12-06 20:55:36 +0000 | [diff] [blame] | 1230 | |
| 1231 | return false; |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | void |
| 1235 | PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 1236 | MachineBasicBlock::iterator MI, |
| 1237 | unsigned DestReg, int FrameIdx, |
Evan Cheng | efb126a | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 1238 | const TargetRegisterClass *RC, |
| 1239 | const TargetRegisterInfo *TRI) const { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 1240 | MachineFunction &MF = *MBB.getParent(); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1241 | SmallVector<MachineInstr*, 4> NewMIs; |
Chris Lattner | 6f306d7 | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 1242 | DebugLoc DL; |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 1243 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1244 | |
| 1245 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 1246 | FuncInfo->setHasSpills(); |
| 1247 | |
Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 1248 | // We need to avoid a situation in which the value from a VRRC register is |
| 1249 | // spilled using an Altivec instruction and reloaded into a VSRC register |
| 1250 | // using a VSX instruction. The issue with this is that the VSX |
| 1251 | // load/store instructions swap the doublewords in the vector and the Altivec |
| 1252 | // ones don't. The register classes on the spill/reload may be different if |
| 1253 | // the register is defined using an Altivec instruction and is then used by a |
| 1254 | // VSX instruction. |
| 1255 | if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass) |
| 1256 | RC = &PPC::VSRCRegClass; |
| 1257 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1258 | bool NonRI = false, SpillsVRS = false; |
| 1259 | if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs, |
| 1260 | NonRI, SpillsVRS)) |
Hal Finkel | bde7f8f | 2011-12-06 20:55:36 +0000 | [diff] [blame] | 1261 | FuncInfo->setSpillsCR(); |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1262 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1263 | if (SpillsVRS) |
| 1264 | FuncInfo->setSpillsVRSAVE(); |
| 1265 | |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1266 | if (NonRI) |
| 1267 | FuncInfo->setHasNonRISpills(); |
| 1268 | |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1269 | for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) |
| 1270 | MBB.insert(MI, NewMIs[i]); |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 1271 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1272 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 1273 | MachineMemOperand *MMO = MF.getMachineMemOperand( |
| 1274 | MachinePointerInfo::getFixedStack(MF, FrameIdx), |
| 1275 | MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIdx), |
| 1276 | MFI.getObjectAlignment(FrameIdx)); |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 1277 | NewMIs.back()->addMemOperand(MF, MMO); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 1280 | bool PPCInstrInfo:: |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 1281 | reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { |
Chris Lattner | 23f22de | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 1282 | assert(Cond.size() == 2 && "Invalid PPC branch opcode!"); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 1283 | if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR) |
| 1284 | Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0); |
| 1285 | else |
| 1286 | // Leave the CR# the same, but invert the condition. |
| 1287 | Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm())); |
Chris Lattner | 23f22de | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 1288 | return false; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 1289 | } |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1290 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1291 | bool PPCInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, |
| 1292 | unsigned Reg, MachineRegisterInfo *MRI) const { |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1293 | // For some instructions, it is legal to fold ZERO into the RA register field. |
| 1294 | // 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] | 1295 | unsigned DefOpc = DefMI.getOpcode(); |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1296 | if (DefOpc != PPC::LI && DefOpc != PPC::LI8) |
| 1297 | return false; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1298 | if (!DefMI.getOperand(1).isImm()) |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1299 | return false; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1300 | if (DefMI.getOperand(1).getImm() != 0) |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1301 | return false; |
| 1302 | |
| 1303 | // Note that we cannot here invert the arguments of an isel in order to fold |
| 1304 | // a ZERO into what is presented as the second argument. All we have here |
| 1305 | // is the condition bit, and that might come from a CR-logical bit operation. |
| 1306 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1307 | const MCInstrDesc &UseMCID = UseMI.getDesc(); |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1308 | |
| 1309 | // Only fold into real machine instructions. |
| 1310 | if (UseMCID.isPseudo()) |
| 1311 | return false; |
| 1312 | |
| 1313 | unsigned UseIdx; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1314 | for (UseIdx = 0; UseIdx < UseMI.getNumOperands(); ++UseIdx) |
| 1315 | if (UseMI.getOperand(UseIdx).isReg() && |
| 1316 | UseMI.getOperand(UseIdx).getReg() == Reg) |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1317 | break; |
| 1318 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1319 | assert(UseIdx < UseMI.getNumOperands() && "Cannot find Reg in UseMI"); |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1320 | assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg"); |
| 1321 | |
| 1322 | const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx]; |
| 1323 | |
| 1324 | // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0 |
| 1325 | // register (which might also be specified as a pointer class kind). |
| 1326 | if (UseInfo->isLookupPtrRegClass()) { |
| 1327 | if (UseInfo->RegClass /* Kind */ != 1) |
| 1328 | return false; |
| 1329 | } else { |
| 1330 | if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID && |
| 1331 | UseInfo->RegClass != PPC::G8RC_NOX0RegClassID) |
| 1332 | return false; |
| 1333 | } |
| 1334 | |
| 1335 | // Make sure this is not tied to an output register (or otherwise |
| 1336 | // constrained). This is true for ST?UX registers, for example, which |
| 1337 | // are tied to their output registers. |
| 1338 | if (UseInfo->Constraints != 0) |
| 1339 | return false; |
| 1340 | |
| 1341 | unsigned ZeroReg; |
| 1342 | if (UseInfo->isLookupPtrRegClass()) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1343 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1344 | ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO; |
| 1345 | } else { |
| 1346 | ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ? |
| 1347 | PPC::ZERO8 : PPC::ZERO; |
| 1348 | } |
| 1349 | |
| 1350 | bool DeleteDef = MRI->hasOneNonDBGUse(Reg); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1351 | UseMI.getOperand(UseIdx).setReg(ZeroReg); |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1352 | |
| 1353 | if (DeleteDef) |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1354 | DefMI.eraseFromParent(); |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1355 | |
| 1356 | return true; |
| 1357 | } |
| 1358 | |
Hal Finkel | 30ae229 | 2013-04-10 18:30:16 +0000 | [diff] [blame] | 1359 | static bool MBBDefinesCTR(MachineBasicBlock &MBB) { |
| 1360 | for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end(); |
| 1361 | I != IE; ++I) |
| 1362 | if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8)) |
| 1363 | return true; |
| 1364 | return false; |
| 1365 | } |
| 1366 | |
| 1367 | // We should make sure that, if we're going to predicate both sides of a |
| 1368 | // condition (a diamond), that both sides don't define the counter register. We |
| 1369 | // can predicate counter-decrement-based branches, but while that predicates |
| 1370 | // the branching, it does not predicate the counter decrement. If we tried to |
| 1371 | // merge the triangle into one predicated block, we'd decrement the counter |
| 1372 | // twice. |
| 1373 | bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB, |
| 1374 | unsigned NumT, unsigned ExtraT, |
| 1375 | MachineBasicBlock &FMBB, |
| 1376 | unsigned NumF, unsigned ExtraF, |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 1377 | BranchProbability Probability) const { |
Hal Finkel | 30ae229 | 2013-04-10 18:30:16 +0000 | [diff] [blame] | 1378 | return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB)); |
| 1379 | } |
| 1380 | |
| 1381 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1382 | bool PPCInstrInfo::isPredicated(const MachineInstr &MI) const { |
Hal Finkel | f29285a | 2013-04-11 01:23:34 +0000 | [diff] [blame] | 1383 | // The predicated branches are identified by their type, not really by the |
| 1384 | // explicit presence of a predicate. Furthermore, some of them can be |
| 1385 | // predicated more than once. Because if conversion won't try to predicate |
| 1386 | // any instruction which already claims to be predicated (by returning true |
| 1387 | // here), always return false. In doing so, we let isPredicable() be the |
| 1388 | // final word on whether not the instruction can be (further) predicated. |
| 1389 | |
| 1390 | return false; |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1393 | bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const { |
| 1394 | if (!MI.isTerminator()) |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1395 | return false; |
| 1396 | |
| 1397 | // Conditional branch is a special case. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1398 | if (MI.isBranch() && !MI.isBarrier()) |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1399 | return true; |
| 1400 | |
| 1401 | return !isPredicated(MI); |
| 1402 | } |
| 1403 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1404 | bool PPCInstrInfo::PredicateInstruction(MachineInstr &MI, |
Ahmed Bougacha | c88bf54 | 2015-06-11 19:30:37 +0000 | [diff] [blame] | 1405 | ArrayRef<MachineOperand> Pred) const { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1406 | unsigned OpC = MI.getOpcode(); |
Hal Finkel | f4a22c0 | 2015-01-13 17:47:54 +0000 | [diff] [blame] | 1407 | if (OpC == PPC::BLR || OpC == PPC::BLR8) { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1408 | if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1409 | bool isPPC64 = Subtarget.isPPC64(); |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1410 | MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) |
| 1411 | : (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR))); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1412 | } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1413 | MI.setDesc(get(PPC::BCLR)); |
| 1414 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 1415 | .addReg(Pred[1].getReg()); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1416 | } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1417 | MI.setDesc(get(PPC::BCLRn)); |
| 1418 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 1419 | .addReg(Pred[1].getReg()); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1420 | } else { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1421 | MI.setDesc(get(PPC::BCCLR)); |
| 1422 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 1423 | .addImm(Pred[0].getImm()) |
| 1424 | .addReg(Pred[1].getReg()); |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | return true; |
| 1428 | } else if (OpC == PPC::B) { |
| 1429 | if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1430 | bool isPPC64 = Subtarget.isPPC64(); |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1431 | MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) |
| 1432 | : (isPPC64 ? PPC::BDZ8 : PPC::BDZ))); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1433 | } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1434 | MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); |
| 1435 | MI.RemoveOperand(0); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1436 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1437 | MI.setDesc(get(PPC::BC)); |
| 1438 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 1439 | .addReg(Pred[1].getReg()) |
| 1440 | .addMBB(MBB); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1441 | } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1442 | MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); |
| 1443 | MI.RemoveOperand(0); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1444 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1445 | MI.setDesc(get(PPC::BCn)); |
| 1446 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 1447 | .addReg(Pred[1].getReg()) |
| 1448 | .addMBB(MBB); |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1449 | } else { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1450 | MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); |
| 1451 | MI.RemoveOperand(0); |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1452 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1453 | MI.setDesc(get(PPC::BCC)); |
| 1454 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 1455 | .addImm(Pred[0].getImm()) |
| 1456 | .addReg(Pred[1].getReg()) |
| 1457 | .addMBB(MBB); |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | return true; |
Hal Finkel | 500b004 | 2013-04-10 06:42:34 +0000 | [diff] [blame] | 1461 | } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 || |
| 1462 | OpC == PPC::BCTRL || OpC == PPC::BCTRL8) { |
| 1463 | if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) |
| 1464 | llvm_unreachable("Cannot predicate bctr[l] on the ctr register"); |
| 1465 | |
| 1466 | bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8; |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1467 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1468 | |
| 1469 | if (Pred[0].getImm() == PPC::PRED_BIT_SET) { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1470 | MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) |
| 1471 | : (setLR ? PPC::BCCTRL : PPC::BCCTR))); |
| 1472 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 1473 | .addReg(Pred[1].getReg()); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1474 | return true; |
| 1475 | } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1476 | MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) |
| 1477 | : (setLR ? PPC::BCCTRLn : PPC::BCCTRn))); |
| 1478 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 1479 | .addReg(Pred[1].getReg()); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1480 | return true; |
| 1481 | } |
| 1482 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1483 | MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) |
| 1484 | : (setLR ? PPC::BCCCTRL : PPC::BCCCTR))); |
| 1485 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 1486 | .addImm(Pred[0].getImm()) |
| 1487 | .addReg(Pred[1].getReg()); |
Hal Finkel | 500b004 | 2013-04-10 06:42:34 +0000 | [diff] [blame] | 1488 | return true; |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | return false; |
| 1492 | } |
| 1493 | |
Ahmed Bougacha | c88bf54 | 2015-06-11 19:30:37 +0000 | [diff] [blame] | 1494 | bool PPCInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1, |
| 1495 | ArrayRef<MachineOperand> Pred2) const { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1496 | assert(Pred1.size() == 2 && "Invalid PPC first predicate"); |
| 1497 | assert(Pred2.size() == 2 && "Invalid PPC second predicate"); |
| 1498 | |
| 1499 | if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR) |
| 1500 | return false; |
| 1501 | if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR) |
| 1502 | return false; |
| 1503 | |
Hal Finkel | 94a6f38 | 2013-12-11 23:12:25 +0000 | [diff] [blame] | 1504 | // P1 can only subsume P2 if they test the same condition register. |
| 1505 | if (Pred1[1].getReg() != Pred2[1].getReg()) |
| 1506 | return false; |
| 1507 | |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1508 | PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm(); |
| 1509 | PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm(); |
| 1510 | |
| 1511 | if (P1 == P2) |
| 1512 | return true; |
| 1513 | |
| 1514 | // Does P1 subsume P2, e.g. GE subsumes GT. |
| 1515 | if (P1 == PPC::PRED_LE && |
| 1516 | (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ)) |
| 1517 | return true; |
| 1518 | if (P1 == PPC::PRED_GE && |
| 1519 | (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ)) |
| 1520 | return true; |
| 1521 | |
| 1522 | return false; |
| 1523 | } |
| 1524 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1525 | bool PPCInstrInfo::DefinesPredicate(MachineInstr &MI, |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1526 | std::vector<MachineOperand> &Pred) const { |
| 1527 | // Note: At the present time, the contents of Pred from this function is |
| 1528 | // unused by IfConversion. This implementation follows ARM by pushing the |
| 1529 | // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of |
| 1530 | // predicate, instructions defining CTR or CTR8 are also included as |
| 1531 | // predicate-defining instructions. |
| 1532 | |
| 1533 | const TargetRegisterClass *RCs[] = |
| 1534 | { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass, |
| 1535 | &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass }; |
| 1536 | |
| 1537 | bool Found = false; |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1538 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 1539 | const MachineOperand &MO = MI.getOperand(i); |
Hal Finkel | af82201 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1540 | for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1541 | const TargetRegisterClass *RC = RCs[c]; |
Hal Finkel | af82201 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1542 | if (MO.isReg()) { |
| 1543 | if (MO.isDef() && RC->contains(MO.getReg())) { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1544 | Pred.push_back(MO); |
| 1545 | Found = true; |
| 1546 | } |
Hal Finkel | af82201 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1547 | } else if (MO.isRegMask()) { |
| 1548 | for (TargetRegisterClass::iterator I = RC->begin(), |
| 1549 | IE = RC->end(); I != IE; ++I) |
| 1550 | if (MO.clobbersPhysReg(*I)) { |
| 1551 | Pred.push_back(MO); |
| 1552 | Found = true; |
| 1553 | } |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1554 | } |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | return Found; |
| 1559 | } |
| 1560 | |
Krzysztof Parzyszek | cc31871 | 2017-03-03 18:30:54 +0000 | [diff] [blame] | 1561 | bool PPCInstrInfo::isPredicable(const MachineInstr &MI) const { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1562 | unsigned OpC = MI.getOpcode(); |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1563 | switch (OpC) { |
| 1564 | default: |
| 1565 | return false; |
| 1566 | case PPC::B: |
| 1567 | case PPC::BLR: |
Hal Finkel | f4a22c0 | 2015-01-13 17:47:54 +0000 | [diff] [blame] | 1568 | case PPC::BLR8: |
Hal Finkel | 500b004 | 2013-04-10 06:42:34 +0000 | [diff] [blame] | 1569 | case PPC::BCTR: |
| 1570 | case PPC::BCTR8: |
| 1571 | case PPC::BCTRL: |
| 1572 | case PPC::BCTRL8: |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1573 | return true; |
| 1574 | } |
| 1575 | } |
| 1576 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1577 | bool PPCInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg, |
| 1578 | unsigned &SrcReg2, int &Mask, |
| 1579 | int &Value) const { |
| 1580 | unsigned Opc = MI.getOpcode(); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1581 | |
| 1582 | switch (Opc) { |
| 1583 | default: return false; |
| 1584 | case PPC::CMPWI: |
| 1585 | case PPC::CMPLWI: |
| 1586 | case PPC::CMPDI: |
| 1587 | case PPC::CMPLDI: |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1588 | SrcReg = MI.getOperand(1).getReg(); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1589 | SrcReg2 = 0; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1590 | Value = MI.getOperand(2).getImm(); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1591 | Mask = 0xFFFF; |
| 1592 | return true; |
| 1593 | case PPC::CMPW: |
| 1594 | case PPC::CMPLW: |
| 1595 | case PPC::CMPD: |
| 1596 | case PPC::CMPLD: |
| 1597 | case PPC::FCMPUS: |
| 1598 | case PPC::FCMPUD: |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1599 | SrcReg = MI.getOperand(1).getReg(); |
| 1600 | SrcReg2 = MI.getOperand(2).getReg(); |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1601 | Value = 0; |
| 1602 | Mask = 0; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1603 | return true; |
| 1604 | } |
| 1605 | } |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1606 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1607 | bool PPCInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, unsigned SrcReg, |
| 1608 | unsigned SrcReg2, int Mask, int Value, |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1609 | const MachineRegisterInfo *MRI) const { |
Hal Finkel | b12da6b | 2013-04-18 22:54:25 +0000 | [diff] [blame] | 1610 | if (DisableCmpOpt) |
| 1611 | return false; |
| 1612 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1613 | int OpC = CmpInstr.getOpcode(); |
| 1614 | unsigned CRReg = CmpInstr.getOperand(0).getReg(); |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1615 | |
| 1616 | // FP record forms set CR1 based on the execption status bits, not a |
| 1617 | // comparison with zero. |
| 1618 | if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD) |
| 1619 | return false; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1620 | |
| 1621 | // The record forms set the condition register based on a signed comparison |
| 1622 | // with zero (so says the ISA manual). This is not as straightforward as it |
| 1623 | // seems, however, because this is always a 64-bit comparison on PPC64, even |
| 1624 | // for instructions that are 32-bit in nature (like slw for example). |
| 1625 | // So, on PPC32, for unsigned comparisons, we can use the record forms only |
| 1626 | // for equality checks (as those don't depend on the sign). On PPC64, |
| 1627 | // we are restricted to equality for unsigned 64-bit comparisons and for |
| 1628 | // signed 32-bit comparisons the applicability is more restricted. |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1629 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1630 | bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW; |
| 1631 | bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW; |
| 1632 | bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD; |
| 1633 | |
| 1634 | // Get the unique definition of SrcReg. |
| 1635 | MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg); |
| 1636 | if (!MI) return false; |
| 1637 | int MIOpC = MI->getOpcode(); |
| 1638 | |
| 1639 | bool equalityOnly = false; |
| 1640 | bool noSub = false; |
| 1641 | if (isPPC64) { |
| 1642 | if (is32BitSignedCompare) { |
| 1643 | // We can perform this optimization only if MI is sign-extending. |
| 1644 | if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo || |
| 1645 | MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo || |
| 1646 | MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo || |
| 1647 | MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo || |
| 1648 | MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) { |
| 1649 | noSub = true; |
| 1650 | } else |
| 1651 | return false; |
| 1652 | } else if (is32BitUnsignedCompare) { |
Kyle Butt | 6131128 | 2016-03-23 19:51:22 +0000 | [diff] [blame] | 1653 | // 32-bit rotate and mask instructions are zero extending only if MB <= ME |
| 1654 | bool isZeroExtendingRotate = |
| 1655 | (MIOpC == PPC::RLWINM || MIOpC == PPC::RLWINMo || |
| 1656 | MIOpC == PPC::RLWNM || MIOpC == PPC::RLWNMo) |
| 1657 | && MI->getOperand(3).getImm() <= MI->getOperand(4).getImm(); |
| 1658 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1659 | // We can perform this optimization, equality only, if MI is |
| 1660 | // zero-extending. |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1661 | // FIXME: Other possible target instructions include ANDISo and |
| 1662 | // RLWINM aliases, such as ROTRWI, EXTLWI, SLWI and SRWI. |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1663 | if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo || |
| 1664 | MIOpC == PPC::SLW || MIOpC == PPC::SLWo || |
Kyle Butt | 6131128 | 2016-03-23 19:51:22 +0000 | [diff] [blame] | 1665 | MIOpC == PPC::SRW || MIOpC == PPC::SRWo || |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1666 | MIOpC == PPC::ANDIo || |
Kyle Butt | 6131128 | 2016-03-23 19:51:22 +0000 | [diff] [blame] | 1667 | isZeroExtendingRotate) { |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1668 | noSub = true; |
| 1669 | equalityOnly = true; |
| 1670 | } else |
| 1671 | return false; |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1672 | } else |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1673 | equalityOnly = is64BitUnsignedCompare; |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1674 | } else |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1675 | equalityOnly = is32BitUnsignedCompare; |
| 1676 | |
| 1677 | if (equalityOnly) { |
| 1678 | // We need to check the uses of the condition register in order to reject |
| 1679 | // non-equality comparisons. |
Hiroshi Inoue | 393ef84 | 2017-07-18 13:31:40 +0000 | [diff] [blame] | 1680 | for (MachineRegisterInfo::use_instr_iterator |
| 1681 | I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end(); |
| 1682 | I != IE; ++I) { |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1683 | MachineInstr *UseMI = &*I; |
| 1684 | if (UseMI->getOpcode() == PPC::BCC) { |
Hiroshi Inoue | 967dc58 | 2017-07-27 08:14:48 +0000 | [diff] [blame] | 1685 | PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm(); |
| 1686 | unsigned PredCond = PPC::getPredicateCondition(Pred); |
| 1687 | // We ignore hint bits when checking for non-equality comparisons. |
| 1688 | if (PredCond != PPC::PRED_EQ && PredCond != PPC::PRED_NE) |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1689 | return false; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1690 | } else if (UseMI->getOpcode() == PPC::ISEL || |
| 1691 | UseMI->getOpcode() == PPC::ISEL8) { |
| 1692 | unsigned SubIdx = UseMI->getOperand(3).getSubReg(); |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1693 | if (SubIdx != PPC::sub_eq) |
| 1694 | return false; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1695 | } else |
| 1696 | return false; |
| 1697 | } |
| 1698 | } |
| 1699 | |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1700 | MachineBasicBlock::iterator I = CmpInstr; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1701 | |
| 1702 | // 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] | 1703 | for (MachineBasicBlock::iterator EL = CmpInstr.getParent()->end(); I != EL; |
| 1704 | ++I) { |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1705 | bool FoundUse = false; |
Hiroshi Inoue | 393ef84 | 2017-07-18 13:31:40 +0000 | [diff] [blame] | 1706 | for (MachineRegisterInfo::use_instr_iterator |
| 1707 | J = MRI->use_instr_begin(CRReg), JE = MRI->use_instr_end(); |
| 1708 | J != JE; ++J) |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1709 | if (&*J == &*I) { |
| 1710 | FoundUse = true; |
| 1711 | break; |
| 1712 | } |
| 1713 | |
| 1714 | if (FoundUse) |
| 1715 | break; |
| 1716 | } |
| 1717 | |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1718 | SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate; |
| 1719 | SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate; |
| 1720 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1721 | // There are two possible candidates which can be changed to set CR[01]. |
| 1722 | // One is MI, the other is a SUB instruction. |
| 1723 | // 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] | 1724 | MachineInstr *Sub = nullptr; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1725 | if (SrcReg2 != 0) |
| 1726 | // MI is not a candidate for CMPrr. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1727 | MI = nullptr; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1728 | // FIXME: Conservatively refuse to convert an instruction which isn't in the |
| 1729 | // same BB as the comparison. This is to allow the check below to avoid calls |
| 1730 | // (and other explicit clobbers); instead we should really check for these |
| 1731 | // more explicitly (in at least a few predecessors). |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1732 | else if (MI->getParent() != CmpInstr.getParent()) |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1733 | return false; |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1734 | else if (Value != 0) { |
| 1735 | // The record-form instructions set CR bit based on signed comparison against 0. |
| 1736 | // We try to convert a compare against 1 or -1 into a compare against 0. |
| 1737 | bool Success = false; |
| 1738 | if (!equalityOnly && MRI->hasOneUse(CRReg)) { |
| 1739 | MachineInstr *UseMI = &*MRI->use_instr_begin(CRReg); |
| 1740 | if (UseMI->getOpcode() == PPC::BCC) { |
| 1741 | PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm(); |
Hiroshi Inoue | 967dc58 | 2017-07-27 08:14:48 +0000 | [diff] [blame] | 1742 | unsigned PredCond = PPC::getPredicateCondition(Pred); |
| 1743 | unsigned PredHint = PPC::getPredicateHint(Pred); |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1744 | int16_t Immed = (int16_t)Value; |
| 1745 | |
Hiroshi Inoue | 967dc58 | 2017-07-27 08:14:48 +0000 | [diff] [blame] | 1746 | // When modyfing the condition in the predicate, we propagate hint bits |
| 1747 | // from the original predicate to the new one. |
| 1748 | if (Immed == -1 && PredCond == PPC::PRED_GT) { |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1749 | // We convert "greater than -1" into "greater than or equal to 0", |
| 1750 | // since we are assuming signed comparison by !equalityOnly |
| 1751 | PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)), |
Hiroshi Inoue | 967dc58 | 2017-07-27 08:14:48 +0000 | [diff] [blame] | 1752 | PPC::getPredicate(PPC::PRED_GE, PredHint))); |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1753 | Success = true; |
| 1754 | } |
Hiroshi Inoue | 967dc58 | 2017-07-27 08:14:48 +0000 | [diff] [blame] | 1755 | else if (Immed == 1 && PredCond == PPC::PRED_LT) { |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1756 | // We convert "less than 1" into "less than or equal to 0". |
| 1757 | PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)), |
Hiroshi Inoue | 967dc58 | 2017-07-27 08:14:48 +0000 | [diff] [blame] | 1758 | PPC::getPredicate(PPC::PRED_LE, PredHint))); |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1759 | Success = true; |
| 1760 | } |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | // PPC does not have a record-form SUBri. |
| 1765 | if (!Success) |
| 1766 | return false; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | // Search for Sub. |
| 1770 | const TargetRegisterInfo *TRI = &getRegisterInfo(); |
| 1771 | --I; |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1772 | |
| 1773 | // Get ready to iterate backward from CmpInstr. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1774 | MachineBasicBlock::iterator E = MI, B = CmpInstr.getParent()->begin(); |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1775 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1776 | for (; I != E && !noSub; --I) { |
| 1777 | const MachineInstr &Instr = *I; |
| 1778 | unsigned IOpC = Instr.getOpcode(); |
| 1779 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1780 | if (&*I != &CmpInstr && (Instr.modifiesRegister(PPC::CR0, TRI) || |
| 1781 | Instr.readsRegister(PPC::CR0, TRI))) |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1782 | // This instruction modifies or uses the record condition register after |
| 1783 | // the one we want to change. While we could do this transformation, it |
| 1784 | // would likely not be profitable. This transformation removes one |
| 1785 | // instruction, and so even forcing RA to generate one move probably |
| 1786 | // makes it unprofitable. |
| 1787 | return false; |
| 1788 | |
| 1789 | // Check whether CmpInstr can be made redundant by the current instruction. |
| 1790 | if ((OpC == PPC::CMPW || OpC == PPC::CMPLW || |
| 1791 | OpC == PPC::CMPD || OpC == PPC::CMPLD) && |
| 1792 | (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) && |
| 1793 | ((Instr.getOperand(1).getReg() == SrcReg && |
| 1794 | Instr.getOperand(2).getReg() == SrcReg2) || |
| 1795 | (Instr.getOperand(1).getReg() == SrcReg2 && |
| 1796 | Instr.getOperand(2).getReg() == SrcReg))) { |
| 1797 | Sub = &*I; |
| 1798 | break; |
| 1799 | } |
| 1800 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1801 | if (I == B) |
| 1802 | // The 'and' is below the comparison instruction. |
| 1803 | return false; |
| 1804 | } |
| 1805 | |
| 1806 | // Return false if no candidates exist. |
| 1807 | if (!MI && !Sub) |
| 1808 | return false; |
| 1809 | |
| 1810 | // The single candidate is called MI. |
| 1811 | if (!MI) MI = Sub; |
| 1812 | |
| 1813 | int NewOpC = -1; |
| 1814 | MIOpC = MI->getOpcode(); |
| 1815 | if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8) |
| 1816 | NewOpC = MIOpC; |
| 1817 | else { |
| 1818 | NewOpC = PPC::getRecordFormOpcode(MIOpC); |
| 1819 | if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1) |
| 1820 | NewOpC = MIOpC; |
| 1821 | } |
| 1822 | |
| 1823 | // FIXME: On the non-embedded POWER architectures, only some of the record |
| 1824 | // forms are fast, and we should use only the fast ones. |
| 1825 | |
| 1826 | // The defining instruction has a record form (or is already a record |
| 1827 | // form). It is possible, however, that we'll need to reverse the condition |
| 1828 | // code of the users. |
| 1829 | if (NewOpC == -1) |
| 1830 | return false; |
| 1831 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1832 | // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP |
| 1833 | // needs to be updated to be based on SUB. Push the condition code |
| 1834 | // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the |
| 1835 | // condition code of these operands will be modified. |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1836 | // Here, Value == 0 means we haven't converted comparison against 1 or -1 to |
| 1837 | // comparison against 0, which may modify predicate. |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1838 | bool ShouldSwap = false; |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1839 | if (Sub && Value == 0) { |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1840 | ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 && |
| 1841 | Sub->getOperand(2).getReg() == SrcReg; |
| 1842 | |
| 1843 | // The operands to subf are the opposite of sub, so only in the fixed-point |
| 1844 | // case, invert the order. |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1845 | ShouldSwap = !ShouldSwap; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | if (ShouldSwap) |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1849 | for (MachineRegisterInfo::use_instr_iterator |
| 1850 | I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end(); |
| 1851 | I != IE; ++I) { |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1852 | MachineInstr *UseMI = &*I; |
| 1853 | if (UseMI->getOpcode() == PPC::BCC) { |
| 1854 | PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm(); |
Hiroshi Inoue | 967dc58 | 2017-07-27 08:14:48 +0000 | [diff] [blame] | 1855 | unsigned PredCond = PPC::getPredicateCondition(Pred); |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1856 | assert((!equalityOnly || |
Hiroshi Inoue | 967dc58 | 2017-07-27 08:14:48 +0000 | [diff] [blame] | 1857 | PredCond == PPC::PRED_EQ || PredCond == PPC::PRED_NE) && |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1858 | "Invalid predicate for equality-only optimization"); |
Hiroshi Inoue | 967dc58 | 2017-07-27 08:14:48 +0000 | [diff] [blame] | 1859 | (void)PredCond; // To suppress warning in release build. |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1860 | PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)), |
Hal Finkel | 0f64e21 | 2013-04-20 05:16:26 +0000 | [diff] [blame] | 1861 | PPC::getSwappedPredicate(Pred))); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1862 | } else if (UseMI->getOpcode() == PPC::ISEL || |
| 1863 | UseMI->getOpcode() == PPC::ISEL8) { |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1864 | unsigned NewSubReg = UseMI->getOperand(3).getSubReg(); |
| 1865 | assert((!equalityOnly || NewSubReg == PPC::sub_eq) && |
| 1866 | "Invalid CR bit for equality-only optimization"); |
| 1867 | |
| 1868 | if (NewSubReg == PPC::sub_lt) |
| 1869 | NewSubReg = PPC::sub_gt; |
| 1870 | else if (NewSubReg == PPC::sub_gt) |
| 1871 | NewSubReg = PPC::sub_lt; |
| 1872 | |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1873 | SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)), |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1874 | NewSubReg)); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1875 | } else // We need to abort on a user we don't understand. |
| 1876 | return false; |
| 1877 | } |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1878 | assert(!(Value != 0 && ShouldSwap) && |
| 1879 | "Non-zero immediate support and ShouldSwap" |
| 1880 | "may conflict in updating predicate"); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1881 | |
| 1882 | // Create a new virtual register to hold the value of the CR set by the |
| 1883 | // record-form instruction. If the instruction was not previously in |
| 1884 | // 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] | 1885 | CmpInstr.eraseFromParent(); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1886 | |
| 1887 | MachineBasicBlock::iterator MII = MI; |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1888 | BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(), |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1889 | get(TargetOpcode::COPY), CRReg) |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1890 | .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1891 | |
Chuang-Yu Cheng | 94f58e7 | 2016-04-12 03:10:52 +0000 | [diff] [blame] | 1892 | // Even if CR0 register were dead before, it is alive now since the |
| 1893 | // instruction we just built uses it. |
| 1894 | MI->clearRegisterDeads(PPC::CR0); |
| 1895 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1896 | if (MIOpC != NewOpC) { |
| 1897 | // We need to be careful here: we're replacing one instruction with |
| 1898 | // another, and we need to make sure that we get all of the right |
| 1899 | // implicit uses and defs. On the other hand, the caller may be holding |
| 1900 | // an iterator to this instruction, and so we can't delete it (this is |
| 1901 | // specifically the case if this is the instruction directly after the |
| 1902 | // compare). |
| 1903 | |
| 1904 | const MCInstrDesc &NewDesc = get(NewOpC); |
| 1905 | MI->setDesc(NewDesc); |
| 1906 | |
| 1907 | if (NewDesc.ImplicitDefs) |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 1908 | for (const MCPhysReg *ImpDefs = NewDesc.getImplicitDefs(); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1909 | *ImpDefs; ++ImpDefs) |
| 1910 | if (!MI->definesRegister(*ImpDefs)) |
| 1911 | MI->addOperand(*MI->getParent()->getParent(), |
| 1912 | MachineOperand::CreateReg(*ImpDefs, true, true)); |
| 1913 | if (NewDesc.ImplicitUses) |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 1914 | for (const MCPhysReg *ImpUses = NewDesc.getImplicitUses(); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1915 | *ImpUses; ++ImpUses) |
| 1916 | if (!MI->readsRegister(*ImpUses)) |
| 1917 | MI->addOperand(*MI->getParent()->getParent(), |
| 1918 | MachineOperand::CreateReg(*ImpUses, false, true)); |
| 1919 | } |
Keno Fischer | 5573483 | 2016-06-01 20:31:07 +0000 | [diff] [blame] | 1920 | assert(MI->definesRegister(PPC::CR0) && |
| 1921 | "Record-form instruction does not define cr0?"); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1922 | |
| 1923 | // Modify the condition code of operands in OperandsToUpdate. |
| 1924 | // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to |
| 1925 | // 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] | 1926 | for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++) |
| 1927 | PredsToUpdate[i].first->setImm(PredsToUpdate[i].second); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1928 | |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1929 | for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++) |
| 1930 | SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1931 | |
| 1932 | return true; |
| 1933 | } |
| 1934 | |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1935 | /// GetInstSize - Return the number of bytes of code the specified |
| 1936 | /// instruction may be. This returns the maximum number of bytes. |
| 1937 | /// |
Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 1938 | unsigned PPCInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1939 | unsigned Opcode = MI.getOpcode(); |
Hal Finkel | a7bbaf6 | 2014-02-02 06:12:27 +0000 | [diff] [blame] | 1940 | |
| 1941 | if (Opcode == PPC::INLINEASM) { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1942 | const MachineFunction *MF = MI.getParent()->getParent(); |
| 1943 | const char *AsmStr = MI.getOperand(0).getSymbolName(); |
Chris Lattner | 7b26fce | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 1944 | return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); |
Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 1945 | } else if (Opcode == TargetOpcode::STACKMAP) { |
Philip Reames | e83c4b3 | 2016-08-23 23:33:29 +0000 | [diff] [blame] | 1946 | StackMapOpers Opers(&MI); |
| 1947 | return Opers.getNumPatchBytes(); |
Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 1948 | } else if (Opcode == TargetOpcode::PATCHPOINT) { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1949 | PatchPointOpers Opers(&MI); |
Philip Reames | e83c4b3 | 2016-08-23 23:33:29 +0000 | [diff] [blame] | 1950 | return Opers.getNumPatchBytes(); |
Hal Finkel | a7bbaf6 | 2014-02-02 06:12:27 +0000 | [diff] [blame] | 1951 | } else { |
Eric Christopher | f48ef33 | 2017-03-27 22:40:51 +0000 | [diff] [blame] | 1952 | return get(Opcode).getSize(); |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1953 | } |
| 1954 | } |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1955 | |
Hal Finkel | 2d55698 | 2015-08-30 07:50:35 +0000 | [diff] [blame] | 1956 | std::pair<unsigned, unsigned> |
| 1957 | PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const { |
| 1958 | const unsigned Mask = PPCII::MO_ACCESS_MASK; |
| 1959 | return std::make_pair(TF & Mask, TF & ~Mask); |
| 1960 | } |
| 1961 | |
| 1962 | ArrayRef<std::pair<unsigned, const char *>> |
| 1963 | PPCInstrInfo::getSerializableDirectMachineOperandTargetFlags() const { |
| 1964 | using namespace PPCII; |
Hal Finkel | 982e8d4 | 2015-08-30 08:07:29 +0000 | [diff] [blame] | 1965 | static const std::pair<unsigned, const char *> TargetFlags[] = { |
Hal Finkel | 2d55698 | 2015-08-30 07:50:35 +0000 | [diff] [blame] | 1966 | {MO_LO, "ppc-lo"}, |
| 1967 | {MO_HA, "ppc-ha"}, |
| 1968 | {MO_TPREL_LO, "ppc-tprel-lo"}, |
| 1969 | {MO_TPREL_HA, "ppc-tprel-ha"}, |
| 1970 | {MO_DTPREL_LO, "ppc-dtprel-lo"}, |
| 1971 | {MO_TLSLD_LO, "ppc-tlsld-lo"}, |
| 1972 | {MO_TOC_LO, "ppc-toc-lo"}, |
| 1973 | {MO_TLS, "ppc-tls"}}; |
| 1974 | return makeArrayRef(TargetFlags); |
| 1975 | } |
| 1976 | |
| 1977 | ArrayRef<std::pair<unsigned, const char *>> |
| 1978 | PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const { |
| 1979 | using namespace PPCII; |
Hal Finkel | 982e8d4 | 2015-08-30 08:07:29 +0000 | [diff] [blame] | 1980 | static const std::pair<unsigned, const char *> TargetFlags[] = { |
Rafael Espindola | a99ccfc | 2016-06-29 14:59:50 +0000 | [diff] [blame] | 1981 | {MO_PLT, "ppc-plt"}, |
Hal Finkel | 2d55698 | 2015-08-30 07:50:35 +0000 | [diff] [blame] | 1982 | {MO_PIC_FLAG, "ppc-pic"}, |
| 1983 | {MO_NLP_FLAG, "ppc-nlp"}, |
| 1984 | {MO_NLP_HIDDEN_FLAG, "ppc-nlp-hidden"}}; |
| 1985 | return makeArrayRef(TargetFlags); |
| 1986 | } |
| 1987 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1988 | bool PPCInstrInfo::expandPostRAPseudo(MachineInstr &MI) const { |
Tim Shen | 3bef27c | 2017-05-16 20:18:06 +0000 | [diff] [blame] | 1989 | auto &MBB = *MI.getParent(); |
| 1990 | auto DL = MI.getDebugLoc(); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1991 | switch (MI.getOpcode()) { |
Tim Shen | a1d8bc5 | 2016-04-19 20:14:52 +0000 | [diff] [blame] | 1992 | case TargetOpcode::LOAD_STACK_GUARD: { |
| 1993 | assert(Subtarget.isTargetLinux() && |
| 1994 | "Only Linux target is expected to contain LOAD_STACK_GUARD"); |
| 1995 | const int64_t Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008; |
| 1996 | const unsigned Reg = Subtarget.isPPC64() ? PPC::X13 : PPC::R2; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1997 | MI.setDesc(get(Subtarget.isPPC64() ? PPC::LD : PPC::LWZ)); |
| 1998 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
Tim Shen | a1d8bc5 | 2016-04-19 20:14:52 +0000 | [diff] [blame] | 1999 | .addImm(Offset) |
| 2000 | .addReg(Reg); |
| 2001 | return true; |
| 2002 | } |
Nemanja Ivanovic | 6354d23 | 2016-10-04 11:25:52 +0000 | [diff] [blame] | 2003 | case PPC::DFLOADf32: |
| 2004 | case PPC::DFLOADf64: |
| 2005 | case PPC::DFSTOREf32: |
| 2006 | case PPC::DFSTOREf64: { |
| 2007 | assert(Subtarget.hasP9Vector() && |
| 2008 | "Invalid D-Form Pseudo-ops on non-P9 target."); |
Hiroshi Inoue | e3c14eb | 2017-05-29 07:12:39 +0000 | [diff] [blame] | 2009 | assert(MI.getOperand(2).isReg() && MI.getOperand(1).isImm() && |
| 2010 | "D-form op must have register and immediate operands"); |
Nemanja Ivanovic | 6354d23 | 2016-10-04 11:25:52 +0000 | [diff] [blame] | 2011 | unsigned UpperOpcode, LowerOpcode; |
| 2012 | switch (MI.getOpcode()) { |
| 2013 | case PPC::DFLOADf32: |
| 2014 | UpperOpcode = PPC::LXSSP; |
| 2015 | LowerOpcode = PPC::LFS; |
| 2016 | break; |
| 2017 | case PPC::DFLOADf64: |
| 2018 | UpperOpcode = PPC::LXSD; |
| 2019 | LowerOpcode = PPC::LFD; |
| 2020 | break; |
| 2021 | case PPC::DFSTOREf32: |
| 2022 | UpperOpcode = PPC::STXSSP; |
| 2023 | LowerOpcode = PPC::STFS; |
| 2024 | break; |
| 2025 | case PPC::DFSTOREf64: |
| 2026 | UpperOpcode = PPC::STXSD; |
| 2027 | LowerOpcode = PPC::STFD; |
| 2028 | break; |
| 2029 | } |
| 2030 | unsigned TargetReg = MI.getOperand(0).getReg(); |
| 2031 | unsigned Opcode; |
| 2032 | if ((TargetReg >= PPC::F0 && TargetReg <= PPC::F31) || |
| 2033 | (TargetReg >= PPC::VSL0 && TargetReg <= PPC::VSL31)) |
| 2034 | Opcode = LowerOpcode; |
| 2035 | else |
| 2036 | Opcode = UpperOpcode; |
| 2037 | MI.setDesc(get(Opcode)); |
| 2038 | return true; |
| 2039 | } |
Zaara Syeda | fcd9697 | 2017-09-21 16:12:33 +0000 | [diff] [blame] | 2040 | case PPC::SPILLTOVSR_LD: { |
| 2041 | unsigned TargetReg = MI.getOperand(0).getReg(); |
| 2042 | if (PPC::VSFRCRegClass.contains(TargetReg)) { |
| 2043 | MI.setDesc(get(PPC::DFLOADf64)); |
| 2044 | return expandPostRAPseudo(MI); |
| 2045 | } |
| 2046 | else |
| 2047 | MI.setDesc(get(PPC::LD)); |
| 2048 | return true; |
| 2049 | } |
| 2050 | case PPC::SPILLTOVSR_ST: { |
| 2051 | unsigned SrcReg = MI.getOperand(0).getReg(); |
| 2052 | if (PPC::VSFRCRegClass.contains(SrcReg)) { |
| 2053 | NumStoreSPILLVSRRCAsVec++; |
| 2054 | MI.setDesc(get(PPC::DFSTOREf64)); |
| 2055 | return expandPostRAPseudo(MI); |
| 2056 | } else { |
| 2057 | NumStoreSPILLVSRRCAsGpr++; |
| 2058 | MI.setDesc(get(PPC::STD)); |
| 2059 | } |
| 2060 | return true; |
| 2061 | } |
| 2062 | case PPC::SPILLTOVSR_LDX: { |
| 2063 | unsigned TargetReg = MI.getOperand(0).getReg(); |
| 2064 | if (PPC::VSFRCRegClass.contains(TargetReg)) |
| 2065 | MI.setDesc(get(PPC::LXSDX)); |
| 2066 | else |
| 2067 | MI.setDesc(get(PPC::LDX)); |
| 2068 | return true; |
| 2069 | } |
| 2070 | case PPC::SPILLTOVSR_STX: { |
| 2071 | unsigned SrcReg = MI.getOperand(0).getReg(); |
| 2072 | if (PPC::VSFRCRegClass.contains(SrcReg)) { |
| 2073 | NumStoreSPILLVSRRCAsVec++; |
| 2074 | MI.setDesc(get(PPC::STXSDX)); |
| 2075 | } else { |
| 2076 | NumStoreSPILLVSRRCAsGpr++; |
| 2077 | MI.setDesc(get(PPC::STDX)); |
| 2078 | } |
| 2079 | return true; |
| 2080 | } |
| 2081 | |
Tim Shen | 3bef27c | 2017-05-16 20:18:06 +0000 | [diff] [blame] | 2082 | case PPC::CFENCE8: { |
| 2083 | auto Val = MI.getOperand(0).getReg(); |
Hiroshi Inoue | 7a08bb1 | 2017-06-15 16:51:28 +0000 | [diff] [blame] | 2084 | BuildMI(MBB, MI, DL, get(PPC::CMPD), PPC::CR7).addReg(Val).addReg(Val); |
Tim Shen | 3bef27c | 2017-05-16 20:18:06 +0000 | [diff] [blame] | 2085 | BuildMI(MBB, MI, DL, get(PPC::CTRL_DEP)) |
| 2086 | .addImm(PPC::PRED_NE_MINUS) |
| 2087 | .addReg(PPC::CR7) |
| 2088 | .addImm(1); |
| 2089 | MI.setDesc(get(PPC::ISYNC)); |
| 2090 | MI.RemoveOperand(0); |
| 2091 | return true; |
| 2092 | } |
Tim Shen | a1d8bc5 | 2016-04-19 20:14:52 +0000 | [diff] [blame] | 2093 | } |
| 2094 | return false; |
| 2095 | } |
Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 2096 | |
| 2097 | const TargetRegisterClass * |
| 2098 | PPCInstrInfo::updatedRC(const TargetRegisterClass *RC) const { |
| 2099 | if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass) |
| 2100 | return &PPC::VSRCRegClass; |
| 2101 | return RC; |
| 2102 | } |
Nemanja Ivanovic | e597bd8 | 2017-05-31 05:40:25 +0000 | [diff] [blame] | 2103 | |
| 2104 | int PPCInstrInfo::getRecordFormOpcode(unsigned Opcode) { |
| 2105 | return PPC::getRecordFormOpcode(Opcode); |
| 2106 | } |
Hiroshi Inoue | e3a3e3c | 2017-10-16 04:12:57 +0000 | [diff] [blame] | 2107 | |
| 2108 | // This function returns true if the machine instruction |
| 2109 | // always outputs a value by sign-extending a 32 bit value, |
| 2110 | // i.e. 0 to 31-th bits are same as 32-th bit. |
| 2111 | static bool isSignExtendingOp(const MachineInstr &MI) { |
| 2112 | int Opcode = MI.getOpcode(); |
| 2113 | if (Opcode == PPC::LI || Opcode == PPC::LI8 || |
| 2114 | Opcode == PPC::LIS || Opcode == PPC::LIS8 || |
| 2115 | Opcode == PPC::SRAW || Opcode == PPC::SRAWo || |
| 2116 | Opcode == PPC::SRAWI || Opcode == PPC::SRAWIo || |
| 2117 | Opcode == PPC::LWA || Opcode == PPC::LWAX || |
| 2118 | Opcode == PPC::LWA_32 || Opcode == PPC::LWAX_32 || |
| 2119 | Opcode == PPC::LHA || Opcode == PPC::LHAX || |
| 2120 | Opcode == PPC::LHA8 || Opcode == PPC::LHAX8 || |
| 2121 | Opcode == PPC::LBZ || Opcode == PPC::LBZX || |
| 2122 | Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 || |
| 2123 | Opcode == PPC::LBZU || Opcode == PPC::LBZUX || |
| 2124 | Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 || |
| 2125 | Opcode == PPC::LHZ || Opcode == PPC::LHZX || |
| 2126 | Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 || |
| 2127 | Opcode == PPC::LHZU || Opcode == PPC::LHZUX || |
| 2128 | Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8 || |
| 2129 | Opcode == PPC::EXTSB || Opcode == PPC::EXTSBo || |
| 2130 | Opcode == PPC::EXTSH || Opcode == PPC::EXTSHo || |
| 2131 | Opcode == PPC::EXTSB8 || Opcode == PPC::EXTSH8 || |
| 2132 | Opcode == PPC::EXTSW || Opcode == PPC::EXTSWo || |
| 2133 | Opcode == PPC::EXTSH8_32_64 || Opcode == PPC::EXTSW_32_64 || |
| 2134 | Opcode == PPC::EXTSB8_32_64) |
| 2135 | return true; |
| 2136 | |
| 2137 | if (Opcode == PPC::RLDICL && MI.getOperand(3).getImm() >= 33) |
| 2138 | return true; |
| 2139 | |
| 2140 | if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINMo || |
| 2141 | Opcode == PPC::RLWNM || Opcode == PPC::RLWNMo) && |
| 2142 | MI.getOperand(3).getImm() > 0 && |
| 2143 | MI.getOperand(3).getImm() <= MI.getOperand(4).getImm()) |
| 2144 | return true; |
| 2145 | |
| 2146 | return false; |
| 2147 | } |
| 2148 | |
| 2149 | // This function returns true if the machine instruction |
| 2150 | // always outputs zeros in higher 32 bits. |
| 2151 | static bool isZeroExtendingOp(const MachineInstr &MI) { |
| 2152 | int Opcode = MI.getOpcode(); |
| 2153 | // The 16-bit immediate is sign-extended in li/lis. |
| 2154 | // If the most significant bit is zero, all higher bits are zero. |
| 2155 | if (Opcode == PPC::LI || Opcode == PPC::LI8 || |
| 2156 | Opcode == PPC::LIS || Opcode == PPC::LIS8) { |
| 2157 | int64_t Imm = MI.getOperand(1).getImm(); |
| 2158 | if (((uint64_t)Imm & ~0x7FFFuLL) == 0) |
| 2159 | return true; |
| 2160 | } |
| 2161 | |
| 2162 | // We have some variations of rotate-and-mask instructions |
| 2163 | // that clear higher 32-bits. |
| 2164 | if ((Opcode == PPC::RLDICL || Opcode == PPC::RLDICLo || |
| 2165 | Opcode == PPC::RLDCL || Opcode == PPC::RLDCLo || |
| 2166 | Opcode == PPC::RLDICL_32_64) && |
| 2167 | MI.getOperand(3).getImm() >= 32) |
| 2168 | return true; |
| 2169 | |
| 2170 | if ((Opcode == PPC::RLDIC || Opcode == PPC::RLDICo) && |
| 2171 | MI.getOperand(3).getImm() >= 32 && |
| 2172 | MI.getOperand(3).getImm() <= 63 - MI.getOperand(2).getImm()) |
| 2173 | return true; |
| 2174 | |
| 2175 | if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINMo || |
| 2176 | Opcode == PPC::RLWNM || Opcode == PPC::RLWNMo || |
| 2177 | Opcode == PPC::RLWINM8 || Opcode == PPC::RLWNM8) && |
| 2178 | MI.getOperand(3).getImm() <= MI.getOperand(4).getImm()) |
| 2179 | return true; |
| 2180 | |
| 2181 | // There are other instructions that clear higher 32-bits. |
| 2182 | if (Opcode == PPC::CNTLZW || Opcode == PPC::CNTLZWo || |
| 2183 | Opcode == PPC::CNTTZW || Opcode == PPC::CNTTZWo || |
| 2184 | Opcode == PPC::CNTLZW8 || Opcode == PPC::CNTTZW8 || |
| 2185 | Opcode == PPC::CNTLZD || Opcode == PPC::CNTLZDo || |
| 2186 | Opcode == PPC::CNTTZD || Opcode == PPC::CNTTZDo || |
| 2187 | Opcode == PPC::POPCNTD || Opcode == PPC::POPCNTW || |
| 2188 | Opcode == PPC::SLW || Opcode == PPC::SLWo || |
| 2189 | Opcode == PPC::SRW || Opcode == PPC::SRWo || |
| 2190 | Opcode == PPC::SLW8 || Opcode == PPC::SRW8 || |
| 2191 | Opcode == PPC::SLWI || Opcode == PPC::SLWIo || |
| 2192 | Opcode == PPC::SRWI || Opcode == PPC::SRWIo || |
| 2193 | Opcode == PPC::LWZ || Opcode == PPC::LWZX || |
| 2194 | Opcode == PPC::LWZU || Opcode == PPC::LWZUX || |
| 2195 | Opcode == PPC::LWBRX || Opcode == PPC::LHBRX || |
| 2196 | Opcode == PPC::LHZ || Opcode == PPC::LHZX || |
| 2197 | Opcode == PPC::LHZU || Opcode == PPC::LHZUX || |
| 2198 | Opcode == PPC::LBZ || Opcode == PPC::LBZX || |
| 2199 | Opcode == PPC::LBZU || Opcode == PPC::LBZUX || |
| 2200 | Opcode == PPC::LWZ8 || Opcode == PPC::LWZX8 || |
| 2201 | Opcode == PPC::LWZU8 || Opcode == PPC::LWZUX8 || |
| 2202 | Opcode == PPC::LWBRX8 || Opcode == PPC::LHBRX8 || |
| 2203 | Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 || |
| 2204 | Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8 || |
| 2205 | Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 || |
| 2206 | Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 || |
| 2207 | Opcode == PPC::ANDIo || Opcode == PPC::ANDISo || |
| 2208 | Opcode == PPC::ROTRWI || Opcode == PPC::ROTRWIo || |
| 2209 | Opcode == PPC::EXTLWI || Opcode == PPC::EXTLWIo || |
| 2210 | Opcode == PPC::MFVSRWZ) |
| 2211 | return true; |
| 2212 | |
| 2213 | return false; |
| 2214 | } |
| 2215 | |
| 2216 | // We limit the max depth to track incoming values of PHIs or binary ops |
| 2217 | // (e.g. AND) to avoid exsessive cost. |
| 2218 | const unsigned MAX_DEPTH = 1; |
| 2219 | |
| 2220 | bool |
| 2221 | PPCInstrInfo::isSignOrZeroExtended(const MachineInstr &MI, bool SignExt, |
| 2222 | const unsigned Depth) const { |
| 2223 | const MachineFunction *MF = MI.getParent()->getParent(); |
| 2224 | const MachineRegisterInfo *MRI = &MF->getRegInfo(); |
| 2225 | |
| 2226 | switch (MI.getOpcode()) { |
| 2227 | case PPC::COPY: { |
| 2228 | unsigned SrcReg = MI.getOperand(1).getReg(); |
| 2229 | |
| 2230 | // In both ELFv1 and v2 ABI, method parameters and the return value |
| 2231 | // are sign- or zero-extended. |
| 2232 | if (MF->getSubtarget<PPCSubtarget>().isSVR4ABI()) { |
| 2233 | const PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>(); |
| 2234 | // We check the ZExt/SExt flags for a method parameter. |
| 2235 | if (MI.getParent()->getBasicBlock() == |
| 2236 | &MF->getFunction()->getEntryBlock()) { |
| 2237 | unsigned VReg = MI.getOperand(0).getReg(); |
| 2238 | if (MF->getRegInfo().isLiveIn(VReg)) |
| 2239 | return SignExt ? FuncInfo->isLiveInSExt(VReg) : |
| 2240 | FuncInfo->isLiveInZExt(VReg); |
| 2241 | } |
| 2242 | |
| 2243 | // For a method return value, we check the ZExt/SExt flags in attribute. |
| 2244 | // We assume the following code sequence for method call. |
| 2245 | // ADJCALLSTACKDOWN 32, %R1<imp-def,dead>, %R1<imp-use> |
| 2246 | // BL8_NOP <ga:@func>,... |
| 2247 | // ADJCALLSTACKUP 32, 0, %R1<imp-def,dead>, %R1<imp-use> |
| 2248 | // %vreg5<def> = COPY %X3; G8RC:%vreg5 |
| 2249 | if (SrcReg == PPC::X3) { |
| 2250 | const MachineBasicBlock *MBB = MI.getParent(); |
| 2251 | MachineBasicBlock::const_instr_iterator II = |
| 2252 | MachineBasicBlock::const_instr_iterator(&MI); |
| 2253 | if (II != MBB->instr_begin() && |
| 2254 | (--II)->getOpcode() == PPC::ADJCALLSTACKUP) { |
| 2255 | const MachineInstr &CallMI = *(--II); |
| 2256 | if (CallMI.isCall() && CallMI.getOperand(0).isGlobal()) { |
| 2257 | const Function *CalleeFn = |
| 2258 | dyn_cast<Function>(CallMI.getOperand(0).getGlobal()); |
Hiroshi Inoue | a7eb78b | 2017-10-16 12:11:15 +0000 | [diff] [blame^] | 2259 | if (!CalleeFn) |
| 2260 | return false; |
Hiroshi Inoue | e3a3e3c | 2017-10-16 04:12:57 +0000 | [diff] [blame] | 2261 | const IntegerType *IntTy = |
| 2262 | dyn_cast<IntegerType>(CalleeFn->getReturnType()); |
| 2263 | const AttributeSet &Attrs = |
| 2264 | CalleeFn->getAttributes().getRetAttributes(); |
| 2265 | if (IntTy && IntTy->getBitWidth() <= 32) |
| 2266 | return Attrs.hasAttribute(SignExt ? Attribute::SExt : |
| 2267 | Attribute::ZExt); |
| 2268 | } |
| 2269 | } |
| 2270 | } |
| 2271 | } |
| 2272 | |
| 2273 | // If this is a copy from another register, we recursively check source. |
| 2274 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) |
| 2275 | return false; |
| 2276 | const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); |
| 2277 | if (SrcMI != NULL) |
| 2278 | return isSignOrZeroExtended(*SrcMI, SignExt, Depth); |
| 2279 | |
| 2280 | return false; |
| 2281 | } |
| 2282 | |
| 2283 | case PPC::ANDIo: |
| 2284 | case PPC::ANDISo: |
| 2285 | case PPC::ORI: |
| 2286 | case PPC::ORIS: |
| 2287 | case PPC::XORI: |
| 2288 | case PPC::XORIS: |
| 2289 | case PPC::ANDIo8: |
| 2290 | case PPC::ANDISo8: |
| 2291 | case PPC::ORI8: |
| 2292 | case PPC::ORIS8: |
| 2293 | case PPC::XORI8: |
| 2294 | case PPC::XORIS8: { |
| 2295 | // logical operation with 16-bit immediate does not change the upper bits. |
| 2296 | // So, we track the operand register as we do for register copy. |
| 2297 | unsigned SrcReg = MI.getOperand(1).getReg(); |
| 2298 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) |
| 2299 | return false; |
| 2300 | const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); |
| 2301 | if (SrcMI != NULL) |
| 2302 | return isSignOrZeroExtended(*SrcMI, SignExt, Depth); |
| 2303 | |
| 2304 | return false; |
| 2305 | } |
| 2306 | |
| 2307 | // If all incoming values are sign-/zero-extended, |
| 2308 | // the output of AND, OR, ISEL or PHI is also sign-/zero-extended. |
| 2309 | case PPC::AND: |
| 2310 | case PPC::AND8: |
| 2311 | case PPC::OR: |
| 2312 | case PPC::OR8: |
| 2313 | case PPC::ISEL: |
| 2314 | case PPC::PHI: { |
| 2315 | if (Depth >= MAX_DEPTH) |
| 2316 | return false; |
| 2317 | |
| 2318 | // The input registers for PHI are operand 1, 3, ... |
| 2319 | // The input registers for others are operand 1 and 2. |
| 2320 | unsigned E = 3, D = 1; |
| 2321 | if (MI.getOpcode() == PPC::PHI) { |
| 2322 | E = MI.getNumOperands(); |
| 2323 | D = 2; |
| 2324 | } |
| 2325 | |
| 2326 | for (unsigned I = 1; I != E; I += D) { |
| 2327 | if (MI.getOperand(I).isReg()) { |
| 2328 | unsigned SrcReg = MI.getOperand(I).getReg(); |
| 2329 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) |
| 2330 | return false; |
| 2331 | const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); |
| 2332 | if (SrcMI == NULL || !isSignOrZeroExtended(*SrcMI, SignExt, Depth+1)) |
| 2333 | return false; |
| 2334 | } |
| 2335 | else |
| 2336 | return false; |
| 2337 | } |
| 2338 | return true; |
| 2339 | } |
| 2340 | |
| 2341 | default: |
| 2342 | return SignExt?isSignExtendingOp(MI): |
| 2343 | isZeroExtendingOp(MI); |
| 2344 | } |
| 2345 | return false; |
| 2346 | } |