blob: 80530ec19bef6750639cf381df1c085cd88e78d5 [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
Zaara Syedafcd96972017-09-21 16:12:33 +000049STATISTIC(NumStoreSPILLVSRRCAsVec,
50 "Number of spillvsrrc spilled to stack as vec");
51STATISTIC(NumStoreSPILLVSRRCAsGpr,
52 "Number of spillvsrrc spilled to stack as gpr");
53STATISTIC(NumGPRtoVSRSpill, "Number of gpr spills to spillvsrrc");
54
Hal Finkel821e0012012-06-08 15:38:25 +000055static cl::
Hal Finkelc6b5deb2012-06-08 19:19:53 +000056opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
57 cl::desc("Disable analysis for CTR loops"));
Hal Finkel821e0012012-06-08 15:38:25 +000058
Hal Finkele6322392013-04-19 22:08:38 +000059static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt",
Hal Finkelb12da6b2013-04-18 22:54:25 +000060cl::desc("Disable compare instruction optimization"), cl::Hidden);
61
Hal Finkel9dcb3582014-03-27 22:46:28 +000062static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy",
63cl::desc("Causes the backend to crash instead of generating a nop VSX copy"),
64cl::Hidden);
65
Hal Finkel8acae522015-07-14 20:02:02 +000066static cl::opt<bool>
67UseOldLatencyCalc("ppc-old-latency-calc", cl::Hidden,
68 cl::desc("Use the old (incorrect) instruction latency calculation"));
69
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000070// Pin the vtable to this file.
71void PPCInstrInfo::anchor() {}
72
Eric Christopher1dcea732014-06-12 21:48:52 +000073PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI)
Tim Shen918ed872017-02-10 21:03:24 +000074 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP,
75 /* CatchRetOpcode */ -1,
76 STI.isPPC64() ? PPC::BLR8 : PPC::BLR),
Eric Christopherea178cf2015-03-12 01:42:51 +000077 Subtarget(STI), RI(STI.getTargetMachine()) {}
Chris Lattner49cadab2006-06-17 00:01:04 +000078
Andrew Trick10ffc2b2010-12-24 05:03:26 +000079/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
80/// this target when scheduling the DAG.
Eric Christopherf047bfd2014-06-13 22:38:52 +000081ScheduleHazardRecognizer *
82PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
83 const ScheduleDAG *DAG) const {
84 unsigned Directive =
85 static_cast<const PPCSubtarget *>(STI)->getDarwinDirective();
Hal Finkel742b5352012-08-28 16:12:39 +000086 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
87 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
Eric Christopherf047bfd2014-06-13 22:38:52 +000088 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +000089 static_cast<const PPCSubtarget *>(STI)->getInstrItineraryData();
Hal Finkel563cc052013-12-02 23:52:46 +000090 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel6fa56972011-10-17 04:03:49 +000091 }
Hal Finkel58ca3602011-12-02 04:58:02 +000092
Eric Christopherf047bfd2014-06-13 22:38:52 +000093 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000094}
95
Hal Finkel58ca3602011-12-02 04:58:02 +000096/// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
97/// to use for this target when scheduling the DAG.
Eric Christophercccae792015-01-30 22:02:31 +000098ScheduleHazardRecognizer *
99PPCInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
100 const ScheduleDAG *DAG) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000101 unsigned Directive =
Eric Christophercccae792015-01-30 22:02:31 +0000102 DAG->MF.getSubtarget<PPCSubtarget>().getDarwinDirective();
Hal Finkel58ca3602011-12-02 04:58:02 +0000103
Nemanja Ivanovic6e29baf2016-05-09 18:54:58 +0000104 // FIXME: Leaving this as-is until we have POWER9 scheduling info
Will Schmidt970ff642014-06-26 13:36:19 +0000105 if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8)
Hal Finkelceb1f122013-12-12 00:19:11 +0000106 return new PPCDispatchGroupSBHazardRecognizer(II, DAG);
107
Hal Finkel58ca3602011-12-02 04:58:02 +0000108 // Most subtargets use a PPC970 recognizer.
Hal Finkel742b5352012-08-28 16:12:39 +0000109 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
110 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
Eric Christopher1dcea732014-06-12 21:48:52 +0000111 assert(DAG->TII && "No InstrInfo?");
Hal Finkel58ca3602011-12-02 04:58:02 +0000112
Eric Christopher1dcea732014-06-12 21:48:52 +0000113 return new PPCHazardRecognizer970(*DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000114 }
115
Hal Finkel563cc052013-12-02 23:52:46 +0000116 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000117}
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000118
Hal Finkel8acae522015-07-14 20:02:02 +0000119unsigned PPCInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000120 const MachineInstr &MI,
Hal Finkel8acae522015-07-14 20:02:02 +0000121 unsigned *PredCost) const {
122 if (!ItinData || UseOldLatencyCalc)
123 return PPCGenInstrInfo::getInstrLatency(ItinData, MI, PredCost);
124
125 // The default implementation of getInstrLatency calls getStageLatency, but
126 // getStageLatency does not do the right thing for us. While we have
127 // itinerary, most cores are fully pipelined, and so the itineraries only
128 // express the first part of the pipeline, not every stage. Instead, we need
129 // to use the listed output operand cycle number (using operand 0 here, which
130 // is an output).
131
132 unsigned Latency = 1;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000133 unsigned DefClass = MI.getDesc().getSchedClass();
134 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
135 const MachineOperand &MO = MI.getOperand(i);
Hal Finkel8acae522015-07-14 20:02:02 +0000136 if (!MO.isReg() || !MO.isDef() || MO.isImplicit())
137 continue;
138
139 int Cycle = ItinData->getOperandCycle(DefClass, i);
140 if (Cycle < 0)
141 continue;
142
143 Latency = std::max(Latency, (unsigned) Cycle);
144 }
145
146 return Latency;
147}
Hal Finkelceb1f122013-12-12 00:19:11 +0000148
149int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000150 const MachineInstr &DefMI, unsigned DefIdx,
151 const MachineInstr &UseMI,
Hal Finkelceb1f122013-12-12 00:19:11 +0000152 unsigned UseIdx) const {
153 int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
154 UseMI, UseIdx);
155
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000156 if (!DefMI.getParent())
Hal Finkel5d36b232015-07-15 08:23:05 +0000157 return Latency;
158
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000159 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
Hal Finkelceb1f122013-12-12 00:19:11 +0000160 unsigned Reg = DefMO.getReg();
161
Hal Finkelceb1f122013-12-12 00:19:11 +0000162 bool IsRegCR;
Andrew Kaylor5c73e1f2015-03-24 23:37:10 +0000163 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Hal Finkelceb1f122013-12-12 00:19:11 +0000164 const MachineRegisterInfo *MRI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000165 &DefMI.getParent()->getParent()->getRegInfo();
Hal Finkelceb1f122013-12-12 00:19:11 +0000166 IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) ||
167 MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass);
168 } else {
169 IsRegCR = PPC::CRRCRegClass.contains(Reg) ||
170 PPC::CRBITRCRegClass.contains(Reg);
171 }
172
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000173 if (UseMI.isBranch() && IsRegCR) {
Hal Finkelceb1f122013-12-12 00:19:11 +0000174 if (Latency < 0)
175 Latency = getInstrLatency(ItinData, DefMI);
176
177 // On some cores, there is an additional delay between writing to a condition
178 // register, and using it from a branch.
Eric Christopher1dcea732014-06-12 21:48:52 +0000179 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000180 switch (Directive) {
181 default: break;
182 case PPC::DIR_7400:
183 case PPC::DIR_750:
184 case PPC::DIR_970:
185 case PPC::DIR_E5500:
186 case PPC::DIR_PWR4:
187 case PPC::DIR_PWR5:
188 case PPC::DIR_PWR5X:
189 case PPC::DIR_PWR6:
190 case PPC::DIR_PWR6X:
191 case PPC::DIR_PWR7:
Will Schmidt970ff642014-06-26 13:36:19 +0000192 case PPC::DIR_PWR8:
Nemanja Ivanovic6e29baf2016-05-09 18:54:58 +0000193 // FIXME: Is this needed for POWER9?
Hal Finkelceb1f122013-12-12 00:19:11 +0000194 Latency += 2;
195 break;
196 }
197 }
198
199 return Latency;
200}
201
Hal Finkel5d36b232015-07-15 08:23:05 +0000202// This function does not list all associative and commutative operations, but
203// only those worth feeding through the machine combiner in an attempt to
204// reduce the critical path. Mostly, this means floating-point operations,
205// because they have high latencies (compared to other operations, such and
206// and/or, which are also associative and commutative, but have low latencies).
Chad Rosier03a47302015-09-21 15:09:11 +0000207bool PPCInstrInfo::isAssociativeAndCommutative(const MachineInstr &Inst) const {
208 switch (Inst.getOpcode()) {
Hal Finkel5d36b232015-07-15 08:23:05 +0000209 // FP Add:
210 case PPC::FADD:
211 case PPC::FADDS:
212 // FP Multiply:
213 case PPC::FMUL:
214 case PPC::FMULS:
215 // Altivec Add:
216 case PPC::VADDFP:
217 // VSX Add:
218 case PPC::XSADDDP:
219 case PPC::XVADDDP:
220 case PPC::XVADDSP:
221 case PPC::XSADDSP:
222 // VSX Multiply:
223 case PPC::XSMULDP:
224 case PPC::XVMULDP:
225 case PPC::XVMULSP:
226 case PPC::XSMULSP:
227 // QPX Add:
228 case PPC::QVFADD:
229 case PPC::QVFADDS:
230 case PPC::QVFADDSs:
231 // QPX Multiply:
232 case PPC::QVFMUL:
233 case PPC::QVFMULS:
234 case PPC::QVFMULSs:
235 return true;
236 default:
237 return false;
238 }
239}
240
Chad Rosier03a47302015-09-21 15:09:11 +0000241bool PPCInstrInfo::getMachineCombinerPatterns(
242 MachineInstr &Root,
Sanjay Patel387e66e2015-11-05 19:34:57 +0000243 SmallVectorImpl<MachineCombinerPattern> &Patterns) const {
Hal Finkel5d36b232015-07-15 08:23:05 +0000244 // Using the machine combiner in this way is potentially expensive, so
245 // restrict to when aggressive optimizations are desired.
246 if (Subtarget.getTargetMachine().getOptLevel() != CodeGenOpt::Aggressive)
247 return false;
248
249 // FP reassociation is only legal when we don't need strict IEEE semantics.
250 if (!Root.getParent()->getParent()->getTarget().Options.UnsafeFPMath)
251 return false;
252
Chad Rosier03a47302015-09-21 15:09:11 +0000253 return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns);
Hal Finkel5d36b232015-07-15 08:23:05 +0000254}
255
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000256// Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
257bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
258 unsigned &SrcReg, unsigned &DstReg,
259 unsigned &SubIdx) const {
260 switch (MI.getOpcode()) {
261 default: return false;
262 case PPC::EXTSW:
263 case PPC::EXTSW_32_64:
264 SrcReg = MI.getOperand(1).getReg();
265 DstReg = MI.getOperand(0).getReg();
266 SubIdx = PPC::sub_32;
267 return true;
268 }
269}
270
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000271unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Chris Lattner91400bd2006-03-16 22:24:02 +0000272 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000273 // Note: This list must be kept consistent with LoadRegFromStackSlot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000274 switch (MI.getOpcode()) {
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000275 default: break;
276 case PPC::LD:
277 case PPC::LWZ:
278 case PPC::LFS:
279 case PPC::LFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000280 case PPC::RESTORE_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000281 case PPC::RESTORE_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000282 case PPC::LVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000283 case PPC::LXVD2X:
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +0000284 case PPC::LXVX:
Hal Finkelc93a9a22015-02-25 01:06:45 +0000285 case PPC::QVLFDX:
286 case PPC::QVLFSXs:
287 case PPC::QVLFDXb:
Hal Finkel37714b82013-03-27 21:21:15 +0000288 case PPC::RESTORE_VRSAVE:
Zaara Syedafcd96972017-09-21 16:12:33 +0000289 case PPC::SPILLTOVSR_LD:
Hal Finkel37714b82013-03-27 21:21:15 +0000290 // Check for the operands added by addFrameReference (the immediate is the
291 // offset which defaults to 0).
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000292 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() &&
293 MI.getOperand(2).isFI()) {
294 FrameIndex = MI.getOperand(2).getIndex();
295 return MI.getOperand(0).getReg();
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000296 }
297 break;
298 }
299 return 0;
Chris Lattnerc327d712006-02-02 20:16:12 +0000300}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000301
Lei Huang84dbbfd2017-06-21 17:17:56 +0000302// For opcodes with the ReMaterializable flag set, this function is called to
303// verify the instruction is really rematable.
304bool PPCInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr &MI,
305 AliasAnalysis *AA) const {
306 switch (MI.getOpcode()) {
307 default:
308 // This function should only be called for opcodes with the ReMaterializable
309 // flag set.
310 llvm_unreachable("Unknown rematerializable operation!");
311 break;
312 case PPC::LI:
313 case PPC::LI8:
314 case PPC::LIS:
315 case PPC::LIS8:
316 case PPC::QVGPCI:
317 case PPC::ADDIStocHA:
318 case PPC::ADDItocL:
319 case PPC::LOAD_STACK_GUARD:
320 return true;
321 }
322 return false;
323}
324
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000325unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Chris Lattnerc327d712006-02-02 20:16:12 +0000326 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000327 // Note: This list must be kept consistent with StoreRegToStackSlot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000328 switch (MI.getOpcode()) {
Chris Lattnerc327d712006-02-02 20:16:12 +0000329 default: break;
Nate Begeman4efb3282006-02-02 21:07:50 +0000330 case PPC::STD:
Chris Lattnerc327d712006-02-02 20:16:12 +0000331 case PPC::STW:
332 case PPC::STFS:
333 case PPC::STFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000334 case PPC::SPILL_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000335 case PPC::SPILL_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000336 case PPC::STVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000337 case PPC::STXVD2X:
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +0000338 case PPC::STXVX:
Hal Finkelc93a9a22015-02-25 01:06:45 +0000339 case PPC::QVSTFDX:
340 case PPC::QVSTFSXs:
341 case PPC::QVSTFDXb:
Hal Finkel37714b82013-03-27 21:21:15 +0000342 case PPC::SPILL_VRSAVE:
Zaara Syedafcd96972017-09-21 16:12:33 +0000343 case PPC::SPILLTOVSR_ST:
Hal Finkel37714b82013-03-27 21:21:15 +0000344 // Check for the operands added by addFrameReference (the immediate is the
345 // offset which defaults to 0).
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000346 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() &&
347 MI.getOperand(2).isFI()) {
348 FrameIndex = MI.getOperand(2).getIndex();
349 return MI.getOperand(0).getReg();
Chris Lattnerc327d712006-02-02 20:16:12 +0000350 }
351 break;
352 }
353 return 0;
354}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000355
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000356MachineInstr *PPCInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI,
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000357 unsigned OpIdx1,
358 unsigned OpIdx2) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000359 MachineFunction &MF = *MI.getParent()->getParent();
Dan Gohman3b460302008-07-07 23:14:23 +0000360
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000361 // Normal instructions can be commuted the obvious way.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000362 if (MI.getOpcode() != PPC::RLWIMI && MI.getOpcode() != PPC::RLWIMIo)
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000363 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
Hal Finkel4c6658f2014-12-12 23:59:36 +0000364 // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a
365 // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because
366 // changing the relative order of the mask operands might change what happens
367 // to the high-bits of the mask (and, thus, the result).
Andrew Trickc416ba62010-12-24 04:28:06 +0000368
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000369 // Cannot commute if it has a non-zero rotate count.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000370 if (MI.getOperand(3).getImm() != 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000371 return nullptr;
Andrew Trickc416ba62010-12-24 04:28:06 +0000372
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000373 // If we have a zero rotate count, we have:
374 // M = mask(MB,ME)
375 // Op0 = (Op1 & ~M) | (Op2 & M)
376 // Change this to:
377 // M = mask((ME+1)&31, (MB-1)&31)
378 // Op0 = (Op2 & ~M) | (Op1 & M)
379
380 // Swap op1/op2
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000381 assert(((OpIdx1 == 1 && OpIdx2 == 2) || (OpIdx1 == 2 && OpIdx2 == 1)) &&
382 "Only the operands 1 and 2 can be swapped in RLSIMI/RLWIMIo.");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000383 unsigned Reg0 = MI.getOperand(0).getReg();
384 unsigned Reg1 = MI.getOperand(1).getReg();
385 unsigned Reg2 = MI.getOperand(2).getReg();
386 unsigned SubReg1 = MI.getOperand(1).getSubReg();
387 unsigned SubReg2 = MI.getOperand(2).getSubReg();
388 bool Reg1IsKill = MI.getOperand(1).isKill();
389 bool Reg2IsKill = MI.getOperand(2).isKill();
Evan Cheng03553bb2008-06-16 07:33:11 +0000390 bool ChangeReg0 = false;
Evan Cheng244183e2008-02-13 02:46:49 +0000391 // If machine instrs are no longer in two-address forms, update
392 // destination register as well.
393 if (Reg0 == Reg1) {
394 // Must be two address instruction!
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000395 assert(MI.getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Cheng244183e2008-02-13 02:46:49 +0000396 "Expecting a two-address instruction!");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000397 assert(MI.getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
Evan Cheng244183e2008-02-13 02:46:49 +0000398 Reg2IsKill = false;
Evan Cheng03553bb2008-06-16 07:33:11 +0000399 ChangeReg0 = true;
Evan Cheng244183e2008-02-13 02:46:49 +0000400 }
Evan Cheng03553bb2008-06-16 07:33:11 +0000401
402 // Masks.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000403 unsigned MB = MI.getOperand(4).getImm();
404 unsigned ME = MI.getOperand(5).getImm();
Evan Cheng03553bb2008-06-16 07:33:11 +0000405
Hal Finkelccf92592015-09-06 04:17:30 +0000406 // We can't commute a trivial mask (there is no way to represent an all-zero
407 // mask).
408 if (MB == 0 && ME == 31)
409 return nullptr;
410
Evan Cheng03553bb2008-06-16 07:33:11 +0000411 if (NewMI) {
412 // Create a new instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000413 unsigned Reg0 = ChangeReg0 ? Reg2 : MI.getOperand(0).getReg();
414 bool Reg0IsDead = MI.getOperand(0).isDead();
415 return BuildMI(MF, MI.getDebugLoc(), MI.getDesc())
416 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
417 .addReg(Reg2, getKillRegState(Reg2IsKill))
418 .addReg(Reg1, getKillRegState(Reg1IsKill))
419 .addImm((ME + 1) & 31)
420 .addImm((MB - 1) & 31);
Evan Cheng03553bb2008-06-16 07:33:11 +0000421 }
422
Andrew Tricke3398282013-12-17 04:50:45 +0000423 if (ChangeReg0) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000424 MI.getOperand(0).setReg(Reg2);
425 MI.getOperand(0).setSubReg(SubReg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000426 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000427 MI.getOperand(2).setReg(Reg1);
428 MI.getOperand(1).setReg(Reg2);
429 MI.getOperand(2).setSubReg(SubReg1);
430 MI.getOperand(1).setSubReg(SubReg2);
431 MI.getOperand(2).setIsKill(Reg1IsKill);
432 MI.getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000433
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000434 // Swap the mask around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000435 MI.getOperand(4).setImm((ME + 1) & 31);
436 MI.getOperand(5).setImm((MB - 1) & 31);
437 return &MI;
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000438}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000439
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000440bool PPCInstrInfo::findCommutedOpIndices(MachineInstr &MI, unsigned &SrcOpIdx1,
Hal Finkel6c32ff32014-03-25 19:26:43 +0000441 unsigned &SrcOpIdx2) const {
442 // For VSX A-Type FMA instructions, it is the first two operands that can be
443 // commuted, however, because the non-encoded tied input operand is listed
444 // first, the operands to swap are actually the second and third.
445
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000446 int AltOpc = PPC::getAltVSXFMAOpcode(MI.getOpcode());
Hal Finkel6c32ff32014-03-25 19:26:43 +0000447 if (AltOpc == -1)
448 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
449
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000450 // The commutable operand indices are 2 and 3. Return them in SrcOpIdx1
451 // and SrcOpIdx2.
452 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2, 3);
Hal Finkel6c32ff32014-03-25 19:26:43 +0000453}
454
Andrew Trickc416ba62010-12-24 04:28:06 +0000455void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000456 MachineBasicBlock::iterator MI) const {
Hal Finkelceb1f122013-12-12 00:19:11 +0000457 // This function is used for scheduling, and the nop wanted here is the type
458 // that terminates dispatch groups on the POWER cores.
Eric Christopher1dcea732014-06-12 21:48:52 +0000459 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000460 unsigned Opcode;
461 switch (Directive) {
462 default: Opcode = PPC::NOP; break;
463 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
464 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
Will Schmidt970ff642014-06-26 13:36:19 +0000465 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 +0000466 // FIXME: Update when POWER9 scheduling model is ready.
467 case PPC::DIR_PWR9: Opcode = PPC::NOP_GT_PWR7; break;
Hal Finkelceb1f122013-12-12 00:19:11 +0000468 }
Chris Lattnera47294ed2006-10-13 21:21:17 +0000469
Hal Finkelceb1f122013-12-12 00:19:11 +0000470 DebugLoc DL;
471 BuildMI(MBB, MI, DL, get(Opcode));
472}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000473
Hans Wennborg9b9a5352017-04-21 21:48:41 +0000474/// Return the noop instruction to use for a noop.
475void PPCInstrInfo::getNoop(MCInst &NopInst) const {
Joerg Sonnenberger7ee0f312014-08-08 19:13:23 +0000476 NopInst.setOpcode(PPC::NOP);
477}
478
Chris Lattnera47294ed2006-10-13 21:21:17 +0000479// Branch analysis.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000480// Note: If the condition register is set to CTR or CTR8 then this is a
481// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000482bool PPCInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
483 MachineBasicBlock *&TBB,
Chris Lattnera47294ed2006-10-13 21:21:17 +0000484 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000485 SmallVectorImpl<MachineOperand> &Cond,
486 bool AllowModify) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000487 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000488
Chris Lattnera47294ed2006-10-13 21:21:17 +0000489 // If the block has no terminators, it just falls into the block after it.
Benjamin Kramer92861d72015-06-25 13:39:03 +0000490 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
491 if (I == MBB.end())
Dale Johannesen4244d122010-04-02 01:38:09 +0000492 return false;
Benjamin Kramer92861d72015-06-25 13:39:03 +0000493
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000494 if (!isUnpredicatedTerminator(*I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000495 return false;
496
497 // Get the last instruction in the block.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000498 MachineInstr &LastInst = *I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000499
Chris Lattnera47294ed2006-10-13 21:21:17 +0000500 // If there is only one terminator instruction, process it.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000501 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000502 if (LastInst.getOpcode() == PPC::B) {
503 if (!LastInst.getOperand(0).isMBB())
Evan Cheng8f43afd2009-05-08 23:09:25 +0000504 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000505 TBB = LastInst.getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000506 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000507 } else if (LastInst.getOpcode() == PPC::BCC) {
508 if (!LastInst.getOperand(2).isMBB())
Evan Cheng8f43afd2009-05-08 23:09:25 +0000509 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000510 // Block ends with fall-through condbranch.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000511 TBB = LastInst.getOperand(2).getMBB();
512 Cond.push_back(LastInst.getOperand(0));
513 Cond.push_back(LastInst.getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000514 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000515 } else if (LastInst.getOpcode() == PPC::BC) {
516 if (!LastInst.getOperand(1).isMBB())
Hal Finkel940ab932014-02-28 00:27:01 +0000517 return true;
518 // Block ends with fall-through condbranch.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000519 TBB = LastInst.getOperand(1).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000520 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000521 Cond.push_back(LastInst.getOperand(0));
Hal Finkel940ab932014-02-28 00:27:01 +0000522 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000523 } else if (LastInst.getOpcode() == PPC::BCn) {
524 if (!LastInst.getOperand(1).isMBB())
Hal Finkel940ab932014-02-28 00:27:01 +0000525 return true;
526 // Block ends with fall-through condbranch.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000527 TBB = LastInst.getOperand(1).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000528 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000529 Cond.push_back(LastInst.getOperand(0));
Hal Finkel940ab932014-02-28 00:27:01 +0000530 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000531 } else if (LastInst.getOpcode() == PPC::BDNZ8 ||
532 LastInst.getOpcode() == PPC::BDNZ) {
533 if (!LastInst.getOperand(0).isMBB())
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000534 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000535 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000536 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000537 TBB = LastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000538 Cond.push_back(MachineOperand::CreateImm(1));
539 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
540 true));
541 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000542 } else if (LastInst.getOpcode() == PPC::BDZ8 ||
543 LastInst.getOpcode() == PPC::BDZ) {
544 if (!LastInst.getOperand(0).isMBB())
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000545 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000546 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000547 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000548 TBB = LastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000549 Cond.push_back(MachineOperand::CreateImm(0));
550 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
551 true));
552 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000553 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000554
Chris Lattnera47294ed2006-10-13 21:21:17 +0000555 // Otherwise, don't know what this is.
556 return true;
557 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000558
Chris Lattnera47294ed2006-10-13 21:21:17 +0000559 // Get the instruction before it if it's a terminator.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000560 MachineInstr &SecondLastInst = *I;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000561
562 // 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 +0000563 if (I != MBB.begin() && isUnpredicatedTerminator(*--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000564 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000565
Chris Lattnere0263792006-11-17 22:14:47 +0000566 // If the block ends with PPC::B and PPC:BCC, handle it.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000567 if (SecondLastInst.getOpcode() == PPC::BCC &&
568 LastInst.getOpcode() == PPC::B) {
569 if (!SecondLastInst.getOperand(2).isMBB() ||
570 !LastInst.getOperand(0).isMBB())
Evan Cheng8f43afd2009-05-08 23:09:25 +0000571 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000572 TBB = SecondLastInst.getOperand(2).getMBB();
573 Cond.push_back(SecondLastInst.getOperand(0));
574 Cond.push_back(SecondLastInst.getOperand(1));
575 FBB = LastInst.getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000576 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000577 } else if (SecondLastInst.getOpcode() == PPC::BC &&
578 LastInst.getOpcode() == PPC::B) {
579 if (!SecondLastInst.getOperand(1).isMBB() ||
580 !LastInst.getOperand(0).isMBB())
Hal Finkel940ab932014-02-28 00:27:01 +0000581 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000582 TBB = SecondLastInst.getOperand(1).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000583 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000584 Cond.push_back(SecondLastInst.getOperand(0));
585 FBB = LastInst.getOperand(0).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000586 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000587 } else if (SecondLastInst.getOpcode() == PPC::BCn &&
588 LastInst.getOpcode() == PPC::B) {
589 if (!SecondLastInst.getOperand(1).isMBB() ||
590 !LastInst.getOperand(0).isMBB())
Hal Finkel940ab932014-02-28 00:27:01 +0000591 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000592 TBB = SecondLastInst.getOperand(1).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000593 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000594 Cond.push_back(SecondLastInst.getOperand(0));
595 FBB = LastInst.getOperand(0).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000596 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000597 } else if ((SecondLastInst.getOpcode() == PPC::BDNZ8 ||
598 SecondLastInst.getOpcode() == PPC::BDNZ) &&
599 LastInst.getOpcode() == PPC::B) {
600 if (!SecondLastInst.getOperand(0).isMBB() ||
601 !LastInst.getOperand(0).isMBB())
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000602 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000603 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000604 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000605 TBB = SecondLastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000606 Cond.push_back(MachineOperand::CreateImm(1));
607 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
608 true));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000609 FBB = LastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000610 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000611 } else if ((SecondLastInst.getOpcode() == PPC::BDZ8 ||
612 SecondLastInst.getOpcode() == PPC::BDZ) &&
613 LastInst.getOpcode() == PPC::B) {
614 if (!SecondLastInst.getOperand(0).isMBB() ||
615 !LastInst.getOperand(0).isMBB())
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000616 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000617 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000618 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000619 TBB = SecondLastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000620 Cond.push_back(MachineOperand::CreateImm(0));
621 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
622 true));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000623 FBB = LastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000624 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000625 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000626
Dale Johannesenc6855462007-06-13 17:59:52 +0000627 // If the block ends with two PPC:Bs, handle it. The second one is not
628 // executed, so remove it.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000629 if (SecondLastInst.getOpcode() == PPC::B && LastInst.getOpcode() == PPC::B) {
630 if (!SecondLastInst.getOperand(0).isMBB())
Evan Cheng8f43afd2009-05-08 23:09:25 +0000631 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000632 TBB = SecondLastInst.getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000633 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000634 if (AllowModify)
635 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000636 return false;
637 }
638
Chris Lattnera47294ed2006-10-13 21:21:17 +0000639 // Otherwise, can't handle this.
640 return true;
641}
642
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000643unsigned PPCInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000644 int *BytesRemoved) const {
645 assert(!BytesRemoved && "code size not handled");
646
Benjamin Kramer92861d72015-06-25 13:39:03 +0000647 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
648 if (I == MBB.end())
649 return 0;
650
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000651 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000652 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000653 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
654 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000655 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000656
Chris Lattnera47294ed2006-10-13 21:21:17 +0000657 // Remove the branch.
658 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000659
Chris Lattnera47294ed2006-10-13 21:21:17 +0000660 I = MBB.end();
661
Evan Cheng99be49d2007-05-18 00:05:48 +0000662 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000663 --I;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000664 if (I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000665 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000666 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
667 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000668 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000669
Chris Lattnera47294ed2006-10-13 21:21:17 +0000670 // Remove the branch.
671 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000672 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000673}
674
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000675unsigned PPCInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000676 MachineBasicBlock *TBB,
677 MachineBasicBlock *FBB,
678 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000679 const DebugLoc &DL,
680 int *BytesAdded) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000681 // Shouldn't be a fall through.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000682 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000683 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000684 "PPC branch conditions have two components!");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000685 assert(!BytesAdded && "code size not handled");
Andrew Trickc416ba62010-12-24 04:28:06 +0000686
Eric Christopher1dcea732014-06-12 21:48:52 +0000687 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000688
Chris Lattner94e04442006-10-21 05:36:13 +0000689 // One-way branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000690 if (!FBB) {
Chris Lattner94e04442006-10-21 05:36:13 +0000691 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000692 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000693 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
694 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
695 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
696 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000697 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
Diana Picus116bbab2017-01-13 09:58:52 +0000698 BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000699 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
Diana Picus116bbab2017-01-13 09:58:52 +0000700 BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000701 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000702 BuildMI(&MBB, DL, get(PPC::BCC))
Diana Picus116bbab2017-01-13 09:58:52 +0000703 .addImm(Cond[0].getImm())
704 .add(Cond[1])
705 .addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000706 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000707 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000708
Chris Lattnerd8816602006-10-21 05:42:09 +0000709 // Two-way Conditional Branch.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000710 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
711 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
712 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
713 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000714 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
Diana Picus116bbab2017-01-13 09:58:52 +0000715 BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000716 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
Diana Picus116bbab2017-01-13 09:58:52 +0000717 BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000718 else
719 BuildMI(&MBB, DL, get(PPC::BCC))
Diana Picus116bbab2017-01-13 09:58:52 +0000720 .addImm(Cond[0].getImm())
721 .add(Cond[1])
722 .addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000723 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000724 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000725}
726
Hal Finkeled6a2852013-04-05 23:29:01 +0000727// Select analysis.
728bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000729 ArrayRef<MachineOperand> Cond,
Hal Finkeled6a2852013-04-05 23:29:01 +0000730 unsigned TrueReg, unsigned FalseReg,
731 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
Hal Finkeled6a2852013-04-05 23:29:01 +0000732 if (Cond.size() != 2)
733 return false;
734
735 // If this is really a bdnz-like condition, then it cannot be turned into a
736 // select.
737 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
738 return false;
739
740 // Check register classes.
741 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
742 const TargetRegisterClass *RC =
743 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
744 if (!RC)
745 return false;
746
747 // isel is for regular integer GPRs only.
748 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkel8e8618a2013-07-15 20:22:58 +0000749 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
750 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
751 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkeled6a2852013-04-05 23:29:01 +0000752 return false;
753
754 // FIXME: These numbers are for the A2, how well they work for other cores is
755 // an open question. On the A2, the isel instruction has a 2-cycle latency
756 // but single-cycle throughput. These numbers are used in combination with
757 // the MispredictPenalty setting from the active SchedMachineModel.
758 CondCycles = 1;
759 TrueCycles = 1;
760 FalseCycles = 1;
761
762 return true;
763}
764
765void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000766 MachineBasicBlock::iterator MI,
767 const DebugLoc &dl, unsigned DestReg,
768 ArrayRef<MachineOperand> Cond, unsigned TrueReg,
769 unsigned FalseReg) const {
Hal Finkeled6a2852013-04-05 23:29:01 +0000770 assert(Cond.size() == 2 &&
771 "PPC branch conditions have two components!");
772
Hal Finkeled6a2852013-04-05 23:29:01 +0000773 // Get the register classes.
774 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
775 const TargetRegisterClass *RC =
776 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
777 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkel8e8618a2013-07-15 20:22:58 +0000778
779 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
780 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
781 assert((Is64Bit ||
782 PPC::GPRCRegClass.hasSubClassEq(RC) ||
783 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000784 "isel is for regular integer GPRs only");
785
Hal Finkel8e8618a2013-07-15 20:22:58 +0000786 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Kyle Buttcec40802016-01-12 21:00:43 +0000787 auto SelectPred = static_cast<PPC::Predicate>(Cond[0].getImm());
Hal Finkeled6a2852013-04-05 23:29:01 +0000788
Kyle Butt132bf362016-01-15 19:20:06 +0000789 unsigned SubIdx = 0;
790 bool SwapOps = false;
Hal Finkeled6a2852013-04-05 23:29:01 +0000791 switch (SelectPred) {
Kyle Buttcec40802016-01-12 21:00:43 +0000792 case PPC::PRED_EQ:
793 case PPC::PRED_EQ_MINUS:
794 case PPC::PRED_EQ_PLUS:
795 SubIdx = PPC::sub_eq; SwapOps = false; break;
796 case PPC::PRED_NE:
797 case PPC::PRED_NE_MINUS:
798 case PPC::PRED_NE_PLUS:
799 SubIdx = PPC::sub_eq; SwapOps = true; break;
800 case PPC::PRED_LT:
801 case PPC::PRED_LT_MINUS:
802 case PPC::PRED_LT_PLUS:
803 SubIdx = PPC::sub_lt; SwapOps = false; break;
804 case PPC::PRED_GE:
805 case PPC::PRED_GE_MINUS:
806 case PPC::PRED_GE_PLUS:
807 SubIdx = PPC::sub_lt; SwapOps = true; break;
808 case PPC::PRED_GT:
809 case PPC::PRED_GT_MINUS:
810 case PPC::PRED_GT_PLUS:
811 SubIdx = PPC::sub_gt; SwapOps = false; break;
812 case PPC::PRED_LE:
813 case PPC::PRED_LE_MINUS:
814 case PPC::PRED_LE_PLUS:
815 SubIdx = PPC::sub_gt; SwapOps = true; break;
816 case PPC::PRED_UN:
817 case PPC::PRED_UN_MINUS:
818 case PPC::PRED_UN_PLUS:
819 SubIdx = PPC::sub_un; SwapOps = false; break;
820 case PPC::PRED_NU:
821 case PPC::PRED_NU_MINUS:
822 case PPC::PRED_NU_PLUS:
823 SubIdx = PPC::sub_un; SwapOps = true; break;
Hal Finkel940ab932014-02-28 00:27:01 +0000824 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break;
825 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
Hal Finkeled6a2852013-04-05 23:29:01 +0000826 }
827
828 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
829 SecondReg = SwapOps ? TrueReg : FalseReg;
830
831 // The first input register of isel cannot be r0. If it is a member
832 // of a register class that can be r0, then copy it first (the
833 // register allocator should eliminate the copy).
834 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
835 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
836 const TargetRegisterClass *FirstRC =
837 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
838 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
839 unsigned OldFirstReg = FirstReg;
840 FirstReg = MRI.createVirtualRegister(FirstRC);
841 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
842 .addReg(OldFirstReg);
843 }
844
845 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
846 .addReg(FirstReg).addReg(SecondReg)
847 .addReg(Cond[1].getReg(), 0, SubIdx);
848}
849
Kit Barton535e69d2015-03-25 19:36:23 +0000850static unsigned getCRBitValue(unsigned CRBit) {
851 unsigned Ret = 4;
852 if (CRBit == PPC::CR0LT || CRBit == PPC::CR1LT ||
853 CRBit == PPC::CR2LT || CRBit == PPC::CR3LT ||
854 CRBit == PPC::CR4LT || CRBit == PPC::CR5LT ||
855 CRBit == PPC::CR6LT || CRBit == PPC::CR7LT)
856 Ret = 3;
857 if (CRBit == PPC::CR0GT || CRBit == PPC::CR1GT ||
858 CRBit == PPC::CR2GT || CRBit == PPC::CR3GT ||
859 CRBit == PPC::CR4GT || CRBit == PPC::CR5GT ||
860 CRBit == PPC::CR6GT || CRBit == PPC::CR7GT)
861 Ret = 2;
862 if (CRBit == PPC::CR0EQ || CRBit == PPC::CR1EQ ||
863 CRBit == PPC::CR2EQ || CRBit == PPC::CR3EQ ||
864 CRBit == PPC::CR4EQ || CRBit == PPC::CR5EQ ||
865 CRBit == PPC::CR6EQ || CRBit == PPC::CR7EQ)
866 Ret = 1;
867 if (CRBit == PPC::CR0UN || CRBit == PPC::CR1UN ||
868 CRBit == PPC::CR2UN || CRBit == PPC::CR3UN ||
869 CRBit == PPC::CR4UN || CRBit == PPC::CR5UN ||
870 CRBit == PPC::CR6UN || CRBit == PPC::CR7UN)
871 Ret = 0;
872
873 assert(Ret != 4 && "Invalid CR bit register");
874 return Ret;
875}
876
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000877void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000878 MachineBasicBlock::iterator I,
879 const DebugLoc &DL, unsigned DestReg,
880 unsigned SrcReg, bool KillSrc) const {
Hal Finkel27774d92014-03-13 07:58:58 +0000881 // We can end up with self copies and similar things as a result of VSX copy
Hal Finkel9dcb3582014-03-27 22:46:28 +0000882 // legalization. Promote them here.
Hal Finkel27774d92014-03-13 07:58:58 +0000883 const TargetRegisterInfo *TRI = &getRegisterInfo();
884 if (PPC::F8RCRegClass.contains(DestReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000885 PPC::VSRCRegClass.contains(SrcReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000886 unsigned SuperReg =
887 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass);
888
Hal Finkel9dcb3582014-03-27 22:46:28 +0000889 if (VSXSelfCopyCrash && SrcReg == SuperReg)
890 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000891
892 DestReg = SuperReg;
Hal Finkel27774d92014-03-13 07:58:58 +0000893 } else if (PPC::F8RCRegClass.contains(SrcReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000894 PPC::VSRCRegClass.contains(DestReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000895 unsigned SuperReg =
896 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass);
897
Hal Finkel9dcb3582014-03-27 22:46:28 +0000898 if (VSXSelfCopyCrash && DestReg == SuperReg)
899 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000900
901 SrcReg = SuperReg;
Hal Finkel27774d92014-03-13 07:58:58 +0000902 }
903
Kit Barton535e69d2015-03-25 19:36:23 +0000904 // Different class register copy
905 if (PPC::CRBITRCRegClass.contains(SrcReg) &&
906 PPC::GPRCRegClass.contains(DestReg)) {
907 unsigned CRReg = getCRFromCRBit(SrcReg);
Richard Trieu7a083812016-02-18 22:09:30 +0000908 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(CRReg);
909 getKillRegState(KillSrc);
Kit Barton535e69d2015-03-25 19:36:23 +0000910 // Rotate the CR bit in the CR fields to be the least significant bit and
911 // then mask with 0x1 (MB = ME = 31).
912 BuildMI(MBB, I, DL, get(PPC::RLWINM), DestReg)
913 .addReg(DestReg, RegState::Kill)
914 .addImm(TRI->getEncodingValue(CRReg) * 4 + (4 - getCRBitValue(SrcReg)))
915 .addImm(31)
916 .addImm(31);
917 return;
918 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
919 PPC::G8RCRegClass.contains(DestReg)) {
Richard Trieu7a083812016-02-18 22:09:30 +0000920 BuildMI(MBB, I, DL, get(PPC::MFOCRF8), DestReg).addReg(SrcReg);
921 getKillRegState(KillSrc);
Kit Barton535e69d2015-03-25 19:36:23 +0000922 return;
923 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
924 PPC::GPRCRegClass.contains(DestReg)) {
Richard Trieu7a083812016-02-18 22:09:30 +0000925 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(SrcReg);
926 getKillRegState(KillSrc);
Kit Barton535e69d2015-03-25 19:36:23 +0000927 return;
Zaara Syedafcd96972017-09-21 16:12:33 +0000928 } else if (PPC::G8RCRegClass.contains(SrcReg) &&
929 PPC::VSFRCRegClass.contains(DestReg)) {
930 BuildMI(MBB, I, DL, get(PPC::MTVSRD), DestReg).addReg(SrcReg);
931 NumGPRtoVSRSpill++;
932 getKillRegState(KillSrc);
933 return;
934 } else if (PPC::VSFRCRegClass.contains(SrcReg) &&
935 PPC::G8RCRegClass.contains(DestReg)) {
936 BuildMI(MBB, I, DL, get(PPC::MFVSRD), DestReg).addReg(SrcReg);
937 getKillRegState(KillSrc);
938 return;
939 }
Kit Barton535e69d2015-03-25 19:36:23 +0000940
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000941 unsigned Opc;
942 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
943 Opc = PPC::OR;
944 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
945 Opc = PPC::OR8;
946 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
947 Opc = PPC::FMR;
948 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
949 Opc = PPC::MCRF;
950 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
951 Opc = PPC::VOR;
Hal Finkel27774d92014-03-13 07:58:58 +0000952 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg))
Hal Finkelbbad2332014-03-24 09:36:36 +0000953 // There are two different ways this can be done:
Hal Finkel27774d92014-03-13 07:58:58 +0000954 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
955 // issue in VSU pipeline 0.
956 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
957 // can go to either pipeline.
Hal Finkelbbad2332014-03-24 09:36:36 +0000958 // We'll always use xxlor here, because in practically all cases where
959 // copies are generated, they are close enough to some use that the
960 // lower-latency form is preferable.
Hal Finkel27774d92014-03-13 07:58:58 +0000961 Opc = PPC::XXLOR;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +0000962 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg) ||
963 PPC::VSSRCRegClass.contains(DestReg, SrcReg))
Hal Finkel19be5062014-03-29 05:29:01 +0000964 Opc = PPC::XXLORf;
Hal Finkelc93a9a22015-02-25 01:06:45 +0000965 else if (PPC::QFRCRegClass.contains(DestReg, SrcReg))
966 Opc = PPC::QVFMR;
967 else if (PPC::QSRCRegClass.contains(DestReg, SrcReg))
968 Opc = PPC::QVFMRs;
969 else if (PPC::QBRCRegClass.contains(DestReg, SrcReg))
970 Opc = PPC::QVFMRb;
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000971 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
972 Opc = PPC::CROR;
973 else
974 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +0000975
Evan Cheng6cc775f2011-06-28 19:10:37 +0000976 const MCInstrDesc &MCID = get(Opc);
977 if (MCID.getNumOperands() == 3)
978 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000979 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
980 else
Evan Cheng6cc775f2011-06-28 19:10:37 +0000981 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +0000982}
983
Hal Finkel8f6834d2011-12-05 17:55:17 +0000984// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000985bool
Dan Gohman3b460302008-07-07 23:14:23 +0000986PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
987 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000988 int FrameIdx,
989 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000990 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000991 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000992 // Note: If additional store instructions are added here,
993 // update isStoreToStackSlot.
994
Chris Lattner6f306d72010-04-02 20:16:16 +0000995 DebugLoc DL;
Hal Finkel4e703bc2014-01-28 05:32:58 +0000996 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
997 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000998 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
999 .addReg(SrcReg,
1000 getKillRegState(isKill)),
1001 FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +00001002 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
1003 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +00001004 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
1005 .addReg(SrcReg,
1006 getKillRegState(isKill)),
1007 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001008 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +00001009 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +00001010 .addReg(SrcReg,
1011 getKillRegState(isKill)),
1012 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001013 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +00001014 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +00001015 .addReg(SrcReg,
1016 getKillRegState(isKill)),
1017 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001018 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +00001019 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
1020 .addReg(SrcReg,
1021 getKillRegState(isKill)),
1022 FrameIdx));
1023 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001024 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +00001025 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT))
1026 .addReg(SrcReg,
1027 getKillRegState(isKill)),
1028 FrameIdx));
1029 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001030 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +00001031 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
1032 .addReg(SrcReg,
1033 getKillRegState(isKill)),
1034 FrameIdx));
1035 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +00001036 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +00001037 unsigned Op = Subtarget.hasP9Vector() ? PPC::STXVX : PPC::STXVD2X;
1038 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op))
Hal Finkel27774d92014-03-13 07:58:58 +00001039 .addReg(SrcReg,
1040 getKillRegState(isKill)),
1041 FrameIdx));
1042 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +00001043 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00001044 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFSTOREf64 : PPC::STXSDX;
1045 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc))
Hal Finkel19be5062014-03-29 05:29:01 +00001046 .addReg(SrcReg,
1047 getKillRegState(isKill)),
1048 FrameIdx));
1049 NonRI = true;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001050 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00001051 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFSTOREf32 : PPC::STXSSPX;
1052 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc))
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001053 .addReg(SrcReg,
1054 getKillRegState(isKill)),
1055 FrameIdx));
1056 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +00001057 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001058 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +00001059 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +00001060 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
1061 .addReg(SrcReg,
1062 getKillRegState(isKill)),
1063 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001064 SpillsVRS = true;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001065 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
1066 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDX))
1067 .addReg(SrcReg,
1068 getKillRegState(isKill)),
1069 FrameIdx));
1070 NonRI = true;
1071 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
1072 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFSXs))
1073 .addReg(SrcReg,
1074 getKillRegState(isKill)),
1075 FrameIdx));
1076 NonRI = true;
1077 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
1078 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDXb))
1079 .addReg(SrcReg,
1080 getKillRegState(isKill)),
1081 FrameIdx));
1082 NonRI = true;
Zaara Syedafcd96972017-09-21 16:12:33 +00001083 } else if (PPC::SPILLTOVSRRCRegClass.hasSubClassEq(RC)) {
1084 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILLTOVSR_ST))
1085 .addReg(SrcReg,
1086 getKillRegState(isKill)),
1087 FrameIdx));
Owen Andersoneee14602008-01-01 21:11:32 +00001088 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001089 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +00001090 }
Bill Wendling632ea652008-03-03 22:19:16 +00001091
1092 return false;
Owen Andersoneee14602008-01-01 21:11:32 +00001093}
1094
1095void
1096PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +00001097 MachineBasicBlock::iterator MI,
1098 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +00001099 const TargetRegisterClass *RC,
1100 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +00001101 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +00001102 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +00001103
Hal Finkelbb420f12013-03-15 05:06:04 +00001104 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1105 FuncInfo->setHasSpills();
1106
Nemanja Ivanovic11049f82016-10-04 06:59:23 +00001107 // We need to avoid a situation in which the value from a VRRC register is
1108 // spilled using an Altivec instruction and reloaded into a VSRC register
1109 // using a VSX instruction. The issue with this is that the VSX
1110 // load/store instructions swap the doublewords in the vector and the Altivec
1111 // ones don't. The register classes on the spill/reload may be different if
1112 // the register is defined using an Altivec instruction and is then used by a
1113 // VSX instruction.
1114 RC = updatedRC(RC);
1115
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001116 bool NonRI = false, SpillsVRS = false;
1117 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
1118 NonRI, SpillsVRS))
Bill Wendling632ea652008-03-03 22:19:16 +00001119 FuncInfo->setSpillsCR();
Bill Wendling632ea652008-03-03 22:19:16 +00001120
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001121 if (SpillsVRS)
1122 FuncInfo->setSpillsVRSAVE();
1123
Hal Finkelfcc51d42013-03-17 04:43:44 +00001124 if (NonRI)
1125 FuncInfo->setHasNonRISpills();
1126
Owen Andersoneee14602008-01-01 21:11:32 +00001127 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1128 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001129
Matthias Braun941a7052016-07-28 18:40:00 +00001130 const MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001131 MachineMemOperand *MMO = MF.getMachineMemOperand(
1132 MachinePointerInfo::getFixedStack(MF, FrameIdx),
1133 MachineMemOperand::MOStore, MFI.getObjectSize(FrameIdx),
1134 MFI.getObjectAlignment(FrameIdx));
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001135 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +00001136}
1137
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001138bool PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, const DebugLoc &DL,
1139 unsigned DestReg, int FrameIdx,
1140 const TargetRegisterClass *RC,
1141 SmallVectorImpl<MachineInstr *> &NewMIs,
1142 bool &NonRI, bool &SpillsVRS) const {
Hal Finkel37714b82013-03-27 21:21:15 +00001143 // Note: If additional load instructions are added here,
1144 // update isLoadFromStackSlot.
1145
Hal Finkel4e703bc2014-01-28 05:32:58 +00001146 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
1147 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +00001148 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
1149 DestReg), FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +00001150 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
1151 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +00001152 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
1153 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001154 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001155 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +00001156 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001157 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001158 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +00001159 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001160 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +00001161 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1162 get(PPC::RESTORE_CR), DestReg),
1163 FrameIdx));
1164 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001165 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +00001166 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1167 get(PPC::RESTORE_CRBIT), DestReg),
1168 FrameIdx));
1169 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001170 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +00001171 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
1172 FrameIdx));
1173 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +00001174 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +00001175 unsigned Op = Subtarget.hasP9Vector() ? PPC::LXVX : PPC::LXVD2X;
1176 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op), DestReg),
Hal Finkel27774d92014-03-13 07:58:58 +00001177 FrameIdx));
1178 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +00001179 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00001180 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFLOADf64 : PPC::LXSDX;
1181 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc),
1182 DestReg), FrameIdx));
Hal Finkel19be5062014-03-29 05:29:01 +00001183 NonRI = true;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001184 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00001185 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFLOADf32 : PPC::LXSSPX;
1186 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc),
1187 DestReg), FrameIdx));
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001188 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +00001189 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001190 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +00001191 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +00001192 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1193 get(PPC::RESTORE_VRSAVE),
1194 DestReg),
1195 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001196 SpillsVRS = true;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001197 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
1198 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDX), DestReg),
1199 FrameIdx));
1200 NonRI = true;
1201 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
1202 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFSXs), DestReg),
1203 FrameIdx));
1204 NonRI = true;
1205 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
1206 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDXb), DestReg),
1207 FrameIdx));
1208 NonRI = true;
Zaara Syedafcd96972017-09-21 16:12:33 +00001209 } else if (PPC::SPILLTOVSRRCRegClass.hasSubClassEq(RC)) {
1210 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILLTOVSR_LD),
1211 DestReg), FrameIdx));
Owen Andersoneee14602008-01-01 21:11:32 +00001212 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001213 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +00001214 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001215
1216 return false;
Owen Andersoneee14602008-01-01 21:11:32 +00001217}
1218
1219void
1220PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +00001221 MachineBasicBlock::iterator MI,
1222 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +00001223 const TargetRegisterClass *RC,
1224 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +00001225 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +00001226 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +00001227 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001228 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelfcc51d42013-03-17 04:43:44 +00001229
1230 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1231 FuncInfo->setHasSpills();
1232
Nemanja Ivanovic11049f82016-10-04 06:59:23 +00001233 // We need to avoid a situation in which the value from a VRRC register is
1234 // spilled using an Altivec instruction and reloaded into a VSRC register
1235 // using a VSX instruction. The issue with this is that the VSX
1236 // load/store instructions swap the doublewords in the vector and the Altivec
1237 // ones don't. The register classes on the spill/reload may be different if
1238 // the register is defined using an Altivec instruction and is then used by a
1239 // VSX instruction.
1240 if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass)
1241 RC = &PPC::VSRCRegClass;
1242
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001243 bool NonRI = false, SpillsVRS = false;
1244 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
1245 NonRI, SpillsVRS))
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001246 FuncInfo->setSpillsCR();
Hal Finkelfcc51d42013-03-17 04:43:44 +00001247
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001248 if (SpillsVRS)
1249 FuncInfo->setSpillsVRSAVE();
1250
Hal Finkelfcc51d42013-03-17 04:43:44 +00001251 if (NonRI)
1252 FuncInfo->setHasNonRISpills();
1253
Owen Andersoneee14602008-01-01 21:11:32 +00001254 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1255 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001256
Matthias Braun941a7052016-07-28 18:40:00 +00001257 const MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001258 MachineMemOperand *MMO = MF.getMachineMemOperand(
1259 MachinePointerInfo::getFixedStack(MF, FrameIdx),
1260 MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIdx),
1261 MFI.getObjectAlignment(FrameIdx));
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001262 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +00001263}
1264
Chris Lattnera47294ed2006-10-13 21:21:17 +00001265bool PPCInstrInfo::
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001266reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +00001267 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel96c2d4d2012-06-08 15:38:21 +00001268 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
1269 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
1270 else
1271 // Leave the CR# the same, but invert the condition.
1272 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +00001273 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +00001274}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001275
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001276bool PPCInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
1277 unsigned Reg, MachineRegisterInfo *MRI) const {
Hal Finkeld61d4f82013-04-06 19:30:30 +00001278 // For some instructions, it is legal to fold ZERO into the RA register field.
1279 // A zero immediate should always be loaded with a single li.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001280 unsigned DefOpc = DefMI.getOpcode();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001281 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
1282 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001283 if (!DefMI.getOperand(1).isImm())
Hal Finkeld61d4f82013-04-06 19:30:30 +00001284 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001285 if (DefMI.getOperand(1).getImm() != 0)
Hal Finkeld61d4f82013-04-06 19:30:30 +00001286 return false;
1287
1288 // Note that we cannot here invert the arguments of an isel in order to fold
1289 // a ZERO into what is presented as the second argument. All we have here
1290 // is the condition bit, and that might come from a CR-logical bit operation.
1291
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001292 const MCInstrDesc &UseMCID = UseMI.getDesc();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001293
1294 // Only fold into real machine instructions.
1295 if (UseMCID.isPseudo())
1296 return false;
1297
1298 unsigned UseIdx;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001299 for (UseIdx = 0; UseIdx < UseMI.getNumOperands(); ++UseIdx)
1300 if (UseMI.getOperand(UseIdx).isReg() &&
1301 UseMI.getOperand(UseIdx).getReg() == Reg)
Hal Finkeld61d4f82013-04-06 19:30:30 +00001302 break;
1303
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001304 assert(UseIdx < UseMI.getNumOperands() && "Cannot find Reg in UseMI");
Hal Finkeld61d4f82013-04-06 19:30:30 +00001305 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
1306
1307 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
1308
1309 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
1310 // register (which might also be specified as a pointer class kind).
1311 if (UseInfo->isLookupPtrRegClass()) {
1312 if (UseInfo->RegClass /* Kind */ != 1)
1313 return false;
1314 } else {
1315 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
1316 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
1317 return false;
1318 }
1319
1320 // Make sure this is not tied to an output register (or otherwise
1321 // constrained). This is true for ST?UX registers, for example, which
1322 // are tied to their output registers.
1323 if (UseInfo->Constraints != 0)
1324 return false;
1325
1326 unsigned ZeroReg;
1327 if (UseInfo->isLookupPtrRegClass()) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001328 bool isPPC64 = Subtarget.isPPC64();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001329 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
1330 } else {
1331 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
1332 PPC::ZERO8 : PPC::ZERO;
1333 }
1334
1335 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001336 UseMI.getOperand(UseIdx).setReg(ZeroReg);
Hal Finkeld61d4f82013-04-06 19:30:30 +00001337
1338 if (DeleteDef)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001339 DefMI.eraseFromParent();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001340
1341 return true;
1342}
1343
Hal Finkel30ae2292013-04-10 18:30:16 +00001344static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
1345 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1346 I != IE; ++I)
1347 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
1348 return true;
1349 return false;
1350}
1351
1352// We should make sure that, if we're going to predicate both sides of a
1353// condition (a diamond), that both sides don't define the counter register. We
1354// can predicate counter-decrement-based branches, but while that predicates
1355// the branching, it does not predicate the counter decrement. If we tried to
1356// merge the triangle into one predicated block, we'd decrement the counter
1357// twice.
1358bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
1359 unsigned NumT, unsigned ExtraT,
1360 MachineBasicBlock &FMBB,
1361 unsigned NumF, unsigned ExtraF,
Cong Houc536bd92015-09-10 23:10:42 +00001362 BranchProbability Probability) const {
Hal Finkel30ae2292013-04-10 18:30:16 +00001363 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
1364}
1365
1366
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001367bool PPCInstrInfo::isPredicated(const MachineInstr &MI) const {
Hal Finkelf29285a2013-04-11 01:23:34 +00001368 // The predicated branches are identified by their type, not really by the
1369 // explicit presence of a predicate. Furthermore, some of them can be
1370 // predicated more than once. Because if conversion won't try to predicate
1371 // any instruction which already claims to be predicated (by returning true
1372 // here), always return false. In doing so, we let isPredicable() be the
1373 // final word on whether not the instruction can be (further) predicated.
1374
1375 return false;
Hal Finkel5711eca2013-04-09 22:58:37 +00001376}
1377
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001378bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const {
1379 if (!MI.isTerminator())
Hal Finkel5711eca2013-04-09 22:58:37 +00001380 return false;
1381
1382 // Conditional branch is a special case.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001383 if (MI.isBranch() && !MI.isBarrier())
Hal Finkel5711eca2013-04-09 22:58:37 +00001384 return true;
1385
1386 return !isPredicated(MI);
1387}
1388
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001389bool PPCInstrInfo::PredicateInstruction(MachineInstr &MI,
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001390 ArrayRef<MachineOperand> Pred) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001391 unsigned OpC = MI.getOpcode();
Hal Finkelf4a22c02015-01-13 17:47:54 +00001392 if (OpC == PPC::BLR || OpC == PPC::BLR8) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001393 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001394 bool isPPC64 = Subtarget.isPPC64();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001395 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR)
1396 : (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
Hal Finkel940ab932014-02-28 00:27:01 +00001397 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001398 MI.setDesc(get(PPC::BCLR));
1399 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1400 .addReg(Pred[1].getReg());
Hal Finkel940ab932014-02-28 00:27:01 +00001401 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001402 MI.setDesc(get(PPC::BCLRn));
1403 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1404 .addReg(Pred[1].getReg());
Hal Finkel940ab932014-02-28 00:27:01 +00001405 } else {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001406 MI.setDesc(get(PPC::BCCLR));
1407 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1408 .addImm(Pred[0].getImm())
1409 .addReg(Pred[1].getReg());
Hal Finkel5711eca2013-04-09 22:58:37 +00001410 }
1411
1412 return true;
1413 } else if (OpC == PPC::B) {
1414 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001415 bool isPPC64 = Subtarget.isPPC64();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001416 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ)
1417 : (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
Hal Finkel940ab932014-02-28 00:27:01 +00001418 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001419 MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1420 MI.RemoveOperand(0);
Hal Finkel940ab932014-02-28 00:27:01 +00001421
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001422 MI.setDesc(get(PPC::BC));
1423 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1424 .addReg(Pred[1].getReg())
1425 .addMBB(MBB);
Hal Finkel940ab932014-02-28 00:27:01 +00001426 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001427 MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1428 MI.RemoveOperand(0);
Hal Finkel940ab932014-02-28 00:27:01 +00001429
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001430 MI.setDesc(get(PPC::BCn));
1431 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1432 .addReg(Pred[1].getReg())
1433 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001434 } else {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001435 MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1436 MI.RemoveOperand(0);
Hal Finkel5711eca2013-04-09 22:58:37 +00001437
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001438 MI.setDesc(get(PPC::BCC));
1439 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1440 .addImm(Pred[0].getImm())
1441 .addReg(Pred[1].getReg())
1442 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001443 }
1444
1445 return true;
Hal Finkel500b0042013-04-10 06:42:34 +00001446 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1447 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1448 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1449 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1450
1451 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
Eric Christopher1dcea732014-06-12 21:48:52 +00001452 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel940ab932014-02-28 00:27:01 +00001453
1454 if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001455 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8)
1456 : (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1457 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1458 .addReg(Pred[1].getReg());
Hal Finkel940ab932014-02-28 00:27:01 +00001459 return true;
1460 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001461 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n)
1462 : (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1463 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1464 .addReg(Pred[1].getReg());
Hal Finkel940ab932014-02-28 00:27:01 +00001465 return true;
1466 }
1467
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001468 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8)
1469 : (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
1470 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1471 .addImm(Pred[0].getImm())
1472 .addReg(Pred[1].getReg());
Hal Finkel500b0042013-04-10 06:42:34 +00001473 return true;
Hal Finkel5711eca2013-04-09 22:58:37 +00001474 }
1475
1476 return false;
1477}
1478
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001479bool PPCInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1480 ArrayRef<MachineOperand> Pred2) const {
Hal Finkel5711eca2013-04-09 22:58:37 +00001481 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1482 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1483
1484 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1485 return false;
1486 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1487 return false;
1488
Hal Finkel94a6f382013-12-11 23:12:25 +00001489 // P1 can only subsume P2 if they test the same condition register.
1490 if (Pred1[1].getReg() != Pred2[1].getReg())
1491 return false;
1492
Hal Finkel5711eca2013-04-09 22:58:37 +00001493 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1494 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1495
1496 if (P1 == P2)
1497 return true;
1498
1499 // Does P1 subsume P2, e.g. GE subsumes GT.
1500 if (P1 == PPC::PRED_LE &&
1501 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1502 return true;
1503 if (P1 == PPC::PRED_GE &&
1504 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1505 return true;
1506
1507 return false;
1508}
1509
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001510bool PPCInstrInfo::DefinesPredicate(MachineInstr &MI,
Hal Finkel5711eca2013-04-09 22:58:37 +00001511 std::vector<MachineOperand> &Pred) const {
1512 // Note: At the present time, the contents of Pred from this function is
1513 // unused by IfConversion. This implementation follows ARM by pushing the
1514 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1515 // predicate, instructions defining CTR or CTR8 are also included as
1516 // predicate-defining instructions.
1517
1518 const TargetRegisterClass *RCs[] =
1519 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1520 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1521
1522 bool Found = false;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001523 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1524 const MachineOperand &MO = MI.getOperand(i);
Hal Finkelaf822012013-04-10 07:17:47 +00001525 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001526 const TargetRegisterClass *RC = RCs[c];
Hal Finkelaf822012013-04-10 07:17:47 +00001527 if (MO.isReg()) {
1528 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001529 Pred.push_back(MO);
1530 Found = true;
1531 }
Hal Finkelaf822012013-04-10 07:17:47 +00001532 } else if (MO.isRegMask()) {
1533 for (TargetRegisterClass::iterator I = RC->begin(),
1534 IE = RC->end(); I != IE; ++I)
1535 if (MO.clobbersPhysReg(*I)) {
1536 Pred.push_back(MO);
1537 Found = true;
1538 }
Hal Finkel5711eca2013-04-09 22:58:37 +00001539 }
1540 }
1541 }
1542
1543 return Found;
1544}
1545
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +00001546bool PPCInstrInfo::isPredicable(const MachineInstr &MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001547 unsigned OpC = MI.getOpcode();
Hal Finkel5711eca2013-04-09 22:58:37 +00001548 switch (OpC) {
1549 default:
1550 return false;
1551 case PPC::B:
1552 case PPC::BLR:
Hal Finkelf4a22c02015-01-13 17:47:54 +00001553 case PPC::BLR8:
Hal Finkel500b0042013-04-10 06:42:34 +00001554 case PPC::BCTR:
1555 case PPC::BCTR8:
1556 case PPC::BCTRL:
1557 case PPC::BCTRL8:
Hal Finkel5711eca2013-04-09 22:58:37 +00001558 return true;
1559 }
1560}
1561
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001562bool PPCInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1563 unsigned &SrcReg2, int &Mask,
1564 int &Value) const {
1565 unsigned Opc = MI.getOpcode();
Hal Finkel82656cb2013-04-18 22:15:08 +00001566
1567 switch (Opc) {
1568 default: return false;
1569 case PPC::CMPWI:
1570 case PPC::CMPLWI:
1571 case PPC::CMPDI:
1572 case PPC::CMPLDI:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001573 SrcReg = MI.getOperand(1).getReg();
Hal Finkel82656cb2013-04-18 22:15:08 +00001574 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001575 Value = MI.getOperand(2).getImm();
Hal Finkel82656cb2013-04-18 22:15:08 +00001576 Mask = 0xFFFF;
1577 return true;
1578 case PPC::CMPW:
1579 case PPC::CMPLW:
1580 case PPC::CMPD:
1581 case PPC::CMPLD:
1582 case PPC::FCMPUS:
1583 case PPC::FCMPUD:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001584 SrcReg = MI.getOperand(1).getReg();
1585 SrcReg2 = MI.getOperand(2).getReg();
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001586 Value = 0;
1587 Mask = 0;
Hal Finkel82656cb2013-04-18 22:15:08 +00001588 return true;
1589 }
1590}
Hal Finkele6322392013-04-19 22:08:38 +00001591
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001592bool PPCInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, unsigned SrcReg,
1593 unsigned SrcReg2, int Mask, int Value,
Hal Finkel82656cb2013-04-18 22:15:08 +00001594 const MachineRegisterInfo *MRI) const {
Hal Finkelb12da6b2013-04-18 22:54:25 +00001595 if (DisableCmpOpt)
1596 return false;
1597
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001598 int OpC = CmpInstr.getOpcode();
1599 unsigned CRReg = CmpInstr.getOperand(0).getReg();
Hal Finkel08e53ee2013-05-08 12:16:14 +00001600
1601 // FP record forms set CR1 based on the execption status bits, not a
1602 // comparison with zero.
1603 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1604 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001605
1606 // The record forms set the condition register based on a signed comparison
1607 // with zero (so says the ISA manual). This is not as straightforward as it
1608 // seems, however, because this is always a 64-bit comparison on PPC64, even
1609 // for instructions that are 32-bit in nature (like slw for example).
1610 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1611 // for equality checks (as those don't depend on the sign). On PPC64,
1612 // we are restricted to equality for unsigned 64-bit comparisons and for
1613 // signed 32-bit comparisons the applicability is more restricted.
Eric Christopher1dcea732014-06-12 21:48:52 +00001614 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel82656cb2013-04-18 22:15:08 +00001615 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1616 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1617 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1618
1619 // Get the unique definition of SrcReg.
1620 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1621 if (!MI) return false;
1622 int MIOpC = MI->getOpcode();
1623
1624 bool equalityOnly = false;
1625 bool noSub = false;
1626 if (isPPC64) {
1627 if (is32BitSignedCompare) {
1628 // We can perform this optimization only if MI is sign-extending.
1629 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1630 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1631 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1632 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1633 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1634 noSub = true;
1635 } else
1636 return false;
1637 } else if (is32BitUnsignedCompare) {
Kyle Butt61311282016-03-23 19:51:22 +00001638 // 32-bit rotate and mask instructions are zero extending only if MB <= ME
1639 bool isZeroExtendingRotate =
1640 (MIOpC == PPC::RLWINM || MIOpC == PPC::RLWINMo ||
1641 MIOpC == PPC::RLWNM || MIOpC == PPC::RLWNMo)
1642 && MI->getOperand(3).getImm() <= MI->getOperand(4).getImm();
1643
Hal Finkel82656cb2013-04-18 22:15:08 +00001644 // We can perform this optimization, equality only, if MI is
1645 // zero-extending.
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001646 // FIXME: Other possible target instructions include ANDISo and
1647 // RLWINM aliases, such as ROTRWI, EXTLWI, SLWI and SRWI.
Hal Finkel82656cb2013-04-18 22:15:08 +00001648 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1649 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
Kyle Butt61311282016-03-23 19:51:22 +00001650 MIOpC == PPC::SRW || MIOpC == PPC::SRWo ||
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001651 MIOpC == PPC::ANDIo ||
Kyle Butt61311282016-03-23 19:51:22 +00001652 isZeroExtendingRotate) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001653 noSub = true;
1654 equalityOnly = true;
1655 } else
1656 return false;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001657 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001658 equalityOnly = is64BitUnsignedCompare;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001659 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001660 equalityOnly = is32BitUnsignedCompare;
1661
1662 if (equalityOnly) {
1663 // We need to check the uses of the condition register in order to reject
1664 // non-equality comparisons.
Hiroshi Inoue393ef842017-07-18 13:31:40 +00001665 for (MachineRegisterInfo::use_instr_iterator
1666 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1667 I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001668 MachineInstr *UseMI = &*I;
1669 if (UseMI->getOpcode() == PPC::BCC) {
Hiroshi Inoue967dc582017-07-27 08:14:48 +00001670 PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm();
1671 unsigned PredCond = PPC::getPredicateCondition(Pred);
1672 // We ignore hint bits when checking for non-equality comparisons.
1673 if (PredCond != PPC::PRED_EQ && PredCond != PPC::PRED_NE)
Hal Finkelc3632452013-05-07 17:49:55 +00001674 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001675 } else if (UseMI->getOpcode() == PPC::ISEL ||
1676 UseMI->getOpcode() == PPC::ISEL8) {
1677 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkelc3632452013-05-07 17:49:55 +00001678 if (SubIdx != PPC::sub_eq)
1679 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001680 } else
1681 return false;
1682 }
1683 }
1684
Hal Finkelc3632452013-05-07 17:49:55 +00001685 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001686
1687 // Scan forward to find the first use of the compare.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001688 for (MachineBasicBlock::iterator EL = CmpInstr.getParent()->end(); I != EL;
1689 ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001690 bool FoundUse = false;
Hiroshi Inoue393ef842017-07-18 13:31:40 +00001691 for (MachineRegisterInfo::use_instr_iterator
1692 J = MRI->use_instr_begin(CRReg), JE = MRI->use_instr_end();
1693 J != JE; ++J)
Hal Finkel82656cb2013-04-18 22:15:08 +00001694 if (&*J == &*I) {
1695 FoundUse = true;
1696 break;
1697 }
1698
1699 if (FoundUse)
1700 break;
1701 }
1702
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001703 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1704 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
1705
Hal Finkel82656cb2013-04-18 22:15:08 +00001706 // There are two possible candidates which can be changed to set CR[01].
1707 // One is MI, the other is a SUB instruction.
1708 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
Craig Topper062a2ba2014-04-25 05:30:21 +00001709 MachineInstr *Sub = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001710 if (SrcReg2 != 0)
1711 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00001712 MI = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001713 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1714 // same BB as the comparison. This is to allow the check below to avoid calls
1715 // (and other explicit clobbers); instead we should really check for these
1716 // more explicitly (in at least a few predecessors).
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001717 else if (MI->getParent() != CmpInstr.getParent())
Hal Finkel82656cb2013-04-18 22:15:08 +00001718 return false;
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001719 else if (Value != 0) {
1720 // The record-form instructions set CR bit based on signed comparison against 0.
1721 // We try to convert a compare against 1 or -1 into a compare against 0.
1722 bool Success = false;
1723 if (!equalityOnly && MRI->hasOneUse(CRReg)) {
1724 MachineInstr *UseMI = &*MRI->use_instr_begin(CRReg);
1725 if (UseMI->getOpcode() == PPC::BCC) {
1726 PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm();
Hiroshi Inoue967dc582017-07-27 08:14:48 +00001727 unsigned PredCond = PPC::getPredicateCondition(Pred);
1728 unsigned PredHint = PPC::getPredicateHint(Pred);
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001729 int16_t Immed = (int16_t)Value;
1730
Hiroshi Inoue967dc582017-07-27 08:14:48 +00001731 // When modyfing the condition in the predicate, we propagate hint bits
1732 // from the original predicate to the new one.
1733 if (Immed == -1 && PredCond == PPC::PRED_GT) {
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001734 // We convert "greater than -1" into "greater than or equal to 0",
1735 // since we are assuming signed comparison by !equalityOnly
1736 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
Hiroshi Inoue967dc582017-07-27 08:14:48 +00001737 PPC::getPredicate(PPC::PRED_GE, PredHint)));
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001738 Success = true;
1739 }
Hiroshi Inoue967dc582017-07-27 08:14:48 +00001740 else if (Immed == 1 && PredCond == PPC::PRED_LT) {
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001741 // We convert "less than 1" into "less than or equal to 0".
1742 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
Hiroshi Inoue967dc582017-07-27 08:14:48 +00001743 PPC::getPredicate(PPC::PRED_LE, PredHint)));
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001744 Success = true;
1745 }
1746 }
1747 }
1748
1749 // PPC does not have a record-form SUBri.
1750 if (!Success)
1751 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001752 }
1753
1754 // Search for Sub.
1755 const TargetRegisterInfo *TRI = &getRegisterInfo();
1756 --I;
Hal Finkelc3632452013-05-07 17:49:55 +00001757
1758 // Get ready to iterate backward from CmpInstr.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001759 MachineBasicBlock::iterator E = MI, B = CmpInstr.getParent()->begin();
Hal Finkelc3632452013-05-07 17:49:55 +00001760
Hal Finkel82656cb2013-04-18 22:15:08 +00001761 for (; I != E && !noSub; --I) {
1762 const MachineInstr &Instr = *I;
1763 unsigned IOpC = Instr.getOpcode();
1764
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001765 if (&*I != &CmpInstr && (Instr.modifiesRegister(PPC::CR0, TRI) ||
1766 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel82656cb2013-04-18 22:15:08 +00001767 // This instruction modifies or uses the record condition register after
1768 // the one we want to change. While we could do this transformation, it
1769 // would likely not be profitable. This transformation removes one
1770 // instruction, and so even forcing RA to generate one move probably
1771 // makes it unprofitable.
1772 return false;
1773
1774 // Check whether CmpInstr can be made redundant by the current instruction.
1775 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1776 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1777 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1778 ((Instr.getOperand(1).getReg() == SrcReg &&
1779 Instr.getOperand(2).getReg() == SrcReg2) ||
1780 (Instr.getOperand(1).getReg() == SrcReg2 &&
1781 Instr.getOperand(2).getReg() == SrcReg))) {
1782 Sub = &*I;
1783 break;
1784 }
1785
Hal Finkel82656cb2013-04-18 22:15:08 +00001786 if (I == B)
1787 // The 'and' is below the comparison instruction.
1788 return false;
1789 }
1790
1791 // Return false if no candidates exist.
1792 if (!MI && !Sub)
1793 return false;
1794
1795 // The single candidate is called MI.
1796 if (!MI) MI = Sub;
1797
1798 int NewOpC = -1;
1799 MIOpC = MI->getOpcode();
1800 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1801 NewOpC = MIOpC;
1802 else {
1803 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1804 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1805 NewOpC = MIOpC;
1806 }
1807
1808 // FIXME: On the non-embedded POWER architectures, only some of the record
1809 // forms are fast, and we should use only the fast ones.
1810
1811 // The defining instruction has a record form (or is already a record
1812 // form). It is possible, however, that we'll need to reverse the condition
1813 // code of the users.
1814 if (NewOpC == -1)
1815 return false;
1816
Hal Finkel82656cb2013-04-18 22:15:08 +00001817 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1818 // needs to be updated to be based on SUB. Push the condition code
1819 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1820 // condition code of these operands will be modified.
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001821 // Here, Value == 0 means we haven't converted comparison against 1 or -1 to
1822 // comparison against 0, which may modify predicate.
Hal Finkel82656cb2013-04-18 22:15:08 +00001823 bool ShouldSwap = false;
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001824 if (Sub && Value == 0) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001825 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1826 Sub->getOperand(2).getReg() == SrcReg;
1827
1828 // The operands to subf are the opposite of sub, so only in the fixed-point
1829 // case, invert the order.
Hal Finkel08e53ee2013-05-08 12:16:14 +00001830 ShouldSwap = !ShouldSwap;
Hal Finkel82656cb2013-04-18 22:15:08 +00001831 }
1832
1833 if (ShouldSwap)
Owen Anderson16c6bf42014-03-13 23:12:04 +00001834 for (MachineRegisterInfo::use_instr_iterator
1835 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1836 I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001837 MachineInstr *UseMI = &*I;
1838 if (UseMI->getOpcode() == PPC::BCC) {
1839 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hiroshi Inoue967dc582017-07-27 08:14:48 +00001840 unsigned PredCond = PPC::getPredicateCondition(Pred);
Hal Finkele6322392013-04-19 22:08:38 +00001841 assert((!equalityOnly ||
Hiroshi Inoue967dc582017-07-27 08:14:48 +00001842 PredCond == PPC::PRED_EQ || PredCond == PPC::PRED_NE) &&
Hal Finkele6322392013-04-19 22:08:38 +00001843 "Invalid predicate for equality-only optimization");
Hiroshi Inoue967dc582017-07-27 08:14:48 +00001844 (void)PredCond; // To suppress warning in release build.
Owen Anderson16c6bf42014-03-13 23:12:04 +00001845 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
Hal Finkel0f64e212013-04-20 05:16:26 +00001846 PPC::getSwappedPredicate(Pred)));
Hal Finkel82656cb2013-04-18 22:15:08 +00001847 } else if (UseMI->getOpcode() == PPC::ISEL ||
1848 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkele6322392013-04-19 22:08:38 +00001849 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1850 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1851 "Invalid CR bit for equality-only optimization");
1852
1853 if (NewSubReg == PPC::sub_lt)
1854 NewSubReg = PPC::sub_gt;
1855 else if (NewSubReg == PPC::sub_gt)
1856 NewSubReg = PPC::sub_lt;
1857
Owen Anderson16c6bf42014-03-13 23:12:04 +00001858 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
Hal Finkele6322392013-04-19 22:08:38 +00001859 NewSubReg));
Hal Finkel82656cb2013-04-18 22:15:08 +00001860 } else // We need to abort on a user we don't understand.
1861 return false;
1862 }
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001863 assert(!(Value != 0 && ShouldSwap) &&
1864 "Non-zero immediate support and ShouldSwap"
1865 "may conflict in updating predicate");
Hal Finkel82656cb2013-04-18 22:15:08 +00001866
1867 // Create a new virtual register to hold the value of the CR set by the
1868 // record-form instruction. If the instruction was not previously in
1869 // record form, then set the kill flag on the CR.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001870 CmpInstr.eraseFromParent();
Hal Finkel82656cb2013-04-18 22:15:08 +00001871
1872 MachineBasicBlock::iterator MII = MI;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001873 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
Hal Finkel82656cb2013-04-18 22:15:08 +00001874 get(TargetOpcode::COPY), CRReg)
Hal Finkel08e53ee2013-05-08 12:16:14 +00001875 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel82656cb2013-04-18 22:15:08 +00001876
Chuang-Yu Cheng94f58e72016-04-12 03:10:52 +00001877 // Even if CR0 register were dead before, it is alive now since the
1878 // instruction we just built uses it.
1879 MI->clearRegisterDeads(PPC::CR0);
1880
Hal Finkel82656cb2013-04-18 22:15:08 +00001881 if (MIOpC != NewOpC) {
1882 // We need to be careful here: we're replacing one instruction with
1883 // another, and we need to make sure that we get all of the right
1884 // implicit uses and defs. On the other hand, the caller may be holding
1885 // an iterator to this instruction, and so we can't delete it (this is
1886 // specifically the case if this is the instruction directly after the
1887 // compare).
1888
1889 const MCInstrDesc &NewDesc = get(NewOpC);
1890 MI->setDesc(NewDesc);
1891
1892 if (NewDesc.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +00001893 for (const MCPhysReg *ImpDefs = NewDesc.getImplicitDefs();
Hal Finkel82656cb2013-04-18 22:15:08 +00001894 *ImpDefs; ++ImpDefs)
1895 if (!MI->definesRegister(*ImpDefs))
1896 MI->addOperand(*MI->getParent()->getParent(),
1897 MachineOperand::CreateReg(*ImpDefs, true, true));
1898 if (NewDesc.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +00001899 for (const MCPhysReg *ImpUses = NewDesc.getImplicitUses();
Hal Finkel82656cb2013-04-18 22:15:08 +00001900 *ImpUses; ++ImpUses)
1901 if (!MI->readsRegister(*ImpUses))
1902 MI->addOperand(*MI->getParent()->getParent(),
1903 MachineOperand::CreateReg(*ImpUses, false, true));
1904 }
Keno Fischer55734832016-06-01 20:31:07 +00001905 assert(MI->definesRegister(PPC::CR0) &&
1906 "Record-form instruction does not define cr0?");
Hal Finkel82656cb2013-04-18 22:15:08 +00001907
1908 // Modify the condition code of operands in OperandsToUpdate.
1909 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1910 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkele6322392013-04-19 22:08:38 +00001911 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1912 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001913
Hal Finkele6322392013-04-19 22:08:38 +00001914 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1915 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001916
1917 return true;
1918}
1919
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001920/// GetInstSize - Return the number of bytes of code the specified
1921/// instruction may be. This returns the maximum number of bytes.
1922///
Sjoerd Meijer89217f82016-07-28 16:32:22 +00001923unsigned PPCInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001924 unsigned Opcode = MI.getOpcode();
Hal Finkela7bbaf62014-02-02 06:12:27 +00001925
1926 if (Opcode == PPC::INLINEASM) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001927 const MachineFunction *MF = MI.getParent()->getParent();
1928 const char *AsmStr = MI.getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +00001929 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Hal Finkel934361a2015-01-14 01:07:51 +00001930 } else if (Opcode == TargetOpcode::STACKMAP) {
Philip Reamese83c4b32016-08-23 23:33:29 +00001931 StackMapOpers Opers(&MI);
1932 return Opers.getNumPatchBytes();
Hal Finkel934361a2015-01-14 01:07:51 +00001933 } else if (Opcode == TargetOpcode::PATCHPOINT) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001934 PatchPointOpers Opers(&MI);
Philip Reamese83c4b32016-08-23 23:33:29 +00001935 return Opers.getNumPatchBytes();
Hal Finkela7bbaf62014-02-02 06:12:27 +00001936 } else {
Eric Christopherf48ef332017-03-27 22:40:51 +00001937 return get(Opcode).getSize();
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001938 }
1939}
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001940
Hal Finkel2d556982015-08-30 07:50:35 +00001941std::pair<unsigned, unsigned>
1942PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
1943 const unsigned Mask = PPCII::MO_ACCESS_MASK;
1944 return std::make_pair(TF & Mask, TF & ~Mask);
1945}
1946
1947ArrayRef<std::pair<unsigned, const char *>>
1948PPCInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
1949 using namespace PPCII;
Hal Finkel982e8d42015-08-30 08:07:29 +00001950 static const std::pair<unsigned, const char *> TargetFlags[] = {
Hal Finkel2d556982015-08-30 07:50:35 +00001951 {MO_LO, "ppc-lo"},
1952 {MO_HA, "ppc-ha"},
1953 {MO_TPREL_LO, "ppc-tprel-lo"},
1954 {MO_TPREL_HA, "ppc-tprel-ha"},
1955 {MO_DTPREL_LO, "ppc-dtprel-lo"},
1956 {MO_TLSLD_LO, "ppc-tlsld-lo"},
1957 {MO_TOC_LO, "ppc-toc-lo"},
1958 {MO_TLS, "ppc-tls"}};
1959 return makeArrayRef(TargetFlags);
1960}
1961
1962ArrayRef<std::pair<unsigned, const char *>>
1963PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
1964 using namespace PPCII;
Hal Finkel982e8d42015-08-30 08:07:29 +00001965 static const std::pair<unsigned, const char *> TargetFlags[] = {
Rafael Espindolaa99ccfc2016-06-29 14:59:50 +00001966 {MO_PLT, "ppc-plt"},
Hal Finkel2d556982015-08-30 07:50:35 +00001967 {MO_PIC_FLAG, "ppc-pic"},
1968 {MO_NLP_FLAG, "ppc-nlp"},
1969 {MO_NLP_HIDDEN_FLAG, "ppc-nlp-hidden"}};
1970 return makeArrayRef(TargetFlags);
1971}
1972
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001973bool PPCInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
Tim Shen3bef27c2017-05-16 20:18:06 +00001974 auto &MBB = *MI.getParent();
1975 auto DL = MI.getDebugLoc();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001976 switch (MI.getOpcode()) {
Tim Shena1d8bc52016-04-19 20:14:52 +00001977 case TargetOpcode::LOAD_STACK_GUARD: {
1978 assert(Subtarget.isTargetLinux() &&
1979 "Only Linux target is expected to contain LOAD_STACK_GUARD");
1980 const int64_t Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008;
1981 const unsigned Reg = Subtarget.isPPC64() ? PPC::X13 : PPC::R2;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001982 MI.setDesc(get(Subtarget.isPPC64() ? PPC::LD : PPC::LWZ));
1983 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
Tim Shena1d8bc52016-04-19 20:14:52 +00001984 .addImm(Offset)
1985 .addReg(Reg);
1986 return true;
1987 }
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00001988 case PPC::DFLOADf32:
1989 case PPC::DFLOADf64:
1990 case PPC::DFSTOREf32:
1991 case PPC::DFSTOREf64: {
1992 assert(Subtarget.hasP9Vector() &&
1993 "Invalid D-Form Pseudo-ops on non-P9 target.");
Hiroshi Inouee3c14eb2017-05-29 07:12:39 +00001994 assert(MI.getOperand(2).isReg() && MI.getOperand(1).isImm() &&
1995 "D-form op must have register and immediate operands");
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00001996 unsigned UpperOpcode, LowerOpcode;
1997 switch (MI.getOpcode()) {
1998 case PPC::DFLOADf32:
1999 UpperOpcode = PPC::LXSSP;
2000 LowerOpcode = PPC::LFS;
2001 break;
2002 case PPC::DFLOADf64:
2003 UpperOpcode = PPC::LXSD;
2004 LowerOpcode = PPC::LFD;
2005 break;
2006 case PPC::DFSTOREf32:
2007 UpperOpcode = PPC::STXSSP;
2008 LowerOpcode = PPC::STFS;
2009 break;
2010 case PPC::DFSTOREf64:
2011 UpperOpcode = PPC::STXSD;
2012 LowerOpcode = PPC::STFD;
2013 break;
2014 }
2015 unsigned TargetReg = MI.getOperand(0).getReg();
2016 unsigned Opcode;
2017 if ((TargetReg >= PPC::F0 && TargetReg <= PPC::F31) ||
2018 (TargetReg >= PPC::VSL0 && TargetReg <= PPC::VSL31))
2019 Opcode = LowerOpcode;
2020 else
2021 Opcode = UpperOpcode;
2022 MI.setDesc(get(Opcode));
2023 return true;
2024 }
Zaara Syedafcd96972017-09-21 16:12:33 +00002025 case PPC::SPILLTOVSR_LD: {
2026 unsigned TargetReg = MI.getOperand(0).getReg();
2027 if (PPC::VSFRCRegClass.contains(TargetReg)) {
2028 MI.setDesc(get(PPC::DFLOADf64));
2029 return expandPostRAPseudo(MI);
2030 }
2031 else
2032 MI.setDesc(get(PPC::LD));
2033 return true;
2034 }
2035 case PPC::SPILLTOVSR_ST: {
2036 unsigned SrcReg = MI.getOperand(0).getReg();
2037 if (PPC::VSFRCRegClass.contains(SrcReg)) {
2038 NumStoreSPILLVSRRCAsVec++;
2039 MI.setDesc(get(PPC::DFSTOREf64));
2040 return expandPostRAPseudo(MI);
2041 } else {
2042 NumStoreSPILLVSRRCAsGpr++;
2043 MI.setDesc(get(PPC::STD));
2044 }
2045 return true;
2046 }
2047 case PPC::SPILLTOVSR_LDX: {
2048 unsigned TargetReg = MI.getOperand(0).getReg();
2049 if (PPC::VSFRCRegClass.contains(TargetReg))
2050 MI.setDesc(get(PPC::LXSDX));
2051 else
2052 MI.setDesc(get(PPC::LDX));
2053 return true;
2054 }
2055 case PPC::SPILLTOVSR_STX: {
2056 unsigned SrcReg = MI.getOperand(0).getReg();
2057 if (PPC::VSFRCRegClass.contains(SrcReg)) {
2058 NumStoreSPILLVSRRCAsVec++;
2059 MI.setDesc(get(PPC::STXSDX));
2060 } else {
2061 NumStoreSPILLVSRRCAsGpr++;
2062 MI.setDesc(get(PPC::STDX));
2063 }
2064 return true;
2065 }
2066
Tim Shen3bef27c2017-05-16 20:18:06 +00002067 case PPC::CFENCE8: {
2068 auto Val = MI.getOperand(0).getReg();
Hiroshi Inoue7a08bb12017-06-15 16:51:28 +00002069 BuildMI(MBB, MI, DL, get(PPC::CMPD), PPC::CR7).addReg(Val).addReg(Val);
Tim Shen3bef27c2017-05-16 20:18:06 +00002070 BuildMI(MBB, MI, DL, get(PPC::CTRL_DEP))
2071 .addImm(PPC::PRED_NE_MINUS)
2072 .addReg(PPC::CR7)
2073 .addImm(1);
2074 MI.setDesc(get(PPC::ISYNC));
2075 MI.RemoveOperand(0);
2076 return true;
2077 }
Tim Shena1d8bc52016-04-19 20:14:52 +00002078 }
2079 return false;
2080}
Nemanja Ivanovic11049f82016-10-04 06:59:23 +00002081
2082const TargetRegisterClass *
2083PPCInstrInfo::updatedRC(const TargetRegisterClass *RC) const {
2084 if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass)
2085 return &PPC::VSRCRegClass;
2086 return RC;
2087}
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00002088
2089int PPCInstrInfo::getRecordFormOpcode(unsigned Opcode) {
2090 return PPC::getRecordFormOpcode(Opcode);
2091}