blob: bf6e40296405de396eccfd48c160a934a67cf4cf [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- PPCInstrInfo.cpp - PowerPC Instruction Information ----------------===//
Misha Brukmanb4402432005-04-21 23:30:14 +00002//
Misha Brukman116f9272004-08-17 04:55:41 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb4402432005-04-21 23:30:14 +00007//
Misha Brukman116f9272004-08-17 04:55:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains the PowerPC implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner6f3b9542005-10-14 23:59:06 +000014#include "PPCInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "MCTargetDesc/PPCPredicates.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000016#include "PPC.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "PPCHazardRecognizers.h"
Owen Andersoneee14602008-01-01 21:11:32 +000018#include "PPCInstrBuilder.h"
Bill Wendling632ea652008-03-03 22:19:16 +000019#include "PPCMachineFunctionInfo.h"
Chris Lattner49cadab2006-06-17 00:01:04 +000020#include "PPCTargetMachine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000022#include "llvm/ADT/Statistic.h"
Hal Finkel174e5902014-03-25 23:29:21 +000023#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Hal Finkelb5aa7e52013-04-08 16:24:03 +000025#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman116f9272004-08-17 04:55:41 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000027#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesenddbf7a82010-02-26 21:09:24 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Hal Finkel9f9f8922012-04-01 19:22:40 +000029#include "llvm/CodeGen/PseudoSourceValue.h"
Eric Christopher1dcea732014-06-12 21:48:52 +000030#include "llvm/CodeGen/ScheduleDAG.h"
Hal Finkel174e5902014-03-25 23:29:21 +000031#include "llvm/CodeGen/SlotIndexes.h"
Hal Finkel934361a2015-01-14 01:07:51 +000032#include "llvm/CodeGen/StackMaps.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000033#include "llvm/MC/MCAsmInfo.h"
Pete Cooper3de83e42015-05-15 21:58:42 +000034#include "llvm/MC/MCInst.h"
Bill Wendling1af20ad2008-03-04 23:13:51 +000035#include "llvm/Support/CommandLine.h"
Hal Finkel174e5902014-03-25 23:29:21 +000036#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000037#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000038#include "llvm/Support/TargetRegistry.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000039#include "llvm/Support/raw_ostream.h"
Misha Brukman116f9272004-08-17 04:55:41 +000040
Dan Gohman20857192010-04-15 17:20:57 +000041using namespace llvm;
Bill Wendling1af20ad2008-03-04 23:13:51 +000042
Chandler Carruthe96dd892014-04-21 22:55:11 +000043#define DEBUG_TYPE "ppc-instr-info"
44
Chandler Carruthd174b722014-04-22 02:03:14 +000045#define GET_INSTRMAP_INFO
46#define GET_INSTRINFO_CTOR_DTOR
47#include "PPCGenInstrInfo.inc"
48
Hal Finkel821e0012012-06-08 15:38:25 +000049static cl::
Hal Finkelc6b5deb2012-06-08 19:19:53 +000050opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
51 cl::desc("Disable analysis for CTR loops"));
Hal Finkel821e0012012-06-08 15:38:25 +000052
Hal Finkele6322392013-04-19 22:08:38 +000053static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt",
Hal Finkelb12da6b2013-04-18 22:54:25 +000054cl::desc("Disable compare instruction optimization"), cl::Hidden);
55
Hal Finkel9dcb3582014-03-27 22:46:28 +000056static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy",
57cl::desc("Causes the backend to crash instead of generating a nop VSX copy"),
58cl::Hidden);
59
Hal Finkel8acae522015-07-14 20:02:02 +000060static cl::opt<bool>
61UseOldLatencyCalc("ppc-old-latency-calc", cl::Hidden,
62 cl::desc("Use the old (incorrect) instruction latency calculation"));
63
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000064// Pin the vtable to this file.
65void PPCInstrInfo::anchor() {}
66
Eric Christopher1dcea732014-06-12 21:48:52 +000067PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI)
68 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
Eric Christopherea178cf2015-03-12 01:42:51 +000069 Subtarget(STI), RI(STI.getTargetMachine()) {}
Chris Lattner49cadab2006-06-17 00:01:04 +000070
Andrew Trick10ffc2b2010-12-24 05:03:26 +000071/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
72/// this target when scheduling the DAG.
Eric Christopherf047bfd2014-06-13 22:38:52 +000073ScheduleHazardRecognizer *
74PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
75 const ScheduleDAG *DAG) const {
76 unsigned Directive =
77 static_cast<const PPCSubtarget *>(STI)->getDarwinDirective();
Hal Finkel742b5352012-08-28 16:12:39 +000078 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
79 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
Eric Christopherf047bfd2014-06-13 22:38:52 +000080 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +000081 static_cast<const PPCSubtarget *>(STI)->getInstrItineraryData();
Hal Finkel563cc052013-12-02 23:52:46 +000082 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel6fa56972011-10-17 04:03:49 +000083 }
Hal Finkel58ca3602011-12-02 04:58:02 +000084
Eric Christopherf047bfd2014-06-13 22:38:52 +000085 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000086}
87
Hal Finkel58ca3602011-12-02 04:58:02 +000088/// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
89/// to use for this target when scheduling the DAG.
Eric Christophercccae792015-01-30 22:02:31 +000090ScheduleHazardRecognizer *
91PPCInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
92 const ScheduleDAG *DAG) const {
Eric Christopher1dcea732014-06-12 21:48:52 +000093 unsigned Directive =
Eric Christophercccae792015-01-30 22:02:31 +000094 DAG->MF.getSubtarget<PPCSubtarget>().getDarwinDirective();
Hal Finkel58ca3602011-12-02 04:58:02 +000095
Will Schmidt970ff642014-06-26 13:36:19 +000096 if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8)
Hal Finkelceb1f122013-12-12 00:19:11 +000097 return new PPCDispatchGroupSBHazardRecognizer(II, DAG);
98
Hal Finkel58ca3602011-12-02 04:58:02 +000099 // Most subtargets use a PPC970 recognizer.
Hal Finkel742b5352012-08-28 16:12:39 +0000100 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
101 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
Eric Christopher1dcea732014-06-12 21:48:52 +0000102 assert(DAG->TII && "No InstrInfo?");
Hal Finkel58ca3602011-12-02 04:58:02 +0000103
Eric Christopher1dcea732014-06-12 21:48:52 +0000104 return new PPCHazardRecognizer970(*DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000105 }
106
Hal Finkel563cc052013-12-02 23:52:46 +0000107 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000108}
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000109
Hal Finkel8acae522015-07-14 20:02:02 +0000110unsigned PPCInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
111 const MachineInstr *MI,
112 unsigned *PredCost) const {
113 if (!ItinData || UseOldLatencyCalc)
114 return PPCGenInstrInfo::getInstrLatency(ItinData, MI, PredCost);
115
116 // The default implementation of getInstrLatency calls getStageLatency, but
117 // getStageLatency does not do the right thing for us. While we have
118 // itinerary, most cores are fully pipelined, and so the itineraries only
119 // express the first part of the pipeline, not every stage. Instead, we need
120 // to use the listed output operand cycle number (using operand 0 here, which
121 // is an output).
122
123 unsigned Latency = 1;
124 unsigned DefClass = MI->getDesc().getSchedClass();
125 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
126 const MachineOperand &MO = MI->getOperand(i);
127 if (!MO.isReg() || !MO.isDef() || MO.isImplicit())
128 continue;
129
130 int Cycle = ItinData->getOperandCycle(DefClass, i);
131 if (Cycle < 0)
132 continue;
133
134 Latency = std::max(Latency, (unsigned) Cycle);
135 }
136
137 return Latency;
138}
Hal Finkelceb1f122013-12-12 00:19:11 +0000139
140int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
141 const MachineInstr *DefMI, unsigned DefIdx,
142 const MachineInstr *UseMI,
143 unsigned UseIdx) const {
144 int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
145 UseMI, UseIdx);
146
147 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
148 unsigned Reg = DefMO.getReg();
149
Hal Finkelceb1f122013-12-12 00:19:11 +0000150 bool IsRegCR;
Andrew Kaylor5c73e1f2015-03-24 23:37:10 +0000151 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Hal Finkelceb1f122013-12-12 00:19:11 +0000152 const MachineRegisterInfo *MRI =
153 &DefMI->getParent()->getParent()->getRegInfo();
154 IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) ||
155 MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass);
156 } else {
157 IsRegCR = PPC::CRRCRegClass.contains(Reg) ||
158 PPC::CRBITRCRegClass.contains(Reg);
159 }
160
161 if (UseMI->isBranch() && IsRegCR) {
162 if (Latency < 0)
163 Latency = getInstrLatency(ItinData, DefMI);
164
165 // On some cores, there is an additional delay between writing to a condition
166 // register, and using it from a branch.
Eric Christopher1dcea732014-06-12 21:48:52 +0000167 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000168 switch (Directive) {
169 default: break;
170 case PPC::DIR_7400:
171 case PPC::DIR_750:
172 case PPC::DIR_970:
173 case PPC::DIR_E5500:
174 case PPC::DIR_PWR4:
175 case PPC::DIR_PWR5:
176 case PPC::DIR_PWR5X:
177 case PPC::DIR_PWR6:
178 case PPC::DIR_PWR6X:
179 case PPC::DIR_PWR7:
Will Schmidt970ff642014-06-26 13:36:19 +0000180 case PPC::DIR_PWR8:
Hal Finkelceb1f122013-12-12 00:19:11 +0000181 Latency += 2;
182 break;
183 }
184 }
185
186 return Latency;
187}
188
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000189// Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
190bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
191 unsigned &SrcReg, unsigned &DstReg,
192 unsigned &SubIdx) const {
193 switch (MI.getOpcode()) {
194 default: return false;
195 case PPC::EXTSW:
196 case PPC::EXTSW_32_64:
197 SrcReg = MI.getOperand(1).getReg();
198 DstReg = MI.getOperand(0).getReg();
199 SubIdx = PPC::sub_32;
200 return true;
201 }
202}
203
Andrew Trickc416ba62010-12-24 04:28:06 +0000204unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner91400bd2006-03-16 22:24:02 +0000205 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000206 // Note: This list must be kept consistent with LoadRegFromStackSlot.
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000207 switch (MI->getOpcode()) {
208 default: break;
209 case PPC::LD:
210 case PPC::LWZ:
211 case PPC::LFS:
212 case PPC::LFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000213 case PPC::RESTORE_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000214 case PPC::RESTORE_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000215 case PPC::LVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000216 case PPC::LXVD2X:
Hal Finkelc93a9a22015-02-25 01:06:45 +0000217 case PPC::QVLFDX:
218 case PPC::QVLFSXs:
219 case PPC::QVLFDXb:
Hal Finkel37714b82013-03-27 21:21:15 +0000220 case PPC::RESTORE_VRSAVE:
221 // Check for the operands added by addFrameReference (the immediate is the
222 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000223 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
224 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000225 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000226 return MI->getOperand(0).getReg();
227 }
228 break;
229 }
230 return 0;
Chris Lattnerc327d712006-02-02 20:16:12 +0000231}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000232
Andrew Trickc416ba62010-12-24 04:28:06 +0000233unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattnerc327d712006-02-02 20:16:12 +0000234 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000235 // Note: This list must be kept consistent with StoreRegToStackSlot.
Chris Lattnerc327d712006-02-02 20:16:12 +0000236 switch (MI->getOpcode()) {
237 default: break;
Nate Begeman4efb3282006-02-02 21:07:50 +0000238 case PPC::STD:
Chris Lattnerc327d712006-02-02 20:16:12 +0000239 case PPC::STW:
240 case PPC::STFS:
241 case PPC::STFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000242 case PPC::SPILL_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000243 case PPC::SPILL_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000244 case PPC::STVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000245 case PPC::STXVD2X:
Hal Finkelc93a9a22015-02-25 01:06:45 +0000246 case PPC::QVSTFDX:
247 case PPC::QVSTFSXs:
248 case PPC::QVSTFDXb:
Hal Finkel37714b82013-03-27 21:21:15 +0000249 case PPC::SPILL_VRSAVE:
250 // Check for the operands added by addFrameReference (the immediate is the
251 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000252 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
253 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000254 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerc327d712006-02-02 20:16:12 +0000255 return MI->getOperand(0).getReg();
256 }
257 break;
258 }
259 return 0;
260}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000261
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000262// commuteInstruction - We can commute rlwimi instructions, but only if the
263// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng03553bb2008-06-16 07:33:11 +0000264MachineInstr *
265PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000266 MachineFunction &MF = *MI->getParent()->getParent();
267
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000268 // Normal instructions can be commuted the obvious way.
Hal Finkel654d43b2013-04-12 02:18:09 +0000269 if (MI->getOpcode() != PPC::RLWIMI &&
Hal Finkel4c6658f2014-12-12 23:59:36 +0000270 MI->getOpcode() != PPC::RLWIMIo)
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000271 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Hal Finkel4c6658f2014-12-12 23:59:36 +0000272 // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a
273 // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because
274 // changing the relative order of the mask operands might change what happens
275 // to the high-bits of the mask (and, thus, the result).
Andrew Trickc416ba62010-12-24 04:28:06 +0000276
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000277 // Cannot commute if it has a non-zero rotate count.
Chris Lattner5c463782007-12-30 20:49:49 +0000278 if (MI->getOperand(3).getImm() != 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000279 return nullptr;
Andrew Trickc416ba62010-12-24 04:28:06 +0000280
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000281 // If we have a zero rotate count, we have:
282 // M = mask(MB,ME)
283 // Op0 = (Op1 & ~M) | (Op2 & M)
284 // Change this to:
285 // M = mask((ME+1)&31, (MB-1)&31)
286 // Op0 = (Op2 & ~M) | (Op1 & M)
287
288 // Swap op1/op2
Evan Cheng244183e2008-02-13 02:46:49 +0000289 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000290 unsigned Reg1 = MI->getOperand(1).getReg();
291 unsigned Reg2 = MI->getOperand(2).getReg();
Andrew Tricke3398282013-12-17 04:50:45 +0000292 unsigned SubReg1 = MI->getOperand(1).getSubReg();
293 unsigned SubReg2 = MI->getOperand(2).getSubReg();
Evan Chengdc2c8742006-11-15 20:58:11 +0000294 bool Reg1IsKill = MI->getOperand(1).isKill();
295 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng03553bb2008-06-16 07:33:11 +0000296 bool ChangeReg0 = false;
Evan Cheng244183e2008-02-13 02:46:49 +0000297 // If machine instrs are no longer in two-address forms, update
298 // destination register as well.
299 if (Reg0 == Reg1) {
300 // Must be two address instruction!
Evan Cheng6cc775f2011-06-28 19:10:37 +0000301 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Cheng244183e2008-02-13 02:46:49 +0000302 "Expecting a two-address instruction!");
Andrew Tricke3398282013-12-17 04:50:45 +0000303 assert(MI->getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
Evan Cheng244183e2008-02-13 02:46:49 +0000304 Reg2IsKill = false;
Evan Cheng03553bb2008-06-16 07:33:11 +0000305 ChangeReg0 = true;
Evan Cheng244183e2008-02-13 02:46:49 +0000306 }
Evan Cheng03553bb2008-06-16 07:33:11 +0000307
308 // Masks.
309 unsigned MB = MI->getOperand(4).getImm();
310 unsigned ME = MI->getOperand(5).getImm();
311
312 if (NewMI) {
313 // Create a new instruction.
314 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
315 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000316 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000317 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
318 .addReg(Reg2, getKillRegState(Reg2IsKill))
319 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng03553bb2008-06-16 07:33:11 +0000320 .addImm((ME+1) & 31)
321 .addImm((MB-1) & 31);
322 }
323
Andrew Tricke3398282013-12-17 04:50:45 +0000324 if (ChangeReg0) {
Evan Cheng03553bb2008-06-16 07:33:11 +0000325 MI->getOperand(0).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000326 MI->getOperand(0).setSubReg(SubReg2);
327 }
Chris Lattner10d63412006-05-04 17:52:23 +0000328 MI->getOperand(2).setReg(Reg1);
329 MI->getOperand(1).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000330 MI->getOperand(2).setSubReg(SubReg1);
331 MI->getOperand(1).setSubReg(SubReg2);
Chris Lattner60055892007-12-30 21:56:09 +0000332 MI->getOperand(2).setIsKill(Reg1IsKill);
333 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000334
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000335 // Swap the mask around.
Chris Lattner5c463782007-12-30 20:49:49 +0000336 MI->getOperand(4).setImm((ME+1) & 31);
337 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000338 return MI;
339}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000340
Hal Finkel6c32ff32014-03-25 19:26:43 +0000341bool PPCInstrInfo::findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
342 unsigned &SrcOpIdx2) const {
343 // For VSX A-Type FMA instructions, it is the first two operands that can be
344 // commuted, however, because the non-encoded tied input operand is listed
345 // first, the operands to swap are actually the second and third.
346
347 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
348 if (AltOpc == -1)
349 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
350
351 SrcOpIdx1 = 2;
352 SrcOpIdx2 = 3;
353 return true;
354}
355
Andrew Trickc416ba62010-12-24 04:28:06 +0000356void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000357 MachineBasicBlock::iterator MI) const {
Hal Finkelceb1f122013-12-12 00:19:11 +0000358 // This function is used for scheduling, and the nop wanted here is the type
359 // that terminates dispatch groups on the POWER cores.
Eric Christopher1dcea732014-06-12 21:48:52 +0000360 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000361 unsigned Opcode;
362 switch (Directive) {
363 default: Opcode = PPC::NOP; break;
364 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
365 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
Will Schmidt970ff642014-06-26 13:36:19 +0000366 case PPC::DIR_PWR8: Opcode = PPC::NOP_GT_PWR7; break; /* FIXME: Update when P8 InstrScheduling model is ready */
Hal Finkelceb1f122013-12-12 00:19:11 +0000367 }
Chris Lattnera47294ed2006-10-13 21:21:17 +0000368
Hal Finkelceb1f122013-12-12 00:19:11 +0000369 DebugLoc DL;
370 BuildMI(MBB, MI, DL, get(Opcode));
371}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000372
Joerg Sonnenberger7ee0f312014-08-08 19:13:23 +0000373/// getNoopForMachoTarget - Return the noop instruction to use for a noop.
374void PPCInstrInfo::getNoopForMachoTarget(MCInst &NopInst) const {
375 NopInst.setOpcode(PPC::NOP);
376}
377
Chris Lattnera47294ed2006-10-13 21:21:17 +0000378// Branch analysis.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000379// Note: If the condition register is set to CTR or CTR8 then this is a
380// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Chris Lattnera47294ed2006-10-13 21:21:17 +0000381bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
382 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000383 SmallVectorImpl<MachineOperand> &Cond,
384 bool AllowModify) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000385 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000386
Chris Lattnera47294ed2006-10-13 21:21:17 +0000387 // If the block has no terminators, it just falls into the block after it.
Benjamin Kramer92861d72015-06-25 13:39:03 +0000388 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
389 if (I == MBB.end())
Dale Johannesen4244d122010-04-02 01:38:09 +0000390 return false;
Benjamin Kramer92861d72015-06-25 13:39:03 +0000391
Dale Johannesen4244d122010-04-02 01:38:09 +0000392 if (!isUnpredicatedTerminator(I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000393 return false;
394
395 // Get the last instruction in the block.
396 MachineInstr *LastInst = I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000397
Chris Lattnera47294ed2006-10-13 21:21:17 +0000398 // If there is only one terminator instruction, process it.
Evan Cheng5514bbe2007-06-08 21:59:56 +0000399 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000400 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000401 if (!LastInst->getOperand(0).isMBB())
402 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000403 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000404 return false;
Chris Lattnere0263792006-11-17 22:14:47 +0000405 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000406 if (!LastInst->getOperand(2).isMBB())
407 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000408 // Block ends with fall-through condbranch.
Chris Lattnera5bb3702007-12-30 23:10:15 +0000409 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000410 Cond.push_back(LastInst->getOperand(0));
411 Cond.push_back(LastInst->getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000412 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000413 } else if (LastInst->getOpcode() == PPC::BC) {
414 if (!LastInst->getOperand(1).isMBB())
415 return true;
416 // Block ends with fall-through condbranch.
417 TBB = LastInst->getOperand(1).getMBB();
418 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
419 Cond.push_back(LastInst->getOperand(0));
420 return false;
421 } else if (LastInst->getOpcode() == PPC::BCn) {
422 if (!LastInst->getOperand(1).isMBB())
423 return true;
424 // Block ends with fall-through condbranch.
425 TBB = LastInst->getOperand(1).getMBB();
426 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
427 Cond.push_back(LastInst->getOperand(0));
428 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000429 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
430 LastInst->getOpcode() == PPC::BDNZ) {
431 if (!LastInst->getOperand(0).isMBB())
432 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000433 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000434 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000435 TBB = LastInst->getOperand(0).getMBB();
436 Cond.push_back(MachineOperand::CreateImm(1));
437 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
438 true));
439 return false;
440 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
441 LastInst->getOpcode() == PPC::BDZ) {
442 if (!LastInst->getOperand(0).isMBB())
443 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000444 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000445 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000446 TBB = LastInst->getOperand(0).getMBB();
447 Cond.push_back(MachineOperand::CreateImm(0));
448 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
449 true));
450 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000451 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000452
Chris Lattnera47294ed2006-10-13 21:21:17 +0000453 // Otherwise, don't know what this is.
454 return true;
455 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000456
Chris Lattnera47294ed2006-10-13 21:21:17 +0000457 // Get the instruction before it if it's a terminator.
458 MachineInstr *SecondLastInst = I;
459
460 // If there are three terminators, we don't know what sort of block this is.
461 if (SecondLastInst && I != MBB.begin() &&
Evan Cheng5514bbe2007-06-08 21:59:56 +0000462 isUnpredicatedTerminator(--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000463 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000464
Chris Lattnere0263792006-11-17 22:14:47 +0000465 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000466 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnera47294ed2006-10-13 21:21:17 +0000467 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000468 if (!SecondLastInst->getOperand(2).isMBB() ||
469 !LastInst->getOperand(0).isMBB())
470 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000471 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000472 Cond.push_back(SecondLastInst->getOperand(0));
473 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattnera5bb3702007-12-30 23:10:15 +0000474 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000475 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000476 } else if (SecondLastInst->getOpcode() == PPC::BC &&
477 LastInst->getOpcode() == PPC::B) {
478 if (!SecondLastInst->getOperand(1).isMBB() ||
479 !LastInst->getOperand(0).isMBB())
480 return true;
481 TBB = SecondLastInst->getOperand(1).getMBB();
482 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
483 Cond.push_back(SecondLastInst->getOperand(0));
484 FBB = LastInst->getOperand(0).getMBB();
485 return false;
486 } else if (SecondLastInst->getOpcode() == PPC::BCn &&
487 LastInst->getOpcode() == PPC::B) {
488 if (!SecondLastInst->getOperand(1).isMBB() ||
489 !LastInst->getOperand(0).isMBB())
490 return true;
491 TBB = SecondLastInst->getOperand(1).getMBB();
492 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
493 Cond.push_back(SecondLastInst->getOperand(0));
494 FBB = LastInst->getOperand(0).getMBB();
495 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000496 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
497 SecondLastInst->getOpcode() == PPC::BDNZ) &&
498 LastInst->getOpcode() == PPC::B) {
499 if (!SecondLastInst->getOperand(0).isMBB() ||
500 !LastInst->getOperand(0).isMBB())
501 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000502 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000503 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000504 TBB = SecondLastInst->getOperand(0).getMBB();
505 Cond.push_back(MachineOperand::CreateImm(1));
506 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
507 true));
508 FBB = LastInst->getOperand(0).getMBB();
509 return false;
510 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
511 SecondLastInst->getOpcode() == PPC::BDZ) &&
512 LastInst->getOpcode() == PPC::B) {
513 if (!SecondLastInst->getOperand(0).isMBB() ||
514 !LastInst->getOperand(0).isMBB())
515 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000516 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000517 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000518 TBB = SecondLastInst->getOperand(0).getMBB();
519 Cond.push_back(MachineOperand::CreateImm(0));
520 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
521 true));
522 FBB = LastInst->getOperand(0).getMBB();
523 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000524 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000525
Dale Johannesenc6855462007-06-13 17:59:52 +0000526 // If the block ends with two PPC:Bs, handle it. The second one is not
527 // executed, so remove it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000528 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesenc6855462007-06-13 17:59:52 +0000529 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000530 if (!SecondLastInst->getOperand(0).isMBB())
531 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000532 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000533 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000534 if (AllowModify)
535 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000536 return false;
537 }
538
Chris Lattnera47294ed2006-10-13 21:21:17 +0000539 // Otherwise, can't handle this.
540 return true;
541}
542
Evan Cheng99be49d2007-05-18 00:05:48 +0000543unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Benjamin Kramer92861d72015-06-25 13:39:03 +0000544 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
545 if (I == MBB.end())
546 return 0;
547
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000548 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000549 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000550 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
551 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000552 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000553
Chris Lattnera47294ed2006-10-13 21:21:17 +0000554 // Remove the branch.
555 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000556
Chris Lattnera47294ed2006-10-13 21:21:17 +0000557 I = MBB.end();
558
Evan Cheng99be49d2007-05-18 00:05:48 +0000559 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000560 --I;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000561 if (I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000562 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000563 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
564 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000565 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000566
Chris Lattnera47294ed2006-10-13 21:21:17 +0000567 // Remove the branch.
568 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000569 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000570}
571
Evan Cheng99be49d2007-05-18 00:05:48 +0000572unsigned
573PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
574 MachineBasicBlock *FBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000575 ArrayRef<MachineOperand> Cond,
Stuart Hastings0125b642010-06-17 22:43:56 +0000576 DebugLoc DL) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000577 // Shouldn't be a fall through.
578 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000579 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000580 "PPC branch conditions have two components!");
Andrew Trickc416ba62010-12-24 04:28:06 +0000581
Eric Christopher1dcea732014-06-12 21:48:52 +0000582 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000583
Chris Lattner94e04442006-10-21 05:36:13 +0000584 // One-way branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000585 if (!FBB) {
Chris Lattner94e04442006-10-21 05:36:13 +0000586 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000587 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000588 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
589 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
590 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
591 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000592 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
593 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
594 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
595 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000596 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000597 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000598 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000599 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000600 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000601
Chris Lattnerd8816602006-10-21 05:42:09 +0000602 // Two-way Conditional Branch.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000603 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
604 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
605 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
606 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000607 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
608 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
609 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
610 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000611 else
612 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000613 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000614 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000615 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000616}
617
Hal Finkeled6a2852013-04-05 23:29:01 +0000618// Select analysis.
619bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000620 ArrayRef<MachineOperand> Cond,
Hal Finkeled6a2852013-04-05 23:29:01 +0000621 unsigned TrueReg, unsigned FalseReg,
622 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000623 if (!Subtarget.hasISEL())
Hal Finkeled6a2852013-04-05 23:29:01 +0000624 return false;
625
626 if (Cond.size() != 2)
627 return false;
628
629 // If this is really a bdnz-like condition, then it cannot be turned into a
630 // select.
631 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
632 return false;
633
634 // Check register classes.
635 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
636 const TargetRegisterClass *RC =
637 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
638 if (!RC)
639 return false;
640
641 // isel is for regular integer GPRs only.
642 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkel8e8618a2013-07-15 20:22:58 +0000643 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
644 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
645 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkeled6a2852013-04-05 23:29:01 +0000646 return false;
647
648 // FIXME: These numbers are for the A2, how well they work for other cores is
649 // an open question. On the A2, the isel instruction has a 2-cycle latency
650 // but single-cycle throughput. These numbers are used in combination with
651 // the MispredictPenalty setting from the active SchedMachineModel.
652 CondCycles = 1;
653 TrueCycles = 1;
654 FalseCycles = 1;
655
656 return true;
657}
658
659void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
660 MachineBasicBlock::iterator MI, DebugLoc dl,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000661 unsigned DestReg, ArrayRef<MachineOperand> Cond,
Hal Finkeled6a2852013-04-05 23:29:01 +0000662 unsigned TrueReg, unsigned FalseReg) const {
663 assert(Cond.size() == 2 &&
664 "PPC branch conditions have two components!");
665
Eric Christopher1dcea732014-06-12 21:48:52 +0000666 assert(Subtarget.hasISEL() &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000667 "Cannot insert select on target without ISEL support");
668
669 // Get the register classes.
670 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
671 const TargetRegisterClass *RC =
672 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
673 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkel8e8618a2013-07-15 20:22:58 +0000674
675 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
676 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
677 assert((Is64Bit ||
678 PPC::GPRCRegClass.hasSubClassEq(RC) ||
679 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000680 "isel is for regular integer GPRs only");
681
Hal Finkel8e8618a2013-07-15 20:22:58 +0000682 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Hal Finkeled6a2852013-04-05 23:29:01 +0000683 unsigned SelectPred = Cond[0].getImm();
684
685 unsigned SubIdx;
686 bool SwapOps;
687 switch (SelectPred) {
688 default: llvm_unreachable("invalid predicate for isel");
689 case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
690 case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
691 case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
692 case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
693 case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
694 case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
695 case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
696 case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
Hal Finkel940ab932014-02-28 00:27:01 +0000697 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break;
698 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
Hal Finkeled6a2852013-04-05 23:29:01 +0000699 }
700
701 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
702 SecondReg = SwapOps ? TrueReg : FalseReg;
703
704 // The first input register of isel cannot be r0. If it is a member
705 // of a register class that can be r0, then copy it first (the
706 // register allocator should eliminate the copy).
707 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
708 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
709 const TargetRegisterClass *FirstRC =
710 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
711 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
712 unsigned OldFirstReg = FirstReg;
713 FirstReg = MRI.createVirtualRegister(FirstRC);
714 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
715 .addReg(OldFirstReg);
716 }
717
718 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
719 .addReg(FirstReg).addReg(SecondReg)
720 .addReg(Cond[1].getReg(), 0, SubIdx);
721}
722
Kit Barton535e69d2015-03-25 19:36:23 +0000723static unsigned getCRBitValue(unsigned CRBit) {
724 unsigned Ret = 4;
725 if (CRBit == PPC::CR0LT || CRBit == PPC::CR1LT ||
726 CRBit == PPC::CR2LT || CRBit == PPC::CR3LT ||
727 CRBit == PPC::CR4LT || CRBit == PPC::CR5LT ||
728 CRBit == PPC::CR6LT || CRBit == PPC::CR7LT)
729 Ret = 3;
730 if (CRBit == PPC::CR0GT || CRBit == PPC::CR1GT ||
731 CRBit == PPC::CR2GT || CRBit == PPC::CR3GT ||
732 CRBit == PPC::CR4GT || CRBit == PPC::CR5GT ||
733 CRBit == PPC::CR6GT || CRBit == PPC::CR7GT)
734 Ret = 2;
735 if (CRBit == PPC::CR0EQ || CRBit == PPC::CR1EQ ||
736 CRBit == PPC::CR2EQ || CRBit == PPC::CR3EQ ||
737 CRBit == PPC::CR4EQ || CRBit == PPC::CR5EQ ||
738 CRBit == PPC::CR6EQ || CRBit == PPC::CR7EQ)
739 Ret = 1;
740 if (CRBit == PPC::CR0UN || CRBit == PPC::CR1UN ||
741 CRBit == PPC::CR2UN || CRBit == PPC::CR3UN ||
742 CRBit == PPC::CR4UN || CRBit == PPC::CR5UN ||
743 CRBit == PPC::CR6UN || CRBit == PPC::CR7UN)
744 Ret = 0;
745
746 assert(Ret != 4 && "Invalid CR bit register");
747 return Ret;
748}
749
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000750void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
751 MachineBasicBlock::iterator I, DebugLoc DL,
752 unsigned DestReg, unsigned SrcReg,
753 bool KillSrc) const {
Hal Finkel27774d92014-03-13 07:58:58 +0000754 // We can end up with self copies and similar things as a result of VSX copy
Hal Finkel9dcb3582014-03-27 22:46:28 +0000755 // legalization. Promote them here.
Hal Finkel27774d92014-03-13 07:58:58 +0000756 const TargetRegisterInfo *TRI = &getRegisterInfo();
757 if (PPC::F8RCRegClass.contains(DestReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000758 PPC::VSRCRegClass.contains(SrcReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000759 unsigned SuperReg =
760 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass);
761
Hal Finkel9dcb3582014-03-27 22:46:28 +0000762 if (VSXSelfCopyCrash && SrcReg == SuperReg)
763 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000764
765 DestReg = SuperReg;
766 } else if (PPC::VRRCRegClass.contains(DestReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000767 PPC::VSRCRegClass.contains(SrcReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000768 unsigned SuperReg =
769 TRI->getMatchingSuperReg(DestReg, PPC::sub_128, &PPC::VSRCRegClass);
770
Hal Finkel9dcb3582014-03-27 22:46:28 +0000771 if (VSXSelfCopyCrash && SrcReg == SuperReg)
772 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000773
774 DestReg = SuperReg;
775 } else if (PPC::F8RCRegClass.contains(SrcReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000776 PPC::VSRCRegClass.contains(DestReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000777 unsigned SuperReg =
778 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass);
779
Hal Finkel9dcb3582014-03-27 22:46:28 +0000780 if (VSXSelfCopyCrash && DestReg == SuperReg)
781 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000782
783 SrcReg = SuperReg;
784 } else if (PPC::VRRCRegClass.contains(SrcReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000785 PPC::VSRCRegClass.contains(DestReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000786 unsigned SuperReg =
787 TRI->getMatchingSuperReg(SrcReg, PPC::sub_128, &PPC::VSRCRegClass);
788
Hal Finkel9dcb3582014-03-27 22:46:28 +0000789 if (VSXSelfCopyCrash && DestReg == SuperReg)
790 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000791
792 SrcReg = SuperReg;
793 }
794
Kit Barton535e69d2015-03-25 19:36:23 +0000795 // Different class register copy
796 if (PPC::CRBITRCRegClass.contains(SrcReg) &&
797 PPC::GPRCRegClass.contains(DestReg)) {
798 unsigned CRReg = getCRFromCRBit(SrcReg);
799 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg)
800 .addReg(CRReg), getKillRegState(KillSrc);
801 // Rotate the CR bit in the CR fields to be the least significant bit and
802 // then mask with 0x1 (MB = ME = 31).
803 BuildMI(MBB, I, DL, get(PPC::RLWINM), DestReg)
804 .addReg(DestReg, RegState::Kill)
805 .addImm(TRI->getEncodingValue(CRReg) * 4 + (4 - getCRBitValue(SrcReg)))
806 .addImm(31)
807 .addImm(31);
808 return;
809 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
810 PPC::G8RCRegClass.contains(DestReg)) {
811 BuildMI(MBB, I, DL, get(PPC::MFOCRF8), DestReg)
812 .addReg(SrcReg), getKillRegState(KillSrc);
813 return;
814 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
815 PPC::GPRCRegClass.contains(DestReg)) {
816 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg)
817 .addReg(SrcReg), getKillRegState(KillSrc);
818 return;
819 }
820
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000821 unsigned Opc;
822 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
823 Opc = PPC::OR;
824 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
825 Opc = PPC::OR8;
826 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
827 Opc = PPC::FMR;
828 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
829 Opc = PPC::MCRF;
830 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
831 Opc = PPC::VOR;
Hal Finkel27774d92014-03-13 07:58:58 +0000832 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg))
Hal Finkelbbad2332014-03-24 09:36:36 +0000833 // There are two different ways this can be done:
Hal Finkel27774d92014-03-13 07:58:58 +0000834 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
835 // issue in VSU pipeline 0.
836 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
837 // can go to either pipeline.
Hal Finkelbbad2332014-03-24 09:36:36 +0000838 // We'll always use xxlor here, because in practically all cases where
839 // copies are generated, they are close enough to some use that the
840 // lower-latency form is preferable.
Hal Finkel27774d92014-03-13 07:58:58 +0000841 Opc = PPC::XXLOR;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +0000842 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg) ||
843 PPC::VSSRCRegClass.contains(DestReg, SrcReg))
Hal Finkel19be5062014-03-29 05:29:01 +0000844 Opc = PPC::XXLORf;
Hal Finkelc93a9a22015-02-25 01:06:45 +0000845 else if (PPC::QFRCRegClass.contains(DestReg, SrcReg))
846 Opc = PPC::QVFMR;
847 else if (PPC::QSRCRegClass.contains(DestReg, SrcReg))
848 Opc = PPC::QVFMRs;
849 else if (PPC::QBRCRegClass.contains(DestReg, SrcReg))
850 Opc = PPC::QVFMRb;
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000851 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
852 Opc = PPC::CROR;
853 else
854 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +0000855
Evan Cheng6cc775f2011-06-28 19:10:37 +0000856 const MCInstrDesc &MCID = get(Opc);
857 if (MCID.getNumOperands() == 3)
858 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000859 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
860 else
Evan Cheng6cc775f2011-06-28 19:10:37 +0000861 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +0000862}
863
Hal Finkel8f6834d2011-12-05 17:55:17 +0000864// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000865bool
Dan Gohman3b460302008-07-07 23:14:23 +0000866PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
867 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000868 int FrameIdx,
869 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000870 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000871 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000872 // Note: If additional store instructions are added here,
873 // update isStoreToStackSlot.
874
Chris Lattner6f306d72010-04-02 20:16:16 +0000875 DebugLoc DL;
Hal Finkel4e703bc2014-01-28 05:32:58 +0000876 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
877 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000878 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
879 .addReg(SrcReg,
880 getKillRegState(isKill)),
881 FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000882 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
883 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000884 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
885 .addReg(SrcReg,
886 getKillRegState(isKill)),
887 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000888 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000889 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000890 .addReg(SrcReg,
891 getKillRegState(isKill)),
892 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000893 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000894 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000895 .addReg(SrcReg,
896 getKillRegState(isKill)),
897 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000898 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000899 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
900 .addReg(SrcReg,
901 getKillRegState(isKill)),
902 FrameIdx));
903 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000904 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000905 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT))
906 .addReg(SrcReg,
907 getKillRegState(isKill)),
908 FrameIdx));
909 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000910 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000911 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
912 .addReg(SrcReg,
913 getKillRegState(isKill)),
914 FrameIdx));
915 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +0000916 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
917 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXVD2X))
918 .addReg(SrcReg,
919 getKillRegState(isKill)),
920 FrameIdx));
921 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +0000922 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
923 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSDX))
924 .addReg(SrcReg,
925 getKillRegState(isKill)),
926 FrameIdx));
927 NonRI = true;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +0000928 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
929 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSSPX))
930 .addReg(SrcReg,
931 getKillRegState(isKill)),
932 FrameIdx));
933 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000934 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +0000935 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +0000936 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000937 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
938 .addReg(SrcReg,
939 getKillRegState(isKill)),
940 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000941 SpillsVRS = true;
Hal Finkelc93a9a22015-02-25 01:06:45 +0000942 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
943 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDX))
944 .addReg(SrcReg,
945 getKillRegState(isKill)),
946 FrameIdx));
947 NonRI = true;
948 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
949 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFSXs))
950 .addReg(SrcReg,
951 getKillRegState(isKill)),
952 FrameIdx));
953 NonRI = true;
954 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
955 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDXb))
956 .addReg(SrcReg,
957 getKillRegState(isKill)),
958 FrameIdx));
959 NonRI = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000960 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000961 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000962 }
Bill Wendling632ea652008-03-03 22:19:16 +0000963
964 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000965}
966
967void
968PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000969 MachineBasicBlock::iterator MI,
970 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000971 const TargetRegisterClass *RC,
972 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000973 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000974 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +0000975
Hal Finkelbb420f12013-03-15 05:06:04 +0000976 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
977 FuncInfo->setHasSpills();
978
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000979 bool NonRI = false, SpillsVRS = false;
980 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
981 NonRI, SpillsVRS))
Bill Wendling632ea652008-03-03 22:19:16 +0000982 FuncInfo->setSpillsCR();
Bill Wendling632ea652008-03-03 22:19:16 +0000983
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000984 if (SpillsVRS)
985 FuncInfo->setSpillsVRSAVE();
986
Hal Finkelfcc51d42013-03-17 04:43:44 +0000987 if (NonRI)
988 FuncInfo->setHasNonRISpills();
989
Owen Andersoneee14602008-01-01 21:11:32 +0000990 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
991 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000992
993 const MachineFrameInfo &MFI = *MF.getFrameInfo();
994 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000995 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000996 MachineMemOperand::MOStore,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000997 MFI.getObjectSize(FrameIdx),
998 MFI.getObjectAlignment(FrameIdx));
999 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +00001000}
1001
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001002bool
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001003PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman3b460302008-07-07 23:14:23 +00001004 unsigned DestReg, int FrameIdx,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +00001005 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +00001006 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001007 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +00001008 // Note: If additional load instructions are added here,
1009 // update isLoadFromStackSlot.
1010
Hal Finkel4e703bc2014-01-28 05:32:58 +00001011 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
1012 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +00001013 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
1014 DestReg), FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +00001015 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
1016 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +00001017 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
1018 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001019 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001020 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +00001021 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001022 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001023 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +00001024 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001025 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +00001026 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1027 get(PPC::RESTORE_CR), DestReg),
1028 FrameIdx));
1029 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001030 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +00001031 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1032 get(PPC::RESTORE_CRBIT), DestReg),
1033 FrameIdx));
1034 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001035 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +00001036 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
1037 FrameIdx));
1038 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +00001039 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
1040 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXVD2X), DestReg),
1041 FrameIdx));
1042 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +00001043 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
1044 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSDX), DestReg),
1045 FrameIdx));
1046 NonRI = true;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001047 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
1048 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSSPX), DestReg),
1049 FrameIdx));
1050 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +00001051 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001052 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +00001053 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +00001054 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1055 get(PPC::RESTORE_VRSAVE),
1056 DestReg),
1057 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001058 SpillsVRS = true;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001059 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
1060 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDX), DestReg),
1061 FrameIdx));
1062 NonRI = true;
1063 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
1064 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFSXs), DestReg),
1065 FrameIdx));
1066 NonRI = true;
1067 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
1068 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDXb), DestReg),
1069 FrameIdx));
1070 NonRI = true;
Owen Andersoneee14602008-01-01 21:11:32 +00001071 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001072 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +00001073 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001074
1075 return false;
Owen Andersoneee14602008-01-01 21:11:32 +00001076}
1077
1078void
1079PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +00001080 MachineBasicBlock::iterator MI,
1081 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +00001082 const TargetRegisterClass *RC,
1083 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +00001084 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +00001085 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +00001086 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001087 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelfcc51d42013-03-17 04:43:44 +00001088
1089 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1090 FuncInfo->setHasSpills();
1091
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001092 bool NonRI = false, SpillsVRS = false;
1093 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
1094 NonRI, SpillsVRS))
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001095 FuncInfo->setSpillsCR();
Hal Finkelfcc51d42013-03-17 04:43:44 +00001096
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001097 if (SpillsVRS)
1098 FuncInfo->setSpillsVRSAVE();
1099
Hal Finkelfcc51d42013-03-17 04:43:44 +00001100 if (NonRI)
1101 FuncInfo->setHasNonRISpills();
1102
Owen Andersoneee14602008-01-01 21:11:32 +00001103 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1104 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001105
1106 const MachineFrameInfo &MFI = *MF.getFrameInfo();
1107 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +00001108 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +00001109 MachineMemOperand::MOLoad,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001110 MFI.getObjectSize(FrameIdx),
1111 MFI.getObjectAlignment(FrameIdx));
1112 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +00001113}
1114
Chris Lattnera47294ed2006-10-13 21:21:17 +00001115bool PPCInstrInfo::
Owen Anderson4f6bf042008-08-14 22:49:33 +00001116ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +00001117 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel96c2d4d2012-06-08 15:38:21 +00001118 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
1119 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
1120 else
1121 // Leave the CR# the same, but invert the condition.
1122 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +00001123 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +00001124}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001125
Hal Finkeld61d4f82013-04-06 19:30:30 +00001126bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
1127 unsigned Reg, MachineRegisterInfo *MRI) const {
1128 // For some instructions, it is legal to fold ZERO into the RA register field.
1129 // A zero immediate should always be loaded with a single li.
1130 unsigned DefOpc = DefMI->getOpcode();
1131 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
1132 return false;
1133 if (!DefMI->getOperand(1).isImm())
1134 return false;
1135 if (DefMI->getOperand(1).getImm() != 0)
1136 return false;
1137
1138 // Note that we cannot here invert the arguments of an isel in order to fold
1139 // a ZERO into what is presented as the second argument. All we have here
1140 // is the condition bit, and that might come from a CR-logical bit operation.
1141
1142 const MCInstrDesc &UseMCID = UseMI->getDesc();
1143
1144 // Only fold into real machine instructions.
1145 if (UseMCID.isPseudo())
1146 return false;
1147
1148 unsigned UseIdx;
1149 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
1150 if (UseMI->getOperand(UseIdx).isReg() &&
1151 UseMI->getOperand(UseIdx).getReg() == Reg)
1152 break;
1153
1154 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
1155 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
1156
1157 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
1158
1159 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
1160 // register (which might also be specified as a pointer class kind).
1161 if (UseInfo->isLookupPtrRegClass()) {
1162 if (UseInfo->RegClass /* Kind */ != 1)
1163 return false;
1164 } else {
1165 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
1166 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
1167 return false;
1168 }
1169
1170 // Make sure this is not tied to an output register (or otherwise
1171 // constrained). This is true for ST?UX registers, for example, which
1172 // are tied to their output registers.
1173 if (UseInfo->Constraints != 0)
1174 return false;
1175
1176 unsigned ZeroReg;
1177 if (UseInfo->isLookupPtrRegClass()) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001178 bool isPPC64 = Subtarget.isPPC64();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001179 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
1180 } else {
1181 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
1182 PPC::ZERO8 : PPC::ZERO;
1183 }
1184
1185 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
1186 UseMI->getOperand(UseIdx).setReg(ZeroReg);
1187
1188 if (DeleteDef)
1189 DefMI->eraseFromParent();
1190
1191 return true;
1192}
1193
Hal Finkel30ae2292013-04-10 18:30:16 +00001194static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
1195 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1196 I != IE; ++I)
1197 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
1198 return true;
1199 return false;
1200}
1201
1202// We should make sure that, if we're going to predicate both sides of a
1203// condition (a diamond), that both sides don't define the counter register. We
1204// can predicate counter-decrement-based branches, but while that predicates
1205// the branching, it does not predicate the counter decrement. If we tried to
1206// merge the triangle into one predicated block, we'd decrement the counter
1207// twice.
1208bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
1209 unsigned NumT, unsigned ExtraT,
1210 MachineBasicBlock &FMBB,
1211 unsigned NumF, unsigned ExtraF,
1212 const BranchProbability &Probability) const {
1213 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
1214}
1215
1216
Hal Finkel5711eca2013-04-09 22:58:37 +00001217bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const {
Hal Finkelf29285a2013-04-11 01:23:34 +00001218 // The predicated branches are identified by their type, not really by the
1219 // explicit presence of a predicate. Furthermore, some of them can be
1220 // predicated more than once. Because if conversion won't try to predicate
1221 // any instruction which already claims to be predicated (by returning true
1222 // here), always return false. In doing so, we let isPredicable() be the
1223 // final word on whether not the instruction can be (further) predicated.
1224
1225 return false;
Hal Finkel5711eca2013-04-09 22:58:37 +00001226}
1227
1228bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
1229 if (!MI->isTerminator())
1230 return false;
1231
1232 // Conditional branch is a special case.
1233 if (MI->isBranch() && !MI->isBarrier())
1234 return true;
1235
1236 return !isPredicated(MI);
1237}
1238
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001239bool PPCInstrInfo::PredicateInstruction(MachineInstr *MI,
1240 ArrayRef<MachineOperand> Pred) const {
Hal Finkel5711eca2013-04-09 22:58:37 +00001241 unsigned OpC = MI->getOpcode();
Hal Finkelf4a22c02015-01-13 17:47:54 +00001242 if (OpC == PPC::BLR || OpC == PPC::BLR8) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001243 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001244 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel5711eca2013-04-09 22:58:37 +00001245 MI->setDesc(get(Pred[0].getImm() ?
1246 (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) :
1247 (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
Hal Finkel940ab932014-02-28 00:27:01 +00001248 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001249 MI->setDesc(get(PPC::BCLR));
1250 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel940ab932014-02-28 00:27:01 +00001251 .addReg(Pred[1].getReg());
1252 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1253 MI->setDesc(get(PPC::BCLRn));
1254 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1255 .addReg(Pred[1].getReg());
1256 } else {
1257 MI->setDesc(get(PPC::BCCLR));
1258 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel5711eca2013-04-09 22:58:37 +00001259 .addImm(Pred[0].getImm())
1260 .addReg(Pred[1].getReg());
1261 }
1262
1263 return true;
1264 } else if (OpC == PPC::B) {
1265 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001266 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel5711eca2013-04-09 22:58:37 +00001267 MI->setDesc(get(Pred[0].getImm() ?
1268 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1269 (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
Hal Finkel940ab932014-02-28 00:27:01 +00001270 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1271 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1272 MI->RemoveOperand(0);
1273
1274 MI->setDesc(get(PPC::BC));
1275 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1276 .addReg(Pred[1].getReg())
1277 .addMBB(MBB);
1278 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1279 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1280 MI->RemoveOperand(0);
1281
1282 MI->setDesc(get(PPC::BCn));
1283 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1284 .addReg(Pred[1].getReg())
1285 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001286 } else {
1287 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1288 MI->RemoveOperand(0);
1289
1290 MI->setDesc(get(PPC::BCC));
1291 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1292 .addImm(Pred[0].getImm())
1293 .addReg(Pred[1].getReg())
1294 .addMBB(MBB);
1295 }
1296
1297 return true;
Hal Finkel500b0042013-04-10 06:42:34 +00001298 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1299 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1300 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1301 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1302
1303 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
Eric Christopher1dcea732014-06-12 21:48:52 +00001304 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel940ab932014-02-28 00:27:01 +00001305
1306 if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1307 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) :
1308 (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1309 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1310 .addReg(Pred[1].getReg());
1311 return true;
1312 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1313 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) :
1314 (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1315 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1316 .addReg(Pred[1].getReg());
1317 return true;
1318 }
1319
1320 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) :
1321 (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
Hal Finkel500b0042013-04-10 06:42:34 +00001322 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1323 .addImm(Pred[0].getImm())
1324 .addReg(Pred[1].getReg());
1325 return true;
Hal Finkel5711eca2013-04-09 22:58:37 +00001326 }
1327
1328 return false;
1329}
1330
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001331bool PPCInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1332 ArrayRef<MachineOperand> Pred2) const {
Hal Finkel5711eca2013-04-09 22:58:37 +00001333 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1334 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1335
1336 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1337 return false;
1338 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1339 return false;
1340
Hal Finkel94a6f382013-12-11 23:12:25 +00001341 // P1 can only subsume P2 if they test the same condition register.
1342 if (Pred1[1].getReg() != Pred2[1].getReg())
1343 return false;
1344
Hal Finkel5711eca2013-04-09 22:58:37 +00001345 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1346 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1347
1348 if (P1 == P2)
1349 return true;
1350
1351 // Does P1 subsume P2, e.g. GE subsumes GT.
1352 if (P1 == PPC::PRED_LE &&
1353 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1354 return true;
1355 if (P1 == PPC::PRED_GE &&
1356 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1357 return true;
1358
1359 return false;
1360}
1361
1362bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI,
1363 std::vector<MachineOperand> &Pred) const {
1364 // Note: At the present time, the contents of Pred from this function is
1365 // unused by IfConversion. This implementation follows ARM by pushing the
1366 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1367 // predicate, instructions defining CTR or CTR8 are also included as
1368 // predicate-defining instructions.
1369
1370 const TargetRegisterClass *RCs[] =
1371 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1372 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1373
1374 bool Found = false;
1375 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1376 const MachineOperand &MO = MI->getOperand(i);
Hal Finkelaf822012013-04-10 07:17:47 +00001377 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001378 const TargetRegisterClass *RC = RCs[c];
Hal Finkelaf822012013-04-10 07:17:47 +00001379 if (MO.isReg()) {
1380 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001381 Pred.push_back(MO);
1382 Found = true;
1383 }
Hal Finkelaf822012013-04-10 07:17:47 +00001384 } else if (MO.isRegMask()) {
1385 for (TargetRegisterClass::iterator I = RC->begin(),
1386 IE = RC->end(); I != IE; ++I)
1387 if (MO.clobbersPhysReg(*I)) {
1388 Pred.push_back(MO);
1389 Found = true;
1390 }
Hal Finkel5711eca2013-04-09 22:58:37 +00001391 }
1392 }
1393 }
1394
1395 return Found;
1396}
1397
1398bool PPCInstrInfo::isPredicable(MachineInstr *MI) const {
1399 unsigned OpC = MI->getOpcode();
1400 switch (OpC) {
1401 default:
1402 return false;
1403 case PPC::B:
1404 case PPC::BLR:
Hal Finkelf4a22c02015-01-13 17:47:54 +00001405 case PPC::BLR8:
Hal Finkel500b0042013-04-10 06:42:34 +00001406 case PPC::BCTR:
1407 case PPC::BCTR8:
1408 case PPC::BCTRL:
1409 case PPC::BCTRL8:
Hal Finkel5711eca2013-04-09 22:58:37 +00001410 return true;
1411 }
1412}
1413
Hal Finkel82656cb2013-04-18 22:15:08 +00001414bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI,
1415 unsigned &SrcReg, unsigned &SrcReg2,
1416 int &Mask, int &Value) const {
1417 unsigned Opc = MI->getOpcode();
1418
1419 switch (Opc) {
1420 default: return false;
1421 case PPC::CMPWI:
1422 case PPC::CMPLWI:
1423 case PPC::CMPDI:
1424 case PPC::CMPLDI:
1425 SrcReg = MI->getOperand(1).getReg();
1426 SrcReg2 = 0;
1427 Value = MI->getOperand(2).getImm();
1428 Mask = 0xFFFF;
1429 return true;
1430 case PPC::CMPW:
1431 case PPC::CMPLW:
1432 case PPC::CMPD:
1433 case PPC::CMPLD:
1434 case PPC::FCMPUS:
1435 case PPC::FCMPUD:
1436 SrcReg = MI->getOperand(1).getReg();
1437 SrcReg2 = MI->getOperand(2).getReg();
1438 return true;
1439 }
1440}
Hal Finkele6322392013-04-19 22:08:38 +00001441
Hal Finkel82656cb2013-04-18 22:15:08 +00001442bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr,
1443 unsigned SrcReg, unsigned SrcReg2,
1444 int Mask, int Value,
1445 const MachineRegisterInfo *MRI) const {
Hal Finkelb12da6b2013-04-18 22:54:25 +00001446 if (DisableCmpOpt)
1447 return false;
1448
Hal Finkel82656cb2013-04-18 22:15:08 +00001449 int OpC = CmpInstr->getOpcode();
1450 unsigned CRReg = CmpInstr->getOperand(0).getReg();
Hal Finkel08e53ee2013-05-08 12:16:14 +00001451
1452 // FP record forms set CR1 based on the execption status bits, not a
1453 // comparison with zero.
1454 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1455 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001456
1457 // The record forms set the condition register based on a signed comparison
1458 // with zero (so says the ISA manual). This is not as straightforward as it
1459 // seems, however, because this is always a 64-bit comparison on PPC64, even
1460 // for instructions that are 32-bit in nature (like slw for example).
1461 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1462 // for equality checks (as those don't depend on the sign). On PPC64,
1463 // we are restricted to equality for unsigned 64-bit comparisons and for
1464 // signed 32-bit comparisons the applicability is more restricted.
Eric Christopher1dcea732014-06-12 21:48:52 +00001465 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel82656cb2013-04-18 22:15:08 +00001466 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1467 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1468 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1469
1470 // Get the unique definition of SrcReg.
1471 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1472 if (!MI) return false;
1473 int MIOpC = MI->getOpcode();
1474
1475 bool equalityOnly = false;
1476 bool noSub = false;
1477 if (isPPC64) {
1478 if (is32BitSignedCompare) {
1479 // We can perform this optimization only if MI is sign-extending.
1480 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1481 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1482 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1483 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1484 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1485 noSub = true;
1486 } else
1487 return false;
1488 } else if (is32BitUnsignedCompare) {
1489 // We can perform this optimization, equality only, if MI is
1490 // zero-extending.
1491 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1492 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
1493 MIOpC == PPC::SRW || MIOpC == PPC::SRWo) {
1494 noSub = true;
1495 equalityOnly = true;
1496 } else
1497 return false;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001498 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001499 equalityOnly = is64BitUnsignedCompare;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001500 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001501 equalityOnly = is32BitUnsignedCompare;
1502
1503 if (equalityOnly) {
1504 // We need to check the uses of the condition register in order to reject
1505 // non-equality comparisons.
Owen Anderson16c6bf42014-03-13 23:12:04 +00001506 for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg),
1507 IE = MRI->use_instr_end(); I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001508 MachineInstr *UseMI = &*I;
1509 if (UseMI->getOpcode() == PPC::BCC) {
1510 unsigned Pred = UseMI->getOperand(0).getImm();
Hal Finkelc3632452013-05-07 17:49:55 +00001511 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE)
1512 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001513 } else if (UseMI->getOpcode() == PPC::ISEL ||
1514 UseMI->getOpcode() == PPC::ISEL8) {
1515 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkelc3632452013-05-07 17:49:55 +00001516 if (SubIdx != PPC::sub_eq)
1517 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001518 } else
1519 return false;
1520 }
1521 }
1522
Hal Finkelc3632452013-05-07 17:49:55 +00001523 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001524
1525 // Scan forward to find the first use of the compare.
1526 for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end();
1527 I != EL; ++I) {
1528 bool FoundUse = false;
Owen Anderson16c6bf42014-03-13 23:12:04 +00001529 for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg),
1530 JE = MRI->use_instr_end(); J != JE; ++J)
Hal Finkel82656cb2013-04-18 22:15:08 +00001531 if (&*J == &*I) {
1532 FoundUse = true;
1533 break;
1534 }
1535
1536 if (FoundUse)
1537 break;
1538 }
1539
Hal Finkel82656cb2013-04-18 22:15:08 +00001540 // There are two possible candidates which can be changed to set CR[01].
1541 // One is MI, the other is a SUB instruction.
1542 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
Craig Topper062a2ba2014-04-25 05:30:21 +00001543 MachineInstr *Sub = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001544 if (SrcReg2 != 0)
1545 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00001546 MI = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001547 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1548 // same BB as the comparison. This is to allow the check below to avoid calls
1549 // (and other explicit clobbers); instead we should really check for these
1550 // more explicitly (in at least a few predecessors).
1551 else if (MI->getParent() != CmpInstr->getParent() || Value != 0) {
1552 // PPC does not have a record-form SUBri.
1553 return false;
1554 }
1555
1556 // Search for Sub.
1557 const TargetRegisterInfo *TRI = &getRegisterInfo();
1558 --I;
Hal Finkelc3632452013-05-07 17:49:55 +00001559
1560 // Get ready to iterate backward from CmpInstr.
1561 MachineBasicBlock::iterator E = MI,
1562 B = CmpInstr->getParent()->begin();
1563
Hal Finkel82656cb2013-04-18 22:15:08 +00001564 for (; I != E && !noSub; --I) {
1565 const MachineInstr &Instr = *I;
1566 unsigned IOpC = Instr.getOpcode();
1567
1568 if (&*I != CmpInstr && (
Hal Finkel08e53ee2013-05-08 12:16:14 +00001569 Instr.modifiesRegister(PPC::CR0, TRI) ||
1570 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel82656cb2013-04-18 22:15:08 +00001571 // This instruction modifies or uses the record condition register after
1572 // the one we want to change. While we could do this transformation, it
1573 // would likely not be profitable. This transformation removes one
1574 // instruction, and so even forcing RA to generate one move probably
1575 // makes it unprofitable.
1576 return false;
1577
1578 // Check whether CmpInstr can be made redundant by the current instruction.
1579 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1580 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1581 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1582 ((Instr.getOperand(1).getReg() == SrcReg &&
1583 Instr.getOperand(2).getReg() == SrcReg2) ||
1584 (Instr.getOperand(1).getReg() == SrcReg2 &&
1585 Instr.getOperand(2).getReg() == SrcReg))) {
1586 Sub = &*I;
1587 break;
1588 }
1589
Hal Finkel82656cb2013-04-18 22:15:08 +00001590 if (I == B)
1591 // The 'and' is below the comparison instruction.
1592 return false;
1593 }
1594
1595 // Return false if no candidates exist.
1596 if (!MI && !Sub)
1597 return false;
1598
1599 // The single candidate is called MI.
1600 if (!MI) MI = Sub;
1601
1602 int NewOpC = -1;
1603 MIOpC = MI->getOpcode();
1604 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1605 NewOpC = MIOpC;
1606 else {
1607 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1608 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1609 NewOpC = MIOpC;
1610 }
1611
1612 // FIXME: On the non-embedded POWER architectures, only some of the record
1613 // forms are fast, and we should use only the fast ones.
1614
1615 // The defining instruction has a record form (or is already a record
1616 // form). It is possible, however, that we'll need to reverse the condition
1617 // code of the users.
1618 if (NewOpC == -1)
1619 return false;
1620
Hal Finkele6322392013-04-19 22:08:38 +00001621 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1622 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
Hal Finkel82656cb2013-04-18 22:15:08 +00001623
1624 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1625 // needs to be updated to be based on SUB. Push the condition code
1626 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1627 // condition code of these operands will be modified.
1628 bool ShouldSwap = false;
1629 if (Sub) {
1630 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1631 Sub->getOperand(2).getReg() == SrcReg;
1632
1633 // The operands to subf are the opposite of sub, so only in the fixed-point
1634 // case, invert the order.
Hal Finkel08e53ee2013-05-08 12:16:14 +00001635 ShouldSwap = !ShouldSwap;
Hal Finkel82656cb2013-04-18 22:15:08 +00001636 }
1637
1638 if (ShouldSwap)
Owen Anderson16c6bf42014-03-13 23:12:04 +00001639 for (MachineRegisterInfo::use_instr_iterator
1640 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1641 I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001642 MachineInstr *UseMI = &*I;
1643 if (UseMI->getOpcode() == PPC::BCC) {
1644 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hal Finkele6322392013-04-19 22:08:38 +00001645 assert((!equalityOnly ||
1646 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1647 "Invalid predicate for equality-only optimization");
Owen Anderson16c6bf42014-03-13 23:12:04 +00001648 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
Hal Finkel0f64e212013-04-20 05:16:26 +00001649 PPC::getSwappedPredicate(Pred)));
Hal Finkel82656cb2013-04-18 22:15:08 +00001650 } else if (UseMI->getOpcode() == PPC::ISEL ||
1651 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkele6322392013-04-19 22:08:38 +00001652 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1653 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1654 "Invalid CR bit for equality-only optimization");
1655
1656 if (NewSubReg == PPC::sub_lt)
1657 NewSubReg = PPC::sub_gt;
1658 else if (NewSubReg == PPC::sub_gt)
1659 NewSubReg = PPC::sub_lt;
1660
Owen Anderson16c6bf42014-03-13 23:12:04 +00001661 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
Hal Finkele6322392013-04-19 22:08:38 +00001662 NewSubReg));
Hal Finkel82656cb2013-04-18 22:15:08 +00001663 } else // We need to abort on a user we don't understand.
1664 return false;
1665 }
1666
1667 // Create a new virtual register to hold the value of the CR set by the
1668 // record-form instruction. If the instruction was not previously in
1669 // record form, then set the kill flag on the CR.
1670 CmpInstr->eraseFromParent();
1671
1672 MachineBasicBlock::iterator MII = MI;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001673 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
Hal Finkel82656cb2013-04-18 22:15:08 +00001674 get(TargetOpcode::COPY), CRReg)
Hal Finkel08e53ee2013-05-08 12:16:14 +00001675 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel82656cb2013-04-18 22:15:08 +00001676
1677 if (MIOpC != NewOpC) {
1678 // We need to be careful here: we're replacing one instruction with
1679 // another, and we need to make sure that we get all of the right
1680 // implicit uses and defs. On the other hand, the caller may be holding
1681 // an iterator to this instruction, and so we can't delete it (this is
1682 // specifically the case if this is the instruction directly after the
1683 // compare).
1684
1685 const MCInstrDesc &NewDesc = get(NewOpC);
1686 MI->setDesc(NewDesc);
1687
1688 if (NewDesc.ImplicitDefs)
1689 for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs();
1690 *ImpDefs; ++ImpDefs)
1691 if (!MI->definesRegister(*ImpDefs))
1692 MI->addOperand(*MI->getParent()->getParent(),
1693 MachineOperand::CreateReg(*ImpDefs, true, true));
1694 if (NewDesc.ImplicitUses)
1695 for (const uint16_t *ImpUses = NewDesc.getImplicitUses();
1696 *ImpUses; ++ImpUses)
1697 if (!MI->readsRegister(*ImpUses))
1698 MI->addOperand(*MI->getParent()->getParent(),
1699 MachineOperand::CreateReg(*ImpUses, false, true));
1700 }
1701
1702 // Modify the condition code of operands in OperandsToUpdate.
1703 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1704 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkele6322392013-04-19 22:08:38 +00001705 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1706 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001707
Hal Finkele6322392013-04-19 22:08:38 +00001708 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1709 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001710
1711 return true;
1712}
1713
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001714/// GetInstSize - Return the number of bytes of code the specified
1715/// instruction may be. This returns the maximum number of bytes.
1716///
1717unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Hal Finkela7bbaf62014-02-02 06:12:27 +00001718 unsigned Opcode = MI->getOpcode();
1719
1720 if (Opcode == PPC::INLINEASM) {
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001721 const MachineFunction *MF = MI->getParent()->getParent();
1722 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +00001723 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Hal Finkel934361a2015-01-14 01:07:51 +00001724 } else if (Opcode == TargetOpcode::STACKMAP) {
1725 return MI->getOperand(1).getImm();
1726 } else if (Opcode == TargetOpcode::PATCHPOINT) {
1727 PatchPointOpers Opers(MI);
1728 return Opers.getMetaOper(PatchPointOpers::NBytesPos).getImm();
Hal Finkela7bbaf62014-02-02 06:12:27 +00001729 } else {
1730 const MCInstrDesc &Desc = get(Opcode);
1731 return Desc.getSize();
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001732 }
1733}
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001734