blob: b4bb50c8093702259efb1ba72ef7519893187897 [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
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000060// Pin the vtable to this file.
61void PPCInstrInfo::anchor() {}
62
Eric Christopher1dcea732014-06-12 21:48:52 +000063PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI)
64 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
Eric Christopherea178cf2015-03-12 01:42:51 +000065 Subtarget(STI), RI(STI.getTargetMachine()) {}
Chris Lattner49cadab2006-06-17 00:01:04 +000066
Andrew Trick10ffc2b2010-12-24 05:03:26 +000067/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
68/// this target when scheduling the DAG.
Eric Christopherf047bfd2014-06-13 22:38:52 +000069ScheduleHazardRecognizer *
70PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
71 const ScheduleDAG *DAG) const {
72 unsigned Directive =
73 static_cast<const PPCSubtarget *>(STI)->getDarwinDirective();
Hal Finkel742b5352012-08-28 16:12:39 +000074 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
75 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
Eric Christopherf047bfd2014-06-13 22:38:52 +000076 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +000077 static_cast<const PPCSubtarget *>(STI)->getInstrItineraryData();
Hal Finkel563cc052013-12-02 23:52:46 +000078 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel6fa56972011-10-17 04:03:49 +000079 }
Hal Finkel58ca3602011-12-02 04:58:02 +000080
Eric Christopherf047bfd2014-06-13 22:38:52 +000081 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000082}
83
Hal Finkel58ca3602011-12-02 04:58:02 +000084/// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
85/// to use for this target when scheduling the DAG.
Eric Christophercccae792015-01-30 22:02:31 +000086ScheduleHazardRecognizer *
87PPCInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
88 const ScheduleDAG *DAG) const {
Eric Christopher1dcea732014-06-12 21:48:52 +000089 unsigned Directive =
Eric Christophercccae792015-01-30 22:02:31 +000090 DAG->MF.getSubtarget<PPCSubtarget>().getDarwinDirective();
Hal Finkel58ca3602011-12-02 04:58:02 +000091
Will Schmidt970ff642014-06-26 13:36:19 +000092 if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8)
Hal Finkelceb1f122013-12-12 00:19:11 +000093 return new PPCDispatchGroupSBHazardRecognizer(II, DAG);
94
Hal Finkel58ca3602011-12-02 04:58:02 +000095 // Most subtargets use a PPC970 recognizer.
Hal Finkel742b5352012-08-28 16:12:39 +000096 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
97 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
Eric Christopher1dcea732014-06-12 21:48:52 +000098 assert(DAG->TII && "No InstrInfo?");
Hal Finkel58ca3602011-12-02 04:58:02 +000099
Eric Christopher1dcea732014-06-12 21:48:52 +0000100 return new PPCHazardRecognizer970(*DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000101 }
102
Hal Finkel563cc052013-12-02 23:52:46 +0000103 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000104}
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000105
Hal Finkelceb1f122013-12-12 00:19:11 +0000106
107int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
108 const MachineInstr *DefMI, unsigned DefIdx,
109 const MachineInstr *UseMI,
110 unsigned UseIdx) const {
111 int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
112 UseMI, UseIdx);
113
114 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
115 unsigned Reg = DefMO.getReg();
116
Hal Finkelceb1f122013-12-12 00:19:11 +0000117 bool IsRegCR;
Andrew Kaylor5c73e1f2015-03-24 23:37:10 +0000118 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Hal Finkelceb1f122013-12-12 00:19:11 +0000119 const MachineRegisterInfo *MRI =
120 &DefMI->getParent()->getParent()->getRegInfo();
121 IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) ||
122 MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass);
123 } else {
124 IsRegCR = PPC::CRRCRegClass.contains(Reg) ||
125 PPC::CRBITRCRegClass.contains(Reg);
126 }
127
128 if (UseMI->isBranch() && IsRegCR) {
129 if (Latency < 0)
130 Latency = getInstrLatency(ItinData, DefMI);
131
132 // On some cores, there is an additional delay between writing to a condition
133 // register, and using it from a branch.
Eric Christopher1dcea732014-06-12 21:48:52 +0000134 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000135 switch (Directive) {
136 default: break;
137 case PPC::DIR_7400:
138 case PPC::DIR_750:
139 case PPC::DIR_970:
140 case PPC::DIR_E5500:
141 case PPC::DIR_PWR4:
142 case PPC::DIR_PWR5:
143 case PPC::DIR_PWR5X:
144 case PPC::DIR_PWR6:
145 case PPC::DIR_PWR6X:
146 case PPC::DIR_PWR7:
Will Schmidt970ff642014-06-26 13:36:19 +0000147 case PPC::DIR_PWR8:
Hal Finkelceb1f122013-12-12 00:19:11 +0000148 Latency += 2;
149 break;
150 }
151 }
152
153 return Latency;
154}
155
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000156// Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
157bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
158 unsigned &SrcReg, unsigned &DstReg,
159 unsigned &SubIdx) const {
160 switch (MI.getOpcode()) {
161 default: return false;
162 case PPC::EXTSW:
163 case PPC::EXTSW_32_64:
164 SrcReg = MI.getOperand(1).getReg();
165 DstReg = MI.getOperand(0).getReg();
166 SubIdx = PPC::sub_32;
167 return true;
168 }
169}
170
Andrew Trickc416ba62010-12-24 04:28:06 +0000171unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner91400bd2006-03-16 22:24:02 +0000172 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000173 // Note: This list must be kept consistent with LoadRegFromStackSlot.
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000174 switch (MI->getOpcode()) {
175 default: break;
176 case PPC::LD:
177 case PPC::LWZ:
178 case PPC::LFS:
179 case PPC::LFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000180 case PPC::RESTORE_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000181 case PPC::RESTORE_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000182 case PPC::LVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000183 case PPC::LXVD2X:
Hal Finkelc93a9a22015-02-25 01:06:45 +0000184 case PPC::QVLFDX:
185 case PPC::QVLFSXs:
186 case PPC::QVLFDXb:
Hal Finkel37714b82013-03-27 21:21:15 +0000187 case PPC::RESTORE_VRSAVE:
188 // Check for the operands added by addFrameReference (the immediate is the
189 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000190 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
191 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000192 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000193 return MI->getOperand(0).getReg();
194 }
195 break;
196 }
197 return 0;
Chris Lattnerc327d712006-02-02 20:16:12 +0000198}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000199
Andrew Trickc416ba62010-12-24 04:28:06 +0000200unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattnerc327d712006-02-02 20:16:12 +0000201 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000202 // Note: This list must be kept consistent with StoreRegToStackSlot.
Chris Lattnerc327d712006-02-02 20:16:12 +0000203 switch (MI->getOpcode()) {
204 default: break;
Nate Begeman4efb3282006-02-02 21:07:50 +0000205 case PPC::STD:
Chris Lattnerc327d712006-02-02 20:16:12 +0000206 case PPC::STW:
207 case PPC::STFS:
208 case PPC::STFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000209 case PPC::SPILL_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000210 case PPC::SPILL_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000211 case PPC::STVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000212 case PPC::STXVD2X:
Hal Finkelc93a9a22015-02-25 01:06:45 +0000213 case PPC::QVSTFDX:
214 case PPC::QVSTFSXs:
215 case PPC::QVSTFDXb:
Hal Finkel37714b82013-03-27 21:21:15 +0000216 case PPC::SPILL_VRSAVE:
217 // Check for the operands added by addFrameReference (the immediate is the
218 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000219 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
220 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000221 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerc327d712006-02-02 20:16:12 +0000222 return MI->getOperand(0).getReg();
223 }
224 break;
225 }
226 return 0;
227}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000228
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000229// commuteInstruction - We can commute rlwimi instructions, but only if the
230// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng03553bb2008-06-16 07:33:11 +0000231MachineInstr *
232PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000233 MachineFunction &MF = *MI->getParent()->getParent();
234
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000235 // Normal instructions can be commuted the obvious way.
Hal Finkel654d43b2013-04-12 02:18:09 +0000236 if (MI->getOpcode() != PPC::RLWIMI &&
Hal Finkel4c6658f2014-12-12 23:59:36 +0000237 MI->getOpcode() != PPC::RLWIMIo)
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000238 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Hal Finkel4c6658f2014-12-12 23:59:36 +0000239 // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a
240 // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because
241 // changing the relative order of the mask operands might change what happens
242 // to the high-bits of the mask (and, thus, the result).
Andrew Trickc416ba62010-12-24 04:28:06 +0000243
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000244 // Cannot commute if it has a non-zero rotate count.
Chris Lattner5c463782007-12-30 20:49:49 +0000245 if (MI->getOperand(3).getImm() != 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000246 return nullptr;
Andrew Trickc416ba62010-12-24 04:28:06 +0000247
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000248 // If we have a zero rotate count, we have:
249 // M = mask(MB,ME)
250 // Op0 = (Op1 & ~M) | (Op2 & M)
251 // Change this to:
252 // M = mask((ME+1)&31, (MB-1)&31)
253 // Op0 = (Op2 & ~M) | (Op1 & M)
254
255 // Swap op1/op2
Evan Cheng244183e2008-02-13 02:46:49 +0000256 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000257 unsigned Reg1 = MI->getOperand(1).getReg();
258 unsigned Reg2 = MI->getOperand(2).getReg();
Andrew Tricke3398282013-12-17 04:50:45 +0000259 unsigned SubReg1 = MI->getOperand(1).getSubReg();
260 unsigned SubReg2 = MI->getOperand(2).getSubReg();
Evan Chengdc2c8742006-11-15 20:58:11 +0000261 bool Reg1IsKill = MI->getOperand(1).isKill();
262 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng03553bb2008-06-16 07:33:11 +0000263 bool ChangeReg0 = false;
Evan Cheng244183e2008-02-13 02:46:49 +0000264 // If machine instrs are no longer in two-address forms, update
265 // destination register as well.
266 if (Reg0 == Reg1) {
267 // Must be two address instruction!
Evan Cheng6cc775f2011-06-28 19:10:37 +0000268 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Cheng244183e2008-02-13 02:46:49 +0000269 "Expecting a two-address instruction!");
Andrew Tricke3398282013-12-17 04:50:45 +0000270 assert(MI->getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
Evan Cheng244183e2008-02-13 02:46:49 +0000271 Reg2IsKill = false;
Evan Cheng03553bb2008-06-16 07:33:11 +0000272 ChangeReg0 = true;
Evan Cheng244183e2008-02-13 02:46:49 +0000273 }
Evan Cheng03553bb2008-06-16 07:33:11 +0000274
275 // Masks.
276 unsigned MB = MI->getOperand(4).getImm();
277 unsigned ME = MI->getOperand(5).getImm();
278
279 if (NewMI) {
280 // Create a new instruction.
281 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
282 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000283 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000284 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
285 .addReg(Reg2, getKillRegState(Reg2IsKill))
286 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng03553bb2008-06-16 07:33:11 +0000287 .addImm((ME+1) & 31)
288 .addImm((MB-1) & 31);
289 }
290
Andrew Tricke3398282013-12-17 04:50:45 +0000291 if (ChangeReg0) {
Evan Cheng03553bb2008-06-16 07:33:11 +0000292 MI->getOperand(0).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000293 MI->getOperand(0).setSubReg(SubReg2);
294 }
Chris Lattner10d63412006-05-04 17:52:23 +0000295 MI->getOperand(2).setReg(Reg1);
296 MI->getOperand(1).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000297 MI->getOperand(2).setSubReg(SubReg1);
298 MI->getOperand(1).setSubReg(SubReg2);
Chris Lattner60055892007-12-30 21:56:09 +0000299 MI->getOperand(2).setIsKill(Reg1IsKill);
300 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000301
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000302 // Swap the mask around.
Chris Lattner5c463782007-12-30 20:49:49 +0000303 MI->getOperand(4).setImm((ME+1) & 31);
304 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000305 return MI;
306}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000307
Hal Finkel6c32ff32014-03-25 19:26:43 +0000308bool PPCInstrInfo::findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
309 unsigned &SrcOpIdx2) const {
310 // For VSX A-Type FMA instructions, it is the first two operands that can be
311 // commuted, however, because the non-encoded tied input operand is listed
312 // first, the operands to swap are actually the second and third.
313
314 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
315 if (AltOpc == -1)
316 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
317
318 SrcOpIdx1 = 2;
319 SrcOpIdx2 = 3;
320 return true;
321}
322
Andrew Trickc416ba62010-12-24 04:28:06 +0000323void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000324 MachineBasicBlock::iterator MI) const {
Hal Finkelceb1f122013-12-12 00:19:11 +0000325 // This function is used for scheduling, and the nop wanted here is the type
326 // that terminates dispatch groups on the POWER cores.
Eric Christopher1dcea732014-06-12 21:48:52 +0000327 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000328 unsigned Opcode;
329 switch (Directive) {
330 default: Opcode = PPC::NOP; break;
331 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
332 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
Will Schmidt970ff642014-06-26 13:36:19 +0000333 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 +0000334 }
Chris Lattnera47294ed2006-10-13 21:21:17 +0000335
Hal Finkelceb1f122013-12-12 00:19:11 +0000336 DebugLoc DL;
337 BuildMI(MBB, MI, DL, get(Opcode));
338}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000339
Joerg Sonnenberger7ee0f312014-08-08 19:13:23 +0000340/// getNoopForMachoTarget - Return the noop instruction to use for a noop.
341void PPCInstrInfo::getNoopForMachoTarget(MCInst &NopInst) const {
342 NopInst.setOpcode(PPC::NOP);
343}
344
Chris Lattnera47294ed2006-10-13 21:21:17 +0000345// Branch analysis.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000346// Note: If the condition register is set to CTR or CTR8 then this is a
347// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Chris Lattnera47294ed2006-10-13 21:21:17 +0000348bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
349 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000350 SmallVectorImpl<MachineOperand> &Cond,
351 bool AllowModify) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000352 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000353
Chris Lattnera47294ed2006-10-13 21:21:17 +0000354 // If the block has no terminators, it just falls into the block after it.
355 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000356 if (I == MBB.begin())
357 return false;
358 --I;
359 while (I->isDebugValue()) {
360 if (I == MBB.begin())
361 return false;
362 --I;
363 }
364 if (!isUnpredicatedTerminator(I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000365 return false;
366
367 // Get the last instruction in the block.
368 MachineInstr *LastInst = I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000369
Chris Lattnera47294ed2006-10-13 21:21:17 +0000370 // If there is only one terminator instruction, process it.
Evan Cheng5514bbe2007-06-08 21:59:56 +0000371 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000372 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000373 if (!LastInst->getOperand(0).isMBB())
374 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000375 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000376 return false;
Chris Lattnere0263792006-11-17 22:14:47 +0000377 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000378 if (!LastInst->getOperand(2).isMBB())
379 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000380 // Block ends with fall-through condbranch.
Chris Lattnera5bb3702007-12-30 23:10:15 +0000381 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000382 Cond.push_back(LastInst->getOperand(0));
383 Cond.push_back(LastInst->getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000384 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000385 } else if (LastInst->getOpcode() == PPC::BC) {
386 if (!LastInst->getOperand(1).isMBB())
387 return true;
388 // Block ends with fall-through condbranch.
389 TBB = LastInst->getOperand(1).getMBB();
390 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
391 Cond.push_back(LastInst->getOperand(0));
392 return false;
393 } else if (LastInst->getOpcode() == PPC::BCn) {
394 if (!LastInst->getOperand(1).isMBB())
395 return true;
396 // Block ends with fall-through condbranch.
397 TBB = LastInst->getOperand(1).getMBB();
398 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
399 Cond.push_back(LastInst->getOperand(0));
400 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000401 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
402 LastInst->getOpcode() == PPC::BDNZ) {
403 if (!LastInst->getOperand(0).isMBB())
404 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000405 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000406 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000407 TBB = LastInst->getOperand(0).getMBB();
408 Cond.push_back(MachineOperand::CreateImm(1));
409 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
410 true));
411 return false;
412 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
413 LastInst->getOpcode() == PPC::BDZ) {
414 if (!LastInst->getOperand(0).isMBB())
415 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000416 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000417 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000418 TBB = LastInst->getOperand(0).getMBB();
419 Cond.push_back(MachineOperand::CreateImm(0));
420 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
421 true));
422 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000423 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000424
Chris Lattnera47294ed2006-10-13 21:21:17 +0000425 // Otherwise, don't know what this is.
426 return true;
427 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000428
Chris Lattnera47294ed2006-10-13 21:21:17 +0000429 // Get the instruction before it if it's a terminator.
430 MachineInstr *SecondLastInst = I;
431
432 // If there are three terminators, we don't know what sort of block this is.
433 if (SecondLastInst && I != MBB.begin() &&
Evan Cheng5514bbe2007-06-08 21:59:56 +0000434 isUnpredicatedTerminator(--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000435 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000436
Chris Lattnere0263792006-11-17 22:14:47 +0000437 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000438 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnera47294ed2006-10-13 21:21:17 +0000439 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000440 if (!SecondLastInst->getOperand(2).isMBB() ||
441 !LastInst->getOperand(0).isMBB())
442 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000443 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000444 Cond.push_back(SecondLastInst->getOperand(0));
445 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattnera5bb3702007-12-30 23:10:15 +0000446 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000447 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000448 } else if (SecondLastInst->getOpcode() == PPC::BC &&
449 LastInst->getOpcode() == PPC::B) {
450 if (!SecondLastInst->getOperand(1).isMBB() ||
451 !LastInst->getOperand(0).isMBB())
452 return true;
453 TBB = SecondLastInst->getOperand(1).getMBB();
454 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
455 Cond.push_back(SecondLastInst->getOperand(0));
456 FBB = LastInst->getOperand(0).getMBB();
457 return false;
458 } else if (SecondLastInst->getOpcode() == PPC::BCn &&
459 LastInst->getOpcode() == PPC::B) {
460 if (!SecondLastInst->getOperand(1).isMBB() ||
461 !LastInst->getOperand(0).isMBB())
462 return true;
463 TBB = SecondLastInst->getOperand(1).getMBB();
464 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
465 Cond.push_back(SecondLastInst->getOperand(0));
466 FBB = LastInst->getOperand(0).getMBB();
467 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000468 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
469 SecondLastInst->getOpcode() == PPC::BDNZ) &&
470 LastInst->getOpcode() == PPC::B) {
471 if (!SecondLastInst->getOperand(0).isMBB() ||
472 !LastInst->getOperand(0).isMBB())
473 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000474 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000475 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000476 TBB = SecondLastInst->getOperand(0).getMBB();
477 Cond.push_back(MachineOperand::CreateImm(1));
478 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
479 true));
480 FBB = LastInst->getOperand(0).getMBB();
481 return false;
482 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
483 SecondLastInst->getOpcode() == PPC::BDZ) &&
484 LastInst->getOpcode() == PPC::B) {
485 if (!SecondLastInst->getOperand(0).isMBB() ||
486 !LastInst->getOperand(0).isMBB())
487 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000488 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000489 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000490 TBB = SecondLastInst->getOperand(0).getMBB();
491 Cond.push_back(MachineOperand::CreateImm(0));
492 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
493 true));
494 FBB = LastInst->getOperand(0).getMBB();
495 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000496 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000497
Dale Johannesenc6855462007-06-13 17:59:52 +0000498 // If the block ends with two PPC:Bs, handle it. The second one is not
499 // executed, so remove it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000500 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesenc6855462007-06-13 17:59:52 +0000501 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000502 if (!SecondLastInst->getOperand(0).isMBB())
503 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000504 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000505 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000506 if (AllowModify)
507 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000508 return false;
509 }
510
Chris Lattnera47294ed2006-10-13 21:21:17 +0000511 // Otherwise, can't handle this.
512 return true;
513}
514
Evan Cheng99be49d2007-05-18 00:05:48 +0000515unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000516 MachineBasicBlock::iterator I = MBB.end();
Evan Cheng99be49d2007-05-18 00:05:48 +0000517 if (I == MBB.begin()) return 0;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000518 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000519 while (I->isDebugValue()) {
520 if (I == MBB.begin())
521 return 0;
522 --I;
523 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000524 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000525 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000526 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
527 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000528 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000529
Chris Lattnera47294ed2006-10-13 21:21:17 +0000530 // Remove the branch.
531 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000532
Chris Lattnera47294ed2006-10-13 21:21:17 +0000533 I = MBB.end();
534
Evan Cheng99be49d2007-05-18 00:05:48 +0000535 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000536 --I;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000537 if (I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000538 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000539 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
540 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000541 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000542
Chris Lattnera47294ed2006-10-13 21:21:17 +0000543 // Remove the branch.
544 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000545 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000546}
547
Evan Cheng99be49d2007-05-18 00:05:48 +0000548unsigned
549PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
550 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000551 const SmallVectorImpl<MachineOperand> &Cond,
552 DebugLoc DL) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000553 // Shouldn't be a fall through.
554 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000555 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000556 "PPC branch conditions have two components!");
Andrew Trickc416ba62010-12-24 04:28:06 +0000557
Eric Christopher1dcea732014-06-12 21:48:52 +0000558 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000559
Chris Lattner94e04442006-10-21 05:36:13 +0000560 // One-way branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000561 if (!FBB) {
Chris Lattner94e04442006-10-21 05:36:13 +0000562 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000563 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000564 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
565 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
566 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
567 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000568 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
569 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
570 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
571 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000572 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000573 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000574 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000575 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000576 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000577
Chris Lattnerd8816602006-10-21 05:42:09 +0000578 // Two-way Conditional Branch.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000579 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
580 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
581 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
582 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000583 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
584 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
585 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
586 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000587 else
588 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000589 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000590 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000591 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000592}
593
Hal Finkeled6a2852013-04-05 23:29:01 +0000594// Select analysis.
595bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
596 const SmallVectorImpl<MachineOperand> &Cond,
597 unsigned TrueReg, unsigned FalseReg,
598 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000599 if (!Subtarget.hasISEL())
Hal Finkeled6a2852013-04-05 23:29:01 +0000600 return false;
601
602 if (Cond.size() != 2)
603 return false;
604
605 // If this is really a bdnz-like condition, then it cannot be turned into a
606 // select.
607 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
608 return false;
609
610 // Check register classes.
611 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
612 const TargetRegisterClass *RC =
613 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
614 if (!RC)
615 return false;
616
617 // isel is for regular integer GPRs only.
618 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkel8e8618a2013-07-15 20:22:58 +0000619 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
620 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
621 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkeled6a2852013-04-05 23:29:01 +0000622 return false;
623
624 // FIXME: These numbers are for the A2, how well they work for other cores is
625 // an open question. On the A2, the isel instruction has a 2-cycle latency
626 // but single-cycle throughput. These numbers are used in combination with
627 // the MispredictPenalty setting from the active SchedMachineModel.
628 CondCycles = 1;
629 TrueCycles = 1;
630 FalseCycles = 1;
631
632 return true;
633}
634
635void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
636 MachineBasicBlock::iterator MI, DebugLoc dl,
637 unsigned DestReg,
638 const SmallVectorImpl<MachineOperand> &Cond,
639 unsigned TrueReg, unsigned FalseReg) const {
640 assert(Cond.size() == 2 &&
641 "PPC branch conditions have two components!");
642
Eric Christopher1dcea732014-06-12 21:48:52 +0000643 assert(Subtarget.hasISEL() &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000644 "Cannot insert select on target without ISEL support");
645
646 // Get the register classes.
647 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
648 const TargetRegisterClass *RC =
649 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
650 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkel8e8618a2013-07-15 20:22:58 +0000651
652 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
653 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
654 assert((Is64Bit ||
655 PPC::GPRCRegClass.hasSubClassEq(RC) ||
656 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000657 "isel is for regular integer GPRs only");
658
Hal Finkel8e8618a2013-07-15 20:22:58 +0000659 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Hal Finkeled6a2852013-04-05 23:29:01 +0000660 unsigned SelectPred = Cond[0].getImm();
661
662 unsigned SubIdx;
663 bool SwapOps;
664 switch (SelectPred) {
665 default: llvm_unreachable("invalid predicate for isel");
666 case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
667 case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
668 case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
669 case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
670 case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
671 case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
672 case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
673 case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
Hal Finkel940ab932014-02-28 00:27:01 +0000674 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break;
675 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
Hal Finkeled6a2852013-04-05 23:29:01 +0000676 }
677
678 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
679 SecondReg = SwapOps ? TrueReg : FalseReg;
680
681 // The first input register of isel cannot be r0. If it is a member
682 // of a register class that can be r0, then copy it first (the
683 // register allocator should eliminate the copy).
684 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
685 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
686 const TargetRegisterClass *FirstRC =
687 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
688 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
689 unsigned OldFirstReg = FirstReg;
690 FirstReg = MRI.createVirtualRegister(FirstRC);
691 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
692 .addReg(OldFirstReg);
693 }
694
695 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
696 .addReg(FirstReg).addReg(SecondReg)
697 .addReg(Cond[1].getReg(), 0, SubIdx);
698}
699
Kit Barton535e69d2015-03-25 19:36:23 +0000700static unsigned getCRBitValue(unsigned CRBit) {
701 unsigned Ret = 4;
702 if (CRBit == PPC::CR0LT || CRBit == PPC::CR1LT ||
703 CRBit == PPC::CR2LT || CRBit == PPC::CR3LT ||
704 CRBit == PPC::CR4LT || CRBit == PPC::CR5LT ||
705 CRBit == PPC::CR6LT || CRBit == PPC::CR7LT)
706 Ret = 3;
707 if (CRBit == PPC::CR0GT || CRBit == PPC::CR1GT ||
708 CRBit == PPC::CR2GT || CRBit == PPC::CR3GT ||
709 CRBit == PPC::CR4GT || CRBit == PPC::CR5GT ||
710 CRBit == PPC::CR6GT || CRBit == PPC::CR7GT)
711 Ret = 2;
712 if (CRBit == PPC::CR0EQ || CRBit == PPC::CR1EQ ||
713 CRBit == PPC::CR2EQ || CRBit == PPC::CR3EQ ||
714 CRBit == PPC::CR4EQ || CRBit == PPC::CR5EQ ||
715 CRBit == PPC::CR6EQ || CRBit == PPC::CR7EQ)
716 Ret = 1;
717 if (CRBit == PPC::CR0UN || CRBit == PPC::CR1UN ||
718 CRBit == PPC::CR2UN || CRBit == PPC::CR3UN ||
719 CRBit == PPC::CR4UN || CRBit == PPC::CR5UN ||
720 CRBit == PPC::CR6UN || CRBit == PPC::CR7UN)
721 Ret = 0;
722
723 assert(Ret != 4 && "Invalid CR bit register");
724 return Ret;
725}
726
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000727void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
728 MachineBasicBlock::iterator I, DebugLoc DL,
729 unsigned DestReg, unsigned SrcReg,
730 bool KillSrc) const {
Hal Finkel27774d92014-03-13 07:58:58 +0000731 // We can end up with self copies and similar things as a result of VSX copy
Hal Finkel9dcb3582014-03-27 22:46:28 +0000732 // legalization. Promote them here.
Hal Finkel27774d92014-03-13 07:58:58 +0000733 const TargetRegisterInfo *TRI = &getRegisterInfo();
734 if (PPC::F8RCRegClass.contains(DestReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000735 PPC::VSRCRegClass.contains(SrcReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000736 unsigned SuperReg =
737 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass);
738
Hal Finkel9dcb3582014-03-27 22:46:28 +0000739 if (VSXSelfCopyCrash && SrcReg == SuperReg)
740 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000741
742 DestReg = SuperReg;
743 } else if (PPC::VRRCRegClass.contains(DestReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000744 PPC::VSRCRegClass.contains(SrcReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000745 unsigned SuperReg =
746 TRI->getMatchingSuperReg(DestReg, PPC::sub_128, &PPC::VSRCRegClass);
747
Hal Finkel9dcb3582014-03-27 22:46:28 +0000748 if (VSXSelfCopyCrash && SrcReg == SuperReg)
749 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000750
751 DestReg = SuperReg;
752 } else if (PPC::F8RCRegClass.contains(SrcReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000753 PPC::VSRCRegClass.contains(DestReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000754 unsigned SuperReg =
755 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass);
756
Hal Finkel9dcb3582014-03-27 22:46:28 +0000757 if (VSXSelfCopyCrash && DestReg == SuperReg)
758 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000759
760 SrcReg = SuperReg;
761 } else if (PPC::VRRCRegClass.contains(SrcReg) &&
Hal Finkel5cedafb2015-02-16 23:46:30 +0000762 PPC::VSRCRegClass.contains(DestReg)) {
Hal Finkel27774d92014-03-13 07:58:58 +0000763 unsigned SuperReg =
764 TRI->getMatchingSuperReg(SrcReg, PPC::sub_128, &PPC::VSRCRegClass);
765
Hal Finkel9dcb3582014-03-27 22:46:28 +0000766 if (VSXSelfCopyCrash && DestReg == SuperReg)
767 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000768
769 SrcReg = SuperReg;
770 }
771
Kit Barton535e69d2015-03-25 19:36:23 +0000772 // Different class register copy
773 if (PPC::CRBITRCRegClass.contains(SrcReg) &&
774 PPC::GPRCRegClass.contains(DestReg)) {
775 unsigned CRReg = getCRFromCRBit(SrcReg);
776 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg)
777 .addReg(CRReg), getKillRegState(KillSrc);
778 // Rotate the CR bit in the CR fields to be the least significant bit and
779 // then mask with 0x1 (MB = ME = 31).
780 BuildMI(MBB, I, DL, get(PPC::RLWINM), DestReg)
781 .addReg(DestReg, RegState::Kill)
782 .addImm(TRI->getEncodingValue(CRReg) * 4 + (4 - getCRBitValue(SrcReg)))
783 .addImm(31)
784 .addImm(31);
785 return;
786 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
787 PPC::G8RCRegClass.contains(DestReg)) {
788 BuildMI(MBB, I, DL, get(PPC::MFOCRF8), DestReg)
789 .addReg(SrcReg), getKillRegState(KillSrc);
790 return;
791 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
792 PPC::GPRCRegClass.contains(DestReg)) {
793 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg)
794 .addReg(SrcReg), getKillRegState(KillSrc);
795 return;
796 }
797
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000798 unsigned Opc;
799 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
800 Opc = PPC::OR;
801 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
802 Opc = PPC::OR8;
803 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
804 Opc = PPC::FMR;
805 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
806 Opc = PPC::MCRF;
807 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
808 Opc = PPC::VOR;
Hal Finkel27774d92014-03-13 07:58:58 +0000809 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg))
Hal Finkelbbad2332014-03-24 09:36:36 +0000810 // There are two different ways this can be done:
Hal Finkel27774d92014-03-13 07:58:58 +0000811 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
812 // issue in VSU pipeline 0.
813 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
814 // can go to either pipeline.
Hal Finkelbbad2332014-03-24 09:36:36 +0000815 // We'll always use xxlor here, because in practically all cases where
816 // copies are generated, they are close enough to some use that the
817 // lower-latency form is preferable.
Hal Finkel27774d92014-03-13 07:58:58 +0000818 Opc = PPC::XXLOR;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +0000819 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg) ||
820 PPC::VSSRCRegClass.contains(DestReg, SrcReg))
Hal Finkel19be5062014-03-29 05:29:01 +0000821 Opc = PPC::XXLORf;
Hal Finkelc93a9a22015-02-25 01:06:45 +0000822 else if (PPC::QFRCRegClass.contains(DestReg, SrcReg))
823 Opc = PPC::QVFMR;
824 else if (PPC::QSRCRegClass.contains(DestReg, SrcReg))
825 Opc = PPC::QVFMRs;
826 else if (PPC::QBRCRegClass.contains(DestReg, SrcReg))
827 Opc = PPC::QVFMRb;
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000828 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
829 Opc = PPC::CROR;
830 else
831 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +0000832
Evan Cheng6cc775f2011-06-28 19:10:37 +0000833 const MCInstrDesc &MCID = get(Opc);
834 if (MCID.getNumOperands() == 3)
835 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000836 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
837 else
Evan Cheng6cc775f2011-06-28 19:10:37 +0000838 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +0000839}
840
Hal Finkel8f6834d2011-12-05 17:55:17 +0000841// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000842bool
Dan Gohman3b460302008-07-07 23:14:23 +0000843PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
844 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000845 int FrameIdx,
846 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000847 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000848 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000849 // Note: If additional store instructions are added here,
850 // update isStoreToStackSlot.
851
Chris Lattner6f306d72010-04-02 20:16:16 +0000852 DebugLoc DL;
Hal Finkel4e703bc2014-01-28 05:32:58 +0000853 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
854 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000855 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
856 .addReg(SrcReg,
857 getKillRegState(isKill)),
858 FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000859 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
860 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000861 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
862 .addReg(SrcReg,
863 getKillRegState(isKill)),
864 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000865 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000866 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000867 .addReg(SrcReg,
868 getKillRegState(isKill)),
869 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000870 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000871 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000872 .addReg(SrcReg,
873 getKillRegState(isKill)),
874 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000875 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000876 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
877 .addReg(SrcReg,
878 getKillRegState(isKill)),
879 FrameIdx));
880 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000881 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000882 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT))
883 .addReg(SrcReg,
884 getKillRegState(isKill)),
885 FrameIdx));
886 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000887 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000888 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
889 .addReg(SrcReg,
890 getKillRegState(isKill)),
891 FrameIdx));
892 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +0000893 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
894 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXVD2X))
895 .addReg(SrcReg,
896 getKillRegState(isKill)),
897 FrameIdx));
898 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +0000899 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
900 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSDX))
901 .addReg(SrcReg,
902 getKillRegState(isKill)),
903 FrameIdx));
904 NonRI = true;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +0000905 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
906 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSSPX))
907 .addReg(SrcReg,
908 getKillRegState(isKill)),
909 FrameIdx));
910 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000911 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +0000912 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +0000913 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000914 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
915 .addReg(SrcReg,
916 getKillRegState(isKill)),
917 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000918 SpillsVRS = true;
Hal Finkelc93a9a22015-02-25 01:06:45 +0000919 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
920 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDX))
921 .addReg(SrcReg,
922 getKillRegState(isKill)),
923 FrameIdx));
924 NonRI = true;
925 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
926 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFSXs))
927 .addReg(SrcReg,
928 getKillRegState(isKill)),
929 FrameIdx));
930 NonRI = true;
931 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
932 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDXb))
933 .addReg(SrcReg,
934 getKillRegState(isKill)),
935 FrameIdx));
936 NonRI = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000937 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000938 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000939 }
Bill Wendling632ea652008-03-03 22:19:16 +0000940
941 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000942}
943
944void
945PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000946 MachineBasicBlock::iterator MI,
947 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000948 const TargetRegisterClass *RC,
949 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000950 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000951 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +0000952
Hal Finkelbb420f12013-03-15 05:06:04 +0000953 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
954 FuncInfo->setHasSpills();
955
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000956 bool NonRI = false, SpillsVRS = false;
957 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
958 NonRI, SpillsVRS))
Bill Wendling632ea652008-03-03 22:19:16 +0000959 FuncInfo->setSpillsCR();
Bill Wendling632ea652008-03-03 22:19:16 +0000960
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000961 if (SpillsVRS)
962 FuncInfo->setSpillsVRSAVE();
963
Hal Finkelfcc51d42013-03-17 04:43:44 +0000964 if (NonRI)
965 FuncInfo->setHasNonRISpills();
966
Owen Andersoneee14602008-01-01 21:11:32 +0000967 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
968 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000969
970 const MachineFrameInfo &MFI = *MF.getFrameInfo();
971 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000972 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000973 MachineMemOperand::MOStore,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000974 MFI.getObjectSize(FrameIdx),
975 MFI.getObjectAlignment(FrameIdx));
976 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000977}
978
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000979bool
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000980PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman3b460302008-07-07 23:14:23 +0000981 unsigned DestReg, int FrameIdx,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000982 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000983 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000984 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000985 // Note: If additional load instructions are added here,
986 // update isLoadFromStackSlot.
987
Hal Finkel4e703bc2014-01-28 05:32:58 +0000988 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
989 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000990 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
991 DestReg), FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000992 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
993 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000994 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
995 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000996 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000997 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000998 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000999 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001000 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +00001001 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +00001002 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +00001003 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1004 get(PPC::RESTORE_CR), DestReg),
1005 FrameIdx));
1006 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001007 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +00001008 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1009 get(PPC::RESTORE_CRBIT), DestReg),
1010 FrameIdx));
1011 return true;
Craig Topperabadc662012-04-20 06:31:50 +00001012 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +00001013 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
1014 FrameIdx));
1015 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +00001016 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
1017 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXVD2X), DestReg),
1018 FrameIdx));
1019 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +00001020 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
1021 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSDX), DestReg),
1022 FrameIdx));
1023 NonRI = true;
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00001024 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
1025 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSSPX), DestReg),
1026 FrameIdx));
1027 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +00001028 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001029 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +00001030 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +00001031 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1032 get(PPC::RESTORE_VRSAVE),
1033 DestReg),
1034 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001035 SpillsVRS = true;
Hal Finkelc93a9a22015-02-25 01:06:45 +00001036 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
1037 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDX), DestReg),
1038 FrameIdx));
1039 NonRI = true;
1040 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
1041 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFSXs), DestReg),
1042 FrameIdx));
1043 NonRI = true;
1044 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
1045 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDXb), DestReg),
1046 FrameIdx));
1047 NonRI = true;
Owen Andersoneee14602008-01-01 21:11:32 +00001048 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001049 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +00001050 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001051
1052 return false;
Owen Andersoneee14602008-01-01 21:11:32 +00001053}
1054
1055void
1056PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +00001057 MachineBasicBlock::iterator MI,
1058 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +00001059 const TargetRegisterClass *RC,
1060 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +00001061 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +00001062 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +00001063 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +00001064 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelfcc51d42013-03-17 04:43:44 +00001065
1066 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1067 FuncInfo->setHasSpills();
1068
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001069 bool NonRI = false, SpillsVRS = false;
1070 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
1071 NonRI, SpillsVRS))
Hal Finkelbde7f8f2011-12-06 20:55:36 +00001072 FuncInfo->setSpillsCR();
Hal Finkelfcc51d42013-03-17 04:43:44 +00001073
Hal Finkelcc1eeda2013-03-23 22:06:03 +00001074 if (SpillsVRS)
1075 FuncInfo->setSpillsVRSAVE();
1076
Hal Finkelfcc51d42013-03-17 04:43:44 +00001077 if (NonRI)
1078 FuncInfo->setHasNonRISpills();
1079
Owen Andersoneee14602008-01-01 21:11:32 +00001080 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1081 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001082
1083 const MachineFrameInfo &MFI = *MF.getFrameInfo();
1084 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +00001085 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +00001086 MachineMemOperand::MOLoad,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +00001087 MFI.getObjectSize(FrameIdx),
1088 MFI.getObjectAlignment(FrameIdx));
1089 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +00001090}
1091
Chris Lattnera47294ed2006-10-13 21:21:17 +00001092bool PPCInstrInfo::
Owen Anderson4f6bf042008-08-14 22:49:33 +00001093ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +00001094 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel96c2d4d2012-06-08 15:38:21 +00001095 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
1096 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
1097 else
1098 // Leave the CR# the same, but invert the condition.
1099 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +00001100 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +00001101}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001102
Hal Finkeld61d4f82013-04-06 19:30:30 +00001103bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
1104 unsigned Reg, MachineRegisterInfo *MRI) const {
1105 // For some instructions, it is legal to fold ZERO into the RA register field.
1106 // A zero immediate should always be loaded with a single li.
1107 unsigned DefOpc = DefMI->getOpcode();
1108 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
1109 return false;
1110 if (!DefMI->getOperand(1).isImm())
1111 return false;
1112 if (DefMI->getOperand(1).getImm() != 0)
1113 return false;
1114
1115 // Note that we cannot here invert the arguments of an isel in order to fold
1116 // a ZERO into what is presented as the second argument. All we have here
1117 // is the condition bit, and that might come from a CR-logical bit operation.
1118
1119 const MCInstrDesc &UseMCID = UseMI->getDesc();
1120
1121 // Only fold into real machine instructions.
1122 if (UseMCID.isPseudo())
1123 return false;
1124
1125 unsigned UseIdx;
1126 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
1127 if (UseMI->getOperand(UseIdx).isReg() &&
1128 UseMI->getOperand(UseIdx).getReg() == Reg)
1129 break;
1130
1131 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
1132 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
1133
1134 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
1135
1136 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
1137 // register (which might also be specified as a pointer class kind).
1138 if (UseInfo->isLookupPtrRegClass()) {
1139 if (UseInfo->RegClass /* Kind */ != 1)
1140 return false;
1141 } else {
1142 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
1143 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
1144 return false;
1145 }
1146
1147 // Make sure this is not tied to an output register (or otherwise
1148 // constrained). This is true for ST?UX registers, for example, which
1149 // are tied to their output registers.
1150 if (UseInfo->Constraints != 0)
1151 return false;
1152
1153 unsigned ZeroReg;
1154 if (UseInfo->isLookupPtrRegClass()) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001155 bool isPPC64 = Subtarget.isPPC64();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001156 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
1157 } else {
1158 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
1159 PPC::ZERO8 : PPC::ZERO;
1160 }
1161
1162 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
1163 UseMI->getOperand(UseIdx).setReg(ZeroReg);
1164
1165 if (DeleteDef)
1166 DefMI->eraseFromParent();
1167
1168 return true;
1169}
1170
Hal Finkel30ae2292013-04-10 18:30:16 +00001171static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
1172 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1173 I != IE; ++I)
1174 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
1175 return true;
1176 return false;
1177}
1178
1179// We should make sure that, if we're going to predicate both sides of a
1180// condition (a diamond), that both sides don't define the counter register. We
1181// can predicate counter-decrement-based branches, but while that predicates
1182// the branching, it does not predicate the counter decrement. If we tried to
1183// merge the triangle into one predicated block, we'd decrement the counter
1184// twice.
1185bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
1186 unsigned NumT, unsigned ExtraT,
1187 MachineBasicBlock &FMBB,
1188 unsigned NumF, unsigned ExtraF,
1189 const BranchProbability &Probability) const {
1190 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
1191}
1192
1193
Hal Finkel5711eca2013-04-09 22:58:37 +00001194bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const {
Hal Finkelf29285a2013-04-11 01:23:34 +00001195 // The predicated branches are identified by their type, not really by the
1196 // explicit presence of a predicate. Furthermore, some of them can be
1197 // predicated more than once. Because if conversion won't try to predicate
1198 // any instruction which already claims to be predicated (by returning true
1199 // here), always return false. In doing so, we let isPredicable() be the
1200 // final word on whether not the instruction can be (further) predicated.
1201
1202 return false;
Hal Finkel5711eca2013-04-09 22:58:37 +00001203}
1204
1205bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
1206 if (!MI->isTerminator())
1207 return false;
1208
1209 // Conditional branch is a special case.
1210 if (MI->isBranch() && !MI->isBarrier())
1211 return true;
1212
1213 return !isPredicated(MI);
1214}
1215
1216bool PPCInstrInfo::PredicateInstruction(
1217 MachineInstr *MI,
1218 const SmallVectorImpl<MachineOperand> &Pred) const {
1219 unsigned OpC = MI->getOpcode();
Hal Finkelf4a22c02015-01-13 17:47:54 +00001220 if (OpC == PPC::BLR || OpC == PPC::BLR8) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001221 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001222 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel5711eca2013-04-09 22:58:37 +00001223 MI->setDesc(get(Pred[0].getImm() ?
1224 (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) :
1225 (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
Hal Finkel940ab932014-02-28 00:27:01 +00001226 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001227 MI->setDesc(get(PPC::BCLR));
1228 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel940ab932014-02-28 00:27:01 +00001229 .addReg(Pred[1].getReg());
1230 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1231 MI->setDesc(get(PPC::BCLRn));
1232 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1233 .addReg(Pred[1].getReg());
1234 } else {
1235 MI->setDesc(get(PPC::BCCLR));
1236 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel5711eca2013-04-09 22:58:37 +00001237 .addImm(Pred[0].getImm())
1238 .addReg(Pred[1].getReg());
1239 }
1240
1241 return true;
1242 } else if (OpC == PPC::B) {
1243 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::BDNZ8 : PPC::BDNZ) :
1247 (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
Hal Finkel940ab932014-02-28 00:27:01 +00001248 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1249 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1250 MI->RemoveOperand(0);
1251
1252 MI->setDesc(get(PPC::BC));
1253 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1254 .addReg(Pred[1].getReg())
1255 .addMBB(MBB);
1256 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1257 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1258 MI->RemoveOperand(0);
1259
1260 MI->setDesc(get(PPC::BCn));
1261 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1262 .addReg(Pred[1].getReg())
1263 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001264 } else {
1265 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1266 MI->RemoveOperand(0);
1267
1268 MI->setDesc(get(PPC::BCC));
1269 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1270 .addImm(Pred[0].getImm())
1271 .addReg(Pred[1].getReg())
1272 .addMBB(MBB);
1273 }
1274
1275 return true;
Hal Finkel500b0042013-04-10 06:42:34 +00001276 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1277 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1278 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1279 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1280
1281 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
Eric Christopher1dcea732014-06-12 21:48:52 +00001282 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel940ab932014-02-28 00:27:01 +00001283
1284 if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1285 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) :
1286 (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1287 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1288 .addReg(Pred[1].getReg());
1289 return true;
1290 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1291 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) :
1292 (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1293 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1294 .addReg(Pred[1].getReg());
1295 return true;
1296 }
1297
1298 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) :
1299 (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
Hal Finkel500b0042013-04-10 06:42:34 +00001300 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1301 .addImm(Pred[0].getImm())
1302 .addReg(Pred[1].getReg());
1303 return true;
Hal Finkel5711eca2013-04-09 22:58:37 +00001304 }
1305
1306 return false;
1307}
1308
1309bool PPCInstrInfo::SubsumesPredicate(
1310 const SmallVectorImpl<MachineOperand> &Pred1,
1311 const SmallVectorImpl<MachineOperand> &Pred2) const {
1312 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1313 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1314
1315 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1316 return false;
1317 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1318 return false;
1319
Hal Finkel94a6f382013-12-11 23:12:25 +00001320 // P1 can only subsume P2 if they test the same condition register.
1321 if (Pred1[1].getReg() != Pred2[1].getReg())
1322 return false;
1323
Hal Finkel5711eca2013-04-09 22:58:37 +00001324 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1325 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1326
1327 if (P1 == P2)
1328 return true;
1329
1330 // Does P1 subsume P2, e.g. GE subsumes GT.
1331 if (P1 == PPC::PRED_LE &&
1332 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1333 return true;
1334 if (P1 == PPC::PRED_GE &&
1335 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1336 return true;
1337
1338 return false;
1339}
1340
1341bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI,
1342 std::vector<MachineOperand> &Pred) const {
1343 // Note: At the present time, the contents of Pred from this function is
1344 // unused by IfConversion. This implementation follows ARM by pushing the
1345 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1346 // predicate, instructions defining CTR or CTR8 are also included as
1347 // predicate-defining instructions.
1348
1349 const TargetRegisterClass *RCs[] =
1350 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1351 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1352
1353 bool Found = false;
1354 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1355 const MachineOperand &MO = MI->getOperand(i);
Hal Finkelaf822012013-04-10 07:17:47 +00001356 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001357 const TargetRegisterClass *RC = RCs[c];
Hal Finkelaf822012013-04-10 07:17:47 +00001358 if (MO.isReg()) {
1359 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001360 Pred.push_back(MO);
1361 Found = true;
1362 }
Hal Finkelaf822012013-04-10 07:17:47 +00001363 } else if (MO.isRegMask()) {
1364 for (TargetRegisterClass::iterator I = RC->begin(),
1365 IE = RC->end(); I != IE; ++I)
1366 if (MO.clobbersPhysReg(*I)) {
1367 Pred.push_back(MO);
1368 Found = true;
1369 }
Hal Finkel5711eca2013-04-09 22:58:37 +00001370 }
1371 }
1372 }
1373
1374 return Found;
1375}
1376
1377bool PPCInstrInfo::isPredicable(MachineInstr *MI) const {
1378 unsigned OpC = MI->getOpcode();
1379 switch (OpC) {
1380 default:
1381 return false;
1382 case PPC::B:
1383 case PPC::BLR:
Hal Finkelf4a22c02015-01-13 17:47:54 +00001384 case PPC::BLR8:
Hal Finkel500b0042013-04-10 06:42:34 +00001385 case PPC::BCTR:
1386 case PPC::BCTR8:
1387 case PPC::BCTRL:
1388 case PPC::BCTRL8:
Hal Finkel5711eca2013-04-09 22:58:37 +00001389 return true;
1390 }
1391}
1392
Hal Finkel82656cb2013-04-18 22:15:08 +00001393bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI,
1394 unsigned &SrcReg, unsigned &SrcReg2,
1395 int &Mask, int &Value) const {
1396 unsigned Opc = MI->getOpcode();
1397
1398 switch (Opc) {
1399 default: return false;
1400 case PPC::CMPWI:
1401 case PPC::CMPLWI:
1402 case PPC::CMPDI:
1403 case PPC::CMPLDI:
1404 SrcReg = MI->getOperand(1).getReg();
1405 SrcReg2 = 0;
1406 Value = MI->getOperand(2).getImm();
1407 Mask = 0xFFFF;
1408 return true;
1409 case PPC::CMPW:
1410 case PPC::CMPLW:
1411 case PPC::CMPD:
1412 case PPC::CMPLD:
1413 case PPC::FCMPUS:
1414 case PPC::FCMPUD:
1415 SrcReg = MI->getOperand(1).getReg();
1416 SrcReg2 = MI->getOperand(2).getReg();
1417 return true;
1418 }
1419}
Hal Finkele6322392013-04-19 22:08:38 +00001420
Hal Finkel82656cb2013-04-18 22:15:08 +00001421bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr,
1422 unsigned SrcReg, unsigned SrcReg2,
1423 int Mask, int Value,
1424 const MachineRegisterInfo *MRI) const {
Hal Finkelb12da6b2013-04-18 22:54:25 +00001425 if (DisableCmpOpt)
1426 return false;
1427
Hal Finkel82656cb2013-04-18 22:15:08 +00001428 int OpC = CmpInstr->getOpcode();
1429 unsigned CRReg = CmpInstr->getOperand(0).getReg();
Hal Finkel08e53ee2013-05-08 12:16:14 +00001430
1431 // FP record forms set CR1 based on the execption status bits, not a
1432 // comparison with zero.
1433 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1434 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001435
1436 // The record forms set the condition register based on a signed comparison
1437 // with zero (so says the ISA manual). This is not as straightforward as it
1438 // seems, however, because this is always a 64-bit comparison on PPC64, even
1439 // for instructions that are 32-bit in nature (like slw for example).
1440 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1441 // for equality checks (as those don't depend on the sign). On PPC64,
1442 // we are restricted to equality for unsigned 64-bit comparisons and for
1443 // signed 32-bit comparisons the applicability is more restricted.
Eric Christopher1dcea732014-06-12 21:48:52 +00001444 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel82656cb2013-04-18 22:15:08 +00001445 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1446 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1447 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1448
1449 // Get the unique definition of SrcReg.
1450 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1451 if (!MI) return false;
1452 int MIOpC = MI->getOpcode();
1453
1454 bool equalityOnly = false;
1455 bool noSub = false;
1456 if (isPPC64) {
1457 if (is32BitSignedCompare) {
1458 // We can perform this optimization only if MI is sign-extending.
1459 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1460 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1461 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1462 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1463 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1464 noSub = true;
1465 } else
1466 return false;
1467 } else if (is32BitUnsignedCompare) {
1468 // We can perform this optimization, equality only, if MI is
1469 // zero-extending.
1470 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1471 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
1472 MIOpC == PPC::SRW || MIOpC == PPC::SRWo) {
1473 noSub = true;
1474 equalityOnly = true;
1475 } else
1476 return false;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001477 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001478 equalityOnly = is64BitUnsignedCompare;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001479 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001480 equalityOnly = is32BitUnsignedCompare;
1481
1482 if (equalityOnly) {
1483 // We need to check the uses of the condition register in order to reject
1484 // non-equality comparisons.
Owen Anderson16c6bf42014-03-13 23:12:04 +00001485 for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg),
1486 IE = MRI->use_instr_end(); I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001487 MachineInstr *UseMI = &*I;
1488 if (UseMI->getOpcode() == PPC::BCC) {
1489 unsigned Pred = UseMI->getOperand(0).getImm();
Hal Finkelc3632452013-05-07 17:49:55 +00001490 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE)
1491 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001492 } else if (UseMI->getOpcode() == PPC::ISEL ||
1493 UseMI->getOpcode() == PPC::ISEL8) {
1494 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkelc3632452013-05-07 17:49:55 +00001495 if (SubIdx != PPC::sub_eq)
1496 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001497 } else
1498 return false;
1499 }
1500 }
1501
Hal Finkelc3632452013-05-07 17:49:55 +00001502 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001503
1504 // Scan forward to find the first use of the compare.
1505 for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end();
1506 I != EL; ++I) {
1507 bool FoundUse = false;
Owen Anderson16c6bf42014-03-13 23:12:04 +00001508 for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg),
1509 JE = MRI->use_instr_end(); J != JE; ++J)
Hal Finkel82656cb2013-04-18 22:15:08 +00001510 if (&*J == &*I) {
1511 FoundUse = true;
1512 break;
1513 }
1514
1515 if (FoundUse)
1516 break;
1517 }
1518
Hal Finkel82656cb2013-04-18 22:15:08 +00001519 // There are two possible candidates which can be changed to set CR[01].
1520 // One is MI, the other is a SUB instruction.
1521 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
Craig Topper062a2ba2014-04-25 05:30:21 +00001522 MachineInstr *Sub = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001523 if (SrcReg2 != 0)
1524 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00001525 MI = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001526 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1527 // same BB as the comparison. This is to allow the check below to avoid calls
1528 // (and other explicit clobbers); instead we should really check for these
1529 // more explicitly (in at least a few predecessors).
1530 else if (MI->getParent() != CmpInstr->getParent() || Value != 0) {
1531 // PPC does not have a record-form SUBri.
1532 return false;
1533 }
1534
1535 // Search for Sub.
1536 const TargetRegisterInfo *TRI = &getRegisterInfo();
1537 --I;
Hal Finkelc3632452013-05-07 17:49:55 +00001538
1539 // Get ready to iterate backward from CmpInstr.
1540 MachineBasicBlock::iterator E = MI,
1541 B = CmpInstr->getParent()->begin();
1542
Hal Finkel82656cb2013-04-18 22:15:08 +00001543 for (; I != E && !noSub; --I) {
1544 const MachineInstr &Instr = *I;
1545 unsigned IOpC = Instr.getOpcode();
1546
1547 if (&*I != CmpInstr && (
Hal Finkel08e53ee2013-05-08 12:16:14 +00001548 Instr.modifiesRegister(PPC::CR0, TRI) ||
1549 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel82656cb2013-04-18 22:15:08 +00001550 // This instruction modifies or uses the record condition register after
1551 // the one we want to change. While we could do this transformation, it
1552 // would likely not be profitable. This transformation removes one
1553 // instruction, and so even forcing RA to generate one move probably
1554 // makes it unprofitable.
1555 return false;
1556
1557 // Check whether CmpInstr can be made redundant by the current instruction.
1558 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1559 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1560 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1561 ((Instr.getOperand(1).getReg() == SrcReg &&
1562 Instr.getOperand(2).getReg() == SrcReg2) ||
1563 (Instr.getOperand(1).getReg() == SrcReg2 &&
1564 Instr.getOperand(2).getReg() == SrcReg))) {
1565 Sub = &*I;
1566 break;
1567 }
1568
Hal Finkel82656cb2013-04-18 22:15:08 +00001569 if (I == B)
1570 // The 'and' is below the comparison instruction.
1571 return false;
1572 }
1573
1574 // Return false if no candidates exist.
1575 if (!MI && !Sub)
1576 return false;
1577
1578 // The single candidate is called MI.
1579 if (!MI) MI = Sub;
1580
1581 int NewOpC = -1;
1582 MIOpC = MI->getOpcode();
1583 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1584 NewOpC = MIOpC;
1585 else {
1586 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1587 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1588 NewOpC = MIOpC;
1589 }
1590
1591 // FIXME: On the non-embedded POWER architectures, only some of the record
1592 // forms are fast, and we should use only the fast ones.
1593
1594 // The defining instruction has a record form (or is already a record
1595 // form). It is possible, however, that we'll need to reverse the condition
1596 // code of the users.
1597 if (NewOpC == -1)
1598 return false;
1599
Hal Finkele6322392013-04-19 22:08:38 +00001600 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1601 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
Hal Finkel82656cb2013-04-18 22:15:08 +00001602
1603 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1604 // needs to be updated to be based on SUB. Push the condition code
1605 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1606 // condition code of these operands will be modified.
1607 bool ShouldSwap = false;
1608 if (Sub) {
1609 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1610 Sub->getOperand(2).getReg() == SrcReg;
1611
1612 // The operands to subf are the opposite of sub, so only in the fixed-point
1613 // case, invert the order.
Hal Finkel08e53ee2013-05-08 12:16:14 +00001614 ShouldSwap = !ShouldSwap;
Hal Finkel82656cb2013-04-18 22:15:08 +00001615 }
1616
1617 if (ShouldSwap)
Owen Anderson16c6bf42014-03-13 23:12:04 +00001618 for (MachineRegisterInfo::use_instr_iterator
1619 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1620 I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001621 MachineInstr *UseMI = &*I;
1622 if (UseMI->getOpcode() == PPC::BCC) {
1623 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hal Finkele6322392013-04-19 22:08:38 +00001624 assert((!equalityOnly ||
1625 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1626 "Invalid predicate for equality-only optimization");
Owen Anderson16c6bf42014-03-13 23:12:04 +00001627 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
Hal Finkel0f64e212013-04-20 05:16:26 +00001628 PPC::getSwappedPredicate(Pred)));
Hal Finkel82656cb2013-04-18 22:15:08 +00001629 } else if (UseMI->getOpcode() == PPC::ISEL ||
1630 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkele6322392013-04-19 22:08:38 +00001631 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1632 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1633 "Invalid CR bit for equality-only optimization");
1634
1635 if (NewSubReg == PPC::sub_lt)
1636 NewSubReg = PPC::sub_gt;
1637 else if (NewSubReg == PPC::sub_gt)
1638 NewSubReg = PPC::sub_lt;
1639
Owen Anderson16c6bf42014-03-13 23:12:04 +00001640 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
Hal Finkele6322392013-04-19 22:08:38 +00001641 NewSubReg));
Hal Finkel82656cb2013-04-18 22:15:08 +00001642 } else // We need to abort on a user we don't understand.
1643 return false;
1644 }
1645
1646 // Create a new virtual register to hold the value of the CR set by the
1647 // record-form instruction. If the instruction was not previously in
1648 // record form, then set the kill flag on the CR.
1649 CmpInstr->eraseFromParent();
1650
1651 MachineBasicBlock::iterator MII = MI;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001652 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
Hal Finkel82656cb2013-04-18 22:15:08 +00001653 get(TargetOpcode::COPY), CRReg)
Hal Finkel08e53ee2013-05-08 12:16:14 +00001654 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel82656cb2013-04-18 22:15:08 +00001655
1656 if (MIOpC != NewOpC) {
1657 // We need to be careful here: we're replacing one instruction with
1658 // another, and we need to make sure that we get all of the right
1659 // implicit uses and defs. On the other hand, the caller may be holding
1660 // an iterator to this instruction, and so we can't delete it (this is
1661 // specifically the case if this is the instruction directly after the
1662 // compare).
1663
1664 const MCInstrDesc &NewDesc = get(NewOpC);
1665 MI->setDesc(NewDesc);
1666
1667 if (NewDesc.ImplicitDefs)
1668 for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs();
1669 *ImpDefs; ++ImpDefs)
1670 if (!MI->definesRegister(*ImpDefs))
1671 MI->addOperand(*MI->getParent()->getParent(),
1672 MachineOperand::CreateReg(*ImpDefs, true, true));
1673 if (NewDesc.ImplicitUses)
1674 for (const uint16_t *ImpUses = NewDesc.getImplicitUses();
1675 *ImpUses; ++ImpUses)
1676 if (!MI->readsRegister(*ImpUses))
1677 MI->addOperand(*MI->getParent()->getParent(),
1678 MachineOperand::CreateReg(*ImpUses, false, true));
1679 }
1680
1681 // Modify the condition code of operands in OperandsToUpdate.
1682 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1683 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkele6322392013-04-19 22:08:38 +00001684 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1685 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001686
Hal Finkele6322392013-04-19 22:08:38 +00001687 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1688 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001689
1690 return true;
1691}
1692
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001693/// GetInstSize - Return the number of bytes of code the specified
1694/// instruction may be. This returns the maximum number of bytes.
1695///
1696unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Hal Finkela7bbaf62014-02-02 06:12:27 +00001697 unsigned Opcode = MI->getOpcode();
1698
1699 if (Opcode == PPC::INLINEASM) {
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001700 const MachineFunction *MF = MI->getParent()->getParent();
1701 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +00001702 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Hal Finkel934361a2015-01-14 01:07:51 +00001703 } else if (Opcode == TargetOpcode::STACKMAP) {
1704 return MI->getOperand(1).getImm();
1705 } else if (Opcode == TargetOpcode::PATCHPOINT) {
1706 PatchPointOpers Opers(MI);
1707 return Opers.getMetaOper(PatchPointOpers::NBytesPos).getImm();
Hal Finkela7bbaf62014-02-02 06:12:27 +00001708 } else {
1709 const MCInstrDesc &Desc = get(Opcode);
1710 return Desc.getSize();
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001711 }
1712}
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001713