1 //===-- PPCInstrInfo.cpp - PowerPC Instruction Information ----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains the PowerPC implementation of the TargetInstrInfo class.
11 //===----------------------------------------------------------------------===//
13 #include "PPCInstrInfo.h"
14 #include "MCTargetDesc/PPCPredicates.h"
16 #include "PPCHazardRecognizers.h"
17 #include "PPCInstrBuilder.h"
18 #include "PPCMachineFunctionInfo.h"
19 #include "PPCTargetMachine.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/CodeGen/LiveIntervals.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineMemOperand.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/PseudoSourceValue.h"
29 #include "llvm/CodeGen/ScheduleDAG.h"
30 #include "llvm/CodeGen/SlotIndexes.h"
31 #include "llvm/CodeGen/StackMaps.h"
32 #include "llvm/MC/MCAsmInfo.h"
33 #include "llvm/MC/MCInst.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/TargetRegistry.h"
38 #include "llvm/Support/raw_ostream.h"
42 #define DEBUG_TYPE "ppc-instr-info"
44 #define GET_INSTRMAP_INFO
45 #define GET_INSTRINFO_CTOR_DTOR
46 #include "PPCGenInstrInfo.inc"
48 STATISTIC(NumStoreSPILLVSRRCAsVec
,
49 "Number of spillvsrrc spilled to stack as vec");
50 STATISTIC(NumStoreSPILLVSRRCAsGpr
,
51 "Number of spillvsrrc spilled to stack as gpr");
52 STATISTIC(NumGPRtoVSRSpill
, "Number of gpr spills to spillvsrrc");
53 STATISTIC(CmpIselsConverted
,
54 "Number of ISELs that depend on comparison of constants converted");
55 STATISTIC(MissedConvertibleImmediateInstrs
,
56 "Number of compare-immediate instructions fed by constants");
57 STATISTIC(NumRcRotatesConvertedToRcAnd
,
58 "Number of record-form rotates converted to record-form andi");
61 opt
<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden
,
62 cl::desc("Disable analysis for CTR loops"));
64 static cl::opt
<bool> DisableCmpOpt("disable-ppc-cmp-opt",
65 cl::desc("Disable compare instruction optimization"), cl::Hidden
);
67 static cl::opt
<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy",
68 cl::desc("Causes the backend to crash instead of generating a nop VSX copy"),
72 UseOldLatencyCalc("ppc-old-latency-calc", cl::Hidden
,
73 cl::desc("Use the old (incorrect) instruction latency calculation"));
75 // Index into the OpcodesForSpill array.
85 SOK_VectorFloat8Spill
,
86 SOK_VectorFloat4Spill
,
94 SOK_LastOpcodeSpill
// This must be last on the enum.
97 // Pin the vtable to this file.
98 void PPCInstrInfo::anchor() {}
100 PPCInstrInfo::PPCInstrInfo(PPCSubtarget
&STI
)
101 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN
, PPC::ADJCALLSTACKUP
,
102 /* CatchRetOpcode */ -1,
103 STI
.isPPC64() ? PPC::BLR8
: PPC::BLR
),
104 Subtarget(STI
), RI(STI
.getTargetMachine()) {}
106 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
107 /// this target when scheduling the DAG.
108 ScheduleHazardRecognizer
*
109 PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo
*STI
,
110 const ScheduleDAG
*DAG
) const {
112 static_cast<const PPCSubtarget
*>(STI
)->getDarwinDirective();
113 if (Directive
== PPC::DIR_440
|| Directive
== PPC::DIR_A2
||
114 Directive
== PPC::DIR_E500mc
|| Directive
== PPC::DIR_E5500
) {
115 const InstrItineraryData
*II
=
116 static_cast<const PPCSubtarget
*>(STI
)->getInstrItineraryData();
117 return new ScoreboardHazardRecognizer(II
, DAG
);
120 return TargetInstrInfo::CreateTargetHazardRecognizer(STI
, DAG
);
123 /// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
124 /// to use for this target when scheduling the DAG.
125 ScheduleHazardRecognizer
*
126 PPCInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData
*II
,
127 const ScheduleDAG
*DAG
) const {
129 DAG
->MF
.getSubtarget
<PPCSubtarget
>().getDarwinDirective();
131 // FIXME: Leaving this as-is until we have POWER9 scheduling info
132 if (Directive
== PPC::DIR_PWR7
|| Directive
== PPC::DIR_PWR8
)
133 return new PPCDispatchGroupSBHazardRecognizer(II
, DAG
);
135 // Most subtargets use a PPC970 recognizer.
136 if (Directive
!= PPC::DIR_440
&& Directive
!= PPC::DIR_A2
&&
137 Directive
!= PPC::DIR_E500mc
&& Directive
!= PPC::DIR_E5500
) {
138 assert(DAG
->TII
&& "No InstrInfo?");
140 return new PPCHazardRecognizer970(*DAG
);
143 return new ScoreboardHazardRecognizer(II
, DAG
);
146 unsigned PPCInstrInfo::getInstrLatency(const InstrItineraryData
*ItinData
,
147 const MachineInstr
&MI
,
148 unsigned *PredCost
) const {
149 if (!ItinData
|| UseOldLatencyCalc
)
150 return PPCGenInstrInfo::getInstrLatency(ItinData
, MI
, PredCost
);
152 // The default implementation of getInstrLatency calls getStageLatency, but
153 // getStageLatency does not do the right thing for us. While we have
154 // itinerary, most cores are fully pipelined, and so the itineraries only
155 // express the first part of the pipeline, not every stage. Instead, we need
156 // to use the listed output operand cycle number (using operand 0 here, which
159 unsigned Latency
= 1;
160 unsigned DefClass
= MI
.getDesc().getSchedClass();
161 for (unsigned i
= 0, e
= MI
.getNumOperands(); i
!= e
; ++i
) {
162 const MachineOperand
&MO
= MI
.getOperand(i
);
163 if (!MO
.isReg() || !MO
.isDef() || MO
.isImplicit())
166 int Cycle
= ItinData
->getOperandCycle(DefClass
, i
);
170 Latency
= std::max(Latency
, (unsigned) Cycle
);
176 int PPCInstrInfo::getOperandLatency(const InstrItineraryData
*ItinData
,
177 const MachineInstr
&DefMI
, unsigned DefIdx
,
178 const MachineInstr
&UseMI
,
179 unsigned UseIdx
) const {
180 int Latency
= PPCGenInstrInfo::getOperandLatency(ItinData
, DefMI
, DefIdx
,
183 if (!DefMI
.getParent())
186 const MachineOperand
&DefMO
= DefMI
.getOperand(DefIdx
);
187 unsigned Reg
= DefMO
.getReg();
190 if (TargetRegisterInfo::isVirtualRegister(Reg
)) {
191 const MachineRegisterInfo
*MRI
=
192 &DefMI
.getParent()->getParent()->getRegInfo();
193 IsRegCR
= MRI
->getRegClass(Reg
)->hasSuperClassEq(&PPC::CRRCRegClass
) ||
194 MRI
->getRegClass(Reg
)->hasSuperClassEq(&PPC::CRBITRCRegClass
);
196 IsRegCR
= PPC::CRRCRegClass
.contains(Reg
) ||
197 PPC::CRBITRCRegClass
.contains(Reg
);
200 if (UseMI
.isBranch() && IsRegCR
) {
202 Latency
= getInstrLatency(ItinData
, DefMI
);
204 // On some cores, there is an additional delay between writing to a condition
205 // register, and using it from a branch.
206 unsigned Directive
= Subtarget
.getDarwinDirective();
220 // FIXME: Is this needed for POWER9?
229 // This function does not list all associative and commutative operations, but
230 // only those worth feeding through the machine combiner in an attempt to
231 // reduce the critical path. Mostly, this means floating-point operations,
232 // because they have high latencies (compared to other operations, such and
233 // and/or, which are also associative and commutative, but have low latencies).
234 bool PPCInstrInfo::isAssociativeAndCommutative(const MachineInstr
&Inst
) const {
235 switch (Inst
.getOpcode()) {
268 bool PPCInstrInfo::getMachineCombinerPatterns(
270 SmallVectorImpl
<MachineCombinerPattern
> &Patterns
) const {
271 // Using the machine combiner in this way is potentially expensive, so
272 // restrict to when aggressive optimizations are desired.
273 if (Subtarget
.getTargetMachine().getOptLevel() != CodeGenOpt::Aggressive
)
276 // FP reassociation is only legal when we don't need strict IEEE semantics.
277 if (!Root
.getParent()->getParent()->getTarget().Options
.UnsafeFPMath
)
280 return TargetInstrInfo::getMachineCombinerPatterns(Root
, Patterns
);
283 // Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
284 bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr
&MI
,
285 unsigned &SrcReg
, unsigned &DstReg
,
286 unsigned &SubIdx
) const {
287 switch (MI
.getOpcode()) {
288 default: return false;
291 case PPC::EXTSW_32_64
:
292 SrcReg
= MI
.getOperand(1).getReg();
293 DstReg
= MI
.getOperand(0).getReg();
294 SubIdx
= PPC::sub_32
;
299 unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr
&MI
,
300 int &FrameIndex
) const {
301 unsigned Opcode
= MI
.getOpcode();
302 const unsigned *OpcodesForSpill
= getLoadOpcodesForSpillArray();
303 const unsigned *End
= OpcodesForSpill
+ SOK_LastOpcodeSpill
;
305 if (End
!= std::find(OpcodesForSpill
, End
, Opcode
)) {
306 // Check for the operands added by addFrameReference (the immediate is the
307 // offset which defaults to 0).
308 if (MI
.getOperand(1).isImm() && !MI
.getOperand(1).getImm() &&
309 MI
.getOperand(2).isFI()) {
310 FrameIndex
= MI
.getOperand(2).getIndex();
311 return MI
.getOperand(0).getReg();
317 // For opcodes with the ReMaterializable flag set, this function is called to
318 // verify the instruction is really rematable.
319 bool PPCInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr
&MI
,
320 AliasAnalysis
*AA
) const {
321 switch (MI
.getOpcode()) {
323 // This function should only be called for opcodes with the ReMaterializable
325 llvm_unreachable("Unknown rematerializable operation!");
332 case PPC::ADDIStocHA
:
334 case PPC::LOAD_STACK_GUARD
:
340 unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr
&MI
,
341 int &FrameIndex
) const {
342 unsigned Opcode
= MI
.getOpcode();
343 const unsigned *OpcodesForSpill
= getStoreOpcodesForSpillArray();
344 const unsigned *End
= OpcodesForSpill
+ SOK_LastOpcodeSpill
;
346 if (End
!= std::find(OpcodesForSpill
, End
, Opcode
)) {
347 if (MI
.getOperand(1).isImm() && !MI
.getOperand(1).getImm() &&
348 MI
.getOperand(2).isFI()) {
349 FrameIndex
= MI
.getOperand(2).getIndex();
350 return MI
.getOperand(0).getReg();
356 MachineInstr
*PPCInstrInfo::commuteInstructionImpl(MachineInstr
&MI
, bool NewMI
,
358 unsigned OpIdx2
) const {
359 MachineFunction
&MF
= *MI
.getParent()->getParent();
361 // Normal instructions can be commuted the obvious way.
362 if (MI
.getOpcode() != PPC::RLWIMI
&& MI
.getOpcode() != PPC::RLWIMIo
)
363 return TargetInstrInfo::commuteInstructionImpl(MI
, NewMI
, OpIdx1
, OpIdx2
);
364 // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a
365 // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because
366 // changing the relative order of the mask operands might change what happens
367 // to the high-bits of the mask (and, thus, the result).
369 // Cannot commute if it has a non-zero rotate count.
370 if (MI
.getOperand(3).getImm() != 0)
373 // If we have a zero rotate count, we have:
375 // Op0 = (Op1 & ~M) | (Op2 & M)
377 // M = mask((ME+1)&31, (MB-1)&31)
378 // Op0 = (Op2 & ~M) | (Op1 & M)
381 assert(((OpIdx1
== 1 && OpIdx2
== 2) || (OpIdx1
== 2 && OpIdx2
== 1)) &&
382 "Only the operands 1 and 2 can be swapped in RLSIMI/RLWIMIo.");
383 unsigned Reg0
= MI
.getOperand(0).getReg();
384 unsigned Reg1
= MI
.getOperand(1).getReg();
385 unsigned Reg2
= MI
.getOperand(2).getReg();
386 unsigned SubReg1
= MI
.getOperand(1).getSubReg();
387 unsigned SubReg2
= MI
.getOperand(2).getSubReg();
388 bool Reg1IsKill
= MI
.getOperand(1).isKill();
389 bool Reg2IsKill
= MI
.getOperand(2).isKill();
390 bool ChangeReg0
= false;
391 // If machine instrs are no longer in two-address forms, update
392 // destination register as well.
394 // Must be two address instruction!
395 assert(MI
.getDesc().getOperandConstraint(0, MCOI::TIED_TO
) &&
396 "Expecting a two-address instruction!");
397 assert(MI
.getOperand(0).getSubReg() == SubReg1
&& "Tied subreg mismatch");
403 unsigned MB
= MI
.getOperand(4).getImm();
404 unsigned ME
= MI
.getOperand(5).getImm();
406 // We can't commute a trivial mask (there is no way to represent an all-zero
408 if (MB
== 0 && ME
== 31)
412 // Create a new instruction.
413 unsigned Reg0
= ChangeReg0
? Reg2
: MI
.getOperand(0).getReg();
414 bool Reg0IsDead
= MI
.getOperand(0).isDead();
415 return BuildMI(MF
, MI
.getDebugLoc(), MI
.getDesc())
416 .addReg(Reg0
, RegState::Define
| getDeadRegState(Reg0IsDead
))
417 .addReg(Reg2
, getKillRegState(Reg2IsKill
))
418 .addReg(Reg1
, getKillRegState(Reg1IsKill
))
419 .addImm((ME
+ 1) & 31)
420 .addImm((MB
- 1) & 31);
424 MI
.getOperand(0).setReg(Reg2
);
425 MI
.getOperand(0).setSubReg(SubReg2
);
427 MI
.getOperand(2).setReg(Reg1
);
428 MI
.getOperand(1).setReg(Reg2
);
429 MI
.getOperand(2).setSubReg(SubReg1
);
430 MI
.getOperand(1).setSubReg(SubReg2
);
431 MI
.getOperand(2).setIsKill(Reg1IsKill
);
432 MI
.getOperand(1).setIsKill(Reg2IsKill
);
434 // Swap the mask around.
435 MI
.getOperand(4).setImm((ME
+ 1) & 31);
436 MI
.getOperand(5).setImm((MB
- 1) & 31);
440 bool PPCInstrInfo::findCommutedOpIndices(MachineInstr
&MI
, unsigned &SrcOpIdx1
,
441 unsigned &SrcOpIdx2
) const {
442 // For VSX A-Type FMA instructions, it is the first two operands that can be
443 // commuted, however, because the non-encoded tied input operand is listed
444 // first, the operands to swap are actually the second and third.
446 int AltOpc
= PPC::getAltVSXFMAOpcode(MI
.getOpcode());
448 return TargetInstrInfo::findCommutedOpIndices(MI
, SrcOpIdx1
, SrcOpIdx2
);
450 // The commutable operand indices are 2 and 3. Return them in SrcOpIdx1
452 return fixCommutedOpIndices(SrcOpIdx1
, SrcOpIdx2
, 2, 3);
455 void PPCInstrInfo::insertNoop(MachineBasicBlock
&MBB
,
456 MachineBasicBlock::iterator MI
) const {
457 // This function is used for scheduling, and the nop wanted here is the type
458 // that terminates dispatch groups on the POWER cores.
459 unsigned Directive
= Subtarget
.getDarwinDirective();
462 default: Opcode
= PPC::NOP
; break;
463 case PPC::DIR_PWR6
: Opcode
= PPC::NOP_GT_PWR6
; break;
464 case PPC::DIR_PWR7
: Opcode
= PPC::NOP_GT_PWR7
; break;
465 case PPC::DIR_PWR8
: Opcode
= PPC::NOP_GT_PWR7
; break; /* FIXME: Update when P8 InstrScheduling model is ready */
466 // FIXME: Update when POWER9 scheduling model is ready.
467 case PPC::DIR_PWR9
: Opcode
= PPC::NOP_GT_PWR7
; break;
471 BuildMI(MBB
, MI
, DL
, get(Opcode
));
474 /// Return the noop instruction to use for a noop.
475 void PPCInstrInfo::getNoop(MCInst
&NopInst
) const {
476 NopInst
.setOpcode(PPC::NOP
);
480 // Note: If the condition register is set to CTR or CTR8 then this is a
481 // BDNZ (imm == 1) or BDZ (imm == 0) branch.
482 bool PPCInstrInfo::analyzeBranch(MachineBasicBlock
&MBB
,
483 MachineBasicBlock
*&TBB
,
484 MachineBasicBlock
*&FBB
,
485 SmallVectorImpl
<MachineOperand
> &Cond
,
486 bool AllowModify
) const {
487 bool isPPC64
= Subtarget
.isPPC64();
489 // If the block has no terminators, it just falls into the block after it.
490 MachineBasicBlock::iterator I
= MBB
.getLastNonDebugInstr();
494 if (!isUnpredicatedTerminator(*I
))
498 // If the BB ends with an unconditional branch to the fallthrough BB,
499 // we eliminate the branch instruction.
500 if (I
->getOpcode() == PPC::B
&&
501 MBB
.isLayoutSuccessor(I
->getOperand(0).getMBB())) {
502 I
->eraseFromParent();
504 // We update iterator after deleting the last branch.
505 I
= MBB
.getLastNonDebugInstr();
506 if (I
== MBB
.end() || !isUnpredicatedTerminator(*I
))
511 // Get the last instruction in the block.
512 MachineInstr
&LastInst
= *I
;
514 // If there is only one terminator instruction, process it.
515 if (I
== MBB
.begin() || !isUnpredicatedTerminator(*--I
)) {
516 if (LastInst
.getOpcode() == PPC::B
) {
517 if (!LastInst
.getOperand(0).isMBB())
519 TBB
= LastInst
.getOperand(0).getMBB();
521 } else if (LastInst
.getOpcode() == PPC::BCC
) {
522 if (!LastInst
.getOperand(2).isMBB())
524 // Block ends with fall-through condbranch.
525 TBB
= LastInst
.getOperand(2).getMBB();
526 Cond
.push_back(LastInst
.getOperand(0));
527 Cond
.push_back(LastInst
.getOperand(1));
529 } else if (LastInst
.getOpcode() == PPC::BC
) {
530 if (!LastInst
.getOperand(1).isMBB())
532 // Block ends with fall-through condbranch.
533 TBB
= LastInst
.getOperand(1).getMBB();
534 Cond
.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET
));
535 Cond
.push_back(LastInst
.getOperand(0));
537 } else if (LastInst
.getOpcode() == PPC::BCn
) {
538 if (!LastInst
.getOperand(1).isMBB())
540 // Block ends with fall-through condbranch.
541 TBB
= LastInst
.getOperand(1).getMBB();
542 Cond
.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET
));
543 Cond
.push_back(LastInst
.getOperand(0));
545 } else if (LastInst
.getOpcode() == PPC::BDNZ8
||
546 LastInst
.getOpcode() == PPC::BDNZ
) {
547 if (!LastInst
.getOperand(0).isMBB())
549 if (DisableCTRLoopAnal
)
551 TBB
= LastInst
.getOperand(0).getMBB();
552 Cond
.push_back(MachineOperand::CreateImm(1));
553 Cond
.push_back(MachineOperand::CreateReg(isPPC64
? PPC::CTR8
: PPC::CTR
,
556 } else if (LastInst
.getOpcode() == PPC::BDZ8
||
557 LastInst
.getOpcode() == PPC::BDZ
) {
558 if (!LastInst
.getOperand(0).isMBB())
560 if (DisableCTRLoopAnal
)
562 TBB
= LastInst
.getOperand(0).getMBB();
563 Cond
.push_back(MachineOperand::CreateImm(0));
564 Cond
.push_back(MachineOperand::CreateReg(isPPC64
? PPC::CTR8
: PPC::CTR
,
569 // Otherwise, don't know what this is.
573 // Get the instruction before it if it's a terminator.
574 MachineInstr
&SecondLastInst
= *I
;
576 // If there are three terminators, we don't know what sort of block this is.
577 if (I
!= MBB
.begin() && isUnpredicatedTerminator(*--I
))
580 // If the block ends with PPC::B and PPC:BCC, handle it.
581 if (SecondLastInst
.getOpcode() == PPC::BCC
&&
582 LastInst
.getOpcode() == PPC::B
) {
583 if (!SecondLastInst
.getOperand(2).isMBB() ||
584 !LastInst
.getOperand(0).isMBB())
586 TBB
= SecondLastInst
.getOperand(2).getMBB();
587 Cond
.push_back(SecondLastInst
.getOperand(0));
588 Cond
.push_back(SecondLastInst
.getOperand(1));
589 FBB
= LastInst
.getOperand(0).getMBB();
591 } else if (SecondLastInst
.getOpcode() == PPC::BC
&&
592 LastInst
.getOpcode() == PPC::B
) {
593 if (!SecondLastInst
.getOperand(1).isMBB() ||
594 !LastInst
.getOperand(0).isMBB())
596 TBB
= SecondLastInst
.getOperand(1).getMBB();
597 Cond
.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET
));
598 Cond
.push_back(SecondLastInst
.getOperand(0));
599 FBB
= LastInst
.getOperand(0).getMBB();
601 } else if (SecondLastInst
.getOpcode() == PPC::BCn
&&
602 LastInst
.getOpcode() == PPC::B
) {
603 if (!SecondLastInst
.getOperand(1).isMBB() ||
604 !LastInst
.getOperand(0).isMBB())
606 TBB
= SecondLastInst
.getOperand(1).getMBB();
607 Cond
.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET
));
608 Cond
.push_back(SecondLastInst
.getOperand(0));
609 FBB
= LastInst
.getOperand(0).getMBB();
611 } else if ((SecondLastInst
.getOpcode() == PPC::BDNZ8
||
612 SecondLastInst
.getOpcode() == PPC::BDNZ
) &&
613 LastInst
.getOpcode() == PPC::B
) {
614 if (!SecondLastInst
.getOperand(0).isMBB() ||
615 !LastInst
.getOperand(0).isMBB())
617 if (DisableCTRLoopAnal
)
619 TBB
= SecondLastInst
.getOperand(0).getMBB();
620 Cond
.push_back(MachineOperand::CreateImm(1));
621 Cond
.push_back(MachineOperand::CreateReg(isPPC64
? PPC::CTR8
: PPC::CTR
,
623 FBB
= LastInst
.getOperand(0).getMBB();
625 } else if ((SecondLastInst
.getOpcode() == PPC::BDZ8
||
626 SecondLastInst
.getOpcode() == PPC::BDZ
) &&
627 LastInst
.getOpcode() == PPC::B
) {
628 if (!SecondLastInst
.getOperand(0).isMBB() ||
629 !LastInst
.getOperand(0).isMBB())
631 if (DisableCTRLoopAnal
)
633 TBB
= SecondLastInst
.getOperand(0).getMBB();
634 Cond
.push_back(MachineOperand::CreateImm(0));
635 Cond
.push_back(MachineOperand::CreateReg(isPPC64
? PPC::CTR8
: PPC::CTR
,
637 FBB
= LastInst
.getOperand(0).getMBB();
641 // If the block ends with two PPC:Bs, handle it. The second one is not
642 // executed, so remove it.
643 if (SecondLastInst
.getOpcode() == PPC::B
&& LastInst
.getOpcode() == PPC::B
) {
644 if (!SecondLastInst
.getOperand(0).isMBB())
646 TBB
= SecondLastInst
.getOperand(0).getMBB();
649 I
->eraseFromParent();
653 // Otherwise, can't handle this.
657 unsigned PPCInstrInfo::removeBranch(MachineBasicBlock
&MBB
,
658 int *BytesRemoved
) const {
659 assert(!BytesRemoved
&& "code size not handled");
661 MachineBasicBlock::iterator I
= MBB
.getLastNonDebugInstr();
665 if (I
->getOpcode() != PPC::B
&& I
->getOpcode() != PPC::BCC
&&
666 I
->getOpcode() != PPC::BC
&& I
->getOpcode() != PPC::BCn
&&
667 I
->getOpcode() != PPC::BDNZ8
&& I
->getOpcode() != PPC::BDNZ
&&
668 I
->getOpcode() != PPC::BDZ8
&& I
->getOpcode() != PPC::BDZ
)
671 // Remove the branch.
672 I
->eraseFromParent();
676 if (I
== MBB
.begin()) return 1;
678 if (I
->getOpcode() != PPC::BCC
&&
679 I
->getOpcode() != PPC::BC
&& I
->getOpcode() != PPC::BCn
&&
680 I
->getOpcode() != PPC::BDNZ8
&& I
->getOpcode() != PPC::BDNZ
&&
681 I
->getOpcode() != PPC::BDZ8
&& I
->getOpcode() != PPC::BDZ
)
684 // Remove the branch.
685 I
->eraseFromParent();
689 unsigned PPCInstrInfo::insertBranch(MachineBasicBlock
&MBB
,
690 MachineBasicBlock
*TBB
,
691 MachineBasicBlock
*FBB
,
692 ArrayRef
<MachineOperand
> Cond
,
694 int *BytesAdded
) const {
695 // Shouldn't be a fall through.
696 assert(TBB
&& "insertBranch must not be told to insert a fallthrough");
697 assert((Cond
.size() == 2 || Cond
.size() == 0) &&
698 "PPC branch conditions have two components!");
699 assert(!BytesAdded
&& "code size not handled");
701 bool isPPC64
= Subtarget
.isPPC64();
705 if (Cond
.empty()) // Unconditional branch
706 BuildMI(&MBB
, DL
, get(PPC::B
)).addMBB(TBB
);
707 else if (Cond
[1].getReg() == PPC::CTR
|| Cond
[1].getReg() == PPC::CTR8
)
708 BuildMI(&MBB
, DL
, get(Cond
[0].getImm() ?
709 (isPPC64
? PPC::BDNZ8
: PPC::BDNZ
) :
710 (isPPC64
? PPC::BDZ8
: PPC::BDZ
))).addMBB(TBB
);
711 else if (Cond
[0].getImm() == PPC::PRED_BIT_SET
)
712 BuildMI(&MBB
, DL
, get(PPC::BC
)).add(Cond
[1]).addMBB(TBB
);
713 else if (Cond
[0].getImm() == PPC::PRED_BIT_UNSET
)
714 BuildMI(&MBB
, DL
, get(PPC::BCn
)).add(Cond
[1]).addMBB(TBB
);
715 else // Conditional branch
716 BuildMI(&MBB
, DL
, get(PPC::BCC
))
717 .addImm(Cond
[0].getImm())
723 // Two-way Conditional Branch.
724 if (Cond
[1].getReg() == PPC::CTR
|| Cond
[1].getReg() == PPC::CTR8
)
725 BuildMI(&MBB
, DL
, get(Cond
[0].getImm() ?
726 (isPPC64
? PPC::BDNZ8
: PPC::BDNZ
) :
727 (isPPC64
? PPC::BDZ8
: PPC::BDZ
))).addMBB(TBB
);
728 else if (Cond
[0].getImm() == PPC::PRED_BIT_SET
)
729 BuildMI(&MBB
, DL
, get(PPC::BC
)).add(Cond
[1]).addMBB(TBB
);
730 else if (Cond
[0].getImm() == PPC::PRED_BIT_UNSET
)
731 BuildMI(&MBB
, DL
, get(PPC::BCn
)).add(Cond
[1]).addMBB(TBB
);
733 BuildMI(&MBB
, DL
, get(PPC::BCC
))
734 .addImm(Cond
[0].getImm())
737 BuildMI(&MBB
, DL
, get(PPC::B
)).addMBB(FBB
);
742 bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock
&MBB
,
743 ArrayRef
<MachineOperand
> Cond
,
744 unsigned TrueReg
, unsigned FalseReg
,
745 int &CondCycles
, int &TrueCycles
, int &FalseCycles
) const {
746 if (Cond
.size() != 2)
749 // If this is really a bdnz-like condition, then it cannot be turned into a
751 if (Cond
[1].getReg() == PPC::CTR
|| Cond
[1].getReg() == PPC::CTR8
)
754 // Check register classes.
755 const MachineRegisterInfo
&MRI
= MBB
.getParent()->getRegInfo();
756 const TargetRegisterClass
*RC
=
757 RI
.getCommonSubClass(MRI
.getRegClass(TrueReg
), MRI
.getRegClass(FalseReg
));
761 // isel is for regular integer GPRs only.
762 if (!PPC::GPRCRegClass
.hasSubClassEq(RC
) &&
763 !PPC::GPRC_NOR0RegClass
.hasSubClassEq(RC
) &&
764 !PPC::G8RCRegClass
.hasSubClassEq(RC
) &&
765 !PPC::G8RC_NOX0RegClass
.hasSubClassEq(RC
))
768 // FIXME: These numbers are for the A2, how well they work for other cores is
769 // an open question. On the A2, the isel instruction has a 2-cycle latency
770 // but single-cycle throughput. These numbers are used in combination with
771 // the MispredictPenalty setting from the active SchedMachineModel.
779 void PPCInstrInfo::insertSelect(MachineBasicBlock
&MBB
,
780 MachineBasicBlock::iterator MI
,
781 const DebugLoc
&dl
, unsigned DestReg
,
782 ArrayRef
<MachineOperand
> Cond
, unsigned TrueReg
,
783 unsigned FalseReg
) const {
784 assert(Cond
.size() == 2 &&
785 "PPC branch conditions have two components!");
787 // Get the register classes.
788 MachineRegisterInfo
&MRI
= MBB
.getParent()->getRegInfo();
789 const TargetRegisterClass
*RC
=
790 RI
.getCommonSubClass(MRI
.getRegClass(TrueReg
), MRI
.getRegClass(FalseReg
));
791 assert(RC
&& "TrueReg and FalseReg must have overlapping register classes");
793 bool Is64Bit
= PPC::G8RCRegClass
.hasSubClassEq(RC
) ||
794 PPC::G8RC_NOX0RegClass
.hasSubClassEq(RC
);
796 PPC::GPRCRegClass
.hasSubClassEq(RC
) ||
797 PPC::GPRC_NOR0RegClass
.hasSubClassEq(RC
)) &&
798 "isel is for regular integer GPRs only");
800 unsigned OpCode
= Is64Bit
? PPC::ISEL8
: PPC::ISEL
;
801 auto SelectPred
= static_cast<PPC::Predicate
>(Cond
[0].getImm());
804 bool SwapOps
= false;
805 switch (SelectPred
) {
807 case PPC::PRED_EQ_MINUS
:
808 case PPC::PRED_EQ_PLUS
:
809 SubIdx
= PPC::sub_eq
; SwapOps
= false; break;
811 case PPC::PRED_NE_MINUS
:
812 case PPC::PRED_NE_PLUS
:
813 SubIdx
= PPC::sub_eq
; SwapOps
= true; break;
815 case PPC::PRED_LT_MINUS
:
816 case PPC::PRED_LT_PLUS
:
817 SubIdx
= PPC::sub_lt
; SwapOps
= false; break;
819 case PPC::PRED_GE_MINUS
:
820 case PPC::PRED_GE_PLUS
:
821 SubIdx
= PPC::sub_lt
; SwapOps
= true; break;
823 case PPC::PRED_GT_MINUS
:
824 case PPC::PRED_GT_PLUS
:
825 SubIdx
= PPC::sub_gt
; SwapOps
= false; break;
827 case PPC::PRED_LE_MINUS
:
828 case PPC::PRED_LE_PLUS
:
829 SubIdx
= PPC::sub_gt
; SwapOps
= true; break;
831 case PPC::PRED_UN_MINUS
:
832 case PPC::PRED_UN_PLUS
:
833 SubIdx
= PPC::sub_un
; SwapOps
= false; break;
835 case PPC::PRED_NU_MINUS
:
836 case PPC::PRED_NU_PLUS
:
837 SubIdx
= PPC::sub_un
; SwapOps
= true; break;
838 case PPC::PRED_BIT_SET
: SubIdx
= 0; SwapOps
= false; break;
839 case PPC::PRED_BIT_UNSET
: SubIdx
= 0; SwapOps
= true; break;
842 unsigned FirstReg
= SwapOps
? FalseReg
: TrueReg
,
843 SecondReg
= SwapOps
? TrueReg
: FalseReg
;
845 // The first input register of isel cannot be r0. If it is a member
846 // of a register class that can be r0, then copy it first (the
847 // register allocator should eliminate the copy).
848 if (MRI
.getRegClass(FirstReg
)->contains(PPC::R0
) ||
849 MRI
.getRegClass(FirstReg
)->contains(PPC::X0
)) {
850 const TargetRegisterClass
*FirstRC
=
851 MRI
.getRegClass(FirstReg
)->contains(PPC::X0
) ?
852 &PPC::G8RC_NOX0RegClass
: &PPC::GPRC_NOR0RegClass
;
853 unsigned OldFirstReg
= FirstReg
;
854 FirstReg
= MRI
.createVirtualRegister(FirstRC
);
855 BuildMI(MBB
, MI
, dl
, get(TargetOpcode::COPY
), FirstReg
)
856 .addReg(OldFirstReg
);
859 BuildMI(MBB
, MI
, dl
, get(OpCode
), DestReg
)
860 .addReg(FirstReg
).addReg(SecondReg
)
861 .addReg(Cond
[1].getReg(), 0, SubIdx
);
864 static unsigned getCRBitValue(unsigned CRBit
) {
866 if (CRBit
== PPC::CR0LT
|| CRBit
== PPC::CR1LT
||
867 CRBit
== PPC::CR2LT
|| CRBit
== PPC::CR3LT
||
868 CRBit
== PPC::CR4LT
|| CRBit
== PPC::CR5LT
||
869 CRBit
== PPC::CR6LT
|| CRBit
== PPC::CR7LT
)
871 if (CRBit
== PPC::CR0GT
|| CRBit
== PPC::CR1GT
||
872 CRBit
== PPC::CR2GT
|| CRBit
== PPC::CR3GT
||
873 CRBit
== PPC::CR4GT
|| CRBit
== PPC::CR5GT
||
874 CRBit
== PPC::CR6GT
|| CRBit
== PPC::CR7GT
)
876 if (CRBit
== PPC::CR0EQ
|| CRBit
== PPC::CR1EQ
||
877 CRBit
== PPC::CR2EQ
|| CRBit
== PPC::CR3EQ
||
878 CRBit
== PPC::CR4EQ
|| CRBit
== PPC::CR5EQ
||
879 CRBit
== PPC::CR6EQ
|| CRBit
== PPC::CR7EQ
)
881 if (CRBit
== PPC::CR0UN
|| CRBit
== PPC::CR1UN
||
882 CRBit
== PPC::CR2UN
|| CRBit
== PPC::CR3UN
||
883 CRBit
== PPC::CR4UN
|| CRBit
== PPC::CR5UN
||
884 CRBit
== PPC::CR6UN
|| CRBit
== PPC::CR7UN
)
887 assert(Ret
!= 4 && "Invalid CR bit register");
891 void PPCInstrInfo::copyPhysReg(MachineBasicBlock
&MBB
,
892 MachineBasicBlock::iterator I
,
893 const DebugLoc
&DL
, unsigned DestReg
,
894 unsigned SrcReg
, bool KillSrc
) const {
895 // We can end up with self copies and similar things as a result of VSX copy
896 // legalization. Promote them here.
897 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
898 if (PPC::F8RCRegClass
.contains(DestReg
) &&
899 PPC::VSRCRegClass
.contains(SrcReg
)) {
901 TRI
->getMatchingSuperReg(DestReg
, PPC::sub_64
, &PPC::VSRCRegClass
);
903 if (VSXSelfCopyCrash
&& SrcReg
== SuperReg
)
904 llvm_unreachable("nop VSX copy");
907 } else if (PPC::F8RCRegClass
.contains(SrcReg
) &&
908 PPC::VSRCRegClass
.contains(DestReg
)) {
910 TRI
->getMatchingSuperReg(SrcReg
, PPC::sub_64
, &PPC::VSRCRegClass
);
912 if (VSXSelfCopyCrash
&& DestReg
== SuperReg
)
913 llvm_unreachable("nop VSX copy");
918 // Different class register copy
919 if (PPC::CRBITRCRegClass
.contains(SrcReg
) &&
920 PPC::GPRCRegClass
.contains(DestReg
)) {
921 unsigned CRReg
= getCRFromCRBit(SrcReg
);
922 BuildMI(MBB
, I
, DL
, get(PPC::MFOCRF
), DestReg
).addReg(CRReg
);
923 getKillRegState(KillSrc
);
924 // Rotate the CR bit in the CR fields to be the least significant bit and
925 // then mask with 0x1 (MB = ME = 31).
926 BuildMI(MBB
, I
, DL
, get(PPC::RLWINM
), DestReg
)
927 .addReg(DestReg
, RegState::Kill
)
928 .addImm(TRI
->getEncodingValue(CRReg
) * 4 + (4 - getCRBitValue(SrcReg
)))
932 } else if (PPC::CRRCRegClass
.contains(SrcReg
) &&
933 PPC::G8RCRegClass
.contains(DestReg
)) {
934 BuildMI(MBB
, I
, DL
, get(PPC::MFOCRF8
), DestReg
).addReg(SrcReg
);
935 getKillRegState(KillSrc
);
937 } else if (PPC::CRRCRegClass
.contains(SrcReg
) &&
938 PPC::GPRCRegClass
.contains(DestReg
)) {
939 BuildMI(MBB
, I
, DL
, get(PPC::MFOCRF
), DestReg
).addReg(SrcReg
);
940 getKillRegState(KillSrc
);
942 } else if (PPC::G8RCRegClass
.contains(SrcReg
) &&
943 PPC::VSFRCRegClass
.contains(DestReg
)) {
944 BuildMI(MBB
, I
, DL
, get(PPC::MTVSRD
), DestReg
).addReg(SrcReg
);
946 getKillRegState(KillSrc
);
948 } else if (PPC::VSFRCRegClass
.contains(SrcReg
) &&
949 PPC::G8RCRegClass
.contains(DestReg
)) {
950 BuildMI(MBB
, I
, DL
, get(PPC::MFVSRD
), DestReg
).addReg(SrcReg
);
951 getKillRegState(KillSrc
);
953 } else if (PPC::SPERCRegClass
.contains(SrcReg
) &&
954 PPC::SPE4RCRegClass
.contains(DestReg
)) {
955 BuildMI(MBB
, I
, DL
, get(PPC::EFSCFD
), DestReg
).addReg(SrcReg
);
956 getKillRegState(KillSrc
);
958 } else if (PPC::SPE4RCRegClass
.contains(SrcReg
) &&
959 PPC::SPERCRegClass
.contains(DestReg
)) {
960 BuildMI(MBB
, I
, DL
, get(PPC::EFDCFS
), DestReg
).addReg(SrcReg
);
961 getKillRegState(KillSrc
);
967 if (PPC::GPRCRegClass
.contains(DestReg
, SrcReg
))
969 else if (PPC::G8RCRegClass
.contains(DestReg
, SrcReg
))
971 else if (PPC::F4RCRegClass
.contains(DestReg
, SrcReg
))
973 else if (PPC::CRRCRegClass
.contains(DestReg
, SrcReg
))
975 else if (PPC::VRRCRegClass
.contains(DestReg
, SrcReg
))
977 else if (PPC::VSRCRegClass
.contains(DestReg
, SrcReg
))
978 // There are two different ways this can be done:
979 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
980 // issue in VSU pipeline 0.
981 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
982 // can go to either pipeline.
983 // We'll always use xxlor here, because in practically all cases where
984 // copies are generated, they are close enough to some use that the
985 // lower-latency form is preferable.
987 else if (PPC::VSFRCRegClass
.contains(DestReg
, SrcReg
) ||
988 PPC::VSSRCRegClass
.contains(DestReg
, SrcReg
))
989 Opc
= (Subtarget
.hasP9Vector()) ? PPC::XSCPSGNDP
: PPC::XXLORf
;
990 else if (PPC::QFRCRegClass
.contains(DestReg
, SrcReg
))
992 else if (PPC::QSRCRegClass
.contains(DestReg
, SrcReg
))
994 else if (PPC::QBRCRegClass
.contains(DestReg
, SrcReg
))
996 else if (PPC::CRBITRCRegClass
.contains(DestReg
, SrcReg
))
998 else if (PPC::SPERCRegClass
.contains(DestReg
, SrcReg
))
1001 llvm_unreachable("Impossible reg-to-reg copy");
1003 const MCInstrDesc
&MCID
= get(Opc
);
1004 if (MCID
.getNumOperands() == 3)
1005 BuildMI(MBB
, I
, DL
, MCID
, DestReg
)
1006 .addReg(SrcReg
).addReg(SrcReg
, getKillRegState(KillSrc
));
1008 BuildMI(MBB
, I
, DL
, MCID
, DestReg
).addReg(SrcReg
, getKillRegState(KillSrc
));
1011 unsigned PPCInstrInfo::getStoreOpcodeForSpill(unsigned Reg
,
1012 const TargetRegisterClass
*RC
)
1014 const unsigned *OpcodesForSpill
= getStoreOpcodesForSpillArray();
1015 int OpcodeIndex
= 0;
1017 if (RC
!= nullptr) {
1018 if (PPC::GPRCRegClass
.hasSubClassEq(RC
) ||
1019 PPC::GPRC_NOR0RegClass
.hasSubClassEq(RC
)) {
1020 OpcodeIndex
= SOK_Int4Spill
;
1021 } else if (PPC::G8RCRegClass
.hasSubClassEq(RC
) ||
1022 PPC::G8RC_NOX0RegClass
.hasSubClassEq(RC
)) {
1023 OpcodeIndex
= SOK_Int8Spill
;
1024 } else if (PPC::F8RCRegClass
.hasSubClassEq(RC
)) {
1025 OpcodeIndex
= SOK_Float8Spill
;
1026 } else if (PPC::F4RCRegClass
.hasSubClassEq(RC
)) {
1027 OpcodeIndex
= SOK_Float4Spill
;
1028 } else if (PPC::SPERCRegClass
.hasSubClassEq(RC
)) {
1029 OpcodeIndex
= SOK_SPESpill
;
1030 } else if (PPC::SPE4RCRegClass
.hasSubClassEq(RC
)) {
1031 OpcodeIndex
= SOK_SPE4Spill
;
1032 } else if (PPC::CRRCRegClass
.hasSubClassEq(RC
)) {
1033 OpcodeIndex
= SOK_CRSpill
;
1034 } else if (PPC::CRBITRCRegClass
.hasSubClassEq(RC
)) {
1035 OpcodeIndex
= SOK_CRBitSpill
;
1036 } else if (PPC::VRRCRegClass
.hasSubClassEq(RC
)) {
1037 OpcodeIndex
= SOK_VRVectorSpill
;
1038 } else if (PPC::VSRCRegClass
.hasSubClassEq(RC
)) {
1039 OpcodeIndex
= SOK_VSXVectorSpill
;
1040 } else if (PPC::VSFRCRegClass
.hasSubClassEq(RC
)) {
1041 OpcodeIndex
= SOK_VectorFloat8Spill
;
1042 } else if (PPC::VSSRCRegClass
.hasSubClassEq(RC
)) {
1043 OpcodeIndex
= SOK_VectorFloat4Spill
;
1044 } else if (PPC::VRSAVERCRegClass
.hasSubClassEq(RC
)) {
1045 OpcodeIndex
= SOK_VRSaveSpill
;
1046 } else if (PPC::QFRCRegClass
.hasSubClassEq(RC
)) {
1047 OpcodeIndex
= SOK_QuadFloat8Spill
;
1048 } else if (PPC::QSRCRegClass
.hasSubClassEq(RC
)) {
1049 OpcodeIndex
= SOK_QuadFloat4Spill
;
1050 } else if (PPC::QBRCRegClass
.hasSubClassEq(RC
)) {
1051 OpcodeIndex
= SOK_QuadBitSpill
;
1052 } else if (PPC::SPILLTOVSRRCRegClass
.hasSubClassEq(RC
)) {
1053 OpcodeIndex
= SOK_SpillToVSR
;
1055 llvm_unreachable("Unknown regclass!");
1058 if (PPC::GPRCRegClass
.contains(Reg
) ||
1059 PPC::GPRC_NOR0RegClass
.contains(Reg
)) {
1060 OpcodeIndex
= SOK_Int4Spill
;
1061 } else if (PPC::G8RCRegClass
.contains(Reg
) ||
1062 PPC::G8RC_NOX0RegClass
.contains(Reg
)) {
1063 OpcodeIndex
= SOK_Int8Spill
;
1064 } else if (PPC::F8RCRegClass
.contains(Reg
)) {
1065 OpcodeIndex
= SOK_Float8Spill
;
1066 } else if (PPC::F4RCRegClass
.contains(Reg
)) {
1067 OpcodeIndex
= SOK_Float4Spill
;
1068 } else if (PPC::CRRCRegClass
.contains(Reg
)) {
1069 OpcodeIndex
= SOK_CRSpill
;
1070 } else if (PPC::CRBITRCRegClass
.contains(Reg
)) {
1071 OpcodeIndex
= SOK_CRBitSpill
;
1072 } else if (PPC::VRRCRegClass
.contains(Reg
)) {
1073 OpcodeIndex
= SOK_VRVectorSpill
;
1074 } else if (PPC::VSRCRegClass
.contains(Reg
)) {
1075 OpcodeIndex
= SOK_VSXVectorSpill
;
1076 } else if (PPC::VSFRCRegClass
.contains(Reg
)) {
1077 OpcodeIndex
= SOK_VectorFloat8Spill
;
1078 } else if (PPC::VSSRCRegClass
.contains(Reg
)) {
1079 OpcodeIndex
= SOK_VectorFloat4Spill
;
1080 } else if (PPC::VRSAVERCRegClass
.contains(Reg
)) {
1081 OpcodeIndex
= SOK_VRSaveSpill
;
1082 } else if (PPC::QFRCRegClass
.contains(Reg
)) {
1083 OpcodeIndex
= SOK_QuadFloat8Spill
;
1084 } else if (PPC::QSRCRegClass
.contains(Reg
)) {
1085 OpcodeIndex
= SOK_QuadFloat4Spill
;
1086 } else if (PPC::QBRCRegClass
.contains(Reg
)) {
1087 OpcodeIndex
= SOK_QuadBitSpill
;
1088 } else if (PPC::SPILLTOVSRRCRegClass
.contains(Reg
)) {
1089 OpcodeIndex
= SOK_SpillToVSR
;
1091 llvm_unreachable("Unknown regclass!");
1094 return OpcodesForSpill
[OpcodeIndex
];
1098 PPCInstrInfo::getLoadOpcodeForSpill(unsigned Reg
,
1099 const TargetRegisterClass
*RC
) const {
1100 const unsigned *OpcodesForSpill
= getLoadOpcodesForSpillArray();
1101 int OpcodeIndex
= 0;
1103 if (RC
!= nullptr) {
1104 if (PPC::GPRCRegClass
.hasSubClassEq(RC
) ||
1105 PPC::GPRC_NOR0RegClass
.hasSubClassEq(RC
)) {
1106 OpcodeIndex
= SOK_Int4Spill
;
1107 } else if (PPC::G8RCRegClass
.hasSubClassEq(RC
) ||
1108 PPC::G8RC_NOX0RegClass
.hasSubClassEq(RC
)) {
1109 OpcodeIndex
= SOK_Int8Spill
;
1110 } else if (PPC::F8RCRegClass
.hasSubClassEq(RC
)) {
1111 OpcodeIndex
= SOK_Float8Spill
;
1112 } else if (PPC::F4RCRegClass
.hasSubClassEq(RC
)) {
1113 OpcodeIndex
= SOK_Float4Spill
;
1114 } else if (PPC::SPERCRegClass
.hasSubClassEq(RC
)) {
1115 OpcodeIndex
= SOK_SPESpill
;
1116 } else if (PPC::SPE4RCRegClass
.hasSubClassEq(RC
)) {
1117 OpcodeIndex
= SOK_SPE4Spill
;
1118 } else if (PPC::CRRCRegClass
.hasSubClassEq(RC
)) {
1119 OpcodeIndex
= SOK_CRSpill
;
1120 } else if (PPC::CRBITRCRegClass
.hasSubClassEq(RC
)) {
1121 OpcodeIndex
= SOK_CRBitSpill
;
1122 } else if (PPC::VRRCRegClass
.hasSubClassEq(RC
)) {
1123 OpcodeIndex
= SOK_VRVectorSpill
;
1124 } else if (PPC::VSRCRegClass
.hasSubClassEq(RC
)) {
1125 OpcodeIndex
= SOK_VSXVectorSpill
;
1126 } else if (PPC::VSFRCRegClass
.hasSubClassEq(RC
)) {
1127 OpcodeIndex
= SOK_VectorFloat8Spill
;
1128 } else if (PPC::VSSRCRegClass
.hasSubClassEq(RC
)) {
1129 OpcodeIndex
= SOK_VectorFloat4Spill
;
1130 } else if (PPC::VRSAVERCRegClass
.hasSubClassEq(RC
)) {
1131 OpcodeIndex
= SOK_VRSaveSpill
;
1132 } else if (PPC::QFRCRegClass
.hasSubClassEq(RC
)) {
1133 OpcodeIndex
= SOK_QuadFloat8Spill
;
1134 } else if (PPC::QSRCRegClass
.hasSubClassEq(RC
)) {
1135 OpcodeIndex
= SOK_QuadFloat4Spill
;
1136 } else if (PPC::QBRCRegClass
.hasSubClassEq(RC
)) {
1137 OpcodeIndex
= SOK_QuadBitSpill
;
1138 } else if (PPC::SPILLTOVSRRCRegClass
.hasSubClassEq(RC
)) {
1139 OpcodeIndex
= SOK_SpillToVSR
;
1141 llvm_unreachable("Unknown regclass!");
1144 if (PPC::GPRCRegClass
.contains(Reg
) ||
1145 PPC::GPRC_NOR0RegClass
.contains(Reg
)) {
1146 OpcodeIndex
= SOK_Int4Spill
;
1147 } else if (PPC::G8RCRegClass
.contains(Reg
) ||
1148 PPC::G8RC_NOX0RegClass
.contains(Reg
)) {
1149 OpcodeIndex
= SOK_Int8Spill
;
1150 } else if (PPC::F8RCRegClass
.contains(Reg
)) {
1151 OpcodeIndex
= SOK_Float8Spill
;
1152 } else if (PPC::F4RCRegClass
.contains(Reg
)) {
1153 OpcodeIndex
= SOK_Float4Spill
;
1154 } else if (PPC::CRRCRegClass
.contains(Reg
)) {
1155 OpcodeIndex
= SOK_CRSpill
;
1156 } else if (PPC::CRBITRCRegClass
.contains(Reg
)) {
1157 OpcodeIndex
= SOK_CRBitSpill
;
1158 } else if (PPC::VRRCRegClass
.contains(Reg
)) {
1159 OpcodeIndex
= SOK_VRVectorSpill
;
1160 } else if (PPC::VSRCRegClass
.contains(Reg
)) {
1161 OpcodeIndex
= SOK_VSXVectorSpill
;
1162 } else if (PPC::VSFRCRegClass
.contains(Reg
)) {
1163 OpcodeIndex
= SOK_VectorFloat8Spill
;
1164 } else if (PPC::VSSRCRegClass
.contains(Reg
)) {
1165 OpcodeIndex
= SOK_VectorFloat4Spill
;
1166 } else if (PPC::VRSAVERCRegClass
.contains(Reg
)) {
1167 OpcodeIndex
= SOK_VRSaveSpill
;
1168 } else if (PPC::QFRCRegClass
.contains(Reg
)) {
1169 OpcodeIndex
= SOK_QuadFloat8Spill
;
1170 } else if (PPC::QSRCRegClass
.contains(Reg
)) {
1171 OpcodeIndex
= SOK_QuadFloat4Spill
;
1172 } else if (PPC::QBRCRegClass
.contains(Reg
)) {
1173 OpcodeIndex
= SOK_QuadBitSpill
;
1174 } else if (PPC::SPILLTOVSRRCRegClass
.contains(Reg
)) {
1175 OpcodeIndex
= SOK_SpillToVSR
;
1177 llvm_unreachable("Unknown regclass!");
1180 return OpcodesForSpill
[OpcodeIndex
];
1183 void PPCInstrInfo::StoreRegToStackSlot(
1184 MachineFunction
&MF
, unsigned SrcReg
, bool isKill
, int FrameIdx
,
1185 const TargetRegisterClass
*RC
,
1186 SmallVectorImpl
<MachineInstr
*> &NewMIs
) const {
1187 unsigned Opcode
= getStoreOpcodeForSpill(PPC::NoRegister
, RC
);
1190 PPCFunctionInfo
*FuncInfo
= MF
.getInfo
<PPCFunctionInfo
>();
1191 FuncInfo
->setHasSpills();
1193 NewMIs
.push_back(addFrameReference(
1194 BuildMI(MF
, DL
, get(Opcode
)).addReg(SrcReg
, getKillRegState(isKill
)),
1197 if (PPC::CRRCRegClass
.hasSubClassEq(RC
) ||
1198 PPC::CRBITRCRegClass
.hasSubClassEq(RC
))
1199 FuncInfo
->setSpillsCR();
1201 if (PPC::VRSAVERCRegClass
.hasSubClassEq(RC
))
1202 FuncInfo
->setSpillsVRSAVE();
1204 if (isXFormMemOp(Opcode
))
1205 FuncInfo
->setHasNonRISpills();
1208 void PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock
&MBB
,
1209 MachineBasicBlock::iterator MI
,
1210 unsigned SrcReg
, bool isKill
,
1212 const TargetRegisterClass
*RC
,
1213 const TargetRegisterInfo
*TRI
) const {
1214 MachineFunction
&MF
= *MBB
.getParent();
1215 SmallVector
<MachineInstr
*, 4> NewMIs
;
1217 // We need to avoid a situation in which the value from a VRRC register is
1218 // spilled using an Altivec instruction and reloaded into a VSRC register
1219 // using a VSX instruction. The issue with this is that the VSX
1220 // load/store instructions swap the doublewords in the vector and the Altivec
1221 // ones don't. The register classes on the spill/reload may be different if
1222 // the register is defined using an Altivec instruction and is then used by a
1226 StoreRegToStackSlot(MF
, SrcReg
, isKill
, FrameIdx
, RC
, NewMIs
);
1228 for (unsigned i
= 0, e
= NewMIs
.size(); i
!= e
; ++i
)
1229 MBB
.insert(MI
, NewMIs
[i
]);
1231 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
1232 MachineMemOperand
*MMO
= MF
.getMachineMemOperand(
1233 MachinePointerInfo::getFixedStack(MF
, FrameIdx
),
1234 MachineMemOperand::MOStore
, MFI
.getObjectSize(FrameIdx
),
1235 MFI
.getObjectAlignment(FrameIdx
));
1236 NewMIs
.back()->addMemOperand(MF
, MMO
);
1239 void PPCInstrInfo::LoadRegFromStackSlot(MachineFunction
&MF
, const DebugLoc
&DL
,
1240 unsigned DestReg
, int FrameIdx
,
1241 const TargetRegisterClass
*RC
,
1242 SmallVectorImpl
<MachineInstr
*> &NewMIs
)
1244 unsigned Opcode
= getLoadOpcodeForSpill(PPC::NoRegister
, RC
);
1245 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(Opcode
), DestReg
),
1247 PPCFunctionInfo
*FuncInfo
= MF
.getInfo
<PPCFunctionInfo
>();
1249 if (PPC::CRRCRegClass
.hasSubClassEq(RC
) ||
1250 PPC::CRBITRCRegClass
.hasSubClassEq(RC
))
1251 FuncInfo
->setSpillsCR();
1253 if (PPC::VRSAVERCRegClass
.hasSubClassEq(RC
))
1254 FuncInfo
->setSpillsVRSAVE();
1256 if (isXFormMemOp(Opcode
))
1257 FuncInfo
->setHasNonRISpills();
1261 PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock
&MBB
,
1262 MachineBasicBlock::iterator MI
,
1263 unsigned DestReg
, int FrameIdx
,
1264 const TargetRegisterClass
*RC
,
1265 const TargetRegisterInfo
*TRI
) const {
1266 MachineFunction
&MF
= *MBB
.getParent();
1267 SmallVector
<MachineInstr
*, 4> NewMIs
;
1269 if (MI
!= MBB
.end()) DL
= MI
->getDebugLoc();
1271 PPCFunctionInfo
*FuncInfo
= MF
.getInfo
<PPCFunctionInfo
>();
1272 FuncInfo
->setHasSpills();
1274 // We need to avoid a situation in which the value from a VRRC register is
1275 // spilled using an Altivec instruction and reloaded into a VSRC register
1276 // using a VSX instruction. The issue with this is that the VSX
1277 // load/store instructions swap the doublewords in the vector and the Altivec
1278 // ones don't. The register classes on the spill/reload may be different if
1279 // the register is defined using an Altivec instruction and is then used by a
1281 if (Subtarget
.hasVSX() && RC
== &PPC::VRRCRegClass
)
1282 RC
= &PPC::VSRCRegClass
;
1284 LoadRegFromStackSlot(MF
, DL
, DestReg
, FrameIdx
, RC
, NewMIs
);
1286 for (unsigned i
= 0, e
= NewMIs
.size(); i
!= e
; ++i
)
1287 MBB
.insert(MI
, NewMIs
[i
]);
1289 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
1290 MachineMemOperand
*MMO
= MF
.getMachineMemOperand(
1291 MachinePointerInfo::getFixedStack(MF
, FrameIdx
),
1292 MachineMemOperand::MOLoad
, MFI
.getObjectSize(FrameIdx
),
1293 MFI
.getObjectAlignment(FrameIdx
));
1294 NewMIs
.back()->addMemOperand(MF
, MMO
);
1298 reverseBranchCondition(SmallVectorImpl
<MachineOperand
> &Cond
) const {
1299 assert(Cond
.size() == 2 && "Invalid PPC branch opcode!");
1300 if (Cond
[1].getReg() == PPC::CTR8
|| Cond
[1].getReg() == PPC::CTR
)
1301 Cond
[0].setImm(Cond
[0].getImm() == 0 ? 1 : 0);
1303 // Leave the CR# the same, but invert the condition.
1304 Cond
[0].setImm(PPC::InvertPredicate((PPC::Predicate
)Cond
[0].getImm()));
1308 bool PPCInstrInfo::FoldImmediate(MachineInstr
&UseMI
, MachineInstr
&DefMI
,
1309 unsigned Reg
, MachineRegisterInfo
*MRI
) const {
1310 // For some instructions, it is legal to fold ZERO into the RA register field.
1311 // A zero immediate should always be loaded with a single li.
1312 unsigned DefOpc
= DefMI
.getOpcode();
1313 if (DefOpc
!= PPC::LI
&& DefOpc
!= PPC::LI8
)
1315 if (!DefMI
.getOperand(1).isImm())
1317 if (DefMI
.getOperand(1).getImm() != 0)
1320 // Note that we cannot here invert the arguments of an isel in order to fold
1321 // a ZERO into what is presented as the second argument. All we have here
1322 // is the condition bit, and that might come from a CR-logical bit operation.
1324 const MCInstrDesc
&UseMCID
= UseMI
.getDesc();
1326 // Only fold into real machine instructions.
1327 if (UseMCID
.isPseudo())
1331 for (UseIdx
= 0; UseIdx
< UseMI
.getNumOperands(); ++UseIdx
)
1332 if (UseMI
.getOperand(UseIdx
).isReg() &&
1333 UseMI
.getOperand(UseIdx
).getReg() == Reg
)
1336 assert(UseIdx
< UseMI
.getNumOperands() && "Cannot find Reg in UseMI");
1337 assert(UseIdx
< UseMCID
.getNumOperands() && "No operand description for Reg");
1339 const MCOperandInfo
*UseInfo
= &UseMCID
.OpInfo
[UseIdx
];
1341 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
1342 // register (which might also be specified as a pointer class kind).
1343 if (UseInfo
->isLookupPtrRegClass()) {
1344 if (UseInfo
->RegClass
/* Kind */ != 1)
1347 if (UseInfo
->RegClass
!= PPC::GPRC_NOR0RegClassID
&&
1348 UseInfo
->RegClass
!= PPC::G8RC_NOX0RegClassID
)
1352 // Make sure this is not tied to an output register (or otherwise
1353 // constrained). This is true for ST?UX registers, for example, which
1354 // are tied to their output registers.
1355 if (UseInfo
->Constraints
!= 0)
1359 if (UseInfo
->isLookupPtrRegClass()) {
1360 bool isPPC64
= Subtarget
.isPPC64();
1361 ZeroReg
= isPPC64
? PPC::ZERO8
: PPC::ZERO
;
1363 ZeroReg
= UseInfo
->RegClass
== PPC::G8RC_NOX0RegClassID
?
1364 PPC::ZERO8
: PPC::ZERO
;
1367 bool DeleteDef
= MRI
->hasOneNonDBGUse(Reg
);
1368 UseMI
.getOperand(UseIdx
).setReg(ZeroReg
);
1371 DefMI
.eraseFromParent();
1376 static bool MBBDefinesCTR(MachineBasicBlock
&MBB
) {
1377 for (MachineBasicBlock::iterator I
= MBB
.begin(), IE
= MBB
.end();
1379 if (I
->definesRegister(PPC::CTR
) || I
->definesRegister(PPC::CTR8
))
1384 // We should make sure that, if we're going to predicate both sides of a
1385 // condition (a diamond), that both sides don't define the counter register. We
1386 // can predicate counter-decrement-based branches, but while that predicates
1387 // the branching, it does not predicate the counter decrement. If we tried to
1388 // merge the triangle into one predicated block, we'd decrement the counter
1390 bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock
&TMBB
,
1391 unsigned NumT
, unsigned ExtraT
,
1392 MachineBasicBlock
&FMBB
,
1393 unsigned NumF
, unsigned ExtraF
,
1394 BranchProbability Probability
) const {
1395 return !(MBBDefinesCTR(TMBB
) && MBBDefinesCTR(FMBB
));
1399 bool PPCInstrInfo::isPredicated(const MachineInstr
&MI
) const {
1400 // The predicated branches are identified by their type, not really by the
1401 // explicit presence of a predicate. Furthermore, some of them can be
1402 // predicated more than once. Because if conversion won't try to predicate
1403 // any instruction which already claims to be predicated (by returning true
1404 // here), always return false. In doing so, we let isPredicable() be the
1405 // final word on whether not the instruction can be (further) predicated.
1410 bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr
&MI
) const {
1411 if (!MI
.isTerminator())
1414 // Conditional branch is a special case.
1415 if (MI
.isBranch() && !MI
.isBarrier())
1418 return !isPredicated(MI
);
1421 bool PPCInstrInfo::PredicateInstruction(MachineInstr
&MI
,
1422 ArrayRef
<MachineOperand
> Pred
) const {
1423 unsigned OpC
= MI
.getOpcode();
1424 if (OpC
== PPC::BLR
|| OpC
== PPC::BLR8
) {
1425 if (Pred
[1].getReg() == PPC::CTR8
|| Pred
[1].getReg() == PPC::CTR
) {
1426 bool isPPC64
= Subtarget
.isPPC64();
1427 MI
.setDesc(get(Pred
[0].getImm() ? (isPPC64
? PPC::BDNZLR8
: PPC::BDNZLR
)
1428 : (isPPC64
? PPC::BDZLR8
: PPC::BDZLR
)));
1429 } else if (Pred
[0].getImm() == PPC::PRED_BIT_SET
) {
1430 MI
.setDesc(get(PPC::BCLR
));
1431 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
).add(Pred
[1]);
1432 } else if (Pred
[0].getImm() == PPC::PRED_BIT_UNSET
) {
1433 MI
.setDesc(get(PPC::BCLRn
));
1434 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
).add(Pred
[1]);
1436 MI
.setDesc(get(PPC::BCCLR
));
1437 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
1438 .addImm(Pred
[0].getImm())
1443 } else if (OpC
== PPC::B
) {
1444 if (Pred
[1].getReg() == PPC::CTR8
|| Pred
[1].getReg() == PPC::CTR
) {
1445 bool isPPC64
= Subtarget
.isPPC64();
1446 MI
.setDesc(get(Pred
[0].getImm() ? (isPPC64
? PPC::BDNZ8
: PPC::BDNZ
)
1447 : (isPPC64
? PPC::BDZ8
: PPC::BDZ
)));
1448 } else if (Pred
[0].getImm() == PPC::PRED_BIT_SET
) {
1449 MachineBasicBlock
*MBB
= MI
.getOperand(0).getMBB();
1450 MI
.RemoveOperand(0);
1452 MI
.setDesc(get(PPC::BC
));
1453 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
1456 } else if (Pred
[0].getImm() == PPC::PRED_BIT_UNSET
) {
1457 MachineBasicBlock
*MBB
= MI
.getOperand(0).getMBB();
1458 MI
.RemoveOperand(0);
1460 MI
.setDesc(get(PPC::BCn
));
1461 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
1465 MachineBasicBlock
*MBB
= MI
.getOperand(0).getMBB();
1466 MI
.RemoveOperand(0);
1468 MI
.setDesc(get(PPC::BCC
));
1469 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
1470 .addImm(Pred
[0].getImm())
1476 } else if (OpC
== PPC::BCTR
|| OpC
== PPC::BCTR8
|| OpC
== PPC::BCTRL
||
1477 OpC
== PPC::BCTRL8
) {
1478 if (Pred
[1].getReg() == PPC::CTR8
|| Pred
[1].getReg() == PPC::CTR
)
1479 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1481 bool setLR
= OpC
== PPC::BCTRL
|| OpC
== PPC::BCTRL8
;
1482 bool isPPC64
= Subtarget
.isPPC64();
1484 if (Pred
[0].getImm() == PPC::PRED_BIT_SET
) {
1485 MI
.setDesc(get(isPPC64
? (setLR
? PPC::BCCTRL8
: PPC::BCCTR8
)
1486 : (setLR
? PPC::BCCTRL
: PPC::BCCTR
)));
1487 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
).add(Pred
[1]);
1489 } else if (Pred
[0].getImm() == PPC::PRED_BIT_UNSET
) {
1490 MI
.setDesc(get(isPPC64
? (setLR
? PPC::BCCTRL8n
: PPC::BCCTR8n
)
1491 : (setLR
? PPC::BCCTRLn
: PPC::BCCTRn
)));
1492 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
).add(Pred
[1]);
1496 MI
.setDesc(get(isPPC64
? (setLR
? PPC::BCCCTRL8
: PPC::BCCCTR8
)
1497 : (setLR
? PPC::BCCCTRL
: PPC::BCCCTR
)));
1498 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
1499 .addImm(Pred
[0].getImm())
1507 bool PPCInstrInfo::SubsumesPredicate(ArrayRef
<MachineOperand
> Pred1
,
1508 ArrayRef
<MachineOperand
> Pred2
) const {
1509 assert(Pred1
.size() == 2 && "Invalid PPC first predicate");
1510 assert(Pred2
.size() == 2 && "Invalid PPC second predicate");
1512 if (Pred1
[1].getReg() == PPC::CTR8
|| Pred1
[1].getReg() == PPC::CTR
)
1514 if (Pred2
[1].getReg() == PPC::CTR8
|| Pred2
[1].getReg() == PPC::CTR
)
1517 // P1 can only subsume P2 if they test the same condition register.
1518 if (Pred1
[1].getReg() != Pred2
[1].getReg())
1521 PPC::Predicate P1
= (PPC::Predicate
) Pred1
[0].getImm();
1522 PPC::Predicate P2
= (PPC::Predicate
) Pred2
[0].getImm();
1527 // Does P1 subsume P2, e.g. GE subsumes GT.
1528 if (P1
== PPC::PRED_LE
&&
1529 (P2
== PPC::PRED_LT
|| P2
== PPC::PRED_EQ
))
1531 if (P1
== PPC::PRED_GE
&&
1532 (P2
== PPC::PRED_GT
|| P2
== PPC::PRED_EQ
))
1538 bool PPCInstrInfo::DefinesPredicate(MachineInstr
&MI
,
1539 std::vector
<MachineOperand
> &Pred
) const {
1540 // Note: At the present time, the contents of Pred from this function is
1541 // unused by IfConversion. This implementation follows ARM by pushing the
1542 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1543 // predicate, instructions defining CTR or CTR8 are also included as
1544 // predicate-defining instructions.
1546 const TargetRegisterClass
*RCs
[] =
1547 { &PPC::CRRCRegClass
, &PPC::CRBITRCRegClass
,
1548 &PPC::CTRRCRegClass
, &PPC::CTRRC8RegClass
};
1551 for (unsigned i
= 0, e
= MI
.getNumOperands(); i
!= e
; ++i
) {
1552 const MachineOperand
&MO
= MI
.getOperand(i
);
1553 for (unsigned c
= 0; c
< array_lengthof(RCs
) && !Found
; ++c
) {
1554 const TargetRegisterClass
*RC
= RCs
[c
];
1556 if (MO
.isDef() && RC
->contains(MO
.getReg())) {
1560 } else if (MO
.isRegMask()) {
1561 for (TargetRegisterClass::iterator I
= RC
->begin(),
1562 IE
= RC
->end(); I
!= IE
; ++I
)
1563 if (MO
.clobbersPhysReg(*I
)) {
1574 bool PPCInstrInfo::isPredicable(const MachineInstr
&MI
) const {
1575 unsigned OpC
= MI
.getOpcode();
1590 bool PPCInstrInfo::analyzeCompare(const MachineInstr
&MI
, unsigned &SrcReg
,
1591 unsigned &SrcReg2
, int &Mask
,
1593 unsigned Opc
= MI
.getOpcode();
1596 default: return false;
1601 SrcReg
= MI
.getOperand(1).getReg();
1603 Value
= MI
.getOperand(2).getImm();
1612 SrcReg
= MI
.getOperand(1).getReg();
1613 SrcReg2
= MI
.getOperand(2).getReg();
1620 bool PPCInstrInfo::optimizeCompareInstr(MachineInstr
&CmpInstr
, unsigned SrcReg
,
1621 unsigned SrcReg2
, int Mask
, int Value
,
1622 const MachineRegisterInfo
*MRI
) const {
1626 int OpC
= CmpInstr
.getOpcode();
1627 unsigned CRReg
= CmpInstr
.getOperand(0).getReg();
1629 // FP record forms set CR1 based on the exception status bits, not a
1630 // comparison with zero.
1631 if (OpC
== PPC::FCMPUS
|| OpC
== PPC::FCMPUD
)
1634 // The record forms set the condition register based on a signed comparison
1635 // with zero (so says the ISA manual). This is not as straightforward as it
1636 // seems, however, because this is always a 64-bit comparison on PPC64, even
1637 // for instructions that are 32-bit in nature (like slw for example).
1638 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1639 // for equality checks (as those don't depend on the sign). On PPC64,
1640 // we are restricted to equality for unsigned 64-bit comparisons and for
1641 // signed 32-bit comparisons the applicability is more restricted.
1642 bool isPPC64
= Subtarget
.isPPC64();
1643 bool is32BitSignedCompare
= OpC
== PPC::CMPWI
|| OpC
== PPC::CMPW
;
1644 bool is32BitUnsignedCompare
= OpC
== PPC::CMPLWI
|| OpC
== PPC::CMPLW
;
1645 bool is64BitUnsignedCompare
= OpC
== PPC::CMPLDI
|| OpC
== PPC::CMPLD
;
1647 // Get the unique definition of SrcReg.
1648 MachineInstr
*MI
= MRI
->getUniqueVRegDef(SrcReg
);
1649 if (!MI
) return false;
1651 bool equalityOnly
= false;
1654 if (is32BitSignedCompare
) {
1655 // We can perform this optimization only if MI is sign-extending.
1656 if (isSignExtended(*MI
))
1660 } else if (is32BitUnsignedCompare
) {
1661 // We can perform this optimization, equality only, if MI is
1663 if (isZeroExtended(*MI
)) {
1665 equalityOnly
= true;
1669 equalityOnly
= is64BitUnsignedCompare
;
1671 equalityOnly
= is32BitUnsignedCompare
;
1674 // We need to check the uses of the condition register in order to reject
1675 // non-equality comparisons.
1676 for (MachineRegisterInfo::use_instr_iterator
1677 I
= MRI
->use_instr_begin(CRReg
), IE
= MRI
->use_instr_end();
1679 MachineInstr
*UseMI
= &*I
;
1680 if (UseMI
->getOpcode() == PPC::BCC
) {
1681 PPC::Predicate Pred
= (PPC::Predicate
)UseMI
->getOperand(0).getImm();
1682 unsigned PredCond
= PPC::getPredicateCondition(Pred
);
1683 // We ignore hint bits when checking for non-equality comparisons.
1684 if (PredCond
!= PPC::PRED_EQ
&& PredCond
!= PPC::PRED_NE
)
1686 } else if (UseMI
->getOpcode() == PPC::ISEL
||
1687 UseMI
->getOpcode() == PPC::ISEL8
) {
1688 unsigned SubIdx
= UseMI
->getOperand(3).getSubReg();
1689 if (SubIdx
!= PPC::sub_eq
)
1696 MachineBasicBlock::iterator I
= CmpInstr
;
1698 // Scan forward to find the first use of the compare.
1699 for (MachineBasicBlock::iterator EL
= CmpInstr
.getParent()->end(); I
!= EL
;
1701 bool FoundUse
= false;
1702 for (MachineRegisterInfo::use_instr_iterator
1703 J
= MRI
->use_instr_begin(CRReg
), JE
= MRI
->use_instr_end();
1714 SmallVector
<std::pair
<MachineOperand
*, PPC::Predicate
>, 4> PredsToUpdate
;
1715 SmallVector
<std::pair
<MachineOperand
*, unsigned>, 4> SubRegsToUpdate
;
1717 // There are two possible candidates which can be changed to set CR[01].
1718 // One is MI, the other is a SUB instruction.
1719 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
1720 MachineInstr
*Sub
= nullptr;
1722 // MI is not a candidate for CMPrr.
1724 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1725 // same BB as the comparison. This is to allow the check below to avoid calls
1726 // (and other explicit clobbers); instead we should really check for these
1727 // more explicitly (in at least a few predecessors).
1728 else if (MI
->getParent() != CmpInstr
.getParent())
1730 else if (Value
!= 0) {
1731 // The record-form instructions set CR bit based on signed comparison
1732 // against 0. We try to convert a compare against 1 or -1 into a compare
1733 // against 0 to exploit record-form instructions. For example, we change
1734 // the condition "greater than -1" into "greater than or equal to 0"
1735 // and "less than 1" into "less than or equal to 0".
1737 // Since we optimize comparison based on a specific branch condition,
1738 // we don't optimize if condition code is used by more than once.
1739 if (equalityOnly
|| !MRI
->hasOneUse(CRReg
))
1742 MachineInstr
*UseMI
= &*MRI
->use_instr_begin(CRReg
);
1743 if (UseMI
->getOpcode() != PPC::BCC
)
1746 PPC::Predicate Pred
= (PPC::Predicate
)UseMI
->getOperand(0).getImm();
1747 PPC::Predicate NewPred
= Pred
;
1748 unsigned PredCond
= PPC::getPredicateCondition(Pred
);
1749 unsigned PredHint
= PPC::getPredicateHint(Pred
);
1750 int16_t Immed
= (int16_t)Value
;
1752 // When modifying the condition in the predicate, we propagate hint bits
1753 // from the original predicate to the new one.
1754 if (Immed
== -1 && PredCond
== PPC::PRED_GT
)
1755 // We convert "greater than -1" into "greater than or equal to 0",
1756 // since we are assuming signed comparison by !equalityOnly
1757 NewPred
= PPC::getPredicate(PPC::PRED_GE
, PredHint
);
1758 else if (Immed
== -1 && PredCond
== PPC::PRED_LE
)
1759 // We convert "less than or equal to -1" into "less than 0".
1760 NewPred
= PPC::getPredicate(PPC::PRED_LT
, PredHint
);
1761 else if (Immed
== 1 && PredCond
== PPC::PRED_LT
)
1762 // We convert "less than 1" into "less than or equal to 0".
1763 NewPred
= PPC::getPredicate(PPC::PRED_LE
, PredHint
);
1764 else if (Immed
== 1 && PredCond
== PPC::PRED_GE
)
1765 // We convert "greater than or equal to 1" into "greater than 0".
1766 NewPred
= PPC::getPredicate(PPC::PRED_GT
, PredHint
);
1770 PredsToUpdate
.push_back(std::make_pair(&(UseMI
->getOperand(0)),
1775 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
1778 // Get ready to iterate backward from CmpInstr.
1779 MachineBasicBlock::iterator E
= MI
, B
= CmpInstr
.getParent()->begin();
1781 for (; I
!= E
&& !noSub
; --I
) {
1782 const MachineInstr
&Instr
= *I
;
1783 unsigned IOpC
= Instr
.getOpcode();
1785 if (&*I
!= &CmpInstr
&& (Instr
.modifiesRegister(PPC::CR0
, TRI
) ||
1786 Instr
.readsRegister(PPC::CR0
, TRI
)))
1787 // This instruction modifies or uses the record condition register after
1788 // the one we want to change. While we could do this transformation, it
1789 // would likely not be profitable. This transformation removes one
1790 // instruction, and so even forcing RA to generate one move probably
1791 // makes it unprofitable.
1794 // Check whether CmpInstr can be made redundant by the current instruction.
1795 if ((OpC
== PPC::CMPW
|| OpC
== PPC::CMPLW
||
1796 OpC
== PPC::CMPD
|| OpC
== PPC::CMPLD
) &&
1797 (IOpC
== PPC::SUBF
|| IOpC
== PPC::SUBF8
) &&
1798 ((Instr
.getOperand(1).getReg() == SrcReg
&&
1799 Instr
.getOperand(2).getReg() == SrcReg2
) ||
1800 (Instr
.getOperand(1).getReg() == SrcReg2
&&
1801 Instr
.getOperand(2).getReg() == SrcReg
))) {
1807 // The 'and' is below the comparison instruction.
1811 // Return false if no candidates exist.
1815 // The single candidate is called MI.
1819 int MIOpC
= MI
->getOpcode();
1820 if (MIOpC
== PPC::ANDIo
|| MIOpC
== PPC::ANDIo8
||
1821 MIOpC
== PPC::ANDISo
|| MIOpC
== PPC::ANDISo8
)
1824 NewOpC
= PPC::getRecordFormOpcode(MIOpC
);
1825 if (NewOpC
== -1 && PPC::getNonRecordFormOpcode(MIOpC
) != -1)
1829 // FIXME: On the non-embedded POWER architectures, only some of the record
1830 // forms are fast, and we should use only the fast ones.
1832 // The defining instruction has a record form (or is already a record
1833 // form). It is possible, however, that we'll need to reverse the condition
1834 // code of the users.
1838 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1839 // needs to be updated to be based on SUB. Push the condition code
1840 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1841 // condition code of these operands will be modified.
1842 // Here, Value == 0 means we haven't converted comparison against 1 or -1 to
1843 // comparison against 0, which may modify predicate.
1844 bool ShouldSwap
= false;
1845 if (Sub
&& Value
== 0) {
1846 ShouldSwap
= SrcReg2
!= 0 && Sub
->getOperand(1).getReg() == SrcReg2
&&
1847 Sub
->getOperand(2).getReg() == SrcReg
;
1849 // The operands to subf are the opposite of sub, so only in the fixed-point
1850 // case, invert the order.
1851 ShouldSwap
= !ShouldSwap
;
1855 for (MachineRegisterInfo::use_instr_iterator
1856 I
= MRI
->use_instr_begin(CRReg
), IE
= MRI
->use_instr_end();
1858 MachineInstr
*UseMI
= &*I
;
1859 if (UseMI
->getOpcode() == PPC::BCC
) {
1860 PPC::Predicate Pred
= (PPC::Predicate
) UseMI
->getOperand(0).getImm();
1861 unsigned PredCond
= PPC::getPredicateCondition(Pred
);
1862 assert((!equalityOnly
||
1863 PredCond
== PPC::PRED_EQ
|| PredCond
== PPC::PRED_NE
) &&
1864 "Invalid predicate for equality-only optimization");
1865 (void)PredCond
; // To suppress warning in release build.
1866 PredsToUpdate
.push_back(std::make_pair(&(UseMI
->getOperand(0)),
1867 PPC::getSwappedPredicate(Pred
)));
1868 } else if (UseMI
->getOpcode() == PPC::ISEL
||
1869 UseMI
->getOpcode() == PPC::ISEL8
) {
1870 unsigned NewSubReg
= UseMI
->getOperand(3).getSubReg();
1871 assert((!equalityOnly
|| NewSubReg
== PPC::sub_eq
) &&
1872 "Invalid CR bit for equality-only optimization");
1874 if (NewSubReg
== PPC::sub_lt
)
1875 NewSubReg
= PPC::sub_gt
;
1876 else if (NewSubReg
== PPC::sub_gt
)
1877 NewSubReg
= PPC::sub_lt
;
1879 SubRegsToUpdate
.push_back(std::make_pair(&(UseMI
->getOperand(3)),
1881 } else // We need to abort on a user we don't understand.
1884 assert(!(Value
!= 0 && ShouldSwap
) &&
1885 "Non-zero immediate support and ShouldSwap"
1886 "may conflict in updating predicate");
1888 // Create a new virtual register to hold the value of the CR set by the
1889 // record-form instruction. If the instruction was not previously in
1890 // record form, then set the kill flag on the CR.
1891 CmpInstr
.eraseFromParent();
1893 MachineBasicBlock::iterator MII
= MI
;
1894 BuildMI(*MI
->getParent(), std::next(MII
), MI
->getDebugLoc(),
1895 get(TargetOpcode::COPY
), CRReg
)
1896 .addReg(PPC::CR0
, MIOpC
!= NewOpC
? RegState::Kill
: 0);
1898 // Even if CR0 register were dead before, it is alive now since the
1899 // instruction we just built uses it.
1900 MI
->clearRegisterDeads(PPC::CR0
);
1902 if (MIOpC
!= NewOpC
) {
1903 // We need to be careful here: we're replacing one instruction with
1904 // another, and we need to make sure that we get all of the right
1905 // implicit uses and defs. On the other hand, the caller may be holding
1906 // an iterator to this instruction, and so we can't delete it (this is
1907 // specifically the case if this is the instruction directly after the
1910 // Rotates are expensive instructions. If we're emitting a record-form
1911 // rotate that can just be an andi/andis, we should just emit that.
1912 if (MIOpC
== PPC::RLWINM
|| MIOpC
== PPC::RLWINM8
) {
1913 unsigned GPRRes
= MI
->getOperand(0).getReg();
1914 int64_t SH
= MI
->getOperand(2).getImm();
1915 int64_t MB
= MI
->getOperand(3).getImm();
1916 int64_t ME
= MI
->getOperand(4).getImm();
1917 // We can only do this if both the start and end of the mask are in the
1919 bool MBInLoHWord
= MB
>= 16;
1920 bool MEInLoHWord
= ME
>= 16;
1921 uint64_t Mask
= ~0LLU;
1923 if (MB
<= ME
&& MBInLoHWord
== MEInLoHWord
&& SH
== 0) {
1924 Mask
= ((1LLU << (32 - MB
)) - 1) & ~((1LLU << (31 - ME
)) - 1);
1925 // The mask value needs to shift right 16 if we're emitting andis.
1926 Mask
>>= MBInLoHWord
? 0 : 16;
1927 NewOpC
= MIOpC
== PPC::RLWINM
?
1928 (MBInLoHWord
? PPC::ANDIo
: PPC::ANDISo
) :
1929 (MBInLoHWord
? PPC::ANDIo8
:PPC::ANDISo8
);
1930 } else if (MRI
->use_empty(GPRRes
) && (ME
== 31) &&
1931 (ME
- MB
+ 1 == SH
) && (MB
>= 16)) {
1932 // If we are rotating by the exact number of bits as are in the mask
1933 // and the mask is in the least significant bits of the register,
1934 // that's just an andis. (as long as the GPR result has no uses).
1935 Mask
= ((1LLU << 32) - 1) & ~((1LLU << (32 - SH
)) - 1);
1937 NewOpC
= MIOpC
== PPC::RLWINM
? PPC::ANDISo
:PPC::ANDISo8
;
1939 // If we've set the mask, we can transform.
1940 if (Mask
!= ~0LLU) {
1941 MI
->RemoveOperand(4);
1942 MI
->RemoveOperand(3);
1943 MI
->getOperand(2).setImm(Mask
);
1944 NumRcRotatesConvertedToRcAnd
++;
1946 } else if (MIOpC
== PPC::RLDICL
&& MI
->getOperand(2).getImm() == 0) {
1947 int64_t MB
= MI
->getOperand(3).getImm();
1949 uint64_t Mask
= (1LLU << (63 - MB
+ 1)) - 1;
1950 NewOpC
= PPC::ANDIo8
;
1951 MI
->RemoveOperand(3);
1952 MI
->getOperand(2).setImm(Mask
);
1953 NumRcRotatesConvertedToRcAnd
++;
1957 const MCInstrDesc
&NewDesc
= get(NewOpC
);
1958 MI
->setDesc(NewDesc
);
1960 if (NewDesc
.ImplicitDefs
)
1961 for (const MCPhysReg
*ImpDefs
= NewDesc
.getImplicitDefs();
1962 *ImpDefs
; ++ImpDefs
)
1963 if (!MI
->definesRegister(*ImpDefs
))
1964 MI
->addOperand(*MI
->getParent()->getParent(),
1965 MachineOperand::CreateReg(*ImpDefs
, true, true));
1966 if (NewDesc
.ImplicitUses
)
1967 for (const MCPhysReg
*ImpUses
= NewDesc
.getImplicitUses();
1968 *ImpUses
; ++ImpUses
)
1969 if (!MI
->readsRegister(*ImpUses
))
1970 MI
->addOperand(*MI
->getParent()->getParent(),
1971 MachineOperand::CreateReg(*ImpUses
, false, true));
1973 assert(MI
->definesRegister(PPC::CR0
) &&
1974 "Record-form instruction does not define cr0?");
1976 // Modify the condition code of operands in OperandsToUpdate.
1977 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1978 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
1979 for (unsigned i
= 0, e
= PredsToUpdate
.size(); i
< e
; i
++)
1980 PredsToUpdate
[i
].first
->setImm(PredsToUpdate
[i
].second
);
1982 for (unsigned i
= 0, e
= SubRegsToUpdate
.size(); i
< e
; i
++)
1983 SubRegsToUpdate
[i
].first
->setSubReg(SubRegsToUpdate
[i
].second
);
1988 /// GetInstSize - Return the number of bytes of code the specified
1989 /// instruction may be. This returns the maximum number of bytes.
1991 unsigned PPCInstrInfo::getInstSizeInBytes(const MachineInstr
&MI
) const {
1992 unsigned Opcode
= MI
.getOpcode();
1994 if (Opcode
== PPC::INLINEASM
) {
1995 const MachineFunction
*MF
= MI
.getParent()->getParent();
1996 const char *AsmStr
= MI
.getOperand(0).getSymbolName();
1997 return getInlineAsmLength(AsmStr
, *MF
->getTarget().getMCAsmInfo());
1998 } else if (Opcode
== TargetOpcode::STACKMAP
) {
1999 StackMapOpers
Opers(&MI
);
2000 return Opers
.getNumPatchBytes();
2001 } else if (Opcode
== TargetOpcode::PATCHPOINT
) {
2002 PatchPointOpers
Opers(&MI
);
2003 return Opers
.getNumPatchBytes();
2005 return get(Opcode
).getSize();
2009 std::pair
<unsigned, unsigned>
2010 PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF
) const {
2011 const unsigned Mask
= PPCII::MO_ACCESS_MASK
;
2012 return std::make_pair(TF
& Mask
, TF
& ~Mask
);
2015 ArrayRef
<std::pair
<unsigned, const char *>>
2016 PPCInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
2017 using namespace PPCII
;
2018 static const std::pair
<unsigned, const char *> TargetFlags
[] = {
2021 {MO_TPREL_LO
, "ppc-tprel-lo"},
2022 {MO_TPREL_HA
, "ppc-tprel-ha"},
2023 {MO_DTPREL_LO
, "ppc-dtprel-lo"},
2024 {MO_TLSLD_LO
, "ppc-tlsld-lo"},
2025 {MO_TOC_LO
, "ppc-toc-lo"},
2026 {MO_TLS
, "ppc-tls"}};
2027 return makeArrayRef(TargetFlags
);
2030 ArrayRef
<std::pair
<unsigned, const char *>>
2031 PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
2032 using namespace PPCII
;
2033 static const std::pair
<unsigned, const char *> TargetFlags
[] = {
2034 {MO_PLT
, "ppc-plt"},
2035 {MO_PIC_FLAG
, "ppc-pic"},
2036 {MO_NLP_FLAG
, "ppc-nlp"},
2037 {MO_NLP_HIDDEN_FLAG
, "ppc-nlp-hidden"}};
2038 return makeArrayRef(TargetFlags
);
2041 // Expand VSX Memory Pseudo instruction to either a VSX or a FP instruction.
2042 // The VSX versions have the advantage of a full 64-register target whereas
2043 // the FP ones have the advantage of lower latency and higher throughput. So
2044 // what we are after is using the faster instructions in low register pressure
2045 // situations and using the larger register file in high register pressure
2047 bool PPCInstrInfo::expandVSXMemPseudo(MachineInstr
&MI
) const {
2048 unsigned UpperOpcode
, LowerOpcode
;
2049 switch (MI
.getOpcode()) {
2050 case PPC::DFLOADf32
:
2051 UpperOpcode
= PPC::LXSSP
;
2052 LowerOpcode
= PPC::LFS
;
2054 case PPC::DFLOADf64
:
2055 UpperOpcode
= PPC::LXSD
;
2056 LowerOpcode
= PPC::LFD
;
2058 case PPC::DFSTOREf32
:
2059 UpperOpcode
= PPC::STXSSP
;
2060 LowerOpcode
= PPC::STFS
;
2062 case PPC::DFSTOREf64
:
2063 UpperOpcode
= PPC::STXSD
;
2064 LowerOpcode
= PPC::STFD
;
2066 case PPC::XFLOADf32
:
2067 UpperOpcode
= PPC::LXSSPX
;
2068 LowerOpcode
= PPC::LFSX
;
2070 case PPC::XFLOADf64
:
2071 UpperOpcode
= PPC::LXSDX
;
2072 LowerOpcode
= PPC::LFDX
;
2074 case PPC::XFSTOREf32
:
2075 UpperOpcode
= PPC::STXSSPX
;
2076 LowerOpcode
= PPC::STFSX
;
2078 case PPC::XFSTOREf64
:
2079 UpperOpcode
= PPC::STXSDX
;
2080 LowerOpcode
= PPC::STFDX
;
2083 UpperOpcode
= PPC::LXSIWAX
;
2084 LowerOpcode
= PPC::LFIWAX
;
2087 UpperOpcode
= PPC::LXSIWZX
;
2088 LowerOpcode
= PPC::LFIWZX
;
2091 UpperOpcode
= PPC::STXSIWX
;
2092 LowerOpcode
= PPC::STFIWX
;
2095 llvm_unreachable("Unknown Operation!");
2098 unsigned TargetReg
= MI
.getOperand(0).getReg();
2100 if ((TargetReg
>= PPC::F0
&& TargetReg
<= PPC::F31
) ||
2101 (TargetReg
>= PPC::VSL0
&& TargetReg
<= PPC::VSL31
))
2102 Opcode
= LowerOpcode
;
2104 Opcode
= UpperOpcode
;
2105 MI
.setDesc(get(Opcode
));
2109 static bool isAnImmediateOperand(const MachineOperand
&MO
) {
2110 return MO
.isCPI() || MO
.isGlobal() || MO
.isImm();
2113 bool PPCInstrInfo::expandPostRAPseudo(MachineInstr
&MI
) const {
2114 auto &MBB
= *MI
.getParent();
2115 auto DL
= MI
.getDebugLoc();
2117 switch (MI
.getOpcode()) {
2118 case TargetOpcode::LOAD_STACK_GUARD
: {
2119 assert(Subtarget
.isTargetLinux() &&
2120 "Only Linux target is expected to contain LOAD_STACK_GUARD");
2121 const int64_t Offset
= Subtarget
.isPPC64() ? -0x7010 : -0x7008;
2122 const unsigned Reg
= Subtarget
.isPPC64() ? PPC::X13
: PPC::R2
;
2123 MI
.setDesc(get(Subtarget
.isPPC64() ? PPC::LD
: PPC::LWZ
));
2124 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
2129 case PPC::DFLOADf32
:
2130 case PPC::DFLOADf64
:
2131 case PPC::DFSTOREf32
:
2132 case PPC::DFSTOREf64
: {
2133 assert(Subtarget
.hasP9Vector() &&
2134 "Invalid D-Form Pseudo-ops on Pre-P9 target.");
2135 assert(MI
.getOperand(2).isReg() &&
2136 isAnImmediateOperand(MI
.getOperand(1)) &&
2137 "D-form op must have register and immediate operands");
2138 return expandVSXMemPseudo(MI
);
2140 case PPC::XFLOADf32
:
2141 case PPC::XFSTOREf32
:
2145 assert(Subtarget
.hasP8Vector() &&
2146 "Invalid X-Form Pseudo-ops on Pre-P8 target.");
2147 assert(MI
.getOperand(2).isReg() && MI
.getOperand(1).isReg() &&
2148 "X-form op must have register and register operands");
2149 return expandVSXMemPseudo(MI
);
2151 case PPC::XFLOADf64
:
2152 case PPC::XFSTOREf64
: {
2153 assert(Subtarget
.hasVSX() &&
2154 "Invalid X-Form Pseudo-ops on target that has no VSX.");
2155 assert(MI
.getOperand(2).isReg() && MI
.getOperand(1).isReg() &&
2156 "X-form op must have register and register operands");
2157 return expandVSXMemPseudo(MI
);
2159 case PPC::SPILLTOVSR_LD
: {
2160 unsigned TargetReg
= MI
.getOperand(0).getReg();
2161 if (PPC::VSFRCRegClass
.contains(TargetReg
)) {
2162 MI
.setDesc(get(PPC::DFLOADf64
));
2163 return expandPostRAPseudo(MI
);
2166 MI
.setDesc(get(PPC::LD
));
2169 case PPC::SPILLTOVSR_ST
: {
2170 unsigned SrcReg
= MI
.getOperand(0).getReg();
2171 if (PPC::VSFRCRegClass
.contains(SrcReg
)) {
2172 NumStoreSPILLVSRRCAsVec
++;
2173 MI
.setDesc(get(PPC::DFSTOREf64
));
2174 return expandPostRAPseudo(MI
);
2176 NumStoreSPILLVSRRCAsGpr
++;
2177 MI
.setDesc(get(PPC::STD
));
2181 case PPC::SPILLTOVSR_LDX
: {
2182 unsigned TargetReg
= MI
.getOperand(0).getReg();
2183 if (PPC::VSFRCRegClass
.contains(TargetReg
))
2184 MI
.setDesc(get(PPC::LXSDX
));
2186 MI
.setDesc(get(PPC::LDX
));
2189 case PPC::SPILLTOVSR_STX
: {
2190 unsigned SrcReg
= MI
.getOperand(0).getReg();
2191 if (PPC::VSFRCRegClass
.contains(SrcReg
)) {
2192 NumStoreSPILLVSRRCAsVec
++;
2193 MI
.setDesc(get(PPC::STXSDX
));
2195 NumStoreSPILLVSRRCAsGpr
++;
2196 MI
.setDesc(get(PPC::STDX
));
2201 case PPC::CFENCE8
: {
2202 auto Val
= MI
.getOperand(0).getReg();
2203 BuildMI(MBB
, MI
, DL
, get(PPC::CMPD
), PPC::CR7
).addReg(Val
).addReg(Val
);
2204 BuildMI(MBB
, MI
, DL
, get(PPC::CTRL_DEP
))
2205 .addImm(PPC::PRED_NE_MINUS
)
2208 MI
.setDesc(get(PPC::ISYNC
));
2209 MI
.RemoveOperand(0);
2216 // Essentially a compile-time implementation of a compare->isel sequence.
2217 // It takes two constants to compare, along with the true/false registers
2218 // and the comparison type (as a subreg to a CR field) and returns one
2219 // of the true/false registers, depending on the comparison results.
2220 static unsigned selectReg(int64_t Imm1
, int64_t Imm2
, unsigned CompareOpc
,
2221 unsigned TrueReg
, unsigned FalseReg
,
2222 unsigned CRSubReg
) {
2223 // Signed comparisons. The immediates are assumed to be sign-extended.
2224 if (CompareOpc
== PPC::CMPWI
|| CompareOpc
== PPC::CMPDI
) {
2226 default: llvm_unreachable("Unknown integer comparison type.");
2228 return Imm1
< Imm2
? TrueReg
: FalseReg
;
2230 return Imm1
> Imm2
? TrueReg
: FalseReg
;
2232 return Imm1
== Imm2
? TrueReg
: FalseReg
;
2235 // Unsigned comparisons.
2236 else if (CompareOpc
== PPC::CMPLWI
|| CompareOpc
== PPC::CMPLDI
) {
2238 default: llvm_unreachable("Unknown integer comparison type.");
2240 return (uint64_t)Imm1
< (uint64_t)Imm2
? TrueReg
: FalseReg
;
2242 return (uint64_t)Imm1
> (uint64_t)Imm2
? TrueReg
: FalseReg
;
2244 return Imm1
== Imm2
? TrueReg
: FalseReg
;
2247 return PPC::NoRegister
;
2250 void PPCInstrInfo::replaceInstrOperandWithImm(MachineInstr
&MI
,
2252 int64_t Imm
) const {
2253 assert(MI
.getOperand(OpNo
).isReg() && "Operand must be a REG");
2254 // Replace the REG with the Immediate.
2255 unsigned InUseReg
= MI
.getOperand(OpNo
).getReg();
2256 MI
.getOperand(OpNo
).ChangeToImmediate(Imm
);
2258 if (empty(MI
.implicit_operands()))
2261 // We need to make sure that the MI didn't have any implicit use
2262 // of this REG any more.
2263 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
2264 int UseOpIdx
= MI
.findRegisterUseOperandIdx(InUseReg
, false, TRI
);
2265 if (UseOpIdx
>= 0) {
2266 MachineOperand
&MO
= MI
.getOperand(UseOpIdx
);
2267 if (MO
.isImplicit())
2268 // The operands must always be in the following order:
2269 // - explicit reg defs,
2270 // - other explicit operands (reg uses, immediates, etc.),
2271 // - implicit reg defs
2272 // - implicit reg uses
2273 // Therefore, removing the implicit operand won't change the explicit
2275 MI
.RemoveOperand(UseOpIdx
);
2279 // Replace an instruction with one that materializes a constant (and sets
2280 // CR0 if the original instruction was a record-form instruction).
2281 void PPCInstrInfo::replaceInstrWithLI(MachineInstr
&MI
,
2282 const LoadImmediateInfo
&LII
) const {
2283 // Remove existing operands.
2284 int OperandToKeep
= LII
.SetCR
? 1 : 0;
2285 for (int i
= MI
.getNumOperands() - 1; i
> OperandToKeep
; i
--)
2286 MI
.RemoveOperand(i
);
2288 // Replace the instruction.
2290 MI
.setDesc(get(LII
.Is64Bit
? PPC::ANDIo8
: PPC::ANDIo
));
2291 // Set the immediate.
2292 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
2293 .addImm(LII
.Imm
).addReg(PPC::CR0
, RegState::ImplicitDefine
);
2297 MI
.setDesc(get(LII
.Is64Bit
? PPC::LI8
: PPC::LI
));
2299 // Set the immediate.
2300 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
2304 MachineInstr
*PPCInstrInfo::getForwardingDefMI(
2306 unsigned &OpNoForForwarding
,
2307 bool &SeenIntermediateUse
) const {
2308 OpNoForForwarding
= ~0U;
2309 MachineInstr
*DefMI
= nullptr;
2310 MachineRegisterInfo
*MRI
= &MI
.getParent()->getParent()->getRegInfo();
2311 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
2312 // If we're in SSA, get the defs through the MRI. Otherwise, only look
2313 // within the basic block to see if the register is defined using an LI/LI8.
2315 for (int i
= 1, e
= MI
.getNumOperands(); i
< e
; i
++) {
2316 if (!MI
.getOperand(i
).isReg())
2318 unsigned Reg
= MI
.getOperand(i
).getReg();
2319 if (!TargetRegisterInfo::isVirtualRegister(Reg
))
2321 unsigned TrueReg
= TRI
->lookThruCopyLike(Reg
, MRI
);
2322 if (TargetRegisterInfo::isVirtualRegister(TrueReg
)) {
2323 DefMI
= MRI
->getVRegDef(TrueReg
);
2324 if (DefMI
->getOpcode() == PPC::LI
|| DefMI
->getOpcode() == PPC::LI8
) {
2325 OpNoForForwarding
= i
;
2331 // Looking back through the definition for each operand could be expensive,
2332 // so exit early if this isn't an instruction that either has an immediate
2333 // form or is already an immediate form that we can handle.
2335 unsigned Opc
= MI
.getOpcode();
2336 bool ConvertibleImmForm
=
2337 Opc
== PPC::CMPWI
|| Opc
== PPC::CMPLWI
||
2338 Opc
== PPC::CMPDI
|| Opc
== PPC::CMPLDI
||
2339 Opc
== PPC::ADDI
|| Opc
== PPC::ADDI8
||
2340 Opc
== PPC::ORI
|| Opc
== PPC::ORI8
||
2341 Opc
== PPC::XORI
|| Opc
== PPC::XORI8
||
2342 Opc
== PPC::RLDICL
|| Opc
== PPC::RLDICLo
||
2343 Opc
== PPC::RLDICL_32
|| Opc
== PPC::RLDICL_32_64
||
2344 Opc
== PPC::RLWINM
|| Opc
== PPC::RLWINMo
||
2345 Opc
== PPC::RLWINM8
|| Opc
== PPC::RLWINM8o
;
2346 if (!instrHasImmForm(MI
, III
, true) && !ConvertibleImmForm
)
2349 // Don't convert or %X, %Y, %Y since that's just a register move.
2350 if ((Opc
== PPC::OR
|| Opc
== PPC::OR8
) &&
2351 MI
.getOperand(1).getReg() == MI
.getOperand(2).getReg())
2353 for (int i
= 1, e
= MI
.getNumOperands(); i
< e
; i
++) {
2354 MachineOperand
&MO
= MI
.getOperand(i
);
2355 SeenIntermediateUse
= false;
2356 if (MO
.isReg() && MO
.isUse() && !MO
.isImplicit()) {
2357 MachineBasicBlock::reverse_iterator E
= MI
.getParent()->rend(), It
= MI
;
2359 unsigned Reg
= MI
.getOperand(i
).getReg();
2360 // MachineInstr::readsRegister only returns true if the machine
2361 // instruction reads the exact register or its super-register. It
2362 // does not consider uses of sub-registers which seems like strange
2363 // behaviour. Nonetheless, if we end up with a 64-bit register here,
2364 // get the corresponding 32-bit register to check.
2365 if (PPC::G8RCRegClass
.contains(Reg
))
2366 Reg
= Reg
- PPC::X0
+ PPC::R0
;
2368 // Is this register defined by some form of add-immediate (including
2369 // load-immediate) within this basic block?
2370 for ( ; It
!= E
; ++It
) {
2371 if (It
->modifiesRegister(Reg
, &getRegisterInfo())) {
2372 switch (It
->getOpcode()) {
2379 OpNoForForwarding
= i
;
2383 } else if (It
->readsRegister(Reg
, &getRegisterInfo()))
2384 // If we see another use of this reg between the def and the MI,
2385 // we want to flat it so the def isn't deleted.
2386 SeenIntermediateUse
= true;
2391 return OpNoForForwarding
== ~0U ? nullptr : DefMI
;
2394 const unsigned *PPCInstrInfo::getStoreOpcodesForSpillArray() const {
2395 static const unsigned OpcodesForSpill
[2][SOK_LastOpcodeSpill
] = {
2397 {PPC::STW
, PPC::STD
, PPC::STFD
, PPC::STFS
, PPC::SPILL_CR
,
2398 PPC::SPILL_CRBIT
, PPC::STVX
, PPC::STXVD2X
, PPC::STXSDX
, PPC::STXSSPX
,
2399 PPC::SPILL_VRSAVE
, PPC::QVSTFDX
, PPC::QVSTFSXs
, PPC::QVSTFDXb
,
2400 PPC::SPILLTOVSR_ST
, PPC::EVSTDD
, PPC::SPESTW
},
2402 {PPC::STW
, PPC::STD
, PPC::STFD
, PPC::STFS
, PPC::SPILL_CR
,
2403 PPC::SPILL_CRBIT
, PPC::STVX
, PPC::STXV
, PPC::DFSTOREf64
, PPC::DFSTOREf32
,
2404 PPC::SPILL_VRSAVE
, PPC::QVSTFDX
, PPC::QVSTFSXs
, PPC::QVSTFDXb
,
2405 PPC::SPILLTOVSR_ST
}};
2407 return OpcodesForSpill
[(Subtarget
.hasP9Vector()) ? 1 : 0];
2410 const unsigned *PPCInstrInfo::getLoadOpcodesForSpillArray() const {
2411 static const unsigned OpcodesForSpill
[2][SOK_LastOpcodeSpill
] = {
2413 {PPC::LWZ
, PPC::LD
, PPC::LFD
, PPC::LFS
, PPC::RESTORE_CR
,
2414 PPC::RESTORE_CRBIT
, PPC::LVX
, PPC::LXVD2X
, PPC::LXSDX
, PPC::LXSSPX
,
2415 PPC::RESTORE_VRSAVE
, PPC::QVLFDX
, PPC::QVLFSXs
, PPC::QVLFDXb
,
2416 PPC::SPILLTOVSR_LD
, PPC::EVLDD
, PPC::SPELWZ
},
2418 {PPC::LWZ
, PPC::LD
, PPC::LFD
, PPC::LFS
, PPC::RESTORE_CR
,
2419 PPC::RESTORE_CRBIT
, PPC::LVX
, PPC::LXV
, PPC::DFLOADf64
, PPC::DFLOADf32
,
2420 PPC::RESTORE_VRSAVE
, PPC::QVLFDX
, PPC::QVLFSXs
, PPC::QVLFDXb
,
2421 PPC::SPILLTOVSR_LD
}};
2423 return OpcodesForSpill
[(Subtarget
.hasP9Vector()) ? 1 : 0];
2426 // If this instruction has an immediate form and one of its operands is a
2427 // result of a load-immediate or an add-immediate, convert it to
2428 // the immediate form if the constant is in range.
2429 bool PPCInstrInfo::convertToImmediateForm(MachineInstr
&MI
,
2430 MachineInstr
**KilledDef
) const {
2431 MachineFunction
*MF
= MI
.getParent()->getParent();
2432 MachineRegisterInfo
*MRI
= &MF
->getRegInfo();
2433 bool PostRA
= !MRI
->isSSA();
2434 bool SeenIntermediateUse
= true;
2435 unsigned ForwardingOperand
= ~0U;
2436 MachineInstr
*DefMI
= getForwardingDefMI(MI
, ForwardingOperand
,
2437 SeenIntermediateUse
);
2440 assert(ForwardingOperand
< MI
.getNumOperands() &&
2441 "The forwarding operand needs to be valid at this point");
2442 bool KillFwdDefMI
= !SeenIntermediateUse
&&
2443 MI
.getOperand(ForwardingOperand
).isKill();
2444 if (KilledDef
&& KillFwdDefMI
)
2448 bool HasImmForm
= instrHasImmForm(MI
, III
, PostRA
);
2449 // If this is a reg+reg instruction that has a reg+imm form,
2450 // and one of the operands is produced by an add-immediate,
2451 // try to convert it.
2452 if (HasImmForm
&& transformToImmFormFedByAdd(MI
, III
, ForwardingOperand
,
2453 *DefMI
, KillFwdDefMI
))
2456 if ((DefMI
->getOpcode() != PPC::LI
&& DefMI
->getOpcode() != PPC::LI8
) ||
2457 !DefMI
->getOperand(1).isImm())
2460 int64_t Immediate
= DefMI
->getOperand(1).getImm();
2461 // Sign-extend to 64-bits.
2462 int64_t SExtImm
= ((uint64_t)Immediate
& ~0x7FFFuLL
) != 0 ?
2463 (Immediate
| 0xFFFFFFFFFFFF0000) : Immediate
;
2465 // If this is a reg+reg instruction that has a reg+imm form,
2466 // and one of the operands is produced by LI, convert it now.
2468 return transformToImmFormFedByLI(MI
, III
, ForwardingOperand
, SExtImm
);
2470 bool ReplaceWithLI
= false;
2471 bool Is64BitLI
= false;
2474 unsigned Opc
= MI
.getOpcode();
2476 default: return false;
2478 // FIXME: Any branches conditional on such a comparison can be made
2479 // unconditional. At this time, this happens too infrequently to be worth
2480 // the implementation effort, but if that ever changes, we could convert
2481 // such a pattern here.
2486 // Doing this post-RA would require dataflow analysis to reliably find uses
2487 // of the CR register set by the compare.
2490 // If a compare-immediate is fed by an immediate and is itself an input of
2491 // an ISEL (the most common case) into a COPY of the correct register.
2492 bool Changed
= false;
2493 unsigned DefReg
= MI
.getOperand(0).getReg();
2494 int64_t Comparand
= MI
.getOperand(2).getImm();
2495 int64_t SExtComparand
= ((uint64_t)Comparand
& ~0x7FFFuLL
) != 0 ?
2496 (Comparand
| 0xFFFFFFFFFFFF0000) : Comparand
;
2498 for (auto &CompareUseMI
: MRI
->use_instructions(DefReg
)) {
2499 unsigned UseOpc
= CompareUseMI
.getOpcode();
2500 if (UseOpc
!= PPC::ISEL
&& UseOpc
!= PPC::ISEL8
)
2502 unsigned CRSubReg
= CompareUseMI
.getOperand(3).getSubReg();
2503 unsigned TrueReg
= CompareUseMI
.getOperand(1).getReg();
2504 unsigned FalseReg
= CompareUseMI
.getOperand(2).getReg();
2505 unsigned RegToCopy
= selectReg(SExtImm
, SExtComparand
, Opc
, TrueReg
,
2506 FalseReg
, CRSubReg
);
2507 if (RegToCopy
== PPC::NoRegister
)
2509 // Can't use PPC::COPY to copy PPC::ZERO[8]. Convert it to LI[8] 0.
2510 if (RegToCopy
== PPC::ZERO
|| RegToCopy
== PPC::ZERO8
) {
2511 CompareUseMI
.setDesc(get(UseOpc
== PPC::ISEL8
? PPC::LI8
: PPC::LI
));
2512 replaceInstrOperandWithImm(CompareUseMI
, 1, 0);
2513 CompareUseMI
.RemoveOperand(3);
2514 CompareUseMI
.RemoveOperand(2);
2518 dbgs() << "Found LI -> CMPI -> ISEL, replacing with a copy.\n");
2519 LLVM_DEBUG(DefMI
->dump(); MI
.dump(); CompareUseMI
.dump());
2520 LLVM_DEBUG(dbgs() << "Is converted to:\n");
2521 // Convert to copy and remove unneeded operands.
2522 CompareUseMI
.setDesc(get(PPC::COPY
));
2523 CompareUseMI
.RemoveOperand(3);
2524 CompareUseMI
.RemoveOperand(RegToCopy
== TrueReg
? 2 : 1);
2525 CmpIselsConverted
++;
2527 LLVM_DEBUG(CompareUseMI
.dump());
2531 // This may end up incremented multiple times since this function is called
2532 // during a fixed-point transformation, but it is only meant to indicate the
2533 // presence of this opportunity.
2534 MissedConvertibleImmediateInstrs
++;
2538 // Immediate forms - may simply be convertable to an LI.
2541 // Does the sum fit in a 16-bit signed field?
2542 int64_t Addend
= MI
.getOperand(2).getImm();
2543 if (isInt
<16>(Addend
+ SExtImm
)) {
2544 ReplaceWithLI
= true;
2545 Is64BitLI
= Opc
== PPC::ADDI8
;
2546 NewImm
= Addend
+ SExtImm
;
2553 case PPC::RLDICL_32
:
2554 case PPC::RLDICL_32_64
: {
2555 // Use APInt's rotate function.
2556 int64_t SH
= MI
.getOperand(2).getImm();
2557 int64_t MB
= MI
.getOperand(3).getImm();
2558 APInt
InVal((Opc
== PPC::RLDICL
|| Opc
== PPC::RLDICLo
) ?
2559 64 : 32, SExtImm
, true);
2560 InVal
= InVal
.rotl(SH
);
2561 uint64_t Mask
= (1LLU << (63 - MB
+ 1)) - 1;
2563 // Can't replace negative values with an LI as that will sign-extend
2564 // and not clear the left bits. If we're setting the CR bit, we will use
2565 // ANDIo which won't sign extend, so that's safe.
2566 if (isUInt
<15>(InVal
.getSExtValue()) ||
2567 (Opc
== PPC::RLDICLo
&& isUInt
<16>(InVal
.getSExtValue()))) {
2568 ReplaceWithLI
= true;
2569 Is64BitLI
= Opc
!= PPC::RLDICL_32
;
2570 NewImm
= InVal
.getSExtValue();
2571 SetCR
= Opc
== PPC::RLDICLo
;
2579 case PPC::RLWINM8o
: {
2580 int64_t SH
= MI
.getOperand(2).getImm();
2581 int64_t MB
= MI
.getOperand(3).getImm();
2582 int64_t ME
= MI
.getOperand(4).getImm();
2583 APInt
InVal(32, SExtImm
, true);
2584 InVal
= InVal
.rotl(SH
);
2585 // Set the bits ( MB + 32 ) to ( ME + 32 ).
2586 uint64_t Mask
= ((1LLU << (32 - MB
)) - 1) & ~((1LLU << (31 - ME
)) - 1);
2588 // Can't replace negative values with an LI as that will sign-extend
2589 // and not clear the left bits. If we're setting the CR bit, we will use
2590 // ANDIo which won't sign extend, so that's safe.
2591 bool ValueFits
= isUInt
<15>(InVal
.getSExtValue());
2592 ValueFits
|= ((Opc
== PPC::RLWINMo
|| Opc
== PPC::RLWINM8o
) &&
2593 isUInt
<16>(InVal
.getSExtValue()));
2595 ReplaceWithLI
= true;
2596 Is64BitLI
= Opc
== PPC::RLWINM8
|| Opc
== PPC::RLWINM8o
;
2597 NewImm
= InVal
.getSExtValue();
2598 SetCR
= Opc
== PPC::RLWINMo
|| Opc
== PPC::RLWINM8o
;
2607 int64_t LogicalImm
= MI
.getOperand(2).getImm();
2609 if (Opc
== PPC::ORI
|| Opc
== PPC::ORI8
)
2610 Result
= LogicalImm
| SExtImm
;
2612 Result
= LogicalImm
^ SExtImm
;
2613 if (isInt
<16>(Result
)) {
2614 ReplaceWithLI
= true;
2615 Is64BitLI
= Opc
== PPC::ORI8
|| Opc
== PPC::XORI8
;
2623 if (ReplaceWithLI
) {
2624 // We need to be careful with CR-setting instructions we're replacing.
2626 // We don't know anything about uses when we're out of SSA, so only
2627 // replace if the new immediate will be reproduced.
2628 bool ImmChanged
= (SExtImm
& NewImm
) != NewImm
;
2629 if (PostRA
&& ImmChanged
)
2633 // If the defining load-immediate has no other uses, we can just replace
2634 // the immediate with the new immediate.
2635 if (MRI
->hasOneUse(DefMI
->getOperand(0).getReg()))
2636 DefMI
->getOperand(1).setImm(NewImm
);
2638 // If we're not using the GPR result of the CR-setting instruction, we
2639 // just need to and with zero/non-zero depending on the new immediate.
2640 else if (MRI
->use_empty(MI
.getOperand(0).getReg())) {
2642 assert(Immediate
&& "Transformation converted zero to non-zero?");
2646 else if (ImmChanged
)
2651 LLVM_DEBUG(dbgs() << "Replacing instruction:\n");
2652 LLVM_DEBUG(MI
.dump());
2653 LLVM_DEBUG(dbgs() << "Fed by:\n");
2654 LLVM_DEBUG(DefMI
->dump());
2655 LoadImmediateInfo LII
;
2657 LII
.Is64Bit
= Is64BitLI
;
2659 // If we're setting the CR, the original load-immediate must be kept (as an
2660 // operand to ANDIo/ANDI8o).
2661 if (KilledDef
&& SetCR
)
2662 *KilledDef
= nullptr;
2663 replaceInstrWithLI(MI
, LII
);
2664 LLVM_DEBUG(dbgs() << "With:\n");
2665 LLVM_DEBUG(MI
.dump());
2671 static bool isVFReg(unsigned Reg
) {
2672 return PPC::VFRCRegClass
.contains(Reg
);
2675 bool PPCInstrInfo::instrHasImmForm(const MachineInstr
&MI
,
2676 ImmInstrInfo
&III
, bool PostRA
) const {
2677 unsigned Opc
= MI
.getOpcode();
2678 // The vast majority of the instructions would need their operand 2 replaced
2679 // with an immediate when switching to the reg+imm form. A marked exception
2680 // are the update form loads/stores for which a constant operand 2 would need
2681 // to turn into a displacement and move operand 1 to the operand 2 position.
2683 III
.OpNoForForwarding
= 2;
2685 III
.ImmMustBeMultipleOf
= 1;
2686 III
.TruncateImmTo
= 0;
2687 III
.IsSummingOperands
= false;
2689 default: return false;
2692 III
.SignedImm
= true;
2693 III
.ZeroIsSpecialOrig
= 0;
2694 III
.ZeroIsSpecialNew
= 1;
2695 III
.IsCommutative
= true;
2696 III
.IsSummingOperands
= true;
2697 III
.ImmOpcode
= Opc
== PPC::ADD4
? PPC::ADDI
: PPC::ADDI8
;
2701 III
.SignedImm
= true;
2702 III
.ZeroIsSpecialOrig
= 0;
2703 III
.ZeroIsSpecialNew
= 0;
2704 III
.IsCommutative
= true;
2705 III
.IsSummingOperands
= true;
2706 III
.ImmOpcode
= Opc
== PPC::ADDC
? PPC::ADDIC
: PPC::ADDIC8
;
2709 III
.SignedImm
= true;
2710 III
.ZeroIsSpecialOrig
= 0;
2711 III
.ZeroIsSpecialNew
= 0;
2712 III
.IsCommutative
= true;
2713 III
.IsSummingOperands
= true;
2714 III
.ImmOpcode
= PPC::ADDICo
;
2718 III
.SignedImm
= true;
2719 III
.ZeroIsSpecialOrig
= 0;
2720 III
.ZeroIsSpecialNew
= 0;
2721 III
.IsCommutative
= false;
2722 III
.ImmOpcode
= Opc
== PPC::SUBFC
? PPC::SUBFIC
: PPC::SUBFIC8
;
2726 III
.SignedImm
= true;
2727 III
.ZeroIsSpecialOrig
= 0;
2728 III
.ZeroIsSpecialNew
= 0;
2729 III
.IsCommutative
= false;
2730 III
.ImmOpcode
= Opc
== PPC::CMPW
? PPC::CMPWI
: PPC::CMPDI
;
2734 III
.SignedImm
= false;
2735 III
.ZeroIsSpecialOrig
= 0;
2736 III
.ZeroIsSpecialNew
= 0;
2737 III
.IsCommutative
= false;
2738 III
.ImmOpcode
= Opc
== PPC::CMPLW
? PPC::CMPLWI
: PPC::CMPLDI
;
2746 III
.SignedImm
= false;
2747 III
.ZeroIsSpecialOrig
= 0;
2748 III
.ZeroIsSpecialNew
= 0;
2749 III
.IsCommutative
= true;
2751 default: llvm_unreachable("Unknown opcode");
2752 case PPC::ANDo
: III
.ImmOpcode
= PPC::ANDIo
; break;
2753 case PPC::AND8o
: III
.ImmOpcode
= PPC::ANDIo8
; break;
2754 case PPC::OR
: III
.ImmOpcode
= PPC::ORI
; break;
2755 case PPC::OR8
: III
.ImmOpcode
= PPC::ORI8
; break;
2756 case PPC::XOR
: III
.ImmOpcode
= PPC::XORI
; break;
2757 case PPC::XOR8
: III
.ImmOpcode
= PPC::XORI8
; break;
2774 III
.SignedImm
= false;
2775 III
.ZeroIsSpecialOrig
= 0;
2776 III
.ZeroIsSpecialNew
= 0;
2777 III
.IsCommutative
= false;
2778 // This isn't actually true, but the instructions ignore any of the
2779 // upper bits, so any immediate loaded with an LI is acceptable.
2780 // This does not apply to shift right algebraic because a value
2781 // out of range will produce a -1/0.
2783 if (Opc
== PPC::RLWNM
|| Opc
== PPC::RLWNM8
||
2784 Opc
== PPC::RLWNMo
|| Opc
== PPC::RLWNM8o
)
2785 III
.TruncateImmTo
= 5;
2787 III
.TruncateImmTo
= 6;
2789 default: llvm_unreachable("Unknown opcode");
2790 case PPC::RLWNM
: III
.ImmOpcode
= PPC::RLWINM
; break;
2791 case PPC::RLWNM8
: III
.ImmOpcode
= PPC::RLWINM8
; break;
2792 case PPC::RLWNMo
: III
.ImmOpcode
= PPC::RLWINMo
; break;
2793 case PPC::RLWNM8o
: III
.ImmOpcode
= PPC::RLWINM8o
; break;
2794 case PPC::SLW
: III
.ImmOpcode
= PPC::RLWINM
; break;
2795 case PPC::SLW8
: III
.ImmOpcode
= PPC::RLWINM8
; break;
2796 case PPC::SLWo
: III
.ImmOpcode
= PPC::RLWINMo
; break;
2797 case PPC::SLW8o
: III
.ImmOpcode
= PPC::RLWINM8o
; break;
2798 case PPC::SRW
: III
.ImmOpcode
= PPC::RLWINM
; break;
2799 case PPC::SRW8
: III
.ImmOpcode
= PPC::RLWINM8
; break;
2800 case PPC::SRWo
: III
.ImmOpcode
= PPC::RLWINMo
; break;
2801 case PPC::SRW8o
: III
.ImmOpcode
= PPC::RLWINM8o
; break;
2804 III
.TruncateImmTo
= 0;
2805 III
.ImmOpcode
= PPC::SRAWI
;
2809 III
.TruncateImmTo
= 0;
2810 III
.ImmOpcode
= PPC::SRAWIo
;
2824 III
.SignedImm
= false;
2825 III
.ZeroIsSpecialOrig
= 0;
2826 III
.ZeroIsSpecialNew
= 0;
2827 III
.IsCommutative
= false;
2828 // This isn't actually true, but the instructions ignore any of the
2829 // upper bits, so any immediate loaded with an LI is acceptable.
2830 // This does not apply to shift right algebraic because a value
2831 // out of range will produce a -1/0.
2833 if (Opc
== PPC::RLDCL
|| Opc
== PPC::RLDCLo
||
2834 Opc
== PPC::RLDCR
|| Opc
== PPC::RLDCRo
)
2835 III
.TruncateImmTo
= 6;
2837 III
.TruncateImmTo
= 7;
2839 default: llvm_unreachable("Unknown opcode");
2840 case PPC::RLDCL
: III
.ImmOpcode
= PPC::RLDICL
; break;
2841 case PPC::RLDCLo
: III
.ImmOpcode
= PPC::RLDICLo
; break;
2842 case PPC::RLDCR
: III
.ImmOpcode
= PPC::RLDICR
; break;
2843 case PPC::RLDCRo
: III
.ImmOpcode
= PPC::RLDICRo
; break;
2844 case PPC::SLD
: III
.ImmOpcode
= PPC::RLDICR
; break;
2845 case PPC::SLDo
: III
.ImmOpcode
= PPC::RLDICRo
; break;
2846 case PPC::SRD
: III
.ImmOpcode
= PPC::RLDICL
; break;
2847 case PPC::SRDo
: III
.ImmOpcode
= PPC::RLDICLo
; break;
2850 III
.TruncateImmTo
= 0;
2851 III
.ImmOpcode
= PPC::SRADI
;
2855 III
.TruncateImmTo
= 0;
2856 III
.ImmOpcode
= PPC::SRADIo
;
2860 // Loads and stores:
2882 III
.SignedImm
= true;
2883 III
.ZeroIsSpecialOrig
= 1;
2884 III
.ZeroIsSpecialNew
= 2;
2885 III
.IsCommutative
= true;
2886 III
.IsSummingOperands
= true;
2888 III
.OpNoForForwarding
= 2;
2890 default: llvm_unreachable("Unknown opcode");
2891 case PPC::LBZX
: III
.ImmOpcode
= PPC::LBZ
; break;
2892 case PPC::LBZX8
: III
.ImmOpcode
= PPC::LBZ8
; break;
2893 case PPC::LHZX
: III
.ImmOpcode
= PPC::LHZ
; break;
2894 case PPC::LHZX8
: III
.ImmOpcode
= PPC::LHZ8
; break;
2895 case PPC::LHAX
: III
.ImmOpcode
= PPC::LHA
; break;
2896 case PPC::LHAX8
: III
.ImmOpcode
= PPC::LHA8
; break;
2897 case PPC::LWZX
: III
.ImmOpcode
= PPC::LWZ
; break;
2898 case PPC::LWZX8
: III
.ImmOpcode
= PPC::LWZ8
; break;
2900 III
.ImmOpcode
= PPC::LWA
;
2901 III
.ImmMustBeMultipleOf
= 4;
2903 case PPC::LDX
: III
.ImmOpcode
= PPC::LD
; III
.ImmMustBeMultipleOf
= 4; break;
2904 case PPC::LFSX
: III
.ImmOpcode
= PPC::LFS
; break;
2905 case PPC::LFDX
: III
.ImmOpcode
= PPC::LFD
; break;
2906 case PPC::STBX
: III
.ImmOpcode
= PPC::STB
; break;
2907 case PPC::STBX8
: III
.ImmOpcode
= PPC::STB8
; break;
2908 case PPC::STHX
: III
.ImmOpcode
= PPC::STH
; break;
2909 case PPC::STHX8
: III
.ImmOpcode
= PPC::STH8
; break;
2910 case PPC::STWX
: III
.ImmOpcode
= PPC::STW
; break;
2911 case PPC::STWX8
: III
.ImmOpcode
= PPC::STW8
; break;
2913 III
.ImmOpcode
= PPC::STD
;
2914 III
.ImmMustBeMultipleOf
= 4;
2916 case PPC::STFSX
: III
.ImmOpcode
= PPC::STFS
; break;
2917 case PPC::STFDX
: III
.ImmOpcode
= PPC::STFD
; break;
2940 III
.SignedImm
= true;
2941 III
.ZeroIsSpecialOrig
= 2;
2942 III
.ZeroIsSpecialNew
= 3;
2943 III
.IsCommutative
= false;
2944 III
.IsSummingOperands
= true;
2946 III
.OpNoForForwarding
= 3;
2948 default: llvm_unreachable("Unknown opcode");
2949 case PPC::LBZUX
: III
.ImmOpcode
= PPC::LBZU
; break;
2950 case PPC::LBZUX8
: III
.ImmOpcode
= PPC::LBZU8
; break;
2951 case PPC::LHZUX
: III
.ImmOpcode
= PPC::LHZU
; break;
2952 case PPC::LHZUX8
: III
.ImmOpcode
= PPC::LHZU8
; break;
2953 case PPC::LHAUX
: III
.ImmOpcode
= PPC::LHAU
; break;
2954 case PPC::LHAUX8
: III
.ImmOpcode
= PPC::LHAU8
; break;
2955 case PPC::LWZUX
: III
.ImmOpcode
= PPC::LWZU
; break;
2956 case PPC::LWZUX8
: III
.ImmOpcode
= PPC::LWZU8
; break;
2958 III
.ImmOpcode
= PPC::LDU
;
2959 III
.ImmMustBeMultipleOf
= 4;
2961 case PPC::LFSUX
: III
.ImmOpcode
= PPC::LFSU
; break;
2962 case PPC::LFDUX
: III
.ImmOpcode
= PPC::LFDU
; break;
2963 case PPC::STBUX
: III
.ImmOpcode
= PPC::STBU
; break;
2964 case PPC::STBUX8
: III
.ImmOpcode
= PPC::STBU8
; break;
2965 case PPC::STHUX
: III
.ImmOpcode
= PPC::STHU
; break;
2966 case PPC::STHUX8
: III
.ImmOpcode
= PPC::STHU8
; break;
2967 case PPC::STWUX
: III
.ImmOpcode
= PPC::STWU
; break;
2968 case PPC::STWUX8
: III
.ImmOpcode
= PPC::STWU8
; break;
2970 III
.ImmOpcode
= PPC::STDU
;
2971 III
.ImmMustBeMultipleOf
= 4;
2973 case PPC::STFSUX
: III
.ImmOpcode
= PPC::STFSU
; break;
2974 case PPC::STFDUX
: III
.ImmOpcode
= PPC::STFDU
; break;
2977 // Power9 and up only. For some of these, the X-Form version has access to all
2978 // 64 VSR's whereas the D-Form only has access to the VR's. We replace those
2979 // with pseudo-ops pre-ra and for post-ra, we check that the register loaded
2980 // into or stored from is one of the VR registers.
2987 case PPC::XFLOADf32
:
2988 case PPC::XFLOADf64
:
2989 case PPC::XFSTOREf32
:
2990 case PPC::XFSTOREf64
:
2991 if (!Subtarget
.hasP9Vector())
2993 III
.SignedImm
= true;
2994 III
.ZeroIsSpecialOrig
= 1;
2995 III
.ZeroIsSpecialNew
= 2;
2996 III
.IsCommutative
= true;
2997 III
.IsSummingOperands
= true;
2999 III
.OpNoForForwarding
= 2;
3000 III
.ImmMustBeMultipleOf
= 4;
3002 default: llvm_unreachable("Unknown opcode");
3004 III
.ImmOpcode
= PPC::LXV
;
3005 III
.ImmMustBeMultipleOf
= 16;
3009 if (isVFReg(MI
.getOperand(0).getReg()))
3010 III
.ImmOpcode
= PPC::LXSSP
;
3012 III
.ImmOpcode
= PPC::LFS
;
3013 III
.ImmMustBeMultipleOf
= 1;
3018 case PPC::XFLOADf32
:
3019 III
.ImmOpcode
= PPC::DFLOADf32
;
3023 if (isVFReg(MI
.getOperand(0).getReg()))
3024 III
.ImmOpcode
= PPC::LXSD
;
3026 III
.ImmOpcode
= PPC::LFD
;
3027 III
.ImmMustBeMultipleOf
= 1;
3032 case PPC::XFLOADf64
:
3033 III
.ImmOpcode
= PPC::DFLOADf64
;
3036 III
.ImmOpcode
= PPC::STXV
;
3037 III
.ImmMustBeMultipleOf
= 16;
3041 if (isVFReg(MI
.getOperand(0).getReg()))
3042 III
.ImmOpcode
= PPC::STXSSP
;
3044 III
.ImmOpcode
= PPC::STFS
;
3045 III
.ImmMustBeMultipleOf
= 1;
3050 case PPC::XFSTOREf32
:
3051 III
.ImmOpcode
= PPC::DFSTOREf32
;
3055 if (isVFReg(MI
.getOperand(0).getReg()))
3056 III
.ImmOpcode
= PPC::STXSD
;
3058 III
.ImmOpcode
= PPC::STFD
;
3059 III
.ImmMustBeMultipleOf
= 1;
3064 case PPC::XFSTOREf64
:
3065 III
.ImmOpcode
= PPC::DFSTOREf64
;
3073 // Utility function for swaping two arbitrary operands of an instruction.
3074 static void swapMIOperands(MachineInstr
&MI
, unsigned Op1
, unsigned Op2
) {
3075 assert(Op1
!= Op2
&& "Cannot swap operand with itself.");
3077 unsigned MaxOp
= std::max(Op1
, Op2
);
3078 unsigned MinOp
= std::min(Op1
, Op2
);
3079 MachineOperand MOp1
= MI
.getOperand(MinOp
);
3080 MachineOperand MOp2
= MI
.getOperand(MaxOp
);
3081 MI
.RemoveOperand(std::max(Op1
, Op2
));
3082 MI
.RemoveOperand(std::min(Op1
, Op2
));
3084 // If the operands we are swapping are the two at the end (the common case)
3085 // we can just remove both and add them in the opposite order.
3086 if (MaxOp
- MinOp
== 1 && MI
.getNumOperands() == MinOp
) {
3087 MI
.addOperand(MOp2
);
3088 MI
.addOperand(MOp1
);
3090 // Store all operands in a temporary vector, remove them and re-add in the
3092 SmallVector
<MachineOperand
, 2> MOps
;
3093 unsigned TotalOps
= MI
.getNumOperands() + 2; // We've already removed 2 ops.
3094 for (unsigned i
= MI
.getNumOperands() - 1; i
>= MinOp
; i
--) {
3095 MOps
.push_back(MI
.getOperand(i
));
3096 MI
.RemoveOperand(i
);
3098 // MOp2 needs to be added next.
3099 MI
.addOperand(MOp2
);
3100 // Now add the rest.
3101 for (unsigned i
= MI
.getNumOperands(); i
< TotalOps
; i
++) {
3103 MI
.addOperand(MOp1
);
3105 MI
.addOperand(MOps
.back());
3112 // Check if the 'MI' that has the index OpNoForForwarding
3113 // meets the requirement described in the ImmInstrInfo.
3114 bool PPCInstrInfo::isUseMIElgibleForForwarding(MachineInstr
&MI
,
3115 const ImmInstrInfo
&III
,
3116 unsigned OpNoForForwarding
3118 // As the algorithm of checking for PPC::ZERO/PPC::ZERO8
3119 // would not work pre-RA, we can only do the check post RA.
3120 MachineRegisterInfo
&MRI
= MI
.getParent()->getParent()->getRegInfo();
3124 // Cannot do the transform if MI isn't summing the operands.
3125 if (!III
.IsSummingOperands
)
3128 // The instruction we are trying to replace must have the ZeroIsSpecialOrig set.
3129 if (!III
.ZeroIsSpecialOrig
)
3132 // We cannot do the transform if the operand we are trying to replace
3133 // isn't the same as the operand the instruction allows.
3134 if (OpNoForForwarding
!= III
.OpNoForForwarding
)
3137 // Check if the instruction we are trying to transform really has
3138 // the special zero register as its operand.
3139 if (MI
.getOperand(III
.ZeroIsSpecialOrig
).getReg() != PPC::ZERO
&&
3140 MI
.getOperand(III
.ZeroIsSpecialOrig
).getReg() != PPC::ZERO8
)
3143 // This machine instruction is convertible if it is,
3144 // 1. summing the operands.
3145 // 2. one of the operands is special zero register.
3146 // 3. the operand we are trying to replace is allowed by the MI.
3150 // Check if the DefMI is the add inst and set the ImmMO and RegMO
3152 bool PPCInstrInfo::isDefMIElgibleForForwarding(MachineInstr
&DefMI
,
3153 const ImmInstrInfo
&III
,
3154 MachineOperand
*&ImmMO
,
3155 MachineOperand
*&RegMO
) const {
3156 unsigned Opc
= DefMI
.getOpcode();
3157 if (Opc
!= PPC::ADDItocL
&& Opc
!= PPC::ADDI
&& Opc
!= PPC::ADDI8
)
3160 assert(DefMI
.getNumOperands() >= 3 &&
3161 "Add inst must have at least three operands");
3162 RegMO
= &DefMI
.getOperand(1);
3163 ImmMO
= &DefMI
.getOperand(2);
3165 // This DefMI is elgible for forwarding if it is:
3167 // 2. one of the operands is Imm/CPI/Global.
3168 return isAnImmediateOperand(*ImmMO
);
3171 bool PPCInstrInfo::isRegElgibleForForwarding(const MachineOperand
&RegMO
,
3172 const MachineInstr
&DefMI
,
3173 const MachineInstr
&MI
,
3178 // z = lfdx 0, x -> z = lfd imm(y)
3179 // The Reg "y" can be forwarded to the MI(z) only when there is no DEF
3180 // of "y" between the DEF of "x" and "z".
3181 // The query is only valid post RA.
3182 const MachineRegisterInfo
&MRI
= MI
.getParent()->getParent()->getRegInfo();
3186 // MachineInstr::readsRegister only returns true if the machine
3187 // instruction reads the exact register or its super-register. It
3188 // does not consider uses of sub-registers which seems like strange
3189 // behaviour. Nonetheless, if we end up with a 64-bit register here,
3190 // get the corresponding 32-bit register to check.
3191 unsigned Reg
= RegMO
.getReg();
3192 if (PPC::G8RCRegClass
.contains(Reg
))
3193 Reg
= Reg
- PPC::X0
+ PPC::R0
;
3195 // Walking the inst in reverse(MI-->DefMI) to get the last DEF of the Reg.
3196 MachineBasicBlock::const_reverse_iterator It
= MI
;
3197 MachineBasicBlock::const_reverse_iterator E
= MI
.getParent()->rend();
3199 for (; It
!= E
; ++It
) {
3200 if (It
->modifiesRegister(Reg
, &getRegisterInfo()) && (&*It
) != &DefMI
)
3202 // Made it to DefMI without encountering a clobber.
3203 if ((&*It
) == &DefMI
)
3206 assert((&*It
) == &DefMI
&& "DefMI is missing");
3208 // If DefMI also uses the register to be forwarded, we can only forward it
3209 // if DefMI is being erased.
3210 if (DefMI
.readsRegister(Reg
, &getRegisterInfo()))
3216 bool PPCInstrInfo::isImmElgibleForForwarding(const MachineOperand
&ImmMO
,
3217 const MachineInstr
&DefMI
,
3218 const ImmInstrInfo
&III
,
3219 int64_t &Imm
) const {
3220 assert(isAnImmediateOperand(ImmMO
) && "ImmMO is NOT an immediate");
3221 if (DefMI
.getOpcode() == PPC::ADDItocL
) {
3222 // The operand for ADDItocL is CPI, which isn't imm at compiling time,
3223 // However, we know that, it is 16-bit width, and has the alignment of 4.
3224 // Check if the instruction met the requirement.
3225 if (III
.ImmMustBeMultipleOf
> 4 ||
3226 III
.TruncateImmTo
|| III
.ImmWidth
!= 16)
3229 // Going from XForm to DForm loads means that the displacement needs to be
3230 // not just an immediate but also a multiple of 4, or 16 depending on the
3231 // load. A DForm load cannot be represented if it is a multiple of say 2.
3232 // XForm loads do not have this restriction.
3233 if (ImmMO
.isGlobal() &&
3234 ImmMO
.getGlobal()->getAlignment() < III
.ImmMustBeMultipleOf
)
3240 if (ImmMO
.isImm()) {
3241 // It is Imm, we need to check if the Imm fit the range.
3242 int64_t Immediate
= ImmMO
.getImm();
3243 // Sign-extend to 64-bits.
3244 Imm
= ((uint64_t)Immediate
& ~0x7FFFuLL
) != 0 ?
3245 (Immediate
| 0xFFFFFFFFFFFF0000) : Immediate
;
3247 if (Imm
% III
.ImmMustBeMultipleOf
)
3249 if (III
.TruncateImmTo
)
3250 Imm
&= ((1 << III
.TruncateImmTo
) - 1);
3251 if (III
.SignedImm
) {
3252 APInt
ActualValue(64, Imm
, true);
3253 if (!ActualValue
.isSignedIntN(III
.ImmWidth
))
3256 uint64_t UnsignedMax
= (1 << III
.ImmWidth
) - 1;
3257 if ((uint64_t)Imm
> UnsignedMax
)
3264 // This ImmMO is forwarded if it meets the requriement describle
3269 // If an X-Form instruction is fed by an add-immediate and one of its operands
3270 // is the literal zero, attempt to forward the source of the add-immediate to
3271 // the corresponding D-Form instruction with the displacement coming from
3272 // the immediate being added.
3273 bool PPCInstrInfo::transformToImmFormFedByAdd(MachineInstr
&MI
,
3274 const ImmInstrInfo
&III
,
3275 unsigned OpNoForForwarding
,
3276 MachineInstr
&DefMI
,
3277 bool KillDefMI
) const {
3280 // x = addi reg, imm <----- DefMI
3281 // y = op 0 , x <----- MI
3283 // OpNoForForwarding
3284 // Check if the MI meet the requirement described in the III.
3285 if (!isUseMIElgibleForForwarding(MI
, III
, OpNoForForwarding
))
3288 // Check if the DefMI meet the requirement
3289 // described in the III. If yes, set the ImmMO and RegMO accordingly.
3290 MachineOperand
*ImmMO
= nullptr;
3291 MachineOperand
*RegMO
= nullptr;
3292 if (!isDefMIElgibleForForwarding(DefMI
, III
, ImmMO
, RegMO
))
3294 assert(ImmMO
&& RegMO
&& "Imm and Reg operand must have been set");
3296 // As we get the Imm operand now, we need to check if the ImmMO meet
3297 // the requirement described in the III. If yes set the Imm.
3299 if (!isImmElgibleForForwarding(*ImmMO
, DefMI
, III
, Imm
))
3302 // Check if the RegMO can be forwarded to MI.
3303 if (!isRegElgibleForForwarding(*RegMO
, DefMI
, MI
, KillDefMI
))
3306 // We know that, the MI and DefMI both meet the pattern, and
3307 // the Imm also meet the requirement with the new Imm-form.
3308 // It is safe to do the transformation now.
3309 LLVM_DEBUG(dbgs() << "Replacing instruction:\n");
3310 LLVM_DEBUG(MI
.dump());
3311 LLVM_DEBUG(dbgs() << "Fed by:\n");
3312 LLVM_DEBUG(DefMI
.dump());
3314 // Update the base reg first.
3315 MI
.getOperand(III
.OpNoForForwarding
).ChangeToRegister(RegMO
->getReg(),
3319 // Then, update the imm.
3320 if (ImmMO
->isImm()) {
3321 // If the ImmMO is Imm, change the operand that has ZERO to that Imm
3323 replaceInstrOperandWithImm(MI
, III
.ZeroIsSpecialOrig
, Imm
);
3326 // Otherwise, it is Constant Pool Index(CPI) or Global,
3327 // which is relocation in fact. We need to replace the special zero
3328 // register with ImmMO.
3329 // Before that, we need to fixup the target flags for imm.
3330 // For some reason, we miss to set the flag for the ImmMO if it is CPI.
3331 if (DefMI
.getOpcode() == PPC::ADDItocL
)
3332 ImmMO
->setTargetFlags(PPCII::MO_TOC_LO
);
3334 // MI didn't have the interface such as MI.setOperand(i) though
3335 // it has MI.getOperand(i). To repalce the ZERO MachineOperand with
3336 // ImmMO, we need to remove ZERO operand and all the operands behind it,
3337 // and, add the ImmMO, then, move back all the operands behind ZERO.
3338 SmallVector
<MachineOperand
, 2> MOps
;
3339 for (unsigned i
= MI
.getNumOperands() - 1; i
>= III
.ZeroIsSpecialOrig
; i
--) {
3340 MOps
.push_back(MI
.getOperand(i
));
3341 MI
.RemoveOperand(i
);
3344 // Remove the last MO in the list, which is ZERO operand in fact.
3346 // Add the imm operand.
3347 MI
.addOperand(*ImmMO
);
3348 // Now add the rest back.
3349 for (auto &MO
: MOps
)
3353 // Update the opcode.
3354 MI
.setDesc(get(III
.ImmOpcode
));
3356 LLVM_DEBUG(dbgs() << "With:\n");
3357 LLVM_DEBUG(MI
.dump());
3362 bool PPCInstrInfo::transformToImmFormFedByLI(MachineInstr
&MI
,
3363 const ImmInstrInfo
&III
,
3364 unsigned ConstantOpNo
,
3365 int64_t Imm
) const {
3366 MachineRegisterInfo
&MRI
= MI
.getParent()->getParent()->getRegInfo();
3367 bool PostRA
= !MRI
.isSSA();
3368 // Exit early if we can't convert this.
3369 if ((ConstantOpNo
!= III
.OpNoForForwarding
) && !III
.IsCommutative
)
3371 if (Imm
% III
.ImmMustBeMultipleOf
)
3373 if (III
.TruncateImmTo
)
3374 Imm
&= ((1 << III
.TruncateImmTo
) - 1);
3375 if (III
.SignedImm
) {
3376 APInt
ActualValue(64, Imm
, true);
3377 if (!ActualValue
.isSignedIntN(III
.ImmWidth
))
3380 uint64_t UnsignedMax
= (1 << III
.ImmWidth
) - 1;
3381 if ((uint64_t)Imm
> UnsignedMax
)
3385 // If we're post-RA, the instructions don't agree on whether register zero is
3386 // special, we can transform this as long as the register operand that will
3387 // end up in the location where zero is special isn't R0.
3388 if (PostRA
&& III
.ZeroIsSpecialOrig
!= III
.ZeroIsSpecialNew
) {
3389 unsigned PosForOrigZero
= III
.ZeroIsSpecialOrig
? III
.ZeroIsSpecialOrig
:
3390 III
.ZeroIsSpecialNew
+ 1;
3391 unsigned OrigZeroReg
= MI
.getOperand(PosForOrigZero
).getReg();
3392 unsigned NewZeroReg
= MI
.getOperand(III
.ZeroIsSpecialNew
).getReg();
3393 // If R0 is in the operand where zero is special for the new instruction,
3394 // it is unsafe to transform if the constant operand isn't that operand.
3395 if ((NewZeroReg
== PPC::R0
|| NewZeroReg
== PPC::X0
) &&
3396 ConstantOpNo
!= III
.ZeroIsSpecialNew
)
3398 if ((OrigZeroReg
== PPC::R0
|| OrigZeroReg
== PPC::X0
) &&
3399 ConstantOpNo
!= PosForOrigZero
)
3403 unsigned Opc
= MI
.getOpcode();
3404 bool SpecialShift32
=
3405 Opc
== PPC::SLW
|| Opc
== PPC::SLWo
|| Opc
== PPC::SRW
|| Opc
== PPC::SRWo
;
3406 bool SpecialShift64
=
3407 Opc
== PPC::SLD
|| Opc
== PPC::SLDo
|| Opc
== PPC::SRD
|| Opc
== PPC::SRDo
;
3408 bool SetCR
= Opc
== PPC::SLWo
|| Opc
== PPC::SRWo
||
3409 Opc
== PPC::SLDo
|| Opc
== PPC::SRDo
;
3411 Opc
== PPC::SRW
|| Opc
== PPC::SRWo
|| Opc
== PPC::SRD
|| Opc
== PPC::SRDo
;
3413 MI
.setDesc(get(III
.ImmOpcode
));
3414 if (ConstantOpNo
== III
.OpNoForForwarding
) {
3415 // Converting shifts to immediate form is a bit tricky since they may do
3416 // one of three things:
3417 // 1. If the shift amount is between OpSize and 2*OpSize, the result is zero
3418 // 2. If the shift amount is zero, the result is unchanged (save for maybe
3420 // 3. If the shift amount is in [1, OpSize), it's just a shift
3421 if (SpecialShift32
|| SpecialShift64
) {
3422 LoadImmediateInfo LII
;
3425 LII
.Is64Bit
= SpecialShift64
;
3426 uint64_t ShAmt
= Imm
& (SpecialShift32
? 0x1F : 0x3F);
3427 if (Imm
& (SpecialShift32
? 0x20 : 0x40))
3428 replaceInstrWithLI(MI
, LII
);
3429 // Shifts by zero don't change the value. If we don't need to set CR0,
3430 // just convert this to a COPY. Can't do this post-RA since we've already
3431 // cleaned up the copies.
3432 else if (!SetCR
&& ShAmt
== 0 && !PostRA
) {
3433 MI
.RemoveOperand(2);
3434 MI
.setDesc(get(PPC::COPY
));
3436 // The 32 bit and 64 bit instructions are quite different.
3437 if (SpecialShift32
) {
3438 // Left shifts use (N, 0, 31-N), right shifts use (32-N, N, 31).
3439 uint64_t SH
= RightShift
? 32 - ShAmt
: ShAmt
;
3440 uint64_t MB
= RightShift
? ShAmt
: 0;
3441 uint64_t ME
= RightShift
? 31 : 31 - ShAmt
;
3442 replaceInstrOperandWithImm(MI
, III
.OpNoForForwarding
, SH
);
3443 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
).addImm(MB
)
3446 // Left shifts use (N, 63-N), right shifts use (64-N, N).
3447 uint64_t SH
= RightShift
? 64 - ShAmt
: ShAmt
;
3448 uint64_t ME
= RightShift
? ShAmt
: 63 - ShAmt
;
3449 replaceInstrOperandWithImm(MI
, III
.OpNoForForwarding
, SH
);
3450 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
).addImm(ME
);
3454 replaceInstrOperandWithImm(MI
, ConstantOpNo
, Imm
);
3456 // Convert commutative instructions (switch the operands and convert the
3457 // desired one to an immediate.
3458 else if (III
.IsCommutative
) {
3459 replaceInstrOperandWithImm(MI
, ConstantOpNo
, Imm
);
3460 swapMIOperands(MI
, ConstantOpNo
, III
.OpNoForForwarding
);
3462 llvm_unreachable("Should have exited early!");
3464 // For instructions for which the constant register replaces a different
3465 // operand than where the immediate goes, we need to swap them.
3466 if (III
.OpNoForForwarding
!= III
.ImmOpNo
)
3467 swapMIOperands(MI
, III
.OpNoForForwarding
, III
.ImmOpNo
);
3469 // If the special R0/X0 register index are different for original instruction
3470 // and new instruction, we need to fix up the register class in new
3472 if (!PostRA
&& III
.ZeroIsSpecialOrig
!= III
.ZeroIsSpecialNew
) {
3473 if (III
.ZeroIsSpecialNew
) {
3474 // If operand at III.ZeroIsSpecialNew is physical reg(eg: ZERO/ZERO8), no
3475 // need to fix up register class.
3476 unsigned RegToModify
= MI
.getOperand(III
.ZeroIsSpecialNew
).getReg();
3477 if (TargetRegisterInfo::isVirtualRegister(RegToModify
)) {
3478 const TargetRegisterClass
*NewRC
=
3479 MRI
.getRegClass(RegToModify
)->hasSuperClassEq(&PPC::GPRCRegClass
) ?
3480 &PPC::GPRC_and_GPRC_NOR0RegClass
: &PPC::G8RC_and_G8RC_NOX0RegClass
;
3481 MRI
.setRegClass(RegToModify
, NewRC
);
3488 const TargetRegisterClass
*
3489 PPCInstrInfo::updatedRC(const TargetRegisterClass
*RC
) const {
3490 if (Subtarget
.hasVSX() && RC
== &PPC::VRRCRegClass
)
3491 return &PPC::VSRCRegClass
;
3495 int PPCInstrInfo::getRecordFormOpcode(unsigned Opcode
) {
3496 return PPC::getRecordFormOpcode(Opcode
);
3499 // This function returns true if the machine instruction
3500 // always outputs a value by sign-extending a 32 bit value,
3501 // i.e. 0 to 31-th bits are same as 32-th bit.
3502 static bool isSignExtendingOp(const MachineInstr
&MI
) {
3503 int Opcode
= MI
.getOpcode();
3504 if (Opcode
== PPC::LI
|| Opcode
== PPC::LI8
||
3505 Opcode
== PPC::LIS
|| Opcode
== PPC::LIS8
||
3506 Opcode
== PPC::SRAW
|| Opcode
== PPC::SRAWo
||
3507 Opcode
== PPC::SRAWI
|| Opcode
== PPC::SRAWIo
||
3508 Opcode
== PPC::LWA
|| Opcode
== PPC::LWAX
||
3509 Opcode
== PPC::LWA_32
|| Opcode
== PPC::LWAX_32
||
3510 Opcode
== PPC::LHA
|| Opcode
== PPC::LHAX
||
3511 Opcode
== PPC::LHA8
|| Opcode
== PPC::LHAX8
||
3512 Opcode
== PPC::LBZ
|| Opcode
== PPC::LBZX
||
3513 Opcode
== PPC::LBZ8
|| Opcode
== PPC::LBZX8
||
3514 Opcode
== PPC::LBZU
|| Opcode
== PPC::LBZUX
||
3515 Opcode
== PPC::LBZU8
|| Opcode
== PPC::LBZUX8
||
3516 Opcode
== PPC::LHZ
|| Opcode
== PPC::LHZX
||
3517 Opcode
== PPC::LHZ8
|| Opcode
== PPC::LHZX8
||
3518 Opcode
== PPC::LHZU
|| Opcode
== PPC::LHZUX
||
3519 Opcode
== PPC::LHZU8
|| Opcode
== PPC::LHZUX8
||
3520 Opcode
== PPC::EXTSB
|| Opcode
== PPC::EXTSBo
||
3521 Opcode
== PPC::EXTSH
|| Opcode
== PPC::EXTSHo
||
3522 Opcode
== PPC::EXTSB8
|| Opcode
== PPC::EXTSH8
||
3523 Opcode
== PPC::EXTSW
|| Opcode
== PPC::EXTSWo
||
3524 Opcode
== PPC::SETB
|| Opcode
== PPC::SETB8
||
3525 Opcode
== PPC::EXTSH8_32_64
|| Opcode
== PPC::EXTSW_32_64
||
3526 Opcode
== PPC::EXTSB8_32_64
)
3529 if (Opcode
== PPC::RLDICL
&& MI
.getOperand(3).getImm() >= 33)
3532 if ((Opcode
== PPC::RLWINM
|| Opcode
== PPC::RLWINMo
||
3533 Opcode
== PPC::RLWNM
|| Opcode
== PPC::RLWNMo
) &&
3534 MI
.getOperand(3).getImm() > 0 &&
3535 MI
.getOperand(3).getImm() <= MI
.getOperand(4).getImm())
3541 // This function returns true if the machine instruction
3542 // always outputs zeros in higher 32 bits.
3543 static bool isZeroExtendingOp(const MachineInstr
&MI
) {
3544 int Opcode
= MI
.getOpcode();
3545 // The 16-bit immediate is sign-extended in li/lis.
3546 // If the most significant bit is zero, all higher bits are zero.
3547 if (Opcode
== PPC::LI
|| Opcode
== PPC::LI8
||
3548 Opcode
== PPC::LIS
|| Opcode
== PPC::LIS8
) {
3549 int64_t Imm
= MI
.getOperand(1).getImm();
3550 if (((uint64_t)Imm
& ~0x7FFFuLL
) == 0)
3554 // We have some variations of rotate-and-mask instructions
3555 // that clear higher 32-bits.
3556 if ((Opcode
== PPC::RLDICL
|| Opcode
== PPC::RLDICLo
||
3557 Opcode
== PPC::RLDCL
|| Opcode
== PPC::RLDCLo
||
3558 Opcode
== PPC::RLDICL_32_64
) &&
3559 MI
.getOperand(3).getImm() >= 32)
3562 if ((Opcode
== PPC::RLDIC
|| Opcode
== PPC::RLDICo
) &&
3563 MI
.getOperand(3).getImm() >= 32 &&
3564 MI
.getOperand(3).getImm() <= 63 - MI
.getOperand(2).getImm())
3567 if ((Opcode
== PPC::RLWINM
|| Opcode
== PPC::RLWINMo
||
3568 Opcode
== PPC::RLWNM
|| Opcode
== PPC::RLWNMo
||
3569 Opcode
== PPC::RLWINM8
|| Opcode
== PPC::RLWNM8
) &&
3570 MI
.getOperand(3).getImm() <= MI
.getOperand(4).getImm())
3573 // There are other instructions that clear higher 32-bits.
3574 if (Opcode
== PPC::CNTLZW
|| Opcode
== PPC::CNTLZWo
||
3575 Opcode
== PPC::CNTTZW
|| Opcode
== PPC::CNTTZWo
||
3576 Opcode
== PPC::CNTLZW8
|| Opcode
== PPC::CNTTZW8
||
3577 Opcode
== PPC::CNTLZD
|| Opcode
== PPC::CNTLZDo
||
3578 Opcode
== PPC::CNTTZD
|| Opcode
== PPC::CNTTZDo
||
3579 Opcode
== PPC::POPCNTD
|| Opcode
== PPC::POPCNTW
||
3580 Opcode
== PPC::SLW
|| Opcode
== PPC::SLWo
||
3581 Opcode
== PPC::SRW
|| Opcode
== PPC::SRWo
||
3582 Opcode
== PPC::SLW8
|| Opcode
== PPC::SRW8
||
3583 Opcode
== PPC::SLWI
|| Opcode
== PPC::SLWIo
||
3584 Opcode
== PPC::SRWI
|| Opcode
== PPC::SRWIo
||
3585 Opcode
== PPC::LWZ
|| Opcode
== PPC::LWZX
||
3586 Opcode
== PPC::LWZU
|| Opcode
== PPC::LWZUX
||
3587 Opcode
== PPC::LWBRX
|| Opcode
== PPC::LHBRX
||
3588 Opcode
== PPC::LHZ
|| Opcode
== PPC::LHZX
||
3589 Opcode
== PPC::LHZU
|| Opcode
== PPC::LHZUX
||
3590 Opcode
== PPC::LBZ
|| Opcode
== PPC::LBZX
||
3591 Opcode
== PPC::LBZU
|| Opcode
== PPC::LBZUX
||
3592 Opcode
== PPC::LWZ8
|| Opcode
== PPC::LWZX8
||
3593 Opcode
== PPC::LWZU8
|| Opcode
== PPC::LWZUX8
||
3594 Opcode
== PPC::LWBRX8
|| Opcode
== PPC::LHBRX8
||
3595 Opcode
== PPC::LHZ8
|| Opcode
== PPC::LHZX8
||
3596 Opcode
== PPC::LHZU8
|| Opcode
== PPC::LHZUX8
||
3597 Opcode
== PPC::LBZ8
|| Opcode
== PPC::LBZX8
||
3598 Opcode
== PPC::LBZU8
|| Opcode
== PPC::LBZUX8
||
3599 Opcode
== PPC::ANDIo
|| Opcode
== PPC::ANDISo
||
3600 Opcode
== PPC::ROTRWI
|| Opcode
== PPC::ROTRWIo
||
3601 Opcode
== PPC::EXTLWI
|| Opcode
== PPC::EXTLWIo
||
3602 Opcode
== PPC::MFVSRWZ
)
3608 // This function returns true if the input MachineInstr is a TOC save
3610 bool PPCInstrInfo::isTOCSaveMI(const MachineInstr
&MI
) const {
3611 if (!MI
.getOperand(1).isImm() || !MI
.getOperand(2).isReg())
3613 unsigned TOCSaveOffset
= Subtarget
.getFrameLowering()->getTOCSaveOffset();
3614 unsigned StackOffset
= MI
.getOperand(1).getImm();
3615 unsigned StackReg
= MI
.getOperand(2).getReg();
3616 if (StackReg
== PPC::X1
&& StackOffset
== TOCSaveOffset
)
3622 // We limit the max depth to track incoming values of PHIs or binary ops
3623 // (e.g. AND) to avoid excessive cost.
3624 const unsigned MAX_DEPTH
= 1;
3627 PPCInstrInfo::isSignOrZeroExtended(const MachineInstr
&MI
, bool SignExt
,
3628 const unsigned Depth
) const {
3629 const MachineFunction
*MF
= MI
.getParent()->getParent();
3630 const MachineRegisterInfo
*MRI
= &MF
->getRegInfo();
3632 // If we know this instruction returns sign- or zero-extended result,
3634 if (SignExt
? isSignExtendingOp(MI
):
3635 isZeroExtendingOp(MI
))
3638 switch (MI
.getOpcode()) {
3640 unsigned SrcReg
= MI
.getOperand(1).getReg();
3642 // In both ELFv1 and v2 ABI, method parameters and the return value
3643 // are sign- or zero-extended.
3644 if (MF
->getSubtarget
<PPCSubtarget
>().isSVR4ABI()) {
3645 const PPCFunctionInfo
*FuncInfo
= MF
->getInfo
<PPCFunctionInfo
>();
3646 // We check the ZExt/SExt flags for a method parameter.
3647 if (MI
.getParent()->getBasicBlock() ==
3648 &MF
->getFunction().getEntryBlock()) {
3649 unsigned VReg
= MI
.getOperand(0).getReg();
3650 if (MF
->getRegInfo().isLiveIn(VReg
))
3651 return SignExt
? FuncInfo
->isLiveInSExt(VReg
) :
3652 FuncInfo
->isLiveInZExt(VReg
);
3655 // For a method return value, we check the ZExt/SExt flags in attribute.
3656 // We assume the following code sequence for method call.
3657 // ADJCALLSTACKDOWN 32, implicit dead %r1, implicit %r1
3658 // BL8_NOP @func,...
3659 // ADJCALLSTACKUP 32, 0, implicit dead %r1, implicit %r1
3660 // %5 = COPY %x3; G8RC:%5
3661 if (SrcReg
== PPC::X3
) {
3662 const MachineBasicBlock
*MBB
= MI
.getParent();
3663 MachineBasicBlock::const_instr_iterator II
=
3664 MachineBasicBlock::const_instr_iterator(&MI
);
3665 if (II
!= MBB
->instr_begin() &&
3666 (--II
)->getOpcode() == PPC::ADJCALLSTACKUP
) {
3667 const MachineInstr
&CallMI
= *(--II
);
3668 if (CallMI
.isCall() && CallMI
.getOperand(0).isGlobal()) {
3669 const Function
*CalleeFn
=
3670 dyn_cast
<Function
>(CallMI
.getOperand(0).getGlobal());
3673 const IntegerType
*IntTy
=
3674 dyn_cast
<IntegerType
>(CalleeFn
->getReturnType());
3675 const AttributeSet
&Attrs
=
3676 CalleeFn
->getAttributes().getRetAttributes();
3677 if (IntTy
&& IntTy
->getBitWidth() <= 32)
3678 return Attrs
.hasAttribute(SignExt
? Attribute::SExt
:
3685 // If this is a copy from another register, we recursively check source.
3686 if (!TargetRegisterInfo::isVirtualRegister(SrcReg
))
3688 const MachineInstr
*SrcMI
= MRI
->getVRegDef(SrcReg
);
3690 return isSignOrZeroExtended(*SrcMI
, SignExt
, Depth
);
3707 // logical operation with 16-bit immediate does not change the upper bits.
3708 // So, we track the operand register as we do for register copy.
3709 unsigned SrcReg
= MI
.getOperand(1).getReg();
3710 if (!TargetRegisterInfo::isVirtualRegister(SrcReg
))
3712 const MachineInstr
*SrcMI
= MRI
->getVRegDef(SrcReg
);
3714 return isSignOrZeroExtended(*SrcMI
, SignExt
, Depth
);
3719 // If all incoming values are sign-/zero-extended,
3720 // the output of OR, ISEL or PHI is also sign-/zero-extended.
3725 if (Depth
>= MAX_DEPTH
)
3728 // The input registers for PHI are operand 1, 3, ...
3729 // The input registers for others are operand 1 and 2.
3730 unsigned E
= 3, D
= 1;
3731 if (MI
.getOpcode() == PPC::PHI
) {
3732 E
= MI
.getNumOperands();
3736 for (unsigned I
= 1; I
!= E
; I
+= D
) {
3737 if (MI
.getOperand(I
).isReg()) {
3738 unsigned SrcReg
= MI
.getOperand(I
).getReg();
3739 if (!TargetRegisterInfo::isVirtualRegister(SrcReg
))
3741 const MachineInstr
*SrcMI
= MRI
->getVRegDef(SrcReg
);
3742 if (SrcMI
== NULL
|| !isSignOrZeroExtended(*SrcMI
, SignExt
, Depth
+1))
3751 // If at least one of the incoming values of an AND is zero extended
3752 // then the output is also zero-extended. If both of the incoming values
3753 // are sign-extended then the output is also sign extended.
3756 if (Depth
>= MAX_DEPTH
)
3759 assert(MI
.getOperand(1).isReg() && MI
.getOperand(2).isReg());
3761 unsigned SrcReg1
= MI
.getOperand(1).getReg();
3762 unsigned SrcReg2
= MI
.getOperand(2).getReg();
3764 if (!TargetRegisterInfo::isVirtualRegister(SrcReg1
) ||
3765 !TargetRegisterInfo::isVirtualRegister(SrcReg2
))
3768 const MachineInstr
*MISrc1
= MRI
->getVRegDef(SrcReg1
);
3769 const MachineInstr
*MISrc2
= MRI
->getVRegDef(SrcReg2
);
3770 if (!MISrc1
|| !MISrc2
)
3774 return isSignOrZeroExtended(*MISrc1
, SignExt
, Depth
+1) &&
3775 isSignOrZeroExtended(*MISrc2
, SignExt
, Depth
+1);
3777 return isSignOrZeroExtended(*MISrc1
, SignExt
, Depth
+1) ||
3778 isSignOrZeroExtended(*MISrc2
, SignExt
, Depth
+1);