blob: 92fb22694273c1ae0d07d116dba824c06f47a5b8 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- PPCInstrInfo.cpp - PowerPC Instruction Information ----------------===//
Misha Brukmanb4402432005-04-21 23:30:14 +00002//
Misha Brukman116f9272004-08-17 04:55:41 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb4402432005-04-21 23:30:14 +00007//
Misha Brukman116f9272004-08-17 04:55:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains the PowerPC implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner6f3b9542005-10-14 23:59:06 +000014#include "PPCInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "MCTargetDesc/PPCPredicates.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000016#include "PPC.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "PPCHazardRecognizers.h"
Owen Andersoneee14602008-01-01 21:11:32 +000018#include "PPCInstrBuilder.h"
Bill Wendling632ea652008-03-03 22:19:16 +000019#include "PPCMachineFunctionInfo.h"
Chris Lattner49cadab2006-06-17 00:01:04 +000020#include "PPCTargetMachine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000022#include "llvm/ADT/Statistic.h"
Hal Finkel174e5902014-03-25 23:29:21 +000023#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Hal Finkelb5aa7e52013-04-08 16:24:03 +000025#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman116f9272004-08-17 04:55:41 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000027#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesenddbf7a82010-02-26 21:09:24 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Hal Finkel9f9f8922012-04-01 19:22:40 +000029#include "llvm/CodeGen/PseudoSourceValue.h"
Eric Christopher1dcea732014-06-12 21:48:52 +000030#include "llvm/CodeGen/ScheduleDAG.h"
Hal Finkel174e5902014-03-25 23:29:21 +000031#include "llvm/CodeGen/SlotIndexes.h"
Hal Finkel934361a2015-01-14 01:07:51 +000032#include "llvm/CodeGen/StackMaps.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000033#include "llvm/MC/MCAsmInfo.h"
Pete Cooper3de83e42015-05-15 21:58:42 +000034#include "llvm/MC/MCInst.h"
Bill Wendling1af20ad2008-03-04 23:13:51 +000035#include "llvm/Support/CommandLine.h"
Hal Finkel174e5902014-03-25 23:29:21 +000036#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000037#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000038#include "llvm/Support/TargetRegistry.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000039#include "llvm/Support/raw_ostream.h"
Misha Brukman116f9272004-08-17 04:55:41 +000040
Dan Gohman20857192010-04-15 17:20:57 +000041using namespace llvm;
Bill Wendling1af20ad2008-03-04 23:13:51 +000042
Chandler Carruthe96dd892014-04-21 22:55:11 +000043#define DEBUG_TYPE "ppc-instr-info"
44
Chandler Carruthd174b722014-04-22 02:03:14 +000045#define GET_INSTRMAP_INFO
46#define GET_INSTRINFO_CTOR_DTOR
47#include "PPCGenInstrInfo.inc"
48
Hal Finkel821e0012012-06-08 15:38:25 +000049static cl::
Hal Finkelc6b5deb2012-06-08 19:19:53 +000050opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
51 cl::desc("Disable analysis for CTR loops"));
Hal Finkel821e0012012-06-08 15:38:25 +000052
Hal Finkele6322392013-04-19 22:08:38 +000053static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt",
Hal Finkelb12da6b2013-04-18 22:54:25 +000054cl::desc("Disable compare instruction optimization"), cl::Hidden);
55
Hal Finkel9dcb3582014-03-27 22:46:28 +000056static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy",
57cl::desc("Causes the backend to crash instead of generating a nop VSX copy"),
58cl::Hidden);
59
Hal Finkel8acae522015-07-14 20:02:02 +000060static cl::opt<bool>
61UseOldLatencyCalc("ppc-old-latency-calc", cl::Hidden,
62 cl::desc("Use the old (incorrect) instruction latency calculation"));
63
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000064// Pin the vtable to this file.
65void PPCInstrInfo::anchor() {}
66
Eric Christopher1dcea732014-06-12 21:48:52 +000067PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI)
Tim Shen918ed872017-02-10 21:03:24 +000068 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP,
69 /* CatchRetOpcode */ -1,
70 STI.isPPC64() ? PPC::BLR8 : PPC::BLR),
Eric Christopherea178cf2015-03-12 01:42:51 +000071 Subtarget(STI), RI(STI.getTargetMachine()) {}
Chris Lattner49cadab2006-06-17 00:01:04 +000072
Andrew Trick10ffc2b2010-12-24 05:03:26 +000073/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
74/// this target when scheduling the DAG.
Eric Christopherf047bfd2014-06-13 22:38:52 +000075ScheduleHazardRecognizer *
76PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
77 const ScheduleDAG *DAG) const {
78 unsigned Directive =
79 static_cast<const PPCSubtarget *>(STI)->getDarwinDirective();
Hal Finkel742b5352012-08-28 16:12:39 +000080 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
81 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
Eric Christopherf047bfd2014-06-13 22:38:52 +000082 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +000083 static_cast<const PPCSubtarget *>(STI)->getInstrItineraryData();
Hal Finkel563cc052013-12-02 23:52:46 +000084 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel6fa56972011-10-17 04:03:49 +000085 }
Hal Finkel58ca3602011-12-02 04:58:02 +000086
Eric Christopherf047bfd2014-06-13 22:38:52 +000087 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000088}
89
Hal Finkel58ca3602011-12-02 04:58:02 +000090/// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
91/// to use for this target when scheduling the DAG.
Eric Christophercccae792015-01-30 22:02:31 +000092ScheduleHazardRecognizer *
93PPCInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
94 const ScheduleDAG *DAG) const {
Eric Christopher1dcea732014-06-12 21:48:52 +000095 unsigned Directive =
Eric Christophercccae792015-01-30 22:02:31 +000096 DAG->MF.getSubtarget<PPCSubtarget>().getDarwinDirective();
Hal Finkel58ca3602011-12-02 04:58:02 +000097
Nemanja Ivanovic6e29baf2016-05-09 18:54:58 +000098 // FIXME: Leaving this as-is until we have POWER9 scheduling info
Will Schmidt970ff642014-06-26 13:36:19 +000099 if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8)
Hal Finkelceb1f122013-12-12 00:19:11 +0000100 return new PPCDispatchGroupSBHazardRecognizer(II, DAG);
101
Hal Finkel58ca3602011-12-02 04:58:02 +0000102 // Most subtargets use a PPC970 recognizer.
Hal Finkel742b5352012-08-28 16:12:39 +0000103 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
104 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
Eric Christopher1dcea732014-06-12 21:48:52 +0000105 assert(DAG->TII && "No InstrInfo?");
Hal Finkel58ca3602011-12-02 04:58:02 +0000106
Eric Christopher1dcea732014-06-12 21:48:52 +0000107 return new PPCHazardRecognizer970(*DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000108 }
109
Hal Finkel563cc052013-12-02 23:52:46 +0000110 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000111}
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000112
Hal Finkel8acae522015-07-14 20:02:02 +0000113unsigned PPCInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000114 const MachineInstr &MI,
Hal Finkel8acae522015-07-14 20:02:02 +0000115 unsigned *PredCost) const {
116 if (!ItinData || UseOldLatencyCalc)
117 return PPCGenInstrInfo::getInstrLatency(ItinData, MI, PredCost);
118
119 // The default implementation of getInstrLatency calls getStageLatency, but
120 // getStageLatency does not do the right thing for us. While we have
121 // itinerary, most cores are fully pipelined, and so the itineraries only
122 // express the first part of the pipeline, not every stage. Instead, we need
123 // to use the listed output operand cycle number (using operand 0 here, which
124 // is an output).
125
126 unsigned Latency = 1;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000127 unsigned DefClass = MI.getDesc().getSchedClass();
128 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
129 const MachineOperand &MO = MI.getOperand(i);
Hal Finkel8acae522015-07-14 20:02:02 +0000130 if (!MO.isReg() || !MO.isDef() || MO.isImplicit())
131 continue;
132
133 int Cycle = ItinData->getOperandCycle(DefClass, i);
134 if (Cycle < 0)
135 continue;
136
137 Latency = std::max(Latency, (unsigned) Cycle);
138 }
139
140 return Latency;
141}
Hal Finkelceb1f122013-12-12 00:19:11 +0000142
143int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000144 const MachineInstr &DefMI, unsigned DefIdx,
145 const MachineInstr &UseMI,
Hal Finkelceb1f122013-12-12 00:19:11 +0000146 unsigned UseIdx) const {
147 int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
148 UseMI, UseIdx);
149
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000150 if (!DefMI.getParent())
Hal Finkel5d36b232015-07-15 08:23:05 +0000151 return Latency;
152
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000153 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
Hal Finkelceb1f122013-12-12 00:19:11 +0000154 unsigned Reg = DefMO.getReg();
155
Hal Finkelceb1f122013-12-12 00:19:11 +0000156 bool IsRegCR;
Andrew Kaylor5c73e1f2015-03-24 23:37:10 +0000157 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Hal Finkelceb1f122013-12-12 00:19:11 +0000158 const MachineRegisterInfo *MRI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000159 &DefMI.getParent()->getParent()->getRegInfo();
Hal Finkelceb1f122013-12-12 00:19:11 +0000160 IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) ||
161 MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass);
162 } else {
163 IsRegCR = PPC::CRRCRegClass.contains(Reg) ||
164 PPC::CRBITRCRegClass.contains(Reg);
165 }
166
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000167 if (UseMI.isBranch() && IsRegCR) {
Hal Finkelceb1f122013-12-12 00:19:11 +0000168 if (Latency < 0)
169 Latency = getInstrLatency(ItinData, DefMI);
170
171 // On some cores, there is an additional delay between writing to a condition
172 // register, and using it from a branch.
Eric Christopher1dcea732014-06-12 21:48:52 +0000173 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000174 switch (Directive) {
175 default: break;
176 case PPC::DIR_7400:
177 case PPC::DIR_750:
178 case PPC::DIR_970:
179 case PPC::DIR_E5500:
180 case PPC::DIR_PWR4:
181 case PPC::DIR_PWR5:
182 case PPC::DIR_PWR5X:
183 case PPC::DIR_PWR6:
184 case PPC::DIR_PWR6X:
185 case PPC::DIR_PWR7:
Will Schmidt970ff642014-06-26 13:36:19 +0000186 case PPC::DIR_PWR8:
Nemanja Ivanovic6e29baf2016-05-09 18:54:58 +0000187 // FIXME: Is this needed for POWER9?
Hal Finkelceb1f122013-12-12 00:19:11 +0000188 Latency += 2;
189 break;
190 }
191 }
192
193 return Latency;
194}
195
Hal Finkel5d36b232015-07-15 08:23:05 +0000196// This function does not list all associative and commutative operations, but
197// only those worth feeding through the machine combiner in an attempt to
198// reduce the critical path. Mostly, this means floating-point operations,
199// because they have high latencies (compared to other operations, such and
200// and/or, which are also associative and commutative, but have low latencies).
Chad Rosier03a47302015-09-21 15:09:11 +0000201bool PPCInstrInfo::isAssociativeAndCommutative(const MachineInstr &Inst) const {
202 switch (Inst.getOpcode()) {
Hal Finkel5d36b232015-07-15 08:23:05 +0000203 // FP Add:
204 case PPC::FADD:
205 case PPC::FADDS:
206 // FP Multiply:
207 case PPC::FMUL:
208 case PPC::FMULS:
209 // Altivec Add:
210 case PPC::VADDFP:
211 // VSX Add:
212 case PPC::XSADDDP:
213 case PPC::XVADDDP:
214 case PPC::XVADDSP:
215 case PPC::XSADDSP:
216 // VSX Multiply:
217 case PPC::XSMULDP:
218 case PPC::XVMULDP:
219 case PPC::XVMULSP:
220 case PPC::XSMULSP:
221 // QPX Add:
222 case PPC::QVFADD:
223 case PPC::QVFADDS:
224 case PPC::QVFADDSs:
225 // QPX Multiply:
226 case PPC::QVFMUL:
227 case PPC::QVFMULS:
228 case PPC::QVFMULSs:
229 return true;
230 default:
231 return false;
232 }
233}
234
Chad Rosier03a47302015-09-21 15:09:11 +0000235bool PPCInstrInfo::getMachineCombinerPatterns(
236 MachineInstr &Root,
Sanjay Patel387e66e2015-11-05 19:34:57 +0000237 SmallVectorImpl<MachineCombinerPattern> &Patterns) const {
Hal Finkel5d36b232015-07-15 08:23:05 +0000238 // Using the machine combiner in this way is potentially expensive, so
239 // restrict to when aggressive optimizations are desired.
240 if (Subtarget.getTargetMachine().getOptLevel() != CodeGenOpt::Aggressive)
241 return false;
242
243 // FP reassociation is only legal when we don't need strict IEEE semantics.
244 if (!Root.getParent()->getParent()->getTarget().Options.UnsafeFPMath)
245 return false;
246
Chad Rosier03a47302015-09-21 15:09:11 +0000247 return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns);
Hal Finkel5d36b232015-07-15 08:23:05 +0000248}
249
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000250// Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
251bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
252 unsigned &SrcReg, unsigned &DstReg,
253 unsigned &SubIdx) const {
254 switch (MI.getOpcode()) {
255 default: return false;
256 case PPC::EXTSW:
257 case PPC::EXTSW_32_64:
258 SrcReg = MI.getOperand(1).getReg();
259 DstReg = MI.getOperand(0).getReg();
260 SubIdx = PPC::sub_32;
261 return true;
262 }
263}
264
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000265unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Chris Lattner91400bd2006-03-16 22:24:02 +0000266 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000267 // Note: This list must be kept consistent with LoadRegFromStackSlot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000268 switch (MI.getOpcode()) {
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000269 default: break;
270 case PPC::LD:
271 case PPC::LWZ:
272 case PPC::LFS:
273 case PPC::LFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000274 case PPC::RESTORE_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000275 case PPC::RESTORE_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000276 case PPC::LVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000277 case PPC::LXVD2X:
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +0000278 case PPC::LXVX:
Hal Finkelc93a9a22015-02-25 01:06:45 +0000279 case PPC::QVLFDX:
280 case PPC::QVLFSXs:
281 case PPC::QVLFDXb:
Hal Finkel37714b82013-03-27 21:21:15 +0000282 case PPC::RESTORE_VRSAVE:
283 // Check for the operands added by addFrameReference (the immediate is the
284 // offset which defaults to 0).
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000285 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() &&
286 MI.getOperand(2).isFI()) {
287 FrameIndex = MI.getOperand(2).getIndex();
288 return MI.getOperand(0).getReg();
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000289 }
290 break;
291 }
292 return 0;
Chris Lattnerc327d712006-02-02 20:16:12 +0000293}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000294
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000295unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Chris Lattnerc327d712006-02-02 20:16:12 +0000296 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000297 // Note: This list must be kept consistent with StoreRegToStackSlot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000298 switch (MI.getOpcode()) {
Chris Lattnerc327d712006-02-02 20:16:12 +0000299 default: break;
Nate Begeman4efb3282006-02-02 21:07:50 +0000300 case PPC::STD:
Chris Lattnerc327d712006-02-02 20:16:12 +0000301 case PPC::STW:
302 case PPC::STFS:
303 case PPC::STFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000304 case PPC::SPILL_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000305 case PPC::SPILL_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000306 case PPC::STVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000307 case PPC::STXVD2X:
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +0000308 case PPC::STXVX:
Hal Finkelc93a9a22015-02-25 01:06:45 +0000309 case PPC::QVSTFDX:
310 case PPC::QVSTFSXs:
311 case PPC::QVSTFDXb:
Hal Finkel37714b82013-03-27 21:21:15 +0000312 case PPC::SPILL_VRSAVE:
313 // Check for the operands added by addFrameReference (the immediate is the
314 // offset which defaults to 0).
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000315 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() &&
316 MI.getOperand(2).isFI()) {
317 FrameIndex = MI.getOperand(2).getIndex();
318 return MI.getOperand(0).getReg();
Chris Lattnerc327d712006-02-02 20:16:12 +0000319 }
320 break;
321 }
322 return 0;
323}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000324
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000325MachineInstr *PPCInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI,
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000326 unsigned OpIdx1,
327 unsigned OpIdx2) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000328 MachineFunction &MF = *MI.getParent()->getParent();
Dan Gohman3b460302008-07-07 23:14:23 +0000329
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000330 // Normal instructions can be commuted the obvious way.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000331 if (MI.getOpcode() != PPC::RLWIMI && MI.getOpcode() != PPC::RLWIMIo)
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000332 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
Hal Finkel4c6658f2014-12-12 23:59:36 +0000333 // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a
334 // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because
335 // changing the relative order of the mask operands might change what happens
336 // to the high-bits of the mask (and, thus, the result).
Andrew Trickc416ba62010-12-24 04:28:06 +0000337
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000338 // Cannot commute if it has a non-zero rotate count.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000339 if (MI.getOperand(3).getImm() != 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000340 return nullptr;
Andrew Trickc416ba62010-12-24 04:28:06 +0000341
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000342 // If we have a zero rotate count, we have:
343 // M = mask(MB,ME)
344 // Op0 = (Op1 & ~M) | (Op2 & M)
345 // Change this to:
346 // M = mask((ME+1)&31, (MB-1)&31)
347 // Op0 = (Op2 & ~M) | (Op1 & M)
348
349 // Swap op1/op2
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000350 assert(((OpIdx1 == 1 && OpIdx2 == 2) || (OpIdx1 == 2 && OpIdx2 == 1)) &&
351 "Only the operands 1 and 2 can be swapped in RLSIMI/RLWIMIo.");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000352 unsigned Reg0 = MI.getOperand(0).getReg();
353 unsigned Reg1 = MI.getOperand(1).getReg();
354 unsigned Reg2 = MI.getOperand(2).getReg();
355 unsigned SubReg1 = MI.getOperand(1).getSubReg();
356 unsigned SubReg2 = MI.getOperand(2).getSubReg();
357 bool Reg1IsKill = MI.getOperand(1).isKill();
358 bool Reg2IsKill = MI.getOperand(2).isKill();
Evan Cheng03553bb2008-06-16 07:33:11 +0000359 bool ChangeReg0 = false;
Evan Cheng244183e2008-02-13 02:46:49 +0000360 // If machine instrs are no longer in two-address forms, update
361 // destination register as well.
362 if (Reg0 == Reg1) {
363 // Must be two address instruction!
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000364 assert(MI.getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Cheng244183e2008-02-13 02:46:49 +0000365 "Expecting a two-address instruction!");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000366 assert(MI.getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
Evan Cheng244183e2008-02-13 02:46:49 +0000367 Reg2IsKill = false;
Evan Cheng03553bb2008-06-16 07:33:11 +0000368 ChangeReg0 = true;
Evan Cheng244183e2008-02-13 02:46:49 +0000369 }
Evan Cheng03553bb2008-06-16 07:33:11 +0000370
371 // Masks.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000372 unsigned MB = MI.getOperand(4).getImm();
373 unsigned ME = MI.getOperand(5).getImm();
Evan Cheng03553bb2008-06-16 07:33:11 +0000374
Hal Finkelccf92592015-09-06 04:17:30 +0000375 // We can't commute a trivial mask (there is no way to represent an all-zero
376 // mask).
377 if (MB == 0 && ME == 31)
378 return nullptr;
379
Evan Cheng03553bb2008-06-16 07:33:11 +0000380 if (NewMI) {
381 // Create a new instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000382 unsigned Reg0 = ChangeReg0 ? Reg2 : MI.getOperand(0).getReg();
383 bool Reg0IsDead = MI.getOperand(0).isDead();
384 return BuildMI(MF, MI.getDebugLoc(), MI.getDesc())
385 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
386 .addReg(Reg2, getKillRegState(Reg2IsKill))
387 .addReg(Reg1, getKillRegState(Reg1IsKill))
388 .addImm((ME + 1) & 31)
389 .addImm((MB - 1) & 31);
Evan Cheng03553bb2008-06-16 07:33:11 +0000390 }
391
Andrew Tricke3398282013-12-17 04:50:45 +0000392 if (ChangeReg0) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000393 MI.getOperand(0).setReg(Reg2);
394 MI.getOperand(0).setSubReg(SubReg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000395 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000396 MI.getOperand(2).setReg(Reg1);
397 MI.getOperand(1).setReg(Reg2);
398 MI.getOperand(2).setSubReg(SubReg1);
399 MI.getOperand(1).setSubReg(SubReg2);
400 MI.getOperand(2).setIsKill(Reg1IsKill);
401 MI.getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000402
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000403 // Swap the mask around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000404 MI.getOperand(4).setImm((ME + 1) & 31);
405 MI.getOperand(5).setImm((MB - 1) & 31);
406 return &MI;
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000407}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000408
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000409bool PPCInstrInfo::findCommutedOpIndices(MachineInstr &MI, unsigned &SrcOpIdx1,
Hal Finkel6c32ff32014-03-25 19:26:43 +0000410 unsigned &SrcOpIdx2) const {
411 // For VSX A-Type FMA instructions, it is the first two operands that can be
412 // commuted, however, because the non-encoded tied input operand is listed
413 // first, the operands to swap are actually the second and third.
414
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000415 int AltOpc = PPC::getAltVSXFMAOpcode(MI.getOpcode());
Hal Finkel6c32ff32014-03-25 19:26:43 +0000416 if (AltOpc == -1)
417 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
418
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000419 // The commutable operand indices are 2 and 3. Return them in SrcOpIdx1
420 // and SrcOpIdx2.
421 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2, 3);
Hal Finkel6c32ff32014-03-25 19:26:43 +0000422}
423
Andrew Trickc416ba62010-12-24 04:28:06 +0000424void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000425 MachineBasicBlock::iterator MI) const {
Hal Finkelceb1f122013-12-12 00:19:11 +0000426 // This function is used for scheduling, and the nop wanted here is the type
427 // that terminates dispatch groups on the POWER cores.
Eric Christopher1dcea732014-06-12 21:48:52 +0000428 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000429 unsigned Opcode;
430 switch (Directive) {
431 default: Opcode = PPC::NOP; break;
432 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
433 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
Will Schmidt970ff642014-06-26 13:36:19 +0000434 case PPC::DIR_PWR8: Opcode = PPC::NOP_GT_PWR7; break; /* FIXME: Update when P8 InstrScheduling model is ready */
Nemanja Ivanovic6e29baf2016-05-09 18:54:58 +0000435 // FIXME: Update when POWER9 scheduling model is ready.
436 case PPC::DIR_PWR9: Opcode = PPC::NOP_GT_PWR7; break;
Hal Finkelceb1f122013-12-12 00:19:11 +0000437 }
Chris Lattnera47294ed2006-10-13 21:21:17 +0000438
Hal Finkelceb1f122013-12-12 00:19:11 +0000439 DebugLoc DL;
440 BuildMI(MBB, MI, DL, get(Opcode));
441}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000442
Joerg Sonnenberger7ee0f312014-08-08 19:13:23 +0000443/// getNoopForMachoTarget - Return the noop instruction to use for a noop.
444void PPCInstrInfo::getNoopForMachoTarget(MCInst &NopInst) const {
445 NopInst.setOpcode(PPC::NOP);
446}
447
Chris Lattnera47294ed2006-10-13 21:21:17 +0000448// Branch analysis.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000449// Note: If the condition register is set to CTR or CTR8 then this is a
450// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000451bool PPCInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
452 MachineBasicBlock *&TBB,
Chris Lattnera47294ed2006-10-13 21:21:17 +0000453 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000454 SmallVectorImpl<MachineOperand> &Cond,
455 bool AllowModify) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000456 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000457
Chris Lattnera47294ed2006-10-13 21:21:17 +0000458 // If the block has no terminators, it just falls into the block after it.
Benjamin Kramer92861d72015-06-25 13:39:03 +0000459 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
460 if (I == MBB.end())
Dale Johannesen4244d122010-04-02 01:38:09 +0000461 return false;
Benjamin Kramer92861d72015-06-25 13:39:03 +0000462
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000463 if (!isUnpredicatedTerminator(*I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000464 return false;
465
466 // Get the last instruction in the block.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000467 MachineInstr &LastInst = *I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000468
Chris Lattnera47294ed2006-10-13 21:21:17 +0000469 // If there is only one terminator instruction, process it.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000470 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000471 if (LastInst.getOpcode() == PPC::B) {
472 if (!LastInst.getOperand(0).isMBB())
Evan Cheng8f43afd2009-05-08 23:09:25 +0000473 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000474 TBB = LastInst.getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000475 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000476 } else if (LastInst.getOpcode() == PPC::BCC) {
477 if (!LastInst.getOperand(2).isMBB())
Evan Cheng8f43afd2009-05-08 23:09:25 +0000478 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000479 // Block ends with fall-through condbranch.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000480 TBB = LastInst.getOperand(2).getMBB();
481 Cond.push_back(LastInst.getOperand(0));
482 Cond.push_back(LastInst.getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000483 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000484 } else if (LastInst.getOpcode() == PPC::BC) {
485 if (!LastInst.getOperand(1).isMBB())
Hal Finkel940ab932014-02-28 00:27:01 +0000486 return true;
487 // Block ends with fall-through condbranch.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000488 TBB = LastInst.getOperand(1).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000489 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000490 Cond.push_back(LastInst.getOperand(0));
Hal Finkel940ab932014-02-28 00:27:01 +0000491 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000492 } else if (LastInst.getOpcode() == PPC::BCn) {
493 if (!LastInst.getOperand(1).isMBB())
Hal Finkel940ab932014-02-28 00:27:01 +0000494 return true;
495 // Block ends with fall-through condbranch.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000496 TBB = LastInst.getOperand(1).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000497 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000498 Cond.push_back(LastInst.getOperand(0));
Hal Finkel940ab932014-02-28 00:27:01 +0000499 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000500 } else if (LastInst.getOpcode() == PPC::BDNZ8 ||
501 LastInst.getOpcode() == PPC::BDNZ) {
502 if (!LastInst.getOperand(0).isMBB())
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000503 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000504 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000505 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000506 TBB = LastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000507 Cond.push_back(MachineOperand::CreateImm(1));
508 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
509 true));
510 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000511 } else if (LastInst.getOpcode() == PPC::BDZ8 ||
512 LastInst.getOpcode() == PPC::BDZ) {
513 if (!LastInst.getOperand(0).isMBB())
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000514 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000515 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000516 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000517 TBB = LastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000518 Cond.push_back(MachineOperand::CreateImm(0));
519 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
520 true));
521 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000522 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000523
Chris Lattnera47294ed2006-10-13 21:21:17 +0000524 // Otherwise, don't know what this is.
525 return true;
526 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000527
Chris Lattnera47294ed2006-10-13 21:21:17 +0000528 // Get the instruction before it if it's a terminator.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000529 MachineInstr &SecondLastInst = *I;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000530
531 // If there are three terminators, we don't know what sort of block this is.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000532 if (I != MBB.begin() && isUnpredicatedTerminator(*--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000533 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000534
Chris Lattnere0263792006-11-17 22:14:47 +0000535 // If the block ends with PPC::B and PPC:BCC, handle it.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000536 if (SecondLastInst.getOpcode() == PPC::BCC &&
537 LastInst.getOpcode() == PPC::B) {
538 if (!SecondLastInst.getOperand(2).isMBB() ||
539 !LastInst.getOperand(0).isMBB())
Evan Cheng8f43afd2009-05-08 23:09:25 +0000540 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000541 TBB = SecondLastInst.getOperand(2).getMBB();
542 Cond.push_back(SecondLastInst.getOperand(0));
543 Cond.push_back(SecondLastInst.getOperand(1));
544 FBB = LastInst.getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000545 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000546 } else if (SecondLastInst.getOpcode() == PPC::BC &&
547 LastInst.getOpcode() == PPC::B) {
548 if (!SecondLastInst.getOperand(1).isMBB() ||
549 !LastInst.getOperand(0).isMBB())
Hal Finkel940ab932014-02-28 00:27:01 +0000550 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000551 TBB = SecondLastInst.getOperand(1).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000552 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000553 Cond.push_back(SecondLastInst.getOperand(0));
554 FBB = LastInst.getOperand(0).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000555 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000556 } else if (SecondLastInst.getOpcode() == PPC::BCn &&
557 LastInst.getOpcode() == PPC::B) {
558 if (!SecondLastInst.getOperand(1).isMBB() ||
559 !LastInst.getOperand(0).isMBB())
Hal Finkel940ab932014-02-28 00:27:01 +0000560 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000561 TBB = SecondLastInst.getOperand(1).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000562 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000563 Cond.push_back(SecondLastInst.getOperand(0));
564 FBB = LastInst.getOperand(0).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000565 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000566 } else if ((SecondLastInst.getOpcode() == PPC::BDNZ8 ||
567 SecondLastInst.getOpcode() == PPC::BDNZ) &&
568 LastInst.getOpcode() == PPC::B) {
569 if (!SecondLastInst.getOperand(0).isMBB() ||
570 !LastInst.getOperand(0).isMBB())
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000571 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000572 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000573 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000574 TBB = SecondLastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000575 Cond.push_back(MachineOperand::CreateImm(1));
576 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
577 true));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000578 FBB = LastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000579 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000580 } else if ((SecondLastInst.getOpcode() == PPC::BDZ8 ||
581 SecondLastInst.getOpcode() == PPC::BDZ) &&
582 LastInst.getOpcode() == PPC::B) {
583 if (!SecondLastInst.getOperand(0).isMBB() ||
584 !LastInst.getOperand(0).isMBB())
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000585 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000586 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000587 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000588 TBB = SecondLastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000589 Cond.push_back(MachineOperand::CreateImm(0));
590 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
591 true));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000592 FBB = LastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000593 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000594 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000595
Dale Johannesenc6855462007-06-13 17:59:52 +0000596 // If the block ends with two PPC:Bs, handle it. The second one is not
597 // executed, so remove it.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000598 if (SecondLastInst.getOpcode() == PPC::B && LastInst.getOpcode() == PPC::B) {
599 if (!SecondLastInst.getOperand(0).isMBB())
Evan Cheng8f43afd2009-05-08 23:09:25 +0000600 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000601 TBB = SecondLastInst.getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000602 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000603 if (AllowModify)
604 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000605 return false;
606 }
607
Chris Lattnera47294ed2006-10-13 21:21:17 +0000608 // Otherwise, can't handle this.
609 return true;
610}
611
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000612unsigned PPCInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000613 int *BytesRemoved) const {
614 assert(!BytesRemoved && "code size not handled");
615
Benjamin Kramer92861d72015-06-25 13:39:03 +0000616 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
617 if (I == MBB.end())
618 return 0;
619
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000620 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000621 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000622 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
623 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000624 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000625
Chris Lattnera47294ed2006-10-13 21:21:17 +0000626 // Remove the branch.
627 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000628
Chris Lattnera47294ed2006-10-13 21:21:17 +0000629 I = MBB.end();
630
Evan Cheng99be49d2007-05-18 00:05:48 +0000631 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000632 --I;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000633 if (I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000634 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000635 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
636 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000637 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000638
Chris Lattnera47294ed2006-10-13 21:21:17 +0000639 // Remove the branch.
640 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000641 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000642}
643
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000644unsigned PPCInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000645 MachineBasicBlock *TBB,
646 MachineBasicBlock *FBB,
647 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000648 const DebugLoc &DL,
649 int *BytesAdded) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000650 // Shouldn't be a fall through.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000651 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000652 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000653 "PPC branch conditions have two components!");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000654 assert(!BytesAdded && "code size not handled");
Andrew Trickc416ba62010-12-24 04:28:06 +0000655
Eric Christopher1dcea732014-06-12 21:48:52 +0000656 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000657
Chris Lattner94e04442006-10-21 05:36:13 +0000658 // One-way branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000659 if (!FBB) {
Chris Lattner94e04442006-10-21 05:36:13 +0000660 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000661 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000662 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
663 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
664 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
665 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000666 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
Diana Picus116bbab2017-01-13 09:58:52 +0000667 BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000668 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
Diana Picus116bbab2017-01-13 09:58:52 +0000669 BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000670 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000671 BuildMI(&MBB, DL, get(PPC::BCC))
Diana Picus116bbab2017-01-13 09:58:52 +0000672 .addImm(Cond[0].getImm())
673 .add(Cond[1])
674 .addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000675 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000676 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000677
Chris Lattnerd8816602006-10-21 05:42:09 +0000678 // Two-way Conditional Branch.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000679 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
680 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
681 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
682 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000683 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
Diana Picus116bbab2017-01-13 09:58:52 +0000684 BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000685 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
Diana Picus116bbab2017-01-13 09:58:52 +0000686 BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000687 else
688 BuildMI(&MBB, DL, get(PPC::BCC))
Diana Picus116bbab2017-01-13 09:58:52 +0000689 .addImm(Cond[0].getImm())
690 .add(Cond[1])
691 .addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000692 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000693 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000694}
695
Hal Finkeled6a2852013-04-05 23:29:01 +0000696// Select analysis.
697bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000698 ArrayRef<MachineOperand> Cond,
Hal Finkeled6a2852013-04-05 23:29:01 +0000699 unsigned TrueReg, unsigned FalseReg,
700 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
Hal Finkeled6a2852013-04-05 23:29:01 +0000701 if (Cond.size() != 2)
702 return false;
703
704 // If this is really a bdnz-like condition, then it cannot be turned into a
705 // select.
706 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
707 return false;
708
709 // Check register classes.
710 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
711 const TargetRegisterClass *RC =
712 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
713 if (!RC)
714 return false;
715
716 // isel is for regular integer GPRs only.
717 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkel8e8618a2013-07-15 20:22:58 +0000718 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
719 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
720 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkeled6a2852013-04-05 23:29:01 +0000721 return false;
722
723 // FIXME: These numbers are for the A2, how well they work for other cores is
724 // an open question. On the A2, the isel instruction has a 2-cycle latency
725 // but single-cycle throughput. These numbers are used in combination with
726 // the MispredictPenalty setting from the active SchedMachineModel.
727 CondCycles = 1;
728 TrueCycles = 1;
729 FalseCycles = 1;
730
731 return true;
732}
733
734void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000735 MachineBasicBlock::iterator MI,
736 const DebugLoc &dl, unsigned DestReg,
737 ArrayRef<MachineOperand> Cond, unsigned TrueReg,
738 unsigned FalseReg) const {
Hal Finkeled6a2852013-04-05 23:29:01 +0000739 assert(Cond.size() == 2 &&
740 "PPC branch conditions have two components!");
741
Hal Finkeled6a2852013-04-05 23:29:01 +0000742 // Get the register classes.
743 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
744 const TargetRegisterClass *RC =
745 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
746 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkel8e8618a2013-07-15 20:22:58 +0000747
748 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
749 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
750 assert((Is64Bit ||
751 PPC::GPRCRegClass.hasSubClassEq(RC) ||
752 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000753 "isel is for regular integer GPRs only");
754
Hal Finkel8e8618a2013-07-15 20:22:58 +0000755 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Kyle Buttcec40802016-01-12 21:00:43 +0000756 auto SelectPred = static_cast<PPC::Predicate>(Cond[0].getImm());
Hal Finkeled6a2852013-04-05 23:29:01 +0000757
Kyle Butt132bf362016-01-15 19:20:06 +0000758 unsigned SubIdx = 0;
759 bool SwapOps = false;
Hal Finkeled6a2852013-04-05 23:29:01 +0000760 switch (SelectPred) {
Kyle Buttcec40802016-01-12 21:00:43 +0000761 case PPC::PRED_EQ:
762 case PPC::PRED_EQ_MINUS:
763 case PPC::PRED_EQ_PLUS:
764 SubIdx = PPC::sub_eq; SwapOps = false; break;
765 case PPC::PRED_NE:
766 case PPC::PRED_NE_MINUS:
767 case PPC::PRED_NE_PLUS:
768 SubIdx = PPC::sub_eq; SwapOps = true; break;
769 case PPC::PRED_LT:
770 case PPC::PRED_LT_MINUS:
771 case PPC::PRED_LT_PLUS:
772 SubIdx = PPC::sub_lt; SwapOps = false; break;
773 case PPC::PRED_GE:
774 case PPC::PRED_GE_MINUS:
775 case PPC::PRED_GE_PLUS:
776 SubIdx = PPC::sub_lt; SwapOps = true; break;
777 case PPC::PRED_GT:
778 case PPC::PRED_GT_MINUS:
779 case PPC::PRED_GT_PLUS:
780 SubIdx = PPC::sub_gt; SwapOps = false; break;
781 case PPC::PRED_LE:
782 case PPC::PRED_LE_MINUS:
783 case PPC::PRED_LE_PLUS:
784 SubIdx = PPC::sub_gt; SwapOps = true; break;
785 case PPC::PRED_UN:
786 case PPC::PRED_UN_MINUS:
787 case PPC::PRED_UN_PLUS:
788 SubIdx = PPC::sub_un; SwapOps = false; break;
789 case PPC::PRED_NU:
790 case PPC::PRED_NU_MINUS:
791 case PPC::PRED_NU_PLUS:
792 SubIdx = PPC::sub_un; SwapOps = true; break;
Hal Finkel940ab932014-02-28 00:27:01 +0000793 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break;
794 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
Hal Finkeled6a2852013-04-05 23:29:01 +0000795 }
796
797 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
798 SecondReg = SwapOps ? TrueReg : FalseReg;
799
800 // The first input register of isel cannot be r0. If it is a member
801 // of a register class that can be r0, then copy it first (the
802 // register allocator should eliminate the copy).
803 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
804 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
805 const TargetRegisterClass *FirstRC =
806 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
807 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
808 unsigned OldFirstReg = FirstReg;
809 FirstReg = MRI.createVirtualRegister(FirstRC);
810 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
811 .addReg(OldFirstReg);
812 }
813
814 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
815 .addReg(FirstReg).addReg(SecondReg)
816 .addReg(Cond[1].getReg(), 0, SubIdx);
817}
818
Kit Barton535e69d2015-03-25 19:36:23 +0000819static unsigned getCRBitValue(unsigned CRBit) {
820 unsigned Ret = 4;
821 if (CRBit == PPC::CR0LT || CRBit == PPC::CR1LT ||
822 CRBit == PPC::CR2LT || CRBit == PPC::CR3LT ||
823 CRBit == PPC::CR4LT || CRBit == PPC::CR5LT ||
824 CRBit == PPC::CR6LT || CRBit == PPC::CR7LT)
825 Ret = 3;
826 if (CRBit == PPC::CR0GT || CRBit == PPC::CR1GT ||
827 CRBit == PPC::CR2GT || CRBit == PPC::CR3GT ||
828 CRBit == PPC::CR4GT || CRBit == PPC::CR5GT ||
829 CRBit == PPC::CR6GT || CRBit == PPC::CR7GT)
830 Ret = 2;
831 if (CRBit == PPC::CR0EQ || CRBit == PPC::CR1EQ ||
832 CRBit == PPC::CR2EQ || CRBit == PPC::CR3EQ ||
833 CRBit == PPC::CR4EQ || CRBit == PPC::CR5EQ ||
834 CRBit == PPC::CR6EQ || CRBit == PPC::CR7EQ)
835 Ret = 1;
836 if (CRBit == PPC::CR0UN || CRBit == PPC::CR1UN ||
837 CRBit == PPC::CR2UN || CRBit == PPC::CR3UN ||
838 CRBit == PPC::CR4UN || CRBit == PPC::CR5UN ||
839 CRBit == PPC::CR6UN || CRBit == PPC::CR7UN)
840 Ret = 0;
841
842 assert(Ret != 4 && "Invalid CR bit register");
843 return Ret;
844}
845
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000846void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000847 MachineBasicBlock::iterator I,
848 const DebugLoc &DL, unsigned DestReg,
849 unsigned SrcReg, bool KillSrc) const {
Hal Finkel27774d92014-03-13 07:58:58 +0000850 // We can end up with self copies and similar things as a result of VSX copy
Hal Finkel9dcb3582014-03-27 22:46:28 +0000851 // legalization. Promote them here.
Hal Finkel27774d92014-03-13 07:58:58 +0000852 const TargetRegisterInfo *TRI = &getRegisterInfo();
853 if (PPC::F8RCRegClass.contains(DestReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000854 PPC::VSRCRegClass.contains(SrcReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000855 unsigned SuperReg =
856 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass);
857
Hal Finkel9dcb3582014-03-27 22:46:28 +0000858 if (VSXSelfCopyCrash && SrcReg == SuperReg)
859 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000860
861 DestReg = SuperReg;
Hal Finkel27774d92014-03-13 07:58:58 +0000862 } else if (PPC::F8RCRegClass.contains(SrcReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000863 PPC::VSRCRegClass.contains(DestReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000864 unsigned SuperReg =
865 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass);
866
Hal Finkel9dcb3582014-03-27 22:46:28 +0000867 if (VSXSelfCopyCrash && DestReg == SuperReg)
868 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000869
870 SrcReg = SuperReg;
Hal Finkel27774d92014-03-13 07:58:58 +0000871 }
872
Kit Barton535e69d2015-03-25 19:36:23 +0000873 // Different class register copy
874 if (PPC::CRBITRCRegClass.contains(SrcReg) &&
875 PPC::GPRCRegClass.contains(DestReg)) {
876 unsigned CRReg = getCRFromCRBit(SrcReg);
Richard Trieu7a083812016-02-18 22:09:30 +0000877 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(CRReg);
878 getKillRegState(KillSrc);
Kit Barton535e69d2015-03-25 19:36:23 +0000879 // Rotate the CR bit in the CR fields to be the least significant bit and
880 // then mask with 0x1 (MB = ME = 31).
881 BuildMI(MBB, I, DL, get(PPC::RLWINM), DestReg)
882 .addReg(DestReg, RegState::Kill)
883 .addImm(TRI->getEncodingValue(CRReg) * 4 + (4 - getCRBitValue(SrcReg)))
884 .addImm(31)
885 .addImm(31);
886 return;
887 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
888 PPC::G8RCRegClass.contains(DestReg)) {
Richard Trieu7a083812016-02-18 22:09:30 +0000889 BuildMI(MBB, I, DL, get(PPC::MFOCRF8), DestReg).addReg(SrcReg);
890 getKillRegState(KillSrc);
Kit Barton535e69d2015-03-25 19:36:23 +0000891 return;
892 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
893 PPC::GPRCRegClass.contains(DestReg)) {
Richard Trieu7a083812016-02-18 22:09:30 +0000894 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(SrcReg);
895 getKillRegState(KillSrc);
Kit Barton535e69d2015-03-25 19:36:23 +0000896 return;
897 }
898
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000899 unsigned Opc;
900 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
901 Opc = PPC::OR;
902 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
903 Opc = PPC::OR8;
904 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
905 Opc = PPC::FMR;
906 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
907 Opc = PPC::MCRF;
908 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
909 Opc = PPC::VOR;
Hal Finkel27774d92014-03-13 07:58:58 +0000910 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg))
Hal Finkelbbad2332014-03-24 09:36:36 +0000911 // There are two different ways this can be done:
Hal Finkel27774d92014-03-13 07:58:58 +0000912 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
913 // issue in VSU pipeline 0.
914 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
915 // can go to either pipeline.
Hal Finkelbbad2332014-03-24 09:36:36 +0000916 // We'll always use xxlor here, because in practically all cases where
917 // copies are generated, they are close enough to some use that the
918 // lower-latency form is preferable.
Hal Finkel27774d92014-03-13 07:58:58 +0000919 Opc = PPC::XXLOR;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +0000920 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg) ||
921 PPC::VSSRCRegClass.contains(DestReg, SrcReg))
Hal Finkel19be5062014-03-29 05:29:01 +0000922 Opc = PPC::XXLORf;
Hal Finkelc93a9a22015-02-25 01:06:45 +0000923 else if (PPC::QFRCRegClass.contains(DestReg, SrcReg))
924 Opc = PPC::QVFMR;
925 else if (PPC::QSRCRegClass.contains(DestReg, SrcReg))
926 Opc = PPC::QVFMRs;
927 else if (PPC::QBRCRegClass.contains(DestReg, SrcReg))
928 Opc = PPC::QVFMRb;
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000929 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
930 Opc = PPC::CROR;
931 else
932 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +0000933
Evan Cheng6cc775f2011-06-28 19:10:37 +0000934 const MCInstrDesc &MCID = get(Opc);
935 if (MCID.getNumOperands() == 3)
936 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000937 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
938 else
Evan Cheng6cc775f2011-06-28 19:10:37 +0000939 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +0000940}
941
Hal Finkel8f6834d2011-12-05 17:55:17 +0000942// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000943bool
Dan Gohman3b460302008-07-07 23:14:23 +0000944PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
945 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000946 int FrameIdx,
947 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000948 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000949 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000950 // Note: If additional store instructions are added here,
951 // update isStoreToStackSlot.
952
Chris Lattner6f306d72010-04-02 20:16:16 +0000953 DebugLoc DL;
Hal Finkel4e703bc2014-01-28 05:32:58 +0000954 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
955 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000956 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
957 .addReg(SrcReg,
958 getKillRegState(isKill)),
959 FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000960 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
961 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000962 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
963 .addReg(SrcReg,
964 getKillRegState(isKill)),
965 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000966 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000967 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000968 .addReg(SrcReg,
969 getKillRegState(isKill)),
970 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000971 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000972 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000973 .addReg(SrcReg,
974 getKillRegState(isKill)),
975 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000976 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000977 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
978 .addReg(SrcReg,
979 getKillRegState(isKill)),
980 FrameIdx));
981 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000982 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000983 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT))
984 .addReg(SrcReg,
985 getKillRegState(isKill)),
986 FrameIdx));
987 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000988 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000989 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
990 .addReg(SrcReg,
991 getKillRegState(isKill)),
992 FrameIdx));
993 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +0000994 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +0000995 unsigned Op = Subtarget.hasP9Vector() ? PPC::STXVX : PPC::STXVD2X;
996 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op))
Hal Finkel27774d92014-03-13 07:58:58 +0000997 .addReg(SrcReg,
998 getKillRegState(isKill)),
999 FrameIdx));
1000 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +00001001 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00001002 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFSTOREf64 : PPC::STXSDX;
1003 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc))
Hal Finkel19be5062014-03-29 05:29:01 +00001004 .addReg(SrcReg,
1005 getKillRegState(isKill)),
1006 FrameIdx));
1007 NonRI = true;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001008 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00001009 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFSTOREf32 : PPC::STXSSPX;
1010 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc))
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001011 .addReg(SrcReg,
1012 getKillRegState(isKill)),
1013 FrameIdx));
1014 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +00001015 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001016 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +00001017 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +00001018 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
1019 .addReg(SrcReg,
1020 getKillRegState(isKill)),
1021 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001022 SpillsVRS = true;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001023 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
1024 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDX))
1025 .addReg(SrcReg,
1026 getKillRegState(isKill)),
1027 FrameIdx));
1028 NonRI = true;
1029 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
1030 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFSXs))
1031 .addReg(SrcReg,
1032 getKillRegState(isKill)),
1033 FrameIdx));
1034 NonRI = true;
1035 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
1036 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDXb))
1037 .addReg(SrcReg,
1038 getKillRegState(isKill)),
1039 FrameIdx));
1040 NonRI = true;
Owen Andersoneee14602008-01-01 21:11:32 +00001041 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001042 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +00001043 }
Bill Wendling632ea652008-03-03 22:19:16 +00001044
1045 return false;
Owen Andersoneee14602008-01-01 21:11:32 +00001046}
1047
1048void
1049PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +00001050 MachineBasicBlock::iterator MI,
1051 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +00001052 const TargetRegisterClass *RC,
1053 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +00001054 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +00001055 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +00001056
Hal Finkelbb420f12013-03-15 05:06:04 +00001057 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1058 FuncInfo->setHasSpills();
1059
Nemanja Ivanovic11049f82016-10-04 06:59:23 +00001060 // We need to avoid a situation in which the value from a VRRC register is
1061 // spilled using an Altivec instruction and reloaded into a VSRC register
1062 // using a VSX instruction. The issue with this is that the VSX
1063 // load/store instructions swap the doublewords in the vector and the Altivec
1064 // ones don't. The register classes on the spill/reload may be different if
1065 // the register is defined using an Altivec instruction and is then used by a
1066 // VSX instruction.
1067 RC = updatedRC(RC);
1068
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001069 bool NonRI = false, SpillsVRS = false;
1070 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
1071 NonRI, SpillsVRS))
Bill Wendling632ea652008-03-03 22:19:16 +00001072 FuncInfo->setSpillsCR();
Bill Wendling632ea652008-03-03 22:19:16 +00001073
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001074 if (SpillsVRS)
1075 FuncInfo->setSpillsVRSAVE();
1076
Hal Finkelfcc51d42013-03-17 04:43:44 +00001077 if (NonRI)
1078 FuncInfo->setHasNonRISpills();
1079
Owen Andersoneee14602008-01-01 21:11:32 +00001080 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1081 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001082
Matthias Braun941a7052016-07-28 18:40:00 +00001083 const MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001084 MachineMemOperand *MMO = MF.getMachineMemOperand(
1085 MachinePointerInfo::getFixedStack(MF, FrameIdx),
1086 MachineMemOperand::MOStore, MFI.getObjectSize(FrameIdx),
1087 MFI.getObjectAlignment(FrameIdx));
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001088 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +00001089}
1090
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001091bool PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, const DebugLoc &DL,
1092 unsigned DestReg, int FrameIdx,
1093 const TargetRegisterClass *RC,
1094 SmallVectorImpl<MachineInstr *> &NewMIs,
1095 bool &NonRI, bool &SpillsVRS) const {
Hal Finkel37714b82013-03-27 21:21:15 +00001096 // Note: If additional load instructions are added here,
1097 // update isLoadFromStackSlot.
1098
Hal Finkel4e703bc2014-01-28 05:32:58 +00001099 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
1100 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +00001101 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
1102 DestReg), FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +00001103 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
1104 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +00001105 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
1106 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001107 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001108 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +00001109 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001110 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001111 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +00001112 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001113 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +00001114 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1115 get(PPC::RESTORE_CR), DestReg),
1116 FrameIdx));
1117 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001118 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +00001119 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1120 get(PPC::RESTORE_CRBIT), DestReg),
1121 FrameIdx));
1122 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001123 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +00001124 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
1125 FrameIdx));
1126 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +00001127 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +00001128 unsigned Op = Subtarget.hasP9Vector() ? PPC::LXVX : PPC::LXVD2X;
1129 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op), DestReg),
Hal Finkel27774d92014-03-13 07:58:58 +00001130 FrameIdx));
1131 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +00001132 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00001133 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFLOADf64 : PPC::LXSDX;
1134 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc),
1135 DestReg), FrameIdx));
Hal Finkel19be5062014-03-29 05:29:01 +00001136 NonRI = true;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001137 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00001138 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFLOADf32 : PPC::LXSSPX;
1139 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc),
1140 DestReg), FrameIdx));
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001141 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +00001142 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001143 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +00001144 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +00001145 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1146 get(PPC::RESTORE_VRSAVE),
1147 DestReg),
1148 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001149 SpillsVRS = true;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001150 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
1151 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDX), DestReg),
1152 FrameIdx));
1153 NonRI = true;
1154 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
1155 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFSXs), DestReg),
1156 FrameIdx));
1157 NonRI = true;
1158 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
1159 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDXb), DestReg),
1160 FrameIdx));
1161 NonRI = true;
Owen Andersoneee14602008-01-01 21:11:32 +00001162 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001163 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +00001164 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001165
1166 return false;
Owen Andersoneee14602008-01-01 21:11:32 +00001167}
1168
1169void
1170PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +00001171 MachineBasicBlock::iterator MI,
1172 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +00001173 const TargetRegisterClass *RC,
1174 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +00001175 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +00001176 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +00001177 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001178 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelfcc51d42013-03-17 04:43:44 +00001179
1180 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1181 FuncInfo->setHasSpills();
1182
Nemanja Ivanovic11049f82016-10-04 06:59:23 +00001183 // We need to avoid a situation in which the value from a VRRC register is
1184 // spilled using an Altivec instruction and reloaded into a VSRC register
1185 // using a VSX instruction. The issue with this is that the VSX
1186 // load/store instructions swap the doublewords in the vector and the Altivec
1187 // ones don't. The register classes on the spill/reload may be different if
1188 // the register is defined using an Altivec instruction and is then used by a
1189 // VSX instruction.
1190 if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass)
1191 RC = &PPC::VSRCRegClass;
1192
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001193 bool NonRI = false, SpillsVRS = false;
1194 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
1195 NonRI, SpillsVRS))
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001196 FuncInfo->setSpillsCR();
Hal Finkelfcc51d42013-03-17 04:43:44 +00001197
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001198 if (SpillsVRS)
1199 FuncInfo->setSpillsVRSAVE();
1200
Hal Finkelfcc51d42013-03-17 04:43:44 +00001201 if (NonRI)
1202 FuncInfo->setHasNonRISpills();
1203
Owen Andersoneee14602008-01-01 21:11:32 +00001204 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1205 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001206
Matthias Braun941a7052016-07-28 18:40:00 +00001207 const MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001208 MachineMemOperand *MMO = MF.getMachineMemOperand(
1209 MachinePointerInfo::getFixedStack(MF, FrameIdx),
1210 MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIdx),
1211 MFI.getObjectAlignment(FrameIdx));
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001212 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +00001213}
1214
Chris Lattnera47294ed2006-10-13 21:21:17 +00001215bool PPCInstrInfo::
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001216reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +00001217 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel96c2d4d2012-06-08 15:38:21 +00001218 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
1219 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
1220 else
1221 // Leave the CR# the same, but invert the condition.
1222 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +00001223 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +00001224}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001225
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001226bool PPCInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
1227 unsigned Reg, MachineRegisterInfo *MRI) const {
Hal Finkeld61d4f82013-04-06 19:30:30 +00001228 // For some instructions, it is legal to fold ZERO into the RA register field.
1229 // A zero immediate should always be loaded with a single li.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001230 unsigned DefOpc = DefMI.getOpcode();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001231 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
1232 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001233 if (!DefMI.getOperand(1).isImm())
Hal Finkeld61d4f82013-04-06 19:30:30 +00001234 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001235 if (DefMI.getOperand(1).getImm() != 0)
Hal Finkeld61d4f82013-04-06 19:30:30 +00001236 return false;
1237
1238 // Note that we cannot here invert the arguments of an isel in order to fold
1239 // a ZERO into what is presented as the second argument. All we have here
1240 // is the condition bit, and that might come from a CR-logical bit operation.
1241
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001242 const MCInstrDesc &UseMCID = UseMI.getDesc();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001243
1244 // Only fold into real machine instructions.
1245 if (UseMCID.isPseudo())
1246 return false;
1247
1248 unsigned UseIdx;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001249 for (UseIdx = 0; UseIdx < UseMI.getNumOperands(); ++UseIdx)
1250 if (UseMI.getOperand(UseIdx).isReg() &&
1251 UseMI.getOperand(UseIdx).getReg() == Reg)
Hal Finkeld61d4f82013-04-06 19:30:30 +00001252 break;
1253
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001254 assert(UseIdx < UseMI.getNumOperands() && "Cannot find Reg in UseMI");
Hal Finkeld61d4f82013-04-06 19:30:30 +00001255 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
1256
1257 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
1258
1259 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
1260 // register (which might also be specified as a pointer class kind).
1261 if (UseInfo->isLookupPtrRegClass()) {
1262 if (UseInfo->RegClass /* Kind */ != 1)
1263 return false;
1264 } else {
1265 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
1266 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
1267 return false;
1268 }
1269
1270 // Make sure this is not tied to an output register (or otherwise
1271 // constrained). This is true for ST?UX registers, for example, which
1272 // are tied to their output registers.
1273 if (UseInfo->Constraints != 0)
1274 return false;
1275
1276 unsigned ZeroReg;
1277 if (UseInfo->isLookupPtrRegClass()) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001278 bool isPPC64 = Subtarget.isPPC64();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001279 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
1280 } else {
1281 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
1282 PPC::ZERO8 : PPC::ZERO;
1283 }
1284
1285 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001286 UseMI.getOperand(UseIdx).setReg(ZeroReg);
Hal Finkeld61d4f82013-04-06 19:30:30 +00001287
1288 if (DeleteDef)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001289 DefMI.eraseFromParent();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001290
1291 return true;
1292}
1293
Hal Finkel30ae2292013-04-10 18:30:16 +00001294static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
1295 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1296 I != IE; ++I)
1297 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
1298 return true;
1299 return false;
1300}
1301
1302// We should make sure that, if we're going to predicate both sides of a
1303// condition (a diamond), that both sides don't define the counter register. We
1304// can predicate counter-decrement-based branches, but while that predicates
1305// the branching, it does not predicate the counter decrement. If we tried to
1306// merge the triangle into one predicated block, we'd decrement the counter
1307// twice.
1308bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
1309 unsigned NumT, unsigned ExtraT,
1310 MachineBasicBlock &FMBB,
1311 unsigned NumF, unsigned ExtraF,
Cong Houc536bd92015-09-10 23:10:42 +00001312 BranchProbability Probability) const {
Hal Finkel30ae2292013-04-10 18:30:16 +00001313 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
1314}
1315
1316
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001317bool PPCInstrInfo::isPredicated(const MachineInstr &MI) const {
Hal Finkelf29285a2013-04-11 01:23:34 +00001318 // The predicated branches are identified by their type, not really by the
1319 // explicit presence of a predicate. Furthermore, some of them can be
1320 // predicated more than once. Because if conversion won't try to predicate
1321 // any instruction which already claims to be predicated (by returning true
1322 // here), always return false. In doing so, we let isPredicable() be the
1323 // final word on whether not the instruction can be (further) predicated.
1324
1325 return false;
Hal Finkel5711eca2013-04-09 22:58:37 +00001326}
1327
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001328bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const {
1329 if (!MI.isTerminator())
Hal Finkel5711eca2013-04-09 22:58:37 +00001330 return false;
1331
1332 // Conditional branch is a special case.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001333 if (MI.isBranch() && !MI.isBarrier())
Hal Finkel5711eca2013-04-09 22:58:37 +00001334 return true;
1335
1336 return !isPredicated(MI);
1337}
1338
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001339bool PPCInstrInfo::PredicateInstruction(MachineInstr &MI,
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001340 ArrayRef<MachineOperand> Pred) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001341 unsigned OpC = MI.getOpcode();
Hal Finkelf4a22c02015-01-13 17:47:54 +00001342 if (OpC == PPC::BLR || OpC == PPC::BLR8) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001343 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001344 bool isPPC64 = Subtarget.isPPC64();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001345 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR)
1346 : (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
Hal Finkel940ab932014-02-28 00:27:01 +00001347 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001348 MI.setDesc(get(PPC::BCLR));
1349 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1350 .addReg(Pred[1].getReg());
Hal Finkel940ab932014-02-28 00:27:01 +00001351 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001352 MI.setDesc(get(PPC::BCLRn));
1353 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1354 .addReg(Pred[1].getReg());
Hal Finkel940ab932014-02-28 00:27:01 +00001355 } else {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001356 MI.setDesc(get(PPC::BCCLR));
1357 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1358 .addImm(Pred[0].getImm())
1359 .addReg(Pred[1].getReg());
Hal Finkel5711eca2013-04-09 22:58:37 +00001360 }
1361
1362 return true;
1363 } else if (OpC == PPC::B) {
1364 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001365 bool isPPC64 = Subtarget.isPPC64();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001366 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ)
1367 : (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
Hal Finkel940ab932014-02-28 00:27:01 +00001368 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001369 MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1370 MI.RemoveOperand(0);
Hal Finkel940ab932014-02-28 00:27:01 +00001371
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001372 MI.setDesc(get(PPC::BC));
1373 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1374 .addReg(Pred[1].getReg())
1375 .addMBB(MBB);
Hal Finkel940ab932014-02-28 00:27:01 +00001376 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001377 MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1378 MI.RemoveOperand(0);
Hal Finkel940ab932014-02-28 00:27:01 +00001379
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001380 MI.setDesc(get(PPC::BCn));
1381 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1382 .addReg(Pred[1].getReg())
1383 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001384 } else {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001385 MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1386 MI.RemoveOperand(0);
Hal Finkel5711eca2013-04-09 22:58:37 +00001387
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001388 MI.setDesc(get(PPC::BCC));
1389 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1390 .addImm(Pred[0].getImm())
1391 .addReg(Pred[1].getReg())
1392 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001393 }
1394
1395 return true;
Hal Finkel500b0042013-04-10 06:42:34 +00001396 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1397 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1398 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1399 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1400
1401 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
Eric Christopher1dcea732014-06-12 21:48:52 +00001402 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel940ab932014-02-28 00:27:01 +00001403
1404 if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001405 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8)
1406 : (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1407 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1408 .addReg(Pred[1].getReg());
Hal Finkel940ab932014-02-28 00:27:01 +00001409 return true;
1410 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001411 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n)
1412 : (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1413 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1414 .addReg(Pred[1].getReg());
Hal Finkel940ab932014-02-28 00:27:01 +00001415 return true;
1416 }
1417
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001418 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8)
1419 : (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
1420 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1421 .addImm(Pred[0].getImm())
1422 .addReg(Pred[1].getReg());
Hal Finkel500b0042013-04-10 06:42:34 +00001423 return true;
Hal Finkel5711eca2013-04-09 22:58:37 +00001424 }
1425
1426 return false;
1427}
1428
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001429bool PPCInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1430 ArrayRef<MachineOperand> Pred2) const {
Hal Finkel5711eca2013-04-09 22:58:37 +00001431 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1432 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1433
1434 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1435 return false;
1436 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1437 return false;
1438
Hal Finkel94a6f382013-12-11 23:12:25 +00001439 // P1 can only subsume P2 if they test the same condition register.
1440 if (Pred1[1].getReg() != Pred2[1].getReg())
1441 return false;
1442
Hal Finkel5711eca2013-04-09 22:58:37 +00001443 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1444 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1445
1446 if (P1 == P2)
1447 return true;
1448
1449 // Does P1 subsume P2, e.g. GE subsumes GT.
1450 if (P1 == PPC::PRED_LE &&
1451 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1452 return true;
1453 if (P1 == PPC::PRED_GE &&
1454 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1455 return true;
1456
1457 return false;
1458}
1459
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001460bool PPCInstrInfo::DefinesPredicate(MachineInstr &MI,
Hal Finkel5711eca2013-04-09 22:58:37 +00001461 std::vector<MachineOperand> &Pred) const {
1462 // Note: At the present time, the contents of Pred from this function is
1463 // unused by IfConversion. This implementation follows ARM by pushing the
1464 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1465 // predicate, instructions defining CTR or CTR8 are also included as
1466 // predicate-defining instructions.
1467
1468 const TargetRegisterClass *RCs[] =
1469 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1470 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1471
1472 bool Found = false;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001473 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1474 const MachineOperand &MO = MI.getOperand(i);
Hal Finkelaf822012013-04-10 07:17:47 +00001475 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001476 const TargetRegisterClass *RC = RCs[c];
Hal Finkelaf822012013-04-10 07:17:47 +00001477 if (MO.isReg()) {
1478 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001479 Pred.push_back(MO);
1480 Found = true;
1481 }
Hal Finkelaf822012013-04-10 07:17:47 +00001482 } else if (MO.isRegMask()) {
1483 for (TargetRegisterClass::iterator I = RC->begin(),
1484 IE = RC->end(); I != IE; ++I)
1485 if (MO.clobbersPhysReg(*I)) {
1486 Pred.push_back(MO);
1487 Found = true;
1488 }
Hal Finkel5711eca2013-04-09 22:58:37 +00001489 }
1490 }
1491 }
1492
1493 return Found;
1494}
1495
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001496bool PPCInstrInfo::isPredicable(MachineInstr &MI) const {
1497 unsigned OpC = MI.getOpcode();
Hal Finkel5711eca2013-04-09 22:58:37 +00001498 switch (OpC) {
1499 default:
1500 return false;
1501 case PPC::B:
1502 case PPC::BLR:
Hal Finkelf4a22c02015-01-13 17:47:54 +00001503 case PPC::BLR8:
Hal Finkel500b0042013-04-10 06:42:34 +00001504 case PPC::BCTR:
1505 case PPC::BCTR8:
1506 case PPC::BCTRL:
1507 case PPC::BCTRL8:
Hal Finkel5711eca2013-04-09 22:58:37 +00001508 return true;
1509 }
1510}
1511
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001512bool PPCInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1513 unsigned &SrcReg2, int &Mask,
1514 int &Value) const {
1515 unsigned Opc = MI.getOpcode();
Hal Finkel82656cb2013-04-18 22:15:08 +00001516
1517 switch (Opc) {
1518 default: return false;
1519 case PPC::CMPWI:
1520 case PPC::CMPLWI:
1521 case PPC::CMPDI:
1522 case PPC::CMPLDI:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001523 SrcReg = MI.getOperand(1).getReg();
Hal Finkel82656cb2013-04-18 22:15:08 +00001524 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001525 Value = MI.getOperand(2).getImm();
Hal Finkel82656cb2013-04-18 22:15:08 +00001526 Mask = 0xFFFF;
1527 return true;
1528 case PPC::CMPW:
1529 case PPC::CMPLW:
1530 case PPC::CMPD:
1531 case PPC::CMPLD:
1532 case PPC::FCMPUS:
1533 case PPC::FCMPUD:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001534 SrcReg = MI.getOperand(1).getReg();
1535 SrcReg2 = MI.getOperand(2).getReg();
Hal Finkel82656cb2013-04-18 22:15:08 +00001536 return true;
1537 }
1538}
Hal Finkele6322392013-04-19 22:08:38 +00001539
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001540bool PPCInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, unsigned SrcReg,
1541 unsigned SrcReg2, int Mask, int Value,
Hal Finkel82656cb2013-04-18 22:15:08 +00001542 const MachineRegisterInfo *MRI) const {
Hal Finkelb12da6b2013-04-18 22:54:25 +00001543 if (DisableCmpOpt)
1544 return false;
1545
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001546 int OpC = CmpInstr.getOpcode();
1547 unsigned CRReg = CmpInstr.getOperand(0).getReg();
Hal Finkel08e53ee2013-05-08 12:16:14 +00001548
1549 // FP record forms set CR1 based on the execption status bits, not a
1550 // comparison with zero.
1551 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1552 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001553
1554 // The record forms set the condition register based on a signed comparison
1555 // with zero (so says the ISA manual). This is not as straightforward as it
1556 // seems, however, because this is always a 64-bit comparison on PPC64, even
1557 // for instructions that are 32-bit in nature (like slw for example).
1558 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1559 // for equality checks (as those don't depend on the sign). On PPC64,
1560 // we are restricted to equality for unsigned 64-bit comparisons and for
1561 // signed 32-bit comparisons the applicability is more restricted.
Eric Christopher1dcea732014-06-12 21:48:52 +00001562 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel82656cb2013-04-18 22:15:08 +00001563 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1564 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1565 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1566
1567 // Get the unique definition of SrcReg.
1568 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1569 if (!MI) return false;
1570 int MIOpC = MI->getOpcode();
1571
1572 bool equalityOnly = false;
1573 bool noSub = false;
1574 if (isPPC64) {
1575 if (is32BitSignedCompare) {
1576 // We can perform this optimization only if MI is sign-extending.
1577 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1578 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1579 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1580 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1581 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1582 noSub = true;
1583 } else
1584 return false;
1585 } else if (is32BitUnsignedCompare) {
Kyle Butt61311282016-03-23 19:51:22 +00001586 // 32-bit rotate and mask instructions are zero extending only if MB <= ME
1587 bool isZeroExtendingRotate =
1588 (MIOpC == PPC::RLWINM || MIOpC == PPC::RLWINMo ||
1589 MIOpC == PPC::RLWNM || MIOpC == PPC::RLWNMo)
1590 && MI->getOperand(3).getImm() <= MI->getOperand(4).getImm();
1591
Hal Finkel82656cb2013-04-18 22:15:08 +00001592 // We can perform this optimization, equality only, if MI is
1593 // zero-extending.
1594 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1595 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
Kyle Butt61311282016-03-23 19:51:22 +00001596 MIOpC == PPC::SRW || MIOpC == PPC::SRWo ||
1597 isZeroExtendingRotate) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001598 noSub = true;
1599 equalityOnly = true;
1600 } else
1601 return false;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001602 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001603 equalityOnly = is64BitUnsignedCompare;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001604 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001605 equalityOnly = is32BitUnsignedCompare;
1606
1607 if (equalityOnly) {
1608 // We need to check the uses of the condition register in order to reject
1609 // non-equality comparisons.
Owen Anderson16c6bf42014-03-13 23:12:04 +00001610 for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg),
1611 IE = MRI->use_instr_end(); I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001612 MachineInstr *UseMI = &*I;
1613 if (UseMI->getOpcode() == PPC::BCC) {
1614 unsigned Pred = UseMI->getOperand(0).getImm();
Hal Finkelc3632452013-05-07 17:49:55 +00001615 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE)
1616 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001617 } else if (UseMI->getOpcode() == PPC::ISEL ||
1618 UseMI->getOpcode() == PPC::ISEL8) {
1619 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkelc3632452013-05-07 17:49:55 +00001620 if (SubIdx != PPC::sub_eq)
1621 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001622 } else
1623 return false;
1624 }
1625 }
1626
Hal Finkelc3632452013-05-07 17:49:55 +00001627 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001628
1629 // Scan forward to find the first use of the compare.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001630 for (MachineBasicBlock::iterator EL = CmpInstr.getParent()->end(); I != EL;
1631 ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001632 bool FoundUse = false;
Owen Anderson16c6bf42014-03-13 23:12:04 +00001633 for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg),
1634 JE = MRI->use_instr_end(); J != JE; ++J)
Hal Finkel82656cb2013-04-18 22:15:08 +00001635 if (&*J == &*I) {
1636 FoundUse = true;
1637 break;
1638 }
1639
1640 if (FoundUse)
1641 break;
1642 }
1643
Hal Finkel82656cb2013-04-18 22:15:08 +00001644 // There are two possible candidates which can be changed to set CR[01].
1645 // One is MI, the other is a SUB instruction.
1646 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
Craig Topper062a2ba2014-04-25 05:30:21 +00001647 MachineInstr *Sub = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001648 if (SrcReg2 != 0)
1649 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00001650 MI = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001651 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1652 // same BB as the comparison. This is to allow the check below to avoid calls
1653 // (and other explicit clobbers); instead we should really check for these
1654 // more explicitly (in at least a few predecessors).
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001655 else if (MI->getParent() != CmpInstr.getParent() || Value != 0) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001656 // PPC does not have a record-form SUBri.
1657 return false;
1658 }
1659
1660 // Search for Sub.
1661 const TargetRegisterInfo *TRI = &getRegisterInfo();
1662 --I;
Hal Finkelc3632452013-05-07 17:49:55 +00001663
1664 // Get ready to iterate backward from CmpInstr.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001665 MachineBasicBlock::iterator E = MI, B = CmpInstr.getParent()->begin();
Hal Finkelc3632452013-05-07 17:49:55 +00001666
Hal Finkel82656cb2013-04-18 22:15:08 +00001667 for (; I != E && !noSub; --I) {
1668 const MachineInstr &Instr = *I;
1669 unsigned IOpC = Instr.getOpcode();
1670
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001671 if (&*I != &CmpInstr && (Instr.modifiesRegister(PPC::CR0, TRI) ||
1672 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel82656cb2013-04-18 22:15:08 +00001673 // This instruction modifies or uses the record condition register after
1674 // the one we want to change. While we could do this transformation, it
1675 // would likely not be profitable. This transformation removes one
1676 // instruction, and so even forcing RA to generate one move probably
1677 // makes it unprofitable.
1678 return false;
1679
1680 // Check whether CmpInstr can be made redundant by the current instruction.
1681 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1682 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1683 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1684 ((Instr.getOperand(1).getReg() == SrcReg &&
1685 Instr.getOperand(2).getReg() == SrcReg2) ||
1686 (Instr.getOperand(1).getReg() == SrcReg2 &&
1687 Instr.getOperand(2).getReg() == SrcReg))) {
1688 Sub = &*I;
1689 break;
1690 }
1691
Hal Finkel82656cb2013-04-18 22:15:08 +00001692 if (I == B)
1693 // The 'and' is below the comparison instruction.
1694 return false;
1695 }
1696
1697 // Return false if no candidates exist.
1698 if (!MI && !Sub)
1699 return false;
1700
1701 // The single candidate is called MI.
1702 if (!MI) MI = Sub;
1703
1704 int NewOpC = -1;
1705 MIOpC = MI->getOpcode();
1706 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1707 NewOpC = MIOpC;
1708 else {
1709 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1710 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1711 NewOpC = MIOpC;
1712 }
1713
1714 // FIXME: On the non-embedded POWER architectures, only some of the record
1715 // forms are fast, and we should use only the fast ones.
1716
1717 // The defining instruction has a record form (or is already a record
1718 // form). It is possible, however, that we'll need to reverse the condition
1719 // code of the users.
1720 if (NewOpC == -1)
1721 return false;
1722
Hal Finkele6322392013-04-19 22:08:38 +00001723 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1724 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
Hal Finkel82656cb2013-04-18 22:15:08 +00001725
1726 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1727 // needs to be updated to be based on SUB. Push the condition code
1728 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1729 // condition code of these operands will be modified.
1730 bool ShouldSwap = false;
1731 if (Sub) {
1732 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1733 Sub->getOperand(2).getReg() == SrcReg;
1734
1735 // The operands to subf are the opposite of sub, so only in the fixed-point
1736 // case, invert the order.
Hal Finkel08e53ee2013-05-08 12:16:14 +00001737 ShouldSwap = !ShouldSwap;
Hal Finkel82656cb2013-04-18 22:15:08 +00001738 }
1739
1740 if (ShouldSwap)
Owen Anderson16c6bf42014-03-13 23:12:04 +00001741 for (MachineRegisterInfo::use_instr_iterator
1742 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1743 I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001744 MachineInstr *UseMI = &*I;
1745 if (UseMI->getOpcode() == PPC::BCC) {
1746 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hal Finkele6322392013-04-19 22:08:38 +00001747 assert((!equalityOnly ||
1748 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1749 "Invalid predicate for equality-only optimization");
Owen Anderson16c6bf42014-03-13 23:12:04 +00001750 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
Hal Finkel0f64e212013-04-20 05:16:26 +00001751 PPC::getSwappedPredicate(Pred)));
Hal Finkel82656cb2013-04-18 22:15:08 +00001752 } else if (UseMI->getOpcode() == PPC::ISEL ||
1753 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkele6322392013-04-19 22:08:38 +00001754 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1755 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1756 "Invalid CR bit for equality-only optimization");
1757
1758 if (NewSubReg == PPC::sub_lt)
1759 NewSubReg = PPC::sub_gt;
1760 else if (NewSubReg == PPC::sub_gt)
1761 NewSubReg = PPC::sub_lt;
1762
Owen Anderson16c6bf42014-03-13 23:12:04 +00001763 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
Hal Finkele6322392013-04-19 22:08:38 +00001764 NewSubReg));
Hal Finkel82656cb2013-04-18 22:15:08 +00001765 } else // We need to abort on a user we don't understand.
1766 return false;
1767 }
1768
1769 // Create a new virtual register to hold the value of the CR set by the
1770 // record-form instruction. If the instruction was not previously in
1771 // record form, then set the kill flag on the CR.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001772 CmpInstr.eraseFromParent();
Hal Finkel82656cb2013-04-18 22:15:08 +00001773
1774 MachineBasicBlock::iterator MII = MI;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001775 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
Hal Finkel82656cb2013-04-18 22:15:08 +00001776 get(TargetOpcode::COPY), CRReg)
Hal Finkel08e53ee2013-05-08 12:16:14 +00001777 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel82656cb2013-04-18 22:15:08 +00001778
Chuang-Yu Cheng94f58e72016-04-12 03:10:52 +00001779 // Even if CR0 register were dead before, it is alive now since the
1780 // instruction we just built uses it.
1781 MI->clearRegisterDeads(PPC::CR0);
1782
Hal Finkel82656cb2013-04-18 22:15:08 +00001783 if (MIOpC != NewOpC) {
1784 // We need to be careful here: we're replacing one instruction with
1785 // another, and we need to make sure that we get all of the right
1786 // implicit uses and defs. On the other hand, the caller may be holding
1787 // an iterator to this instruction, and so we can't delete it (this is
1788 // specifically the case if this is the instruction directly after the
1789 // compare).
1790
1791 const MCInstrDesc &NewDesc = get(NewOpC);
1792 MI->setDesc(NewDesc);
1793
1794 if (NewDesc.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +00001795 for (const MCPhysReg *ImpDefs = NewDesc.getImplicitDefs();
Hal Finkel82656cb2013-04-18 22:15:08 +00001796 *ImpDefs; ++ImpDefs)
1797 if (!MI->definesRegister(*ImpDefs))
1798 MI->addOperand(*MI->getParent()->getParent(),
1799 MachineOperand::CreateReg(*ImpDefs, true, true));
1800 if (NewDesc.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +00001801 for (const MCPhysReg *ImpUses = NewDesc.getImplicitUses();
Hal Finkel82656cb2013-04-18 22:15:08 +00001802 *ImpUses; ++ImpUses)
1803 if (!MI->readsRegister(*ImpUses))
1804 MI->addOperand(*MI->getParent()->getParent(),
1805 MachineOperand::CreateReg(*ImpUses, false, true));
1806 }
Keno Fischer55734832016-06-01 20:31:07 +00001807 assert(MI->definesRegister(PPC::CR0) &&
1808 "Record-form instruction does not define cr0?");
Hal Finkel82656cb2013-04-18 22:15:08 +00001809
1810 // Modify the condition code of operands in OperandsToUpdate.
1811 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1812 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkele6322392013-04-19 22:08:38 +00001813 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1814 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001815
Hal Finkele6322392013-04-19 22:08:38 +00001816 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1817 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001818
1819 return true;
1820}
1821
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001822/// GetInstSize - Return the number of bytes of code the specified
1823/// instruction may be. This returns the maximum number of bytes.
1824///
Sjoerd Meijer89217f82016-07-28 16:32:22 +00001825unsigned PPCInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001826 unsigned Opcode = MI.getOpcode();
Hal Finkela7bbaf62014-02-02 06:12:27 +00001827
1828 if (Opcode == PPC::INLINEASM) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001829 const MachineFunction *MF = MI.getParent()->getParent();
1830 const char *AsmStr = MI.getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +00001831 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Hal Finkel934361a2015-01-14 01:07:51 +00001832 } else if (Opcode == TargetOpcode::STACKMAP) {
Philip Reamese83c4b32016-08-23 23:33:29 +00001833 StackMapOpers Opers(&MI);
1834 return Opers.getNumPatchBytes();
Hal Finkel934361a2015-01-14 01:07:51 +00001835 } else if (Opcode == TargetOpcode::PATCHPOINT) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001836 PatchPointOpers Opers(&MI);
Philip Reamese83c4b32016-08-23 23:33:29 +00001837 return Opers.getNumPatchBytes();
Hal Finkela7bbaf62014-02-02 06:12:27 +00001838 } else {
1839 const MCInstrDesc &Desc = get(Opcode);
1840 return Desc.getSize();
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001841 }
1842}
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001843
Hal Finkel2d556982015-08-30 07:50:35 +00001844std::pair<unsigned, unsigned>
1845PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
1846 const unsigned Mask = PPCII::MO_ACCESS_MASK;
1847 return std::make_pair(TF & Mask, TF & ~Mask);
1848}
1849
1850ArrayRef<std::pair<unsigned, const char *>>
1851PPCInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
1852 using namespace PPCII;
Hal Finkel982e8d42015-08-30 08:07:29 +00001853 static const std::pair<unsigned, const char *> TargetFlags[] = {
Hal Finkel2d556982015-08-30 07:50:35 +00001854 {MO_LO, "ppc-lo"},
1855 {MO_HA, "ppc-ha"},
1856 {MO_TPREL_LO, "ppc-tprel-lo"},
1857 {MO_TPREL_HA, "ppc-tprel-ha"},
1858 {MO_DTPREL_LO, "ppc-dtprel-lo"},
1859 {MO_TLSLD_LO, "ppc-tlsld-lo"},
1860 {MO_TOC_LO, "ppc-toc-lo"},
1861 {MO_TLS, "ppc-tls"}};
1862 return makeArrayRef(TargetFlags);
1863}
1864
1865ArrayRef<std::pair<unsigned, const char *>>
1866PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
1867 using namespace PPCII;
Hal Finkel982e8d42015-08-30 08:07:29 +00001868 static const std::pair<unsigned, const char *> TargetFlags[] = {
Rafael Espindolaa99ccfc2016-06-29 14:59:50 +00001869 {MO_PLT, "ppc-plt"},
Hal Finkel2d556982015-08-30 07:50:35 +00001870 {MO_PIC_FLAG, "ppc-pic"},
1871 {MO_NLP_FLAG, "ppc-nlp"},
1872 {MO_NLP_HIDDEN_FLAG, "ppc-nlp-hidden"}};
1873 return makeArrayRef(TargetFlags);
1874}
1875
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001876bool PPCInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1877 switch (MI.getOpcode()) {
Tim Shena1d8bc52016-04-19 20:14:52 +00001878 case TargetOpcode::LOAD_STACK_GUARD: {
1879 assert(Subtarget.isTargetLinux() &&
1880 "Only Linux target is expected to contain LOAD_STACK_GUARD");
1881 const int64_t Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008;
1882 const unsigned Reg = Subtarget.isPPC64() ? PPC::X13 : PPC::R2;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001883 MI.setDesc(get(Subtarget.isPPC64() ? PPC::LD : PPC::LWZ));
1884 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
Tim Shena1d8bc52016-04-19 20:14:52 +00001885 .addImm(Offset)
1886 .addReg(Reg);
1887 return true;
1888 }
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00001889 case PPC::DFLOADf32:
1890 case PPC::DFLOADf64:
1891 case PPC::DFSTOREf32:
1892 case PPC::DFSTOREf64: {
1893 assert(Subtarget.hasP9Vector() &&
1894 "Invalid D-Form Pseudo-ops on non-P9 target.");
1895 unsigned UpperOpcode, LowerOpcode;
1896 switch (MI.getOpcode()) {
1897 case PPC::DFLOADf32:
1898 UpperOpcode = PPC::LXSSP;
1899 LowerOpcode = PPC::LFS;
1900 break;
1901 case PPC::DFLOADf64:
1902 UpperOpcode = PPC::LXSD;
1903 LowerOpcode = PPC::LFD;
1904 break;
1905 case PPC::DFSTOREf32:
1906 UpperOpcode = PPC::STXSSP;
1907 LowerOpcode = PPC::STFS;
1908 break;
1909 case PPC::DFSTOREf64:
1910 UpperOpcode = PPC::STXSD;
1911 LowerOpcode = PPC::STFD;
1912 break;
1913 }
1914 unsigned TargetReg = MI.getOperand(0).getReg();
1915 unsigned Opcode;
1916 if ((TargetReg >= PPC::F0 && TargetReg <= PPC::F31) ||
1917 (TargetReg >= PPC::VSL0 && TargetReg <= PPC::VSL31))
1918 Opcode = LowerOpcode;
1919 else
1920 Opcode = UpperOpcode;
1921 MI.setDesc(get(Opcode));
1922 return true;
1923 }
Tim Shena1d8bc52016-04-19 20:14:52 +00001924 }
1925 return false;
1926}
Nemanja Ivanovic11049f82016-10-04 06:59:23 +00001927
1928const TargetRegisterClass *
1929PPCInstrInfo::updatedRC(const TargetRegisterClass *RC) const {
1930 if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass)
1931 return &PPC::VSRCRegClass;
1932 return RC;
1933}