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