[PowerPC] Convert r+r instructions to r+i (pre and post RA)
[llvm-core.git] / lib / Target / PowerPC / PPCInstrInfo.cpp
bloba9060b417945e3fdb8ee939c1387726f1bdd806a
1 //===-- PPCInstrInfo.cpp - PowerPC Instruction Information ----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the PowerPC implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "PPCInstrInfo.h"
15 #include "MCTargetDesc/PPCPredicates.h"
16 #include "PPC.h"
17 #include "PPCHazardRecognizers.h"
18 #include "PPCInstrBuilder.h"
19 #include "PPCMachineFunctionInfo.h"
20 #include "PPCTargetMachine.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/CodeGen/LiveIntervals.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineMemOperand.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/CodeGen/PseudoSourceValue.h"
30 #include "llvm/CodeGen/ScheduleDAG.h"
31 #include "llvm/CodeGen/SlotIndexes.h"
32 #include "llvm/CodeGen/StackMaps.h"
33 #include "llvm/MC/MCAsmInfo.h"
34 #include "llvm/MC/MCInst.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/TargetRegistry.h"
39 #include "llvm/Support/raw_ostream.h"
41 using namespace llvm;
43 #define DEBUG_TYPE "ppc-instr-info"
45 #define GET_INSTRMAP_INFO
46 #define GET_INSTRINFO_CTOR_DTOR
47 #include "PPCGenInstrInfo.inc"
49 STATISTIC(NumStoreSPILLVSRRCAsVec,
50 "Number of spillvsrrc spilled to stack as vec");
51 STATISTIC(NumStoreSPILLVSRRCAsGpr,
52 "Number of spillvsrrc spilled to stack as gpr");
53 STATISTIC(NumGPRtoVSRSpill, "Number of gpr spills to spillvsrrc");
54 STATISTIC(CmpIselsConverted,
55 "Number of ISELs that depend on comparison of constants converted");
56 STATISTIC(MissedConvertibleImmediateInstrs,
57 "Number of compare-immediate instructions fed by constants");
59 static cl::
60 opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
61 cl::desc("Disable analysis for CTR loops"));
63 static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt",
64 cl::desc("Disable compare instruction optimization"), cl::Hidden);
66 static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy",
67 cl::desc("Causes the backend to crash instead of generating a nop VSX copy"),
68 cl::Hidden);
70 static cl::opt<bool>
71 UseOldLatencyCalc("ppc-old-latency-calc", cl::Hidden,
72 cl::desc("Use the old (incorrect) instruction latency calculation"));
74 // Pin the vtable to this file.
75 void PPCInstrInfo::anchor() {}
77 PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI)
78 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP,
79 /* CatchRetOpcode */ -1,
80 STI.isPPC64() ? PPC::BLR8 : PPC::BLR),
81 Subtarget(STI), RI(STI.getTargetMachine()) {}
83 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
84 /// this target when scheduling the DAG.
85 ScheduleHazardRecognizer *
86 PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
87 const ScheduleDAG *DAG) const {
88 unsigned Directive =
89 static_cast<const PPCSubtarget *>(STI)->getDarwinDirective();
90 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
91 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
92 const InstrItineraryData *II =
93 static_cast<const PPCSubtarget *>(STI)->getInstrItineraryData();
94 return new ScoreboardHazardRecognizer(II, DAG);
97 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
100 /// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
101 /// to use for this target when scheduling the DAG.
102 ScheduleHazardRecognizer *
103 PPCInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
104 const ScheduleDAG *DAG) const {
105 unsigned Directive =
106 DAG->MF.getSubtarget<PPCSubtarget>().getDarwinDirective();
108 // FIXME: Leaving this as-is until we have POWER9 scheduling info
109 if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8)
110 return new PPCDispatchGroupSBHazardRecognizer(II, DAG);
112 // Most subtargets use a PPC970 recognizer.
113 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
114 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
115 assert(DAG->TII && "No InstrInfo?");
117 return new PPCHazardRecognizer970(*DAG);
120 return new ScoreboardHazardRecognizer(II, DAG);
123 unsigned PPCInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
124 const MachineInstr &MI,
125 unsigned *PredCost) const {
126 if (!ItinData || UseOldLatencyCalc)
127 return PPCGenInstrInfo::getInstrLatency(ItinData, MI, PredCost);
129 // The default implementation of getInstrLatency calls getStageLatency, but
130 // getStageLatency does not do the right thing for us. While we have
131 // itinerary, most cores are fully pipelined, and so the itineraries only
132 // express the first part of the pipeline, not every stage. Instead, we need
133 // to use the listed output operand cycle number (using operand 0 here, which
134 // is an output).
136 unsigned Latency = 1;
137 unsigned DefClass = MI.getDesc().getSchedClass();
138 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
139 const MachineOperand &MO = MI.getOperand(i);
140 if (!MO.isReg() || !MO.isDef() || MO.isImplicit())
141 continue;
143 int Cycle = ItinData->getOperandCycle(DefClass, i);
144 if (Cycle < 0)
145 continue;
147 Latency = std::max(Latency, (unsigned) Cycle);
150 return Latency;
153 int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
154 const MachineInstr &DefMI, unsigned DefIdx,
155 const MachineInstr &UseMI,
156 unsigned UseIdx) const {
157 int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
158 UseMI, UseIdx);
160 if (!DefMI.getParent())
161 return Latency;
163 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
164 unsigned Reg = DefMO.getReg();
166 bool IsRegCR;
167 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
168 const MachineRegisterInfo *MRI =
169 &DefMI.getParent()->getParent()->getRegInfo();
170 IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) ||
171 MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass);
172 } else {
173 IsRegCR = PPC::CRRCRegClass.contains(Reg) ||
174 PPC::CRBITRCRegClass.contains(Reg);
177 if (UseMI.isBranch() && IsRegCR) {
178 if (Latency < 0)
179 Latency = getInstrLatency(ItinData, DefMI);
181 // On some cores, there is an additional delay between writing to a condition
182 // register, and using it from a branch.
183 unsigned Directive = Subtarget.getDarwinDirective();
184 switch (Directive) {
185 default: break;
186 case PPC::DIR_7400:
187 case PPC::DIR_750:
188 case PPC::DIR_970:
189 case PPC::DIR_E5500:
190 case PPC::DIR_PWR4:
191 case PPC::DIR_PWR5:
192 case PPC::DIR_PWR5X:
193 case PPC::DIR_PWR6:
194 case PPC::DIR_PWR6X:
195 case PPC::DIR_PWR7:
196 case PPC::DIR_PWR8:
197 // FIXME: Is this needed for POWER9?
198 Latency += 2;
199 break;
203 return Latency;
206 // This function does not list all associative and commutative operations, but
207 // only those worth feeding through the machine combiner in an attempt to
208 // reduce the critical path. Mostly, this means floating-point operations,
209 // because they have high latencies (compared to other operations, such and
210 // and/or, which are also associative and commutative, but have low latencies).
211 bool PPCInstrInfo::isAssociativeAndCommutative(const MachineInstr &Inst) const {
212 switch (Inst.getOpcode()) {
213 // FP Add:
214 case PPC::FADD:
215 case PPC::FADDS:
216 // FP Multiply:
217 case PPC::FMUL:
218 case PPC::FMULS:
219 // Altivec Add:
220 case PPC::VADDFP:
221 // VSX Add:
222 case PPC::XSADDDP:
223 case PPC::XVADDDP:
224 case PPC::XVADDSP:
225 case PPC::XSADDSP:
226 // VSX Multiply:
227 case PPC::XSMULDP:
228 case PPC::XVMULDP:
229 case PPC::XVMULSP:
230 case PPC::XSMULSP:
231 // QPX Add:
232 case PPC::QVFADD:
233 case PPC::QVFADDS:
234 case PPC::QVFADDSs:
235 // QPX Multiply:
236 case PPC::QVFMUL:
237 case PPC::QVFMULS:
238 case PPC::QVFMULSs:
239 return true;
240 default:
241 return false;
245 bool PPCInstrInfo::getMachineCombinerPatterns(
246 MachineInstr &Root,
247 SmallVectorImpl<MachineCombinerPattern> &Patterns) const {
248 // Using the machine combiner in this way is potentially expensive, so
249 // restrict to when aggressive optimizations are desired.
250 if (Subtarget.getTargetMachine().getOptLevel() != CodeGenOpt::Aggressive)
251 return false;
253 // FP reassociation is only legal when we don't need strict IEEE semantics.
254 if (!Root.getParent()->getParent()->getTarget().Options.UnsafeFPMath)
255 return false;
257 return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns);
260 // Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
261 bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
262 unsigned &SrcReg, unsigned &DstReg,
263 unsigned &SubIdx) const {
264 switch (MI.getOpcode()) {
265 default: return false;
266 case PPC::EXTSW:
267 case PPC::EXTSW_32:
268 case PPC::EXTSW_32_64:
269 SrcReg = MI.getOperand(1).getReg();
270 DstReg = MI.getOperand(0).getReg();
271 SubIdx = PPC::sub_32;
272 return true;
276 unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
277 int &FrameIndex) const {
278 // Note: This list must be kept consistent with LoadRegFromStackSlot.
279 switch (MI.getOpcode()) {
280 default: break;
281 case PPC::LD:
282 case PPC::LWZ:
283 case PPC::LFS:
284 case PPC::LFD:
285 case PPC::RESTORE_CR:
286 case PPC::RESTORE_CRBIT:
287 case PPC::LVX:
288 case PPC::LXVD2X:
289 case PPC::LXV:
290 case PPC::QVLFDX:
291 case PPC::QVLFSXs:
292 case PPC::QVLFDXb:
293 case PPC::RESTORE_VRSAVE:
294 case PPC::SPILLTOVSR_LD:
295 // Check for the operands added by addFrameReference (the immediate is the
296 // offset which defaults to 0).
297 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() &&
298 MI.getOperand(2).isFI()) {
299 FrameIndex = MI.getOperand(2).getIndex();
300 return MI.getOperand(0).getReg();
302 break;
304 return 0;
307 // For opcodes with the ReMaterializable flag set, this function is called to
308 // verify the instruction is really rematable.
309 bool PPCInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr &MI,
310 AliasAnalysis *AA) const {
311 switch (MI.getOpcode()) {
312 default:
313 // This function should only be called for opcodes with the ReMaterializable
314 // flag set.
315 llvm_unreachable("Unknown rematerializable operation!");
316 break;
317 case PPC::LI:
318 case PPC::LI8:
319 case PPC::LIS:
320 case PPC::LIS8:
321 case PPC::QVGPCI:
322 case PPC::ADDIStocHA:
323 case PPC::ADDItocL:
324 case PPC::LOAD_STACK_GUARD:
325 return true;
327 return false;
330 unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
331 int &FrameIndex) const {
332 // Note: This list must be kept consistent with StoreRegToStackSlot.
333 switch (MI.getOpcode()) {
334 default: break;
335 case PPC::STD:
336 case PPC::STW:
337 case PPC::STFS:
338 case PPC::STFD:
339 case PPC::SPILL_CR:
340 case PPC::SPILL_CRBIT:
341 case PPC::STVX:
342 case PPC::STXVD2X:
343 case PPC::STXV:
344 case PPC::QVSTFDX:
345 case PPC::QVSTFSXs:
346 case PPC::QVSTFDXb:
347 case PPC::SPILL_VRSAVE:
348 case PPC::SPILLTOVSR_ST:
349 // Check for the operands added by addFrameReference (the immediate is the
350 // offset which defaults to 0).
351 if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() &&
352 MI.getOperand(2).isFI()) {
353 FrameIndex = MI.getOperand(2).getIndex();
354 return MI.getOperand(0).getReg();
356 break;
358 return 0;
361 MachineInstr *PPCInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI,
362 unsigned OpIdx1,
363 unsigned OpIdx2) const {
364 MachineFunction &MF = *MI.getParent()->getParent();
366 // Normal instructions can be commuted the obvious way.
367 if (MI.getOpcode() != PPC::RLWIMI && MI.getOpcode() != PPC::RLWIMIo)
368 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
369 // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a
370 // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because
371 // changing the relative order of the mask operands might change what happens
372 // to the high-bits of the mask (and, thus, the result).
374 // Cannot commute if it has a non-zero rotate count.
375 if (MI.getOperand(3).getImm() != 0)
376 return nullptr;
378 // If we have a zero rotate count, we have:
379 // M = mask(MB,ME)
380 // Op0 = (Op1 & ~M) | (Op2 & M)
381 // Change this to:
382 // M = mask((ME+1)&31, (MB-1)&31)
383 // Op0 = (Op2 & ~M) | (Op1 & M)
385 // Swap op1/op2
386 assert(((OpIdx1 == 1 && OpIdx2 == 2) || (OpIdx1 == 2 && OpIdx2 == 1)) &&
387 "Only the operands 1 and 2 can be swapped in RLSIMI/RLWIMIo.");
388 unsigned Reg0 = MI.getOperand(0).getReg();
389 unsigned Reg1 = MI.getOperand(1).getReg();
390 unsigned Reg2 = MI.getOperand(2).getReg();
391 unsigned SubReg1 = MI.getOperand(1).getSubReg();
392 unsigned SubReg2 = MI.getOperand(2).getSubReg();
393 bool Reg1IsKill = MI.getOperand(1).isKill();
394 bool Reg2IsKill = MI.getOperand(2).isKill();
395 bool ChangeReg0 = false;
396 // If machine instrs are no longer in two-address forms, update
397 // destination register as well.
398 if (Reg0 == Reg1) {
399 // Must be two address instruction!
400 assert(MI.getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
401 "Expecting a two-address instruction!");
402 assert(MI.getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
403 Reg2IsKill = false;
404 ChangeReg0 = true;
407 // Masks.
408 unsigned MB = MI.getOperand(4).getImm();
409 unsigned ME = MI.getOperand(5).getImm();
411 // We can't commute a trivial mask (there is no way to represent an all-zero
412 // mask).
413 if (MB == 0 && ME == 31)
414 return nullptr;
416 if (NewMI) {
417 // Create a new instruction.
418 unsigned Reg0 = ChangeReg0 ? Reg2 : MI.getOperand(0).getReg();
419 bool Reg0IsDead = MI.getOperand(0).isDead();
420 return BuildMI(MF, MI.getDebugLoc(), MI.getDesc())
421 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
422 .addReg(Reg2, getKillRegState(Reg2IsKill))
423 .addReg(Reg1, getKillRegState(Reg1IsKill))
424 .addImm((ME + 1) & 31)
425 .addImm((MB - 1) & 31);
428 if (ChangeReg0) {
429 MI.getOperand(0).setReg(Reg2);
430 MI.getOperand(0).setSubReg(SubReg2);
432 MI.getOperand(2).setReg(Reg1);
433 MI.getOperand(1).setReg(Reg2);
434 MI.getOperand(2).setSubReg(SubReg1);
435 MI.getOperand(1).setSubReg(SubReg2);
436 MI.getOperand(2).setIsKill(Reg1IsKill);
437 MI.getOperand(1).setIsKill(Reg2IsKill);
439 // Swap the mask around.
440 MI.getOperand(4).setImm((ME + 1) & 31);
441 MI.getOperand(5).setImm((MB - 1) & 31);
442 return &MI;
445 bool PPCInstrInfo::findCommutedOpIndices(MachineInstr &MI, unsigned &SrcOpIdx1,
446 unsigned &SrcOpIdx2) const {
447 // For VSX A-Type FMA instructions, it is the first two operands that can be
448 // commuted, however, because the non-encoded tied input operand is listed
449 // first, the operands to swap are actually the second and third.
451 int AltOpc = PPC::getAltVSXFMAOpcode(MI.getOpcode());
452 if (AltOpc == -1)
453 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
455 // The commutable operand indices are 2 and 3. Return them in SrcOpIdx1
456 // and SrcOpIdx2.
457 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2, 3);
460 void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
461 MachineBasicBlock::iterator MI) const {
462 // This function is used for scheduling, and the nop wanted here is the type
463 // that terminates dispatch groups on the POWER cores.
464 unsigned Directive = Subtarget.getDarwinDirective();
465 unsigned Opcode;
466 switch (Directive) {
467 default: Opcode = PPC::NOP; break;
468 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
469 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
470 case PPC::DIR_PWR8: Opcode = PPC::NOP_GT_PWR7; break; /* FIXME: Update when P8 InstrScheduling model is ready */
471 // FIXME: Update when POWER9 scheduling model is ready.
472 case PPC::DIR_PWR9: Opcode = PPC::NOP_GT_PWR7; break;
475 DebugLoc DL;
476 BuildMI(MBB, MI, DL, get(Opcode));
479 /// Return the noop instruction to use for a noop.
480 void PPCInstrInfo::getNoop(MCInst &NopInst) const {
481 NopInst.setOpcode(PPC::NOP);
484 // Branch analysis.
485 // Note: If the condition register is set to CTR or CTR8 then this is a
486 // BDNZ (imm == 1) or BDZ (imm == 0) branch.
487 bool PPCInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
488 MachineBasicBlock *&TBB,
489 MachineBasicBlock *&FBB,
490 SmallVectorImpl<MachineOperand> &Cond,
491 bool AllowModify) const {
492 bool isPPC64 = Subtarget.isPPC64();
494 // If the block has no terminators, it just falls into the block after it.
495 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
496 if (I == MBB.end())
497 return false;
499 if (!isUnpredicatedTerminator(*I))
500 return false;
502 if (AllowModify) {
503 // If the BB ends with an unconditional branch to the fallthrough BB,
504 // we eliminate the branch instruction.
505 if (I->getOpcode() == PPC::B &&
506 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
507 I->eraseFromParent();
509 // We update iterator after deleting the last branch.
510 I = MBB.getLastNonDebugInstr();
511 if (I == MBB.end() || !isUnpredicatedTerminator(*I))
512 return false;
516 // Get the last instruction in the block.
517 MachineInstr &LastInst = *I;
519 // If there is only one terminator instruction, process it.
520 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
521 if (LastInst.getOpcode() == PPC::B) {
522 if (!LastInst.getOperand(0).isMBB())
523 return true;
524 TBB = LastInst.getOperand(0).getMBB();
525 return false;
526 } else if (LastInst.getOpcode() == PPC::BCC) {
527 if (!LastInst.getOperand(2).isMBB())
528 return true;
529 // Block ends with fall-through condbranch.
530 TBB = LastInst.getOperand(2).getMBB();
531 Cond.push_back(LastInst.getOperand(0));
532 Cond.push_back(LastInst.getOperand(1));
533 return false;
534 } else if (LastInst.getOpcode() == PPC::BC) {
535 if (!LastInst.getOperand(1).isMBB())
536 return true;
537 // Block ends with fall-through condbranch.
538 TBB = LastInst.getOperand(1).getMBB();
539 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
540 Cond.push_back(LastInst.getOperand(0));
541 return false;
542 } else if (LastInst.getOpcode() == PPC::BCn) {
543 if (!LastInst.getOperand(1).isMBB())
544 return true;
545 // Block ends with fall-through condbranch.
546 TBB = LastInst.getOperand(1).getMBB();
547 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
548 Cond.push_back(LastInst.getOperand(0));
549 return false;
550 } else if (LastInst.getOpcode() == PPC::BDNZ8 ||
551 LastInst.getOpcode() == PPC::BDNZ) {
552 if (!LastInst.getOperand(0).isMBB())
553 return true;
554 if (DisableCTRLoopAnal)
555 return true;
556 TBB = LastInst.getOperand(0).getMBB();
557 Cond.push_back(MachineOperand::CreateImm(1));
558 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
559 true));
560 return false;
561 } else if (LastInst.getOpcode() == PPC::BDZ8 ||
562 LastInst.getOpcode() == PPC::BDZ) {
563 if (!LastInst.getOperand(0).isMBB())
564 return true;
565 if (DisableCTRLoopAnal)
566 return true;
567 TBB = LastInst.getOperand(0).getMBB();
568 Cond.push_back(MachineOperand::CreateImm(0));
569 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
570 true));
571 return false;
574 // Otherwise, don't know what this is.
575 return true;
578 // Get the instruction before it if it's a terminator.
579 MachineInstr &SecondLastInst = *I;
581 // If there are three terminators, we don't know what sort of block this is.
582 if (I != MBB.begin() && isUnpredicatedTerminator(*--I))
583 return true;
585 // If the block ends with PPC::B and PPC:BCC, handle it.
586 if (SecondLastInst.getOpcode() == PPC::BCC &&
587 LastInst.getOpcode() == PPC::B) {
588 if (!SecondLastInst.getOperand(2).isMBB() ||
589 !LastInst.getOperand(0).isMBB())
590 return true;
591 TBB = SecondLastInst.getOperand(2).getMBB();
592 Cond.push_back(SecondLastInst.getOperand(0));
593 Cond.push_back(SecondLastInst.getOperand(1));
594 FBB = LastInst.getOperand(0).getMBB();
595 return false;
596 } else if (SecondLastInst.getOpcode() == PPC::BC &&
597 LastInst.getOpcode() == PPC::B) {
598 if (!SecondLastInst.getOperand(1).isMBB() ||
599 !LastInst.getOperand(0).isMBB())
600 return true;
601 TBB = SecondLastInst.getOperand(1).getMBB();
602 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
603 Cond.push_back(SecondLastInst.getOperand(0));
604 FBB = LastInst.getOperand(0).getMBB();
605 return false;
606 } else if (SecondLastInst.getOpcode() == PPC::BCn &&
607 LastInst.getOpcode() == PPC::B) {
608 if (!SecondLastInst.getOperand(1).isMBB() ||
609 !LastInst.getOperand(0).isMBB())
610 return true;
611 TBB = SecondLastInst.getOperand(1).getMBB();
612 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
613 Cond.push_back(SecondLastInst.getOperand(0));
614 FBB = LastInst.getOperand(0).getMBB();
615 return false;
616 } else if ((SecondLastInst.getOpcode() == PPC::BDNZ8 ||
617 SecondLastInst.getOpcode() == PPC::BDNZ) &&
618 LastInst.getOpcode() == PPC::B) {
619 if (!SecondLastInst.getOperand(0).isMBB() ||
620 !LastInst.getOperand(0).isMBB())
621 return true;
622 if (DisableCTRLoopAnal)
623 return true;
624 TBB = SecondLastInst.getOperand(0).getMBB();
625 Cond.push_back(MachineOperand::CreateImm(1));
626 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
627 true));
628 FBB = LastInst.getOperand(0).getMBB();
629 return false;
630 } else if ((SecondLastInst.getOpcode() == PPC::BDZ8 ||
631 SecondLastInst.getOpcode() == PPC::BDZ) &&
632 LastInst.getOpcode() == PPC::B) {
633 if (!SecondLastInst.getOperand(0).isMBB() ||
634 !LastInst.getOperand(0).isMBB())
635 return true;
636 if (DisableCTRLoopAnal)
637 return true;
638 TBB = SecondLastInst.getOperand(0).getMBB();
639 Cond.push_back(MachineOperand::CreateImm(0));
640 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
641 true));
642 FBB = LastInst.getOperand(0).getMBB();
643 return false;
646 // If the block ends with two PPC:Bs, handle it. The second one is not
647 // executed, so remove it.
648 if (SecondLastInst.getOpcode() == PPC::B && LastInst.getOpcode() == PPC::B) {
649 if (!SecondLastInst.getOperand(0).isMBB())
650 return true;
651 TBB = SecondLastInst.getOperand(0).getMBB();
652 I = LastInst;
653 if (AllowModify)
654 I->eraseFromParent();
655 return false;
658 // Otherwise, can't handle this.
659 return true;
662 unsigned PPCInstrInfo::removeBranch(MachineBasicBlock &MBB,
663 int *BytesRemoved) const {
664 assert(!BytesRemoved && "code size not handled");
666 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
667 if (I == MBB.end())
668 return 0;
670 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
671 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
672 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
673 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
674 return 0;
676 // Remove the branch.
677 I->eraseFromParent();
679 I = MBB.end();
681 if (I == MBB.begin()) return 1;
682 --I;
683 if (I->getOpcode() != PPC::BCC &&
684 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
685 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
686 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
687 return 1;
689 // Remove the branch.
690 I->eraseFromParent();
691 return 2;
694 unsigned PPCInstrInfo::insertBranch(MachineBasicBlock &MBB,
695 MachineBasicBlock *TBB,
696 MachineBasicBlock *FBB,
697 ArrayRef<MachineOperand> Cond,
698 const DebugLoc &DL,
699 int *BytesAdded) const {
700 // Shouldn't be a fall through.
701 assert(TBB && "insertBranch must not be told to insert a fallthrough");
702 assert((Cond.size() == 2 || Cond.size() == 0) &&
703 "PPC branch conditions have two components!");
704 assert(!BytesAdded && "code size not handled");
706 bool isPPC64 = Subtarget.isPPC64();
708 // One-way branch.
709 if (!FBB) {
710 if (Cond.empty()) // Unconditional branch
711 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
712 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
713 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
714 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
715 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
716 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
717 BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB);
718 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
719 BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB);
720 else // Conditional branch
721 BuildMI(&MBB, DL, get(PPC::BCC))
722 .addImm(Cond[0].getImm())
723 .add(Cond[1])
724 .addMBB(TBB);
725 return 1;
728 // Two-way Conditional Branch.
729 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
730 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
731 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
732 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
733 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
734 BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB);
735 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
736 BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB);
737 else
738 BuildMI(&MBB, DL, get(PPC::BCC))
739 .addImm(Cond[0].getImm())
740 .add(Cond[1])
741 .addMBB(TBB);
742 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
743 return 2;
746 // Select analysis.
747 bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
748 ArrayRef<MachineOperand> Cond,
749 unsigned TrueReg, unsigned FalseReg,
750 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
751 if (Cond.size() != 2)
752 return false;
754 // If this is really a bdnz-like condition, then it cannot be turned into a
755 // select.
756 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
757 return false;
759 // Check register classes.
760 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
761 const TargetRegisterClass *RC =
762 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
763 if (!RC)
764 return false;
766 // isel is for regular integer GPRs only.
767 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
768 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
769 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
770 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
771 return false;
773 // FIXME: These numbers are for the A2, how well they work for other cores is
774 // an open question. On the A2, the isel instruction has a 2-cycle latency
775 // but single-cycle throughput. These numbers are used in combination with
776 // the MispredictPenalty setting from the active SchedMachineModel.
777 CondCycles = 1;
778 TrueCycles = 1;
779 FalseCycles = 1;
781 return true;
784 void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
785 MachineBasicBlock::iterator MI,
786 const DebugLoc &dl, unsigned DestReg,
787 ArrayRef<MachineOperand> Cond, unsigned TrueReg,
788 unsigned FalseReg) const {
789 assert(Cond.size() == 2 &&
790 "PPC branch conditions have two components!");
792 // Get the register classes.
793 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
794 const TargetRegisterClass *RC =
795 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
796 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
798 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
799 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
800 assert((Is64Bit ||
801 PPC::GPRCRegClass.hasSubClassEq(RC) ||
802 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
803 "isel is for regular integer GPRs only");
805 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
806 auto SelectPred = static_cast<PPC::Predicate>(Cond[0].getImm());
808 unsigned SubIdx = 0;
809 bool SwapOps = false;
810 switch (SelectPred) {
811 case PPC::PRED_EQ:
812 case PPC::PRED_EQ_MINUS:
813 case PPC::PRED_EQ_PLUS:
814 SubIdx = PPC::sub_eq; SwapOps = false; break;
815 case PPC::PRED_NE:
816 case PPC::PRED_NE_MINUS:
817 case PPC::PRED_NE_PLUS:
818 SubIdx = PPC::sub_eq; SwapOps = true; break;
819 case PPC::PRED_LT:
820 case PPC::PRED_LT_MINUS:
821 case PPC::PRED_LT_PLUS:
822 SubIdx = PPC::sub_lt; SwapOps = false; break;
823 case PPC::PRED_GE:
824 case PPC::PRED_GE_MINUS:
825 case PPC::PRED_GE_PLUS:
826 SubIdx = PPC::sub_lt; SwapOps = true; break;
827 case PPC::PRED_GT:
828 case PPC::PRED_GT_MINUS:
829 case PPC::PRED_GT_PLUS:
830 SubIdx = PPC::sub_gt; SwapOps = false; break;
831 case PPC::PRED_LE:
832 case PPC::PRED_LE_MINUS:
833 case PPC::PRED_LE_PLUS:
834 SubIdx = PPC::sub_gt; SwapOps = true; break;
835 case PPC::PRED_UN:
836 case PPC::PRED_UN_MINUS:
837 case PPC::PRED_UN_PLUS:
838 SubIdx = PPC::sub_un; SwapOps = false; break;
839 case PPC::PRED_NU:
840 case PPC::PRED_NU_MINUS:
841 case PPC::PRED_NU_PLUS:
842 SubIdx = PPC::sub_un; SwapOps = true; break;
843 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break;
844 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
847 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
848 SecondReg = SwapOps ? TrueReg : FalseReg;
850 // The first input register of isel cannot be r0. If it is a member
851 // of a register class that can be r0, then copy it first (the
852 // register allocator should eliminate the copy).
853 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
854 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
855 const TargetRegisterClass *FirstRC =
856 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
857 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
858 unsigned OldFirstReg = FirstReg;
859 FirstReg = MRI.createVirtualRegister(FirstRC);
860 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
861 .addReg(OldFirstReg);
864 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
865 .addReg(FirstReg).addReg(SecondReg)
866 .addReg(Cond[1].getReg(), 0, SubIdx);
869 static unsigned getCRBitValue(unsigned CRBit) {
870 unsigned Ret = 4;
871 if (CRBit == PPC::CR0LT || CRBit == PPC::CR1LT ||
872 CRBit == PPC::CR2LT || CRBit == PPC::CR3LT ||
873 CRBit == PPC::CR4LT || CRBit == PPC::CR5LT ||
874 CRBit == PPC::CR6LT || CRBit == PPC::CR7LT)
875 Ret = 3;
876 if (CRBit == PPC::CR0GT || CRBit == PPC::CR1GT ||
877 CRBit == PPC::CR2GT || CRBit == PPC::CR3GT ||
878 CRBit == PPC::CR4GT || CRBit == PPC::CR5GT ||
879 CRBit == PPC::CR6GT || CRBit == PPC::CR7GT)
880 Ret = 2;
881 if (CRBit == PPC::CR0EQ || CRBit == PPC::CR1EQ ||
882 CRBit == PPC::CR2EQ || CRBit == PPC::CR3EQ ||
883 CRBit == PPC::CR4EQ || CRBit == PPC::CR5EQ ||
884 CRBit == PPC::CR6EQ || CRBit == PPC::CR7EQ)
885 Ret = 1;
886 if (CRBit == PPC::CR0UN || CRBit == PPC::CR1UN ||
887 CRBit == PPC::CR2UN || CRBit == PPC::CR3UN ||
888 CRBit == PPC::CR4UN || CRBit == PPC::CR5UN ||
889 CRBit == PPC::CR6UN || CRBit == PPC::CR7UN)
890 Ret = 0;
892 assert(Ret != 4 && "Invalid CR bit register");
893 return Ret;
896 void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
897 MachineBasicBlock::iterator I,
898 const DebugLoc &DL, unsigned DestReg,
899 unsigned SrcReg, bool KillSrc) const {
900 // We can end up with self copies and similar things as a result of VSX copy
901 // legalization. Promote them here.
902 const TargetRegisterInfo *TRI = &getRegisterInfo();
903 if (PPC::F8RCRegClass.contains(DestReg) &&
904 PPC::VSRCRegClass.contains(SrcReg)) {
905 unsigned SuperReg =
906 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass);
908 if (VSXSelfCopyCrash && SrcReg == SuperReg)
909 llvm_unreachable("nop VSX copy");
911 DestReg = SuperReg;
912 } else if (PPC::F8RCRegClass.contains(SrcReg) &&
913 PPC::VSRCRegClass.contains(DestReg)) {
914 unsigned SuperReg =
915 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass);
917 if (VSXSelfCopyCrash && DestReg == SuperReg)
918 llvm_unreachable("nop VSX copy");
920 SrcReg = SuperReg;
923 // Different class register copy
924 if (PPC::CRBITRCRegClass.contains(SrcReg) &&
925 PPC::GPRCRegClass.contains(DestReg)) {
926 unsigned CRReg = getCRFromCRBit(SrcReg);
927 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(CRReg);
928 getKillRegState(KillSrc);
929 // Rotate the CR bit in the CR fields to be the least significant bit and
930 // then mask with 0x1 (MB = ME = 31).
931 BuildMI(MBB, I, DL, get(PPC::RLWINM), DestReg)
932 .addReg(DestReg, RegState::Kill)
933 .addImm(TRI->getEncodingValue(CRReg) * 4 + (4 - getCRBitValue(SrcReg)))
934 .addImm(31)
935 .addImm(31);
936 return;
937 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
938 PPC::G8RCRegClass.contains(DestReg)) {
939 BuildMI(MBB, I, DL, get(PPC::MFOCRF8), DestReg).addReg(SrcReg);
940 getKillRegState(KillSrc);
941 return;
942 } else if (PPC::CRRCRegClass.contains(SrcReg) &&
943 PPC::GPRCRegClass.contains(DestReg)) {
944 BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(SrcReg);
945 getKillRegState(KillSrc);
946 return;
947 } else if (PPC::G8RCRegClass.contains(SrcReg) &&
948 PPC::VSFRCRegClass.contains(DestReg)) {
949 BuildMI(MBB, I, DL, get(PPC::MTVSRD), DestReg).addReg(SrcReg);
950 NumGPRtoVSRSpill++;
951 getKillRegState(KillSrc);
952 return;
953 } else if (PPC::VSFRCRegClass.contains(SrcReg) &&
954 PPC::G8RCRegClass.contains(DestReg)) {
955 BuildMI(MBB, I, DL, get(PPC::MFVSRD), DestReg).addReg(SrcReg);
956 getKillRegState(KillSrc);
957 return;
960 unsigned Opc;
961 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
962 Opc = PPC::OR;
963 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
964 Opc = PPC::OR8;
965 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
966 Opc = PPC::FMR;
967 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
968 Opc = PPC::MCRF;
969 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
970 Opc = PPC::VOR;
971 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg))
972 // There are two different ways this can be done:
973 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
974 // issue in VSU pipeline 0.
975 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
976 // can go to either pipeline.
977 // We'll always use xxlor here, because in practically all cases where
978 // copies are generated, they are close enough to some use that the
979 // lower-latency form is preferable.
980 Opc = PPC::XXLOR;
981 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg) ||
982 PPC::VSSRCRegClass.contains(DestReg, SrcReg))
983 Opc = PPC::XXLORf;
984 else if (PPC::QFRCRegClass.contains(DestReg, SrcReg))
985 Opc = PPC::QVFMR;
986 else if (PPC::QSRCRegClass.contains(DestReg, SrcReg))
987 Opc = PPC::QVFMRs;
988 else if (PPC::QBRCRegClass.contains(DestReg, SrcReg))
989 Opc = PPC::QVFMRb;
990 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
991 Opc = PPC::CROR;
992 else
993 llvm_unreachable("Impossible reg-to-reg copy");
995 const MCInstrDesc &MCID = get(Opc);
996 if (MCID.getNumOperands() == 3)
997 BuildMI(MBB, I, DL, MCID, DestReg)
998 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
999 else
1000 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
1003 // This function returns true if a CR spill is necessary and false otherwise.
1004 bool
1005 PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
1006 unsigned SrcReg, bool isKill,
1007 int FrameIdx,
1008 const TargetRegisterClass *RC,
1009 SmallVectorImpl<MachineInstr*> &NewMIs,
1010 bool &NonRI, bool &SpillsVRS) const{
1011 // Note: If additional store instructions are added here,
1012 // update isStoreToStackSlot.
1014 DebugLoc DL;
1015 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
1016 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
1017 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
1018 .addReg(SrcReg,
1019 getKillRegState(isKill)),
1020 FrameIdx));
1021 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
1022 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
1023 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
1024 .addReg(SrcReg,
1025 getKillRegState(isKill)),
1026 FrameIdx));
1027 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
1028 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
1029 .addReg(SrcReg,
1030 getKillRegState(isKill)),
1031 FrameIdx));
1032 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
1033 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
1034 .addReg(SrcReg,
1035 getKillRegState(isKill)),
1036 FrameIdx));
1037 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
1038 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
1039 .addReg(SrcReg,
1040 getKillRegState(isKill)),
1041 FrameIdx));
1042 return true;
1043 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
1044 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT))
1045 .addReg(SrcReg,
1046 getKillRegState(isKill)),
1047 FrameIdx));
1048 return true;
1049 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
1050 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
1051 .addReg(SrcReg,
1052 getKillRegState(isKill)),
1053 FrameIdx));
1054 NonRI = true;
1055 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
1056 unsigned Op = Subtarget.hasP9Vector() ? PPC::STXV : PPC::STXVD2X;
1057 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op))
1058 .addReg(SrcReg,
1059 getKillRegState(isKill)),
1060 FrameIdx));
1061 NonRI = true;
1062 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
1063 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFSTOREf64 : PPC::STXSDX;
1064 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc))
1065 .addReg(SrcReg,
1066 getKillRegState(isKill)),
1067 FrameIdx));
1068 NonRI = true;
1069 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
1070 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFSTOREf32 : PPC::STXSSPX;
1071 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc))
1072 .addReg(SrcReg,
1073 getKillRegState(isKill)),
1074 FrameIdx));
1075 NonRI = true;
1076 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
1077 assert(Subtarget.isDarwin() &&
1078 "VRSAVE only needs spill/restore on Darwin");
1079 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
1080 .addReg(SrcReg,
1081 getKillRegState(isKill)),
1082 FrameIdx));
1083 SpillsVRS = true;
1084 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
1085 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDX))
1086 .addReg(SrcReg,
1087 getKillRegState(isKill)),
1088 FrameIdx));
1089 NonRI = true;
1090 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
1091 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFSXs))
1092 .addReg(SrcReg,
1093 getKillRegState(isKill)),
1094 FrameIdx));
1095 NonRI = true;
1096 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
1097 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVSTFDXb))
1098 .addReg(SrcReg,
1099 getKillRegState(isKill)),
1100 FrameIdx));
1101 NonRI = true;
1102 } else if (PPC::SPILLTOVSRRCRegClass.hasSubClassEq(RC)) {
1103 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILLTOVSR_ST))
1104 .addReg(SrcReg,
1105 getKillRegState(isKill)),
1106 FrameIdx));
1107 } else {
1108 llvm_unreachable("Unknown regclass!");
1111 return false;
1114 void
1115 PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
1116 MachineBasicBlock::iterator MI,
1117 unsigned SrcReg, bool isKill, int FrameIdx,
1118 const TargetRegisterClass *RC,
1119 const TargetRegisterInfo *TRI) const {
1120 MachineFunction &MF = *MBB.getParent();
1121 SmallVector<MachineInstr*, 4> NewMIs;
1123 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1124 FuncInfo->setHasSpills();
1126 // We need to avoid a situation in which the value from a VRRC register is
1127 // spilled using an Altivec instruction and reloaded into a VSRC register
1128 // using a VSX instruction. The issue with this is that the VSX
1129 // load/store instructions swap the doublewords in the vector and the Altivec
1130 // ones don't. The register classes on the spill/reload may be different if
1131 // the register is defined using an Altivec instruction and is then used by a
1132 // VSX instruction.
1133 RC = updatedRC(RC);
1135 bool NonRI = false, SpillsVRS = false;
1136 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
1137 NonRI, SpillsVRS))
1138 FuncInfo->setSpillsCR();
1140 if (SpillsVRS)
1141 FuncInfo->setSpillsVRSAVE();
1143 if (NonRI)
1144 FuncInfo->setHasNonRISpills();
1146 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1147 MBB.insert(MI, NewMIs[i]);
1149 const MachineFrameInfo &MFI = MF.getFrameInfo();
1150 MachineMemOperand *MMO = MF.getMachineMemOperand(
1151 MachinePointerInfo::getFixedStack(MF, FrameIdx),
1152 MachineMemOperand::MOStore, MFI.getObjectSize(FrameIdx),
1153 MFI.getObjectAlignment(FrameIdx));
1154 NewMIs.back()->addMemOperand(MF, MMO);
1157 bool PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, const DebugLoc &DL,
1158 unsigned DestReg, int FrameIdx,
1159 const TargetRegisterClass *RC,
1160 SmallVectorImpl<MachineInstr *> &NewMIs,
1161 bool &NonRI, bool &SpillsVRS) const {
1162 // Note: If additional load instructions are added here,
1163 // update isLoadFromStackSlot.
1165 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
1166 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
1167 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
1168 DestReg), FrameIdx));
1169 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
1170 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
1171 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
1172 FrameIdx));
1173 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
1174 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
1175 FrameIdx));
1176 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
1177 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
1178 FrameIdx));
1179 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
1180 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1181 get(PPC::RESTORE_CR), DestReg),
1182 FrameIdx));
1183 return true;
1184 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
1185 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1186 get(PPC::RESTORE_CRBIT), DestReg),
1187 FrameIdx));
1188 return true;
1189 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
1190 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
1191 FrameIdx));
1192 NonRI = true;
1193 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
1194 unsigned Op = Subtarget.hasP9Vector() ? PPC::LXV : PPC::LXVD2X;
1195 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Op), DestReg),
1196 FrameIdx));
1197 NonRI = true;
1198 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
1199 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFLOADf64 : PPC::LXSDX;
1200 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc),
1201 DestReg), FrameIdx));
1202 NonRI = true;
1203 } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
1204 unsigned Opc = Subtarget.hasP9Vector() ? PPC::DFLOADf32 : PPC::LXSSPX;
1205 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opc),
1206 DestReg), FrameIdx));
1207 NonRI = true;
1208 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
1209 assert(Subtarget.isDarwin() &&
1210 "VRSAVE only needs spill/restore on Darwin");
1211 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
1212 get(PPC::RESTORE_VRSAVE),
1213 DestReg),
1214 FrameIdx));
1215 SpillsVRS = true;
1216 } else if (PPC::QFRCRegClass.hasSubClassEq(RC)) {
1217 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDX), DestReg),
1218 FrameIdx));
1219 NonRI = true;
1220 } else if (PPC::QSRCRegClass.hasSubClassEq(RC)) {
1221 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFSXs), DestReg),
1222 FrameIdx));
1223 NonRI = true;
1224 } else if (PPC::QBRCRegClass.hasSubClassEq(RC)) {
1225 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::QVLFDXb), DestReg),
1226 FrameIdx));
1227 NonRI = true;
1228 } else if (PPC::SPILLTOVSRRCRegClass.hasSubClassEq(RC)) {
1229 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILLTOVSR_LD),
1230 DestReg), FrameIdx));
1231 } else {
1232 llvm_unreachable("Unknown regclass!");
1235 return false;
1238 void
1239 PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
1240 MachineBasicBlock::iterator MI,
1241 unsigned DestReg, int FrameIdx,
1242 const TargetRegisterClass *RC,
1243 const TargetRegisterInfo *TRI) const {
1244 MachineFunction &MF = *MBB.getParent();
1245 SmallVector<MachineInstr*, 4> NewMIs;
1246 DebugLoc DL;
1247 if (MI != MBB.end()) DL = MI->getDebugLoc();
1249 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1250 FuncInfo->setHasSpills();
1252 // We need to avoid a situation in which the value from a VRRC register is
1253 // spilled using an Altivec instruction and reloaded into a VSRC register
1254 // using a VSX instruction. The issue with this is that the VSX
1255 // load/store instructions swap the doublewords in the vector and the Altivec
1256 // ones don't. The register classes on the spill/reload may be different if
1257 // the register is defined using an Altivec instruction and is then used by a
1258 // VSX instruction.
1259 if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass)
1260 RC = &PPC::VSRCRegClass;
1262 bool NonRI = false, SpillsVRS = false;
1263 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
1264 NonRI, SpillsVRS))
1265 FuncInfo->setSpillsCR();
1267 if (SpillsVRS)
1268 FuncInfo->setSpillsVRSAVE();
1270 if (NonRI)
1271 FuncInfo->setHasNonRISpills();
1273 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1274 MBB.insert(MI, NewMIs[i]);
1276 const MachineFrameInfo &MFI = MF.getFrameInfo();
1277 MachineMemOperand *MMO = MF.getMachineMemOperand(
1278 MachinePointerInfo::getFixedStack(MF, FrameIdx),
1279 MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIdx),
1280 MFI.getObjectAlignment(FrameIdx));
1281 NewMIs.back()->addMemOperand(MF, MMO);
1284 bool PPCInstrInfo::
1285 reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
1286 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
1287 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
1288 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
1289 else
1290 // Leave the CR# the same, but invert the condition.
1291 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
1292 return false;
1295 bool PPCInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
1296 unsigned Reg, MachineRegisterInfo *MRI) const {
1297 // For some instructions, it is legal to fold ZERO into the RA register field.
1298 // A zero immediate should always be loaded with a single li.
1299 unsigned DefOpc = DefMI.getOpcode();
1300 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
1301 return false;
1302 if (!DefMI.getOperand(1).isImm())
1303 return false;
1304 if (DefMI.getOperand(1).getImm() != 0)
1305 return false;
1307 // Note that we cannot here invert the arguments of an isel in order to fold
1308 // a ZERO into what is presented as the second argument. All we have here
1309 // is the condition bit, and that might come from a CR-logical bit operation.
1311 const MCInstrDesc &UseMCID = UseMI.getDesc();
1313 // Only fold into real machine instructions.
1314 if (UseMCID.isPseudo())
1315 return false;
1317 unsigned UseIdx;
1318 for (UseIdx = 0; UseIdx < UseMI.getNumOperands(); ++UseIdx)
1319 if (UseMI.getOperand(UseIdx).isReg() &&
1320 UseMI.getOperand(UseIdx).getReg() == Reg)
1321 break;
1323 assert(UseIdx < UseMI.getNumOperands() && "Cannot find Reg in UseMI");
1324 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
1326 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
1328 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
1329 // register (which might also be specified as a pointer class kind).
1330 if (UseInfo->isLookupPtrRegClass()) {
1331 if (UseInfo->RegClass /* Kind */ != 1)
1332 return false;
1333 } else {
1334 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
1335 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
1336 return false;
1339 // Make sure this is not tied to an output register (or otherwise
1340 // constrained). This is true for ST?UX registers, for example, which
1341 // are tied to their output registers.
1342 if (UseInfo->Constraints != 0)
1343 return false;
1345 unsigned ZeroReg;
1346 if (UseInfo->isLookupPtrRegClass()) {
1347 bool isPPC64 = Subtarget.isPPC64();
1348 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
1349 } else {
1350 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
1351 PPC::ZERO8 : PPC::ZERO;
1354 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
1355 UseMI.getOperand(UseIdx).setReg(ZeroReg);
1357 if (DeleteDef)
1358 DefMI.eraseFromParent();
1360 return true;
1363 static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
1364 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1365 I != IE; ++I)
1366 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
1367 return true;
1368 return false;
1371 // We should make sure that, if we're going to predicate both sides of a
1372 // condition (a diamond), that both sides don't define the counter register. We
1373 // can predicate counter-decrement-based branches, but while that predicates
1374 // the branching, it does not predicate the counter decrement. If we tried to
1375 // merge the triangle into one predicated block, we'd decrement the counter
1376 // twice.
1377 bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
1378 unsigned NumT, unsigned ExtraT,
1379 MachineBasicBlock &FMBB,
1380 unsigned NumF, unsigned ExtraF,
1381 BranchProbability Probability) const {
1382 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
1386 bool PPCInstrInfo::isPredicated(const MachineInstr &MI) const {
1387 // The predicated branches are identified by their type, not really by the
1388 // explicit presence of a predicate. Furthermore, some of them can be
1389 // predicated more than once. Because if conversion won't try to predicate
1390 // any instruction which already claims to be predicated (by returning true
1391 // here), always return false. In doing so, we let isPredicable() be the
1392 // final word on whether not the instruction can be (further) predicated.
1394 return false;
1397 bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const {
1398 if (!MI.isTerminator())
1399 return false;
1401 // Conditional branch is a special case.
1402 if (MI.isBranch() && !MI.isBarrier())
1403 return true;
1405 return !isPredicated(MI);
1408 bool PPCInstrInfo::PredicateInstruction(MachineInstr &MI,
1409 ArrayRef<MachineOperand> Pred) const {
1410 unsigned OpC = MI.getOpcode();
1411 if (OpC == PPC::BLR || OpC == PPC::BLR8) {
1412 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
1413 bool isPPC64 = Subtarget.isPPC64();
1414 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR)
1415 : (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
1416 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1417 MI.setDesc(get(PPC::BCLR));
1418 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1419 .addReg(Pred[1].getReg());
1420 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1421 MI.setDesc(get(PPC::BCLRn));
1422 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1423 .addReg(Pred[1].getReg());
1424 } else {
1425 MI.setDesc(get(PPC::BCCLR));
1426 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1427 .addImm(Pred[0].getImm())
1428 .addReg(Pred[1].getReg());
1431 return true;
1432 } else if (OpC == PPC::B) {
1433 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
1434 bool isPPC64 = Subtarget.isPPC64();
1435 MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ)
1436 : (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
1437 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1438 MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1439 MI.RemoveOperand(0);
1441 MI.setDesc(get(PPC::BC));
1442 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1443 .addReg(Pred[1].getReg())
1444 .addMBB(MBB);
1445 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1446 MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1447 MI.RemoveOperand(0);
1449 MI.setDesc(get(PPC::BCn));
1450 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1451 .addReg(Pred[1].getReg())
1452 .addMBB(MBB);
1453 } else {
1454 MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1455 MI.RemoveOperand(0);
1457 MI.setDesc(get(PPC::BCC));
1458 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1459 .addImm(Pred[0].getImm())
1460 .addReg(Pred[1].getReg())
1461 .addMBB(MBB);
1464 return true;
1465 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1466 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1467 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1468 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1470 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
1471 bool isPPC64 = Subtarget.isPPC64();
1473 if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1474 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8)
1475 : (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1476 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1477 .addReg(Pred[1].getReg());
1478 return true;
1479 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1480 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n)
1481 : (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1482 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1483 .addReg(Pred[1].getReg());
1484 return true;
1487 MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8)
1488 : (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
1489 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1490 .addImm(Pred[0].getImm())
1491 .addReg(Pred[1].getReg());
1492 return true;
1495 return false;
1498 bool PPCInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1499 ArrayRef<MachineOperand> Pred2) const {
1500 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1501 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1503 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1504 return false;
1505 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1506 return false;
1508 // P1 can only subsume P2 if they test the same condition register.
1509 if (Pred1[1].getReg() != Pred2[1].getReg())
1510 return false;
1512 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1513 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1515 if (P1 == P2)
1516 return true;
1518 // Does P1 subsume P2, e.g. GE subsumes GT.
1519 if (P1 == PPC::PRED_LE &&
1520 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1521 return true;
1522 if (P1 == PPC::PRED_GE &&
1523 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1524 return true;
1526 return false;
1529 bool PPCInstrInfo::DefinesPredicate(MachineInstr &MI,
1530 std::vector<MachineOperand> &Pred) const {
1531 // Note: At the present time, the contents of Pred from this function is
1532 // unused by IfConversion. This implementation follows ARM by pushing the
1533 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1534 // predicate, instructions defining CTR or CTR8 are also included as
1535 // predicate-defining instructions.
1537 const TargetRegisterClass *RCs[] =
1538 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1539 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1541 bool Found = false;
1542 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1543 const MachineOperand &MO = MI.getOperand(i);
1544 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
1545 const TargetRegisterClass *RC = RCs[c];
1546 if (MO.isReg()) {
1547 if (MO.isDef() && RC->contains(MO.getReg())) {
1548 Pred.push_back(MO);
1549 Found = true;
1551 } else if (MO.isRegMask()) {
1552 for (TargetRegisterClass::iterator I = RC->begin(),
1553 IE = RC->end(); I != IE; ++I)
1554 if (MO.clobbersPhysReg(*I)) {
1555 Pred.push_back(MO);
1556 Found = true;
1562 return Found;
1565 bool PPCInstrInfo::isPredicable(const MachineInstr &MI) const {
1566 unsigned OpC = MI.getOpcode();
1567 switch (OpC) {
1568 default:
1569 return false;
1570 case PPC::B:
1571 case PPC::BLR:
1572 case PPC::BLR8:
1573 case PPC::BCTR:
1574 case PPC::BCTR8:
1575 case PPC::BCTRL:
1576 case PPC::BCTRL8:
1577 return true;
1581 bool PPCInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1582 unsigned &SrcReg2, int &Mask,
1583 int &Value) const {
1584 unsigned Opc = MI.getOpcode();
1586 switch (Opc) {
1587 default: return false;
1588 case PPC::CMPWI:
1589 case PPC::CMPLWI:
1590 case PPC::CMPDI:
1591 case PPC::CMPLDI:
1592 SrcReg = MI.getOperand(1).getReg();
1593 SrcReg2 = 0;
1594 Value = MI.getOperand(2).getImm();
1595 Mask = 0xFFFF;
1596 return true;
1597 case PPC::CMPW:
1598 case PPC::CMPLW:
1599 case PPC::CMPD:
1600 case PPC::CMPLD:
1601 case PPC::FCMPUS:
1602 case PPC::FCMPUD:
1603 SrcReg = MI.getOperand(1).getReg();
1604 SrcReg2 = MI.getOperand(2).getReg();
1605 Value = 0;
1606 Mask = 0;
1607 return true;
1611 bool PPCInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, unsigned SrcReg,
1612 unsigned SrcReg2, int Mask, int Value,
1613 const MachineRegisterInfo *MRI) const {
1614 if (DisableCmpOpt)
1615 return false;
1617 int OpC = CmpInstr.getOpcode();
1618 unsigned CRReg = CmpInstr.getOperand(0).getReg();
1620 // FP record forms set CR1 based on the execption status bits, not a
1621 // comparison with zero.
1622 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1623 return false;
1625 // The record forms set the condition register based on a signed comparison
1626 // with zero (so says the ISA manual). This is not as straightforward as it
1627 // seems, however, because this is always a 64-bit comparison on PPC64, even
1628 // for instructions that are 32-bit in nature (like slw for example).
1629 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1630 // for equality checks (as those don't depend on the sign). On PPC64,
1631 // we are restricted to equality for unsigned 64-bit comparisons and for
1632 // signed 32-bit comparisons the applicability is more restricted.
1633 bool isPPC64 = Subtarget.isPPC64();
1634 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1635 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1636 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1638 // Get the unique definition of SrcReg.
1639 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1640 if (!MI) return false;
1642 bool equalityOnly = false;
1643 bool noSub = false;
1644 if (isPPC64) {
1645 if (is32BitSignedCompare) {
1646 // We can perform this optimization only if MI is sign-extending.
1647 if (isSignExtended(*MI))
1648 noSub = true;
1649 else
1650 return false;
1651 } else if (is32BitUnsignedCompare) {
1652 // We can perform this optimization, equality only, if MI is
1653 // zero-extending.
1654 if (isZeroExtended(*MI)) {
1655 noSub = true;
1656 equalityOnly = true;
1657 } else
1658 return false;
1659 } else
1660 equalityOnly = is64BitUnsignedCompare;
1661 } else
1662 equalityOnly = is32BitUnsignedCompare;
1664 if (equalityOnly) {
1665 // We need to check the uses of the condition register in order to reject
1666 // non-equality comparisons.
1667 for (MachineRegisterInfo::use_instr_iterator
1668 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1669 I != IE; ++I) {
1670 MachineInstr *UseMI = &*I;
1671 if (UseMI->getOpcode() == PPC::BCC) {
1672 PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm();
1673 unsigned PredCond = PPC::getPredicateCondition(Pred);
1674 // We ignore hint bits when checking for non-equality comparisons.
1675 if (PredCond != PPC::PRED_EQ && PredCond != PPC::PRED_NE)
1676 return false;
1677 } else if (UseMI->getOpcode() == PPC::ISEL ||
1678 UseMI->getOpcode() == PPC::ISEL8) {
1679 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
1680 if (SubIdx != PPC::sub_eq)
1681 return false;
1682 } else
1683 return false;
1687 MachineBasicBlock::iterator I = CmpInstr;
1689 // Scan forward to find the first use of the compare.
1690 for (MachineBasicBlock::iterator EL = CmpInstr.getParent()->end(); I != EL;
1691 ++I) {
1692 bool FoundUse = false;
1693 for (MachineRegisterInfo::use_instr_iterator
1694 J = MRI->use_instr_begin(CRReg), JE = MRI->use_instr_end();
1695 J != JE; ++J)
1696 if (&*J == &*I) {
1697 FoundUse = true;
1698 break;
1701 if (FoundUse)
1702 break;
1705 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1706 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
1708 // There are two possible candidates which can be changed to set CR[01].
1709 // One is MI, the other is a SUB instruction.
1710 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
1711 MachineInstr *Sub = nullptr;
1712 if (SrcReg2 != 0)
1713 // MI is not a candidate for CMPrr.
1714 MI = nullptr;
1715 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1716 // same BB as the comparison. This is to allow the check below to avoid calls
1717 // (and other explicit clobbers); instead we should really check for these
1718 // more explicitly (in at least a few predecessors).
1719 else if (MI->getParent() != CmpInstr.getParent())
1720 return false;
1721 else if (Value != 0) {
1722 // The record-form instructions set CR bit based on signed comparison
1723 // against 0. We try to convert a compare against 1 or -1 into a compare
1724 // against 0 to exploit record-form instructions. For example, we change
1725 // the condition "greater than -1" into "greater than or equal to 0"
1726 // and "less than 1" into "less than or equal to 0".
1728 // Since we optimize comparison based on a specific branch condition,
1729 // we don't optimize if condition code is used by more than once.
1730 if (equalityOnly || !MRI->hasOneUse(CRReg))
1731 return false;
1733 MachineInstr *UseMI = &*MRI->use_instr_begin(CRReg);
1734 if (UseMI->getOpcode() != PPC::BCC)
1735 return false;
1737 PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm();
1738 PPC::Predicate NewPred = Pred;
1739 unsigned PredCond = PPC::getPredicateCondition(Pred);
1740 unsigned PredHint = PPC::getPredicateHint(Pred);
1741 int16_t Immed = (int16_t)Value;
1743 // When modyfing the condition in the predicate, we propagate hint bits
1744 // from the original predicate to the new one.
1745 if (Immed == -1 && PredCond == PPC::PRED_GT)
1746 // We convert "greater than -1" into "greater than or equal to 0",
1747 // since we are assuming signed comparison by !equalityOnly
1748 NewPred = PPC::getPredicate(PPC::PRED_GE, PredHint);
1749 else if (Immed == -1 && PredCond == PPC::PRED_LE)
1750 // We convert "less than or equal to -1" into "less than 0".
1751 NewPred = PPC::getPredicate(PPC::PRED_LT, PredHint);
1752 else if (Immed == 1 && PredCond == PPC::PRED_LT)
1753 // We convert "less than 1" into "less than or equal to 0".
1754 NewPred = PPC::getPredicate(PPC::PRED_LE, PredHint);
1755 else if (Immed == 1 && PredCond == PPC::PRED_GE)
1756 // We convert "greater than or equal to 1" into "greater than 0".
1757 NewPred = PPC::getPredicate(PPC::PRED_GT, PredHint);
1758 else
1759 return false;
1761 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
1762 NewPred));
1765 // Search for Sub.
1766 const TargetRegisterInfo *TRI = &getRegisterInfo();
1767 --I;
1769 // Get ready to iterate backward from CmpInstr.
1770 MachineBasicBlock::iterator E = MI, B = CmpInstr.getParent()->begin();
1772 for (; I != E && !noSub; --I) {
1773 const MachineInstr &Instr = *I;
1774 unsigned IOpC = Instr.getOpcode();
1776 if (&*I != &CmpInstr && (Instr.modifiesRegister(PPC::CR0, TRI) ||
1777 Instr.readsRegister(PPC::CR0, TRI)))
1778 // This instruction modifies or uses the record condition register after
1779 // the one we want to change. While we could do this transformation, it
1780 // would likely not be profitable. This transformation removes one
1781 // instruction, and so even forcing RA to generate one move probably
1782 // makes it unprofitable.
1783 return false;
1785 // Check whether CmpInstr can be made redundant by the current instruction.
1786 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1787 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1788 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1789 ((Instr.getOperand(1).getReg() == SrcReg &&
1790 Instr.getOperand(2).getReg() == SrcReg2) ||
1791 (Instr.getOperand(1).getReg() == SrcReg2 &&
1792 Instr.getOperand(2).getReg() == SrcReg))) {
1793 Sub = &*I;
1794 break;
1797 if (I == B)
1798 // The 'and' is below the comparison instruction.
1799 return false;
1802 // Return false if no candidates exist.
1803 if (!MI && !Sub)
1804 return false;
1806 // The single candidate is called MI.
1807 if (!MI) MI = Sub;
1809 int NewOpC = -1;
1810 int MIOpC = MI->getOpcode();
1811 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1812 NewOpC = MIOpC;
1813 else {
1814 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1815 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1816 NewOpC = MIOpC;
1819 // FIXME: On the non-embedded POWER architectures, only some of the record
1820 // forms are fast, and we should use only the fast ones.
1822 // The defining instruction has a record form (or is already a record
1823 // form). It is possible, however, that we'll need to reverse the condition
1824 // code of the users.
1825 if (NewOpC == -1)
1826 return false;
1828 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1829 // needs to be updated to be based on SUB. Push the condition code
1830 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1831 // condition code of these operands will be modified.
1832 // Here, Value == 0 means we haven't converted comparison against 1 or -1 to
1833 // comparison against 0, which may modify predicate.
1834 bool ShouldSwap = false;
1835 if (Sub && Value == 0) {
1836 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1837 Sub->getOperand(2).getReg() == SrcReg;
1839 // The operands to subf are the opposite of sub, so only in the fixed-point
1840 // case, invert the order.
1841 ShouldSwap = !ShouldSwap;
1844 if (ShouldSwap)
1845 for (MachineRegisterInfo::use_instr_iterator
1846 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1847 I != IE; ++I) {
1848 MachineInstr *UseMI = &*I;
1849 if (UseMI->getOpcode() == PPC::BCC) {
1850 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
1851 unsigned PredCond = PPC::getPredicateCondition(Pred);
1852 assert((!equalityOnly ||
1853 PredCond == PPC::PRED_EQ || PredCond == PPC::PRED_NE) &&
1854 "Invalid predicate for equality-only optimization");
1855 (void)PredCond; // To suppress warning in release build.
1856 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
1857 PPC::getSwappedPredicate(Pred)));
1858 } else if (UseMI->getOpcode() == PPC::ISEL ||
1859 UseMI->getOpcode() == PPC::ISEL8) {
1860 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1861 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1862 "Invalid CR bit for equality-only optimization");
1864 if (NewSubReg == PPC::sub_lt)
1865 NewSubReg = PPC::sub_gt;
1866 else if (NewSubReg == PPC::sub_gt)
1867 NewSubReg = PPC::sub_lt;
1869 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
1870 NewSubReg));
1871 } else // We need to abort on a user we don't understand.
1872 return false;
1874 assert(!(Value != 0 && ShouldSwap) &&
1875 "Non-zero immediate support and ShouldSwap"
1876 "may conflict in updating predicate");
1878 // Create a new virtual register to hold the value of the CR set by the
1879 // record-form instruction. If the instruction was not previously in
1880 // record form, then set the kill flag on the CR.
1881 CmpInstr.eraseFromParent();
1883 MachineBasicBlock::iterator MII = MI;
1884 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
1885 get(TargetOpcode::COPY), CRReg)
1886 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
1888 // Even if CR0 register were dead before, it is alive now since the
1889 // instruction we just built uses it.
1890 MI->clearRegisterDeads(PPC::CR0);
1892 if (MIOpC != NewOpC) {
1893 // We need to be careful here: we're replacing one instruction with
1894 // another, and we need to make sure that we get all of the right
1895 // implicit uses and defs. On the other hand, the caller may be holding
1896 // an iterator to this instruction, and so we can't delete it (this is
1897 // specifically the case if this is the instruction directly after the
1898 // compare).
1900 const MCInstrDesc &NewDesc = get(NewOpC);
1901 MI->setDesc(NewDesc);
1903 if (NewDesc.ImplicitDefs)
1904 for (const MCPhysReg *ImpDefs = NewDesc.getImplicitDefs();
1905 *ImpDefs; ++ImpDefs)
1906 if (!MI->definesRegister(*ImpDefs))
1907 MI->addOperand(*MI->getParent()->getParent(),
1908 MachineOperand::CreateReg(*ImpDefs, true, true));
1909 if (NewDesc.ImplicitUses)
1910 for (const MCPhysReg *ImpUses = NewDesc.getImplicitUses();
1911 *ImpUses; ++ImpUses)
1912 if (!MI->readsRegister(*ImpUses))
1913 MI->addOperand(*MI->getParent()->getParent(),
1914 MachineOperand::CreateReg(*ImpUses, false, true));
1916 assert(MI->definesRegister(PPC::CR0) &&
1917 "Record-form instruction does not define cr0?");
1919 // Modify the condition code of operands in OperandsToUpdate.
1920 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1921 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
1922 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1923 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
1925 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1926 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
1928 return true;
1931 /// GetInstSize - Return the number of bytes of code the specified
1932 /// instruction may be. This returns the maximum number of bytes.
1934 unsigned PPCInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
1935 unsigned Opcode = MI.getOpcode();
1937 if (Opcode == PPC::INLINEASM) {
1938 const MachineFunction *MF = MI.getParent()->getParent();
1939 const char *AsmStr = MI.getOperand(0).getSymbolName();
1940 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
1941 } else if (Opcode == TargetOpcode::STACKMAP) {
1942 StackMapOpers Opers(&MI);
1943 return Opers.getNumPatchBytes();
1944 } else if (Opcode == TargetOpcode::PATCHPOINT) {
1945 PatchPointOpers Opers(&MI);
1946 return Opers.getNumPatchBytes();
1947 } else {
1948 return get(Opcode).getSize();
1952 std::pair<unsigned, unsigned>
1953 PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
1954 const unsigned Mask = PPCII::MO_ACCESS_MASK;
1955 return std::make_pair(TF & Mask, TF & ~Mask);
1958 ArrayRef<std::pair<unsigned, const char *>>
1959 PPCInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
1960 using namespace PPCII;
1961 static const std::pair<unsigned, const char *> TargetFlags[] = {
1962 {MO_LO, "ppc-lo"},
1963 {MO_HA, "ppc-ha"},
1964 {MO_TPREL_LO, "ppc-tprel-lo"},
1965 {MO_TPREL_HA, "ppc-tprel-ha"},
1966 {MO_DTPREL_LO, "ppc-dtprel-lo"},
1967 {MO_TLSLD_LO, "ppc-tlsld-lo"},
1968 {MO_TOC_LO, "ppc-toc-lo"},
1969 {MO_TLS, "ppc-tls"}};
1970 return makeArrayRef(TargetFlags);
1973 ArrayRef<std::pair<unsigned, const char *>>
1974 PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
1975 using namespace PPCII;
1976 static const std::pair<unsigned, const char *> TargetFlags[] = {
1977 {MO_PLT, "ppc-plt"},
1978 {MO_PIC_FLAG, "ppc-pic"},
1979 {MO_NLP_FLAG, "ppc-nlp"},
1980 {MO_NLP_HIDDEN_FLAG, "ppc-nlp-hidden"}};
1981 return makeArrayRef(TargetFlags);
1984 // Expand VSX Memory Pseudo instruction to either a VSX or a FP instruction.
1985 // The VSX versions have the advantage of a full 64-register target whereas
1986 // the FP ones have the advantage of lower latency and higher throughput. So
1987 // what we are after is using the faster instructions in low register pressure
1988 // situations and using the larger register file in high register pressure
1989 // situations.
1990 bool PPCInstrInfo::expandVSXMemPseudo(MachineInstr &MI) const {
1991 unsigned UpperOpcode, LowerOpcode;
1992 switch (MI.getOpcode()) {
1993 case PPC::DFLOADf32:
1994 UpperOpcode = PPC::LXSSP;
1995 LowerOpcode = PPC::LFS;
1996 break;
1997 case PPC::DFLOADf64:
1998 UpperOpcode = PPC::LXSD;
1999 LowerOpcode = PPC::LFD;
2000 break;
2001 case PPC::DFSTOREf32:
2002 UpperOpcode = PPC::STXSSP;
2003 LowerOpcode = PPC::STFS;
2004 break;
2005 case PPC::DFSTOREf64:
2006 UpperOpcode = PPC::STXSD;
2007 LowerOpcode = PPC::STFD;
2008 break;
2009 case PPC::XFLOADf32:
2010 UpperOpcode = PPC::LXSSPX;
2011 LowerOpcode = PPC::LFSX;
2012 break;
2013 case PPC::XFLOADf64:
2014 UpperOpcode = PPC::LXSDX;
2015 LowerOpcode = PPC::LFDX;
2016 break;
2017 case PPC::XFSTOREf32:
2018 UpperOpcode = PPC::STXSSPX;
2019 LowerOpcode = PPC::STFSX;
2020 break;
2021 case PPC::XFSTOREf64:
2022 UpperOpcode = PPC::STXSDX;
2023 LowerOpcode = PPC::STFDX;
2024 break;
2025 case PPC::LIWAX:
2026 UpperOpcode = PPC::LXSIWAX;
2027 LowerOpcode = PPC::LFIWAX;
2028 break;
2029 case PPC::LIWZX:
2030 UpperOpcode = PPC::LXSIWZX;
2031 LowerOpcode = PPC::LFIWZX;
2032 break;
2033 case PPC::STIWX:
2034 UpperOpcode = PPC::STXSIWX;
2035 LowerOpcode = PPC::STFIWX;
2036 break;
2037 default:
2038 llvm_unreachable("Unknown Operation!");
2041 unsigned TargetReg = MI.getOperand(0).getReg();
2042 unsigned Opcode;
2043 if ((TargetReg >= PPC::F0 && TargetReg <= PPC::F31) ||
2044 (TargetReg >= PPC::VSL0 && TargetReg <= PPC::VSL31))
2045 Opcode = LowerOpcode;
2046 else
2047 Opcode = UpperOpcode;
2048 MI.setDesc(get(Opcode));
2049 return true;
2052 bool PPCInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
2053 auto &MBB = *MI.getParent();
2054 auto DL = MI.getDebugLoc();
2056 switch (MI.getOpcode()) {
2057 case TargetOpcode::LOAD_STACK_GUARD: {
2058 assert(Subtarget.isTargetLinux() &&
2059 "Only Linux target is expected to contain LOAD_STACK_GUARD");
2060 const int64_t Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008;
2061 const unsigned Reg = Subtarget.isPPC64() ? PPC::X13 : PPC::R2;
2062 MI.setDesc(get(Subtarget.isPPC64() ? PPC::LD : PPC::LWZ));
2063 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
2064 .addImm(Offset)
2065 .addReg(Reg);
2066 return true;
2068 case PPC::DFLOADf32:
2069 case PPC::DFLOADf64:
2070 case PPC::DFSTOREf32:
2071 case PPC::DFSTOREf64: {
2072 assert(Subtarget.hasP9Vector() &&
2073 "Invalid D-Form Pseudo-ops on Pre-P9 target.");
2074 assert(MI.getOperand(2).isReg() && MI.getOperand(1).isImm() &&
2075 "D-form op must have register and immediate operands");
2076 return expandVSXMemPseudo(MI);
2078 case PPC::XFLOADf32:
2079 case PPC::XFSTOREf32:
2080 case PPC::LIWAX:
2081 case PPC::LIWZX:
2082 case PPC::STIWX: {
2083 assert(Subtarget.hasP8Vector() &&
2084 "Invalid X-Form Pseudo-ops on Pre-P8 target.");
2085 assert(MI.getOperand(2).isReg() && MI.getOperand(1).isReg() &&
2086 "X-form op must have register and register operands");
2087 return expandVSXMemPseudo(MI);
2089 case PPC::XFLOADf64:
2090 case PPC::XFSTOREf64: {
2091 assert(Subtarget.hasVSX() &&
2092 "Invalid X-Form Pseudo-ops on target that has no VSX.");
2093 assert(MI.getOperand(2).isReg() && MI.getOperand(1).isReg() &&
2094 "X-form op must have register and register operands");
2095 return expandVSXMemPseudo(MI);
2097 case PPC::SPILLTOVSR_LD: {
2098 unsigned TargetReg = MI.getOperand(0).getReg();
2099 if (PPC::VSFRCRegClass.contains(TargetReg)) {
2100 MI.setDesc(get(PPC::DFLOADf64));
2101 return expandPostRAPseudo(MI);
2103 else
2104 MI.setDesc(get(PPC::LD));
2105 return true;
2107 case PPC::SPILLTOVSR_ST: {
2108 unsigned SrcReg = MI.getOperand(0).getReg();
2109 if (PPC::VSFRCRegClass.contains(SrcReg)) {
2110 NumStoreSPILLVSRRCAsVec++;
2111 MI.setDesc(get(PPC::DFSTOREf64));
2112 return expandPostRAPseudo(MI);
2113 } else {
2114 NumStoreSPILLVSRRCAsGpr++;
2115 MI.setDesc(get(PPC::STD));
2117 return true;
2119 case PPC::SPILLTOVSR_LDX: {
2120 unsigned TargetReg = MI.getOperand(0).getReg();
2121 if (PPC::VSFRCRegClass.contains(TargetReg))
2122 MI.setDesc(get(PPC::LXSDX));
2123 else
2124 MI.setDesc(get(PPC::LDX));
2125 return true;
2127 case PPC::SPILLTOVSR_STX: {
2128 unsigned SrcReg = MI.getOperand(0).getReg();
2129 if (PPC::VSFRCRegClass.contains(SrcReg)) {
2130 NumStoreSPILLVSRRCAsVec++;
2131 MI.setDesc(get(PPC::STXSDX));
2132 } else {
2133 NumStoreSPILLVSRRCAsGpr++;
2134 MI.setDesc(get(PPC::STDX));
2136 return true;
2139 case PPC::CFENCE8: {
2140 auto Val = MI.getOperand(0).getReg();
2141 BuildMI(MBB, MI, DL, get(PPC::CMPD), PPC::CR7).addReg(Val).addReg(Val);
2142 BuildMI(MBB, MI, DL, get(PPC::CTRL_DEP))
2143 .addImm(PPC::PRED_NE_MINUS)
2144 .addReg(PPC::CR7)
2145 .addImm(1);
2146 MI.setDesc(get(PPC::ISYNC));
2147 MI.RemoveOperand(0);
2148 return true;
2151 return false;
2154 unsigned PPCInstrInfo::lookThruCopyLike(unsigned SrcReg,
2155 const MachineRegisterInfo *MRI) {
2156 while (true) {
2157 MachineInstr *MI = MRI->getVRegDef(SrcReg);
2158 if (!MI->isCopyLike())
2159 return SrcReg;
2161 unsigned CopySrcReg;
2162 if (MI->isCopy())
2163 CopySrcReg = MI->getOperand(1).getReg();
2164 else {
2165 assert(MI->isSubregToReg() && "Bad opcode for lookThruCopyLike");
2166 CopySrcReg = MI->getOperand(2).getReg();
2169 if (!TargetRegisterInfo::isVirtualRegister(CopySrcReg))
2170 return CopySrcReg;
2172 SrcReg = CopySrcReg;
2176 // Essentially a compile-time implementation of a compare->isel sequence.
2177 // It takes two constants to compare, along with the true/false registers
2178 // and the comparison type (as a subreg to a CR field) and returns one
2179 // of the true/false registers, depending on the comparison results.
2180 static unsigned selectReg(int64_t Imm1, int64_t Imm2, unsigned CompareOpc,
2181 unsigned TrueReg, unsigned FalseReg,
2182 unsigned CRSubReg) {
2183 // Signed comparisons. The immediates are assumed to be sign-extended.
2184 if (CompareOpc == PPC::CMPWI || CompareOpc == PPC::CMPDI) {
2185 switch (CRSubReg) {
2186 default: llvm_unreachable("Unknown integer comparison type.");
2187 case PPC::sub_lt:
2188 return Imm1 < Imm2 ? TrueReg : FalseReg;
2189 case PPC::sub_gt:
2190 return Imm1 > Imm2 ? TrueReg : FalseReg;
2191 case PPC::sub_eq:
2192 return Imm1 == Imm2 ? TrueReg : FalseReg;
2195 // Unsigned comparisons.
2196 else if (CompareOpc == PPC::CMPLWI || CompareOpc == PPC::CMPLDI) {
2197 switch (CRSubReg) {
2198 default: llvm_unreachable("Unknown integer comparison type.");
2199 case PPC::sub_lt:
2200 return (uint64_t)Imm1 < (uint64_t)Imm2 ? TrueReg : FalseReg;
2201 case PPC::sub_gt:
2202 return (uint64_t)Imm1 > (uint64_t)Imm2 ? TrueReg : FalseReg;
2203 case PPC::sub_eq:
2204 return Imm1 == Imm2 ? TrueReg : FalseReg;
2207 return PPC::NoRegister;
2210 // Replace an instruction with one that materializes a constant (and sets
2211 // CR0 if the original instruction was a record-form instruction).
2212 void PPCInstrInfo::replaceInstrWithLI(MachineInstr &MI,
2213 const LoadImmediateInfo &LII) const {
2214 // Remove existing operands.
2215 int OperandToKeep = LII.SetCR ? 1 : 0;
2216 for (int i = MI.getNumOperands() - 1; i > OperandToKeep; i--)
2217 MI.RemoveOperand(i);
2219 // Replace the instruction.
2220 if (LII.SetCR)
2221 MI.setDesc(get(LII.Is64Bit ? PPC::ANDIo8 : PPC::ANDIo));
2222 else
2223 MI.setDesc(get(LII.Is64Bit ? PPC::LI8 : PPC::LI));
2225 // Set the immediate.
2226 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
2227 .addImm(LII.Imm);
2230 MachineInstr *PPCInstrInfo::getConstantDefMI(MachineInstr &MI,
2231 unsigned &ConstOp,
2232 bool &SeenIntermediateUse) const {
2233 ConstOp = ~0U;
2234 MachineInstr *DefMI = nullptr;
2235 MachineRegisterInfo *MRI = &MI.getParent()->getParent()->getRegInfo();
2236 // If we'ere in SSA, get the defs through the MRI. Otherwise, only look
2237 // within the basic block to see if the register is defined using an LI/LI8.
2238 if (MRI->isSSA()) {
2239 for (int i = 1, e = MI.getNumOperands(); i < e; i++) {
2240 if (!MI.getOperand(i).isReg())
2241 continue;
2242 unsigned Reg = MI.getOperand(i).getReg();
2243 if (!TargetRegisterInfo::isVirtualRegister(Reg))
2244 continue;
2245 unsigned TrueReg = lookThruCopyLike(Reg, MRI);
2246 if (TargetRegisterInfo::isVirtualRegister(TrueReg)) {
2247 DefMI = MRI->getVRegDef(TrueReg);
2248 if (DefMI->getOpcode() == PPC::LI || DefMI->getOpcode() == PPC::LI8) {
2249 ConstOp = i;
2250 break;
2254 } else {
2255 // Looking back through the definition for each operand could be expensive,
2256 // so exit early if this isn't an instruction that either has an immediate
2257 // form or is already an immediate form that we can handle.
2258 ImmInstrInfo III;
2259 unsigned Opc = MI.getOpcode();
2260 bool ConvertibleImmForm =
2261 Opc == PPC::CMPWI || Opc == PPC::CMPLWI ||
2262 Opc == PPC::CMPDI || Opc == PPC::CMPLDI ||
2263 Opc == PPC::ADDI || Opc == PPC::ADDI8 ||
2264 Opc == PPC::ORI || Opc == PPC::ORI8 ||
2265 Opc == PPC::XORI || Opc == PPC::XORI8 ||
2266 Opc == PPC::RLDICL || Opc == PPC::RLDICLo ||
2267 Opc == PPC::RLDICL_32 || Opc == PPC::RLDICL_32_64 ||
2268 Opc == PPC::RLWINM || Opc == PPC::RLWINMo ||
2269 Opc == PPC::RLWINM8 || Opc == PPC::RLWINM8o;
2270 if (!instrHasImmForm(MI, III) && !ConvertibleImmForm)
2271 return nullptr;
2273 // Don't convert or %X, %Y, %Y since that's just a register move.
2274 if ((Opc == PPC::OR || Opc == PPC::OR8) &&
2275 MI.getOperand(1).getReg() == MI.getOperand(2).getReg())
2276 return nullptr;
2277 for (int i = 1, e = MI.getNumOperands(); i < e; i++) {
2278 MachineOperand &MO = MI.getOperand(i);
2279 SeenIntermediateUse = false;
2280 if (MO.isReg() && MO.isUse() && !MO.isImplicit()) {
2281 MachineBasicBlock::reverse_iterator E = MI.getParent()->rend(), It = MI;
2282 It++;
2283 unsigned Reg = MI.getOperand(i).getReg();
2285 // Is this register defined by a load-immediate in this block?
2286 for ( ; It != E; ++It) {
2287 if (It->modifiesRegister(Reg, &getRegisterInfo())) {
2288 if (It->getOpcode() == PPC::LI || It->getOpcode() == PPC::LI8) {
2289 ConstOp = i;
2290 return &*It;
2291 } else
2292 break;
2293 } else if (It->readsRegister(Reg, &getRegisterInfo()))
2294 // If we see another use of this reg between the def and the MI,
2295 // we want to flat it so the def isn't deleted.
2296 SeenIntermediateUse = true;
2301 return ConstOp == ~0U ? nullptr : DefMI;
2304 // If this instruction has an immediate form and one of its operands is a
2305 // result of a load-immediate, convert it to the immediate form if the constant
2306 // is in range.
2307 bool PPCInstrInfo::convertToImmediateForm(MachineInstr &MI,
2308 MachineInstr **KilledDef) const {
2309 MachineFunction *MF = MI.getParent()->getParent();
2310 MachineRegisterInfo *MRI = &MF->getRegInfo();
2311 bool PostRA = !MRI->isSSA();
2312 bool SeenIntermediateUse = true;
2313 unsigned ConstantOperand = ~0U;
2314 MachineInstr *DefMI = getConstantDefMI(MI, ConstantOperand,
2315 SeenIntermediateUse);
2316 if (!DefMI || !DefMI->getOperand(1).isImm())
2317 return false;
2318 assert(ConstantOperand < MI.getNumOperands() &&
2319 "The constant operand needs to be valid at this point");
2321 int64_t Immediate = DefMI->getOperand(1).getImm();
2322 // Sign-extend to 64-bits.
2323 int64_t SExtImm = ((uint64_t)Immediate & ~0x7FFFuLL) != 0 ?
2324 (Immediate | 0xFFFFFFFFFFFF0000) : Immediate;
2326 if (KilledDef && MI.getOperand(ConstantOperand).isKill() &&
2327 !SeenIntermediateUse)
2328 *KilledDef = DefMI;
2330 // If this is a reg+reg instruction that has a reg+imm form, convert it now.
2331 ImmInstrInfo III;
2332 if (instrHasImmForm(MI, III))
2333 return transformToImmForm(MI, III, ConstantOperand, SExtImm);
2335 bool ReplaceWithLI = false;
2336 bool Is64BitLI = false;
2337 int64_t NewImm = 0;
2338 bool SetCR = false;
2339 unsigned Opc = MI.getOpcode();
2340 switch (Opc) {
2341 default: return false;
2343 // FIXME: Any branches conditional on such a comparison can be made
2344 // unconditional. At this time, this happens too infrequently to be worth
2345 // the implementation effort, but if that ever changes, we could convert
2346 // such a pattern here.
2347 case PPC::CMPWI:
2348 case PPC::CMPLWI:
2349 case PPC::CMPDI:
2350 case PPC::CMPLDI: {
2351 // Doing this post-RA would require dataflow analysis to reliably find uses
2352 // of the CR register set by the compare.
2353 if (PostRA)
2354 return false;
2355 // If a compare-immediate is fed by an immediate and is itself an input of
2356 // an ISEL (the most common case) into a COPY of the correct register.
2357 bool Changed = false;
2358 unsigned DefReg = MI.getOperand(0).getReg();
2359 int64_t Comparand = MI.getOperand(2).getImm();
2360 int64_t SExtComparand = ((uint64_t)Comparand & ~0x7FFFuLL) != 0 ?
2361 (Comparand | 0xFFFFFFFFFFFF0000) : Comparand;
2363 for (auto &CompareUseMI : MRI->use_instructions(DefReg)) {
2364 unsigned UseOpc = CompareUseMI.getOpcode();
2365 if (UseOpc != PPC::ISEL && UseOpc != PPC::ISEL8)
2366 continue;
2367 unsigned CRSubReg = CompareUseMI.getOperand(3).getSubReg();
2368 unsigned TrueReg = CompareUseMI.getOperand(1).getReg();
2369 unsigned FalseReg = CompareUseMI.getOperand(2).getReg();
2370 unsigned RegToCopy = selectReg(SExtImm, SExtComparand, Opc, TrueReg,
2371 FalseReg, CRSubReg);
2372 if (RegToCopy == PPC::NoRegister)
2373 continue;
2374 // Can't use PPC::COPY to copy PPC::ZERO[8]. Convert it to LI[8] 0.
2375 if (RegToCopy == PPC::ZERO || RegToCopy == PPC::ZERO8) {
2376 CompareUseMI.setDesc(get(UseOpc == PPC::ISEL8 ? PPC::LI8 : PPC::LI));
2377 CompareUseMI.getOperand(1).ChangeToImmediate(0);
2378 CompareUseMI.RemoveOperand(3);
2379 CompareUseMI.RemoveOperand(2);
2380 continue;
2382 DEBUG(dbgs() << "Found LI -> CMPI -> ISEL, replacing with a copy.\n");
2383 DEBUG(DefMI->dump(); MI.dump(); CompareUseMI.dump());
2384 DEBUG(dbgs() << "Is converted to:\n");
2385 // Convert to copy and remove unneeded operands.
2386 CompareUseMI.setDesc(get(PPC::COPY));
2387 CompareUseMI.RemoveOperand(3);
2388 CompareUseMI.RemoveOperand(RegToCopy == TrueReg ? 2 : 1);
2389 CmpIselsConverted++;
2390 Changed = true;
2391 DEBUG(CompareUseMI.dump());
2393 if (Changed)
2394 return true;
2395 // This may end up incremented multiple times since this function is called
2396 // during a fixed-point transformation, but it is only meant to indicate the
2397 // presence of this opportunity.
2398 MissedConvertibleImmediateInstrs++;
2399 return false;
2402 // Immediate forms - may simply be convertable to an LI.
2403 case PPC::ADDI:
2404 case PPC::ADDI8: {
2405 // Does the sum fit in a 16-bit signed field?
2406 int64_t Addend = MI.getOperand(2).getImm();
2407 if (isInt<16>(Addend + SExtImm)) {
2408 ReplaceWithLI = true;
2409 Is64BitLI = Opc == PPC::ADDI8;
2410 NewImm = Addend + SExtImm;
2411 break;
2414 case PPC::RLDICL:
2415 case PPC::RLDICLo:
2416 case PPC::RLDICL_32:
2417 case PPC::RLDICL_32_64: {
2418 // Use APInt's rotate function.
2419 int64_t SH = MI.getOperand(2).getImm();
2420 int64_t MB = MI.getOperand(3).getImm();
2421 APInt InVal(Opc == PPC::RLDICL ? 64 : 32, SExtImm, true);
2422 InVal = InVal.rotl(SH);
2423 uint64_t Mask = (1LU << (63 - MB + 1)) - 1;
2424 InVal &= Mask;
2425 // Can't replace negative values with an LI as that will sign-extend
2426 // and not clear the left bits. If we're setting the CR bit, we will use
2427 // ANDIo which won't sign extend, so that's safe.
2428 if (isUInt<15>(InVal.getSExtValue()) ||
2429 (Opc == PPC::RLDICLo && isUInt<16>(InVal.getSExtValue()))) {
2430 ReplaceWithLI = true;
2431 Is64BitLI = Opc != PPC::RLDICL_32;
2432 NewImm = InVal.getSExtValue();
2433 SetCR = Opc == PPC::RLDICLo;
2434 break;
2436 return false;
2438 case PPC::RLWINM:
2439 case PPC::RLWINM8:
2440 case PPC::RLWINMo:
2441 case PPC::RLWINM8o: {
2442 int64_t SH = MI.getOperand(2).getImm();
2443 int64_t MB = MI.getOperand(3).getImm();
2444 int64_t ME = MI.getOperand(4).getImm();
2445 APInt InVal(32, SExtImm, true);
2446 InVal = InVal.rotl(SH);
2447 // Set the bits ( MB + 32 ) to ( ME + 32 ).
2448 uint64_t Mask = ((1 << (32 - MB)) - 1) & ~((1 << (31 - ME)) - 1);
2449 InVal &= Mask;
2450 // Can't replace negative values with an LI as that will sign-extend
2451 // and not clear the left bits. If we're setting the CR bit, we will use
2452 // ANDIo which won't sign extend, so that's safe.
2453 bool ValueFits = isUInt<15>(InVal.getSExtValue());
2454 ValueFits |= ((Opc == PPC::RLWINMo || Opc == PPC::RLWINM8o) &&
2455 isUInt<16>(InVal.getSExtValue()));
2456 if (ValueFits) {
2457 ReplaceWithLI = true;
2458 Is64BitLI = Opc == PPC::RLWINM8 || Opc == PPC::RLWINM8o;
2459 NewImm = InVal.getSExtValue();
2460 SetCR = Opc == PPC::RLWINMo || Opc == PPC::RLWINM8o;
2461 break;
2463 return false;
2465 case PPC::ORI:
2466 case PPC::ORI8:
2467 case PPC::XORI:
2468 case PPC::XORI8: {
2469 int64_t LogicalImm = MI.getOperand(2).getImm();
2470 int64_t Result = 0;
2471 if (Opc == PPC::ORI || Opc == PPC::ORI8)
2472 Result = LogicalImm | SExtImm;
2473 else
2474 Result = LogicalImm ^ SExtImm;
2475 if (isInt<16>(Result)) {
2476 ReplaceWithLI = true;
2477 Is64BitLI = Opc == PPC::ORI8 || Opc == PPC::XORI8;
2478 NewImm = Result;
2479 break;
2481 return false;
2485 if (ReplaceWithLI) {
2486 DEBUG(dbgs() << "Replacing instruction:\n");
2487 DEBUG(MI.dump());
2488 DEBUG(dbgs() << "Fed by:\n");
2489 DEBUG(DefMI->dump());
2490 LoadImmediateInfo LII;
2491 LII.Imm = NewImm;
2492 LII.Is64Bit = Is64BitLI;
2493 LII.SetCR = SetCR;
2494 // If we're setting the CR, the original load-immediate must be kept (as an
2495 // operand to ANDIo/ANDI8o).
2496 if (KilledDef && SetCR)
2497 *KilledDef = nullptr;
2498 replaceInstrWithLI(MI, LII);
2499 DEBUG(dbgs() << "With:\n");
2500 DEBUG(MI.dump());
2501 return true;
2503 return false;
2506 bool PPCInstrInfo::instrHasImmForm(const MachineInstr &MI,
2507 ImmInstrInfo &III) const {
2508 unsigned Opc = MI.getOpcode();
2509 // The vast majority of the instructions would need their operand 2 replaced
2510 // with an immediate when switching to the reg+imm form. A marked exception
2511 // are the update form loads/stores for which a constant operand 2 would need
2512 // to turn into a displacement and move operand 1 to the operand 2 position.
2513 III.ImmOpNo = 2;
2514 III.ConstantOpNo = 2;
2515 III.ImmWidth = 16;
2516 III.ImmMustBeMultipleOf = 1;
2517 switch (Opc) {
2518 default: return false;
2519 case PPC::ADD4:
2520 case PPC::ADD8:
2521 III.SignedImm = true;
2522 III.ZeroIsSpecialOrig = 0;
2523 III.ZeroIsSpecialNew = 1;
2524 III.IsCommutative = true;
2525 III.ImmOpcode = Opc == PPC::ADD4 ? PPC::ADDI : PPC::ADDI8;
2526 break;
2527 case PPC::ADDC:
2528 case PPC::ADDC8:
2529 III.SignedImm = true;
2530 III.ZeroIsSpecialOrig = 0;
2531 III.ZeroIsSpecialNew = 0;
2532 III.IsCommutative = true;
2533 III.ImmOpcode = Opc == PPC::ADDC ? PPC::ADDIC : PPC::ADDIC8;
2534 break;
2535 case PPC::ADDCo:
2536 III.SignedImm = true;
2537 III.ZeroIsSpecialOrig = 0;
2538 III.ZeroIsSpecialNew = 0;
2539 III.IsCommutative = true;
2540 III.ImmOpcode = PPC::ADDICo;
2541 break;
2542 case PPC::SUBFC:
2543 case PPC::SUBFC8:
2544 III.SignedImm = true;
2545 III.ZeroIsSpecialOrig = 0;
2546 III.ZeroIsSpecialNew = 0;
2547 III.IsCommutative = false;
2548 III.ImmOpcode = Opc == PPC::SUBFC ? PPC::SUBFIC : PPC::SUBFIC8;
2549 break;
2550 case PPC::CMPW:
2551 case PPC::CMPD:
2552 III.SignedImm = true;
2553 III.ZeroIsSpecialOrig = 0;
2554 III.ZeroIsSpecialNew = 0;
2555 III.IsCommutative = false;
2556 III.ImmOpcode = Opc == PPC::CMPW ? PPC::CMPWI : PPC::CMPDI;
2557 break;
2558 case PPC::CMPLW:
2559 case PPC::CMPLD:
2560 III.SignedImm = false;
2561 III.ZeroIsSpecialOrig = 0;
2562 III.ZeroIsSpecialNew = 0;
2563 III.IsCommutative = false;
2564 III.ImmOpcode = Opc == PPC::CMPLW ? PPC::CMPLWI : PPC::CMPLDI;
2565 break;
2566 case PPC::ANDo:
2567 case PPC::AND8o:
2568 case PPC::OR:
2569 case PPC::OR8:
2570 case PPC::XOR:
2571 case PPC::XOR8:
2572 III.SignedImm = false;
2573 III.ZeroIsSpecialOrig = 0;
2574 III.ZeroIsSpecialNew = 0;
2575 III.IsCommutative = true;
2576 switch(Opc) {
2577 default: llvm_unreachable("Unknown opcode");
2578 case PPC::ANDo: III.ImmOpcode = PPC::ANDIo; break;
2579 case PPC::AND8o: III.ImmOpcode = PPC::ANDIo8; break;
2580 case PPC::OR: III.ImmOpcode = PPC::ORI; break;
2581 case PPC::OR8: III.ImmOpcode = PPC::ORI8; break;
2582 case PPC::XOR: III.ImmOpcode = PPC::XORI; break;
2583 case PPC::XOR8: III.ImmOpcode = PPC::XORI8; break;
2585 break;
2586 case PPC::RLWNM:
2587 case PPC::RLWNM8:
2588 case PPC::RLWNMo:
2589 case PPC::RLWNM8o:
2590 case PPC::RLDCL:
2591 case PPC::RLDCLo:
2592 case PPC::RLDCR:
2593 case PPC::RLDCRo:
2594 case PPC::SLW:
2595 case PPC::SLW8:
2596 case PPC::SLWo:
2597 case PPC::SLW8o:
2598 case PPC::SRW:
2599 case PPC::SRW8:
2600 case PPC::SRWo:
2601 case PPC::SRW8o:
2602 case PPC::SRAW:
2603 case PPC::SRAWo:
2604 case PPC::SLD:
2605 case PPC::SLDo:
2606 case PPC::SRD:
2607 case PPC::SRDo:
2608 case PPC::SRAD:
2609 case PPC::SRADo:
2610 III.SignedImm = false;
2611 III.ZeroIsSpecialOrig = 0;
2612 III.ZeroIsSpecialNew = 0;
2613 III.IsCommutative = false;
2614 // This isn't actually true, but the instructions ignore any of the
2615 // upper bits, so any immediate loaded with an LI is acceptable.
2616 III.ImmWidth = 16;
2617 switch(Opc) {
2618 default: llvm_unreachable("Unknown opcode");
2619 case PPC::RLWNM: III.ImmOpcode = PPC::RLWINM; break;
2620 case PPC::RLWNM8: III.ImmOpcode = PPC::RLWINM8; break;
2621 case PPC::RLWNMo: III.ImmOpcode = PPC::RLWINMo; break;
2622 case PPC::RLWNM8o: III.ImmOpcode = PPC::RLWINM8o; break;
2623 case PPC::RLDCL: III.ImmOpcode = PPC::RLDICL; break;
2624 case PPC::RLDCLo: III.ImmOpcode = PPC::RLDICLo; break;
2625 case PPC::RLDCR: III.ImmOpcode = PPC::RLDICR; break;
2626 case PPC::RLDCRo: III.ImmOpcode = PPC::RLDICRo; break;
2627 case PPC::SLW: III.ImmOpcode = PPC::RLWINM; break;
2628 case PPC::SLW8: III.ImmOpcode = PPC::RLWINM8; break;
2629 case PPC::SLWo: III.ImmOpcode = PPC::RLWINMo; break;
2630 case PPC::SLW8o: III.ImmOpcode = PPC::RLWINM8o; break;
2631 case PPC::SRW: III.ImmOpcode = PPC::RLWINM; break;
2632 case PPC::SRW8: III.ImmOpcode = PPC::RLWINM8; break;
2633 case PPC::SRWo: III.ImmOpcode = PPC::RLWINMo; break;
2634 case PPC::SRW8o: III.ImmOpcode = PPC::RLWINM8o; break;
2635 case PPC::SRAW: III.ImmOpcode = PPC::SRAWI; break;
2636 case PPC::SRAWo: III.ImmOpcode = PPC::SRAWIo; break;
2637 case PPC::SLD: III.ImmOpcode = PPC::RLDICR; break;
2638 case PPC::SLDo: III.ImmOpcode = PPC::RLDICRo; break;
2639 case PPC::SRD: III.ImmOpcode = PPC::RLDICL; break;
2640 case PPC::SRDo: III.ImmOpcode = PPC::RLDICLo; break;
2641 case PPC::SRAD: III.ImmOpcode = PPC::SRADI; break;
2642 case PPC::SRADo: III.ImmOpcode = PPC::SRADIo; break;
2644 break;
2645 // Loads and stores:
2646 case PPC::LBZX:
2647 case PPC::LBZX8:
2648 case PPC::LHZX:
2649 case PPC::LHZX8:
2650 case PPC::LHAX:
2651 case PPC::LHAX8:
2652 case PPC::LWZX:
2653 case PPC::LWZX8:
2654 case PPC::LWAX:
2655 case PPC::LDX:
2656 case PPC::LFSX:
2657 case PPC::LFDX:
2658 case PPC::STBX:
2659 case PPC::STBX8:
2660 case PPC::STHX:
2661 case PPC::STHX8:
2662 case PPC::STWX:
2663 case PPC::STWX8:
2664 case PPC::STDX:
2665 case PPC::STFSX:
2666 case PPC::STFDX:
2667 III.SignedImm = true;
2668 III.ZeroIsSpecialOrig = 1;
2669 III.ZeroIsSpecialNew = 2;
2670 III.IsCommutative = true;
2671 III.ImmOpNo = 1;
2672 III.ConstantOpNo = 2;
2673 switch(Opc) {
2674 default: llvm_unreachable("Unknown opcode");
2675 case PPC::LBZX: III.ImmOpcode = PPC::LBZ; break;
2676 case PPC::LBZX8: III.ImmOpcode = PPC::LBZ8; break;
2677 case PPC::LHZX: III.ImmOpcode = PPC::LHZ; break;
2678 case PPC::LHZX8: III.ImmOpcode = PPC::LHZ8; break;
2679 case PPC::LHAX: III.ImmOpcode = PPC::LHA; break;
2680 case PPC::LHAX8: III.ImmOpcode = PPC::LHA8; break;
2681 case PPC::LWZX: III.ImmOpcode = PPC::LWZ; break;
2682 case PPC::LWZX8: III.ImmOpcode = PPC::LWZ8; break;
2683 case PPC::LWAX:
2684 III.ImmOpcode = PPC::LWA;
2685 III.ImmMustBeMultipleOf = 4;
2686 break;
2687 case PPC::LDX: III.ImmOpcode = PPC::LD; III.ImmMustBeMultipleOf = 4; break;
2688 case PPC::LFSX: III.ImmOpcode = PPC::LFS; break;
2689 case PPC::LFDX: III.ImmOpcode = PPC::LFD; break;
2690 case PPC::STBX: III.ImmOpcode = PPC::STB; break;
2691 case PPC::STBX8: III.ImmOpcode = PPC::STB8; break;
2692 case PPC::STHX: III.ImmOpcode = PPC::STH; break;
2693 case PPC::STHX8: III.ImmOpcode = PPC::STH8; break;
2694 case PPC::STWX: III.ImmOpcode = PPC::STW; break;
2695 case PPC::STWX8: III.ImmOpcode = PPC::STW8; break;
2696 case PPC::STDX:
2697 III.ImmOpcode = PPC::STD;
2698 III.ImmMustBeMultipleOf = 4;
2699 break;
2700 case PPC::STFSX: III.ImmOpcode = PPC::STFS; break;
2701 case PPC::STFDX: III.ImmOpcode = PPC::STFD; break;
2703 break;
2704 case PPC::LBZUX:
2705 case PPC::LBZUX8:
2706 case PPC::LHZUX:
2707 case PPC::LHZUX8:
2708 case PPC::LHAUX:
2709 case PPC::LHAUX8:
2710 case PPC::LWZUX:
2711 case PPC::LWZUX8:
2712 case PPC::LDUX:
2713 case PPC::LFSUX:
2714 case PPC::LFDUX:
2715 case PPC::STBUX:
2716 case PPC::STBUX8:
2717 case PPC::STHUX:
2718 case PPC::STHUX8:
2719 case PPC::STWUX:
2720 case PPC::STWUX8:
2721 case PPC::STDUX:
2722 case PPC::STFSUX:
2723 case PPC::STFDUX:
2724 III.SignedImm = true;
2725 III.ZeroIsSpecialOrig = 2;
2726 III.ZeroIsSpecialNew = 3;
2727 III.IsCommutative = false;
2728 III.ImmOpNo = 2;
2729 III.ConstantOpNo = 3;
2730 switch(Opc) {
2731 default: llvm_unreachable("Unknown opcode");
2732 case PPC::LBZUX: III.ImmOpcode = PPC::LBZU; break;
2733 case PPC::LBZUX8: III.ImmOpcode = PPC::LBZU8; break;
2734 case PPC::LHZUX: III.ImmOpcode = PPC::LHZU; break;
2735 case PPC::LHZUX8: III.ImmOpcode = PPC::LHZU8; break;
2736 case PPC::LHAUX: III.ImmOpcode = PPC::LHAU; break;
2737 case PPC::LHAUX8: III.ImmOpcode = PPC::LHAU8; break;
2738 case PPC::LWZUX: III.ImmOpcode = PPC::LWZU; break;
2739 case PPC::LWZUX8: III.ImmOpcode = PPC::LWZU8; break;
2740 case PPC::LDUX:
2741 III.ImmOpcode = PPC::LDU;
2742 III.ImmMustBeMultipleOf = 4;
2743 break;
2744 case PPC::LFSUX: III.ImmOpcode = PPC::LFSU; break;
2745 case PPC::LFDUX: III.ImmOpcode = PPC::LFDU; break;
2746 case PPC::STBUX: III.ImmOpcode = PPC::STBU; break;
2747 case PPC::STBUX8: III.ImmOpcode = PPC::STBU8; break;
2748 case PPC::STHUX: III.ImmOpcode = PPC::STHU; break;
2749 case PPC::STHUX8: III.ImmOpcode = PPC::STHU8; break;
2750 case PPC::STWUX: III.ImmOpcode = PPC::STWU; break;
2751 case PPC::STWUX8: III.ImmOpcode = PPC::STWU8; break;
2752 case PPC::STDUX:
2753 III.ImmOpcode = PPC::STDU;
2754 III.ImmMustBeMultipleOf = 4;
2755 break;
2756 case PPC::STFSUX: III.ImmOpcode = PPC::STFSU; break;
2757 case PPC::STFDUX: III.ImmOpcode = PPC::STFDU; break;
2759 break;
2760 // Power9 only.
2761 case PPC::LXVX:
2762 case PPC::LXSSPX:
2763 case PPC::LXSDX:
2764 case PPC::STXVX:
2765 case PPC::STXSSPX:
2766 case PPC::STXSDX:
2767 if (!Subtarget.hasP9Vector())
2768 return false;
2769 III.SignedImm = true;
2770 III.ZeroIsSpecialOrig = 1;
2771 III.ZeroIsSpecialNew = 2;
2772 III.IsCommutative = true;
2773 III.ImmOpNo = 1;
2774 III.ConstantOpNo = 2;
2775 switch(Opc) {
2776 default: llvm_unreachable("Unknown opcode");
2777 case PPC::LXVX:
2778 III.ImmOpcode = PPC::LXV;
2779 III.ImmMustBeMultipleOf = 16;
2780 break;
2781 case PPC::LXSSPX:
2782 III.ImmOpcode = PPC::LXSSP;
2783 III.ImmMustBeMultipleOf = 4;
2784 break;
2785 case PPC::LXSDX:
2786 III.ImmOpcode = PPC::LXSD;
2787 III.ImmMustBeMultipleOf = 4;
2788 break;
2789 case PPC::STXVX:
2790 III.ImmOpcode = PPC::STXV;
2791 III.ImmMustBeMultipleOf = 16;
2792 break;
2793 case PPC::STXSSPX:
2794 III.ImmOpcode = PPC::STXSSP;
2795 III.ImmMustBeMultipleOf = 4;
2796 break;
2797 case PPC::STXSDX:
2798 III.ImmOpcode = PPC::STXSD;
2799 III.ImmMustBeMultipleOf = 4;
2800 break;
2802 break;
2804 return true;
2807 // Utility function for swaping two arbitrary operands of an instruction.
2808 static void swapMIOperands(MachineInstr &MI, unsigned Op1, unsigned Op2) {
2809 assert(Op1 != Op2 && "Cannot swap operand with itself.");
2811 unsigned MaxOp = std::max(Op1, Op2);
2812 unsigned MinOp = std::min(Op1, Op2);
2813 MachineOperand MOp1 = MI.getOperand(MinOp);
2814 MachineOperand MOp2 = MI.getOperand(MaxOp);
2815 MI.RemoveOperand(std::max(Op1, Op2));
2816 MI.RemoveOperand(std::min(Op1, Op2));
2818 // If the operands we are swapping are the two at the end (the common case)
2819 // we can just remove both and add them in the opposite order.
2820 if (MaxOp - MinOp == 1 && MI.getNumOperands() == MinOp) {
2821 MI.addOperand(MOp2);
2822 MI.addOperand(MOp1);
2823 } else {
2824 // Store all operands in a temporary vector, remove them and re-add in the
2825 // right order.
2826 SmallVector<MachineOperand, 2> MOps;
2827 unsigned TotalOps = MI.getNumOperands() + 2; // We've already removed 2 ops.
2828 for (unsigned i = MI.getNumOperands() - 1; i >= MinOp; i--) {
2829 MOps.push_back(MI.getOperand(i));
2830 MI.RemoveOperand(i);
2832 // MOp2 needs to be added next.
2833 MI.addOperand(MOp2);
2834 // Now add the rest.
2835 for (unsigned i = MI.getNumOperands(); i < TotalOps; i++) {
2836 if (i == MaxOp)
2837 MI.addOperand(MOp1);
2838 else {
2839 MI.addOperand(MOps.back());
2840 MOps.pop_back();
2846 bool PPCInstrInfo::transformToImmForm(MachineInstr &MI, const ImmInstrInfo &III,
2847 unsigned ConstantOpNo,
2848 int64_t Imm) const {
2849 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
2850 bool PostRA = !MRI.isSSA();
2851 // Exit early if we can't convert this.
2852 if ((ConstantOpNo != III.ConstantOpNo) && !III.IsCommutative)
2853 return false;
2854 if (Imm % III.ImmMustBeMultipleOf)
2855 return false;
2856 if (III.SignedImm) {
2857 APInt ActualValue(64, Imm, true);
2858 if (!ActualValue.isSignedIntN(III.ImmWidth))
2859 return false;
2860 } else {
2861 uint64_t UnsignedMax = (1 << III.ImmWidth) - 1;
2862 if ((uint64_t)Imm > UnsignedMax)
2863 return false;
2866 // If we're post-RA, the instructions don't agree on whether register zero is
2867 // special, we can transform this as long as the register operand that will
2868 // end up in the location where zero is special isn't R0.
2869 if (PostRA && III.ZeroIsSpecialOrig != III.ZeroIsSpecialNew) {
2870 unsigned PosForOrigZero = III.ZeroIsSpecialOrig ? III.ZeroIsSpecialOrig :
2871 III.ZeroIsSpecialNew + 1;
2872 unsigned OrigZeroReg = MI.getOperand(PosForOrigZero).getReg();
2873 unsigned NewZeroReg = MI.getOperand(III.ZeroIsSpecialNew).getReg();
2874 // If R0 is in the operand where zero is special for the new instruction,
2875 // it is unsafe to transform if the constant operand isn't that operand.
2876 if ((NewZeroReg == PPC::R0 || NewZeroReg == PPC::X0) &&
2877 ConstantOpNo != III.ZeroIsSpecialNew)
2878 return false;
2879 if ((OrigZeroReg == PPC::R0 || OrigZeroReg == PPC::X0) &&
2880 ConstantOpNo != PosForOrigZero)
2881 return false;
2884 unsigned Opc = MI.getOpcode();
2885 bool SpecialShift32 =
2886 Opc == PPC::SLW || Opc == PPC::SLWo || Opc == PPC::SRW || Opc == PPC::SRWo;
2887 bool SpecialShift64 =
2888 Opc == PPC::SLD || Opc == PPC::SLDo || Opc == PPC::SRD || Opc == PPC::SRDo;
2889 bool SetCR = Opc == PPC::SLWo || Opc == PPC::SRWo ||
2890 Opc == PPC::SLDo || Opc == PPC::SRDo;
2891 bool RightShift =
2892 Opc == PPC::SRW || Opc == PPC::SRWo || Opc == PPC::SRD || Opc == PPC::SRDo;
2894 MI.setDesc(get(III.ImmOpcode));
2895 if (ConstantOpNo == III.ConstantOpNo) {
2896 // Converting shifts to immediate form is a bit tricky since they may do
2897 // one of three things:
2898 // 1. If the shift amount is between OpSize and 2*OpSize, the result is zero
2899 // 2. If the shift amount is zero, the result is unchanged (save for maybe
2900 // setting CR0)
2901 // 3. If the shift amount is in [1, OpSize), it's just a shift
2902 if (SpecialShift32 || SpecialShift64) {
2903 LoadImmediateInfo LII;
2904 LII.Imm = 0;
2905 LII.SetCR = SetCR;
2906 LII.Is64Bit = SpecialShift64;
2907 uint64_t ShAmt = Imm & (SpecialShift32 ? 0x1F : 0x3F);
2908 if (Imm & (SpecialShift32 ? 0x20 : 0x40))
2909 replaceInstrWithLI(MI, LII);
2910 // Shifts by zero don't change the value. If we don't need to set CR0,
2911 // just convert this to a COPY. Can't do this post-RA since we've already
2912 // cleaned up the copies.
2913 else if (!SetCR && ShAmt == 0 && !PostRA) {
2914 MI.RemoveOperand(2);
2915 MI.setDesc(get(PPC::COPY));
2916 } else {
2917 // The 32 bit and 64 bit instructions are quite different.
2918 if (SpecialShift32) {
2919 // Left shifts use (N, 0, 31-N), right shifts use (32-N, N, 31).
2920 uint64_t SH = RightShift ? 32 - ShAmt : ShAmt;
2921 uint64_t MB = RightShift ? ShAmt : 0;
2922 uint64_t ME = RightShift ? 31 : 31 - ShAmt;
2923 MI.getOperand(III.ConstantOpNo).ChangeToImmediate(SH);
2924 MachineInstrBuilder(*MI.getParent()->getParent(), MI).addImm(MB)
2925 .addImm(ME);
2926 } else {
2927 // Left shifts use (N, 63-N), right shifts use (64-N, N).
2928 uint64_t SH = RightShift ? 64 - ShAmt : ShAmt;
2929 uint64_t ME = RightShift ? ShAmt : 63 - ShAmt;
2930 MI.getOperand(III.ConstantOpNo).ChangeToImmediate(SH);
2931 MachineInstrBuilder(*MI.getParent()->getParent(), MI).addImm(ME);
2934 } else
2935 MI.getOperand(ConstantOpNo).ChangeToImmediate(Imm);
2937 // Convert commutative instructions (switch the operands and convert the
2938 // desired one to an immediate.
2939 else if (III.IsCommutative) {
2940 MI.getOperand(ConstantOpNo).ChangeToImmediate(Imm);
2941 swapMIOperands(MI, ConstantOpNo, III.ConstantOpNo);
2942 } else
2943 llvm_unreachable("Should have exited early!");
2945 // For instructions for which the constant register replaces a different
2946 // operand than where the immediate goes, we need to swap them.
2947 if (III.ConstantOpNo != III.ImmOpNo)
2948 swapMIOperands(MI, III.ConstantOpNo, III.ImmOpNo);
2950 // If the R0/X0 register is special for the original instruction and not for
2951 // the new instruction (or vice versa), we need to fix up the register class.
2952 if (!PostRA && III.ZeroIsSpecialOrig != III.ZeroIsSpecialNew) {
2953 if (!III.ZeroIsSpecialOrig) {
2954 unsigned RegToModify = MI.getOperand(III.ZeroIsSpecialNew).getReg();
2955 const TargetRegisterClass *NewRC =
2956 MRI.getRegClass(RegToModify)->hasSuperClassEq(&PPC::GPRCRegClass) ?
2957 &PPC::GPRC_and_GPRC_NOR0RegClass : &PPC::G8RC_and_G8RC_NOX0RegClass;
2958 MRI.setRegClass(RegToModify, NewRC);
2961 return true;
2964 const TargetRegisterClass *
2965 PPCInstrInfo::updatedRC(const TargetRegisterClass *RC) const {
2966 if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass)
2967 return &PPC::VSRCRegClass;
2968 return RC;
2971 int PPCInstrInfo::getRecordFormOpcode(unsigned Opcode) {
2972 return PPC::getRecordFormOpcode(Opcode);
2975 // This function returns true if the machine instruction
2976 // always outputs a value by sign-extending a 32 bit value,
2977 // i.e. 0 to 31-th bits are same as 32-th bit.
2978 static bool isSignExtendingOp(const MachineInstr &MI) {
2979 int Opcode = MI.getOpcode();
2980 if (Opcode == PPC::LI || Opcode == PPC::LI8 ||
2981 Opcode == PPC::LIS || Opcode == PPC::LIS8 ||
2982 Opcode == PPC::SRAW || Opcode == PPC::SRAWo ||
2983 Opcode == PPC::SRAWI || Opcode == PPC::SRAWIo ||
2984 Opcode == PPC::LWA || Opcode == PPC::LWAX ||
2985 Opcode == PPC::LWA_32 || Opcode == PPC::LWAX_32 ||
2986 Opcode == PPC::LHA || Opcode == PPC::LHAX ||
2987 Opcode == PPC::LHA8 || Opcode == PPC::LHAX8 ||
2988 Opcode == PPC::LBZ || Opcode == PPC::LBZX ||
2989 Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 ||
2990 Opcode == PPC::LBZU || Opcode == PPC::LBZUX ||
2991 Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 ||
2992 Opcode == PPC::LHZ || Opcode == PPC::LHZX ||
2993 Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 ||
2994 Opcode == PPC::LHZU || Opcode == PPC::LHZUX ||
2995 Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8 ||
2996 Opcode == PPC::EXTSB || Opcode == PPC::EXTSBo ||
2997 Opcode == PPC::EXTSH || Opcode == PPC::EXTSHo ||
2998 Opcode == PPC::EXTSB8 || Opcode == PPC::EXTSH8 ||
2999 Opcode == PPC::EXTSW || Opcode == PPC::EXTSWo ||
3000 Opcode == PPC::EXTSH8_32_64 || Opcode == PPC::EXTSW_32_64 ||
3001 Opcode == PPC::EXTSB8_32_64)
3002 return true;
3004 if (Opcode == PPC::RLDICL && MI.getOperand(3).getImm() >= 33)
3005 return true;
3007 if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINMo ||
3008 Opcode == PPC::RLWNM || Opcode == PPC::RLWNMo) &&
3009 MI.getOperand(3).getImm() > 0 &&
3010 MI.getOperand(3).getImm() <= MI.getOperand(4).getImm())
3011 return true;
3013 return false;
3016 // This function returns true if the machine instruction
3017 // always outputs zeros in higher 32 bits.
3018 static bool isZeroExtendingOp(const MachineInstr &MI) {
3019 int Opcode = MI.getOpcode();
3020 // The 16-bit immediate is sign-extended in li/lis.
3021 // If the most significant bit is zero, all higher bits are zero.
3022 if (Opcode == PPC::LI || Opcode == PPC::LI8 ||
3023 Opcode == PPC::LIS || Opcode == PPC::LIS8) {
3024 int64_t Imm = MI.getOperand(1).getImm();
3025 if (((uint64_t)Imm & ~0x7FFFuLL) == 0)
3026 return true;
3029 // We have some variations of rotate-and-mask instructions
3030 // that clear higher 32-bits.
3031 if ((Opcode == PPC::RLDICL || Opcode == PPC::RLDICLo ||
3032 Opcode == PPC::RLDCL || Opcode == PPC::RLDCLo ||
3033 Opcode == PPC::RLDICL_32_64) &&
3034 MI.getOperand(3).getImm() >= 32)
3035 return true;
3037 if ((Opcode == PPC::RLDIC || Opcode == PPC::RLDICo) &&
3038 MI.getOperand(3).getImm() >= 32 &&
3039 MI.getOperand(3).getImm() <= 63 - MI.getOperand(2).getImm())
3040 return true;
3042 if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINMo ||
3043 Opcode == PPC::RLWNM || Opcode == PPC::RLWNMo ||
3044 Opcode == PPC::RLWINM8 || Opcode == PPC::RLWNM8) &&
3045 MI.getOperand(3).getImm() <= MI.getOperand(4).getImm())
3046 return true;
3048 // There are other instructions that clear higher 32-bits.
3049 if (Opcode == PPC::CNTLZW || Opcode == PPC::CNTLZWo ||
3050 Opcode == PPC::CNTTZW || Opcode == PPC::CNTTZWo ||
3051 Opcode == PPC::CNTLZW8 || Opcode == PPC::CNTTZW8 ||
3052 Opcode == PPC::CNTLZD || Opcode == PPC::CNTLZDo ||
3053 Opcode == PPC::CNTTZD || Opcode == PPC::CNTTZDo ||
3054 Opcode == PPC::POPCNTD || Opcode == PPC::POPCNTW ||
3055 Opcode == PPC::SLW || Opcode == PPC::SLWo ||
3056 Opcode == PPC::SRW || Opcode == PPC::SRWo ||
3057 Opcode == PPC::SLW8 || Opcode == PPC::SRW8 ||
3058 Opcode == PPC::SLWI || Opcode == PPC::SLWIo ||
3059 Opcode == PPC::SRWI || Opcode == PPC::SRWIo ||
3060 Opcode == PPC::LWZ || Opcode == PPC::LWZX ||
3061 Opcode == PPC::LWZU || Opcode == PPC::LWZUX ||
3062 Opcode == PPC::LWBRX || Opcode == PPC::LHBRX ||
3063 Opcode == PPC::LHZ || Opcode == PPC::LHZX ||
3064 Opcode == PPC::LHZU || Opcode == PPC::LHZUX ||
3065 Opcode == PPC::LBZ || Opcode == PPC::LBZX ||
3066 Opcode == PPC::LBZU || Opcode == PPC::LBZUX ||
3067 Opcode == PPC::LWZ8 || Opcode == PPC::LWZX8 ||
3068 Opcode == PPC::LWZU8 || Opcode == PPC::LWZUX8 ||
3069 Opcode == PPC::LWBRX8 || Opcode == PPC::LHBRX8 ||
3070 Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 ||
3071 Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8 ||
3072 Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 ||
3073 Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 ||
3074 Opcode == PPC::ANDIo || Opcode == PPC::ANDISo ||
3075 Opcode == PPC::ROTRWI || Opcode == PPC::ROTRWIo ||
3076 Opcode == PPC::EXTLWI || Opcode == PPC::EXTLWIo ||
3077 Opcode == PPC::MFVSRWZ)
3078 return true;
3080 return false;
3083 // This function returns true if the input MachineInstr is a TOC save
3084 // instruction.
3085 bool PPCInstrInfo::isTOCSaveMI(const MachineInstr &MI) const {
3086 if (!MI.getOperand(1).isImm() || !MI.getOperand(2).isReg())
3087 return false;
3088 unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset();
3089 unsigned StackOffset = MI.getOperand(1).getImm();
3090 unsigned StackReg = MI.getOperand(2).getReg();
3091 if (StackReg == PPC::X1 && StackOffset == TOCSaveOffset)
3092 return true;
3094 return false;
3097 // We limit the max depth to track incoming values of PHIs or binary ops
3098 // (e.g. AND) to avoid exsessive cost.
3099 const unsigned MAX_DEPTH = 1;
3101 bool
3102 PPCInstrInfo::isSignOrZeroExtended(const MachineInstr &MI, bool SignExt,
3103 const unsigned Depth) const {
3104 const MachineFunction *MF = MI.getParent()->getParent();
3105 const MachineRegisterInfo *MRI = &MF->getRegInfo();
3107 // If we know this instruction returns sign- or zero-extended result,
3108 // return true.
3109 if (SignExt ? isSignExtendingOp(MI):
3110 isZeroExtendingOp(MI))
3111 return true;
3113 switch (MI.getOpcode()) {
3114 case PPC::COPY: {
3115 unsigned SrcReg = MI.getOperand(1).getReg();
3117 // In both ELFv1 and v2 ABI, method parameters and the return value
3118 // are sign- or zero-extended.
3119 if (MF->getSubtarget<PPCSubtarget>().isSVR4ABI()) {
3120 const PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
3121 // We check the ZExt/SExt flags for a method parameter.
3122 if (MI.getParent()->getBasicBlock() ==
3123 &MF->getFunction()->getEntryBlock()) {
3124 unsigned VReg = MI.getOperand(0).getReg();
3125 if (MF->getRegInfo().isLiveIn(VReg))
3126 return SignExt ? FuncInfo->isLiveInSExt(VReg) :
3127 FuncInfo->isLiveInZExt(VReg);
3130 // For a method return value, we check the ZExt/SExt flags in attribute.
3131 // We assume the following code sequence for method call.
3132 // ADJCALLSTACKDOWN 32, implicit dead %r1, implicit %r1
3133 // BL8_NOP @func,...
3134 // ADJCALLSTACKUP 32, 0, implicit dead %r1, implicit %r1
3135 // %5 = COPY %x3; G8RC:%5
3136 if (SrcReg == PPC::X3) {
3137 const MachineBasicBlock *MBB = MI.getParent();
3138 MachineBasicBlock::const_instr_iterator II =
3139 MachineBasicBlock::const_instr_iterator(&MI);
3140 if (II != MBB->instr_begin() &&
3141 (--II)->getOpcode() == PPC::ADJCALLSTACKUP) {
3142 const MachineInstr &CallMI = *(--II);
3143 if (CallMI.isCall() && CallMI.getOperand(0).isGlobal()) {
3144 const Function *CalleeFn =
3145 dyn_cast<Function>(CallMI.getOperand(0).getGlobal());
3146 if (!CalleeFn)
3147 return false;
3148 const IntegerType *IntTy =
3149 dyn_cast<IntegerType>(CalleeFn->getReturnType());
3150 const AttributeSet &Attrs =
3151 CalleeFn->getAttributes().getRetAttributes();
3152 if (IntTy && IntTy->getBitWidth() <= 32)
3153 return Attrs.hasAttribute(SignExt ? Attribute::SExt :
3154 Attribute::ZExt);
3160 // If this is a copy from another register, we recursively check source.
3161 if (!TargetRegisterInfo::isVirtualRegister(SrcReg))
3162 return false;
3163 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
3164 if (SrcMI != NULL)
3165 return isSignOrZeroExtended(*SrcMI, SignExt, Depth);
3167 return false;
3170 case PPC::ANDIo:
3171 case PPC::ANDISo:
3172 case PPC::ORI:
3173 case PPC::ORIS:
3174 case PPC::XORI:
3175 case PPC::XORIS:
3176 case PPC::ANDIo8:
3177 case PPC::ANDISo8:
3178 case PPC::ORI8:
3179 case PPC::ORIS8:
3180 case PPC::XORI8:
3181 case PPC::XORIS8: {
3182 // logical operation with 16-bit immediate does not change the upper bits.
3183 // So, we track the operand register as we do for register copy.
3184 unsigned SrcReg = MI.getOperand(1).getReg();
3185 if (!TargetRegisterInfo::isVirtualRegister(SrcReg))
3186 return false;
3187 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
3188 if (SrcMI != NULL)
3189 return isSignOrZeroExtended(*SrcMI, SignExt, Depth);
3191 return false;
3194 // If all incoming values are sign-/zero-extended,
3195 // the output of OR, ISEL or PHI is also sign-/zero-extended.
3196 case PPC::OR:
3197 case PPC::OR8:
3198 case PPC::ISEL:
3199 case PPC::PHI: {
3200 if (Depth >= MAX_DEPTH)
3201 return false;
3203 // The input registers for PHI are operand 1, 3, ...
3204 // The input registers for others are operand 1 and 2.
3205 unsigned E = 3, D = 1;
3206 if (MI.getOpcode() == PPC::PHI) {
3207 E = MI.getNumOperands();
3208 D = 2;
3211 for (unsigned I = 1; I != E; I += D) {
3212 if (MI.getOperand(I).isReg()) {
3213 unsigned SrcReg = MI.getOperand(I).getReg();
3214 if (!TargetRegisterInfo::isVirtualRegister(SrcReg))
3215 return false;
3216 const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
3217 if (SrcMI == NULL || !isSignOrZeroExtended(*SrcMI, SignExt, Depth+1))
3218 return false;
3220 else
3221 return false;
3223 return true;
3226 // If at least one of the incoming values of an AND is zero extended
3227 // then the output is also zero-extended. If both of the incoming values
3228 // are sign-extended then the output is also sign extended.
3229 case PPC::AND:
3230 case PPC::AND8: {
3231 if (Depth >= MAX_DEPTH)
3232 return false;
3234 assert(MI.getOperand(1).isReg() && MI.getOperand(2).isReg());
3236 unsigned SrcReg1 = MI.getOperand(1).getReg();
3237 unsigned SrcReg2 = MI.getOperand(2).getReg();
3239 if (!TargetRegisterInfo::isVirtualRegister(SrcReg1) ||
3240 !TargetRegisterInfo::isVirtualRegister(SrcReg2))
3241 return false;
3243 const MachineInstr *MISrc1 = MRI->getVRegDef(SrcReg1);
3244 const MachineInstr *MISrc2 = MRI->getVRegDef(SrcReg2);
3245 if (!MISrc1 || !MISrc2)
3246 return false;
3248 if(SignExt)
3249 return isSignOrZeroExtended(*MISrc1, SignExt, Depth+1) &&
3250 isSignOrZeroExtended(*MISrc2, SignExt, Depth+1);
3251 else
3252 return isSignOrZeroExtended(*MISrc1, SignExt, Depth+1) ||
3253 isSignOrZeroExtended(*MISrc2, SignExt, Depth+1);
3256 default:
3257 break;
3259 return false;