blob: 76f97b1ceaf01d0adc241e62b8a5e586a9e84a49 [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 +0000192// This function does not list all associative and commutative operations, but
193// only those worth feeding through the machine combiner in an attempt to
194// reduce the critical path. Mostly, this means floating-point operations,
195// because they have high latencies (compared to other operations, such and
196// and/or, which are also associative and commutative, but have low latencies).
Chad Rosier03a47302015-09-21 15:09:11 +0000197bool PPCInstrInfo::isAssociativeAndCommutative(const MachineInstr &Inst) const {
198 switch (Inst.getOpcode()) {
Hal Finkel5d36b232015-07-15 08:23:05 +0000199 // FP Add:
200 case PPC::FADD:
201 case PPC::FADDS:
202 // FP Multiply:
203 case PPC::FMUL:
204 case PPC::FMULS:
205 // Altivec Add:
206 case PPC::VADDFP:
207 // VSX Add:
208 case PPC::XSADDDP:
209 case PPC::XVADDDP:
210 case PPC::XVADDSP:
211 case PPC::XSADDSP:
212 // VSX Multiply:
213 case PPC::XSMULDP:
214 case PPC::XVMULDP:
215 case PPC::XVMULSP:
216 case PPC::XSMULSP:
217 // QPX Add:
218 case PPC::QVFADD:
219 case PPC::QVFADDS:
220 case PPC::QVFADDSs:
221 // QPX Multiply:
222 case PPC::QVFMUL:
223 case PPC::QVFMULS:
224 case PPC::QVFMULSs:
225 return true;
226 default:
227 return false;
228 }
229}
230
Chad Rosier03a47302015-09-21 15:09:11 +0000231bool PPCInstrInfo::getMachineCombinerPatterns(
232 MachineInstr &Root,
Sanjay Patel387e66e2015-11-05 19:34:57 +0000233 SmallVectorImpl<MachineCombinerPattern> &Patterns) const {
Hal Finkel5d36b232015-07-15 08:23:05 +0000234 // Using the machine combiner in this way is potentially expensive, so
235 // restrict to when aggressive optimizations are desired.
236 if (Subtarget.getTargetMachine().getOptLevel() != CodeGenOpt::Aggressive)
237 return false;
238
239 // FP reassociation is only legal when we don't need strict IEEE semantics.
240 if (!Root.getParent()->getParent()->getTarget().Options.UnsafeFPMath)
241 return false;
242
Chad Rosier03a47302015-09-21 15:09:11 +0000243 return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns);
Hal Finkel5d36b232015-07-15 08:23:05 +0000244}
245
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000246// Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
247bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
248 unsigned &SrcReg, unsigned &DstReg,
249 unsigned &SubIdx) const {
250 switch (MI.getOpcode()) {
251 default: return false;
252 case PPC::EXTSW:
253 case PPC::EXTSW_32_64:
254 SrcReg = MI.getOperand(1).getReg();
255 DstReg = MI.getOperand(0).getReg();
256 SubIdx = PPC::sub_32;
257 return true;
258 }
259}
260
Andrew Trickc416ba62010-12-24 04:28:06 +0000261unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner91400bd2006-03-16 22:24:02 +0000262 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000263 // Note: This list must be kept consistent with LoadRegFromStackSlot.
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000264 switch (MI->getOpcode()) {
265 default: break;
266 case PPC::LD:
267 case PPC::LWZ:
268 case PPC::LFS:
269 case PPC::LFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000270 case PPC::RESTORE_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000271 case PPC::RESTORE_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000272 case PPC::LVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000273 case PPC::LXVD2X:
Hal Finkelc93a9a22015-02-25 01:06:45 +0000274 case PPC::QVLFDX:
275 case PPC::QVLFSXs:
276 case PPC::QVLFDXb:
Hal Finkel37714b82013-03-27 21:21:15 +0000277 case PPC::RESTORE_VRSAVE:
278 // Check for the operands added by addFrameReference (the immediate is the
279 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000280 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
281 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000282 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000283 return MI->getOperand(0).getReg();
284 }
285 break;
286 }
287 return 0;
Chris Lattnerc327d712006-02-02 20:16:12 +0000288}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000289
Andrew Trickc416ba62010-12-24 04:28:06 +0000290unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattnerc327d712006-02-02 20:16:12 +0000291 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000292 // Note: This list must be kept consistent with StoreRegToStackSlot.
Chris Lattnerc327d712006-02-02 20:16:12 +0000293 switch (MI->getOpcode()) {
294 default: break;
Nate Begeman4efb3282006-02-02 21:07:50 +0000295 case PPC::STD:
Chris Lattnerc327d712006-02-02 20:16:12 +0000296 case PPC::STW:
297 case PPC::STFS:
298 case PPC::STFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000299 case PPC::SPILL_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000300 case PPC::SPILL_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000301 case PPC::STVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000302 case PPC::STXVD2X:
Hal Finkelc93a9a22015-02-25 01:06:45 +0000303 case PPC::QVSTFDX:
304 case PPC::QVSTFSXs:
305 case PPC::QVSTFDXb:
Hal Finkel37714b82013-03-27 21:21:15 +0000306 case PPC::SPILL_VRSAVE:
307 // Check for the operands added by addFrameReference (the immediate is the
308 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000309 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
310 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000311 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerc327d712006-02-02 20:16:12 +0000312 return MI->getOperand(0).getReg();
313 }
314 break;
315 }
316 return 0;
317}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000318
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000319MachineInstr *PPCInstrInfo::commuteInstructionImpl(MachineInstr *MI,
320 bool NewMI,
321 unsigned OpIdx1,
322 unsigned OpIdx2) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000323 MachineFunction &MF = *MI->getParent()->getParent();
324
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000325 // Normal instructions can be commuted the obvious way.
Hal Finkel654d43b2013-04-12 02:18:09 +0000326 if (MI->getOpcode() != PPC::RLWIMI &&
Hal Finkel4c6658f2014-12-12 23:59:36 +0000327 MI->getOpcode() != PPC::RLWIMIo)
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000328 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
Hal Finkel4c6658f2014-12-12 23:59:36 +0000329 // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a
330 // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because
331 // changing the relative order of the mask operands might change what happens
332 // to the high-bits of the mask (and, thus, the result).
Andrew Trickc416ba62010-12-24 04:28:06 +0000333
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000334 // Cannot commute if it has a non-zero rotate count.
Chris Lattner5c463782007-12-30 20:49:49 +0000335 if (MI->getOperand(3).getImm() != 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000336 return nullptr;
Andrew Trickc416ba62010-12-24 04:28:06 +0000337
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000338 // If we have a zero rotate count, we have:
339 // M = mask(MB,ME)
340 // Op0 = (Op1 & ~M) | (Op2 & M)
341 // Change this to:
342 // M = mask((ME+1)&31, (MB-1)&31)
343 // Op0 = (Op2 & ~M) | (Op1 & M)
344
345 // Swap op1/op2
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000346 assert(((OpIdx1 == 1 && OpIdx2 == 2) || (OpIdx1 == 2 && OpIdx2 == 1)) &&
347 "Only the operands 1 and 2 can be swapped in RLSIMI/RLWIMIo.");
Evan Cheng244183e2008-02-13 02:46:49 +0000348 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000349 unsigned Reg1 = MI->getOperand(1).getReg();
350 unsigned Reg2 = MI->getOperand(2).getReg();
Andrew Tricke3398282013-12-17 04:50:45 +0000351 unsigned SubReg1 = MI->getOperand(1).getSubReg();
352 unsigned SubReg2 = MI->getOperand(2).getSubReg();
Evan Chengdc2c8742006-11-15 20:58:11 +0000353 bool Reg1IsKill = MI->getOperand(1).isKill();
354 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng03553bb2008-06-16 07:33:11 +0000355 bool ChangeReg0 = false;
Evan Cheng244183e2008-02-13 02:46:49 +0000356 // If machine instrs are no longer in two-address forms, update
357 // destination register as well.
358 if (Reg0 == Reg1) {
359 // Must be two address instruction!
Evan Cheng6cc775f2011-06-28 19:10:37 +0000360 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Cheng244183e2008-02-13 02:46:49 +0000361 "Expecting a two-address instruction!");
Andrew Tricke3398282013-12-17 04:50:45 +0000362 assert(MI->getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
Evan Cheng244183e2008-02-13 02:46:49 +0000363 Reg2IsKill = false;
Evan Cheng03553bb2008-06-16 07:33:11 +0000364 ChangeReg0 = true;
Evan Cheng244183e2008-02-13 02:46:49 +0000365 }
Evan Cheng03553bb2008-06-16 07:33:11 +0000366
367 // Masks.
368 unsigned MB = MI->getOperand(4).getImm();
369 unsigned ME = MI->getOperand(5).getImm();
370
Hal Finkelccf92592015-09-06 04:17:30 +0000371 // We can't commute a trivial mask (there is no way to represent an all-zero
372 // mask).
373 if (MB == 0 && ME == 31)
374 return nullptr;
375
Evan Cheng03553bb2008-06-16 07:33:11 +0000376 if (NewMI) {
377 // Create a new instruction.
378 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
379 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000380 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000381 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
382 .addReg(Reg2, getKillRegState(Reg2IsKill))
383 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng03553bb2008-06-16 07:33:11 +0000384 .addImm((ME+1) & 31)
385 .addImm((MB-1) & 31);
386 }
387
Andrew Tricke3398282013-12-17 04:50:45 +0000388 if (ChangeReg0) {
Evan Cheng03553bb2008-06-16 07:33:11 +0000389 MI->getOperand(0).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000390 MI->getOperand(0).setSubReg(SubReg2);
391 }
Chris Lattner10d63412006-05-04 17:52:23 +0000392 MI->getOperand(2).setReg(Reg1);
393 MI->getOperand(1).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000394 MI->getOperand(2).setSubReg(SubReg1);
395 MI->getOperand(1).setSubReg(SubReg2);
Chris Lattner60055892007-12-30 21:56:09 +0000396 MI->getOperand(2).setIsKill(Reg1IsKill);
397 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000398
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000399 // Swap the mask around.
Chris Lattner5c463782007-12-30 20:49:49 +0000400 MI->getOperand(4).setImm((ME+1) & 31);
401 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000402 return MI;
403}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000404
Hal Finkel6c32ff32014-03-25 19:26:43 +0000405bool PPCInstrInfo::findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
406 unsigned &SrcOpIdx2) const {
407 // For VSX A-Type FMA instructions, it is the first two operands that can be
408 // commuted, however, because the non-encoded tied input operand is listed
409 // first, the operands to swap are actually the second and third.
410
411 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
412 if (AltOpc == -1)
413 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
414
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000415 // The commutable operand indices are 2 and 3. Return them in SrcOpIdx1
416 // and SrcOpIdx2.
417 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2, 3);
Hal Finkel6c32ff32014-03-25 19:26:43 +0000418}
419
Andrew Trickc416ba62010-12-24 04:28:06 +0000420void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000421 MachineBasicBlock::iterator MI) const {
Hal Finkelceb1f122013-12-12 00:19:11 +0000422 // This function is used for scheduling, and the nop wanted here is the type
423 // that terminates dispatch groups on the POWER cores.
Eric Christopher1dcea732014-06-12 21:48:52 +0000424 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000425 unsigned Opcode;
426 switch (Directive) {
427 default: Opcode = PPC::NOP; break;
428 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
429 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
Will Schmidt970ff642014-06-26 13:36:19 +0000430 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 +0000431 }
Chris Lattnera47294ed2006-10-13 21:21:17 +0000432
Hal Finkelceb1f122013-12-12 00:19:11 +0000433 DebugLoc DL;
434 BuildMI(MBB, MI, DL, get(Opcode));
435}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000436
Joerg Sonnenberger7ee0f312014-08-08 19:13:23 +0000437/// getNoopForMachoTarget - Return the noop instruction to use for a noop.
438void PPCInstrInfo::getNoopForMachoTarget(MCInst &NopInst) const {
439 NopInst.setOpcode(PPC::NOP);
440}
441
Chris Lattnera47294ed2006-10-13 21:21:17 +0000442// Branch analysis.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000443// Note: If the condition register is set to CTR or CTR8 then this is a
444// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Chris Lattnera47294ed2006-10-13 21:21:17 +0000445bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
446 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000447 SmallVectorImpl<MachineOperand> &Cond,
448 bool AllowModify) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000449 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000450
Chris Lattnera47294ed2006-10-13 21:21:17 +0000451 // If the block has no terminators, it just falls into the block after it.
Benjamin Kramer92861d72015-06-25 13:39:03 +0000452 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
453 if (I == MBB.end())
Dale Johannesen4244d122010-04-02 01:38:09 +0000454 return false;
Benjamin Kramer92861d72015-06-25 13:39:03 +0000455
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000456 if (!isUnpredicatedTerminator(*I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000457 return false;
458
459 // Get the last instruction in the block.
460 MachineInstr *LastInst = I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000461
Chris Lattnera47294ed2006-10-13 21:21:17 +0000462 // If there is only one terminator instruction, process it.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000463 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000464 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000465 if (!LastInst->getOperand(0).isMBB())
466 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000467 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000468 return false;
Chris Lattnere0263792006-11-17 22:14:47 +0000469 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000470 if (!LastInst->getOperand(2).isMBB())
471 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000472 // Block ends with fall-through condbranch.
Chris Lattnera5bb3702007-12-30 23:10:15 +0000473 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000474 Cond.push_back(LastInst->getOperand(0));
475 Cond.push_back(LastInst->getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000476 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000477 } else if (LastInst->getOpcode() == PPC::BC) {
478 if (!LastInst->getOperand(1).isMBB())
479 return true;
480 // Block ends with fall-through condbranch.
481 TBB = LastInst->getOperand(1).getMBB();
482 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
483 Cond.push_back(LastInst->getOperand(0));
484 return false;
485 } else if (LastInst->getOpcode() == PPC::BCn) {
486 if (!LastInst->getOperand(1).isMBB())
487 return true;
488 // Block ends with fall-through condbranch.
489 TBB = LastInst->getOperand(1).getMBB();
490 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
491 Cond.push_back(LastInst->getOperand(0));
492 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000493 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
494 LastInst->getOpcode() == PPC::BDNZ) {
495 if (!LastInst->getOperand(0).isMBB())
496 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000497 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000498 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000499 TBB = LastInst->getOperand(0).getMBB();
500 Cond.push_back(MachineOperand::CreateImm(1));
501 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
502 true));
503 return false;
504 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
505 LastInst->getOpcode() == PPC::BDZ) {
506 if (!LastInst->getOperand(0).isMBB())
507 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000508 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000509 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000510 TBB = LastInst->getOperand(0).getMBB();
511 Cond.push_back(MachineOperand::CreateImm(0));
512 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
513 true));
514 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000515 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000516
Chris Lattnera47294ed2006-10-13 21:21:17 +0000517 // Otherwise, don't know what this is.
518 return true;
519 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000520
Chris Lattnera47294ed2006-10-13 21:21:17 +0000521 // Get the instruction before it if it's a terminator.
522 MachineInstr *SecondLastInst = I;
523
524 // If there are three terminators, we don't know what sort of block this is.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000525 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(*--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000526 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000527
Chris Lattnere0263792006-11-17 22:14:47 +0000528 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000529 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnera47294ed2006-10-13 21:21:17 +0000530 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000531 if (!SecondLastInst->getOperand(2).isMBB() ||
532 !LastInst->getOperand(0).isMBB())
533 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000534 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000535 Cond.push_back(SecondLastInst->getOperand(0));
536 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattnera5bb3702007-12-30 23:10:15 +0000537 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000538 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000539 } else if (SecondLastInst->getOpcode() == PPC::BC &&
540 LastInst->getOpcode() == PPC::B) {
541 if (!SecondLastInst->getOperand(1).isMBB() ||
542 !LastInst->getOperand(0).isMBB())
543 return true;
544 TBB = SecondLastInst->getOperand(1).getMBB();
545 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
546 Cond.push_back(SecondLastInst->getOperand(0));
547 FBB = LastInst->getOperand(0).getMBB();
548 return false;
549 } else if (SecondLastInst->getOpcode() == PPC::BCn &&
550 LastInst->getOpcode() == PPC::B) {
551 if (!SecondLastInst->getOperand(1).isMBB() ||
552 !LastInst->getOperand(0).isMBB())
553 return true;
554 TBB = SecondLastInst->getOperand(1).getMBB();
555 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
556 Cond.push_back(SecondLastInst->getOperand(0));
557 FBB = LastInst->getOperand(0).getMBB();
558 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000559 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
560 SecondLastInst->getOpcode() == PPC::BDNZ) &&
561 LastInst->getOpcode() == PPC::B) {
562 if (!SecondLastInst->getOperand(0).isMBB() ||
563 !LastInst->getOperand(0).isMBB())
564 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000565 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000566 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000567 TBB = SecondLastInst->getOperand(0).getMBB();
568 Cond.push_back(MachineOperand::CreateImm(1));
569 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
570 true));
571 FBB = LastInst->getOperand(0).getMBB();
572 return false;
573 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
574 SecondLastInst->getOpcode() == PPC::BDZ) &&
575 LastInst->getOpcode() == PPC::B) {
576 if (!SecondLastInst->getOperand(0).isMBB() ||
577 !LastInst->getOperand(0).isMBB())
578 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000579 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000580 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000581 TBB = SecondLastInst->getOperand(0).getMBB();
582 Cond.push_back(MachineOperand::CreateImm(0));
583 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
584 true));
585 FBB = LastInst->getOperand(0).getMBB();
586 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000587 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000588
Dale Johannesenc6855462007-06-13 17:59:52 +0000589 // If the block ends with two PPC:Bs, handle it. The second one is not
590 // executed, so remove it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000591 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesenc6855462007-06-13 17:59:52 +0000592 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000593 if (!SecondLastInst->getOperand(0).isMBB())
594 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000595 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000596 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000597 if (AllowModify)
598 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000599 return false;
600 }
601
Chris Lattnera47294ed2006-10-13 21:21:17 +0000602 // Otherwise, can't handle this.
603 return true;
604}
605
Evan Cheng99be49d2007-05-18 00:05:48 +0000606unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Benjamin Kramer92861d72015-06-25 13:39:03 +0000607 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
608 if (I == MBB.end())
609 return 0;
610
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000611 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000612 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000613 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
614 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000615 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000616
Chris Lattnera47294ed2006-10-13 21:21:17 +0000617 // Remove the branch.
618 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000619
Chris Lattnera47294ed2006-10-13 21:21:17 +0000620 I = MBB.end();
621
Evan Cheng99be49d2007-05-18 00:05:48 +0000622 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000623 --I;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000624 if (I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000625 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000626 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
627 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000628 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000629
Chris Lattnera47294ed2006-10-13 21:21:17 +0000630 // Remove the branch.
631 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000632 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000633}
634
Evan Cheng99be49d2007-05-18 00:05:48 +0000635unsigned
636PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
637 MachineBasicBlock *FBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000638 ArrayRef<MachineOperand> Cond,
Stuart Hastings0125b642010-06-17 22:43:56 +0000639 DebugLoc DL) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000640 // Shouldn't be a fall through.
641 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000642 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000643 "PPC branch conditions have two components!");
Andrew Trickc416ba62010-12-24 04:28:06 +0000644
Eric Christopher1dcea732014-06-12 21:48:52 +0000645 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000646
Chris Lattner94e04442006-10-21 05:36:13 +0000647 // One-way branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000648 if (!FBB) {
Chris Lattner94e04442006-10-21 05:36:13 +0000649 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000650 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000651 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
652 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
653 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
654 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000655 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
656 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
657 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
658 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000659 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000660 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000661 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000662 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000663 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000664
Chris Lattnerd8816602006-10-21 05:42:09 +0000665 // Two-way Conditional Branch.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000666 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
667 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
668 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
669 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000670 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
671 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
672 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
673 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000674 else
675 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000676 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000677 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000678 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000679}
680
Hal Finkeled6a2852013-04-05 23:29:01 +0000681// Select analysis.
682bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000683 ArrayRef<MachineOperand> Cond,
Hal Finkeled6a2852013-04-05 23:29:01 +0000684 unsigned TrueReg, unsigned FalseReg,
685 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000686 if (!Subtarget.hasISEL())
Hal Finkeled6a2852013-04-05 23:29:01 +0000687 return false;
688
689 if (Cond.size() != 2)
690 return false;
691
692 // If this is really a bdnz-like condition, then it cannot be turned into a
693 // select.
694 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
695 return false;
696
697 // Check register classes.
698 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
699 const TargetRegisterClass *RC =
700 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
701 if (!RC)
702 return false;
703
704 // isel is for regular integer GPRs only.
705 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkel8e8618a2013-07-15 20:22:58 +0000706 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
707 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
708 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkeled6a2852013-04-05 23:29:01 +0000709 return false;
710
711 // FIXME: These numbers are for the A2, how well they work for other cores is
712 // an open question. On the A2, the isel instruction has a 2-cycle latency
713 // but single-cycle throughput. These numbers are used in combination with
714 // the MispredictPenalty setting from the active SchedMachineModel.
715 CondCycles = 1;
716 TrueCycles = 1;
717 FalseCycles = 1;
718
719 return true;
720}
721
722void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
723 MachineBasicBlock::iterator MI, DebugLoc dl,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000724 unsigned DestReg, ArrayRef<MachineOperand> Cond,
Hal Finkeled6a2852013-04-05 23:29:01 +0000725 unsigned TrueReg, unsigned FalseReg) const {
726 assert(Cond.size() == 2 &&
727 "PPC branch conditions have two components!");
728
Eric Christopher1dcea732014-06-12 21:48:52 +0000729 assert(Subtarget.hasISEL() &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000730 "Cannot insert select on target without ISEL support");
731
732 // Get the register classes.
733 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
734 const TargetRegisterClass *RC =
735 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
736 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkel8e8618a2013-07-15 20:22:58 +0000737
738 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
739 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
740 assert((Is64Bit ||
741 PPC::GPRCRegClass.hasSubClassEq(RC) ||
742 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000743 "isel is for regular integer GPRs only");
744
Hal Finkel8e8618a2013-07-15 20:22:58 +0000745 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Kyle Buttcec40802016-01-12 21:00:43 +0000746 auto SelectPred = static_cast<PPC::Predicate>(Cond[0].getImm());
Hal Finkeled6a2852013-04-05 23:29:01 +0000747
Kyle Butt132bf362016-01-15 19:20:06 +0000748 unsigned SubIdx = 0;
749 bool SwapOps = false;
Hal Finkeled6a2852013-04-05 23:29:01 +0000750 switch (SelectPred) {
Kyle Buttcec40802016-01-12 21:00:43 +0000751 case PPC::PRED_EQ:
752 case PPC::PRED_EQ_MINUS:
753 case PPC::PRED_EQ_PLUS:
754 SubIdx = PPC::sub_eq; SwapOps = false; break;
755 case PPC::PRED_NE:
756 case PPC::PRED_NE_MINUS:
757 case PPC::PRED_NE_PLUS:
758 SubIdx = PPC::sub_eq; SwapOps = true; break;
759 case PPC::PRED_LT:
760 case PPC::PRED_LT_MINUS:
761 case PPC::PRED_LT_PLUS:
762 SubIdx = PPC::sub_lt; SwapOps = false; break;
763 case PPC::PRED_GE:
764 case PPC::PRED_GE_MINUS:
765 case PPC::PRED_GE_PLUS:
766 SubIdx = PPC::sub_lt; SwapOps = true; break;
767 case PPC::PRED_GT:
768 case PPC::PRED_GT_MINUS:
769 case PPC::PRED_GT_PLUS:
770 SubIdx = PPC::sub_gt; SwapOps = false; break;
771 case PPC::PRED_LE:
772 case PPC::PRED_LE_MINUS:
773 case PPC::PRED_LE_PLUS:
774 SubIdx = PPC::sub_gt; SwapOps = true; break;
775 case PPC::PRED_UN:
776 case PPC::PRED_UN_MINUS:
777 case PPC::PRED_UN_PLUS:
778 SubIdx = PPC::sub_un; SwapOps = false; break;
779 case PPC::PRED_NU:
780 case PPC::PRED_NU_MINUS:
781 case PPC::PRED_NU_PLUS:
782 SubIdx = PPC::sub_un; SwapOps = true; break;
Hal Finkel940ab932014-02-28 00:27:01 +0000783 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break;
784 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
Hal Finkeled6a2852013-04-05 23:29:01 +0000785 }
786
787 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
788 SecondReg = SwapOps ? TrueReg : FalseReg;
789
790 // The first input register of isel cannot be r0. If it is a member
791 // of a register class that can be r0, then copy it first (the
792 // register allocator should eliminate the copy).
793 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
794 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
795 const TargetRegisterClass *FirstRC =
796 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
797 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
798 unsigned OldFirstReg = FirstReg;
799 FirstReg = MRI.createVirtualRegister(FirstRC);
800 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
801 .addReg(OldFirstReg);
802 }
803
804 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
805 .addReg(FirstReg).addReg(SecondReg)
806 .addReg(Cond[1].getReg(), 0, SubIdx);
807}
808
Kit Barton535e69d2015-03-25 19:36:23 +0000809static unsigned getCRBitValue(unsigned CRBit) {
810 unsigned Ret = 4;
811 if (CRBit == PPC::CR0LT || CRBit == PPC::CR1LT ||
812 CRBit == PPC::CR2LT || CRBit == PPC::CR3LT ||
813 CRBit == PPC::CR4LT || CRBit == PPC::CR5LT ||
814 CRBit == PPC::CR6LT || CRBit == PPC::CR7LT)
815 Ret = 3;
816 if (CRBit == PPC::CR0GT || CRBit == PPC::CR1GT ||
817 CRBit == PPC::CR2GT || CRBit == PPC::CR3GT ||
818 CRBit == PPC::CR4GT || CRBit == PPC::CR5GT ||
819 CRBit == PPC::CR6GT || CRBit == PPC::CR7GT)
820 Ret = 2;
821 if (CRBit == PPC::CR0EQ || CRBit == PPC::CR1EQ ||
822 CRBit == PPC::CR2EQ || CRBit == PPC::CR3EQ ||
823 CRBit == PPC::CR4EQ || CRBit == PPC::CR5EQ ||
824 CRBit == PPC::CR6EQ || CRBit == PPC::CR7EQ)
825 Ret = 1;
826 if (CRBit == PPC::CR0UN || CRBit == PPC::CR1UN ||
827 CRBit == PPC::CR2UN || CRBit == PPC::CR3UN ||
828 CRBit == PPC::CR4UN || CRBit == PPC::CR5UN ||
829 CRBit == PPC::CR6UN || CRBit == PPC::CR7UN)
830 Ret = 0;
831
832 assert(Ret != 4 && "Invalid CR bit register");
833 return Ret;
834}
835
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000836void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
837 MachineBasicBlock::iterator I, DebugLoc DL,
838 unsigned DestReg, unsigned SrcReg,
839 bool KillSrc) const {
Hal Finkel27774d92014-03-13 07:58:58 +0000840 // We can end up with self copies and similar things as a result of VSX copy
Hal Finkel9dcb3582014-03-27 22:46:28 +0000841 // legalization. Promote them here.
Hal Finkel27774d92014-03-13 07:58:58 +0000842 const TargetRegisterInfo *TRI = &getRegisterInfo();
843 if (PPC::F8RCRegClass.contains(DestReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000844 PPC::VSRCRegClass.contains(SrcReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000845 unsigned SuperReg =
846 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass);
847
Hal Finkel9dcb3582014-03-27 22:46:28 +0000848 if (VSXSelfCopyCrash && SrcReg == SuperReg)
849 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000850
851 DestReg = SuperReg;
852 } else if (PPC::VRRCRegClass.contains(DestReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000853 PPC::VSRCRegClass.contains(SrcReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000854 unsigned SuperReg =
855 TRI->getMatchingSuperReg(DestReg, PPC::sub_128, &PPC::VSRCRegClass);
856
Hal Finkel9dcb3582014-03-27 22:46:28 +0000857 if (VSXSelfCopyCrash && SrcReg == SuperReg)
858 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000859
860 DestReg = SuperReg;
861 } else if (PPC::F8RCRegClass.contains(SrcReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000862 PPC::VSRCRegClass.contains(DestReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000863 unsigned SuperReg =
864 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass);
865
Hal Finkel9dcb3582014-03-27 22:46:28 +0000866 if (VSXSelfCopyCrash && DestReg == SuperReg)
867 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000868
869 SrcReg = SuperReg;
870 } else if (PPC::VRRCRegClass.contains(SrcReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000871 PPC::VSRCRegClass.contains(DestReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000872 unsigned SuperReg =
873 TRI->getMatchingSuperReg(SrcReg, PPC::sub_128, &PPC::VSRCRegClass);
874
Hal Finkel9dcb3582014-03-27 22:46:28 +0000875 if (VSXSelfCopyCrash && DestReg == SuperReg)
876 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000877
878 SrcReg = SuperReg;
879 }
880
Kit Barton535e69d2015-03-25 19:36:23 +0000881 // Different class register copy
882 if (PPC::CRBITRCRegClass.contains(SrcReg) &&
883 PPC::GPRCRegClass.contains(DestReg)) {
884 unsigned CRReg = getCRFromCRBit(SrcReg);
Richard Trieu7a083812016-02-18 22:09:30 +0000885 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(CRReg);
886 getKillRegState(KillSrc);
Kit Barton535e69d2015-03-25 19:36:23 +0000887 // Rotate the CR bit in the CR fields to be the least significant bit and
888 // then mask with 0x1 (MB = ME = 31).
889 BuildMI(MBB, I, DL, get(PPC::RLWINM), DestReg)
890 .addReg(DestReg, RegState::Kill)
891 .addImm(TRI->getEncodingValue(CRReg) * 4 + (4 - getCRBitValue(SrcReg)))
892 .addImm(31)
893 .addImm(31);
894 return;
895 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
896 PPC::G8RCRegClass.contains(DestReg)) {
Richard Trieu7a083812016-02-18 22:09:30 +0000897 BuildMI(MBB, I, DL, get(PPC::MFOCRF8), DestReg).addReg(SrcReg);
898 getKillRegState(KillSrc);
Kit Barton535e69d2015-03-25 19:36:23 +0000899 return;
900 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
901 PPC::GPRCRegClass.contains(DestReg)) {
Richard Trieu7a083812016-02-18 22:09:30 +0000902 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(SrcReg);
903 getKillRegState(KillSrc);
Kit Barton535e69d2015-03-25 19:36:23 +0000904 return;
905 }
906
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000907 unsigned Opc;
908 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
909 Opc = PPC::OR;
910 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
911 Opc = PPC::OR8;
912 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
913 Opc = PPC::FMR;
914 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
915 Opc = PPC::MCRF;
916 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
917 Opc = PPC::VOR;
Hal Finkel27774d92014-03-13 07:58:58 +0000918 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg))
Hal Finkelbbad2332014-03-24 09:36:36 +0000919 // There are two different ways this can be done:
Hal Finkel27774d92014-03-13 07:58:58 +0000920 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
921 // issue in VSU pipeline 0.
922 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
923 // can go to either pipeline.
Hal Finkelbbad2332014-03-24 09:36:36 +0000924 // We'll always use xxlor here, because in practically all cases where
925 // copies are generated, they are close enough to some use that the
926 // lower-latency form is preferable.
Hal Finkel27774d92014-03-13 07:58:58 +0000927 Opc = PPC::XXLOR;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +0000928 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg) ||
929 PPC::VSSRCRegClass.contains(DestReg, SrcReg))
Hal Finkel19be5062014-03-29 05:29:01 +0000930 Opc = PPC::XXLORf;
Hal Finkelc93a9a22015-02-25 01:06:45 +0000931 else if (PPC::QFRCRegClass.contains(DestReg, SrcReg))
932 Opc = PPC::QVFMR;
933 else if (PPC::QSRCRegClass.contains(DestReg, SrcReg))
934 Opc = PPC::QVFMRs;
935 else if (PPC::QBRCRegClass.contains(DestReg, SrcReg))
936 Opc = PPC::QVFMRb;
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000937 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
938 Opc = PPC::CROR;
939 else
940 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +0000941
Evan Cheng6cc775f2011-06-28 19:10:37 +0000942 const MCInstrDesc &MCID = get(Opc);
943 if (MCID.getNumOperands() == 3)
944 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000945 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
946 else
Evan Cheng6cc775f2011-06-28 19:10:37 +0000947 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +0000948}
949
Hal Finkel8f6834d2011-12-05 17:55:17 +0000950// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000951bool
Dan Gohman3b460302008-07-07 23:14:23 +0000952PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
953 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000954 int FrameIdx,
955 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000956 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000957 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000958 // Note: If additional store instructions are added here,
959 // update isStoreToStackSlot.
960
Chris Lattner6f306d72010-04-02 20:16:16 +0000961 DebugLoc DL;
Hal Finkel4e703bc2014-01-28 05:32:58 +0000962 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
963 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000964 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
965 .addReg(SrcReg,
966 getKillRegState(isKill)),
967 FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000968 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
969 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000970 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
971 .addReg(SrcReg,
972 getKillRegState(isKill)),
973 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000974 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000975 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000976 .addReg(SrcReg,
977 getKillRegState(isKill)),
978 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000979 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000980 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000981 .addReg(SrcReg,
982 getKillRegState(isKill)),
983 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000984 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000985 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
986 .addReg(SrcReg,
987 getKillRegState(isKill)),
988 FrameIdx));
989 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000990 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000991 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT))
992 .addReg(SrcReg,
993 getKillRegState(isKill)),
994 FrameIdx));
995 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000996 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000997 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
998 .addReg(SrcReg,
999 getKillRegState(isKill)),
1000 FrameIdx));
1001 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +00001002 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
1003 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXVD2X))
1004 .addReg(SrcReg,
1005 getKillRegState(isKill)),
1006 FrameIdx));
1007 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +00001008 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
1009 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSDX))
1010 .addReg(SrcReg,
1011 getKillRegState(isKill)),
1012 FrameIdx));
1013 NonRI = true;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001014 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
1015 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSSPX))
1016 .addReg(SrcReg,
1017 getKillRegState(isKill)),
1018 FrameIdx));
1019 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +00001020 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001021 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +00001022 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +00001023 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
1024 .addReg(SrcReg,
1025 getKillRegState(isKill)),
1026 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001027 SpillsVRS = true;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001028 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
1029 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDX))
1030 .addReg(SrcReg,
1031 getKillRegState(isKill)),
1032 FrameIdx));
1033 NonRI = true;
1034 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
1035 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFSXs))
1036 .addReg(SrcReg,
1037 getKillRegState(isKill)),
1038 FrameIdx));
1039 NonRI = true;
1040 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
1041 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDXb))
1042 .addReg(SrcReg,
1043 getKillRegState(isKill)),
1044 FrameIdx));
1045 NonRI = true;
Owen Andersoneee14602008-01-01 21:11:32 +00001046 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001047 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +00001048 }
Bill Wendling632ea652008-03-03 22:19:16 +00001049
1050 return false;
Owen Andersoneee14602008-01-01 21:11:32 +00001051}
1052
1053void
1054PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +00001055 MachineBasicBlock::iterator MI,
1056 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +00001057 const TargetRegisterClass *RC,
1058 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +00001059 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +00001060 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +00001061
Hal Finkelbb420f12013-03-15 05:06:04 +00001062 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1063 FuncInfo->setHasSpills();
1064
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001065 bool NonRI = false, SpillsVRS = false;
1066 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
1067 NonRI, SpillsVRS))
Bill Wendling632ea652008-03-03 22:19:16 +00001068 FuncInfo->setSpillsCR();
Bill Wendling632ea652008-03-03 22:19:16 +00001069
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001070 if (SpillsVRS)
1071 FuncInfo->setSpillsVRSAVE();
1072
Hal Finkelfcc51d42013-03-17 04:43:44 +00001073 if (NonRI)
1074 FuncInfo->setHasNonRISpills();
1075
Owen Andersoneee14602008-01-01 21:11:32 +00001076 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1077 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001078
1079 const MachineFrameInfo &MFI = *MF.getFrameInfo();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001080 MachineMemOperand *MMO = MF.getMachineMemOperand(
1081 MachinePointerInfo::getFixedStack(MF, FrameIdx),
1082 MachineMemOperand::MOStore, MFI.getObjectSize(FrameIdx),
1083 MFI.getObjectAlignment(FrameIdx));
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001084 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +00001085}
1086
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001087bool
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001088PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman3b460302008-07-07 23:14:23 +00001089 unsigned DestReg, int FrameIdx,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +00001090 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +00001091 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001092 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +00001093 // Note: If additional load instructions are added here,
1094 // update isLoadFromStackSlot.
1095
Hal Finkel4e703bc2014-01-28 05:32:58 +00001096 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
1097 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +00001098 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
1099 DestReg), FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +00001100 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
1101 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +00001102 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
1103 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001104 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001105 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +00001106 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001107 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001108 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +00001109 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001110 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +00001111 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1112 get(PPC::RESTORE_CR), DestReg),
1113 FrameIdx));
1114 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001115 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +00001116 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1117 get(PPC::RESTORE_CRBIT), DestReg),
1118 FrameIdx));
1119 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001120 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +00001121 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
1122 FrameIdx));
1123 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +00001124 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
1125 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXVD2X), DestReg),
1126 FrameIdx));
1127 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +00001128 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
1129 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSDX), DestReg),
1130 FrameIdx));
1131 NonRI = true;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001132 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
1133 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSSPX), DestReg),
1134 FrameIdx));
1135 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +00001136 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001137 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +00001138 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +00001139 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1140 get(PPC::RESTORE_VRSAVE),
1141 DestReg),
1142 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001143 SpillsVRS = true;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001144 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
1145 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDX), DestReg),
1146 FrameIdx));
1147 NonRI = true;
1148 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
1149 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFSXs), DestReg),
1150 FrameIdx));
1151 NonRI = true;
1152 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
1153 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDXb), DestReg),
1154 FrameIdx));
1155 NonRI = true;
Owen Andersoneee14602008-01-01 21:11:32 +00001156 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001157 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +00001158 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001159
1160 return false;
Owen Andersoneee14602008-01-01 21:11:32 +00001161}
1162
1163void
1164PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +00001165 MachineBasicBlock::iterator MI,
1166 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +00001167 const TargetRegisterClass *RC,
1168 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +00001169 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +00001170 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +00001171 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001172 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelfcc51d42013-03-17 04:43:44 +00001173
1174 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1175 FuncInfo->setHasSpills();
1176
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001177 bool NonRI = false, SpillsVRS = false;
1178 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
1179 NonRI, SpillsVRS))
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001180 FuncInfo->setSpillsCR();
Hal Finkelfcc51d42013-03-17 04:43:44 +00001181
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001182 if (SpillsVRS)
1183 FuncInfo->setSpillsVRSAVE();
1184
Hal Finkelfcc51d42013-03-17 04:43:44 +00001185 if (NonRI)
1186 FuncInfo->setHasNonRISpills();
1187
Owen Andersoneee14602008-01-01 21:11:32 +00001188 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1189 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001190
1191 const MachineFrameInfo &MFI = *MF.getFrameInfo();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001192 MachineMemOperand *MMO = MF.getMachineMemOperand(
1193 MachinePointerInfo::getFixedStack(MF, FrameIdx),
1194 MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIdx),
1195 MFI.getObjectAlignment(FrameIdx));
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001196 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +00001197}
1198
Chris Lattnera47294ed2006-10-13 21:21:17 +00001199bool PPCInstrInfo::
Owen Anderson4f6bf042008-08-14 22:49:33 +00001200ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +00001201 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel96c2d4d2012-06-08 15:38:21 +00001202 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
1203 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
1204 else
1205 // Leave the CR# the same, but invert the condition.
1206 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +00001207 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +00001208}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001209
Hal Finkeld61d4f82013-04-06 19:30:30 +00001210bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
1211 unsigned Reg, MachineRegisterInfo *MRI) const {
1212 // For some instructions, it is legal to fold ZERO into the RA register field.
1213 // A zero immediate should always be loaded with a single li.
1214 unsigned DefOpc = DefMI->getOpcode();
1215 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
1216 return false;
1217 if (!DefMI->getOperand(1).isImm())
1218 return false;
1219 if (DefMI->getOperand(1).getImm() != 0)
1220 return false;
1221
1222 // Note that we cannot here invert the arguments of an isel in order to fold
1223 // a ZERO into what is presented as the second argument. All we have here
1224 // is the condition bit, and that might come from a CR-logical bit operation.
1225
1226 const MCInstrDesc &UseMCID = UseMI->getDesc();
1227
1228 // Only fold into real machine instructions.
1229 if (UseMCID.isPseudo())
1230 return false;
1231
1232 unsigned UseIdx;
1233 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
1234 if (UseMI->getOperand(UseIdx).isReg() &&
1235 UseMI->getOperand(UseIdx).getReg() == Reg)
1236 break;
1237
1238 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
1239 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
1240
1241 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
1242
1243 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
1244 // register (which might also be specified as a pointer class kind).
1245 if (UseInfo->isLookupPtrRegClass()) {
1246 if (UseInfo->RegClass /* Kind */ != 1)
1247 return false;
1248 } else {
1249 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
1250 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
1251 return false;
1252 }
1253
1254 // Make sure this is not tied to an output register (or otherwise
1255 // constrained). This is true for ST?UX registers, for example, which
1256 // are tied to their output registers.
1257 if (UseInfo->Constraints != 0)
1258 return false;
1259
1260 unsigned ZeroReg;
1261 if (UseInfo->isLookupPtrRegClass()) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001262 bool isPPC64 = Subtarget.isPPC64();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001263 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
1264 } else {
1265 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
1266 PPC::ZERO8 : PPC::ZERO;
1267 }
1268
1269 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
1270 UseMI->getOperand(UseIdx).setReg(ZeroReg);
1271
1272 if (DeleteDef)
1273 DefMI->eraseFromParent();
1274
1275 return true;
1276}
1277
Hal Finkel30ae2292013-04-10 18:30:16 +00001278static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
1279 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1280 I != IE; ++I)
1281 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
1282 return true;
1283 return false;
1284}
1285
1286// We should make sure that, if we're going to predicate both sides of a
1287// condition (a diamond), that both sides don't define the counter register. We
1288// can predicate counter-decrement-based branches, but while that predicates
1289// the branching, it does not predicate the counter decrement. If we tried to
1290// merge the triangle into one predicated block, we'd decrement the counter
1291// twice.
1292bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
1293 unsigned NumT, unsigned ExtraT,
1294 MachineBasicBlock &FMBB,
1295 unsigned NumF, unsigned ExtraF,
Cong Houc536bd92015-09-10 23:10:42 +00001296 BranchProbability Probability) const {
Hal Finkel30ae2292013-04-10 18:30:16 +00001297 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
1298}
1299
1300
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001301bool PPCInstrInfo::isPredicated(const MachineInstr &MI) const {
Hal Finkelf29285a2013-04-11 01:23:34 +00001302 // The predicated branches are identified by their type, not really by the
1303 // explicit presence of a predicate. Furthermore, some of them can be
1304 // predicated more than once. Because if conversion won't try to predicate
1305 // any instruction which already claims to be predicated (by returning true
1306 // here), always return false. In doing so, we let isPredicable() be the
1307 // final word on whether not the instruction can be (further) predicated.
1308
1309 return false;
Hal Finkel5711eca2013-04-09 22:58:37 +00001310}
1311
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001312bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const {
1313 if (!MI.isTerminator())
Hal Finkel5711eca2013-04-09 22:58:37 +00001314 return false;
1315
1316 // Conditional branch is a special case.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001317 if (MI.isBranch() && !MI.isBarrier())
Hal Finkel5711eca2013-04-09 22:58:37 +00001318 return true;
1319
1320 return !isPredicated(MI);
1321}
1322
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001323bool PPCInstrInfo::PredicateInstruction(MachineInstr &MI,
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001324 ArrayRef<MachineOperand> Pred) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001325 unsigned OpC = MI.getOpcode();
Hal Finkelf4a22c02015-01-13 17:47:54 +00001326 if (OpC == PPC::BLR || OpC == PPC::BLR8) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001327 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001328 bool isPPC64 = Subtarget.isPPC64();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001329 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR)
1330 : (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
Hal Finkel940ab932014-02-28 00:27:01 +00001331 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001332 MI.setDesc(get(PPC::BCLR));
1333 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1334 .addReg(Pred[1].getReg());
Hal Finkel940ab932014-02-28 00:27:01 +00001335 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001336 MI.setDesc(get(PPC::BCLRn));
1337 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1338 .addReg(Pred[1].getReg());
Hal Finkel940ab932014-02-28 00:27:01 +00001339 } else {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001340 MI.setDesc(get(PPC::BCCLR));
1341 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1342 .addImm(Pred[0].getImm())
1343 .addReg(Pred[1].getReg());
Hal Finkel5711eca2013-04-09 22:58:37 +00001344 }
1345
1346 return true;
1347 } else if (OpC == PPC::B) {
1348 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001349 bool isPPC64 = Subtarget.isPPC64();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001350 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ)
1351 : (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
Hal Finkel940ab932014-02-28 00:27:01 +00001352 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001353 MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1354 MI.RemoveOperand(0);
Hal Finkel940ab932014-02-28 00:27:01 +00001355
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001356 MI.setDesc(get(PPC::BC));
1357 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1358 .addReg(Pred[1].getReg())
1359 .addMBB(MBB);
Hal Finkel940ab932014-02-28 00:27:01 +00001360 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001361 MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1362 MI.RemoveOperand(0);
Hal Finkel940ab932014-02-28 00:27:01 +00001363
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001364 MI.setDesc(get(PPC::BCn));
1365 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1366 .addReg(Pred[1].getReg())
1367 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001368 } else {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001369 MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1370 MI.RemoveOperand(0);
Hal Finkel5711eca2013-04-09 22:58:37 +00001371
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001372 MI.setDesc(get(PPC::BCC));
1373 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1374 .addImm(Pred[0].getImm())
1375 .addReg(Pred[1].getReg())
1376 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001377 }
1378
1379 return true;
Hal Finkel500b0042013-04-10 06:42:34 +00001380 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1381 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1382 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1383 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1384
1385 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
Eric Christopher1dcea732014-06-12 21:48:52 +00001386 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel940ab932014-02-28 00:27:01 +00001387
1388 if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001389 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8)
1390 : (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1391 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1392 .addReg(Pred[1].getReg());
Hal Finkel940ab932014-02-28 00:27:01 +00001393 return true;
1394 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001395 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n)
1396 : (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1397 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1398 .addReg(Pred[1].getReg());
Hal Finkel940ab932014-02-28 00:27:01 +00001399 return true;
1400 }
1401
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001402 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8)
1403 : (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
1404 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1405 .addImm(Pred[0].getImm())
1406 .addReg(Pred[1].getReg());
Hal Finkel500b0042013-04-10 06:42:34 +00001407 return true;
Hal Finkel5711eca2013-04-09 22:58:37 +00001408 }
1409
1410 return false;
1411}
1412
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001413bool PPCInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1414 ArrayRef<MachineOperand> Pred2) const {
Hal Finkel5711eca2013-04-09 22:58:37 +00001415 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1416 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1417
1418 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1419 return false;
1420 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1421 return false;
1422
Hal Finkel94a6f382013-12-11 23:12:25 +00001423 // P1 can only subsume P2 if they test the same condition register.
1424 if (Pred1[1].getReg() != Pred2[1].getReg())
1425 return false;
1426
Hal Finkel5711eca2013-04-09 22:58:37 +00001427 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1428 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1429
1430 if (P1 == P2)
1431 return true;
1432
1433 // Does P1 subsume P2, e.g. GE subsumes GT.
1434 if (P1 == PPC::PRED_LE &&
1435 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1436 return true;
1437 if (P1 == PPC::PRED_GE &&
1438 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1439 return true;
1440
1441 return false;
1442}
1443
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001444bool PPCInstrInfo::DefinesPredicate(MachineInstr &MI,
Hal Finkel5711eca2013-04-09 22:58:37 +00001445 std::vector<MachineOperand> &Pred) const {
1446 // Note: At the present time, the contents of Pred from this function is
1447 // unused by IfConversion. This implementation follows ARM by pushing the
1448 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1449 // predicate, instructions defining CTR or CTR8 are also included as
1450 // predicate-defining instructions.
1451
1452 const TargetRegisterClass *RCs[] =
1453 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1454 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1455
1456 bool Found = false;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001457 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1458 const MachineOperand &MO = MI.getOperand(i);
Hal Finkelaf822012013-04-10 07:17:47 +00001459 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001460 const TargetRegisterClass *RC = RCs[c];
Hal Finkelaf822012013-04-10 07:17:47 +00001461 if (MO.isReg()) {
1462 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001463 Pred.push_back(MO);
1464 Found = true;
1465 }
Hal Finkelaf822012013-04-10 07:17:47 +00001466 } else if (MO.isRegMask()) {
1467 for (TargetRegisterClass::iterator I = RC->begin(),
1468 IE = RC->end(); I != IE; ++I)
1469 if (MO.clobbersPhysReg(*I)) {
1470 Pred.push_back(MO);
1471 Found = true;
1472 }
Hal Finkel5711eca2013-04-09 22:58:37 +00001473 }
1474 }
1475 }
1476
1477 return Found;
1478}
1479
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001480bool PPCInstrInfo::isPredicable(MachineInstr &MI) const {
1481 unsigned OpC = MI.getOpcode();
Hal Finkel5711eca2013-04-09 22:58:37 +00001482 switch (OpC) {
1483 default:
1484 return false;
1485 case PPC::B:
1486 case PPC::BLR:
Hal Finkelf4a22c02015-01-13 17:47:54 +00001487 case PPC::BLR8:
Hal Finkel500b0042013-04-10 06:42:34 +00001488 case PPC::BCTR:
1489 case PPC::BCTR8:
1490 case PPC::BCTRL:
1491 case PPC::BCTRL8:
Hal Finkel5711eca2013-04-09 22:58:37 +00001492 return true;
1493 }
1494}
1495
Hal Finkel82656cb2013-04-18 22:15:08 +00001496bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI,
1497 unsigned &SrcReg, unsigned &SrcReg2,
1498 int &Mask, int &Value) const {
1499 unsigned Opc = MI->getOpcode();
1500
1501 switch (Opc) {
1502 default: return false;
1503 case PPC::CMPWI:
1504 case PPC::CMPLWI:
1505 case PPC::CMPDI:
1506 case PPC::CMPLDI:
1507 SrcReg = MI->getOperand(1).getReg();
1508 SrcReg2 = 0;
1509 Value = MI->getOperand(2).getImm();
1510 Mask = 0xFFFF;
1511 return true;
1512 case PPC::CMPW:
1513 case PPC::CMPLW:
1514 case PPC::CMPD:
1515 case PPC::CMPLD:
1516 case PPC::FCMPUS:
1517 case PPC::FCMPUD:
1518 SrcReg = MI->getOperand(1).getReg();
1519 SrcReg2 = MI->getOperand(2).getReg();
1520 return true;
1521 }
1522}
Hal Finkele6322392013-04-19 22:08:38 +00001523
Hal Finkel82656cb2013-04-18 22:15:08 +00001524bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr,
1525 unsigned SrcReg, unsigned SrcReg2,
1526 int Mask, int Value,
1527 const MachineRegisterInfo *MRI) const {
Hal Finkelb12da6b2013-04-18 22:54:25 +00001528 if (DisableCmpOpt)
1529 return false;
1530
Hal Finkel82656cb2013-04-18 22:15:08 +00001531 int OpC = CmpInstr->getOpcode();
1532 unsigned CRReg = CmpInstr->getOperand(0).getReg();
Hal Finkel08e53ee2013-05-08 12:16:14 +00001533
1534 // FP record forms set CR1 based on the execption status bits, not a
1535 // comparison with zero.
1536 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1537 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001538
1539 // The record forms set the condition register based on a signed comparison
1540 // with zero (so says the ISA manual). This is not as straightforward as it
1541 // seems, however, because this is always a 64-bit comparison on PPC64, even
1542 // for instructions that are 32-bit in nature (like slw for example).
1543 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1544 // for equality checks (as those don't depend on the sign). On PPC64,
1545 // we are restricted to equality for unsigned 64-bit comparisons and for
1546 // signed 32-bit comparisons the applicability is more restricted.
Eric Christopher1dcea732014-06-12 21:48:52 +00001547 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel82656cb2013-04-18 22:15:08 +00001548 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1549 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1550 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1551
1552 // Get the unique definition of SrcReg.
1553 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1554 if (!MI) return false;
1555 int MIOpC = MI->getOpcode();
1556
1557 bool equalityOnly = false;
1558 bool noSub = false;
1559 if (isPPC64) {
1560 if (is32BitSignedCompare) {
1561 // We can perform this optimization only if MI is sign-extending.
1562 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1563 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1564 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1565 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1566 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1567 noSub = true;
1568 } else
1569 return false;
1570 } else if (is32BitUnsignedCompare) {
Kyle Butt61311282016-03-23 19:51:22 +00001571 // 32-bit rotate and mask instructions are zero extending only if MB <= ME
1572 bool isZeroExtendingRotate =
1573 (MIOpC == PPC::RLWINM || MIOpC == PPC::RLWINMo ||
1574 MIOpC == PPC::RLWNM || MIOpC == PPC::RLWNMo)
1575 && MI->getOperand(3).getImm() <= MI->getOperand(4).getImm();
1576
Hal Finkel82656cb2013-04-18 22:15:08 +00001577 // We can perform this optimization, equality only, if MI is
1578 // zero-extending.
1579 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1580 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
Kyle Butt61311282016-03-23 19:51:22 +00001581 MIOpC == PPC::SRW || MIOpC == PPC::SRWo ||
1582 isZeroExtendingRotate) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001583 noSub = true;
1584 equalityOnly = true;
1585 } else
1586 return false;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001587 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001588 equalityOnly = is64BitUnsignedCompare;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001589 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001590 equalityOnly = is32BitUnsignedCompare;
1591
1592 if (equalityOnly) {
1593 // We need to check the uses of the condition register in order to reject
1594 // non-equality comparisons.
Owen Anderson16c6bf42014-03-13 23:12:04 +00001595 for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg),
1596 IE = MRI->use_instr_end(); I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001597 MachineInstr *UseMI = &*I;
1598 if (UseMI->getOpcode() == PPC::BCC) {
1599 unsigned Pred = UseMI->getOperand(0).getImm();
Hal Finkelc3632452013-05-07 17:49:55 +00001600 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE)
1601 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001602 } else if (UseMI->getOpcode() == PPC::ISEL ||
1603 UseMI->getOpcode() == PPC::ISEL8) {
1604 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkelc3632452013-05-07 17:49:55 +00001605 if (SubIdx != PPC::sub_eq)
1606 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001607 } else
1608 return false;
1609 }
1610 }
1611
Hal Finkelc3632452013-05-07 17:49:55 +00001612 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001613
1614 // Scan forward to find the first use of the compare.
1615 for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end();
1616 I != EL; ++I) {
1617 bool FoundUse = false;
Owen Anderson16c6bf42014-03-13 23:12:04 +00001618 for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg),
1619 JE = MRI->use_instr_end(); J != JE; ++J)
Hal Finkel82656cb2013-04-18 22:15:08 +00001620 if (&*J == &*I) {
1621 FoundUse = true;
1622 break;
1623 }
1624
1625 if (FoundUse)
1626 break;
1627 }
1628
Hal Finkel82656cb2013-04-18 22:15:08 +00001629 // There are two possible candidates which can be changed to set CR[01].
1630 // One is MI, the other is a SUB instruction.
1631 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
Craig Topper062a2ba2014-04-25 05:30:21 +00001632 MachineInstr *Sub = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001633 if (SrcReg2 != 0)
1634 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00001635 MI = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001636 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1637 // same BB as the comparison. This is to allow the check below to avoid calls
1638 // (and other explicit clobbers); instead we should really check for these
1639 // more explicitly (in at least a few predecessors).
1640 else if (MI->getParent() != CmpInstr->getParent() || Value != 0) {
1641 // PPC does not have a record-form SUBri.
1642 return false;
1643 }
1644
1645 // Search for Sub.
1646 const TargetRegisterInfo *TRI = &getRegisterInfo();
1647 --I;
Hal Finkelc3632452013-05-07 17:49:55 +00001648
1649 // Get ready to iterate backward from CmpInstr.
1650 MachineBasicBlock::iterator E = MI,
1651 B = CmpInstr->getParent()->begin();
1652
Hal Finkel82656cb2013-04-18 22:15:08 +00001653 for (; I != E && !noSub; --I) {
1654 const MachineInstr &Instr = *I;
1655 unsigned IOpC = Instr.getOpcode();
1656
1657 if (&*I != CmpInstr && (
Hal Finkel08e53ee2013-05-08 12:16:14 +00001658 Instr.modifiesRegister(PPC::CR0, TRI) ||
1659 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel82656cb2013-04-18 22:15:08 +00001660 // This instruction modifies or uses the record condition register after
1661 // the one we want to change. While we could do this transformation, it
1662 // would likely not be profitable. This transformation removes one
1663 // instruction, and so even forcing RA to generate one move probably
1664 // makes it unprofitable.
1665 return false;
1666
1667 // Check whether CmpInstr can be made redundant by the current instruction.
1668 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1669 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1670 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1671 ((Instr.getOperand(1).getReg() == SrcReg &&
1672 Instr.getOperand(2).getReg() == SrcReg2) ||
1673 (Instr.getOperand(1).getReg() == SrcReg2 &&
1674 Instr.getOperand(2).getReg() == SrcReg))) {
1675 Sub = &*I;
1676 break;
1677 }
1678
Hal Finkel82656cb2013-04-18 22:15:08 +00001679 if (I == B)
1680 // The 'and' is below the comparison instruction.
1681 return false;
1682 }
1683
1684 // Return false if no candidates exist.
1685 if (!MI && !Sub)
1686 return false;
1687
1688 // The single candidate is called MI.
1689 if (!MI) MI = Sub;
1690
1691 int NewOpC = -1;
1692 MIOpC = MI->getOpcode();
1693 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1694 NewOpC = MIOpC;
1695 else {
1696 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1697 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1698 NewOpC = MIOpC;
1699 }
1700
1701 // FIXME: On the non-embedded POWER architectures, only some of the record
1702 // forms are fast, and we should use only the fast ones.
1703
1704 // The defining instruction has a record form (or is already a record
1705 // form). It is possible, however, that we'll need to reverse the condition
1706 // code of the users.
1707 if (NewOpC == -1)
1708 return false;
1709
Hal Finkele6322392013-04-19 22:08:38 +00001710 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1711 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
Hal Finkel82656cb2013-04-18 22:15:08 +00001712
1713 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1714 // needs to be updated to be based on SUB. Push the condition code
1715 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1716 // condition code of these operands will be modified.
1717 bool ShouldSwap = false;
1718 if (Sub) {
1719 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1720 Sub->getOperand(2).getReg() == SrcReg;
1721
1722 // The operands to subf are the opposite of sub, so only in the fixed-point
1723 // case, invert the order.
Hal Finkel08e53ee2013-05-08 12:16:14 +00001724 ShouldSwap = !ShouldSwap;
Hal Finkel82656cb2013-04-18 22:15:08 +00001725 }
1726
1727 if (ShouldSwap)
Owen Anderson16c6bf42014-03-13 23:12:04 +00001728 for (MachineRegisterInfo::use_instr_iterator
1729 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1730 I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001731 MachineInstr *UseMI = &*I;
1732 if (UseMI->getOpcode() == PPC::BCC) {
1733 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hal Finkele6322392013-04-19 22:08:38 +00001734 assert((!equalityOnly ||
1735 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1736 "Invalid predicate for equality-only optimization");
Owen Anderson16c6bf42014-03-13 23:12:04 +00001737 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
Hal Finkel0f64e212013-04-20 05:16:26 +00001738 PPC::getSwappedPredicate(Pred)));
Hal Finkel82656cb2013-04-18 22:15:08 +00001739 } else if (UseMI->getOpcode() == PPC::ISEL ||
1740 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkele6322392013-04-19 22:08:38 +00001741 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1742 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1743 "Invalid CR bit for equality-only optimization");
1744
1745 if (NewSubReg == PPC::sub_lt)
1746 NewSubReg = PPC::sub_gt;
1747 else if (NewSubReg == PPC::sub_gt)
1748 NewSubReg = PPC::sub_lt;
1749
Owen Anderson16c6bf42014-03-13 23:12:04 +00001750 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
Hal Finkele6322392013-04-19 22:08:38 +00001751 NewSubReg));
Hal Finkel82656cb2013-04-18 22:15:08 +00001752 } else // We need to abort on a user we don't understand.
1753 return false;
1754 }
1755
1756 // Create a new virtual register to hold the value of the CR set by the
1757 // record-form instruction. If the instruction was not previously in
1758 // record form, then set the kill flag on the CR.
1759 CmpInstr->eraseFromParent();
1760
1761 MachineBasicBlock::iterator MII = MI;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001762 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
Hal Finkel82656cb2013-04-18 22:15:08 +00001763 get(TargetOpcode::COPY), CRReg)
Hal Finkel08e53ee2013-05-08 12:16:14 +00001764 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel82656cb2013-04-18 22:15:08 +00001765
1766 if (MIOpC != NewOpC) {
1767 // We need to be careful here: we're replacing one instruction with
1768 // another, and we need to make sure that we get all of the right
1769 // implicit uses and defs. On the other hand, the caller may be holding
1770 // an iterator to this instruction, and so we can't delete it (this is
1771 // specifically the case if this is the instruction directly after the
1772 // compare).
1773
1774 const MCInstrDesc &NewDesc = get(NewOpC);
1775 MI->setDesc(NewDesc);
1776
1777 if (NewDesc.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +00001778 for (const MCPhysReg *ImpDefs = NewDesc.getImplicitDefs();
Hal Finkel82656cb2013-04-18 22:15:08 +00001779 *ImpDefs; ++ImpDefs)
1780 if (!MI->definesRegister(*ImpDefs))
1781 MI->addOperand(*MI->getParent()->getParent(),
1782 MachineOperand::CreateReg(*ImpDefs, true, true));
1783 if (NewDesc.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +00001784 for (const MCPhysReg *ImpUses = NewDesc.getImplicitUses();
Hal Finkel82656cb2013-04-18 22:15:08 +00001785 *ImpUses; ++ImpUses)
1786 if (!MI->readsRegister(*ImpUses))
1787 MI->addOperand(*MI->getParent()->getParent(),
1788 MachineOperand::CreateReg(*ImpUses, false, true));
1789 }
1790
1791 // Modify the condition code of operands in OperandsToUpdate.
1792 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1793 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkele6322392013-04-19 22:08:38 +00001794 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1795 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001796
Hal Finkele6322392013-04-19 22:08:38 +00001797 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1798 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001799
1800 return true;
1801}
1802
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001803/// GetInstSize - Return the number of bytes of code the specified
1804/// instruction may be. This returns the maximum number of bytes.
1805///
1806unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Hal Finkela7bbaf62014-02-02 06:12:27 +00001807 unsigned Opcode = MI->getOpcode();
1808
1809 if (Opcode == PPC::INLINEASM) {
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001810 const MachineFunction *MF = MI->getParent()->getParent();
1811 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +00001812 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Hal Finkel934361a2015-01-14 01:07:51 +00001813 } else if (Opcode == TargetOpcode::STACKMAP) {
1814 return MI->getOperand(1).getImm();
1815 } else if (Opcode == TargetOpcode::PATCHPOINT) {
1816 PatchPointOpers Opers(MI);
1817 return Opers.getMetaOper(PatchPointOpers::NBytesPos).getImm();
Hal Finkela7bbaf62014-02-02 06:12:27 +00001818 } else {
1819 const MCInstrDesc &Desc = get(Opcode);
1820 return Desc.getSize();
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001821 }
1822}
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001823
Hal Finkel2d556982015-08-30 07:50:35 +00001824std::pair<unsigned, unsigned>
1825PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
1826 const unsigned Mask = PPCII::MO_ACCESS_MASK;
1827 return std::make_pair(TF & Mask, TF & ~Mask);
1828}
1829
1830ArrayRef<std::pair<unsigned, const char *>>
1831PPCInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
1832 using namespace PPCII;
Hal Finkel982e8d42015-08-30 08:07:29 +00001833 static const std::pair<unsigned, const char *> TargetFlags[] = {
Hal Finkel2d556982015-08-30 07:50:35 +00001834 {MO_LO, "ppc-lo"},
1835 {MO_HA, "ppc-ha"},
1836 {MO_TPREL_LO, "ppc-tprel-lo"},
1837 {MO_TPREL_HA, "ppc-tprel-ha"},
1838 {MO_DTPREL_LO, "ppc-dtprel-lo"},
1839 {MO_TLSLD_LO, "ppc-tlsld-lo"},
1840 {MO_TOC_LO, "ppc-toc-lo"},
1841 {MO_TLS, "ppc-tls"}};
1842 return makeArrayRef(TargetFlags);
1843}
1844
1845ArrayRef<std::pair<unsigned, const char *>>
1846PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
1847 using namespace PPCII;
Hal Finkel982e8d42015-08-30 08:07:29 +00001848 static const std::pair<unsigned, const char *> TargetFlags[] = {
Hal Finkel2d556982015-08-30 07:50:35 +00001849 {MO_PLT_OR_STUB, "ppc-plt-or-stub"},
1850 {MO_PIC_FLAG, "ppc-pic"},
1851 {MO_NLP_FLAG, "ppc-nlp"},
1852 {MO_NLP_HIDDEN_FLAG, "ppc-nlp-hidden"}};
1853 return makeArrayRef(TargetFlags);
1854}
1855