blob: 6b5d78b58149431e7cd897990c908c420b018357 [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"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Hal Finkelb5aa7e52013-04-08 16:24:03 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman116f9272004-08-17 04:55:41 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000026#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesenddbf7a82010-02-26 21:09:24 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Hal Finkel9f9f8922012-04-01 19:22:40 +000028#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000029#include "llvm/MC/MCAsmInfo.h"
Bill Wendling1af20ad2008-03-04 23:13:51 +000030#include "llvm/Support/CommandLine.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000031#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000032#include "llvm/Support/TargetRegistry.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000033#include "llvm/Support/raw_ostream.h"
Misha Brukman116f9272004-08-17 04:55:41 +000034
Hal Finkel82656cb2013-04-18 22:15:08 +000035#define GET_INSTRMAP_INFO
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000036#define GET_INSTRINFO_CTOR_DTOR
Evan Cheng1e210d02011-06-28 20:07:07 +000037#include "PPCGenInstrInfo.inc"
38
Dan Gohman20857192010-04-15 17:20:57 +000039using namespace llvm;
Bill Wendling1af20ad2008-03-04 23:13:51 +000040
Hal Finkel821e0012012-06-08 15:38:25 +000041static cl::
Hal Finkelc6b5deb2012-06-08 19:19:53 +000042opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
43 cl::desc("Disable analysis for CTR loops"));
Hal Finkel821e0012012-06-08 15:38:25 +000044
Hal Finkele6322392013-04-19 22:08:38 +000045static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt",
Hal Finkelb12da6b2013-04-18 22:54:25 +000046cl::desc("Disable compare instruction optimization"), cl::Hidden);
47
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000048// Pin the vtable to this file.
49void PPCInstrInfo::anchor() {}
50
Chris Lattner49cadab2006-06-17 00:01:04 +000051PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Evan Cheng703a0fb2011-07-01 17:57:27 +000052 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
Bill Wendling5e7656b2013-06-07 07:55:53 +000053 TM(tm), RI(*TM.getSubtargetImpl()) {}
Chris Lattner49cadab2006-06-17 00:01:04 +000054
Andrew Trick10ffc2b2010-12-24 05:03:26 +000055/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
56/// this target when scheduling the DAG.
57ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetHazardRecognizer(
58 const TargetMachine *TM,
59 const ScheduleDAG *DAG) const {
Hal Finkel6fa56972011-10-17 04:03:49 +000060 unsigned Directive = TM->getSubtarget<PPCSubtarget>().getDarwinDirective();
Hal Finkel742b5352012-08-28 16:12:39 +000061 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
62 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
Hal Finkel6f0ae782011-11-22 16:21:04 +000063 const InstrItineraryData *II = TM->getInstrItineraryData();
Hal Finkel563cc052013-12-02 23:52:46 +000064 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel6fa56972011-10-17 04:03:49 +000065 }
Hal Finkel58ca3602011-12-02 04:58:02 +000066
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +000067 return TargetInstrInfo::CreateTargetHazardRecognizer(TM, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000068}
69
Hal Finkel58ca3602011-12-02 04:58:02 +000070/// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
71/// to use for this target when scheduling the DAG.
72ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetPostRAHazardRecognizer(
73 const InstrItineraryData *II,
74 const ScheduleDAG *DAG) const {
75 unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
76
Hal Finkelceb1f122013-12-12 00:19:11 +000077 if (Directive == PPC::DIR_PWR7)
78 return new PPCDispatchGroupSBHazardRecognizer(II, DAG);
79
Hal Finkel58ca3602011-12-02 04:58:02 +000080 // Most subtargets use a PPC970 recognizer.
Hal Finkel742b5352012-08-28 16:12:39 +000081 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
82 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
Benjamin Kramerf0ec1992013-06-07 11:23:35 +000083 assert(TM.getInstrInfo() && "No InstrInfo?");
Hal Finkel58ca3602011-12-02 04:58:02 +000084
Bill Wendling5e7656b2013-06-07 07:55:53 +000085 return new PPCHazardRecognizer970(TM);
Hal Finkel58ca3602011-12-02 04:58:02 +000086 }
87
Hal Finkel563cc052013-12-02 23:52:46 +000088 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +000089}
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +000090
Hal Finkelceb1f122013-12-12 00:19:11 +000091
92int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
93 const MachineInstr *DefMI, unsigned DefIdx,
94 const MachineInstr *UseMI,
95 unsigned UseIdx) const {
96 int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
97 UseMI, UseIdx);
98
99 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
100 unsigned Reg = DefMO.getReg();
101
102 const TargetRegisterInfo *TRI = &getRegisterInfo();
103 bool IsRegCR;
104 if (TRI->isVirtualRegister(Reg)) {
105 const MachineRegisterInfo *MRI =
106 &DefMI->getParent()->getParent()->getRegInfo();
107 IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) ||
108 MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass);
109 } else {
110 IsRegCR = PPC::CRRCRegClass.contains(Reg) ||
111 PPC::CRBITRCRegClass.contains(Reg);
112 }
113
114 if (UseMI->isBranch() && IsRegCR) {
115 if (Latency < 0)
116 Latency = getInstrLatency(ItinData, DefMI);
117
118 // On some cores, there is an additional delay between writing to a condition
119 // register, and using it from a branch.
120 unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
121 switch (Directive) {
122 default: break;
123 case PPC::DIR_7400:
124 case PPC::DIR_750:
125 case PPC::DIR_970:
126 case PPC::DIR_E5500:
127 case PPC::DIR_PWR4:
128 case PPC::DIR_PWR5:
129 case PPC::DIR_PWR5X:
130 case PPC::DIR_PWR6:
131 case PPC::DIR_PWR6X:
132 case PPC::DIR_PWR7:
133 Latency += 2;
134 break;
135 }
136 }
137
138 return Latency;
139}
140
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000141// Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
142bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
143 unsigned &SrcReg, unsigned &DstReg,
144 unsigned &SubIdx) const {
145 switch (MI.getOpcode()) {
146 default: return false;
147 case PPC::EXTSW:
148 case PPC::EXTSW_32_64:
149 SrcReg = MI.getOperand(1).getReg();
150 DstReg = MI.getOperand(0).getReg();
151 SubIdx = PPC::sub_32;
152 return true;
153 }
154}
155
Andrew Trickc416ba62010-12-24 04:28:06 +0000156unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner91400bd2006-03-16 22:24:02 +0000157 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000158 // Note: This list must be kept consistent with LoadRegFromStackSlot.
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000159 switch (MI->getOpcode()) {
160 default: break;
161 case PPC::LD:
162 case PPC::LWZ:
163 case PPC::LFS:
164 case PPC::LFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000165 case PPC::RESTORE_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000166 case PPC::RESTORE_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000167 case PPC::LVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000168 case PPC::LXVD2X:
Hal Finkel37714b82013-03-27 21:21:15 +0000169 case PPC::RESTORE_VRSAVE:
170 // Check for the operands added by addFrameReference (the immediate is the
171 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000172 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
173 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000174 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000175 return MI->getOperand(0).getReg();
176 }
177 break;
178 }
179 return 0;
Chris Lattnerc327d712006-02-02 20:16:12 +0000180}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000181
Andrew Trickc416ba62010-12-24 04:28:06 +0000182unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattnerc327d712006-02-02 20:16:12 +0000183 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000184 // Note: This list must be kept consistent with StoreRegToStackSlot.
Chris Lattnerc327d712006-02-02 20:16:12 +0000185 switch (MI->getOpcode()) {
186 default: break;
Nate Begeman4efb3282006-02-02 21:07:50 +0000187 case PPC::STD:
Chris Lattnerc327d712006-02-02 20:16:12 +0000188 case PPC::STW:
189 case PPC::STFS:
190 case PPC::STFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000191 case PPC::SPILL_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000192 case PPC::SPILL_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000193 case PPC::STVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000194 case PPC::STXVD2X:
Hal Finkel37714b82013-03-27 21:21:15 +0000195 case PPC::SPILL_VRSAVE:
196 // Check for the operands added by addFrameReference (the immediate is the
197 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000198 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
199 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000200 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerc327d712006-02-02 20:16:12 +0000201 return MI->getOperand(0).getReg();
202 }
203 break;
204 }
205 return 0;
206}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000207
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000208// commuteInstruction - We can commute rlwimi instructions, but only if the
209// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng03553bb2008-06-16 07:33:11 +0000210MachineInstr *
211PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000212 MachineFunction &MF = *MI->getParent()->getParent();
213
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000214 // Normal instructions can be commuted the obvious way.
Hal Finkel654d43b2013-04-12 02:18:09 +0000215 if (MI->getOpcode() != PPC::RLWIMI &&
Hal Finkel940ab932014-02-28 00:27:01 +0000216 MI->getOpcode() != PPC::RLWIMIo &&
217 MI->getOpcode() != PPC::RLWIMI8 &&
218 MI->getOpcode() != PPC::RLWIMI8o)
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000219 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Andrew Trickc416ba62010-12-24 04:28:06 +0000220
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000221 // Cannot commute if it has a non-zero rotate count.
Chris Lattner5c463782007-12-30 20:49:49 +0000222 if (MI->getOperand(3).getImm() != 0)
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000223 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000224
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000225 // If we have a zero rotate count, we have:
226 // M = mask(MB,ME)
227 // Op0 = (Op1 & ~M) | (Op2 & M)
228 // Change this to:
229 // M = mask((ME+1)&31, (MB-1)&31)
230 // Op0 = (Op2 & ~M) | (Op1 & M)
231
232 // Swap op1/op2
Evan Cheng244183e2008-02-13 02:46:49 +0000233 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000234 unsigned Reg1 = MI->getOperand(1).getReg();
235 unsigned Reg2 = MI->getOperand(2).getReg();
Andrew Tricke3398282013-12-17 04:50:45 +0000236 unsigned SubReg1 = MI->getOperand(1).getSubReg();
237 unsigned SubReg2 = MI->getOperand(2).getSubReg();
Evan Chengdc2c8742006-11-15 20:58:11 +0000238 bool Reg1IsKill = MI->getOperand(1).isKill();
239 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng03553bb2008-06-16 07:33:11 +0000240 bool ChangeReg0 = false;
Evan Cheng244183e2008-02-13 02:46:49 +0000241 // If machine instrs are no longer in two-address forms, update
242 // destination register as well.
243 if (Reg0 == Reg1) {
244 // Must be two address instruction!
Evan Cheng6cc775f2011-06-28 19:10:37 +0000245 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Cheng244183e2008-02-13 02:46:49 +0000246 "Expecting a two-address instruction!");
Andrew Tricke3398282013-12-17 04:50:45 +0000247 assert(MI->getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
Evan Cheng244183e2008-02-13 02:46:49 +0000248 Reg2IsKill = false;
Evan Cheng03553bb2008-06-16 07:33:11 +0000249 ChangeReg0 = true;
Evan Cheng244183e2008-02-13 02:46:49 +0000250 }
Evan Cheng03553bb2008-06-16 07:33:11 +0000251
252 // Masks.
253 unsigned MB = MI->getOperand(4).getImm();
254 unsigned ME = MI->getOperand(5).getImm();
255
256 if (NewMI) {
257 // Create a new instruction.
258 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
259 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000260 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000261 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
262 .addReg(Reg2, getKillRegState(Reg2IsKill))
263 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng03553bb2008-06-16 07:33:11 +0000264 .addImm((ME+1) & 31)
265 .addImm((MB-1) & 31);
266 }
267
Andrew Tricke3398282013-12-17 04:50:45 +0000268 if (ChangeReg0) {
Evan Cheng03553bb2008-06-16 07:33:11 +0000269 MI->getOperand(0).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000270 MI->getOperand(0).setSubReg(SubReg2);
271 }
Chris Lattner10d63412006-05-04 17:52:23 +0000272 MI->getOperand(2).setReg(Reg1);
273 MI->getOperand(1).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000274 MI->getOperand(2).setSubReg(SubReg1);
275 MI->getOperand(1).setSubReg(SubReg2);
Chris Lattner60055892007-12-30 21:56:09 +0000276 MI->getOperand(2).setIsKill(Reg1IsKill);
277 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000278
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000279 // Swap the mask around.
Chris Lattner5c463782007-12-30 20:49:49 +0000280 MI->getOperand(4).setImm((ME+1) & 31);
281 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000282 return MI;
283}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000284
Andrew Trickc416ba62010-12-24 04:28:06 +0000285void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000286 MachineBasicBlock::iterator MI) const {
Hal Finkelceb1f122013-12-12 00:19:11 +0000287 // This function is used for scheduling, and the nop wanted here is the type
288 // that terminates dispatch groups on the POWER cores.
289 unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
290 unsigned Opcode;
291 switch (Directive) {
292 default: Opcode = PPC::NOP; break;
293 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
294 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
295 }
Chris Lattnera47294ed2006-10-13 21:21:17 +0000296
Hal Finkelceb1f122013-12-12 00:19:11 +0000297 DebugLoc DL;
298 BuildMI(MBB, MI, DL, get(Opcode));
299}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000300
301// Branch analysis.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000302// Note: If the condition register is set to CTR or CTR8 then this is a
303// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Chris Lattnera47294ed2006-10-13 21:21:17 +0000304bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
305 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000306 SmallVectorImpl<MachineOperand> &Cond,
307 bool AllowModify) const {
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000308 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
309
Chris Lattnera47294ed2006-10-13 21:21:17 +0000310 // If the block has no terminators, it just falls into the block after it.
311 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000312 if (I == MBB.begin())
313 return false;
314 --I;
315 while (I->isDebugValue()) {
316 if (I == MBB.begin())
317 return false;
318 --I;
319 }
320 if (!isUnpredicatedTerminator(I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000321 return false;
322
323 // Get the last instruction in the block.
324 MachineInstr *LastInst = I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000325
Chris Lattnera47294ed2006-10-13 21:21:17 +0000326 // If there is only one terminator instruction, process it.
Evan Cheng5514bbe2007-06-08 21:59:56 +0000327 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000328 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000329 if (!LastInst->getOperand(0).isMBB())
330 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000331 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000332 return false;
Chris Lattnere0263792006-11-17 22:14:47 +0000333 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000334 if (!LastInst->getOperand(2).isMBB())
335 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000336 // Block ends with fall-through condbranch.
Chris Lattnera5bb3702007-12-30 23:10:15 +0000337 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000338 Cond.push_back(LastInst->getOperand(0));
339 Cond.push_back(LastInst->getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000340 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000341 } else if (LastInst->getOpcode() == PPC::BC) {
342 if (!LastInst->getOperand(1).isMBB())
343 return true;
344 // Block ends with fall-through condbranch.
345 TBB = LastInst->getOperand(1).getMBB();
346 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
347 Cond.push_back(LastInst->getOperand(0));
348 return false;
349 } else if (LastInst->getOpcode() == PPC::BCn) {
350 if (!LastInst->getOperand(1).isMBB())
351 return true;
352 // Block ends with fall-through condbranch.
353 TBB = LastInst->getOperand(1).getMBB();
354 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
355 Cond.push_back(LastInst->getOperand(0));
356 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000357 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
358 LastInst->getOpcode() == PPC::BDNZ) {
359 if (!LastInst->getOperand(0).isMBB())
360 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000361 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000362 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000363 TBB = LastInst->getOperand(0).getMBB();
364 Cond.push_back(MachineOperand::CreateImm(1));
365 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
366 true));
367 return false;
368 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
369 LastInst->getOpcode() == PPC::BDZ) {
370 if (!LastInst->getOperand(0).isMBB())
371 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000372 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000373 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000374 TBB = LastInst->getOperand(0).getMBB();
375 Cond.push_back(MachineOperand::CreateImm(0));
376 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
377 true));
378 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000379 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000380
Chris Lattnera47294ed2006-10-13 21:21:17 +0000381 // Otherwise, don't know what this is.
382 return true;
383 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000384
Chris Lattnera47294ed2006-10-13 21:21:17 +0000385 // Get the instruction before it if it's a terminator.
386 MachineInstr *SecondLastInst = I;
387
388 // If there are three terminators, we don't know what sort of block this is.
389 if (SecondLastInst && I != MBB.begin() &&
Evan Cheng5514bbe2007-06-08 21:59:56 +0000390 isUnpredicatedTerminator(--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000391 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000392
Chris Lattnere0263792006-11-17 22:14:47 +0000393 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000394 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnera47294ed2006-10-13 21:21:17 +0000395 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000396 if (!SecondLastInst->getOperand(2).isMBB() ||
397 !LastInst->getOperand(0).isMBB())
398 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000399 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000400 Cond.push_back(SecondLastInst->getOperand(0));
401 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattnera5bb3702007-12-30 23:10:15 +0000402 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000403 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000404 } else if (SecondLastInst->getOpcode() == PPC::BC &&
405 LastInst->getOpcode() == PPC::B) {
406 if (!SecondLastInst->getOperand(1).isMBB() ||
407 !LastInst->getOperand(0).isMBB())
408 return true;
409 TBB = SecondLastInst->getOperand(1).getMBB();
410 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
411 Cond.push_back(SecondLastInst->getOperand(0));
412 FBB = LastInst->getOperand(0).getMBB();
413 return false;
414 } else if (SecondLastInst->getOpcode() == PPC::BCn &&
415 LastInst->getOpcode() == PPC::B) {
416 if (!SecondLastInst->getOperand(1).isMBB() ||
417 !LastInst->getOperand(0).isMBB())
418 return true;
419 TBB = SecondLastInst->getOperand(1).getMBB();
420 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
421 Cond.push_back(SecondLastInst->getOperand(0));
422 FBB = LastInst->getOperand(0).getMBB();
423 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000424 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
425 SecondLastInst->getOpcode() == PPC::BDNZ) &&
426 LastInst->getOpcode() == PPC::B) {
427 if (!SecondLastInst->getOperand(0).isMBB() ||
428 !LastInst->getOperand(0).isMBB())
429 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000430 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000431 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000432 TBB = SecondLastInst->getOperand(0).getMBB();
433 Cond.push_back(MachineOperand::CreateImm(1));
434 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
435 true));
436 FBB = LastInst->getOperand(0).getMBB();
437 return false;
438 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
439 SecondLastInst->getOpcode() == PPC::BDZ) &&
440 LastInst->getOpcode() == PPC::B) {
441 if (!SecondLastInst->getOperand(0).isMBB() ||
442 !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 = SecondLastInst->getOperand(0).getMBB();
447 Cond.push_back(MachineOperand::CreateImm(0));
448 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
449 true));
450 FBB = LastInst->getOperand(0).getMBB();
451 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000452 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000453
Dale Johannesenc6855462007-06-13 17:59:52 +0000454 // If the block ends with two PPC:Bs, handle it. The second one is not
455 // executed, so remove it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000456 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesenc6855462007-06-13 17:59:52 +0000457 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000458 if (!SecondLastInst->getOperand(0).isMBB())
459 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000460 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000461 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000462 if (AllowModify)
463 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000464 return false;
465 }
466
Chris Lattnera47294ed2006-10-13 21:21:17 +0000467 // Otherwise, can't handle this.
468 return true;
469}
470
Evan Cheng99be49d2007-05-18 00:05:48 +0000471unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000472 MachineBasicBlock::iterator I = MBB.end();
Evan Cheng99be49d2007-05-18 00:05:48 +0000473 if (I == MBB.begin()) return 0;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000474 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000475 while (I->isDebugValue()) {
476 if (I == MBB.begin())
477 return 0;
478 --I;
479 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000480 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000481 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000482 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
483 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000484 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000485
Chris Lattnera47294ed2006-10-13 21:21:17 +0000486 // Remove the branch.
487 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000488
Chris Lattnera47294ed2006-10-13 21:21:17 +0000489 I = MBB.end();
490
Evan Cheng99be49d2007-05-18 00:05:48 +0000491 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000492 --I;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000493 if (I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000494 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000495 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
496 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000497 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000498
Chris Lattnera47294ed2006-10-13 21:21:17 +0000499 // Remove the branch.
500 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000501 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000502}
503
Evan Cheng99be49d2007-05-18 00:05:48 +0000504unsigned
505PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
506 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000507 const SmallVectorImpl<MachineOperand> &Cond,
508 DebugLoc DL) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000509 // Shouldn't be a fall through.
510 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000511 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000512 "PPC branch conditions have two components!");
Andrew Trickc416ba62010-12-24 04:28:06 +0000513
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000514 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
515
Chris Lattner94e04442006-10-21 05:36:13 +0000516 // One-way branch.
Chris Lattnera61f0102006-10-17 18:06:55 +0000517 if (FBB == 0) {
Chris Lattner94e04442006-10-21 05:36:13 +0000518 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000519 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000520 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
521 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
522 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
523 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000524 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
525 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
526 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
527 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000528 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000529 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000530 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000531 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000532 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000533
Chris Lattnerd8816602006-10-21 05:42:09 +0000534 // Two-way Conditional Branch.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000535 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
536 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
537 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
538 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000539 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
540 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
541 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
542 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000543 else
544 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000545 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000546 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000547 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000548}
549
Hal Finkeled6a2852013-04-05 23:29:01 +0000550// Select analysis.
551bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
552 const SmallVectorImpl<MachineOperand> &Cond,
553 unsigned TrueReg, unsigned FalseReg,
554 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
555 if (!TM.getSubtargetImpl()->hasISEL())
556 return false;
557
558 if (Cond.size() != 2)
559 return false;
560
561 // If this is really a bdnz-like condition, then it cannot be turned into a
562 // select.
563 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
564 return false;
565
566 // Check register classes.
567 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
568 const TargetRegisterClass *RC =
569 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
570 if (!RC)
571 return false;
572
573 // isel is for regular integer GPRs only.
574 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkel8e8618a2013-07-15 20:22:58 +0000575 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
576 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
577 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkeled6a2852013-04-05 23:29:01 +0000578 return false;
579
580 // FIXME: These numbers are for the A2, how well they work for other cores is
581 // an open question. On the A2, the isel instruction has a 2-cycle latency
582 // but single-cycle throughput. These numbers are used in combination with
583 // the MispredictPenalty setting from the active SchedMachineModel.
584 CondCycles = 1;
585 TrueCycles = 1;
586 FalseCycles = 1;
587
588 return true;
589}
590
591void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
592 MachineBasicBlock::iterator MI, DebugLoc dl,
593 unsigned DestReg,
594 const SmallVectorImpl<MachineOperand> &Cond,
595 unsigned TrueReg, unsigned FalseReg) const {
596 assert(Cond.size() == 2 &&
597 "PPC branch conditions have two components!");
598
599 assert(TM.getSubtargetImpl()->hasISEL() &&
600 "Cannot insert select on target without ISEL support");
601
602 // Get the register classes.
603 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
604 const TargetRegisterClass *RC =
605 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
606 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkel8e8618a2013-07-15 20:22:58 +0000607
608 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
609 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
610 assert((Is64Bit ||
611 PPC::GPRCRegClass.hasSubClassEq(RC) ||
612 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000613 "isel is for regular integer GPRs only");
614
Hal Finkel8e8618a2013-07-15 20:22:58 +0000615 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Hal Finkeled6a2852013-04-05 23:29:01 +0000616 unsigned SelectPred = Cond[0].getImm();
617
618 unsigned SubIdx;
619 bool SwapOps;
620 switch (SelectPred) {
621 default: llvm_unreachable("invalid predicate for isel");
622 case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
623 case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
624 case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
625 case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
626 case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
627 case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
628 case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
629 case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
Hal Finkel940ab932014-02-28 00:27:01 +0000630 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break;
631 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
Hal Finkeled6a2852013-04-05 23:29:01 +0000632 }
633
634 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
635 SecondReg = SwapOps ? TrueReg : FalseReg;
636
637 // The first input register of isel cannot be r0. If it is a member
638 // of a register class that can be r0, then copy it first (the
639 // register allocator should eliminate the copy).
640 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
641 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
642 const TargetRegisterClass *FirstRC =
643 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
644 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
645 unsigned OldFirstReg = FirstReg;
646 FirstReg = MRI.createVirtualRegister(FirstRC);
647 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
648 .addReg(OldFirstReg);
649 }
650
651 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
652 .addReg(FirstReg).addReg(SecondReg)
653 .addReg(Cond[1].getReg(), 0, SubIdx);
654}
655
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000656void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
657 MachineBasicBlock::iterator I, DebugLoc DL,
658 unsigned DestReg, unsigned SrcReg,
659 bool KillSrc) const {
Hal Finkel27774d92014-03-13 07:58:58 +0000660 // We can end up with self copies and similar things as a result of VSX copy
661 // legalization. Promote (or just ignore) them here.
662 const TargetRegisterInfo *TRI = &getRegisterInfo();
663 if (PPC::F8RCRegClass.contains(DestReg) &&
664 PPC::VSLRCRegClass.contains(SrcReg)) {
665 unsigned SuperReg =
666 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass);
667
668 if (SrcReg == SuperReg)
669 return;
670
671 DestReg = SuperReg;
672 } else if (PPC::VRRCRegClass.contains(DestReg) &&
673 PPC::VSHRCRegClass.contains(SrcReg)) {
674 unsigned SuperReg =
675 TRI->getMatchingSuperReg(DestReg, PPC::sub_128, &PPC::VSRCRegClass);
676
677 if (SrcReg == SuperReg)
678 return;
679
680 DestReg = SuperReg;
681 } else if (PPC::F8RCRegClass.contains(SrcReg) &&
682 PPC::VSLRCRegClass.contains(DestReg)) {
683 unsigned SuperReg =
684 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass);
685
686 if (DestReg == SuperReg)
687 return;
688
689 SrcReg = SuperReg;
690 } else if (PPC::VRRCRegClass.contains(SrcReg) &&
691 PPC::VSHRCRegClass.contains(DestReg)) {
692 unsigned SuperReg =
693 TRI->getMatchingSuperReg(SrcReg, PPC::sub_128, &PPC::VSRCRegClass);
694
695 if (DestReg == SuperReg)
696 return;
697
698 SrcReg = SuperReg;
699 }
700
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000701 unsigned Opc;
702 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
703 Opc = PPC::OR;
704 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
705 Opc = PPC::OR8;
706 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
707 Opc = PPC::FMR;
708 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
709 Opc = PPC::MCRF;
710 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
711 Opc = PPC::VOR;
Hal Finkel27774d92014-03-13 07:58:58 +0000712 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg))
Hal Finkelbbad2332014-03-24 09:36:36 +0000713 // There are two different ways this can be done:
Hal Finkel27774d92014-03-13 07:58:58 +0000714 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
715 // issue in VSU pipeline 0.
716 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
717 // can go to either pipeline.
Hal Finkelbbad2332014-03-24 09:36:36 +0000718 // We'll always use xxlor here, because in practically all cases where
719 // copies are generated, they are close enough to some use that the
720 // lower-latency form is preferable.
Hal Finkel27774d92014-03-13 07:58:58 +0000721 Opc = PPC::XXLOR;
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000722 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
723 Opc = PPC::CROR;
724 else
725 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +0000726
Evan Cheng6cc775f2011-06-28 19:10:37 +0000727 const MCInstrDesc &MCID = get(Opc);
728 if (MCID.getNumOperands() == 3)
729 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000730 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
731 else
Evan Cheng6cc775f2011-06-28 19:10:37 +0000732 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +0000733}
734
Hal Finkel8f6834d2011-12-05 17:55:17 +0000735// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000736bool
Dan Gohman3b460302008-07-07 23:14:23 +0000737PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
738 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000739 int FrameIdx,
740 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000741 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000742 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000743 // Note: If additional store instructions are added here,
744 // update isStoreToStackSlot.
745
Chris Lattner6f306d72010-04-02 20:16:16 +0000746 DebugLoc DL;
Hal Finkel4e703bc2014-01-28 05:32:58 +0000747 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
748 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000749 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
750 .addReg(SrcReg,
751 getKillRegState(isKill)),
752 FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000753 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
754 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000755 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
756 .addReg(SrcReg,
757 getKillRegState(isKill)),
758 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000759 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000760 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000761 .addReg(SrcReg,
762 getKillRegState(isKill)),
763 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000764 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000765 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000766 .addReg(SrcReg,
767 getKillRegState(isKill)),
768 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000769 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000770 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
771 .addReg(SrcReg,
772 getKillRegState(isKill)),
773 FrameIdx));
774 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000775 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000776 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT))
777 .addReg(SrcReg,
778 getKillRegState(isKill)),
779 FrameIdx));
780 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000781 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000782 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
783 .addReg(SrcReg,
784 getKillRegState(isKill)),
785 FrameIdx));
786 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +0000787 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
788 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXVD2X))
789 .addReg(SrcReg,
790 getKillRegState(isKill)),
791 FrameIdx));
792 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000793 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Hal Finkela7b06302013-03-27 00:02:20 +0000794 assert(TM.getSubtargetImpl()->isDarwin() &&
795 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000796 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
797 .addReg(SrcReg,
798 getKillRegState(isKill)),
799 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000800 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000801 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000802 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000803 }
Bill Wendling632ea652008-03-03 22:19:16 +0000804
805 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000806}
807
808void
809PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000810 MachineBasicBlock::iterator MI,
811 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000812 const TargetRegisterClass *RC,
813 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000814 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000815 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +0000816
Hal Finkelbb420f12013-03-15 05:06:04 +0000817 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
818 FuncInfo->setHasSpills();
819
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000820 bool NonRI = false, SpillsVRS = false;
821 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
822 NonRI, SpillsVRS))
Bill Wendling632ea652008-03-03 22:19:16 +0000823 FuncInfo->setSpillsCR();
Bill Wendling632ea652008-03-03 22:19:16 +0000824
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000825 if (SpillsVRS)
826 FuncInfo->setSpillsVRSAVE();
827
Hal Finkelfcc51d42013-03-17 04:43:44 +0000828 if (NonRI)
829 FuncInfo->setHasNonRISpills();
830
Owen Andersoneee14602008-01-01 21:11:32 +0000831 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
832 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000833
834 const MachineFrameInfo &MFI = *MF.getFrameInfo();
835 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000836 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000837 MachineMemOperand::MOStore,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000838 MFI.getObjectSize(FrameIdx),
839 MFI.getObjectAlignment(FrameIdx));
840 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000841}
842
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000843bool
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000844PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman3b460302008-07-07 23:14:23 +0000845 unsigned DestReg, int FrameIdx,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000846 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 load instructions are added here,
850 // update isLoadFromStackSlot.
851
Hal Finkel4e703bc2014-01-28 05:32:58 +0000852 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
853 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000854 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
855 DestReg), FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000856 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
857 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000858 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
859 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000860 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000861 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000862 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000863 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000864 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000865 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000866 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000867 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
868 get(PPC::RESTORE_CR), DestReg),
869 FrameIdx));
870 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000871 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000872 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
873 get(PPC::RESTORE_CRBIT), DestReg),
874 FrameIdx));
875 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000876 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000877 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
878 FrameIdx));
879 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +0000880 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
881 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXVD2X), DestReg),
882 FrameIdx));
883 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000884 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Hal Finkela7b06302013-03-27 00:02:20 +0000885 assert(TM.getSubtargetImpl()->isDarwin() &&
886 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000887 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
888 get(PPC::RESTORE_VRSAVE),
889 DestReg),
890 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000891 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000892 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000893 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000894 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000895
896 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000897}
898
899void
900PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000901 MachineBasicBlock::iterator MI,
902 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000903 const TargetRegisterClass *RC,
904 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000905 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000906 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +0000907 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000908 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000909
910 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
911 FuncInfo->setHasSpills();
912
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000913 bool NonRI = false, SpillsVRS = false;
914 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
915 NonRI, SpillsVRS))
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000916 FuncInfo->setSpillsCR();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000917
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000918 if (SpillsVRS)
919 FuncInfo->setSpillsVRSAVE();
920
Hal Finkelfcc51d42013-03-17 04:43:44 +0000921 if (NonRI)
922 FuncInfo->setHasNonRISpills();
923
Owen Andersoneee14602008-01-01 21:11:32 +0000924 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
925 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000926
927 const MachineFrameInfo &MFI = *MF.getFrameInfo();
928 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000929 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000930 MachineMemOperand::MOLoad,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000931 MFI.getObjectSize(FrameIdx),
932 MFI.getObjectAlignment(FrameIdx));
933 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000934}
935
Chris Lattnera47294ed2006-10-13 21:21:17 +0000936bool PPCInstrInfo::
Owen Anderson4f6bf042008-08-14 22:49:33 +0000937ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +0000938 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000939 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
940 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
941 else
942 // Leave the CR# the same, but invert the condition.
943 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +0000944 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000945}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000946
Hal Finkeld61d4f82013-04-06 19:30:30 +0000947bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
948 unsigned Reg, MachineRegisterInfo *MRI) const {
949 // For some instructions, it is legal to fold ZERO into the RA register field.
950 // A zero immediate should always be loaded with a single li.
951 unsigned DefOpc = DefMI->getOpcode();
952 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
953 return false;
954 if (!DefMI->getOperand(1).isImm())
955 return false;
956 if (DefMI->getOperand(1).getImm() != 0)
957 return false;
958
959 // Note that we cannot here invert the arguments of an isel in order to fold
960 // a ZERO into what is presented as the second argument. All we have here
961 // is the condition bit, and that might come from a CR-logical bit operation.
962
963 const MCInstrDesc &UseMCID = UseMI->getDesc();
964
965 // Only fold into real machine instructions.
966 if (UseMCID.isPseudo())
967 return false;
968
969 unsigned UseIdx;
970 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
971 if (UseMI->getOperand(UseIdx).isReg() &&
972 UseMI->getOperand(UseIdx).getReg() == Reg)
973 break;
974
975 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
976 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
977
978 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
979
980 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
981 // register (which might also be specified as a pointer class kind).
982 if (UseInfo->isLookupPtrRegClass()) {
983 if (UseInfo->RegClass /* Kind */ != 1)
984 return false;
985 } else {
986 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
987 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
988 return false;
989 }
990
991 // Make sure this is not tied to an output register (or otherwise
992 // constrained). This is true for ST?UX registers, for example, which
993 // are tied to their output registers.
994 if (UseInfo->Constraints != 0)
995 return false;
996
997 unsigned ZeroReg;
998 if (UseInfo->isLookupPtrRegClass()) {
999 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1000 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
1001 } else {
1002 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
1003 PPC::ZERO8 : PPC::ZERO;
1004 }
1005
1006 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
1007 UseMI->getOperand(UseIdx).setReg(ZeroReg);
1008
1009 if (DeleteDef)
1010 DefMI->eraseFromParent();
1011
1012 return true;
1013}
1014
Hal Finkel30ae2292013-04-10 18:30:16 +00001015static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
1016 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1017 I != IE; ++I)
1018 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
1019 return true;
1020 return false;
1021}
1022
1023// We should make sure that, if we're going to predicate both sides of a
1024// condition (a diamond), that both sides don't define the counter register. We
1025// can predicate counter-decrement-based branches, but while that predicates
1026// the branching, it does not predicate the counter decrement. If we tried to
1027// merge the triangle into one predicated block, we'd decrement the counter
1028// twice.
1029bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
1030 unsigned NumT, unsigned ExtraT,
1031 MachineBasicBlock &FMBB,
1032 unsigned NumF, unsigned ExtraF,
1033 const BranchProbability &Probability) const {
1034 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
1035}
1036
1037
Hal Finkel5711eca2013-04-09 22:58:37 +00001038bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const {
Hal Finkelf29285a2013-04-11 01:23:34 +00001039 // The predicated branches are identified by their type, not really by the
1040 // explicit presence of a predicate. Furthermore, some of them can be
1041 // predicated more than once. Because if conversion won't try to predicate
1042 // any instruction which already claims to be predicated (by returning true
1043 // here), always return false. In doing so, we let isPredicable() be the
1044 // final word on whether not the instruction can be (further) predicated.
1045
1046 return false;
Hal Finkel5711eca2013-04-09 22:58:37 +00001047}
1048
1049bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
1050 if (!MI->isTerminator())
1051 return false;
1052
1053 // Conditional branch is a special case.
1054 if (MI->isBranch() && !MI->isBarrier())
1055 return true;
1056
1057 return !isPredicated(MI);
1058}
1059
1060bool PPCInstrInfo::PredicateInstruction(
1061 MachineInstr *MI,
1062 const SmallVectorImpl<MachineOperand> &Pred) const {
1063 unsigned OpC = MI->getOpcode();
1064 if (OpC == PPC::BLR) {
1065 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
1066 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1067 MI->setDesc(get(Pred[0].getImm() ?
1068 (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) :
1069 (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
Hal Finkel940ab932014-02-28 00:27:01 +00001070 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001071 MI->setDesc(get(PPC::BCLR));
1072 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel940ab932014-02-28 00:27:01 +00001073 .addReg(Pred[1].getReg());
1074 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1075 MI->setDesc(get(PPC::BCLRn));
1076 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1077 .addReg(Pred[1].getReg());
1078 } else {
1079 MI->setDesc(get(PPC::BCCLR));
1080 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel5711eca2013-04-09 22:58:37 +00001081 .addImm(Pred[0].getImm())
1082 .addReg(Pred[1].getReg());
1083 }
1084
1085 return true;
1086 } else if (OpC == PPC::B) {
1087 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
1088 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1089 MI->setDesc(get(Pred[0].getImm() ?
1090 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1091 (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
Hal Finkel940ab932014-02-28 00:27:01 +00001092 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1093 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1094 MI->RemoveOperand(0);
1095
1096 MI->setDesc(get(PPC::BC));
1097 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1098 .addReg(Pred[1].getReg())
1099 .addMBB(MBB);
1100 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1101 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1102 MI->RemoveOperand(0);
1103
1104 MI->setDesc(get(PPC::BCn));
1105 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1106 .addReg(Pred[1].getReg())
1107 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001108 } else {
1109 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1110 MI->RemoveOperand(0);
1111
1112 MI->setDesc(get(PPC::BCC));
1113 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1114 .addImm(Pred[0].getImm())
1115 .addReg(Pred[1].getReg())
1116 .addMBB(MBB);
1117 }
1118
1119 return true;
Hal Finkel500b0042013-04-10 06:42:34 +00001120 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1121 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1122 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1123 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1124
1125 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
1126 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
Hal Finkel940ab932014-02-28 00:27:01 +00001127
1128 if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1129 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) :
1130 (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1131 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1132 .addReg(Pred[1].getReg());
1133 return true;
1134 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1135 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) :
1136 (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1137 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1138 .addReg(Pred[1].getReg());
1139 return true;
1140 }
1141
1142 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) :
1143 (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
Hal Finkel500b0042013-04-10 06:42:34 +00001144 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1145 .addImm(Pred[0].getImm())
1146 .addReg(Pred[1].getReg());
1147 return true;
Hal Finkel5711eca2013-04-09 22:58:37 +00001148 }
1149
1150 return false;
1151}
1152
1153bool PPCInstrInfo::SubsumesPredicate(
1154 const SmallVectorImpl<MachineOperand> &Pred1,
1155 const SmallVectorImpl<MachineOperand> &Pred2) const {
1156 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1157 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1158
1159 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1160 return false;
1161 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1162 return false;
1163
Hal Finkel94a6f382013-12-11 23:12:25 +00001164 // P1 can only subsume P2 if they test the same condition register.
1165 if (Pred1[1].getReg() != Pred2[1].getReg())
1166 return false;
1167
Hal Finkel5711eca2013-04-09 22:58:37 +00001168 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1169 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1170
1171 if (P1 == P2)
1172 return true;
1173
1174 // Does P1 subsume P2, e.g. GE subsumes GT.
1175 if (P1 == PPC::PRED_LE &&
1176 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1177 return true;
1178 if (P1 == PPC::PRED_GE &&
1179 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1180 return true;
1181
1182 return false;
1183}
1184
1185bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI,
1186 std::vector<MachineOperand> &Pred) const {
1187 // Note: At the present time, the contents of Pred from this function is
1188 // unused by IfConversion. This implementation follows ARM by pushing the
1189 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1190 // predicate, instructions defining CTR or CTR8 are also included as
1191 // predicate-defining instructions.
1192
1193 const TargetRegisterClass *RCs[] =
1194 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1195 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1196
1197 bool Found = false;
1198 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1199 const MachineOperand &MO = MI->getOperand(i);
Hal Finkelaf822012013-04-10 07:17:47 +00001200 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001201 const TargetRegisterClass *RC = RCs[c];
Hal Finkelaf822012013-04-10 07:17:47 +00001202 if (MO.isReg()) {
1203 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001204 Pred.push_back(MO);
1205 Found = true;
1206 }
Hal Finkelaf822012013-04-10 07:17:47 +00001207 } else if (MO.isRegMask()) {
1208 for (TargetRegisterClass::iterator I = RC->begin(),
1209 IE = RC->end(); I != IE; ++I)
1210 if (MO.clobbersPhysReg(*I)) {
1211 Pred.push_back(MO);
1212 Found = true;
1213 }
Hal Finkel5711eca2013-04-09 22:58:37 +00001214 }
1215 }
1216 }
1217
1218 return Found;
1219}
1220
1221bool PPCInstrInfo::isPredicable(MachineInstr *MI) const {
1222 unsigned OpC = MI->getOpcode();
1223 switch (OpC) {
1224 default:
1225 return false;
1226 case PPC::B:
1227 case PPC::BLR:
Hal Finkel500b0042013-04-10 06:42:34 +00001228 case PPC::BCTR:
1229 case PPC::BCTR8:
1230 case PPC::BCTRL:
1231 case PPC::BCTRL8:
Hal Finkel5711eca2013-04-09 22:58:37 +00001232 return true;
1233 }
1234}
1235
Hal Finkel82656cb2013-04-18 22:15:08 +00001236bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI,
1237 unsigned &SrcReg, unsigned &SrcReg2,
1238 int &Mask, int &Value) const {
1239 unsigned Opc = MI->getOpcode();
1240
1241 switch (Opc) {
1242 default: return false;
1243 case PPC::CMPWI:
1244 case PPC::CMPLWI:
1245 case PPC::CMPDI:
1246 case PPC::CMPLDI:
1247 SrcReg = MI->getOperand(1).getReg();
1248 SrcReg2 = 0;
1249 Value = MI->getOperand(2).getImm();
1250 Mask = 0xFFFF;
1251 return true;
1252 case PPC::CMPW:
1253 case PPC::CMPLW:
1254 case PPC::CMPD:
1255 case PPC::CMPLD:
1256 case PPC::FCMPUS:
1257 case PPC::FCMPUD:
1258 SrcReg = MI->getOperand(1).getReg();
1259 SrcReg2 = MI->getOperand(2).getReg();
1260 return true;
1261 }
1262}
Hal Finkele6322392013-04-19 22:08:38 +00001263
Hal Finkel82656cb2013-04-18 22:15:08 +00001264bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr,
1265 unsigned SrcReg, unsigned SrcReg2,
1266 int Mask, int Value,
1267 const MachineRegisterInfo *MRI) const {
Hal Finkelb12da6b2013-04-18 22:54:25 +00001268 if (DisableCmpOpt)
1269 return false;
1270
Hal Finkel82656cb2013-04-18 22:15:08 +00001271 int OpC = CmpInstr->getOpcode();
1272 unsigned CRReg = CmpInstr->getOperand(0).getReg();
Hal Finkel08e53ee2013-05-08 12:16:14 +00001273
1274 // FP record forms set CR1 based on the execption status bits, not a
1275 // comparison with zero.
1276 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1277 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001278
1279 // The record forms set the condition register based on a signed comparison
1280 // with zero (so says the ISA manual). This is not as straightforward as it
1281 // seems, however, because this is always a 64-bit comparison on PPC64, even
1282 // for instructions that are 32-bit in nature (like slw for example).
1283 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1284 // for equality checks (as those don't depend on the sign). On PPC64,
1285 // we are restricted to equality for unsigned 64-bit comparisons and for
1286 // signed 32-bit comparisons the applicability is more restricted.
1287 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1288 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1289 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1290 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1291
1292 // Get the unique definition of SrcReg.
1293 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1294 if (!MI) return false;
1295 int MIOpC = MI->getOpcode();
1296
1297 bool equalityOnly = false;
1298 bool noSub = false;
1299 if (isPPC64) {
1300 if (is32BitSignedCompare) {
1301 // We can perform this optimization only if MI is sign-extending.
1302 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1303 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1304 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1305 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1306 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1307 noSub = true;
1308 } else
1309 return false;
1310 } else if (is32BitUnsignedCompare) {
1311 // We can perform this optimization, equality only, if MI is
1312 // zero-extending.
1313 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1314 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
1315 MIOpC == PPC::SRW || MIOpC == PPC::SRWo) {
1316 noSub = true;
1317 equalityOnly = true;
1318 } else
1319 return false;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001320 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001321 equalityOnly = is64BitUnsignedCompare;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001322 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001323 equalityOnly = is32BitUnsignedCompare;
1324
1325 if (equalityOnly) {
1326 // We need to check the uses of the condition register in order to reject
1327 // non-equality comparisons.
Owen Anderson16c6bf42014-03-13 23:12:04 +00001328 for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg),
1329 IE = MRI->use_instr_end(); I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001330 MachineInstr *UseMI = &*I;
1331 if (UseMI->getOpcode() == PPC::BCC) {
1332 unsigned Pred = UseMI->getOperand(0).getImm();
Hal Finkelc3632452013-05-07 17:49:55 +00001333 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE)
1334 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001335 } else if (UseMI->getOpcode() == PPC::ISEL ||
1336 UseMI->getOpcode() == PPC::ISEL8) {
1337 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkelc3632452013-05-07 17:49:55 +00001338 if (SubIdx != PPC::sub_eq)
1339 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001340 } else
1341 return false;
1342 }
1343 }
1344
Hal Finkelc3632452013-05-07 17:49:55 +00001345 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001346
1347 // Scan forward to find the first use of the compare.
1348 for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end();
1349 I != EL; ++I) {
1350 bool FoundUse = false;
Owen Anderson16c6bf42014-03-13 23:12:04 +00001351 for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg),
1352 JE = MRI->use_instr_end(); J != JE; ++J)
Hal Finkel82656cb2013-04-18 22:15:08 +00001353 if (&*J == &*I) {
1354 FoundUse = true;
1355 break;
1356 }
1357
1358 if (FoundUse)
1359 break;
1360 }
1361
Hal Finkel82656cb2013-04-18 22:15:08 +00001362 // There are two possible candidates which can be changed to set CR[01].
1363 // One is MI, the other is a SUB instruction.
1364 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
1365 MachineInstr *Sub = NULL;
1366 if (SrcReg2 != 0)
1367 // MI is not a candidate for CMPrr.
1368 MI = NULL;
1369 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1370 // same BB as the comparison. This is to allow the check below to avoid calls
1371 // (and other explicit clobbers); instead we should really check for these
1372 // more explicitly (in at least a few predecessors).
1373 else if (MI->getParent() != CmpInstr->getParent() || Value != 0) {
1374 // PPC does not have a record-form SUBri.
1375 return false;
1376 }
1377
1378 // Search for Sub.
1379 const TargetRegisterInfo *TRI = &getRegisterInfo();
1380 --I;
Hal Finkelc3632452013-05-07 17:49:55 +00001381
1382 // Get ready to iterate backward from CmpInstr.
1383 MachineBasicBlock::iterator E = MI,
1384 B = CmpInstr->getParent()->begin();
1385
Hal Finkel82656cb2013-04-18 22:15:08 +00001386 for (; I != E && !noSub; --I) {
1387 const MachineInstr &Instr = *I;
1388 unsigned IOpC = Instr.getOpcode();
1389
1390 if (&*I != CmpInstr && (
Hal Finkel08e53ee2013-05-08 12:16:14 +00001391 Instr.modifiesRegister(PPC::CR0, TRI) ||
1392 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel82656cb2013-04-18 22:15:08 +00001393 // This instruction modifies or uses the record condition register after
1394 // the one we want to change. While we could do this transformation, it
1395 // would likely not be profitable. This transformation removes one
1396 // instruction, and so even forcing RA to generate one move probably
1397 // makes it unprofitable.
1398 return false;
1399
1400 // Check whether CmpInstr can be made redundant by the current instruction.
1401 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1402 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1403 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1404 ((Instr.getOperand(1).getReg() == SrcReg &&
1405 Instr.getOperand(2).getReg() == SrcReg2) ||
1406 (Instr.getOperand(1).getReg() == SrcReg2 &&
1407 Instr.getOperand(2).getReg() == SrcReg))) {
1408 Sub = &*I;
1409 break;
1410 }
1411
Hal Finkel82656cb2013-04-18 22:15:08 +00001412 if (I == B)
1413 // The 'and' is below the comparison instruction.
1414 return false;
1415 }
1416
1417 // Return false if no candidates exist.
1418 if (!MI && !Sub)
1419 return false;
1420
1421 // The single candidate is called MI.
1422 if (!MI) MI = Sub;
1423
1424 int NewOpC = -1;
1425 MIOpC = MI->getOpcode();
1426 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1427 NewOpC = MIOpC;
1428 else {
1429 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1430 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1431 NewOpC = MIOpC;
1432 }
1433
1434 // FIXME: On the non-embedded POWER architectures, only some of the record
1435 // forms are fast, and we should use only the fast ones.
1436
1437 // The defining instruction has a record form (or is already a record
1438 // form). It is possible, however, that we'll need to reverse the condition
1439 // code of the users.
1440 if (NewOpC == -1)
1441 return false;
1442
Hal Finkele6322392013-04-19 22:08:38 +00001443 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1444 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
Hal Finkel82656cb2013-04-18 22:15:08 +00001445
1446 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1447 // needs to be updated to be based on SUB. Push the condition code
1448 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1449 // condition code of these operands will be modified.
1450 bool ShouldSwap = false;
1451 if (Sub) {
1452 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1453 Sub->getOperand(2).getReg() == SrcReg;
1454
1455 // The operands to subf are the opposite of sub, so only in the fixed-point
1456 // case, invert the order.
Hal Finkel08e53ee2013-05-08 12:16:14 +00001457 ShouldSwap = !ShouldSwap;
Hal Finkel82656cb2013-04-18 22:15:08 +00001458 }
1459
1460 if (ShouldSwap)
Owen Anderson16c6bf42014-03-13 23:12:04 +00001461 for (MachineRegisterInfo::use_instr_iterator
1462 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1463 I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001464 MachineInstr *UseMI = &*I;
1465 if (UseMI->getOpcode() == PPC::BCC) {
1466 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hal Finkele6322392013-04-19 22:08:38 +00001467 assert((!equalityOnly ||
1468 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1469 "Invalid predicate for equality-only optimization");
Owen Anderson16c6bf42014-03-13 23:12:04 +00001470 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
Hal Finkel0f64e212013-04-20 05:16:26 +00001471 PPC::getSwappedPredicate(Pred)));
Hal Finkel82656cb2013-04-18 22:15:08 +00001472 } else if (UseMI->getOpcode() == PPC::ISEL ||
1473 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkele6322392013-04-19 22:08:38 +00001474 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1475 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1476 "Invalid CR bit for equality-only optimization");
1477
1478 if (NewSubReg == PPC::sub_lt)
1479 NewSubReg = PPC::sub_gt;
1480 else if (NewSubReg == PPC::sub_gt)
1481 NewSubReg = PPC::sub_lt;
1482
Owen Anderson16c6bf42014-03-13 23:12:04 +00001483 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
Hal Finkele6322392013-04-19 22:08:38 +00001484 NewSubReg));
Hal Finkel82656cb2013-04-18 22:15:08 +00001485 } else // We need to abort on a user we don't understand.
1486 return false;
1487 }
1488
1489 // Create a new virtual register to hold the value of the CR set by the
1490 // record-form instruction. If the instruction was not previously in
1491 // record form, then set the kill flag on the CR.
1492 CmpInstr->eraseFromParent();
1493
1494 MachineBasicBlock::iterator MII = MI;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001495 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
Hal Finkel82656cb2013-04-18 22:15:08 +00001496 get(TargetOpcode::COPY), CRReg)
Hal Finkel08e53ee2013-05-08 12:16:14 +00001497 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel82656cb2013-04-18 22:15:08 +00001498
1499 if (MIOpC != NewOpC) {
1500 // We need to be careful here: we're replacing one instruction with
1501 // another, and we need to make sure that we get all of the right
1502 // implicit uses and defs. On the other hand, the caller may be holding
1503 // an iterator to this instruction, and so we can't delete it (this is
1504 // specifically the case if this is the instruction directly after the
1505 // compare).
1506
1507 const MCInstrDesc &NewDesc = get(NewOpC);
1508 MI->setDesc(NewDesc);
1509
1510 if (NewDesc.ImplicitDefs)
1511 for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs();
1512 *ImpDefs; ++ImpDefs)
1513 if (!MI->definesRegister(*ImpDefs))
1514 MI->addOperand(*MI->getParent()->getParent(),
1515 MachineOperand::CreateReg(*ImpDefs, true, true));
1516 if (NewDesc.ImplicitUses)
1517 for (const uint16_t *ImpUses = NewDesc.getImplicitUses();
1518 *ImpUses; ++ImpUses)
1519 if (!MI->readsRegister(*ImpUses))
1520 MI->addOperand(*MI->getParent()->getParent(),
1521 MachineOperand::CreateReg(*ImpUses, false, true));
1522 }
1523
1524 // Modify the condition code of operands in OperandsToUpdate.
1525 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1526 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkele6322392013-04-19 22:08:38 +00001527 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1528 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001529
Hal Finkele6322392013-04-19 22:08:38 +00001530 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1531 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001532
1533 return true;
1534}
1535
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001536/// GetInstSize - Return the number of bytes of code the specified
1537/// instruction may be. This returns the maximum number of bytes.
1538///
1539unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Hal Finkela7bbaf62014-02-02 06:12:27 +00001540 unsigned Opcode = MI->getOpcode();
1541
1542 if (Opcode == PPC::INLINEASM) {
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001543 const MachineFunction *MF = MI->getParent()->getParent();
1544 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +00001545 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Hal Finkela7bbaf62014-02-02 06:12:27 +00001546 } else {
1547 const MCInstrDesc &Desc = get(Opcode);
1548 return Desc.getSize();
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001549 }
1550}
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001551
Hal Finkel27774d92014-03-13 07:58:58 +00001552
1553#undef DEBUG_TYPE
1554#define DEBUG_TYPE "ppc-vsx-copy"
1555
1556namespace llvm {
1557 void initializePPCVSXCopyPass(PassRegistry&);
1558}
1559
1560namespace {
1561 // PPCVSXCopy pass - For copies between VSX registers and non-VSX registers
1562 // (Altivec and scalar floating-point registers), we need to transform the
1563 // copies into subregister copies with other restrictions.
1564 struct PPCVSXCopy : public MachineFunctionPass {
1565 static char ID;
1566 PPCVSXCopy() : MachineFunctionPass(ID) {
1567 initializePPCVSXCopyPass(*PassRegistry::getPassRegistry());
1568 }
1569
1570 const PPCTargetMachine *TM;
1571 const PPCInstrInfo *TII;
1572
1573 bool IsRegInClass(unsigned Reg, const TargetRegisterClass *RC,
1574 MachineRegisterInfo &MRI) {
1575 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1576 return RC->hasSubClassEq(MRI.getRegClass(Reg));
1577 } else if (RC->contains(Reg)) {
1578 return true;
1579 }
1580
1581 return false;
1582 }
1583
1584 bool IsVSReg(unsigned Reg, MachineRegisterInfo &MRI) {
1585 return IsRegInClass(Reg, &PPC::VSRCRegClass, MRI);
1586 }
1587
1588 bool IsVRReg(unsigned Reg, MachineRegisterInfo &MRI) {
1589 return IsRegInClass(Reg, &PPC::VRRCRegClass, MRI);
1590 }
1591
1592 bool IsF8Reg(unsigned Reg, MachineRegisterInfo &MRI) {
1593 return IsRegInClass(Reg, &PPC::F8RCRegClass, MRI);
1594 }
1595
1596protected:
1597 bool processBlock(MachineBasicBlock &MBB) {
1598 bool Changed = false;
1599
1600 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
1601 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1602 I != IE; ++I) {
1603 MachineInstr *MI = I;
1604 if (!MI->isFullCopy())
1605 continue;
1606
1607 MachineOperand &DstMO = MI->getOperand(0);
1608 MachineOperand &SrcMO = MI->getOperand(1);
1609
1610 if ( IsVSReg(DstMO.getReg(), MRI) &&
1611 !IsVSReg(SrcMO.getReg(), MRI)) {
1612 // This is a copy *to* a VSX register from a non-VSX register.
1613 Changed = true;
1614
1615 const TargetRegisterClass *SrcRC =
1616 IsVRReg(SrcMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
1617 &PPC::VSLRCRegClass;
1618 assert((IsF8Reg(SrcMO.getReg(), MRI) ||
1619 IsVRReg(SrcMO.getReg(), MRI)) &&
1620 "Unknown source for a VSX copy");
1621
1622 unsigned NewVReg = MRI.createVirtualRegister(SrcRC);
1623 BuildMI(MBB, MI, MI->getDebugLoc(),
1624 TII->get(TargetOpcode::SUBREG_TO_REG), NewVReg)
1625 .addImm(1) // add 1, not 0, because there is no implicit clearing
1626 // of the high bits.
1627 .addOperand(SrcMO)
1628 .addImm(IsVRReg(SrcMO.getReg(), MRI) ? PPC::sub_128 :
1629 PPC::sub_64);
1630
1631 // The source of the original copy is now the new virtual register.
1632 SrcMO.setReg(NewVReg);
1633 } else if (!IsVSReg(DstMO.getReg(), MRI) &&
1634 IsVSReg(SrcMO.getReg(), MRI)) {
1635 // This is a copy *from* a VSX register to a non-VSX register.
1636 Changed = true;
1637
1638 const TargetRegisterClass *DstRC =
1639 IsVRReg(DstMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
1640 &PPC::VSLRCRegClass;
1641 assert((IsF8Reg(DstMO.getReg(), MRI) ||
1642 IsVRReg(DstMO.getReg(), MRI)) &&
1643 "Unknown destination for a VSX copy");
1644
1645 // Copy the VSX value into a new VSX register of the correct subclass.
1646 unsigned NewVReg = MRI.createVirtualRegister(DstRC);
1647 BuildMI(MBB, MI, MI->getDebugLoc(),
1648 TII->get(TargetOpcode::COPY), NewVReg)
1649 .addOperand(SrcMO);
1650
1651 // Transform the original copy into a subregister extraction copy.
1652 SrcMO.setReg(NewVReg);
1653 SrcMO.setSubReg(IsVRReg(DstMO.getReg(), MRI) ? PPC::sub_128 :
1654 PPC::sub_64);
1655 }
1656 }
1657
1658 return Changed;
1659 }
1660
1661public:
1662 virtual bool runOnMachineFunction(MachineFunction &MF) {
1663 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
1664 TII = TM->getInstrInfo();
1665
1666 bool Changed = false;
1667
1668 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
1669 MachineBasicBlock &B = *I++;
1670 if (processBlock(B))
1671 Changed = true;
1672 }
1673
1674 return Changed;
1675 }
1676
1677 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1678 MachineFunctionPass::getAnalysisUsage(AU);
1679 }
1680 };
1681}
1682
1683INITIALIZE_PASS(PPCVSXCopy, DEBUG_TYPE,
1684 "PowerPC VSX Copy Legalization", false, false)
1685
1686char PPCVSXCopy::ID = 0;
1687FunctionPass*
1688llvm::createPPCVSXCopyPass() { return new PPCVSXCopy(); }
1689
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001690#undef DEBUG_TYPE
1691#define DEBUG_TYPE "ppc-early-ret"
1692STATISTIC(NumBCLR, "Number of early conditional returns");
1693STATISTIC(NumBLR, "Number of early returns");
1694
1695namespace llvm {
1696 void initializePPCEarlyReturnPass(PassRegistry&);
1697}
1698
1699namespace {
1700 // PPCEarlyReturn pass - For simple functions without epilogue code, move
1701 // returns up, and create conditional returns, to avoid unnecessary
1702 // branch-to-blr sequences.
1703 struct PPCEarlyReturn : public MachineFunctionPass {
1704 static char ID;
1705 PPCEarlyReturn() : MachineFunctionPass(ID) {
1706 initializePPCEarlyReturnPass(*PassRegistry::getPassRegistry());
1707 }
1708
1709 const PPCTargetMachine *TM;
1710 const PPCInstrInfo *TII;
1711
1712protected:
Hal Finkel21aad9a2013-04-09 18:25:18 +00001713 bool processBlock(MachineBasicBlock &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001714 bool Changed = false;
1715
Hal Finkel21aad9a2013-04-09 18:25:18 +00001716 MachineBasicBlock::iterator I = ReturnMBB.begin();
1717 I = ReturnMBB.SkipPHIsAndLabels(I);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001718
1719 // The block must be essentially empty except for the blr.
Hal Finkel21aad9a2013-04-09 18:25:18 +00001720 if (I == ReturnMBB.end() || I->getOpcode() != PPC::BLR ||
1721 I != ReturnMBB.getLastNonDebugInstr())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001722 return Changed;
1723
1724 SmallVector<MachineBasicBlock*, 8> PredToRemove;
Hal Finkel21aad9a2013-04-09 18:25:18 +00001725 for (MachineBasicBlock::pred_iterator PI = ReturnMBB.pred_begin(),
1726 PIE = ReturnMBB.pred_end(); PI != PIE; ++PI) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001727 bool OtherReference = false, BlockChanged = false;
Hal Finkel21aad9a2013-04-09 18:25:18 +00001728 for (MachineBasicBlock::iterator J = (*PI)->getLastNonDebugInstr();;) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001729 if (J->getOpcode() == PPC::B) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00001730 if (J->getOperand(0).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001731 // This is an unconditional branch to the return. Replace the
Andrew Trick9defbd82013-12-17 04:50:40 +00001732 // branch with a blr.
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001733 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BLR));
Hal Finkel21aad9a2013-04-09 18:25:18 +00001734 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001735 K->eraseFromParent();
1736 BlockChanged = true;
1737 ++NumBLR;
1738 continue;
1739 }
1740 } else if (J->getOpcode() == PPC::BCC) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00001741 if (J->getOperand(2).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001742 // This is a conditional branch to the return. Replace the branch
1743 // with a bclr.
Hal Finkel940ab932014-02-28 00:27:01 +00001744 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BCCLR))
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001745 .addImm(J->getOperand(0).getImm())
1746 .addReg(J->getOperand(1).getReg());
Hal Finkel21aad9a2013-04-09 18:25:18 +00001747 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001748 K->eraseFromParent();
1749 BlockChanged = true;
1750 ++NumBCLR;
1751 continue;
1752 }
Hal Finkel940ab932014-02-28 00:27:01 +00001753 } else if (J->getOpcode() == PPC::BC || J->getOpcode() == PPC::BCn) {
1754 if (J->getOperand(1).getMBB() == &ReturnMBB) {
1755 // This is a conditional branch to the return. Replace the branch
1756 // with a bclr.
1757 BuildMI(**PI, J, J->getDebugLoc(),
1758 TII->get(J->getOpcode() == PPC::BC ?
1759 PPC::BCLR : PPC::BCLRn))
1760 .addReg(J->getOperand(0).getReg());
1761 MachineBasicBlock::iterator K = J--;
1762 K->eraseFromParent();
1763 BlockChanged = true;
1764 ++NumBCLR;
1765 continue;
1766 }
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001767 } else if (J->isBranch()) {
1768 if (J->isIndirectBranch()) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00001769 if (ReturnMBB.hasAddressTaken())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001770 OtherReference = true;
1771 } else
1772 for (unsigned i = 0; i < J->getNumOperands(); ++i)
1773 if (J->getOperand(i).isMBB() &&
Hal Finkel21aad9a2013-04-09 18:25:18 +00001774 J->getOperand(i).getMBB() == &ReturnMBB)
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001775 OtherReference = true;
Hal Finkel21aad9a2013-04-09 18:25:18 +00001776 } else if (!J->isTerminator() && !J->isDebugValue())
1777 break;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001778
Hal Finkel21aad9a2013-04-09 18:25:18 +00001779 if (J == (*PI)->begin())
1780 break;
1781
1782 --J;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001783 }
1784
Hal Finkel21aad9a2013-04-09 18:25:18 +00001785 if ((*PI)->canFallThrough() && (*PI)->isLayoutSuccessor(&ReturnMBB))
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001786 OtherReference = true;
1787
Andrew Trick9defbd82013-12-17 04:50:40 +00001788 // Predecessors are stored in a vector and can't be removed here.
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001789 if (!OtherReference && BlockChanged) {
1790 PredToRemove.push_back(*PI);
1791 }
1792
1793 if (BlockChanged)
1794 Changed = true;
1795 }
1796
1797 for (unsigned i = 0, ie = PredToRemove.size(); i != ie; ++i)
Hal Finkel21aad9a2013-04-09 18:25:18 +00001798 PredToRemove[i]->removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001799
Hal Finkel21aad9a2013-04-09 18:25:18 +00001800 if (Changed && !ReturnMBB.hasAddressTaken()) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001801 // We now might be able to merge this blr-only block into its
1802 // by-layout predecessor.
Hal Finkel21aad9a2013-04-09 18:25:18 +00001803 if (ReturnMBB.pred_size() == 1 &&
1804 (*ReturnMBB.pred_begin())->isLayoutSuccessor(&ReturnMBB)) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001805 // Move the blr into the preceding block.
Hal Finkel21aad9a2013-04-09 18:25:18 +00001806 MachineBasicBlock &PrevMBB = **ReturnMBB.pred_begin();
1807 PrevMBB.splice(PrevMBB.end(), &ReturnMBB, I);
1808 PrevMBB.removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001809 }
1810
Hal Finkel21aad9a2013-04-09 18:25:18 +00001811 if (ReturnMBB.pred_empty())
1812 ReturnMBB.eraseFromParent();
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001813 }
1814
1815 return Changed;
1816 }
1817
1818public:
1819 virtual bool runOnMachineFunction(MachineFunction &MF) {
1820 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
1821 TII = TM->getInstrInfo();
1822
1823 bool Changed = false;
1824
Hal Finkel21aad9a2013-04-09 18:25:18 +00001825 // If the function does not have at least two blocks, then there is
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001826 // nothing to do.
1827 if (MF.size() < 2)
1828 return Changed;
1829
1830 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
Andrew Trick9defbd82013-12-17 04:50:40 +00001831 MachineBasicBlock &B = *I++;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001832 if (processBlock(B))
1833 Changed = true;
1834 }
1835
1836 return Changed;
1837 }
1838
1839 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1840 MachineFunctionPass::getAnalysisUsage(AU);
1841 }
1842 };
1843}
1844
1845INITIALIZE_PASS(PPCEarlyReturn, DEBUG_TYPE,
1846 "PowerPC Early-Return Creation", false, false)
1847
1848char PPCEarlyReturn::ID = 0;
1849FunctionPass*
1850llvm::createPPCEarlyReturnPass() { return new PPCEarlyReturn(); }