blob: 70e223d78ada650c9bf626be37dc304dfb895cb9 [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
Hal Finkelccf92592015-09-06 04:17:30 +0000574 // We can't commute a trivial mask (there is no way to represent an all-zero
575 // mask).
576 if (MB == 0 && ME == 31)
577 return nullptr;
578
Evan Cheng03553bb2008-06-16 07:33:11 +0000579 if (NewMI) {
580 // Create a new instruction.
581 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
582 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000583 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000584 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
585 .addReg(Reg2, getKillRegState(Reg2IsKill))
586 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng03553bb2008-06-16 07:33:11 +0000587 .addImm((ME+1) & 31)
588 .addImm((MB-1) & 31);
589 }
590
Andrew Tricke3398282013-12-17 04:50:45 +0000591 if (ChangeReg0) {
Evan Cheng03553bb2008-06-16 07:33:11 +0000592 MI->getOperand(0).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000593 MI->getOperand(0).setSubReg(SubReg2);
594 }
Chris Lattner10d63412006-05-04 17:52:23 +0000595 MI->getOperand(2).setReg(Reg1);
596 MI->getOperand(1).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000597 MI->getOperand(2).setSubReg(SubReg1);
598 MI->getOperand(1).setSubReg(SubReg2);
Chris Lattner60055892007-12-30 21:56:09 +0000599 MI->getOperand(2).setIsKill(Reg1IsKill);
600 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000601
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000602 // Swap the mask around.
Chris Lattner5c463782007-12-30 20:49:49 +0000603 MI->getOperand(4).setImm((ME+1) & 31);
604 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000605 return MI;
606}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000607
Hal Finkel6c32ff32014-03-25 19:26:43 +0000608bool PPCInstrInfo::findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
609 unsigned &SrcOpIdx2) const {
610 // For VSX A-Type FMA instructions, it is the first two operands that can be
611 // commuted, however, because the non-encoded tied input operand is listed
612 // first, the operands to swap are actually the second and third.
613
614 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
615 if (AltOpc == -1)
616 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
617
618 SrcOpIdx1 = 2;
619 SrcOpIdx2 = 3;
620 return true;
621}
622
Andrew Trickc416ba62010-12-24 04:28:06 +0000623void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000624 MachineBasicBlock::iterator MI) const {
Hal Finkelceb1f122013-12-12 00:19:11 +0000625 // This function is used for scheduling, and the nop wanted here is the type
626 // that terminates dispatch groups on the POWER cores.
Eric Christopher1dcea732014-06-12 21:48:52 +0000627 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000628 unsigned Opcode;
629 switch (Directive) {
630 default: Opcode = PPC::NOP; break;
631 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
632 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
Will Schmidt970ff642014-06-26 13:36:19 +0000633 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 +0000634 }
Chris Lattnera47294ed2006-10-13 21:21:17 +0000635
Hal Finkelceb1f122013-12-12 00:19:11 +0000636 DebugLoc DL;
637 BuildMI(MBB, MI, DL, get(Opcode));
638}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000639
Joerg Sonnenberger7ee0f312014-08-08 19:13:23 +0000640/// getNoopForMachoTarget - Return the noop instruction to use for a noop.
641void PPCInstrInfo::getNoopForMachoTarget(MCInst &NopInst) const {
642 NopInst.setOpcode(PPC::NOP);
643}
644
Chris Lattnera47294ed2006-10-13 21:21:17 +0000645// Branch analysis.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000646// Note: If the condition register is set to CTR or CTR8 then this is a
647// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Chris Lattnera47294ed2006-10-13 21:21:17 +0000648bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
649 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000650 SmallVectorImpl<MachineOperand> &Cond,
651 bool AllowModify) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000652 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000653
Chris Lattnera47294ed2006-10-13 21:21:17 +0000654 // If the block has no terminators, it just falls into the block after it.
Benjamin Kramer92861d72015-06-25 13:39:03 +0000655 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
656 if (I == MBB.end())
Dale Johannesen4244d122010-04-02 01:38:09 +0000657 return false;
Benjamin Kramer92861d72015-06-25 13:39:03 +0000658
Dale Johannesen4244d122010-04-02 01:38:09 +0000659 if (!isUnpredicatedTerminator(I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000660 return false;
661
662 // Get the last instruction in the block.
663 MachineInstr *LastInst = I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000664
Chris Lattnera47294ed2006-10-13 21:21:17 +0000665 // If there is only one terminator instruction, process it.
Evan Cheng5514bbe2007-06-08 21:59:56 +0000666 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000667 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000668 if (!LastInst->getOperand(0).isMBB())
669 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000670 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000671 return false;
Chris Lattnere0263792006-11-17 22:14:47 +0000672 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000673 if (!LastInst->getOperand(2).isMBB())
674 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000675 // Block ends with fall-through condbranch.
Chris Lattnera5bb3702007-12-30 23:10:15 +0000676 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000677 Cond.push_back(LastInst->getOperand(0));
678 Cond.push_back(LastInst->getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000679 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000680 } else if (LastInst->getOpcode() == PPC::BC) {
681 if (!LastInst->getOperand(1).isMBB())
682 return true;
683 // Block ends with fall-through condbranch.
684 TBB = LastInst->getOperand(1).getMBB();
685 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
686 Cond.push_back(LastInst->getOperand(0));
687 return false;
688 } else if (LastInst->getOpcode() == PPC::BCn) {
689 if (!LastInst->getOperand(1).isMBB())
690 return true;
691 // Block ends with fall-through condbranch.
692 TBB = LastInst->getOperand(1).getMBB();
693 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
694 Cond.push_back(LastInst->getOperand(0));
695 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000696 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
697 LastInst->getOpcode() == PPC::BDNZ) {
698 if (!LastInst->getOperand(0).isMBB())
699 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000700 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000701 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000702 TBB = LastInst->getOperand(0).getMBB();
703 Cond.push_back(MachineOperand::CreateImm(1));
704 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
705 true));
706 return false;
707 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
708 LastInst->getOpcode() == PPC::BDZ) {
709 if (!LastInst->getOperand(0).isMBB())
710 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000711 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000712 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000713 TBB = LastInst->getOperand(0).getMBB();
714 Cond.push_back(MachineOperand::CreateImm(0));
715 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
716 true));
717 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000718 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000719
Chris Lattnera47294ed2006-10-13 21:21:17 +0000720 // Otherwise, don't know what this is.
721 return true;
722 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000723
Chris Lattnera47294ed2006-10-13 21:21:17 +0000724 // Get the instruction before it if it's a terminator.
725 MachineInstr *SecondLastInst = I;
726
727 // If there are three terminators, we don't know what sort of block this is.
728 if (SecondLastInst && I != MBB.begin() &&
Evan Cheng5514bbe2007-06-08 21:59:56 +0000729 isUnpredicatedTerminator(--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000730 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000731
Chris Lattnere0263792006-11-17 22:14:47 +0000732 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000733 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnera47294ed2006-10-13 21:21:17 +0000734 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000735 if (!SecondLastInst->getOperand(2).isMBB() ||
736 !LastInst->getOperand(0).isMBB())
737 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000738 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000739 Cond.push_back(SecondLastInst->getOperand(0));
740 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattnera5bb3702007-12-30 23:10:15 +0000741 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000742 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000743 } else if (SecondLastInst->getOpcode() == PPC::BC &&
744 LastInst->getOpcode() == PPC::B) {
745 if (!SecondLastInst->getOperand(1).isMBB() ||
746 !LastInst->getOperand(0).isMBB())
747 return true;
748 TBB = SecondLastInst->getOperand(1).getMBB();
749 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
750 Cond.push_back(SecondLastInst->getOperand(0));
751 FBB = LastInst->getOperand(0).getMBB();
752 return false;
753 } else if (SecondLastInst->getOpcode() == PPC::BCn &&
754 LastInst->getOpcode() == PPC::B) {
755 if (!SecondLastInst->getOperand(1).isMBB() ||
756 !LastInst->getOperand(0).isMBB())
757 return true;
758 TBB = SecondLastInst->getOperand(1).getMBB();
759 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
760 Cond.push_back(SecondLastInst->getOperand(0));
761 FBB = LastInst->getOperand(0).getMBB();
762 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000763 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
764 SecondLastInst->getOpcode() == PPC::BDNZ) &&
765 LastInst->getOpcode() == PPC::B) {
766 if (!SecondLastInst->getOperand(0).isMBB() ||
767 !LastInst->getOperand(0).isMBB())
768 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000769 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000770 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000771 TBB = SecondLastInst->getOperand(0).getMBB();
772 Cond.push_back(MachineOperand::CreateImm(1));
773 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
774 true));
775 FBB = LastInst->getOperand(0).getMBB();
776 return false;
777 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
778 SecondLastInst->getOpcode() == PPC::BDZ) &&
779 LastInst->getOpcode() == PPC::B) {
780 if (!SecondLastInst->getOperand(0).isMBB() ||
781 !LastInst->getOperand(0).isMBB())
782 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000783 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000784 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000785 TBB = SecondLastInst->getOperand(0).getMBB();
786 Cond.push_back(MachineOperand::CreateImm(0));
787 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
788 true));
789 FBB = LastInst->getOperand(0).getMBB();
790 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000791 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000792
Dale Johannesenc6855462007-06-13 17:59:52 +0000793 // If the block ends with two PPC:Bs, handle it. The second one is not
794 // executed, so remove it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000795 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesenc6855462007-06-13 17:59:52 +0000796 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000797 if (!SecondLastInst->getOperand(0).isMBB())
798 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000799 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000800 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000801 if (AllowModify)
802 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000803 return false;
804 }
805
Chris Lattnera47294ed2006-10-13 21:21:17 +0000806 // Otherwise, can't handle this.
807 return true;
808}
809
Evan Cheng99be49d2007-05-18 00:05:48 +0000810unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Benjamin Kramer92861d72015-06-25 13:39:03 +0000811 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
812 if (I == MBB.end())
813 return 0;
814
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000815 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000816 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000817 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
818 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000819 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000820
Chris Lattnera47294ed2006-10-13 21:21:17 +0000821 // Remove the branch.
822 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000823
Chris Lattnera47294ed2006-10-13 21:21:17 +0000824 I = MBB.end();
825
Evan Cheng99be49d2007-05-18 00:05:48 +0000826 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000827 --I;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000828 if (I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000829 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000830 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
831 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000832 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000833
Chris Lattnera47294ed2006-10-13 21:21:17 +0000834 // Remove the branch.
835 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000836 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000837}
838
Evan Cheng99be49d2007-05-18 00:05:48 +0000839unsigned
840PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
841 MachineBasicBlock *FBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000842 ArrayRef<MachineOperand> Cond,
Stuart Hastings0125b642010-06-17 22:43:56 +0000843 DebugLoc DL) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000844 // Shouldn't be a fall through.
845 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000846 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000847 "PPC branch conditions have two components!");
Andrew Trickc416ba62010-12-24 04:28:06 +0000848
Eric Christopher1dcea732014-06-12 21:48:52 +0000849 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000850
Chris Lattner94e04442006-10-21 05:36:13 +0000851 // One-way branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000852 if (!FBB) {
Chris Lattner94e04442006-10-21 05:36:13 +0000853 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000854 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000855 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
856 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
857 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
858 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000859 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
860 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
861 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
862 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000863 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000864 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000865 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000866 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000867 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000868
Chris Lattnerd8816602006-10-21 05:42:09 +0000869 // Two-way Conditional Branch.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000870 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
871 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
872 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
873 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000874 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
875 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
876 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
877 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000878 else
879 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000880 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000881 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000882 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000883}
884
Hal Finkeled6a2852013-04-05 23:29:01 +0000885// Select analysis.
886bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000887 ArrayRef<MachineOperand> Cond,
Hal Finkeled6a2852013-04-05 23:29:01 +0000888 unsigned TrueReg, unsigned FalseReg,
889 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000890 if (!Subtarget.hasISEL())
Hal Finkeled6a2852013-04-05 23:29:01 +0000891 return false;
892
893 if (Cond.size() != 2)
894 return false;
895
896 // If this is really a bdnz-like condition, then it cannot be turned into a
897 // select.
898 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
899 return false;
900
901 // Check register classes.
902 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
903 const TargetRegisterClass *RC =
904 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
905 if (!RC)
906 return false;
907
908 // isel is for regular integer GPRs only.
909 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkel8e8618a2013-07-15 20:22:58 +0000910 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
911 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
912 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkeled6a2852013-04-05 23:29:01 +0000913 return false;
914
915 // FIXME: These numbers are for the A2, how well they work for other cores is
916 // an open question. On the A2, the isel instruction has a 2-cycle latency
917 // but single-cycle throughput. These numbers are used in combination with
918 // the MispredictPenalty setting from the active SchedMachineModel.
919 CondCycles = 1;
920 TrueCycles = 1;
921 FalseCycles = 1;
922
923 return true;
924}
925
926void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
927 MachineBasicBlock::iterator MI, DebugLoc dl,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000928 unsigned DestReg, ArrayRef<MachineOperand> Cond,
Hal Finkeled6a2852013-04-05 23:29:01 +0000929 unsigned TrueReg, unsigned FalseReg) const {
930 assert(Cond.size() == 2 &&
931 "PPC branch conditions have two components!");
932
Eric Christopher1dcea732014-06-12 21:48:52 +0000933 assert(Subtarget.hasISEL() &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000934 "Cannot insert select on target without ISEL support");
935
936 // Get the register classes.
937 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
938 const TargetRegisterClass *RC =
939 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
940 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkel8e8618a2013-07-15 20:22:58 +0000941
942 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
943 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
944 assert((Is64Bit ||
945 PPC::GPRCRegClass.hasSubClassEq(RC) ||
946 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000947 "isel is for regular integer GPRs only");
948
Hal Finkel8e8618a2013-07-15 20:22:58 +0000949 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Hal Finkeled6a2852013-04-05 23:29:01 +0000950 unsigned SelectPred = Cond[0].getImm();
951
952 unsigned SubIdx;
953 bool SwapOps;
954 switch (SelectPred) {
955 default: llvm_unreachable("invalid predicate for isel");
956 case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
957 case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
958 case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
959 case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
960 case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
961 case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
962 case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
963 case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
Hal Finkel940ab932014-02-28 00:27:01 +0000964 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break;
965 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
Hal Finkeled6a2852013-04-05 23:29:01 +0000966 }
967
968 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
969 SecondReg = SwapOps ? TrueReg : FalseReg;
970
971 // The first input register of isel cannot be r0. If it is a member
972 // of a register class that can be r0, then copy it first (the
973 // register allocator should eliminate the copy).
974 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
975 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
976 const TargetRegisterClass *FirstRC =
977 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
978 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
979 unsigned OldFirstReg = FirstReg;
980 FirstReg = MRI.createVirtualRegister(FirstRC);
981 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
982 .addReg(OldFirstReg);
983 }
984
985 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
986 .addReg(FirstReg).addReg(SecondReg)
987 .addReg(Cond[1].getReg(), 0, SubIdx);
988}
989
Kit Barton535e69d2015-03-25 19:36:23 +0000990static unsigned getCRBitValue(unsigned CRBit) {
991 unsigned Ret = 4;
992 if (CRBit == PPC::CR0LT || CRBit == PPC::CR1LT ||
993 CRBit == PPC::CR2LT || CRBit == PPC::CR3LT ||
994 CRBit == PPC::CR4LT || CRBit == PPC::CR5LT ||
995 CRBit == PPC::CR6LT || CRBit == PPC::CR7LT)
996 Ret = 3;
997 if (CRBit == PPC::CR0GT || CRBit == PPC::CR1GT ||
998 CRBit == PPC::CR2GT || CRBit == PPC::CR3GT ||
999 CRBit == PPC::CR4GT || CRBit == PPC::CR5GT ||
1000 CRBit == PPC::CR6GT || CRBit == PPC::CR7GT)
1001 Ret = 2;
1002 if (CRBit == PPC::CR0EQ || CRBit == PPC::CR1EQ ||
1003 CRBit == PPC::CR2EQ || CRBit == PPC::CR3EQ ||
1004 CRBit == PPC::CR4EQ || CRBit == PPC::CR5EQ ||
1005 CRBit == PPC::CR6EQ || CRBit == PPC::CR7EQ)
1006 Ret = 1;
1007 if (CRBit == PPC::CR0UN || CRBit == PPC::CR1UN ||
1008 CRBit == PPC::CR2UN || CRBit == PPC::CR3UN ||
1009 CRBit == PPC::CR4UN || CRBit == PPC::CR5UN ||
1010 CRBit == PPC::CR6UN || CRBit == PPC::CR7UN)
1011 Ret = 0;
1012
1013 assert(Ret != 4 && "Invalid CR bit register");
1014 return Ret;
1015}
1016
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +00001017void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
1018 MachineBasicBlock::iterator I, DebugLoc DL,
1019 unsigned DestReg, unsigned SrcReg,
1020 bool KillSrc) const {
Hal Finkel27774d92014-03-13 07:58:58 +00001021 // We can end up with self copies and similar things as a result of VSX copy
Hal Finkel9dcb3582014-03-27 22:46:28 +00001022 // legalization. Promote them here.
Hal Finkel27774d92014-03-13 07:58:58 +00001023 const TargetRegisterInfo *TRI = &getRegisterInfo();
1024 if (PPC::F8RCRegClass.contains(DestReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +00001025 PPC::VSRCRegClass.contains(SrcReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +00001026 unsigned SuperReg =
1027 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass);
1028
Hal Finkel9dcb3582014-03-27 22:46:28 +00001029 if (VSXSelfCopyCrash && SrcReg == SuperReg)
1030 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +00001031
1032 DestReg = SuperReg;
1033 } else if (PPC::VRRCRegClass.contains(DestReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +00001034 PPC::VSRCRegClass.contains(SrcReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +00001035 unsigned SuperReg =
1036 TRI->getMatchingSuperReg(DestReg, PPC::sub_128, &PPC::VSRCRegClass);
1037
Hal Finkel9dcb3582014-03-27 22:46:28 +00001038 if (VSXSelfCopyCrash && SrcReg == SuperReg)
1039 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +00001040
1041 DestReg = SuperReg;
1042 } else if (PPC::F8RCRegClass.contains(SrcReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +00001043 PPC::VSRCRegClass.contains(DestReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +00001044 unsigned SuperReg =
1045 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass);
1046
Hal Finkel9dcb3582014-03-27 22:46:28 +00001047 if (VSXSelfCopyCrash && DestReg == SuperReg)
1048 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +00001049
1050 SrcReg = SuperReg;
1051 } else if (PPC::VRRCRegClass.contains(SrcReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +00001052 PPC::VSRCRegClass.contains(DestReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +00001053 unsigned SuperReg =
1054 TRI->getMatchingSuperReg(SrcReg, PPC::sub_128, &PPC::VSRCRegClass);
1055
Hal Finkel9dcb3582014-03-27 22:46:28 +00001056 if (VSXSelfCopyCrash && DestReg == SuperReg)
1057 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +00001058
1059 SrcReg = SuperReg;
1060 }
1061
Kit Barton535e69d2015-03-25 19:36:23 +00001062 // Different class register copy
1063 if (PPC::CRBITRCRegClass.contains(SrcReg) &&
1064 PPC::GPRCRegClass.contains(DestReg)) {
1065 unsigned CRReg = getCRFromCRBit(SrcReg);
1066 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg)
1067 .addReg(CRReg), getKillRegState(KillSrc);
1068 // Rotate the CR bit in the CR fields to be the least significant bit and
1069 // then mask with 0x1 (MB = ME = 31).
1070 BuildMI(MBB, I, DL, get(PPC::RLWINM), DestReg)
1071 .addReg(DestReg, RegState::Kill)
1072 .addImm(TRI->getEncodingValue(CRReg) * 4 + (4 - getCRBitValue(SrcReg)))
1073 .addImm(31)
1074 .addImm(31);
1075 return;
1076 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
1077 PPC::G8RCRegClass.contains(DestReg)) {
1078 BuildMI(MBB, I, DL, get(PPC::MFOCRF8), DestReg)
1079 .addReg(SrcReg), getKillRegState(KillSrc);
1080 return;
1081 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
1082 PPC::GPRCRegClass.contains(DestReg)) {
1083 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg)
1084 .addReg(SrcReg), getKillRegState(KillSrc);
1085 return;
1086 }
1087
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +00001088 unsigned Opc;
1089 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
1090 Opc = PPC::OR;
1091 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
1092 Opc = PPC::OR8;
1093 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
1094 Opc = PPC::FMR;
1095 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
1096 Opc = PPC::MCRF;
1097 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
1098 Opc = PPC::VOR;
Hal Finkel27774d92014-03-13 07:58:58 +00001099 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg))
Hal Finkelbbad2332014-03-24 09:36:36 +00001100 // There are two different ways this can be done:
Hal Finkel27774d92014-03-13 07:58:58 +00001101 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
1102 // issue in VSU pipeline 0.
1103 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
1104 // can go to either pipeline.
Hal Finkelbbad2332014-03-24 09:36:36 +00001105 // We'll always use xxlor here, because in practically all cases where
1106 // copies are generated, they are close enough to some use that the
1107 // lower-latency form is preferable.
Hal Finkel27774d92014-03-13 07:58:58 +00001108 Opc = PPC::XXLOR;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001109 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg) ||
1110 PPC::VSSRCRegClass.contains(DestReg, SrcReg))
Hal Finkel19be5062014-03-29 05:29:01 +00001111 Opc = PPC::XXLORf;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001112 else if (PPC::QFRCRegClass.contains(DestReg, SrcReg))
1113 Opc = PPC::QVFMR;
1114 else if (PPC::QSRCRegClass.contains(DestReg, SrcReg))
1115 Opc = PPC::QVFMRs;
1116 else if (PPC::QBRCRegClass.contains(DestReg, SrcReg))
1117 Opc = PPC::QVFMRb;
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +00001118 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
1119 Opc = PPC::CROR;
1120 else
1121 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +00001122
Evan Cheng6cc775f2011-06-28 19:10:37 +00001123 const MCInstrDesc &MCID = get(Opc);
1124 if (MCID.getNumOperands() == 3)
1125 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +00001126 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
1127 else
Evan Cheng6cc775f2011-06-28 19:10:37 +00001128 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +00001129}
1130
Hal Finkel8f6834d2011-12-05 17:55:17 +00001131// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +00001132bool
Dan Gohman3b460302008-07-07 23:14:23 +00001133PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
1134 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +00001135 int FrameIdx,
1136 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +00001137 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001138 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +00001139 // Note: If additional store instructions are added here,
1140 // update isStoreToStackSlot.
1141
Chris Lattner6f306d72010-04-02 20:16:16 +00001142 DebugLoc DL;
Hal Finkel4e703bc2014-01-28 05:32:58 +00001143 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
1144 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +00001145 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
1146 .addReg(SrcReg,
1147 getKillRegState(isKill)),
1148 FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +00001149 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
1150 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +00001151 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
1152 .addReg(SrcReg,
1153 getKillRegState(isKill)),
1154 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001155 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +00001156 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
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::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +00001161 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +00001162 .addReg(SrcReg,
1163 getKillRegState(isKill)),
1164 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001165 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +00001166 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
1167 .addReg(SrcReg,
1168 getKillRegState(isKill)),
1169 FrameIdx));
1170 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001171 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +00001172 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT))
1173 .addReg(SrcReg,
1174 getKillRegState(isKill)),
1175 FrameIdx));
1176 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001177 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +00001178 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
1179 .addReg(SrcReg,
1180 getKillRegState(isKill)),
1181 FrameIdx));
1182 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +00001183 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
1184 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXVD2X))
1185 .addReg(SrcReg,
1186 getKillRegState(isKill)),
1187 FrameIdx));
1188 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +00001189 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
1190 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSDX))
1191 .addReg(SrcReg,
1192 getKillRegState(isKill)),
1193 FrameIdx));
1194 NonRI = true;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001195 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
1196 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSSPX))
1197 .addReg(SrcReg,
1198 getKillRegState(isKill)),
1199 FrameIdx));
1200 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +00001201 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001202 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +00001203 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +00001204 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
1205 .addReg(SrcReg,
1206 getKillRegState(isKill)),
1207 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001208 SpillsVRS = true;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001209 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
1210 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDX))
1211 .addReg(SrcReg,
1212 getKillRegState(isKill)),
1213 FrameIdx));
1214 NonRI = true;
1215 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
1216 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFSXs))
1217 .addReg(SrcReg,
1218 getKillRegState(isKill)),
1219 FrameIdx));
1220 NonRI = true;
1221 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
1222 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDXb))
1223 .addReg(SrcReg,
1224 getKillRegState(isKill)),
1225 FrameIdx));
1226 NonRI = true;
Owen Andersoneee14602008-01-01 21:11:32 +00001227 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001228 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +00001229 }
Bill Wendling632ea652008-03-03 22:19:16 +00001230
1231 return false;
Owen Andersoneee14602008-01-01 21:11:32 +00001232}
1233
1234void
1235PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +00001236 MachineBasicBlock::iterator MI,
1237 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +00001238 const TargetRegisterClass *RC,
1239 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +00001240 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +00001241 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +00001242
Hal Finkelbb420f12013-03-15 05:06:04 +00001243 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1244 FuncInfo->setHasSpills();
1245
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001246 bool NonRI = false, SpillsVRS = false;
1247 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
1248 NonRI, SpillsVRS))
Bill Wendling632ea652008-03-03 22:19:16 +00001249 FuncInfo->setSpillsCR();
Bill Wendling632ea652008-03-03 22:19:16 +00001250
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001251 if (SpillsVRS)
1252 FuncInfo->setSpillsVRSAVE();
1253
Hal Finkelfcc51d42013-03-17 04:43:44 +00001254 if (NonRI)
1255 FuncInfo->setHasNonRISpills();
1256
Owen Andersoneee14602008-01-01 21:11:32 +00001257 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1258 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001259
1260 const MachineFrameInfo &MFI = *MF.getFrameInfo();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001261 MachineMemOperand *MMO = MF.getMachineMemOperand(
1262 MachinePointerInfo::getFixedStack(MF, FrameIdx),
1263 MachineMemOperand::MOStore, MFI.getObjectSize(FrameIdx),
1264 MFI.getObjectAlignment(FrameIdx));
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001265 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +00001266}
1267
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001268bool
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001269PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman3b460302008-07-07 23:14:23 +00001270 unsigned DestReg, int FrameIdx,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +00001271 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +00001272 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001273 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +00001274 // Note: If additional load instructions are added here,
1275 // update isLoadFromStackSlot.
1276
Hal Finkel4e703bc2014-01-28 05:32:58 +00001277 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
1278 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +00001279 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
1280 DestReg), FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +00001281 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
1282 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +00001283 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
1284 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001285 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001286 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +00001287 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001288 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001289 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +00001290 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001291 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +00001292 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1293 get(PPC::RESTORE_CR), DestReg),
1294 FrameIdx));
1295 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001296 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +00001297 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1298 get(PPC::RESTORE_CRBIT), DestReg),
1299 FrameIdx));
1300 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001301 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +00001302 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
1303 FrameIdx));
1304 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +00001305 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
1306 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXVD2X), DestReg),
1307 FrameIdx));
1308 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +00001309 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
1310 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSDX), DestReg),
1311 FrameIdx));
1312 NonRI = true;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001313 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
1314 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSSPX), DestReg),
1315 FrameIdx));
1316 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +00001317 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001318 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +00001319 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +00001320 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1321 get(PPC::RESTORE_VRSAVE),
1322 DestReg),
1323 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001324 SpillsVRS = true;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001325 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
1326 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDX), DestReg),
1327 FrameIdx));
1328 NonRI = true;
1329 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
1330 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFSXs), DestReg),
1331 FrameIdx));
1332 NonRI = true;
1333 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
1334 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDXb), DestReg),
1335 FrameIdx));
1336 NonRI = true;
Owen Andersoneee14602008-01-01 21:11:32 +00001337 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001338 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +00001339 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001340
1341 return false;
Owen Andersoneee14602008-01-01 21:11:32 +00001342}
1343
1344void
1345PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +00001346 MachineBasicBlock::iterator MI,
1347 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +00001348 const TargetRegisterClass *RC,
1349 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +00001350 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +00001351 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +00001352 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001353 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelfcc51d42013-03-17 04:43:44 +00001354
1355 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1356 FuncInfo->setHasSpills();
1357
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001358 bool NonRI = false, SpillsVRS = false;
1359 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
1360 NonRI, SpillsVRS))
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001361 FuncInfo->setSpillsCR();
Hal Finkelfcc51d42013-03-17 04:43:44 +00001362
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001363 if (SpillsVRS)
1364 FuncInfo->setSpillsVRSAVE();
1365
Hal Finkelfcc51d42013-03-17 04:43:44 +00001366 if (NonRI)
1367 FuncInfo->setHasNonRISpills();
1368
Owen Andersoneee14602008-01-01 21:11:32 +00001369 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1370 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001371
1372 const MachineFrameInfo &MFI = *MF.getFrameInfo();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001373 MachineMemOperand *MMO = MF.getMachineMemOperand(
1374 MachinePointerInfo::getFixedStack(MF, FrameIdx),
1375 MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIdx),
1376 MFI.getObjectAlignment(FrameIdx));
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001377 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +00001378}
1379
Chris Lattnera47294ed2006-10-13 21:21:17 +00001380bool PPCInstrInfo::
Owen Anderson4f6bf042008-08-14 22:49:33 +00001381ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +00001382 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel96c2d4d2012-06-08 15:38:21 +00001383 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
1384 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
1385 else
1386 // Leave the CR# the same, but invert the condition.
1387 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +00001388 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +00001389}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001390
Hal Finkeld61d4f82013-04-06 19:30:30 +00001391bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
1392 unsigned Reg, MachineRegisterInfo *MRI) const {
1393 // For some instructions, it is legal to fold ZERO into the RA register field.
1394 // A zero immediate should always be loaded with a single li.
1395 unsigned DefOpc = DefMI->getOpcode();
1396 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
1397 return false;
1398 if (!DefMI->getOperand(1).isImm())
1399 return false;
1400 if (DefMI->getOperand(1).getImm() != 0)
1401 return false;
1402
1403 // Note that we cannot here invert the arguments of an isel in order to fold
1404 // a ZERO into what is presented as the second argument. All we have here
1405 // is the condition bit, and that might come from a CR-logical bit operation.
1406
1407 const MCInstrDesc &UseMCID = UseMI->getDesc();
1408
1409 // Only fold into real machine instructions.
1410 if (UseMCID.isPseudo())
1411 return false;
1412
1413 unsigned UseIdx;
1414 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
1415 if (UseMI->getOperand(UseIdx).isReg() &&
1416 UseMI->getOperand(UseIdx).getReg() == Reg)
1417 break;
1418
1419 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
1420 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
1421
1422 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
1423
1424 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
1425 // register (which might also be specified as a pointer class kind).
1426 if (UseInfo->isLookupPtrRegClass()) {
1427 if (UseInfo->RegClass /* Kind */ != 1)
1428 return false;
1429 } else {
1430 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
1431 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
1432 return false;
1433 }
1434
1435 // Make sure this is not tied to an output register (or otherwise
1436 // constrained). This is true for ST?UX registers, for example, which
1437 // are tied to their output registers.
1438 if (UseInfo->Constraints != 0)
1439 return false;
1440
1441 unsigned ZeroReg;
1442 if (UseInfo->isLookupPtrRegClass()) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001443 bool isPPC64 = Subtarget.isPPC64();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001444 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
1445 } else {
1446 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
1447 PPC::ZERO8 : PPC::ZERO;
1448 }
1449
1450 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
1451 UseMI->getOperand(UseIdx).setReg(ZeroReg);
1452
1453 if (DeleteDef)
1454 DefMI->eraseFromParent();
1455
1456 return true;
1457}
1458
Hal Finkel30ae2292013-04-10 18:30:16 +00001459static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
1460 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1461 I != IE; ++I)
1462 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
1463 return true;
1464 return false;
1465}
1466
1467// We should make sure that, if we're going to predicate both sides of a
1468// condition (a diamond), that both sides don't define the counter register. We
1469// can predicate counter-decrement-based branches, but while that predicates
1470// the branching, it does not predicate the counter decrement. If we tried to
1471// merge the triangle into one predicated block, we'd decrement the counter
1472// twice.
1473bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
1474 unsigned NumT, unsigned ExtraT,
1475 MachineBasicBlock &FMBB,
1476 unsigned NumF, unsigned ExtraF,
1477 const BranchProbability &Probability) const {
1478 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
1479}
1480
1481
Hal Finkel5711eca2013-04-09 22:58:37 +00001482bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const {
Hal Finkelf29285a2013-04-11 01:23:34 +00001483 // The predicated branches are identified by their type, not really by the
1484 // explicit presence of a predicate. Furthermore, some of them can be
1485 // predicated more than once. Because if conversion won't try to predicate
1486 // any instruction which already claims to be predicated (by returning true
1487 // here), always return false. In doing so, we let isPredicable() be the
1488 // final word on whether not the instruction can be (further) predicated.
1489
1490 return false;
Hal Finkel5711eca2013-04-09 22:58:37 +00001491}
1492
1493bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
1494 if (!MI->isTerminator())
1495 return false;
1496
1497 // Conditional branch is a special case.
1498 if (MI->isBranch() && !MI->isBarrier())
1499 return true;
1500
1501 return !isPredicated(MI);
1502}
1503
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001504bool PPCInstrInfo::PredicateInstruction(MachineInstr *MI,
1505 ArrayRef<MachineOperand> Pred) const {
Hal Finkel5711eca2013-04-09 22:58:37 +00001506 unsigned OpC = MI->getOpcode();
Hal Finkelf4a22c02015-01-13 17:47:54 +00001507 if (OpC == PPC::BLR || OpC == PPC::BLR8) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001508 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001509 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel5711eca2013-04-09 22:58:37 +00001510 MI->setDesc(get(Pred[0].getImm() ?
1511 (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) :
1512 (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
Hal Finkel940ab932014-02-28 00:27:01 +00001513 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001514 MI->setDesc(get(PPC::BCLR));
1515 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel940ab932014-02-28 00:27:01 +00001516 .addReg(Pred[1].getReg());
1517 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1518 MI->setDesc(get(PPC::BCLRn));
1519 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1520 .addReg(Pred[1].getReg());
1521 } else {
1522 MI->setDesc(get(PPC::BCCLR));
1523 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel5711eca2013-04-09 22:58:37 +00001524 .addImm(Pred[0].getImm())
1525 .addReg(Pred[1].getReg());
1526 }
1527
1528 return true;
1529 } else if (OpC == PPC::B) {
1530 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001531 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel5711eca2013-04-09 22:58:37 +00001532 MI->setDesc(get(Pred[0].getImm() ?
1533 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1534 (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
Hal Finkel940ab932014-02-28 00:27:01 +00001535 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1536 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1537 MI->RemoveOperand(0);
1538
1539 MI->setDesc(get(PPC::BC));
1540 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1541 .addReg(Pred[1].getReg())
1542 .addMBB(MBB);
1543 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1544 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1545 MI->RemoveOperand(0);
1546
1547 MI->setDesc(get(PPC::BCn));
1548 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1549 .addReg(Pred[1].getReg())
1550 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001551 } else {
1552 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1553 MI->RemoveOperand(0);
1554
1555 MI->setDesc(get(PPC::BCC));
1556 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1557 .addImm(Pred[0].getImm())
1558 .addReg(Pred[1].getReg())
1559 .addMBB(MBB);
1560 }
1561
1562 return true;
Hal Finkel500b0042013-04-10 06:42:34 +00001563 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1564 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1565 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1566 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1567
1568 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
Eric Christopher1dcea732014-06-12 21:48:52 +00001569 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel940ab932014-02-28 00:27:01 +00001570
1571 if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1572 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) :
1573 (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1574 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1575 .addReg(Pred[1].getReg());
1576 return true;
1577 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1578 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) :
1579 (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1580 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1581 .addReg(Pred[1].getReg());
1582 return true;
1583 }
1584
1585 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) :
1586 (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
Hal Finkel500b0042013-04-10 06:42:34 +00001587 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1588 .addImm(Pred[0].getImm())
1589 .addReg(Pred[1].getReg());
1590 return true;
Hal Finkel5711eca2013-04-09 22:58:37 +00001591 }
1592
1593 return false;
1594}
1595
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001596bool PPCInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1597 ArrayRef<MachineOperand> Pred2) const {
Hal Finkel5711eca2013-04-09 22:58:37 +00001598 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1599 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1600
1601 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1602 return false;
1603 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1604 return false;
1605
Hal Finkel94a6f382013-12-11 23:12:25 +00001606 // P1 can only subsume P2 if they test the same condition register.
1607 if (Pred1[1].getReg() != Pred2[1].getReg())
1608 return false;
1609
Hal Finkel5711eca2013-04-09 22:58:37 +00001610 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1611 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1612
1613 if (P1 == P2)
1614 return true;
1615
1616 // Does P1 subsume P2, e.g. GE subsumes GT.
1617 if (P1 == PPC::PRED_LE &&
1618 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1619 return true;
1620 if (P1 == PPC::PRED_GE &&
1621 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1622 return true;
1623
1624 return false;
1625}
1626
1627bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI,
1628 std::vector<MachineOperand> &Pred) const {
1629 // Note: At the present time, the contents of Pred from this function is
1630 // unused by IfConversion. This implementation follows ARM by pushing the
1631 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1632 // predicate, instructions defining CTR or CTR8 are also included as
1633 // predicate-defining instructions.
1634
1635 const TargetRegisterClass *RCs[] =
1636 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1637 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1638
1639 bool Found = false;
1640 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1641 const MachineOperand &MO = MI->getOperand(i);
Hal Finkelaf822012013-04-10 07:17:47 +00001642 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001643 const TargetRegisterClass *RC = RCs[c];
Hal Finkelaf822012013-04-10 07:17:47 +00001644 if (MO.isReg()) {
1645 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001646 Pred.push_back(MO);
1647 Found = true;
1648 }
Hal Finkelaf822012013-04-10 07:17:47 +00001649 } else if (MO.isRegMask()) {
1650 for (TargetRegisterClass::iterator I = RC->begin(),
1651 IE = RC->end(); I != IE; ++I)
1652 if (MO.clobbersPhysReg(*I)) {
1653 Pred.push_back(MO);
1654 Found = true;
1655 }
Hal Finkel5711eca2013-04-09 22:58:37 +00001656 }
1657 }
1658 }
1659
1660 return Found;
1661}
1662
1663bool PPCInstrInfo::isPredicable(MachineInstr *MI) const {
1664 unsigned OpC = MI->getOpcode();
1665 switch (OpC) {
1666 default:
1667 return false;
1668 case PPC::B:
1669 case PPC::BLR:
Hal Finkelf4a22c02015-01-13 17:47:54 +00001670 case PPC::BLR8:
Hal Finkel500b0042013-04-10 06:42:34 +00001671 case PPC::BCTR:
1672 case PPC::BCTR8:
1673 case PPC::BCTRL:
1674 case PPC::BCTRL8:
Hal Finkel5711eca2013-04-09 22:58:37 +00001675 return true;
1676 }
1677}
1678
Hal Finkel82656cb2013-04-18 22:15:08 +00001679bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI,
1680 unsigned &SrcReg, unsigned &SrcReg2,
1681 int &Mask, int &Value) const {
1682 unsigned Opc = MI->getOpcode();
1683
1684 switch (Opc) {
1685 default: return false;
1686 case PPC::CMPWI:
1687 case PPC::CMPLWI:
1688 case PPC::CMPDI:
1689 case PPC::CMPLDI:
1690 SrcReg = MI->getOperand(1).getReg();
1691 SrcReg2 = 0;
1692 Value = MI->getOperand(2).getImm();
1693 Mask = 0xFFFF;
1694 return true;
1695 case PPC::CMPW:
1696 case PPC::CMPLW:
1697 case PPC::CMPD:
1698 case PPC::CMPLD:
1699 case PPC::FCMPUS:
1700 case PPC::FCMPUD:
1701 SrcReg = MI->getOperand(1).getReg();
1702 SrcReg2 = MI->getOperand(2).getReg();
1703 return true;
1704 }
1705}
Hal Finkele6322392013-04-19 22:08:38 +00001706
Hal Finkel82656cb2013-04-18 22:15:08 +00001707bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr,
1708 unsigned SrcReg, unsigned SrcReg2,
1709 int Mask, int Value,
1710 const MachineRegisterInfo *MRI) const {
Hal Finkelb12da6b2013-04-18 22:54:25 +00001711 if (DisableCmpOpt)
1712 return false;
1713
Hal Finkel82656cb2013-04-18 22:15:08 +00001714 int OpC = CmpInstr->getOpcode();
1715 unsigned CRReg = CmpInstr->getOperand(0).getReg();
Hal Finkel08e53ee2013-05-08 12:16:14 +00001716
1717 // FP record forms set CR1 based on the execption status bits, not a
1718 // comparison with zero.
1719 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1720 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001721
1722 // The record forms set the condition register based on a signed comparison
1723 // with zero (so says the ISA manual). This is not as straightforward as it
1724 // seems, however, because this is always a 64-bit comparison on PPC64, even
1725 // for instructions that are 32-bit in nature (like slw for example).
1726 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1727 // for equality checks (as those don't depend on the sign). On PPC64,
1728 // we are restricted to equality for unsigned 64-bit comparisons and for
1729 // signed 32-bit comparisons the applicability is more restricted.
Eric Christopher1dcea732014-06-12 21:48:52 +00001730 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel82656cb2013-04-18 22:15:08 +00001731 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1732 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1733 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1734
1735 // Get the unique definition of SrcReg.
1736 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1737 if (!MI) return false;
1738 int MIOpC = MI->getOpcode();
1739
1740 bool equalityOnly = false;
1741 bool noSub = false;
1742 if (isPPC64) {
1743 if (is32BitSignedCompare) {
1744 // We can perform this optimization only if MI is sign-extending.
1745 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1746 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1747 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1748 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1749 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1750 noSub = true;
1751 } else
1752 return false;
1753 } else if (is32BitUnsignedCompare) {
1754 // We can perform this optimization, equality only, if MI is
1755 // zero-extending.
1756 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1757 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
1758 MIOpC == PPC::SRW || MIOpC == PPC::SRWo) {
1759 noSub = true;
1760 equalityOnly = true;
1761 } else
1762 return false;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001763 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001764 equalityOnly = is64BitUnsignedCompare;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001765 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001766 equalityOnly = is32BitUnsignedCompare;
1767
1768 if (equalityOnly) {
1769 // We need to check the uses of the condition register in order to reject
1770 // non-equality comparisons.
Owen Anderson16c6bf42014-03-13 23:12:04 +00001771 for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg),
1772 IE = MRI->use_instr_end(); I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001773 MachineInstr *UseMI = &*I;
1774 if (UseMI->getOpcode() == PPC::BCC) {
1775 unsigned Pred = UseMI->getOperand(0).getImm();
Hal Finkelc3632452013-05-07 17:49:55 +00001776 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE)
1777 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001778 } else if (UseMI->getOpcode() == PPC::ISEL ||
1779 UseMI->getOpcode() == PPC::ISEL8) {
1780 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkelc3632452013-05-07 17:49:55 +00001781 if (SubIdx != PPC::sub_eq)
1782 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001783 } else
1784 return false;
1785 }
1786 }
1787
Hal Finkelc3632452013-05-07 17:49:55 +00001788 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001789
1790 // Scan forward to find the first use of the compare.
1791 for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end();
1792 I != EL; ++I) {
1793 bool FoundUse = false;
Owen Anderson16c6bf42014-03-13 23:12:04 +00001794 for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg),
1795 JE = MRI->use_instr_end(); J != JE; ++J)
Hal Finkel82656cb2013-04-18 22:15:08 +00001796 if (&*J == &*I) {
1797 FoundUse = true;
1798 break;
1799 }
1800
1801 if (FoundUse)
1802 break;
1803 }
1804
Hal Finkel82656cb2013-04-18 22:15:08 +00001805 // There are two possible candidates which can be changed to set CR[01].
1806 // One is MI, the other is a SUB instruction.
1807 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
Craig Topper062a2ba2014-04-25 05:30:21 +00001808 MachineInstr *Sub = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001809 if (SrcReg2 != 0)
1810 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00001811 MI = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001812 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1813 // same BB as the comparison. This is to allow the check below to avoid calls
1814 // (and other explicit clobbers); instead we should really check for these
1815 // more explicitly (in at least a few predecessors).
1816 else if (MI->getParent() != CmpInstr->getParent() || Value != 0) {
1817 // PPC does not have a record-form SUBri.
1818 return false;
1819 }
1820
1821 // Search for Sub.
1822 const TargetRegisterInfo *TRI = &getRegisterInfo();
1823 --I;
Hal Finkelc3632452013-05-07 17:49:55 +00001824
1825 // Get ready to iterate backward from CmpInstr.
1826 MachineBasicBlock::iterator E = MI,
1827 B = CmpInstr->getParent()->begin();
1828
Hal Finkel82656cb2013-04-18 22:15:08 +00001829 for (; I != E && !noSub; --I) {
1830 const MachineInstr &Instr = *I;
1831 unsigned IOpC = Instr.getOpcode();
1832
1833 if (&*I != CmpInstr && (
Hal Finkel08e53ee2013-05-08 12:16:14 +00001834 Instr.modifiesRegister(PPC::CR0, TRI) ||
1835 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel82656cb2013-04-18 22:15:08 +00001836 // This instruction modifies or uses the record condition register after
1837 // the one we want to change. While we could do this transformation, it
1838 // would likely not be profitable. This transformation removes one
1839 // instruction, and so even forcing RA to generate one move probably
1840 // makes it unprofitable.
1841 return false;
1842
1843 // Check whether CmpInstr can be made redundant by the current instruction.
1844 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1845 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1846 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1847 ((Instr.getOperand(1).getReg() == SrcReg &&
1848 Instr.getOperand(2).getReg() == SrcReg2) ||
1849 (Instr.getOperand(1).getReg() == SrcReg2 &&
1850 Instr.getOperand(2).getReg() == SrcReg))) {
1851 Sub = &*I;
1852 break;
1853 }
1854
Hal Finkel82656cb2013-04-18 22:15:08 +00001855 if (I == B)
1856 // The 'and' is below the comparison instruction.
1857 return false;
1858 }
1859
1860 // Return false if no candidates exist.
1861 if (!MI && !Sub)
1862 return false;
1863
1864 // The single candidate is called MI.
1865 if (!MI) MI = Sub;
1866
1867 int NewOpC = -1;
1868 MIOpC = MI->getOpcode();
1869 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1870 NewOpC = MIOpC;
1871 else {
1872 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1873 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1874 NewOpC = MIOpC;
1875 }
1876
1877 // FIXME: On the non-embedded POWER architectures, only some of the record
1878 // forms are fast, and we should use only the fast ones.
1879
1880 // The defining instruction has a record form (or is already a record
1881 // form). It is possible, however, that we'll need to reverse the condition
1882 // code of the users.
1883 if (NewOpC == -1)
1884 return false;
1885
Hal Finkele6322392013-04-19 22:08:38 +00001886 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1887 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
Hal Finkel82656cb2013-04-18 22:15:08 +00001888
1889 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1890 // needs to be updated to be based on SUB. Push the condition code
1891 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1892 // condition code of these operands will be modified.
1893 bool ShouldSwap = false;
1894 if (Sub) {
1895 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1896 Sub->getOperand(2).getReg() == SrcReg;
1897
1898 // The operands to subf are the opposite of sub, so only in the fixed-point
1899 // case, invert the order.
Hal Finkel08e53ee2013-05-08 12:16:14 +00001900 ShouldSwap = !ShouldSwap;
Hal Finkel82656cb2013-04-18 22:15:08 +00001901 }
1902
1903 if (ShouldSwap)
Owen Anderson16c6bf42014-03-13 23:12:04 +00001904 for (MachineRegisterInfo::use_instr_iterator
1905 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1906 I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001907 MachineInstr *UseMI = &*I;
1908 if (UseMI->getOpcode() == PPC::BCC) {
1909 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hal Finkele6322392013-04-19 22:08:38 +00001910 assert((!equalityOnly ||
1911 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1912 "Invalid predicate for equality-only optimization");
Owen Anderson16c6bf42014-03-13 23:12:04 +00001913 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
Hal Finkel0f64e212013-04-20 05:16:26 +00001914 PPC::getSwappedPredicate(Pred)));
Hal Finkel82656cb2013-04-18 22:15:08 +00001915 } else if (UseMI->getOpcode() == PPC::ISEL ||
1916 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkele6322392013-04-19 22:08:38 +00001917 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1918 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1919 "Invalid CR bit for equality-only optimization");
1920
1921 if (NewSubReg == PPC::sub_lt)
1922 NewSubReg = PPC::sub_gt;
1923 else if (NewSubReg == PPC::sub_gt)
1924 NewSubReg = PPC::sub_lt;
1925
Owen Anderson16c6bf42014-03-13 23:12:04 +00001926 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
Hal Finkele6322392013-04-19 22:08:38 +00001927 NewSubReg));
Hal Finkel82656cb2013-04-18 22:15:08 +00001928 } else // We need to abort on a user we don't understand.
1929 return false;
1930 }
1931
1932 // Create a new virtual register to hold the value of the CR set by the
1933 // record-form instruction. If the instruction was not previously in
1934 // record form, then set the kill flag on the CR.
1935 CmpInstr->eraseFromParent();
1936
1937 MachineBasicBlock::iterator MII = MI;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001938 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
Hal Finkel82656cb2013-04-18 22:15:08 +00001939 get(TargetOpcode::COPY), CRReg)
Hal Finkel08e53ee2013-05-08 12:16:14 +00001940 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel82656cb2013-04-18 22:15:08 +00001941
1942 if (MIOpC != NewOpC) {
1943 // We need to be careful here: we're replacing one instruction with
1944 // another, and we need to make sure that we get all of the right
1945 // implicit uses and defs. On the other hand, the caller may be holding
1946 // an iterator to this instruction, and so we can't delete it (this is
1947 // specifically the case if this is the instruction directly after the
1948 // compare).
1949
1950 const MCInstrDesc &NewDesc = get(NewOpC);
1951 MI->setDesc(NewDesc);
1952
1953 if (NewDesc.ImplicitDefs)
1954 for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs();
1955 *ImpDefs; ++ImpDefs)
1956 if (!MI->definesRegister(*ImpDefs))
1957 MI->addOperand(*MI->getParent()->getParent(),
1958 MachineOperand::CreateReg(*ImpDefs, true, true));
1959 if (NewDesc.ImplicitUses)
1960 for (const uint16_t *ImpUses = NewDesc.getImplicitUses();
1961 *ImpUses; ++ImpUses)
1962 if (!MI->readsRegister(*ImpUses))
1963 MI->addOperand(*MI->getParent()->getParent(),
1964 MachineOperand::CreateReg(*ImpUses, false, true));
1965 }
1966
1967 // Modify the condition code of operands in OperandsToUpdate.
1968 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1969 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkele6322392013-04-19 22:08:38 +00001970 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1971 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001972
Hal Finkele6322392013-04-19 22:08:38 +00001973 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1974 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001975
1976 return true;
1977}
1978
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001979/// GetInstSize - Return the number of bytes of code the specified
1980/// instruction may be. This returns the maximum number of bytes.
1981///
1982unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Hal Finkela7bbaf62014-02-02 06:12:27 +00001983 unsigned Opcode = MI->getOpcode();
1984
1985 if (Opcode == PPC::INLINEASM) {
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001986 const MachineFunction *MF = MI->getParent()->getParent();
1987 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +00001988 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Hal Finkel934361a2015-01-14 01:07:51 +00001989 } else if (Opcode == TargetOpcode::STACKMAP) {
1990 return MI->getOperand(1).getImm();
1991 } else if (Opcode == TargetOpcode::PATCHPOINT) {
1992 PatchPointOpers Opers(MI);
1993 return Opers.getMetaOper(PatchPointOpers::NBytesPos).getImm();
Hal Finkela7bbaf62014-02-02 06:12:27 +00001994 } else {
1995 const MCInstrDesc &Desc = get(Opcode);
1996 return Desc.getSize();
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001997 }
1998}
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001999
Hal Finkel2d556982015-08-30 07:50:35 +00002000std::pair<unsigned, unsigned>
2001PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
2002 const unsigned Mask = PPCII::MO_ACCESS_MASK;
2003 return std::make_pair(TF & Mask, TF & ~Mask);
2004}
2005
2006ArrayRef<std::pair<unsigned, const char *>>
2007PPCInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
2008 using namespace PPCII;
Hal Finkel982e8d42015-08-30 08:07:29 +00002009 static const std::pair<unsigned, const char *> TargetFlags[] = {
Hal Finkel2d556982015-08-30 07:50:35 +00002010 {MO_LO, "ppc-lo"},
2011 {MO_HA, "ppc-ha"},
2012 {MO_TPREL_LO, "ppc-tprel-lo"},
2013 {MO_TPREL_HA, "ppc-tprel-ha"},
2014 {MO_DTPREL_LO, "ppc-dtprel-lo"},
2015 {MO_TLSLD_LO, "ppc-tlsld-lo"},
2016 {MO_TOC_LO, "ppc-toc-lo"},
2017 {MO_TLS, "ppc-tls"}};
2018 return makeArrayRef(TargetFlags);
2019}
2020
2021ArrayRef<std::pair<unsigned, const char *>>
2022PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
2023 using namespace PPCII;
Hal Finkel982e8d42015-08-30 08:07:29 +00002024 static const std::pair<unsigned, const char *> TargetFlags[] = {
Hal Finkel2d556982015-08-30 07:50:35 +00002025 {MO_PLT_OR_STUB, "ppc-plt-or-stub"},
2026 {MO_PIC_FLAG, "ppc-pic"},
2027 {MO_NLP_FLAG, "ppc-nlp"},
2028 {MO_NLP_HIDDEN_FLAG, "ppc-nlp-hidden"}};
2029 return makeArrayRef(TargetFlags);
2030}
2031