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
,
93 SOK_LastOpcodeSpill
// This must be last on the enum.
96 // Pin the vtable to this file.
97 void PPCInstrInfo::anchor() {}
99 PPCInstrInfo::PPCInstrInfo(PPCSubtarget
&STI
)
100 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN
, PPC::ADJCALLSTACKUP
,
101 /* CatchRetOpcode */ -1,
102 STI
.isPPC64() ? PPC::BLR8
: PPC::BLR
),
103 Subtarget(STI
), RI(STI
.getTargetMachine()) {}
105 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
106 /// this target when scheduling the DAG.
107 ScheduleHazardRecognizer
*
108 PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo
*STI
,
109 const ScheduleDAG
*DAG
) const {
111 static_cast<const PPCSubtarget
*>(STI
)->getDarwinDirective();
112 if (Directive
== PPC::DIR_440
|| Directive
== PPC::DIR_A2
||
113 Directive
== PPC::DIR_E500mc
|| Directive
== PPC::DIR_E5500
) {
114 const InstrItineraryData
*II
=
115 static_cast<const PPCSubtarget
*>(STI
)->getInstrItineraryData();
116 return new ScoreboardHazardRecognizer(II
, DAG
);
119 return TargetInstrInfo::CreateTargetHazardRecognizer(STI
, DAG
);
122 /// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
123 /// to use for this target when scheduling the DAG.
124 ScheduleHazardRecognizer
*
125 PPCInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData
*II
,
126 const ScheduleDAG
*DAG
) const {
128 DAG
->MF
.getSubtarget
<PPCSubtarget
>().getDarwinDirective();
130 // FIXME: Leaving this as-is until we have POWER9 scheduling info
131 if (Directive
== PPC::DIR_PWR7
|| Directive
== PPC::DIR_PWR8
)
132 return new PPCDispatchGroupSBHazardRecognizer(II
, DAG
);
134 // Most subtargets use a PPC970 recognizer.
135 if (Directive
!= PPC::DIR_440
&& Directive
!= PPC::DIR_A2
&&
136 Directive
!= PPC::DIR_E500mc
&& Directive
!= PPC::DIR_E5500
) {
137 assert(DAG
->TII
&& "No InstrInfo?");
139 return new PPCHazardRecognizer970(*DAG
);
142 return new ScoreboardHazardRecognizer(II
, DAG
);
145 unsigned PPCInstrInfo::getInstrLatency(const InstrItineraryData
*ItinData
,
146 const MachineInstr
&MI
,
147 unsigned *PredCost
) const {
148 if (!ItinData
|| UseOldLatencyCalc
)
149 return PPCGenInstrInfo::getInstrLatency(ItinData
, MI
, PredCost
);
151 // The default implementation of getInstrLatency calls getStageLatency, but
152 // getStageLatency does not do the right thing for us. While we have
153 // itinerary, most cores are fully pipelined, and so the itineraries only
154 // express the first part of the pipeline, not every stage. Instead, we need
155 // to use the listed output operand cycle number (using operand 0 here, which
158 unsigned Latency
= 1;
159 unsigned DefClass
= MI
.getDesc().getSchedClass();
160 for (unsigned i
= 0, e
= MI
.getNumOperands(); i
!= e
; ++i
) {
161 const MachineOperand
&MO
= MI
.getOperand(i
);
162 if (!MO
.isReg() || !MO
.isDef() || MO
.isImplicit())
165 int Cycle
= ItinData
->getOperandCycle(DefClass
, i
);
169 Latency
= std::max(Latency
, (unsigned) Cycle
);
175 int PPCInstrInfo::getOperandLatency(const InstrItineraryData
*ItinData
,
176 const MachineInstr
&DefMI
, unsigned DefIdx
,
177 const MachineInstr
&UseMI
,
178 unsigned UseIdx
) const {
179 int Latency
= PPCGenInstrInfo::getOperandLatency(ItinData
, DefMI
, DefIdx
,
182 if (!DefMI
.getParent())
185 const MachineOperand
&DefMO
= DefMI
.getOperand(DefIdx
);
186 Register Reg
= DefMO
.getReg();
189 if (Register::isVirtualRegister(Reg
)) {
190 const MachineRegisterInfo
*MRI
=
191 &DefMI
.getParent()->getParent()->getRegInfo();
192 IsRegCR
= MRI
->getRegClass(Reg
)->hasSuperClassEq(&PPC::CRRCRegClass
) ||
193 MRI
->getRegClass(Reg
)->hasSuperClassEq(&PPC::CRBITRCRegClass
);
195 IsRegCR
= PPC::CRRCRegClass
.contains(Reg
) ||
196 PPC::CRBITRCRegClass
.contains(Reg
);
199 if (UseMI
.isBranch() && IsRegCR
) {
201 Latency
= getInstrLatency(ItinData
, DefMI
);
203 // On some cores, there is an additional delay between writing to a condition
204 // register, and using it from a branch.
205 unsigned Directive
= Subtarget
.getDarwinDirective();
219 // FIXME: Is this needed for POWER9?
228 // This function does not list all associative and commutative operations, but
229 // only those worth feeding through the machine combiner in an attempt to
230 // reduce the critical path. Mostly, this means floating-point operations,
231 // because they have high latencies (compared to other operations, such and
232 // and/or, which are also associative and commutative, but have low latencies).
233 bool PPCInstrInfo::isAssociativeAndCommutative(const MachineInstr
&Inst
) const {
234 switch (Inst
.getOpcode()) {
267 bool PPCInstrInfo::getMachineCombinerPatterns(
269 SmallVectorImpl
<MachineCombinerPattern
> &Patterns
) const {
270 // Using the machine combiner in this way is potentially expensive, so
271 // restrict to when aggressive optimizations are desired.
272 if (Subtarget
.getTargetMachine().getOptLevel() != CodeGenOpt::Aggressive
)
275 // FP reassociation is only legal when we don't need strict IEEE semantics.
276 if (!Root
.getParent()->getParent()->getTarget().Options
.UnsafeFPMath
)
279 return TargetInstrInfo::getMachineCombinerPatterns(Root
, Patterns
);
282 // Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
283 bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr
&MI
,
284 unsigned &SrcReg
, unsigned &DstReg
,
285 unsigned &SubIdx
) const {
286 switch (MI
.getOpcode()) {
287 default: return false;
290 case PPC::EXTSW_32_64
:
291 SrcReg
= MI
.getOperand(1).getReg();
292 DstReg
= MI
.getOperand(0).getReg();
293 SubIdx
= PPC::sub_32
;
298 unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr
&MI
,
299 int &FrameIndex
) const {
300 unsigned Opcode
= MI
.getOpcode();
301 const unsigned *OpcodesForSpill
= getLoadOpcodesForSpillArray();
302 const unsigned *End
= OpcodesForSpill
+ SOK_LastOpcodeSpill
;
304 if (End
!= std::find(OpcodesForSpill
, End
, Opcode
)) {
305 // Check for the operands added by addFrameReference (the immediate is the
306 // offset which defaults to 0).
307 if (MI
.getOperand(1).isImm() && !MI
.getOperand(1).getImm() &&
308 MI
.getOperand(2).isFI()) {
309 FrameIndex
= MI
.getOperand(2).getIndex();
310 return MI
.getOperand(0).getReg();
316 // For opcodes with the ReMaterializable flag set, this function is called to
317 // verify the instruction is really rematable.
318 bool PPCInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr
&MI
,
319 AliasAnalysis
*AA
) const {
320 switch (MI
.getOpcode()) {
322 // This function should only be called for opcodes with the ReMaterializable
324 llvm_unreachable("Unknown rematerializable operation!");
331 case PPC::ADDIStocHA
:
332 case PPC::ADDIStocHA8
:
334 case PPC::LOAD_STACK_GUARD
:
338 case PPC::XXLEQVOnes
:
342 case PPC::V_SETALLONESB
:
343 case PPC::V_SETALLONESH
:
344 case PPC::V_SETALLONES
:
352 unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr
&MI
,
353 int &FrameIndex
) const {
354 unsigned Opcode
= MI
.getOpcode();
355 const unsigned *OpcodesForSpill
= getStoreOpcodesForSpillArray();
356 const unsigned *End
= OpcodesForSpill
+ SOK_LastOpcodeSpill
;
358 if (End
!= std::find(OpcodesForSpill
, End
, Opcode
)) {
359 if (MI
.getOperand(1).isImm() && !MI
.getOperand(1).getImm() &&
360 MI
.getOperand(2).isFI()) {
361 FrameIndex
= MI
.getOperand(2).getIndex();
362 return MI
.getOperand(0).getReg();
368 MachineInstr
*PPCInstrInfo::commuteInstructionImpl(MachineInstr
&MI
, bool NewMI
,
370 unsigned OpIdx2
) const {
371 MachineFunction
&MF
= *MI
.getParent()->getParent();
373 // Normal instructions can be commuted the obvious way.
374 if (MI
.getOpcode() != PPC::RLWIMI
&& MI
.getOpcode() != PPC::RLWIMIo
)
375 return TargetInstrInfo::commuteInstructionImpl(MI
, NewMI
, OpIdx1
, OpIdx2
);
376 // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a
377 // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because
378 // changing the relative order of the mask operands might change what happens
379 // to the high-bits of the mask (and, thus, the result).
381 // Cannot commute if it has a non-zero rotate count.
382 if (MI
.getOperand(3).getImm() != 0)
385 // If we have a zero rotate count, we have:
387 // Op0 = (Op1 & ~M) | (Op2 & M)
389 // M = mask((ME+1)&31, (MB-1)&31)
390 // Op0 = (Op2 & ~M) | (Op1 & M)
393 assert(((OpIdx1
== 1 && OpIdx2
== 2) || (OpIdx1
== 2 && OpIdx2
== 1)) &&
394 "Only the operands 1 and 2 can be swapped in RLSIMI/RLWIMIo.");
395 Register Reg0
= MI
.getOperand(0).getReg();
396 Register Reg1
= MI
.getOperand(1).getReg();
397 Register Reg2
= MI
.getOperand(2).getReg();
398 unsigned SubReg1
= MI
.getOperand(1).getSubReg();
399 unsigned SubReg2
= MI
.getOperand(2).getSubReg();
400 bool Reg1IsKill
= MI
.getOperand(1).isKill();
401 bool Reg2IsKill
= MI
.getOperand(2).isKill();
402 bool ChangeReg0
= false;
403 // If machine instrs are no longer in two-address forms, update
404 // destination register as well.
406 // Must be two address instruction!
407 assert(MI
.getDesc().getOperandConstraint(0, MCOI::TIED_TO
) &&
408 "Expecting a two-address instruction!");
409 assert(MI
.getOperand(0).getSubReg() == SubReg1
&& "Tied subreg mismatch");
415 unsigned MB
= MI
.getOperand(4).getImm();
416 unsigned ME
= MI
.getOperand(5).getImm();
418 // We can't commute a trivial mask (there is no way to represent an all-zero
420 if (MB
== 0 && ME
== 31)
424 // Create a new instruction.
425 Register Reg0
= ChangeReg0
? Reg2
: MI
.getOperand(0).getReg();
426 bool Reg0IsDead
= MI
.getOperand(0).isDead();
427 return BuildMI(MF
, MI
.getDebugLoc(), MI
.getDesc())
428 .addReg(Reg0
, RegState::Define
| getDeadRegState(Reg0IsDead
))
429 .addReg(Reg2
, getKillRegState(Reg2IsKill
))
430 .addReg(Reg1
, getKillRegState(Reg1IsKill
))
431 .addImm((ME
+ 1) & 31)
432 .addImm((MB
- 1) & 31);
436 MI
.getOperand(0).setReg(Reg2
);
437 MI
.getOperand(0).setSubReg(SubReg2
);
439 MI
.getOperand(2).setReg(Reg1
);
440 MI
.getOperand(1).setReg(Reg2
);
441 MI
.getOperand(2).setSubReg(SubReg1
);
442 MI
.getOperand(1).setSubReg(SubReg2
);
443 MI
.getOperand(2).setIsKill(Reg1IsKill
);
444 MI
.getOperand(1).setIsKill(Reg2IsKill
);
446 // Swap the mask around.
447 MI
.getOperand(4).setImm((ME
+ 1) & 31);
448 MI
.getOperand(5).setImm((MB
- 1) & 31);
452 bool PPCInstrInfo::findCommutedOpIndices(const MachineInstr
&MI
,
454 unsigned &SrcOpIdx2
) const {
455 // For VSX A-Type FMA instructions, it is the first two operands that can be
456 // commuted, however, because the non-encoded tied input operand is listed
457 // first, the operands to swap are actually the second and third.
459 int AltOpc
= PPC::getAltVSXFMAOpcode(MI
.getOpcode());
461 return TargetInstrInfo::findCommutedOpIndices(MI
, SrcOpIdx1
, SrcOpIdx2
);
463 // The commutable operand indices are 2 and 3. Return them in SrcOpIdx1
465 return fixCommutedOpIndices(SrcOpIdx1
, SrcOpIdx2
, 2, 3);
468 void PPCInstrInfo::insertNoop(MachineBasicBlock
&MBB
,
469 MachineBasicBlock::iterator MI
) const {
470 // This function is used for scheduling, and the nop wanted here is the type
471 // that terminates dispatch groups on the POWER cores.
472 unsigned Directive
= Subtarget
.getDarwinDirective();
475 default: Opcode
= PPC::NOP
; break;
476 case PPC::DIR_PWR6
: Opcode
= PPC::NOP_GT_PWR6
; break;
477 case PPC::DIR_PWR7
: Opcode
= PPC::NOP_GT_PWR7
; break;
478 case PPC::DIR_PWR8
: Opcode
= PPC::NOP_GT_PWR7
; break; /* FIXME: Update when P8 InstrScheduling model is ready */
479 // FIXME: Update when POWER9 scheduling model is ready.
480 case PPC::DIR_PWR9
: Opcode
= PPC::NOP_GT_PWR7
; break;
484 BuildMI(MBB
, MI
, DL
, get(Opcode
));
487 /// Return the noop instruction to use for a noop.
488 void PPCInstrInfo::getNoop(MCInst
&NopInst
) const {
489 NopInst
.setOpcode(PPC::NOP
);
493 // Note: If the condition register is set to CTR or CTR8 then this is a
494 // BDNZ (imm == 1) or BDZ (imm == 0) branch.
495 bool PPCInstrInfo::analyzeBranch(MachineBasicBlock
&MBB
,
496 MachineBasicBlock
*&TBB
,
497 MachineBasicBlock
*&FBB
,
498 SmallVectorImpl
<MachineOperand
> &Cond
,
499 bool AllowModify
) const {
500 bool isPPC64
= Subtarget
.isPPC64();
502 // If the block has no terminators, it just falls into the block after it.
503 MachineBasicBlock::iterator I
= MBB
.getLastNonDebugInstr();
507 if (!isUnpredicatedTerminator(*I
))
511 // If the BB ends with an unconditional branch to the fallthrough BB,
512 // we eliminate the branch instruction.
513 if (I
->getOpcode() == PPC::B
&&
514 MBB
.isLayoutSuccessor(I
->getOperand(0).getMBB())) {
515 I
->eraseFromParent();
517 // We update iterator after deleting the last branch.
518 I
= MBB
.getLastNonDebugInstr();
519 if (I
== MBB
.end() || !isUnpredicatedTerminator(*I
))
524 // Get the last instruction in the block.
525 MachineInstr
&LastInst
= *I
;
527 // If there is only one terminator instruction, process it.
528 if (I
== MBB
.begin() || !isUnpredicatedTerminator(*--I
)) {
529 if (LastInst
.getOpcode() == PPC::B
) {
530 if (!LastInst
.getOperand(0).isMBB())
532 TBB
= LastInst
.getOperand(0).getMBB();
534 } else if (LastInst
.getOpcode() == PPC::BCC
) {
535 if (!LastInst
.getOperand(2).isMBB())
537 // Block ends with fall-through condbranch.
538 TBB
= LastInst
.getOperand(2).getMBB();
539 Cond
.push_back(LastInst
.getOperand(0));
540 Cond
.push_back(LastInst
.getOperand(1));
542 } else if (LastInst
.getOpcode() == PPC::BC
) {
543 if (!LastInst
.getOperand(1).isMBB())
545 // Block ends with fall-through condbranch.
546 TBB
= LastInst
.getOperand(1).getMBB();
547 Cond
.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET
));
548 Cond
.push_back(LastInst
.getOperand(0));
550 } else if (LastInst
.getOpcode() == PPC::BCn
) {
551 if (!LastInst
.getOperand(1).isMBB())
553 // Block ends with fall-through condbranch.
554 TBB
= LastInst
.getOperand(1).getMBB();
555 Cond
.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET
));
556 Cond
.push_back(LastInst
.getOperand(0));
558 } else if (LastInst
.getOpcode() == PPC::BDNZ8
||
559 LastInst
.getOpcode() == PPC::BDNZ
) {
560 if (!LastInst
.getOperand(0).isMBB())
562 if (DisableCTRLoopAnal
)
564 TBB
= LastInst
.getOperand(0).getMBB();
565 Cond
.push_back(MachineOperand::CreateImm(1));
566 Cond
.push_back(MachineOperand::CreateReg(isPPC64
? PPC::CTR8
: PPC::CTR
,
569 } else if (LastInst
.getOpcode() == PPC::BDZ8
||
570 LastInst
.getOpcode() == PPC::BDZ
) {
571 if (!LastInst
.getOperand(0).isMBB())
573 if (DisableCTRLoopAnal
)
575 TBB
= LastInst
.getOperand(0).getMBB();
576 Cond
.push_back(MachineOperand::CreateImm(0));
577 Cond
.push_back(MachineOperand::CreateReg(isPPC64
? PPC::CTR8
: PPC::CTR
,
582 // Otherwise, don't know what this is.
586 // Get the instruction before it if it's a terminator.
587 MachineInstr
&SecondLastInst
= *I
;
589 // If there are three terminators, we don't know what sort of block this is.
590 if (I
!= MBB
.begin() && isUnpredicatedTerminator(*--I
))
593 // If the block ends with PPC::B and PPC:BCC, handle it.
594 if (SecondLastInst
.getOpcode() == PPC::BCC
&&
595 LastInst
.getOpcode() == PPC::B
) {
596 if (!SecondLastInst
.getOperand(2).isMBB() ||
597 !LastInst
.getOperand(0).isMBB())
599 TBB
= SecondLastInst
.getOperand(2).getMBB();
600 Cond
.push_back(SecondLastInst
.getOperand(0));
601 Cond
.push_back(SecondLastInst
.getOperand(1));
602 FBB
= LastInst
.getOperand(0).getMBB();
604 } else if (SecondLastInst
.getOpcode() == PPC::BC
&&
605 LastInst
.getOpcode() == PPC::B
) {
606 if (!SecondLastInst
.getOperand(1).isMBB() ||
607 !LastInst
.getOperand(0).isMBB())
609 TBB
= SecondLastInst
.getOperand(1).getMBB();
610 Cond
.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET
));
611 Cond
.push_back(SecondLastInst
.getOperand(0));
612 FBB
= LastInst
.getOperand(0).getMBB();
614 } else if (SecondLastInst
.getOpcode() == PPC::BCn
&&
615 LastInst
.getOpcode() == PPC::B
) {
616 if (!SecondLastInst
.getOperand(1).isMBB() ||
617 !LastInst
.getOperand(0).isMBB())
619 TBB
= SecondLastInst
.getOperand(1).getMBB();
620 Cond
.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET
));
621 Cond
.push_back(SecondLastInst
.getOperand(0));
622 FBB
= LastInst
.getOperand(0).getMBB();
624 } else if ((SecondLastInst
.getOpcode() == PPC::BDNZ8
||
625 SecondLastInst
.getOpcode() == PPC::BDNZ
) &&
626 LastInst
.getOpcode() == PPC::B
) {
627 if (!SecondLastInst
.getOperand(0).isMBB() ||
628 !LastInst
.getOperand(0).isMBB())
630 if (DisableCTRLoopAnal
)
632 TBB
= SecondLastInst
.getOperand(0).getMBB();
633 Cond
.push_back(MachineOperand::CreateImm(1));
634 Cond
.push_back(MachineOperand::CreateReg(isPPC64
? PPC::CTR8
: PPC::CTR
,
636 FBB
= LastInst
.getOperand(0).getMBB();
638 } else if ((SecondLastInst
.getOpcode() == PPC::BDZ8
||
639 SecondLastInst
.getOpcode() == PPC::BDZ
) &&
640 LastInst
.getOpcode() == PPC::B
) {
641 if (!SecondLastInst
.getOperand(0).isMBB() ||
642 !LastInst
.getOperand(0).isMBB())
644 if (DisableCTRLoopAnal
)
646 TBB
= SecondLastInst
.getOperand(0).getMBB();
647 Cond
.push_back(MachineOperand::CreateImm(0));
648 Cond
.push_back(MachineOperand::CreateReg(isPPC64
? PPC::CTR8
: PPC::CTR
,
650 FBB
= LastInst
.getOperand(0).getMBB();
654 // If the block ends with two PPC:Bs, handle it. The second one is not
655 // executed, so remove it.
656 if (SecondLastInst
.getOpcode() == PPC::B
&& LastInst
.getOpcode() == PPC::B
) {
657 if (!SecondLastInst
.getOperand(0).isMBB())
659 TBB
= SecondLastInst
.getOperand(0).getMBB();
662 I
->eraseFromParent();
666 // Otherwise, can't handle this.
670 unsigned PPCInstrInfo::removeBranch(MachineBasicBlock
&MBB
,
671 int *BytesRemoved
) const {
672 assert(!BytesRemoved
&& "code size not handled");
674 MachineBasicBlock::iterator I
= MBB
.getLastNonDebugInstr();
678 if (I
->getOpcode() != PPC::B
&& 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 if (I
== MBB
.begin()) return 1;
691 if (I
->getOpcode() != PPC::BCC
&&
692 I
->getOpcode() != PPC::BC
&& I
->getOpcode() != PPC::BCn
&&
693 I
->getOpcode() != PPC::BDNZ8
&& I
->getOpcode() != PPC::BDNZ
&&
694 I
->getOpcode() != PPC::BDZ8
&& I
->getOpcode() != PPC::BDZ
)
697 // Remove the branch.
698 I
->eraseFromParent();
702 unsigned PPCInstrInfo::insertBranch(MachineBasicBlock
&MBB
,
703 MachineBasicBlock
*TBB
,
704 MachineBasicBlock
*FBB
,
705 ArrayRef
<MachineOperand
> Cond
,
707 int *BytesAdded
) const {
708 // Shouldn't be a fall through.
709 assert(TBB
&& "insertBranch must not be told to insert a fallthrough");
710 assert((Cond
.size() == 2 || Cond
.size() == 0) &&
711 "PPC branch conditions have two components!");
712 assert(!BytesAdded
&& "code size not handled");
714 bool isPPC64
= Subtarget
.isPPC64();
718 if (Cond
.empty()) // Unconditional branch
719 BuildMI(&MBB
, DL
, get(PPC::B
)).addMBB(TBB
);
720 else if (Cond
[1].getReg() == PPC::CTR
|| Cond
[1].getReg() == PPC::CTR8
)
721 BuildMI(&MBB
, DL
, get(Cond
[0].getImm() ?
722 (isPPC64
? PPC::BDNZ8
: PPC::BDNZ
) :
723 (isPPC64
? PPC::BDZ8
: PPC::BDZ
))).addMBB(TBB
);
724 else if (Cond
[0].getImm() == PPC::PRED_BIT_SET
)
725 BuildMI(&MBB
, DL
, get(PPC::BC
)).add(Cond
[1]).addMBB(TBB
);
726 else if (Cond
[0].getImm() == PPC::PRED_BIT_UNSET
)
727 BuildMI(&MBB
, DL
, get(PPC::BCn
)).add(Cond
[1]).addMBB(TBB
);
728 else // Conditional branch
729 BuildMI(&MBB
, DL
, get(PPC::BCC
))
730 .addImm(Cond
[0].getImm())
736 // Two-way Conditional Branch.
737 if (Cond
[1].getReg() == PPC::CTR
|| Cond
[1].getReg() == PPC::CTR8
)
738 BuildMI(&MBB
, DL
, get(Cond
[0].getImm() ?
739 (isPPC64
? PPC::BDNZ8
: PPC::BDNZ
) :
740 (isPPC64
? PPC::BDZ8
: PPC::BDZ
))).addMBB(TBB
);
741 else if (Cond
[0].getImm() == PPC::PRED_BIT_SET
)
742 BuildMI(&MBB
, DL
, get(PPC::BC
)).add(Cond
[1]).addMBB(TBB
);
743 else if (Cond
[0].getImm() == PPC::PRED_BIT_UNSET
)
744 BuildMI(&MBB
, DL
, get(PPC::BCn
)).add(Cond
[1]).addMBB(TBB
);
746 BuildMI(&MBB
, DL
, get(PPC::BCC
))
747 .addImm(Cond
[0].getImm())
750 BuildMI(&MBB
, DL
, get(PPC::B
)).addMBB(FBB
);
755 bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock
&MBB
,
756 ArrayRef
<MachineOperand
> Cond
,
757 unsigned TrueReg
, unsigned FalseReg
,
758 int &CondCycles
, int &TrueCycles
, int &FalseCycles
) const {
759 if (Cond
.size() != 2)
762 // If this is really a bdnz-like condition, then it cannot be turned into a
764 if (Cond
[1].getReg() == PPC::CTR
|| Cond
[1].getReg() == PPC::CTR8
)
767 // Check register classes.
768 const MachineRegisterInfo
&MRI
= MBB
.getParent()->getRegInfo();
769 const TargetRegisterClass
*RC
=
770 RI
.getCommonSubClass(MRI
.getRegClass(TrueReg
), MRI
.getRegClass(FalseReg
));
774 // isel is for regular integer GPRs only.
775 if (!PPC::GPRCRegClass
.hasSubClassEq(RC
) &&
776 !PPC::GPRC_NOR0RegClass
.hasSubClassEq(RC
) &&
777 !PPC::G8RCRegClass
.hasSubClassEq(RC
) &&
778 !PPC::G8RC_NOX0RegClass
.hasSubClassEq(RC
))
781 // FIXME: These numbers are for the A2, how well they work for other cores is
782 // an open question. On the A2, the isel instruction has a 2-cycle latency
783 // but single-cycle throughput. These numbers are used in combination with
784 // the MispredictPenalty setting from the active SchedMachineModel.
792 void PPCInstrInfo::insertSelect(MachineBasicBlock
&MBB
,
793 MachineBasicBlock::iterator MI
,
794 const DebugLoc
&dl
, unsigned DestReg
,
795 ArrayRef
<MachineOperand
> Cond
, unsigned TrueReg
,
796 unsigned FalseReg
) const {
797 assert(Cond
.size() == 2 &&
798 "PPC branch conditions have two components!");
800 // Get the register classes.
801 MachineRegisterInfo
&MRI
= MBB
.getParent()->getRegInfo();
802 const TargetRegisterClass
*RC
=
803 RI
.getCommonSubClass(MRI
.getRegClass(TrueReg
), MRI
.getRegClass(FalseReg
));
804 assert(RC
&& "TrueReg and FalseReg must have overlapping register classes");
806 bool Is64Bit
= PPC::G8RCRegClass
.hasSubClassEq(RC
) ||
807 PPC::G8RC_NOX0RegClass
.hasSubClassEq(RC
);
809 PPC::GPRCRegClass
.hasSubClassEq(RC
) ||
810 PPC::GPRC_NOR0RegClass
.hasSubClassEq(RC
)) &&
811 "isel is for regular integer GPRs only");
813 unsigned OpCode
= Is64Bit
? PPC::ISEL8
: PPC::ISEL
;
814 auto SelectPred
= static_cast<PPC::Predicate
>(Cond
[0].getImm());
817 bool SwapOps
= false;
818 switch (SelectPred
) {
820 case PPC::PRED_EQ_MINUS
:
821 case PPC::PRED_EQ_PLUS
:
822 SubIdx
= PPC::sub_eq
; SwapOps
= false; break;
824 case PPC::PRED_NE_MINUS
:
825 case PPC::PRED_NE_PLUS
:
826 SubIdx
= PPC::sub_eq
; SwapOps
= true; break;
828 case PPC::PRED_LT_MINUS
:
829 case PPC::PRED_LT_PLUS
:
830 SubIdx
= PPC::sub_lt
; SwapOps
= false; break;
832 case PPC::PRED_GE_MINUS
:
833 case PPC::PRED_GE_PLUS
:
834 SubIdx
= PPC::sub_lt
; SwapOps
= true; break;
836 case PPC::PRED_GT_MINUS
:
837 case PPC::PRED_GT_PLUS
:
838 SubIdx
= PPC::sub_gt
; SwapOps
= false; break;
840 case PPC::PRED_LE_MINUS
:
841 case PPC::PRED_LE_PLUS
:
842 SubIdx
= PPC::sub_gt
; SwapOps
= true; break;
844 case PPC::PRED_UN_MINUS
:
845 case PPC::PRED_UN_PLUS
:
846 SubIdx
= PPC::sub_un
; SwapOps
= false; break;
848 case PPC::PRED_NU_MINUS
:
849 case PPC::PRED_NU_PLUS
:
850 SubIdx
= PPC::sub_un
; SwapOps
= true; break;
851 case PPC::PRED_BIT_SET
: SubIdx
= 0; SwapOps
= false; break;
852 case PPC::PRED_BIT_UNSET
: SubIdx
= 0; SwapOps
= true; break;
855 unsigned FirstReg
= SwapOps
? FalseReg
: TrueReg
,
856 SecondReg
= SwapOps
? TrueReg
: FalseReg
;
858 // The first input register of isel cannot be r0. If it is a member
859 // of a register class that can be r0, then copy it first (the
860 // register allocator should eliminate the copy).
861 if (MRI
.getRegClass(FirstReg
)->contains(PPC::R0
) ||
862 MRI
.getRegClass(FirstReg
)->contains(PPC::X0
)) {
863 const TargetRegisterClass
*FirstRC
=
864 MRI
.getRegClass(FirstReg
)->contains(PPC::X0
) ?
865 &PPC::G8RC_NOX0RegClass
: &PPC::GPRC_NOR0RegClass
;
866 unsigned OldFirstReg
= FirstReg
;
867 FirstReg
= MRI
.createVirtualRegister(FirstRC
);
868 BuildMI(MBB
, MI
, dl
, get(TargetOpcode::COPY
), FirstReg
)
869 .addReg(OldFirstReg
);
872 BuildMI(MBB
, MI
, dl
, get(OpCode
), DestReg
)
873 .addReg(FirstReg
).addReg(SecondReg
)
874 .addReg(Cond
[1].getReg(), 0, SubIdx
);
877 static unsigned getCRBitValue(unsigned CRBit
) {
879 if (CRBit
== PPC::CR0LT
|| CRBit
== PPC::CR1LT
||
880 CRBit
== PPC::CR2LT
|| CRBit
== PPC::CR3LT
||
881 CRBit
== PPC::CR4LT
|| CRBit
== PPC::CR5LT
||
882 CRBit
== PPC::CR6LT
|| CRBit
== PPC::CR7LT
)
884 if (CRBit
== PPC::CR0GT
|| CRBit
== PPC::CR1GT
||
885 CRBit
== PPC::CR2GT
|| CRBit
== PPC::CR3GT
||
886 CRBit
== PPC::CR4GT
|| CRBit
== PPC::CR5GT
||
887 CRBit
== PPC::CR6GT
|| CRBit
== PPC::CR7GT
)
889 if (CRBit
== PPC::CR0EQ
|| CRBit
== PPC::CR1EQ
||
890 CRBit
== PPC::CR2EQ
|| CRBit
== PPC::CR3EQ
||
891 CRBit
== PPC::CR4EQ
|| CRBit
== PPC::CR5EQ
||
892 CRBit
== PPC::CR6EQ
|| CRBit
== PPC::CR7EQ
)
894 if (CRBit
== PPC::CR0UN
|| CRBit
== PPC::CR1UN
||
895 CRBit
== PPC::CR2UN
|| CRBit
== PPC::CR3UN
||
896 CRBit
== PPC::CR4UN
|| CRBit
== PPC::CR5UN
||
897 CRBit
== PPC::CR6UN
|| CRBit
== PPC::CR7UN
)
900 assert(Ret
!= 4 && "Invalid CR bit register");
904 void PPCInstrInfo::copyPhysReg(MachineBasicBlock
&MBB
,
905 MachineBasicBlock::iterator I
,
906 const DebugLoc
&DL
, unsigned DestReg
,
907 unsigned SrcReg
, bool KillSrc
) const {
908 // We can end up with self copies and similar things as a result of VSX copy
909 // legalization. Promote them here.
910 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
911 if (PPC::F8RCRegClass
.contains(DestReg
) &&
912 PPC::VSRCRegClass
.contains(SrcReg
)) {
914 TRI
->getMatchingSuperReg(DestReg
, PPC::sub_64
, &PPC::VSRCRegClass
);
916 if (VSXSelfCopyCrash
&& SrcReg
== SuperReg
)
917 llvm_unreachable("nop VSX copy");
920 } else if (PPC::F8RCRegClass
.contains(SrcReg
) &&
921 PPC::VSRCRegClass
.contains(DestReg
)) {
923 TRI
->getMatchingSuperReg(SrcReg
, PPC::sub_64
, &PPC::VSRCRegClass
);
925 if (VSXSelfCopyCrash
&& DestReg
== SuperReg
)
926 llvm_unreachable("nop VSX copy");
931 // Different class register copy
932 if (PPC::CRBITRCRegClass
.contains(SrcReg
) &&
933 PPC::GPRCRegClass
.contains(DestReg
)) {
934 unsigned CRReg
= getCRFromCRBit(SrcReg
);
935 BuildMI(MBB
, I
, DL
, get(PPC::MFOCRF
), DestReg
).addReg(CRReg
);
936 getKillRegState(KillSrc
);
937 // Rotate the CR bit in the CR fields to be the least significant bit and
938 // then mask with 0x1 (MB = ME = 31).
939 BuildMI(MBB
, I
, DL
, get(PPC::RLWINM
), DestReg
)
940 .addReg(DestReg
, RegState::Kill
)
941 .addImm(TRI
->getEncodingValue(CRReg
) * 4 + (4 - getCRBitValue(SrcReg
)))
945 } else if (PPC::CRRCRegClass
.contains(SrcReg
) &&
946 PPC::G8RCRegClass
.contains(DestReg
)) {
947 BuildMI(MBB
, I
, DL
, get(PPC::MFOCRF8
), DestReg
).addReg(SrcReg
);
948 getKillRegState(KillSrc
);
950 } else if (PPC::CRRCRegClass
.contains(SrcReg
) &&
951 PPC::GPRCRegClass
.contains(DestReg
)) {
952 BuildMI(MBB
, I
, DL
, get(PPC::MFOCRF
), DestReg
).addReg(SrcReg
);
953 getKillRegState(KillSrc
);
955 } else if (PPC::G8RCRegClass
.contains(SrcReg
) &&
956 PPC::VSFRCRegClass
.contains(DestReg
)) {
957 assert(Subtarget
.hasDirectMove() &&
958 "Subtarget doesn't support directmove, don't know how to copy.");
959 BuildMI(MBB
, I
, DL
, get(PPC::MTVSRD
), DestReg
).addReg(SrcReg
);
961 getKillRegState(KillSrc
);
963 } else if (PPC::VSFRCRegClass
.contains(SrcReg
) &&
964 PPC::G8RCRegClass
.contains(DestReg
)) {
965 assert(Subtarget
.hasDirectMove() &&
966 "Subtarget doesn't support directmove, don't know how to copy.");
967 BuildMI(MBB
, I
, DL
, get(PPC::MFVSRD
), DestReg
).addReg(SrcReg
);
968 getKillRegState(KillSrc
);
970 } else if (PPC::SPERCRegClass
.contains(SrcReg
) &&
971 PPC::GPRCRegClass
.contains(DestReg
)) {
972 BuildMI(MBB
, I
, DL
, get(PPC::EFSCFD
), DestReg
).addReg(SrcReg
);
973 getKillRegState(KillSrc
);
975 } else if (PPC::GPRCRegClass
.contains(SrcReg
) &&
976 PPC::SPERCRegClass
.contains(DestReg
)) {
977 BuildMI(MBB
, I
, DL
, get(PPC::EFDCFS
), DestReg
).addReg(SrcReg
);
978 getKillRegState(KillSrc
);
983 if (PPC::GPRCRegClass
.contains(DestReg
, SrcReg
))
985 else if (PPC::G8RCRegClass
.contains(DestReg
, SrcReg
))
987 else if (PPC::F4RCRegClass
.contains(DestReg
, SrcReg
))
989 else if (PPC::CRRCRegClass
.contains(DestReg
, SrcReg
))
991 else if (PPC::VRRCRegClass
.contains(DestReg
, SrcReg
))
993 else if (PPC::VSRCRegClass
.contains(DestReg
, SrcReg
))
994 // There are two different ways this can be done:
995 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
996 // issue in VSU pipeline 0.
997 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
998 // can go to either pipeline.
999 // We'll always use xxlor here, because in practically all cases where
1000 // copies are generated, they are close enough to some use that the
1001 // lower-latency form is preferable.
1003 else if (PPC::VSFRCRegClass
.contains(DestReg
, SrcReg
) ||
1004 PPC::VSSRCRegClass
.contains(DestReg
, SrcReg
))
1005 Opc
= (Subtarget
.hasP9Vector()) ? PPC::XSCPSGNDP
: PPC::XXLORf
;
1006 else if (PPC::QFRCRegClass
.contains(DestReg
, SrcReg
))
1008 else if (PPC::QSRCRegClass
.contains(DestReg
, SrcReg
))
1010 else if (PPC::QBRCRegClass
.contains(DestReg
, SrcReg
))
1012 else if (PPC::CRBITRCRegClass
.contains(DestReg
, SrcReg
))
1014 else if (PPC::SPERCRegClass
.contains(DestReg
, SrcReg
))
1017 llvm_unreachable("Impossible reg-to-reg copy");
1019 const MCInstrDesc
&MCID
= get(Opc
);
1020 if (MCID
.getNumOperands() == 3)
1021 BuildMI(MBB
, I
, DL
, MCID
, DestReg
)
1022 .addReg(SrcReg
).addReg(SrcReg
, getKillRegState(KillSrc
));
1024 BuildMI(MBB
, I
, DL
, MCID
, DestReg
).addReg(SrcReg
, getKillRegState(KillSrc
));
1027 unsigned PPCInstrInfo::getStoreOpcodeForSpill(unsigned Reg
,
1028 const TargetRegisterClass
*RC
)
1030 const unsigned *OpcodesForSpill
= getStoreOpcodesForSpillArray();
1031 int OpcodeIndex
= 0;
1033 if (RC
!= nullptr) {
1034 if (PPC::GPRCRegClass
.hasSubClassEq(RC
) ||
1035 PPC::GPRC_NOR0RegClass
.hasSubClassEq(RC
)) {
1036 OpcodeIndex
= SOK_Int4Spill
;
1037 } else if (PPC::G8RCRegClass
.hasSubClassEq(RC
) ||
1038 PPC::G8RC_NOX0RegClass
.hasSubClassEq(RC
)) {
1039 OpcodeIndex
= SOK_Int8Spill
;
1040 } else if (PPC::F8RCRegClass
.hasSubClassEq(RC
)) {
1041 OpcodeIndex
= SOK_Float8Spill
;
1042 } else if (PPC::F4RCRegClass
.hasSubClassEq(RC
)) {
1043 OpcodeIndex
= SOK_Float4Spill
;
1044 } else if (PPC::SPERCRegClass
.hasSubClassEq(RC
)) {
1045 OpcodeIndex
= SOK_SPESpill
;
1046 } else if (PPC::CRRCRegClass
.hasSubClassEq(RC
)) {
1047 OpcodeIndex
= SOK_CRSpill
;
1048 } else if (PPC::CRBITRCRegClass
.hasSubClassEq(RC
)) {
1049 OpcodeIndex
= SOK_CRBitSpill
;
1050 } else if (PPC::VRRCRegClass
.hasSubClassEq(RC
)) {
1051 OpcodeIndex
= SOK_VRVectorSpill
;
1052 } else if (PPC::VSRCRegClass
.hasSubClassEq(RC
)) {
1053 OpcodeIndex
= SOK_VSXVectorSpill
;
1054 } else if (PPC::VSFRCRegClass
.hasSubClassEq(RC
)) {
1055 OpcodeIndex
= SOK_VectorFloat8Spill
;
1056 } else if (PPC::VSSRCRegClass
.hasSubClassEq(RC
)) {
1057 OpcodeIndex
= SOK_VectorFloat4Spill
;
1058 } else if (PPC::VRSAVERCRegClass
.hasSubClassEq(RC
)) {
1059 OpcodeIndex
= SOK_VRSaveSpill
;
1060 } else if (PPC::QFRCRegClass
.hasSubClassEq(RC
)) {
1061 OpcodeIndex
= SOK_QuadFloat8Spill
;
1062 } else if (PPC::QSRCRegClass
.hasSubClassEq(RC
)) {
1063 OpcodeIndex
= SOK_QuadFloat4Spill
;
1064 } else if (PPC::QBRCRegClass
.hasSubClassEq(RC
)) {
1065 OpcodeIndex
= SOK_QuadBitSpill
;
1066 } else if (PPC::SPILLTOVSRRCRegClass
.hasSubClassEq(RC
)) {
1067 OpcodeIndex
= SOK_SpillToVSR
;
1069 llvm_unreachable("Unknown regclass!");
1072 if (PPC::GPRCRegClass
.contains(Reg
) ||
1073 PPC::GPRC_NOR0RegClass
.contains(Reg
)) {
1074 OpcodeIndex
= SOK_Int4Spill
;
1075 } else if (PPC::G8RCRegClass
.contains(Reg
) ||
1076 PPC::G8RC_NOX0RegClass
.contains(Reg
)) {
1077 OpcodeIndex
= SOK_Int8Spill
;
1078 } else if (PPC::F8RCRegClass
.contains(Reg
)) {
1079 OpcodeIndex
= SOK_Float8Spill
;
1080 } else if (PPC::F4RCRegClass
.contains(Reg
)) {
1081 OpcodeIndex
= SOK_Float4Spill
;
1082 } else if (PPC::SPERCRegClass
.contains(Reg
)) {
1083 OpcodeIndex
= SOK_SPESpill
;
1084 } else if (PPC::CRRCRegClass
.contains(Reg
)) {
1085 OpcodeIndex
= SOK_CRSpill
;
1086 } else if (PPC::CRBITRCRegClass
.contains(Reg
)) {
1087 OpcodeIndex
= SOK_CRBitSpill
;
1088 } else if (PPC::VRRCRegClass
.contains(Reg
)) {
1089 OpcodeIndex
= SOK_VRVectorSpill
;
1090 } else if (PPC::VSRCRegClass
.contains(Reg
)) {
1091 OpcodeIndex
= SOK_VSXVectorSpill
;
1092 } else if (PPC::VSFRCRegClass
.contains(Reg
)) {
1093 OpcodeIndex
= SOK_VectorFloat8Spill
;
1094 } else if (PPC::VSSRCRegClass
.contains(Reg
)) {
1095 OpcodeIndex
= SOK_VectorFloat4Spill
;
1096 } else if (PPC::VRSAVERCRegClass
.contains(Reg
)) {
1097 OpcodeIndex
= SOK_VRSaveSpill
;
1098 } else if (PPC::QFRCRegClass
.contains(Reg
)) {
1099 OpcodeIndex
= SOK_QuadFloat8Spill
;
1100 } else if (PPC::QSRCRegClass
.contains(Reg
)) {
1101 OpcodeIndex
= SOK_QuadFloat4Spill
;
1102 } else if (PPC::QBRCRegClass
.contains(Reg
)) {
1103 OpcodeIndex
= SOK_QuadBitSpill
;
1104 } else if (PPC::SPILLTOVSRRCRegClass
.contains(Reg
)) {
1105 OpcodeIndex
= SOK_SpillToVSR
;
1107 llvm_unreachable("Unknown regclass!");
1110 return OpcodesForSpill
[OpcodeIndex
];
1114 PPCInstrInfo::getLoadOpcodeForSpill(unsigned Reg
,
1115 const TargetRegisterClass
*RC
) const {
1116 const unsigned *OpcodesForSpill
= getLoadOpcodesForSpillArray();
1117 int OpcodeIndex
= 0;
1119 if (RC
!= nullptr) {
1120 if (PPC::GPRCRegClass
.hasSubClassEq(RC
) ||
1121 PPC::GPRC_NOR0RegClass
.hasSubClassEq(RC
)) {
1122 OpcodeIndex
= SOK_Int4Spill
;
1123 } else if (PPC::G8RCRegClass
.hasSubClassEq(RC
) ||
1124 PPC::G8RC_NOX0RegClass
.hasSubClassEq(RC
)) {
1125 OpcodeIndex
= SOK_Int8Spill
;
1126 } else if (PPC::F8RCRegClass
.hasSubClassEq(RC
)) {
1127 OpcodeIndex
= SOK_Float8Spill
;
1128 } else if (PPC::F4RCRegClass
.hasSubClassEq(RC
)) {
1129 OpcodeIndex
= SOK_Float4Spill
;
1130 } else if (PPC::SPERCRegClass
.hasSubClassEq(RC
)) {
1131 OpcodeIndex
= SOK_SPESpill
;
1132 } else if (PPC::CRRCRegClass
.hasSubClassEq(RC
)) {
1133 OpcodeIndex
= SOK_CRSpill
;
1134 } else if (PPC::CRBITRCRegClass
.hasSubClassEq(RC
)) {
1135 OpcodeIndex
= SOK_CRBitSpill
;
1136 } else if (PPC::VRRCRegClass
.hasSubClassEq(RC
)) {
1137 OpcodeIndex
= SOK_VRVectorSpill
;
1138 } else if (PPC::VSRCRegClass
.hasSubClassEq(RC
)) {
1139 OpcodeIndex
= SOK_VSXVectorSpill
;
1140 } else if (PPC::VSFRCRegClass
.hasSubClassEq(RC
)) {
1141 OpcodeIndex
= SOK_VectorFloat8Spill
;
1142 } else if (PPC::VSSRCRegClass
.hasSubClassEq(RC
)) {
1143 OpcodeIndex
= SOK_VectorFloat4Spill
;
1144 } else if (PPC::VRSAVERCRegClass
.hasSubClassEq(RC
)) {
1145 OpcodeIndex
= SOK_VRSaveSpill
;
1146 } else if (PPC::QFRCRegClass
.hasSubClassEq(RC
)) {
1147 OpcodeIndex
= SOK_QuadFloat8Spill
;
1148 } else if (PPC::QSRCRegClass
.hasSubClassEq(RC
)) {
1149 OpcodeIndex
= SOK_QuadFloat4Spill
;
1150 } else if (PPC::QBRCRegClass
.hasSubClassEq(RC
)) {
1151 OpcodeIndex
= SOK_QuadBitSpill
;
1152 } else if (PPC::SPILLTOVSRRCRegClass
.hasSubClassEq(RC
)) {
1153 OpcodeIndex
= SOK_SpillToVSR
;
1155 llvm_unreachable("Unknown regclass!");
1158 if (PPC::GPRCRegClass
.contains(Reg
) ||
1159 PPC::GPRC_NOR0RegClass
.contains(Reg
)) {
1160 OpcodeIndex
= SOK_Int4Spill
;
1161 } else if (PPC::G8RCRegClass
.contains(Reg
) ||
1162 PPC::G8RC_NOX0RegClass
.contains(Reg
)) {
1163 OpcodeIndex
= SOK_Int8Spill
;
1164 } else if (PPC::F8RCRegClass
.contains(Reg
)) {
1165 OpcodeIndex
= SOK_Float8Spill
;
1166 } else if (PPC::F4RCRegClass
.contains(Reg
)) {
1167 OpcodeIndex
= SOK_Float4Spill
;
1168 } else if (PPC::SPERCRegClass
.contains(Reg
)) {
1169 OpcodeIndex
= SOK_SPESpill
;
1170 } else if (PPC::CRRCRegClass
.contains(Reg
)) {
1171 OpcodeIndex
= SOK_CRSpill
;
1172 } else if (PPC::CRBITRCRegClass
.contains(Reg
)) {
1173 OpcodeIndex
= SOK_CRBitSpill
;
1174 } else if (PPC::VRRCRegClass
.contains(Reg
)) {
1175 OpcodeIndex
= SOK_VRVectorSpill
;
1176 } else if (PPC::VSRCRegClass
.contains(Reg
)) {
1177 OpcodeIndex
= SOK_VSXVectorSpill
;
1178 } else if (PPC::VSFRCRegClass
.contains(Reg
)) {
1179 OpcodeIndex
= SOK_VectorFloat8Spill
;
1180 } else if (PPC::VSSRCRegClass
.contains(Reg
)) {
1181 OpcodeIndex
= SOK_VectorFloat4Spill
;
1182 } else if (PPC::VRSAVERCRegClass
.contains(Reg
)) {
1183 OpcodeIndex
= SOK_VRSaveSpill
;
1184 } else if (PPC::QFRCRegClass
.contains(Reg
)) {
1185 OpcodeIndex
= SOK_QuadFloat8Spill
;
1186 } else if (PPC::QSRCRegClass
.contains(Reg
)) {
1187 OpcodeIndex
= SOK_QuadFloat4Spill
;
1188 } else if (PPC::QBRCRegClass
.contains(Reg
)) {
1189 OpcodeIndex
= SOK_QuadBitSpill
;
1190 } else if (PPC::SPILLTOVSRRCRegClass
.contains(Reg
)) {
1191 OpcodeIndex
= SOK_SpillToVSR
;
1193 llvm_unreachable("Unknown regclass!");
1196 return OpcodesForSpill
[OpcodeIndex
];
1199 void PPCInstrInfo::StoreRegToStackSlot(
1200 MachineFunction
&MF
, unsigned SrcReg
, bool isKill
, int FrameIdx
,
1201 const TargetRegisterClass
*RC
,
1202 SmallVectorImpl
<MachineInstr
*> &NewMIs
) const {
1203 unsigned Opcode
= getStoreOpcodeForSpill(PPC::NoRegister
, RC
);
1206 PPCFunctionInfo
*FuncInfo
= MF
.getInfo
<PPCFunctionInfo
>();
1207 FuncInfo
->setHasSpills();
1209 NewMIs
.push_back(addFrameReference(
1210 BuildMI(MF
, DL
, get(Opcode
)).addReg(SrcReg
, getKillRegState(isKill
)),
1213 if (PPC::CRRCRegClass
.hasSubClassEq(RC
) ||
1214 PPC::CRBITRCRegClass
.hasSubClassEq(RC
))
1215 FuncInfo
->setSpillsCR();
1217 if (PPC::VRSAVERCRegClass
.hasSubClassEq(RC
))
1218 FuncInfo
->setSpillsVRSAVE();
1220 if (isXFormMemOp(Opcode
))
1221 FuncInfo
->setHasNonRISpills();
1224 void PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock
&MBB
,
1225 MachineBasicBlock::iterator MI
,
1226 unsigned SrcReg
, bool isKill
,
1228 const TargetRegisterClass
*RC
,
1229 const TargetRegisterInfo
*TRI
) const {
1230 MachineFunction
&MF
= *MBB
.getParent();
1231 SmallVector
<MachineInstr
*, 4> NewMIs
;
1233 // We need to avoid a situation in which the value from a VRRC register is
1234 // spilled using an Altivec instruction and reloaded into a VSRC register
1235 // using a VSX instruction. The issue with this is that the VSX
1236 // load/store instructions swap the doublewords in the vector and the Altivec
1237 // ones don't. The register classes on the spill/reload may be different if
1238 // the register is defined using an Altivec instruction and is then used by a
1242 StoreRegToStackSlot(MF
, SrcReg
, isKill
, FrameIdx
, RC
, NewMIs
);
1244 for (unsigned i
= 0, e
= NewMIs
.size(); i
!= e
; ++i
)
1245 MBB
.insert(MI
, NewMIs
[i
]);
1247 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
1248 MachineMemOperand
*MMO
= MF
.getMachineMemOperand(
1249 MachinePointerInfo::getFixedStack(MF
, FrameIdx
),
1250 MachineMemOperand::MOStore
, MFI
.getObjectSize(FrameIdx
),
1251 MFI
.getObjectAlignment(FrameIdx
));
1252 NewMIs
.back()->addMemOperand(MF
, MMO
);
1255 void PPCInstrInfo::LoadRegFromStackSlot(MachineFunction
&MF
, const DebugLoc
&DL
,
1256 unsigned DestReg
, int FrameIdx
,
1257 const TargetRegisterClass
*RC
,
1258 SmallVectorImpl
<MachineInstr
*> &NewMIs
)
1260 unsigned Opcode
= getLoadOpcodeForSpill(PPC::NoRegister
, RC
);
1261 NewMIs
.push_back(addFrameReference(BuildMI(MF
, DL
, get(Opcode
), DestReg
),
1263 PPCFunctionInfo
*FuncInfo
= MF
.getInfo
<PPCFunctionInfo
>();
1265 if (PPC::CRRCRegClass
.hasSubClassEq(RC
) ||
1266 PPC::CRBITRCRegClass
.hasSubClassEq(RC
))
1267 FuncInfo
->setSpillsCR();
1269 if (PPC::VRSAVERCRegClass
.hasSubClassEq(RC
))
1270 FuncInfo
->setSpillsVRSAVE();
1272 if (isXFormMemOp(Opcode
))
1273 FuncInfo
->setHasNonRISpills();
1277 PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock
&MBB
,
1278 MachineBasicBlock::iterator MI
,
1279 unsigned DestReg
, int FrameIdx
,
1280 const TargetRegisterClass
*RC
,
1281 const TargetRegisterInfo
*TRI
) const {
1282 MachineFunction
&MF
= *MBB
.getParent();
1283 SmallVector
<MachineInstr
*, 4> NewMIs
;
1285 if (MI
!= MBB
.end()) DL
= MI
->getDebugLoc();
1287 PPCFunctionInfo
*FuncInfo
= MF
.getInfo
<PPCFunctionInfo
>();
1288 FuncInfo
->setHasSpills();
1290 // We need to avoid a situation in which the value from a VRRC register is
1291 // spilled using an Altivec instruction and reloaded into a VSRC register
1292 // using a VSX instruction. The issue with this is that the VSX
1293 // load/store instructions swap the doublewords in the vector and the Altivec
1294 // ones don't. The register classes on the spill/reload may be different if
1295 // the register is defined using an Altivec instruction and is then used by a
1297 if (Subtarget
.hasVSX() && RC
== &PPC::VRRCRegClass
)
1298 RC
= &PPC::VSRCRegClass
;
1300 LoadRegFromStackSlot(MF
, DL
, DestReg
, FrameIdx
, RC
, NewMIs
);
1302 for (unsigned i
= 0, e
= NewMIs
.size(); i
!= e
; ++i
)
1303 MBB
.insert(MI
, NewMIs
[i
]);
1305 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
1306 MachineMemOperand
*MMO
= MF
.getMachineMemOperand(
1307 MachinePointerInfo::getFixedStack(MF
, FrameIdx
),
1308 MachineMemOperand::MOLoad
, MFI
.getObjectSize(FrameIdx
),
1309 MFI
.getObjectAlignment(FrameIdx
));
1310 NewMIs
.back()->addMemOperand(MF
, MMO
);
1314 reverseBranchCondition(SmallVectorImpl
<MachineOperand
> &Cond
) const {
1315 assert(Cond
.size() == 2 && "Invalid PPC branch opcode!");
1316 if (Cond
[1].getReg() == PPC::CTR8
|| Cond
[1].getReg() == PPC::CTR
)
1317 Cond
[0].setImm(Cond
[0].getImm() == 0 ? 1 : 0);
1319 // Leave the CR# the same, but invert the condition.
1320 Cond
[0].setImm(PPC::InvertPredicate((PPC::Predicate
)Cond
[0].getImm()));
1324 bool PPCInstrInfo::FoldImmediate(MachineInstr
&UseMI
, MachineInstr
&DefMI
,
1325 unsigned Reg
, MachineRegisterInfo
*MRI
) const {
1326 // For some instructions, it is legal to fold ZERO into the RA register field.
1327 // A zero immediate should always be loaded with a single li.
1328 unsigned DefOpc
= DefMI
.getOpcode();
1329 if (DefOpc
!= PPC::LI
&& DefOpc
!= PPC::LI8
)
1331 if (!DefMI
.getOperand(1).isImm())
1333 if (DefMI
.getOperand(1).getImm() != 0)
1336 // Note that we cannot here invert the arguments of an isel in order to fold
1337 // a ZERO into what is presented as the second argument. All we have here
1338 // is the condition bit, and that might come from a CR-logical bit operation.
1340 const MCInstrDesc
&UseMCID
= UseMI
.getDesc();
1342 // Only fold into real machine instructions.
1343 if (UseMCID
.isPseudo())
1347 for (UseIdx
= 0; UseIdx
< UseMI
.getNumOperands(); ++UseIdx
)
1348 if (UseMI
.getOperand(UseIdx
).isReg() &&
1349 UseMI
.getOperand(UseIdx
).getReg() == Reg
)
1352 assert(UseIdx
< UseMI
.getNumOperands() && "Cannot find Reg in UseMI");
1353 assert(UseIdx
< UseMCID
.getNumOperands() && "No operand description for Reg");
1355 const MCOperandInfo
*UseInfo
= &UseMCID
.OpInfo
[UseIdx
];
1357 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
1358 // register (which might also be specified as a pointer class kind).
1359 if (UseInfo
->isLookupPtrRegClass()) {
1360 if (UseInfo
->RegClass
/* Kind */ != 1)
1363 if (UseInfo
->RegClass
!= PPC::GPRC_NOR0RegClassID
&&
1364 UseInfo
->RegClass
!= PPC::G8RC_NOX0RegClassID
)
1368 // Make sure this is not tied to an output register (or otherwise
1369 // constrained). This is true for ST?UX registers, for example, which
1370 // are tied to their output registers.
1371 if (UseInfo
->Constraints
!= 0)
1375 if (UseInfo
->isLookupPtrRegClass()) {
1376 bool isPPC64
= Subtarget
.isPPC64();
1377 ZeroReg
= isPPC64
? PPC::ZERO8
: PPC::ZERO
;
1379 ZeroReg
= UseInfo
->RegClass
== PPC::G8RC_NOX0RegClassID
?
1380 PPC::ZERO8
: PPC::ZERO
;
1383 bool DeleteDef
= MRI
->hasOneNonDBGUse(Reg
);
1384 UseMI
.getOperand(UseIdx
).setReg(ZeroReg
);
1387 DefMI
.eraseFromParent();
1392 static bool MBBDefinesCTR(MachineBasicBlock
&MBB
) {
1393 for (MachineBasicBlock::iterator I
= MBB
.begin(), IE
= MBB
.end();
1395 if (I
->definesRegister(PPC::CTR
) || I
->definesRegister(PPC::CTR8
))
1400 // We should make sure that, if we're going to predicate both sides of a
1401 // condition (a diamond), that both sides don't define the counter register. We
1402 // can predicate counter-decrement-based branches, but while that predicates
1403 // the branching, it does not predicate the counter decrement. If we tried to
1404 // merge the triangle into one predicated block, we'd decrement the counter
1406 bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock
&TMBB
,
1407 unsigned NumT
, unsigned ExtraT
,
1408 MachineBasicBlock
&FMBB
,
1409 unsigned NumF
, unsigned ExtraF
,
1410 BranchProbability Probability
) const {
1411 return !(MBBDefinesCTR(TMBB
) && MBBDefinesCTR(FMBB
));
1415 bool PPCInstrInfo::isPredicated(const MachineInstr
&MI
) const {
1416 // The predicated branches are identified by their type, not really by the
1417 // explicit presence of a predicate. Furthermore, some of them can be
1418 // predicated more than once. Because if conversion won't try to predicate
1419 // any instruction which already claims to be predicated (by returning true
1420 // here), always return false. In doing so, we let isPredicable() be the
1421 // final word on whether not the instruction can be (further) predicated.
1426 bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr
&MI
) const {
1427 if (!MI
.isTerminator())
1430 // Conditional branch is a special case.
1431 if (MI
.isBranch() && !MI
.isBarrier())
1434 return !isPredicated(MI
);
1437 bool PPCInstrInfo::PredicateInstruction(MachineInstr
&MI
,
1438 ArrayRef
<MachineOperand
> Pred
) const {
1439 unsigned OpC
= MI
.getOpcode();
1440 if (OpC
== PPC::BLR
|| OpC
== PPC::BLR8
) {
1441 if (Pred
[1].getReg() == PPC::CTR8
|| Pred
[1].getReg() == PPC::CTR
) {
1442 bool isPPC64
= Subtarget
.isPPC64();
1443 MI
.setDesc(get(Pred
[0].getImm() ? (isPPC64
? PPC::BDNZLR8
: PPC::BDNZLR
)
1444 : (isPPC64
? PPC::BDZLR8
: PPC::BDZLR
)));
1445 } else if (Pred
[0].getImm() == PPC::PRED_BIT_SET
) {
1446 MI
.setDesc(get(PPC::BCLR
));
1447 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
).add(Pred
[1]);
1448 } else if (Pred
[0].getImm() == PPC::PRED_BIT_UNSET
) {
1449 MI
.setDesc(get(PPC::BCLRn
));
1450 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
).add(Pred
[1]);
1452 MI
.setDesc(get(PPC::BCCLR
));
1453 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
1454 .addImm(Pred
[0].getImm())
1459 } else if (OpC
== PPC::B
) {
1460 if (Pred
[1].getReg() == PPC::CTR8
|| Pred
[1].getReg() == PPC::CTR
) {
1461 bool isPPC64
= Subtarget
.isPPC64();
1462 MI
.setDesc(get(Pred
[0].getImm() ? (isPPC64
? PPC::BDNZ8
: PPC::BDNZ
)
1463 : (isPPC64
? PPC::BDZ8
: PPC::BDZ
)));
1464 } else if (Pred
[0].getImm() == PPC::PRED_BIT_SET
) {
1465 MachineBasicBlock
*MBB
= MI
.getOperand(0).getMBB();
1466 MI
.RemoveOperand(0);
1468 MI
.setDesc(get(PPC::BC
));
1469 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
1472 } else if (Pred
[0].getImm() == PPC::PRED_BIT_UNSET
) {
1473 MachineBasicBlock
*MBB
= MI
.getOperand(0).getMBB();
1474 MI
.RemoveOperand(0);
1476 MI
.setDesc(get(PPC::BCn
));
1477 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
1481 MachineBasicBlock
*MBB
= MI
.getOperand(0).getMBB();
1482 MI
.RemoveOperand(0);
1484 MI
.setDesc(get(PPC::BCC
));
1485 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
1486 .addImm(Pred
[0].getImm())
1492 } else if (OpC
== PPC::BCTR
|| OpC
== PPC::BCTR8
|| OpC
== PPC::BCTRL
||
1493 OpC
== PPC::BCTRL8
) {
1494 if (Pred
[1].getReg() == PPC::CTR8
|| Pred
[1].getReg() == PPC::CTR
)
1495 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1497 bool setLR
= OpC
== PPC::BCTRL
|| OpC
== PPC::BCTRL8
;
1498 bool isPPC64
= Subtarget
.isPPC64();
1500 if (Pred
[0].getImm() == PPC::PRED_BIT_SET
) {
1501 MI
.setDesc(get(isPPC64
? (setLR
? PPC::BCCTRL8
: PPC::BCCTR8
)
1502 : (setLR
? PPC::BCCTRL
: PPC::BCCTR
)));
1503 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
).add(Pred
[1]);
1505 } else if (Pred
[0].getImm() == PPC::PRED_BIT_UNSET
) {
1506 MI
.setDesc(get(isPPC64
? (setLR
? PPC::BCCTRL8n
: PPC::BCCTR8n
)
1507 : (setLR
? PPC::BCCTRLn
: PPC::BCCTRn
)));
1508 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
).add(Pred
[1]);
1512 MI
.setDesc(get(isPPC64
? (setLR
? PPC::BCCCTRL8
: PPC::BCCCTR8
)
1513 : (setLR
? PPC::BCCCTRL
: PPC::BCCCTR
)));
1514 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
1515 .addImm(Pred
[0].getImm())
1523 bool PPCInstrInfo::SubsumesPredicate(ArrayRef
<MachineOperand
> Pred1
,
1524 ArrayRef
<MachineOperand
> Pred2
) const {
1525 assert(Pred1
.size() == 2 && "Invalid PPC first predicate");
1526 assert(Pred2
.size() == 2 && "Invalid PPC second predicate");
1528 if (Pred1
[1].getReg() == PPC::CTR8
|| Pred1
[1].getReg() == PPC::CTR
)
1530 if (Pred2
[1].getReg() == PPC::CTR8
|| Pred2
[1].getReg() == PPC::CTR
)
1533 // P1 can only subsume P2 if they test the same condition register.
1534 if (Pred1
[1].getReg() != Pred2
[1].getReg())
1537 PPC::Predicate P1
= (PPC::Predicate
) Pred1
[0].getImm();
1538 PPC::Predicate P2
= (PPC::Predicate
) Pred2
[0].getImm();
1543 // Does P1 subsume P2, e.g. GE subsumes GT.
1544 if (P1
== PPC::PRED_LE
&&
1545 (P2
== PPC::PRED_LT
|| P2
== PPC::PRED_EQ
))
1547 if (P1
== PPC::PRED_GE
&&
1548 (P2
== PPC::PRED_GT
|| P2
== PPC::PRED_EQ
))
1554 bool PPCInstrInfo::DefinesPredicate(MachineInstr
&MI
,
1555 std::vector
<MachineOperand
> &Pred
) const {
1556 // Note: At the present time, the contents of Pred from this function is
1557 // unused by IfConversion. This implementation follows ARM by pushing the
1558 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1559 // predicate, instructions defining CTR or CTR8 are also included as
1560 // predicate-defining instructions.
1562 const TargetRegisterClass
*RCs
[] =
1563 { &PPC::CRRCRegClass
, &PPC::CRBITRCRegClass
,
1564 &PPC::CTRRCRegClass
, &PPC::CTRRC8RegClass
};
1567 for (unsigned i
= 0, e
= MI
.getNumOperands(); i
!= e
; ++i
) {
1568 const MachineOperand
&MO
= MI
.getOperand(i
);
1569 for (unsigned c
= 0; c
< array_lengthof(RCs
) && !Found
; ++c
) {
1570 const TargetRegisterClass
*RC
= RCs
[c
];
1572 if (MO
.isDef() && RC
->contains(MO
.getReg())) {
1576 } else if (MO
.isRegMask()) {
1577 for (TargetRegisterClass::iterator I
= RC
->begin(),
1578 IE
= RC
->end(); I
!= IE
; ++I
)
1579 if (MO
.clobbersPhysReg(*I
)) {
1590 bool PPCInstrInfo::isPredicable(const MachineInstr
&MI
) const {
1591 unsigned OpC
= MI
.getOpcode();
1606 bool PPCInstrInfo::analyzeCompare(const MachineInstr
&MI
, unsigned &SrcReg
,
1607 unsigned &SrcReg2
, int &Mask
,
1609 unsigned Opc
= MI
.getOpcode();
1612 default: return false;
1617 SrcReg
= MI
.getOperand(1).getReg();
1619 Value
= MI
.getOperand(2).getImm();
1628 SrcReg
= MI
.getOperand(1).getReg();
1629 SrcReg2
= MI
.getOperand(2).getReg();
1636 bool PPCInstrInfo::optimizeCompareInstr(MachineInstr
&CmpInstr
, unsigned SrcReg
,
1637 unsigned SrcReg2
, int Mask
, int Value
,
1638 const MachineRegisterInfo
*MRI
) const {
1642 int OpC
= CmpInstr
.getOpcode();
1643 Register CRReg
= CmpInstr
.getOperand(0).getReg();
1645 // FP record forms set CR1 based on the exception status bits, not a
1646 // comparison with zero.
1647 if (OpC
== PPC::FCMPUS
|| OpC
== PPC::FCMPUD
)
1650 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
1651 // The record forms set the condition register based on a signed comparison
1652 // with zero (so says the ISA manual). This is not as straightforward as it
1653 // seems, however, because this is always a 64-bit comparison on PPC64, even
1654 // for instructions that are 32-bit in nature (like slw for example).
1655 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1656 // for equality checks (as those don't depend on the sign). On PPC64,
1657 // we are restricted to equality for unsigned 64-bit comparisons and for
1658 // signed 32-bit comparisons the applicability is more restricted.
1659 bool isPPC64
= Subtarget
.isPPC64();
1660 bool is32BitSignedCompare
= OpC
== PPC::CMPWI
|| OpC
== PPC::CMPW
;
1661 bool is32BitUnsignedCompare
= OpC
== PPC::CMPLWI
|| OpC
== PPC::CMPLW
;
1662 bool is64BitUnsignedCompare
= OpC
== PPC::CMPLDI
|| OpC
== PPC::CMPLD
;
1664 // Look through copies unless that gets us to a physical register.
1665 unsigned ActualSrc
= TRI
->lookThruCopyLike(SrcReg
, MRI
);
1666 if (Register::isVirtualRegister(ActualSrc
))
1669 // Get the unique definition of SrcReg.
1670 MachineInstr
*MI
= MRI
->getUniqueVRegDef(SrcReg
);
1671 if (!MI
) return false;
1673 bool equalityOnly
= false;
1676 if (is32BitSignedCompare
) {
1677 // We can perform this optimization only if MI is sign-extending.
1678 if (isSignExtended(*MI
))
1682 } else if (is32BitUnsignedCompare
) {
1683 // We can perform this optimization, equality only, if MI is
1685 if (isZeroExtended(*MI
)) {
1687 equalityOnly
= true;
1691 equalityOnly
= is64BitUnsignedCompare
;
1693 equalityOnly
= is32BitUnsignedCompare
;
1696 // We need to check the uses of the condition register in order to reject
1697 // non-equality comparisons.
1698 for (MachineRegisterInfo::use_instr_iterator
1699 I
= MRI
->use_instr_begin(CRReg
), IE
= MRI
->use_instr_end();
1701 MachineInstr
*UseMI
= &*I
;
1702 if (UseMI
->getOpcode() == PPC::BCC
) {
1703 PPC::Predicate Pred
= (PPC::Predicate
)UseMI
->getOperand(0).getImm();
1704 unsigned PredCond
= PPC::getPredicateCondition(Pred
);
1705 // We ignore hint bits when checking for non-equality comparisons.
1706 if (PredCond
!= PPC::PRED_EQ
&& PredCond
!= PPC::PRED_NE
)
1708 } else if (UseMI
->getOpcode() == PPC::ISEL
||
1709 UseMI
->getOpcode() == PPC::ISEL8
) {
1710 unsigned SubIdx
= UseMI
->getOperand(3).getSubReg();
1711 if (SubIdx
!= PPC::sub_eq
)
1718 MachineBasicBlock::iterator I
= CmpInstr
;
1720 // Scan forward to find the first use of the compare.
1721 for (MachineBasicBlock::iterator EL
= CmpInstr
.getParent()->end(); I
!= EL
;
1723 bool FoundUse
= false;
1724 for (MachineRegisterInfo::use_instr_iterator
1725 J
= MRI
->use_instr_begin(CRReg
), JE
= MRI
->use_instr_end();
1736 SmallVector
<std::pair
<MachineOperand
*, PPC::Predicate
>, 4> PredsToUpdate
;
1737 SmallVector
<std::pair
<MachineOperand
*, unsigned>, 4> SubRegsToUpdate
;
1739 // There are two possible candidates which can be changed to set CR[01].
1740 // One is MI, the other is a SUB instruction.
1741 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
1742 MachineInstr
*Sub
= nullptr;
1744 // MI is not a candidate for CMPrr.
1746 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1747 // same BB as the comparison. This is to allow the check below to avoid calls
1748 // (and other explicit clobbers); instead we should really check for these
1749 // more explicitly (in at least a few predecessors).
1750 else if (MI
->getParent() != CmpInstr
.getParent())
1752 else if (Value
!= 0) {
1753 // The record-form instructions set CR bit based on signed comparison
1754 // against 0. We try to convert a compare against 1 or -1 into a compare
1755 // against 0 to exploit record-form instructions. For example, we change
1756 // the condition "greater than -1" into "greater than or equal to 0"
1757 // and "less than 1" into "less than or equal to 0".
1759 // Since we optimize comparison based on a specific branch condition,
1760 // we don't optimize if condition code is used by more than once.
1761 if (equalityOnly
|| !MRI
->hasOneUse(CRReg
))
1764 MachineInstr
*UseMI
= &*MRI
->use_instr_begin(CRReg
);
1765 if (UseMI
->getOpcode() != PPC::BCC
)
1768 PPC::Predicate Pred
= (PPC::Predicate
)UseMI
->getOperand(0).getImm();
1769 unsigned PredCond
= PPC::getPredicateCondition(Pred
);
1770 unsigned PredHint
= PPC::getPredicateHint(Pred
);
1771 int16_t Immed
= (int16_t)Value
;
1773 // When modifying the condition in the predicate, we propagate hint bits
1774 // from the original predicate to the new one.
1775 if (Immed
== -1 && PredCond
== PPC::PRED_GT
)
1776 // We convert "greater than -1" into "greater than or equal to 0",
1777 // since we are assuming signed comparison by !equalityOnly
1778 Pred
= PPC::getPredicate(PPC::PRED_GE
, PredHint
);
1779 else if (Immed
== -1 && PredCond
== PPC::PRED_LE
)
1780 // We convert "less than or equal to -1" into "less than 0".
1781 Pred
= PPC::getPredicate(PPC::PRED_LT
, PredHint
);
1782 else if (Immed
== 1 && PredCond
== PPC::PRED_LT
)
1783 // We convert "less than 1" into "less than or equal to 0".
1784 Pred
= PPC::getPredicate(PPC::PRED_LE
, PredHint
);
1785 else if (Immed
== 1 && PredCond
== PPC::PRED_GE
)
1786 // We convert "greater than or equal to 1" into "greater than 0".
1787 Pred
= PPC::getPredicate(PPC::PRED_GT
, PredHint
);
1791 PredsToUpdate
.push_back(std::make_pair(&(UseMI
->getOperand(0)), Pred
));
1797 // Get ready to iterate backward from CmpInstr.
1798 MachineBasicBlock::iterator E
= MI
, B
= CmpInstr
.getParent()->begin();
1800 for (; I
!= E
&& !noSub
; --I
) {
1801 const MachineInstr
&Instr
= *I
;
1802 unsigned IOpC
= Instr
.getOpcode();
1804 if (&*I
!= &CmpInstr
&& (Instr
.modifiesRegister(PPC::CR0
, TRI
) ||
1805 Instr
.readsRegister(PPC::CR0
, TRI
)))
1806 // This instruction modifies or uses the record condition register after
1807 // the one we want to change. While we could do this transformation, it
1808 // would likely not be profitable. This transformation removes one
1809 // instruction, and so even forcing RA to generate one move probably
1810 // makes it unprofitable.
1813 // Check whether CmpInstr can be made redundant by the current instruction.
1814 if ((OpC
== PPC::CMPW
|| OpC
== PPC::CMPLW
||
1815 OpC
== PPC::CMPD
|| OpC
== PPC::CMPLD
) &&
1816 (IOpC
== PPC::SUBF
|| IOpC
== PPC::SUBF8
) &&
1817 ((Instr
.getOperand(1).getReg() == SrcReg
&&
1818 Instr
.getOperand(2).getReg() == SrcReg2
) ||
1819 (Instr
.getOperand(1).getReg() == SrcReg2
&&
1820 Instr
.getOperand(2).getReg() == SrcReg
))) {
1826 // The 'and' is below the comparison instruction.
1830 // Return false if no candidates exist.
1834 // The single candidate is called MI.
1838 int MIOpC
= MI
->getOpcode();
1839 if (MIOpC
== PPC::ANDIo
|| MIOpC
== PPC::ANDIo8
||
1840 MIOpC
== PPC::ANDISo
|| MIOpC
== PPC::ANDISo8
)
1843 NewOpC
= PPC::getRecordFormOpcode(MIOpC
);
1844 if (NewOpC
== -1 && PPC::getNonRecordFormOpcode(MIOpC
) != -1)
1848 // FIXME: On the non-embedded POWER architectures, only some of the record
1849 // forms are fast, and we should use only the fast ones.
1851 // The defining instruction has a record form (or is already a record
1852 // form). It is possible, however, that we'll need to reverse the condition
1853 // code of the users.
1857 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1858 // needs to be updated to be based on SUB. Push the condition code
1859 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1860 // condition code of these operands will be modified.
1861 // Here, Value == 0 means we haven't converted comparison against 1 or -1 to
1862 // comparison against 0, which may modify predicate.
1863 bool ShouldSwap
= false;
1864 if (Sub
&& Value
== 0) {
1865 ShouldSwap
= SrcReg2
!= 0 && Sub
->getOperand(1).getReg() == SrcReg2
&&
1866 Sub
->getOperand(2).getReg() == SrcReg
;
1868 // The operands to subf are the opposite of sub, so only in the fixed-point
1869 // case, invert the order.
1870 ShouldSwap
= !ShouldSwap
;
1874 for (MachineRegisterInfo::use_instr_iterator
1875 I
= MRI
->use_instr_begin(CRReg
), IE
= MRI
->use_instr_end();
1877 MachineInstr
*UseMI
= &*I
;
1878 if (UseMI
->getOpcode() == PPC::BCC
) {
1879 PPC::Predicate Pred
= (PPC::Predicate
) UseMI
->getOperand(0).getImm();
1880 unsigned PredCond
= PPC::getPredicateCondition(Pred
);
1881 assert((!equalityOnly
||
1882 PredCond
== PPC::PRED_EQ
|| PredCond
== PPC::PRED_NE
) &&
1883 "Invalid predicate for equality-only optimization");
1884 (void)PredCond
; // To suppress warning in release build.
1885 PredsToUpdate
.push_back(std::make_pair(&(UseMI
->getOperand(0)),
1886 PPC::getSwappedPredicate(Pred
)));
1887 } else if (UseMI
->getOpcode() == PPC::ISEL
||
1888 UseMI
->getOpcode() == PPC::ISEL8
) {
1889 unsigned NewSubReg
= UseMI
->getOperand(3).getSubReg();
1890 assert((!equalityOnly
|| NewSubReg
== PPC::sub_eq
) &&
1891 "Invalid CR bit for equality-only optimization");
1893 if (NewSubReg
== PPC::sub_lt
)
1894 NewSubReg
= PPC::sub_gt
;
1895 else if (NewSubReg
== PPC::sub_gt
)
1896 NewSubReg
= PPC::sub_lt
;
1898 SubRegsToUpdate
.push_back(std::make_pair(&(UseMI
->getOperand(3)),
1900 } else // We need to abort on a user we don't understand.
1903 assert(!(Value
!= 0 && ShouldSwap
) &&
1904 "Non-zero immediate support and ShouldSwap"
1905 "may conflict in updating predicate");
1907 // Create a new virtual register to hold the value of the CR set by the
1908 // record-form instruction. If the instruction was not previously in
1909 // record form, then set the kill flag on the CR.
1910 CmpInstr
.eraseFromParent();
1912 MachineBasicBlock::iterator MII
= MI
;
1913 BuildMI(*MI
->getParent(), std::next(MII
), MI
->getDebugLoc(),
1914 get(TargetOpcode::COPY
), CRReg
)
1915 .addReg(PPC::CR0
, MIOpC
!= NewOpC
? RegState::Kill
: 0);
1917 // Even if CR0 register were dead before, it is alive now since the
1918 // instruction we just built uses it.
1919 MI
->clearRegisterDeads(PPC::CR0
);
1921 if (MIOpC
!= NewOpC
) {
1922 // We need to be careful here: we're replacing one instruction with
1923 // another, and we need to make sure that we get all of the right
1924 // implicit uses and defs. On the other hand, the caller may be holding
1925 // an iterator to this instruction, and so we can't delete it (this is
1926 // specifically the case if this is the instruction directly after the
1929 // Rotates are expensive instructions. If we're emitting a record-form
1930 // rotate that can just be an andi/andis, we should just emit that.
1931 if (MIOpC
== PPC::RLWINM
|| MIOpC
== PPC::RLWINM8
) {
1932 Register GPRRes
= MI
->getOperand(0).getReg();
1933 int64_t SH
= MI
->getOperand(2).getImm();
1934 int64_t MB
= MI
->getOperand(3).getImm();
1935 int64_t ME
= MI
->getOperand(4).getImm();
1936 // We can only do this if both the start and end of the mask are in the
1938 bool MBInLoHWord
= MB
>= 16;
1939 bool MEInLoHWord
= ME
>= 16;
1940 uint64_t Mask
= ~0LLU;
1942 if (MB
<= ME
&& MBInLoHWord
== MEInLoHWord
&& SH
== 0) {
1943 Mask
= ((1LLU << (32 - MB
)) - 1) & ~((1LLU << (31 - ME
)) - 1);
1944 // The mask value needs to shift right 16 if we're emitting andis.
1945 Mask
>>= MBInLoHWord
? 0 : 16;
1946 NewOpC
= MIOpC
== PPC::RLWINM
?
1947 (MBInLoHWord
? PPC::ANDIo
: PPC::ANDISo
) :
1948 (MBInLoHWord
? PPC::ANDIo8
:PPC::ANDISo8
);
1949 } else if (MRI
->use_empty(GPRRes
) && (ME
== 31) &&
1950 (ME
- MB
+ 1 == SH
) && (MB
>= 16)) {
1951 // If we are rotating by the exact number of bits as are in the mask
1952 // and the mask is in the least significant bits of the register,
1953 // that's just an andis. (as long as the GPR result has no uses).
1954 Mask
= ((1LLU << 32) - 1) & ~((1LLU << (32 - SH
)) - 1);
1956 NewOpC
= MIOpC
== PPC::RLWINM
? PPC::ANDISo
:PPC::ANDISo8
;
1958 // If we've set the mask, we can transform.
1959 if (Mask
!= ~0LLU) {
1960 MI
->RemoveOperand(4);
1961 MI
->RemoveOperand(3);
1962 MI
->getOperand(2).setImm(Mask
);
1963 NumRcRotatesConvertedToRcAnd
++;
1965 } else if (MIOpC
== PPC::RLDICL
&& MI
->getOperand(2).getImm() == 0) {
1966 int64_t MB
= MI
->getOperand(3).getImm();
1968 uint64_t Mask
= (1LLU << (63 - MB
+ 1)) - 1;
1969 NewOpC
= PPC::ANDIo8
;
1970 MI
->RemoveOperand(3);
1971 MI
->getOperand(2).setImm(Mask
);
1972 NumRcRotatesConvertedToRcAnd
++;
1976 const MCInstrDesc
&NewDesc
= get(NewOpC
);
1977 MI
->setDesc(NewDesc
);
1979 if (NewDesc
.ImplicitDefs
)
1980 for (const MCPhysReg
*ImpDefs
= NewDesc
.getImplicitDefs();
1981 *ImpDefs
; ++ImpDefs
)
1982 if (!MI
->definesRegister(*ImpDefs
))
1983 MI
->addOperand(*MI
->getParent()->getParent(),
1984 MachineOperand::CreateReg(*ImpDefs
, true, true));
1985 if (NewDesc
.ImplicitUses
)
1986 for (const MCPhysReg
*ImpUses
= NewDesc
.getImplicitUses();
1987 *ImpUses
; ++ImpUses
)
1988 if (!MI
->readsRegister(*ImpUses
))
1989 MI
->addOperand(*MI
->getParent()->getParent(),
1990 MachineOperand::CreateReg(*ImpUses
, false, true));
1992 assert(MI
->definesRegister(PPC::CR0
) &&
1993 "Record-form instruction does not define cr0?");
1995 // Modify the condition code of operands in OperandsToUpdate.
1996 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1997 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
1998 for (unsigned i
= 0, e
= PredsToUpdate
.size(); i
< e
; i
++)
1999 PredsToUpdate
[i
].first
->setImm(PredsToUpdate
[i
].second
);
2001 for (unsigned i
= 0, e
= SubRegsToUpdate
.size(); i
< e
; i
++)
2002 SubRegsToUpdate
[i
].first
->setSubReg(SubRegsToUpdate
[i
].second
);
2007 /// GetInstSize - Return the number of bytes of code the specified
2008 /// instruction may be. This returns the maximum number of bytes.
2010 unsigned PPCInstrInfo::getInstSizeInBytes(const MachineInstr
&MI
) const {
2011 unsigned Opcode
= MI
.getOpcode();
2013 if (Opcode
== PPC::INLINEASM
|| Opcode
== PPC::INLINEASM_BR
) {
2014 const MachineFunction
*MF
= MI
.getParent()->getParent();
2015 const char *AsmStr
= MI
.getOperand(0).getSymbolName();
2016 return getInlineAsmLength(AsmStr
, *MF
->getTarget().getMCAsmInfo());
2017 } else if (Opcode
== TargetOpcode::STACKMAP
) {
2018 StackMapOpers
Opers(&MI
);
2019 return Opers
.getNumPatchBytes();
2020 } else if (Opcode
== TargetOpcode::PATCHPOINT
) {
2021 PatchPointOpers
Opers(&MI
);
2022 return Opers
.getNumPatchBytes();
2024 return get(Opcode
).getSize();
2028 std::pair
<unsigned, unsigned>
2029 PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF
) const {
2030 const unsigned Mask
= PPCII::MO_ACCESS_MASK
;
2031 return std::make_pair(TF
& Mask
, TF
& ~Mask
);
2034 ArrayRef
<std::pair
<unsigned, const char *>>
2035 PPCInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
2036 using namespace PPCII
;
2037 static const std::pair
<unsigned, const char *> TargetFlags
[] = {
2040 {MO_TPREL_LO
, "ppc-tprel-lo"},
2041 {MO_TPREL_HA
, "ppc-tprel-ha"},
2042 {MO_DTPREL_LO
, "ppc-dtprel-lo"},
2043 {MO_TLSLD_LO
, "ppc-tlsld-lo"},
2044 {MO_TOC_LO
, "ppc-toc-lo"},
2045 {MO_TLS
, "ppc-tls"}};
2046 return makeArrayRef(TargetFlags
);
2049 ArrayRef
<std::pair
<unsigned, const char *>>
2050 PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
2051 using namespace PPCII
;
2052 static const std::pair
<unsigned, const char *> TargetFlags
[] = {
2053 {MO_PLT
, "ppc-plt"},
2054 {MO_PIC_FLAG
, "ppc-pic"},
2055 {MO_NLP_FLAG
, "ppc-nlp"},
2056 {MO_NLP_HIDDEN_FLAG
, "ppc-nlp-hidden"}};
2057 return makeArrayRef(TargetFlags
);
2060 // Expand VSX Memory Pseudo instruction to either a VSX or a FP instruction.
2061 // The VSX versions have the advantage of a full 64-register target whereas
2062 // the FP ones have the advantage of lower latency and higher throughput. So
2063 // what we are after is using the faster instructions in low register pressure
2064 // situations and using the larger register file in high register pressure
2066 bool PPCInstrInfo::expandVSXMemPseudo(MachineInstr
&MI
) const {
2067 unsigned UpperOpcode
, LowerOpcode
;
2068 switch (MI
.getOpcode()) {
2069 case PPC::DFLOADf32
:
2070 UpperOpcode
= PPC::LXSSP
;
2071 LowerOpcode
= PPC::LFS
;
2073 case PPC::DFLOADf64
:
2074 UpperOpcode
= PPC::LXSD
;
2075 LowerOpcode
= PPC::LFD
;
2077 case PPC::DFSTOREf32
:
2078 UpperOpcode
= PPC::STXSSP
;
2079 LowerOpcode
= PPC::STFS
;
2081 case PPC::DFSTOREf64
:
2082 UpperOpcode
= PPC::STXSD
;
2083 LowerOpcode
= PPC::STFD
;
2085 case PPC::XFLOADf32
:
2086 UpperOpcode
= PPC::LXSSPX
;
2087 LowerOpcode
= PPC::LFSX
;
2089 case PPC::XFLOADf64
:
2090 UpperOpcode
= PPC::LXSDX
;
2091 LowerOpcode
= PPC::LFDX
;
2093 case PPC::XFSTOREf32
:
2094 UpperOpcode
= PPC::STXSSPX
;
2095 LowerOpcode
= PPC::STFSX
;
2097 case PPC::XFSTOREf64
:
2098 UpperOpcode
= PPC::STXSDX
;
2099 LowerOpcode
= PPC::STFDX
;
2102 UpperOpcode
= PPC::LXSIWAX
;
2103 LowerOpcode
= PPC::LFIWAX
;
2106 UpperOpcode
= PPC::LXSIWZX
;
2107 LowerOpcode
= PPC::LFIWZX
;
2110 UpperOpcode
= PPC::STXSIWX
;
2111 LowerOpcode
= PPC::STFIWX
;
2114 llvm_unreachable("Unknown Operation!");
2117 Register TargetReg
= MI
.getOperand(0).getReg();
2119 if ((TargetReg
>= PPC::F0
&& TargetReg
<= PPC::F31
) ||
2120 (TargetReg
>= PPC::VSL0
&& TargetReg
<= PPC::VSL31
))
2121 Opcode
= LowerOpcode
;
2123 Opcode
= UpperOpcode
;
2124 MI
.setDesc(get(Opcode
));
2128 static bool isAnImmediateOperand(const MachineOperand
&MO
) {
2129 return MO
.isCPI() || MO
.isGlobal() || MO
.isImm();
2132 bool PPCInstrInfo::expandPostRAPseudo(MachineInstr
&MI
) const {
2133 auto &MBB
= *MI
.getParent();
2134 auto DL
= MI
.getDebugLoc();
2136 switch (MI
.getOpcode()) {
2137 case TargetOpcode::LOAD_STACK_GUARD
: {
2138 assert(Subtarget
.isTargetLinux() &&
2139 "Only Linux target is expected to contain LOAD_STACK_GUARD");
2140 const int64_t Offset
= Subtarget
.isPPC64() ? -0x7010 : -0x7008;
2141 const unsigned Reg
= Subtarget
.isPPC64() ? PPC::X13
: PPC::R2
;
2142 MI
.setDesc(get(Subtarget
.isPPC64() ? PPC::LD
: PPC::LWZ
));
2143 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
2148 case PPC::DFLOADf32
:
2149 case PPC::DFLOADf64
:
2150 case PPC::DFSTOREf32
:
2151 case PPC::DFSTOREf64
: {
2152 assert(Subtarget
.hasP9Vector() &&
2153 "Invalid D-Form Pseudo-ops on Pre-P9 target.");
2154 assert(MI
.getOperand(2).isReg() &&
2155 isAnImmediateOperand(MI
.getOperand(1)) &&
2156 "D-form op must have register and immediate operands");
2157 return expandVSXMemPseudo(MI
);
2159 case PPC::XFLOADf32
:
2160 case PPC::XFSTOREf32
:
2164 assert(Subtarget
.hasP8Vector() &&
2165 "Invalid X-Form Pseudo-ops on Pre-P8 target.");
2166 assert(MI
.getOperand(2).isReg() && MI
.getOperand(1).isReg() &&
2167 "X-form op must have register and register operands");
2168 return expandVSXMemPseudo(MI
);
2170 case PPC::XFLOADf64
:
2171 case PPC::XFSTOREf64
: {
2172 assert(Subtarget
.hasVSX() &&
2173 "Invalid X-Form Pseudo-ops on target that has no VSX.");
2174 assert(MI
.getOperand(2).isReg() && MI
.getOperand(1).isReg() &&
2175 "X-form op must have register and register operands");
2176 return expandVSXMemPseudo(MI
);
2178 case PPC::SPILLTOVSR_LD
: {
2179 Register TargetReg
= MI
.getOperand(0).getReg();
2180 if (PPC::VSFRCRegClass
.contains(TargetReg
)) {
2181 MI
.setDesc(get(PPC::DFLOADf64
));
2182 return expandPostRAPseudo(MI
);
2185 MI
.setDesc(get(PPC::LD
));
2188 case PPC::SPILLTOVSR_ST
: {
2189 Register SrcReg
= MI
.getOperand(0).getReg();
2190 if (PPC::VSFRCRegClass
.contains(SrcReg
)) {
2191 NumStoreSPILLVSRRCAsVec
++;
2192 MI
.setDesc(get(PPC::DFSTOREf64
));
2193 return expandPostRAPseudo(MI
);
2195 NumStoreSPILLVSRRCAsGpr
++;
2196 MI
.setDesc(get(PPC::STD
));
2200 case PPC::SPILLTOVSR_LDX
: {
2201 Register TargetReg
= MI
.getOperand(0).getReg();
2202 if (PPC::VSFRCRegClass
.contains(TargetReg
))
2203 MI
.setDesc(get(PPC::LXSDX
));
2205 MI
.setDesc(get(PPC::LDX
));
2208 case PPC::SPILLTOVSR_STX
: {
2209 Register SrcReg
= MI
.getOperand(0).getReg();
2210 if (PPC::VSFRCRegClass
.contains(SrcReg
)) {
2211 NumStoreSPILLVSRRCAsVec
++;
2212 MI
.setDesc(get(PPC::STXSDX
));
2214 NumStoreSPILLVSRRCAsGpr
++;
2215 MI
.setDesc(get(PPC::STDX
));
2220 case PPC::CFENCE8
: {
2221 auto Val
= MI
.getOperand(0).getReg();
2222 BuildMI(MBB
, MI
, DL
, get(PPC::CMPD
), PPC::CR7
).addReg(Val
).addReg(Val
);
2223 BuildMI(MBB
, MI
, DL
, get(PPC::CTRL_DEP
))
2224 .addImm(PPC::PRED_NE_MINUS
)
2227 MI
.setDesc(get(PPC::ISYNC
));
2228 MI
.RemoveOperand(0);
2235 // Essentially a compile-time implementation of a compare->isel sequence.
2236 // It takes two constants to compare, along with the true/false registers
2237 // and the comparison type (as a subreg to a CR field) and returns one
2238 // of the true/false registers, depending on the comparison results.
2239 static unsigned selectReg(int64_t Imm1
, int64_t Imm2
, unsigned CompareOpc
,
2240 unsigned TrueReg
, unsigned FalseReg
,
2241 unsigned CRSubReg
) {
2242 // Signed comparisons. The immediates are assumed to be sign-extended.
2243 if (CompareOpc
== PPC::CMPWI
|| CompareOpc
== PPC::CMPDI
) {
2245 default: llvm_unreachable("Unknown integer comparison type.");
2247 return Imm1
< Imm2
? TrueReg
: FalseReg
;
2249 return Imm1
> Imm2
? TrueReg
: FalseReg
;
2251 return Imm1
== Imm2
? TrueReg
: FalseReg
;
2254 // Unsigned comparisons.
2255 else if (CompareOpc
== PPC::CMPLWI
|| CompareOpc
== PPC::CMPLDI
) {
2257 default: llvm_unreachable("Unknown integer comparison type.");
2259 return (uint64_t)Imm1
< (uint64_t)Imm2
? TrueReg
: FalseReg
;
2261 return (uint64_t)Imm1
> (uint64_t)Imm2
? TrueReg
: FalseReg
;
2263 return Imm1
== Imm2
? TrueReg
: FalseReg
;
2266 return PPC::NoRegister
;
2269 void PPCInstrInfo::replaceInstrOperandWithImm(MachineInstr
&MI
,
2271 int64_t Imm
) const {
2272 assert(MI
.getOperand(OpNo
).isReg() && "Operand must be a REG");
2273 // Replace the REG with the Immediate.
2274 Register InUseReg
= MI
.getOperand(OpNo
).getReg();
2275 MI
.getOperand(OpNo
).ChangeToImmediate(Imm
);
2277 if (MI
.implicit_operands().empty())
2280 // We need to make sure that the MI didn't have any implicit use
2281 // of this REG any more.
2282 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
2283 int UseOpIdx
= MI
.findRegisterUseOperandIdx(InUseReg
, false, TRI
);
2284 if (UseOpIdx
>= 0) {
2285 MachineOperand
&MO
= MI
.getOperand(UseOpIdx
);
2286 if (MO
.isImplicit())
2287 // The operands must always be in the following order:
2288 // - explicit reg defs,
2289 // - other explicit operands (reg uses, immediates, etc.),
2290 // - implicit reg defs
2291 // - implicit reg uses
2292 // Therefore, removing the implicit operand won't change the explicit
2294 MI
.RemoveOperand(UseOpIdx
);
2298 // Replace an instruction with one that materializes a constant (and sets
2299 // CR0 if the original instruction was a record-form instruction).
2300 void PPCInstrInfo::replaceInstrWithLI(MachineInstr
&MI
,
2301 const LoadImmediateInfo
&LII
) const {
2302 // Remove existing operands.
2303 int OperandToKeep
= LII
.SetCR
? 1 : 0;
2304 for (int i
= MI
.getNumOperands() - 1; i
> OperandToKeep
; i
--)
2305 MI
.RemoveOperand(i
);
2307 // Replace the instruction.
2309 MI
.setDesc(get(LII
.Is64Bit
? PPC::ANDIo8
: PPC::ANDIo
));
2310 // Set the immediate.
2311 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
2312 .addImm(LII
.Imm
).addReg(PPC::CR0
, RegState::ImplicitDefine
);
2316 MI
.setDesc(get(LII
.Is64Bit
? PPC::LI8
: PPC::LI
));
2318 // Set the immediate.
2319 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
)
2323 MachineInstr
*PPCInstrInfo::getDefMIPostRA(unsigned Reg
, MachineInstr
&MI
,
2324 bool &SeenIntermediateUse
) const {
2325 assert(!MI
.getParent()->getParent()->getRegInfo().isSSA() &&
2326 "Should be called after register allocation.");
2327 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
2328 MachineBasicBlock::reverse_iterator E
= MI
.getParent()->rend(), It
= MI
;
2330 SeenIntermediateUse
= false;
2331 for (; It
!= E
; ++It
) {
2332 if (It
->modifiesRegister(Reg
, TRI
))
2334 if (It
->readsRegister(Reg
, TRI
))
2335 SeenIntermediateUse
= true;
2340 MachineInstr
*PPCInstrInfo::getForwardingDefMI(
2342 unsigned &OpNoForForwarding
,
2343 bool &SeenIntermediateUse
) const {
2344 OpNoForForwarding
= ~0U;
2345 MachineInstr
*DefMI
= nullptr;
2346 MachineRegisterInfo
*MRI
= &MI
.getParent()->getParent()->getRegInfo();
2347 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
2348 // If we're in SSA, get the defs through the MRI. Otherwise, only look
2349 // within the basic block to see if the register is defined using an LI/LI8.
2351 for (int i
= 1, e
= MI
.getNumOperands(); i
< e
; i
++) {
2352 if (!MI
.getOperand(i
).isReg())
2354 Register Reg
= MI
.getOperand(i
).getReg();
2355 if (!Register::isVirtualRegister(Reg
))
2357 unsigned TrueReg
= TRI
->lookThruCopyLike(Reg
, MRI
);
2358 if (Register::isVirtualRegister(TrueReg
)) {
2359 DefMI
= MRI
->getVRegDef(TrueReg
);
2360 if (DefMI
->getOpcode() == PPC::LI
|| DefMI
->getOpcode() == PPC::LI8
) {
2361 OpNoForForwarding
= i
;
2367 // Looking back through the definition for each operand could be expensive,
2368 // so exit early if this isn't an instruction that either has an immediate
2369 // form or is already an immediate form that we can handle.
2371 unsigned Opc
= MI
.getOpcode();
2372 bool ConvertibleImmForm
=
2373 Opc
== PPC::CMPWI
|| Opc
== PPC::CMPLWI
||
2374 Opc
== PPC::CMPDI
|| Opc
== PPC::CMPLDI
||
2375 Opc
== PPC::ADDI
|| Opc
== PPC::ADDI8
||
2376 Opc
== PPC::ORI
|| Opc
== PPC::ORI8
||
2377 Opc
== PPC::XORI
|| Opc
== PPC::XORI8
||
2378 Opc
== PPC::RLDICL
|| Opc
== PPC::RLDICLo
||
2379 Opc
== PPC::RLDICL_32
|| Opc
== PPC::RLDICL_32_64
||
2380 Opc
== PPC::RLWINM
|| Opc
== PPC::RLWINMo
||
2381 Opc
== PPC::RLWINM8
|| Opc
== PPC::RLWINM8o
;
2382 bool IsVFReg
= (MI
.getNumOperands() && MI
.getOperand(0).isReg())
2383 ? isVFRegister(MI
.getOperand(0).getReg())
2385 if (!ConvertibleImmForm
&& !instrHasImmForm(Opc
, IsVFReg
, III
, true))
2388 // Don't convert or %X, %Y, %Y since that's just a register move.
2389 if ((Opc
== PPC::OR
|| Opc
== PPC::OR8
) &&
2390 MI
.getOperand(1).getReg() == MI
.getOperand(2).getReg())
2392 for (int i
= 1, e
= MI
.getNumOperands(); i
< e
; i
++) {
2393 MachineOperand
&MO
= MI
.getOperand(i
);
2394 SeenIntermediateUse
= false;
2395 if (MO
.isReg() && MO
.isUse() && !MO
.isImplicit()) {
2396 Register Reg
= MI
.getOperand(i
).getReg();
2397 // If we see another use of this reg between the def and the MI,
2398 // we want to flat it so the def isn't deleted.
2399 MachineInstr
*DefMI
= getDefMIPostRA(Reg
, MI
, SeenIntermediateUse
);
2401 // Is this register defined by some form of add-immediate (including
2402 // load-immediate) within this basic block?
2403 switch (DefMI
->getOpcode()) {
2411 OpNoForForwarding
= i
;
2418 return OpNoForForwarding
== ~0U ? nullptr : DefMI
;
2421 const unsigned *PPCInstrInfo::getStoreOpcodesForSpillArray() const {
2422 static const unsigned OpcodesForSpill
[2][SOK_LastOpcodeSpill
] = {
2424 {PPC::STW
, PPC::STD
, PPC::STFD
, PPC::STFS
, PPC::SPILL_CR
,
2425 PPC::SPILL_CRBIT
, PPC::STVX
, PPC::STXVD2X
, PPC::STXSDX
, PPC::STXSSPX
,
2426 PPC::SPILL_VRSAVE
, PPC::QVSTFDX
, PPC::QVSTFSXs
, PPC::QVSTFDXb
,
2427 PPC::SPILLTOVSR_ST
, PPC::EVSTDD
},
2429 {PPC::STW
, PPC::STD
, PPC::STFD
, PPC::STFS
, PPC::SPILL_CR
,
2430 PPC::SPILL_CRBIT
, PPC::STVX
, PPC::STXV
, PPC::DFSTOREf64
, PPC::DFSTOREf32
,
2431 PPC::SPILL_VRSAVE
, PPC::QVSTFDX
, PPC::QVSTFSXs
, PPC::QVSTFDXb
,
2432 PPC::SPILLTOVSR_ST
}};
2434 return OpcodesForSpill
[(Subtarget
.hasP9Vector()) ? 1 : 0];
2437 const unsigned *PPCInstrInfo::getLoadOpcodesForSpillArray() const {
2438 static const unsigned OpcodesForSpill
[2][SOK_LastOpcodeSpill
] = {
2440 {PPC::LWZ
, PPC::LD
, PPC::LFD
, PPC::LFS
, PPC::RESTORE_CR
,
2441 PPC::RESTORE_CRBIT
, PPC::LVX
, PPC::LXVD2X
, PPC::LXSDX
, PPC::LXSSPX
,
2442 PPC::RESTORE_VRSAVE
, PPC::QVLFDX
, PPC::QVLFSXs
, PPC::QVLFDXb
,
2443 PPC::SPILLTOVSR_LD
, PPC::EVLDD
},
2445 {PPC::LWZ
, PPC::LD
, PPC::LFD
, PPC::LFS
, PPC::RESTORE_CR
,
2446 PPC::RESTORE_CRBIT
, PPC::LVX
, PPC::LXV
, PPC::DFLOADf64
, PPC::DFLOADf32
,
2447 PPC::RESTORE_VRSAVE
, PPC::QVLFDX
, PPC::QVLFSXs
, PPC::QVLFDXb
,
2448 PPC::SPILLTOVSR_LD
}};
2450 return OpcodesForSpill
[(Subtarget
.hasP9Vector()) ? 1 : 0];
2453 void PPCInstrInfo::fixupIsDeadOrKill(MachineInstr
&StartMI
, MachineInstr
&EndMI
,
2454 unsigned RegNo
) const {
2455 const MachineRegisterInfo
&MRI
=
2456 StartMI
.getParent()->getParent()->getRegInfo();
2460 // Instructions between [StartMI, EndMI] should be in same basic block.
2461 assert((StartMI
.getParent() == EndMI
.getParent()) &&
2462 "Instructions are not in same basic block");
2464 bool IsKillSet
= false;
2466 auto clearOperandKillInfo
= [=] (MachineInstr
&MI
, unsigned Index
) {
2467 MachineOperand
&MO
= MI
.getOperand(Index
);
2468 if (MO
.isReg() && MO
.isUse() && MO
.isKill() &&
2469 getRegisterInfo().regsOverlap(MO
.getReg(), RegNo
))
2470 MO
.setIsKill(false);
2473 // Set killed flag for EndMI.
2474 // No need to do anything if EndMI defines RegNo.
2476 EndMI
.findRegisterUseOperandIdx(RegNo
, false, &getRegisterInfo());
2477 if (UseIndex
!= -1) {
2478 EndMI
.getOperand(UseIndex
).setIsKill(true);
2480 // Clear killed flag for other EndMI operands related to RegNo. In some
2481 // upexpected cases, killed may be set multiple times for same register
2482 // operand in same MI.
2483 for (int i
= 0, e
= EndMI
.getNumOperands(); i
!= e
; ++i
)
2485 clearOperandKillInfo(EndMI
, i
);
2488 // Walking the inst in reverse order (EndMI -> StartMI].
2489 MachineBasicBlock::reverse_iterator It
= EndMI
;
2490 MachineBasicBlock::reverse_iterator E
= EndMI
.getParent()->rend();
2491 // EndMI has been handled above, skip it here.
2493 MachineOperand
*MO
= nullptr;
2494 for (; It
!= E
; ++It
) {
2495 // Skip insturctions which could not be a def/use of RegNo.
2496 if (It
->isDebugInstr() || It
->isPosition())
2499 // Clear killed flag for all It operands related to RegNo. In some
2500 // upexpected cases, killed may be set multiple times for same register
2501 // operand in same MI.
2502 for (int i
= 0, e
= It
->getNumOperands(); i
!= e
; ++i
)
2503 clearOperandKillInfo(*It
, i
);
2505 // If killed is not set, set killed for its last use or set dead for its def
2508 if ((MO
= It
->findRegisterUseOperand(RegNo
, false, &getRegisterInfo()))) {
2509 // Use found, set it killed.
2511 MO
->setIsKill(true);
2513 } else if ((MO
= It
->findRegisterDefOperand(RegNo
, false, true,
2514 &getRegisterInfo()))) {
2515 // No use found, set dead for its def.
2516 assert(&*It
== &StartMI
&& "No new def between StartMI and EndMI.");
2517 MO
->setIsDead(true);
2522 if ((&*It
) == &StartMI
)
2525 // Ensure RegMo liveness is killed after EndMI.
2526 assert((IsKillSet
|| (MO
&& MO
->isDead())) &&
2527 "RegNo should be killed or dead");
2530 // If this instruction has an immediate form and one of its operands is a
2531 // result of a load-immediate or an add-immediate, convert it to
2532 // the immediate form if the constant is in range.
2533 bool PPCInstrInfo::convertToImmediateForm(MachineInstr
&MI
,
2534 MachineInstr
**KilledDef
) const {
2535 MachineFunction
*MF
= MI
.getParent()->getParent();
2536 MachineRegisterInfo
*MRI
= &MF
->getRegInfo();
2537 bool PostRA
= !MRI
->isSSA();
2538 bool SeenIntermediateUse
= true;
2539 unsigned ForwardingOperand
= ~0U;
2540 MachineInstr
*DefMI
= getForwardingDefMI(MI
, ForwardingOperand
,
2541 SeenIntermediateUse
);
2544 assert(ForwardingOperand
< MI
.getNumOperands() &&
2545 "The forwarding operand needs to be valid at this point");
2546 bool IsForwardingOperandKilled
= MI
.getOperand(ForwardingOperand
).isKill();
2547 bool KillFwdDefMI
= !SeenIntermediateUse
&& IsForwardingOperandKilled
;
2548 Register ForwardingOperandReg
= MI
.getOperand(ForwardingOperand
).getReg();
2549 if (KilledDef
&& KillFwdDefMI
)
2553 bool IsVFReg
= MI
.getOperand(0).isReg()
2554 ? isVFRegister(MI
.getOperand(0).getReg())
2556 bool HasImmForm
= instrHasImmForm(MI
.getOpcode(), IsVFReg
, III
, PostRA
);
2557 // If this is a reg+reg instruction that has a reg+imm form,
2558 // and one of the operands is produced by an add-immediate,
2559 // try to convert it.
2561 transformToImmFormFedByAdd(MI
, III
, ForwardingOperand
, *DefMI
,
2565 if ((DefMI
->getOpcode() != PPC::LI
&& DefMI
->getOpcode() != PPC::LI8
) ||
2566 !DefMI
->getOperand(1).isImm())
2569 int64_t Immediate
= DefMI
->getOperand(1).getImm();
2570 // Sign-extend to 64-bits.
2571 int64_t SExtImm
= ((uint64_t)Immediate
& ~0x7FFFuLL
) != 0 ?
2572 (Immediate
| 0xFFFFFFFFFFFF0000) : Immediate
;
2574 // If this is a reg+reg instruction that has a reg+imm form,
2575 // and one of the operands is produced by LI, convert it now.
2577 return transformToImmFormFedByLI(MI
, III
, ForwardingOperand
, *DefMI
, SExtImm
);
2579 bool ReplaceWithLI
= false;
2580 bool Is64BitLI
= false;
2583 unsigned Opc
= MI
.getOpcode();
2585 default: return false;
2587 // FIXME: Any branches conditional on such a comparison can be made
2588 // unconditional. At this time, this happens too infrequently to be worth
2589 // the implementation effort, but if that ever changes, we could convert
2590 // such a pattern here.
2595 // Doing this post-RA would require dataflow analysis to reliably find uses
2596 // of the CR register set by the compare.
2597 // No need to fixup killed/dead flag since this transformation is only valid
2601 // If a compare-immediate is fed by an immediate and is itself an input of
2602 // an ISEL (the most common case) into a COPY of the correct register.
2603 bool Changed
= false;
2604 Register DefReg
= MI
.getOperand(0).getReg();
2605 int64_t Comparand
= MI
.getOperand(2).getImm();
2606 int64_t SExtComparand
= ((uint64_t)Comparand
& ~0x7FFFuLL
) != 0 ?
2607 (Comparand
| 0xFFFFFFFFFFFF0000) : Comparand
;
2609 for (auto &CompareUseMI
: MRI
->use_instructions(DefReg
)) {
2610 unsigned UseOpc
= CompareUseMI
.getOpcode();
2611 if (UseOpc
!= PPC::ISEL
&& UseOpc
!= PPC::ISEL8
)
2613 unsigned CRSubReg
= CompareUseMI
.getOperand(3).getSubReg();
2614 Register TrueReg
= CompareUseMI
.getOperand(1).getReg();
2615 Register FalseReg
= CompareUseMI
.getOperand(2).getReg();
2616 unsigned RegToCopy
= selectReg(SExtImm
, SExtComparand
, Opc
, TrueReg
,
2617 FalseReg
, CRSubReg
);
2618 if (RegToCopy
== PPC::NoRegister
)
2620 // Can't use PPC::COPY to copy PPC::ZERO[8]. Convert it to LI[8] 0.
2621 if (RegToCopy
== PPC::ZERO
|| RegToCopy
== PPC::ZERO8
) {
2622 CompareUseMI
.setDesc(get(UseOpc
== PPC::ISEL8
? PPC::LI8
: PPC::LI
));
2623 replaceInstrOperandWithImm(CompareUseMI
, 1, 0);
2624 CompareUseMI
.RemoveOperand(3);
2625 CompareUseMI
.RemoveOperand(2);
2629 dbgs() << "Found LI -> CMPI -> ISEL, replacing with a copy.\n");
2630 LLVM_DEBUG(DefMI
->dump(); MI
.dump(); CompareUseMI
.dump());
2631 LLVM_DEBUG(dbgs() << "Is converted to:\n");
2632 // Convert to copy and remove unneeded operands.
2633 CompareUseMI
.setDesc(get(PPC::COPY
));
2634 CompareUseMI
.RemoveOperand(3);
2635 CompareUseMI
.RemoveOperand(RegToCopy
== TrueReg
? 2 : 1);
2636 CmpIselsConverted
++;
2638 LLVM_DEBUG(CompareUseMI
.dump());
2642 // This may end up incremented multiple times since this function is called
2643 // during a fixed-point transformation, but it is only meant to indicate the
2644 // presence of this opportunity.
2645 MissedConvertibleImmediateInstrs
++;
2649 // Immediate forms - may simply be convertable to an LI.
2652 // Does the sum fit in a 16-bit signed field?
2653 int64_t Addend
= MI
.getOperand(2).getImm();
2654 if (isInt
<16>(Addend
+ SExtImm
)) {
2655 ReplaceWithLI
= true;
2656 Is64BitLI
= Opc
== PPC::ADDI8
;
2657 NewImm
= Addend
+ SExtImm
;
2664 case PPC::RLDICL_32
:
2665 case PPC::RLDICL_32_64
: {
2666 // Use APInt's rotate function.
2667 int64_t SH
= MI
.getOperand(2).getImm();
2668 int64_t MB
= MI
.getOperand(3).getImm();
2669 APInt
InVal((Opc
== PPC::RLDICL
|| Opc
== PPC::RLDICLo
) ?
2670 64 : 32, SExtImm
, true);
2671 InVal
= InVal
.rotl(SH
);
2672 uint64_t Mask
= (1LLU << (63 - MB
+ 1)) - 1;
2674 // Can't replace negative values with an LI as that will sign-extend
2675 // and not clear the left bits. If we're setting the CR bit, we will use
2676 // ANDIo which won't sign extend, so that's safe.
2677 if (isUInt
<15>(InVal
.getSExtValue()) ||
2678 (Opc
== PPC::RLDICLo
&& isUInt
<16>(InVal
.getSExtValue()))) {
2679 ReplaceWithLI
= true;
2680 Is64BitLI
= Opc
!= PPC::RLDICL_32
;
2681 NewImm
= InVal
.getSExtValue();
2682 SetCR
= Opc
== PPC::RLDICLo
;
2690 case PPC::RLWINM8o
: {
2691 int64_t SH
= MI
.getOperand(2).getImm();
2692 int64_t MB
= MI
.getOperand(3).getImm();
2693 int64_t ME
= MI
.getOperand(4).getImm();
2694 APInt
InVal(32, SExtImm
, true);
2695 InVal
= InVal
.rotl(SH
);
2696 // Set the bits ( MB + 32 ) to ( ME + 32 ).
2697 uint64_t Mask
= ((1LLU << (32 - MB
)) - 1) & ~((1LLU << (31 - ME
)) - 1);
2699 // Can't replace negative values with an LI as that will sign-extend
2700 // and not clear the left bits. If we're setting the CR bit, we will use
2701 // ANDIo which won't sign extend, so that's safe.
2702 bool ValueFits
= isUInt
<15>(InVal
.getSExtValue());
2703 ValueFits
|= ((Opc
== PPC::RLWINMo
|| Opc
== PPC::RLWINM8o
) &&
2704 isUInt
<16>(InVal
.getSExtValue()));
2706 ReplaceWithLI
= true;
2707 Is64BitLI
= Opc
== PPC::RLWINM8
|| Opc
== PPC::RLWINM8o
;
2708 NewImm
= InVal
.getSExtValue();
2709 SetCR
= Opc
== PPC::RLWINMo
|| Opc
== PPC::RLWINM8o
;
2718 int64_t LogicalImm
= MI
.getOperand(2).getImm();
2720 if (Opc
== PPC::ORI
|| Opc
== PPC::ORI8
)
2721 Result
= LogicalImm
| SExtImm
;
2723 Result
= LogicalImm
^ SExtImm
;
2724 if (isInt
<16>(Result
)) {
2725 ReplaceWithLI
= true;
2726 Is64BitLI
= Opc
== PPC::ORI8
|| Opc
== PPC::XORI8
;
2734 if (ReplaceWithLI
) {
2735 // We need to be careful with CR-setting instructions we're replacing.
2737 // We don't know anything about uses when we're out of SSA, so only
2738 // replace if the new immediate will be reproduced.
2739 bool ImmChanged
= (SExtImm
& NewImm
) != NewImm
;
2740 if (PostRA
&& ImmChanged
)
2744 // If the defining load-immediate has no other uses, we can just replace
2745 // the immediate with the new immediate.
2746 if (MRI
->hasOneUse(DefMI
->getOperand(0).getReg()))
2747 DefMI
->getOperand(1).setImm(NewImm
);
2749 // If we're not using the GPR result of the CR-setting instruction, we
2750 // just need to and with zero/non-zero depending on the new immediate.
2751 else if (MRI
->use_empty(MI
.getOperand(0).getReg())) {
2753 assert(Immediate
&& "Transformation converted zero to non-zero?");
2757 else if (ImmChanged
)
2762 LLVM_DEBUG(dbgs() << "Replacing instruction:\n");
2763 LLVM_DEBUG(MI
.dump());
2764 LLVM_DEBUG(dbgs() << "Fed by:\n");
2765 LLVM_DEBUG(DefMI
->dump());
2766 LoadImmediateInfo LII
;
2768 LII
.Is64Bit
= Is64BitLI
;
2770 // If we're setting the CR, the original load-immediate must be kept (as an
2771 // operand to ANDIo/ANDI8o).
2772 if (KilledDef
&& SetCR
)
2773 *KilledDef
= nullptr;
2774 replaceInstrWithLI(MI
, LII
);
2776 // Fixup killed/dead flag after transformation.
2778 // ForwardingOperandReg = LI imm1
2779 // y = op2 imm2, ForwardingOperandReg(killed)
2780 if (IsForwardingOperandKilled
)
2781 fixupIsDeadOrKill(*DefMI
, MI
, ForwardingOperandReg
);
2783 LLVM_DEBUG(dbgs() << "With:\n");
2784 LLVM_DEBUG(MI
.dump());
2790 bool PPCInstrInfo::instrHasImmForm(unsigned Opc
, bool IsVFReg
,
2791 ImmInstrInfo
&III
, bool PostRA
) const {
2792 // The vast majority of the instructions would need their operand 2 replaced
2793 // with an immediate when switching to the reg+imm form. A marked exception
2794 // are the update form loads/stores for which a constant operand 2 would need
2795 // to turn into a displacement and move operand 1 to the operand 2 position.
2797 III
.OpNoForForwarding
= 2;
2799 III
.ImmMustBeMultipleOf
= 1;
2800 III
.TruncateImmTo
= 0;
2801 III
.IsSummingOperands
= false;
2803 default: return false;
2806 III
.SignedImm
= true;
2807 III
.ZeroIsSpecialOrig
= 0;
2808 III
.ZeroIsSpecialNew
= 1;
2809 III
.IsCommutative
= true;
2810 III
.IsSummingOperands
= true;
2811 III
.ImmOpcode
= Opc
== PPC::ADD4
? PPC::ADDI
: PPC::ADDI8
;
2815 III
.SignedImm
= true;
2816 III
.ZeroIsSpecialOrig
= 0;
2817 III
.ZeroIsSpecialNew
= 0;
2818 III
.IsCommutative
= true;
2819 III
.IsSummingOperands
= true;
2820 III
.ImmOpcode
= Opc
== PPC::ADDC
? PPC::ADDIC
: PPC::ADDIC8
;
2823 III
.SignedImm
= true;
2824 III
.ZeroIsSpecialOrig
= 0;
2825 III
.ZeroIsSpecialNew
= 0;
2826 III
.IsCommutative
= true;
2827 III
.IsSummingOperands
= true;
2828 III
.ImmOpcode
= PPC::ADDICo
;
2832 III
.SignedImm
= true;
2833 III
.ZeroIsSpecialOrig
= 0;
2834 III
.ZeroIsSpecialNew
= 0;
2835 III
.IsCommutative
= false;
2836 III
.ImmOpcode
= Opc
== PPC::SUBFC
? PPC::SUBFIC
: PPC::SUBFIC8
;
2840 III
.SignedImm
= true;
2841 III
.ZeroIsSpecialOrig
= 0;
2842 III
.ZeroIsSpecialNew
= 0;
2843 III
.IsCommutative
= false;
2844 III
.ImmOpcode
= Opc
== PPC::CMPW
? PPC::CMPWI
: PPC::CMPDI
;
2848 III
.SignedImm
= false;
2849 III
.ZeroIsSpecialOrig
= 0;
2850 III
.ZeroIsSpecialNew
= 0;
2851 III
.IsCommutative
= false;
2852 III
.ImmOpcode
= Opc
== PPC::CMPLW
? PPC::CMPLWI
: PPC::CMPLDI
;
2860 III
.SignedImm
= false;
2861 III
.ZeroIsSpecialOrig
= 0;
2862 III
.ZeroIsSpecialNew
= 0;
2863 III
.IsCommutative
= true;
2865 default: llvm_unreachable("Unknown opcode");
2866 case PPC::ANDo
: III
.ImmOpcode
= PPC::ANDIo
; break;
2867 case PPC::AND8o
: III
.ImmOpcode
= PPC::ANDIo8
; break;
2868 case PPC::OR
: III
.ImmOpcode
= PPC::ORI
; break;
2869 case PPC::OR8
: III
.ImmOpcode
= PPC::ORI8
; break;
2870 case PPC::XOR
: III
.ImmOpcode
= PPC::XORI
; break;
2871 case PPC::XOR8
: III
.ImmOpcode
= PPC::XORI8
; break;
2888 III
.SignedImm
= false;
2889 III
.ZeroIsSpecialOrig
= 0;
2890 III
.ZeroIsSpecialNew
= 0;
2891 III
.IsCommutative
= false;
2892 // This isn't actually true, but the instructions ignore any of the
2893 // upper bits, so any immediate loaded with an LI is acceptable.
2894 // This does not apply to shift right algebraic because a value
2895 // out of range will produce a -1/0.
2897 if (Opc
== PPC::RLWNM
|| Opc
== PPC::RLWNM8
||
2898 Opc
== PPC::RLWNMo
|| Opc
== PPC::RLWNM8o
)
2899 III
.TruncateImmTo
= 5;
2901 III
.TruncateImmTo
= 6;
2903 default: llvm_unreachable("Unknown opcode");
2904 case PPC::RLWNM
: III
.ImmOpcode
= PPC::RLWINM
; break;
2905 case PPC::RLWNM8
: III
.ImmOpcode
= PPC::RLWINM8
; break;
2906 case PPC::RLWNMo
: III
.ImmOpcode
= PPC::RLWINMo
; break;
2907 case PPC::RLWNM8o
: III
.ImmOpcode
= PPC::RLWINM8o
; break;
2908 case PPC::SLW
: III
.ImmOpcode
= PPC::RLWINM
; break;
2909 case PPC::SLW8
: III
.ImmOpcode
= PPC::RLWINM8
; break;
2910 case PPC::SLWo
: III
.ImmOpcode
= PPC::RLWINMo
; break;
2911 case PPC::SLW8o
: III
.ImmOpcode
= PPC::RLWINM8o
; break;
2912 case PPC::SRW
: III
.ImmOpcode
= PPC::RLWINM
; break;
2913 case PPC::SRW8
: III
.ImmOpcode
= PPC::RLWINM8
; break;
2914 case PPC::SRWo
: III
.ImmOpcode
= PPC::RLWINMo
; break;
2915 case PPC::SRW8o
: III
.ImmOpcode
= PPC::RLWINM8o
; break;
2918 III
.TruncateImmTo
= 0;
2919 III
.ImmOpcode
= PPC::SRAWI
;
2923 III
.TruncateImmTo
= 0;
2924 III
.ImmOpcode
= PPC::SRAWIo
;
2938 III
.SignedImm
= false;
2939 III
.ZeroIsSpecialOrig
= 0;
2940 III
.ZeroIsSpecialNew
= 0;
2941 III
.IsCommutative
= false;
2942 // This isn't actually true, but the instructions ignore any of the
2943 // upper bits, so any immediate loaded with an LI is acceptable.
2944 // This does not apply to shift right algebraic because a value
2945 // out of range will produce a -1/0.
2947 if (Opc
== PPC::RLDCL
|| Opc
== PPC::RLDCLo
||
2948 Opc
== PPC::RLDCR
|| Opc
== PPC::RLDCRo
)
2949 III
.TruncateImmTo
= 6;
2951 III
.TruncateImmTo
= 7;
2953 default: llvm_unreachable("Unknown opcode");
2954 case PPC::RLDCL
: III
.ImmOpcode
= PPC::RLDICL
; break;
2955 case PPC::RLDCLo
: III
.ImmOpcode
= PPC::RLDICLo
; break;
2956 case PPC::RLDCR
: III
.ImmOpcode
= PPC::RLDICR
; break;
2957 case PPC::RLDCRo
: III
.ImmOpcode
= PPC::RLDICRo
; break;
2958 case PPC::SLD
: III
.ImmOpcode
= PPC::RLDICR
; break;
2959 case PPC::SLDo
: III
.ImmOpcode
= PPC::RLDICRo
; break;
2960 case PPC::SRD
: III
.ImmOpcode
= PPC::RLDICL
; break;
2961 case PPC::SRDo
: III
.ImmOpcode
= PPC::RLDICLo
; break;
2964 III
.TruncateImmTo
= 0;
2965 III
.ImmOpcode
= PPC::SRADI
;
2969 III
.TruncateImmTo
= 0;
2970 III
.ImmOpcode
= PPC::SRADIo
;
2974 // Loads and stores:
2996 III
.SignedImm
= true;
2997 III
.ZeroIsSpecialOrig
= 1;
2998 III
.ZeroIsSpecialNew
= 2;
2999 III
.IsCommutative
= true;
3000 III
.IsSummingOperands
= true;
3002 III
.OpNoForForwarding
= 2;
3004 default: llvm_unreachable("Unknown opcode");
3005 case PPC::LBZX
: III
.ImmOpcode
= PPC::LBZ
; break;
3006 case PPC::LBZX8
: III
.ImmOpcode
= PPC::LBZ8
; break;
3007 case PPC::LHZX
: III
.ImmOpcode
= PPC::LHZ
; break;
3008 case PPC::LHZX8
: III
.ImmOpcode
= PPC::LHZ8
; break;
3009 case PPC::LHAX
: III
.ImmOpcode
= PPC::LHA
; break;
3010 case PPC::LHAX8
: III
.ImmOpcode
= PPC::LHA8
; break;
3011 case PPC::LWZX
: III
.ImmOpcode
= PPC::LWZ
; break;
3012 case PPC::LWZX8
: III
.ImmOpcode
= PPC::LWZ8
; break;
3014 III
.ImmOpcode
= PPC::LWA
;
3015 III
.ImmMustBeMultipleOf
= 4;
3017 case PPC::LDX
: III
.ImmOpcode
= PPC::LD
; III
.ImmMustBeMultipleOf
= 4; break;
3018 case PPC::LFSX
: III
.ImmOpcode
= PPC::LFS
; break;
3019 case PPC::LFDX
: III
.ImmOpcode
= PPC::LFD
; break;
3020 case PPC::STBX
: III
.ImmOpcode
= PPC::STB
; break;
3021 case PPC::STBX8
: III
.ImmOpcode
= PPC::STB8
; break;
3022 case PPC::STHX
: III
.ImmOpcode
= PPC::STH
; break;
3023 case PPC::STHX8
: III
.ImmOpcode
= PPC::STH8
; break;
3024 case PPC::STWX
: III
.ImmOpcode
= PPC::STW
; break;
3025 case PPC::STWX8
: III
.ImmOpcode
= PPC::STW8
; break;
3027 III
.ImmOpcode
= PPC::STD
;
3028 III
.ImmMustBeMultipleOf
= 4;
3030 case PPC::STFSX
: III
.ImmOpcode
= PPC::STFS
; break;
3031 case PPC::STFDX
: III
.ImmOpcode
= PPC::STFD
; break;
3054 III
.SignedImm
= true;
3055 III
.ZeroIsSpecialOrig
= 2;
3056 III
.ZeroIsSpecialNew
= 3;
3057 III
.IsCommutative
= false;
3058 III
.IsSummingOperands
= true;
3060 III
.OpNoForForwarding
= 3;
3062 default: llvm_unreachable("Unknown opcode");
3063 case PPC::LBZUX
: III
.ImmOpcode
= PPC::LBZU
; break;
3064 case PPC::LBZUX8
: III
.ImmOpcode
= PPC::LBZU8
; break;
3065 case PPC::LHZUX
: III
.ImmOpcode
= PPC::LHZU
; break;
3066 case PPC::LHZUX8
: III
.ImmOpcode
= PPC::LHZU8
; break;
3067 case PPC::LHAUX
: III
.ImmOpcode
= PPC::LHAU
; break;
3068 case PPC::LHAUX8
: III
.ImmOpcode
= PPC::LHAU8
; break;
3069 case PPC::LWZUX
: III
.ImmOpcode
= PPC::LWZU
; break;
3070 case PPC::LWZUX8
: III
.ImmOpcode
= PPC::LWZU8
; break;
3072 III
.ImmOpcode
= PPC::LDU
;
3073 III
.ImmMustBeMultipleOf
= 4;
3075 case PPC::LFSUX
: III
.ImmOpcode
= PPC::LFSU
; break;
3076 case PPC::LFDUX
: III
.ImmOpcode
= PPC::LFDU
; break;
3077 case PPC::STBUX
: III
.ImmOpcode
= PPC::STBU
; break;
3078 case PPC::STBUX8
: III
.ImmOpcode
= PPC::STBU8
; break;
3079 case PPC::STHUX
: III
.ImmOpcode
= PPC::STHU
; break;
3080 case PPC::STHUX8
: III
.ImmOpcode
= PPC::STHU8
; break;
3081 case PPC::STWUX
: III
.ImmOpcode
= PPC::STWU
; break;
3082 case PPC::STWUX8
: III
.ImmOpcode
= PPC::STWU8
; break;
3084 III
.ImmOpcode
= PPC::STDU
;
3085 III
.ImmMustBeMultipleOf
= 4;
3087 case PPC::STFSUX
: III
.ImmOpcode
= PPC::STFSU
; break;
3088 case PPC::STFDUX
: III
.ImmOpcode
= PPC::STFDU
; break;
3091 // Power9 and up only. For some of these, the X-Form version has access to all
3092 // 64 VSR's whereas the D-Form only has access to the VR's. We replace those
3093 // with pseudo-ops pre-ra and for post-ra, we check that the register loaded
3094 // into or stored from is one of the VR registers.
3101 case PPC::XFLOADf32
:
3102 case PPC::XFLOADf64
:
3103 case PPC::XFSTOREf32
:
3104 case PPC::XFSTOREf64
:
3105 if (!Subtarget
.hasP9Vector())
3107 III
.SignedImm
= true;
3108 III
.ZeroIsSpecialOrig
= 1;
3109 III
.ZeroIsSpecialNew
= 2;
3110 III
.IsCommutative
= true;
3111 III
.IsSummingOperands
= true;
3113 III
.OpNoForForwarding
= 2;
3114 III
.ImmMustBeMultipleOf
= 4;
3116 default: llvm_unreachable("Unknown opcode");
3118 III
.ImmOpcode
= PPC::LXV
;
3119 III
.ImmMustBeMultipleOf
= 16;
3124 III
.ImmOpcode
= PPC::LXSSP
;
3126 III
.ImmOpcode
= PPC::LFS
;
3127 III
.ImmMustBeMultipleOf
= 1;
3132 case PPC::XFLOADf32
:
3133 III
.ImmOpcode
= PPC::DFLOADf32
;
3138 III
.ImmOpcode
= PPC::LXSD
;
3140 III
.ImmOpcode
= PPC::LFD
;
3141 III
.ImmMustBeMultipleOf
= 1;
3146 case PPC::XFLOADf64
:
3147 III
.ImmOpcode
= PPC::DFLOADf64
;
3150 III
.ImmOpcode
= PPC::STXV
;
3151 III
.ImmMustBeMultipleOf
= 16;
3156 III
.ImmOpcode
= PPC::STXSSP
;
3158 III
.ImmOpcode
= PPC::STFS
;
3159 III
.ImmMustBeMultipleOf
= 1;
3164 case PPC::XFSTOREf32
:
3165 III
.ImmOpcode
= PPC::DFSTOREf32
;
3170 III
.ImmOpcode
= PPC::STXSD
;
3172 III
.ImmOpcode
= PPC::STFD
;
3173 III
.ImmMustBeMultipleOf
= 1;
3178 case PPC::XFSTOREf64
:
3179 III
.ImmOpcode
= PPC::DFSTOREf64
;
3187 // Utility function for swaping two arbitrary operands of an instruction.
3188 static void swapMIOperands(MachineInstr
&MI
, unsigned Op1
, unsigned Op2
) {
3189 assert(Op1
!= Op2
&& "Cannot swap operand with itself.");
3191 unsigned MaxOp
= std::max(Op1
, Op2
);
3192 unsigned MinOp
= std::min(Op1
, Op2
);
3193 MachineOperand MOp1
= MI
.getOperand(MinOp
);
3194 MachineOperand MOp2
= MI
.getOperand(MaxOp
);
3195 MI
.RemoveOperand(std::max(Op1
, Op2
));
3196 MI
.RemoveOperand(std::min(Op1
, Op2
));
3198 // If the operands we are swapping are the two at the end (the common case)
3199 // we can just remove both and add them in the opposite order.
3200 if (MaxOp
- MinOp
== 1 && MI
.getNumOperands() == MinOp
) {
3201 MI
.addOperand(MOp2
);
3202 MI
.addOperand(MOp1
);
3204 // Store all operands in a temporary vector, remove them and re-add in the
3206 SmallVector
<MachineOperand
, 2> MOps
;
3207 unsigned TotalOps
= MI
.getNumOperands() + 2; // We've already removed 2 ops.
3208 for (unsigned i
= MI
.getNumOperands() - 1; i
>= MinOp
; i
--) {
3209 MOps
.push_back(MI
.getOperand(i
));
3210 MI
.RemoveOperand(i
);
3212 // MOp2 needs to be added next.
3213 MI
.addOperand(MOp2
);
3214 // Now add the rest.
3215 for (unsigned i
= MI
.getNumOperands(); i
< TotalOps
; i
++) {
3217 MI
.addOperand(MOp1
);
3219 MI
.addOperand(MOps
.back());
3226 // Check if the 'MI' that has the index OpNoForForwarding
3227 // meets the requirement described in the ImmInstrInfo.
3228 bool PPCInstrInfo::isUseMIElgibleForForwarding(MachineInstr
&MI
,
3229 const ImmInstrInfo
&III
,
3230 unsigned OpNoForForwarding
3232 // As the algorithm of checking for PPC::ZERO/PPC::ZERO8
3233 // would not work pre-RA, we can only do the check post RA.
3234 MachineRegisterInfo
&MRI
= MI
.getParent()->getParent()->getRegInfo();
3238 // Cannot do the transform if MI isn't summing the operands.
3239 if (!III
.IsSummingOperands
)
3242 // The instruction we are trying to replace must have the ZeroIsSpecialOrig set.
3243 if (!III
.ZeroIsSpecialOrig
)
3246 // We cannot do the transform if the operand we are trying to replace
3247 // isn't the same as the operand the instruction allows.
3248 if (OpNoForForwarding
!= III
.OpNoForForwarding
)
3251 // Check if the instruction we are trying to transform really has
3252 // the special zero register as its operand.
3253 if (MI
.getOperand(III
.ZeroIsSpecialOrig
).getReg() != PPC::ZERO
&&
3254 MI
.getOperand(III
.ZeroIsSpecialOrig
).getReg() != PPC::ZERO8
)
3257 // This machine instruction is convertible if it is,
3258 // 1. summing the operands.
3259 // 2. one of the operands is special zero register.
3260 // 3. the operand we are trying to replace is allowed by the MI.
3264 // Check if the DefMI is the add inst and set the ImmMO and RegMO
3266 bool PPCInstrInfo::isDefMIElgibleForForwarding(MachineInstr
&DefMI
,
3267 const ImmInstrInfo
&III
,
3268 MachineOperand
*&ImmMO
,
3269 MachineOperand
*&RegMO
) const {
3270 unsigned Opc
= DefMI
.getOpcode();
3271 if (Opc
!= PPC::ADDItocL
&& Opc
!= PPC::ADDI
&& Opc
!= PPC::ADDI8
)
3274 assert(DefMI
.getNumOperands() >= 3 &&
3275 "Add inst must have at least three operands");
3276 RegMO
= &DefMI
.getOperand(1);
3277 ImmMO
= &DefMI
.getOperand(2);
3279 // This DefMI is elgible for forwarding if it is:
3281 // 2. one of the operands is Imm/CPI/Global.
3282 return isAnImmediateOperand(*ImmMO
);
3285 bool PPCInstrInfo::isRegElgibleForForwarding(
3286 const MachineOperand
&RegMO
, const MachineInstr
&DefMI
,
3287 const MachineInstr
&MI
, bool KillDefMI
,
3288 bool &IsFwdFeederRegKilled
) const {
3291 // z = lfdx 0, x -> z = lfd imm(y)
3292 // The Reg "y" can be forwarded to the MI(z) only when there is no DEF
3293 // of "y" between the DEF of "x" and "z".
3294 // The query is only valid post RA.
3295 const MachineRegisterInfo
&MRI
= MI
.getParent()->getParent()->getRegInfo();
3299 Register Reg
= RegMO
.getReg();
3301 // Walking the inst in reverse(MI-->DefMI) to get the last DEF of the Reg.
3302 MachineBasicBlock::const_reverse_iterator It
= MI
;
3303 MachineBasicBlock::const_reverse_iterator E
= MI
.getParent()->rend();
3305 for (; It
!= E
; ++It
) {
3306 if (It
->modifiesRegister(Reg
, &getRegisterInfo()) && (&*It
) != &DefMI
)
3308 else if (It
->killsRegister(Reg
, &getRegisterInfo()) && (&*It
) != &DefMI
)
3309 IsFwdFeederRegKilled
= true;
3310 // Made it to DefMI without encountering a clobber.
3311 if ((&*It
) == &DefMI
)
3314 assert((&*It
) == &DefMI
&& "DefMI is missing");
3316 // If DefMI also defines the register to be forwarded, we can only forward it
3317 // if DefMI is being erased.
3318 if (DefMI
.modifiesRegister(Reg
, &getRegisterInfo()))
3324 bool PPCInstrInfo::isImmElgibleForForwarding(const MachineOperand
&ImmMO
,
3325 const MachineInstr
&DefMI
,
3326 const ImmInstrInfo
&III
,
3327 int64_t &Imm
) const {
3328 assert(isAnImmediateOperand(ImmMO
) && "ImmMO is NOT an immediate");
3329 if (DefMI
.getOpcode() == PPC::ADDItocL
) {
3330 // The operand for ADDItocL is CPI, which isn't imm at compiling time,
3331 // However, we know that, it is 16-bit width, and has the alignment of 4.
3332 // Check if the instruction met the requirement.
3333 if (III
.ImmMustBeMultipleOf
> 4 ||
3334 III
.TruncateImmTo
|| III
.ImmWidth
!= 16)
3337 // Going from XForm to DForm loads means that the displacement needs to be
3338 // not just an immediate but also a multiple of 4, or 16 depending on the
3339 // load. A DForm load cannot be represented if it is a multiple of say 2.
3340 // XForm loads do not have this restriction.
3341 if (ImmMO
.isGlobal() &&
3342 ImmMO
.getGlobal()->getAlignment() < III
.ImmMustBeMultipleOf
)
3348 if (ImmMO
.isImm()) {
3349 // It is Imm, we need to check if the Imm fit the range.
3350 int64_t Immediate
= ImmMO
.getImm();
3351 // Sign-extend to 64-bits.
3352 Imm
= ((uint64_t)Immediate
& ~0x7FFFuLL
) != 0 ?
3353 (Immediate
| 0xFFFFFFFFFFFF0000) : Immediate
;
3355 if (Imm
% III
.ImmMustBeMultipleOf
)
3357 if (III
.TruncateImmTo
)
3358 Imm
&= ((1 << III
.TruncateImmTo
) - 1);
3359 if (III
.SignedImm
) {
3360 APInt
ActualValue(64, Imm
, true);
3361 if (!ActualValue
.isSignedIntN(III
.ImmWidth
))
3364 uint64_t UnsignedMax
= (1 << III
.ImmWidth
) - 1;
3365 if ((uint64_t)Imm
> UnsignedMax
)
3372 // This ImmMO is forwarded if it meets the requriement describle
3377 // If an X-Form instruction is fed by an add-immediate and one of its operands
3378 // is the literal zero, attempt to forward the source of the add-immediate to
3379 // the corresponding D-Form instruction with the displacement coming from
3380 // the immediate being added.
3381 bool PPCInstrInfo::transformToImmFormFedByAdd(
3382 MachineInstr
&MI
, const ImmInstrInfo
&III
, unsigned OpNoForForwarding
,
3383 MachineInstr
&DefMI
, bool KillDefMI
) const {
3386 // x = addi reg, imm <----- DefMI
3387 // y = op 0 , x <----- MI
3389 // OpNoForForwarding
3390 // Check if the MI meet the requirement described in the III.
3391 if (!isUseMIElgibleForForwarding(MI
, III
, OpNoForForwarding
))
3394 // Check if the DefMI meet the requirement
3395 // described in the III. If yes, set the ImmMO and RegMO accordingly.
3396 MachineOperand
*ImmMO
= nullptr;
3397 MachineOperand
*RegMO
= nullptr;
3398 if (!isDefMIElgibleForForwarding(DefMI
, III
, ImmMO
, RegMO
))
3400 assert(ImmMO
&& RegMO
&& "Imm and Reg operand must have been set");
3402 // As we get the Imm operand now, we need to check if the ImmMO meet
3403 // the requirement described in the III. If yes set the Imm.
3405 if (!isImmElgibleForForwarding(*ImmMO
, DefMI
, III
, Imm
))
3408 bool IsFwdFeederRegKilled
= false;
3409 // Check if the RegMO can be forwarded to MI.
3410 if (!isRegElgibleForForwarding(*RegMO
, DefMI
, MI
, KillDefMI
,
3411 IsFwdFeederRegKilled
))
3414 // Get killed info in case fixup needed after transformation.
3415 unsigned ForwardKilledOperandReg
= ~0U;
3416 MachineRegisterInfo
&MRI
= MI
.getParent()->getParent()->getRegInfo();
3417 bool PostRA
= !MRI
.isSSA();
3418 if (PostRA
&& MI
.getOperand(OpNoForForwarding
).isKill())
3419 ForwardKilledOperandReg
= MI
.getOperand(OpNoForForwarding
).getReg();
3421 // We know that, the MI and DefMI both meet the pattern, and
3422 // the Imm also meet the requirement with the new Imm-form.
3423 // It is safe to do the transformation now.
3424 LLVM_DEBUG(dbgs() << "Replacing instruction:\n");
3425 LLVM_DEBUG(MI
.dump());
3426 LLVM_DEBUG(dbgs() << "Fed by:\n");
3427 LLVM_DEBUG(DefMI
.dump());
3429 // Update the base reg first.
3430 MI
.getOperand(III
.OpNoForForwarding
).ChangeToRegister(RegMO
->getReg(),
3434 // Then, update the imm.
3435 if (ImmMO
->isImm()) {
3436 // If the ImmMO is Imm, change the operand that has ZERO to that Imm
3438 replaceInstrOperandWithImm(MI
, III
.ZeroIsSpecialOrig
, Imm
);
3441 // Otherwise, it is Constant Pool Index(CPI) or Global,
3442 // which is relocation in fact. We need to replace the special zero
3443 // register with ImmMO.
3444 // Before that, we need to fixup the target flags for imm.
3445 // For some reason, we miss to set the flag for the ImmMO if it is CPI.
3446 if (DefMI
.getOpcode() == PPC::ADDItocL
)
3447 ImmMO
->setTargetFlags(PPCII::MO_TOC_LO
);
3449 // MI didn't have the interface such as MI.setOperand(i) though
3450 // it has MI.getOperand(i). To repalce the ZERO MachineOperand with
3451 // ImmMO, we need to remove ZERO operand and all the operands behind it,
3452 // and, add the ImmMO, then, move back all the operands behind ZERO.
3453 SmallVector
<MachineOperand
, 2> MOps
;
3454 for (unsigned i
= MI
.getNumOperands() - 1; i
>= III
.ZeroIsSpecialOrig
; i
--) {
3455 MOps
.push_back(MI
.getOperand(i
));
3456 MI
.RemoveOperand(i
);
3459 // Remove the last MO in the list, which is ZERO operand in fact.
3461 // Add the imm operand.
3462 MI
.addOperand(*ImmMO
);
3463 // Now add the rest back.
3464 for (auto &MO
: MOps
)
3468 // Update the opcode.
3469 MI
.setDesc(get(III
.ImmOpcode
));
3471 // Fix up killed/dead flag after transformation.
3473 // x = ADD KilledFwdFeederReg, imm
3474 // n = opn KilledFwdFeederReg(killed), regn
3477 // x = ADD reg(killed), imm
3479 if (IsFwdFeederRegKilled
|| RegMO
->isKill())
3480 fixupIsDeadOrKill(DefMI
, MI
, RegMO
->getReg());
3482 // ForwardKilledOperandReg = ADD reg, imm
3483 // y = XOP 0, ForwardKilledOperandReg(killed)
3484 if (ForwardKilledOperandReg
!= ~0U)
3485 fixupIsDeadOrKill(DefMI
, MI
, ForwardKilledOperandReg
);
3487 LLVM_DEBUG(dbgs() << "With:\n");
3488 LLVM_DEBUG(MI
.dump());
3493 bool PPCInstrInfo::transformToImmFormFedByLI(MachineInstr
&MI
,
3494 const ImmInstrInfo
&III
,
3495 unsigned ConstantOpNo
,
3496 MachineInstr
&DefMI
,
3497 int64_t Imm
) const {
3498 MachineRegisterInfo
&MRI
= MI
.getParent()->getParent()->getRegInfo();
3499 bool PostRA
= !MRI
.isSSA();
3500 // Exit early if we can't convert this.
3501 if ((ConstantOpNo
!= III
.OpNoForForwarding
) && !III
.IsCommutative
)
3503 if (Imm
% III
.ImmMustBeMultipleOf
)
3505 if (III
.TruncateImmTo
)
3506 Imm
&= ((1 << III
.TruncateImmTo
) - 1);
3507 if (III
.SignedImm
) {
3508 APInt
ActualValue(64, Imm
, true);
3509 if (!ActualValue
.isSignedIntN(III
.ImmWidth
))
3512 uint64_t UnsignedMax
= (1 << III
.ImmWidth
) - 1;
3513 if ((uint64_t)Imm
> UnsignedMax
)
3517 // If we're post-RA, the instructions don't agree on whether register zero is
3518 // special, we can transform this as long as the register operand that will
3519 // end up in the location where zero is special isn't R0.
3520 if (PostRA
&& III
.ZeroIsSpecialOrig
!= III
.ZeroIsSpecialNew
) {
3521 unsigned PosForOrigZero
= III
.ZeroIsSpecialOrig
? III
.ZeroIsSpecialOrig
:
3522 III
.ZeroIsSpecialNew
+ 1;
3523 Register OrigZeroReg
= MI
.getOperand(PosForOrigZero
).getReg();
3524 Register NewZeroReg
= MI
.getOperand(III
.ZeroIsSpecialNew
).getReg();
3525 // If R0 is in the operand where zero is special for the new instruction,
3526 // it is unsafe to transform if the constant operand isn't that operand.
3527 if ((NewZeroReg
== PPC::R0
|| NewZeroReg
== PPC::X0
) &&
3528 ConstantOpNo
!= III
.ZeroIsSpecialNew
)
3530 if ((OrigZeroReg
== PPC::R0
|| OrigZeroReg
== PPC::X0
) &&
3531 ConstantOpNo
!= PosForOrigZero
)
3535 // Get killed info in case fixup needed after transformation.
3536 unsigned ForwardKilledOperandReg
= ~0U;
3537 if (PostRA
&& MI
.getOperand(ConstantOpNo
).isKill())
3538 ForwardKilledOperandReg
= MI
.getOperand(ConstantOpNo
).getReg();
3540 unsigned Opc
= MI
.getOpcode();
3541 bool SpecialShift32
=
3542 Opc
== PPC::SLW
|| Opc
== PPC::SLWo
|| Opc
== PPC::SRW
|| Opc
== PPC::SRWo
;
3543 bool SpecialShift64
=
3544 Opc
== PPC::SLD
|| Opc
== PPC::SLDo
|| Opc
== PPC::SRD
|| Opc
== PPC::SRDo
;
3545 bool SetCR
= Opc
== PPC::SLWo
|| Opc
== PPC::SRWo
||
3546 Opc
== PPC::SLDo
|| Opc
== PPC::SRDo
;
3548 Opc
== PPC::SRW
|| Opc
== PPC::SRWo
|| Opc
== PPC::SRD
|| Opc
== PPC::SRDo
;
3550 MI
.setDesc(get(III
.ImmOpcode
));
3551 if (ConstantOpNo
== III
.OpNoForForwarding
) {
3552 // Converting shifts to immediate form is a bit tricky since they may do
3553 // one of three things:
3554 // 1. If the shift amount is between OpSize and 2*OpSize, the result is zero
3555 // 2. If the shift amount is zero, the result is unchanged (save for maybe
3557 // 3. If the shift amount is in [1, OpSize), it's just a shift
3558 if (SpecialShift32
|| SpecialShift64
) {
3559 LoadImmediateInfo LII
;
3562 LII
.Is64Bit
= SpecialShift64
;
3563 uint64_t ShAmt
= Imm
& (SpecialShift32
? 0x1F : 0x3F);
3564 if (Imm
& (SpecialShift32
? 0x20 : 0x40))
3565 replaceInstrWithLI(MI
, LII
);
3566 // Shifts by zero don't change the value. If we don't need to set CR0,
3567 // just convert this to a COPY. Can't do this post-RA since we've already
3568 // cleaned up the copies.
3569 else if (!SetCR
&& ShAmt
== 0 && !PostRA
) {
3570 MI
.RemoveOperand(2);
3571 MI
.setDesc(get(PPC::COPY
));
3573 // The 32 bit and 64 bit instructions are quite different.
3574 if (SpecialShift32
) {
3575 // Left shifts use (N, 0, 31-N).
3576 // Right shifts use (32-N, N, 31) if 0 < N < 32.
3577 // use (0, 0, 31) if N == 0.
3578 uint64_t SH
= ShAmt
== 0 ? 0 : RightShift
? 32 - ShAmt
: ShAmt
;
3579 uint64_t MB
= RightShift
? ShAmt
: 0;
3580 uint64_t ME
= RightShift
? 31 : 31 - ShAmt
;
3581 replaceInstrOperandWithImm(MI
, III
.OpNoForForwarding
, SH
);
3582 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
).addImm(MB
)
3585 // Left shifts use (N, 63-N).
3586 // Right shifts use (64-N, N) if 0 < N < 64.
3587 // use (0, 0) if N == 0.
3588 uint64_t SH
= ShAmt
== 0 ? 0 : RightShift
? 64 - ShAmt
: ShAmt
;
3589 uint64_t ME
= RightShift
? ShAmt
: 63 - ShAmt
;
3590 replaceInstrOperandWithImm(MI
, III
.OpNoForForwarding
, SH
);
3591 MachineInstrBuilder(*MI
.getParent()->getParent(), MI
).addImm(ME
);
3595 replaceInstrOperandWithImm(MI
, ConstantOpNo
, Imm
);
3597 // Convert commutative instructions (switch the operands and convert the
3598 // desired one to an immediate.
3599 else if (III
.IsCommutative
) {
3600 replaceInstrOperandWithImm(MI
, ConstantOpNo
, Imm
);
3601 swapMIOperands(MI
, ConstantOpNo
, III
.OpNoForForwarding
);
3603 llvm_unreachable("Should have exited early!");
3605 // For instructions for which the constant register replaces a different
3606 // operand than where the immediate goes, we need to swap them.
3607 if (III
.OpNoForForwarding
!= III
.ImmOpNo
)
3608 swapMIOperands(MI
, III
.OpNoForForwarding
, III
.ImmOpNo
);
3610 // If the special R0/X0 register index are different for original instruction
3611 // and new instruction, we need to fix up the register class in new
3613 if (!PostRA
&& III
.ZeroIsSpecialOrig
!= III
.ZeroIsSpecialNew
) {
3614 if (III
.ZeroIsSpecialNew
) {
3615 // If operand at III.ZeroIsSpecialNew is physical reg(eg: ZERO/ZERO8), no
3616 // need to fix up register class.
3617 Register RegToModify
= MI
.getOperand(III
.ZeroIsSpecialNew
).getReg();
3618 if (Register::isVirtualRegister(RegToModify
)) {
3619 const TargetRegisterClass
*NewRC
=
3620 MRI
.getRegClass(RegToModify
)->hasSuperClassEq(&PPC::GPRCRegClass
) ?
3621 &PPC::GPRC_and_GPRC_NOR0RegClass
: &PPC::G8RC_and_G8RC_NOX0RegClass
;
3622 MRI
.setRegClass(RegToModify
, NewRC
);
3627 // Fix up killed/dead flag after transformation.
3629 // ForwardKilledOperandReg = LI imm
3630 // y = XOP reg, ForwardKilledOperandReg(killed)
3631 if (ForwardKilledOperandReg
!= ~0U)
3632 fixupIsDeadOrKill(DefMI
, MI
, ForwardKilledOperandReg
);
3636 const TargetRegisterClass
*
3637 PPCInstrInfo::updatedRC(const TargetRegisterClass
*RC
) const {
3638 if (Subtarget
.hasVSX() && RC
== &PPC::VRRCRegClass
)
3639 return &PPC::VSRCRegClass
;
3643 int PPCInstrInfo::getRecordFormOpcode(unsigned Opcode
) {
3644 return PPC::getRecordFormOpcode(Opcode
);
3647 // This function returns true if the machine instruction
3648 // always outputs a value by sign-extending a 32 bit value,
3649 // i.e. 0 to 31-th bits are same as 32-th bit.
3650 static bool isSignExtendingOp(const MachineInstr
&MI
) {
3651 int Opcode
= MI
.getOpcode();
3652 if (Opcode
== PPC::LI
|| Opcode
== PPC::LI8
||
3653 Opcode
== PPC::LIS
|| Opcode
== PPC::LIS8
||
3654 Opcode
== PPC::SRAW
|| Opcode
== PPC::SRAWo
||
3655 Opcode
== PPC::SRAWI
|| Opcode
== PPC::SRAWIo
||
3656 Opcode
== PPC::LWA
|| Opcode
== PPC::LWAX
||
3657 Opcode
== PPC::LWA_32
|| Opcode
== PPC::LWAX_32
||
3658 Opcode
== PPC::LHA
|| Opcode
== PPC::LHAX
||
3659 Opcode
== PPC::LHA8
|| Opcode
== PPC::LHAX8
||
3660 Opcode
== PPC::LBZ
|| Opcode
== PPC::LBZX
||
3661 Opcode
== PPC::LBZ8
|| Opcode
== PPC::LBZX8
||
3662 Opcode
== PPC::LBZU
|| Opcode
== PPC::LBZUX
||
3663 Opcode
== PPC::LBZU8
|| Opcode
== PPC::LBZUX8
||
3664 Opcode
== PPC::LHZ
|| Opcode
== PPC::LHZX
||
3665 Opcode
== PPC::LHZ8
|| Opcode
== PPC::LHZX8
||
3666 Opcode
== PPC::LHZU
|| Opcode
== PPC::LHZUX
||
3667 Opcode
== PPC::LHZU8
|| Opcode
== PPC::LHZUX8
||
3668 Opcode
== PPC::EXTSB
|| Opcode
== PPC::EXTSBo
||
3669 Opcode
== PPC::EXTSH
|| Opcode
== PPC::EXTSHo
||
3670 Opcode
== PPC::EXTSB8
|| Opcode
== PPC::EXTSH8
||
3671 Opcode
== PPC::EXTSW
|| Opcode
== PPC::EXTSWo
||
3672 Opcode
== PPC::SETB
|| Opcode
== PPC::SETB8
||
3673 Opcode
== PPC::EXTSH8_32_64
|| Opcode
== PPC::EXTSW_32_64
||
3674 Opcode
== PPC::EXTSB8_32_64
)
3677 if (Opcode
== PPC::RLDICL
&& MI
.getOperand(3).getImm() >= 33)
3680 if ((Opcode
== PPC::RLWINM
|| Opcode
== PPC::RLWINMo
||
3681 Opcode
== PPC::RLWNM
|| Opcode
== PPC::RLWNMo
) &&
3682 MI
.getOperand(3).getImm() > 0 &&
3683 MI
.getOperand(3).getImm() <= MI
.getOperand(4).getImm())
3689 // This function returns true if the machine instruction
3690 // always outputs zeros in higher 32 bits.
3691 static bool isZeroExtendingOp(const MachineInstr
&MI
) {
3692 int Opcode
= MI
.getOpcode();
3693 // The 16-bit immediate is sign-extended in li/lis.
3694 // If the most significant bit is zero, all higher bits are zero.
3695 if (Opcode
== PPC::LI
|| Opcode
== PPC::LI8
||
3696 Opcode
== PPC::LIS
|| Opcode
== PPC::LIS8
) {
3697 int64_t Imm
= MI
.getOperand(1).getImm();
3698 if (((uint64_t)Imm
& ~0x7FFFuLL
) == 0)
3702 // We have some variations of rotate-and-mask instructions
3703 // that clear higher 32-bits.
3704 if ((Opcode
== PPC::RLDICL
|| Opcode
== PPC::RLDICLo
||
3705 Opcode
== PPC::RLDCL
|| Opcode
== PPC::RLDCLo
||
3706 Opcode
== PPC::RLDICL_32_64
) &&
3707 MI
.getOperand(3).getImm() >= 32)
3710 if ((Opcode
== PPC::RLDIC
|| Opcode
== PPC::RLDICo
) &&
3711 MI
.getOperand(3).getImm() >= 32 &&
3712 MI
.getOperand(3).getImm() <= 63 - MI
.getOperand(2).getImm())
3715 if ((Opcode
== PPC::RLWINM
|| Opcode
== PPC::RLWINMo
||
3716 Opcode
== PPC::RLWNM
|| Opcode
== PPC::RLWNMo
||
3717 Opcode
== PPC::RLWINM8
|| Opcode
== PPC::RLWNM8
) &&
3718 MI
.getOperand(3).getImm() <= MI
.getOperand(4).getImm())
3721 // There are other instructions that clear higher 32-bits.
3722 if (Opcode
== PPC::CNTLZW
|| Opcode
== PPC::CNTLZWo
||
3723 Opcode
== PPC::CNTTZW
|| Opcode
== PPC::CNTTZWo
||
3724 Opcode
== PPC::CNTLZW8
|| Opcode
== PPC::CNTTZW8
||
3725 Opcode
== PPC::CNTLZD
|| Opcode
== PPC::CNTLZDo
||
3726 Opcode
== PPC::CNTTZD
|| Opcode
== PPC::CNTTZDo
||
3727 Opcode
== PPC::POPCNTD
|| Opcode
== PPC::POPCNTW
||
3728 Opcode
== PPC::SLW
|| Opcode
== PPC::SLWo
||
3729 Opcode
== PPC::SRW
|| Opcode
== PPC::SRWo
||
3730 Opcode
== PPC::SLW8
|| Opcode
== PPC::SRW8
||
3731 Opcode
== PPC::SLWI
|| Opcode
== PPC::SLWIo
||
3732 Opcode
== PPC::SRWI
|| Opcode
== PPC::SRWIo
||
3733 Opcode
== PPC::LWZ
|| Opcode
== PPC::LWZX
||
3734 Opcode
== PPC::LWZU
|| Opcode
== PPC::LWZUX
||
3735 Opcode
== PPC::LWBRX
|| Opcode
== PPC::LHBRX
||
3736 Opcode
== PPC::LHZ
|| Opcode
== PPC::LHZX
||
3737 Opcode
== PPC::LHZU
|| Opcode
== PPC::LHZUX
||
3738 Opcode
== PPC::LBZ
|| Opcode
== PPC::LBZX
||
3739 Opcode
== PPC::LBZU
|| Opcode
== PPC::LBZUX
||
3740 Opcode
== PPC::LWZ8
|| Opcode
== PPC::LWZX8
||
3741 Opcode
== PPC::LWZU8
|| Opcode
== PPC::LWZUX8
||
3742 Opcode
== PPC::LWBRX8
|| Opcode
== PPC::LHBRX8
||
3743 Opcode
== PPC::LHZ8
|| Opcode
== PPC::LHZX8
||
3744 Opcode
== PPC::LHZU8
|| Opcode
== PPC::LHZUX8
||
3745 Opcode
== PPC::LBZ8
|| Opcode
== PPC::LBZX8
||
3746 Opcode
== PPC::LBZU8
|| Opcode
== PPC::LBZUX8
||
3747 Opcode
== PPC::ANDIo
|| Opcode
== PPC::ANDISo
||
3748 Opcode
== PPC::ROTRWI
|| Opcode
== PPC::ROTRWIo
||
3749 Opcode
== PPC::EXTLWI
|| Opcode
== PPC::EXTLWIo
||
3750 Opcode
== PPC::MFVSRWZ
)
3756 // This function returns true if the input MachineInstr is a TOC save
3758 bool PPCInstrInfo::isTOCSaveMI(const MachineInstr
&MI
) const {
3759 if (!MI
.getOperand(1).isImm() || !MI
.getOperand(2).isReg())
3761 unsigned TOCSaveOffset
= Subtarget
.getFrameLowering()->getTOCSaveOffset();
3762 unsigned StackOffset
= MI
.getOperand(1).getImm();
3763 Register StackReg
= MI
.getOperand(2).getReg();
3764 if (StackReg
== PPC::X1
&& StackOffset
== TOCSaveOffset
)
3770 // We limit the max depth to track incoming values of PHIs or binary ops
3771 // (e.g. AND) to avoid excessive cost.
3772 const unsigned MAX_DEPTH
= 1;
3775 PPCInstrInfo::isSignOrZeroExtended(const MachineInstr
&MI
, bool SignExt
,
3776 const unsigned Depth
) const {
3777 const MachineFunction
*MF
= MI
.getParent()->getParent();
3778 const MachineRegisterInfo
*MRI
= &MF
->getRegInfo();
3780 // If we know this instruction returns sign- or zero-extended result,
3782 if (SignExt
? isSignExtendingOp(MI
):
3783 isZeroExtendingOp(MI
))
3786 switch (MI
.getOpcode()) {
3788 Register SrcReg
= MI
.getOperand(1).getReg();
3790 // In both ELFv1 and v2 ABI, method parameters and the return value
3791 // are sign- or zero-extended.
3792 if (MF
->getSubtarget
<PPCSubtarget
>().isSVR4ABI()) {
3793 const PPCFunctionInfo
*FuncInfo
= MF
->getInfo
<PPCFunctionInfo
>();
3794 // We check the ZExt/SExt flags for a method parameter.
3795 if (MI
.getParent()->getBasicBlock() ==
3796 &MF
->getFunction().getEntryBlock()) {
3797 Register VReg
= MI
.getOperand(0).getReg();
3798 if (MF
->getRegInfo().isLiveIn(VReg
))
3799 return SignExt
? FuncInfo
->isLiveInSExt(VReg
) :
3800 FuncInfo
->isLiveInZExt(VReg
);
3803 // For a method return value, we check the ZExt/SExt flags in attribute.
3804 // We assume the following code sequence for method call.
3805 // ADJCALLSTACKDOWN 32, implicit dead %r1, implicit %r1
3806 // BL8_NOP @func,...
3807 // ADJCALLSTACKUP 32, 0, implicit dead %r1, implicit %r1
3808 // %5 = COPY %x3; G8RC:%5
3809 if (SrcReg
== PPC::X3
) {
3810 const MachineBasicBlock
*MBB
= MI
.getParent();
3811 MachineBasicBlock::const_instr_iterator II
=
3812 MachineBasicBlock::const_instr_iterator(&MI
);
3813 if (II
!= MBB
->instr_begin() &&
3814 (--II
)->getOpcode() == PPC::ADJCALLSTACKUP
) {
3815 const MachineInstr
&CallMI
= *(--II
);
3816 if (CallMI
.isCall() && CallMI
.getOperand(0).isGlobal()) {
3817 const Function
*CalleeFn
=
3818 dyn_cast
<Function
>(CallMI
.getOperand(0).getGlobal());
3821 const IntegerType
*IntTy
=
3822 dyn_cast
<IntegerType
>(CalleeFn
->getReturnType());
3823 const AttributeSet
&Attrs
=
3824 CalleeFn
->getAttributes().getRetAttributes();
3825 if (IntTy
&& IntTy
->getBitWidth() <= 32)
3826 return Attrs
.hasAttribute(SignExt
? Attribute::SExt
:
3833 // If this is a copy from another register, we recursively check source.
3834 if (!Register::isVirtualRegister(SrcReg
))
3836 const MachineInstr
*SrcMI
= MRI
->getVRegDef(SrcReg
);
3838 return isSignOrZeroExtended(*SrcMI
, SignExt
, Depth
);
3855 // logical operation with 16-bit immediate does not change the upper bits.
3856 // So, we track the operand register as we do for register copy.
3857 Register SrcReg
= MI
.getOperand(1).getReg();
3858 if (!Register::isVirtualRegister(SrcReg
))
3860 const MachineInstr
*SrcMI
= MRI
->getVRegDef(SrcReg
);
3862 return isSignOrZeroExtended(*SrcMI
, SignExt
, Depth
);
3867 // If all incoming values are sign-/zero-extended,
3868 // the output of OR, ISEL or PHI is also sign-/zero-extended.
3873 if (Depth
>= MAX_DEPTH
)
3876 // The input registers for PHI are operand 1, 3, ...
3877 // The input registers for others are operand 1 and 2.
3878 unsigned E
= 3, D
= 1;
3879 if (MI
.getOpcode() == PPC::PHI
) {
3880 E
= MI
.getNumOperands();
3884 for (unsigned I
= 1; I
!= E
; I
+= D
) {
3885 if (MI
.getOperand(I
).isReg()) {
3886 Register SrcReg
= MI
.getOperand(I
).getReg();
3887 if (!Register::isVirtualRegister(SrcReg
))
3889 const MachineInstr
*SrcMI
= MRI
->getVRegDef(SrcReg
);
3890 if (SrcMI
== NULL
|| !isSignOrZeroExtended(*SrcMI
, SignExt
, Depth
+1))
3899 // If at least one of the incoming values of an AND is zero extended
3900 // then the output is also zero-extended. If both of the incoming values
3901 // are sign-extended then the output is also sign extended.
3904 if (Depth
>= MAX_DEPTH
)
3907 assert(MI
.getOperand(1).isReg() && MI
.getOperand(2).isReg());
3909 Register SrcReg1
= MI
.getOperand(1).getReg();
3910 Register SrcReg2
= MI
.getOperand(2).getReg();
3912 if (!Register::isVirtualRegister(SrcReg1
) ||
3913 !Register::isVirtualRegister(SrcReg2
))
3916 const MachineInstr
*MISrc1
= MRI
->getVRegDef(SrcReg1
);
3917 const MachineInstr
*MISrc2
= MRI
->getVRegDef(SrcReg2
);
3918 if (!MISrc1
|| !MISrc2
)
3922 return isSignOrZeroExtended(*MISrc1
, SignExt
, Depth
+1) &&
3923 isSignOrZeroExtended(*MISrc2
, SignExt
, Depth
+1);
3925 return isSignOrZeroExtended(*MISrc1
, SignExt
, Depth
+1) ||
3926 isSignOrZeroExtended(*MISrc2
, SignExt
, Depth
+1);
3935 bool PPCInstrInfo::isBDNZ(unsigned Opcode
) const {
3936 return (Opcode
== (Subtarget
.isPPC64() ? PPC::BDNZ8
: PPC::BDNZ
));
3940 class PPCPipelinerLoopInfo
: public TargetInstrInfo::PipelinerLoopInfo
{
3941 MachineInstr
*Loop
, *EndLoop
, *LoopCount
;
3942 MachineFunction
*MF
;
3943 const TargetInstrInfo
*TII
;
3947 PPCPipelinerLoopInfo(MachineInstr
*Loop
, MachineInstr
*EndLoop
,
3948 MachineInstr
*LoopCount
)
3949 : Loop(Loop
), EndLoop(EndLoop
), LoopCount(LoopCount
),
3950 MF(Loop
->getParent()->getParent()),
3951 TII(MF
->getSubtarget().getInstrInfo()) {
3952 // Inspect the Loop instruction up-front, as it may be deleted when we call
3953 // createTripCountGreaterCondition.
3954 if (LoopCount
->getOpcode() == PPC::LI8
|| LoopCount
->getOpcode() == PPC::LI
)
3955 TripCount
= LoopCount
->getOperand(1).getImm();
3960 bool shouldIgnoreForPipelining(const MachineInstr
*MI
) const override
{
3961 // Only ignore the terminator.
3962 return MI
== EndLoop
;
3966 createTripCountGreaterCondition(int TC
, MachineBasicBlock
&MBB
,
3967 SmallVectorImpl
<MachineOperand
> &Cond
) override
{
3968 if (TripCount
== -1) {
3969 // Since BDZ/BDZ8 that we will insert will also decrease the ctr by 1,
3970 // so we don't need to generate any thing here.
3971 Cond
.push_back(MachineOperand::CreateImm(0));
3972 Cond
.push_back(MachineOperand::CreateReg(
3973 MF
->getSubtarget
<PPCSubtarget
>().isPPC64() ? PPC::CTR8
: PPC::CTR
,
3978 return TripCount
> TC
;
3981 void setPreheader(MachineBasicBlock
*NewPreheader
) override
{
3982 // Do nothing. We want the LOOP setup instruction to stay in the *old*
3983 // preheader, so we can use BDZ in the prologs to adapt the loop trip count.
3986 void adjustTripCount(int TripCountAdjust
) override
{
3987 // If the loop trip count is a compile-time value, then just change the
3989 if (LoopCount
->getOpcode() == PPC::LI8
||
3990 LoopCount
->getOpcode() == PPC::LI
) {
3991 int64_t TripCount
= LoopCount
->getOperand(1).getImm() + TripCountAdjust
;
3992 LoopCount
->getOperand(1).setImm(TripCount
);
3996 // Since BDZ/BDZ8 that we will insert will also decrease the ctr by 1,
3997 // so we don't need to generate any thing here.
4000 void disposed() override
{
4001 Loop
->eraseFromParent();
4002 // Ensure the loop setup instruction is deleted too.
4003 LoopCount
->eraseFromParent();
4008 std::unique_ptr
<TargetInstrInfo::PipelinerLoopInfo
>
4009 PPCInstrInfo::analyzeLoopForPipelining(MachineBasicBlock
*LoopBB
) const {
4010 // We really "analyze" only hardware loops right now.
4011 MachineBasicBlock::iterator I
= LoopBB
->getFirstTerminator();
4012 MachineBasicBlock
*Preheader
= *LoopBB
->pred_begin();
4013 if (Preheader
== LoopBB
)
4014 Preheader
= *std::next(LoopBB
->pred_begin());
4015 MachineFunction
*MF
= Preheader
->getParent();
4017 if (I
!= LoopBB
->end() && isBDNZ(I
->getOpcode())) {
4018 SmallPtrSet
<MachineBasicBlock
*, 8> Visited
;
4019 if (MachineInstr
*LoopInst
= findLoopInstr(*Preheader
, Visited
)) {
4020 Register LoopCountReg
= LoopInst
->getOperand(0).getReg();
4021 MachineRegisterInfo
&MRI
= MF
->getRegInfo();
4022 MachineInstr
*LoopCount
= MRI
.getUniqueVRegDef(LoopCountReg
);
4023 return std::make_unique
<PPCPipelinerLoopInfo
>(LoopInst
, &*I
, LoopCount
);
4029 MachineInstr
*PPCInstrInfo::findLoopInstr(
4030 MachineBasicBlock
&PreHeader
,
4031 SmallPtrSet
<MachineBasicBlock
*, 8> &Visited
) const {
4033 unsigned LOOPi
= (Subtarget
.isPPC64() ? PPC::MTCTR8loop
: PPC::MTCTRloop
);
4035 // The loop set-up instruction should be in preheader
4036 for (auto &I
: PreHeader
.instrs())
4037 if (I
.getOpcode() == LOOPi
)
4042 // Return true if get the base operand, byte offset of an instruction and the
4043 // memory width. Width is the size of memory that is being loaded/stored.
4044 bool PPCInstrInfo::getMemOperandWithOffsetWidth(
4045 const MachineInstr
&LdSt
,
4046 const MachineOperand
*&BaseReg
,
4049 const TargetRegisterInfo
*TRI
) const {
4050 assert(LdSt
.mayLoadOrStore() && "Expected a memory operation.");
4052 // Handle only loads/stores with base register followed by immediate offset.
4053 if (LdSt
.getNumExplicitOperands() != 3)
4055 if (!LdSt
.getOperand(1).isImm() || !LdSt
.getOperand(2).isReg())
4058 if (!LdSt
.hasOneMemOperand())
4061 Width
= (*LdSt
.memoperands_begin())->getSize();
4062 Offset
= LdSt
.getOperand(1).getImm();
4063 BaseReg
= &LdSt
.getOperand(2);
4067 bool PPCInstrInfo::areMemAccessesTriviallyDisjoint(
4068 const MachineInstr
&MIa
, const MachineInstr
&MIb
) const {
4069 assert(MIa
.mayLoadOrStore() && "MIa must be a load or store.");
4070 assert(MIb
.mayLoadOrStore() && "MIb must be a load or store.");
4072 if (MIa
.hasUnmodeledSideEffects() || MIb
.hasUnmodeledSideEffects() ||
4073 MIa
.hasOrderedMemoryRef() || MIb
.hasOrderedMemoryRef())
4076 // Retrieve the base register, offset from the base register and width. Width
4077 // is the size of memory that is being loaded/stored (e.g. 1, 2, 4). If
4078 // base registers are identical, and the offset of a lower memory access +
4079 // the width doesn't overlap the offset of a higher memory access,
4080 // then the memory accesses are different.
4081 const TargetRegisterInfo
*TRI
= &getRegisterInfo();
4082 const MachineOperand
*BaseOpA
= nullptr, *BaseOpB
= nullptr;
4083 int64_t OffsetA
= 0, OffsetB
= 0;
4084 unsigned int WidthA
= 0, WidthB
= 0;
4085 if (getMemOperandWithOffsetWidth(MIa
, BaseOpA
, OffsetA
, WidthA
, TRI
) &&
4086 getMemOperandWithOffsetWidth(MIb
, BaseOpB
, OffsetB
, WidthB
, TRI
)) {
4087 if (BaseOpA
->isIdenticalTo(*BaseOpB
)) {
4088 int LowOffset
= std::min(OffsetA
, OffsetB
);
4089 int HighOffset
= std::max(OffsetA
, OffsetB
);
4090 int LowWidth
= (LowOffset
== OffsetA
) ? WidthA
: WidthB
;
4091 if (LowOffset
+ LowWidth
<= HighOffset
)