blob: ec74d309f68af000e8abbc5212a9a91706740222 [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"
Matthias Braunf8422972017-12-13 02:51:04 +000023#include "llvm/CodeGen/LiveIntervals.h"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Hal Finkelb5aa7e52013-04-08 16:24:03 +000025#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman116f9272004-08-17 04:55:41 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000027#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesenddbf7a82010-02-26 21:09:24 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Hal Finkel9f9f8922012-04-01 19:22:40 +000029#include "llvm/CodeGen/PseudoSourceValue.h"
Eric Christopher1dcea732014-06-12 21:48:52 +000030#include "llvm/CodeGen/ScheduleDAG.h"
Hal Finkel174e5902014-03-25 23:29:21 +000031#include "llvm/CodeGen/SlotIndexes.h"
Hal Finkel934361a2015-01-14 01:07:51 +000032#include "llvm/CodeGen/StackMaps.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000033#include "llvm/MC/MCAsmInfo.h"
Pete Cooper3de83e42015-05-15 21:58:42 +000034#include "llvm/MC/MCInst.h"
Bill Wendling1af20ad2008-03-04 23:13:51 +000035#include "llvm/Support/CommandLine.h"
Hal Finkel174e5902014-03-25 23:29:21 +000036#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000037#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000038#include "llvm/Support/TargetRegistry.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000039#include "llvm/Support/raw_ostream.h"
Misha Brukman116f9272004-08-17 04:55:41 +000040
Dan Gohman20857192010-04-15 17:20:57 +000041using namespace llvm;
Bill Wendling1af20ad2008-03-04 23:13:51 +000042
Chandler Carruthe96dd892014-04-21 22:55:11 +000043#define DEBUG_TYPE "ppc-instr-info"
44
Chandler Carruthd174b722014-04-22 02:03:14 +000045#define GET_INSTRMAP_INFO
46#define GET_INSTRINFO_CTOR_DTOR
47#include "PPCGenInstrInfo.inc"
48
Zaara Syedafcd96972017-09-21 16:12:33 +000049STATISTIC(NumStoreSPILLVSRRCAsVec,
50 "Number of spillvsrrc spilled to stack as vec");
51STATISTIC(NumStoreSPILLVSRRCAsGpr,
52 "Number of spillvsrrc spilled to stack as gpr");
53STATISTIC(NumGPRtoVSRSpill, "Number of gpr spills to spillvsrrc");
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +000054STATISTIC(CmpIselsConverted,
55 "Number of ISELs that depend on comparison of constants converted");
56STATISTIC(MissedConvertibleImmediateInstrs,
57 "Number of compare-immediate instructions fed by constants");
Zaara Syedafcd96972017-09-21 16:12:33 +000058
Hal Finkel821e0012012-06-08 15:38:25 +000059static cl::
Hal Finkelc6b5deb2012-06-08 19:19:53 +000060opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
61 cl::desc("Disable analysis for CTR loops"));
Hal Finkel821e0012012-06-08 15:38:25 +000062
Hal Finkele6322392013-04-19 22:08:38 +000063static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt",
Hal Finkelb12da6b2013-04-18 22:54:25 +000064cl::desc("Disable compare instruction optimization"), cl::Hidden);
65
Hal Finkel9dcb3582014-03-27 22:46:28 +000066static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy",
67cl::desc("Causes the backend to crash instead of generating a nop VSX copy"),
68cl::Hidden);
69
Hal Finkel8acae522015-07-14 20:02:02 +000070static cl::opt<bool>
71UseOldLatencyCalc("ppc-old-latency-calc", cl::Hidden,
72 cl::desc("Use the old (incorrect) instruction latency calculation"));
73
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000074// Pin the vtable to this file.
75void PPCInstrInfo::anchor() {}
76
Eric Christopher1dcea732014-06-12 21:48:52 +000077PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI)
Tim Shen918ed872017-02-10 21:03:24 +000078 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP,
79 /* CatchRetOpcode */ -1,
80 STI.isPPC64() ? PPC::BLR8 : PPC::BLR),
Eric Christopherea178cf2015-03-12 01:42:51 +000081 Subtarget(STI), RI(STI.getTargetMachine()) {}
Chris Lattner49cadab2006-06-17 00:01:04 +000082
Andrew Trick10ffc2b2010-12-24 05:03:26 +000083/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
84/// this target when scheduling the DAG.
Eric Christopherf047bfd2014-06-13 22:38:52 +000085ScheduleHazardRecognizer *
86PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
87 const ScheduleDAG *DAG) const {
88 unsigned Directive =
89 static_cast<const PPCSubtarget *>(STI)->getDarwinDirective();
Hal Finkel742b5352012-08-28 16:12:39 +000090 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
91 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
Eric Christopherf047bfd2014-06-13 22:38:52 +000092 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +000093 static_cast<const PPCSubtarget *>(STI)->getInstrItineraryData();
Hal Finkel563cc052013-12-02 23:52:46 +000094 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel6fa56972011-10-17 04:03:49 +000095 }
Hal Finkel58ca3602011-12-02 04:58:02 +000096
Eric Christopherf047bfd2014-06-13 22:38:52 +000097 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000098}
99
Hal Finkel58ca3602011-12-02 04:58:02 +0000100/// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
101/// to use for this target when scheduling the DAG.
Eric Christophercccae792015-01-30 22:02:31 +0000102ScheduleHazardRecognizer *
103PPCInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
104 const ScheduleDAG *DAG) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000105 unsigned Directive =
Eric Christophercccae792015-01-30 22:02:31 +0000106 DAG->MF.getSubtarget<PPCSubtarget>().getDarwinDirective();
Hal Finkel58ca3602011-12-02 04:58:02 +0000107
Nemanja Ivanovic6e29baf2016-05-09 18:54:58 +0000108 // FIXME: Leaving this as-is until we have POWER9 scheduling info
Will Schmidt970ff642014-06-26 13:36:19 +0000109 if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8)
Hal Finkelceb1f122013-12-12 00:19:11 +0000110 return new PPCDispatchGroupSBHazardRecognizer(II, DAG);
111
Hal Finkel58ca3602011-12-02 04:58:02 +0000112 // Most subtargets use a PPC970 recognizer.
Hal Finkel742b5352012-08-28 16:12:39 +0000113 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
114 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
Eric Christopher1dcea732014-06-12 21:48:52 +0000115 assert(DAG->TII && "No InstrInfo?");
Hal Finkel58ca3602011-12-02 04:58:02 +0000116
Eric Christopher1dcea732014-06-12 21:48:52 +0000117 return new PPCHazardRecognizer970(*DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000118 }
119
Hal Finkel563cc052013-12-02 23:52:46 +0000120 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000121}
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000122
Hal Finkel8acae522015-07-14 20:02:02 +0000123unsigned PPCInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000124 const MachineInstr &MI,
Hal Finkel8acae522015-07-14 20:02:02 +0000125 unsigned *PredCost) const {
126 if (!ItinData || UseOldLatencyCalc)
127 return PPCGenInstrInfo::getInstrLatency(ItinData, MI, PredCost);
128
129 // The default implementation of getInstrLatency calls getStageLatency, but
130 // getStageLatency does not do the right thing for us. While we have
131 // itinerary, most cores are fully pipelined, and so the itineraries only
132 // express the first part of the pipeline, not every stage. Instead, we need
133 // to use the listed output operand cycle number (using operand 0 here, which
134 // is an output).
135
136 unsigned Latency = 1;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000137 unsigned DefClass = MI.getDesc().getSchedClass();
138 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
139 const MachineOperand &MO = MI.getOperand(i);
Hal Finkel8acae522015-07-14 20:02:02 +0000140 if (!MO.isReg() || !MO.isDef() || MO.isImplicit())
141 continue;
142
143 int Cycle = ItinData->getOperandCycle(DefClass, i);
144 if (Cycle < 0)
145 continue;
146
147 Latency = std::max(Latency, (unsigned) Cycle);
148 }
149
150 return Latency;
151}
Hal Finkelceb1f122013-12-12 00:19:11 +0000152
153int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000154 const MachineInstr &DefMI, unsigned DefIdx,
155 const MachineInstr &UseMI,
Hal Finkelceb1f122013-12-12 00:19:11 +0000156 unsigned UseIdx) const {
157 int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
158 UseMI, UseIdx);
159
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000160 if (!DefMI.getParent())
Hal Finkel5d36b232015-07-15 08:23:05 +0000161 return Latency;
162
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000163 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
Hal Finkelceb1f122013-12-12 00:19:11 +0000164 unsigned Reg = DefMO.getReg();
165
Hal Finkelceb1f122013-12-12 00:19:11 +0000166 bool IsRegCR;
Andrew Kaylor5c73e1f2015-03-24 23:37:10 +0000167 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Hal Finkelceb1f122013-12-12 00:19:11 +0000168 const MachineRegisterInfo *MRI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000169 &DefMI.getParent()->getParent()->getRegInfo();
Hal Finkelceb1f122013-12-12 00:19:11 +0000170 IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) ||
171 MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass);
172 } else {
173 IsRegCR = PPC::CRRCRegClass.contains(Reg) ||
174 PPC::CRBITRCRegClass.contains(Reg);
175 }
176
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000177 if (UseMI.isBranch() && IsRegCR) {
Hal Finkelceb1f122013-12-12 00:19:11 +0000178 if (Latency < 0)
179 Latency = getInstrLatency(ItinData, DefMI);
180
181 // On some cores, there is an additional delay between writing to a condition
182 // register, and using it from a branch.
Eric Christopher1dcea732014-06-12 21:48:52 +0000183 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000184 switch (Directive) {
185 default: break;
186 case PPC::DIR_7400:
187 case PPC::DIR_750:
188 case PPC::DIR_970:
189 case PPC::DIR_E5500:
190 case PPC::DIR_PWR4:
191 case PPC::DIR_PWR5:
192 case PPC::DIR_PWR5X:
193 case PPC::DIR_PWR6:
194 case PPC::DIR_PWR6X:
195 case PPC::DIR_PWR7:
Will Schmidt970ff642014-06-26 13:36:19 +0000196 case PPC::DIR_PWR8:
Nemanja Ivanovic6e29baf2016-05-09 18:54:58 +0000197 // FIXME: Is this needed for POWER9?
Hal Finkelceb1f122013-12-12 00:19:11 +0000198 Latency += 2;
199 break;
200 }
201 }
202
203 return Latency;
204}
205
Hal Finkel5d36b232015-07-15 08:23:05 +0000206// This function does not list all associative and commutative operations, but
207// only those worth feeding through the machine combiner in an attempt to
208// reduce the critical path. Mostly, this means floating-point operations,
209// because they have high latencies (compared to other operations, such and
210// and/or, which are also associative and commutative, but have low latencies).
Chad Rosier03a47302015-09-21 15:09:11 +0000211bool PPCInstrInfo::isAssociativeAndCommutative(const MachineInstr &Inst) const {
212 switch (Inst.getOpcode()) {
Hal Finkel5d36b232015-07-15 08:23:05 +0000213 // FP Add:
214 case PPC::FADD:
215 case PPC::FADDS:
216 // FP Multiply:
217 case PPC::FMUL:
218 case PPC::FMULS:
219 // Altivec Add:
220 case PPC::VADDFP:
221 // VSX Add:
222 case PPC::XSADDDP:
223 case PPC::XVADDDP:
224 case PPC::XVADDSP:
225 case PPC::XSADDSP:
226 // VSX Multiply:
227 case PPC::XSMULDP:
228 case PPC::XVMULDP:
229 case PPC::XVMULSP:
230 case PPC::XSMULSP:
231 // QPX Add:
232 case PPC::QVFADD:
233 case PPC::QVFADDS:
234 case PPC::QVFADDSs:
235 // QPX Multiply:
236 case PPC::QVFMUL:
237 case PPC::QVFMULS:
238 case PPC::QVFMULSs:
239 return true;
240 default:
241 return false;
242 }
243}
244
Chad Rosier03a47302015-09-21 15:09:11 +0000245bool PPCInstrInfo::getMachineCombinerPatterns(
246 MachineInstr &Root,
Sanjay Patel387e66e2015-11-05 19:34:57 +0000247 SmallVectorImpl<MachineCombinerPattern> &Patterns) const {
Hal Finkel5d36b232015-07-15 08:23:05 +0000248 // Using the machine combiner in this way is potentially expensive, so
249 // restrict to when aggressive optimizations are desired.
250 if (Subtarget.getTargetMachine().getOptLevel() != CodeGenOpt::Aggressive)
251 return false;
252
253 // FP reassociation is only legal when we don't need strict IEEE semantics.
254 if (!Root.getParent()->getParent()->getTarget().Options.UnsafeFPMath)
255 return false;
256
Chad Rosier03a47302015-09-21 15:09:11 +0000257 return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns);
Hal Finkel5d36b232015-07-15 08:23:05 +0000258}
259
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000260// Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
261bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
262 unsigned &SrcReg, unsigned &DstReg,
263 unsigned &SubIdx) const {
264 switch (MI.getOpcode()) {
265 default: return false;
266 case PPC::EXTSW:
Hiroshi Inouee3a3e3c2017-10-16 04:12:57 +0000267 case PPC::EXTSW_32:
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000268 case PPC::EXTSW_32_64:
269 SrcReg = MI.getOperand(1).getReg();
270 DstReg = MI.getOperand(0).getReg();
271 SubIdx = PPC::sub_32;
272 return true;
273 }
274}
275
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000276unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Chris Lattner91400bd2006-03-16 22:24:02 +0000277 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000278 // Note: This list must be kept consistent with LoadRegFromStackSlot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000279 switch (MI.getOpcode()) {
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000280 default: break;
281 case PPC::LD:
282 case PPC::LWZ:
283 case PPC::LFS:
284 case PPC::LFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000285 case PPC::RESTORE_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000286 case PPC::RESTORE_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000287 case PPC::LVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000288 case PPC::LXVD2X:
Lei Huang263dc4e2017-10-11 20:20:58 +0000289 case PPC::LXV:
Hal Finkelc93a9a22015-02-25 01:06:45 +0000290 case PPC::QVLFDX:
291 case PPC::QVLFSXs:
292 case PPC::QVLFDXb:
Hal Finkel37714b82013-03-27 21:21:15 +0000293 case PPC::RESTORE_VRSAVE:
Zaara Syedafcd96972017-09-21 16:12:33 +0000294 case PPC::SPILLTOVSR_LD:
Hal Finkel37714b82013-03-27 21:21:15 +0000295 // Check for the operands added by addFrameReference (the immediate is the
296 // offset which defaults to 0).
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000297 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() &&
298 MI.getOperand(2).isFI()) {
299 FrameIndex = MI.getOperand(2).getIndex();
300 return MI.getOperand(0).getReg();
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000301 }
302 break;
303 }
304 return 0;
Chris Lattnerc327d712006-02-02 20:16:12 +0000305}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000306
Lei Huang84dbbfd2017-06-21 17:17:56 +0000307// For opcodes with the ReMaterializable flag set, this function is called to
308// verify the instruction is really rematable.
309bool PPCInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr &MI,
310 AliasAnalysis *AA) const {
311 switch (MI.getOpcode()) {
312 default:
313 // This function should only be called for opcodes with the ReMaterializable
314 // flag set.
315 llvm_unreachable("Unknown rematerializable operation!");
316 break;
317 case PPC::LI:
318 case PPC::LI8:
319 case PPC::LIS:
320 case PPC::LIS8:
321 case PPC::QVGPCI:
322 case PPC::ADDIStocHA:
323 case PPC::ADDItocL:
324 case PPC::LOAD_STACK_GUARD:
325 return true;
326 }
327 return false;
328}
329
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000330unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Chris Lattnerc327d712006-02-02 20:16:12 +0000331 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000332 // Note: This list must be kept consistent with StoreRegToStackSlot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000333 switch (MI.getOpcode()) {
Chris Lattnerc327d712006-02-02 20:16:12 +0000334 default: break;
Nate Begeman4efb3282006-02-02 21:07:50 +0000335 case PPC::STD:
Chris Lattnerc327d712006-02-02 20:16:12 +0000336 case PPC::STW:
337 case PPC::STFS:
338 case PPC::STFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000339 case PPC::SPILL_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000340 case PPC::SPILL_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000341 case PPC::STVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000342 case PPC::STXVD2X:
Lei Huang263dc4e2017-10-11 20:20:58 +0000343 case PPC::STXV:
Hal Finkelc93a9a22015-02-25 01:06:45 +0000344 case PPC::QVSTFDX:
345 case PPC::QVSTFSXs:
346 case PPC::QVSTFDXb:
Hal Finkel37714b82013-03-27 21:21:15 +0000347 case PPC::SPILL_VRSAVE:
Zaara Syedafcd96972017-09-21 16:12:33 +0000348 case PPC::SPILLTOVSR_ST:
Hal Finkel37714b82013-03-27 21:21:15 +0000349 // Check for the operands added by addFrameReference (the immediate is the
350 // offset which defaults to 0).
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000351 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() &&
352 MI.getOperand(2).isFI()) {
353 FrameIndex = MI.getOperand(2).getIndex();
354 return MI.getOperand(0).getReg();
Chris Lattnerc327d712006-02-02 20:16:12 +0000355 }
356 break;
357 }
358 return 0;
359}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000360
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000361MachineInstr *PPCInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI,
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000362 unsigned OpIdx1,
363 unsigned OpIdx2) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000364 MachineFunction &MF = *MI.getParent()->getParent();
Dan Gohman3b460302008-07-07 23:14:23 +0000365
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000366 // Normal instructions can be commuted the obvious way.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000367 if (MI.getOpcode() != PPC::RLWIMI && MI.getOpcode() != PPC::RLWIMIo)
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000368 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
Hal Finkel4c6658f2014-12-12 23:59:36 +0000369 // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a
370 // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because
371 // changing the relative order of the mask operands might change what happens
372 // to the high-bits of the mask (and, thus, the result).
Andrew Trickc416ba62010-12-24 04:28:06 +0000373
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000374 // Cannot commute if it has a non-zero rotate count.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000375 if (MI.getOperand(3).getImm() != 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000376 return nullptr;
Andrew Trickc416ba62010-12-24 04:28:06 +0000377
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000378 // If we have a zero rotate count, we have:
379 // M = mask(MB,ME)
380 // Op0 = (Op1 & ~M) | (Op2 & M)
381 // Change this to:
382 // M = mask((ME+1)&31, (MB-1)&31)
383 // Op0 = (Op2 & ~M) | (Op1 & M)
384
385 // Swap op1/op2
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000386 assert(((OpIdx1 == 1 && OpIdx2 == 2) || (OpIdx1 == 2 && OpIdx2 == 1)) &&
387 "Only the operands 1 and 2 can be swapped in RLSIMI/RLWIMIo.");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000388 unsigned Reg0 = MI.getOperand(0).getReg();
389 unsigned Reg1 = MI.getOperand(1).getReg();
390 unsigned Reg2 = MI.getOperand(2).getReg();
391 unsigned SubReg1 = MI.getOperand(1).getSubReg();
392 unsigned SubReg2 = MI.getOperand(2).getSubReg();
393 bool Reg1IsKill = MI.getOperand(1).isKill();
394 bool Reg2IsKill = MI.getOperand(2).isKill();
Evan Cheng03553bb2008-06-16 07:33:11 +0000395 bool ChangeReg0 = false;
Evan Cheng244183e2008-02-13 02:46:49 +0000396 // If machine instrs are no longer in two-address forms, update
397 // destination register as well.
398 if (Reg0 == Reg1) {
399 // Must be two address instruction!
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000400 assert(MI.getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Cheng244183e2008-02-13 02:46:49 +0000401 "Expecting a two-address instruction!");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000402 assert(MI.getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
Evan Cheng244183e2008-02-13 02:46:49 +0000403 Reg2IsKill = false;
Evan Cheng03553bb2008-06-16 07:33:11 +0000404 ChangeReg0 = true;
Evan Cheng244183e2008-02-13 02:46:49 +0000405 }
Evan Cheng03553bb2008-06-16 07:33:11 +0000406
407 // Masks.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000408 unsigned MB = MI.getOperand(4).getImm();
409 unsigned ME = MI.getOperand(5).getImm();
Evan Cheng03553bb2008-06-16 07:33:11 +0000410
Hal Finkelccf92592015-09-06 04:17:30 +0000411 // We can't commute a trivial mask (there is no way to represent an all-zero
412 // mask).
413 if (MB == 0 && ME == 31)
414 return nullptr;
415
Evan Cheng03553bb2008-06-16 07:33:11 +0000416 if (NewMI) {
417 // Create a new instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000418 unsigned Reg0 = ChangeReg0 ? Reg2 : MI.getOperand(0).getReg();
419 bool Reg0IsDead = MI.getOperand(0).isDead();
420 return BuildMI(MF, MI.getDebugLoc(), MI.getDesc())
421 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
422 .addReg(Reg2, getKillRegState(Reg2IsKill))
423 .addReg(Reg1, getKillRegState(Reg1IsKill))
424 .addImm((ME + 1) & 31)
425 .addImm((MB - 1) & 31);
Evan Cheng03553bb2008-06-16 07:33:11 +0000426 }
427
Andrew Tricke3398282013-12-17 04:50:45 +0000428 if (ChangeReg0) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000429 MI.getOperand(0).setReg(Reg2);
430 MI.getOperand(0).setSubReg(SubReg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000431 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000432 MI.getOperand(2).setReg(Reg1);
433 MI.getOperand(1).setReg(Reg2);
434 MI.getOperand(2).setSubReg(SubReg1);
435 MI.getOperand(1).setSubReg(SubReg2);
436 MI.getOperand(2).setIsKill(Reg1IsKill);
437 MI.getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000438
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000439 // Swap the mask around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000440 MI.getOperand(4).setImm((ME + 1) & 31);
441 MI.getOperand(5).setImm((MB - 1) & 31);
442 return &MI;
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000443}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000444
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000445bool PPCInstrInfo::findCommutedOpIndices(MachineInstr &MI, unsigned &SrcOpIdx1,
Hal Finkel6c32ff32014-03-25 19:26:43 +0000446 unsigned &SrcOpIdx2) const {
447 // For VSX A-Type FMA instructions, it is the first two operands that can be
448 // commuted, however, because the non-encoded tied input operand is listed
449 // first, the operands to swap are actually the second and third.
450
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000451 int AltOpc = PPC::getAltVSXFMAOpcode(MI.getOpcode());
Hal Finkel6c32ff32014-03-25 19:26:43 +0000452 if (AltOpc == -1)
453 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
454
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000455 // The commutable operand indices are 2 and 3. Return them in SrcOpIdx1
456 // and SrcOpIdx2.
457 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2, 3);
Hal Finkel6c32ff32014-03-25 19:26:43 +0000458}
459
Andrew Trickc416ba62010-12-24 04:28:06 +0000460void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000461 MachineBasicBlock::iterator MI) const {
Hal Finkelceb1f122013-12-12 00:19:11 +0000462 // This function is used for scheduling, and the nop wanted here is the type
463 // that terminates dispatch groups on the POWER cores.
Eric Christopher1dcea732014-06-12 21:48:52 +0000464 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000465 unsigned Opcode;
466 switch (Directive) {
467 default: Opcode = PPC::NOP; break;
468 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
469 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
Will Schmidt970ff642014-06-26 13:36:19 +0000470 case PPC::DIR_PWR8: Opcode = PPC::NOP_GT_PWR7; break; /* FIXME: Update when P8 InstrScheduling model is ready */
Nemanja Ivanovic6e29baf2016-05-09 18:54:58 +0000471 // FIXME: Update when POWER9 scheduling model is ready.
472 case PPC::DIR_PWR9: Opcode = PPC::NOP_GT_PWR7; break;
Hal Finkelceb1f122013-12-12 00:19:11 +0000473 }
Chris Lattnera47294ed2006-10-13 21:21:17 +0000474
Hal Finkelceb1f122013-12-12 00:19:11 +0000475 DebugLoc DL;
476 BuildMI(MBB, MI, DL, get(Opcode));
477}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000478
Hans Wennborg9b9a5352017-04-21 21:48:41 +0000479/// Return the noop instruction to use for a noop.
480void PPCInstrInfo::getNoop(MCInst &NopInst) const {
Joerg Sonnenberger7ee0f312014-08-08 19:13:23 +0000481 NopInst.setOpcode(PPC::NOP);
482}
483
Chris Lattnera47294ed2006-10-13 21:21:17 +0000484// Branch analysis.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000485// Note: If the condition register is set to CTR or CTR8 then this is a
486// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000487bool PPCInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
488 MachineBasicBlock *&TBB,
Chris Lattnera47294ed2006-10-13 21:21:17 +0000489 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000490 SmallVectorImpl<MachineOperand> &Cond,
491 bool AllowModify) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000492 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000493
Chris Lattnera47294ed2006-10-13 21:21:17 +0000494 // If the block has no terminators, it just falls into the block after it.
Benjamin Kramer92861d72015-06-25 13:39:03 +0000495 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
496 if (I == MBB.end())
Dale Johannesen4244d122010-04-02 01:38:09 +0000497 return false;
Benjamin Kramer92861d72015-06-25 13:39:03 +0000498
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000499 if (!isUnpredicatedTerminator(*I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000500 return false;
501
Hiroshi Inoueed1ffa42017-09-27 10:33:02 +0000502 if (AllowModify) {
503 // If the BB ends with an unconditional branch to the fallthrough BB,
504 // we eliminate the branch instruction.
505 if (I->getOpcode() == PPC::B &&
506 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
507 I->eraseFromParent();
508
509 // We update iterator after deleting the last branch.
510 I = MBB.getLastNonDebugInstr();
511 if (I == MBB.end() || !isUnpredicatedTerminator(*I))
512 return false;
513 }
514 }
515
Chris Lattnera47294ed2006-10-13 21:21:17 +0000516 // Get the last instruction in the block.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000517 MachineInstr &LastInst = *I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000518
Chris Lattnera47294ed2006-10-13 21:21:17 +0000519 // If there is only one terminator instruction, process it.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000520 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000521 if (LastInst.getOpcode() == PPC::B) {
522 if (!LastInst.getOperand(0).isMBB())
Evan Cheng8f43afd2009-05-08 23:09:25 +0000523 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000524 TBB = LastInst.getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000525 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000526 } else if (LastInst.getOpcode() == PPC::BCC) {
527 if (!LastInst.getOperand(2).isMBB())
Evan Cheng8f43afd2009-05-08 23:09:25 +0000528 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000529 // Block ends with fall-through condbranch.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000530 TBB = LastInst.getOperand(2).getMBB();
531 Cond.push_back(LastInst.getOperand(0));
532 Cond.push_back(LastInst.getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000533 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000534 } else if (LastInst.getOpcode() == PPC::BC) {
535 if (!LastInst.getOperand(1).isMBB())
Hal Finkel940ab932014-02-28 00:27:01 +0000536 return true;
537 // Block ends with fall-through condbranch.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000538 TBB = LastInst.getOperand(1).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000539 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000540 Cond.push_back(LastInst.getOperand(0));
Hal Finkel940ab932014-02-28 00:27:01 +0000541 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000542 } else if (LastInst.getOpcode() == PPC::BCn) {
543 if (!LastInst.getOperand(1).isMBB())
Hal Finkel940ab932014-02-28 00:27:01 +0000544 return true;
545 // Block ends with fall-through condbranch.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000546 TBB = LastInst.getOperand(1).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000547 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000548 Cond.push_back(LastInst.getOperand(0));
Hal Finkel940ab932014-02-28 00:27:01 +0000549 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000550 } else if (LastInst.getOpcode() == PPC::BDNZ8 ||
551 LastInst.getOpcode() == PPC::BDNZ) {
552 if (!LastInst.getOperand(0).isMBB())
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000553 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000554 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000555 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000556 TBB = LastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000557 Cond.push_back(MachineOperand::CreateImm(1));
558 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
559 true));
560 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000561 } else if (LastInst.getOpcode() == PPC::BDZ8 ||
562 LastInst.getOpcode() == PPC::BDZ) {
563 if (!LastInst.getOperand(0).isMBB())
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000564 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000565 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000566 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000567 TBB = LastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000568 Cond.push_back(MachineOperand::CreateImm(0));
569 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
570 true));
571 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000572 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000573
Chris Lattnera47294ed2006-10-13 21:21:17 +0000574 // Otherwise, don't know what this is.
575 return true;
576 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000577
Chris Lattnera47294ed2006-10-13 21:21:17 +0000578 // Get the instruction before it if it's a terminator.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000579 MachineInstr &SecondLastInst = *I;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000580
581 // If there are three terminators, we don't know what sort of block this is.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000582 if (I != MBB.begin() && isUnpredicatedTerminator(*--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000583 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000584
Chris Lattnere0263792006-11-17 22:14:47 +0000585 // If the block ends with PPC::B and PPC:BCC, handle it.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000586 if (SecondLastInst.getOpcode() == PPC::BCC &&
587 LastInst.getOpcode() == PPC::B) {
588 if (!SecondLastInst.getOperand(2).isMBB() ||
589 !LastInst.getOperand(0).isMBB())
Evan Cheng8f43afd2009-05-08 23:09:25 +0000590 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000591 TBB = SecondLastInst.getOperand(2).getMBB();
592 Cond.push_back(SecondLastInst.getOperand(0));
593 Cond.push_back(SecondLastInst.getOperand(1));
594 FBB = LastInst.getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000595 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000596 } else if (SecondLastInst.getOpcode() == PPC::BC &&
597 LastInst.getOpcode() == PPC::B) {
598 if (!SecondLastInst.getOperand(1).isMBB() ||
599 !LastInst.getOperand(0).isMBB())
Hal Finkel940ab932014-02-28 00:27:01 +0000600 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000601 TBB = SecondLastInst.getOperand(1).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000602 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000603 Cond.push_back(SecondLastInst.getOperand(0));
604 FBB = LastInst.getOperand(0).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000605 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000606 } else if (SecondLastInst.getOpcode() == PPC::BCn &&
607 LastInst.getOpcode() == PPC::B) {
608 if (!SecondLastInst.getOperand(1).isMBB() ||
609 !LastInst.getOperand(0).isMBB())
Hal Finkel940ab932014-02-28 00:27:01 +0000610 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000611 TBB = SecondLastInst.getOperand(1).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000612 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000613 Cond.push_back(SecondLastInst.getOperand(0));
614 FBB = LastInst.getOperand(0).getMBB();
Hal Finkel940ab932014-02-28 00:27:01 +0000615 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000616 } else if ((SecondLastInst.getOpcode() == PPC::BDNZ8 ||
617 SecondLastInst.getOpcode() == PPC::BDNZ) &&
618 LastInst.getOpcode() == PPC::B) {
619 if (!SecondLastInst.getOperand(0).isMBB() ||
620 !LastInst.getOperand(0).isMBB())
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000621 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000622 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000623 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000624 TBB = SecondLastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000625 Cond.push_back(MachineOperand::CreateImm(1));
626 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
627 true));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000628 FBB = LastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000629 return false;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000630 } else if ((SecondLastInst.getOpcode() == PPC::BDZ8 ||
631 SecondLastInst.getOpcode() == PPC::BDZ) &&
632 LastInst.getOpcode() == PPC::B) {
633 if (!SecondLastInst.getOperand(0).isMBB() ||
634 !LastInst.getOperand(0).isMBB())
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000635 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000636 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000637 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000638 TBB = SecondLastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000639 Cond.push_back(MachineOperand::CreateImm(0));
640 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
641 true));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000642 FBB = LastInst.getOperand(0).getMBB();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000643 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000644 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000645
Dale Johannesenc6855462007-06-13 17:59:52 +0000646 // If the block ends with two PPC:Bs, handle it. The second one is not
647 // executed, so remove it.
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000648 if (SecondLastInst.getOpcode() == PPC::B && LastInst.getOpcode() == PPC::B) {
649 if (!SecondLastInst.getOperand(0).isMBB())
Evan Cheng8f43afd2009-05-08 23:09:25 +0000650 return true;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000651 TBB = SecondLastInst.getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000652 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000653 if (AllowModify)
654 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000655 return false;
656 }
657
Chris Lattnera47294ed2006-10-13 21:21:17 +0000658 // Otherwise, can't handle this.
659 return true;
660}
661
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000662unsigned PPCInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000663 int *BytesRemoved) const {
664 assert(!BytesRemoved && "code size not handled");
665
Benjamin Kramer92861d72015-06-25 13:39:03 +0000666 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
667 if (I == MBB.end())
668 return 0;
669
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000670 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000671 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000672 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
673 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000674 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000675
Chris Lattnera47294ed2006-10-13 21:21:17 +0000676 // Remove the branch.
677 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000678
Chris Lattnera47294ed2006-10-13 21:21:17 +0000679 I = MBB.end();
680
Evan Cheng99be49d2007-05-18 00:05:48 +0000681 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000682 --I;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000683 if (I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000684 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000685 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
686 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000687 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000688
Chris Lattnera47294ed2006-10-13 21:21:17 +0000689 // Remove the branch.
690 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000691 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000692}
693
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000694unsigned PPCInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000695 MachineBasicBlock *TBB,
696 MachineBasicBlock *FBB,
697 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000698 const DebugLoc &DL,
699 int *BytesAdded) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000700 // Shouldn't be a fall through.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000701 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000702 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000703 "PPC branch conditions have two components!");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000704 assert(!BytesAdded && "code size not handled");
Andrew Trickc416ba62010-12-24 04:28:06 +0000705
Eric Christopher1dcea732014-06-12 21:48:52 +0000706 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000707
Chris Lattner94e04442006-10-21 05:36:13 +0000708 // One-way branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000709 if (!FBB) {
Chris Lattner94e04442006-10-21 05:36:13 +0000710 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000711 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000712 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
713 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
714 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
715 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000716 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
Diana Picus116bbab2017-01-13 09:58:52 +0000717 BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000718 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
Diana Picus116bbab2017-01-13 09:58:52 +0000719 BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000720 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000721 BuildMI(&MBB, DL, get(PPC::BCC))
Diana Picus116bbab2017-01-13 09:58:52 +0000722 .addImm(Cond[0].getImm())
723 .add(Cond[1])
724 .addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000725 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000726 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000727
Chris Lattnerd8816602006-10-21 05:42:09 +0000728 // Two-way Conditional Branch.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000729 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
730 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
731 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
732 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000733 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
Diana Picus116bbab2017-01-13 09:58:52 +0000734 BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000735 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
Diana Picus116bbab2017-01-13 09:58:52 +0000736 BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000737 else
738 BuildMI(&MBB, DL, get(PPC::BCC))
Diana Picus116bbab2017-01-13 09:58:52 +0000739 .addImm(Cond[0].getImm())
740 .add(Cond[1])
741 .addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000742 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000743 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000744}
745
Hal Finkeled6a2852013-04-05 23:29:01 +0000746// Select analysis.
747bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000748 ArrayRef<MachineOperand> Cond,
Hal Finkeled6a2852013-04-05 23:29:01 +0000749 unsigned TrueReg, unsigned FalseReg,
750 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
Hal Finkeled6a2852013-04-05 23:29:01 +0000751 if (Cond.size() != 2)
752 return false;
753
754 // If this is really a bdnz-like condition, then it cannot be turned into a
755 // select.
756 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
757 return false;
758
759 // Check register classes.
760 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
761 const TargetRegisterClass *RC =
762 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
763 if (!RC)
764 return false;
765
766 // isel is for regular integer GPRs only.
767 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkel8e8618a2013-07-15 20:22:58 +0000768 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
769 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
770 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkeled6a2852013-04-05 23:29:01 +0000771 return false;
772
773 // FIXME: These numbers are for the A2, how well they work for other cores is
774 // an open question. On the A2, the isel instruction has a 2-cycle latency
775 // but single-cycle throughput. These numbers are used in combination with
776 // the MispredictPenalty setting from the active SchedMachineModel.
777 CondCycles = 1;
778 TrueCycles = 1;
779 FalseCycles = 1;
780
781 return true;
782}
783
784void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000785 MachineBasicBlock::iterator MI,
786 const DebugLoc &dl, unsigned DestReg,
787 ArrayRef<MachineOperand> Cond, unsigned TrueReg,
788 unsigned FalseReg) const {
Hal Finkeled6a2852013-04-05 23:29:01 +0000789 assert(Cond.size() == 2 &&
790 "PPC branch conditions have two components!");
791
Hal Finkeled6a2852013-04-05 23:29:01 +0000792 // Get the register classes.
793 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
794 const TargetRegisterClass *RC =
795 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
796 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkel8e8618a2013-07-15 20:22:58 +0000797
798 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
799 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
800 assert((Is64Bit ||
801 PPC::GPRCRegClass.hasSubClassEq(RC) ||
802 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000803 "isel is for regular integer GPRs only");
804
Hal Finkel8e8618a2013-07-15 20:22:58 +0000805 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Kyle Buttcec40802016-01-12 21:00:43 +0000806 auto SelectPred = static_cast<PPC::Predicate>(Cond[0].getImm());
Hal Finkeled6a2852013-04-05 23:29:01 +0000807
Kyle Butt132bf362016-01-15 19:20:06 +0000808 unsigned SubIdx = 0;
809 bool SwapOps = false;
Hal Finkeled6a2852013-04-05 23:29:01 +0000810 switch (SelectPred) {
Kyle Buttcec40802016-01-12 21:00:43 +0000811 case PPC::PRED_EQ:
812 case PPC::PRED_EQ_MINUS:
813 case PPC::PRED_EQ_PLUS:
814 SubIdx = PPC::sub_eq; SwapOps = false; break;
815 case PPC::PRED_NE:
816 case PPC::PRED_NE_MINUS:
817 case PPC::PRED_NE_PLUS:
818 SubIdx = PPC::sub_eq; SwapOps = true; break;
819 case PPC::PRED_LT:
820 case PPC::PRED_LT_MINUS:
821 case PPC::PRED_LT_PLUS:
822 SubIdx = PPC::sub_lt; SwapOps = false; break;
823 case PPC::PRED_GE:
824 case PPC::PRED_GE_MINUS:
825 case PPC::PRED_GE_PLUS:
826 SubIdx = PPC::sub_lt; SwapOps = true; break;
827 case PPC::PRED_GT:
828 case PPC::PRED_GT_MINUS:
829 case PPC::PRED_GT_PLUS:
830 SubIdx = PPC::sub_gt; SwapOps = false; break;
831 case PPC::PRED_LE:
832 case PPC::PRED_LE_MINUS:
833 case PPC::PRED_LE_PLUS:
834 SubIdx = PPC::sub_gt; SwapOps = true; break;
835 case PPC::PRED_UN:
836 case PPC::PRED_UN_MINUS:
837 case PPC::PRED_UN_PLUS:
838 SubIdx = PPC::sub_un; SwapOps = false; break;
839 case PPC::PRED_NU:
840 case PPC::PRED_NU_MINUS:
841 case PPC::PRED_NU_PLUS:
842 SubIdx = PPC::sub_un; SwapOps = true; break;
Hal Finkel940ab932014-02-28 00:27:01 +0000843 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break;
844 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
Hal Finkeled6a2852013-04-05 23:29:01 +0000845 }
846
847 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
848 SecondReg = SwapOps ? TrueReg : FalseReg;
849
850 // The first input register of isel cannot be r0. If it is a member
851 // of a register class that can be r0, then copy it first (the
852 // register allocator should eliminate the copy).
853 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
854 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
855 const TargetRegisterClass *FirstRC =
856 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
857 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
858 unsigned OldFirstReg = FirstReg;
859 FirstReg = MRI.createVirtualRegister(FirstRC);
860 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
861 .addReg(OldFirstReg);
862 }
863
864 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
865 .addReg(FirstReg).addReg(SecondReg)
866 .addReg(Cond[1].getReg(), 0, SubIdx);
867}
868
Kit Barton535e69d2015-03-25 19:36:23 +0000869static unsigned getCRBitValue(unsigned CRBit) {
870 unsigned Ret = 4;
871 if (CRBit == PPC::CR0LT || CRBit == PPC::CR1LT ||
872 CRBit == PPC::CR2LT || CRBit == PPC::CR3LT ||
873 CRBit == PPC::CR4LT || CRBit == PPC::CR5LT ||
874 CRBit == PPC::CR6LT || CRBit == PPC::CR7LT)
875 Ret = 3;
876 if (CRBit == PPC::CR0GT || CRBit == PPC::CR1GT ||
877 CRBit == PPC::CR2GT || CRBit == PPC::CR3GT ||
878 CRBit == PPC::CR4GT || CRBit == PPC::CR5GT ||
879 CRBit == PPC::CR6GT || CRBit == PPC::CR7GT)
880 Ret = 2;
881 if (CRBit == PPC::CR0EQ || CRBit == PPC::CR1EQ ||
882 CRBit == PPC::CR2EQ || CRBit == PPC::CR3EQ ||
883 CRBit == PPC::CR4EQ || CRBit == PPC::CR5EQ ||
884 CRBit == PPC::CR6EQ || CRBit == PPC::CR7EQ)
885 Ret = 1;
886 if (CRBit == PPC::CR0UN || CRBit == PPC::CR1UN ||
887 CRBit == PPC::CR2UN || CRBit == PPC::CR3UN ||
888 CRBit == PPC::CR4UN || CRBit == PPC::CR5UN ||
889 CRBit == PPC::CR6UN || CRBit == PPC::CR7UN)
890 Ret = 0;
891
892 assert(Ret != 4 && "Invalid CR bit register");
893 return Ret;
894}
895
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000896void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000897 MachineBasicBlock::iterator I,
898 const DebugLoc &DL, unsigned DestReg,
899 unsigned SrcReg, bool KillSrc) const {
Hal Finkel27774d92014-03-13 07:58:58 +0000900 // We can end up with self copies and similar things as a result of VSX copy
Hal Finkel9dcb3582014-03-27 22:46:28 +0000901 // legalization. Promote them here.
Hal Finkel27774d92014-03-13 07:58:58 +0000902 const TargetRegisterInfo *TRI = &getRegisterInfo();
903 if (PPC::F8RCRegClass.contains(DestReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000904 PPC::VSRCRegClass.contains(SrcReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000905 unsigned SuperReg =
906 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass);
907
Hal Finkel9dcb3582014-03-27 22:46:28 +0000908 if (VSXSelfCopyCrash && SrcReg == SuperReg)
909 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000910
911 DestReg = SuperReg;
Hal Finkel27774d92014-03-13 07:58:58 +0000912 } else if (PPC::F8RCRegClass.contains(SrcReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000913 PPC::VSRCRegClass.contains(DestReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000914 unsigned SuperReg =
915 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass);
916
Hal Finkel9dcb3582014-03-27 22:46:28 +0000917 if (VSXSelfCopyCrash && DestReg == SuperReg)
918 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000919
920 SrcReg = SuperReg;
Hal Finkel27774d92014-03-13 07:58:58 +0000921 }
922
Kit Barton535e69d2015-03-25 19:36:23 +0000923 // Different class register copy
924 if (PPC::CRBITRCRegClass.contains(SrcReg) &&
925 PPC::GPRCRegClass.contains(DestReg)) {
926 unsigned CRReg = getCRFromCRBit(SrcReg);
Richard Trieu7a083812016-02-18 22:09:30 +0000927 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(CRReg);
928 getKillRegState(KillSrc);
Kit Barton535e69d2015-03-25 19:36:23 +0000929 // Rotate the CR bit in the CR fields to be the least significant bit and
930 // then mask with 0x1 (MB = ME = 31).
931 BuildMI(MBB, I, DL, get(PPC::RLWINM), DestReg)
932 .addReg(DestReg, RegState::Kill)
933 .addImm(TRI->getEncodingValue(CRReg) * 4 + (4 - getCRBitValue(SrcReg)))
934 .addImm(31)
935 .addImm(31);
936 return;
937 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
938 PPC::G8RCRegClass.contains(DestReg)) {
Richard Trieu7a083812016-02-18 22:09:30 +0000939 BuildMI(MBB, I, DL, get(PPC::MFOCRF8), DestReg).addReg(SrcReg);
940 getKillRegState(KillSrc);
Kit Barton535e69d2015-03-25 19:36:23 +0000941 return;
942 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
943 PPC::GPRCRegClass.contains(DestReg)) {
Richard Trieu7a083812016-02-18 22:09:30 +0000944 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(SrcReg);
945 getKillRegState(KillSrc);
Kit Barton535e69d2015-03-25 19:36:23 +0000946 return;
Zaara Syedafcd96972017-09-21 16:12:33 +0000947 } else if (PPC::G8RCRegClass.contains(SrcReg) &&
948 PPC::VSFRCRegClass.contains(DestReg)) {
949 BuildMI(MBB, I, DL, get(PPC::MTVSRD), DestReg).addReg(SrcReg);
950 NumGPRtoVSRSpill++;
951 getKillRegState(KillSrc);
952 return;
953 } else if (PPC::VSFRCRegClass.contains(SrcReg) &&
954 PPC::G8RCRegClass.contains(DestReg)) {
955 BuildMI(MBB, I, DL, get(PPC::MFVSRD), DestReg).addReg(SrcReg);
956 getKillRegState(KillSrc);
957 return;
958 }
Kit Barton535e69d2015-03-25 19:36:23 +0000959
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000960 unsigned Opc;
961 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
962 Opc = PPC::OR;
963 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
964 Opc = PPC::OR8;
965 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
966 Opc = PPC::FMR;
967 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
968 Opc = PPC::MCRF;
969 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
970 Opc = PPC::VOR;
Hal Finkel27774d92014-03-13 07:58:58 +0000971 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg))
Hal Finkelbbad2332014-03-24 09:36:36 +0000972 // There are two different ways this can be done:
Hal Finkel27774d92014-03-13 07:58:58 +0000973 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
974 // issue in VSU pipeline 0.
975 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
976 // can go to either pipeline.
Hal Finkelbbad2332014-03-24 09:36:36 +0000977 // We'll always use xxlor here, because in practically all cases where
978 // copies are generated, they are close enough to some use that the
979 // lower-latency form is preferable.
Hal Finkel27774d92014-03-13 07:58:58 +0000980 Opc = PPC::XXLOR;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +0000981 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg) ||
982 PPC::VSSRCRegClass.contains(DestReg, SrcReg))
Hal Finkel19be5062014-03-29 05:29:01 +0000983 Opc = PPC::XXLORf;
Hal Finkelc93a9a22015-02-25 01:06:45 +0000984 else if (PPC::QFRCRegClass.contains(DestReg, SrcReg))
985 Opc = PPC::QVFMR;
986 else if (PPC::QSRCRegClass.contains(DestReg, SrcReg))
987 Opc = PPC::QVFMRs;
988 else if (PPC::QBRCRegClass.contains(DestReg, SrcReg))
989 Opc = PPC::QVFMRb;
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000990 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
991 Opc = PPC::CROR;
992 else
993 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +0000994
Evan Cheng6cc775f2011-06-28 19:10:37 +0000995 const MCInstrDesc &MCID = get(Opc);
996 if (MCID.getNumOperands() == 3)
997 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000998 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
999 else
Evan Cheng6cc775f2011-06-28 19:10:37 +00001000 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +00001001}
1002
Hal Finkel8f6834d2011-12-05 17:55:17 +00001003// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +00001004bool
Dan Gohman3b460302008-07-07 23:14:23 +00001005PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
1006 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +00001007 int FrameIdx,
1008 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +00001009 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001010 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +00001011 // Note: If additional store instructions are added here,
1012 // update isStoreToStackSlot.
1013
Chris Lattner6f306d72010-04-02 20:16:16 +00001014 DebugLoc DL;
Hal Finkel4e703bc2014-01-28 05:32:58 +00001015 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
1016 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +00001017 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
1018 .addReg(SrcReg,
1019 getKillRegState(isKill)),
1020 FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +00001021 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
1022 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +00001023 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
1024 .addReg(SrcReg,
1025 getKillRegState(isKill)),
1026 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001027 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +00001028 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +00001029 .addReg(SrcReg,
1030 getKillRegState(isKill)),
1031 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001032 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +00001033 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +00001034 .addReg(SrcReg,
1035 getKillRegState(isKill)),
1036 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001037 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +00001038 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
1039 .addReg(SrcReg,
1040 getKillRegState(isKill)),
1041 FrameIdx));
1042 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001043 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +00001044 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT))
1045 .addReg(SrcReg,
1046 getKillRegState(isKill)),
1047 FrameIdx));
1048 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001049 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +00001050 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
1051 .addReg(SrcReg,
1052 getKillRegState(isKill)),
1053 FrameIdx));
1054 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +00001055 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
Lei Huang263dc4e2017-10-11 20:20:58 +00001056 unsigned Op = Subtarget.hasP9Vector() ? PPC::STXV : PPC::STXVD2X;
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +00001057 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op))
Hal Finkel27774d92014-03-13 07:58:58 +00001058 .addReg(SrcReg,
1059 getKillRegState(isKill)),
1060 FrameIdx));
1061 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +00001062 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00001063 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFSTOREf64 : PPC::STXSDX;
1064 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc))
Hal Finkel19be5062014-03-29 05:29:01 +00001065 .addReg(SrcReg,
1066 getKillRegState(isKill)),
1067 FrameIdx));
1068 NonRI = true;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001069 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00001070 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFSTOREf32 : PPC::STXSSPX;
1071 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc))
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001072 .addReg(SrcReg,
1073 getKillRegState(isKill)),
1074 FrameIdx));
1075 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +00001076 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001077 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +00001078 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +00001079 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
1080 .addReg(SrcReg,
1081 getKillRegState(isKill)),
1082 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001083 SpillsVRS = true;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001084 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
1085 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDX))
1086 .addReg(SrcReg,
1087 getKillRegState(isKill)),
1088 FrameIdx));
1089 NonRI = true;
1090 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
1091 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFSXs))
1092 .addReg(SrcReg,
1093 getKillRegState(isKill)),
1094 FrameIdx));
1095 NonRI = true;
1096 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
1097 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDXb))
1098 .addReg(SrcReg,
1099 getKillRegState(isKill)),
1100 FrameIdx));
1101 NonRI = true;
Zaara Syedafcd96972017-09-21 16:12:33 +00001102 } else if (PPC::SPILLTOVSRRCRegClass.hasSubClassEq(RC)) {
1103 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILLTOVSR_ST))
1104 .addReg(SrcReg,
1105 getKillRegState(isKill)),
1106 FrameIdx));
Owen Andersoneee14602008-01-01 21:11:32 +00001107 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001108 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +00001109 }
Bill Wendling632ea652008-03-03 22:19:16 +00001110
1111 return false;
Owen Andersoneee14602008-01-01 21:11:32 +00001112}
1113
1114void
1115PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +00001116 MachineBasicBlock::iterator MI,
1117 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +00001118 const TargetRegisterClass *RC,
1119 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +00001120 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +00001121 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +00001122
Hal Finkelbb420f12013-03-15 05:06:04 +00001123 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1124 FuncInfo->setHasSpills();
1125
Nemanja Ivanovic11049f82016-10-04 06:59:23 +00001126 // We need to avoid a situation in which the value from a VRRC register is
1127 // spilled using an Altivec instruction and reloaded into a VSRC register
1128 // using a VSX instruction. The issue with this is that the VSX
1129 // load/store instructions swap the doublewords in the vector and the Altivec
1130 // ones don't. The register classes on the spill/reload may be different if
1131 // the register is defined using an Altivec instruction and is then used by a
1132 // VSX instruction.
1133 RC = updatedRC(RC);
1134
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001135 bool NonRI = false, SpillsVRS = false;
1136 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
1137 NonRI, SpillsVRS))
Bill Wendling632ea652008-03-03 22:19:16 +00001138 FuncInfo->setSpillsCR();
Bill Wendling632ea652008-03-03 22:19:16 +00001139
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001140 if (SpillsVRS)
1141 FuncInfo->setSpillsVRSAVE();
1142
Hal Finkelfcc51d42013-03-17 04:43:44 +00001143 if (NonRI)
1144 FuncInfo->setHasNonRISpills();
1145
Owen Andersoneee14602008-01-01 21:11:32 +00001146 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1147 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001148
Matthias Braun941a7052016-07-28 18:40:00 +00001149 const MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001150 MachineMemOperand *MMO = MF.getMachineMemOperand(
1151 MachinePointerInfo::getFixedStack(MF, FrameIdx),
1152 MachineMemOperand::MOStore, MFI.getObjectSize(FrameIdx),
1153 MFI.getObjectAlignment(FrameIdx));
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001154 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +00001155}
1156
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001157bool PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, const DebugLoc &DL,
1158 unsigned DestReg, int FrameIdx,
1159 const TargetRegisterClass *RC,
1160 SmallVectorImpl<MachineInstr *> &NewMIs,
1161 bool &NonRI, bool &SpillsVRS) const {
Hal Finkel37714b82013-03-27 21:21:15 +00001162 // Note: If additional load instructions are added here,
1163 // update isLoadFromStackSlot.
1164
Hal Finkel4e703bc2014-01-28 05:32:58 +00001165 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
1166 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +00001167 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
1168 DestReg), FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +00001169 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
1170 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +00001171 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
1172 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001173 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001174 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +00001175 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001176 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001177 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +00001178 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001179 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +00001180 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1181 get(PPC::RESTORE_CR), DestReg),
1182 FrameIdx));
1183 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001184 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +00001185 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1186 get(PPC::RESTORE_CRBIT), DestReg),
1187 FrameIdx));
1188 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001189 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +00001190 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
1191 FrameIdx));
1192 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +00001193 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
Lei Huang263dc4e2017-10-11 20:20:58 +00001194 unsigned Op = Subtarget.hasP9Vector() ? PPC::LXV : PPC::LXVD2X;
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +00001195 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op), DestReg),
Hal Finkel27774d92014-03-13 07:58:58 +00001196 FrameIdx));
1197 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +00001198 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00001199 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFLOADf64 : PPC::LXSDX;
1200 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc),
1201 DestReg), FrameIdx));
Hal Finkel19be5062014-03-29 05:29:01 +00001202 NonRI = true;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001203 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00001204 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFLOADf32 : PPC::LXSSPX;
1205 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc),
1206 DestReg), FrameIdx));
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001207 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +00001208 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001209 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +00001210 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +00001211 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1212 get(PPC::RESTORE_VRSAVE),
1213 DestReg),
1214 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001215 SpillsVRS = true;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001216 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
1217 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDX), DestReg),
1218 FrameIdx));
1219 NonRI = true;
1220 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
1221 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFSXs), DestReg),
1222 FrameIdx));
1223 NonRI = true;
1224 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
1225 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDXb), DestReg),
1226 FrameIdx));
1227 NonRI = true;
Zaara Syedafcd96972017-09-21 16:12:33 +00001228 } else if (PPC::SPILLTOVSRRCRegClass.hasSubClassEq(RC)) {
1229 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILLTOVSR_LD),
1230 DestReg), FrameIdx));
Owen Andersoneee14602008-01-01 21:11:32 +00001231 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001232 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +00001233 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001234
1235 return false;
Owen Andersoneee14602008-01-01 21:11:32 +00001236}
1237
1238void
1239PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +00001240 MachineBasicBlock::iterator MI,
1241 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +00001242 const TargetRegisterClass *RC,
1243 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +00001244 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +00001245 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +00001246 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001247 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelfcc51d42013-03-17 04:43:44 +00001248
1249 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1250 FuncInfo->setHasSpills();
1251
Nemanja Ivanovic11049f82016-10-04 06:59:23 +00001252 // We need to avoid a situation in which the value from a VRRC register is
1253 // spilled using an Altivec instruction and reloaded into a VSRC register
1254 // using a VSX instruction. The issue with this is that the VSX
1255 // load/store instructions swap the doublewords in the vector and the Altivec
1256 // ones don't. The register classes on the spill/reload may be different if
1257 // the register is defined using an Altivec instruction and is then used by a
1258 // VSX instruction.
1259 if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass)
1260 RC = &PPC::VSRCRegClass;
1261
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001262 bool NonRI = false, SpillsVRS = false;
1263 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
1264 NonRI, SpillsVRS))
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001265 FuncInfo->setSpillsCR();
Hal Finkelfcc51d42013-03-17 04:43:44 +00001266
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001267 if (SpillsVRS)
1268 FuncInfo->setSpillsVRSAVE();
1269
Hal Finkelfcc51d42013-03-17 04:43:44 +00001270 if (NonRI)
1271 FuncInfo->setHasNonRISpills();
1272
Owen Andersoneee14602008-01-01 21:11:32 +00001273 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1274 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001275
Matthias Braun941a7052016-07-28 18:40:00 +00001276 const MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001277 MachineMemOperand *MMO = MF.getMachineMemOperand(
1278 MachinePointerInfo::getFixedStack(MF, FrameIdx),
1279 MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIdx),
1280 MFI.getObjectAlignment(FrameIdx));
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001281 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +00001282}
1283
Chris Lattnera47294ed2006-10-13 21:21:17 +00001284bool PPCInstrInfo::
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001285reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +00001286 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel96c2d4d2012-06-08 15:38:21 +00001287 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
1288 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
1289 else
1290 // Leave the CR# the same, but invert the condition.
1291 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +00001292 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +00001293}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001294
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001295bool PPCInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
1296 unsigned Reg, MachineRegisterInfo *MRI) const {
Hal Finkeld61d4f82013-04-06 19:30:30 +00001297 // For some instructions, it is legal to fold ZERO into the RA register field.
1298 // A zero immediate should always be loaded with a single li.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001299 unsigned DefOpc = DefMI.getOpcode();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001300 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
1301 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001302 if (!DefMI.getOperand(1).isImm())
Hal Finkeld61d4f82013-04-06 19:30:30 +00001303 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001304 if (DefMI.getOperand(1).getImm() != 0)
Hal Finkeld61d4f82013-04-06 19:30:30 +00001305 return false;
1306
1307 // Note that we cannot here invert the arguments of an isel in order to fold
1308 // a ZERO into what is presented as the second argument. All we have here
1309 // is the condition bit, and that might come from a CR-logical bit operation.
1310
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001311 const MCInstrDesc &UseMCID = UseMI.getDesc();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001312
1313 // Only fold into real machine instructions.
1314 if (UseMCID.isPseudo())
1315 return false;
1316
1317 unsigned UseIdx;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001318 for (UseIdx = 0; UseIdx < UseMI.getNumOperands(); ++UseIdx)
1319 if (UseMI.getOperand(UseIdx).isReg() &&
1320 UseMI.getOperand(UseIdx).getReg() == Reg)
Hal Finkeld61d4f82013-04-06 19:30:30 +00001321 break;
1322
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001323 assert(UseIdx < UseMI.getNumOperands() && "Cannot find Reg in UseMI");
Hal Finkeld61d4f82013-04-06 19:30:30 +00001324 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
1325
1326 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
1327
1328 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
1329 // register (which might also be specified as a pointer class kind).
1330 if (UseInfo->isLookupPtrRegClass()) {
1331 if (UseInfo->RegClass /* Kind */ != 1)
1332 return false;
1333 } else {
1334 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
1335 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
1336 return false;
1337 }
1338
1339 // Make sure this is not tied to an output register (or otherwise
1340 // constrained). This is true for ST?UX registers, for example, which
1341 // are tied to their output registers.
1342 if (UseInfo->Constraints != 0)
1343 return false;
1344
1345 unsigned ZeroReg;
1346 if (UseInfo->isLookupPtrRegClass()) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001347 bool isPPC64 = Subtarget.isPPC64();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001348 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
1349 } else {
1350 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
1351 PPC::ZERO8 : PPC::ZERO;
1352 }
1353
1354 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001355 UseMI.getOperand(UseIdx).setReg(ZeroReg);
Hal Finkeld61d4f82013-04-06 19:30:30 +00001356
1357 if (DeleteDef)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001358 DefMI.eraseFromParent();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001359
1360 return true;
1361}
1362
Hal Finkel30ae2292013-04-10 18:30:16 +00001363static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
1364 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1365 I != IE; ++I)
1366 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
1367 return true;
1368 return false;
1369}
1370
1371// We should make sure that, if we're going to predicate both sides of a
1372// condition (a diamond), that both sides don't define the counter register. We
1373// can predicate counter-decrement-based branches, but while that predicates
1374// the branching, it does not predicate the counter decrement. If we tried to
1375// merge the triangle into one predicated block, we'd decrement the counter
1376// twice.
1377bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
1378 unsigned NumT, unsigned ExtraT,
1379 MachineBasicBlock &FMBB,
1380 unsigned NumF, unsigned ExtraF,
Cong Houc536bd92015-09-10 23:10:42 +00001381 BranchProbability Probability) const {
Hal Finkel30ae2292013-04-10 18:30:16 +00001382 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
1383}
1384
1385
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001386bool PPCInstrInfo::isPredicated(const MachineInstr &MI) const {
Hal Finkelf29285a2013-04-11 01:23:34 +00001387 // The predicated branches are identified by their type, not really by the
1388 // explicit presence of a predicate. Furthermore, some of them can be
1389 // predicated more than once. Because if conversion won't try to predicate
1390 // any instruction which already claims to be predicated (by returning true
1391 // here), always return false. In doing so, we let isPredicable() be the
1392 // final word on whether not the instruction can be (further) predicated.
1393
1394 return false;
Hal Finkel5711eca2013-04-09 22:58:37 +00001395}
1396
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001397bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const {
1398 if (!MI.isTerminator())
Hal Finkel5711eca2013-04-09 22:58:37 +00001399 return false;
1400
1401 // Conditional branch is a special case.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001402 if (MI.isBranch() && !MI.isBarrier())
Hal Finkel5711eca2013-04-09 22:58:37 +00001403 return true;
1404
1405 return !isPredicated(MI);
1406}
1407
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001408bool PPCInstrInfo::PredicateInstruction(MachineInstr &MI,
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001409 ArrayRef<MachineOperand> Pred) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001410 unsigned OpC = MI.getOpcode();
Hal Finkelf4a22c02015-01-13 17:47:54 +00001411 if (OpC == PPC::BLR || OpC == PPC::BLR8) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001412 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001413 bool isPPC64 = Subtarget.isPPC64();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001414 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR)
1415 : (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
Hal Finkel940ab932014-02-28 00:27:01 +00001416 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001417 MI.setDesc(get(PPC::BCLR));
1418 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1419 .addReg(Pred[1].getReg());
Hal Finkel940ab932014-02-28 00:27:01 +00001420 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001421 MI.setDesc(get(PPC::BCLRn));
1422 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1423 .addReg(Pred[1].getReg());
Hal Finkel940ab932014-02-28 00:27:01 +00001424 } else {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001425 MI.setDesc(get(PPC::BCCLR));
1426 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1427 .addImm(Pred[0].getImm())
1428 .addReg(Pred[1].getReg());
Hal Finkel5711eca2013-04-09 22:58:37 +00001429 }
1430
1431 return true;
1432 } else if (OpC == PPC::B) {
1433 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001434 bool isPPC64 = Subtarget.isPPC64();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001435 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ)
1436 : (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
Hal Finkel940ab932014-02-28 00:27:01 +00001437 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001438 MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1439 MI.RemoveOperand(0);
Hal Finkel940ab932014-02-28 00:27:01 +00001440
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001441 MI.setDesc(get(PPC::BC));
1442 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1443 .addReg(Pred[1].getReg())
1444 .addMBB(MBB);
Hal Finkel940ab932014-02-28 00:27:01 +00001445 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001446 MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1447 MI.RemoveOperand(0);
Hal Finkel940ab932014-02-28 00:27:01 +00001448
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001449 MI.setDesc(get(PPC::BCn));
1450 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1451 .addReg(Pred[1].getReg())
1452 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001453 } else {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001454 MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1455 MI.RemoveOperand(0);
Hal Finkel5711eca2013-04-09 22:58:37 +00001456
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001457 MI.setDesc(get(PPC::BCC));
1458 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1459 .addImm(Pred[0].getImm())
1460 .addReg(Pred[1].getReg())
1461 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001462 }
1463
1464 return true;
Hal Finkel500b0042013-04-10 06:42:34 +00001465 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1466 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1467 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1468 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1469
1470 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
Eric Christopher1dcea732014-06-12 21:48:52 +00001471 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel940ab932014-02-28 00:27:01 +00001472
1473 if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001474 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8)
1475 : (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1476 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1477 .addReg(Pred[1].getReg());
Hal Finkel940ab932014-02-28 00:27:01 +00001478 return true;
1479 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001480 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n)
1481 : (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1482 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1483 .addReg(Pred[1].getReg());
Hal Finkel940ab932014-02-28 00:27:01 +00001484 return true;
1485 }
1486
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001487 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8)
1488 : (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
1489 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1490 .addImm(Pred[0].getImm())
1491 .addReg(Pred[1].getReg());
Hal Finkel500b0042013-04-10 06:42:34 +00001492 return true;
Hal Finkel5711eca2013-04-09 22:58:37 +00001493 }
1494
1495 return false;
1496}
1497
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001498bool PPCInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1499 ArrayRef<MachineOperand> Pred2) const {
Hal Finkel5711eca2013-04-09 22:58:37 +00001500 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1501 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1502
1503 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1504 return false;
1505 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1506 return false;
1507
Hal Finkel94a6f382013-12-11 23:12:25 +00001508 // P1 can only subsume P2 if they test the same condition register.
1509 if (Pred1[1].getReg() != Pred2[1].getReg())
1510 return false;
1511
Hal Finkel5711eca2013-04-09 22:58:37 +00001512 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1513 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1514
1515 if (P1 == P2)
1516 return true;
1517
1518 // Does P1 subsume P2, e.g. GE subsumes GT.
1519 if (P1 == PPC::PRED_LE &&
1520 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1521 return true;
1522 if (P1 == PPC::PRED_GE &&
1523 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1524 return true;
1525
1526 return false;
1527}
1528
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001529bool PPCInstrInfo::DefinesPredicate(MachineInstr &MI,
Hal Finkel5711eca2013-04-09 22:58:37 +00001530 std::vector<MachineOperand> &Pred) const {
1531 // Note: At the present time, the contents of Pred from this function is
1532 // unused by IfConversion. This implementation follows ARM by pushing the
1533 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1534 // predicate, instructions defining CTR or CTR8 are also included as
1535 // predicate-defining instructions.
1536
1537 const TargetRegisterClass *RCs[] =
1538 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1539 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1540
1541 bool Found = false;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001542 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1543 const MachineOperand &MO = MI.getOperand(i);
Hal Finkelaf822012013-04-10 07:17:47 +00001544 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001545 const TargetRegisterClass *RC = RCs[c];
Hal Finkelaf822012013-04-10 07:17:47 +00001546 if (MO.isReg()) {
1547 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001548 Pred.push_back(MO);
1549 Found = true;
1550 }
Hal Finkelaf822012013-04-10 07:17:47 +00001551 } else if (MO.isRegMask()) {
1552 for (TargetRegisterClass::iterator I = RC->begin(),
1553 IE = RC->end(); I != IE; ++I)
1554 if (MO.clobbersPhysReg(*I)) {
1555 Pred.push_back(MO);
1556 Found = true;
1557 }
Hal Finkel5711eca2013-04-09 22:58:37 +00001558 }
1559 }
1560 }
1561
1562 return Found;
1563}
1564
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +00001565bool PPCInstrInfo::isPredicable(const MachineInstr &MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001566 unsigned OpC = MI.getOpcode();
Hal Finkel5711eca2013-04-09 22:58:37 +00001567 switch (OpC) {
1568 default:
1569 return false;
1570 case PPC::B:
1571 case PPC::BLR:
Hal Finkelf4a22c02015-01-13 17:47:54 +00001572 case PPC::BLR8:
Hal Finkel500b0042013-04-10 06:42:34 +00001573 case PPC::BCTR:
1574 case PPC::BCTR8:
1575 case PPC::BCTRL:
1576 case PPC::BCTRL8:
Hal Finkel5711eca2013-04-09 22:58:37 +00001577 return true;
1578 }
1579}
1580
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001581bool PPCInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1582 unsigned &SrcReg2, int &Mask,
1583 int &Value) const {
1584 unsigned Opc = MI.getOpcode();
Hal Finkel82656cb2013-04-18 22:15:08 +00001585
1586 switch (Opc) {
1587 default: return false;
1588 case PPC::CMPWI:
1589 case PPC::CMPLWI:
1590 case PPC::CMPDI:
1591 case PPC::CMPLDI:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001592 SrcReg = MI.getOperand(1).getReg();
Hal Finkel82656cb2013-04-18 22:15:08 +00001593 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001594 Value = MI.getOperand(2).getImm();
Hal Finkel82656cb2013-04-18 22:15:08 +00001595 Mask = 0xFFFF;
1596 return true;
1597 case PPC::CMPW:
1598 case PPC::CMPLW:
1599 case PPC::CMPD:
1600 case PPC::CMPLD:
1601 case PPC::FCMPUS:
1602 case PPC::FCMPUD:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001603 SrcReg = MI.getOperand(1).getReg();
1604 SrcReg2 = MI.getOperand(2).getReg();
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001605 Value = 0;
1606 Mask = 0;
Hal Finkel82656cb2013-04-18 22:15:08 +00001607 return true;
1608 }
1609}
Hal Finkele6322392013-04-19 22:08:38 +00001610
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001611bool PPCInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, unsigned SrcReg,
1612 unsigned SrcReg2, int Mask, int Value,
Hal Finkel82656cb2013-04-18 22:15:08 +00001613 const MachineRegisterInfo *MRI) const {
Hal Finkelb12da6b2013-04-18 22:54:25 +00001614 if (DisableCmpOpt)
1615 return false;
1616
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001617 int OpC = CmpInstr.getOpcode();
1618 unsigned CRReg = CmpInstr.getOperand(0).getReg();
Hal Finkel08e53ee2013-05-08 12:16:14 +00001619
1620 // FP record forms set CR1 based on the execption status bits, not a
1621 // comparison with zero.
1622 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1623 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001624
1625 // The record forms set the condition register based on a signed comparison
1626 // with zero (so says the ISA manual). This is not as straightforward as it
1627 // seems, however, because this is always a 64-bit comparison on PPC64, even
1628 // for instructions that are 32-bit in nature (like slw for example).
1629 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1630 // for equality checks (as those don't depend on the sign). On PPC64,
1631 // we are restricted to equality for unsigned 64-bit comparisons and for
1632 // signed 32-bit comparisons the applicability is more restricted.
Eric Christopher1dcea732014-06-12 21:48:52 +00001633 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel82656cb2013-04-18 22:15:08 +00001634 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1635 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1636 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1637
1638 // Get the unique definition of SrcReg.
1639 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1640 if (!MI) return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001641
1642 bool equalityOnly = false;
1643 bool noSub = false;
1644 if (isPPC64) {
1645 if (is32BitSignedCompare) {
1646 // We can perform this optimization only if MI is sign-extending.
Hiroshi Inoue5388e662017-10-18 10:31:19 +00001647 if (isSignExtended(*MI))
Hal Finkel82656cb2013-04-18 22:15:08 +00001648 noSub = true;
Hiroshi Inoue5388e662017-10-18 10:31:19 +00001649 else
Hal Finkel82656cb2013-04-18 22:15:08 +00001650 return false;
1651 } else if (is32BitUnsignedCompare) {
1652 // We can perform this optimization, equality only, if MI is
1653 // zero-extending.
Hiroshi Inoue5388e662017-10-18 10:31:19 +00001654 if (isZeroExtended(*MI)) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001655 noSub = true;
1656 equalityOnly = true;
1657 } else
1658 return false;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001659 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001660 equalityOnly = is64BitUnsignedCompare;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001661 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001662 equalityOnly = is32BitUnsignedCompare;
1663
1664 if (equalityOnly) {
1665 // We need to check the uses of the condition register in order to reject
1666 // non-equality comparisons.
Hiroshi Inoue393ef842017-07-18 13:31:40 +00001667 for (MachineRegisterInfo::use_instr_iterator
1668 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1669 I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001670 MachineInstr *UseMI = &*I;
1671 if (UseMI->getOpcode() == PPC::BCC) {
Hiroshi Inoue967dc582017-07-27 08:14:48 +00001672 PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm();
1673 unsigned PredCond = PPC::getPredicateCondition(Pred);
1674 // We ignore hint bits when checking for non-equality comparisons.
1675 if (PredCond != PPC::PRED_EQ && PredCond != PPC::PRED_NE)
Hal Finkelc3632452013-05-07 17:49:55 +00001676 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001677 } else if (UseMI->getOpcode() == PPC::ISEL ||
1678 UseMI->getOpcode() == PPC::ISEL8) {
1679 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkelc3632452013-05-07 17:49:55 +00001680 if (SubIdx != PPC::sub_eq)
1681 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001682 } else
1683 return false;
1684 }
1685 }
1686
Hal Finkelc3632452013-05-07 17:49:55 +00001687 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001688
1689 // Scan forward to find the first use of the compare.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001690 for (MachineBasicBlock::iterator EL = CmpInstr.getParent()->end(); I != EL;
1691 ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001692 bool FoundUse = false;
Hiroshi Inoue393ef842017-07-18 13:31:40 +00001693 for (MachineRegisterInfo::use_instr_iterator
1694 J = MRI->use_instr_begin(CRReg), JE = MRI->use_instr_end();
1695 J != JE; ++J)
Hal Finkel82656cb2013-04-18 22:15:08 +00001696 if (&*J == &*I) {
1697 FoundUse = true;
1698 break;
1699 }
1700
1701 if (FoundUse)
1702 break;
1703 }
1704
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001705 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1706 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
1707
Hal Finkel82656cb2013-04-18 22:15:08 +00001708 // There are two possible candidates which can be changed to set CR[01].
1709 // One is MI, the other is a SUB instruction.
1710 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
Craig Topper062a2ba2014-04-25 05:30:21 +00001711 MachineInstr *Sub = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001712 if (SrcReg2 != 0)
1713 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00001714 MI = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001715 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1716 // same BB as the comparison. This is to allow the check below to avoid calls
1717 // (and other explicit clobbers); instead we should really check for these
1718 // more explicitly (in at least a few predecessors).
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001719 else if (MI->getParent() != CmpInstr.getParent())
Hal Finkel82656cb2013-04-18 22:15:08 +00001720 return false;
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001721 else if (Value != 0) {
Hiroshi Inoueb72b1fb2017-10-26 09:01:51 +00001722 // The record-form instructions set CR bit based on signed comparison
1723 // against 0. We try to convert a compare against 1 or -1 into a compare
1724 // against 0 to exploit record-form instructions. For example, we change
1725 // the condition "greater than -1" into "greater than or equal to 0"
1726 // and "less than 1" into "less than or equal to 0".
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001727
Hiroshi Inoueb72b1fb2017-10-26 09:01:51 +00001728 // Since we optimize comparison based on a specific branch condition,
1729 // we don't optimize if condition code is used by more than once.
1730 if (equalityOnly || !MRI->hasOneUse(CRReg))
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001731 return false;
Hiroshi Inoueb72b1fb2017-10-26 09:01:51 +00001732
1733 MachineInstr *UseMI = &*MRI->use_instr_begin(CRReg);
1734 if (UseMI->getOpcode() != PPC::BCC)
1735 return false;
1736
1737 PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm();
1738 PPC::Predicate NewPred = Pred;
1739 unsigned PredCond = PPC::getPredicateCondition(Pred);
1740 unsigned PredHint = PPC::getPredicateHint(Pred);
1741 int16_t Immed = (int16_t)Value;
1742
1743 // When modyfing the condition in the predicate, we propagate hint bits
1744 // from the original predicate to the new one.
1745 if (Immed == -1 && PredCond == PPC::PRED_GT)
1746 // We convert "greater than -1" into "greater than or equal to 0",
1747 // since we are assuming signed comparison by !equalityOnly
1748 NewPred = PPC::getPredicate(PPC::PRED_GE, PredHint);
1749 else if (Immed == -1 && PredCond == PPC::PRED_LE)
1750 // We convert "less than or equal to -1" into "less than 0".
1751 NewPred = PPC::getPredicate(PPC::PRED_LT, PredHint);
1752 else if (Immed == 1 && PredCond == PPC::PRED_LT)
1753 // We convert "less than 1" into "less than or equal to 0".
1754 NewPred = PPC::getPredicate(PPC::PRED_LE, PredHint);
1755 else if (Immed == 1 && PredCond == PPC::PRED_GE)
1756 // We convert "greater than or equal to 1" into "greater than 0".
1757 NewPred = PPC::getPredicate(PPC::PRED_GT, PredHint);
1758 else
1759 return false;
1760
1761 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
1762 NewPred));
Hal Finkel82656cb2013-04-18 22:15:08 +00001763 }
1764
1765 // Search for Sub.
1766 const TargetRegisterInfo *TRI = &getRegisterInfo();
1767 --I;
Hal Finkelc3632452013-05-07 17:49:55 +00001768
1769 // Get ready to iterate backward from CmpInstr.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001770 MachineBasicBlock::iterator E = MI, B = CmpInstr.getParent()->begin();
Hal Finkelc3632452013-05-07 17:49:55 +00001771
Hal Finkel82656cb2013-04-18 22:15:08 +00001772 for (; I != E && !noSub; --I) {
1773 const MachineInstr &Instr = *I;
1774 unsigned IOpC = Instr.getOpcode();
1775
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001776 if (&*I != &CmpInstr && (Instr.modifiesRegister(PPC::CR0, TRI) ||
1777 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel82656cb2013-04-18 22:15:08 +00001778 // This instruction modifies or uses the record condition register after
1779 // the one we want to change. While we could do this transformation, it
1780 // would likely not be profitable. This transformation removes one
1781 // instruction, and so even forcing RA to generate one move probably
1782 // makes it unprofitable.
1783 return false;
1784
1785 // Check whether CmpInstr can be made redundant by the current instruction.
1786 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1787 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1788 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1789 ((Instr.getOperand(1).getReg() == SrcReg &&
1790 Instr.getOperand(2).getReg() == SrcReg2) ||
1791 (Instr.getOperand(1).getReg() == SrcReg2 &&
1792 Instr.getOperand(2).getReg() == SrcReg))) {
1793 Sub = &*I;
1794 break;
1795 }
1796
Hal Finkel82656cb2013-04-18 22:15:08 +00001797 if (I == B)
1798 // The 'and' is below the comparison instruction.
1799 return false;
1800 }
1801
1802 // Return false if no candidates exist.
1803 if (!MI && !Sub)
1804 return false;
1805
1806 // The single candidate is called MI.
1807 if (!MI) MI = Sub;
1808
1809 int NewOpC = -1;
Hiroshi Inoue5388e662017-10-18 10:31:19 +00001810 int MIOpC = MI->getOpcode();
Hal Finkel82656cb2013-04-18 22:15:08 +00001811 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1812 NewOpC = MIOpC;
1813 else {
1814 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1815 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1816 NewOpC = MIOpC;
1817 }
1818
1819 // FIXME: On the non-embedded POWER architectures, only some of the record
1820 // forms are fast, and we should use only the fast ones.
1821
1822 // The defining instruction has a record form (or is already a record
1823 // form). It is possible, however, that we'll need to reverse the condition
1824 // code of the users.
1825 if (NewOpC == -1)
1826 return false;
1827
Hal Finkel82656cb2013-04-18 22:15:08 +00001828 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1829 // needs to be updated to be based on SUB. Push the condition code
1830 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1831 // condition code of these operands will be modified.
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001832 // Here, Value == 0 means we haven't converted comparison against 1 or -1 to
1833 // comparison against 0, which may modify predicate.
Hal Finkel82656cb2013-04-18 22:15:08 +00001834 bool ShouldSwap = false;
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001835 if (Sub && Value == 0) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001836 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1837 Sub->getOperand(2).getReg() == SrcReg;
1838
1839 // The operands to subf are the opposite of sub, so only in the fixed-point
1840 // case, invert the order.
Hal Finkel08e53ee2013-05-08 12:16:14 +00001841 ShouldSwap = !ShouldSwap;
Hal Finkel82656cb2013-04-18 22:15:08 +00001842 }
1843
1844 if (ShouldSwap)
Owen Anderson16c6bf42014-03-13 23:12:04 +00001845 for (MachineRegisterInfo::use_instr_iterator
1846 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1847 I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001848 MachineInstr *UseMI = &*I;
1849 if (UseMI->getOpcode() == PPC::BCC) {
1850 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hiroshi Inoue967dc582017-07-27 08:14:48 +00001851 unsigned PredCond = PPC::getPredicateCondition(Pred);
Hal Finkele6322392013-04-19 22:08:38 +00001852 assert((!equalityOnly ||
Hiroshi Inoue967dc582017-07-27 08:14:48 +00001853 PredCond == PPC::PRED_EQ || PredCond == PPC::PRED_NE) &&
Hal Finkele6322392013-04-19 22:08:38 +00001854 "Invalid predicate for equality-only optimization");
Hiroshi Inoue967dc582017-07-27 08:14:48 +00001855 (void)PredCond; // To suppress warning in release build.
Owen Anderson16c6bf42014-03-13 23:12:04 +00001856 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
Hal Finkel0f64e212013-04-20 05:16:26 +00001857 PPC::getSwappedPredicate(Pred)));
Hal Finkel82656cb2013-04-18 22:15:08 +00001858 } else if (UseMI->getOpcode() == PPC::ISEL ||
1859 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkele6322392013-04-19 22:08:38 +00001860 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1861 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1862 "Invalid CR bit for equality-only optimization");
1863
1864 if (NewSubReg == PPC::sub_lt)
1865 NewSubReg = PPC::sub_gt;
1866 else if (NewSubReg == PPC::sub_gt)
1867 NewSubReg = PPC::sub_lt;
1868
Owen Anderson16c6bf42014-03-13 23:12:04 +00001869 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
Hal Finkele6322392013-04-19 22:08:38 +00001870 NewSubReg));
Hal Finkel82656cb2013-04-18 22:15:08 +00001871 } else // We need to abort on a user we don't understand.
1872 return false;
1873 }
Hiroshi Inoue37e63b12017-05-21 06:00:05 +00001874 assert(!(Value != 0 && ShouldSwap) &&
1875 "Non-zero immediate support and ShouldSwap"
1876 "may conflict in updating predicate");
Hal Finkel82656cb2013-04-18 22:15:08 +00001877
1878 // Create a new virtual register to hold the value of the CR set by the
1879 // record-form instruction. If the instruction was not previously in
1880 // record form, then set the kill flag on the CR.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001881 CmpInstr.eraseFromParent();
Hal Finkel82656cb2013-04-18 22:15:08 +00001882
1883 MachineBasicBlock::iterator MII = MI;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001884 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
Hal Finkel82656cb2013-04-18 22:15:08 +00001885 get(TargetOpcode::COPY), CRReg)
Hal Finkel08e53ee2013-05-08 12:16:14 +00001886 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel82656cb2013-04-18 22:15:08 +00001887
Chuang-Yu Cheng94f58e72016-04-12 03:10:52 +00001888 // Even if CR0 register were dead before, it is alive now since the
1889 // instruction we just built uses it.
1890 MI->clearRegisterDeads(PPC::CR0);
1891
Hal Finkel82656cb2013-04-18 22:15:08 +00001892 if (MIOpC != NewOpC) {
1893 // We need to be careful here: we're replacing one instruction with
1894 // another, and we need to make sure that we get all of the right
1895 // implicit uses and defs. On the other hand, the caller may be holding
1896 // an iterator to this instruction, and so we can't delete it (this is
1897 // specifically the case if this is the instruction directly after the
1898 // compare).
1899
1900 const MCInstrDesc &NewDesc = get(NewOpC);
1901 MI->setDesc(NewDesc);
1902
1903 if (NewDesc.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +00001904 for (const MCPhysReg *ImpDefs = NewDesc.getImplicitDefs();
Hal Finkel82656cb2013-04-18 22:15:08 +00001905 *ImpDefs; ++ImpDefs)
1906 if (!MI->definesRegister(*ImpDefs))
1907 MI->addOperand(*MI->getParent()->getParent(),
1908 MachineOperand::CreateReg(*ImpDefs, true, true));
1909 if (NewDesc.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +00001910 for (const MCPhysReg *ImpUses = NewDesc.getImplicitUses();
Hal Finkel82656cb2013-04-18 22:15:08 +00001911 *ImpUses; ++ImpUses)
1912 if (!MI->readsRegister(*ImpUses))
1913 MI->addOperand(*MI->getParent()->getParent(),
1914 MachineOperand::CreateReg(*ImpUses, false, true));
1915 }
Keno Fischer55734832016-06-01 20:31:07 +00001916 assert(MI->definesRegister(PPC::CR0) &&
1917 "Record-form instruction does not define cr0?");
Hal Finkel82656cb2013-04-18 22:15:08 +00001918
1919 // Modify the condition code of operands in OperandsToUpdate.
1920 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1921 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkele6322392013-04-19 22:08:38 +00001922 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1923 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001924
Hal Finkele6322392013-04-19 22:08:38 +00001925 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1926 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001927
1928 return true;
1929}
1930
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001931/// GetInstSize - Return the number of bytes of code the specified
1932/// instruction may be. This returns the maximum number of bytes.
1933///
Sjoerd Meijer89217f82016-07-28 16:32:22 +00001934unsigned PPCInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001935 unsigned Opcode = MI.getOpcode();
Hal Finkela7bbaf62014-02-02 06:12:27 +00001936
1937 if (Opcode == PPC::INLINEASM) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001938 const MachineFunction *MF = MI.getParent()->getParent();
1939 const char *AsmStr = MI.getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +00001940 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Hal Finkel934361a2015-01-14 01:07:51 +00001941 } else if (Opcode == TargetOpcode::STACKMAP) {
Philip Reamese83c4b32016-08-23 23:33:29 +00001942 StackMapOpers Opers(&MI);
1943 return Opers.getNumPatchBytes();
Hal Finkel934361a2015-01-14 01:07:51 +00001944 } else if (Opcode == TargetOpcode::PATCHPOINT) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001945 PatchPointOpers Opers(&MI);
Philip Reamese83c4b32016-08-23 23:33:29 +00001946 return Opers.getNumPatchBytes();
Hal Finkela7bbaf62014-02-02 06:12:27 +00001947 } else {
Eric Christopherf48ef332017-03-27 22:40:51 +00001948 return get(Opcode).getSize();
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001949 }
1950}
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001951
Hal Finkel2d556982015-08-30 07:50:35 +00001952std::pair<unsigned, unsigned>
1953PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
1954 const unsigned Mask = PPCII::MO_ACCESS_MASK;
1955 return std::make_pair(TF & Mask, TF & ~Mask);
1956}
1957
1958ArrayRef<std::pair<unsigned, const char *>>
1959PPCInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
1960 using namespace PPCII;
Hal Finkel982e8d42015-08-30 08:07:29 +00001961 static const std::pair<unsigned, const char *> TargetFlags[] = {
Hal Finkel2d556982015-08-30 07:50:35 +00001962 {MO_LO, "ppc-lo"},
1963 {MO_HA, "ppc-ha"},
1964 {MO_TPREL_LO, "ppc-tprel-lo"},
1965 {MO_TPREL_HA, "ppc-tprel-ha"},
1966 {MO_DTPREL_LO, "ppc-dtprel-lo"},
1967 {MO_TLSLD_LO, "ppc-tlsld-lo"},
1968 {MO_TOC_LO, "ppc-toc-lo"},
1969 {MO_TLS, "ppc-tls"}};
1970 return makeArrayRef(TargetFlags);
1971}
1972
1973ArrayRef<std::pair<unsigned, const char *>>
1974PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
1975 using namespace PPCII;
Hal Finkel982e8d42015-08-30 08:07:29 +00001976 static const std::pair<unsigned, const char *> TargetFlags[] = {
Rafael Espindolaa99ccfc2016-06-29 14:59:50 +00001977 {MO_PLT, "ppc-plt"},
Hal Finkel2d556982015-08-30 07:50:35 +00001978 {MO_PIC_FLAG, "ppc-pic"},
1979 {MO_NLP_FLAG, "ppc-nlp"},
1980 {MO_NLP_HIDDEN_FLAG, "ppc-nlp-hidden"}};
1981 return makeArrayRef(TargetFlags);
1982}
1983
Tony Jiang438bf4a2017-11-20 14:38:30 +00001984// Expand VSX Memory Pseudo instruction to either a VSX or a FP instruction.
1985// The VSX versions have the advantage of a full 64-register target whereas
1986// the FP ones have the advantage of lower latency and higher throughput. So
1987// what we are after is using the faster instructions in low register pressure
1988// situations and using the larger register file in high register pressure
1989// situations.
1990bool PPCInstrInfo::expandVSXMemPseudo(MachineInstr &MI) const {
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00001991 unsigned UpperOpcode, LowerOpcode;
1992 switch (MI.getOpcode()) {
1993 case PPC::DFLOADf32:
1994 UpperOpcode = PPC::LXSSP;
1995 LowerOpcode = PPC::LFS;
1996 break;
1997 case PPC::DFLOADf64:
1998 UpperOpcode = PPC::LXSD;
1999 LowerOpcode = PPC::LFD;
2000 break;
2001 case PPC::DFSTOREf32:
2002 UpperOpcode = PPC::STXSSP;
2003 LowerOpcode = PPC::STFS;
2004 break;
2005 case PPC::DFSTOREf64:
2006 UpperOpcode = PPC::STXSD;
2007 LowerOpcode = PPC::STFD;
2008 break;
Tony Jiang438bf4a2017-11-20 14:38:30 +00002009 case PPC::XFLOADf32:
2010 UpperOpcode = PPC::LXSSPX;
2011 LowerOpcode = PPC::LFSX;
2012 break;
2013 case PPC::XFLOADf64:
2014 UpperOpcode = PPC::LXSDX;
2015 LowerOpcode = PPC::LFDX;
2016 break;
2017 case PPC::XFSTOREf32:
2018 UpperOpcode = PPC::STXSSPX;
2019 LowerOpcode = PPC::STFSX;
2020 break;
2021 case PPC::XFSTOREf64:
2022 UpperOpcode = PPC::STXSDX;
2023 LowerOpcode = PPC::STFDX;
2024 break;
2025 case PPC::LIWAX:
2026 UpperOpcode = PPC::LXSIWAX;
2027 LowerOpcode = PPC::LFIWAX;
2028 break;
2029 case PPC::LIWZX:
2030 UpperOpcode = PPC::LXSIWZX;
2031 LowerOpcode = PPC::LFIWZX;
2032 break;
2033 case PPC::STIWX:
2034 UpperOpcode = PPC::STXSIWX;
2035 LowerOpcode = PPC::STFIWX;
2036 break;
2037 default:
2038 llvm_unreachable("Unknown Operation!");
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00002039 }
Tony Jiang438bf4a2017-11-20 14:38:30 +00002040
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00002041 unsigned TargetReg = MI.getOperand(0).getReg();
2042 unsigned Opcode;
2043 if ((TargetReg >= PPC::F0 && TargetReg <= PPC::F31) ||
2044 (TargetReg >= PPC::VSL0 && TargetReg <= PPC::VSL31))
2045 Opcode = LowerOpcode;
2046 else
2047 Opcode = UpperOpcode;
2048 MI.setDesc(get(Opcode));
2049 return true;
Tony Jiang438bf4a2017-11-20 14:38:30 +00002050}
2051
2052bool PPCInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
2053 auto &MBB = *MI.getParent();
2054 auto DL = MI.getDebugLoc();
2055
2056 switch (MI.getOpcode()) {
2057 case TargetOpcode::LOAD_STACK_GUARD: {
2058 assert(Subtarget.isTargetLinux() &&
2059 "Only Linux target is expected to contain LOAD_STACK_GUARD");
2060 const int64_t Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008;
2061 const unsigned Reg = Subtarget.isPPC64() ? PPC::X13 : PPC::R2;
2062 MI.setDesc(get(Subtarget.isPPC64() ? PPC::LD : PPC::LWZ));
2063 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
2064 .addImm(Offset)
2065 .addReg(Reg);
2066 return true;
2067 }
2068 case PPC::DFLOADf32:
2069 case PPC::DFLOADf64:
2070 case PPC::DFSTOREf32:
2071 case PPC::DFSTOREf64: {
2072 assert(Subtarget.hasP9Vector() &&
2073 "Invalid D-Form Pseudo-ops on Pre-P9 target.");
2074 assert(MI.getOperand(2).isReg() && MI.getOperand(1).isImm() &&
2075 "D-form op must have register and immediate operands");
2076 return expandVSXMemPseudo(MI);
2077 }
2078 case PPC::XFLOADf32:
2079 case PPC::XFSTOREf32:
2080 case PPC::LIWAX:
2081 case PPC::LIWZX:
2082 case PPC::STIWX: {
2083 assert(Subtarget.hasP8Vector() &&
2084 "Invalid X-Form Pseudo-ops on Pre-P8 target.");
2085 assert(MI.getOperand(2).isReg() && MI.getOperand(1).isReg() &&
2086 "X-form op must have register and register operands");
2087 return expandVSXMemPseudo(MI);
2088 }
2089 case PPC::XFLOADf64:
2090 case PPC::XFSTOREf64: {
2091 assert(Subtarget.hasVSX() &&
2092 "Invalid X-Form Pseudo-ops on target that has no VSX.");
2093 assert(MI.getOperand(2).isReg() && MI.getOperand(1).isReg() &&
2094 "X-form op must have register and register operands");
2095 return expandVSXMemPseudo(MI);
Nemanja Ivanovic6354d232016-10-04 11:25:52 +00002096 }
Zaara Syedafcd96972017-09-21 16:12:33 +00002097 case PPC::SPILLTOVSR_LD: {
2098 unsigned TargetReg = MI.getOperand(0).getReg();
2099 if (PPC::VSFRCRegClass.contains(TargetReg)) {
2100 MI.setDesc(get(PPC::DFLOADf64));
2101 return expandPostRAPseudo(MI);
2102 }
2103 else
2104 MI.setDesc(get(PPC::LD));
2105 return true;
2106 }
2107 case PPC::SPILLTOVSR_ST: {
2108 unsigned SrcReg = MI.getOperand(0).getReg();
2109 if (PPC::VSFRCRegClass.contains(SrcReg)) {
2110 NumStoreSPILLVSRRCAsVec++;
2111 MI.setDesc(get(PPC::DFSTOREf64));
2112 return expandPostRAPseudo(MI);
2113 } else {
2114 NumStoreSPILLVSRRCAsGpr++;
2115 MI.setDesc(get(PPC::STD));
2116 }
2117 return true;
2118 }
2119 case PPC::SPILLTOVSR_LDX: {
2120 unsigned TargetReg = MI.getOperand(0).getReg();
2121 if (PPC::VSFRCRegClass.contains(TargetReg))
2122 MI.setDesc(get(PPC::LXSDX));
2123 else
2124 MI.setDesc(get(PPC::LDX));
2125 return true;
2126 }
2127 case PPC::SPILLTOVSR_STX: {
2128 unsigned SrcReg = MI.getOperand(0).getReg();
2129 if (PPC::VSFRCRegClass.contains(SrcReg)) {
2130 NumStoreSPILLVSRRCAsVec++;
2131 MI.setDesc(get(PPC::STXSDX));
2132 } else {
2133 NumStoreSPILLVSRRCAsGpr++;
2134 MI.setDesc(get(PPC::STDX));
2135 }
2136 return true;
2137 }
2138
Tim Shen3bef27c2017-05-16 20:18:06 +00002139 case PPC::CFENCE8: {
2140 auto Val = MI.getOperand(0).getReg();
Hiroshi Inoue7a08bb12017-06-15 16:51:28 +00002141 BuildMI(MBB, MI, DL, get(PPC::CMPD), PPC::CR7).addReg(Val).addReg(Val);
Tim Shen3bef27c2017-05-16 20:18:06 +00002142 BuildMI(MBB, MI, DL, get(PPC::CTRL_DEP))
2143 .addImm(PPC::PRED_NE_MINUS)
2144 .addReg(PPC::CR7)
2145 .addImm(1);
2146 MI.setDesc(get(PPC::ISYNC));
2147 MI.RemoveOperand(0);
2148 return true;
2149 }
Tim Shena1d8bc52016-04-19 20:14:52 +00002150 }
2151 return false;
2152}
Nemanja Ivanovic11049f82016-10-04 06:59:23 +00002153
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002154unsigned PPCInstrInfo::lookThruCopyLike(unsigned SrcReg,
2155 const MachineRegisterInfo *MRI) {
2156 while (true) {
2157 MachineInstr *MI = MRI->getVRegDef(SrcReg);
2158 if (!MI->isCopyLike())
2159 return SrcReg;
2160
2161 unsigned CopySrcReg;
2162 if (MI->isCopy())
2163 CopySrcReg = MI->getOperand(1).getReg();
2164 else {
2165 assert(MI->isSubregToReg() && "Bad opcode for lookThruCopyLike");
2166 CopySrcReg = MI->getOperand(2).getReg();
2167 }
2168
2169 if (!TargetRegisterInfo::isVirtualRegister(CopySrcReg))
2170 return CopySrcReg;
2171
2172 SrcReg = CopySrcReg;
2173 }
2174}
2175
2176// Essentially a compile-time implementation of a compare->isel sequence.
2177// It takes two constants to compare, along with the true/false registers
2178// and the comparison type (as a subreg to a CR field) and returns one
2179// of the true/false registers, depending on the comparison results.
2180static unsigned selectReg(int64_t Imm1, int64_t Imm2, unsigned CompareOpc,
2181 unsigned TrueReg, unsigned FalseReg,
2182 unsigned CRSubReg) {
2183 // Signed comparisons. The immediates are assumed to be sign-extended.
2184 if (CompareOpc == PPC::CMPWI || CompareOpc == PPC::CMPDI) {
2185 switch (CRSubReg) {
2186 default: llvm_unreachable("Unknown integer comparison type.");
2187 case PPC::sub_lt:
2188 return Imm1 < Imm2 ? TrueReg : FalseReg;
2189 case PPC::sub_gt:
2190 return Imm1 > Imm2 ? TrueReg : FalseReg;
2191 case PPC::sub_eq:
2192 return Imm1 == Imm2 ? TrueReg : FalseReg;
2193 }
2194 }
2195 // Unsigned comparisons.
2196 else if (CompareOpc == PPC::CMPLWI || CompareOpc == PPC::CMPLDI) {
2197 switch (CRSubReg) {
2198 default: llvm_unreachable("Unknown integer comparison type.");
2199 case PPC::sub_lt:
2200 return (uint64_t)Imm1 < (uint64_t)Imm2 ? TrueReg : FalseReg;
2201 case PPC::sub_gt:
2202 return (uint64_t)Imm1 > (uint64_t)Imm2 ? TrueReg : FalseReg;
2203 case PPC::sub_eq:
2204 return Imm1 == Imm2 ? TrueReg : FalseReg;
2205 }
2206 }
2207 return PPC::NoRegister;
2208}
2209
2210// Replace an instruction with one that materializes a constant (and sets
2211// CR0 if the original instruction was a record-form instruction).
2212void PPCInstrInfo::replaceInstrWithLI(MachineInstr &MI,
2213 const LoadImmediateInfo &LII) const {
2214 // Remove existing operands.
2215 int OperandToKeep = LII.SetCR ? 1 : 0;
2216 for (int i = MI.getNumOperands() - 1; i > OperandToKeep; i--)
2217 MI.RemoveOperand(i);
2218
2219 // Replace the instruction.
Nemanja Ivanovic74ecf592017-12-15 09:51:34 +00002220 if (LII.SetCR) {
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002221 MI.setDesc(get(LII.Is64Bit ? PPC::ANDIo8 : PPC::ANDIo));
Nemanja Ivanovic74ecf592017-12-15 09:51:34 +00002222 // Set the immediate.
2223 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
2224 .addImm(LII.Imm).addReg(PPC::CR0, RegState::ImplicitDefine);
2225 return;
2226 }
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002227 else
2228 MI.setDesc(get(LII.Is64Bit ? PPC::LI8 : PPC::LI));
2229
2230 // Set the immediate.
2231 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
2232 .addImm(LII.Imm);
2233}
2234
2235MachineInstr *PPCInstrInfo::getConstantDefMI(MachineInstr &MI,
2236 unsigned &ConstOp,
2237 bool &SeenIntermediateUse) const {
2238 ConstOp = ~0U;
2239 MachineInstr *DefMI = nullptr;
2240 MachineRegisterInfo *MRI = &MI.getParent()->getParent()->getRegInfo();
2241 // If we'ere in SSA, get the defs through the MRI. Otherwise, only look
2242 // within the basic block to see if the register is defined using an LI/LI8.
2243 if (MRI->isSSA()) {
2244 for (int i = 1, e = MI.getNumOperands(); i < e; i++) {
2245 if (!MI.getOperand(i).isReg())
2246 continue;
2247 unsigned Reg = MI.getOperand(i).getReg();
2248 if (!TargetRegisterInfo::isVirtualRegister(Reg))
2249 continue;
2250 unsigned TrueReg = lookThruCopyLike(Reg, MRI);
2251 if (TargetRegisterInfo::isVirtualRegister(TrueReg)) {
2252 DefMI = MRI->getVRegDef(TrueReg);
2253 if (DefMI->getOpcode() == PPC::LI || DefMI->getOpcode() == PPC::LI8) {
2254 ConstOp = i;
2255 break;
2256 }
2257 }
2258 }
2259 } else {
2260 // Looking back through the definition for each operand could be expensive,
2261 // so exit early if this isn't an instruction that either has an immediate
2262 // form or is already an immediate form that we can handle.
2263 ImmInstrInfo III;
2264 unsigned Opc = MI.getOpcode();
2265 bool ConvertibleImmForm =
2266 Opc == PPC::CMPWI || Opc == PPC::CMPLWI ||
2267 Opc == PPC::CMPDI || Opc == PPC::CMPLDI ||
2268 Opc == PPC::ADDI || Opc == PPC::ADDI8 ||
2269 Opc == PPC::ORI || Opc == PPC::ORI8 ||
2270 Opc == PPC::XORI || Opc == PPC::XORI8 ||
2271 Opc == PPC::RLDICL || Opc == PPC::RLDICLo ||
2272 Opc == PPC::RLDICL_32 || Opc == PPC::RLDICL_32_64 ||
2273 Opc == PPC::RLWINM || Opc == PPC::RLWINMo ||
2274 Opc == PPC::RLWINM8 || Opc == PPC::RLWINM8o;
2275 if (!instrHasImmForm(MI, III) && !ConvertibleImmForm)
2276 return nullptr;
2277
2278 // Don't convert or %X, %Y, %Y since that's just a register move.
2279 if ((Opc == PPC::OR || Opc == PPC::OR8) &&
2280 MI.getOperand(1).getReg() == MI.getOperand(2).getReg())
2281 return nullptr;
2282 for (int i = 1, e = MI.getNumOperands(); i < e; i++) {
2283 MachineOperand &MO = MI.getOperand(i);
2284 SeenIntermediateUse = false;
2285 if (MO.isReg() && MO.isUse() && !MO.isImplicit()) {
2286 MachineBasicBlock::reverse_iterator E = MI.getParent()->rend(), It = MI;
2287 It++;
2288 unsigned Reg = MI.getOperand(i).getReg();
Nemanja Ivanovic6ab32de2017-12-15 14:17:45 +00002289 // MachineInstr::readsRegister only returns true if the machine
2290 // instruction reads the exact register or its super-register. It
2291 // does not consider uses of sub-registers which seems like strange
2292 // behaviour. Nonetheless, if we end up with a 64-bit register here,
2293 // get the corresponding 32-bit register to check.
2294 if (PPC::G8RCRegClass.contains(Reg))
2295 Reg = Reg - PPC::X0 + PPC::R0;
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002296
2297 // Is this register defined by a load-immediate in this block?
2298 for ( ; It != E; ++It) {
2299 if (It->modifiesRegister(Reg, &getRegisterInfo())) {
2300 if (It->getOpcode() == PPC::LI || It->getOpcode() == PPC::LI8) {
2301 ConstOp = i;
2302 return &*It;
2303 } else
2304 break;
2305 } else if (It->readsRegister(Reg, &getRegisterInfo()))
2306 // If we see another use of this reg between the def and the MI,
2307 // we want to flat it so the def isn't deleted.
2308 SeenIntermediateUse = true;
2309 }
2310 }
2311 }
2312 }
2313 return ConstOp == ~0U ? nullptr : DefMI;
2314}
2315
2316// If this instruction has an immediate form and one of its operands is a
2317// result of a load-immediate, convert it to the immediate form if the constant
2318// is in range.
2319bool PPCInstrInfo::convertToImmediateForm(MachineInstr &MI,
2320 MachineInstr **KilledDef) const {
2321 MachineFunction *MF = MI.getParent()->getParent();
2322 MachineRegisterInfo *MRI = &MF->getRegInfo();
2323 bool PostRA = !MRI->isSSA();
2324 bool SeenIntermediateUse = true;
2325 unsigned ConstantOperand = ~0U;
2326 MachineInstr *DefMI = getConstantDefMI(MI, ConstantOperand,
2327 SeenIntermediateUse);
2328 if (!DefMI || !DefMI->getOperand(1).isImm())
2329 return false;
2330 assert(ConstantOperand < MI.getNumOperands() &&
2331 "The constant operand needs to be valid at this point");
2332
2333 int64_t Immediate = DefMI->getOperand(1).getImm();
2334 // Sign-extend to 64-bits.
2335 int64_t SExtImm = ((uint64_t)Immediate & ~0x7FFFuLL) != 0 ?
2336 (Immediate | 0xFFFFFFFFFFFF0000) : Immediate;
2337
2338 if (KilledDef && MI.getOperand(ConstantOperand).isKill() &&
2339 !SeenIntermediateUse)
2340 *KilledDef = DefMI;
2341
2342 // If this is a reg+reg instruction that has a reg+imm form, convert it now.
2343 ImmInstrInfo III;
2344 if (instrHasImmForm(MI, III))
2345 return transformToImmForm(MI, III, ConstantOperand, SExtImm);
2346
2347 bool ReplaceWithLI = false;
2348 bool Is64BitLI = false;
2349 int64_t NewImm = 0;
2350 bool SetCR = false;
2351 unsigned Opc = MI.getOpcode();
2352 switch (Opc) {
2353 default: return false;
2354
2355 // FIXME: Any branches conditional on such a comparison can be made
2356 // unconditional. At this time, this happens too infrequently to be worth
2357 // the implementation effort, but if that ever changes, we could convert
2358 // such a pattern here.
2359 case PPC::CMPWI:
2360 case PPC::CMPLWI:
2361 case PPC::CMPDI:
2362 case PPC::CMPLDI: {
2363 // Doing this post-RA would require dataflow analysis to reliably find uses
2364 // of the CR register set by the compare.
2365 if (PostRA)
2366 return false;
2367 // If a compare-immediate is fed by an immediate and is itself an input of
2368 // an ISEL (the most common case) into a COPY of the correct register.
2369 bool Changed = false;
2370 unsigned DefReg = MI.getOperand(0).getReg();
2371 int64_t Comparand = MI.getOperand(2).getImm();
2372 int64_t SExtComparand = ((uint64_t)Comparand & ~0x7FFFuLL) != 0 ?
2373 (Comparand | 0xFFFFFFFFFFFF0000) : Comparand;
2374
2375 for (auto &CompareUseMI : MRI->use_instructions(DefReg)) {
2376 unsigned UseOpc = CompareUseMI.getOpcode();
2377 if (UseOpc != PPC::ISEL && UseOpc != PPC::ISEL8)
2378 continue;
2379 unsigned CRSubReg = CompareUseMI.getOperand(3).getSubReg();
2380 unsigned TrueReg = CompareUseMI.getOperand(1).getReg();
2381 unsigned FalseReg = CompareUseMI.getOperand(2).getReg();
2382 unsigned RegToCopy = selectReg(SExtImm, SExtComparand, Opc, TrueReg,
2383 FalseReg, CRSubReg);
2384 if (RegToCopy == PPC::NoRegister)
2385 continue;
2386 // Can't use PPC::COPY to copy PPC::ZERO[8]. Convert it to LI[8] 0.
2387 if (RegToCopy == PPC::ZERO || RegToCopy == PPC::ZERO8) {
2388 CompareUseMI.setDesc(get(UseOpc == PPC::ISEL8 ? PPC::LI8 : PPC::LI));
2389 CompareUseMI.getOperand(1).ChangeToImmediate(0);
2390 CompareUseMI.RemoveOperand(3);
2391 CompareUseMI.RemoveOperand(2);
2392 continue;
2393 }
2394 DEBUG(dbgs() << "Found LI -> CMPI -> ISEL, replacing with a copy.\n");
2395 DEBUG(DefMI->dump(); MI.dump(); CompareUseMI.dump());
2396 DEBUG(dbgs() << "Is converted to:\n");
2397 // Convert to copy and remove unneeded operands.
2398 CompareUseMI.setDesc(get(PPC::COPY));
2399 CompareUseMI.RemoveOperand(3);
2400 CompareUseMI.RemoveOperand(RegToCopy == TrueReg ? 2 : 1);
2401 CmpIselsConverted++;
2402 Changed = true;
2403 DEBUG(CompareUseMI.dump());
2404 }
2405 if (Changed)
2406 return true;
2407 // This may end up incremented multiple times since this function is called
2408 // during a fixed-point transformation, but it is only meant to indicate the
2409 // presence of this opportunity.
2410 MissedConvertibleImmediateInstrs++;
2411 return false;
2412 }
2413
2414 // Immediate forms - may simply be convertable to an LI.
2415 case PPC::ADDI:
2416 case PPC::ADDI8: {
2417 // Does the sum fit in a 16-bit signed field?
2418 int64_t Addend = MI.getOperand(2).getImm();
2419 if (isInt<16>(Addend + SExtImm)) {
2420 ReplaceWithLI = true;
2421 Is64BitLI = Opc == PPC::ADDI8;
2422 NewImm = Addend + SExtImm;
2423 break;
2424 }
Nemanja Ivanovic1794cdc2017-12-15 11:47:48 +00002425 return false;
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002426 }
2427 case PPC::RLDICL:
2428 case PPC::RLDICLo:
2429 case PPC::RLDICL_32:
2430 case PPC::RLDICL_32_64: {
2431 // Use APInt's rotate function.
2432 int64_t SH = MI.getOperand(2).getImm();
2433 int64_t MB = MI.getOperand(3).getImm();
2434 APInt InVal(Opc == PPC::RLDICL ? 64 : 32, SExtImm, true);
2435 InVal = InVal.rotl(SH);
Nemanja Ivanovic4e1f5e02017-12-29 12:22:27 +00002436 uint64_t Mask = (1LLU << (63 - MB + 1)) - 1;
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002437 InVal &= Mask;
2438 // Can't replace negative values with an LI as that will sign-extend
2439 // and not clear the left bits. If we're setting the CR bit, we will use
2440 // ANDIo which won't sign extend, so that's safe.
2441 if (isUInt<15>(InVal.getSExtValue()) ||
2442 (Opc == PPC::RLDICLo && isUInt<16>(InVal.getSExtValue()))) {
2443 ReplaceWithLI = true;
2444 Is64BitLI = Opc != PPC::RLDICL_32;
2445 NewImm = InVal.getSExtValue();
2446 SetCR = Opc == PPC::RLDICLo;
Benjamin Kramer309124e2018-01-12 15:03:24 +00002447 if (SetCR && (SExtImm & NewImm) != NewImm)
2448 return false;
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002449 break;
2450 }
2451 return false;
2452 }
2453 case PPC::RLWINM:
2454 case PPC::RLWINM8:
2455 case PPC::RLWINMo:
2456 case PPC::RLWINM8o: {
2457 int64_t SH = MI.getOperand(2).getImm();
2458 int64_t MB = MI.getOperand(3).getImm();
2459 int64_t ME = MI.getOperand(4).getImm();
2460 APInt InVal(32, SExtImm, true);
2461 InVal = InVal.rotl(SH);
Nemanja Ivanovic4e1f5e02017-12-29 12:22:27 +00002462 // Set the bits ( MB + 32 ) to ( ME + 32 ).
2463 uint64_t Mask = ((1LLU << (32 - MB)) - 1) & ~((1LLU << (31 - ME)) - 1);
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002464 InVal &= Mask;
2465 // Can't replace negative values with an LI as that will sign-extend
2466 // and not clear the left bits. If we're setting the CR bit, we will use
2467 // ANDIo which won't sign extend, so that's safe.
2468 bool ValueFits = isUInt<15>(InVal.getSExtValue());
2469 ValueFits |= ((Opc == PPC::RLWINMo || Opc == PPC::RLWINM8o) &&
2470 isUInt<16>(InVal.getSExtValue()));
2471 if (ValueFits) {
2472 ReplaceWithLI = true;
2473 Is64BitLI = Opc == PPC::RLWINM8 || Opc == PPC::RLWINM8o;
2474 NewImm = InVal.getSExtValue();
2475 SetCR = Opc == PPC::RLWINMo || Opc == PPC::RLWINM8o;
Benjamin Kramer309124e2018-01-12 15:03:24 +00002476 if (SetCR && (SExtImm & NewImm) != NewImm)
2477 return false;
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002478 break;
2479 }
2480 return false;
2481 }
2482 case PPC::ORI:
2483 case PPC::ORI8:
2484 case PPC::XORI:
2485 case PPC::XORI8: {
2486 int64_t LogicalImm = MI.getOperand(2).getImm();
2487 int64_t Result = 0;
2488 if (Opc == PPC::ORI || Opc == PPC::ORI8)
2489 Result = LogicalImm | SExtImm;
2490 else
2491 Result = LogicalImm ^ SExtImm;
2492 if (isInt<16>(Result)) {
2493 ReplaceWithLI = true;
2494 Is64BitLI = Opc == PPC::ORI8 || Opc == PPC::XORI8;
2495 NewImm = Result;
2496 break;
2497 }
2498 return false;
2499 }
2500 }
2501
2502 if (ReplaceWithLI) {
2503 DEBUG(dbgs() << "Replacing instruction:\n");
2504 DEBUG(MI.dump());
2505 DEBUG(dbgs() << "Fed by:\n");
2506 DEBUG(DefMI->dump());
2507 LoadImmediateInfo LII;
2508 LII.Imm = NewImm;
2509 LII.Is64Bit = Is64BitLI;
2510 LII.SetCR = SetCR;
2511 // If we're setting the CR, the original load-immediate must be kept (as an
2512 // operand to ANDIo/ANDI8o).
2513 if (KilledDef && SetCR)
2514 *KilledDef = nullptr;
2515 replaceInstrWithLI(MI, LII);
2516 DEBUG(dbgs() << "With:\n");
2517 DEBUG(MI.dump());
2518 return true;
2519 }
2520 return false;
2521}
2522
2523bool PPCInstrInfo::instrHasImmForm(const MachineInstr &MI,
2524 ImmInstrInfo &III) const {
2525 unsigned Opc = MI.getOpcode();
2526 // The vast majority of the instructions would need their operand 2 replaced
2527 // with an immediate when switching to the reg+imm form. A marked exception
2528 // are the update form loads/stores for which a constant operand 2 would need
2529 // to turn into a displacement and move operand 1 to the operand 2 position.
2530 III.ImmOpNo = 2;
2531 III.ConstantOpNo = 2;
2532 III.ImmWidth = 16;
2533 III.ImmMustBeMultipleOf = 1;
Nemanja Ivanovic4e1f5e02017-12-29 12:22:27 +00002534 III.TruncateImmTo = 0;
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002535 switch (Opc) {
2536 default: return false;
2537 case PPC::ADD4:
2538 case PPC::ADD8:
2539 III.SignedImm = true;
2540 III.ZeroIsSpecialOrig = 0;
2541 III.ZeroIsSpecialNew = 1;
2542 III.IsCommutative = true;
2543 III.ImmOpcode = Opc == PPC::ADD4 ? PPC::ADDI : PPC::ADDI8;
2544 break;
2545 case PPC::ADDC:
2546 case PPC::ADDC8:
2547 III.SignedImm = true;
2548 III.ZeroIsSpecialOrig = 0;
2549 III.ZeroIsSpecialNew = 0;
2550 III.IsCommutative = true;
2551 III.ImmOpcode = Opc == PPC::ADDC ? PPC::ADDIC : PPC::ADDIC8;
2552 break;
2553 case PPC::ADDCo:
2554 III.SignedImm = true;
2555 III.ZeroIsSpecialOrig = 0;
2556 III.ZeroIsSpecialNew = 0;
2557 III.IsCommutative = true;
2558 III.ImmOpcode = PPC::ADDICo;
2559 break;
2560 case PPC::SUBFC:
2561 case PPC::SUBFC8:
2562 III.SignedImm = true;
2563 III.ZeroIsSpecialOrig = 0;
2564 III.ZeroIsSpecialNew = 0;
2565 III.IsCommutative = false;
2566 III.ImmOpcode = Opc == PPC::SUBFC ? PPC::SUBFIC : PPC::SUBFIC8;
2567 break;
2568 case PPC::CMPW:
2569 case PPC::CMPD:
2570 III.SignedImm = true;
2571 III.ZeroIsSpecialOrig = 0;
2572 III.ZeroIsSpecialNew = 0;
2573 III.IsCommutative = false;
2574 III.ImmOpcode = Opc == PPC::CMPW ? PPC::CMPWI : PPC::CMPDI;
2575 break;
2576 case PPC::CMPLW:
2577 case PPC::CMPLD:
2578 III.SignedImm = false;
2579 III.ZeroIsSpecialOrig = 0;
2580 III.ZeroIsSpecialNew = 0;
2581 III.IsCommutative = false;
2582 III.ImmOpcode = Opc == PPC::CMPLW ? PPC::CMPLWI : PPC::CMPLDI;
2583 break;
2584 case PPC::ANDo:
2585 case PPC::AND8o:
2586 case PPC::OR:
2587 case PPC::OR8:
2588 case PPC::XOR:
2589 case PPC::XOR8:
2590 III.SignedImm = false;
2591 III.ZeroIsSpecialOrig = 0;
2592 III.ZeroIsSpecialNew = 0;
2593 III.IsCommutative = true;
2594 switch(Opc) {
2595 default: llvm_unreachable("Unknown opcode");
2596 case PPC::ANDo: III.ImmOpcode = PPC::ANDIo; break;
2597 case PPC::AND8o: III.ImmOpcode = PPC::ANDIo8; break;
2598 case PPC::OR: III.ImmOpcode = PPC::ORI; break;
2599 case PPC::OR8: III.ImmOpcode = PPC::ORI8; break;
2600 case PPC::XOR: III.ImmOpcode = PPC::XORI; break;
2601 case PPC::XOR8: III.ImmOpcode = PPC::XORI8; break;
2602 }
2603 break;
2604 case PPC::RLWNM:
2605 case PPC::RLWNM8:
2606 case PPC::RLWNMo:
2607 case PPC::RLWNM8o:
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002608 case PPC::SLW:
2609 case PPC::SLW8:
2610 case PPC::SLWo:
2611 case PPC::SLW8o:
2612 case PPC::SRW:
2613 case PPC::SRW8:
2614 case PPC::SRWo:
2615 case PPC::SRW8o:
2616 case PPC::SRAW:
2617 case PPC::SRAWo:
Nemanja Ivanovic4e1f5e02017-12-29 12:22:27 +00002618 III.SignedImm = false;
2619 III.ZeroIsSpecialOrig = 0;
2620 III.ZeroIsSpecialNew = 0;
2621 III.IsCommutative = false;
2622 // This isn't actually true, but the instructions ignore any of the
2623 // upper bits, so any immediate loaded with an LI is acceptable.
2624 // This does not apply to shift right algebraic because a value
2625 // out of range will produce a -1/0.
2626 III.ImmWidth = 16;
2627 if (Opc == PPC::RLWNM || Opc == PPC::RLWNM8 ||
2628 Opc == PPC::RLWNMo || Opc == PPC::RLWNM8o)
2629 III.TruncateImmTo = 5;
2630 else
2631 III.TruncateImmTo = 6;
2632 switch(Opc) {
2633 default: llvm_unreachable("Unknown opcode");
2634 case PPC::RLWNM: III.ImmOpcode = PPC::RLWINM; break;
2635 case PPC::RLWNM8: III.ImmOpcode = PPC::RLWINM8; break;
2636 case PPC::RLWNMo: III.ImmOpcode = PPC::RLWINMo; break;
2637 case PPC::RLWNM8o: III.ImmOpcode = PPC::RLWINM8o; break;
2638 case PPC::SLW: III.ImmOpcode = PPC::RLWINM; break;
2639 case PPC::SLW8: III.ImmOpcode = PPC::RLWINM8; break;
2640 case PPC::SLWo: III.ImmOpcode = PPC::RLWINMo; break;
2641 case PPC::SLW8o: III.ImmOpcode = PPC::RLWINM8o; break;
2642 case PPC::SRW: III.ImmOpcode = PPC::RLWINM; break;
2643 case PPC::SRW8: III.ImmOpcode = PPC::RLWINM8; break;
2644 case PPC::SRWo: III.ImmOpcode = PPC::RLWINMo; break;
2645 case PPC::SRW8o: III.ImmOpcode = PPC::RLWINM8o; break;
2646 case PPC::SRAW:
2647 III.ImmWidth = 5;
2648 III.TruncateImmTo = 0;
2649 III.ImmOpcode = PPC::SRAWI;
2650 break;
2651 case PPC::SRAWo:
2652 III.ImmWidth = 5;
2653 III.TruncateImmTo = 0;
2654 III.ImmOpcode = PPC::SRAWIo;
2655 break;
2656 }
2657 break;
2658 case PPC::RLDCL:
2659 case PPC::RLDCLo:
2660 case PPC::RLDCR:
2661 case PPC::RLDCRo:
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002662 case PPC::SLD:
2663 case PPC::SLDo:
2664 case PPC::SRD:
2665 case PPC::SRDo:
2666 case PPC::SRAD:
2667 case PPC::SRADo:
2668 III.SignedImm = false;
2669 III.ZeroIsSpecialOrig = 0;
2670 III.ZeroIsSpecialNew = 0;
2671 III.IsCommutative = false;
2672 // This isn't actually true, but the instructions ignore any of the
2673 // upper bits, so any immediate loaded with an LI is acceptable.
Nemanja Ivanovic4e1f5e02017-12-29 12:22:27 +00002674 // This does not apply to shift right algebraic because a value
2675 // out of range will produce a -1/0.
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002676 III.ImmWidth = 16;
Nemanja Ivanovic4e1f5e02017-12-29 12:22:27 +00002677 if (Opc == PPC::RLDCL || Opc == PPC::RLDCLo ||
2678 Opc == PPC::RLDCR || Opc == PPC::RLDCRo)
2679 III.TruncateImmTo = 6;
2680 else
2681 III.TruncateImmTo = 7;
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002682 switch(Opc) {
2683 default: llvm_unreachable("Unknown opcode");
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002684 case PPC::RLDCL: III.ImmOpcode = PPC::RLDICL; break;
2685 case PPC::RLDCLo: III.ImmOpcode = PPC::RLDICLo; break;
2686 case PPC::RLDCR: III.ImmOpcode = PPC::RLDICR; break;
2687 case PPC::RLDCRo: III.ImmOpcode = PPC::RLDICRo; break;
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002688 case PPC::SLD: III.ImmOpcode = PPC::RLDICR; break;
2689 case PPC::SLDo: III.ImmOpcode = PPC::RLDICRo; break;
2690 case PPC::SRD: III.ImmOpcode = PPC::RLDICL; break;
2691 case PPC::SRDo: III.ImmOpcode = PPC::RLDICLo; break;
Nemanja Ivanovic4e1f5e02017-12-29 12:22:27 +00002692 case PPC::SRAD:
2693 III.ImmWidth = 6;
2694 III.TruncateImmTo = 0;
2695 III.ImmOpcode = PPC::SRADI;
2696 break;
2697 case PPC::SRADo:
2698 III.ImmWidth = 6;
2699 III.TruncateImmTo = 0;
2700 III.ImmOpcode = PPC::SRADIo;
2701 break;
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002702 }
2703 break;
2704 // Loads and stores:
2705 case PPC::LBZX:
2706 case PPC::LBZX8:
2707 case PPC::LHZX:
2708 case PPC::LHZX8:
2709 case PPC::LHAX:
2710 case PPC::LHAX8:
2711 case PPC::LWZX:
2712 case PPC::LWZX8:
2713 case PPC::LWAX:
2714 case PPC::LDX:
2715 case PPC::LFSX:
2716 case PPC::LFDX:
2717 case PPC::STBX:
2718 case PPC::STBX8:
2719 case PPC::STHX:
2720 case PPC::STHX8:
2721 case PPC::STWX:
2722 case PPC::STWX8:
2723 case PPC::STDX:
2724 case PPC::STFSX:
2725 case PPC::STFDX:
2726 III.SignedImm = true;
2727 III.ZeroIsSpecialOrig = 1;
2728 III.ZeroIsSpecialNew = 2;
2729 III.IsCommutative = true;
2730 III.ImmOpNo = 1;
2731 III.ConstantOpNo = 2;
2732 switch(Opc) {
2733 default: llvm_unreachable("Unknown opcode");
2734 case PPC::LBZX: III.ImmOpcode = PPC::LBZ; break;
2735 case PPC::LBZX8: III.ImmOpcode = PPC::LBZ8; break;
2736 case PPC::LHZX: III.ImmOpcode = PPC::LHZ; break;
2737 case PPC::LHZX8: III.ImmOpcode = PPC::LHZ8; break;
2738 case PPC::LHAX: III.ImmOpcode = PPC::LHA; break;
2739 case PPC::LHAX8: III.ImmOpcode = PPC::LHA8; break;
2740 case PPC::LWZX: III.ImmOpcode = PPC::LWZ; break;
2741 case PPC::LWZX8: III.ImmOpcode = PPC::LWZ8; break;
2742 case PPC::LWAX:
2743 III.ImmOpcode = PPC::LWA;
2744 III.ImmMustBeMultipleOf = 4;
2745 break;
2746 case PPC::LDX: III.ImmOpcode = PPC::LD; III.ImmMustBeMultipleOf = 4; break;
2747 case PPC::LFSX: III.ImmOpcode = PPC::LFS; break;
2748 case PPC::LFDX: III.ImmOpcode = PPC::LFD; break;
2749 case PPC::STBX: III.ImmOpcode = PPC::STB; break;
2750 case PPC::STBX8: III.ImmOpcode = PPC::STB8; break;
2751 case PPC::STHX: III.ImmOpcode = PPC::STH; break;
2752 case PPC::STHX8: III.ImmOpcode = PPC::STH8; break;
2753 case PPC::STWX: III.ImmOpcode = PPC::STW; break;
2754 case PPC::STWX8: III.ImmOpcode = PPC::STW8; break;
2755 case PPC::STDX:
2756 III.ImmOpcode = PPC::STD;
2757 III.ImmMustBeMultipleOf = 4;
2758 break;
2759 case PPC::STFSX: III.ImmOpcode = PPC::STFS; break;
2760 case PPC::STFDX: III.ImmOpcode = PPC::STFD; break;
2761 }
2762 break;
2763 case PPC::LBZUX:
2764 case PPC::LBZUX8:
2765 case PPC::LHZUX:
2766 case PPC::LHZUX8:
2767 case PPC::LHAUX:
2768 case PPC::LHAUX8:
2769 case PPC::LWZUX:
2770 case PPC::LWZUX8:
2771 case PPC::LDUX:
2772 case PPC::LFSUX:
2773 case PPC::LFDUX:
2774 case PPC::STBUX:
2775 case PPC::STBUX8:
2776 case PPC::STHUX:
2777 case PPC::STHUX8:
2778 case PPC::STWUX:
2779 case PPC::STWUX8:
2780 case PPC::STDUX:
2781 case PPC::STFSUX:
2782 case PPC::STFDUX:
2783 III.SignedImm = true;
2784 III.ZeroIsSpecialOrig = 2;
2785 III.ZeroIsSpecialNew = 3;
2786 III.IsCommutative = false;
2787 III.ImmOpNo = 2;
2788 III.ConstantOpNo = 3;
2789 switch(Opc) {
2790 default: llvm_unreachable("Unknown opcode");
2791 case PPC::LBZUX: III.ImmOpcode = PPC::LBZU; break;
2792 case PPC::LBZUX8: III.ImmOpcode = PPC::LBZU8; break;
2793 case PPC::LHZUX: III.ImmOpcode = PPC::LHZU; break;
2794 case PPC::LHZUX8: III.ImmOpcode = PPC::LHZU8; break;
2795 case PPC::LHAUX: III.ImmOpcode = PPC::LHAU; break;
2796 case PPC::LHAUX8: III.ImmOpcode = PPC::LHAU8; break;
2797 case PPC::LWZUX: III.ImmOpcode = PPC::LWZU; break;
2798 case PPC::LWZUX8: III.ImmOpcode = PPC::LWZU8; break;
2799 case PPC::LDUX:
2800 III.ImmOpcode = PPC::LDU;
2801 III.ImmMustBeMultipleOf = 4;
2802 break;
2803 case PPC::LFSUX: III.ImmOpcode = PPC::LFSU; break;
2804 case PPC::LFDUX: III.ImmOpcode = PPC::LFDU; break;
2805 case PPC::STBUX: III.ImmOpcode = PPC::STBU; break;
2806 case PPC::STBUX8: III.ImmOpcode = PPC::STBU8; break;
2807 case PPC::STHUX: III.ImmOpcode = PPC::STHU; break;
2808 case PPC::STHUX8: III.ImmOpcode = PPC::STHU8; break;
2809 case PPC::STWUX: III.ImmOpcode = PPC::STWU; break;
2810 case PPC::STWUX8: III.ImmOpcode = PPC::STWU8; break;
2811 case PPC::STDUX:
2812 III.ImmOpcode = PPC::STDU;
2813 III.ImmMustBeMultipleOf = 4;
2814 break;
2815 case PPC::STFSUX: III.ImmOpcode = PPC::STFSU; break;
2816 case PPC::STFDUX: III.ImmOpcode = PPC::STFDU; break;
2817 }
2818 break;
2819 // Power9 only.
2820 case PPC::LXVX:
2821 case PPC::LXSSPX:
2822 case PPC::LXSDX:
2823 case PPC::STXVX:
2824 case PPC::STXSSPX:
2825 case PPC::STXSDX:
2826 if (!Subtarget.hasP9Vector())
2827 return false;
2828 III.SignedImm = true;
2829 III.ZeroIsSpecialOrig = 1;
2830 III.ZeroIsSpecialNew = 2;
2831 III.IsCommutative = true;
2832 III.ImmOpNo = 1;
2833 III.ConstantOpNo = 2;
2834 switch(Opc) {
2835 default: llvm_unreachable("Unknown opcode");
2836 case PPC::LXVX:
2837 III.ImmOpcode = PPC::LXV;
2838 III.ImmMustBeMultipleOf = 16;
2839 break;
2840 case PPC::LXSSPX:
2841 III.ImmOpcode = PPC::LXSSP;
2842 III.ImmMustBeMultipleOf = 4;
2843 break;
2844 case PPC::LXSDX:
2845 III.ImmOpcode = PPC::LXSD;
2846 III.ImmMustBeMultipleOf = 4;
2847 break;
2848 case PPC::STXVX:
2849 III.ImmOpcode = PPC::STXV;
2850 III.ImmMustBeMultipleOf = 16;
2851 break;
2852 case PPC::STXSSPX:
2853 III.ImmOpcode = PPC::STXSSP;
2854 III.ImmMustBeMultipleOf = 4;
2855 break;
2856 case PPC::STXSDX:
2857 III.ImmOpcode = PPC::STXSD;
2858 III.ImmMustBeMultipleOf = 4;
2859 break;
2860 }
2861 break;
2862 }
2863 return true;
2864}
2865
2866// Utility function for swaping two arbitrary operands of an instruction.
2867static void swapMIOperands(MachineInstr &MI, unsigned Op1, unsigned Op2) {
2868 assert(Op1 != Op2 && "Cannot swap operand with itself.");
2869
2870 unsigned MaxOp = std::max(Op1, Op2);
2871 unsigned MinOp = std::min(Op1, Op2);
2872 MachineOperand MOp1 = MI.getOperand(MinOp);
2873 MachineOperand MOp2 = MI.getOperand(MaxOp);
2874 MI.RemoveOperand(std::max(Op1, Op2));
2875 MI.RemoveOperand(std::min(Op1, Op2));
2876
2877 // If the operands we are swapping are the two at the end (the common case)
2878 // we can just remove both and add them in the opposite order.
2879 if (MaxOp - MinOp == 1 && MI.getNumOperands() == MinOp) {
2880 MI.addOperand(MOp2);
2881 MI.addOperand(MOp1);
2882 } else {
2883 // Store all operands in a temporary vector, remove them and re-add in the
2884 // right order.
2885 SmallVector<MachineOperand, 2> MOps;
2886 unsigned TotalOps = MI.getNumOperands() + 2; // We've already removed 2 ops.
2887 for (unsigned i = MI.getNumOperands() - 1; i >= MinOp; i--) {
2888 MOps.push_back(MI.getOperand(i));
2889 MI.RemoveOperand(i);
2890 }
2891 // MOp2 needs to be added next.
2892 MI.addOperand(MOp2);
2893 // Now add the rest.
2894 for (unsigned i = MI.getNumOperands(); i < TotalOps; i++) {
2895 if (i == MaxOp)
2896 MI.addOperand(MOp1);
2897 else {
2898 MI.addOperand(MOps.back());
2899 MOps.pop_back();
2900 }
2901 }
2902 }
2903}
2904
2905bool PPCInstrInfo::transformToImmForm(MachineInstr &MI, const ImmInstrInfo &III,
2906 unsigned ConstantOpNo,
2907 int64_t Imm) const {
2908 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
2909 bool PostRA = !MRI.isSSA();
2910 // Exit early if we can't convert this.
2911 if ((ConstantOpNo != III.ConstantOpNo) && !III.IsCommutative)
2912 return false;
2913 if (Imm % III.ImmMustBeMultipleOf)
2914 return false;
Nemanja Ivanovic4e1f5e02017-12-29 12:22:27 +00002915 if (III.TruncateImmTo)
2916 Imm &= ((1 << III.TruncateImmTo) - 1);
Nemanja Ivanovic6995e5d2017-12-15 07:27:53 +00002917 if (III.SignedImm) {
2918 APInt ActualValue(64, Imm, true);
2919 if (!ActualValue.isSignedIntN(III.ImmWidth))
2920 return false;
2921 } else {
2922 uint64_t UnsignedMax = (1 << III.ImmWidth) - 1;
2923 if ((uint64_t)Imm > UnsignedMax)
2924 return false;
2925 }
2926
2927 // If we're post-RA, the instructions don't agree on whether register zero is
2928 // special, we can transform this as long as the register operand that will
2929 // end up in the location where zero is special isn't R0.
2930 if (PostRA && III.ZeroIsSpecialOrig != III.ZeroIsSpecialNew) {
2931 unsigned PosForOrigZero = III.ZeroIsSpecialOrig ? III.ZeroIsSpecialOrig :
2932 III.ZeroIsSpecialNew + 1;
2933 unsigned OrigZeroReg = MI.getOperand(PosForOrigZero).getReg();
2934 unsigned NewZeroReg = MI.getOperand(III.ZeroIsSpecialNew).getReg();
2935 // If R0 is in the operand where zero is special for the new instruction,
2936 // it is unsafe to transform if the constant operand isn't that operand.
2937 if ((NewZeroReg == PPC::R0 || NewZeroReg == PPC::X0) &&
2938 ConstantOpNo != III.ZeroIsSpecialNew)
2939 return false;
2940 if ((OrigZeroReg == PPC::R0 || OrigZeroReg == PPC::X0) &&
2941 ConstantOpNo != PosForOrigZero)
2942 return false;
2943 }
2944
2945 unsigned Opc = MI.getOpcode();
2946 bool SpecialShift32 =
2947 Opc == PPC::SLW || Opc == PPC::SLWo || Opc == PPC::SRW || Opc == PPC::SRWo;
2948 bool SpecialShift64 =
2949 Opc == PPC::SLD || Opc == PPC::SLDo || Opc == PPC::SRD || Opc == PPC::SRDo;
2950 bool SetCR = Opc == PPC::SLWo || Opc == PPC::SRWo ||
2951 Opc == PPC::SLDo || Opc == PPC::SRDo;
2952 bool RightShift =
2953 Opc == PPC::SRW || Opc == PPC::SRWo || Opc == PPC::SRD || Opc == PPC::SRDo;
2954
2955 MI.setDesc(get(III.ImmOpcode));
2956 if (ConstantOpNo == III.ConstantOpNo) {
2957 // Converting shifts to immediate form is a bit tricky since they may do
2958 // one of three things:
2959 // 1. If the shift amount is between OpSize and 2*OpSize, the result is zero
2960 // 2. If the shift amount is zero, the result is unchanged (save for maybe
2961 // setting CR0)
2962 // 3. If the shift amount is in [1, OpSize), it's just a shift
2963 if (SpecialShift32 || SpecialShift64) {
2964 LoadImmediateInfo LII;
2965 LII.Imm = 0;
2966 LII.SetCR = SetCR;
2967 LII.Is64Bit = SpecialShift64;
2968 uint64_t ShAmt = Imm & (SpecialShift32 ? 0x1F : 0x3F);
2969 if (Imm & (SpecialShift32 ? 0x20 : 0x40))
2970 replaceInstrWithLI(MI, LII);
2971 // Shifts by zero don't change the value. If we don't need to set CR0,
2972 // just convert this to a COPY. Can't do this post-RA since we've already
2973 // cleaned up the copies.
2974 else if (!SetCR && ShAmt == 0 && !PostRA) {
2975 MI.RemoveOperand(2);
2976 MI.setDesc(get(PPC::COPY));
2977 } else {
2978 // The 32 bit and 64 bit instructions are quite different.
2979 if (SpecialShift32) {
2980 // Left shifts use (N, 0, 31-N), right shifts use (32-N, N, 31).
2981 uint64_t SH = RightShift ? 32 - ShAmt : ShAmt;
2982 uint64_t MB = RightShift ? ShAmt : 0;
2983 uint64_t ME = RightShift ? 31 : 31 - ShAmt;
2984 MI.getOperand(III.ConstantOpNo).ChangeToImmediate(SH);
2985 MachineInstrBuilder(*MI.getParent()->getParent(), MI).addImm(MB)
2986 .addImm(ME);
2987 } else {
2988 // Left shifts use (N, 63-N), right shifts use (64-N, N).
2989 uint64_t SH = RightShift ? 64 - ShAmt : ShAmt;
2990 uint64_t ME = RightShift ? ShAmt : 63 - ShAmt;
2991 MI.getOperand(III.ConstantOpNo).ChangeToImmediate(SH);
2992 MachineInstrBuilder(*MI.getParent()->getParent(), MI).addImm(ME);
2993 }
2994 }
2995 } else
2996 MI.getOperand(ConstantOpNo).ChangeToImmediate(Imm);
2997 }
2998 // Convert commutative instructions (switch the operands and convert the
2999 // desired one to an immediate.
3000 else if (III.IsCommutative) {
3001 MI.getOperand(ConstantOpNo).ChangeToImmediate(Imm);
3002 swapMIOperands(MI, ConstantOpNo, III.ConstantOpNo);
3003 } else
3004 llvm_unreachable("Should have exited early!");
3005
3006 // For instructions for which the constant register replaces a different
3007 // operand than where the immediate goes, we need to swap them.
3008 if (III.ConstantOpNo != III.ImmOpNo)
3009 swapMIOperands(MI, III.ConstantOpNo, III.ImmOpNo);
3010
3011 // If the R0/X0 register is special for the original instruction and not for
3012 // the new instruction (or vice versa), we need to fix up the register class.
3013 if (!PostRA && III.ZeroIsSpecialOrig != III.ZeroIsSpecialNew) {
3014 if (!III.ZeroIsSpecialOrig) {
3015 unsigned RegToModify = MI.getOperand(III.ZeroIsSpecialNew).getReg();
3016 const TargetRegisterClass *NewRC =
3017 MRI.getRegClass(RegToModify)->hasSuperClassEq(&PPC::GPRCRegClass) ?
3018 &PPC::GPRC_and_GPRC_NOR0RegClass : &PPC::G8RC_and_G8RC_NOX0RegClass;
3019 MRI.setRegClass(RegToModify, NewRC);
3020 }
3021 }
3022 return true;
3023}
3024
Nemanja Ivanovic11049f82016-10-04 06:59:23 +00003025const TargetRegisterClass *
3026PPCInstrInfo::updatedRC(const TargetRegisterClass *RC) const {
3027 if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass)
3028 return &PPC::VSRCRegClass;
3029 return RC;
3030}
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00003031
3032int PPCInstrInfo::getRecordFormOpcode(unsigned Opcode) {
3033 return PPC::getRecordFormOpcode(Opcode);
3034}
Hiroshi Inouee3a3e3c2017-10-16 04:12:57 +00003035
3036// This function returns true if the machine instruction
3037// always outputs a value by sign-extending a 32 bit value,
3038// i.e. 0 to 31-th bits are same as 32-th bit.
3039static bool isSignExtendingOp(const MachineInstr &MI) {
3040 int Opcode = MI.getOpcode();
3041 if (Opcode == PPC::LI || Opcode == PPC::LI8 ||
3042 Opcode == PPC::LIS || Opcode == PPC::LIS8 ||
3043 Opcode == PPC::SRAW || Opcode == PPC::SRAWo ||
3044 Opcode == PPC::SRAWI || Opcode == PPC::SRAWIo ||
3045 Opcode == PPC::LWA || Opcode == PPC::LWAX ||
3046 Opcode == PPC::LWA_32 || Opcode == PPC::LWAX_32 ||
3047 Opcode == PPC::LHA || Opcode == PPC::LHAX ||
3048 Opcode == PPC::LHA8 || Opcode == PPC::LHAX8 ||
3049 Opcode == PPC::LBZ || Opcode == PPC::LBZX ||
3050 Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 ||
3051 Opcode == PPC::LBZU || Opcode == PPC::LBZUX ||
3052 Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 ||
3053 Opcode == PPC::LHZ || Opcode == PPC::LHZX ||
3054 Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 ||
3055 Opcode == PPC::LHZU || Opcode == PPC::LHZUX ||
3056 Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8 ||
3057 Opcode == PPC::EXTSB || Opcode == PPC::EXTSBo ||
3058 Opcode == PPC::EXTSH || Opcode == PPC::EXTSHo ||
3059 Opcode == PPC::EXTSB8 || Opcode == PPC::EXTSH8 ||
3060 Opcode == PPC::EXTSW || Opcode == PPC::EXTSWo ||
3061 Opcode == PPC::EXTSH8_32_64 || Opcode == PPC::EXTSW_32_64 ||
3062 Opcode == PPC::EXTSB8_32_64)
3063 return true;
3064
3065 if (Opcode == PPC::RLDICL && MI.getOperand(3).getImm() >= 33)
3066 return true;
3067
3068 if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINMo ||
3069 Opcode == PPC::RLWNM || Opcode == PPC::RLWNMo) &&
3070 MI.getOperand(3).getImm() > 0 &&
3071 MI.getOperand(3).getImm() <= MI.getOperand(4).getImm())
3072 return true;
3073
3074 return false;
3075}
3076
3077// This function returns true if the machine instruction
3078// always outputs zeros in higher 32 bits.
3079static bool isZeroExtendingOp(const MachineInstr &MI) {
3080 int Opcode = MI.getOpcode();
3081 // The 16-bit immediate is sign-extended in li/lis.
3082 // If the most significant bit is zero, all higher bits are zero.
3083 if (Opcode == PPC::LI || Opcode == PPC::LI8 ||
3084 Opcode == PPC::LIS || Opcode == PPC::LIS8) {
3085 int64_t Imm = MI.getOperand(1).getImm();
3086 if (((uint64_t)Imm & ~0x7FFFuLL) == 0)
3087 return true;
3088 }
3089
3090 // We have some variations of rotate-and-mask instructions
3091 // that clear higher 32-bits.
3092 if ((Opcode == PPC::RLDICL || Opcode == PPC::RLDICLo ||
3093 Opcode == PPC::RLDCL || Opcode == PPC::RLDCLo ||
3094 Opcode == PPC::RLDICL_32_64) &&
3095 MI.getOperand(3).getImm() >= 32)
3096 return true;
3097
3098 if ((Opcode == PPC::RLDIC || Opcode == PPC::RLDICo) &&
3099 MI.getOperand(3).getImm() >= 32 &&
3100 MI.getOperand(3).getImm() <= 63 - MI.getOperand(2).getImm())
3101 return true;
3102
3103 if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINMo ||
3104 Opcode == PPC::RLWNM || Opcode == PPC::RLWNMo ||
3105 Opcode == PPC::RLWINM8 || Opcode == PPC::RLWNM8) &&
3106 MI.getOperand(3).getImm() <= MI.getOperand(4).getImm())
3107 return true;
3108
3109 // There are other instructions that clear higher 32-bits.
3110 if (Opcode == PPC::CNTLZW || Opcode == PPC::CNTLZWo ||
3111 Opcode == PPC::CNTTZW || Opcode == PPC::CNTTZWo ||
3112 Opcode == PPC::CNTLZW8 || Opcode == PPC::CNTTZW8 ||
3113 Opcode == PPC::CNTLZD || Opcode == PPC::CNTLZDo ||
3114 Opcode == PPC::CNTTZD || Opcode == PPC::CNTTZDo ||
3115 Opcode == PPC::POPCNTD || Opcode == PPC::POPCNTW ||
3116 Opcode == PPC::SLW || Opcode == PPC::SLWo ||
3117 Opcode == PPC::SRW || Opcode == PPC::SRWo ||
3118 Opcode == PPC::SLW8 || Opcode == PPC::SRW8 ||
3119 Opcode == PPC::SLWI || Opcode == PPC::SLWIo ||
3120 Opcode == PPC::SRWI || Opcode == PPC::SRWIo ||
3121 Opcode == PPC::LWZ || Opcode == PPC::LWZX ||
3122 Opcode == PPC::LWZU || Opcode == PPC::LWZUX ||
3123 Opcode == PPC::LWBRX || Opcode == PPC::LHBRX ||
3124 Opcode == PPC::LHZ || Opcode == PPC::LHZX ||
3125 Opcode == PPC::LHZU || Opcode == PPC::LHZUX ||
3126 Opcode == PPC::LBZ || Opcode == PPC::LBZX ||
3127 Opcode == PPC::LBZU || Opcode == PPC::LBZUX ||
3128 Opcode == PPC::LWZ8 || Opcode == PPC::LWZX8 ||
3129 Opcode == PPC::LWZU8 || Opcode == PPC::LWZUX8 ||
3130 Opcode == PPC::LWBRX8 || Opcode == PPC::LHBRX8 ||
3131 Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 ||
3132 Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8 ||
3133 Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 ||
3134 Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 ||
3135 Opcode == PPC::ANDIo || Opcode == PPC::ANDISo ||
3136 Opcode == PPC::ROTRWI || Opcode == PPC::ROTRWIo ||
3137 Opcode == PPC::EXTLWI || Opcode == PPC::EXTLWIo ||
3138 Opcode == PPC::MFVSRWZ)
3139 return true;
3140
3141 return false;
3142}
3143
Zaara Syedaf94d58d2017-11-27 20:26:36 +00003144// This function returns true if the input MachineInstr is a TOC save
3145// instruction.
3146bool PPCInstrInfo::isTOCSaveMI(const MachineInstr &MI) const {
3147 if (!MI.getOperand(1).isImm() || !MI.getOperand(2).isReg())
3148 return false;
3149 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset();
3150 unsigned StackOffset = MI.getOperand(1).getImm();
3151 unsigned StackReg = MI.getOperand(2).getReg();
3152 if (StackReg == PPC::X1 && StackOffset == TOCSaveOffset)
3153 return true;
3154
3155 return false;
3156}
3157
Hiroshi Inouee3a3e3c2017-10-16 04:12:57 +00003158// We limit the max depth to track incoming values of PHIs or binary ops
3159// (e.g. AND) to avoid exsessive cost.
3160const unsigned MAX_DEPTH = 1;
3161
3162bool
3163PPCInstrInfo::isSignOrZeroExtended(const MachineInstr &MI, bool SignExt,
3164 const unsigned Depth) const {
3165 const MachineFunction *MF = MI.getParent()->getParent();
3166 const MachineRegisterInfo *MRI = &MF->getRegInfo();
3167
Hiroshi Inoue5388e662017-10-18 10:31:19 +00003168 // If we know this instruction returns sign- or zero-extended result,
3169 // return true.
3170 if (SignExt ? isSignExtendingOp(MI):
3171 isZeroExtendingOp(MI))
3172 return true;
3173
Hiroshi Inouee3a3e3c2017-10-16 04:12:57 +00003174 switch (MI.getOpcode()) {
3175 case PPC::COPY: {
3176 unsigned SrcReg = MI.getOperand(1).getReg();
3177
3178 // In both ELFv1 and v2 ABI, method parameters and the return value
3179 // are sign- or zero-extended.
3180 if (MF->getSubtarget<PPCSubtarget>().isSVR4ABI()) {
3181 const PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
3182 // We check the ZExt/SExt flags for a method parameter.
3183 if (MI.getParent()->getBasicBlock() ==
Matthias Braunf1caa282017-12-15 22:22:58 +00003184 &MF->getFunction().getEntryBlock()) {
Hiroshi Inouee3a3e3c2017-10-16 04:12:57 +00003185 unsigned VReg = MI.getOperand(0).getReg();
3186 if (MF->getRegInfo().isLiveIn(VReg))
3187 return SignExt ? FuncInfo->isLiveInSExt(VReg) :
3188 FuncInfo->isLiveInZExt(VReg);
3189 }
3190
3191 // For a method return value, we check the ZExt/SExt flags in attribute.
3192 // We assume the following code sequence for method call.
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00003193 // ADJCALLSTACKDOWN 32, implicit dead %r1, implicit %r1
Francis Visoiu Mistrih5df3bbf2017-12-14 10:03:09 +00003194 // BL8_NOP @func,...
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00003195 // ADJCALLSTACKUP 32, 0, implicit dead %r1, implicit %r1
3196 // %5 = COPY %x3; G8RC:%5
Hiroshi Inouee3a3e3c2017-10-16 04:12:57 +00003197 if (SrcReg == PPC::X3) {
3198 const MachineBasicBlock *MBB = MI.getParent();
3199 MachineBasicBlock::const_instr_iterator II =
3200 MachineBasicBlock::const_instr_iterator(&MI);
3201 if (II != MBB->instr_begin() &&
3202 (--II)->getOpcode() == PPC::ADJCALLSTACKUP) {
3203 const MachineInstr &CallMI = *(--II);
3204 if (CallMI.isCall() && CallMI.getOperand(0).isGlobal()) {
3205 const Function *CalleeFn =
3206 dyn_cast<Function>(CallMI.getOperand(0).getGlobal());
Hiroshi Inouea7eb78b2017-10-16 12:11:15 +00003207 if (!CalleeFn)
3208 return false;
Hiroshi Inouee3a3e3c2017-10-16 04:12:57 +00003209 const IntegerType *IntTy =
3210 dyn_cast<IntegerType>(CalleeFn->getReturnType());
3211 const AttributeSet &Attrs =
3212 CalleeFn->getAttributes().getRetAttributes();
3213 if (IntTy && IntTy->getBitWidth() <= 32)
3214 return Attrs.hasAttribute(SignExt ? Attribute::SExt :
3215 Attribute::ZExt);
3216 }
3217 }
3218 }
3219 }
3220
3221 // If this is a copy from another register, we recursively check source.
3222 if (!TargetRegisterInfo::isVirtualRegister(SrcReg))
3223 return false;
3224 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
3225 if (SrcMI != NULL)
3226 return isSignOrZeroExtended(*SrcMI, SignExt, Depth);
3227
3228 return false;
3229 }
3230
3231 case PPC::ANDIo:
3232 case PPC::ANDISo:
3233 case PPC::ORI:
3234 case PPC::ORIS:
3235 case PPC::XORI:
3236 case PPC::XORIS:
3237 case PPC::ANDIo8:
3238 case PPC::ANDISo8:
3239 case PPC::ORI8:
3240 case PPC::ORIS8:
3241 case PPC::XORI8:
3242 case PPC::XORIS8: {
3243 // logical operation with 16-bit immediate does not change the upper bits.
3244 // So, we track the operand register as we do for register copy.
3245 unsigned SrcReg = MI.getOperand(1).getReg();
3246 if (!TargetRegisterInfo::isVirtualRegister(SrcReg))
3247 return false;
3248 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
3249 if (SrcMI != NULL)
3250 return isSignOrZeroExtended(*SrcMI, SignExt, Depth);
3251
3252 return false;
3253 }
3254
3255 // If all incoming values are sign-/zero-extended,
Sean Fertileaab3ef72017-11-29 04:09:29 +00003256 // the output of OR, ISEL or PHI is also sign-/zero-extended.
Hiroshi Inouee3a3e3c2017-10-16 04:12:57 +00003257 case PPC::OR:
3258 case PPC::OR8:
3259 case PPC::ISEL:
3260 case PPC::PHI: {
3261 if (Depth >= MAX_DEPTH)
3262 return false;
3263
3264 // The input registers for PHI are operand 1, 3, ...
3265 // The input registers for others are operand 1 and 2.
3266 unsigned E = 3, D = 1;
3267 if (MI.getOpcode() == PPC::PHI) {
3268 E = MI.getNumOperands();
3269 D = 2;
3270 }
3271
3272 for (unsigned I = 1; I != E; I += D) {
3273 if (MI.getOperand(I).isReg()) {
3274 unsigned SrcReg = MI.getOperand(I).getReg();
3275 if (!TargetRegisterInfo::isVirtualRegister(SrcReg))
3276 return false;
3277 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
3278 if (SrcMI == NULL || !isSignOrZeroExtended(*SrcMI, SignExt, Depth+1))
3279 return false;
3280 }
3281 else
3282 return false;
3283 }
3284 return true;
3285 }
3286
Sean Fertileaab3ef72017-11-29 04:09:29 +00003287 // If at least one of the incoming values of an AND is zero extended
3288 // then the output is also zero-extended. If both of the incoming values
3289 // are sign-extended then the output is also sign extended.
3290 case PPC::AND:
3291 case PPC::AND8: {
3292 if (Depth >= MAX_DEPTH)
3293 return false;
3294
3295 assert(MI.getOperand(1).isReg() && MI.getOperand(2).isReg());
3296
3297 unsigned SrcReg1 = MI.getOperand(1).getReg();
3298 unsigned SrcReg2 = MI.getOperand(2).getReg();
3299
3300 if (!TargetRegisterInfo::isVirtualRegister(SrcReg1) ||
3301 !TargetRegisterInfo::isVirtualRegister(SrcReg2))
3302 return false;
3303
3304 const MachineInstr *MISrc1 = MRI->getVRegDef(SrcReg1);
3305 const MachineInstr *MISrc2 = MRI->getVRegDef(SrcReg2);
3306 if (!MISrc1 || !MISrc2)
3307 return false;
3308
3309 if(SignExt)
3310 return isSignOrZeroExtended(*MISrc1, SignExt, Depth+1) &&
3311 isSignOrZeroExtended(*MISrc2, SignExt, Depth+1);
3312 else
3313 return isSignOrZeroExtended(*MISrc1, SignExt, Depth+1) ||
3314 isSignOrZeroExtended(*MISrc2, SignExt, Depth+1);
3315 }
3316
Hiroshi Inouee3a3e3c2017-10-16 04:12:57 +00003317 default:
Hiroshi Inoue5388e662017-10-18 10:31:19 +00003318 break;
Hiroshi Inouee3a3e3c2017-10-16 04:12:57 +00003319 }
3320 return false;
3321}