blob: b302d41f203a5706a07b0f8963c00da15f48b4fc [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- PPCInstrInfo.cpp - PowerPC Instruction Information ----------------===//
Misha Brukmanb4402432005-04-21 23:30:14 +00002//
Misha Brukman116f9272004-08-17 04:55:41 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb4402432005-04-21 23:30:14 +00007//
Misha Brukman116f9272004-08-17 04:55:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains the PowerPC implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner6f3b9542005-10-14 23:59:06 +000014#include "PPCInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "MCTargetDesc/PPCPredicates.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000016#include "PPC.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "PPCHazardRecognizers.h"
Owen Andersoneee14602008-01-01 21:11:32 +000018#include "PPCInstrBuilder.h"
Bill Wendling632ea652008-03-03 22:19:16 +000019#include "PPCMachineFunctionInfo.h"
Chris Lattner49cadab2006-06-17 00:01:04 +000020#include "PPCTargetMachine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000022#include "llvm/ADT/Statistic.h"
Hal Finkel174e5902014-03-25 23:29:21 +000023#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Hal Finkelb5aa7e52013-04-08 16:24:03 +000025#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman116f9272004-08-17 04:55:41 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000027#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesenddbf7a82010-02-26 21:09:24 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Hal Finkel9f9f8922012-04-01 19:22:40 +000029#include "llvm/CodeGen/PseudoSourceValue.h"
Eric Christopher1dcea732014-06-12 21:48:52 +000030#include "llvm/CodeGen/ScheduleDAG.h"
Hal Finkel174e5902014-03-25 23:29:21 +000031#include "llvm/CodeGen/SlotIndexes.h"
Hal Finkel934361a2015-01-14 01:07:51 +000032#include "llvm/CodeGen/StackMaps.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000033#include "llvm/MC/MCAsmInfo.h"
Pete Cooper3de83e42015-05-15 21:58:42 +000034#include "llvm/MC/MCInst.h"
Bill Wendling1af20ad2008-03-04 23:13:51 +000035#include "llvm/Support/CommandLine.h"
Hal Finkel174e5902014-03-25 23:29:21 +000036#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000037#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000038#include "llvm/Support/TargetRegistry.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000039#include "llvm/Support/raw_ostream.h"
Misha Brukman116f9272004-08-17 04:55:41 +000040
Dan Gohman20857192010-04-15 17:20:57 +000041using namespace llvm;
Bill Wendling1af20ad2008-03-04 23:13:51 +000042
Chandler Carruthe96dd892014-04-21 22:55:11 +000043#define DEBUG_TYPE "ppc-instr-info"
44
Chandler Carruthd174b722014-04-22 02:03:14 +000045#define GET_INSTRMAP_INFO
46#define GET_INSTRINFO_CTOR_DTOR
47#include "PPCGenInstrInfo.inc"
48
Hal Finkel821e0012012-06-08 15:38:25 +000049static cl::
Hal Finkelc6b5deb2012-06-08 19:19:53 +000050opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
51 cl::desc("Disable analysis for CTR loops"));
Hal Finkel821e0012012-06-08 15:38:25 +000052
Hal Finkele6322392013-04-19 22:08:38 +000053static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt",
Hal Finkelb12da6b2013-04-18 22:54:25 +000054cl::desc("Disable compare instruction optimization"), cl::Hidden);
55
Hal Finkel9dcb3582014-03-27 22:46:28 +000056static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy",
57cl::desc("Causes the backend to crash instead of generating a nop VSX copy"),
58cl::Hidden);
59
Hal Finkel8acae522015-07-14 20:02:02 +000060static cl::opt<bool>
61UseOldLatencyCalc("ppc-old-latency-calc", cl::Hidden,
62 cl::desc("Use the old (incorrect) instruction latency calculation"));
63
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000064// Pin the vtable to this file.
65void PPCInstrInfo::anchor() {}
66
Eric Christopher1dcea732014-06-12 21:48:52 +000067PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI)
68 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
Eric Christopherea178cf2015-03-12 01:42:51 +000069 Subtarget(STI), RI(STI.getTargetMachine()) {}
Chris Lattner49cadab2006-06-17 00:01:04 +000070
Andrew Trick10ffc2b2010-12-24 05:03:26 +000071/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
72/// this target when scheduling the DAG.
Eric Christopherf047bfd2014-06-13 22:38:52 +000073ScheduleHazardRecognizer *
74PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
75 const ScheduleDAG *DAG) const {
76 unsigned Directive =
77 static_cast<const PPCSubtarget *>(STI)->getDarwinDirective();
Hal Finkel742b5352012-08-28 16:12:39 +000078 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
79 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
Eric Christopherf047bfd2014-06-13 22:38:52 +000080 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +000081 static_cast<const PPCSubtarget *>(STI)->getInstrItineraryData();
Hal Finkel563cc052013-12-02 23:52:46 +000082 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel6fa56972011-10-17 04:03:49 +000083 }
Hal Finkel58ca3602011-12-02 04:58:02 +000084
Eric Christopherf047bfd2014-06-13 22:38:52 +000085 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000086}
87
Hal Finkel58ca3602011-12-02 04:58:02 +000088/// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
89/// to use for this target when scheduling the DAG.
Eric Christophercccae792015-01-30 22:02:31 +000090ScheduleHazardRecognizer *
91PPCInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
92 const ScheduleDAG *DAG) const {
Eric Christopher1dcea732014-06-12 21:48:52 +000093 unsigned Directive =
Eric Christophercccae792015-01-30 22:02:31 +000094 DAG->MF.getSubtarget<PPCSubtarget>().getDarwinDirective();
Hal Finkel58ca3602011-12-02 04:58:02 +000095
Will Schmidt970ff642014-06-26 13:36:19 +000096 if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8)
Hal Finkelceb1f122013-12-12 00:19:11 +000097 return new PPCDispatchGroupSBHazardRecognizer(II, DAG);
98
Hal Finkel58ca3602011-12-02 04:58:02 +000099 // Most subtargets use a PPC970 recognizer.
Hal Finkel742b5352012-08-28 16:12:39 +0000100 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
101 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
Eric Christopher1dcea732014-06-12 21:48:52 +0000102 assert(DAG->TII && "No InstrInfo?");
Hal Finkel58ca3602011-12-02 04:58:02 +0000103
Eric Christopher1dcea732014-06-12 21:48:52 +0000104 return new PPCHazardRecognizer970(*DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000105 }
106
Hal Finkel563cc052013-12-02 23:52:46 +0000107 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000108}
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000109
Hal Finkel8acae522015-07-14 20:02:02 +0000110unsigned PPCInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
111 const MachineInstr *MI,
112 unsigned *PredCost) const {
113 if (!ItinData || UseOldLatencyCalc)
114 return PPCGenInstrInfo::getInstrLatency(ItinData, MI, PredCost);
115
116 // The default implementation of getInstrLatency calls getStageLatency, but
117 // getStageLatency does not do the right thing for us. While we have
118 // itinerary, most cores are fully pipelined, and so the itineraries only
119 // express the first part of the pipeline, not every stage. Instead, we need
120 // to use the listed output operand cycle number (using operand 0 here, which
121 // is an output).
122
123 unsigned Latency = 1;
124 unsigned DefClass = MI->getDesc().getSchedClass();
125 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
126 const MachineOperand &MO = MI->getOperand(i);
127 if (!MO.isReg() || !MO.isDef() || MO.isImplicit())
128 continue;
129
130 int Cycle = ItinData->getOperandCycle(DefClass, i);
131 if (Cycle < 0)
132 continue;
133
134 Latency = std::max(Latency, (unsigned) Cycle);
135 }
136
137 return Latency;
138}
Hal Finkelceb1f122013-12-12 00:19:11 +0000139
140int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
141 const MachineInstr *DefMI, unsigned DefIdx,
142 const MachineInstr *UseMI,
143 unsigned UseIdx) const {
144 int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
145 UseMI, UseIdx);
146
Hal Finkel5d36b232015-07-15 08:23:05 +0000147 if (!DefMI->getParent())
148 return Latency;
149
Hal Finkelceb1f122013-12-12 00:19:11 +0000150 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
151 unsigned Reg = DefMO.getReg();
152
Hal Finkelceb1f122013-12-12 00:19:11 +0000153 bool IsRegCR;
Andrew Kaylor5c73e1f2015-03-24 23:37:10 +0000154 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Hal Finkelceb1f122013-12-12 00:19:11 +0000155 const MachineRegisterInfo *MRI =
156 &DefMI->getParent()->getParent()->getRegInfo();
157 IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) ||
158 MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass);
159 } else {
160 IsRegCR = PPC::CRRCRegClass.contains(Reg) ||
161 PPC::CRBITRCRegClass.contains(Reg);
162 }
163
164 if (UseMI->isBranch() && IsRegCR) {
165 if (Latency < 0)
166 Latency = getInstrLatency(ItinData, DefMI);
167
168 // On some cores, there is an additional delay between writing to a condition
169 // register, and using it from a branch.
Eric Christopher1dcea732014-06-12 21:48:52 +0000170 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000171 switch (Directive) {
172 default: break;
173 case PPC::DIR_7400:
174 case PPC::DIR_750:
175 case PPC::DIR_970:
176 case PPC::DIR_E5500:
177 case PPC::DIR_PWR4:
178 case PPC::DIR_PWR5:
179 case PPC::DIR_PWR5X:
180 case PPC::DIR_PWR6:
181 case PPC::DIR_PWR6X:
182 case PPC::DIR_PWR7:
Will Schmidt970ff642014-06-26 13:36:19 +0000183 case PPC::DIR_PWR8:
Hal Finkelceb1f122013-12-12 00:19:11 +0000184 Latency += 2;
185 break;
186 }
187 }
188
189 return Latency;
190}
191
Hal Finkel5d36b232015-07-15 08:23:05 +0000192static bool hasVirtualRegDefsInBasicBlock(const MachineInstr &Inst,
193 const MachineBasicBlock *MBB) {
194 const MachineOperand &Op1 = Inst.getOperand(1);
195 const MachineOperand &Op2 = Inst.getOperand(2);
196 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
197
198 // We need virtual register definitions.
199 MachineInstr *MI1 = nullptr;
200 MachineInstr *MI2 = nullptr;
201 if (Op1.isReg() && TargetRegisterInfo::isVirtualRegister(Op1.getReg()))
202 MI1 = MRI.getUniqueVRegDef(Op1.getReg());
203 if (Op2.isReg() && TargetRegisterInfo::isVirtualRegister(Op2.getReg()))
204 MI2 = MRI.getUniqueVRegDef(Op2.getReg());
205
206 // And they need to be in the trace (otherwise, they won't have a depth).
207 if (MI1 && MI2 && MI1->getParent() == MBB && MI2->getParent() == MBB)
208 return true;
209
210 return false;
211}
212
213static bool hasReassocSibling(const MachineInstr &Inst, bool &Commuted) {
214 const MachineBasicBlock *MBB = Inst.getParent();
215 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
216 MachineInstr *MI1 = MRI.getUniqueVRegDef(Inst.getOperand(1).getReg());
217 MachineInstr *MI2 = MRI.getUniqueVRegDef(Inst.getOperand(2).getReg());
218 unsigned AssocOpcode = Inst.getOpcode();
219
220 // If only one operand has the same opcode and it's the second source operand,
221 // the operands must be commuted.
222 Commuted = MI1->getOpcode() != AssocOpcode && MI2->getOpcode() == AssocOpcode;
223 if (Commuted)
224 std::swap(MI1, MI2);
225
226 // 1. The previous instruction must be the same type as Inst.
227 // 2. The previous instruction must have virtual register definitions for its
228 // operands in the same basic block as Inst.
229 // 3. The previous instruction's result must only be used by Inst.
230 if (MI1->getOpcode() == AssocOpcode &&
231 hasVirtualRegDefsInBasicBlock(*MI1, MBB) &&
232 MRI.hasOneNonDBGUse(MI1->getOperand(0).getReg()))
233 return true;
234
235 return false;
236}
237
238// This function does not list all associative and commutative operations, but
239// only those worth feeding through the machine combiner in an attempt to
240// reduce the critical path. Mostly, this means floating-point operations,
241// because they have high latencies (compared to other operations, such and
242// and/or, which are also associative and commutative, but have low latencies).
243//
244// The concept is that these operations can benefit from this kind of
245// transformation:
246//
247// A = ? op ?
248// B = A op X
249// C = B op Y
250// -->
251// A = ? op ?
252// B = X op Y
253// C = A op B
254//
255// breaking the dependency between A and B, allowing them to be executed in
256// parallel (or back-to-back in a pipeline) instead of depending on each other.
257static bool isAssociativeAndCommutative(unsigned Opcode) {
258 switch (Opcode) {
259 // FP Add:
260 case PPC::FADD:
261 case PPC::FADDS:
262 // FP Multiply:
263 case PPC::FMUL:
264 case PPC::FMULS:
265 // Altivec Add:
266 case PPC::VADDFP:
267 // VSX Add:
268 case PPC::XSADDDP:
269 case PPC::XVADDDP:
270 case PPC::XVADDSP:
271 case PPC::XSADDSP:
272 // VSX Multiply:
273 case PPC::XSMULDP:
274 case PPC::XVMULDP:
275 case PPC::XVMULSP:
276 case PPC::XSMULSP:
277 // QPX Add:
278 case PPC::QVFADD:
279 case PPC::QVFADDS:
280 case PPC::QVFADDSs:
281 // QPX Multiply:
282 case PPC::QVFMUL:
283 case PPC::QVFMULS:
284 case PPC::QVFMULSs:
285 return true;
286 default:
287 return false;
288 }
289}
290
291/// Return true if the input instruction is part of a chain of dependent ops
292/// that are suitable for reassociation, otherwise return false.
293/// If the instruction's operands must be commuted to have a previous
294/// instruction of the same type define the first source operand, Commuted will
295/// be set to true.
296static bool isReassocCandidate(const MachineInstr &Inst, bool &Commuted) {
297 // 1. The operation must be associative and commutative.
298 // 2. The instruction must have virtual register definitions for its
299 // operands in the same basic block.
300 // 3. The instruction must have a reassociable sibling.
301 if (isAssociativeAndCommutative(Inst.getOpcode()) &&
302 hasVirtualRegDefsInBasicBlock(Inst, Inst.getParent()) &&
303 hasReassocSibling(Inst, Commuted))
304 return true;
305
306 return false;
307}
308
309bool PPCInstrInfo::getMachineCombinerPatterns(MachineInstr &Root,
310 SmallVectorImpl<MachineCombinerPattern::MC_PATTERN> &Patterns) const {
311 // Using the machine combiner in this way is potentially expensive, so
312 // restrict to when aggressive optimizations are desired.
313 if (Subtarget.getTargetMachine().getOptLevel() != CodeGenOpt::Aggressive)
314 return false;
315
316 // FP reassociation is only legal when we don't need strict IEEE semantics.
317 if (!Root.getParent()->getParent()->getTarget().Options.UnsafeFPMath)
318 return false;
319
320 // Look for this reassociation pattern:
321 // B = A op X (Prev)
322 // C = B op Y (Root)
323
324 // FIXME: We should also match FMA operations here, where we consider the
325 // 'part' of the FMA, either the addition or the multiplication, paired with
326 // an actual addition or multiplication.
327
328 bool Commute;
329 if (isReassocCandidate(Root, Commute)) {
330 // We found a sequence of instructions that may be suitable for a
331 // reassociation of operands to increase ILP. Specify each commutation
332 // possibility for the Prev instruction in the sequence and let the
333 // machine combiner decide if changing the operands is worthwhile.
334 if (Commute) {
335 Patterns.push_back(MachineCombinerPattern::MC_REASSOC_AX_YB);
336 Patterns.push_back(MachineCombinerPattern::MC_REASSOC_XA_YB);
337 } else {
338 Patterns.push_back(MachineCombinerPattern::MC_REASSOC_AX_BY);
339 Patterns.push_back(MachineCombinerPattern::MC_REASSOC_XA_BY);
340 }
341 return true;
342 }
343
344 return false;
345}
346
347/// Attempt the following reassociation to reduce critical path length:
348/// B = A op X (Prev)
349/// C = B op Y (Root)
350/// ===>
351/// B = X op Y
352/// C = A op B
353static void reassociateOps(MachineInstr &Root, MachineInstr &Prev,
354 MachineCombinerPattern::MC_PATTERN Pattern,
355 SmallVectorImpl<MachineInstr *> &InsInstrs,
356 SmallVectorImpl<MachineInstr *> &DelInstrs,
357 DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) {
358 MachineFunction *MF = Root.getParent()->getParent();
359 MachineRegisterInfo &MRI = MF->getRegInfo();
360 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
361 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
362 const TargetRegisterClass *RC = Root.getRegClassConstraint(0, TII, TRI);
363
364 // This array encodes the operand index for each parameter because the
365 // operands may be commuted. Each row corresponds to a pattern value,
366 // and each column specifies the index of A, B, X, Y.
367 unsigned OpIdx[4][4] = {
368 { 1, 1, 2, 2 },
369 { 1, 2, 2, 1 },
370 { 2, 1, 1, 2 },
371 { 2, 2, 1, 1 }
372 };
373
374 MachineOperand &OpA = Prev.getOperand(OpIdx[Pattern][0]);
375 MachineOperand &OpB = Root.getOperand(OpIdx[Pattern][1]);
376 MachineOperand &OpX = Prev.getOperand(OpIdx[Pattern][2]);
377 MachineOperand &OpY = Root.getOperand(OpIdx[Pattern][3]);
378 MachineOperand &OpC = Root.getOperand(0);
379
380 unsigned RegA = OpA.getReg();
381 unsigned RegB = OpB.getReg();
382 unsigned RegX = OpX.getReg();
383 unsigned RegY = OpY.getReg();
384 unsigned RegC = OpC.getReg();
385
386 if (TargetRegisterInfo::isVirtualRegister(RegA))
387 MRI.constrainRegClass(RegA, RC);
388 if (TargetRegisterInfo::isVirtualRegister(RegB))
389 MRI.constrainRegClass(RegB, RC);
390 if (TargetRegisterInfo::isVirtualRegister(RegX))
391 MRI.constrainRegClass(RegX, RC);
392 if (TargetRegisterInfo::isVirtualRegister(RegY))
393 MRI.constrainRegClass(RegY, RC);
394 if (TargetRegisterInfo::isVirtualRegister(RegC))
395 MRI.constrainRegClass(RegC, RC);
396
397 // Create a new virtual register for the result of (X op Y) instead of
398 // recycling RegB because the MachineCombiner's computation of the critical
399 // path requires a new register definition rather than an existing one.
400 unsigned NewVR = MRI.createVirtualRegister(RC);
401 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
402
403 unsigned Opcode = Root.getOpcode();
404 bool KillA = OpA.isKill();
405 bool KillX = OpX.isKill();
406 bool KillY = OpY.isKill();
407
408 // Create new instructions for insertion.
409 MachineInstrBuilder MIB1 =
410 BuildMI(*MF, Prev.getDebugLoc(), TII->get(Opcode), NewVR)
411 .addReg(RegX, getKillRegState(KillX))
412 .addReg(RegY, getKillRegState(KillY));
413 InsInstrs.push_back(MIB1);
414
415 MachineInstrBuilder MIB2 =
416 BuildMI(*MF, Root.getDebugLoc(), TII->get(Opcode), RegC)
417 .addReg(RegA, getKillRegState(KillA))
418 .addReg(NewVR, getKillRegState(true));
419 InsInstrs.push_back(MIB2);
420
421 // Record old instructions for deletion.
422 DelInstrs.push_back(&Prev);
423 DelInstrs.push_back(&Root);
424}
425
426void PPCInstrInfo::genAlternativeCodeSequence(
427 MachineInstr &Root,
428 MachineCombinerPattern::MC_PATTERN Pattern,
429 SmallVectorImpl<MachineInstr *> &InsInstrs,
430 SmallVectorImpl<MachineInstr *> &DelInstrs,
431 DenseMap<unsigned, unsigned> &InstIdxForVirtReg) const {
432 MachineRegisterInfo &MRI = Root.getParent()->getParent()->getRegInfo();
433
434 // Select the previous instruction in the sequence based on the input pattern.
435 MachineInstr *Prev = nullptr;
436 switch (Pattern) {
437 case MachineCombinerPattern::MC_REASSOC_AX_BY:
438 case MachineCombinerPattern::MC_REASSOC_XA_BY:
439 Prev = MRI.getUniqueVRegDef(Root.getOperand(1).getReg());
440 break;
441 case MachineCombinerPattern::MC_REASSOC_AX_YB:
442 case MachineCombinerPattern::MC_REASSOC_XA_YB:
443 Prev = MRI.getUniqueVRegDef(Root.getOperand(2).getReg());
444 }
445 assert(Prev && "Unknown pattern for machine combiner");
446
447 reassociateOps(Root, *Prev, Pattern, InsInstrs, DelInstrs, InstIdxForVirtReg);
448 return;
449}
450
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000451// Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
452bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
453 unsigned &SrcReg, unsigned &DstReg,
454 unsigned &SubIdx) const {
455 switch (MI.getOpcode()) {
456 default: return false;
457 case PPC::EXTSW:
458 case PPC::EXTSW_32_64:
459 SrcReg = MI.getOperand(1).getReg();
460 DstReg = MI.getOperand(0).getReg();
461 SubIdx = PPC::sub_32;
462 return true;
463 }
464}
465
Andrew Trickc416ba62010-12-24 04:28:06 +0000466unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner91400bd2006-03-16 22:24:02 +0000467 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000468 // Note: This list must be kept consistent with LoadRegFromStackSlot.
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000469 switch (MI->getOpcode()) {
470 default: break;
471 case PPC::LD:
472 case PPC::LWZ:
473 case PPC::LFS:
474 case PPC::LFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000475 case PPC::RESTORE_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000476 case PPC::RESTORE_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000477 case PPC::LVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000478 case PPC::LXVD2X:
Hal Finkelc93a9a22015-02-25 01:06:45 +0000479 case PPC::QVLFDX:
480 case PPC::QVLFSXs:
481 case PPC::QVLFDXb:
Hal Finkel37714b82013-03-27 21:21:15 +0000482 case PPC::RESTORE_VRSAVE:
483 // Check for the operands added by addFrameReference (the immediate is the
484 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000485 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
486 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000487 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000488 return MI->getOperand(0).getReg();
489 }
490 break;
491 }
492 return 0;
Chris Lattnerc327d712006-02-02 20:16:12 +0000493}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000494
Andrew Trickc416ba62010-12-24 04:28:06 +0000495unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattnerc327d712006-02-02 20:16:12 +0000496 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000497 // Note: This list must be kept consistent with StoreRegToStackSlot.
Chris Lattnerc327d712006-02-02 20:16:12 +0000498 switch (MI->getOpcode()) {
499 default: break;
Nate Begeman4efb3282006-02-02 21:07:50 +0000500 case PPC::STD:
Chris Lattnerc327d712006-02-02 20:16:12 +0000501 case PPC::STW:
502 case PPC::STFS:
503 case PPC::STFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000504 case PPC::SPILL_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000505 case PPC::SPILL_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000506 case PPC::STVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000507 case PPC::STXVD2X:
Hal Finkelc93a9a22015-02-25 01:06:45 +0000508 case PPC::QVSTFDX:
509 case PPC::QVSTFSXs:
510 case PPC::QVSTFDXb:
Hal Finkel37714b82013-03-27 21:21:15 +0000511 case PPC::SPILL_VRSAVE:
512 // Check for the operands added by addFrameReference (the immediate is the
513 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000514 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
515 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000516 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerc327d712006-02-02 20:16:12 +0000517 return MI->getOperand(0).getReg();
518 }
519 break;
520 }
521 return 0;
522}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000523
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000524// commuteInstruction - We can commute rlwimi instructions, but only if the
525// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng03553bb2008-06-16 07:33:11 +0000526MachineInstr *
527PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000528 MachineFunction &MF = *MI->getParent()->getParent();
529
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000530 // Normal instructions can be commuted the obvious way.
Hal Finkel654d43b2013-04-12 02:18:09 +0000531 if (MI->getOpcode() != PPC::RLWIMI &&
Hal Finkel4c6658f2014-12-12 23:59:36 +0000532 MI->getOpcode() != PPC::RLWIMIo)
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000533 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Hal Finkel4c6658f2014-12-12 23:59:36 +0000534 // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a
535 // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because
536 // changing the relative order of the mask operands might change what happens
537 // to the high-bits of the mask (and, thus, the result).
Andrew Trickc416ba62010-12-24 04:28:06 +0000538
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000539 // Cannot commute if it has a non-zero rotate count.
Chris Lattner5c463782007-12-30 20:49:49 +0000540 if (MI->getOperand(3).getImm() != 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000541 return nullptr;
Andrew Trickc416ba62010-12-24 04:28:06 +0000542
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000543 // If we have a zero rotate count, we have:
544 // M = mask(MB,ME)
545 // Op0 = (Op1 & ~M) | (Op2 & M)
546 // Change this to:
547 // M = mask((ME+1)&31, (MB-1)&31)
548 // Op0 = (Op2 & ~M) | (Op1 & M)
549
550 // Swap op1/op2
Evan Cheng244183e2008-02-13 02:46:49 +0000551 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000552 unsigned Reg1 = MI->getOperand(1).getReg();
553 unsigned Reg2 = MI->getOperand(2).getReg();
Andrew Tricke3398282013-12-17 04:50:45 +0000554 unsigned SubReg1 = MI->getOperand(1).getSubReg();
555 unsigned SubReg2 = MI->getOperand(2).getSubReg();
Evan Chengdc2c8742006-11-15 20:58:11 +0000556 bool Reg1IsKill = MI->getOperand(1).isKill();
557 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng03553bb2008-06-16 07:33:11 +0000558 bool ChangeReg0 = false;
Evan Cheng244183e2008-02-13 02:46:49 +0000559 // If machine instrs are no longer in two-address forms, update
560 // destination register as well.
561 if (Reg0 == Reg1) {
562 // Must be two address instruction!
Evan Cheng6cc775f2011-06-28 19:10:37 +0000563 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Cheng244183e2008-02-13 02:46:49 +0000564 "Expecting a two-address instruction!");
Andrew Tricke3398282013-12-17 04:50:45 +0000565 assert(MI->getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
Evan Cheng244183e2008-02-13 02:46:49 +0000566 Reg2IsKill = false;
Evan Cheng03553bb2008-06-16 07:33:11 +0000567 ChangeReg0 = true;
Evan Cheng244183e2008-02-13 02:46:49 +0000568 }
Evan Cheng03553bb2008-06-16 07:33:11 +0000569
570 // Masks.
571 unsigned MB = MI->getOperand(4).getImm();
572 unsigned ME = MI->getOperand(5).getImm();
573
574 if (NewMI) {
575 // Create a new instruction.
576 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
577 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000578 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000579 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
580 .addReg(Reg2, getKillRegState(Reg2IsKill))
581 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng03553bb2008-06-16 07:33:11 +0000582 .addImm((ME+1) & 31)
583 .addImm((MB-1) & 31);
584 }
585
Andrew Tricke3398282013-12-17 04:50:45 +0000586 if (ChangeReg0) {
Evan Cheng03553bb2008-06-16 07:33:11 +0000587 MI->getOperand(0).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000588 MI->getOperand(0).setSubReg(SubReg2);
589 }
Chris Lattner10d63412006-05-04 17:52:23 +0000590 MI->getOperand(2).setReg(Reg1);
591 MI->getOperand(1).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000592 MI->getOperand(2).setSubReg(SubReg1);
593 MI->getOperand(1).setSubReg(SubReg2);
Chris Lattner60055892007-12-30 21:56:09 +0000594 MI->getOperand(2).setIsKill(Reg1IsKill);
595 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000596
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000597 // Swap the mask around.
Chris Lattner5c463782007-12-30 20:49:49 +0000598 MI->getOperand(4).setImm((ME+1) & 31);
599 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000600 return MI;
601}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000602
Hal Finkel6c32ff32014-03-25 19:26:43 +0000603bool PPCInstrInfo::findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
604 unsigned &SrcOpIdx2) const {
605 // For VSX A-Type FMA instructions, it is the first two operands that can be
606 // commuted, however, because the non-encoded tied input operand is listed
607 // first, the operands to swap are actually the second and third.
608
609 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
610 if (AltOpc == -1)
611 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
612
613 SrcOpIdx1 = 2;
614 SrcOpIdx2 = 3;
615 return true;
616}
617
Andrew Trickc416ba62010-12-24 04:28:06 +0000618void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000619 MachineBasicBlock::iterator MI) const {
Hal Finkelceb1f122013-12-12 00:19:11 +0000620 // This function is used for scheduling, and the nop wanted here is the type
621 // that terminates dispatch groups on the POWER cores.
Eric Christopher1dcea732014-06-12 21:48:52 +0000622 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000623 unsigned Opcode;
624 switch (Directive) {
625 default: Opcode = PPC::NOP; break;
626 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
627 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
Will Schmidt970ff642014-06-26 13:36:19 +0000628 case PPC::DIR_PWR8: Opcode = PPC::NOP_GT_PWR7; break; /* FIXME: Update when P8 InstrScheduling model is ready */
Hal Finkelceb1f122013-12-12 00:19:11 +0000629 }
Chris Lattnera47294ed2006-10-13 21:21:17 +0000630
Hal Finkelceb1f122013-12-12 00:19:11 +0000631 DebugLoc DL;
632 BuildMI(MBB, MI, DL, get(Opcode));
633}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000634
Joerg Sonnenberger7ee0f312014-08-08 19:13:23 +0000635/// getNoopForMachoTarget - Return the noop instruction to use for a noop.
636void PPCInstrInfo::getNoopForMachoTarget(MCInst &NopInst) const {
637 NopInst.setOpcode(PPC::NOP);
638}
639
Chris Lattnera47294ed2006-10-13 21:21:17 +0000640// Branch analysis.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000641// Note: If the condition register is set to CTR or CTR8 then this is a
642// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Chris Lattnera47294ed2006-10-13 21:21:17 +0000643bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
644 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000645 SmallVectorImpl<MachineOperand> &Cond,
646 bool AllowModify) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000647 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000648
Chris Lattnera47294ed2006-10-13 21:21:17 +0000649 // If the block has no terminators, it just falls into the block after it.
Benjamin Kramer92861d72015-06-25 13:39:03 +0000650 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
651 if (I == MBB.end())
Dale Johannesen4244d122010-04-02 01:38:09 +0000652 return false;
Benjamin Kramer92861d72015-06-25 13:39:03 +0000653
Dale Johannesen4244d122010-04-02 01:38:09 +0000654 if (!isUnpredicatedTerminator(I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000655 return false;
656
657 // Get the last instruction in the block.
658 MachineInstr *LastInst = I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000659
Chris Lattnera47294ed2006-10-13 21:21:17 +0000660 // If there is only one terminator instruction, process it.
Evan Cheng5514bbe2007-06-08 21:59:56 +0000661 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000662 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000663 if (!LastInst->getOperand(0).isMBB())
664 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000665 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000666 return false;
Chris Lattnere0263792006-11-17 22:14:47 +0000667 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000668 if (!LastInst->getOperand(2).isMBB())
669 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000670 // Block ends with fall-through condbranch.
Chris Lattnera5bb3702007-12-30 23:10:15 +0000671 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000672 Cond.push_back(LastInst->getOperand(0));
673 Cond.push_back(LastInst->getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000674 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000675 } else if (LastInst->getOpcode() == PPC::BC) {
676 if (!LastInst->getOperand(1).isMBB())
677 return true;
678 // Block ends with fall-through condbranch.
679 TBB = LastInst->getOperand(1).getMBB();
680 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
681 Cond.push_back(LastInst->getOperand(0));
682 return false;
683 } else if (LastInst->getOpcode() == PPC::BCn) {
684 if (!LastInst->getOperand(1).isMBB())
685 return true;
686 // Block ends with fall-through condbranch.
687 TBB = LastInst->getOperand(1).getMBB();
688 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
689 Cond.push_back(LastInst->getOperand(0));
690 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000691 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
692 LastInst->getOpcode() == PPC::BDNZ) {
693 if (!LastInst->getOperand(0).isMBB())
694 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000695 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000696 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000697 TBB = LastInst->getOperand(0).getMBB();
698 Cond.push_back(MachineOperand::CreateImm(1));
699 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
700 true));
701 return false;
702 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
703 LastInst->getOpcode() == PPC::BDZ) {
704 if (!LastInst->getOperand(0).isMBB())
705 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000706 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000707 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000708 TBB = LastInst->getOperand(0).getMBB();
709 Cond.push_back(MachineOperand::CreateImm(0));
710 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
711 true));
712 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000713 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000714
Chris Lattnera47294ed2006-10-13 21:21:17 +0000715 // Otherwise, don't know what this is.
716 return true;
717 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000718
Chris Lattnera47294ed2006-10-13 21:21:17 +0000719 // Get the instruction before it if it's a terminator.
720 MachineInstr *SecondLastInst = I;
721
722 // If there are three terminators, we don't know what sort of block this is.
723 if (SecondLastInst && I != MBB.begin() &&
Evan Cheng5514bbe2007-06-08 21:59:56 +0000724 isUnpredicatedTerminator(--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000725 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000726
Chris Lattnere0263792006-11-17 22:14:47 +0000727 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000728 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnera47294ed2006-10-13 21:21:17 +0000729 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000730 if (!SecondLastInst->getOperand(2).isMBB() ||
731 !LastInst->getOperand(0).isMBB())
732 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000733 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000734 Cond.push_back(SecondLastInst->getOperand(0));
735 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattnera5bb3702007-12-30 23:10:15 +0000736 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000737 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000738 } else if (SecondLastInst->getOpcode() == PPC::BC &&
739 LastInst->getOpcode() == PPC::B) {
740 if (!SecondLastInst->getOperand(1).isMBB() ||
741 !LastInst->getOperand(0).isMBB())
742 return true;
743 TBB = SecondLastInst->getOperand(1).getMBB();
744 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
745 Cond.push_back(SecondLastInst->getOperand(0));
746 FBB = LastInst->getOperand(0).getMBB();
747 return false;
748 } else if (SecondLastInst->getOpcode() == PPC::BCn &&
749 LastInst->getOpcode() == PPC::B) {
750 if (!SecondLastInst->getOperand(1).isMBB() ||
751 !LastInst->getOperand(0).isMBB())
752 return true;
753 TBB = SecondLastInst->getOperand(1).getMBB();
754 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
755 Cond.push_back(SecondLastInst->getOperand(0));
756 FBB = LastInst->getOperand(0).getMBB();
757 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000758 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
759 SecondLastInst->getOpcode() == PPC::BDNZ) &&
760 LastInst->getOpcode() == PPC::B) {
761 if (!SecondLastInst->getOperand(0).isMBB() ||
762 !LastInst->getOperand(0).isMBB())
763 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000764 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000765 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000766 TBB = SecondLastInst->getOperand(0).getMBB();
767 Cond.push_back(MachineOperand::CreateImm(1));
768 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
769 true));
770 FBB = LastInst->getOperand(0).getMBB();
771 return false;
772 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
773 SecondLastInst->getOpcode() == PPC::BDZ) &&
774 LastInst->getOpcode() == PPC::B) {
775 if (!SecondLastInst->getOperand(0).isMBB() ||
776 !LastInst->getOperand(0).isMBB())
777 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000778 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000779 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000780 TBB = SecondLastInst->getOperand(0).getMBB();
781 Cond.push_back(MachineOperand::CreateImm(0));
782 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
783 true));
784 FBB = LastInst->getOperand(0).getMBB();
785 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000786 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000787
Dale Johannesenc6855462007-06-13 17:59:52 +0000788 // If the block ends with two PPC:Bs, handle it. The second one is not
789 // executed, so remove it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000790 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesenc6855462007-06-13 17:59:52 +0000791 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000792 if (!SecondLastInst->getOperand(0).isMBB())
793 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000794 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000795 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000796 if (AllowModify)
797 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000798 return false;
799 }
800
Chris Lattnera47294ed2006-10-13 21:21:17 +0000801 // Otherwise, can't handle this.
802 return true;
803}
804
Evan Cheng99be49d2007-05-18 00:05:48 +0000805unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Benjamin Kramer92861d72015-06-25 13:39:03 +0000806 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
807 if (I == MBB.end())
808 return 0;
809
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000810 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000811 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000812 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
813 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000814 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000815
Chris Lattnera47294ed2006-10-13 21:21:17 +0000816 // Remove the branch.
817 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000818
Chris Lattnera47294ed2006-10-13 21:21:17 +0000819 I = MBB.end();
820
Evan Cheng99be49d2007-05-18 00:05:48 +0000821 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000822 --I;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000823 if (I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000824 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000825 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
826 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000827 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000828
Chris Lattnera47294ed2006-10-13 21:21:17 +0000829 // Remove the branch.
830 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000831 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000832}
833
Evan Cheng99be49d2007-05-18 00:05:48 +0000834unsigned
835PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
836 MachineBasicBlock *FBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000837 ArrayRef<MachineOperand> Cond,
Stuart Hastings0125b642010-06-17 22:43:56 +0000838 DebugLoc DL) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000839 // Shouldn't be a fall through.
840 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000841 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000842 "PPC branch conditions have two components!");
Andrew Trickc416ba62010-12-24 04:28:06 +0000843
Eric Christopher1dcea732014-06-12 21:48:52 +0000844 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000845
Chris Lattner94e04442006-10-21 05:36:13 +0000846 // One-way branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000847 if (!FBB) {
Chris Lattner94e04442006-10-21 05:36:13 +0000848 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000849 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000850 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
851 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
852 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
853 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000854 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
855 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
856 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
857 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000858 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000859 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000860 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000861 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000862 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000863
Chris Lattnerd8816602006-10-21 05:42:09 +0000864 // Two-way Conditional Branch.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000865 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
866 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
867 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
868 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000869 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
870 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
871 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
872 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000873 else
874 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000875 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000876 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000877 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000878}
879
Hal Finkeled6a2852013-04-05 23:29:01 +0000880// Select analysis.
881bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000882 ArrayRef<MachineOperand> Cond,
Hal Finkeled6a2852013-04-05 23:29:01 +0000883 unsigned TrueReg, unsigned FalseReg,
884 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000885 if (!Subtarget.hasISEL())
Hal Finkeled6a2852013-04-05 23:29:01 +0000886 return false;
887
888 if (Cond.size() != 2)
889 return false;
890
891 // If this is really a bdnz-like condition, then it cannot be turned into a
892 // select.
893 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
894 return false;
895
896 // Check register classes.
897 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
898 const TargetRegisterClass *RC =
899 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
900 if (!RC)
901 return false;
902
903 // isel is for regular integer GPRs only.
904 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkel8e8618a2013-07-15 20:22:58 +0000905 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
906 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
907 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkeled6a2852013-04-05 23:29:01 +0000908 return false;
909
910 // FIXME: These numbers are for the A2, how well they work for other cores is
911 // an open question. On the A2, the isel instruction has a 2-cycle latency
912 // but single-cycle throughput. These numbers are used in combination with
913 // the MispredictPenalty setting from the active SchedMachineModel.
914 CondCycles = 1;
915 TrueCycles = 1;
916 FalseCycles = 1;
917
918 return true;
919}
920
921void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
922 MachineBasicBlock::iterator MI, DebugLoc dl,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000923 unsigned DestReg, ArrayRef<MachineOperand> Cond,
Hal Finkeled6a2852013-04-05 23:29:01 +0000924 unsigned TrueReg, unsigned FalseReg) const {
925 assert(Cond.size() == 2 &&
926 "PPC branch conditions have two components!");
927
Eric Christopher1dcea732014-06-12 21:48:52 +0000928 assert(Subtarget.hasISEL() &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000929 "Cannot insert select on target without ISEL support");
930
931 // Get the register classes.
932 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
933 const TargetRegisterClass *RC =
934 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
935 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkel8e8618a2013-07-15 20:22:58 +0000936
937 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
938 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
939 assert((Is64Bit ||
940 PPC::GPRCRegClass.hasSubClassEq(RC) ||
941 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000942 "isel is for regular integer GPRs only");
943
Hal Finkel8e8618a2013-07-15 20:22:58 +0000944 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Hal Finkeled6a2852013-04-05 23:29:01 +0000945 unsigned SelectPred = Cond[0].getImm();
946
947 unsigned SubIdx;
948 bool SwapOps;
949 switch (SelectPred) {
950 default: llvm_unreachable("invalid predicate for isel");
951 case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
952 case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
953 case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
954 case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
955 case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
956 case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
957 case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
958 case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
Hal Finkel940ab932014-02-28 00:27:01 +0000959 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break;
960 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
Hal Finkeled6a2852013-04-05 23:29:01 +0000961 }
962
963 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
964 SecondReg = SwapOps ? TrueReg : FalseReg;
965
966 // The first input register of isel cannot be r0. If it is a member
967 // of a register class that can be r0, then copy it first (the
968 // register allocator should eliminate the copy).
969 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
970 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
971 const TargetRegisterClass *FirstRC =
972 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
973 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
974 unsigned OldFirstReg = FirstReg;
975 FirstReg = MRI.createVirtualRegister(FirstRC);
976 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
977 .addReg(OldFirstReg);
978 }
979
980 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
981 .addReg(FirstReg).addReg(SecondReg)
982 .addReg(Cond[1].getReg(), 0, SubIdx);
983}
984
Kit Barton535e69d2015-03-25 19:36:23 +0000985static unsigned getCRBitValue(unsigned CRBit) {
986 unsigned Ret = 4;
987 if (CRBit == PPC::CR0LT || CRBit == PPC::CR1LT ||
988 CRBit == PPC::CR2LT || CRBit == PPC::CR3LT ||
989 CRBit == PPC::CR4LT || CRBit == PPC::CR5LT ||
990 CRBit == PPC::CR6LT || CRBit == PPC::CR7LT)
991 Ret = 3;
992 if (CRBit == PPC::CR0GT || CRBit == PPC::CR1GT ||
993 CRBit == PPC::CR2GT || CRBit == PPC::CR3GT ||
994 CRBit == PPC::CR4GT || CRBit == PPC::CR5GT ||
995 CRBit == PPC::CR6GT || CRBit == PPC::CR7GT)
996 Ret = 2;
997 if (CRBit == PPC::CR0EQ || CRBit == PPC::CR1EQ ||
998 CRBit == PPC::CR2EQ || CRBit == PPC::CR3EQ ||
999 CRBit == PPC::CR4EQ || CRBit == PPC::CR5EQ ||
1000 CRBit == PPC::CR6EQ || CRBit == PPC::CR7EQ)
1001 Ret = 1;
1002 if (CRBit == PPC::CR0UN || CRBit == PPC::CR1UN ||
1003 CRBit == PPC::CR2UN || CRBit == PPC::CR3UN ||
1004 CRBit == PPC::CR4UN || CRBit == PPC::CR5UN ||
1005 CRBit == PPC::CR6UN || CRBit == PPC::CR7UN)
1006 Ret = 0;
1007
1008 assert(Ret != 4 && "Invalid CR bit register");
1009 return Ret;
1010}
1011
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +00001012void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
1013 MachineBasicBlock::iterator I, DebugLoc DL,
1014 unsigned DestReg, unsigned SrcReg,
1015 bool KillSrc) const {
Hal Finkel27774d92014-03-13 07:58:58 +00001016 // We can end up with self copies and similar things as a result of VSX copy
Hal Finkel9dcb3582014-03-27 22:46:28 +00001017 // legalization. Promote them here.
Hal Finkel27774d92014-03-13 07:58:58 +00001018 const TargetRegisterInfo *TRI = &getRegisterInfo();
1019 if (PPC::F8RCRegClass.contains(DestReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +00001020 PPC::VSRCRegClass.contains(SrcReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +00001021 unsigned SuperReg =
1022 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass);
1023
Hal Finkel9dcb3582014-03-27 22:46:28 +00001024 if (VSXSelfCopyCrash && SrcReg == SuperReg)
1025 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +00001026
1027 DestReg = SuperReg;
1028 } else if (PPC::VRRCRegClass.contains(DestReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +00001029 PPC::VSRCRegClass.contains(SrcReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +00001030 unsigned SuperReg =
1031 TRI->getMatchingSuperReg(DestReg, PPC::sub_128, &PPC::VSRCRegClass);
1032
Hal Finkel9dcb3582014-03-27 22:46:28 +00001033 if (VSXSelfCopyCrash && SrcReg == SuperReg)
1034 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +00001035
1036 DestReg = SuperReg;
1037 } else if (PPC::F8RCRegClass.contains(SrcReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +00001038 PPC::VSRCRegClass.contains(DestReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +00001039 unsigned SuperReg =
1040 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass);
1041
Hal Finkel9dcb3582014-03-27 22:46:28 +00001042 if (VSXSelfCopyCrash && DestReg == SuperReg)
1043 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +00001044
1045 SrcReg = SuperReg;
1046 } else if (PPC::VRRCRegClass.contains(SrcReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +00001047 PPC::VSRCRegClass.contains(DestReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +00001048 unsigned SuperReg =
1049 TRI->getMatchingSuperReg(SrcReg, PPC::sub_128, &PPC::VSRCRegClass);
1050
Hal Finkel9dcb3582014-03-27 22:46:28 +00001051 if (VSXSelfCopyCrash && DestReg == SuperReg)
1052 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +00001053
1054 SrcReg = SuperReg;
1055 }
1056
Kit Barton535e69d2015-03-25 19:36:23 +00001057 // Different class register copy
1058 if (PPC::CRBITRCRegClass.contains(SrcReg) &&
1059 PPC::GPRCRegClass.contains(DestReg)) {
1060 unsigned CRReg = getCRFromCRBit(SrcReg);
1061 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg)
1062 .addReg(CRReg), getKillRegState(KillSrc);
1063 // Rotate the CR bit in the CR fields to be the least significant bit and
1064 // then mask with 0x1 (MB = ME = 31).
1065 BuildMI(MBB, I, DL, get(PPC::RLWINM), DestReg)
1066 .addReg(DestReg, RegState::Kill)
1067 .addImm(TRI->getEncodingValue(CRReg) * 4 + (4 - getCRBitValue(SrcReg)))
1068 .addImm(31)
1069 .addImm(31);
1070 return;
1071 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
1072 PPC::G8RCRegClass.contains(DestReg)) {
1073 BuildMI(MBB, I, DL, get(PPC::MFOCRF8), DestReg)
1074 .addReg(SrcReg), getKillRegState(KillSrc);
1075 return;
1076 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
1077 PPC::GPRCRegClass.contains(DestReg)) {
1078 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg)
1079 .addReg(SrcReg), getKillRegState(KillSrc);
1080 return;
1081 }
1082
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +00001083 unsigned Opc;
1084 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
1085 Opc = PPC::OR;
1086 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
1087 Opc = PPC::OR8;
1088 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
1089 Opc = PPC::FMR;
1090 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
1091 Opc = PPC::MCRF;
1092 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
1093 Opc = PPC::VOR;
Hal Finkel27774d92014-03-13 07:58:58 +00001094 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg))
Hal Finkelbbad2332014-03-24 09:36:36 +00001095 // There are two different ways this can be done:
Hal Finkel27774d92014-03-13 07:58:58 +00001096 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
1097 // issue in VSU pipeline 0.
1098 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
1099 // can go to either pipeline.
Hal Finkelbbad2332014-03-24 09:36:36 +00001100 // We'll always use xxlor here, because in practically all cases where
1101 // copies are generated, they are close enough to some use that the
1102 // lower-latency form is preferable.
Hal Finkel27774d92014-03-13 07:58:58 +00001103 Opc = PPC::XXLOR;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001104 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg) ||
1105 PPC::VSSRCRegClass.contains(DestReg, SrcReg))
Hal Finkel19be5062014-03-29 05:29:01 +00001106 Opc = PPC::XXLORf;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001107 else if (PPC::QFRCRegClass.contains(DestReg, SrcReg))
1108 Opc = PPC::QVFMR;
1109 else if (PPC::QSRCRegClass.contains(DestReg, SrcReg))
1110 Opc = PPC::QVFMRs;
1111 else if (PPC::QBRCRegClass.contains(DestReg, SrcReg))
1112 Opc = PPC::QVFMRb;
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +00001113 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
1114 Opc = PPC::CROR;
1115 else
1116 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +00001117
Evan Cheng6cc775f2011-06-28 19:10:37 +00001118 const MCInstrDesc &MCID = get(Opc);
1119 if (MCID.getNumOperands() == 3)
1120 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +00001121 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
1122 else
Evan Cheng6cc775f2011-06-28 19:10:37 +00001123 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +00001124}
1125
Hal Finkel8f6834d2011-12-05 17:55:17 +00001126// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +00001127bool
Dan Gohman3b460302008-07-07 23:14:23 +00001128PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
1129 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +00001130 int FrameIdx,
1131 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +00001132 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001133 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +00001134 // Note: If additional store instructions are added here,
1135 // update isStoreToStackSlot.
1136
Chris Lattner6f306d72010-04-02 20:16:16 +00001137 DebugLoc DL;
Hal Finkel4e703bc2014-01-28 05:32:58 +00001138 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
1139 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +00001140 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
1141 .addReg(SrcReg,
1142 getKillRegState(isKill)),
1143 FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +00001144 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
1145 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +00001146 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
1147 .addReg(SrcReg,
1148 getKillRegState(isKill)),
1149 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001150 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +00001151 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +00001152 .addReg(SrcReg,
1153 getKillRegState(isKill)),
1154 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001155 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +00001156 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +00001157 .addReg(SrcReg,
1158 getKillRegState(isKill)),
1159 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, get(PPC::SPILL_CR))
1162 .addReg(SrcReg,
1163 getKillRegState(isKill)),
1164 FrameIdx));
1165 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001166 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +00001167 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT))
1168 .addReg(SrcReg,
1169 getKillRegState(isKill)),
1170 FrameIdx));
1171 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001172 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +00001173 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
1174 .addReg(SrcReg,
1175 getKillRegState(isKill)),
1176 FrameIdx));
1177 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +00001178 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
1179 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXVD2X))
1180 .addReg(SrcReg,
1181 getKillRegState(isKill)),
1182 FrameIdx));
1183 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +00001184 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
1185 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSDX))
1186 .addReg(SrcReg,
1187 getKillRegState(isKill)),
1188 FrameIdx));
1189 NonRI = true;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001190 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
1191 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSSPX))
1192 .addReg(SrcReg,
1193 getKillRegState(isKill)),
1194 FrameIdx));
1195 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +00001196 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001197 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +00001198 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +00001199 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
1200 .addReg(SrcReg,
1201 getKillRegState(isKill)),
1202 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001203 SpillsVRS = true;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001204 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
1205 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDX))
1206 .addReg(SrcReg,
1207 getKillRegState(isKill)),
1208 FrameIdx));
1209 NonRI = true;
1210 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
1211 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFSXs))
1212 .addReg(SrcReg,
1213 getKillRegState(isKill)),
1214 FrameIdx));
1215 NonRI = true;
1216 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
1217 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDXb))
1218 .addReg(SrcReg,
1219 getKillRegState(isKill)),
1220 FrameIdx));
1221 NonRI = true;
Owen Andersoneee14602008-01-01 21:11:32 +00001222 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001223 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +00001224 }
Bill Wendling632ea652008-03-03 22:19:16 +00001225
1226 return false;
Owen Andersoneee14602008-01-01 21:11:32 +00001227}
1228
1229void
1230PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +00001231 MachineBasicBlock::iterator MI,
1232 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +00001233 const TargetRegisterClass *RC,
1234 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +00001235 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +00001236 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +00001237
Hal Finkelbb420f12013-03-15 05:06:04 +00001238 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1239 FuncInfo->setHasSpills();
1240
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001241 bool NonRI = false, SpillsVRS = false;
1242 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
1243 NonRI, SpillsVRS))
Bill Wendling632ea652008-03-03 22:19:16 +00001244 FuncInfo->setSpillsCR();
Bill Wendling632ea652008-03-03 22:19:16 +00001245
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001246 if (SpillsVRS)
1247 FuncInfo->setSpillsVRSAVE();
1248
Hal Finkelfcc51d42013-03-17 04:43:44 +00001249 if (NonRI)
1250 FuncInfo->setHasNonRISpills();
1251
Owen Andersoneee14602008-01-01 21:11:32 +00001252 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1253 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001254
1255 const MachineFrameInfo &MFI = *MF.getFrameInfo();
1256 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +00001257 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +00001258 MachineMemOperand::MOStore,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001259 MFI.getObjectSize(FrameIdx),
1260 MFI.getObjectAlignment(FrameIdx));
1261 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +00001262}
1263
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001264bool
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001265PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman3b460302008-07-07 23:14:23 +00001266 unsigned DestReg, int FrameIdx,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +00001267 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +00001268 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001269 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +00001270 // Note: If additional load instructions are added here,
1271 // update isLoadFromStackSlot.
1272
Hal Finkel4e703bc2014-01-28 05:32:58 +00001273 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
1274 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +00001275 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
1276 DestReg), FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +00001277 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
1278 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +00001279 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
1280 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001281 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001282 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +00001283 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001284 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001285 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +00001286 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001287 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +00001288 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1289 get(PPC::RESTORE_CR), DestReg),
1290 FrameIdx));
1291 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001292 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +00001293 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1294 get(PPC::RESTORE_CRBIT), DestReg),
1295 FrameIdx));
1296 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001297 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +00001298 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
1299 FrameIdx));
1300 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +00001301 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
1302 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXVD2X), DestReg),
1303 FrameIdx));
1304 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +00001305 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
1306 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSDX), DestReg),
1307 FrameIdx));
1308 NonRI = true;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001309 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
1310 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSSPX), DestReg),
1311 FrameIdx));
1312 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +00001313 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001314 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +00001315 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +00001316 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1317 get(PPC::RESTORE_VRSAVE),
1318 DestReg),
1319 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001320 SpillsVRS = true;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001321 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
1322 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDX), DestReg),
1323 FrameIdx));
1324 NonRI = true;
1325 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
1326 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFSXs), DestReg),
1327 FrameIdx));
1328 NonRI = true;
1329 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
1330 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDXb), DestReg),
1331 FrameIdx));
1332 NonRI = true;
Owen Andersoneee14602008-01-01 21:11:32 +00001333 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001334 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +00001335 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001336
1337 return false;
Owen Andersoneee14602008-01-01 21:11:32 +00001338}
1339
1340void
1341PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +00001342 MachineBasicBlock::iterator MI,
1343 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +00001344 const TargetRegisterClass *RC,
1345 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +00001346 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +00001347 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +00001348 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001349 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelfcc51d42013-03-17 04:43:44 +00001350
1351 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1352 FuncInfo->setHasSpills();
1353
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001354 bool NonRI = false, SpillsVRS = false;
1355 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
1356 NonRI, SpillsVRS))
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001357 FuncInfo->setSpillsCR();
Hal Finkelfcc51d42013-03-17 04:43:44 +00001358
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001359 if (SpillsVRS)
1360 FuncInfo->setSpillsVRSAVE();
1361
Hal Finkelfcc51d42013-03-17 04:43:44 +00001362 if (NonRI)
1363 FuncInfo->setHasNonRISpills();
1364
Owen Andersoneee14602008-01-01 21:11:32 +00001365 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1366 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001367
1368 const MachineFrameInfo &MFI = *MF.getFrameInfo();
1369 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +00001370 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +00001371 MachineMemOperand::MOLoad,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001372 MFI.getObjectSize(FrameIdx),
1373 MFI.getObjectAlignment(FrameIdx));
1374 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +00001375}
1376
Chris Lattnera47294ed2006-10-13 21:21:17 +00001377bool PPCInstrInfo::
Owen Anderson4f6bf042008-08-14 22:49:33 +00001378ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +00001379 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel96c2d4d2012-06-08 15:38:21 +00001380 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
1381 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
1382 else
1383 // Leave the CR# the same, but invert the condition.
1384 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +00001385 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +00001386}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001387
Hal Finkeld61d4f82013-04-06 19:30:30 +00001388bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
1389 unsigned Reg, MachineRegisterInfo *MRI) const {
1390 // For some instructions, it is legal to fold ZERO into the RA register field.
1391 // A zero immediate should always be loaded with a single li.
1392 unsigned DefOpc = DefMI->getOpcode();
1393 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
1394 return false;
1395 if (!DefMI->getOperand(1).isImm())
1396 return false;
1397 if (DefMI->getOperand(1).getImm() != 0)
1398 return false;
1399
1400 // Note that we cannot here invert the arguments of an isel in order to fold
1401 // a ZERO into what is presented as the second argument. All we have here
1402 // is the condition bit, and that might come from a CR-logical bit operation.
1403
1404 const MCInstrDesc &UseMCID = UseMI->getDesc();
1405
1406 // Only fold into real machine instructions.
1407 if (UseMCID.isPseudo())
1408 return false;
1409
1410 unsigned UseIdx;
1411 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
1412 if (UseMI->getOperand(UseIdx).isReg() &&
1413 UseMI->getOperand(UseIdx).getReg() == Reg)
1414 break;
1415
1416 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
1417 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
1418
1419 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
1420
1421 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
1422 // register (which might also be specified as a pointer class kind).
1423 if (UseInfo->isLookupPtrRegClass()) {
1424 if (UseInfo->RegClass /* Kind */ != 1)
1425 return false;
1426 } else {
1427 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
1428 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
1429 return false;
1430 }
1431
1432 // Make sure this is not tied to an output register (or otherwise
1433 // constrained). This is true for ST?UX registers, for example, which
1434 // are tied to their output registers.
1435 if (UseInfo->Constraints != 0)
1436 return false;
1437
1438 unsigned ZeroReg;
1439 if (UseInfo->isLookupPtrRegClass()) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001440 bool isPPC64 = Subtarget.isPPC64();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001441 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
1442 } else {
1443 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
1444 PPC::ZERO8 : PPC::ZERO;
1445 }
1446
1447 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
1448 UseMI->getOperand(UseIdx).setReg(ZeroReg);
1449
1450 if (DeleteDef)
1451 DefMI->eraseFromParent();
1452
1453 return true;
1454}
1455
Hal Finkel30ae2292013-04-10 18:30:16 +00001456static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
1457 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1458 I != IE; ++I)
1459 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
1460 return true;
1461 return false;
1462}
1463
1464// We should make sure that, if we're going to predicate both sides of a
1465// condition (a diamond), that both sides don't define the counter register. We
1466// can predicate counter-decrement-based branches, but while that predicates
1467// the branching, it does not predicate the counter decrement. If we tried to
1468// merge the triangle into one predicated block, we'd decrement the counter
1469// twice.
1470bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
1471 unsigned NumT, unsigned ExtraT,
1472 MachineBasicBlock &FMBB,
1473 unsigned NumF, unsigned ExtraF,
1474 const BranchProbability &Probability) const {
1475 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
1476}
1477
1478
Hal Finkel5711eca2013-04-09 22:58:37 +00001479bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const {
Hal Finkelf29285a2013-04-11 01:23:34 +00001480 // The predicated branches are identified by their type, not really by the
1481 // explicit presence of a predicate. Furthermore, some of them can be
1482 // predicated more than once. Because if conversion won't try to predicate
1483 // any instruction which already claims to be predicated (by returning true
1484 // here), always return false. In doing so, we let isPredicable() be the
1485 // final word on whether not the instruction can be (further) predicated.
1486
1487 return false;
Hal Finkel5711eca2013-04-09 22:58:37 +00001488}
1489
1490bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
1491 if (!MI->isTerminator())
1492 return false;
1493
1494 // Conditional branch is a special case.
1495 if (MI->isBranch() && !MI->isBarrier())
1496 return true;
1497
1498 return !isPredicated(MI);
1499}
1500
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001501bool PPCInstrInfo::PredicateInstruction(MachineInstr *MI,
1502 ArrayRef<MachineOperand> Pred) const {
Hal Finkel5711eca2013-04-09 22:58:37 +00001503 unsigned OpC = MI->getOpcode();
Hal Finkelf4a22c02015-01-13 17:47:54 +00001504 if (OpC == PPC::BLR || OpC == PPC::BLR8) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001505 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001506 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel5711eca2013-04-09 22:58:37 +00001507 MI->setDesc(get(Pred[0].getImm() ?
1508 (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) :
1509 (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
Hal Finkel940ab932014-02-28 00:27:01 +00001510 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001511 MI->setDesc(get(PPC::BCLR));
1512 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel940ab932014-02-28 00:27:01 +00001513 .addReg(Pred[1].getReg());
1514 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1515 MI->setDesc(get(PPC::BCLRn));
1516 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1517 .addReg(Pred[1].getReg());
1518 } else {
1519 MI->setDesc(get(PPC::BCCLR));
1520 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel5711eca2013-04-09 22:58:37 +00001521 .addImm(Pred[0].getImm())
1522 .addReg(Pred[1].getReg());
1523 }
1524
1525 return true;
1526 } else if (OpC == PPC::B) {
1527 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001528 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel5711eca2013-04-09 22:58:37 +00001529 MI->setDesc(get(Pred[0].getImm() ?
1530 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1531 (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
Hal Finkel940ab932014-02-28 00:27:01 +00001532 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1533 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1534 MI->RemoveOperand(0);
1535
1536 MI->setDesc(get(PPC::BC));
1537 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1538 .addReg(Pred[1].getReg())
1539 .addMBB(MBB);
1540 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1541 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1542 MI->RemoveOperand(0);
1543
1544 MI->setDesc(get(PPC::BCn));
1545 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1546 .addReg(Pred[1].getReg())
1547 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001548 } else {
1549 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1550 MI->RemoveOperand(0);
1551
1552 MI->setDesc(get(PPC::BCC));
1553 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1554 .addImm(Pred[0].getImm())
1555 .addReg(Pred[1].getReg())
1556 .addMBB(MBB);
1557 }
1558
1559 return true;
Hal Finkel500b0042013-04-10 06:42:34 +00001560 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1561 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1562 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1563 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1564
1565 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
Eric Christopher1dcea732014-06-12 21:48:52 +00001566 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel940ab932014-02-28 00:27:01 +00001567
1568 if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1569 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) :
1570 (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1571 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1572 .addReg(Pred[1].getReg());
1573 return true;
1574 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1575 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) :
1576 (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1577 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1578 .addReg(Pred[1].getReg());
1579 return true;
1580 }
1581
1582 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) :
1583 (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
Hal Finkel500b0042013-04-10 06:42:34 +00001584 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1585 .addImm(Pred[0].getImm())
1586 .addReg(Pred[1].getReg());
1587 return true;
Hal Finkel5711eca2013-04-09 22:58:37 +00001588 }
1589
1590 return false;
1591}
1592
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001593bool PPCInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1594 ArrayRef<MachineOperand> Pred2) const {
Hal Finkel5711eca2013-04-09 22:58:37 +00001595 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1596 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1597
1598 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1599 return false;
1600 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1601 return false;
1602
Hal Finkel94a6f382013-12-11 23:12:25 +00001603 // P1 can only subsume P2 if they test the same condition register.
1604 if (Pred1[1].getReg() != Pred2[1].getReg())
1605 return false;
1606
Hal Finkel5711eca2013-04-09 22:58:37 +00001607 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1608 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1609
1610 if (P1 == P2)
1611 return true;
1612
1613 // Does P1 subsume P2, e.g. GE subsumes GT.
1614 if (P1 == PPC::PRED_LE &&
1615 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1616 return true;
1617 if (P1 == PPC::PRED_GE &&
1618 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1619 return true;
1620
1621 return false;
1622}
1623
1624bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI,
1625 std::vector<MachineOperand> &Pred) const {
1626 // Note: At the present time, the contents of Pred from this function is
1627 // unused by IfConversion. This implementation follows ARM by pushing the
1628 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1629 // predicate, instructions defining CTR or CTR8 are also included as
1630 // predicate-defining instructions.
1631
1632 const TargetRegisterClass *RCs[] =
1633 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1634 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1635
1636 bool Found = false;
1637 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1638 const MachineOperand &MO = MI->getOperand(i);
Hal Finkelaf822012013-04-10 07:17:47 +00001639 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001640 const TargetRegisterClass *RC = RCs[c];
Hal Finkelaf822012013-04-10 07:17:47 +00001641 if (MO.isReg()) {
1642 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001643 Pred.push_back(MO);
1644 Found = true;
1645 }
Hal Finkelaf822012013-04-10 07:17:47 +00001646 } else if (MO.isRegMask()) {
1647 for (TargetRegisterClass::iterator I = RC->begin(),
1648 IE = RC->end(); I != IE; ++I)
1649 if (MO.clobbersPhysReg(*I)) {
1650 Pred.push_back(MO);
1651 Found = true;
1652 }
Hal Finkel5711eca2013-04-09 22:58:37 +00001653 }
1654 }
1655 }
1656
1657 return Found;
1658}
1659
1660bool PPCInstrInfo::isPredicable(MachineInstr *MI) const {
1661 unsigned OpC = MI->getOpcode();
1662 switch (OpC) {
1663 default:
1664 return false;
1665 case PPC::B:
1666 case PPC::BLR:
Hal Finkelf4a22c02015-01-13 17:47:54 +00001667 case PPC::BLR8:
Hal Finkel500b0042013-04-10 06:42:34 +00001668 case PPC::BCTR:
1669 case PPC::BCTR8:
1670 case PPC::BCTRL:
1671 case PPC::BCTRL8:
Hal Finkel5711eca2013-04-09 22:58:37 +00001672 return true;
1673 }
1674}
1675
Hal Finkel82656cb2013-04-18 22:15:08 +00001676bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI,
1677 unsigned &SrcReg, unsigned &SrcReg2,
1678 int &Mask, int &Value) const {
1679 unsigned Opc = MI->getOpcode();
1680
1681 switch (Opc) {
1682 default: return false;
1683 case PPC::CMPWI:
1684 case PPC::CMPLWI:
1685 case PPC::CMPDI:
1686 case PPC::CMPLDI:
1687 SrcReg = MI->getOperand(1).getReg();
1688 SrcReg2 = 0;
1689 Value = MI->getOperand(2).getImm();
1690 Mask = 0xFFFF;
1691 return true;
1692 case PPC::CMPW:
1693 case PPC::CMPLW:
1694 case PPC::CMPD:
1695 case PPC::CMPLD:
1696 case PPC::FCMPUS:
1697 case PPC::FCMPUD:
1698 SrcReg = MI->getOperand(1).getReg();
1699 SrcReg2 = MI->getOperand(2).getReg();
1700 return true;
1701 }
1702}
Hal Finkele6322392013-04-19 22:08:38 +00001703
Hal Finkel82656cb2013-04-18 22:15:08 +00001704bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr,
1705 unsigned SrcReg, unsigned SrcReg2,
1706 int Mask, int Value,
1707 const MachineRegisterInfo *MRI) const {
Hal Finkelb12da6b2013-04-18 22:54:25 +00001708 if (DisableCmpOpt)
1709 return false;
1710
Hal Finkel82656cb2013-04-18 22:15:08 +00001711 int OpC = CmpInstr->getOpcode();
1712 unsigned CRReg = CmpInstr->getOperand(0).getReg();
Hal Finkel08e53ee2013-05-08 12:16:14 +00001713
1714 // FP record forms set CR1 based on the execption status bits, not a
1715 // comparison with zero.
1716 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1717 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001718
1719 // The record forms set the condition register based on a signed comparison
1720 // with zero (so says the ISA manual). This is not as straightforward as it
1721 // seems, however, because this is always a 64-bit comparison on PPC64, even
1722 // for instructions that are 32-bit in nature (like slw for example).
1723 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1724 // for equality checks (as those don't depend on the sign). On PPC64,
1725 // we are restricted to equality for unsigned 64-bit comparisons and for
1726 // signed 32-bit comparisons the applicability is more restricted.
Eric Christopher1dcea732014-06-12 21:48:52 +00001727 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel82656cb2013-04-18 22:15:08 +00001728 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1729 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1730 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1731
1732 // Get the unique definition of SrcReg.
1733 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1734 if (!MI) return false;
1735 int MIOpC = MI->getOpcode();
1736
1737 bool equalityOnly = false;
1738 bool noSub = false;
1739 if (isPPC64) {
1740 if (is32BitSignedCompare) {
1741 // We can perform this optimization only if MI is sign-extending.
1742 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1743 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1744 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1745 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1746 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1747 noSub = true;
1748 } else
1749 return false;
1750 } else if (is32BitUnsignedCompare) {
1751 // We can perform this optimization, equality only, if MI is
1752 // zero-extending.
1753 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1754 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
1755 MIOpC == PPC::SRW || MIOpC == PPC::SRWo) {
1756 noSub = true;
1757 equalityOnly = true;
1758 } else
1759 return false;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001760 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001761 equalityOnly = is64BitUnsignedCompare;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001762 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001763 equalityOnly = is32BitUnsignedCompare;
1764
1765 if (equalityOnly) {
1766 // We need to check the uses of the condition register in order to reject
1767 // non-equality comparisons.
Owen Anderson16c6bf42014-03-13 23:12:04 +00001768 for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg),
1769 IE = MRI->use_instr_end(); I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001770 MachineInstr *UseMI = &*I;
1771 if (UseMI->getOpcode() == PPC::BCC) {
1772 unsigned Pred = UseMI->getOperand(0).getImm();
Hal Finkelc3632452013-05-07 17:49:55 +00001773 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE)
1774 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001775 } else if (UseMI->getOpcode() == PPC::ISEL ||
1776 UseMI->getOpcode() == PPC::ISEL8) {
1777 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkelc3632452013-05-07 17:49:55 +00001778 if (SubIdx != PPC::sub_eq)
1779 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001780 } else
1781 return false;
1782 }
1783 }
1784
Hal Finkelc3632452013-05-07 17:49:55 +00001785 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001786
1787 // Scan forward to find the first use of the compare.
1788 for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end();
1789 I != EL; ++I) {
1790 bool FoundUse = false;
Owen Anderson16c6bf42014-03-13 23:12:04 +00001791 for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg),
1792 JE = MRI->use_instr_end(); J != JE; ++J)
Hal Finkel82656cb2013-04-18 22:15:08 +00001793 if (&*J == &*I) {
1794 FoundUse = true;
1795 break;
1796 }
1797
1798 if (FoundUse)
1799 break;
1800 }
1801
Hal Finkel82656cb2013-04-18 22:15:08 +00001802 // There are two possible candidates which can be changed to set CR[01].
1803 // One is MI, the other is a SUB instruction.
1804 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
Craig Topper062a2ba2014-04-25 05:30:21 +00001805 MachineInstr *Sub = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001806 if (SrcReg2 != 0)
1807 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00001808 MI = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001809 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1810 // same BB as the comparison. This is to allow the check below to avoid calls
1811 // (and other explicit clobbers); instead we should really check for these
1812 // more explicitly (in at least a few predecessors).
1813 else if (MI->getParent() != CmpInstr->getParent() || Value != 0) {
1814 // PPC does not have a record-form SUBri.
1815 return false;
1816 }
1817
1818 // Search for Sub.
1819 const TargetRegisterInfo *TRI = &getRegisterInfo();
1820 --I;
Hal Finkelc3632452013-05-07 17:49:55 +00001821
1822 // Get ready to iterate backward from CmpInstr.
1823 MachineBasicBlock::iterator E = MI,
1824 B = CmpInstr->getParent()->begin();
1825
Hal Finkel82656cb2013-04-18 22:15:08 +00001826 for (; I != E && !noSub; --I) {
1827 const MachineInstr &Instr = *I;
1828 unsigned IOpC = Instr.getOpcode();
1829
1830 if (&*I != CmpInstr && (
Hal Finkel08e53ee2013-05-08 12:16:14 +00001831 Instr.modifiesRegister(PPC::CR0, TRI) ||
1832 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel82656cb2013-04-18 22:15:08 +00001833 // This instruction modifies or uses the record condition register after
1834 // the one we want to change. While we could do this transformation, it
1835 // would likely not be profitable. This transformation removes one
1836 // instruction, and so even forcing RA to generate one move probably
1837 // makes it unprofitable.
1838 return false;
1839
1840 // Check whether CmpInstr can be made redundant by the current instruction.
1841 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1842 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1843 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1844 ((Instr.getOperand(1).getReg() == SrcReg &&
1845 Instr.getOperand(2).getReg() == SrcReg2) ||
1846 (Instr.getOperand(1).getReg() == SrcReg2 &&
1847 Instr.getOperand(2).getReg() == SrcReg))) {
1848 Sub = &*I;
1849 break;
1850 }
1851
Hal Finkel82656cb2013-04-18 22:15:08 +00001852 if (I == B)
1853 // The 'and' is below the comparison instruction.
1854 return false;
1855 }
1856
1857 // Return false if no candidates exist.
1858 if (!MI && !Sub)
1859 return false;
1860
1861 // The single candidate is called MI.
1862 if (!MI) MI = Sub;
1863
1864 int NewOpC = -1;
1865 MIOpC = MI->getOpcode();
1866 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1867 NewOpC = MIOpC;
1868 else {
1869 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1870 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1871 NewOpC = MIOpC;
1872 }
1873
1874 // FIXME: On the non-embedded POWER architectures, only some of the record
1875 // forms are fast, and we should use only the fast ones.
1876
1877 // The defining instruction has a record form (or is already a record
1878 // form). It is possible, however, that we'll need to reverse the condition
1879 // code of the users.
1880 if (NewOpC == -1)
1881 return false;
1882
Hal Finkele6322392013-04-19 22:08:38 +00001883 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1884 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
Hal Finkel82656cb2013-04-18 22:15:08 +00001885
1886 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1887 // needs to be updated to be based on SUB. Push the condition code
1888 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1889 // condition code of these operands will be modified.
1890 bool ShouldSwap = false;
1891 if (Sub) {
1892 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1893 Sub->getOperand(2).getReg() == SrcReg;
1894
1895 // The operands to subf are the opposite of sub, so only in the fixed-point
1896 // case, invert the order.
Hal Finkel08e53ee2013-05-08 12:16:14 +00001897 ShouldSwap = !ShouldSwap;
Hal Finkel82656cb2013-04-18 22:15:08 +00001898 }
1899
1900 if (ShouldSwap)
Owen Anderson16c6bf42014-03-13 23:12:04 +00001901 for (MachineRegisterInfo::use_instr_iterator
1902 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1903 I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001904 MachineInstr *UseMI = &*I;
1905 if (UseMI->getOpcode() == PPC::BCC) {
1906 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hal Finkele6322392013-04-19 22:08:38 +00001907 assert((!equalityOnly ||
1908 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1909 "Invalid predicate for equality-only optimization");
Owen Anderson16c6bf42014-03-13 23:12:04 +00001910 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
Hal Finkel0f64e212013-04-20 05:16:26 +00001911 PPC::getSwappedPredicate(Pred)));
Hal Finkel82656cb2013-04-18 22:15:08 +00001912 } else if (UseMI->getOpcode() == PPC::ISEL ||
1913 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkele6322392013-04-19 22:08:38 +00001914 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1915 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1916 "Invalid CR bit for equality-only optimization");
1917
1918 if (NewSubReg == PPC::sub_lt)
1919 NewSubReg = PPC::sub_gt;
1920 else if (NewSubReg == PPC::sub_gt)
1921 NewSubReg = PPC::sub_lt;
1922
Owen Anderson16c6bf42014-03-13 23:12:04 +00001923 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
Hal Finkele6322392013-04-19 22:08:38 +00001924 NewSubReg));
Hal Finkel82656cb2013-04-18 22:15:08 +00001925 } else // We need to abort on a user we don't understand.
1926 return false;
1927 }
1928
1929 // Create a new virtual register to hold the value of the CR set by the
1930 // record-form instruction. If the instruction was not previously in
1931 // record form, then set the kill flag on the CR.
1932 CmpInstr->eraseFromParent();
1933
1934 MachineBasicBlock::iterator MII = MI;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001935 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
Hal Finkel82656cb2013-04-18 22:15:08 +00001936 get(TargetOpcode::COPY), CRReg)
Hal Finkel08e53ee2013-05-08 12:16:14 +00001937 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel82656cb2013-04-18 22:15:08 +00001938
1939 if (MIOpC != NewOpC) {
1940 // We need to be careful here: we're replacing one instruction with
1941 // another, and we need to make sure that we get all of the right
1942 // implicit uses and defs. On the other hand, the caller may be holding
1943 // an iterator to this instruction, and so we can't delete it (this is
1944 // specifically the case if this is the instruction directly after the
1945 // compare).
1946
1947 const MCInstrDesc &NewDesc = get(NewOpC);
1948 MI->setDesc(NewDesc);
1949
1950 if (NewDesc.ImplicitDefs)
1951 for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs();
1952 *ImpDefs; ++ImpDefs)
1953 if (!MI->definesRegister(*ImpDefs))
1954 MI->addOperand(*MI->getParent()->getParent(),
1955 MachineOperand::CreateReg(*ImpDefs, true, true));
1956 if (NewDesc.ImplicitUses)
1957 for (const uint16_t *ImpUses = NewDesc.getImplicitUses();
1958 *ImpUses; ++ImpUses)
1959 if (!MI->readsRegister(*ImpUses))
1960 MI->addOperand(*MI->getParent()->getParent(),
1961 MachineOperand::CreateReg(*ImpUses, false, true));
1962 }
1963
1964 // Modify the condition code of operands in OperandsToUpdate.
1965 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1966 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkele6322392013-04-19 22:08:38 +00001967 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1968 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001969
Hal Finkele6322392013-04-19 22:08:38 +00001970 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1971 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001972
1973 return true;
1974}
1975
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001976/// GetInstSize - Return the number of bytes of code the specified
1977/// instruction may be. This returns the maximum number of bytes.
1978///
1979unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Hal Finkela7bbaf62014-02-02 06:12:27 +00001980 unsigned Opcode = MI->getOpcode();
1981
1982 if (Opcode == PPC::INLINEASM) {
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001983 const MachineFunction *MF = MI->getParent()->getParent();
1984 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +00001985 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Hal Finkel934361a2015-01-14 01:07:51 +00001986 } else if (Opcode == TargetOpcode::STACKMAP) {
1987 return MI->getOperand(1).getImm();
1988 } else if (Opcode == TargetOpcode::PATCHPOINT) {
1989 PatchPointOpers Opers(MI);
1990 return Opers.getMetaOper(PatchPointOpers::NBytesPos).getImm();
Hal Finkela7bbaf62014-02-02 06:12:27 +00001991 } else {
1992 const MCInstrDesc &Desc = get(Opcode);
1993 return Desc.getSize();
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001994 }
1995}
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001996