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" |
Matthias Braun | f842297 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/LiveIntervals.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"); |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 54 | STATISTIC(CmpIselsConverted, |
| 55 | "Number of ISELs that depend on comparison of constants converted"); |
| 56 | STATISTIC(MissedConvertibleImmediateInstrs, |
| 57 | "Number of compare-immediate instructions fed by constants"); |
Zaara Syeda | fcd9697 | 2017-09-21 16:12:33 +0000 | [diff] [blame] | 58 | |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 59 | static cl:: |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 60 | opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden, |
| 61 | cl::desc("Disable analysis for CTR loops")); |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 62 | |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 63 | static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt", |
Hal Finkel | b12da6b | 2013-04-18 22:54:25 +0000 | [diff] [blame] | 64 | cl::desc("Disable compare instruction optimization"), cl::Hidden); |
| 65 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 66 | static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy", |
| 67 | cl::desc("Causes the backend to crash instead of generating a nop VSX copy"), |
| 68 | cl::Hidden); |
| 69 | |
Hal Finkel | 8acae52 | 2015-07-14 20:02:02 +0000 | [diff] [blame] | 70 | static cl::opt<bool> |
| 71 | UseOldLatencyCalc("ppc-old-latency-calc", cl::Hidden, |
| 72 | cl::desc("Use the old (incorrect) instruction latency calculation")); |
| 73 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 74 | // Pin the vtable to this file. |
| 75 | void PPCInstrInfo::anchor() {} |
| 76 | |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 77 | PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI) |
Tim Shen | 918ed87 | 2017-02-10 21:03:24 +0000 | [diff] [blame] | 78 | : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP, |
| 79 | /* CatchRetOpcode */ -1, |
| 80 | STI.isPPC64() ? PPC::BLR8 : PPC::BLR), |
Eric Christopher | ea178cf | 2015-03-12 01:42:51 +0000 | [diff] [blame] | 81 | Subtarget(STI), RI(STI.getTargetMachine()) {} |
Chris Lattner | 49cadab | 2006-06-17 00:01:04 +0000 | [diff] [blame] | 82 | |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 83 | /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for |
| 84 | /// this target when scheduling the DAG. |
Eric Christopher | f047bfd | 2014-06-13 22:38:52 +0000 | [diff] [blame] | 85 | ScheduleHazardRecognizer * |
| 86 | PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI, |
| 87 | const ScheduleDAG *DAG) const { |
| 88 | unsigned Directive = |
| 89 | static_cast<const PPCSubtarget *>(STI)->getDarwinDirective(); |
Hal Finkel | 742b535 | 2012-08-28 16:12:39 +0000 | [diff] [blame] | 90 | if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 || |
| 91 | Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) { |
Eric Christopher | f047bfd | 2014-06-13 22:38:52 +0000 | [diff] [blame] | 92 | const InstrItineraryData *II = |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 93 | static_cast<const PPCSubtarget *>(STI)->getInstrItineraryData(); |
Hal Finkel | 563cc05 | 2013-12-02 23:52:46 +0000 | [diff] [blame] | 94 | return new ScoreboardHazardRecognizer(II, DAG); |
Hal Finkel | 6fa5697 | 2011-10-17 04:03:49 +0000 | [diff] [blame] | 95 | } |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 96 | |
Eric Christopher | f047bfd | 2014-06-13 22:38:52 +0000 | [diff] [blame] | 97 | return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG); |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 100 | /// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer |
| 101 | /// to use for this target when scheduling the DAG. |
Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 102 | ScheduleHazardRecognizer * |
| 103 | PPCInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, |
| 104 | const ScheduleDAG *DAG) const { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 105 | unsigned Directive = |
Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 106 | DAG->MF.getSubtarget<PPCSubtarget>().getDarwinDirective(); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 107 | |
Nemanja Ivanovic | 6e29baf | 2016-05-09 18:54:58 +0000 | [diff] [blame] | 108 | // FIXME: Leaving this as-is until we have POWER9 scheduling info |
Will Schmidt | 970ff64 | 2014-06-26 13:36:19 +0000 | [diff] [blame] | 109 | if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8) |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 110 | return new PPCDispatchGroupSBHazardRecognizer(II, DAG); |
| 111 | |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 112 | // Most subtargets use a PPC970 recognizer. |
Hal Finkel | 742b535 | 2012-08-28 16:12:39 +0000 | [diff] [blame] | 113 | if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 && |
| 114 | Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 115 | assert(DAG->TII && "No InstrInfo?"); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 116 | |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 117 | return new PPCHazardRecognizer970(*DAG); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Hal Finkel | 563cc05 | 2013-12-02 23:52:46 +0000 | [diff] [blame] | 120 | return new ScoreboardHazardRecognizer(II, DAG); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 121 | } |
Jakob Stoklund Olesen | 0f855e4 | 2012-06-19 21:14:34 +0000 | [diff] [blame] | 122 | |
Hal Finkel | 8acae52 | 2015-07-14 20:02:02 +0000 | [diff] [blame] | 123 | unsigned PPCInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 124 | const MachineInstr &MI, |
Hal Finkel | 8acae52 | 2015-07-14 20:02:02 +0000 | [diff] [blame] | 125 | unsigned *PredCost) const { |
| 126 | if (!ItinData || UseOldLatencyCalc) |
| 127 | return PPCGenInstrInfo::getInstrLatency(ItinData, MI, PredCost); |
| 128 | |
| 129 | // The default implementation of getInstrLatency calls getStageLatency, but |
| 130 | // getStageLatency does not do the right thing for us. While we have |
| 131 | // itinerary, most cores are fully pipelined, and so the itineraries only |
| 132 | // express the first part of the pipeline, not every stage. Instead, we need |
| 133 | // to use the listed output operand cycle number (using operand 0 here, which |
| 134 | // is an output). |
| 135 | |
| 136 | unsigned Latency = 1; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 137 | unsigned DefClass = MI.getDesc().getSchedClass(); |
| 138 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 139 | const MachineOperand &MO = MI.getOperand(i); |
Hal Finkel | 8acae52 | 2015-07-14 20:02:02 +0000 | [diff] [blame] | 140 | if (!MO.isReg() || !MO.isDef() || MO.isImplicit()) |
| 141 | continue; |
| 142 | |
| 143 | int Cycle = ItinData->getOperandCycle(DefClass, i); |
| 144 | if (Cycle < 0) |
| 145 | continue; |
| 146 | |
| 147 | Latency = std::max(Latency, (unsigned) Cycle); |
| 148 | } |
| 149 | |
| 150 | return Latency; |
| 151 | } |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 152 | |
| 153 | int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 154 | const MachineInstr &DefMI, unsigned DefIdx, |
| 155 | const MachineInstr &UseMI, |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 156 | unsigned UseIdx) const { |
| 157 | int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx, |
| 158 | UseMI, UseIdx); |
| 159 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 160 | if (!DefMI.getParent()) |
Hal Finkel | 5d36b23 | 2015-07-15 08:23:05 +0000 | [diff] [blame] | 161 | return Latency; |
| 162 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 163 | const MachineOperand &DefMO = DefMI.getOperand(DefIdx); |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 164 | unsigned Reg = DefMO.getReg(); |
| 165 | |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 166 | bool IsRegCR; |
Andrew Kaylor | 5c73e1f | 2015-03-24 23:37:10 +0000 | [diff] [blame] | 167 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 168 | const MachineRegisterInfo *MRI = |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 169 | &DefMI.getParent()->getParent()->getRegInfo(); |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 170 | IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) || |
| 171 | MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass); |
| 172 | } else { |
| 173 | IsRegCR = PPC::CRRCRegClass.contains(Reg) || |
| 174 | PPC::CRBITRCRegClass.contains(Reg); |
| 175 | } |
| 176 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 177 | if (UseMI.isBranch() && IsRegCR) { |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 178 | if (Latency < 0) |
| 179 | Latency = getInstrLatency(ItinData, DefMI); |
| 180 | |
| 181 | // On some cores, there is an additional delay between writing to a condition |
| 182 | // register, and using it from a branch. |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 183 | unsigned Directive = Subtarget.getDarwinDirective(); |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 184 | switch (Directive) { |
| 185 | default: break; |
| 186 | case PPC::DIR_7400: |
| 187 | case PPC::DIR_750: |
| 188 | case PPC::DIR_970: |
| 189 | case PPC::DIR_E5500: |
| 190 | case PPC::DIR_PWR4: |
| 191 | case PPC::DIR_PWR5: |
| 192 | case PPC::DIR_PWR5X: |
| 193 | case PPC::DIR_PWR6: |
| 194 | case PPC::DIR_PWR6X: |
| 195 | case PPC::DIR_PWR7: |
Will Schmidt | 970ff64 | 2014-06-26 13:36:19 +0000 | [diff] [blame] | 196 | case PPC::DIR_PWR8: |
Nemanja Ivanovic | 6e29baf | 2016-05-09 18:54:58 +0000 | [diff] [blame] | 197 | // FIXME: Is this needed for POWER9? |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 198 | Latency += 2; |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return Latency; |
| 204 | } |
| 205 | |
Hal Finkel | 5d36b23 | 2015-07-15 08:23:05 +0000 | [diff] [blame] | 206 | // This function does not list all associative and commutative operations, but |
| 207 | // only those worth feeding through the machine combiner in an attempt to |
| 208 | // reduce the critical path. Mostly, this means floating-point operations, |
| 209 | // because they have high latencies (compared to other operations, such and |
| 210 | // and/or, which are also associative and commutative, but have low latencies). |
Chad Rosier | 03a4730 | 2015-09-21 15:09:11 +0000 | [diff] [blame] | 211 | bool PPCInstrInfo::isAssociativeAndCommutative(const MachineInstr &Inst) const { |
| 212 | switch (Inst.getOpcode()) { |
Hal Finkel | 5d36b23 | 2015-07-15 08:23:05 +0000 | [diff] [blame] | 213 | // FP Add: |
| 214 | case PPC::FADD: |
| 215 | case PPC::FADDS: |
| 216 | // FP Multiply: |
| 217 | case PPC::FMUL: |
| 218 | case PPC::FMULS: |
| 219 | // Altivec Add: |
| 220 | case PPC::VADDFP: |
| 221 | // VSX Add: |
| 222 | case PPC::XSADDDP: |
| 223 | case PPC::XVADDDP: |
| 224 | case PPC::XVADDSP: |
| 225 | case PPC::XSADDSP: |
| 226 | // VSX Multiply: |
| 227 | case PPC::XSMULDP: |
| 228 | case PPC::XVMULDP: |
| 229 | case PPC::XVMULSP: |
| 230 | case PPC::XSMULSP: |
| 231 | // QPX Add: |
| 232 | case PPC::QVFADD: |
| 233 | case PPC::QVFADDS: |
| 234 | case PPC::QVFADDSs: |
| 235 | // QPX Multiply: |
| 236 | case PPC::QVFMUL: |
| 237 | case PPC::QVFMULS: |
| 238 | case PPC::QVFMULSs: |
| 239 | return true; |
| 240 | default: |
| 241 | return false; |
| 242 | } |
| 243 | } |
| 244 | |
Chad Rosier | 03a4730 | 2015-09-21 15:09:11 +0000 | [diff] [blame] | 245 | bool PPCInstrInfo::getMachineCombinerPatterns( |
| 246 | MachineInstr &Root, |
Sanjay Patel | 387e66e | 2015-11-05 19:34:57 +0000 | [diff] [blame] | 247 | SmallVectorImpl<MachineCombinerPattern> &Patterns) const { |
Hal Finkel | 5d36b23 | 2015-07-15 08:23:05 +0000 | [diff] [blame] | 248 | // Using the machine combiner in this way is potentially expensive, so |
| 249 | // restrict to when aggressive optimizations are desired. |
| 250 | if (Subtarget.getTargetMachine().getOptLevel() != CodeGenOpt::Aggressive) |
| 251 | return false; |
| 252 | |
| 253 | // FP reassociation is only legal when we don't need strict IEEE semantics. |
| 254 | if (!Root.getParent()->getParent()->getTarget().Options.UnsafeFPMath) |
| 255 | return false; |
| 256 | |
Chad Rosier | 03a4730 | 2015-09-21 15:09:11 +0000 | [diff] [blame] | 257 | return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns); |
Hal Finkel | 5d36b23 | 2015-07-15 08:23:05 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Jakob Stoklund Olesen | 0f855e4 | 2012-06-19 21:14:34 +0000 | [diff] [blame] | 260 | // Detect 32 -> 64-bit extensions where we may reuse the low sub-register. |
| 261 | bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI, |
| 262 | unsigned &SrcReg, unsigned &DstReg, |
| 263 | unsigned &SubIdx) const { |
| 264 | switch (MI.getOpcode()) { |
| 265 | default: return false; |
| 266 | case PPC::EXTSW: |
Hiroshi Inoue | e3a3e3c | 2017-10-16 04:12:57 +0000 | [diff] [blame] | 267 | case PPC::EXTSW_32: |
Jakob Stoklund Olesen | 0f855e4 | 2012-06-19 21:14:34 +0000 | [diff] [blame] | 268 | case PPC::EXTSW_32_64: |
| 269 | SrcReg = MI.getOperand(1).getReg(); |
| 270 | DstReg = MI.getOperand(0).getReg(); |
| 271 | SubIdx = PPC::sub_32; |
| 272 | return true; |
| 273 | } |
| 274 | } |
| 275 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 276 | unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, |
Chris Lattner | 91400bd | 2006-03-16 22:24:02 +0000 | [diff] [blame] | 277 | int &FrameIndex) const { |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 278 | // Note: This list must be kept consistent with LoadRegFromStackSlot. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 279 | switch (MI.getOpcode()) { |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 280 | default: break; |
| 281 | case PPC::LD: |
| 282 | case PPC::LWZ: |
| 283 | case PPC::LFS: |
| 284 | case PPC::LFD: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 285 | case PPC::RESTORE_CR: |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 286 | case PPC::RESTORE_CRBIT: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 287 | case PPC::LVX: |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 288 | case PPC::LXVD2X: |
Lei Huang | 263dc4e | 2017-10-11 20:20:58 +0000 | [diff] [blame] | 289 | case PPC::LXV: |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 290 | case PPC::QVLFDX: |
| 291 | case PPC::QVLFSXs: |
| 292 | case PPC::QVLFDXb: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 293 | case PPC::RESTORE_VRSAVE: |
Zaara Syeda | fcd9697 | 2017-09-21 16:12:33 +0000 | [diff] [blame] | 294 | case PPC::SPILLTOVSR_LD: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 295 | // Check for the operands added by addFrameReference (the immediate is the |
| 296 | // offset which defaults to 0). |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 297 | if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() && |
| 298 | MI.getOperand(2).isFI()) { |
| 299 | FrameIndex = MI.getOperand(2).getIndex(); |
| 300 | return MI.getOperand(0).getReg(); |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 301 | } |
| 302 | break; |
| 303 | } |
| 304 | return 0; |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 305 | } |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 306 | |
Lei Huang | 84dbbfd | 2017-06-21 17:17:56 +0000 | [diff] [blame] | 307 | // For opcodes with the ReMaterializable flag set, this function is called to |
| 308 | // verify the instruction is really rematable. |
| 309 | bool PPCInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr &MI, |
| 310 | AliasAnalysis *AA) const { |
| 311 | switch (MI.getOpcode()) { |
| 312 | default: |
| 313 | // This function should only be called for opcodes with the ReMaterializable |
| 314 | // flag set. |
| 315 | llvm_unreachable("Unknown rematerializable operation!"); |
| 316 | break; |
| 317 | case PPC::LI: |
| 318 | case PPC::LI8: |
| 319 | case PPC::LIS: |
| 320 | case PPC::LIS8: |
| 321 | case PPC::QVGPCI: |
| 322 | case PPC::ADDIStocHA: |
| 323 | case PPC::ADDItocL: |
| 324 | case PPC::LOAD_STACK_GUARD: |
| 325 | return true; |
| 326 | } |
| 327 | return false; |
| 328 | } |
| 329 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 330 | unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr &MI, |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 331 | int &FrameIndex) const { |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 332 | // Note: This list must be kept consistent with StoreRegToStackSlot. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 333 | switch (MI.getOpcode()) { |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 334 | default: break; |
Nate Begeman | 4efb328 | 2006-02-02 21:07:50 +0000 | [diff] [blame] | 335 | case PPC::STD: |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 336 | case PPC::STW: |
| 337 | case PPC::STFS: |
| 338 | case PPC::STFD: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 339 | case PPC::SPILL_CR: |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 340 | case PPC::SPILL_CRBIT: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 341 | case PPC::STVX: |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 342 | case PPC::STXVD2X: |
Lei Huang | 263dc4e | 2017-10-11 20:20:58 +0000 | [diff] [blame] | 343 | case PPC::STXV: |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 344 | case PPC::QVSTFDX: |
| 345 | case PPC::QVSTFSXs: |
| 346 | case PPC::QVSTFDXb: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 347 | case PPC::SPILL_VRSAVE: |
Zaara Syeda | fcd9697 | 2017-09-21 16:12:33 +0000 | [diff] [blame] | 348 | case PPC::SPILLTOVSR_ST: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 349 | // Check for the operands added by addFrameReference (the immediate is the |
| 350 | // offset which defaults to 0). |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 351 | if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() && |
| 352 | MI.getOperand(2).isFI()) { |
| 353 | FrameIndex = MI.getOperand(2).getIndex(); |
| 354 | return MI.getOperand(0).getReg(); |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 355 | } |
| 356 | break; |
| 357 | } |
| 358 | return 0; |
| 359 | } |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 360 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 361 | MachineInstr *PPCInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI, |
Andrew Kaylor | 16c4da0 | 2015-09-28 20:33:22 +0000 | [diff] [blame] | 362 | unsigned OpIdx1, |
| 363 | unsigned OpIdx2) const { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 364 | MachineFunction &MF = *MI.getParent()->getParent(); |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 365 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 366 | // Normal instructions can be commuted the obvious way. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 367 | if (MI.getOpcode() != PPC::RLWIMI && MI.getOpcode() != PPC::RLWIMIo) |
Andrew Kaylor | 16c4da0 | 2015-09-28 20:33:22 +0000 | [diff] [blame] | 368 | return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); |
Hal Finkel | 4c6658f | 2014-12-12 23:59:36 +0000 | [diff] [blame] | 369 | // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a |
| 370 | // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because |
| 371 | // changing the relative order of the mask operands might change what happens |
| 372 | // to the high-bits of the mask (and, thus, the result). |
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 | // 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] | 375 | if (MI.getOperand(3).getImm() != 0) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 376 | return nullptr; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 377 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 378 | // If we have a zero rotate count, we have: |
| 379 | // M = mask(MB,ME) |
| 380 | // Op0 = (Op1 & ~M) | (Op2 & M) |
| 381 | // Change this to: |
| 382 | // M = mask((ME+1)&31, (MB-1)&31) |
| 383 | // Op0 = (Op2 & ~M) | (Op1 & M) |
| 384 | |
| 385 | // Swap op1/op2 |
Andrew Kaylor | 16c4da0 | 2015-09-28 20:33:22 +0000 | [diff] [blame] | 386 | assert(((OpIdx1 == 1 && OpIdx2 == 2) || (OpIdx1 == 2 && OpIdx2 == 1)) && |
| 387 | "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] | 388 | unsigned Reg0 = MI.getOperand(0).getReg(); |
| 389 | unsigned Reg1 = MI.getOperand(1).getReg(); |
| 390 | unsigned Reg2 = MI.getOperand(2).getReg(); |
| 391 | unsigned SubReg1 = MI.getOperand(1).getSubReg(); |
| 392 | unsigned SubReg2 = MI.getOperand(2).getSubReg(); |
| 393 | bool Reg1IsKill = MI.getOperand(1).isKill(); |
| 394 | bool Reg2IsKill = MI.getOperand(2).isKill(); |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 395 | bool ChangeReg0 = false; |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 396 | // If machine instrs are no longer in two-address forms, update |
| 397 | // destination register as well. |
| 398 | if (Reg0 == Reg1) { |
| 399 | // Must be two address instruction! |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 400 | assert(MI.getDesc().getOperandConstraint(0, MCOI::TIED_TO) && |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 401 | "Expecting a two-address instruction!"); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 402 | assert(MI.getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch"); |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 403 | Reg2IsKill = false; |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 404 | ChangeReg0 = true; |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 405 | } |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 406 | |
| 407 | // Masks. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 408 | unsigned MB = MI.getOperand(4).getImm(); |
| 409 | unsigned ME = MI.getOperand(5).getImm(); |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 410 | |
Hal Finkel | ccf9259 | 2015-09-06 04:17:30 +0000 | [diff] [blame] | 411 | // We can't commute a trivial mask (there is no way to represent an all-zero |
| 412 | // mask). |
| 413 | if (MB == 0 && ME == 31) |
| 414 | return nullptr; |
| 415 | |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 416 | if (NewMI) { |
| 417 | // Create a new instruction. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 418 | unsigned Reg0 = ChangeReg0 ? Reg2 : MI.getOperand(0).getReg(); |
| 419 | bool Reg0IsDead = MI.getOperand(0).isDead(); |
| 420 | return BuildMI(MF, MI.getDebugLoc(), MI.getDesc()) |
| 421 | .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead)) |
| 422 | .addReg(Reg2, getKillRegState(Reg2IsKill)) |
| 423 | .addReg(Reg1, getKillRegState(Reg1IsKill)) |
| 424 | .addImm((ME + 1) & 31) |
| 425 | .addImm((MB - 1) & 31); |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 428 | if (ChangeReg0) { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 429 | MI.getOperand(0).setReg(Reg2); |
| 430 | MI.getOperand(0).setSubReg(SubReg2); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 431 | } |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 432 | MI.getOperand(2).setReg(Reg1); |
| 433 | MI.getOperand(1).setReg(Reg2); |
| 434 | MI.getOperand(2).setSubReg(SubReg1); |
| 435 | MI.getOperand(1).setSubReg(SubReg2); |
| 436 | MI.getOperand(2).setIsKill(Reg1IsKill); |
| 437 | MI.getOperand(1).setIsKill(Reg2IsKill); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 438 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 439 | // Swap the mask around. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 440 | MI.getOperand(4).setImm((ME + 1) & 31); |
| 441 | MI.getOperand(5).setImm((MB - 1) & 31); |
| 442 | return &MI; |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 443 | } |
Chris Lattner | ea79d9fd73 | 2006-03-05 23:49:55 +0000 | [diff] [blame] | 444 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 445 | bool PPCInstrInfo::findCommutedOpIndices(MachineInstr &MI, unsigned &SrcOpIdx1, |
Hal Finkel | 6c32ff3 | 2014-03-25 19:26:43 +0000 | [diff] [blame] | 446 | unsigned &SrcOpIdx2) const { |
| 447 | // For VSX A-Type FMA instructions, it is the first two operands that can be |
| 448 | // commuted, however, because the non-encoded tied input operand is listed |
| 449 | // first, the operands to swap are actually the second and third. |
| 450 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 451 | int AltOpc = PPC::getAltVSXFMAOpcode(MI.getOpcode()); |
Hal Finkel | 6c32ff3 | 2014-03-25 19:26:43 +0000 | [diff] [blame] | 452 | if (AltOpc == -1) |
| 453 | return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2); |
| 454 | |
Andrew Kaylor | 16c4da0 | 2015-09-28 20:33:22 +0000 | [diff] [blame] | 455 | // The commutable operand indices are 2 and 3. Return them in SrcOpIdx1 |
| 456 | // and SrcOpIdx2. |
| 457 | return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2, 3); |
Hal Finkel | 6c32ff3 | 2014-03-25 19:26:43 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 460 | void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB, |
Chris Lattner | ea79d9fd73 | 2006-03-05 23:49:55 +0000 | [diff] [blame] | 461 | MachineBasicBlock::iterator MI) const { |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 462 | // This function is used for scheduling, and the nop wanted here is the type |
| 463 | // that terminates dispatch groups on the POWER cores. |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 464 | unsigned Directive = Subtarget.getDarwinDirective(); |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 465 | unsigned Opcode; |
| 466 | switch (Directive) { |
| 467 | default: Opcode = PPC::NOP; break; |
| 468 | case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break; |
| 469 | case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break; |
Will Schmidt | 970ff64 | 2014-06-26 13:36:19 +0000 | [diff] [blame] | 470 | 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] | 471 | // FIXME: Update when POWER9 scheduling model is ready. |
| 472 | case PPC::DIR_PWR9: Opcode = PPC::NOP_GT_PWR7; break; |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 473 | } |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 474 | |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 475 | DebugLoc DL; |
| 476 | BuildMI(MBB, MI, DL, get(Opcode)); |
| 477 | } |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 478 | |
Hans Wennborg | 9b9a535 | 2017-04-21 21:48:41 +0000 | [diff] [blame] | 479 | /// Return the noop instruction to use for a noop. |
| 480 | void PPCInstrInfo::getNoop(MCInst &NopInst) const { |
Joerg Sonnenberger | 7ee0f31 | 2014-08-08 19:13:23 +0000 | [diff] [blame] | 481 | NopInst.setOpcode(PPC::NOP); |
| 482 | } |
| 483 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 484 | // Branch analysis. |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 485 | // Note: If the condition register is set to CTR or CTR8 then this is a |
| 486 | // BDNZ (imm == 1) or BDZ (imm == 0) branch. |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 487 | bool PPCInstrInfo::analyzeBranch(MachineBasicBlock &MBB, |
| 488 | MachineBasicBlock *&TBB, |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 489 | MachineBasicBlock *&FBB, |
Evan Cheng | 64dfcac | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 490 | SmallVectorImpl<MachineOperand> &Cond, |
| 491 | bool AllowModify) const { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 492 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 493 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 494 | // 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] | 495 | MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); |
| 496 | if (I == MBB.end()) |
Dale Johannesen | 4244d12 | 2010-04-02 01:38:09 +0000 | [diff] [blame] | 497 | return false; |
Benjamin Kramer | 92861d7 | 2015-06-25 13:39:03 +0000 | [diff] [blame] | 498 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 499 | if (!isUnpredicatedTerminator(*I)) |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 500 | return false; |
| 501 | |
Hiroshi Inoue | ed1ffa4 | 2017-09-27 10:33:02 +0000 | [diff] [blame] | 502 | if (AllowModify) { |
| 503 | // If the BB ends with an unconditional branch to the fallthrough BB, |
| 504 | // we eliminate the branch instruction. |
| 505 | if (I->getOpcode() == PPC::B && |
| 506 | MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) { |
| 507 | I->eraseFromParent(); |
| 508 | |
| 509 | // We update iterator after deleting the last branch. |
| 510 | I = MBB.getLastNonDebugInstr(); |
| 511 | if (I == MBB.end() || !isUnpredicatedTerminator(*I)) |
| 512 | return false; |
| 513 | } |
| 514 | } |
| 515 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 516 | // Get the last instruction in the block. |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 517 | MachineInstr &LastInst = *I; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 518 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 519 | // If there is only one terminator instruction, process it. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 520 | if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) { |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 521 | if (LastInst.getOpcode() == PPC::B) { |
| 522 | if (!LastInst.getOperand(0).isMBB()) |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 523 | return true; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 524 | TBB = LastInst.getOperand(0).getMBB(); |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 525 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 526 | } else if (LastInst.getOpcode() == PPC::BCC) { |
| 527 | if (!LastInst.getOperand(2).isMBB()) |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 528 | return true; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 529 | // Block ends with fall-through condbranch. |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 530 | TBB = LastInst.getOperand(2).getMBB(); |
| 531 | Cond.push_back(LastInst.getOperand(0)); |
| 532 | Cond.push_back(LastInst.getOperand(1)); |
Chris Lattner | 23f22de | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 533 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 534 | } else if (LastInst.getOpcode() == PPC::BC) { |
| 535 | if (!LastInst.getOperand(1).isMBB()) |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 536 | return true; |
| 537 | // Block ends with fall-through condbranch. |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 538 | TBB = LastInst.getOperand(1).getMBB(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 539 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 540 | Cond.push_back(LastInst.getOperand(0)); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 541 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 542 | } else if (LastInst.getOpcode() == PPC::BCn) { |
| 543 | if (!LastInst.getOperand(1).isMBB()) |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 544 | return true; |
| 545 | // Block ends with fall-through condbranch. |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 546 | TBB = LastInst.getOperand(1).getMBB(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 547 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 548 | Cond.push_back(LastInst.getOperand(0)); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 549 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 550 | } else if (LastInst.getOpcode() == PPC::BDNZ8 || |
| 551 | LastInst.getOpcode() == PPC::BDNZ) { |
| 552 | if (!LastInst.getOperand(0).isMBB()) |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 553 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 554 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 555 | return true; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 556 | TBB = LastInst.getOperand(0).getMBB(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 557 | Cond.push_back(MachineOperand::CreateImm(1)); |
| 558 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 559 | true)); |
| 560 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 561 | } else if (LastInst.getOpcode() == PPC::BDZ8 || |
| 562 | LastInst.getOpcode() == PPC::BDZ) { |
| 563 | if (!LastInst.getOperand(0).isMBB()) |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 564 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 565 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 566 | return true; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 567 | TBB = LastInst.getOperand(0).getMBB(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 568 | Cond.push_back(MachineOperand::CreateImm(0)); |
| 569 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 570 | true)); |
| 571 | return false; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 572 | } |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 573 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 574 | // Otherwise, don't know what this is. |
| 575 | return true; |
| 576 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 577 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 578 | // 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] | 579 | MachineInstr &SecondLastInst = *I; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 580 | |
| 581 | // 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] | 582 | if (I != MBB.begin() && isUnpredicatedTerminator(*--I)) |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 583 | return true; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 584 | |
Chris Lattner | e026379 | 2006-11-17 22:14:47 +0000 | [diff] [blame] | 585 | // 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] | 586 | if (SecondLastInst.getOpcode() == PPC::BCC && |
| 587 | LastInst.getOpcode() == PPC::B) { |
| 588 | if (!SecondLastInst.getOperand(2).isMBB() || |
| 589 | !LastInst.getOperand(0).isMBB()) |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 590 | return true; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 591 | TBB = SecondLastInst.getOperand(2).getMBB(); |
| 592 | Cond.push_back(SecondLastInst.getOperand(0)); |
| 593 | Cond.push_back(SecondLastInst.getOperand(1)); |
| 594 | FBB = LastInst.getOperand(0).getMBB(); |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 595 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 596 | } else if (SecondLastInst.getOpcode() == PPC::BC && |
| 597 | LastInst.getOpcode() == PPC::B) { |
| 598 | if (!SecondLastInst.getOperand(1).isMBB() || |
| 599 | !LastInst.getOperand(0).isMBB()) |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 600 | return true; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 601 | TBB = SecondLastInst.getOperand(1).getMBB(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 602 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 603 | Cond.push_back(SecondLastInst.getOperand(0)); |
| 604 | FBB = LastInst.getOperand(0).getMBB(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 605 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 606 | } else if (SecondLastInst.getOpcode() == PPC::BCn && |
| 607 | LastInst.getOpcode() == PPC::B) { |
| 608 | if (!SecondLastInst.getOperand(1).isMBB() || |
| 609 | !LastInst.getOperand(0).isMBB()) |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 610 | return true; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 611 | TBB = SecondLastInst.getOperand(1).getMBB(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 612 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 613 | Cond.push_back(SecondLastInst.getOperand(0)); |
| 614 | FBB = LastInst.getOperand(0).getMBB(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 615 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 616 | } else if ((SecondLastInst.getOpcode() == PPC::BDNZ8 || |
| 617 | SecondLastInst.getOpcode() == PPC::BDNZ) && |
| 618 | LastInst.getOpcode() == PPC::B) { |
| 619 | if (!SecondLastInst.getOperand(0).isMBB() || |
| 620 | !LastInst.getOperand(0).isMBB()) |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 621 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 622 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 623 | return true; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 624 | TBB = SecondLastInst.getOperand(0).getMBB(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 625 | Cond.push_back(MachineOperand::CreateImm(1)); |
| 626 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 627 | true)); |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 628 | FBB = LastInst.getOperand(0).getMBB(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 629 | return false; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 630 | } else if ((SecondLastInst.getOpcode() == PPC::BDZ8 || |
| 631 | SecondLastInst.getOpcode() == PPC::BDZ) && |
| 632 | LastInst.getOpcode() == PPC::B) { |
| 633 | if (!SecondLastInst.getOperand(0).isMBB() || |
| 634 | !LastInst.getOperand(0).isMBB()) |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 635 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 636 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 637 | return true; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 638 | TBB = SecondLastInst.getOperand(0).getMBB(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 639 | Cond.push_back(MachineOperand::CreateImm(0)); |
| 640 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 641 | true)); |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 642 | FBB = LastInst.getOperand(0).getMBB(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 643 | return false; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 644 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 645 | |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 646 | // If the block ends with two PPC:Bs, handle it. The second one is not |
| 647 | // executed, so remove it. |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 648 | if (SecondLastInst.getOpcode() == PPC::B && LastInst.getOpcode() == PPC::B) { |
| 649 | if (!SecondLastInst.getOperand(0).isMBB()) |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 650 | return true; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 651 | TBB = SecondLastInst.getOperand(0).getMBB(); |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 652 | I = LastInst; |
Evan Cheng | 64dfcac | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 653 | if (AllowModify) |
| 654 | I->eraseFromParent(); |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 655 | return false; |
| 656 | } |
| 657 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 658 | // Otherwise, can't handle this. |
| 659 | return true; |
| 660 | } |
| 661 | |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 662 | unsigned PPCInstrInfo::removeBranch(MachineBasicBlock &MBB, |
Matt Arsenault | a2b036e | 2016-09-14 17:23:48 +0000 | [diff] [blame] | 663 | int *BytesRemoved) const { |
| 664 | assert(!BytesRemoved && "code size not handled"); |
| 665 | |
Benjamin Kramer | 92861d7 | 2015-06-25 13:39:03 +0000 | [diff] [blame] | 666 | MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); |
| 667 | if (I == MBB.end()) |
| 668 | return 0; |
| 669 | |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 670 | if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC && |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 671 | I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 672 | I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && |
| 673 | I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 674 | return 0; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 675 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 676 | // Remove the branch. |
| 677 | I->eraseFromParent(); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 678 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 679 | I = MBB.end(); |
| 680 | |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 681 | if (I == MBB.begin()) return 1; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 682 | --I; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 683 | if (I->getOpcode() != PPC::BCC && |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 684 | I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 685 | I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && |
| 686 | I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 687 | return 1; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 688 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 689 | // Remove the branch. |
| 690 | I->eraseFromParent(); |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 691 | return 2; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 694 | unsigned PPCInstrInfo::insertBranch(MachineBasicBlock &MBB, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 695 | MachineBasicBlock *TBB, |
| 696 | MachineBasicBlock *FBB, |
| 697 | ArrayRef<MachineOperand> Cond, |
Matt Arsenault | a2b036e | 2016-09-14 17:23:48 +0000 | [diff] [blame] | 698 | const DebugLoc &DL, |
| 699 | int *BytesAdded) const { |
Chris Lattner | a61f010 | 2006-10-17 18:06:55 +0000 | [diff] [blame] | 700 | // Shouldn't be a fall through. |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 701 | assert(TBB && "insertBranch must not be told to insert a fallthrough"); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 702 | assert((Cond.size() == 2 || Cond.size() == 0) && |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 703 | "PPC branch conditions have two components!"); |
Matt Arsenault | a2b036e | 2016-09-14 17:23:48 +0000 | [diff] [blame] | 704 | assert(!BytesAdded && "code size not handled"); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 705 | |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 706 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 707 | |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 708 | // One-way branch. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 709 | if (!FBB) { |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 710 | if (Cond.empty()) // Unconditional branch |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 711 | BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 712 | else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 713 | BuildMI(&MBB, DL, get(Cond[0].getImm() ? |
| 714 | (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : |
| 715 | (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 716 | else if (Cond[0].getImm() == PPC::PRED_BIT_SET) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 717 | BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 718 | else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 719 | BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB); |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 720 | else // Conditional branch |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 721 | BuildMI(&MBB, DL, get(PPC::BCC)) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 722 | .addImm(Cond[0].getImm()) |
| 723 | .add(Cond[1]) |
| 724 | .addMBB(TBB); |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 725 | return 1; |
Chris Lattner | a61f010 | 2006-10-17 18:06:55 +0000 | [diff] [blame] | 726 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 727 | |
Chris Lattner | d881660 | 2006-10-21 05:42:09 +0000 | [diff] [blame] | 728 | // Two-way Conditional Branch. |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 729 | if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 730 | BuildMI(&MBB, DL, get(Cond[0].getImm() ? |
| 731 | (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : |
| 732 | (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 733 | else if (Cond[0].getImm() == PPC::PRED_BIT_SET) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 734 | BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 735 | else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 736 | BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 737 | else |
| 738 | BuildMI(&MBB, DL, get(PPC::BCC)) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 739 | .addImm(Cond[0].getImm()) |
| 740 | .add(Cond[1]) |
| 741 | .addMBB(TBB); |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 742 | BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB); |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 743 | return 2; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 746 | // Select analysis. |
| 747 | bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB, |
Ahmed Bougacha | c88bf54 | 2015-06-11 19:30:37 +0000 | [diff] [blame] | 748 | ArrayRef<MachineOperand> Cond, |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 749 | unsigned TrueReg, unsigned FalseReg, |
| 750 | int &CondCycles, int &TrueCycles, int &FalseCycles) const { |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 751 | if (Cond.size() != 2) |
| 752 | return false; |
| 753 | |
| 754 | // If this is really a bdnz-like condition, then it cannot be turned into a |
| 755 | // select. |
| 756 | if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 757 | return false; |
| 758 | |
| 759 | // Check register classes. |
| 760 | const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 761 | const TargetRegisterClass *RC = |
| 762 | RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); |
| 763 | if (!RC) |
| 764 | return false; |
| 765 | |
| 766 | // isel is for regular integer GPRs only. |
| 767 | if (!PPC::GPRCRegClass.hasSubClassEq(RC) && |
Hal Finkel | 8e8618a | 2013-07-15 20:22:58 +0000 | [diff] [blame] | 768 | !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) && |
| 769 | !PPC::G8RCRegClass.hasSubClassEq(RC) && |
| 770 | !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 771 | return false; |
| 772 | |
| 773 | // FIXME: These numbers are for the A2, how well they work for other cores is |
| 774 | // an open question. On the A2, the isel instruction has a 2-cycle latency |
| 775 | // but single-cycle throughput. These numbers are used in combination with |
| 776 | // the MispredictPenalty setting from the active SchedMachineModel. |
| 777 | CondCycles = 1; |
| 778 | TrueCycles = 1; |
| 779 | FalseCycles = 1; |
| 780 | |
| 781 | return true; |
| 782 | } |
| 783 | |
| 784 | void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 785 | MachineBasicBlock::iterator MI, |
| 786 | const DebugLoc &dl, unsigned DestReg, |
| 787 | ArrayRef<MachineOperand> Cond, unsigned TrueReg, |
| 788 | unsigned FalseReg) const { |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 789 | assert(Cond.size() == 2 && |
| 790 | "PPC branch conditions have two components!"); |
| 791 | |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 792 | // Get the register classes. |
| 793 | MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 794 | const TargetRegisterClass *RC = |
| 795 | RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); |
| 796 | assert(RC && "TrueReg and FalseReg must have overlapping register classes"); |
Hal Finkel | 8e8618a | 2013-07-15 20:22:58 +0000 | [diff] [blame] | 797 | |
| 798 | bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) || |
| 799 | PPC::G8RC_NOX0RegClass.hasSubClassEq(RC); |
| 800 | assert((Is64Bit || |
| 801 | PPC::GPRCRegClass.hasSubClassEq(RC) || |
| 802 | PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) && |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 803 | "isel is for regular integer GPRs only"); |
| 804 | |
Hal Finkel | 8e8618a | 2013-07-15 20:22:58 +0000 | [diff] [blame] | 805 | unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL; |
Kyle Butt | cec4080 | 2016-01-12 21:00:43 +0000 | [diff] [blame] | 806 | auto SelectPred = static_cast<PPC::Predicate>(Cond[0].getImm()); |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 807 | |
Kyle Butt | 132bf36 | 2016-01-15 19:20:06 +0000 | [diff] [blame] | 808 | unsigned SubIdx = 0; |
| 809 | bool SwapOps = false; |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 810 | switch (SelectPred) { |
Kyle Butt | cec4080 | 2016-01-12 21:00:43 +0000 | [diff] [blame] | 811 | case PPC::PRED_EQ: |
| 812 | case PPC::PRED_EQ_MINUS: |
| 813 | case PPC::PRED_EQ_PLUS: |
| 814 | SubIdx = PPC::sub_eq; SwapOps = false; break; |
| 815 | case PPC::PRED_NE: |
| 816 | case PPC::PRED_NE_MINUS: |
| 817 | case PPC::PRED_NE_PLUS: |
| 818 | SubIdx = PPC::sub_eq; SwapOps = true; break; |
| 819 | case PPC::PRED_LT: |
| 820 | case PPC::PRED_LT_MINUS: |
| 821 | case PPC::PRED_LT_PLUS: |
| 822 | SubIdx = PPC::sub_lt; SwapOps = false; break; |
| 823 | case PPC::PRED_GE: |
| 824 | case PPC::PRED_GE_MINUS: |
| 825 | case PPC::PRED_GE_PLUS: |
| 826 | SubIdx = PPC::sub_lt; SwapOps = true; break; |
| 827 | case PPC::PRED_GT: |
| 828 | case PPC::PRED_GT_MINUS: |
| 829 | case PPC::PRED_GT_PLUS: |
| 830 | SubIdx = PPC::sub_gt; SwapOps = false; break; |
| 831 | case PPC::PRED_LE: |
| 832 | case PPC::PRED_LE_MINUS: |
| 833 | case PPC::PRED_LE_PLUS: |
| 834 | SubIdx = PPC::sub_gt; SwapOps = true; break; |
| 835 | case PPC::PRED_UN: |
| 836 | case PPC::PRED_UN_MINUS: |
| 837 | case PPC::PRED_UN_PLUS: |
| 838 | SubIdx = PPC::sub_un; SwapOps = false; break; |
| 839 | case PPC::PRED_NU: |
| 840 | case PPC::PRED_NU_MINUS: |
| 841 | case PPC::PRED_NU_PLUS: |
| 842 | SubIdx = PPC::sub_un; SwapOps = true; break; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 843 | case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break; |
| 844 | case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break; |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | unsigned FirstReg = SwapOps ? FalseReg : TrueReg, |
| 848 | SecondReg = SwapOps ? TrueReg : FalseReg; |
| 849 | |
| 850 | // The first input register of isel cannot be r0. If it is a member |
| 851 | // of a register class that can be r0, then copy it first (the |
| 852 | // register allocator should eliminate the copy). |
| 853 | if (MRI.getRegClass(FirstReg)->contains(PPC::R0) || |
| 854 | MRI.getRegClass(FirstReg)->contains(PPC::X0)) { |
| 855 | const TargetRegisterClass *FirstRC = |
| 856 | MRI.getRegClass(FirstReg)->contains(PPC::X0) ? |
| 857 | &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass; |
| 858 | unsigned OldFirstReg = FirstReg; |
| 859 | FirstReg = MRI.createVirtualRegister(FirstRC); |
| 860 | BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg) |
| 861 | .addReg(OldFirstReg); |
| 862 | } |
| 863 | |
| 864 | BuildMI(MBB, MI, dl, get(OpCode), DestReg) |
| 865 | .addReg(FirstReg).addReg(SecondReg) |
| 866 | .addReg(Cond[1].getReg(), 0, SubIdx); |
| 867 | } |
| 868 | |
Kit Barton | 535e69d | 2015-03-25 19:36:23 +0000 | [diff] [blame] | 869 | static unsigned getCRBitValue(unsigned CRBit) { |
| 870 | unsigned Ret = 4; |
| 871 | if (CRBit == PPC::CR0LT || CRBit == PPC::CR1LT || |
| 872 | CRBit == PPC::CR2LT || CRBit == PPC::CR3LT || |
| 873 | CRBit == PPC::CR4LT || CRBit == PPC::CR5LT || |
| 874 | CRBit == PPC::CR6LT || CRBit == PPC::CR7LT) |
| 875 | Ret = 3; |
| 876 | if (CRBit == PPC::CR0GT || CRBit == PPC::CR1GT || |
| 877 | CRBit == PPC::CR2GT || CRBit == PPC::CR3GT || |
| 878 | CRBit == PPC::CR4GT || CRBit == PPC::CR5GT || |
| 879 | CRBit == PPC::CR6GT || CRBit == PPC::CR7GT) |
| 880 | Ret = 2; |
| 881 | if (CRBit == PPC::CR0EQ || CRBit == PPC::CR1EQ || |
| 882 | CRBit == PPC::CR2EQ || CRBit == PPC::CR3EQ || |
| 883 | CRBit == PPC::CR4EQ || CRBit == PPC::CR5EQ || |
| 884 | CRBit == PPC::CR6EQ || CRBit == PPC::CR7EQ) |
| 885 | Ret = 1; |
| 886 | if (CRBit == PPC::CR0UN || CRBit == PPC::CR1UN || |
| 887 | CRBit == PPC::CR2UN || CRBit == PPC::CR3UN || |
| 888 | CRBit == PPC::CR4UN || CRBit == PPC::CR5UN || |
| 889 | CRBit == PPC::CR6UN || CRBit == PPC::CR7UN) |
| 890 | Ret = 0; |
| 891 | |
| 892 | assert(Ret != 4 && "Invalid CR bit register"); |
| 893 | return Ret; |
| 894 | } |
| 895 | |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 896 | void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 897 | MachineBasicBlock::iterator I, |
| 898 | const DebugLoc &DL, unsigned DestReg, |
| 899 | unsigned SrcReg, bool KillSrc) const { |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 900 | // 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] | 901 | // legalization. Promote them here. |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 902 | const TargetRegisterInfo *TRI = &getRegisterInfo(); |
| 903 | if (PPC::F8RCRegClass.contains(DestReg) && |
Hal Finkel | 5cedafb | 2015-02-16 23:46:30 +0000 | [diff] [blame] | 904 | PPC::VSRCRegClass.contains(SrcReg)) { |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 905 | unsigned SuperReg = |
| 906 | TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass); |
| 907 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 908 | if (VSXSelfCopyCrash && SrcReg == SuperReg) |
| 909 | llvm_unreachable("nop VSX copy"); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 910 | |
| 911 | DestReg = SuperReg; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 912 | } else if (PPC::F8RCRegClass.contains(SrcReg) && |
Hal Finkel | 5cedafb | 2015-02-16 23:46:30 +0000 | [diff] [blame] | 913 | PPC::VSRCRegClass.contains(DestReg)) { |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 914 | unsigned SuperReg = |
| 915 | TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass); |
| 916 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 917 | if (VSXSelfCopyCrash && DestReg == SuperReg) |
| 918 | llvm_unreachable("nop VSX copy"); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 919 | |
| 920 | SrcReg = SuperReg; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Kit Barton | 535e69d | 2015-03-25 19:36:23 +0000 | [diff] [blame] | 923 | // Different class register copy |
| 924 | if (PPC::CRBITRCRegClass.contains(SrcReg) && |
| 925 | PPC::GPRCRegClass.contains(DestReg)) { |
| 926 | unsigned CRReg = getCRFromCRBit(SrcReg); |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 927 | BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(CRReg); |
| 928 | getKillRegState(KillSrc); |
Kit Barton | 535e69d | 2015-03-25 19:36:23 +0000 | [diff] [blame] | 929 | // Rotate the CR bit in the CR fields to be the least significant bit and |
| 930 | // then mask with 0x1 (MB = ME = 31). |
| 931 | BuildMI(MBB, I, DL, get(PPC::RLWINM), DestReg) |
| 932 | .addReg(DestReg, RegState::Kill) |
| 933 | .addImm(TRI->getEncodingValue(CRReg) * 4 + (4 - getCRBitValue(SrcReg))) |
| 934 | .addImm(31) |
| 935 | .addImm(31); |
| 936 | return; |
| 937 | } else if (PPC::CRRCRegClass.contains(SrcReg) && |
| 938 | PPC::G8RCRegClass.contains(DestReg)) { |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 939 | BuildMI(MBB, I, DL, get(PPC::MFOCRF8), DestReg).addReg(SrcReg); |
| 940 | getKillRegState(KillSrc); |
Kit Barton | 535e69d | 2015-03-25 19:36:23 +0000 | [diff] [blame] | 941 | return; |
| 942 | } else if (PPC::CRRCRegClass.contains(SrcReg) && |
| 943 | PPC::GPRCRegClass.contains(DestReg)) { |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 944 | BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(SrcReg); |
| 945 | getKillRegState(KillSrc); |
Kit Barton | 535e69d | 2015-03-25 19:36:23 +0000 | [diff] [blame] | 946 | return; |
Zaara Syeda | fcd9697 | 2017-09-21 16:12:33 +0000 | [diff] [blame] | 947 | } else if (PPC::G8RCRegClass.contains(SrcReg) && |
| 948 | PPC::VSFRCRegClass.contains(DestReg)) { |
| 949 | BuildMI(MBB, I, DL, get(PPC::MTVSRD), DestReg).addReg(SrcReg); |
| 950 | NumGPRtoVSRSpill++; |
| 951 | getKillRegState(KillSrc); |
| 952 | return; |
| 953 | } else if (PPC::VSFRCRegClass.contains(SrcReg) && |
| 954 | PPC::G8RCRegClass.contains(DestReg)) { |
| 955 | BuildMI(MBB, I, DL, get(PPC::MFVSRD), DestReg).addReg(SrcReg); |
| 956 | getKillRegState(KillSrc); |
| 957 | return; |
| 958 | } |
Kit Barton | 535e69d | 2015-03-25 19:36:23 +0000 | [diff] [blame] | 959 | |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 960 | unsigned Opc; |
| 961 | if (PPC::GPRCRegClass.contains(DestReg, SrcReg)) |
| 962 | Opc = PPC::OR; |
| 963 | else if (PPC::G8RCRegClass.contains(DestReg, SrcReg)) |
| 964 | Opc = PPC::OR8; |
| 965 | else if (PPC::F4RCRegClass.contains(DestReg, SrcReg)) |
| 966 | Opc = PPC::FMR; |
| 967 | else if (PPC::CRRCRegClass.contains(DestReg, SrcReg)) |
| 968 | Opc = PPC::MCRF; |
| 969 | else if (PPC::VRRCRegClass.contains(DestReg, SrcReg)) |
| 970 | Opc = PPC::VOR; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 971 | else if (PPC::VSRCRegClass.contains(DestReg, SrcReg)) |
Hal Finkel | bbad233 | 2014-03-24 09:36:36 +0000 | [diff] [blame] | 972 | // There are two different ways this can be done: |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 973 | // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only |
| 974 | // issue in VSU pipeline 0. |
| 975 | // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but |
| 976 | // can go to either pipeline. |
Hal Finkel | bbad233 | 2014-03-24 09:36:36 +0000 | [diff] [blame] | 977 | // We'll always use xxlor here, because in practically all cases where |
| 978 | // copies are generated, they are close enough to some use that the |
| 979 | // lower-latency form is preferable. |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 980 | Opc = PPC::XXLOR; |
Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 981 | else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg) || |
| 982 | PPC::VSSRCRegClass.contains(DestReg, SrcReg)) |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 983 | Opc = PPC::XXLORf; |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 984 | else if (PPC::QFRCRegClass.contains(DestReg, SrcReg)) |
| 985 | Opc = PPC::QVFMR; |
| 986 | else if (PPC::QSRCRegClass.contains(DestReg, SrcReg)) |
| 987 | Opc = PPC::QVFMRs; |
| 988 | else if (PPC::QBRCRegClass.contains(DestReg, SrcReg)) |
| 989 | Opc = PPC::QVFMRb; |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 990 | else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg)) |
| 991 | Opc = PPC::CROR; |
| 992 | else |
| 993 | llvm_unreachable("Impossible reg-to-reg copy"); |
Owen Anderson | 7a73ae9 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 994 | |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 995 | const MCInstrDesc &MCID = get(Opc); |
| 996 | if (MCID.getNumOperands() == 3) |
| 997 | BuildMI(MBB, I, DL, MCID, DestReg) |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 998 | .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc)); |
| 999 | else |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1000 | BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc)); |
Owen Anderson | 7a73ae9 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Hal Finkel | 8f6834d | 2011-12-05 17:55:17 +0000 | [diff] [blame] | 1003 | // 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] | 1004 | bool |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 1005 | PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF, |
| 1006 | unsigned SrcReg, bool isKill, |
Bill Wendling | c6c48fc | 2008-03-10 22:49:16 +0000 | [diff] [blame] | 1007 | int FrameIdx, |
| 1008 | const TargetRegisterClass *RC, |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1009 | SmallVectorImpl<MachineInstr*> &NewMIs, |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1010 | bool &NonRI, bool &SpillsVRS) const{ |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 1011 | // Note: If additional store instructions are added here, |
| 1012 | // update isStoreToStackSlot. |
| 1013 | |
Chris Lattner | 6f306d7 | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 1014 | DebugLoc DL; |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 1015 | if (PPC::GPRCRegClass.hasSubClassEq(RC) || |
| 1016 | PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 794e05b | 2013-03-23 17:14:27 +0000 | [diff] [blame] | 1017 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW)) |
| 1018 | .addReg(SrcReg, |
| 1019 | getKillRegState(isKill)), |
| 1020 | FrameIdx)); |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 1021 | } else if (PPC::G8RCRegClass.hasSubClassEq(RC) || |
| 1022 | PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 794e05b | 2013-03-23 17:14:27 +0000 | [diff] [blame] | 1023 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD)) |
| 1024 | .addReg(SrcReg, |
| 1025 | getKillRegState(isKill)), |
| 1026 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1027 | } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { |
Dale Johannesen | 6b8c76a | 2009-02-12 23:08:38 +0000 | [diff] [blame] | 1028 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD)) |
Bill Wendling | f7b83c7 | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 1029 | .addReg(SrcReg, |
| 1030 | getKillRegState(isKill)), |
| 1031 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1032 | } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { |
Dale Johannesen | 6b8c76a | 2009-02-12 23:08:38 +0000 | [diff] [blame] | 1033 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS)) |
Bill Wendling | f7b83c7 | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 1034 | .addReg(SrcReg, |
| 1035 | getKillRegState(isKill)), |
| 1036 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1037 | } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | e154c8f | 2013-03-12 14:12:16 +0000 | [diff] [blame] | 1038 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR)) |
| 1039 | .addReg(SrcReg, |
| 1040 | getKillRegState(isKill)), |
| 1041 | FrameIdx)); |
| 1042 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1043 | } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1044 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT)) |
| 1045 | .addReg(SrcReg, |
| 1046 | getKillRegState(isKill)), |
| 1047 | FrameIdx)); |
| 1048 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1049 | } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1050 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX)) |
| 1051 | .addReg(SrcReg, |
| 1052 | getKillRegState(isKill)), |
| 1053 | FrameIdx)); |
| 1054 | NonRI = true; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 1055 | } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { |
Lei Huang | 263dc4e | 2017-10-11 20:20:58 +0000 | [diff] [blame] | 1056 | unsigned Op = Subtarget.hasP9Vector() ? PPC::STXV : PPC::STXVD2X; |
Nemanja Ivanovic | 6e7879c | 2016-09-22 09:52:19 +0000 | [diff] [blame] | 1057 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op)) |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 1058 | .addReg(SrcReg, |
| 1059 | getKillRegState(isKill)), |
| 1060 | FrameIdx)); |
| 1061 | NonRI = true; |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 1062 | } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { |
Nemanja Ivanovic | 6354d23 | 2016-10-04 11:25:52 +0000 | [diff] [blame] | 1063 | unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFSTOREf64 : PPC::STXSDX; |
| 1064 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc)) |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 1065 | .addReg(SrcReg, |
| 1066 | getKillRegState(isKill)), |
| 1067 | FrameIdx)); |
| 1068 | NonRI = true; |
Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 1069 | } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) { |
Nemanja Ivanovic | 6354d23 | 2016-10-04 11:25:52 +0000 | [diff] [blame] | 1070 | unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFSTOREf32 : PPC::STXSSPX; |
| 1071 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc)) |
Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 1072 | .addReg(SrcReg, |
| 1073 | getKillRegState(isKill)), |
| 1074 | FrameIdx)); |
| 1075 | NonRI = true; |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 1076 | } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1077 | assert(Subtarget.isDarwin() && |
Hal Finkel | a7b0630 | 2013-03-27 00:02:20 +0000 | [diff] [blame] | 1078 | "VRSAVE only needs spill/restore on Darwin"); |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 1079 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE)) |
| 1080 | .addReg(SrcReg, |
| 1081 | getKillRegState(isKill)), |
| 1082 | FrameIdx)); |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1083 | SpillsVRS = true; |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 1084 | } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) { |
| 1085 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDX)) |
| 1086 | .addReg(SrcReg, |
| 1087 | getKillRegState(isKill)), |
| 1088 | FrameIdx)); |
| 1089 | NonRI = true; |
| 1090 | } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) { |
| 1091 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFSXs)) |
| 1092 | .addReg(SrcReg, |
| 1093 | getKillRegState(isKill)), |
| 1094 | FrameIdx)); |
| 1095 | NonRI = true; |
| 1096 | } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) { |
| 1097 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDXb)) |
| 1098 | .addReg(SrcReg, |
| 1099 | getKillRegState(isKill)), |
| 1100 | FrameIdx)); |
| 1101 | NonRI = true; |
Zaara Syeda | fcd9697 | 2017-09-21 16:12:33 +0000 | [diff] [blame] | 1102 | } else if (PPC::SPILLTOVSRRCRegClass.hasSubClassEq(RC)) { |
| 1103 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILLTOVSR_ST)) |
| 1104 | .addReg(SrcReg, |
| 1105 | getKillRegState(isKill)), |
| 1106 | FrameIdx)); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1107 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1108 | llvm_unreachable("Unknown regclass!"); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1109 | } |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 1110 | |
| 1111 | return false; |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | void |
| 1115 | PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 1116 | MachineBasicBlock::iterator MI, |
| 1117 | unsigned SrcReg, bool isKill, int FrameIdx, |
Evan Cheng | efb126a | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 1118 | const TargetRegisterClass *RC, |
| 1119 | const TargetRegisterInfo *TRI) const { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 1120 | MachineFunction &MF = *MBB.getParent(); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1121 | SmallVector<MachineInstr*, 4> NewMIs; |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 1122 | |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 1123 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 1124 | FuncInfo->setHasSpills(); |
| 1125 | |
Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 1126 | // We need to avoid a situation in which the value from a VRRC register is |
| 1127 | // spilled using an Altivec instruction and reloaded into a VSRC register |
| 1128 | // using a VSX instruction. The issue with this is that the VSX |
| 1129 | // load/store instructions swap the doublewords in the vector and the Altivec |
| 1130 | // ones don't. The register classes on the spill/reload may be different if |
| 1131 | // the register is defined using an Altivec instruction and is then used by a |
| 1132 | // VSX instruction. |
| 1133 | RC = updatedRC(RC); |
| 1134 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1135 | bool NonRI = false, SpillsVRS = false; |
| 1136 | if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs, |
| 1137 | NonRI, SpillsVRS)) |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 1138 | FuncInfo->setSpillsCR(); |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 1139 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1140 | if (SpillsVRS) |
| 1141 | FuncInfo->setSpillsVRSAVE(); |
| 1142 | |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1143 | if (NonRI) |
| 1144 | FuncInfo->setHasNonRISpills(); |
| 1145 | |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1146 | for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) |
| 1147 | MBB.insert(MI, NewMIs[i]); |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 1148 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1149 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 1150 | MachineMemOperand *MMO = MF.getMachineMemOperand( |
| 1151 | MachinePointerInfo::getFixedStack(MF, FrameIdx), |
| 1152 | MachineMemOperand::MOStore, MFI.getObjectSize(FrameIdx), |
| 1153 | MFI.getObjectAlignment(FrameIdx)); |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 1154 | NewMIs.back()->addMemOperand(MF, MMO); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 1157 | bool PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, const DebugLoc &DL, |
| 1158 | unsigned DestReg, int FrameIdx, |
| 1159 | const TargetRegisterClass *RC, |
| 1160 | SmallVectorImpl<MachineInstr *> &NewMIs, |
| 1161 | bool &NonRI, bool &SpillsVRS) const { |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 1162 | // Note: If additional load instructions are added here, |
| 1163 | // update isLoadFromStackSlot. |
| 1164 | |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 1165 | if (PPC::GPRCRegClass.hasSubClassEq(RC) || |
| 1166 | PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 5791f51 | 2013-03-27 19:10:40 +0000 | [diff] [blame] | 1167 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), |
| 1168 | DestReg), FrameIdx)); |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 1169 | } else if (PPC::G8RCRegClass.hasSubClassEq(RC) || |
| 1170 | PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 5791f51 | 2013-03-27 19:10:40 +0000 | [diff] [blame] | 1171 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg), |
| 1172 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1173 | } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 1174 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg), |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1175 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1176 | } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 1177 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg), |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1178 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1179 | } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | e154c8f | 2013-03-12 14:12:16 +0000 | [diff] [blame] | 1180 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, |
| 1181 | get(PPC::RESTORE_CR), DestReg), |
| 1182 | FrameIdx)); |
| 1183 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1184 | } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1185 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, |
| 1186 | get(PPC::RESTORE_CRBIT), DestReg), |
| 1187 | FrameIdx)); |
| 1188 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1189 | } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1190 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg), |
| 1191 | FrameIdx)); |
| 1192 | NonRI = true; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 1193 | } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { |
Lei Huang | 263dc4e | 2017-10-11 20:20:58 +0000 | [diff] [blame] | 1194 | unsigned Op = Subtarget.hasP9Vector() ? PPC::LXV : PPC::LXVD2X; |
Nemanja Ivanovic | 6e7879c | 2016-09-22 09:52:19 +0000 | [diff] [blame] | 1195 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op), DestReg), |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 1196 | FrameIdx)); |
| 1197 | NonRI = true; |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 1198 | } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { |
Nemanja Ivanovic | 6354d23 | 2016-10-04 11:25:52 +0000 | [diff] [blame] | 1199 | unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFLOADf64 : PPC::LXSDX; |
| 1200 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc), |
| 1201 | DestReg), FrameIdx)); |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 1202 | NonRI = true; |
Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 1203 | } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) { |
Nemanja Ivanovic | 6354d23 | 2016-10-04 11:25:52 +0000 | [diff] [blame] | 1204 | unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFLOADf32 : PPC::LXSSPX; |
| 1205 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc), |
| 1206 | DestReg), FrameIdx)); |
Nemanja Ivanovic | f3c94b1 | 2015-05-07 18:24:05 +0000 | [diff] [blame] | 1207 | NonRI = true; |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 1208 | } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1209 | assert(Subtarget.isDarwin() && |
Hal Finkel | a7b0630 | 2013-03-27 00:02:20 +0000 | [diff] [blame] | 1210 | "VRSAVE only needs spill/restore on Darwin"); |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 1211 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, |
| 1212 | get(PPC::RESTORE_VRSAVE), |
| 1213 | DestReg), |
| 1214 | FrameIdx)); |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1215 | SpillsVRS = true; |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 1216 | } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) { |
| 1217 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDX), DestReg), |
| 1218 | FrameIdx)); |
| 1219 | NonRI = true; |
| 1220 | } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) { |
| 1221 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFSXs), DestReg), |
| 1222 | FrameIdx)); |
| 1223 | NonRI = true; |
| 1224 | } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) { |
| 1225 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDXb), DestReg), |
| 1226 | FrameIdx)); |
| 1227 | NonRI = true; |
Zaara Syeda | fcd9697 | 2017-09-21 16:12:33 +0000 | [diff] [blame] | 1228 | } else if (PPC::SPILLTOVSRRCRegClass.hasSubClassEq(RC)) { |
| 1229 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILLTOVSR_LD), |
| 1230 | DestReg), FrameIdx)); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1231 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1232 | llvm_unreachable("Unknown regclass!"); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1233 | } |
Hal Finkel | bde7f8f | 2011-12-06 20:55:36 +0000 | [diff] [blame] | 1234 | |
| 1235 | return false; |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | void |
| 1239 | PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 1240 | MachineBasicBlock::iterator MI, |
| 1241 | unsigned DestReg, int FrameIdx, |
Evan Cheng | efb126a | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 1242 | const TargetRegisterClass *RC, |
| 1243 | const TargetRegisterInfo *TRI) const { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 1244 | MachineFunction &MF = *MBB.getParent(); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1245 | SmallVector<MachineInstr*, 4> NewMIs; |
Chris Lattner | 6f306d7 | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 1246 | DebugLoc DL; |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 1247 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1248 | |
| 1249 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 1250 | FuncInfo->setHasSpills(); |
| 1251 | |
Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 1252 | // We need to avoid a situation in which the value from a VRRC register is |
| 1253 | // spilled using an Altivec instruction and reloaded into a VSRC register |
| 1254 | // using a VSX instruction. The issue with this is that the VSX |
| 1255 | // load/store instructions swap the doublewords in the vector and the Altivec |
| 1256 | // ones don't. The register classes on the spill/reload may be different if |
| 1257 | // the register is defined using an Altivec instruction and is then used by a |
| 1258 | // VSX instruction. |
| 1259 | if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass) |
| 1260 | RC = &PPC::VSRCRegClass; |
| 1261 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1262 | bool NonRI = false, SpillsVRS = false; |
| 1263 | if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs, |
| 1264 | NonRI, SpillsVRS)) |
Hal Finkel | bde7f8f | 2011-12-06 20:55:36 +0000 | [diff] [blame] | 1265 | FuncInfo->setSpillsCR(); |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1266 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1267 | if (SpillsVRS) |
| 1268 | FuncInfo->setSpillsVRSAVE(); |
| 1269 | |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 1270 | if (NonRI) |
| 1271 | FuncInfo->setHasNonRISpills(); |
| 1272 | |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1273 | for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) |
| 1274 | MBB.insert(MI, NewMIs[i]); |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 1275 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1276 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 1277 | MachineMemOperand *MMO = MF.getMachineMemOperand( |
| 1278 | MachinePointerInfo::getFixedStack(MF, FrameIdx), |
| 1279 | MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIdx), |
| 1280 | MFI.getObjectAlignment(FrameIdx)); |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 1281 | NewMIs.back()->addMemOperand(MF, MMO); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 1284 | bool PPCInstrInfo:: |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 1285 | reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { |
Chris Lattner | 23f22de | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 1286 | assert(Cond.size() == 2 && "Invalid PPC branch opcode!"); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 1287 | if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR) |
| 1288 | Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0); |
| 1289 | else |
| 1290 | // Leave the CR# the same, but invert the condition. |
| 1291 | Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm())); |
Chris Lattner | 23f22de | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 1292 | return false; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 1293 | } |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1294 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1295 | bool PPCInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, |
| 1296 | unsigned Reg, MachineRegisterInfo *MRI) const { |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1297 | // For some instructions, it is legal to fold ZERO into the RA register field. |
| 1298 | // 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] | 1299 | unsigned DefOpc = DefMI.getOpcode(); |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1300 | if (DefOpc != PPC::LI && DefOpc != PPC::LI8) |
| 1301 | return false; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1302 | if (!DefMI.getOperand(1).isImm()) |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1303 | return false; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1304 | if (DefMI.getOperand(1).getImm() != 0) |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1305 | return false; |
| 1306 | |
| 1307 | // Note that we cannot here invert the arguments of an isel in order to fold |
| 1308 | // a ZERO into what is presented as the second argument. All we have here |
| 1309 | // is the condition bit, and that might come from a CR-logical bit operation. |
| 1310 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1311 | const MCInstrDesc &UseMCID = UseMI.getDesc(); |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1312 | |
| 1313 | // Only fold into real machine instructions. |
| 1314 | if (UseMCID.isPseudo()) |
| 1315 | return false; |
| 1316 | |
| 1317 | unsigned UseIdx; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1318 | for (UseIdx = 0; UseIdx < UseMI.getNumOperands(); ++UseIdx) |
| 1319 | if (UseMI.getOperand(UseIdx).isReg() && |
| 1320 | UseMI.getOperand(UseIdx).getReg() == Reg) |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1321 | break; |
| 1322 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1323 | assert(UseIdx < UseMI.getNumOperands() && "Cannot find Reg in UseMI"); |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1324 | assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg"); |
| 1325 | |
| 1326 | const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx]; |
| 1327 | |
| 1328 | // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0 |
| 1329 | // register (which might also be specified as a pointer class kind). |
| 1330 | if (UseInfo->isLookupPtrRegClass()) { |
| 1331 | if (UseInfo->RegClass /* Kind */ != 1) |
| 1332 | return false; |
| 1333 | } else { |
| 1334 | if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID && |
| 1335 | UseInfo->RegClass != PPC::G8RC_NOX0RegClassID) |
| 1336 | return false; |
| 1337 | } |
| 1338 | |
| 1339 | // Make sure this is not tied to an output register (or otherwise |
| 1340 | // constrained). This is true for ST?UX registers, for example, which |
| 1341 | // are tied to their output registers. |
| 1342 | if (UseInfo->Constraints != 0) |
| 1343 | return false; |
| 1344 | |
| 1345 | unsigned ZeroReg; |
| 1346 | if (UseInfo->isLookupPtrRegClass()) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1347 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1348 | ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO; |
| 1349 | } else { |
| 1350 | ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ? |
| 1351 | PPC::ZERO8 : PPC::ZERO; |
| 1352 | } |
| 1353 | |
| 1354 | bool DeleteDef = MRI->hasOneNonDBGUse(Reg); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1355 | UseMI.getOperand(UseIdx).setReg(ZeroReg); |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1356 | |
| 1357 | if (DeleteDef) |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1358 | DefMI.eraseFromParent(); |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1359 | |
| 1360 | return true; |
| 1361 | } |
| 1362 | |
Hal Finkel | 30ae229 | 2013-04-10 18:30:16 +0000 | [diff] [blame] | 1363 | static bool MBBDefinesCTR(MachineBasicBlock &MBB) { |
| 1364 | for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end(); |
| 1365 | I != IE; ++I) |
| 1366 | if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8)) |
| 1367 | return true; |
| 1368 | return false; |
| 1369 | } |
| 1370 | |
| 1371 | // We should make sure that, if we're going to predicate both sides of a |
| 1372 | // condition (a diamond), that both sides don't define the counter register. We |
| 1373 | // can predicate counter-decrement-based branches, but while that predicates |
| 1374 | // the branching, it does not predicate the counter decrement. If we tried to |
| 1375 | // merge the triangle into one predicated block, we'd decrement the counter |
| 1376 | // twice. |
| 1377 | bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB, |
| 1378 | unsigned NumT, unsigned ExtraT, |
| 1379 | MachineBasicBlock &FMBB, |
| 1380 | unsigned NumF, unsigned ExtraF, |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 1381 | BranchProbability Probability) const { |
Hal Finkel | 30ae229 | 2013-04-10 18:30:16 +0000 | [diff] [blame] | 1382 | return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB)); |
| 1383 | } |
| 1384 | |
| 1385 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1386 | bool PPCInstrInfo::isPredicated(const MachineInstr &MI) const { |
Hal Finkel | f29285a | 2013-04-11 01:23:34 +0000 | [diff] [blame] | 1387 | // The predicated branches are identified by their type, not really by the |
| 1388 | // explicit presence of a predicate. Furthermore, some of them can be |
| 1389 | // predicated more than once. Because if conversion won't try to predicate |
| 1390 | // any instruction which already claims to be predicated (by returning true |
| 1391 | // here), always return false. In doing so, we let isPredicable() be the |
| 1392 | // final word on whether not the instruction can be (further) predicated. |
| 1393 | |
| 1394 | return false; |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1397 | bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const { |
| 1398 | if (!MI.isTerminator()) |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1399 | return false; |
| 1400 | |
| 1401 | // Conditional branch is a special case. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1402 | if (MI.isBranch() && !MI.isBarrier()) |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1403 | return true; |
| 1404 | |
| 1405 | return !isPredicated(MI); |
| 1406 | } |
| 1407 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1408 | bool PPCInstrInfo::PredicateInstruction(MachineInstr &MI, |
Ahmed Bougacha | c88bf54 | 2015-06-11 19:30:37 +0000 | [diff] [blame] | 1409 | ArrayRef<MachineOperand> Pred) const { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1410 | unsigned OpC = MI.getOpcode(); |
Hal Finkel | f4a22c0 | 2015-01-13 17:47:54 +0000 | [diff] [blame] | 1411 | if (OpC == PPC::BLR || OpC == PPC::BLR8) { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1412 | if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1413 | bool isPPC64 = Subtarget.isPPC64(); |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1414 | MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) |
| 1415 | : (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR))); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1416 | } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1417 | MI.setDesc(get(PPC::BCLR)); |
| 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 if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1421 | MI.setDesc(get(PPC::BCLRn)); |
| 1422 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 1423 | .addReg(Pred[1].getReg()); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1424 | } else { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1425 | MI.setDesc(get(PPC::BCCLR)); |
| 1426 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 1427 | .addImm(Pred[0].getImm()) |
| 1428 | .addReg(Pred[1].getReg()); |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | return true; |
| 1432 | } else if (OpC == PPC::B) { |
| 1433 | if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1434 | bool isPPC64 = Subtarget.isPPC64(); |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1435 | MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) |
| 1436 | : (isPPC64 ? PPC::BDZ8 : PPC::BDZ))); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1437 | } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1438 | MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); |
| 1439 | MI.RemoveOperand(0); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1440 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1441 | MI.setDesc(get(PPC::BC)); |
| 1442 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 1443 | .addReg(Pred[1].getReg()) |
| 1444 | .addMBB(MBB); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1445 | } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1446 | MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); |
| 1447 | MI.RemoveOperand(0); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1448 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1449 | MI.setDesc(get(PPC::BCn)); |
| 1450 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 1451 | .addReg(Pred[1].getReg()) |
| 1452 | .addMBB(MBB); |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1453 | } else { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1454 | MachineBasicBlock *MBB = MI.getOperand(0).getMBB(); |
| 1455 | MI.RemoveOperand(0); |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1456 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1457 | MI.setDesc(get(PPC::BCC)); |
| 1458 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 1459 | .addImm(Pred[0].getImm()) |
| 1460 | .addReg(Pred[1].getReg()) |
| 1461 | .addMBB(MBB); |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | return true; |
Hal Finkel | 500b004 | 2013-04-10 06:42:34 +0000 | [diff] [blame] | 1465 | } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 || |
| 1466 | OpC == PPC::BCTRL || OpC == PPC::BCTRL8) { |
| 1467 | if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) |
| 1468 | llvm_unreachable("Cannot predicate bctr[l] on the ctr register"); |
| 1469 | |
| 1470 | bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8; |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1471 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1472 | |
| 1473 | if (Pred[0].getImm() == PPC::PRED_BIT_SET) { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1474 | MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) |
| 1475 | : (setLR ? PPC::BCCTRL : PPC::BCCTR))); |
| 1476 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 1477 | .addReg(Pred[1].getReg()); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1478 | return true; |
| 1479 | } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1480 | MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) |
| 1481 | : (setLR ? PPC::BCCTRLn : PPC::BCCTRn))); |
| 1482 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 1483 | .addReg(Pred[1].getReg()); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1484 | return true; |
| 1485 | } |
| 1486 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1487 | MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) |
| 1488 | : (setLR ? PPC::BCCCTRL : PPC::BCCCTR))); |
| 1489 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 1490 | .addImm(Pred[0].getImm()) |
| 1491 | .addReg(Pred[1].getReg()); |
Hal Finkel | 500b004 | 2013-04-10 06:42:34 +0000 | [diff] [blame] | 1492 | return true; |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | return false; |
| 1496 | } |
| 1497 | |
Ahmed Bougacha | c88bf54 | 2015-06-11 19:30:37 +0000 | [diff] [blame] | 1498 | bool PPCInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1, |
| 1499 | ArrayRef<MachineOperand> Pred2) const { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1500 | assert(Pred1.size() == 2 && "Invalid PPC first predicate"); |
| 1501 | assert(Pred2.size() == 2 && "Invalid PPC second predicate"); |
| 1502 | |
| 1503 | if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR) |
| 1504 | return false; |
| 1505 | if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR) |
| 1506 | return false; |
| 1507 | |
Hal Finkel | 94a6f38 | 2013-12-11 23:12:25 +0000 | [diff] [blame] | 1508 | // P1 can only subsume P2 if they test the same condition register. |
| 1509 | if (Pred1[1].getReg() != Pred2[1].getReg()) |
| 1510 | return false; |
| 1511 | |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1512 | PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm(); |
| 1513 | PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm(); |
| 1514 | |
| 1515 | if (P1 == P2) |
| 1516 | return true; |
| 1517 | |
| 1518 | // Does P1 subsume P2, e.g. GE subsumes GT. |
| 1519 | if (P1 == PPC::PRED_LE && |
| 1520 | (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ)) |
| 1521 | return true; |
| 1522 | if (P1 == PPC::PRED_GE && |
| 1523 | (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ)) |
| 1524 | return true; |
| 1525 | |
| 1526 | return false; |
| 1527 | } |
| 1528 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1529 | bool PPCInstrInfo::DefinesPredicate(MachineInstr &MI, |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1530 | std::vector<MachineOperand> &Pred) const { |
| 1531 | // Note: At the present time, the contents of Pred from this function is |
| 1532 | // unused by IfConversion. This implementation follows ARM by pushing the |
| 1533 | // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of |
| 1534 | // predicate, instructions defining CTR or CTR8 are also included as |
| 1535 | // predicate-defining instructions. |
| 1536 | |
| 1537 | const TargetRegisterClass *RCs[] = |
| 1538 | { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass, |
| 1539 | &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass }; |
| 1540 | |
| 1541 | bool Found = false; |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1542 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 1543 | const MachineOperand &MO = MI.getOperand(i); |
Hal Finkel | af82201 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1544 | for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1545 | const TargetRegisterClass *RC = RCs[c]; |
Hal Finkel | af82201 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1546 | if (MO.isReg()) { |
| 1547 | if (MO.isDef() && RC->contains(MO.getReg())) { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1548 | Pred.push_back(MO); |
| 1549 | Found = true; |
| 1550 | } |
Hal Finkel | af82201 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1551 | } else if (MO.isRegMask()) { |
| 1552 | for (TargetRegisterClass::iterator I = RC->begin(), |
| 1553 | IE = RC->end(); I != IE; ++I) |
| 1554 | if (MO.clobbersPhysReg(*I)) { |
| 1555 | Pred.push_back(MO); |
| 1556 | Found = true; |
| 1557 | } |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1558 | } |
| 1559 | } |
| 1560 | } |
| 1561 | |
| 1562 | return Found; |
| 1563 | } |
| 1564 | |
Krzysztof Parzyszek | cc31871 | 2017-03-03 18:30:54 +0000 | [diff] [blame] | 1565 | bool PPCInstrInfo::isPredicable(const MachineInstr &MI) const { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1566 | unsigned OpC = MI.getOpcode(); |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1567 | switch (OpC) { |
| 1568 | default: |
| 1569 | return false; |
| 1570 | case PPC::B: |
| 1571 | case PPC::BLR: |
Hal Finkel | f4a22c0 | 2015-01-13 17:47:54 +0000 | [diff] [blame] | 1572 | case PPC::BLR8: |
Hal Finkel | 500b004 | 2013-04-10 06:42:34 +0000 | [diff] [blame] | 1573 | case PPC::BCTR: |
| 1574 | case PPC::BCTR8: |
| 1575 | case PPC::BCTRL: |
| 1576 | case PPC::BCTRL8: |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1577 | return true; |
| 1578 | } |
| 1579 | } |
| 1580 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1581 | bool PPCInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg, |
| 1582 | unsigned &SrcReg2, int &Mask, |
| 1583 | int &Value) const { |
| 1584 | unsigned Opc = MI.getOpcode(); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1585 | |
| 1586 | switch (Opc) { |
| 1587 | default: return false; |
| 1588 | case PPC::CMPWI: |
| 1589 | case PPC::CMPLWI: |
| 1590 | case PPC::CMPDI: |
| 1591 | case PPC::CMPLDI: |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1592 | SrcReg = MI.getOperand(1).getReg(); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1593 | SrcReg2 = 0; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1594 | Value = MI.getOperand(2).getImm(); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1595 | Mask = 0xFFFF; |
| 1596 | return true; |
| 1597 | case PPC::CMPW: |
| 1598 | case PPC::CMPLW: |
| 1599 | case PPC::CMPD: |
| 1600 | case PPC::CMPLD: |
| 1601 | case PPC::FCMPUS: |
| 1602 | case PPC::FCMPUD: |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1603 | SrcReg = MI.getOperand(1).getReg(); |
| 1604 | SrcReg2 = MI.getOperand(2).getReg(); |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1605 | Value = 0; |
| 1606 | Mask = 0; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1607 | return true; |
| 1608 | } |
| 1609 | } |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1610 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1611 | bool PPCInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, unsigned SrcReg, |
| 1612 | unsigned SrcReg2, int Mask, int Value, |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1613 | const MachineRegisterInfo *MRI) const { |
Hal Finkel | b12da6b | 2013-04-18 22:54:25 +0000 | [diff] [blame] | 1614 | if (DisableCmpOpt) |
| 1615 | return false; |
| 1616 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1617 | int OpC = CmpInstr.getOpcode(); |
| 1618 | unsigned CRReg = CmpInstr.getOperand(0).getReg(); |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1619 | |
| 1620 | // FP record forms set CR1 based on the execption status bits, not a |
| 1621 | // comparison with zero. |
| 1622 | if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD) |
| 1623 | return false; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1624 | |
| 1625 | // The record forms set the condition register based on a signed comparison |
| 1626 | // with zero (so says the ISA manual). This is not as straightforward as it |
| 1627 | // seems, however, because this is always a 64-bit comparison on PPC64, even |
| 1628 | // for instructions that are 32-bit in nature (like slw for example). |
| 1629 | // So, on PPC32, for unsigned comparisons, we can use the record forms only |
| 1630 | // for equality checks (as those don't depend on the sign). On PPC64, |
| 1631 | // we are restricted to equality for unsigned 64-bit comparisons and for |
| 1632 | // signed 32-bit comparisons the applicability is more restricted. |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1633 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1634 | bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW; |
| 1635 | bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW; |
| 1636 | bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD; |
| 1637 | |
| 1638 | // Get the unique definition of SrcReg. |
| 1639 | MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg); |
| 1640 | if (!MI) return false; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1641 | |
| 1642 | bool equalityOnly = false; |
| 1643 | bool noSub = false; |
| 1644 | if (isPPC64) { |
| 1645 | if (is32BitSignedCompare) { |
| 1646 | // We can perform this optimization only if MI is sign-extending. |
Hiroshi Inoue | 5388e66 | 2017-10-18 10:31:19 +0000 | [diff] [blame] | 1647 | if (isSignExtended(*MI)) |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1648 | noSub = true; |
Hiroshi Inoue | 5388e66 | 2017-10-18 10:31:19 +0000 | [diff] [blame] | 1649 | else |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1650 | return false; |
| 1651 | } else if (is32BitUnsignedCompare) { |
| 1652 | // We can perform this optimization, equality only, if MI is |
| 1653 | // zero-extending. |
Hiroshi Inoue | 5388e66 | 2017-10-18 10:31:19 +0000 | [diff] [blame] | 1654 | if (isZeroExtended(*MI)) { |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1655 | noSub = true; |
| 1656 | equalityOnly = true; |
| 1657 | } else |
| 1658 | return false; |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1659 | } else |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1660 | equalityOnly = is64BitUnsignedCompare; |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1661 | } else |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1662 | equalityOnly = is32BitUnsignedCompare; |
| 1663 | |
| 1664 | if (equalityOnly) { |
| 1665 | // We need to check the uses of the condition register in order to reject |
| 1666 | // non-equality comparisons. |
Hiroshi Inoue | 393ef84 | 2017-07-18 13:31:40 +0000 | [diff] [blame] | 1667 | for (MachineRegisterInfo::use_instr_iterator |
| 1668 | I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end(); |
| 1669 | I != IE; ++I) { |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1670 | MachineInstr *UseMI = &*I; |
| 1671 | if (UseMI->getOpcode() == PPC::BCC) { |
Hiroshi Inoue | 967dc58 | 2017-07-27 08:14:48 +0000 | [diff] [blame] | 1672 | PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm(); |
| 1673 | unsigned PredCond = PPC::getPredicateCondition(Pred); |
| 1674 | // We ignore hint bits when checking for non-equality comparisons. |
| 1675 | if (PredCond != PPC::PRED_EQ && PredCond != PPC::PRED_NE) |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1676 | return false; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1677 | } else if (UseMI->getOpcode() == PPC::ISEL || |
| 1678 | UseMI->getOpcode() == PPC::ISEL8) { |
| 1679 | unsigned SubIdx = UseMI->getOperand(3).getSubReg(); |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1680 | if (SubIdx != PPC::sub_eq) |
| 1681 | return false; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1682 | } else |
| 1683 | return false; |
| 1684 | } |
| 1685 | } |
| 1686 | |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1687 | MachineBasicBlock::iterator I = CmpInstr; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1688 | |
| 1689 | // 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] | 1690 | for (MachineBasicBlock::iterator EL = CmpInstr.getParent()->end(); I != EL; |
| 1691 | ++I) { |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1692 | bool FoundUse = false; |
Hiroshi Inoue | 393ef84 | 2017-07-18 13:31:40 +0000 | [diff] [blame] | 1693 | for (MachineRegisterInfo::use_instr_iterator |
| 1694 | J = MRI->use_instr_begin(CRReg), JE = MRI->use_instr_end(); |
| 1695 | J != JE; ++J) |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1696 | if (&*J == &*I) { |
| 1697 | FoundUse = true; |
| 1698 | break; |
| 1699 | } |
| 1700 | |
| 1701 | if (FoundUse) |
| 1702 | break; |
| 1703 | } |
| 1704 | |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1705 | SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate; |
| 1706 | SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate; |
| 1707 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1708 | // There are two possible candidates which can be changed to set CR[01]. |
| 1709 | // One is MI, the other is a SUB instruction. |
| 1710 | // 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] | 1711 | MachineInstr *Sub = nullptr; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1712 | if (SrcReg2 != 0) |
| 1713 | // MI is not a candidate for CMPrr. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1714 | MI = nullptr; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1715 | // FIXME: Conservatively refuse to convert an instruction which isn't in the |
| 1716 | // same BB as the comparison. This is to allow the check below to avoid calls |
| 1717 | // (and other explicit clobbers); instead we should really check for these |
| 1718 | // more explicitly (in at least a few predecessors). |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1719 | else if (MI->getParent() != CmpInstr.getParent()) |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1720 | return false; |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1721 | else if (Value != 0) { |
Hiroshi Inoue | b72b1fb | 2017-10-26 09:01:51 +0000 | [diff] [blame] | 1722 | // The record-form instructions set CR bit based on signed comparison |
| 1723 | // against 0. We try to convert a compare against 1 or -1 into a compare |
| 1724 | // against 0 to exploit record-form instructions. For example, we change |
| 1725 | // the condition "greater than -1" into "greater than or equal to 0" |
| 1726 | // and "less than 1" into "less than or equal to 0". |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1727 | |
Hiroshi Inoue | b72b1fb | 2017-10-26 09:01:51 +0000 | [diff] [blame] | 1728 | // Since we optimize comparison based on a specific branch condition, |
| 1729 | // we don't optimize if condition code is used by more than once. |
| 1730 | if (equalityOnly || !MRI->hasOneUse(CRReg)) |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1731 | return false; |
Hiroshi Inoue | b72b1fb | 2017-10-26 09:01:51 +0000 | [diff] [blame] | 1732 | |
| 1733 | MachineInstr *UseMI = &*MRI->use_instr_begin(CRReg); |
| 1734 | if (UseMI->getOpcode() != PPC::BCC) |
| 1735 | return false; |
| 1736 | |
| 1737 | PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm(); |
| 1738 | PPC::Predicate NewPred = Pred; |
| 1739 | unsigned PredCond = PPC::getPredicateCondition(Pred); |
| 1740 | unsigned PredHint = PPC::getPredicateHint(Pred); |
| 1741 | int16_t Immed = (int16_t)Value; |
| 1742 | |
| 1743 | // When modyfing the condition in the predicate, we propagate hint bits |
| 1744 | // from the original predicate to the new one. |
| 1745 | if (Immed == -1 && PredCond == PPC::PRED_GT) |
| 1746 | // We convert "greater than -1" into "greater than or equal to 0", |
| 1747 | // since we are assuming signed comparison by !equalityOnly |
| 1748 | NewPred = PPC::getPredicate(PPC::PRED_GE, PredHint); |
| 1749 | else if (Immed == -1 && PredCond == PPC::PRED_LE) |
| 1750 | // We convert "less than or equal to -1" into "less than 0". |
| 1751 | NewPred = PPC::getPredicate(PPC::PRED_LT, PredHint); |
| 1752 | else if (Immed == 1 && PredCond == PPC::PRED_LT) |
| 1753 | // We convert "less than 1" into "less than or equal to 0". |
| 1754 | NewPred = PPC::getPredicate(PPC::PRED_LE, PredHint); |
| 1755 | else if (Immed == 1 && PredCond == PPC::PRED_GE) |
| 1756 | // We convert "greater than or equal to 1" into "greater than 0". |
| 1757 | NewPred = PPC::getPredicate(PPC::PRED_GT, PredHint); |
| 1758 | else |
| 1759 | return false; |
| 1760 | |
| 1761 | PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)), |
| 1762 | NewPred)); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | // Search for Sub. |
| 1766 | const TargetRegisterInfo *TRI = &getRegisterInfo(); |
| 1767 | --I; |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1768 | |
| 1769 | // Get ready to iterate backward from CmpInstr. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1770 | MachineBasicBlock::iterator E = MI, B = CmpInstr.getParent()->begin(); |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1771 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1772 | for (; I != E && !noSub; --I) { |
| 1773 | const MachineInstr &Instr = *I; |
| 1774 | unsigned IOpC = Instr.getOpcode(); |
| 1775 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1776 | if (&*I != &CmpInstr && (Instr.modifiesRegister(PPC::CR0, TRI) || |
| 1777 | Instr.readsRegister(PPC::CR0, TRI))) |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1778 | // This instruction modifies or uses the record condition register after |
| 1779 | // the one we want to change. While we could do this transformation, it |
| 1780 | // would likely not be profitable. This transformation removes one |
| 1781 | // instruction, and so even forcing RA to generate one move probably |
| 1782 | // makes it unprofitable. |
| 1783 | return false; |
| 1784 | |
| 1785 | // Check whether CmpInstr can be made redundant by the current instruction. |
| 1786 | if ((OpC == PPC::CMPW || OpC == PPC::CMPLW || |
| 1787 | OpC == PPC::CMPD || OpC == PPC::CMPLD) && |
| 1788 | (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) && |
| 1789 | ((Instr.getOperand(1).getReg() == SrcReg && |
| 1790 | Instr.getOperand(2).getReg() == SrcReg2) || |
| 1791 | (Instr.getOperand(1).getReg() == SrcReg2 && |
| 1792 | Instr.getOperand(2).getReg() == SrcReg))) { |
| 1793 | Sub = &*I; |
| 1794 | break; |
| 1795 | } |
| 1796 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1797 | if (I == B) |
| 1798 | // The 'and' is below the comparison instruction. |
| 1799 | return false; |
| 1800 | } |
| 1801 | |
| 1802 | // Return false if no candidates exist. |
| 1803 | if (!MI && !Sub) |
| 1804 | return false; |
| 1805 | |
| 1806 | // The single candidate is called MI. |
| 1807 | if (!MI) MI = Sub; |
| 1808 | |
| 1809 | int NewOpC = -1; |
Hiroshi Inoue | 5388e66 | 2017-10-18 10:31:19 +0000 | [diff] [blame] | 1810 | int MIOpC = MI->getOpcode(); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1811 | if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8) |
| 1812 | NewOpC = MIOpC; |
| 1813 | else { |
| 1814 | NewOpC = PPC::getRecordFormOpcode(MIOpC); |
| 1815 | if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1) |
| 1816 | NewOpC = MIOpC; |
| 1817 | } |
| 1818 | |
| 1819 | // FIXME: On the non-embedded POWER architectures, only some of the record |
| 1820 | // forms are fast, and we should use only the fast ones. |
| 1821 | |
| 1822 | // The defining instruction has a record form (or is already a record |
| 1823 | // form). It is possible, however, that we'll need to reverse the condition |
| 1824 | // code of the users. |
| 1825 | if (NewOpC == -1) |
| 1826 | return false; |
| 1827 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1828 | // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP |
| 1829 | // needs to be updated to be based on SUB. Push the condition code |
| 1830 | // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the |
| 1831 | // condition code of these operands will be modified. |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1832 | // Here, Value == 0 means we haven't converted comparison against 1 or -1 to |
| 1833 | // comparison against 0, which may modify predicate. |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1834 | bool ShouldSwap = false; |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1835 | if (Sub && Value == 0) { |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1836 | ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 && |
| 1837 | Sub->getOperand(2).getReg() == SrcReg; |
| 1838 | |
| 1839 | // The operands to subf are the opposite of sub, so only in the fixed-point |
| 1840 | // case, invert the order. |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1841 | ShouldSwap = !ShouldSwap; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | if (ShouldSwap) |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1845 | for (MachineRegisterInfo::use_instr_iterator |
| 1846 | I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end(); |
| 1847 | I != IE; ++I) { |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1848 | MachineInstr *UseMI = &*I; |
| 1849 | if (UseMI->getOpcode() == PPC::BCC) { |
| 1850 | PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm(); |
Hiroshi Inoue | 967dc58 | 2017-07-27 08:14:48 +0000 | [diff] [blame] | 1851 | unsigned PredCond = PPC::getPredicateCondition(Pred); |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1852 | assert((!equalityOnly || |
Hiroshi Inoue | 967dc58 | 2017-07-27 08:14:48 +0000 | [diff] [blame] | 1853 | PredCond == PPC::PRED_EQ || PredCond == PPC::PRED_NE) && |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1854 | "Invalid predicate for equality-only optimization"); |
Hiroshi Inoue | 967dc58 | 2017-07-27 08:14:48 +0000 | [diff] [blame] | 1855 | (void)PredCond; // To suppress warning in release build. |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1856 | PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)), |
Hal Finkel | 0f64e21 | 2013-04-20 05:16:26 +0000 | [diff] [blame] | 1857 | PPC::getSwappedPredicate(Pred))); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1858 | } else if (UseMI->getOpcode() == PPC::ISEL || |
| 1859 | UseMI->getOpcode() == PPC::ISEL8) { |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1860 | unsigned NewSubReg = UseMI->getOperand(3).getSubReg(); |
| 1861 | assert((!equalityOnly || NewSubReg == PPC::sub_eq) && |
| 1862 | "Invalid CR bit for equality-only optimization"); |
| 1863 | |
| 1864 | if (NewSubReg == PPC::sub_lt) |
| 1865 | NewSubReg = PPC::sub_gt; |
| 1866 | else if (NewSubReg == PPC::sub_gt) |
| 1867 | NewSubReg = PPC::sub_lt; |
| 1868 | |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1869 | SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)), |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1870 | NewSubReg)); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1871 | } else // We need to abort on a user we don't understand. |
| 1872 | return false; |
| 1873 | } |
Hiroshi Inoue | 37e63b1 | 2017-05-21 06:00:05 +0000 | [diff] [blame] | 1874 | assert(!(Value != 0 && ShouldSwap) && |
| 1875 | "Non-zero immediate support and ShouldSwap" |
| 1876 | "may conflict in updating predicate"); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1877 | |
| 1878 | // Create a new virtual register to hold the value of the CR set by the |
| 1879 | // record-form instruction. If the instruction was not previously in |
| 1880 | // 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] | 1881 | CmpInstr.eraseFromParent(); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1882 | |
| 1883 | MachineBasicBlock::iterator MII = MI; |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1884 | BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(), |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1885 | get(TargetOpcode::COPY), CRReg) |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1886 | .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1887 | |
Chuang-Yu Cheng | 94f58e7 | 2016-04-12 03:10:52 +0000 | [diff] [blame] | 1888 | // Even if CR0 register were dead before, it is alive now since the |
| 1889 | // instruction we just built uses it. |
| 1890 | MI->clearRegisterDeads(PPC::CR0); |
| 1891 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1892 | if (MIOpC != NewOpC) { |
| 1893 | // We need to be careful here: we're replacing one instruction with |
| 1894 | // another, and we need to make sure that we get all of the right |
| 1895 | // implicit uses and defs. On the other hand, the caller may be holding |
| 1896 | // an iterator to this instruction, and so we can't delete it (this is |
| 1897 | // specifically the case if this is the instruction directly after the |
| 1898 | // compare). |
| 1899 | |
| 1900 | const MCInstrDesc &NewDesc = get(NewOpC); |
| 1901 | MI->setDesc(NewDesc); |
| 1902 | |
| 1903 | if (NewDesc.ImplicitDefs) |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 1904 | for (const MCPhysReg *ImpDefs = NewDesc.getImplicitDefs(); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1905 | *ImpDefs; ++ImpDefs) |
| 1906 | if (!MI->definesRegister(*ImpDefs)) |
| 1907 | MI->addOperand(*MI->getParent()->getParent(), |
| 1908 | MachineOperand::CreateReg(*ImpDefs, true, true)); |
| 1909 | if (NewDesc.ImplicitUses) |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 1910 | for (const MCPhysReg *ImpUses = NewDesc.getImplicitUses(); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1911 | *ImpUses; ++ImpUses) |
| 1912 | if (!MI->readsRegister(*ImpUses)) |
| 1913 | MI->addOperand(*MI->getParent()->getParent(), |
| 1914 | MachineOperand::CreateReg(*ImpUses, false, true)); |
| 1915 | } |
Keno Fischer | 5573483 | 2016-06-01 20:31:07 +0000 | [diff] [blame] | 1916 | assert(MI->definesRegister(PPC::CR0) && |
| 1917 | "Record-form instruction does not define cr0?"); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1918 | |
| 1919 | // Modify the condition code of operands in OperandsToUpdate. |
| 1920 | // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to |
| 1921 | // 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] | 1922 | for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++) |
| 1923 | PredsToUpdate[i].first->setImm(PredsToUpdate[i].second); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1924 | |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1925 | for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++) |
| 1926 | SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1927 | |
| 1928 | return true; |
| 1929 | } |
| 1930 | |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1931 | /// GetInstSize - Return the number of bytes of code the specified |
| 1932 | /// instruction may be. This returns the maximum number of bytes. |
| 1933 | /// |
Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 1934 | unsigned PPCInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1935 | unsigned Opcode = MI.getOpcode(); |
Hal Finkel | a7bbaf6 | 2014-02-02 06:12:27 +0000 | [diff] [blame] | 1936 | |
| 1937 | if (Opcode == PPC::INLINEASM) { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1938 | const MachineFunction *MF = MI.getParent()->getParent(); |
| 1939 | const char *AsmStr = MI.getOperand(0).getSymbolName(); |
Chris Lattner | 7b26fce | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 1940 | return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); |
Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 1941 | } else if (Opcode == TargetOpcode::STACKMAP) { |
Philip Reames | e83c4b3 | 2016-08-23 23:33:29 +0000 | [diff] [blame] | 1942 | StackMapOpers Opers(&MI); |
| 1943 | return Opers.getNumPatchBytes(); |
Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 1944 | } else if (Opcode == TargetOpcode::PATCHPOINT) { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1945 | PatchPointOpers Opers(&MI); |
Philip Reames | e83c4b3 | 2016-08-23 23:33:29 +0000 | [diff] [blame] | 1946 | return Opers.getNumPatchBytes(); |
Hal Finkel | a7bbaf6 | 2014-02-02 06:12:27 +0000 | [diff] [blame] | 1947 | } else { |
Eric Christopher | f48ef33 | 2017-03-27 22:40:51 +0000 | [diff] [blame] | 1948 | return get(Opcode).getSize(); |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1949 | } |
| 1950 | } |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1951 | |
Hal Finkel | 2d55698 | 2015-08-30 07:50:35 +0000 | [diff] [blame] | 1952 | std::pair<unsigned, unsigned> |
| 1953 | PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const { |
| 1954 | const unsigned Mask = PPCII::MO_ACCESS_MASK; |
| 1955 | return std::make_pair(TF & Mask, TF & ~Mask); |
| 1956 | } |
| 1957 | |
| 1958 | ArrayRef<std::pair<unsigned, const char *>> |
| 1959 | PPCInstrInfo::getSerializableDirectMachineOperandTargetFlags() const { |
| 1960 | using namespace PPCII; |
Hal Finkel | 982e8d4 | 2015-08-30 08:07:29 +0000 | [diff] [blame] | 1961 | static const std::pair<unsigned, const char *> TargetFlags[] = { |
Hal Finkel | 2d55698 | 2015-08-30 07:50:35 +0000 | [diff] [blame] | 1962 | {MO_LO, "ppc-lo"}, |
| 1963 | {MO_HA, "ppc-ha"}, |
| 1964 | {MO_TPREL_LO, "ppc-tprel-lo"}, |
| 1965 | {MO_TPREL_HA, "ppc-tprel-ha"}, |
| 1966 | {MO_DTPREL_LO, "ppc-dtprel-lo"}, |
| 1967 | {MO_TLSLD_LO, "ppc-tlsld-lo"}, |
| 1968 | {MO_TOC_LO, "ppc-toc-lo"}, |
| 1969 | {MO_TLS, "ppc-tls"}}; |
| 1970 | return makeArrayRef(TargetFlags); |
| 1971 | } |
| 1972 | |
| 1973 | ArrayRef<std::pair<unsigned, const char *>> |
| 1974 | PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const { |
| 1975 | using namespace PPCII; |
Hal Finkel | 982e8d4 | 2015-08-30 08:07:29 +0000 | [diff] [blame] | 1976 | static const std::pair<unsigned, const char *> TargetFlags[] = { |
Rafael Espindola | a99ccfc | 2016-06-29 14:59:50 +0000 | [diff] [blame] | 1977 | {MO_PLT, "ppc-plt"}, |
Hal Finkel | 2d55698 | 2015-08-30 07:50:35 +0000 | [diff] [blame] | 1978 | {MO_PIC_FLAG, "ppc-pic"}, |
| 1979 | {MO_NLP_FLAG, "ppc-nlp"}, |
| 1980 | {MO_NLP_HIDDEN_FLAG, "ppc-nlp-hidden"}}; |
| 1981 | return makeArrayRef(TargetFlags); |
| 1982 | } |
| 1983 | |
Tony Jiang | 438bf4a | 2017-11-20 14:38:30 +0000 | [diff] [blame] | 1984 | // Expand VSX Memory Pseudo instruction to either a VSX or a FP instruction. |
| 1985 | // The VSX versions have the advantage of a full 64-register target whereas |
| 1986 | // the FP ones have the advantage of lower latency and higher throughput. So |
| 1987 | // what we are after is using the faster instructions in low register pressure |
| 1988 | // situations and using the larger register file in high register pressure |
| 1989 | // situations. |
| 1990 | bool PPCInstrInfo::expandVSXMemPseudo(MachineInstr &MI) const { |
Nemanja Ivanovic | 6354d23 | 2016-10-04 11:25:52 +0000 | [diff] [blame] | 1991 | unsigned UpperOpcode, LowerOpcode; |
| 1992 | switch (MI.getOpcode()) { |
| 1993 | case PPC::DFLOADf32: |
| 1994 | UpperOpcode = PPC::LXSSP; |
| 1995 | LowerOpcode = PPC::LFS; |
| 1996 | break; |
| 1997 | case PPC::DFLOADf64: |
| 1998 | UpperOpcode = PPC::LXSD; |
| 1999 | LowerOpcode = PPC::LFD; |
| 2000 | break; |
| 2001 | case PPC::DFSTOREf32: |
| 2002 | UpperOpcode = PPC::STXSSP; |
| 2003 | LowerOpcode = PPC::STFS; |
| 2004 | break; |
| 2005 | case PPC::DFSTOREf64: |
| 2006 | UpperOpcode = PPC::STXSD; |
| 2007 | LowerOpcode = PPC::STFD; |
| 2008 | break; |
Tony Jiang | 438bf4a | 2017-11-20 14:38:30 +0000 | [diff] [blame] | 2009 | case PPC::XFLOADf32: |
| 2010 | UpperOpcode = PPC::LXSSPX; |
| 2011 | LowerOpcode = PPC::LFSX; |
| 2012 | break; |
| 2013 | case PPC::XFLOADf64: |
| 2014 | UpperOpcode = PPC::LXSDX; |
| 2015 | LowerOpcode = PPC::LFDX; |
| 2016 | break; |
| 2017 | case PPC::XFSTOREf32: |
| 2018 | UpperOpcode = PPC::STXSSPX; |
| 2019 | LowerOpcode = PPC::STFSX; |
| 2020 | break; |
| 2021 | case PPC::XFSTOREf64: |
| 2022 | UpperOpcode = PPC::STXSDX; |
| 2023 | LowerOpcode = PPC::STFDX; |
| 2024 | break; |
| 2025 | case PPC::LIWAX: |
| 2026 | UpperOpcode = PPC::LXSIWAX; |
| 2027 | LowerOpcode = PPC::LFIWAX; |
| 2028 | break; |
| 2029 | case PPC::LIWZX: |
| 2030 | UpperOpcode = PPC::LXSIWZX; |
| 2031 | LowerOpcode = PPC::LFIWZX; |
| 2032 | break; |
| 2033 | case PPC::STIWX: |
| 2034 | UpperOpcode = PPC::STXSIWX; |
| 2035 | LowerOpcode = PPC::STFIWX; |
| 2036 | break; |
| 2037 | default: |
| 2038 | llvm_unreachable("Unknown Operation!"); |
Nemanja Ivanovic | 6354d23 | 2016-10-04 11:25:52 +0000 | [diff] [blame] | 2039 | } |
Tony Jiang | 438bf4a | 2017-11-20 14:38:30 +0000 | [diff] [blame] | 2040 | |
Nemanja Ivanovic | 6354d23 | 2016-10-04 11:25:52 +0000 | [diff] [blame] | 2041 | unsigned TargetReg = MI.getOperand(0).getReg(); |
| 2042 | unsigned Opcode; |
| 2043 | if ((TargetReg >= PPC::F0 && TargetReg <= PPC::F31) || |
| 2044 | (TargetReg >= PPC::VSL0 && TargetReg <= PPC::VSL31)) |
| 2045 | Opcode = LowerOpcode; |
| 2046 | else |
| 2047 | Opcode = UpperOpcode; |
| 2048 | MI.setDesc(get(Opcode)); |
| 2049 | return true; |
Tony Jiang | 438bf4a | 2017-11-20 14:38:30 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | bool PPCInstrInfo::expandPostRAPseudo(MachineInstr &MI) const { |
| 2053 | auto &MBB = *MI.getParent(); |
| 2054 | auto DL = MI.getDebugLoc(); |
| 2055 | |
| 2056 | switch (MI.getOpcode()) { |
| 2057 | case TargetOpcode::LOAD_STACK_GUARD: { |
| 2058 | assert(Subtarget.isTargetLinux() && |
| 2059 | "Only Linux target is expected to contain LOAD_STACK_GUARD"); |
| 2060 | const int64_t Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008; |
| 2061 | const unsigned Reg = Subtarget.isPPC64() ? PPC::X13 : PPC::R2; |
| 2062 | MI.setDesc(get(Subtarget.isPPC64() ? PPC::LD : PPC::LWZ)); |
| 2063 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 2064 | .addImm(Offset) |
| 2065 | .addReg(Reg); |
| 2066 | return true; |
| 2067 | } |
| 2068 | case PPC::DFLOADf32: |
| 2069 | case PPC::DFLOADf64: |
| 2070 | case PPC::DFSTOREf32: |
| 2071 | case PPC::DFSTOREf64: { |
| 2072 | assert(Subtarget.hasP9Vector() && |
| 2073 | "Invalid D-Form Pseudo-ops on Pre-P9 target."); |
| 2074 | assert(MI.getOperand(2).isReg() && MI.getOperand(1).isImm() && |
| 2075 | "D-form op must have register and immediate operands"); |
| 2076 | return expandVSXMemPseudo(MI); |
| 2077 | } |
| 2078 | case PPC::XFLOADf32: |
| 2079 | case PPC::XFSTOREf32: |
| 2080 | case PPC::LIWAX: |
| 2081 | case PPC::LIWZX: |
| 2082 | case PPC::STIWX: { |
| 2083 | assert(Subtarget.hasP8Vector() && |
| 2084 | "Invalid X-Form Pseudo-ops on Pre-P8 target."); |
| 2085 | assert(MI.getOperand(2).isReg() && MI.getOperand(1).isReg() && |
| 2086 | "X-form op must have register and register operands"); |
| 2087 | return expandVSXMemPseudo(MI); |
| 2088 | } |
| 2089 | case PPC::XFLOADf64: |
| 2090 | case PPC::XFSTOREf64: { |
| 2091 | assert(Subtarget.hasVSX() && |
| 2092 | "Invalid X-Form Pseudo-ops on target that has no VSX."); |
| 2093 | assert(MI.getOperand(2).isReg() && MI.getOperand(1).isReg() && |
| 2094 | "X-form op must have register and register operands"); |
| 2095 | return expandVSXMemPseudo(MI); |
Nemanja Ivanovic | 6354d23 | 2016-10-04 11:25:52 +0000 | [diff] [blame] | 2096 | } |
Zaara Syeda | fcd9697 | 2017-09-21 16:12:33 +0000 | [diff] [blame] | 2097 | case PPC::SPILLTOVSR_LD: { |
| 2098 | unsigned TargetReg = MI.getOperand(0).getReg(); |
| 2099 | if (PPC::VSFRCRegClass.contains(TargetReg)) { |
| 2100 | MI.setDesc(get(PPC::DFLOADf64)); |
| 2101 | return expandPostRAPseudo(MI); |
| 2102 | } |
| 2103 | else |
| 2104 | MI.setDesc(get(PPC::LD)); |
| 2105 | return true; |
| 2106 | } |
| 2107 | case PPC::SPILLTOVSR_ST: { |
| 2108 | unsigned SrcReg = MI.getOperand(0).getReg(); |
| 2109 | if (PPC::VSFRCRegClass.contains(SrcReg)) { |
| 2110 | NumStoreSPILLVSRRCAsVec++; |
| 2111 | MI.setDesc(get(PPC::DFSTOREf64)); |
| 2112 | return expandPostRAPseudo(MI); |
| 2113 | } else { |
| 2114 | NumStoreSPILLVSRRCAsGpr++; |
| 2115 | MI.setDesc(get(PPC::STD)); |
| 2116 | } |
| 2117 | return true; |
| 2118 | } |
| 2119 | case PPC::SPILLTOVSR_LDX: { |
| 2120 | unsigned TargetReg = MI.getOperand(0).getReg(); |
| 2121 | if (PPC::VSFRCRegClass.contains(TargetReg)) |
| 2122 | MI.setDesc(get(PPC::LXSDX)); |
| 2123 | else |
| 2124 | MI.setDesc(get(PPC::LDX)); |
| 2125 | return true; |
| 2126 | } |
| 2127 | case PPC::SPILLTOVSR_STX: { |
| 2128 | unsigned SrcReg = MI.getOperand(0).getReg(); |
| 2129 | if (PPC::VSFRCRegClass.contains(SrcReg)) { |
| 2130 | NumStoreSPILLVSRRCAsVec++; |
| 2131 | MI.setDesc(get(PPC::STXSDX)); |
| 2132 | } else { |
| 2133 | NumStoreSPILLVSRRCAsGpr++; |
| 2134 | MI.setDesc(get(PPC::STDX)); |
| 2135 | } |
| 2136 | return true; |
| 2137 | } |
| 2138 | |
Tim Shen | 3bef27c | 2017-05-16 20:18:06 +0000 | [diff] [blame] | 2139 | case PPC::CFENCE8: { |
| 2140 | auto Val = MI.getOperand(0).getReg(); |
Hiroshi Inoue | 7a08bb1 | 2017-06-15 16:51:28 +0000 | [diff] [blame] | 2141 | 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] | 2142 | BuildMI(MBB, MI, DL, get(PPC::CTRL_DEP)) |
| 2143 | .addImm(PPC::PRED_NE_MINUS) |
| 2144 | .addReg(PPC::CR7) |
| 2145 | .addImm(1); |
| 2146 | MI.setDesc(get(PPC::ISYNC)); |
| 2147 | MI.RemoveOperand(0); |
| 2148 | return true; |
| 2149 | } |
Tim Shen | a1d8bc5 | 2016-04-19 20:14:52 +0000 | [diff] [blame] | 2150 | } |
| 2151 | return false; |
| 2152 | } |
Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 2153 | |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2154 | unsigned PPCInstrInfo::lookThruCopyLike(unsigned SrcReg, |
| 2155 | const MachineRegisterInfo *MRI) { |
| 2156 | while (true) { |
| 2157 | MachineInstr *MI = MRI->getVRegDef(SrcReg); |
| 2158 | if (!MI->isCopyLike()) |
| 2159 | return SrcReg; |
| 2160 | |
| 2161 | unsigned CopySrcReg; |
| 2162 | if (MI->isCopy()) |
| 2163 | CopySrcReg = MI->getOperand(1).getReg(); |
| 2164 | else { |
| 2165 | assert(MI->isSubregToReg() && "Bad opcode for lookThruCopyLike"); |
| 2166 | CopySrcReg = MI->getOperand(2).getReg(); |
| 2167 | } |
| 2168 | |
| 2169 | if (!TargetRegisterInfo::isVirtualRegister(CopySrcReg)) |
| 2170 | return CopySrcReg; |
| 2171 | |
| 2172 | SrcReg = CopySrcReg; |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | // Essentially a compile-time implementation of a compare->isel sequence. |
| 2177 | // It takes two constants to compare, along with the true/false registers |
| 2178 | // and the comparison type (as a subreg to a CR field) and returns one |
| 2179 | // of the true/false registers, depending on the comparison results. |
| 2180 | static unsigned selectReg(int64_t Imm1, int64_t Imm2, unsigned CompareOpc, |
| 2181 | unsigned TrueReg, unsigned FalseReg, |
| 2182 | unsigned CRSubReg) { |
| 2183 | // Signed comparisons. The immediates are assumed to be sign-extended. |
| 2184 | if (CompareOpc == PPC::CMPWI || CompareOpc == PPC::CMPDI) { |
| 2185 | switch (CRSubReg) { |
| 2186 | default: llvm_unreachable("Unknown integer comparison type."); |
| 2187 | case PPC::sub_lt: |
| 2188 | return Imm1 < Imm2 ? TrueReg : FalseReg; |
| 2189 | case PPC::sub_gt: |
| 2190 | return Imm1 > Imm2 ? TrueReg : FalseReg; |
| 2191 | case PPC::sub_eq: |
| 2192 | return Imm1 == Imm2 ? TrueReg : FalseReg; |
| 2193 | } |
| 2194 | } |
| 2195 | // Unsigned comparisons. |
| 2196 | else if (CompareOpc == PPC::CMPLWI || CompareOpc == PPC::CMPLDI) { |
| 2197 | switch (CRSubReg) { |
| 2198 | default: llvm_unreachable("Unknown integer comparison type."); |
| 2199 | case PPC::sub_lt: |
| 2200 | return (uint64_t)Imm1 < (uint64_t)Imm2 ? TrueReg : FalseReg; |
| 2201 | case PPC::sub_gt: |
| 2202 | return (uint64_t)Imm1 > (uint64_t)Imm2 ? TrueReg : FalseReg; |
| 2203 | case PPC::sub_eq: |
| 2204 | return Imm1 == Imm2 ? TrueReg : FalseReg; |
| 2205 | } |
| 2206 | } |
| 2207 | return PPC::NoRegister; |
| 2208 | } |
| 2209 | |
| 2210 | // Replace an instruction with one that materializes a constant (and sets |
| 2211 | // CR0 if the original instruction was a record-form instruction). |
| 2212 | void PPCInstrInfo::replaceInstrWithLI(MachineInstr &MI, |
| 2213 | const LoadImmediateInfo &LII) const { |
| 2214 | // Remove existing operands. |
| 2215 | int OperandToKeep = LII.SetCR ? 1 : 0; |
| 2216 | for (int i = MI.getNumOperands() - 1; i > OperandToKeep; i--) |
| 2217 | MI.RemoveOperand(i); |
| 2218 | |
| 2219 | // Replace the instruction. |
Nemanja Ivanovic | 74ecf59 | 2017-12-15 09:51:34 +0000 | [diff] [blame] | 2220 | if (LII.SetCR) { |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2221 | MI.setDesc(get(LII.Is64Bit ? PPC::ANDIo8 : PPC::ANDIo)); |
Nemanja Ivanovic | 74ecf59 | 2017-12-15 09:51:34 +0000 | [diff] [blame] | 2222 | // Set the immediate. |
| 2223 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 2224 | .addImm(LII.Imm).addReg(PPC::CR0, RegState::ImplicitDefine); |
| 2225 | return; |
| 2226 | } |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2227 | else |
| 2228 | MI.setDesc(get(LII.Is64Bit ? PPC::LI8 : PPC::LI)); |
| 2229 | |
| 2230 | // Set the immediate. |
| 2231 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 2232 | .addImm(LII.Imm); |
| 2233 | } |
| 2234 | |
| 2235 | MachineInstr *PPCInstrInfo::getConstantDefMI(MachineInstr &MI, |
| 2236 | unsigned &ConstOp, |
| 2237 | bool &SeenIntermediateUse) const { |
| 2238 | ConstOp = ~0U; |
| 2239 | MachineInstr *DefMI = nullptr; |
| 2240 | MachineRegisterInfo *MRI = &MI.getParent()->getParent()->getRegInfo(); |
| 2241 | // If we'ere in SSA, get the defs through the MRI. Otherwise, only look |
| 2242 | // within the basic block to see if the register is defined using an LI/LI8. |
| 2243 | if (MRI->isSSA()) { |
| 2244 | for (int i = 1, e = MI.getNumOperands(); i < e; i++) { |
| 2245 | if (!MI.getOperand(i).isReg()) |
| 2246 | continue; |
| 2247 | unsigned Reg = MI.getOperand(i).getReg(); |
| 2248 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 2249 | continue; |
| 2250 | unsigned TrueReg = lookThruCopyLike(Reg, MRI); |
| 2251 | if (TargetRegisterInfo::isVirtualRegister(TrueReg)) { |
| 2252 | DefMI = MRI->getVRegDef(TrueReg); |
| 2253 | if (DefMI->getOpcode() == PPC::LI || DefMI->getOpcode() == PPC::LI8) { |
| 2254 | ConstOp = i; |
| 2255 | break; |
| 2256 | } |
| 2257 | } |
| 2258 | } |
| 2259 | } else { |
| 2260 | // Looking back through the definition for each operand could be expensive, |
| 2261 | // so exit early if this isn't an instruction that either has an immediate |
| 2262 | // form or is already an immediate form that we can handle. |
| 2263 | ImmInstrInfo III; |
| 2264 | unsigned Opc = MI.getOpcode(); |
| 2265 | bool ConvertibleImmForm = |
| 2266 | Opc == PPC::CMPWI || Opc == PPC::CMPLWI || |
| 2267 | Opc == PPC::CMPDI || Opc == PPC::CMPLDI || |
| 2268 | Opc == PPC::ADDI || Opc == PPC::ADDI8 || |
| 2269 | Opc == PPC::ORI || Opc == PPC::ORI8 || |
| 2270 | Opc == PPC::XORI || Opc == PPC::XORI8 || |
| 2271 | Opc == PPC::RLDICL || Opc == PPC::RLDICLo || |
| 2272 | Opc == PPC::RLDICL_32 || Opc == PPC::RLDICL_32_64 || |
| 2273 | Opc == PPC::RLWINM || Opc == PPC::RLWINMo || |
| 2274 | Opc == PPC::RLWINM8 || Opc == PPC::RLWINM8o; |
| 2275 | if (!instrHasImmForm(MI, III) && !ConvertibleImmForm) |
| 2276 | return nullptr; |
| 2277 | |
| 2278 | // Don't convert or %X, %Y, %Y since that's just a register move. |
| 2279 | if ((Opc == PPC::OR || Opc == PPC::OR8) && |
| 2280 | MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) |
| 2281 | return nullptr; |
| 2282 | for (int i = 1, e = MI.getNumOperands(); i < e; i++) { |
| 2283 | MachineOperand &MO = MI.getOperand(i); |
| 2284 | SeenIntermediateUse = false; |
| 2285 | if (MO.isReg() && MO.isUse() && !MO.isImplicit()) { |
| 2286 | MachineBasicBlock::reverse_iterator E = MI.getParent()->rend(), It = MI; |
| 2287 | It++; |
| 2288 | unsigned Reg = MI.getOperand(i).getReg(); |
Nemanja Ivanovic | 6ab32de | 2017-12-15 14:17:45 +0000 | [diff] [blame] | 2289 | // MachineInstr::readsRegister only returns true if the machine |
| 2290 | // instruction reads the exact register or its super-register. It |
| 2291 | // does not consider uses of sub-registers which seems like strange |
| 2292 | // behaviour. Nonetheless, if we end up with a 64-bit register here, |
| 2293 | // get the corresponding 32-bit register to check. |
| 2294 | if (PPC::G8RCRegClass.contains(Reg)) |
| 2295 | Reg = Reg - PPC::X0 + PPC::R0; |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2296 | |
| 2297 | // Is this register defined by a load-immediate in this block? |
| 2298 | for ( ; It != E; ++It) { |
| 2299 | if (It->modifiesRegister(Reg, &getRegisterInfo())) { |
| 2300 | if (It->getOpcode() == PPC::LI || It->getOpcode() == PPC::LI8) { |
| 2301 | ConstOp = i; |
| 2302 | return &*It; |
| 2303 | } else |
| 2304 | break; |
| 2305 | } else if (It->readsRegister(Reg, &getRegisterInfo())) |
| 2306 | // If we see another use of this reg between the def and the MI, |
| 2307 | // we want to flat it so the def isn't deleted. |
| 2308 | SeenIntermediateUse = true; |
| 2309 | } |
| 2310 | } |
| 2311 | } |
| 2312 | } |
| 2313 | return ConstOp == ~0U ? nullptr : DefMI; |
| 2314 | } |
| 2315 | |
| 2316 | // If this instruction has an immediate form and one of its operands is a |
| 2317 | // result of a load-immediate, convert it to the immediate form if the constant |
| 2318 | // is in range. |
| 2319 | bool PPCInstrInfo::convertToImmediateForm(MachineInstr &MI, |
| 2320 | MachineInstr **KilledDef) const { |
| 2321 | MachineFunction *MF = MI.getParent()->getParent(); |
| 2322 | MachineRegisterInfo *MRI = &MF->getRegInfo(); |
| 2323 | bool PostRA = !MRI->isSSA(); |
| 2324 | bool SeenIntermediateUse = true; |
| 2325 | unsigned ConstantOperand = ~0U; |
| 2326 | MachineInstr *DefMI = getConstantDefMI(MI, ConstantOperand, |
| 2327 | SeenIntermediateUse); |
| 2328 | if (!DefMI || !DefMI->getOperand(1).isImm()) |
| 2329 | return false; |
| 2330 | assert(ConstantOperand < MI.getNumOperands() && |
| 2331 | "The constant operand needs to be valid at this point"); |
| 2332 | |
| 2333 | int64_t Immediate = DefMI->getOperand(1).getImm(); |
| 2334 | // Sign-extend to 64-bits. |
| 2335 | int64_t SExtImm = ((uint64_t)Immediate & ~0x7FFFuLL) != 0 ? |
| 2336 | (Immediate | 0xFFFFFFFFFFFF0000) : Immediate; |
| 2337 | |
| 2338 | if (KilledDef && MI.getOperand(ConstantOperand).isKill() && |
| 2339 | !SeenIntermediateUse) |
| 2340 | *KilledDef = DefMI; |
| 2341 | |
| 2342 | // If this is a reg+reg instruction that has a reg+imm form, convert it now. |
| 2343 | ImmInstrInfo III; |
| 2344 | if (instrHasImmForm(MI, III)) |
| 2345 | return transformToImmForm(MI, III, ConstantOperand, SExtImm); |
| 2346 | |
| 2347 | bool ReplaceWithLI = false; |
| 2348 | bool Is64BitLI = false; |
| 2349 | int64_t NewImm = 0; |
| 2350 | bool SetCR = false; |
| 2351 | unsigned Opc = MI.getOpcode(); |
| 2352 | switch (Opc) { |
| 2353 | default: return false; |
| 2354 | |
| 2355 | // FIXME: Any branches conditional on such a comparison can be made |
| 2356 | // unconditional. At this time, this happens too infrequently to be worth |
| 2357 | // the implementation effort, but if that ever changes, we could convert |
| 2358 | // such a pattern here. |
| 2359 | case PPC::CMPWI: |
| 2360 | case PPC::CMPLWI: |
| 2361 | case PPC::CMPDI: |
| 2362 | case PPC::CMPLDI: { |
| 2363 | // Doing this post-RA would require dataflow analysis to reliably find uses |
| 2364 | // of the CR register set by the compare. |
| 2365 | if (PostRA) |
| 2366 | return false; |
| 2367 | // If a compare-immediate is fed by an immediate and is itself an input of |
| 2368 | // an ISEL (the most common case) into a COPY of the correct register. |
| 2369 | bool Changed = false; |
| 2370 | unsigned DefReg = MI.getOperand(0).getReg(); |
| 2371 | int64_t Comparand = MI.getOperand(2).getImm(); |
| 2372 | int64_t SExtComparand = ((uint64_t)Comparand & ~0x7FFFuLL) != 0 ? |
| 2373 | (Comparand | 0xFFFFFFFFFFFF0000) : Comparand; |
| 2374 | |
| 2375 | for (auto &CompareUseMI : MRI->use_instructions(DefReg)) { |
| 2376 | unsigned UseOpc = CompareUseMI.getOpcode(); |
| 2377 | if (UseOpc != PPC::ISEL && UseOpc != PPC::ISEL8) |
| 2378 | continue; |
| 2379 | unsigned CRSubReg = CompareUseMI.getOperand(3).getSubReg(); |
| 2380 | unsigned TrueReg = CompareUseMI.getOperand(1).getReg(); |
| 2381 | unsigned FalseReg = CompareUseMI.getOperand(2).getReg(); |
| 2382 | unsigned RegToCopy = selectReg(SExtImm, SExtComparand, Opc, TrueReg, |
| 2383 | FalseReg, CRSubReg); |
| 2384 | if (RegToCopy == PPC::NoRegister) |
| 2385 | continue; |
| 2386 | // Can't use PPC::COPY to copy PPC::ZERO[8]. Convert it to LI[8] 0. |
| 2387 | if (RegToCopy == PPC::ZERO || RegToCopy == PPC::ZERO8) { |
| 2388 | CompareUseMI.setDesc(get(UseOpc == PPC::ISEL8 ? PPC::LI8 : PPC::LI)); |
| 2389 | CompareUseMI.getOperand(1).ChangeToImmediate(0); |
| 2390 | CompareUseMI.RemoveOperand(3); |
| 2391 | CompareUseMI.RemoveOperand(2); |
| 2392 | continue; |
| 2393 | } |
| 2394 | DEBUG(dbgs() << "Found LI -> CMPI -> ISEL, replacing with a copy.\n"); |
| 2395 | DEBUG(DefMI->dump(); MI.dump(); CompareUseMI.dump()); |
| 2396 | DEBUG(dbgs() << "Is converted to:\n"); |
| 2397 | // Convert to copy and remove unneeded operands. |
| 2398 | CompareUseMI.setDesc(get(PPC::COPY)); |
| 2399 | CompareUseMI.RemoveOperand(3); |
| 2400 | CompareUseMI.RemoveOperand(RegToCopy == TrueReg ? 2 : 1); |
| 2401 | CmpIselsConverted++; |
| 2402 | Changed = true; |
| 2403 | DEBUG(CompareUseMI.dump()); |
| 2404 | } |
| 2405 | if (Changed) |
| 2406 | return true; |
| 2407 | // This may end up incremented multiple times since this function is called |
| 2408 | // during a fixed-point transformation, but it is only meant to indicate the |
| 2409 | // presence of this opportunity. |
| 2410 | MissedConvertibleImmediateInstrs++; |
| 2411 | return false; |
| 2412 | } |
| 2413 | |
| 2414 | // Immediate forms - may simply be convertable to an LI. |
| 2415 | case PPC::ADDI: |
| 2416 | case PPC::ADDI8: { |
| 2417 | // Does the sum fit in a 16-bit signed field? |
| 2418 | int64_t Addend = MI.getOperand(2).getImm(); |
| 2419 | if (isInt<16>(Addend + SExtImm)) { |
| 2420 | ReplaceWithLI = true; |
| 2421 | Is64BitLI = Opc == PPC::ADDI8; |
| 2422 | NewImm = Addend + SExtImm; |
| 2423 | break; |
| 2424 | } |
Nemanja Ivanovic | 1794cdc | 2017-12-15 11:47:48 +0000 | [diff] [blame] | 2425 | return false; |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2426 | } |
| 2427 | case PPC::RLDICL: |
| 2428 | case PPC::RLDICLo: |
| 2429 | case PPC::RLDICL_32: |
| 2430 | case PPC::RLDICL_32_64: { |
| 2431 | // Use APInt's rotate function. |
| 2432 | int64_t SH = MI.getOperand(2).getImm(); |
| 2433 | int64_t MB = MI.getOperand(3).getImm(); |
| 2434 | APInt InVal(Opc == PPC::RLDICL ? 64 : 32, SExtImm, true); |
| 2435 | InVal = InVal.rotl(SH); |
Nemanja Ivanovic | 4e1f5e0 | 2017-12-29 12:22:27 +0000 | [diff] [blame] | 2436 | uint64_t Mask = (1LLU << (63 - MB + 1)) - 1; |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2437 | InVal &= Mask; |
| 2438 | // Can't replace negative values with an LI as that will sign-extend |
| 2439 | // and not clear the left bits. If we're setting the CR bit, we will use |
| 2440 | // ANDIo which won't sign extend, so that's safe. |
| 2441 | if (isUInt<15>(InVal.getSExtValue()) || |
| 2442 | (Opc == PPC::RLDICLo && isUInt<16>(InVal.getSExtValue()))) { |
| 2443 | ReplaceWithLI = true; |
| 2444 | Is64BitLI = Opc != PPC::RLDICL_32; |
| 2445 | NewImm = InVal.getSExtValue(); |
| 2446 | SetCR = Opc == PPC::RLDICLo; |
Benjamin Kramer | 309124e | 2018-01-12 15:03:24 +0000 | [diff] [blame] | 2447 | if (SetCR && (SExtImm & NewImm) != NewImm) |
| 2448 | return false; |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2449 | break; |
| 2450 | } |
| 2451 | return false; |
| 2452 | } |
| 2453 | case PPC::RLWINM: |
| 2454 | case PPC::RLWINM8: |
| 2455 | case PPC::RLWINMo: |
| 2456 | case PPC::RLWINM8o: { |
| 2457 | int64_t SH = MI.getOperand(2).getImm(); |
| 2458 | int64_t MB = MI.getOperand(3).getImm(); |
| 2459 | int64_t ME = MI.getOperand(4).getImm(); |
| 2460 | APInt InVal(32, SExtImm, true); |
| 2461 | InVal = InVal.rotl(SH); |
Nemanja Ivanovic | 4e1f5e0 | 2017-12-29 12:22:27 +0000 | [diff] [blame] | 2462 | // Set the bits ( MB + 32 ) to ( ME + 32 ). |
| 2463 | uint64_t Mask = ((1LLU << (32 - MB)) - 1) & ~((1LLU << (31 - ME)) - 1); |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2464 | InVal &= Mask; |
| 2465 | // Can't replace negative values with an LI as that will sign-extend |
| 2466 | // and not clear the left bits. If we're setting the CR bit, we will use |
| 2467 | // ANDIo which won't sign extend, so that's safe. |
| 2468 | bool ValueFits = isUInt<15>(InVal.getSExtValue()); |
| 2469 | ValueFits |= ((Opc == PPC::RLWINMo || Opc == PPC::RLWINM8o) && |
| 2470 | isUInt<16>(InVal.getSExtValue())); |
| 2471 | if (ValueFits) { |
| 2472 | ReplaceWithLI = true; |
| 2473 | Is64BitLI = Opc == PPC::RLWINM8 || Opc == PPC::RLWINM8o; |
| 2474 | NewImm = InVal.getSExtValue(); |
| 2475 | SetCR = Opc == PPC::RLWINMo || Opc == PPC::RLWINM8o; |
Benjamin Kramer | 309124e | 2018-01-12 15:03:24 +0000 | [diff] [blame] | 2476 | if (SetCR && (SExtImm & NewImm) != NewImm) |
| 2477 | return false; |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2478 | break; |
| 2479 | } |
| 2480 | return false; |
| 2481 | } |
| 2482 | case PPC::ORI: |
| 2483 | case PPC::ORI8: |
| 2484 | case PPC::XORI: |
| 2485 | case PPC::XORI8: { |
| 2486 | int64_t LogicalImm = MI.getOperand(2).getImm(); |
| 2487 | int64_t Result = 0; |
| 2488 | if (Opc == PPC::ORI || Opc == PPC::ORI8) |
| 2489 | Result = LogicalImm | SExtImm; |
| 2490 | else |
| 2491 | Result = LogicalImm ^ SExtImm; |
| 2492 | if (isInt<16>(Result)) { |
| 2493 | ReplaceWithLI = true; |
| 2494 | Is64BitLI = Opc == PPC::ORI8 || Opc == PPC::XORI8; |
| 2495 | NewImm = Result; |
| 2496 | break; |
| 2497 | } |
| 2498 | return false; |
| 2499 | } |
| 2500 | } |
| 2501 | |
| 2502 | if (ReplaceWithLI) { |
| 2503 | DEBUG(dbgs() << "Replacing instruction:\n"); |
| 2504 | DEBUG(MI.dump()); |
| 2505 | DEBUG(dbgs() << "Fed by:\n"); |
| 2506 | DEBUG(DefMI->dump()); |
| 2507 | LoadImmediateInfo LII; |
| 2508 | LII.Imm = NewImm; |
| 2509 | LII.Is64Bit = Is64BitLI; |
| 2510 | LII.SetCR = SetCR; |
| 2511 | // If we're setting the CR, the original load-immediate must be kept (as an |
| 2512 | // operand to ANDIo/ANDI8o). |
| 2513 | if (KilledDef && SetCR) |
| 2514 | *KilledDef = nullptr; |
| 2515 | replaceInstrWithLI(MI, LII); |
| 2516 | DEBUG(dbgs() << "With:\n"); |
| 2517 | DEBUG(MI.dump()); |
| 2518 | return true; |
| 2519 | } |
| 2520 | return false; |
| 2521 | } |
| 2522 | |
| 2523 | bool PPCInstrInfo::instrHasImmForm(const MachineInstr &MI, |
| 2524 | ImmInstrInfo &III) const { |
| 2525 | unsigned Opc = MI.getOpcode(); |
| 2526 | // The vast majority of the instructions would need their operand 2 replaced |
| 2527 | // with an immediate when switching to the reg+imm form. A marked exception |
| 2528 | // are the update form loads/stores for which a constant operand 2 would need |
| 2529 | // to turn into a displacement and move operand 1 to the operand 2 position. |
| 2530 | III.ImmOpNo = 2; |
| 2531 | III.ConstantOpNo = 2; |
| 2532 | III.ImmWidth = 16; |
| 2533 | III.ImmMustBeMultipleOf = 1; |
Nemanja Ivanovic | 4e1f5e0 | 2017-12-29 12:22:27 +0000 | [diff] [blame] | 2534 | III.TruncateImmTo = 0; |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2535 | switch (Opc) { |
| 2536 | default: return false; |
| 2537 | case PPC::ADD4: |
| 2538 | case PPC::ADD8: |
| 2539 | III.SignedImm = true; |
| 2540 | III.ZeroIsSpecialOrig = 0; |
| 2541 | III.ZeroIsSpecialNew = 1; |
| 2542 | III.IsCommutative = true; |
| 2543 | III.ImmOpcode = Opc == PPC::ADD4 ? PPC::ADDI : PPC::ADDI8; |
| 2544 | break; |
| 2545 | case PPC::ADDC: |
| 2546 | case PPC::ADDC8: |
| 2547 | III.SignedImm = true; |
| 2548 | III.ZeroIsSpecialOrig = 0; |
| 2549 | III.ZeroIsSpecialNew = 0; |
| 2550 | III.IsCommutative = true; |
| 2551 | III.ImmOpcode = Opc == PPC::ADDC ? PPC::ADDIC : PPC::ADDIC8; |
| 2552 | break; |
| 2553 | case PPC::ADDCo: |
| 2554 | III.SignedImm = true; |
| 2555 | III.ZeroIsSpecialOrig = 0; |
| 2556 | III.ZeroIsSpecialNew = 0; |
| 2557 | III.IsCommutative = true; |
| 2558 | III.ImmOpcode = PPC::ADDICo; |
| 2559 | break; |
| 2560 | case PPC::SUBFC: |
| 2561 | case PPC::SUBFC8: |
| 2562 | III.SignedImm = true; |
| 2563 | III.ZeroIsSpecialOrig = 0; |
| 2564 | III.ZeroIsSpecialNew = 0; |
| 2565 | III.IsCommutative = false; |
| 2566 | III.ImmOpcode = Opc == PPC::SUBFC ? PPC::SUBFIC : PPC::SUBFIC8; |
| 2567 | break; |
| 2568 | case PPC::CMPW: |
| 2569 | case PPC::CMPD: |
| 2570 | III.SignedImm = true; |
| 2571 | III.ZeroIsSpecialOrig = 0; |
| 2572 | III.ZeroIsSpecialNew = 0; |
| 2573 | III.IsCommutative = false; |
| 2574 | III.ImmOpcode = Opc == PPC::CMPW ? PPC::CMPWI : PPC::CMPDI; |
| 2575 | break; |
| 2576 | case PPC::CMPLW: |
| 2577 | case PPC::CMPLD: |
| 2578 | III.SignedImm = false; |
| 2579 | III.ZeroIsSpecialOrig = 0; |
| 2580 | III.ZeroIsSpecialNew = 0; |
| 2581 | III.IsCommutative = false; |
| 2582 | III.ImmOpcode = Opc == PPC::CMPLW ? PPC::CMPLWI : PPC::CMPLDI; |
| 2583 | break; |
| 2584 | case PPC::ANDo: |
| 2585 | case PPC::AND8o: |
| 2586 | case PPC::OR: |
| 2587 | case PPC::OR8: |
| 2588 | case PPC::XOR: |
| 2589 | case PPC::XOR8: |
| 2590 | III.SignedImm = false; |
| 2591 | III.ZeroIsSpecialOrig = 0; |
| 2592 | III.ZeroIsSpecialNew = 0; |
| 2593 | III.IsCommutative = true; |
| 2594 | switch(Opc) { |
| 2595 | default: llvm_unreachable("Unknown opcode"); |
| 2596 | case PPC::ANDo: III.ImmOpcode = PPC::ANDIo; break; |
| 2597 | case PPC::AND8o: III.ImmOpcode = PPC::ANDIo8; break; |
| 2598 | case PPC::OR: III.ImmOpcode = PPC::ORI; break; |
| 2599 | case PPC::OR8: III.ImmOpcode = PPC::ORI8; break; |
| 2600 | case PPC::XOR: III.ImmOpcode = PPC::XORI; break; |
| 2601 | case PPC::XOR8: III.ImmOpcode = PPC::XORI8; break; |
| 2602 | } |
| 2603 | break; |
| 2604 | case PPC::RLWNM: |
| 2605 | case PPC::RLWNM8: |
| 2606 | case PPC::RLWNMo: |
| 2607 | case PPC::RLWNM8o: |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2608 | case PPC::SLW: |
| 2609 | case PPC::SLW8: |
| 2610 | case PPC::SLWo: |
| 2611 | case PPC::SLW8o: |
| 2612 | case PPC::SRW: |
| 2613 | case PPC::SRW8: |
| 2614 | case PPC::SRWo: |
| 2615 | case PPC::SRW8o: |
| 2616 | case PPC::SRAW: |
| 2617 | case PPC::SRAWo: |
Nemanja Ivanovic | 4e1f5e0 | 2017-12-29 12:22:27 +0000 | [diff] [blame] | 2618 | III.SignedImm = false; |
| 2619 | III.ZeroIsSpecialOrig = 0; |
| 2620 | III.ZeroIsSpecialNew = 0; |
| 2621 | III.IsCommutative = false; |
| 2622 | // This isn't actually true, but the instructions ignore any of the |
| 2623 | // upper bits, so any immediate loaded with an LI is acceptable. |
| 2624 | // This does not apply to shift right algebraic because a value |
| 2625 | // out of range will produce a -1/0. |
| 2626 | III.ImmWidth = 16; |
| 2627 | if (Opc == PPC::RLWNM || Opc == PPC::RLWNM8 || |
| 2628 | Opc == PPC::RLWNMo || Opc == PPC::RLWNM8o) |
| 2629 | III.TruncateImmTo = 5; |
| 2630 | else |
| 2631 | III.TruncateImmTo = 6; |
| 2632 | switch(Opc) { |
| 2633 | default: llvm_unreachable("Unknown opcode"); |
| 2634 | case PPC::RLWNM: III.ImmOpcode = PPC::RLWINM; break; |
| 2635 | case PPC::RLWNM8: III.ImmOpcode = PPC::RLWINM8; break; |
| 2636 | case PPC::RLWNMo: III.ImmOpcode = PPC::RLWINMo; break; |
| 2637 | case PPC::RLWNM8o: III.ImmOpcode = PPC::RLWINM8o; break; |
| 2638 | case PPC::SLW: III.ImmOpcode = PPC::RLWINM; break; |
| 2639 | case PPC::SLW8: III.ImmOpcode = PPC::RLWINM8; break; |
| 2640 | case PPC::SLWo: III.ImmOpcode = PPC::RLWINMo; break; |
| 2641 | case PPC::SLW8o: III.ImmOpcode = PPC::RLWINM8o; break; |
| 2642 | case PPC::SRW: III.ImmOpcode = PPC::RLWINM; break; |
| 2643 | case PPC::SRW8: III.ImmOpcode = PPC::RLWINM8; break; |
| 2644 | case PPC::SRWo: III.ImmOpcode = PPC::RLWINMo; break; |
| 2645 | case PPC::SRW8o: III.ImmOpcode = PPC::RLWINM8o; break; |
| 2646 | case PPC::SRAW: |
| 2647 | III.ImmWidth = 5; |
| 2648 | III.TruncateImmTo = 0; |
| 2649 | III.ImmOpcode = PPC::SRAWI; |
| 2650 | break; |
| 2651 | case PPC::SRAWo: |
| 2652 | III.ImmWidth = 5; |
| 2653 | III.TruncateImmTo = 0; |
| 2654 | III.ImmOpcode = PPC::SRAWIo; |
| 2655 | break; |
| 2656 | } |
| 2657 | break; |
| 2658 | case PPC::RLDCL: |
| 2659 | case PPC::RLDCLo: |
| 2660 | case PPC::RLDCR: |
| 2661 | case PPC::RLDCRo: |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2662 | case PPC::SLD: |
| 2663 | case PPC::SLDo: |
| 2664 | case PPC::SRD: |
| 2665 | case PPC::SRDo: |
| 2666 | case PPC::SRAD: |
| 2667 | case PPC::SRADo: |
| 2668 | III.SignedImm = false; |
| 2669 | III.ZeroIsSpecialOrig = 0; |
| 2670 | III.ZeroIsSpecialNew = 0; |
| 2671 | III.IsCommutative = false; |
| 2672 | // This isn't actually true, but the instructions ignore any of the |
| 2673 | // upper bits, so any immediate loaded with an LI is acceptable. |
Nemanja Ivanovic | 4e1f5e0 | 2017-12-29 12:22:27 +0000 | [diff] [blame] | 2674 | // This does not apply to shift right algebraic because a value |
| 2675 | // out of range will produce a -1/0. |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2676 | III.ImmWidth = 16; |
Nemanja Ivanovic | 4e1f5e0 | 2017-12-29 12:22:27 +0000 | [diff] [blame] | 2677 | if (Opc == PPC::RLDCL || Opc == PPC::RLDCLo || |
| 2678 | Opc == PPC::RLDCR || Opc == PPC::RLDCRo) |
| 2679 | III.TruncateImmTo = 6; |
| 2680 | else |
| 2681 | III.TruncateImmTo = 7; |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2682 | switch(Opc) { |
| 2683 | default: llvm_unreachable("Unknown opcode"); |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2684 | case PPC::RLDCL: III.ImmOpcode = PPC::RLDICL; break; |
| 2685 | case PPC::RLDCLo: III.ImmOpcode = PPC::RLDICLo; break; |
| 2686 | case PPC::RLDCR: III.ImmOpcode = PPC::RLDICR; break; |
| 2687 | case PPC::RLDCRo: III.ImmOpcode = PPC::RLDICRo; break; |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2688 | case PPC::SLD: III.ImmOpcode = PPC::RLDICR; break; |
| 2689 | case PPC::SLDo: III.ImmOpcode = PPC::RLDICRo; break; |
| 2690 | case PPC::SRD: III.ImmOpcode = PPC::RLDICL; break; |
| 2691 | case PPC::SRDo: III.ImmOpcode = PPC::RLDICLo; break; |
Nemanja Ivanovic | 4e1f5e0 | 2017-12-29 12:22:27 +0000 | [diff] [blame] | 2692 | case PPC::SRAD: |
| 2693 | III.ImmWidth = 6; |
| 2694 | III.TruncateImmTo = 0; |
| 2695 | III.ImmOpcode = PPC::SRADI; |
| 2696 | break; |
| 2697 | case PPC::SRADo: |
| 2698 | III.ImmWidth = 6; |
| 2699 | III.TruncateImmTo = 0; |
| 2700 | III.ImmOpcode = PPC::SRADIo; |
| 2701 | break; |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2702 | } |
| 2703 | break; |
| 2704 | // Loads and stores: |
| 2705 | case PPC::LBZX: |
| 2706 | case PPC::LBZX8: |
| 2707 | case PPC::LHZX: |
| 2708 | case PPC::LHZX8: |
| 2709 | case PPC::LHAX: |
| 2710 | case PPC::LHAX8: |
| 2711 | case PPC::LWZX: |
| 2712 | case PPC::LWZX8: |
| 2713 | case PPC::LWAX: |
| 2714 | case PPC::LDX: |
| 2715 | case PPC::LFSX: |
| 2716 | case PPC::LFDX: |
| 2717 | case PPC::STBX: |
| 2718 | case PPC::STBX8: |
| 2719 | case PPC::STHX: |
| 2720 | case PPC::STHX8: |
| 2721 | case PPC::STWX: |
| 2722 | case PPC::STWX8: |
| 2723 | case PPC::STDX: |
| 2724 | case PPC::STFSX: |
| 2725 | case PPC::STFDX: |
| 2726 | III.SignedImm = true; |
| 2727 | III.ZeroIsSpecialOrig = 1; |
| 2728 | III.ZeroIsSpecialNew = 2; |
| 2729 | III.IsCommutative = true; |
| 2730 | III.ImmOpNo = 1; |
| 2731 | III.ConstantOpNo = 2; |
| 2732 | switch(Opc) { |
| 2733 | default: llvm_unreachable("Unknown opcode"); |
| 2734 | case PPC::LBZX: III.ImmOpcode = PPC::LBZ; break; |
| 2735 | case PPC::LBZX8: III.ImmOpcode = PPC::LBZ8; break; |
| 2736 | case PPC::LHZX: III.ImmOpcode = PPC::LHZ; break; |
| 2737 | case PPC::LHZX8: III.ImmOpcode = PPC::LHZ8; break; |
| 2738 | case PPC::LHAX: III.ImmOpcode = PPC::LHA; break; |
| 2739 | case PPC::LHAX8: III.ImmOpcode = PPC::LHA8; break; |
| 2740 | case PPC::LWZX: III.ImmOpcode = PPC::LWZ; break; |
| 2741 | case PPC::LWZX8: III.ImmOpcode = PPC::LWZ8; break; |
| 2742 | case PPC::LWAX: |
| 2743 | III.ImmOpcode = PPC::LWA; |
| 2744 | III.ImmMustBeMultipleOf = 4; |
| 2745 | break; |
| 2746 | case PPC::LDX: III.ImmOpcode = PPC::LD; III.ImmMustBeMultipleOf = 4; break; |
| 2747 | case PPC::LFSX: III.ImmOpcode = PPC::LFS; break; |
| 2748 | case PPC::LFDX: III.ImmOpcode = PPC::LFD; break; |
| 2749 | case PPC::STBX: III.ImmOpcode = PPC::STB; break; |
| 2750 | case PPC::STBX8: III.ImmOpcode = PPC::STB8; break; |
| 2751 | case PPC::STHX: III.ImmOpcode = PPC::STH; break; |
| 2752 | case PPC::STHX8: III.ImmOpcode = PPC::STH8; break; |
| 2753 | case PPC::STWX: III.ImmOpcode = PPC::STW; break; |
| 2754 | case PPC::STWX8: III.ImmOpcode = PPC::STW8; break; |
| 2755 | case PPC::STDX: |
| 2756 | III.ImmOpcode = PPC::STD; |
| 2757 | III.ImmMustBeMultipleOf = 4; |
| 2758 | break; |
| 2759 | case PPC::STFSX: III.ImmOpcode = PPC::STFS; break; |
| 2760 | case PPC::STFDX: III.ImmOpcode = PPC::STFD; break; |
| 2761 | } |
| 2762 | break; |
| 2763 | case PPC::LBZUX: |
| 2764 | case PPC::LBZUX8: |
| 2765 | case PPC::LHZUX: |
| 2766 | case PPC::LHZUX8: |
| 2767 | case PPC::LHAUX: |
| 2768 | case PPC::LHAUX8: |
| 2769 | case PPC::LWZUX: |
| 2770 | case PPC::LWZUX8: |
| 2771 | case PPC::LDUX: |
| 2772 | case PPC::LFSUX: |
| 2773 | case PPC::LFDUX: |
| 2774 | case PPC::STBUX: |
| 2775 | case PPC::STBUX8: |
| 2776 | case PPC::STHUX: |
| 2777 | case PPC::STHUX8: |
| 2778 | case PPC::STWUX: |
| 2779 | case PPC::STWUX8: |
| 2780 | case PPC::STDUX: |
| 2781 | case PPC::STFSUX: |
| 2782 | case PPC::STFDUX: |
| 2783 | III.SignedImm = true; |
| 2784 | III.ZeroIsSpecialOrig = 2; |
| 2785 | III.ZeroIsSpecialNew = 3; |
| 2786 | III.IsCommutative = false; |
| 2787 | III.ImmOpNo = 2; |
| 2788 | III.ConstantOpNo = 3; |
| 2789 | switch(Opc) { |
| 2790 | default: llvm_unreachable("Unknown opcode"); |
| 2791 | case PPC::LBZUX: III.ImmOpcode = PPC::LBZU; break; |
| 2792 | case PPC::LBZUX8: III.ImmOpcode = PPC::LBZU8; break; |
| 2793 | case PPC::LHZUX: III.ImmOpcode = PPC::LHZU; break; |
| 2794 | case PPC::LHZUX8: III.ImmOpcode = PPC::LHZU8; break; |
| 2795 | case PPC::LHAUX: III.ImmOpcode = PPC::LHAU; break; |
| 2796 | case PPC::LHAUX8: III.ImmOpcode = PPC::LHAU8; break; |
| 2797 | case PPC::LWZUX: III.ImmOpcode = PPC::LWZU; break; |
| 2798 | case PPC::LWZUX8: III.ImmOpcode = PPC::LWZU8; break; |
| 2799 | case PPC::LDUX: |
| 2800 | III.ImmOpcode = PPC::LDU; |
| 2801 | III.ImmMustBeMultipleOf = 4; |
| 2802 | break; |
| 2803 | case PPC::LFSUX: III.ImmOpcode = PPC::LFSU; break; |
| 2804 | case PPC::LFDUX: III.ImmOpcode = PPC::LFDU; break; |
| 2805 | case PPC::STBUX: III.ImmOpcode = PPC::STBU; break; |
| 2806 | case PPC::STBUX8: III.ImmOpcode = PPC::STBU8; break; |
| 2807 | case PPC::STHUX: III.ImmOpcode = PPC::STHU; break; |
| 2808 | case PPC::STHUX8: III.ImmOpcode = PPC::STHU8; break; |
| 2809 | case PPC::STWUX: III.ImmOpcode = PPC::STWU; break; |
| 2810 | case PPC::STWUX8: III.ImmOpcode = PPC::STWU8; break; |
| 2811 | case PPC::STDUX: |
| 2812 | III.ImmOpcode = PPC::STDU; |
| 2813 | III.ImmMustBeMultipleOf = 4; |
| 2814 | break; |
| 2815 | case PPC::STFSUX: III.ImmOpcode = PPC::STFSU; break; |
| 2816 | case PPC::STFDUX: III.ImmOpcode = PPC::STFDU; break; |
| 2817 | } |
| 2818 | break; |
| 2819 | // Power9 only. |
| 2820 | case PPC::LXVX: |
| 2821 | case PPC::LXSSPX: |
| 2822 | case PPC::LXSDX: |
| 2823 | case PPC::STXVX: |
| 2824 | case PPC::STXSSPX: |
| 2825 | case PPC::STXSDX: |
| 2826 | if (!Subtarget.hasP9Vector()) |
| 2827 | return false; |
| 2828 | III.SignedImm = true; |
| 2829 | III.ZeroIsSpecialOrig = 1; |
| 2830 | III.ZeroIsSpecialNew = 2; |
| 2831 | III.IsCommutative = true; |
| 2832 | III.ImmOpNo = 1; |
| 2833 | III.ConstantOpNo = 2; |
| 2834 | switch(Opc) { |
| 2835 | default: llvm_unreachable("Unknown opcode"); |
| 2836 | case PPC::LXVX: |
| 2837 | III.ImmOpcode = PPC::LXV; |
| 2838 | III.ImmMustBeMultipleOf = 16; |
| 2839 | break; |
| 2840 | case PPC::LXSSPX: |
| 2841 | III.ImmOpcode = PPC::LXSSP; |
| 2842 | III.ImmMustBeMultipleOf = 4; |
| 2843 | break; |
| 2844 | case PPC::LXSDX: |
| 2845 | III.ImmOpcode = PPC::LXSD; |
| 2846 | III.ImmMustBeMultipleOf = 4; |
| 2847 | break; |
| 2848 | case PPC::STXVX: |
| 2849 | III.ImmOpcode = PPC::STXV; |
| 2850 | III.ImmMustBeMultipleOf = 16; |
| 2851 | break; |
| 2852 | case PPC::STXSSPX: |
| 2853 | III.ImmOpcode = PPC::STXSSP; |
| 2854 | III.ImmMustBeMultipleOf = 4; |
| 2855 | break; |
| 2856 | case PPC::STXSDX: |
| 2857 | III.ImmOpcode = PPC::STXSD; |
| 2858 | III.ImmMustBeMultipleOf = 4; |
| 2859 | break; |
| 2860 | } |
| 2861 | break; |
| 2862 | } |
| 2863 | return true; |
| 2864 | } |
| 2865 | |
| 2866 | // Utility function for swaping two arbitrary operands of an instruction. |
| 2867 | static void swapMIOperands(MachineInstr &MI, unsigned Op1, unsigned Op2) { |
| 2868 | assert(Op1 != Op2 && "Cannot swap operand with itself."); |
| 2869 | |
| 2870 | unsigned MaxOp = std::max(Op1, Op2); |
| 2871 | unsigned MinOp = std::min(Op1, Op2); |
| 2872 | MachineOperand MOp1 = MI.getOperand(MinOp); |
| 2873 | MachineOperand MOp2 = MI.getOperand(MaxOp); |
| 2874 | MI.RemoveOperand(std::max(Op1, Op2)); |
| 2875 | MI.RemoveOperand(std::min(Op1, Op2)); |
| 2876 | |
| 2877 | // If the operands we are swapping are the two at the end (the common case) |
| 2878 | // we can just remove both and add them in the opposite order. |
| 2879 | if (MaxOp - MinOp == 1 && MI.getNumOperands() == MinOp) { |
| 2880 | MI.addOperand(MOp2); |
| 2881 | MI.addOperand(MOp1); |
| 2882 | } else { |
| 2883 | // Store all operands in a temporary vector, remove them and re-add in the |
| 2884 | // right order. |
| 2885 | SmallVector<MachineOperand, 2> MOps; |
| 2886 | unsigned TotalOps = MI.getNumOperands() + 2; // We've already removed 2 ops. |
| 2887 | for (unsigned i = MI.getNumOperands() - 1; i >= MinOp; i--) { |
| 2888 | MOps.push_back(MI.getOperand(i)); |
| 2889 | MI.RemoveOperand(i); |
| 2890 | } |
| 2891 | // MOp2 needs to be added next. |
| 2892 | MI.addOperand(MOp2); |
| 2893 | // Now add the rest. |
| 2894 | for (unsigned i = MI.getNumOperands(); i < TotalOps; i++) { |
| 2895 | if (i == MaxOp) |
| 2896 | MI.addOperand(MOp1); |
| 2897 | else { |
| 2898 | MI.addOperand(MOps.back()); |
| 2899 | MOps.pop_back(); |
| 2900 | } |
| 2901 | } |
| 2902 | } |
| 2903 | } |
| 2904 | |
| 2905 | bool PPCInstrInfo::transformToImmForm(MachineInstr &MI, const ImmInstrInfo &III, |
| 2906 | unsigned ConstantOpNo, |
| 2907 | int64_t Imm) const { |
| 2908 | MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); |
| 2909 | bool PostRA = !MRI.isSSA(); |
| 2910 | // Exit early if we can't convert this. |
| 2911 | if ((ConstantOpNo != III.ConstantOpNo) && !III.IsCommutative) |
| 2912 | return false; |
| 2913 | if (Imm % III.ImmMustBeMultipleOf) |
| 2914 | return false; |
Nemanja Ivanovic | 4e1f5e0 | 2017-12-29 12:22:27 +0000 | [diff] [blame] | 2915 | if (III.TruncateImmTo) |
| 2916 | Imm &= ((1 << III.TruncateImmTo) - 1); |
Nemanja Ivanovic | 6995e5d | 2017-12-15 07:27:53 +0000 | [diff] [blame] | 2917 | if (III.SignedImm) { |
| 2918 | APInt ActualValue(64, Imm, true); |
| 2919 | if (!ActualValue.isSignedIntN(III.ImmWidth)) |
| 2920 | return false; |
| 2921 | } else { |
| 2922 | uint64_t UnsignedMax = (1 << III.ImmWidth) - 1; |
| 2923 | if ((uint64_t)Imm > UnsignedMax) |
| 2924 | return false; |
| 2925 | } |
| 2926 | |
| 2927 | // If we're post-RA, the instructions don't agree on whether register zero is |
| 2928 | // special, we can transform this as long as the register operand that will |
| 2929 | // end up in the location where zero is special isn't R0. |
| 2930 | if (PostRA && III.ZeroIsSpecialOrig != III.ZeroIsSpecialNew) { |
| 2931 | unsigned PosForOrigZero = III.ZeroIsSpecialOrig ? III.ZeroIsSpecialOrig : |
| 2932 | III.ZeroIsSpecialNew + 1; |
| 2933 | unsigned OrigZeroReg = MI.getOperand(PosForOrigZero).getReg(); |
| 2934 | unsigned NewZeroReg = MI.getOperand(III.ZeroIsSpecialNew).getReg(); |
| 2935 | // If R0 is in the operand where zero is special for the new instruction, |
| 2936 | // it is unsafe to transform if the constant operand isn't that operand. |
| 2937 | if ((NewZeroReg == PPC::R0 || NewZeroReg == PPC::X0) && |
| 2938 | ConstantOpNo != III.ZeroIsSpecialNew) |
| 2939 | return false; |
| 2940 | if ((OrigZeroReg == PPC::R0 || OrigZeroReg == PPC::X0) && |
| 2941 | ConstantOpNo != PosForOrigZero) |
| 2942 | return false; |
| 2943 | } |
| 2944 | |
| 2945 | unsigned Opc = MI.getOpcode(); |
| 2946 | bool SpecialShift32 = |
| 2947 | Opc == PPC::SLW || Opc == PPC::SLWo || Opc == PPC::SRW || Opc == PPC::SRWo; |
| 2948 | bool SpecialShift64 = |
| 2949 | Opc == PPC::SLD || Opc == PPC::SLDo || Opc == PPC::SRD || Opc == PPC::SRDo; |
| 2950 | bool SetCR = Opc == PPC::SLWo || Opc == PPC::SRWo || |
| 2951 | Opc == PPC::SLDo || Opc == PPC::SRDo; |
| 2952 | bool RightShift = |
| 2953 | Opc == PPC::SRW || Opc == PPC::SRWo || Opc == PPC::SRD || Opc == PPC::SRDo; |
| 2954 | |
| 2955 | MI.setDesc(get(III.ImmOpcode)); |
| 2956 | if (ConstantOpNo == III.ConstantOpNo) { |
| 2957 | // Converting shifts to immediate form is a bit tricky since they may do |
| 2958 | // one of three things: |
| 2959 | // 1. If the shift amount is between OpSize and 2*OpSize, the result is zero |
| 2960 | // 2. If the shift amount is zero, the result is unchanged (save for maybe |
| 2961 | // setting CR0) |
| 2962 | // 3. If the shift amount is in [1, OpSize), it's just a shift |
| 2963 | if (SpecialShift32 || SpecialShift64) { |
| 2964 | LoadImmediateInfo LII; |
| 2965 | LII.Imm = 0; |
| 2966 | LII.SetCR = SetCR; |
| 2967 | LII.Is64Bit = SpecialShift64; |
| 2968 | uint64_t ShAmt = Imm & (SpecialShift32 ? 0x1F : 0x3F); |
| 2969 | if (Imm & (SpecialShift32 ? 0x20 : 0x40)) |
| 2970 | replaceInstrWithLI(MI, LII); |
| 2971 | // Shifts by zero don't change the value. If we don't need to set CR0, |
| 2972 | // just convert this to a COPY. Can't do this post-RA since we've already |
| 2973 | // cleaned up the copies. |
| 2974 | else if (!SetCR && ShAmt == 0 && !PostRA) { |
| 2975 | MI.RemoveOperand(2); |
| 2976 | MI.setDesc(get(PPC::COPY)); |
| 2977 | } else { |
| 2978 | // The 32 bit and 64 bit instructions are quite different. |
| 2979 | if (SpecialShift32) { |
| 2980 | // Left shifts use (N, 0, 31-N), right shifts use (32-N, N, 31). |
| 2981 | uint64_t SH = RightShift ? 32 - ShAmt : ShAmt; |
| 2982 | uint64_t MB = RightShift ? ShAmt : 0; |
| 2983 | uint64_t ME = RightShift ? 31 : 31 - ShAmt; |
| 2984 | MI.getOperand(III.ConstantOpNo).ChangeToImmediate(SH); |
| 2985 | MachineInstrBuilder(*MI.getParent()->getParent(), MI).addImm(MB) |
| 2986 | .addImm(ME); |
| 2987 | } else { |
| 2988 | // Left shifts use (N, 63-N), right shifts use (64-N, N). |
| 2989 | uint64_t SH = RightShift ? 64 - ShAmt : ShAmt; |
| 2990 | uint64_t ME = RightShift ? ShAmt : 63 - ShAmt; |
| 2991 | MI.getOperand(III.ConstantOpNo).ChangeToImmediate(SH); |
| 2992 | MachineInstrBuilder(*MI.getParent()->getParent(), MI).addImm(ME); |
| 2993 | } |
| 2994 | } |
| 2995 | } else |
| 2996 | MI.getOperand(ConstantOpNo).ChangeToImmediate(Imm); |
| 2997 | } |
| 2998 | // Convert commutative instructions (switch the operands and convert the |
| 2999 | // desired one to an immediate. |
| 3000 | else if (III.IsCommutative) { |
| 3001 | MI.getOperand(ConstantOpNo).ChangeToImmediate(Imm); |
| 3002 | swapMIOperands(MI, ConstantOpNo, III.ConstantOpNo); |
| 3003 | } else |
| 3004 | llvm_unreachable("Should have exited early!"); |
| 3005 | |
| 3006 | // For instructions for which the constant register replaces a different |
| 3007 | // operand than where the immediate goes, we need to swap them. |
| 3008 | if (III.ConstantOpNo != III.ImmOpNo) |
| 3009 | swapMIOperands(MI, III.ConstantOpNo, III.ImmOpNo); |
| 3010 | |
| 3011 | // If the R0/X0 register is special for the original instruction and not for |
| 3012 | // the new instruction (or vice versa), we need to fix up the register class. |
| 3013 | if (!PostRA && III.ZeroIsSpecialOrig != III.ZeroIsSpecialNew) { |
| 3014 | if (!III.ZeroIsSpecialOrig) { |
| 3015 | unsigned RegToModify = MI.getOperand(III.ZeroIsSpecialNew).getReg(); |
| 3016 | const TargetRegisterClass *NewRC = |
| 3017 | MRI.getRegClass(RegToModify)->hasSuperClassEq(&PPC::GPRCRegClass) ? |
| 3018 | &PPC::GPRC_and_GPRC_NOR0RegClass : &PPC::G8RC_and_G8RC_NOX0RegClass; |
| 3019 | MRI.setRegClass(RegToModify, NewRC); |
| 3020 | } |
| 3021 | } |
| 3022 | return true; |
| 3023 | } |
| 3024 | |
Nemanja Ivanovic | 11049f8 | 2016-10-04 06:59:23 +0000 | [diff] [blame] | 3025 | const TargetRegisterClass * |
| 3026 | PPCInstrInfo::updatedRC(const TargetRegisterClass *RC) const { |
| 3027 | if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass) |
| 3028 | return &PPC::VSRCRegClass; |
| 3029 | return RC; |
| 3030 | } |
Nemanja Ivanovic | e597bd8 | 2017-05-31 05:40:25 +0000 | [diff] [blame] | 3031 | |
| 3032 | int PPCInstrInfo::getRecordFormOpcode(unsigned Opcode) { |
| 3033 | return PPC::getRecordFormOpcode(Opcode); |
| 3034 | } |
Hiroshi Inoue | e3a3e3c | 2017-10-16 04:12:57 +0000 | [diff] [blame] | 3035 | |
| 3036 | // This function returns true if the machine instruction |
| 3037 | // always outputs a value by sign-extending a 32 bit value, |
| 3038 | // i.e. 0 to 31-th bits are same as 32-th bit. |
| 3039 | static bool isSignExtendingOp(const MachineInstr &MI) { |
| 3040 | int Opcode = MI.getOpcode(); |
| 3041 | if (Opcode == PPC::LI || Opcode == PPC::LI8 || |
| 3042 | Opcode == PPC::LIS || Opcode == PPC::LIS8 || |
| 3043 | Opcode == PPC::SRAW || Opcode == PPC::SRAWo || |
| 3044 | Opcode == PPC::SRAWI || Opcode == PPC::SRAWIo || |
| 3045 | Opcode == PPC::LWA || Opcode == PPC::LWAX || |
| 3046 | Opcode == PPC::LWA_32 || Opcode == PPC::LWAX_32 || |
| 3047 | Opcode == PPC::LHA || Opcode == PPC::LHAX || |
| 3048 | Opcode == PPC::LHA8 || Opcode == PPC::LHAX8 || |
| 3049 | Opcode == PPC::LBZ || Opcode == PPC::LBZX || |
| 3050 | Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 || |
| 3051 | Opcode == PPC::LBZU || Opcode == PPC::LBZUX || |
| 3052 | Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 || |
| 3053 | Opcode == PPC::LHZ || Opcode == PPC::LHZX || |
| 3054 | Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 || |
| 3055 | Opcode == PPC::LHZU || Opcode == PPC::LHZUX || |
| 3056 | Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8 || |
| 3057 | Opcode == PPC::EXTSB || Opcode == PPC::EXTSBo || |
| 3058 | Opcode == PPC::EXTSH || Opcode == PPC::EXTSHo || |
| 3059 | Opcode == PPC::EXTSB8 || Opcode == PPC::EXTSH8 || |
| 3060 | Opcode == PPC::EXTSW || Opcode == PPC::EXTSWo || |
| 3061 | Opcode == PPC::EXTSH8_32_64 || Opcode == PPC::EXTSW_32_64 || |
| 3062 | Opcode == PPC::EXTSB8_32_64) |
| 3063 | return true; |
| 3064 | |
| 3065 | if (Opcode == PPC::RLDICL && MI.getOperand(3).getImm() >= 33) |
| 3066 | return true; |
| 3067 | |
| 3068 | if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINMo || |
| 3069 | Opcode == PPC::RLWNM || Opcode == PPC::RLWNMo) && |
| 3070 | MI.getOperand(3).getImm() > 0 && |
| 3071 | MI.getOperand(3).getImm() <= MI.getOperand(4).getImm()) |
| 3072 | return true; |
| 3073 | |
| 3074 | return false; |
| 3075 | } |
| 3076 | |
| 3077 | // This function returns true if the machine instruction |
| 3078 | // always outputs zeros in higher 32 bits. |
| 3079 | static bool isZeroExtendingOp(const MachineInstr &MI) { |
| 3080 | int Opcode = MI.getOpcode(); |
| 3081 | // The 16-bit immediate is sign-extended in li/lis. |
| 3082 | // If the most significant bit is zero, all higher bits are zero. |
| 3083 | if (Opcode == PPC::LI || Opcode == PPC::LI8 || |
| 3084 | Opcode == PPC::LIS || Opcode == PPC::LIS8) { |
| 3085 | int64_t Imm = MI.getOperand(1).getImm(); |
| 3086 | if (((uint64_t)Imm & ~0x7FFFuLL) == 0) |
| 3087 | return true; |
| 3088 | } |
| 3089 | |
| 3090 | // We have some variations of rotate-and-mask instructions |
| 3091 | // that clear higher 32-bits. |
| 3092 | if ((Opcode == PPC::RLDICL || Opcode == PPC::RLDICLo || |
| 3093 | Opcode == PPC::RLDCL || Opcode == PPC::RLDCLo || |
| 3094 | Opcode == PPC::RLDICL_32_64) && |
| 3095 | MI.getOperand(3).getImm() >= 32) |
| 3096 | return true; |
| 3097 | |
| 3098 | if ((Opcode == PPC::RLDIC || Opcode == PPC::RLDICo) && |
| 3099 | MI.getOperand(3).getImm() >= 32 && |
| 3100 | MI.getOperand(3).getImm() <= 63 - MI.getOperand(2).getImm()) |
| 3101 | return true; |
| 3102 | |
| 3103 | if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINMo || |
| 3104 | Opcode == PPC::RLWNM || Opcode == PPC::RLWNMo || |
| 3105 | Opcode == PPC::RLWINM8 || Opcode == PPC::RLWNM8) && |
| 3106 | MI.getOperand(3).getImm() <= MI.getOperand(4).getImm()) |
| 3107 | return true; |
| 3108 | |
| 3109 | // There are other instructions that clear higher 32-bits. |
| 3110 | if (Opcode == PPC::CNTLZW || Opcode == PPC::CNTLZWo || |
| 3111 | Opcode == PPC::CNTTZW || Opcode == PPC::CNTTZWo || |
| 3112 | Opcode == PPC::CNTLZW8 || Opcode == PPC::CNTTZW8 || |
| 3113 | Opcode == PPC::CNTLZD || Opcode == PPC::CNTLZDo || |
| 3114 | Opcode == PPC::CNTTZD || Opcode == PPC::CNTTZDo || |
| 3115 | Opcode == PPC::POPCNTD || Opcode == PPC::POPCNTW || |
| 3116 | Opcode == PPC::SLW || Opcode == PPC::SLWo || |
| 3117 | Opcode == PPC::SRW || Opcode == PPC::SRWo || |
| 3118 | Opcode == PPC::SLW8 || Opcode == PPC::SRW8 || |
| 3119 | Opcode == PPC::SLWI || Opcode == PPC::SLWIo || |
| 3120 | Opcode == PPC::SRWI || Opcode == PPC::SRWIo || |
| 3121 | Opcode == PPC::LWZ || Opcode == PPC::LWZX || |
| 3122 | Opcode == PPC::LWZU || Opcode == PPC::LWZUX || |
| 3123 | Opcode == PPC::LWBRX || Opcode == PPC::LHBRX || |
| 3124 | Opcode == PPC::LHZ || Opcode == PPC::LHZX || |
| 3125 | Opcode == PPC::LHZU || Opcode == PPC::LHZUX || |
| 3126 | Opcode == PPC::LBZ || Opcode == PPC::LBZX || |
| 3127 | Opcode == PPC::LBZU || Opcode == PPC::LBZUX || |
| 3128 | Opcode == PPC::LWZ8 || Opcode == PPC::LWZX8 || |
| 3129 | Opcode == PPC::LWZU8 || Opcode == PPC::LWZUX8 || |
| 3130 | Opcode == PPC::LWBRX8 || Opcode == PPC::LHBRX8 || |
| 3131 | Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 || |
| 3132 | Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8 || |
| 3133 | Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 || |
| 3134 | Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 || |
| 3135 | Opcode == PPC::ANDIo || Opcode == PPC::ANDISo || |
| 3136 | Opcode == PPC::ROTRWI || Opcode == PPC::ROTRWIo || |
| 3137 | Opcode == PPC::EXTLWI || Opcode == PPC::EXTLWIo || |
| 3138 | Opcode == PPC::MFVSRWZ) |
| 3139 | return true; |
| 3140 | |
| 3141 | return false; |
| 3142 | } |
| 3143 | |
Zaara Syeda | f94d58d | 2017-11-27 20:26:36 +0000 | [diff] [blame] | 3144 | // This function returns true if the input MachineInstr is a TOC save |
| 3145 | // instruction. |
| 3146 | bool PPCInstrInfo::isTOCSaveMI(const MachineInstr &MI) const { |
| 3147 | if (!MI.getOperand(1).isImm() || !MI.getOperand(2).isReg()) |
| 3148 | return false; |
| 3149 | unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset(); |
| 3150 | unsigned StackOffset = MI.getOperand(1).getImm(); |
| 3151 | unsigned StackReg = MI.getOperand(2).getReg(); |
| 3152 | if (StackReg == PPC::X1 && StackOffset == TOCSaveOffset) |
| 3153 | return true; |
| 3154 | |
| 3155 | return false; |
| 3156 | } |
| 3157 | |
Hiroshi Inoue | e3a3e3c | 2017-10-16 04:12:57 +0000 | [diff] [blame] | 3158 | // We limit the max depth to track incoming values of PHIs or binary ops |
| 3159 | // (e.g. AND) to avoid exsessive cost. |
| 3160 | const unsigned MAX_DEPTH = 1; |
| 3161 | |
| 3162 | bool |
| 3163 | PPCInstrInfo::isSignOrZeroExtended(const MachineInstr &MI, bool SignExt, |
| 3164 | const unsigned Depth) const { |
| 3165 | const MachineFunction *MF = MI.getParent()->getParent(); |
| 3166 | const MachineRegisterInfo *MRI = &MF->getRegInfo(); |
| 3167 | |
Hiroshi Inoue | 5388e66 | 2017-10-18 10:31:19 +0000 | [diff] [blame] | 3168 | // If we know this instruction returns sign- or zero-extended result, |
| 3169 | // return true. |
| 3170 | if (SignExt ? isSignExtendingOp(MI): |
| 3171 | isZeroExtendingOp(MI)) |
| 3172 | return true; |
| 3173 | |
Hiroshi Inoue | e3a3e3c | 2017-10-16 04:12:57 +0000 | [diff] [blame] | 3174 | switch (MI.getOpcode()) { |
| 3175 | case PPC::COPY: { |
| 3176 | unsigned SrcReg = MI.getOperand(1).getReg(); |
| 3177 | |
| 3178 | // In both ELFv1 and v2 ABI, method parameters and the return value |
| 3179 | // are sign- or zero-extended. |
| 3180 | if (MF->getSubtarget<PPCSubtarget>().isSVR4ABI()) { |
| 3181 | const PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>(); |
| 3182 | // We check the ZExt/SExt flags for a method parameter. |
| 3183 | if (MI.getParent()->getBasicBlock() == |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 3184 | &MF->getFunction().getEntryBlock()) { |
Hiroshi Inoue | e3a3e3c | 2017-10-16 04:12:57 +0000 | [diff] [blame] | 3185 | unsigned VReg = MI.getOperand(0).getReg(); |
| 3186 | if (MF->getRegInfo().isLiveIn(VReg)) |
| 3187 | return SignExt ? FuncInfo->isLiveInSExt(VReg) : |
| 3188 | FuncInfo->isLiveInZExt(VReg); |
| 3189 | } |
| 3190 | |
| 3191 | // For a method return value, we check the ZExt/SExt flags in attribute. |
| 3192 | // We assume the following code sequence for method call. |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 3193 | // ADJCALLSTACKDOWN 32, implicit dead %r1, implicit %r1 |
Francis Visoiu Mistrih | 5df3bbf | 2017-12-14 10:03:09 +0000 | [diff] [blame] | 3194 | // BL8_NOP @func,... |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 3195 | // ADJCALLSTACKUP 32, 0, implicit dead %r1, implicit %r1 |
| 3196 | // %5 = COPY %x3; G8RC:%5 |
Hiroshi Inoue | e3a3e3c | 2017-10-16 04:12:57 +0000 | [diff] [blame] | 3197 | if (SrcReg == PPC::X3) { |
| 3198 | const MachineBasicBlock *MBB = MI.getParent(); |
| 3199 | MachineBasicBlock::const_instr_iterator II = |
| 3200 | MachineBasicBlock::const_instr_iterator(&MI); |
| 3201 | if (II != MBB->instr_begin() && |
| 3202 | (--II)->getOpcode() == PPC::ADJCALLSTACKUP) { |
| 3203 | const MachineInstr &CallMI = *(--II); |
| 3204 | if (CallMI.isCall() && CallMI.getOperand(0).isGlobal()) { |
| 3205 | const Function *CalleeFn = |
| 3206 | dyn_cast<Function>(CallMI.getOperand(0).getGlobal()); |
Hiroshi Inoue | a7eb78b | 2017-10-16 12:11:15 +0000 | [diff] [blame] | 3207 | if (!CalleeFn) |
| 3208 | return false; |
Hiroshi Inoue | e3a3e3c | 2017-10-16 04:12:57 +0000 | [diff] [blame] | 3209 | const IntegerType *IntTy = |
| 3210 | dyn_cast<IntegerType>(CalleeFn->getReturnType()); |
| 3211 | const AttributeSet &Attrs = |
| 3212 | CalleeFn->getAttributes().getRetAttributes(); |
| 3213 | if (IntTy && IntTy->getBitWidth() <= 32) |
| 3214 | return Attrs.hasAttribute(SignExt ? Attribute::SExt : |
| 3215 | Attribute::ZExt); |
| 3216 | } |
| 3217 | } |
| 3218 | } |
| 3219 | } |
| 3220 | |
| 3221 | // If this is a copy from another register, we recursively check source. |
| 3222 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) |
| 3223 | return false; |
| 3224 | const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); |
| 3225 | if (SrcMI != NULL) |
| 3226 | return isSignOrZeroExtended(*SrcMI, SignExt, Depth); |
| 3227 | |
| 3228 | return false; |
| 3229 | } |
| 3230 | |
| 3231 | case PPC::ANDIo: |
| 3232 | case PPC::ANDISo: |
| 3233 | case PPC::ORI: |
| 3234 | case PPC::ORIS: |
| 3235 | case PPC::XORI: |
| 3236 | case PPC::XORIS: |
| 3237 | case PPC::ANDIo8: |
| 3238 | case PPC::ANDISo8: |
| 3239 | case PPC::ORI8: |
| 3240 | case PPC::ORIS8: |
| 3241 | case PPC::XORI8: |
| 3242 | case PPC::XORIS8: { |
| 3243 | // logical operation with 16-bit immediate does not change the upper bits. |
| 3244 | // So, we track the operand register as we do for register copy. |
| 3245 | unsigned SrcReg = MI.getOperand(1).getReg(); |
| 3246 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) |
| 3247 | return false; |
| 3248 | const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); |
| 3249 | if (SrcMI != NULL) |
| 3250 | return isSignOrZeroExtended(*SrcMI, SignExt, Depth); |
| 3251 | |
| 3252 | return false; |
| 3253 | } |
| 3254 | |
| 3255 | // If all incoming values are sign-/zero-extended, |
Sean Fertile | aab3ef7 | 2017-11-29 04:09:29 +0000 | [diff] [blame] | 3256 | // the output of OR, ISEL or PHI is also sign-/zero-extended. |
Hiroshi Inoue | e3a3e3c | 2017-10-16 04:12:57 +0000 | [diff] [blame] | 3257 | case PPC::OR: |
| 3258 | case PPC::OR8: |
| 3259 | case PPC::ISEL: |
| 3260 | case PPC::PHI: { |
| 3261 | if (Depth >= MAX_DEPTH) |
| 3262 | return false; |
| 3263 | |
| 3264 | // The input registers for PHI are operand 1, 3, ... |
| 3265 | // The input registers for others are operand 1 and 2. |
| 3266 | unsigned E = 3, D = 1; |
| 3267 | if (MI.getOpcode() == PPC::PHI) { |
| 3268 | E = MI.getNumOperands(); |
| 3269 | D = 2; |
| 3270 | } |
| 3271 | |
| 3272 | for (unsigned I = 1; I != E; I += D) { |
| 3273 | if (MI.getOperand(I).isReg()) { |
| 3274 | unsigned SrcReg = MI.getOperand(I).getReg(); |
| 3275 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) |
| 3276 | return false; |
| 3277 | const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); |
| 3278 | if (SrcMI == NULL || !isSignOrZeroExtended(*SrcMI, SignExt, Depth+1)) |
| 3279 | return false; |
| 3280 | } |
| 3281 | else |
| 3282 | return false; |
| 3283 | } |
| 3284 | return true; |
| 3285 | } |
| 3286 | |
Sean Fertile | aab3ef7 | 2017-11-29 04:09:29 +0000 | [diff] [blame] | 3287 | // If at least one of the incoming values of an AND is zero extended |
| 3288 | // then the output is also zero-extended. If both of the incoming values |
| 3289 | // are sign-extended then the output is also sign extended. |
| 3290 | case PPC::AND: |
| 3291 | case PPC::AND8: { |
| 3292 | if (Depth >= MAX_DEPTH) |
| 3293 | return false; |
| 3294 | |
| 3295 | assert(MI.getOperand(1).isReg() && MI.getOperand(2).isReg()); |
| 3296 | |
| 3297 | unsigned SrcReg1 = MI.getOperand(1).getReg(); |
| 3298 | unsigned SrcReg2 = MI.getOperand(2).getReg(); |
| 3299 | |
| 3300 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg1) || |
| 3301 | !TargetRegisterInfo::isVirtualRegister(SrcReg2)) |
| 3302 | return false; |
| 3303 | |
| 3304 | const MachineInstr *MISrc1 = MRI->getVRegDef(SrcReg1); |
| 3305 | const MachineInstr *MISrc2 = MRI->getVRegDef(SrcReg2); |
| 3306 | if (!MISrc1 || !MISrc2) |
| 3307 | return false; |
| 3308 | |
| 3309 | if(SignExt) |
| 3310 | return isSignOrZeroExtended(*MISrc1, SignExt, Depth+1) && |
| 3311 | isSignOrZeroExtended(*MISrc2, SignExt, Depth+1); |
| 3312 | else |
| 3313 | return isSignOrZeroExtended(*MISrc1, SignExt, Depth+1) || |
| 3314 | isSignOrZeroExtended(*MISrc2, SignExt, Depth+1); |
| 3315 | } |
| 3316 | |
Hiroshi Inoue | e3a3e3c | 2017-10-16 04:12:57 +0000 | [diff] [blame] | 3317 | default: |
Hiroshi Inoue | 5388e66 | 2017-10-18 10:31:19 +0000 | [diff] [blame] | 3318 | break; |
Hiroshi Inoue | e3a3e3c | 2017-10-16 04:12:57 +0000 | [diff] [blame] | 3319 | } |
| 3320 | return false; |
| 3321 | } |