1 //===-- PPCInstrInfo.cpp - PowerPC Instruction Information ----------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the PowerPC implementation of the TargetInstrInfo class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "PPCInstrInfo.h"
14 #include "MCTargetDesc/PPCPredicates.h"
15 #include "PPC.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/Analysis/AliasAnalysis.h"
23 #include "llvm/CodeGen/LiveIntervals.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineMemOperand.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/CodeGen/PseudoSourceValue.h"
30 #include "llvm/CodeGen/ScheduleDAG.h"
31 #include "llvm/CodeGen/SlotIndexes.h"
32 #include "llvm/CodeGen/StackMaps.h"
33 #include "llvm/MC/MCAsmInfo.h"
34 #include "llvm/MC/MCInst.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/TargetRegistry.h"
39 #include "llvm/Support/raw_ostream.h"
40 
41 using namespace llvm;
42 
43 #define DEBUG_TYPE "ppc-instr-info"
44 
45 #define GET_INSTRMAP_INFO
46 #define GET_INSTRINFO_CTOR_DTOR
47 #include "PPCGenInstrInfo.inc"
48 
49 STATISTIC(NumStoreSPILLVSRRCAsVec,
50           "Number of spillvsrrc spilled to stack as vec");
51 STATISTIC(NumStoreSPILLVSRRCAsGpr,
52           "Number of spillvsrrc spilled to stack as gpr");
53 STATISTIC(NumGPRtoVSRSpill, "Number of gpr spills to spillvsrrc");
54 STATISTIC(CmpIselsConverted,
55           "Number of ISELs that depend on comparison of constants converted");
56 STATISTIC(MissedConvertibleImmediateInstrs,
57           "Number of compare-immediate instructions fed by constants");
58 STATISTIC(NumRcRotatesConvertedToRcAnd,
59           "Number of record-form rotates converted to record-form andi");
60 
61 static cl::
62 opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
63             cl::desc("Disable analysis for CTR loops"));
64 
65 static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt",
66 cl::desc("Disable compare instruction optimization"), cl::Hidden);
67 
68 static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy",
69 cl::desc("Causes the backend to crash instead of generating a nop VSX copy"),
70 cl::Hidden);
71 
72 static cl::opt<bool>
73 UseOldLatencyCalc("ppc-old-latency-calc", cl::Hidden,
74   cl::desc("Use the old (incorrect) instruction latency calculation"));
75 
76 // Pin the vtable to this file.
77 void PPCInstrInfo::anchor() {}
78 
79 PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI)
80     : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP,
81                       /* CatchRetOpcode */ -1,
82                       STI.isPPC64() ? PPC::BLR8 : PPC::BLR),
83       Subtarget(STI), RI(STI.getTargetMachine()) {}
84 
85 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
86 /// this target when scheduling the DAG.
87 ScheduleHazardRecognizer *
88 PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
89                                            const ScheduleDAG *DAG) const {
90   unsigned Directive =
91       static_cast<const PPCSubtarget *>(STI)->getCPUDirective();
92   if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
93       Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
94     const InstrItineraryData *II =
95         static_cast<const PPCSubtarget *>(STI)->getInstrItineraryData();
96     return new ScoreboardHazardRecognizer(II, DAG);
97   }
98 
99   return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
100 }
101 
102 /// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
103 /// to use for this target when scheduling the DAG.
104 ScheduleHazardRecognizer *
105 PPCInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
106                                                  const ScheduleDAG *DAG) const {
107   unsigned Directive =
108       DAG->MF.getSubtarget<PPCSubtarget>().getCPUDirective();
109 
110   // FIXME: Leaving this as-is until we have POWER9 scheduling info
111   if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8)
112     return new PPCDispatchGroupSBHazardRecognizer(II, DAG);
113 
114   // Most subtargets use a PPC970 recognizer.
115   if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
116       Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
117     assert(DAG->TII && "No InstrInfo?");
118 
119     return new PPCHazardRecognizer970(*DAG);
120   }
121 
122   return new ScoreboardHazardRecognizer(II, DAG);
123 }
124 
125 unsigned PPCInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
126                                        const MachineInstr &MI,
127                                        unsigned *PredCost) const {
128   if (!ItinData || UseOldLatencyCalc)
129     return PPCGenInstrInfo::getInstrLatency(ItinData, MI, PredCost);
130 
131   // The default implementation of getInstrLatency calls getStageLatency, but
132   // getStageLatency does not do the right thing for us. While we have
133   // itinerary, most cores are fully pipelined, and so the itineraries only
134   // express the first part of the pipeline, not every stage. Instead, we need
135   // to use the listed output operand cycle number (using operand 0 here, which
136   // is an output).
137 
138   unsigned Latency = 1;
139   unsigned DefClass = MI.getDesc().getSchedClass();
140   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
141     const MachineOperand &MO = MI.getOperand(i);
142     if (!MO.isReg() || !MO.isDef() || MO.isImplicit())
143       continue;
144 
145     int Cycle = ItinData->getOperandCycle(DefClass, i);
146     if (Cycle < 0)
147       continue;
148 
149     Latency = std::max(Latency, (unsigned) Cycle);
150   }
151 
152   return Latency;
153 }
154 
155 int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
156                                     const MachineInstr &DefMI, unsigned DefIdx,
157                                     const MachineInstr &UseMI,
158                                     unsigned UseIdx) const {
159   int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
160                                                    UseMI, UseIdx);
161 
162   if (!DefMI.getParent())
163     return Latency;
164 
165   const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
166   Register Reg = DefMO.getReg();
167 
168   bool IsRegCR;
169   if (Register::isVirtualRegister(Reg)) {
170     const MachineRegisterInfo *MRI =
171         &DefMI.getParent()->getParent()->getRegInfo();
172     IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) ||
173               MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass);
174   } else {
175     IsRegCR = PPC::CRRCRegClass.contains(Reg) ||
176               PPC::CRBITRCRegClass.contains(Reg);
177   }
178 
179   if (UseMI.isBranch() && IsRegCR) {
180     if (Latency < 0)
181       Latency = getInstrLatency(ItinData, DefMI);
182 
183     // On some cores, there is an additional delay between writing to a condition
184     // register, and using it from a branch.
185     unsigned Directive = Subtarget.getCPUDirective();
186     switch (Directive) {
187     default: break;
188     case PPC::DIR_7400:
189     case PPC::DIR_750:
190     case PPC::DIR_970:
191     case PPC::DIR_E5500:
192     case PPC::DIR_PWR4:
193     case PPC::DIR_PWR5:
194     case PPC::DIR_PWR5X:
195     case PPC::DIR_PWR6:
196     case PPC::DIR_PWR6X:
197     case PPC::DIR_PWR7:
198     case PPC::DIR_PWR8:
199     // FIXME: Is this needed for POWER9?
200       Latency += 2;
201       break;
202     }
203   }
204 
205   return Latency;
206 }
207 
208 /// This is an architecture-specific helper function of reassociateOps.
209 /// Set special operand attributes for new instructions after reassociation.
210 void PPCInstrInfo::setSpecialOperandAttr(MachineInstr &OldMI1,
211                                          MachineInstr &OldMI2,
212                                          MachineInstr &NewMI1,
213                                          MachineInstr &NewMI2) const {
214   // Propagate FP flags from the original instructions.
215   // But clear poison-generating flags because those may not be valid now.
216   uint16_t IntersectedFlags = OldMI1.getFlags() & OldMI2.getFlags();
217   NewMI1.setFlags(IntersectedFlags);
218   NewMI1.clearFlag(MachineInstr::MIFlag::NoSWrap);
219   NewMI1.clearFlag(MachineInstr::MIFlag::NoUWrap);
220   NewMI1.clearFlag(MachineInstr::MIFlag::IsExact);
221 
222   NewMI2.setFlags(IntersectedFlags);
223   NewMI2.clearFlag(MachineInstr::MIFlag::NoSWrap);
224   NewMI2.clearFlag(MachineInstr::MIFlag::NoUWrap);
225   NewMI2.clearFlag(MachineInstr::MIFlag::IsExact);
226 }
227 
228 void PPCInstrInfo::setSpecialOperandAttr(MachineInstr &MI,
229                                          uint16_t Flags) const {
230   MI.setFlags(Flags);
231   MI.clearFlag(MachineInstr::MIFlag::NoSWrap);
232   MI.clearFlag(MachineInstr::MIFlag::NoUWrap);
233   MI.clearFlag(MachineInstr::MIFlag::IsExact);
234 }
235 
236 // This function does not list all associative and commutative operations, but
237 // only those worth feeding through the machine combiner in an attempt to
238 // reduce the critical path. Mostly, this means floating-point operations,
239 // because they have high latencies(>=5) (compared to other operations, such as
240 // and/or, which are also associative and commutative, but have low latencies).
241 bool PPCInstrInfo::isAssociativeAndCommutative(const MachineInstr &Inst) const {
242   switch (Inst.getOpcode()) {
243   // Floating point:
244   // FP Add:
245   case PPC::FADD:
246   case PPC::FADDS:
247   // FP Multiply:
248   case PPC::FMUL:
249   case PPC::FMULS:
250   // Altivec Add:
251   case PPC::VADDFP:
252   // VSX Add:
253   case PPC::XSADDDP:
254   case PPC::XVADDDP:
255   case PPC::XVADDSP:
256   case PPC::XSADDSP:
257   // VSX Multiply:
258   case PPC::XSMULDP:
259   case PPC::XVMULDP:
260   case PPC::XVMULSP:
261   case PPC::XSMULSP:
262     return Inst.getFlag(MachineInstr::MIFlag::FmReassoc) &&
263            Inst.getFlag(MachineInstr::MIFlag::FmNsz);
264   // Fixed point:
265   // Multiply:
266   case PPC::MULHD:
267   case PPC::MULLD:
268   case PPC::MULHW:
269   case PPC::MULLW:
270     return true;
271   default:
272     return false;
273   }
274 }
275 
276 #define InfoArrayIdxFMAInst 0
277 #define InfoArrayIdxFAddInst 1
278 #define InfoArrayIdxFMULInst 2
279 #define InfoArrayIdxAddOpIdx 3
280 #define InfoArrayIdxMULOpIdx 4
281 // Array keeps info for FMA instructions:
282 // Index 0(InfoArrayIdxFMAInst): FMA instruction;
283 // Index 1(InfoArrayIdxFAddInst): ADD instruction assoaicted with FMA;
284 // Index 2(InfoArrayIdxFMULInst): MUL instruction assoaicted with FMA;
285 // Index 3(InfoArrayIdxAddOpIdx): ADD operand index in FMA operands;
286 // Index 4(InfoArrayIdxMULOpIdx): first MUL operand index in FMA operands;
287 //                                second MUL operand index is plus 1.
288 static const uint16_t FMAOpIdxInfo[][5] = {
289     // FIXME: Add more FMA instructions like XSNMADDADP and so on.
290     {PPC::XSMADDADP, PPC::XSADDDP, PPC::XSMULDP, 1, 2},
291     {PPC::XSMADDASP, PPC::XSADDSP, PPC::XSMULSP, 1, 2},
292     {PPC::XVMADDADP, PPC::XVADDDP, PPC::XVMULDP, 1, 2},
293     {PPC::XVMADDASP, PPC::XVADDSP, PPC::XVMULSP, 1, 2},
294     {PPC::FMADD, PPC::FADD, PPC::FMUL, 3, 1},
295     {PPC::FMADDS, PPC::FADDS, PPC::FMULS, 3, 1}};
296 
297 // Check if an opcode is a FMA instruction. If it is, return the index in array
298 // FMAOpIdxInfo. Otherwise, return -1.
299 int16_t PPCInstrInfo::getFMAOpIdxInfo(unsigned Opcode) const {
300   for (unsigned I = 0; I < array_lengthof(FMAOpIdxInfo); I++)
301     if (FMAOpIdxInfo[I][InfoArrayIdxFMAInst] == Opcode)
302       return I;
303   return -1;
304 }
305 
306 // Try to reassociate FMA chains like below:
307 //
308 // Pattern 1:
309 //   A =  FADD X,  Y          (Leaf)
310 //   B =  FMA  A,  M21,  M22  (Prev)
311 //   C =  FMA  B,  M31,  M32  (Root)
312 // -->
313 //   A =  FMA  X,  M21,  M22
314 //   B =  FMA  Y,  M31,  M32
315 //   C =  FADD A,  B
316 //
317 // Pattern 2:
318 //   A =  FMA  X,  M11,  M12  (Leaf)
319 //   B =  FMA  A,  M21,  M22  (Prev)
320 //   C =  FMA  B,  M31,  M32  (Root)
321 // -->
322 //   A =  FMUL M11,  M12
323 //   B =  FMA  X,  M21,  M22
324 //   D =  FMA  A,  M31,  M32
325 //   C =  FADD B,  D
326 //
327 // breaking the dependency between A and B, allowing FMA to be executed in
328 // parallel (or back-to-back in a pipeline) instead of depending on each other.
329 bool PPCInstrInfo::getFMAPatterns(
330     MachineInstr &Root,
331     SmallVectorImpl<MachineCombinerPattern> &Patterns) const {
332   MachineBasicBlock *MBB = Root.getParent();
333   const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
334 
335   auto IsAllOpsVirtualReg = [](const MachineInstr &Instr) {
336     for (const auto &MO : Instr.explicit_operands())
337       if (!(MO.isReg() && Register::isVirtualRegister(MO.getReg())))
338         return false;
339     return true;
340   };
341 
342   auto IsReassociable = [&](const MachineInstr &Instr, int16_t &AddOpIdx,
343                             bool IsLeaf, bool IsAdd) {
344     int16_t Idx = -1;
345     if (!IsAdd) {
346       Idx = getFMAOpIdxInfo(Instr.getOpcode());
347       if (Idx < 0)
348         return false;
349     } else if (Instr.getOpcode() !=
350                FMAOpIdxInfo[getFMAOpIdxInfo(Root.getOpcode())]
351                            [InfoArrayIdxFAddInst])
352       return false;
353 
354     // Instruction can be reassociated.
355     // fast math flags may prohibit reassociation.
356     if (!(Instr.getFlag(MachineInstr::MIFlag::FmReassoc) &&
357           Instr.getFlag(MachineInstr::MIFlag::FmNsz)))
358       return false;
359 
360     // Instruction operands are virtual registers for reassociation.
361     if (!IsAllOpsVirtualReg(Instr))
362       return false;
363 
364     if (IsAdd && IsLeaf)
365       return true;
366 
367     AddOpIdx = FMAOpIdxInfo[Idx][InfoArrayIdxAddOpIdx];
368 
369     const MachineOperand &OpAdd = Instr.getOperand(AddOpIdx);
370     MachineInstr *MIAdd = MRI.getUniqueVRegDef(OpAdd.getReg());
371     // If 'add' operand's def is not in current block, don't do ILP related opt.
372     if (!MIAdd || MIAdd->getParent() != MBB)
373       return false;
374 
375     // If this is not Leaf FMA Instr, its 'add' operand should only have one use
376     // as this fma will be changed later.
377     return IsLeaf ? true : MRI.hasOneNonDBGUse(OpAdd.getReg());
378   };
379 
380   int16_t AddOpIdx = -1;
381   // Root must be a valid FMA like instruction.
382   if (!IsReassociable(Root, AddOpIdx, false, false))
383     return false;
384 
385   assert((AddOpIdx >= 0) && "add operand index not right!");
386 
387   Register RegB = Root.getOperand(AddOpIdx).getReg();
388   MachineInstr *Prev = MRI.getUniqueVRegDef(RegB);
389 
390   // Prev must be a valid FMA like instruction.
391   AddOpIdx = -1;
392   if (!IsReassociable(*Prev, AddOpIdx, false, false))
393     return false;
394 
395   assert((AddOpIdx >= 0) && "add operand index not right!");
396 
397   Register RegA = Prev->getOperand(AddOpIdx).getReg();
398   MachineInstr *Leaf = MRI.getUniqueVRegDef(RegA);
399   AddOpIdx = -1;
400   if (IsReassociable(*Leaf, AddOpIdx, true, false)) {
401     Patterns.push_back(MachineCombinerPattern::REASSOC_XMM_AMM_BMM);
402     return true;
403   }
404   if (IsReassociable(*Leaf, AddOpIdx, true, true)) {
405     Patterns.push_back(MachineCombinerPattern::REASSOC_XY_AMM_BMM);
406     return true;
407   }
408   return false;
409 }
410 
411 bool PPCInstrInfo::getMachineCombinerPatterns(
412     MachineInstr &Root,
413     SmallVectorImpl<MachineCombinerPattern> &Patterns) const {
414   // Using the machine combiner in this way is potentially expensive, so
415   // restrict to when aggressive optimizations are desired.
416   if (Subtarget.getTargetMachine().getOptLevel() != CodeGenOpt::Aggressive)
417     return false;
418 
419   if (getFMAPatterns(Root, Patterns))
420     return true;
421 
422   return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns);
423 }
424 
425 void PPCInstrInfo::genAlternativeCodeSequence(
426     MachineInstr &Root, MachineCombinerPattern Pattern,
427     SmallVectorImpl<MachineInstr *> &InsInstrs,
428     SmallVectorImpl<MachineInstr *> &DelInstrs,
429     DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const {
430   switch (Pattern) {
431   case MachineCombinerPattern::REASSOC_XY_AMM_BMM:
432   case MachineCombinerPattern::REASSOC_XMM_AMM_BMM:
433     reassociateFMA(Root, Pattern, InsInstrs, DelInstrs, InstrIdxForVirtReg);
434     break;
435   default:
436     // Reassociate default patterns.
437     TargetInstrInfo::genAlternativeCodeSequence(Root, Pattern, InsInstrs,
438                                                 DelInstrs, InstrIdxForVirtReg);
439     break;
440   }
441 }
442 
443 // Currently, only handle two patterns REASSOC_XY_AMM_BMM and
444 // REASSOC_XMM_AMM_BMM. See comments for getFMAPatterns.
445 void PPCInstrInfo::reassociateFMA(
446     MachineInstr &Root, MachineCombinerPattern Pattern,
447     SmallVectorImpl<MachineInstr *> &InsInstrs,
448     SmallVectorImpl<MachineInstr *> &DelInstrs,
449     DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const {
450   MachineFunction *MF = Root.getMF();
451   MachineRegisterInfo &MRI = MF->getRegInfo();
452   MachineOperand &OpC = Root.getOperand(0);
453   Register RegC = OpC.getReg();
454   const TargetRegisterClass *RC = MRI.getRegClass(RegC);
455   MRI.constrainRegClass(RegC, RC);
456 
457   unsigned FmaOp = Root.getOpcode();
458   int16_t Idx = getFMAOpIdxInfo(FmaOp);
459   assert(Idx >= 0 && "Root must be a FMA instruction");
460 
461   uint16_t AddOpIdx = FMAOpIdxInfo[Idx][InfoArrayIdxAddOpIdx];
462   uint16_t FirstMulOpIdx = FMAOpIdxInfo[Idx][InfoArrayIdxMULOpIdx];
463   MachineInstr *Prev = MRI.getUniqueVRegDef(Root.getOperand(AddOpIdx).getReg());
464   MachineInstr *Leaf =
465       MRI.getUniqueVRegDef(Prev->getOperand(AddOpIdx).getReg());
466   uint16_t IntersectedFlags =
467       Root.getFlags() & Prev->getFlags() & Leaf->getFlags();
468 
469   auto GetOperandInfo = [&](const MachineOperand &Operand, Register &Reg,
470                             bool &KillFlag) {
471     Reg = Operand.getReg();
472     MRI.constrainRegClass(Reg, RC);
473     KillFlag = Operand.isKill();
474   };
475 
476   auto GetFMAInstrInfo = [&](const MachineInstr &Instr, Register &MulOp1,
477                              Register &MulOp2, bool &MulOp1KillFlag,
478                              bool &MulOp2KillFlag) {
479     GetOperandInfo(Instr.getOperand(FirstMulOpIdx), MulOp1, MulOp1KillFlag);
480     GetOperandInfo(Instr.getOperand(FirstMulOpIdx + 1), MulOp2, MulOp2KillFlag);
481   };
482 
483   Register RegM11, RegM12, RegX, RegY, RegM21, RegM22, RegM31, RegM32;
484   bool KillX = false, KillY = false, KillM11 = false, KillM12 = false,
485        KillM21 = false, KillM22 = false, KillM31 = false, KillM32 = false;
486 
487   GetFMAInstrInfo(Root, RegM31, RegM32, KillM31, KillM32);
488   GetFMAInstrInfo(*Prev, RegM21, RegM22, KillM21, KillM22);
489 
490   if (Pattern == MachineCombinerPattern::REASSOC_XMM_AMM_BMM) {
491     GetFMAInstrInfo(*Leaf, RegM11, RegM12, KillM11, KillM12);
492     GetOperandInfo(Leaf->getOperand(AddOpIdx), RegX, KillX);
493   } else if (Pattern == MachineCombinerPattern::REASSOC_XY_AMM_BMM) {
494     GetOperandInfo(Leaf->getOperand(1), RegX, KillX);
495     GetOperandInfo(Leaf->getOperand(2), RegY, KillY);
496   }
497 
498   // Create new virtual registers for the new results instead of
499   // recycling legacy ones because the MachineCombiner's computation of the
500   // critical path requires a new register definition rather than an existing
501   // one.
502   Register NewVRA = MRI.createVirtualRegister(RC);
503   InstrIdxForVirtReg.insert(std::make_pair(NewVRA, 0));
504 
505   Register NewVRB = MRI.createVirtualRegister(RC);
506   InstrIdxForVirtReg.insert(std::make_pair(NewVRB, 1));
507 
508   Register NewVRD = 0;
509   if (Pattern == MachineCombinerPattern::REASSOC_XMM_AMM_BMM) {
510     NewVRD = MRI.createVirtualRegister(RC);
511     InstrIdxForVirtReg.insert(std::make_pair(NewVRD, 2));
512   }
513 
514   auto AdjustOperandOrder = [&](MachineInstr *MI, Register RegAdd, bool KillAdd,
515                                 Register RegMul1, bool KillRegMul1,
516                                 Register RegMul2, bool KillRegMul2) {
517     MI->getOperand(AddOpIdx).setReg(RegAdd);
518     MI->getOperand(AddOpIdx).setIsKill(KillAdd);
519     MI->getOperand(FirstMulOpIdx).setReg(RegMul1);
520     MI->getOperand(FirstMulOpIdx).setIsKill(KillRegMul1);
521     MI->getOperand(FirstMulOpIdx + 1).setReg(RegMul2);
522     MI->getOperand(FirstMulOpIdx + 1).setIsKill(KillRegMul2);
523   };
524 
525   if (Pattern == MachineCombinerPattern::REASSOC_XY_AMM_BMM) {
526     // Create new instructions for insertion.
527     MachineInstrBuilder MINewB =
528         BuildMI(*MF, Prev->getDebugLoc(), get(FmaOp), NewVRB)
529             .addReg(RegX, getKillRegState(KillX))
530             .addReg(RegM21, getKillRegState(KillM21))
531             .addReg(RegM22, getKillRegState(KillM22));
532     MachineInstrBuilder MINewA =
533         BuildMI(*MF, Root.getDebugLoc(), get(FmaOp), NewVRA)
534             .addReg(RegY, getKillRegState(KillY))
535             .addReg(RegM31, getKillRegState(KillM31))
536             .addReg(RegM32, getKillRegState(KillM32));
537     // If AddOpIdx is not 1, adjust the order.
538     if (AddOpIdx != 1) {
539       AdjustOperandOrder(MINewB, RegX, KillX, RegM21, KillM21, RegM22, KillM22);
540       AdjustOperandOrder(MINewA, RegY, KillY, RegM31, KillM31, RegM32, KillM32);
541     }
542 
543     MachineInstrBuilder MINewC =
544         BuildMI(*MF, Root.getDebugLoc(),
545                 get(FMAOpIdxInfo[Idx][InfoArrayIdxFAddInst]), RegC)
546             .addReg(NewVRB, getKillRegState(true))
547             .addReg(NewVRA, getKillRegState(true));
548 
549     // Update flags for newly created instructions.
550     setSpecialOperandAttr(*MINewA, IntersectedFlags);
551     setSpecialOperandAttr(*MINewB, IntersectedFlags);
552     setSpecialOperandAttr(*MINewC, IntersectedFlags);
553 
554     // Record new instructions for insertion.
555     InsInstrs.push_back(MINewA);
556     InsInstrs.push_back(MINewB);
557     InsInstrs.push_back(MINewC);
558   } else if (Pattern == MachineCombinerPattern::REASSOC_XMM_AMM_BMM) {
559     assert(NewVRD && "new FMA register not created!");
560     // Create new instructions for insertion.
561     MachineInstrBuilder MINewA =
562         BuildMI(*MF, Leaf->getDebugLoc(),
563                 get(FMAOpIdxInfo[Idx][InfoArrayIdxFMULInst]), NewVRA)
564             .addReg(RegM11, getKillRegState(KillM11))
565             .addReg(RegM12, getKillRegState(KillM12));
566     MachineInstrBuilder MINewB =
567         BuildMI(*MF, Prev->getDebugLoc(), get(FmaOp), NewVRB)
568             .addReg(RegX, getKillRegState(KillX))
569             .addReg(RegM21, getKillRegState(KillM21))
570             .addReg(RegM22, getKillRegState(KillM22));
571     MachineInstrBuilder MINewD =
572         BuildMI(*MF, Root.getDebugLoc(), get(FmaOp), NewVRD)
573             .addReg(NewVRA, getKillRegState(true))
574             .addReg(RegM31, getKillRegState(KillM31))
575             .addReg(RegM32, getKillRegState(KillM32));
576     // If AddOpIdx is not 1, adjust the order.
577     if (AddOpIdx != 1) {
578       AdjustOperandOrder(MINewB, RegX, KillX, RegM21, KillM21, RegM22, KillM22);
579       AdjustOperandOrder(MINewD, NewVRA, true, RegM31, KillM31, RegM32,
580                          KillM32);
581     }
582 
583     MachineInstrBuilder MINewC =
584         BuildMI(*MF, Root.getDebugLoc(),
585                 get(FMAOpIdxInfo[Idx][InfoArrayIdxFAddInst]), RegC)
586             .addReg(NewVRB, getKillRegState(true))
587             .addReg(NewVRD, getKillRegState(true));
588 
589     // Update flags for newly created instructions.
590     setSpecialOperandAttr(*MINewA, IntersectedFlags);
591     setSpecialOperandAttr(*MINewB, IntersectedFlags);
592     setSpecialOperandAttr(*MINewD, IntersectedFlags);
593     setSpecialOperandAttr(*MINewC, IntersectedFlags);
594 
595     // Record new instructions for insertion.
596     InsInstrs.push_back(MINewA);
597     InsInstrs.push_back(MINewB);
598     InsInstrs.push_back(MINewD);
599     InsInstrs.push_back(MINewC);
600   }
601 
602   assert(!InsInstrs.empty() &&
603          "Insertion instructions set should not be empty!");
604 
605   // Record old instructions for deletion.
606   DelInstrs.push_back(Leaf);
607   DelInstrs.push_back(Prev);
608   DelInstrs.push_back(&Root);
609 }
610 
611 // Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
612 bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
613                                          Register &SrcReg, Register &DstReg,
614                                          unsigned &SubIdx) const {
615   switch (MI.getOpcode()) {
616   default: return false;
617   case PPC::EXTSW:
618   case PPC::EXTSW_32:
619   case PPC::EXTSW_32_64:
620     SrcReg = MI.getOperand(1).getReg();
621     DstReg = MI.getOperand(0).getReg();
622     SubIdx = PPC::sub_32;
623     return true;
624   }
625 }
626 
627 unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
628                                            int &FrameIndex) const {
629   unsigned Opcode = MI.getOpcode();
630   const unsigned *OpcodesForSpill = getLoadOpcodesForSpillArray();
631   const unsigned *End = OpcodesForSpill + SOK_LastOpcodeSpill;
632 
633   if (End != std::find(OpcodesForSpill, End, Opcode)) {
634     // Check for the operands added by addFrameReference (the immediate is the
635     // offset which defaults to 0).
636     if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() &&
637         MI.getOperand(2).isFI()) {
638       FrameIndex = MI.getOperand(2).getIndex();
639       return MI.getOperand(0).getReg();
640     }
641   }
642   return 0;
643 }
644 
645 // For opcodes with the ReMaterializable flag set, this function is called to
646 // verify the instruction is really rematable.
647 bool PPCInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr &MI,
648                                                      AliasAnalysis *AA) const {
649   switch (MI.getOpcode()) {
650   default:
651     // This function should only be called for opcodes with the ReMaterializable
652     // flag set.
653     llvm_unreachable("Unknown rematerializable operation!");
654     break;
655   case PPC::LI:
656   case PPC::LI8:
657   case PPC::LIS:
658   case PPC::LIS8:
659   case PPC::ADDIStocHA:
660   case PPC::ADDIStocHA8:
661   case PPC::ADDItocL:
662   case PPC::LOAD_STACK_GUARD:
663   case PPC::XXLXORz:
664   case PPC::XXLXORspz:
665   case PPC::XXLXORdpz:
666   case PPC::XXLEQVOnes:
667   case PPC::V_SET0B:
668   case PPC::V_SET0H:
669   case PPC::V_SET0:
670   case PPC::V_SETALLONESB:
671   case PPC::V_SETALLONESH:
672   case PPC::V_SETALLONES:
673   case PPC::CRSET:
674   case PPC::CRUNSET:
675     return true;
676   }
677   return false;
678 }
679 
680 unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
681                                           int &FrameIndex) const {
682   unsigned Opcode = MI.getOpcode();
683   const unsigned *OpcodesForSpill = getStoreOpcodesForSpillArray();
684   const unsigned *End = OpcodesForSpill + SOK_LastOpcodeSpill;
685 
686   if (End != std::find(OpcodesForSpill, End, Opcode)) {
687     if (MI.getOperand(1).isImm() && !MI.getOperand(1).getImm() &&
688         MI.getOperand(2).isFI()) {
689       FrameIndex = MI.getOperand(2).getIndex();
690       return MI.getOperand(0).getReg();
691     }
692   }
693   return 0;
694 }
695 
696 MachineInstr *PPCInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI,
697                                                    unsigned OpIdx1,
698                                                    unsigned OpIdx2) const {
699   MachineFunction &MF = *MI.getParent()->getParent();
700 
701   // Normal instructions can be commuted the obvious way.
702   if (MI.getOpcode() != PPC::RLWIMI && MI.getOpcode() != PPC::RLWIMI_rec)
703     return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
704   // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a
705   // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because
706   // changing the relative order of the mask operands might change what happens
707   // to the high-bits of the mask (and, thus, the result).
708 
709   // Cannot commute if it has a non-zero rotate count.
710   if (MI.getOperand(3).getImm() != 0)
711     return nullptr;
712 
713   // If we have a zero rotate count, we have:
714   //   M = mask(MB,ME)
715   //   Op0 = (Op1 & ~M) | (Op2 & M)
716   // Change this to:
717   //   M = mask((ME+1)&31, (MB-1)&31)
718   //   Op0 = (Op2 & ~M) | (Op1 & M)
719 
720   // Swap op1/op2
721   assert(((OpIdx1 == 1 && OpIdx2 == 2) || (OpIdx1 == 2 && OpIdx2 == 1)) &&
722          "Only the operands 1 and 2 can be swapped in RLSIMI/RLWIMI_rec.");
723   Register Reg0 = MI.getOperand(0).getReg();
724   Register Reg1 = MI.getOperand(1).getReg();
725   Register Reg2 = MI.getOperand(2).getReg();
726   unsigned SubReg1 = MI.getOperand(1).getSubReg();
727   unsigned SubReg2 = MI.getOperand(2).getSubReg();
728   bool Reg1IsKill = MI.getOperand(1).isKill();
729   bool Reg2IsKill = MI.getOperand(2).isKill();
730   bool ChangeReg0 = false;
731   // If machine instrs are no longer in two-address forms, update
732   // destination register as well.
733   if (Reg0 == Reg1) {
734     // Must be two address instruction!
735     assert(MI.getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
736            "Expecting a two-address instruction!");
737     assert(MI.getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
738     Reg2IsKill = false;
739     ChangeReg0 = true;
740   }
741 
742   // Masks.
743   unsigned MB = MI.getOperand(4).getImm();
744   unsigned ME = MI.getOperand(5).getImm();
745 
746   // We can't commute a trivial mask (there is no way to represent an all-zero
747   // mask).
748   if (MB == 0 && ME == 31)
749     return nullptr;
750 
751   if (NewMI) {
752     // Create a new instruction.
753     Register Reg0 = ChangeReg0 ? Reg2 : MI.getOperand(0).getReg();
754     bool Reg0IsDead = MI.getOperand(0).isDead();
755     return BuildMI(MF, MI.getDebugLoc(), MI.getDesc())
756         .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
757         .addReg(Reg2, getKillRegState(Reg2IsKill))
758         .addReg(Reg1, getKillRegState(Reg1IsKill))
759         .addImm((ME + 1) & 31)
760         .addImm((MB - 1) & 31);
761   }
762 
763   if (ChangeReg0) {
764     MI.getOperand(0).setReg(Reg2);
765     MI.getOperand(0).setSubReg(SubReg2);
766   }
767   MI.getOperand(2).setReg(Reg1);
768   MI.getOperand(1).setReg(Reg2);
769   MI.getOperand(2).setSubReg(SubReg1);
770   MI.getOperand(1).setSubReg(SubReg2);
771   MI.getOperand(2).setIsKill(Reg1IsKill);
772   MI.getOperand(1).setIsKill(Reg2IsKill);
773 
774   // Swap the mask around.
775   MI.getOperand(4).setImm((ME + 1) & 31);
776   MI.getOperand(5).setImm((MB - 1) & 31);
777   return &MI;
778 }
779 
780 bool PPCInstrInfo::findCommutedOpIndices(const MachineInstr &MI,
781                                          unsigned &SrcOpIdx1,
782                                          unsigned &SrcOpIdx2) const {
783   // For VSX A-Type FMA instructions, it is the first two operands that can be
784   // commuted, however, because the non-encoded tied input operand is listed
785   // first, the operands to swap are actually the second and third.
786 
787   int AltOpc = PPC::getAltVSXFMAOpcode(MI.getOpcode());
788   if (AltOpc == -1)
789     return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
790 
791   // The commutable operand indices are 2 and 3. Return them in SrcOpIdx1
792   // and SrcOpIdx2.
793   return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2, 3);
794 }
795 
796 void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
797                               MachineBasicBlock::iterator MI) const {
798   // This function is used for scheduling, and the nop wanted here is the type
799   // that terminates dispatch groups on the POWER cores.
800   unsigned Directive = Subtarget.getCPUDirective();
801   unsigned Opcode;
802   switch (Directive) {
803   default:            Opcode = PPC::NOP; break;
804   case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
805   case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
806   case PPC::DIR_PWR8: Opcode = PPC::NOP_GT_PWR7; break; /* FIXME: Update when P8 InstrScheduling model is ready */
807   // FIXME: Update when POWER9 scheduling model is ready.
808   case PPC::DIR_PWR9: Opcode = PPC::NOP_GT_PWR7; break;
809   }
810 
811   DebugLoc DL;
812   BuildMI(MBB, MI, DL, get(Opcode));
813 }
814 
815 /// Return the noop instruction to use for a noop.
816 void PPCInstrInfo::getNoop(MCInst &NopInst) const {
817   NopInst.setOpcode(PPC::NOP);
818 }
819 
820 // Branch analysis.
821 // Note: If the condition register is set to CTR or CTR8 then this is a
822 // BDNZ (imm == 1) or BDZ (imm == 0) branch.
823 bool PPCInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
824                                  MachineBasicBlock *&TBB,
825                                  MachineBasicBlock *&FBB,
826                                  SmallVectorImpl<MachineOperand> &Cond,
827                                  bool AllowModify) const {
828   bool isPPC64 = Subtarget.isPPC64();
829 
830   // If the block has no terminators, it just falls into the block after it.
831   MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
832   if (I == MBB.end())
833     return false;
834 
835   if (!isUnpredicatedTerminator(*I))
836     return false;
837 
838   if (AllowModify) {
839     // If the BB ends with an unconditional branch to the fallthrough BB,
840     // we eliminate the branch instruction.
841     if (I->getOpcode() == PPC::B &&
842         MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
843       I->eraseFromParent();
844 
845       // We update iterator after deleting the last branch.
846       I = MBB.getLastNonDebugInstr();
847       if (I == MBB.end() || !isUnpredicatedTerminator(*I))
848         return false;
849     }
850   }
851 
852   // Get the last instruction in the block.
853   MachineInstr &LastInst = *I;
854 
855   // If there is only one terminator instruction, process it.
856   if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
857     if (LastInst.getOpcode() == PPC::B) {
858       if (!LastInst.getOperand(0).isMBB())
859         return true;
860       TBB = LastInst.getOperand(0).getMBB();
861       return false;
862     } else if (LastInst.getOpcode() == PPC::BCC) {
863       if (!LastInst.getOperand(2).isMBB())
864         return true;
865       // Block ends with fall-through condbranch.
866       TBB = LastInst.getOperand(2).getMBB();
867       Cond.push_back(LastInst.getOperand(0));
868       Cond.push_back(LastInst.getOperand(1));
869       return false;
870     } else if (LastInst.getOpcode() == PPC::BC) {
871       if (!LastInst.getOperand(1).isMBB())
872         return true;
873       // Block ends with fall-through condbranch.
874       TBB = LastInst.getOperand(1).getMBB();
875       Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
876       Cond.push_back(LastInst.getOperand(0));
877       return false;
878     } else if (LastInst.getOpcode() == PPC::BCn) {
879       if (!LastInst.getOperand(1).isMBB())
880         return true;
881       // Block ends with fall-through condbranch.
882       TBB = LastInst.getOperand(1).getMBB();
883       Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
884       Cond.push_back(LastInst.getOperand(0));
885       return false;
886     } else if (LastInst.getOpcode() == PPC::BDNZ8 ||
887                LastInst.getOpcode() == PPC::BDNZ) {
888       if (!LastInst.getOperand(0).isMBB())
889         return true;
890       if (DisableCTRLoopAnal)
891         return true;
892       TBB = LastInst.getOperand(0).getMBB();
893       Cond.push_back(MachineOperand::CreateImm(1));
894       Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
895                                                true));
896       return false;
897     } else if (LastInst.getOpcode() == PPC::BDZ8 ||
898                LastInst.getOpcode() == PPC::BDZ) {
899       if (!LastInst.getOperand(0).isMBB())
900         return true;
901       if (DisableCTRLoopAnal)
902         return true;
903       TBB = LastInst.getOperand(0).getMBB();
904       Cond.push_back(MachineOperand::CreateImm(0));
905       Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
906                                                true));
907       return false;
908     }
909 
910     // Otherwise, don't know what this is.
911     return true;
912   }
913 
914   // Get the instruction before it if it's a terminator.
915   MachineInstr &SecondLastInst = *I;
916 
917   // If there are three terminators, we don't know what sort of block this is.
918   if (I != MBB.begin() && isUnpredicatedTerminator(*--I))
919     return true;
920 
921   // If the block ends with PPC::B and PPC:BCC, handle it.
922   if (SecondLastInst.getOpcode() == PPC::BCC &&
923       LastInst.getOpcode() == PPC::B) {
924     if (!SecondLastInst.getOperand(2).isMBB() ||
925         !LastInst.getOperand(0).isMBB())
926       return true;
927     TBB = SecondLastInst.getOperand(2).getMBB();
928     Cond.push_back(SecondLastInst.getOperand(0));
929     Cond.push_back(SecondLastInst.getOperand(1));
930     FBB = LastInst.getOperand(0).getMBB();
931     return false;
932   } else if (SecondLastInst.getOpcode() == PPC::BC &&
933              LastInst.getOpcode() == PPC::B) {
934     if (!SecondLastInst.getOperand(1).isMBB() ||
935         !LastInst.getOperand(0).isMBB())
936       return true;
937     TBB = SecondLastInst.getOperand(1).getMBB();
938     Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
939     Cond.push_back(SecondLastInst.getOperand(0));
940     FBB = LastInst.getOperand(0).getMBB();
941     return false;
942   } else if (SecondLastInst.getOpcode() == PPC::BCn &&
943              LastInst.getOpcode() == PPC::B) {
944     if (!SecondLastInst.getOperand(1).isMBB() ||
945         !LastInst.getOperand(0).isMBB())
946       return true;
947     TBB = SecondLastInst.getOperand(1).getMBB();
948     Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
949     Cond.push_back(SecondLastInst.getOperand(0));
950     FBB = LastInst.getOperand(0).getMBB();
951     return false;
952   } else if ((SecondLastInst.getOpcode() == PPC::BDNZ8 ||
953               SecondLastInst.getOpcode() == PPC::BDNZ) &&
954              LastInst.getOpcode() == PPC::B) {
955     if (!SecondLastInst.getOperand(0).isMBB() ||
956         !LastInst.getOperand(0).isMBB())
957       return true;
958     if (DisableCTRLoopAnal)
959       return true;
960     TBB = SecondLastInst.getOperand(0).getMBB();
961     Cond.push_back(MachineOperand::CreateImm(1));
962     Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
963                                              true));
964     FBB = LastInst.getOperand(0).getMBB();
965     return false;
966   } else if ((SecondLastInst.getOpcode() == PPC::BDZ8 ||
967               SecondLastInst.getOpcode() == PPC::BDZ) &&
968              LastInst.getOpcode() == PPC::B) {
969     if (!SecondLastInst.getOperand(0).isMBB() ||
970         !LastInst.getOperand(0).isMBB())
971       return true;
972     if (DisableCTRLoopAnal)
973       return true;
974     TBB = SecondLastInst.getOperand(0).getMBB();
975     Cond.push_back(MachineOperand::CreateImm(0));
976     Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
977                                              true));
978     FBB = LastInst.getOperand(0).getMBB();
979     return false;
980   }
981 
982   // If the block ends with two PPC:Bs, handle it.  The second one is not
983   // executed, so remove it.
984   if (SecondLastInst.getOpcode() == PPC::B && LastInst.getOpcode() == PPC::B) {
985     if (!SecondLastInst.getOperand(0).isMBB())
986       return true;
987     TBB = SecondLastInst.getOperand(0).getMBB();
988     I = LastInst;
989     if (AllowModify)
990       I->eraseFromParent();
991     return false;
992   }
993 
994   // Otherwise, can't handle this.
995   return true;
996 }
997 
998 unsigned PPCInstrInfo::removeBranch(MachineBasicBlock &MBB,
999                                     int *BytesRemoved) const {
1000   assert(!BytesRemoved && "code size not handled");
1001 
1002   MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
1003   if (I == MBB.end())
1004     return 0;
1005 
1006   if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
1007       I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
1008       I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
1009       I->getOpcode() != PPC::BDZ8  && I->getOpcode() != PPC::BDZ)
1010     return 0;
1011 
1012   // Remove the branch.
1013   I->eraseFromParent();
1014 
1015   I = MBB.end();
1016 
1017   if (I == MBB.begin()) return 1;
1018   --I;
1019   if (I->getOpcode() != PPC::BCC &&
1020       I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
1021       I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
1022       I->getOpcode() != PPC::BDZ8  && I->getOpcode() != PPC::BDZ)
1023     return 1;
1024 
1025   // Remove the branch.
1026   I->eraseFromParent();
1027   return 2;
1028 }
1029 
1030 unsigned PPCInstrInfo::insertBranch(MachineBasicBlock &MBB,
1031                                     MachineBasicBlock *TBB,
1032                                     MachineBasicBlock *FBB,
1033                                     ArrayRef<MachineOperand> Cond,
1034                                     const DebugLoc &DL,
1035                                     int *BytesAdded) const {
1036   // Shouldn't be a fall through.
1037   assert(TBB && "insertBranch must not be told to insert a fallthrough");
1038   assert((Cond.size() == 2 || Cond.size() == 0) &&
1039          "PPC branch conditions have two components!");
1040   assert(!BytesAdded && "code size not handled");
1041 
1042   bool isPPC64 = Subtarget.isPPC64();
1043 
1044   // One-way branch.
1045   if (!FBB) {
1046     if (Cond.empty())   // Unconditional branch
1047       BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
1048     else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
1049       BuildMI(&MBB, DL, get(Cond[0].getImm() ?
1050                               (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1051                               (isPPC64 ? PPC::BDZ8  : PPC::BDZ))).addMBB(TBB);
1052     else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
1053       BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB);
1054     else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
1055       BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB);
1056     else                // Conditional branch
1057       BuildMI(&MBB, DL, get(PPC::BCC))
1058           .addImm(Cond[0].getImm())
1059           .add(Cond[1])
1060           .addMBB(TBB);
1061     return 1;
1062   }
1063 
1064   // Two-way Conditional Branch.
1065   if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
1066     BuildMI(&MBB, DL, get(Cond[0].getImm() ?
1067                             (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1068                             (isPPC64 ? PPC::BDZ8  : PPC::BDZ))).addMBB(TBB);
1069   else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
1070     BuildMI(&MBB, DL, get(PPC::BC)).add(Cond[1]).addMBB(TBB);
1071   else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
1072     BuildMI(&MBB, DL, get(PPC::BCn)).add(Cond[1]).addMBB(TBB);
1073   else
1074     BuildMI(&MBB, DL, get(PPC::BCC))
1075         .addImm(Cond[0].getImm())
1076         .add(Cond[1])
1077         .addMBB(TBB);
1078   BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
1079   return 2;
1080 }
1081 
1082 // Select analysis.
1083 bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
1084                                    ArrayRef<MachineOperand> Cond,
1085                                    Register DstReg, Register TrueReg,
1086                                    Register FalseReg, int &CondCycles,
1087                                    int &TrueCycles, int &FalseCycles) const {
1088   if (Cond.size() != 2)
1089     return false;
1090 
1091   // If this is really a bdnz-like condition, then it cannot be turned into a
1092   // select.
1093   if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
1094     return false;
1095 
1096   // Check register classes.
1097   const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
1098   const TargetRegisterClass *RC =
1099     RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
1100   if (!RC)
1101     return false;
1102 
1103   // isel is for regular integer GPRs only.
1104   if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
1105       !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
1106       !PPC::G8RCRegClass.hasSubClassEq(RC) &&
1107       !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
1108     return false;
1109 
1110   // FIXME: These numbers are for the A2, how well they work for other cores is
1111   // an open question. On the A2, the isel instruction has a 2-cycle latency
1112   // but single-cycle throughput. These numbers are used in combination with
1113   // the MispredictPenalty setting from the active SchedMachineModel.
1114   CondCycles = 1;
1115   TrueCycles = 1;
1116   FalseCycles = 1;
1117 
1118   return true;
1119 }
1120 
1121 void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
1122                                 MachineBasicBlock::iterator MI,
1123                                 const DebugLoc &dl, Register DestReg,
1124                                 ArrayRef<MachineOperand> Cond, Register TrueReg,
1125                                 Register FalseReg) const {
1126   assert(Cond.size() == 2 &&
1127          "PPC branch conditions have two components!");
1128 
1129   // Get the register classes.
1130   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
1131   const TargetRegisterClass *RC =
1132     RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
1133   assert(RC && "TrueReg and FalseReg must have overlapping register classes");
1134 
1135   bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
1136                  PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
1137   assert((Is64Bit ||
1138           PPC::GPRCRegClass.hasSubClassEq(RC) ||
1139           PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
1140          "isel is for regular integer GPRs only");
1141 
1142   unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
1143   auto SelectPred = static_cast<PPC::Predicate>(Cond[0].getImm());
1144 
1145   unsigned SubIdx = 0;
1146   bool SwapOps = false;
1147   switch (SelectPred) {
1148   case PPC::PRED_EQ:
1149   case PPC::PRED_EQ_MINUS:
1150   case PPC::PRED_EQ_PLUS:
1151       SubIdx = PPC::sub_eq; SwapOps = false; break;
1152   case PPC::PRED_NE:
1153   case PPC::PRED_NE_MINUS:
1154   case PPC::PRED_NE_PLUS:
1155       SubIdx = PPC::sub_eq; SwapOps = true; break;
1156   case PPC::PRED_LT:
1157   case PPC::PRED_LT_MINUS:
1158   case PPC::PRED_LT_PLUS:
1159       SubIdx = PPC::sub_lt; SwapOps = false; break;
1160   case PPC::PRED_GE:
1161   case PPC::PRED_GE_MINUS:
1162   case PPC::PRED_GE_PLUS:
1163       SubIdx = PPC::sub_lt; SwapOps = true; break;
1164   case PPC::PRED_GT:
1165   case PPC::PRED_GT_MINUS:
1166   case PPC::PRED_GT_PLUS:
1167       SubIdx = PPC::sub_gt; SwapOps = false; break;
1168   case PPC::PRED_LE:
1169   case PPC::PRED_LE_MINUS:
1170   case PPC::PRED_LE_PLUS:
1171       SubIdx = PPC::sub_gt; SwapOps = true; break;
1172   case PPC::PRED_UN:
1173   case PPC::PRED_UN_MINUS:
1174   case PPC::PRED_UN_PLUS:
1175       SubIdx = PPC::sub_un; SwapOps = false; break;
1176   case PPC::PRED_NU:
1177   case PPC::PRED_NU_MINUS:
1178   case PPC::PRED_NU_PLUS:
1179       SubIdx = PPC::sub_un; SwapOps = true; break;
1180   case PPC::PRED_BIT_SET:   SubIdx = 0; SwapOps = false; break;
1181   case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
1182   }
1183 
1184   Register FirstReg =  SwapOps ? FalseReg : TrueReg,
1185            SecondReg = SwapOps ? TrueReg  : FalseReg;
1186 
1187   // The first input register of isel cannot be r0. If it is a member
1188   // of a register class that can be r0, then copy it first (the
1189   // register allocator should eliminate the copy).
1190   if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
1191       MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
1192     const TargetRegisterClass *FirstRC =
1193       MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
1194         &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
1195     Register OldFirstReg = FirstReg;
1196     FirstReg = MRI.createVirtualRegister(FirstRC);
1197     BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
1198       .addReg(OldFirstReg);
1199   }
1200 
1201   BuildMI(MBB, MI, dl, get(OpCode), DestReg)
1202     .addReg(FirstReg).addReg(SecondReg)
1203     .addReg(Cond[1].getReg(), 0, SubIdx);
1204 }
1205 
1206 static unsigned getCRBitValue(unsigned CRBit) {
1207   unsigned Ret = 4;
1208   if (CRBit == PPC::CR0LT || CRBit == PPC::CR1LT ||
1209       CRBit == PPC::CR2LT || CRBit == PPC::CR3LT ||
1210       CRBit == PPC::CR4LT || CRBit == PPC::CR5LT ||
1211       CRBit == PPC::CR6LT || CRBit == PPC::CR7LT)
1212     Ret = 3;
1213   if (CRBit == PPC::CR0GT || CRBit == PPC::CR1GT ||
1214       CRBit == PPC::CR2GT || CRBit == PPC::CR3GT ||
1215       CRBit == PPC::CR4GT || CRBit == PPC::CR5GT ||
1216       CRBit == PPC::CR6GT || CRBit == PPC::CR7GT)
1217     Ret = 2;
1218   if (CRBit == PPC::CR0EQ || CRBit == PPC::CR1EQ ||
1219       CRBit == PPC::CR2EQ || CRBit == PPC::CR3EQ ||
1220       CRBit == PPC::CR4EQ || CRBit == PPC::CR5EQ ||
1221       CRBit == PPC::CR6EQ || CRBit == PPC::CR7EQ)
1222     Ret = 1;
1223   if (CRBit == PPC::CR0UN || CRBit == PPC::CR1UN ||
1224       CRBit == PPC::CR2UN || CRBit == PPC::CR3UN ||
1225       CRBit == PPC::CR4UN || CRBit == PPC::CR5UN ||
1226       CRBit == PPC::CR6UN || CRBit == PPC::CR7UN)
1227     Ret = 0;
1228 
1229   assert(Ret != 4 && "Invalid CR bit register");
1230   return Ret;
1231 }
1232 
1233 void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
1234                                MachineBasicBlock::iterator I,
1235                                const DebugLoc &DL, MCRegister DestReg,
1236                                MCRegister SrcReg, bool KillSrc) const {
1237   // We can end up with self copies and similar things as a result of VSX copy
1238   // legalization. Promote them here.
1239   const TargetRegisterInfo *TRI = &getRegisterInfo();
1240   if (PPC::F8RCRegClass.contains(DestReg) &&
1241       PPC::VSRCRegClass.contains(SrcReg)) {
1242     MCRegister SuperReg =
1243         TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass);
1244 
1245     if (VSXSelfCopyCrash && SrcReg == SuperReg)
1246       llvm_unreachable("nop VSX copy");
1247 
1248     DestReg = SuperReg;
1249   } else if (PPC::F8RCRegClass.contains(SrcReg) &&
1250              PPC::VSRCRegClass.contains(DestReg)) {
1251     MCRegister SuperReg =
1252         TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass);
1253 
1254     if (VSXSelfCopyCrash && DestReg == SuperReg)
1255       llvm_unreachable("nop VSX copy");
1256 
1257     SrcReg = SuperReg;
1258   }
1259 
1260   // Different class register copy
1261   if (PPC::CRBITRCRegClass.contains(SrcReg) &&
1262       PPC::GPRCRegClass.contains(DestReg)) {
1263     MCRegister CRReg = getCRFromCRBit(SrcReg);
1264     BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(CRReg);
1265     getKillRegState(KillSrc);
1266     // Rotate the CR bit in the CR fields to be the least significant bit and
1267     // then mask with 0x1 (MB = ME = 31).
1268     BuildMI(MBB, I, DL, get(PPC::RLWINM), DestReg)
1269        .addReg(DestReg, RegState::Kill)
1270        .addImm(TRI->getEncodingValue(CRReg) * 4 + (4 - getCRBitValue(SrcReg)))
1271        .addImm(31)
1272        .addImm(31);
1273     return;
1274   } else if (PPC::CRRCRegClass.contains(SrcReg) &&
1275       PPC::G8RCRegClass.contains(DestReg)) {
1276     BuildMI(MBB, I, DL, get(PPC::MFOCRF8), DestReg).addReg(SrcReg);
1277     getKillRegState(KillSrc);
1278     return;
1279   } else if (PPC::CRRCRegClass.contains(SrcReg) &&
1280       PPC::GPRCRegClass.contains(DestReg)) {
1281     BuildMI(MBB, I, DL, get(PPC::MFOCRF), DestReg).addReg(SrcReg);
1282     getKillRegState(KillSrc);
1283     return;
1284   } else if (PPC::G8RCRegClass.contains(SrcReg) &&
1285              PPC::VSFRCRegClass.contains(DestReg)) {
1286     assert(Subtarget.hasDirectMove() &&
1287            "Subtarget doesn't support directmove, don't know how to copy.");
1288     BuildMI(MBB, I, DL, get(PPC::MTVSRD), DestReg).addReg(SrcReg);
1289     NumGPRtoVSRSpill++;
1290     getKillRegState(KillSrc);
1291     return;
1292   } else if (PPC::VSFRCRegClass.contains(SrcReg) &&
1293              PPC::G8RCRegClass.contains(DestReg)) {
1294     assert(Subtarget.hasDirectMove() &&
1295            "Subtarget doesn't support directmove, don't know how to copy.");
1296     BuildMI(MBB, I, DL, get(PPC::MFVSRD), DestReg).addReg(SrcReg);
1297     getKillRegState(KillSrc);
1298     return;
1299   } else if (PPC::SPERCRegClass.contains(SrcReg) &&
1300              PPC::GPRCRegClass.contains(DestReg)) {
1301     BuildMI(MBB, I, DL, get(PPC::EFSCFD), DestReg).addReg(SrcReg);
1302     getKillRegState(KillSrc);
1303     return;
1304   } else if (PPC::GPRCRegClass.contains(SrcReg) &&
1305              PPC::SPERCRegClass.contains(DestReg)) {
1306     BuildMI(MBB, I, DL, get(PPC::EFDCFS), DestReg).addReg(SrcReg);
1307     getKillRegState(KillSrc);
1308     return;
1309   }
1310 
1311   unsigned Opc;
1312   if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
1313     Opc = PPC::OR;
1314   else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
1315     Opc = PPC::OR8;
1316   else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
1317     Opc = PPC::FMR;
1318   else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
1319     Opc = PPC::MCRF;
1320   else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
1321     Opc = PPC::VOR;
1322   else if (PPC::VSRCRegClass.contains(DestReg, SrcReg))
1323     // There are two different ways this can be done:
1324     //   1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
1325     //      issue in VSU pipeline 0.
1326     //   2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
1327     //      can go to either pipeline.
1328     // We'll always use xxlor here, because in practically all cases where
1329     // copies are generated, they are close enough to some use that the
1330     // lower-latency form is preferable.
1331     Opc = PPC::XXLOR;
1332   else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg) ||
1333            PPC::VSSRCRegClass.contains(DestReg, SrcReg))
1334     Opc = (Subtarget.hasP9Vector()) ? PPC::XSCPSGNDP : PPC::XXLORf;
1335   else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
1336     Opc = PPC::CROR;
1337   else if (PPC::SPERCRegClass.contains(DestReg, SrcReg))
1338     Opc = PPC::EVOR;
1339   else
1340     llvm_unreachable("Impossible reg-to-reg copy");
1341 
1342   const MCInstrDesc &MCID = get(Opc);
1343   if (MCID.getNumOperands() == 3)
1344     BuildMI(MBB, I, DL, MCID, DestReg)
1345       .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
1346   else
1347     BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
1348 }
1349 
1350 static unsigned getSpillIndex(const TargetRegisterClass *RC) {
1351   int OpcodeIndex = 0;
1352 
1353   if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
1354       PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
1355     OpcodeIndex = SOK_Int4Spill;
1356   } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
1357              PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
1358     OpcodeIndex = SOK_Int8Spill;
1359   } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
1360     OpcodeIndex = SOK_Float8Spill;
1361   } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
1362     OpcodeIndex = SOK_Float4Spill;
1363   } else if (PPC::SPERCRegClass.hasSubClassEq(RC)) {
1364     OpcodeIndex = SOK_SPESpill;
1365   } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
1366     OpcodeIndex = SOK_CRSpill;
1367   } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
1368     OpcodeIndex = SOK_CRBitSpill;
1369   } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
1370     OpcodeIndex = SOK_VRVectorSpill;
1371   } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
1372     OpcodeIndex = SOK_VSXVectorSpill;
1373   } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
1374     OpcodeIndex = SOK_VectorFloat8Spill;
1375   } else if (PPC::VSSRCRegClass.hasSubClassEq(RC)) {
1376     OpcodeIndex = SOK_VectorFloat4Spill;
1377   } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
1378     OpcodeIndex = SOK_VRSaveSpill;
1379   } else if (PPC::SPILLTOVSRRCRegClass.hasSubClassEq(RC)) {
1380     OpcodeIndex = SOK_SpillToVSR;
1381   } else {
1382     llvm_unreachable("Unknown regclass!");
1383   }
1384   return OpcodeIndex;
1385 }
1386 
1387 unsigned
1388 PPCInstrInfo::getStoreOpcodeForSpill(const TargetRegisterClass *RC) const {
1389   const unsigned *OpcodesForSpill = getStoreOpcodesForSpillArray();
1390   return OpcodesForSpill[getSpillIndex(RC)];
1391 }
1392 
1393 unsigned
1394 PPCInstrInfo::getLoadOpcodeForSpill(const TargetRegisterClass *RC) const {
1395   const unsigned *OpcodesForSpill = getLoadOpcodesForSpillArray();
1396   return OpcodesForSpill[getSpillIndex(RC)];
1397 }
1398 
1399 void PPCInstrInfo::StoreRegToStackSlot(
1400     MachineFunction &MF, unsigned SrcReg, bool isKill, int FrameIdx,
1401     const TargetRegisterClass *RC,
1402     SmallVectorImpl<MachineInstr *> &NewMIs) const {
1403   unsigned Opcode = getStoreOpcodeForSpill(RC);
1404   DebugLoc DL;
1405 
1406   PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1407   FuncInfo->setHasSpills();
1408 
1409   NewMIs.push_back(addFrameReference(
1410       BuildMI(MF, DL, get(Opcode)).addReg(SrcReg, getKillRegState(isKill)),
1411       FrameIdx));
1412 
1413   if (PPC::CRRCRegClass.hasSubClassEq(RC) ||
1414       PPC::CRBITRCRegClass.hasSubClassEq(RC))
1415     FuncInfo->setSpillsCR();
1416 
1417   if (PPC::VRSAVERCRegClass.hasSubClassEq(RC))
1418     FuncInfo->setSpillsVRSAVE();
1419 
1420   if (isXFormMemOp(Opcode))
1421     FuncInfo->setHasNonRISpills();
1422 }
1423 
1424 void PPCInstrInfo::storeRegToStackSlotNoUpd(
1425     MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, unsigned SrcReg,
1426     bool isKill, int FrameIdx, const TargetRegisterClass *RC,
1427     const TargetRegisterInfo *TRI) const {
1428   MachineFunction &MF = *MBB.getParent();
1429   SmallVector<MachineInstr *, 4> NewMIs;
1430 
1431   StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs);
1432 
1433   for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1434     MBB.insert(MI, NewMIs[i]);
1435 
1436   const MachineFrameInfo &MFI = MF.getFrameInfo();
1437   MachineMemOperand *MMO = MF.getMachineMemOperand(
1438       MachinePointerInfo::getFixedStack(MF, FrameIdx),
1439       MachineMemOperand::MOStore, MFI.getObjectSize(FrameIdx),
1440       MFI.getObjectAlign(FrameIdx));
1441   NewMIs.back()->addMemOperand(MF, MMO);
1442 }
1443 
1444 void PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
1445                                        MachineBasicBlock::iterator MI,
1446                                        Register SrcReg, bool isKill,
1447                                        int FrameIdx,
1448                                        const TargetRegisterClass *RC,
1449                                        const TargetRegisterInfo *TRI) const {
1450   // We need to avoid a situation in which the value from a VRRC register is
1451   // spilled using an Altivec instruction and reloaded into a VSRC register
1452   // using a VSX instruction. The issue with this is that the VSX
1453   // load/store instructions swap the doublewords in the vector and the Altivec
1454   // ones don't. The register classes on the spill/reload may be different if
1455   // the register is defined using an Altivec instruction and is then used by a
1456   // VSX instruction.
1457   RC = updatedRC(RC);
1458   storeRegToStackSlotNoUpd(MBB, MI, SrcReg, isKill, FrameIdx, RC, TRI);
1459 }
1460 
1461 void PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, const DebugLoc &DL,
1462                                         unsigned DestReg, int FrameIdx,
1463                                         const TargetRegisterClass *RC,
1464                                         SmallVectorImpl<MachineInstr *> &NewMIs)
1465                                         const {
1466   unsigned Opcode = getLoadOpcodeForSpill(RC);
1467   NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(Opcode), DestReg),
1468                                      FrameIdx));
1469   PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1470 
1471   if (PPC::CRRCRegClass.hasSubClassEq(RC) ||
1472       PPC::CRBITRCRegClass.hasSubClassEq(RC))
1473     FuncInfo->setSpillsCR();
1474 
1475   if (PPC::VRSAVERCRegClass.hasSubClassEq(RC))
1476     FuncInfo->setSpillsVRSAVE();
1477 
1478   if (isXFormMemOp(Opcode))
1479     FuncInfo->setHasNonRISpills();
1480 }
1481 
1482 void PPCInstrInfo::loadRegFromStackSlotNoUpd(
1483     MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, unsigned DestReg,
1484     int FrameIdx, const TargetRegisterClass *RC,
1485     const TargetRegisterInfo *TRI) const {
1486   MachineFunction &MF = *MBB.getParent();
1487   SmallVector<MachineInstr*, 4> NewMIs;
1488   DebugLoc DL;
1489   if (MI != MBB.end()) DL = MI->getDebugLoc();
1490 
1491   PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1492   FuncInfo->setHasSpills();
1493 
1494   LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs);
1495 
1496   for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
1497     MBB.insert(MI, NewMIs[i]);
1498 
1499   const MachineFrameInfo &MFI = MF.getFrameInfo();
1500   MachineMemOperand *MMO = MF.getMachineMemOperand(
1501       MachinePointerInfo::getFixedStack(MF, FrameIdx),
1502       MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIdx),
1503       MFI.getObjectAlign(FrameIdx));
1504   NewMIs.back()->addMemOperand(MF, MMO);
1505 }
1506 
1507 void PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
1508                                         MachineBasicBlock::iterator MI,
1509                                         Register DestReg, int FrameIdx,
1510                                         const TargetRegisterClass *RC,
1511                                         const TargetRegisterInfo *TRI) const {
1512   // We need to avoid a situation in which the value from a VRRC register is
1513   // spilled using an Altivec instruction and reloaded into a VSRC register
1514   // using a VSX instruction. The issue with this is that the VSX
1515   // load/store instructions swap the doublewords in the vector and the Altivec
1516   // ones don't. The register classes on the spill/reload may be different if
1517   // the register is defined using an Altivec instruction and is then used by a
1518   // VSX instruction.
1519   RC = updatedRC(RC);
1520 
1521   loadRegFromStackSlotNoUpd(MBB, MI, DestReg, FrameIdx, RC, TRI);
1522 }
1523 
1524 bool PPCInstrInfo::
1525 reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
1526   assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
1527   if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
1528     Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
1529   else
1530     // Leave the CR# the same, but invert the condition.
1531     Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
1532   return false;
1533 }
1534 
1535 // For some instructions, it is legal to fold ZERO into the RA register field.
1536 // This function performs that fold by replacing the operand with PPC::ZERO,
1537 // it does not consider whether the load immediate zero is no longer in use.
1538 bool PPCInstrInfo::onlyFoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
1539                                      Register Reg) const {
1540   // A zero immediate should always be loaded with a single li.
1541   unsigned DefOpc = DefMI.getOpcode();
1542   if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
1543     return false;
1544   if (!DefMI.getOperand(1).isImm())
1545     return false;
1546   if (DefMI.getOperand(1).getImm() != 0)
1547     return false;
1548 
1549   // Note that we cannot here invert the arguments of an isel in order to fold
1550   // a ZERO into what is presented as the second argument. All we have here
1551   // is the condition bit, and that might come from a CR-logical bit operation.
1552 
1553   const MCInstrDesc &UseMCID = UseMI.getDesc();
1554 
1555   // Only fold into real machine instructions.
1556   if (UseMCID.isPseudo())
1557     return false;
1558 
1559   // We need to find which of the User's operands is to be folded, that will be
1560   // the operand that matches the given register ID.
1561   unsigned UseIdx;
1562   for (UseIdx = 0; UseIdx < UseMI.getNumOperands(); ++UseIdx)
1563     if (UseMI.getOperand(UseIdx).isReg() &&
1564         UseMI.getOperand(UseIdx).getReg() == Reg)
1565       break;
1566 
1567   assert(UseIdx < UseMI.getNumOperands() && "Cannot find Reg in UseMI");
1568   assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
1569 
1570   const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
1571 
1572   // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
1573   // register (which might also be specified as a pointer class kind).
1574   if (UseInfo->isLookupPtrRegClass()) {
1575     if (UseInfo->RegClass /* Kind */ != 1)
1576       return false;
1577   } else {
1578     if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
1579         UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
1580       return false;
1581   }
1582 
1583   // Make sure this is not tied to an output register (or otherwise
1584   // constrained). This is true for ST?UX registers, for example, which
1585   // are tied to their output registers.
1586   if (UseInfo->Constraints != 0)
1587     return false;
1588 
1589   MCRegister ZeroReg;
1590   if (UseInfo->isLookupPtrRegClass()) {
1591     bool isPPC64 = Subtarget.isPPC64();
1592     ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
1593   } else {
1594     ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
1595               PPC::ZERO8 : PPC::ZERO;
1596   }
1597 
1598   UseMI.getOperand(UseIdx).setReg(ZeroReg);
1599   return true;
1600 }
1601 
1602 // Folds zero into instructions which have a load immediate zero as an operand
1603 // but also recognize zero as immediate zero. If the definition of the load
1604 // has no more users it is deleted.
1605 bool PPCInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
1606                                  Register Reg, MachineRegisterInfo *MRI) const {
1607   bool Changed = onlyFoldImmediate(UseMI, DefMI, Reg);
1608   if (MRI->use_nodbg_empty(Reg))
1609     DefMI.eraseFromParent();
1610   return Changed;
1611 }
1612 
1613 static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
1614   for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1615        I != IE; ++I)
1616     if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
1617       return true;
1618   return false;
1619 }
1620 
1621 // We should make sure that, if we're going to predicate both sides of a
1622 // condition (a diamond), that both sides don't define the counter register. We
1623 // can predicate counter-decrement-based branches, but while that predicates
1624 // the branching, it does not predicate the counter decrement. If we tried to
1625 // merge the triangle into one predicated block, we'd decrement the counter
1626 // twice.
1627 bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
1628                      unsigned NumT, unsigned ExtraT,
1629                      MachineBasicBlock &FMBB,
1630                      unsigned NumF, unsigned ExtraF,
1631                      BranchProbability Probability) const {
1632   return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
1633 }
1634 
1635 
1636 bool PPCInstrInfo::isPredicated(const MachineInstr &MI) const {
1637   // The predicated branches are identified by their type, not really by the
1638   // explicit presence of a predicate. Furthermore, some of them can be
1639   // predicated more than once. Because if conversion won't try to predicate
1640   // any instruction which already claims to be predicated (by returning true
1641   // here), always return false. In doing so, we let isPredicable() be the
1642   // final word on whether not the instruction can be (further) predicated.
1643 
1644   return false;
1645 }
1646 
1647 bool PPCInstrInfo::PredicateInstruction(MachineInstr &MI,
1648                                         ArrayRef<MachineOperand> Pred) const {
1649   unsigned OpC = MI.getOpcode();
1650   if (OpC == PPC::BLR || OpC == PPC::BLR8) {
1651     if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
1652       bool isPPC64 = Subtarget.isPPC64();
1653       MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR)
1654                                       : (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
1655       // Need add Def and Use for CTR implicit operand.
1656       MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1657           .addReg(Pred[1].getReg(), RegState::Implicit)
1658           .addReg(Pred[1].getReg(), RegState::ImplicitDefine);
1659     } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1660       MI.setDesc(get(PPC::BCLR));
1661       MachineInstrBuilder(*MI.getParent()->getParent(), MI).add(Pred[1]);
1662     } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1663       MI.setDesc(get(PPC::BCLRn));
1664       MachineInstrBuilder(*MI.getParent()->getParent(), MI).add(Pred[1]);
1665     } else {
1666       MI.setDesc(get(PPC::BCCLR));
1667       MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1668           .addImm(Pred[0].getImm())
1669           .add(Pred[1]);
1670     }
1671 
1672     return true;
1673   } else if (OpC == PPC::B) {
1674     if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
1675       bool isPPC64 = Subtarget.isPPC64();
1676       MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ)
1677                                       : (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
1678       // Need add Def and Use for CTR implicit operand.
1679       MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1680           .addReg(Pred[1].getReg(), RegState::Implicit)
1681           .addReg(Pred[1].getReg(), RegState::ImplicitDefine);
1682     } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1683       MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1684       MI.RemoveOperand(0);
1685 
1686       MI.setDesc(get(PPC::BC));
1687       MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1688           .add(Pred[1])
1689           .addMBB(MBB);
1690     } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1691       MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1692       MI.RemoveOperand(0);
1693 
1694       MI.setDesc(get(PPC::BCn));
1695       MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1696           .add(Pred[1])
1697           .addMBB(MBB);
1698     } else {
1699       MachineBasicBlock *MBB = MI.getOperand(0).getMBB();
1700       MI.RemoveOperand(0);
1701 
1702       MI.setDesc(get(PPC::BCC));
1703       MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1704           .addImm(Pred[0].getImm())
1705           .add(Pred[1])
1706           .addMBB(MBB);
1707     }
1708 
1709     return true;
1710   } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 || OpC == PPC::BCTRL ||
1711              OpC == PPC::BCTRL8) {
1712     if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1713       llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1714 
1715     bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
1716     bool isPPC64 = Subtarget.isPPC64();
1717 
1718     if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1719       MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8)
1720                              : (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1721       MachineInstrBuilder(*MI.getParent()->getParent(), MI).add(Pred[1]);
1722     } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1723       MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n)
1724                              : (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1725       MachineInstrBuilder(*MI.getParent()->getParent(), MI).add(Pred[1]);
1726     } else {
1727       MI.setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8)
1728                              : (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
1729       MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1730           .addImm(Pred[0].getImm())
1731           .add(Pred[1]);
1732     }
1733 
1734     // Need add Def and Use for LR implicit operand.
1735     if (setLR)
1736       MachineInstrBuilder(*MI.getParent()->getParent(), MI)
1737           .addReg(isPPC64 ? PPC::LR8 : PPC::LR, RegState::Implicit)
1738           .addReg(isPPC64 ? PPC::LR8 : PPC::LR, RegState::ImplicitDefine);
1739 
1740     return true;
1741   }
1742 
1743   return false;
1744 }
1745 
1746 bool PPCInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1747                                      ArrayRef<MachineOperand> Pred2) const {
1748   assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1749   assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1750 
1751   if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1752     return false;
1753   if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1754     return false;
1755 
1756   // P1 can only subsume P2 if they test the same condition register.
1757   if (Pred1[1].getReg() != Pred2[1].getReg())
1758     return false;
1759 
1760   PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1761   PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1762 
1763   if (P1 == P2)
1764     return true;
1765 
1766   // Does P1 subsume P2, e.g. GE subsumes GT.
1767   if (P1 == PPC::PRED_LE &&
1768       (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1769     return true;
1770   if (P1 == PPC::PRED_GE &&
1771       (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1772     return true;
1773 
1774   return false;
1775 }
1776 
1777 bool PPCInstrInfo::DefinesPredicate(MachineInstr &MI,
1778                                     std::vector<MachineOperand> &Pred) const {
1779   // Note: At the present time, the contents of Pred from this function is
1780   // unused by IfConversion. This implementation follows ARM by pushing the
1781   // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1782   // predicate, instructions defining CTR or CTR8 are also included as
1783   // predicate-defining instructions.
1784 
1785   const TargetRegisterClass *RCs[] =
1786     { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1787       &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1788 
1789   bool Found = false;
1790   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1791     const MachineOperand &MO = MI.getOperand(i);
1792     for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
1793       const TargetRegisterClass *RC = RCs[c];
1794       if (MO.isReg()) {
1795         if (MO.isDef() && RC->contains(MO.getReg())) {
1796           Pred.push_back(MO);
1797           Found = true;
1798         }
1799       } else if (MO.isRegMask()) {
1800         for (TargetRegisterClass::iterator I = RC->begin(),
1801              IE = RC->end(); I != IE; ++I)
1802           if (MO.clobbersPhysReg(*I)) {
1803             Pred.push_back(MO);
1804             Found = true;
1805           }
1806       }
1807     }
1808   }
1809 
1810   return Found;
1811 }
1812 
1813 bool PPCInstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg,
1814                                   Register &SrcReg2, int &Mask,
1815                                   int &Value) const {
1816   unsigned Opc = MI.getOpcode();
1817 
1818   switch (Opc) {
1819   default: return false;
1820   case PPC::CMPWI:
1821   case PPC::CMPLWI:
1822   case PPC::CMPDI:
1823   case PPC::CMPLDI:
1824     SrcReg = MI.getOperand(1).getReg();
1825     SrcReg2 = 0;
1826     Value = MI.getOperand(2).getImm();
1827     Mask = 0xFFFF;
1828     return true;
1829   case PPC::CMPW:
1830   case PPC::CMPLW:
1831   case PPC::CMPD:
1832   case PPC::CMPLD:
1833   case PPC::FCMPUS:
1834   case PPC::FCMPUD:
1835     SrcReg = MI.getOperand(1).getReg();
1836     SrcReg2 = MI.getOperand(2).getReg();
1837     Value = 0;
1838     Mask = 0;
1839     return true;
1840   }
1841 }
1842 
1843 bool PPCInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg,
1844                                         Register SrcReg2, int Mask, int Value,
1845                                         const MachineRegisterInfo *MRI) const {
1846   if (DisableCmpOpt)
1847     return false;
1848 
1849   int OpC = CmpInstr.getOpcode();
1850   Register CRReg = CmpInstr.getOperand(0).getReg();
1851 
1852   // FP record forms set CR1 based on the exception status bits, not a
1853   // comparison with zero.
1854   if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1855     return false;
1856 
1857   const TargetRegisterInfo *TRI = &getRegisterInfo();
1858   // The record forms set the condition register based on a signed comparison
1859   // with zero (so says the ISA manual). This is not as straightforward as it
1860   // seems, however, because this is always a 64-bit comparison on PPC64, even
1861   // for instructions that are 32-bit in nature (like slw for example).
1862   // So, on PPC32, for unsigned comparisons, we can use the record forms only
1863   // for equality checks (as those don't depend on the sign). On PPC64,
1864   // we are restricted to equality for unsigned 64-bit comparisons and for
1865   // signed 32-bit comparisons the applicability is more restricted.
1866   bool isPPC64 = Subtarget.isPPC64();
1867   bool is32BitSignedCompare   = OpC ==  PPC::CMPWI || OpC == PPC::CMPW;
1868   bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1869   bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1870 
1871   // Look through copies unless that gets us to a physical register.
1872   Register ActualSrc = TRI->lookThruCopyLike(SrcReg, MRI);
1873   if (ActualSrc.isVirtual())
1874     SrcReg = ActualSrc;
1875 
1876   // Get the unique definition of SrcReg.
1877   MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1878   if (!MI) return false;
1879 
1880   bool equalityOnly = false;
1881   bool noSub = false;
1882   if (isPPC64) {
1883     if (is32BitSignedCompare) {
1884       // We can perform this optimization only if MI is sign-extending.
1885       if (isSignExtended(*MI))
1886         noSub = true;
1887       else
1888         return false;
1889     } else if (is32BitUnsignedCompare) {
1890       // We can perform this optimization, equality only, if MI is
1891       // zero-extending.
1892       if (isZeroExtended(*MI)) {
1893         noSub = true;
1894         equalityOnly = true;
1895       } else
1896         return false;
1897     } else
1898       equalityOnly = is64BitUnsignedCompare;
1899   } else
1900     equalityOnly = is32BitUnsignedCompare;
1901 
1902   if (equalityOnly) {
1903     // We need to check the uses of the condition register in order to reject
1904     // non-equality comparisons.
1905     for (MachineRegisterInfo::use_instr_iterator
1906          I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1907          I != IE; ++I) {
1908       MachineInstr *UseMI = &*I;
1909       if (UseMI->getOpcode() == PPC::BCC) {
1910         PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm();
1911         unsigned PredCond = PPC::getPredicateCondition(Pred);
1912         // We ignore hint bits when checking for non-equality comparisons.
1913         if (PredCond != PPC::PRED_EQ && PredCond != PPC::PRED_NE)
1914           return false;
1915       } else if (UseMI->getOpcode() == PPC::ISEL ||
1916                  UseMI->getOpcode() == PPC::ISEL8) {
1917         unsigned SubIdx = UseMI->getOperand(3).getSubReg();
1918         if (SubIdx != PPC::sub_eq)
1919           return false;
1920       } else
1921         return false;
1922     }
1923   }
1924 
1925   MachineBasicBlock::iterator I = CmpInstr;
1926 
1927   // Scan forward to find the first use of the compare.
1928   for (MachineBasicBlock::iterator EL = CmpInstr.getParent()->end(); I != EL;
1929        ++I) {
1930     bool FoundUse = false;
1931     for (MachineRegisterInfo::use_instr_iterator
1932          J = MRI->use_instr_begin(CRReg), JE = MRI->use_instr_end();
1933          J != JE; ++J)
1934       if (&*J == &*I) {
1935         FoundUse = true;
1936         break;
1937       }
1938 
1939     if (FoundUse)
1940       break;
1941   }
1942 
1943   SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1944   SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
1945 
1946   // There are two possible candidates which can be changed to set CR[01].
1947   // One is MI, the other is a SUB instruction.
1948   // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
1949   MachineInstr *Sub = nullptr;
1950   if (SrcReg2 != 0)
1951     // MI is not a candidate for CMPrr.
1952     MI = nullptr;
1953   // FIXME: Conservatively refuse to convert an instruction which isn't in the
1954   // same BB as the comparison. This is to allow the check below to avoid calls
1955   // (and other explicit clobbers); instead we should really check for these
1956   // more explicitly (in at least a few predecessors).
1957   else if (MI->getParent() != CmpInstr.getParent())
1958     return false;
1959   else if (Value != 0) {
1960     // The record-form instructions set CR bit based on signed comparison
1961     // against 0. We try to convert a compare against 1 or -1 into a compare
1962     // against 0 to exploit record-form instructions. For example, we change
1963     // the condition "greater than -1" into "greater than or equal to 0"
1964     // and "less than 1" into "less than or equal to 0".
1965 
1966     // Since we optimize comparison based on a specific branch condition,
1967     // we don't optimize if condition code is used by more than once.
1968     if (equalityOnly || !MRI->hasOneUse(CRReg))
1969       return false;
1970 
1971     MachineInstr *UseMI = &*MRI->use_instr_begin(CRReg);
1972     if (UseMI->getOpcode() != PPC::BCC)
1973       return false;
1974 
1975     PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm();
1976     unsigned PredCond = PPC::getPredicateCondition(Pred);
1977     unsigned PredHint = PPC::getPredicateHint(Pred);
1978     int16_t Immed = (int16_t)Value;
1979 
1980     // When modifying the condition in the predicate, we propagate hint bits
1981     // from the original predicate to the new one.
1982     if (Immed == -1 && PredCond == PPC::PRED_GT)
1983       // We convert "greater than -1" into "greater than or equal to 0",
1984       // since we are assuming signed comparison by !equalityOnly
1985       Pred = PPC::getPredicate(PPC::PRED_GE, PredHint);
1986     else if (Immed == -1 && PredCond == PPC::PRED_LE)
1987       // We convert "less than or equal to -1" into "less than 0".
1988       Pred = PPC::getPredicate(PPC::PRED_LT, PredHint);
1989     else if (Immed == 1 && PredCond == PPC::PRED_LT)
1990       // We convert "less than 1" into "less than or equal to 0".
1991       Pred = PPC::getPredicate(PPC::PRED_LE, PredHint);
1992     else if (Immed == 1 && PredCond == PPC::PRED_GE)
1993       // We convert "greater than or equal to 1" into "greater than 0".
1994       Pred = PPC::getPredicate(PPC::PRED_GT, PredHint);
1995     else
1996       return false;
1997 
1998     PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)), Pred));
1999   }
2000 
2001   // Search for Sub.
2002   --I;
2003 
2004   // Get ready to iterate backward from CmpInstr.
2005   MachineBasicBlock::iterator E = MI, B = CmpInstr.getParent()->begin();
2006 
2007   for (; I != E && !noSub; --I) {
2008     const MachineInstr &Instr = *I;
2009     unsigned IOpC = Instr.getOpcode();
2010 
2011     if (&*I != &CmpInstr && (Instr.modifiesRegister(PPC::CR0, TRI) ||
2012                              Instr.readsRegister(PPC::CR0, TRI)))
2013       // This instruction modifies or uses the record condition register after
2014       // the one we want to change. While we could do this transformation, it
2015       // would likely not be profitable. This transformation removes one
2016       // instruction, and so even forcing RA to generate one move probably
2017       // makes it unprofitable.
2018       return false;
2019 
2020     // Check whether CmpInstr can be made redundant by the current instruction.
2021     if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
2022          OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
2023         (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
2024         ((Instr.getOperand(1).getReg() == SrcReg &&
2025           Instr.getOperand(2).getReg() == SrcReg2) ||
2026         (Instr.getOperand(1).getReg() == SrcReg2 &&
2027          Instr.getOperand(2).getReg() == SrcReg))) {
2028       Sub = &*I;
2029       break;
2030     }
2031 
2032     if (I == B)
2033       // The 'and' is below the comparison instruction.
2034       return false;
2035   }
2036 
2037   // Return false if no candidates exist.
2038   if (!MI && !Sub)
2039     return false;
2040 
2041   // The single candidate is called MI.
2042   if (!MI) MI = Sub;
2043 
2044   int NewOpC = -1;
2045   int MIOpC = MI->getOpcode();
2046   if (MIOpC == PPC::ANDI_rec || MIOpC == PPC::ANDI8_rec ||
2047       MIOpC == PPC::ANDIS_rec || MIOpC == PPC::ANDIS8_rec)
2048     NewOpC = MIOpC;
2049   else {
2050     NewOpC = PPC::getRecordFormOpcode(MIOpC);
2051     if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
2052       NewOpC = MIOpC;
2053   }
2054 
2055   // FIXME: On the non-embedded POWER architectures, only some of the record
2056   // forms are fast, and we should use only the fast ones.
2057 
2058   // The defining instruction has a record form (or is already a record
2059   // form). It is possible, however, that we'll need to reverse the condition
2060   // code of the users.
2061   if (NewOpC == -1)
2062     return false;
2063 
2064   // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
2065   // needs to be updated to be based on SUB.  Push the condition code
2066   // operands to OperandsToUpdate.  If it is safe to remove CmpInstr, the
2067   // condition code of these operands will be modified.
2068   // Here, Value == 0 means we haven't converted comparison against 1 or -1 to
2069   // comparison against 0, which may modify predicate.
2070   bool ShouldSwap = false;
2071   if (Sub && Value == 0) {
2072     ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
2073       Sub->getOperand(2).getReg() == SrcReg;
2074 
2075     // The operands to subf are the opposite of sub, so only in the fixed-point
2076     // case, invert the order.
2077     ShouldSwap = !ShouldSwap;
2078   }
2079 
2080   if (ShouldSwap)
2081     for (MachineRegisterInfo::use_instr_iterator
2082          I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
2083          I != IE; ++I) {
2084       MachineInstr *UseMI = &*I;
2085       if (UseMI->getOpcode() == PPC::BCC) {
2086         PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
2087         unsigned PredCond = PPC::getPredicateCondition(Pred);
2088         assert((!equalityOnly ||
2089                 PredCond == PPC::PRED_EQ || PredCond == PPC::PRED_NE) &&
2090                "Invalid predicate for equality-only optimization");
2091         (void)PredCond; // To suppress warning in release build.
2092         PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
2093                                 PPC::getSwappedPredicate(Pred)));
2094       } else if (UseMI->getOpcode() == PPC::ISEL ||
2095                  UseMI->getOpcode() == PPC::ISEL8) {
2096         unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
2097         assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
2098                "Invalid CR bit for equality-only optimization");
2099 
2100         if (NewSubReg == PPC::sub_lt)
2101           NewSubReg = PPC::sub_gt;
2102         else if (NewSubReg == PPC::sub_gt)
2103           NewSubReg = PPC::sub_lt;
2104 
2105         SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
2106                                                  NewSubReg));
2107       } else // We need to abort on a user we don't understand.
2108         return false;
2109     }
2110   assert(!(Value != 0 && ShouldSwap) &&
2111          "Non-zero immediate support and ShouldSwap"
2112          "may conflict in updating predicate");
2113 
2114   // Create a new virtual register to hold the value of the CR set by the
2115   // record-form instruction. If the instruction was not previously in
2116   // record form, then set the kill flag on the CR.
2117   CmpInstr.eraseFromParent();
2118 
2119   MachineBasicBlock::iterator MII = MI;
2120   BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
2121           get(TargetOpcode::COPY), CRReg)
2122     .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
2123 
2124   // Even if CR0 register were dead before, it is alive now since the
2125   // instruction we just built uses it.
2126   MI->clearRegisterDeads(PPC::CR0);
2127 
2128   if (MIOpC != NewOpC) {
2129     // We need to be careful here: we're replacing one instruction with
2130     // another, and we need to make sure that we get all of the right
2131     // implicit uses and defs. On the other hand, the caller may be holding
2132     // an iterator to this instruction, and so we can't delete it (this is
2133     // specifically the case if this is the instruction directly after the
2134     // compare).
2135 
2136     // Rotates are expensive instructions. If we're emitting a record-form
2137     // rotate that can just be an andi/andis, we should just emit that.
2138     if (MIOpC == PPC::RLWINM || MIOpC == PPC::RLWINM8) {
2139       Register GPRRes = MI->getOperand(0).getReg();
2140       int64_t SH = MI->getOperand(2).getImm();
2141       int64_t MB = MI->getOperand(3).getImm();
2142       int64_t ME = MI->getOperand(4).getImm();
2143       // We can only do this if both the start and end of the mask are in the
2144       // same halfword.
2145       bool MBInLoHWord = MB >= 16;
2146       bool MEInLoHWord = ME >= 16;
2147       uint64_t Mask = ~0LLU;
2148 
2149       if (MB <= ME && MBInLoHWord == MEInLoHWord && SH == 0) {
2150         Mask = ((1LLU << (32 - MB)) - 1) & ~((1LLU << (31 - ME)) - 1);
2151         // The mask value needs to shift right 16 if we're emitting andis.
2152         Mask >>= MBInLoHWord ? 0 : 16;
2153         NewOpC = MIOpC == PPC::RLWINM
2154                      ? (MBInLoHWord ? PPC::ANDI_rec : PPC::ANDIS_rec)
2155                      : (MBInLoHWord ? PPC::ANDI8_rec : PPC::ANDIS8_rec);
2156       } else if (MRI->use_empty(GPRRes) && (ME == 31) &&
2157                  (ME - MB + 1 == SH) && (MB >= 16)) {
2158         // If we are rotating by the exact number of bits as are in the mask
2159         // and the mask is in the least significant bits of the register,
2160         // that's just an andis. (as long as the GPR result has no uses).
2161         Mask = ((1LLU << 32) - 1) & ~((1LLU << (32 - SH)) - 1);
2162         Mask >>= 16;
2163         NewOpC = MIOpC == PPC::RLWINM ? PPC::ANDIS_rec : PPC::ANDIS8_rec;
2164       }
2165       // If we've set the mask, we can transform.
2166       if (Mask != ~0LLU) {
2167         MI->RemoveOperand(4);
2168         MI->RemoveOperand(3);
2169         MI->getOperand(2).setImm(Mask);
2170         NumRcRotatesConvertedToRcAnd++;
2171       }
2172     } else if (MIOpC == PPC::RLDICL && MI->getOperand(2).getImm() == 0) {
2173       int64_t MB = MI->getOperand(3).getImm();
2174       if (MB >= 48) {
2175         uint64_t Mask = (1LLU << (63 - MB + 1)) - 1;
2176         NewOpC = PPC::ANDI8_rec;
2177         MI->RemoveOperand(3);
2178         MI->getOperand(2).setImm(Mask);
2179         NumRcRotatesConvertedToRcAnd++;
2180       }
2181     }
2182 
2183     const MCInstrDesc &NewDesc = get(NewOpC);
2184     MI->setDesc(NewDesc);
2185 
2186     if (NewDesc.ImplicitDefs)
2187       for (const MCPhysReg *ImpDefs = NewDesc.getImplicitDefs();
2188            *ImpDefs; ++ImpDefs)
2189         if (!MI->definesRegister(*ImpDefs))
2190           MI->addOperand(*MI->getParent()->getParent(),
2191                          MachineOperand::CreateReg(*ImpDefs, true, true));
2192     if (NewDesc.ImplicitUses)
2193       for (const MCPhysReg *ImpUses = NewDesc.getImplicitUses();
2194            *ImpUses; ++ImpUses)
2195         if (!MI->readsRegister(*ImpUses))
2196           MI->addOperand(*MI->getParent()->getParent(),
2197                          MachineOperand::CreateReg(*ImpUses, false, true));
2198   }
2199   assert(MI->definesRegister(PPC::CR0) &&
2200          "Record-form instruction does not define cr0?");
2201 
2202   // Modify the condition code of operands in OperandsToUpdate.
2203   // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2204   // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
2205   for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
2206     PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
2207 
2208   for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
2209     SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
2210 
2211   return true;
2212 }
2213 
2214 /// GetInstSize - Return the number of bytes of code the specified
2215 /// instruction may be.  This returns the maximum number of bytes.
2216 ///
2217 unsigned PPCInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
2218   unsigned Opcode = MI.getOpcode();
2219 
2220   if (Opcode == PPC::INLINEASM || Opcode == PPC::INLINEASM_BR) {
2221     const MachineFunction *MF = MI.getParent()->getParent();
2222     const char *AsmStr = MI.getOperand(0).getSymbolName();
2223     return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
2224   } else if (Opcode == TargetOpcode::STACKMAP) {
2225     StackMapOpers Opers(&MI);
2226     return Opers.getNumPatchBytes();
2227   } else if (Opcode == TargetOpcode::PATCHPOINT) {
2228     PatchPointOpers Opers(&MI);
2229     return Opers.getNumPatchBytes();
2230   } else {
2231     return get(Opcode).getSize();
2232   }
2233 }
2234 
2235 std::pair<unsigned, unsigned>
2236 PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
2237   const unsigned Mask = PPCII::MO_ACCESS_MASK;
2238   return std::make_pair(TF & Mask, TF & ~Mask);
2239 }
2240 
2241 ArrayRef<std::pair<unsigned, const char *>>
2242 PPCInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
2243   using namespace PPCII;
2244   static const std::pair<unsigned, const char *> TargetFlags[] = {
2245       {MO_LO, "ppc-lo"},
2246       {MO_HA, "ppc-ha"},
2247       {MO_TPREL_LO, "ppc-tprel-lo"},
2248       {MO_TPREL_HA, "ppc-tprel-ha"},
2249       {MO_DTPREL_LO, "ppc-dtprel-lo"},
2250       {MO_TLSLD_LO, "ppc-tlsld-lo"},
2251       {MO_TOC_LO, "ppc-toc-lo"},
2252       {MO_TLS, "ppc-tls"}};
2253   return makeArrayRef(TargetFlags);
2254 }
2255 
2256 ArrayRef<std::pair<unsigned, const char *>>
2257 PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
2258   using namespace PPCII;
2259   static const std::pair<unsigned, const char *> TargetFlags[] = {
2260       {MO_PLT, "ppc-plt"},
2261       {MO_PIC_FLAG, "ppc-pic"},
2262       {MO_PCREL_FLAG, "ppc-pcrel"},
2263       {MO_GOT_FLAG, "ppc-got"},
2264       {MO_PCREL_OPT_FLAG, "ppc-opt-pcrel"}};
2265   return makeArrayRef(TargetFlags);
2266 }
2267 
2268 // Expand VSX Memory Pseudo instruction to either a VSX or a FP instruction.
2269 // The VSX versions have the advantage of a full 64-register target whereas
2270 // the FP ones have the advantage of lower latency and higher throughput. So
2271 // what we are after is using the faster instructions in low register pressure
2272 // situations and using the larger register file in high register pressure
2273 // situations.
2274 bool PPCInstrInfo::expandVSXMemPseudo(MachineInstr &MI) const {
2275     unsigned UpperOpcode, LowerOpcode;
2276     switch (MI.getOpcode()) {
2277     case PPC::DFLOADf32:
2278       UpperOpcode = PPC::LXSSP;
2279       LowerOpcode = PPC::LFS;
2280       break;
2281     case PPC::DFLOADf64:
2282       UpperOpcode = PPC::LXSD;
2283       LowerOpcode = PPC::LFD;
2284       break;
2285     case PPC::DFSTOREf32:
2286       UpperOpcode = PPC::STXSSP;
2287       LowerOpcode = PPC::STFS;
2288       break;
2289     case PPC::DFSTOREf64:
2290       UpperOpcode = PPC::STXSD;
2291       LowerOpcode = PPC::STFD;
2292       break;
2293     case PPC::XFLOADf32:
2294       UpperOpcode = PPC::LXSSPX;
2295       LowerOpcode = PPC::LFSX;
2296       break;
2297     case PPC::XFLOADf64:
2298       UpperOpcode = PPC::LXSDX;
2299       LowerOpcode = PPC::LFDX;
2300       break;
2301     case PPC::XFSTOREf32:
2302       UpperOpcode = PPC::STXSSPX;
2303       LowerOpcode = PPC::STFSX;
2304       break;
2305     case PPC::XFSTOREf64:
2306       UpperOpcode = PPC::STXSDX;
2307       LowerOpcode = PPC::STFDX;
2308       break;
2309     case PPC::LIWAX:
2310       UpperOpcode = PPC::LXSIWAX;
2311       LowerOpcode = PPC::LFIWAX;
2312       break;
2313     case PPC::LIWZX:
2314       UpperOpcode = PPC::LXSIWZX;
2315       LowerOpcode = PPC::LFIWZX;
2316       break;
2317     case PPC::STIWX:
2318       UpperOpcode = PPC::STXSIWX;
2319       LowerOpcode = PPC::STFIWX;
2320       break;
2321     default:
2322       llvm_unreachable("Unknown Operation!");
2323     }
2324 
2325     Register TargetReg = MI.getOperand(0).getReg();
2326     unsigned Opcode;
2327     if ((TargetReg >= PPC::F0 && TargetReg <= PPC::F31) ||
2328         (TargetReg >= PPC::VSL0 && TargetReg <= PPC::VSL31))
2329       Opcode = LowerOpcode;
2330     else
2331       Opcode = UpperOpcode;
2332     MI.setDesc(get(Opcode));
2333     return true;
2334 }
2335 
2336 static bool isAnImmediateOperand(const MachineOperand &MO) {
2337   return MO.isCPI() || MO.isGlobal() || MO.isImm();
2338 }
2339 
2340 bool PPCInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
2341   auto &MBB = *MI.getParent();
2342   auto DL = MI.getDebugLoc();
2343 
2344   switch (MI.getOpcode()) {
2345   case TargetOpcode::LOAD_STACK_GUARD: {
2346     assert(Subtarget.isTargetLinux() &&
2347            "Only Linux target is expected to contain LOAD_STACK_GUARD");
2348     const int64_t Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008;
2349     const unsigned Reg = Subtarget.isPPC64() ? PPC::X13 : PPC::R2;
2350     MI.setDesc(get(Subtarget.isPPC64() ? PPC::LD : PPC::LWZ));
2351     MachineInstrBuilder(*MI.getParent()->getParent(), MI)
2352         .addImm(Offset)
2353         .addReg(Reg);
2354     return true;
2355   }
2356   case PPC::DFLOADf32:
2357   case PPC::DFLOADf64:
2358   case PPC::DFSTOREf32:
2359   case PPC::DFSTOREf64: {
2360     assert(Subtarget.hasP9Vector() &&
2361            "Invalid D-Form Pseudo-ops on Pre-P9 target.");
2362     assert(MI.getOperand(2).isReg() &&
2363            isAnImmediateOperand(MI.getOperand(1)) &&
2364            "D-form op must have register and immediate operands");
2365     return expandVSXMemPseudo(MI);
2366   }
2367   case PPC::XFLOADf32:
2368   case PPC::XFSTOREf32:
2369   case PPC::LIWAX:
2370   case PPC::LIWZX:
2371   case PPC::STIWX: {
2372     assert(Subtarget.hasP8Vector() &&
2373            "Invalid X-Form Pseudo-ops on Pre-P8 target.");
2374     assert(MI.getOperand(2).isReg() && MI.getOperand(1).isReg() &&
2375            "X-form op must have register and register operands");
2376     return expandVSXMemPseudo(MI);
2377   }
2378   case PPC::XFLOADf64:
2379   case PPC::XFSTOREf64: {
2380     assert(Subtarget.hasVSX() &&
2381            "Invalid X-Form Pseudo-ops on target that has no VSX.");
2382     assert(MI.getOperand(2).isReg() && MI.getOperand(1).isReg() &&
2383            "X-form op must have register and register operands");
2384     return expandVSXMemPseudo(MI);
2385   }
2386   case PPC::SPILLTOVSR_LD: {
2387     Register TargetReg = MI.getOperand(0).getReg();
2388     if (PPC::VSFRCRegClass.contains(TargetReg)) {
2389       MI.setDesc(get(PPC::DFLOADf64));
2390       return expandPostRAPseudo(MI);
2391     }
2392     else
2393       MI.setDesc(get(PPC::LD));
2394     return true;
2395   }
2396   case PPC::SPILLTOVSR_ST: {
2397     Register SrcReg = MI.getOperand(0).getReg();
2398     if (PPC::VSFRCRegClass.contains(SrcReg)) {
2399       NumStoreSPILLVSRRCAsVec++;
2400       MI.setDesc(get(PPC::DFSTOREf64));
2401       return expandPostRAPseudo(MI);
2402     } else {
2403       NumStoreSPILLVSRRCAsGpr++;
2404       MI.setDesc(get(PPC::STD));
2405     }
2406     return true;
2407   }
2408   case PPC::SPILLTOVSR_LDX: {
2409     Register TargetReg = MI.getOperand(0).getReg();
2410     if (PPC::VSFRCRegClass.contains(TargetReg))
2411       MI.setDesc(get(PPC::LXSDX));
2412     else
2413       MI.setDesc(get(PPC::LDX));
2414     return true;
2415   }
2416   case PPC::SPILLTOVSR_STX: {
2417     Register SrcReg = MI.getOperand(0).getReg();
2418     if (PPC::VSFRCRegClass.contains(SrcReg)) {
2419       NumStoreSPILLVSRRCAsVec++;
2420       MI.setDesc(get(PPC::STXSDX));
2421     } else {
2422       NumStoreSPILLVSRRCAsGpr++;
2423       MI.setDesc(get(PPC::STDX));
2424     }
2425     return true;
2426   }
2427 
2428   case PPC::CFENCE8: {
2429     auto Val = MI.getOperand(0).getReg();
2430     BuildMI(MBB, MI, DL, get(PPC::CMPD), PPC::CR7).addReg(Val).addReg(Val);
2431     BuildMI(MBB, MI, DL, get(PPC::CTRL_DEP))
2432         .addImm(PPC::PRED_NE_MINUS)
2433         .addReg(PPC::CR7)
2434         .addImm(1);
2435     MI.setDesc(get(PPC::ISYNC));
2436     MI.RemoveOperand(0);
2437     return true;
2438   }
2439   }
2440   return false;
2441 }
2442 
2443 // Essentially a compile-time implementation of a compare->isel sequence.
2444 // It takes two constants to compare, along with the true/false registers
2445 // and the comparison type (as a subreg to a CR field) and returns one
2446 // of the true/false registers, depending on the comparison results.
2447 static unsigned selectReg(int64_t Imm1, int64_t Imm2, unsigned CompareOpc,
2448                           unsigned TrueReg, unsigned FalseReg,
2449                           unsigned CRSubReg) {
2450   // Signed comparisons. The immediates are assumed to be sign-extended.
2451   if (CompareOpc == PPC::CMPWI || CompareOpc == PPC::CMPDI) {
2452     switch (CRSubReg) {
2453     default: llvm_unreachable("Unknown integer comparison type.");
2454     case PPC::sub_lt:
2455       return Imm1 < Imm2 ? TrueReg : FalseReg;
2456     case PPC::sub_gt:
2457       return Imm1 > Imm2 ? TrueReg : FalseReg;
2458     case PPC::sub_eq:
2459       return Imm1 == Imm2 ? TrueReg : FalseReg;
2460     }
2461   }
2462   // Unsigned comparisons.
2463   else if (CompareOpc == PPC::CMPLWI || CompareOpc == PPC::CMPLDI) {
2464     switch (CRSubReg) {
2465     default: llvm_unreachable("Unknown integer comparison type.");
2466     case PPC::sub_lt:
2467       return (uint64_t)Imm1 < (uint64_t)Imm2 ? TrueReg : FalseReg;
2468     case PPC::sub_gt:
2469       return (uint64_t)Imm1 > (uint64_t)Imm2 ? TrueReg : FalseReg;
2470     case PPC::sub_eq:
2471       return Imm1 == Imm2 ? TrueReg : FalseReg;
2472     }
2473   }
2474   return PPC::NoRegister;
2475 }
2476 
2477 void PPCInstrInfo::replaceInstrOperandWithImm(MachineInstr &MI,
2478                                               unsigned OpNo,
2479                                               int64_t Imm) const {
2480   assert(MI.getOperand(OpNo).isReg() && "Operand must be a REG");
2481   // Replace the REG with the Immediate.
2482   Register InUseReg = MI.getOperand(OpNo).getReg();
2483   MI.getOperand(OpNo).ChangeToImmediate(Imm);
2484 
2485   if (MI.implicit_operands().empty())
2486     return;
2487 
2488   // We need to make sure that the MI didn't have any implicit use
2489   // of this REG any more.
2490   const TargetRegisterInfo *TRI = &getRegisterInfo();
2491   int UseOpIdx = MI.findRegisterUseOperandIdx(InUseReg, false, TRI);
2492   if (UseOpIdx >= 0) {
2493     MachineOperand &MO = MI.getOperand(UseOpIdx);
2494     if (MO.isImplicit())
2495       // The operands must always be in the following order:
2496       // - explicit reg defs,
2497       // - other explicit operands (reg uses, immediates, etc.),
2498       // - implicit reg defs
2499       // - implicit reg uses
2500       // Therefore, removing the implicit operand won't change the explicit
2501       // operands layout.
2502       MI.RemoveOperand(UseOpIdx);
2503   }
2504 }
2505 
2506 // Replace an instruction with one that materializes a constant (and sets
2507 // CR0 if the original instruction was a record-form instruction).
2508 void PPCInstrInfo::replaceInstrWithLI(MachineInstr &MI,
2509                                       const LoadImmediateInfo &LII) const {
2510   // Remove existing operands.
2511   int OperandToKeep = LII.SetCR ? 1 : 0;
2512   for (int i = MI.getNumOperands() - 1; i > OperandToKeep; i--)
2513     MI.RemoveOperand(i);
2514 
2515   // Replace the instruction.
2516   if (LII.SetCR) {
2517     MI.setDesc(get(LII.Is64Bit ? PPC::ANDI8_rec : PPC::ANDI_rec));
2518     // Set the immediate.
2519     MachineInstrBuilder(*MI.getParent()->getParent(), MI)
2520         .addImm(LII.Imm).addReg(PPC::CR0, RegState::ImplicitDefine);
2521     return;
2522   }
2523   else
2524     MI.setDesc(get(LII.Is64Bit ? PPC::LI8 : PPC::LI));
2525 
2526   // Set the immediate.
2527   MachineInstrBuilder(*MI.getParent()->getParent(), MI)
2528       .addImm(LII.Imm);
2529 }
2530 
2531 MachineInstr *PPCInstrInfo::getDefMIPostRA(unsigned Reg, MachineInstr &MI,
2532                                            bool &SeenIntermediateUse) const {
2533   assert(!MI.getParent()->getParent()->getRegInfo().isSSA() &&
2534          "Should be called after register allocation.");
2535   const TargetRegisterInfo *TRI = &getRegisterInfo();
2536   MachineBasicBlock::reverse_iterator E = MI.getParent()->rend(), It = MI;
2537   It++;
2538   SeenIntermediateUse = false;
2539   for (; It != E; ++It) {
2540     if (It->modifiesRegister(Reg, TRI))
2541       return &*It;
2542     if (It->readsRegister(Reg, TRI))
2543       SeenIntermediateUse = true;
2544   }
2545   return nullptr;
2546 }
2547 
2548 MachineInstr *PPCInstrInfo::getForwardingDefMI(
2549   MachineInstr &MI,
2550   unsigned &OpNoForForwarding,
2551   bool &SeenIntermediateUse) const {
2552   OpNoForForwarding = ~0U;
2553   MachineInstr *DefMI = nullptr;
2554   MachineRegisterInfo *MRI = &MI.getParent()->getParent()->getRegInfo();
2555   const TargetRegisterInfo *TRI = &getRegisterInfo();
2556   // If we're in SSA, get the defs through the MRI. Otherwise, only look
2557   // within the basic block to see if the register is defined using an
2558   // LI/LI8/ADDI/ADDI8.
2559   if (MRI->isSSA()) {
2560     for (int i = 1, e = MI.getNumOperands(); i < e; i++) {
2561       if (!MI.getOperand(i).isReg())
2562         continue;
2563       Register Reg = MI.getOperand(i).getReg();
2564       if (!Register::isVirtualRegister(Reg))
2565         continue;
2566       unsigned TrueReg = TRI->lookThruCopyLike(Reg, MRI);
2567       if (Register::isVirtualRegister(TrueReg)) {
2568         DefMI = MRI->getVRegDef(TrueReg);
2569         if (DefMI->getOpcode() == PPC::LI || DefMI->getOpcode() == PPC::LI8 ||
2570             DefMI->getOpcode() == PPC::ADDI ||
2571             DefMI->getOpcode() == PPC::ADDI8) {
2572           OpNoForForwarding = i;
2573           // The ADDI and LI operand maybe exist in one instruction at same
2574           // time. we prefer to fold LI operand as LI only has one Imm operand
2575           // and is more possible to be converted. So if current DefMI is
2576           // ADDI/ADDI8, we continue to find possible LI/LI8.
2577           if (DefMI->getOpcode() == PPC::LI || DefMI->getOpcode() == PPC::LI8)
2578             break;
2579         }
2580       }
2581     }
2582   } else {
2583     // Looking back through the definition for each operand could be expensive,
2584     // so exit early if this isn't an instruction that either has an immediate
2585     // form or is already an immediate form that we can handle.
2586     ImmInstrInfo III;
2587     unsigned Opc = MI.getOpcode();
2588     bool ConvertibleImmForm =
2589         Opc == PPC::CMPWI || Opc == PPC::CMPLWI || Opc == PPC::CMPDI ||
2590         Opc == PPC::CMPLDI || Opc == PPC::ADDI || Opc == PPC::ADDI8 ||
2591         Opc == PPC::ORI || Opc == PPC::ORI8 || Opc == PPC::XORI ||
2592         Opc == PPC::XORI8 || Opc == PPC::RLDICL || Opc == PPC::RLDICL_rec ||
2593         Opc == PPC::RLDICL_32 || Opc == PPC::RLDICL_32_64 ||
2594         Opc == PPC::RLWINM || Opc == PPC::RLWINM_rec || Opc == PPC::RLWINM8 ||
2595         Opc == PPC::RLWINM8_rec;
2596     bool IsVFReg = (MI.getNumOperands() && MI.getOperand(0).isReg())
2597                        ? isVFRegister(MI.getOperand(0).getReg())
2598                        : false;
2599     if (!ConvertibleImmForm && !instrHasImmForm(Opc, IsVFReg, III, true))
2600       return nullptr;
2601 
2602     // Don't convert or %X, %Y, %Y since that's just a register move.
2603     if ((Opc == PPC::OR || Opc == PPC::OR8) &&
2604         MI.getOperand(1).getReg() == MI.getOperand(2).getReg())
2605       return nullptr;
2606     for (int i = 1, e = MI.getNumOperands(); i < e; i++) {
2607       MachineOperand &MO = MI.getOperand(i);
2608       SeenIntermediateUse = false;
2609       if (MO.isReg() && MO.isUse() && !MO.isImplicit()) {
2610         Register Reg = MI.getOperand(i).getReg();
2611         // If we see another use of this reg between the def and the MI,
2612         // we want to flat it so the def isn't deleted.
2613         MachineInstr *DefMI = getDefMIPostRA(Reg, MI, SeenIntermediateUse);
2614         if (DefMI) {
2615           // Is this register defined by some form of add-immediate (including
2616           // load-immediate) within this basic block?
2617           switch (DefMI->getOpcode()) {
2618           default:
2619             break;
2620           case PPC::LI:
2621           case PPC::LI8:
2622           case PPC::ADDItocL:
2623           case PPC::ADDI:
2624           case PPC::ADDI8:
2625             OpNoForForwarding = i;
2626             return DefMI;
2627           }
2628         }
2629       }
2630     }
2631   }
2632   return OpNoForForwarding == ~0U ? nullptr : DefMI;
2633 }
2634 
2635 unsigned PPCInstrInfo::getSpillTarget() const {
2636   return Subtarget.hasP9Vector() ? 1 : 0;
2637 }
2638 
2639 const unsigned *PPCInstrInfo::getStoreOpcodesForSpillArray() const {
2640   return StoreSpillOpcodesArray[getSpillTarget()];
2641 }
2642 
2643 const unsigned *PPCInstrInfo::getLoadOpcodesForSpillArray() const {
2644   return LoadSpillOpcodesArray[getSpillTarget()];
2645 }
2646 
2647 void PPCInstrInfo::fixupIsDeadOrKill(MachineInstr &StartMI, MachineInstr &EndMI,
2648                                      unsigned RegNo) const {
2649   // Conservatively clear kill flag for the register if the instructions are in
2650   // different basic blocks and in SSA form, because the kill flag may no longer
2651   // be right. There is no need to bother with dead flags since defs with no
2652   // uses will be handled by DCE.
2653   MachineRegisterInfo &MRI = StartMI.getParent()->getParent()->getRegInfo();
2654   if (MRI.isSSA() && (StartMI.getParent() != EndMI.getParent())) {
2655     MRI.clearKillFlags(RegNo);
2656     return;
2657   }
2658 
2659   // Instructions between [StartMI, EndMI] should be in same basic block.
2660   assert((StartMI.getParent() == EndMI.getParent()) &&
2661          "Instructions are not in same basic block");
2662 
2663   bool IsKillSet = false;
2664 
2665   auto clearOperandKillInfo = [=] (MachineInstr &MI, unsigned Index) {
2666     MachineOperand &MO = MI.getOperand(Index);
2667     if (MO.isReg() && MO.isUse() && MO.isKill() &&
2668         getRegisterInfo().regsOverlap(MO.getReg(), RegNo))
2669       MO.setIsKill(false);
2670   };
2671 
2672   // Set killed flag for EndMI.
2673   // No need to do anything if EndMI defines RegNo.
2674   int UseIndex =
2675       EndMI.findRegisterUseOperandIdx(RegNo, false, &getRegisterInfo());
2676   if (UseIndex != -1) {
2677     EndMI.getOperand(UseIndex).setIsKill(true);
2678     IsKillSet = true;
2679     // Clear killed flag for other EndMI operands related to RegNo. In some
2680     // upexpected cases, killed may be set multiple times for same register
2681     // operand in same MI.
2682     for (int i = 0, e = EndMI.getNumOperands(); i != e; ++i)
2683       if (i != UseIndex)
2684         clearOperandKillInfo(EndMI, i);
2685   }
2686 
2687   // Walking the inst in reverse order (EndMI -> StartMI].
2688   MachineBasicBlock::reverse_iterator It = EndMI;
2689   MachineBasicBlock::reverse_iterator E = EndMI.getParent()->rend();
2690   // EndMI has been handled above, skip it here.
2691   It++;
2692   MachineOperand *MO = nullptr;
2693   for (; It != E; ++It) {
2694     // Skip insturctions which could not be a def/use of RegNo.
2695     if (It->isDebugInstr() || It->isPosition())
2696       continue;
2697 
2698     // Clear killed flag for all It operands related to RegNo. In some
2699     // upexpected cases, killed may be set multiple times for same register
2700     // operand in same MI.
2701     for (int i = 0, e = It->getNumOperands(); i != e; ++i)
2702         clearOperandKillInfo(*It, i);
2703 
2704     // If killed is not set, set killed for its last use or set dead for its def
2705     // if no use found.
2706     if (!IsKillSet) {
2707       if ((MO = It->findRegisterUseOperand(RegNo, false, &getRegisterInfo()))) {
2708         // Use found, set it killed.
2709         IsKillSet = true;
2710         MO->setIsKill(true);
2711         continue;
2712       } else if ((MO = It->findRegisterDefOperand(RegNo, false, true,
2713                                                   &getRegisterInfo()))) {
2714         // No use found, set dead for its def.
2715         assert(&*It == &StartMI && "No new def between StartMI and EndMI.");
2716         MO->setIsDead(true);
2717         break;
2718       }
2719     }
2720 
2721     if ((&*It) == &StartMI)
2722       break;
2723   }
2724   // Ensure RegMo liveness is killed after EndMI.
2725   assert((IsKillSet || (MO && MO->isDead())) &&
2726          "RegNo should be killed or dead");
2727 }
2728 
2729 // This opt tries to convert the following imm form to an index form to save an
2730 // add for stack variables.
2731 // Return false if no such pattern found.
2732 //
2733 // ADDI instr: ToBeChangedReg = ADDI FrameBaseReg, OffsetAddi
2734 // ADD instr:  ToBeDeletedReg = ADD ToBeChangedReg(killed), ScaleReg
2735 // Imm instr:  Reg            = op OffsetImm, ToBeDeletedReg(killed)
2736 //
2737 // can be converted to:
2738 //
2739 // new ADDI instr: ToBeChangedReg = ADDI FrameBaseReg, (OffsetAddi + OffsetImm)
2740 // Index instr:    Reg            = opx ScaleReg, ToBeChangedReg(killed)
2741 //
2742 // In order to eliminate ADD instr, make sure that:
2743 // 1: (OffsetAddi + OffsetImm) must be int16 since this offset will be used in
2744 //    new ADDI instr and ADDI can only take int16 Imm.
2745 // 2: ToBeChangedReg must be killed in ADD instr and there is no other use
2746 //    between ADDI and ADD instr since its original def in ADDI will be changed
2747 //    in new ADDI instr. And also there should be no new def for it between
2748 //    ADD and Imm instr as ToBeChangedReg will be used in Index instr.
2749 // 3: ToBeDeletedReg must be killed in Imm instr and there is no other use
2750 //    between ADD and Imm instr since ADD instr will be eliminated.
2751 // 4: ScaleReg must not be redefined between ADD and Imm instr since it will be
2752 //    moved to Index instr.
2753 bool PPCInstrInfo::foldFrameOffset(MachineInstr &MI) const {
2754   MachineFunction *MF = MI.getParent()->getParent();
2755   MachineRegisterInfo *MRI = &MF->getRegInfo();
2756   bool PostRA = !MRI->isSSA();
2757   // Do this opt after PEI which is after RA. The reason is stack slot expansion
2758   // in PEI may expose such opportunities since in PEI, stack slot offsets to
2759   // frame base(OffsetAddi) are determined.
2760   if (!PostRA)
2761     return false;
2762   unsigned ToBeDeletedReg = 0;
2763   int64_t OffsetImm = 0;
2764   unsigned XFormOpcode = 0;
2765   ImmInstrInfo III;
2766 
2767   // Check if Imm instr meets requirement.
2768   if (!isImmInstrEligibleForFolding(MI, ToBeDeletedReg, XFormOpcode, OffsetImm,
2769                                     III))
2770     return false;
2771 
2772   bool OtherIntermediateUse = false;
2773   MachineInstr *ADDMI = getDefMIPostRA(ToBeDeletedReg, MI, OtherIntermediateUse);
2774 
2775   // Exit if there is other use between ADD and Imm instr or no def found.
2776   if (OtherIntermediateUse || !ADDMI)
2777     return false;
2778 
2779   // Check if ADD instr meets requirement.
2780   if (!isADDInstrEligibleForFolding(*ADDMI))
2781     return false;
2782 
2783   unsigned ScaleRegIdx = 0;
2784   int64_t OffsetAddi = 0;
2785   MachineInstr *ADDIMI = nullptr;
2786 
2787   // Check if there is a valid ToBeChangedReg in ADDMI.
2788   // 1: It must be killed.
2789   // 2: Its definition must be a valid ADDIMI.
2790   // 3: It must satify int16 offset requirement.
2791   if (isValidToBeChangedReg(ADDMI, 1, ADDIMI, OffsetAddi, OffsetImm))
2792     ScaleRegIdx = 2;
2793   else if (isValidToBeChangedReg(ADDMI, 2, ADDIMI, OffsetAddi, OffsetImm))
2794     ScaleRegIdx = 1;
2795   else
2796     return false;
2797 
2798   assert(ADDIMI && "There should be ADDIMI for valid ToBeChangedReg.");
2799   unsigned ToBeChangedReg = ADDIMI->getOperand(0).getReg();
2800   unsigned ScaleReg = ADDMI->getOperand(ScaleRegIdx).getReg();
2801   auto NewDefFor = [&](unsigned Reg, MachineBasicBlock::iterator Start,
2802                        MachineBasicBlock::iterator End) {
2803     for (auto It = ++Start; It != End; It++)
2804       if (It->modifiesRegister(Reg, &getRegisterInfo()))
2805         return true;
2806     return false;
2807   };
2808 
2809   // We are trying to replace the ImmOpNo with ScaleReg. Give up if it is
2810   // treated as special zero when ScaleReg is R0/X0 register.
2811   if (III.ZeroIsSpecialOrig == III.ImmOpNo &&
2812       (ScaleReg == PPC::R0 || ScaleReg == PPC::X0))
2813     return false;
2814 
2815   // Make sure no other def for ToBeChangedReg and ScaleReg between ADD Instr
2816   // and Imm Instr.
2817   if (NewDefFor(ToBeChangedReg, *ADDMI, MI) || NewDefFor(ScaleReg, *ADDMI, MI))
2818     return false;
2819 
2820   // Now start to do the transformation.
2821   LLVM_DEBUG(dbgs() << "Replace instruction: "
2822                     << "\n");
2823   LLVM_DEBUG(ADDIMI->dump());
2824   LLVM_DEBUG(ADDMI->dump());
2825   LLVM_DEBUG(MI.dump());
2826   LLVM_DEBUG(dbgs() << "with: "
2827                     << "\n");
2828 
2829   // Update ADDI instr.
2830   ADDIMI->getOperand(2).setImm(OffsetAddi + OffsetImm);
2831 
2832   // Update Imm instr.
2833   MI.setDesc(get(XFormOpcode));
2834   MI.getOperand(III.ImmOpNo)
2835       .ChangeToRegister(ScaleReg, false, false,
2836                         ADDMI->getOperand(ScaleRegIdx).isKill());
2837 
2838   MI.getOperand(III.OpNoForForwarding)
2839       .ChangeToRegister(ToBeChangedReg, false, false, true);
2840 
2841   // Eliminate ADD instr.
2842   ADDMI->eraseFromParent();
2843 
2844   LLVM_DEBUG(ADDIMI->dump());
2845   LLVM_DEBUG(MI.dump());
2846 
2847   return true;
2848 }
2849 
2850 bool PPCInstrInfo::isADDIInstrEligibleForFolding(MachineInstr &ADDIMI,
2851                                                  int64_t &Imm) const {
2852   unsigned Opc = ADDIMI.getOpcode();
2853 
2854   // Exit if the instruction is not ADDI.
2855   if (Opc != PPC::ADDI && Opc != PPC::ADDI8)
2856     return false;
2857 
2858   // The operand may not necessarily be an immediate - it could be a relocation.
2859   if (!ADDIMI.getOperand(2).isImm())
2860     return false;
2861 
2862   Imm = ADDIMI.getOperand(2).getImm();
2863 
2864   return true;
2865 }
2866 
2867 bool PPCInstrInfo::isADDInstrEligibleForFolding(MachineInstr &ADDMI) const {
2868   unsigned Opc = ADDMI.getOpcode();
2869 
2870   // Exit if the instruction is not ADD.
2871   return Opc == PPC::ADD4 || Opc == PPC::ADD8;
2872 }
2873 
2874 bool PPCInstrInfo::isImmInstrEligibleForFolding(MachineInstr &MI,
2875                                                 unsigned &ToBeDeletedReg,
2876                                                 unsigned &XFormOpcode,
2877                                                 int64_t &OffsetImm,
2878                                                 ImmInstrInfo &III) const {
2879   // Only handle load/store.
2880   if (!MI.mayLoadOrStore())
2881     return false;
2882 
2883   unsigned Opc = MI.getOpcode();
2884 
2885   XFormOpcode = RI.getMappedIdxOpcForImmOpc(Opc);
2886 
2887   // Exit if instruction has no index form.
2888   if (XFormOpcode == PPC::INSTRUCTION_LIST_END)
2889     return false;
2890 
2891   // TODO: sync the logic between instrHasImmForm() and ImmToIdxMap.
2892   if (!instrHasImmForm(XFormOpcode, isVFRegister(MI.getOperand(0).getReg()),
2893                        III, true))
2894     return false;
2895 
2896   if (!III.IsSummingOperands)
2897     return false;
2898 
2899   MachineOperand ImmOperand = MI.getOperand(III.ImmOpNo);
2900   MachineOperand RegOperand = MI.getOperand(III.OpNoForForwarding);
2901   // Only support imm operands, not relocation slots or others.
2902   if (!ImmOperand.isImm())
2903     return false;
2904 
2905   assert(RegOperand.isReg() && "Instruction format is not right");
2906 
2907   // There are other use for ToBeDeletedReg after Imm instr, can not delete it.
2908   if (!RegOperand.isKill())
2909     return false;
2910 
2911   ToBeDeletedReg = RegOperand.getReg();
2912   OffsetImm = ImmOperand.getImm();
2913 
2914   return true;
2915 }
2916 
2917 bool PPCInstrInfo::isValidToBeChangedReg(MachineInstr *ADDMI, unsigned Index,
2918                                          MachineInstr *&ADDIMI,
2919                                          int64_t &OffsetAddi,
2920                                          int64_t OffsetImm) const {
2921   assert((Index == 1 || Index == 2) && "Invalid operand index for add.");
2922   MachineOperand &MO = ADDMI->getOperand(Index);
2923 
2924   if (!MO.isKill())
2925     return false;
2926 
2927   bool OtherIntermediateUse = false;
2928 
2929   ADDIMI = getDefMIPostRA(MO.getReg(), *ADDMI, OtherIntermediateUse);
2930   // Currently handle only one "add + Imminstr" pair case, exit if other
2931   // intermediate use for ToBeChangedReg found.
2932   // TODO: handle the cases where there are other "add + Imminstr" pairs
2933   // with same offset in Imminstr which is like:
2934   //
2935   // ADDI instr: ToBeChangedReg  = ADDI FrameBaseReg, OffsetAddi
2936   // ADD instr1: ToBeDeletedReg1 = ADD ToBeChangedReg, ScaleReg1
2937   // Imm instr1: Reg1            = op1 OffsetImm, ToBeDeletedReg1(killed)
2938   // ADD instr2: ToBeDeletedReg2 = ADD ToBeChangedReg(killed), ScaleReg2
2939   // Imm instr2: Reg2            = op2 OffsetImm, ToBeDeletedReg2(killed)
2940   //
2941   // can be converted to:
2942   //
2943   // new ADDI instr: ToBeChangedReg = ADDI FrameBaseReg,
2944   //                                       (OffsetAddi + OffsetImm)
2945   // Index instr1:   Reg1           = opx1 ScaleReg1, ToBeChangedReg
2946   // Index instr2:   Reg2           = opx2 ScaleReg2, ToBeChangedReg(killed)
2947 
2948   if (OtherIntermediateUse || !ADDIMI)
2949     return false;
2950   // Check if ADDI instr meets requirement.
2951   if (!isADDIInstrEligibleForFolding(*ADDIMI, OffsetAddi))
2952     return false;
2953 
2954   if (isInt<16>(OffsetAddi + OffsetImm))
2955     return true;
2956   return false;
2957 }
2958 
2959 // If this instruction has an immediate form and one of its operands is a
2960 // result of a load-immediate or an add-immediate, convert it to
2961 // the immediate form if the constant is in range.
2962 bool PPCInstrInfo::convertToImmediateForm(MachineInstr &MI,
2963                                           MachineInstr **KilledDef) const {
2964   MachineFunction *MF = MI.getParent()->getParent();
2965   MachineRegisterInfo *MRI = &MF->getRegInfo();
2966   bool PostRA = !MRI->isSSA();
2967   bool SeenIntermediateUse = true;
2968   unsigned ForwardingOperand = ~0U;
2969   MachineInstr *DefMI = getForwardingDefMI(MI, ForwardingOperand,
2970                                            SeenIntermediateUse);
2971   if (!DefMI)
2972     return false;
2973   assert(ForwardingOperand < MI.getNumOperands() &&
2974          "The forwarding operand needs to be valid at this point");
2975   bool IsForwardingOperandKilled = MI.getOperand(ForwardingOperand).isKill();
2976   bool KillFwdDefMI = !SeenIntermediateUse && IsForwardingOperandKilled;
2977   if (KilledDef && KillFwdDefMI)
2978     *KilledDef = DefMI;
2979 
2980   // If this is a imm instruction and its register operands is produced by ADDI,
2981   // put the imm into imm inst directly.
2982   if (RI.getMappedIdxOpcForImmOpc(MI.getOpcode()) !=
2983           PPC::INSTRUCTION_LIST_END &&
2984       transformToNewImmFormFedByAdd(MI, *DefMI, ForwardingOperand))
2985     return true;
2986 
2987   ImmInstrInfo III;
2988   bool IsVFReg = MI.getOperand(0).isReg()
2989                      ? isVFRegister(MI.getOperand(0).getReg())
2990                      : false;
2991   bool HasImmForm = instrHasImmForm(MI.getOpcode(), IsVFReg, III, PostRA);
2992   // If this is a reg+reg instruction that has a reg+imm form,
2993   // and one of the operands is produced by an add-immediate,
2994   // try to convert it.
2995   if (HasImmForm &&
2996       transformToImmFormFedByAdd(MI, III, ForwardingOperand, *DefMI,
2997                                  KillFwdDefMI))
2998     return true;
2999 
3000   // If this is a reg+reg instruction that has a reg+imm form,
3001   // and one of the operands is produced by LI, convert it now.
3002   if (HasImmForm &&
3003       transformToImmFormFedByLI(MI, III, ForwardingOperand, *DefMI))
3004     return true;
3005 
3006   // If this is not a reg+reg, but the DefMI is LI/LI8, check if its user MI
3007   // can be simpified to LI.
3008   if (!HasImmForm && simplifyToLI(MI, *DefMI, ForwardingOperand, KilledDef))
3009     return true;
3010 
3011   return false;
3012 }
3013 
3014 bool PPCInstrInfo::instrHasImmForm(unsigned Opc, bool IsVFReg,
3015                                    ImmInstrInfo &III, bool PostRA) const {
3016   // The vast majority of the instructions would need their operand 2 replaced
3017   // with an immediate when switching to the reg+imm form. A marked exception
3018   // are the update form loads/stores for which a constant operand 2 would need
3019   // to turn into a displacement and move operand 1 to the operand 2 position.
3020   III.ImmOpNo = 2;
3021   III.OpNoForForwarding = 2;
3022   III.ImmWidth = 16;
3023   III.ImmMustBeMultipleOf = 1;
3024   III.TruncateImmTo = 0;
3025   III.IsSummingOperands = false;
3026   switch (Opc) {
3027   default: return false;
3028   case PPC::ADD4:
3029   case PPC::ADD8:
3030     III.SignedImm = true;
3031     III.ZeroIsSpecialOrig = 0;
3032     III.ZeroIsSpecialNew = 1;
3033     III.IsCommutative = true;
3034     III.IsSummingOperands = true;
3035     III.ImmOpcode = Opc == PPC::ADD4 ? PPC::ADDI : PPC::ADDI8;
3036     break;
3037   case PPC::ADDC:
3038   case PPC::ADDC8:
3039     III.SignedImm = true;
3040     III.ZeroIsSpecialOrig = 0;
3041     III.ZeroIsSpecialNew = 0;
3042     III.IsCommutative = true;
3043     III.IsSummingOperands = true;
3044     III.ImmOpcode = Opc == PPC::ADDC ? PPC::ADDIC : PPC::ADDIC8;
3045     break;
3046   case PPC::ADDC_rec:
3047     III.SignedImm = true;
3048     III.ZeroIsSpecialOrig = 0;
3049     III.ZeroIsSpecialNew = 0;
3050     III.IsCommutative = true;
3051     III.IsSummingOperands = true;
3052     III.ImmOpcode = PPC::ADDIC_rec;
3053     break;
3054   case PPC::SUBFC:
3055   case PPC::SUBFC8:
3056     III.SignedImm = true;
3057     III.ZeroIsSpecialOrig = 0;
3058     III.ZeroIsSpecialNew = 0;
3059     III.IsCommutative = false;
3060     III.ImmOpcode = Opc == PPC::SUBFC ? PPC::SUBFIC : PPC::SUBFIC8;
3061     break;
3062   case PPC::CMPW:
3063   case PPC::CMPD:
3064     III.SignedImm = true;
3065     III.ZeroIsSpecialOrig = 0;
3066     III.ZeroIsSpecialNew = 0;
3067     III.IsCommutative = false;
3068     III.ImmOpcode = Opc == PPC::CMPW ? PPC::CMPWI : PPC::CMPDI;
3069     break;
3070   case PPC::CMPLW:
3071   case PPC::CMPLD:
3072     III.SignedImm = false;
3073     III.ZeroIsSpecialOrig = 0;
3074     III.ZeroIsSpecialNew = 0;
3075     III.IsCommutative = false;
3076     III.ImmOpcode = Opc == PPC::CMPLW ? PPC::CMPLWI : PPC::CMPLDI;
3077     break;
3078   case PPC::AND_rec:
3079   case PPC::AND8_rec:
3080   case PPC::OR:
3081   case PPC::OR8:
3082   case PPC::XOR:
3083   case PPC::XOR8:
3084     III.SignedImm = false;
3085     III.ZeroIsSpecialOrig = 0;
3086     III.ZeroIsSpecialNew = 0;
3087     III.IsCommutative = true;
3088     switch(Opc) {
3089     default: llvm_unreachable("Unknown opcode");
3090     case PPC::AND_rec:
3091       III.ImmOpcode = PPC::ANDI_rec;
3092       break;
3093     case PPC::AND8_rec:
3094       III.ImmOpcode = PPC::ANDI8_rec;
3095       break;
3096     case PPC::OR: III.ImmOpcode = PPC::ORI; break;
3097     case PPC::OR8: III.ImmOpcode = PPC::ORI8; break;
3098     case PPC::XOR: III.ImmOpcode = PPC::XORI; break;
3099     case PPC::XOR8: III.ImmOpcode = PPC::XORI8; break;
3100     }
3101     break;
3102   case PPC::RLWNM:
3103   case PPC::RLWNM8:
3104   case PPC::RLWNM_rec:
3105   case PPC::RLWNM8_rec:
3106   case PPC::SLW:
3107   case PPC::SLW8:
3108   case PPC::SLW_rec:
3109   case PPC::SLW8_rec:
3110   case PPC::SRW:
3111   case PPC::SRW8:
3112   case PPC::SRW_rec:
3113   case PPC::SRW8_rec:
3114   case PPC::SRAW:
3115   case PPC::SRAW_rec:
3116     III.SignedImm = false;
3117     III.ZeroIsSpecialOrig = 0;
3118     III.ZeroIsSpecialNew = 0;
3119     III.IsCommutative = false;
3120     // This isn't actually true, but the instructions ignore any of the
3121     // upper bits, so any immediate loaded with an LI is acceptable.
3122     // This does not apply to shift right algebraic because a value
3123     // out of range will produce a -1/0.
3124     III.ImmWidth = 16;
3125     if (Opc == PPC::RLWNM || Opc == PPC::RLWNM8 || Opc == PPC::RLWNM_rec ||
3126         Opc == PPC::RLWNM8_rec)
3127       III.TruncateImmTo = 5;
3128     else
3129       III.TruncateImmTo = 6;
3130     switch(Opc) {
3131     default: llvm_unreachable("Unknown opcode");
3132     case PPC::RLWNM: III.ImmOpcode = PPC::RLWINM; break;
3133     case PPC::RLWNM8: III.ImmOpcode = PPC::RLWINM8; break;
3134     case PPC::RLWNM_rec:
3135       III.ImmOpcode = PPC::RLWINM_rec;
3136       break;
3137     case PPC::RLWNM8_rec:
3138       III.ImmOpcode = PPC::RLWINM8_rec;
3139       break;
3140     case PPC::SLW: III.ImmOpcode = PPC::RLWINM; break;
3141     case PPC::SLW8: III.ImmOpcode = PPC::RLWINM8; break;
3142     case PPC::SLW_rec:
3143       III.ImmOpcode = PPC::RLWINM_rec;
3144       break;
3145     case PPC::SLW8_rec:
3146       III.ImmOpcode = PPC::RLWINM8_rec;
3147       break;
3148     case PPC::SRW: III.ImmOpcode = PPC::RLWINM; break;
3149     case PPC::SRW8: III.ImmOpcode = PPC::RLWINM8; break;
3150     case PPC::SRW_rec:
3151       III.ImmOpcode = PPC::RLWINM_rec;
3152       break;
3153     case PPC::SRW8_rec:
3154       III.ImmOpcode = PPC::RLWINM8_rec;
3155       break;
3156     case PPC::SRAW:
3157       III.ImmWidth = 5;
3158       III.TruncateImmTo = 0;
3159       III.ImmOpcode = PPC::SRAWI;
3160       break;
3161     case PPC::SRAW_rec:
3162       III.ImmWidth = 5;
3163       III.TruncateImmTo = 0;
3164       III.ImmOpcode = PPC::SRAWI_rec;
3165       break;
3166     }
3167     break;
3168   case PPC::RLDCL:
3169   case PPC::RLDCL_rec:
3170   case PPC::RLDCR:
3171   case PPC::RLDCR_rec:
3172   case PPC::SLD:
3173   case PPC::SLD_rec:
3174   case PPC::SRD:
3175   case PPC::SRD_rec:
3176   case PPC::SRAD:
3177   case PPC::SRAD_rec:
3178     III.SignedImm = false;
3179     III.ZeroIsSpecialOrig = 0;
3180     III.ZeroIsSpecialNew = 0;
3181     III.IsCommutative = false;
3182     // This isn't actually true, but the instructions ignore any of the
3183     // upper bits, so any immediate loaded with an LI is acceptable.
3184     // This does not apply to shift right algebraic because a value
3185     // out of range will produce a -1/0.
3186     III.ImmWidth = 16;
3187     if (Opc == PPC::RLDCL || Opc == PPC::RLDCL_rec || Opc == PPC::RLDCR ||
3188         Opc == PPC::RLDCR_rec)
3189       III.TruncateImmTo = 6;
3190     else
3191       III.TruncateImmTo = 7;
3192     switch(Opc) {
3193     default: llvm_unreachable("Unknown opcode");
3194     case PPC::RLDCL: III.ImmOpcode = PPC::RLDICL; break;
3195     case PPC::RLDCL_rec:
3196       III.ImmOpcode = PPC::RLDICL_rec;
3197       break;
3198     case PPC::RLDCR: III.ImmOpcode = PPC::RLDICR; break;
3199     case PPC::RLDCR_rec:
3200       III.ImmOpcode = PPC::RLDICR_rec;
3201       break;
3202     case PPC::SLD: III.ImmOpcode = PPC::RLDICR; break;
3203     case PPC::SLD_rec:
3204       III.ImmOpcode = PPC::RLDICR_rec;
3205       break;
3206     case PPC::SRD: III.ImmOpcode = PPC::RLDICL; break;
3207     case PPC::SRD_rec:
3208       III.ImmOpcode = PPC::RLDICL_rec;
3209       break;
3210     case PPC::SRAD:
3211       III.ImmWidth = 6;
3212       III.TruncateImmTo = 0;
3213       III.ImmOpcode = PPC::SRADI;
3214        break;
3215     case PPC::SRAD_rec:
3216       III.ImmWidth = 6;
3217       III.TruncateImmTo = 0;
3218       III.ImmOpcode = PPC::SRADI_rec;
3219       break;
3220     }
3221     break;
3222   // Loads and stores:
3223   case PPC::LBZX:
3224   case PPC::LBZX8:
3225   case PPC::LHZX:
3226   case PPC::LHZX8:
3227   case PPC::LHAX:
3228   case PPC::LHAX8:
3229   case PPC::LWZX:
3230   case PPC::LWZX8:
3231   case PPC::LWAX:
3232   case PPC::LDX:
3233   case PPC::LFSX:
3234   case PPC::LFDX:
3235   case PPC::STBX:
3236   case PPC::STBX8:
3237   case PPC::STHX:
3238   case PPC::STHX8:
3239   case PPC::STWX:
3240   case PPC::STWX8:
3241   case PPC::STDX:
3242   case PPC::STFSX:
3243   case PPC::STFDX:
3244     III.SignedImm = true;
3245     III.ZeroIsSpecialOrig = 1;
3246     III.ZeroIsSpecialNew = 2;
3247     III.IsCommutative = true;
3248     III.IsSummingOperands = true;
3249     III.ImmOpNo = 1;
3250     III.OpNoForForwarding = 2;
3251     switch(Opc) {
3252     default: llvm_unreachable("Unknown opcode");
3253     case PPC::LBZX: III.ImmOpcode = PPC::LBZ; break;
3254     case PPC::LBZX8: III.ImmOpcode = PPC::LBZ8; break;
3255     case PPC::LHZX: III.ImmOpcode = PPC::LHZ; break;
3256     case PPC::LHZX8: III.ImmOpcode = PPC::LHZ8; break;
3257     case PPC::LHAX: III.ImmOpcode = PPC::LHA; break;
3258     case PPC::LHAX8: III.ImmOpcode = PPC::LHA8; break;
3259     case PPC::LWZX: III.ImmOpcode = PPC::LWZ; break;
3260     case PPC::LWZX8: III.ImmOpcode = PPC::LWZ8; break;
3261     case PPC::LWAX:
3262       III.ImmOpcode = PPC::LWA;
3263       III.ImmMustBeMultipleOf = 4;
3264       break;
3265     case PPC::LDX: III.ImmOpcode = PPC::LD; III.ImmMustBeMultipleOf = 4; break;
3266     case PPC::LFSX: III.ImmOpcode = PPC::LFS; break;
3267     case PPC::LFDX: III.ImmOpcode = PPC::LFD; break;
3268     case PPC::STBX: III.ImmOpcode = PPC::STB; break;
3269     case PPC::STBX8: III.ImmOpcode = PPC::STB8; break;
3270     case PPC::STHX: III.ImmOpcode = PPC::STH; break;
3271     case PPC::STHX8: III.ImmOpcode = PPC::STH8; break;
3272     case PPC::STWX: III.ImmOpcode = PPC::STW; break;
3273     case PPC::STWX8: III.ImmOpcode = PPC::STW8; break;
3274     case PPC::STDX:
3275       III.ImmOpcode = PPC::STD;
3276       III.ImmMustBeMultipleOf = 4;
3277       break;
3278     case PPC::STFSX: III.ImmOpcode = PPC::STFS; break;
3279     case PPC::STFDX: III.ImmOpcode = PPC::STFD; break;
3280     }
3281     break;
3282   case PPC::LBZUX:
3283   case PPC::LBZUX8:
3284   case PPC::LHZUX:
3285   case PPC::LHZUX8:
3286   case PPC::LHAUX:
3287   case PPC::LHAUX8:
3288   case PPC::LWZUX:
3289   case PPC::LWZUX8:
3290   case PPC::LDUX:
3291   case PPC::LFSUX:
3292   case PPC::LFDUX:
3293   case PPC::STBUX:
3294   case PPC::STBUX8:
3295   case PPC::STHUX:
3296   case PPC::STHUX8:
3297   case PPC::STWUX:
3298   case PPC::STWUX8:
3299   case PPC::STDUX:
3300   case PPC::STFSUX:
3301   case PPC::STFDUX:
3302     III.SignedImm = true;
3303     III.ZeroIsSpecialOrig = 2;
3304     III.ZeroIsSpecialNew = 3;
3305     III.IsCommutative = false;
3306     III.IsSummingOperands = true;
3307     III.ImmOpNo = 2;
3308     III.OpNoForForwarding = 3;
3309     switch(Opc) {
3310     default: llvm_unreachable("Unknown opcode");
3311     case PPC::LBZUX: III.ImmOpcode = PPC::LBZU; break;
3312     case PPC::LBZUX8: III.ImmOpcode = PPC::LBZU8; break;
3313     case PPC::LHZUX: III.ImmOpcode = PPC::LHZU; break;
3314     case PPC::LHZUX8: III.ImmOpcode = PPC::LHZU8; break;
3315     case PPC::LHAUX: III.ImmOpcode = PPC::LHAU; break;
3316     case PPC::LHAUX8: III.ImmOpcode = PPC::LHAU8; break;
3317     case PPC::LWZUX: III.ImmOpcode = PPC::LWZU; break;
3318     case PPC::LWZUX8: III.ImmOpcode = PPC::LWZU8; break;
3319     case PPC::LDUX:
3320       III.ImmOpcode = PPC::LDU;
3321       III.ImmMustBeMultipleOf = 4;
3322       break;
3323     case PPC::LFSUX: III.ImmOpcode = PPC::LFSU; break;
3324     case PPC::LFDUX: III.ImmOpcode = PPC::LFDU; break;
3325     case PPC::STBUX: III.ImmOpcode = PPC::STBU; break;
3326     case PPC::STBUX8: III.ImmOpcode = PPC::STBU8; break;
3327     case PPC::STHUX: III.ImmOpcode = PPC::STHU; break;
3328     case PPC::STHUX8: III.ImmOpcode = PPC::STHU8; break;
3329     case PPC::STWUX: III.ImmOpcode = PPC::STWU; break;
3330     case PPC::STWUX8: III.ImmOpcode = PPC::STWU8; break;
3331     case PPC::STDUX:
3332       III.ImmOpcode = PPC::STDU;
3333       III.ImmMustBeMultipleOf = 4;
3334       break;
3335     case PPC::STFSUX: III.ImmOpcode = PPC::STFSU; break;
3336     case PPC::STFDUX: III.ImmOpcode = PPC::STFDU; break;
3337     }
3338     break;
3339   // Power9 and up only. For some of these, the X-Form version has access to all
3340   // 64 VSR's whereas the D-Form only has access to the VR's. We replace those
3341   // with pseudo-ops pre-ra and for post-ra, we check that the register loaded
3342   // into or stored from is one of the VR registers.
3343   case PPC::LXVX:
3344   case PPC::LXSSPX:
3345   case PPC::LXSDX:
3346   case PPC::STXVX:
3347   case PPC::STXSSPX:
3348   case PPC::STXSDX:
3349   case PPC::XFLOADf32:
3350   case PPC::XFLOADf64:
3351   case PPC::XFSTOREf32:
3352   case PPC::XFSTOREf64:
3353     if (!Subtarget.hasP9Vector())
3354       return false;
3355     III.SignedImm = true;
3356     III.ZeroIsSpecialOrig = 1;
3357     III.ZeroIsSpecialNew = 2;
3358     III.IsCommutative = true;
3359     III.IsSummingOperands = true;
3360     III.ImmOpNo = 1;
3361     III.OpNoForForwarding = 2;
3362     III.ImmMustBeMultipleOf = 4;
3363     switch(Opc) {
3364     default: llvm_unreachable("Unknown opcode");
3365     case PPC::LXVX:
3366       III.ImmOpcode = PPC::LXV;
3367       III.ImmMustBeMultipleOf = 16;
3368       break;
3369     case PPC::LXSSPX:
3370       if (PostRA) {
3371         if (IsVFReg)
3372           III.ImmOpcode = PPC::LXSSP;
3373         else {
3374           III.ImmOpcode = PPC::LFS;
3375           III.ImmMustBeMultipleOf = 1;
3376         }
3377         break;
3378       }
3379       LLVM_FALLTHROUGH;
3380     case PPC::XFLOADf32:
3381       III.ImmOpcode = PPC::DFLOADf32;
3382       break;
3383     case PPC::LXSDX:
3384       if (PostRA) {
3385         if (IsVFReg)
3386           III.ImmOpcode = PPC::LXSD;
3387         else {
3388           III.ImmOpcode = PPC::LFD;
3389           III.ImmMustBeMultipleOf = 1;
3390         }
3391         break;
3392       }
3393       LLVM_FALLTHROUGH;
3394     case PPC::XFLOADf64:
3395       III.ImmOpcode = PPC::DFLOADf64;
3396       break;
3397     case PPC::STXVX:
3398       III.ImmOpcode = PPC::STXV;
3399       III.ImmMustBeMultipleOf = 16;
3400       break;
3401     case PPC::STXSSPX:
3402       if (PostRA) {
3403         if (IsVFReg)
3404           III.ImmOpcode = PPC::STXSSP;
3405         else {
3406           III.ImmOpcode = PPC::STFS;
3407           III.ImmMustBeMultipleOf = 1;
3408         }
3409         break;
3410       }
3411       LLVM_FALLTHROUGH;
3412     case PPC::XFSTOREf32:
3413       III.ImmOpcode = PPC::DFSTOREf32;
3414       break;
3415     case PPC::STXSDX:
3416       if (PostRA) {
3417         if (IsVFReg)
3418           III.ImmOpcode = PPC::STXSD;
3419         else {
3420           III.ImmOpcode = PPC::STFD;
3421           III.ImmMustBeMultipleOf = 1;
3422         }
3423         break;
3424       }
3425       LLVM_FALLTHROUGH;
3426     case PPC::XFSTOREf64:
3427       III.ImmOpcode = PPC::DFSTOREf64;
3428       break;
3429     }
3430     break;
3431   }
3432   return true;
3433 }
3434 
3435 // Utility function for swaping two arbitrary operands of an instruction.
3436 static void swapMIOperands(MachineInstr &MI, unsigned Op1, unsigned Op2) {
3437   assert(Op1 != Op2 && "Cannot swap operand with itself.");
3438 
3439   unsigned MaxOp = std::max(Op1, Op2);
3440   unsigned MinOp = std::min(Op1, Op2);
3441   MachineOperand MOp1 = MI.getOperand(MinOp);
3442   MachineOperand MOp2 = MI.getOperand(MaxOp);
3443   MI.RemoveOperand(std::max(Op1, Op2));
3444   MI.RemoveOperand(std::min(Op1, Op2));
3445 
3446   // If the operands we are swapping are the two at the end (the common case)
3447   // we can just remove both and add them in the opposite order.
3448   if (MaxOp - MinOp == 1 && MI.getNumOperands() == MinOp) {
3449     MI.addOperand(MOp2);
3450     MI.addOperand(MOp1);
3451   } else {
3452     // Store all operands in a temporary vector, remove them and re-add in the
3453     // right order.
3454     SmallVector<MachineOperand, 2> MOps;
3455     unsigned TotalOps = MI.getNumOperands() + 2; // We've already removed 2 ops.
3456     for (unsigned i = MI.getNumOperands() - 1; i >= MinOp; i--) {
3457       MOps.push_back(MI.getOperand(i));
3458       MI.RemoveOperand(i);
3459     }
3460     // MOp2 needs to be added next.
3461     MI.addOperand(MOp2);
3462     // Now add the rest.
3463     for (unsigned i = MI.getNumOperands(); i < TotalOps; i++) {
3464       if (i == MaxOp)
3465         MI.addOperand(MOp1);
3466       else {
3467         MI.addOperand(MOps.back());
3468         MOps.pop_back();
3469       }
3470     }
3471   }
3472 }
3473 
3474 // Check if the 'MI' that has the index OpNoForForwarding
3475 // meets the requirement described in the ImmInstrInfo.
3476 bool PPCInstrInfo::isUseMIElgibleForForwarding(MachineInstr &MI,
3477                                                const ImmInstrInfo &III,
3478                                                unsigned OpNoForForwarding
3479                                                ) const {
3480   // As the algorithm of checking for PPC::ZERO/PPC::ZERO8
3481   // would not work pre-RA, we can only do the check post RA.
3482   MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
3483   if (MRI.isSSA())
3484     return false;
3485 
3486   // Cannot do the transform if MI isn't summing the operands.
3487   if (!III.IsSummingOperands)
3488     return false;
3489 
3490   // The instruction we are trying to replace must have the ZeroIsSpecialOrig set.
3491   if (!III.ZeroIsSpecialOrig)
3492     return false;
3493 
3494   // We cannot do the transform if the operand we are trying to replace
3495   // isn't the same as the operand the instruction allows.
3496   if (OpNoForForwarding != III.OpNoForForwarding)
3497     return false;
3498 
3499   // Check if the instruction we are trying to transform really has
3500   // the special zero register as its operand.
3501   if (MI.getOperand(III.ZeroIsSpecialOrig).getReg() != PPC::ZERO &&
3502       MI.getOperand(III.ZeroIsSpecialOrig).getReg() != PPC::ZERO8)
3503     return false;
3504 
3505   // This machine instruction is convertible if it is,
3506   // 1. summing the operands.
3507   // 2. one of the operands is special zero register.
3508   // 3. the operand we are trying to replace is allowed by the MI.
3509   return true;
3510 }
3511 
3512 // Check if the DefMI is the add inst and set the ImmMO and RegMO
3513 // accordingly.
3514 bool PPCInstrInfo::isDefMIElgibleForForwarding(MachineInstr &DefMI,
3515                                                const ImmInstrInfo &III,
3516                                                MachineOperand *&ImmMO,
3517                                                MachineOperand *&RegMO) const {
3518   unsigned Opc = DefMI.getOpcode();
3519   if (Opc != PPC::ADDItocL && Opc != PPC::ADDI && Opc != PPC::ADDI8)
3520     return false;
3521 
3522   assert(DefMI.getNumOperands() >= 3 &&
3523          "Add inst must have at least three operands");
3524   RegMO = &DefMI.getOperand(1);
3525   ImmMO = &DefMI.getOperand(2);
3526 
3527   // Before RA, ADDI first operand could be a frame index.
3528   if (!RegMO->isReg())
3529     return false;
3530 
3531   // This DefMI is elgible for forwarding if it is:
3532   // 1. add inst
3533   // 2. one of the operands is Imm/CPI/Global.
3534   return isAnImmediateOperand(*ImmMO);
3535 }
3536 
3537 bool PPCInstrInfo::isRegElgibleForForwarding(
3538     const MachineOperand &RegMO, const MachineInstr &DefMI,
3539     const MachineInstr &MI, bool KillDefMI,
3540     bool &IsFwdFeederRegKilled) const {
3541   // x = addi y, imm
3542   // ...
3543   // z = lfdx 0, x   -> z = lfd imm(y)
3544   // The Reg "y" can be forwarded to the MI(z) only when there is no DEF
3545   // of "y" between the DEF of "x" and "z".
3546   // The query is only valid post RA.
3547   const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
3548   if (MRI.isSSA())
3549     return false;
3550 
3551   Register Reg = RegMO.getReg();
3552 
3553   // Walking the inst in reverse(MI-->DefMI) to get the last DEF of the Reg.
3554   MachineBasicBlock::const_reverse_iterator It = MI;
3555   MachineBasicBlock::const_reverse_iterator E = MI.getParent()->rend();
3556   It++;
3557   for (; It != E; ++It) {
3558     if (It->modifiesRegister(Reg, &getRegisterInfo()) && (&*It) != &DefMI)
3559       return false;
3560     else if (It->killsRegister(Reg, &getRegisterInfo()) && (&*It) != &DefMI)
3561       IsFwdFeederRegKilled = true;
3562     // Made it to DefMI without encountering a clobber.
3563     if ((&*It) == &DefMI)
3564       break;
3565   }
3566   assert((&*It) == &DefMI && "DefMI is missing");
3567 
3568   // If DefMI also defines the register to be forwarded, we can only forward it
3569   // if DefMI is being erased.
3570   if (DefMI.modifiesRegister(Reg, &getRegisterInfo()))
3571     return KillDefMI;
3572 
3573   return true;
3574 }
3575 
3576 bool PPCInstrInfo::isImmElgibleForForwarding(const MachineOperand &ImmMO,
3577                                              const MachineInstr &DefMI,
3578                                              const ImmInstrInfo &III,
3579                                              int64_t &Imm,
3580                                              int64_t BaseImm) const {
3581   assert(isAnImmediateOperand(ImmMO) && "ImmMO is NOT an immediate");
3582   if (DefMI.getOpcode() == PPC::ADDItocL) {
3583     // The operand for ADDItocL is CPI, which isn't imm at compiling time,
3584     // However, we know that, it is 16-bit width, and has the alignment of 4.
3585     // Check if the instruction met the requirement.
3586     if (III.ImmMustBeMultipleOf > 4 ||
3587        III.TruncateImmTo || III.ImmWidth != 16)
3588       return false;
3589 
3590     // Going from XForm to DForm loads means that the displacement needs to be
3591     // not just an immediate but also a multiple of 4, or 16 depending on the
3592     // load. A DForm load cannot be represented if it is a multiple of say 2.
3593     // XForm loads do not have this restriction.
3594     if (ImmMO.isGlobal()) {
3595       const DataLayout &DL = ImmMO.getGlobal()->getParent()->getDataLayout();
3596       if (ImmMO.getGlobal()->getPointerAlignment(DL) < III.ImmMustBeMultipleOf)
3597         return false;
3598     }
3599 
3600     return true;
3601   }
3602 
3603   if (ImmMO.isImm()) {
3604     // It is Imm, we need to check if the Imm fit the range.
3605     // Sign-extend to 64-bits.
3606     // DefMI may be folded with another imm form instruction, the result Imm is
3607     // the sum of Imm of DefMI and BaseImm which is from imm form instruction.
3608     Imm = SignExtend64<16>(ImmMO.getImm() + BaseImm);
3609 
3610     if (Imm % III.ImmMustBeMultipleOf)
3611       return false;
3612     if (III.TruncateImmTo)
3613       Imm &= ((1 << III.TruncateImmTo) - 1);
3614     if (III.SignedImm) {
3615       APInt ActualValue(64, Imm, true);
3616       if (!ActualValue.isSignedIntN(III.ImmWidth))
3617         return false;
3618     } else {
3619       uint64_t UnsignedMax = (1 << III.ImmWidth) - 1;
3620       if ((uint64_t)Imm > UnsignedMax)
3621         return false;
3622     }
3623   }
3624   else
3625     return false;
3626 
3627   // This ImmMO is forwarded if it meets the requriement describle
3628   // in ImmInstrInfo
3629   return true;
3630 }
3631 
3632 bool PPCInstrInfo::simplifyToLI(MachineInstr &MI, MachineInstr &DefMI,
3633                                 unsigned OpNoForForwarding,
3634                                 MachineInstr **KilledDef) const {
3635   if ((DefMI.getOpcode() != PPC::LI && DefMI.getOpcode() != PPC::LI8) ||
3636       !DefMI.getOperand(1).isImm())
3637     return false;
3638 
3639   MachineFunction *MF = MI.getParent()->getParent();
3640   MachineRegisterInfo *MRI = &MF->getRegInfo();
3641   bool PostRA = !MRI->isSSA();
3642 
3643   int64_t Immediate = DefMI.getOperand(1).getImm();
3644   // Sign-extend to 64-bits.
3645   int64_t SExtImm = SignExtend64<16>(Immediate);
3646 
3647   bool IsForwardingOperandKilled = MI.getOperand(OpNoForForwarding).isKill();
3648   Register ForwardingOperandReg = MI.getOperand(OpNoForForwarding).getReg();
3649 
3650   bool ReplaceWithLI = false;
3651   bool Is64BitLI = false;
3652   int64_t NewImm = 0;
3653   bool SetCR = false;
3654   unsigned Opc = MI.getOpcode();
3655   switch (Opc) {
3656   default:
3657     return false;
3658 
3659   // FIXME: Any branches conditional on such a comparison can be made
3660   // unconditional. At this time, this happens too infrequently to be worth
3661   // the implementation effort, but if that ever changes, we could convert
3662   // such a pattern here.
3663   case PPC::CMPWI:
3664   case PPC::CMPLWI:
3665   case PPC::CMPDI:
3666   case PPC::CMPLDI: {
3667     // Doing this post-RA would require dataflow analysis to reliably find uses
3668     // of the CR register set by the compare.
3669     // No need to fixup killed/dead flag since this transformation is only valid
3670     // before RA.
3671     if (PostRA)
3672       return false;
3673     // If a compare-immediate is fed by an immediate and is itself an input of
3674     // an ISEL (the most common case) into a COPY of the correct register.
3675     bool Changed = false;
3676     Register DefReg = MI.getOperand(0).getReg();
3677     int64_t Comparand = MI.getOperand(2).getImm();
3678     int64_t SExtComparand = ((uint64_t)Comparand & ~0x7FFFuLL) != 0
3679                                 ? (Comparand | 0xFFFFFFFFFFFF0000)
3680                                 : Comparand;
3681 
3682     for (auto &CompareUseMI : MRI->use_instructions(DefReg)) {
3683       unsigned UseOpc = CompareUseMI.getOpcode();
3684       if (UseOpc != PPC::ISEL && UseOpc != PPC::ISEL8)
3685         continue;
3686       unsigned CRSubReg = CompareUseMI.getOperand(3).getSubReg();
3687       Register TrueReg = CompareUseMI.getOperand(1).getReg();
3688       Register FalseReg = CompareUseMI.getOperand(2).getReg();
3689       unsigned RegToCopy =
3690           selectReg(SExtImm, SExtComparand, Opc, TrueReg, FalseReg, CRSubReg);
3691       if (RegToCopy == PPC::NoRegister)
3692         continue;
3693       // Can't use PPC::COPY to copy PPC::ZERO[8]. Convert it to LI[8] 0.
3694       if (RegToCopy == PPC::ZERO || RegToCopy == PPC::ZERO8) {
3695         CompareUseMI.setDesc(get(UseOpc == PPC::ISEL8 ? PPC::LI8 : PPC::LI));
3696         replaceInstrOperandWithImm(CompareUseMI, 1, 0);
3697         CompareUseMI.RemoveOperand(3);
3698         CompareUseMI.RemoveOperand(2);
3699         continue;
3700       }
3701       LLVM_DEBUG(
3702           dbgs() << "Found LI -> CMPI -> ISEL, replacing with a copy.\n");
3703       LLVM_DEBUG(DefMI.dump(); MI.dump(); CompareUseMI.dump());
3704       LLVM_DEBUG(dbgs() << "Is converted to:\n");
3705       // Convert to copy and remove unneeded operands.
3706       CompareUseMI.setDesc(get(PPC::COPY));
3707       CompareUseMI.RemoveOperand(3);
3708       CompareUseMI.RemoveOperand(RegToCopy == TrueReg ? 2 : 1);
3709       CmpIselsConverted++;
3710       Changed = true;
3711       LLVM_DEBUG(CompareUseMI.dump());
3712     }
3713     if (Changed)
3714       return true;
3715     // This may end up incremented multiple times since this function is called
3716     // during a fixed-point transformation, but it is only meant to indicate the
3717     // presence of this opportunity.
3718     MissedConvertibleImmediateInstrs++;
3719     return false;
3720   }
3721 
3722   // Immediate forms - may simply be convertable to an LI.
3723   case PPC::ADDI:
3724   case PPC::ADDI8: {
3725     // Does the sum fit in a 16-bit signed field?
3726     int64_t Addend = MI.getOperand(2).getImm();
3727     if (isInt<16>(Addend + SExtImm)) {
3728       ReplaceWithLI = true;
3729       Is64BitLI = Opc == PPC::ADDI8;
3730       NewImm = Addend + SExtImm;
3731       break;
3732     }
3733     return false;
3734   }
3735   case PPC::RLDICL:
3736   case PPC::RLDICL_rec:
3737   case PPC::RLDICL_32:
3738   case PPC::RLDICL_32_64: {
3739     // Use APInt's rotate function.
3740     int64_t SH = MI.getOperand(2).getImm();
3741     int64_t MB = MI.getOperand(3).getImm();
3742     APInt InVal((Opc == PPC::RLDICL || Opc == PPC::RLDICL_rec) ? 64 : 32,
3743                 SExtImm, true);
3744     InVal = InVal.rotl(SH);
3745     uint64_t Mask = MB == 0 ? -1LLU : (1LLU << (63 - MB + 1)) - 1;
3746     InVal &= Mask;
3747     // Can't replace negative values with an LI as that will sign-extend
3748     // and not clear the left bits. If we're setting the CR bit, we will use
3749     // ANDI_rec which won't sign extend, so that's safe.
3750     if (isUInt<15>(InVal.getSExtValue()) ||
3751         (Opc == PPC::RLDICL_rec && isUInt<16>(InVal.getSExtValue()))) {
3752       ReplaceWithLI = true;
3753       Is64BitLI = Opc != PPC::RLDICL_32;
3754       NewImm = InVal.getSExtValue();
3755       SetCR = Opc == PPC::RLDICL_rec;
3756       break;
3757     }
3758     return false;
3759   }
3760   case PPC::RLWINM:
3761   case PPC::RLWINM8:
3762   case PPC::RLWINM_rec:
3763   case PPC::RLWINM8_rec: {
3764     int64_t SH = MI.getOperand(2).getImm();
3765     int64_t MB = MI.getOperand(3).getImm();
3766     int64_t ME = MI.getOperand(4).getImm();
3767     APInt InVal(32, SExtImm, true);
3768     InVal = InVal.rotl(SH);
3769     APInt Mask = APInt::getBitsSetWithWrap(32, 32 - ME - 1, 32 - MB);
3770     InVal &= Mask;
3771     // Can't replace negative values with an LI as that will sign-extend
3772     // and not clear the left bits. If we're setting the CR bit, we will use
3773     // ANDI_rec which won't sign extend, so that's safe.
3774     bool ValueFits = isUInt<15>(InVal.getSExtValue());
3775     ValueFits |= ((Opc == PPC::RLWINM_rec || Opc == PPC::RLWINM8_rec) &&
3776                   isUInt<16>(InVal.getSExtValue()));
3777     if (ValueFits) {
3778       ReplaceWithLI = true;
3779       Is64BitLI = Opc == PPC::RLWINM8 || Opc == PPC::RLWINM8_rec;
3780       NewImm = InVal.getSExtValue();
3781       SetCR = Opc == PPC::RLWINM_rec || Opc == PPC::RLWINM8_rec;
3782       break;
3783     }
3784     return false;
3785   }
3786   case PPC::ORI:
3787   case PPC::ORI8:
3788   case PPC::XORI:
3789   case PPC::XORI8: {
3790     int64_t LogicalImm = MI.getOperand(2).getImm();
3791     int64_t Result = 0;
3792     if (Opc == PPC::ORI || Opc == PPC::ORI8)
3793       Result = LogicalImm | SExtImm;
3794     else
3795       Result = LogicalImm ^ SExtImm;
3796     if (isInt<16>(Result)) {
3797       ReplaceWithLI = true;
3798       Is64BitLI = Opc == PPC::ORI8 || Opc == PPC::XORI8;
3799       NewImm = Result;
3800       break;
3801     }
3802     return false;
3803   }
3804   }
3805 
3806   if (ReplaceWithLI) {
3807     // We need to be careful with CR-setting instructions we're replacing.
3808     if (SetCR) {
3809       // We don't know anything about uses when we're out of SSA, so only
3810       // replace if the new immediate will be reproduced.
3811       bool ImmChanged = (SExtImm & NewImm) != NewImm;
3812       if (PostRA && ImmChanged)
3813         return false;
3814 
3815       if (!PostRA) {
3816         // If the defining load-immediate has no other uses, we can just replace
3817         // the immediate with the new immediate.
3818         if (MRI->hasOneUse(DefMI.getOperand(0).getReg()))
3819           DefMI.getOperand(1).setImm(NewImm);
3820 
3821         // If we're not using the GPR result of the CR-setting instruction, we
3822         // just need to and with zero/non-zero depending on the new immediate.
3823         else if (MRI->use_empty(MI.getOperand(0).getReg())) {
3824           if (NewImm) {
3825             assert(Immediate && "Transformation converted zero to non-zero?");
3826             NewImm = Immediate;
3827           }
3828         } else if (ImmChanged)
3829           return false;
3830       }
3831     }
3832 
3833     LLVM_DEBUG(dbgs() << "Replacing instruction:\n");
3834     LLVM_DEBUG(MI.dump());
3835     LLVM_DEBUG(dbgs() << "Fed by:\n");
3836     LLVM_DEBUG(DefMI.dump());
3837     LoadImmediateInfo LII;
3838     LII.Imm = NewImm;
3839     LII.Is64Bit = Is64BitLI;
3840     LII.SetCR = SetCR;
3841     // If we're setting the CR, the original load-immediate must be kept (as an
3842     // operand to ANDI_rec/ANDI8_rec).
3843     if (KilledDef && SetCR)
3844       *KilledDef = nullptr;
3845     replaceInstrWithLI(MI, LII);
3846 
3847     // Fixup killed/dead flag after transformation.
3848     // Pattern:
3849     // ForwardingOperandReg = LI imm1
3850     // y = op2 imm2, ForwardingOperandReg(killed)
3851     if (IsForwardingOperandKilled)
3852       fixupIsDeadOrKill(DefMI, MI, ForwardingOperandReg);
3853 
3854     LLVM_DEBUG(dbgs() << "With:\n");
3855     LLVM_DEBUG(MI.dump());
3856     return true;
3857   }
3858   return false;
3859 }
3860 
3861 bool PPCInstrInfo::transformToNewImmFormFedByAdd(
3862     MachineInstr &MI, MachineInstr &DefMI, unsigned OpNoForForwarding) const {
3863   MachineRegisterInfo *MRI = &MI.getParent()->getParent()->getRegInfo();
3864   bool PostRA = !MRI->isSSA();
3865   // FIXME: extend this to post-ra. Need to do some change in getForwardingDefMI
3866   // for post-ra.
3867   if (PostRA)
3868     return false;
3869 
3870   // Only handle load/store.
3871   if (!MI.mayLoadOrStore())
3872     return false;
3873 
3874   unsigned XFormOpcode = RI.getMappedIdxOpcForImmOpc(MI.getOpcode());
3875 
3876   assert((XFormOpcode != PPC::INSTRUCTION_LIST_END) &&
3877          "MI must have x-form opcode");
3878 
3879   // get Imm Form info.
3880   ImmInstrInfo III;
3881   bool IsVFReg = MI.getOperand(0).isReg()
3882                      ? isVFRegister(MI.getOperand(0).getReg())
3883                      : false;
3884 
3885   if (!instrHasImmForm(XFormOpcode, IsVFReg, III, PostRA))
3886     return false;
3887 
3888   if (!III.IsSummingOperands)
3889     return false;
3890 
3891   if (OpNoForForwarding != III.OpNoForForwarding)
3892     return false;
3893 
3894   MachineOperand ImmOperandMI = MI.getOperand(III.ImmOpNo);
3895   if (!ImmOperandMI.isImm())
3896     return false;
3897 
3898   // Check DefMI.
3899   MachineOperand *ImmMO = nullptr;
3900   MachineOperand *RegMO = nullptr;
3901   if (!isDefMIElgibleForForwarding(DefMI, III, ImmMO, RegMO))
3902     return false;
3903   assert(ImmMO && RegMO && "Imm and Reg operand must have been set");
3904 
3905   // Check Imm.
3906   // Set ImmBase from imm instruction as base and get new Imm inside
3907   // isImmElgibleForForwarding.
3908   int64_t ImmBase = ImmOperandMI.getImm();
3909   int64_t Imm = 0;
3910   if (!isImmElgibleForForwarding(*ImmMO, DefMI, III, Imm, ImmBase))
3911     return false;
3912 
3913   // Get killed info in case fixup needed after transformation.
3914   unsigned ForwardKilledOperandReg = ~0U;
3915   if (MI.getOperand(III.OpNoForForwarding).isKill())
3916     ForwardKilledOperandReg = MI.getOperand(III.OpNoForForwarding).getReg();
3917 
3918   // Do the transform
3919   LLVM_DEBUG(dbgs() << "Replacing instruction:\n");
3920   LLVM_DEBUG(MI.dump());
3921   LLVM_DEBUG(dbgs() << "Fed by:\n");
3922   LLVM_DEBUG(DefMI.dump());
3923 
3924   MI.getOperand(III.OpNoForForwarding).setReg(RegMO->getReg());
3925   MI.getOperand(III.OpNoForForwarding).setIsKill(RegMO->isKill());
3926   MI.getOperand(III.ImmOpNo).setImm(Imm);
3927 
3928   // FIXME: fix kill/dead flag if MI and DefMI are not in same basic block.
3929   if (DefMI.getParent() == MI.getParent()) {
3930     // Check if reg is killed between MI and DefMI.
3931     auto IsKilledFor = [&](unsigned Reg) {
3932       MachineBasicBlock::const_reverse_iterator It = MI;
3933       MachineBasicBlock::const_reverse_iterator E = DefMI;
3934       It++;
3935       for (; It != E; ++It) {
3936         if (It->killsRegister(Reg))
3937           return true;
3938       }
3939       return false;
3940     };
3941 
3942     // Update kill flag
3943     if (RegMO->isKill() || IsKilledFor(RegMO->getReg()))
3944       fixupIsDeadOrKill(DefMI, MI, RegMO->getReg());
3945     if (ForwardKilledOperandReg != ~0U)
3946       fixupIsDeadOrKill(DefMI, MI, ForwardKilledOperandReg);
3947   }
3948 
3949   LLVM_DEBUG(dbgs() << "With:\n");
3950   LLVM_DEBUG(MI.dump());
3951   return true;
3952 }
3953 
3954 // If an X-Form instruction is fed by an add-immediate and one of its operands
3955 // is the literal zero, attempt to forward the source of the add-immediate to
3956 // the corresponding D-Form instruction with the displacement coming from
3957 // the immediate being added.
3958 bool PPCInstrInfo::transformToImmFormFedByAdd(
3959     MachineInstr &MI, const ImmInstrInfo &III, unsigned OpNoForForwarding,
3960     MachineInstr &DefMI, bool KillDefMI) const {
3961   //         RegMO ImmMO
3962   //           |    |
3963   // x = addi reg, imm  <----- DefMI
3964   // y = op    0 ,  x   <----- MI
3965   //                |
3966   //         OpNoForForwarding
3967   // Check if the MI meet the requirement described in the III.
3968   if (!isUseMIElgibleForForwarding(MI, III, OpNoForForwarding))
3969     return false;
3970 
3971   // Check if the DefMI meet the requirement
3972   // described in the III. If yes, set the ImmMO and RegMO accordingly.
3973   MachineOperand *ImmMO = nullptr;
3974   MachineOperand *RegMO = nullptr;
3975   if (!isDefMIElgibleForForwarding(DefMI, III, ImmMO, RegMO))
3976     return false;
3977   assert(ImmMO && RegMO && "Imm and Reg operand must have been set");
3978 
3979   // As we get the Imm operand now, we need to check if the ImmMO meet
3980   // the requirement described in the III. If yes set the Imm.
3981   int64_t Imm = 0;
3982   if (!isImmElgibleForForwarding(*ImmMO, DefMI, III, Imm))
3983     return false;
3984 
3985   bool IsFwdFeederRegKilled = false;
3986   // Check if the RegMO can be forwarded to MI.
3987   if (!isRegElgibleForForwarding(*RegMO, DefMI, MI, KillDefMI,
3988                                  IsFwdFeederRegKilled))
3989     return false;
3990 
3991   // Get killed info in case fixup needed after transformation.
3992   unsigned ForwardKilledOperandReg = ~0U;
3993   MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
3994   bool PostRA = !MRI.isSSA();
3995   if (PostRA && MI.getOperand(OpNoForForwarding).isKill())
3996     ForwardKilledOperandReg = MI.getOperand(OpNoForForwarding).getReg();
3997 
3998   // We know that, the MI and DefMI both meet the pattern, and
3999   // the Imm also meet the requirement with the new Imm-form.
4000   // It is safe to do the transformation now.
4001   LLVM_DEBUG(dbgs() << "Replacing instruction:\n");
4002   LLVM_DEBUG(MI.dump());
4003   LLVM_DEBUG(dbgs() << "Fed by:\n");
4004   LLVM_DEBUG(DefMI.dump());
4005 
4006   // Update the base reg first.
4007   MI.getOperand(III.OpNoForForwarding).ChangeToRegister(RegMO->getReg(),
4008                                                         false, false,
4009                                                         RegMO->isKill());
4010 
4011   // Then, update the imm.
4012   if (ImmMO->isImm()) {
4013     // If the ImmMO is Imm, change the operand that has ZERO to that Imm
4014     // directly.
4015     replaceInstrOperandWithImm(MI, III.ZeroIsSpecialOrig, Imm);
4016   }
4017   else {
4018     // Otherwise, it is Constant Pool Index(CPI) or Global,
4019     // which is relocation in fact. We need to replace the special zero
4020     // register with ImmMO.
4021     // Before that, we need to fixup the target flags for imm.
4022     // For some reason, we miss to set the flag for the ImmMO if it is CPI.
4023     if (DefMI.getOpcode() == PPC::ADDItocL)
4024       ImmMO->setTargetFlags(PPCII::MO_TOC_LO);
4025 
4026     // MI didn't have the interface such as MI.setOperand(i) though
4027     // it has MI.getOperand(i). To repalce the ZERO MachineOperand with
4028     // ImmMO, we need to remove ZERO operand and all the operands behind it,
4029     // and, add the ImmMO, then, move back all the operands behind ZERO.
4030     SmallVector<MachineOperand, 2> MOps;
4031     for (unsigned i = MI.getNumOperands() - 1; i >= III.ZeroIsSpecialOrig; i--) {
4032       MOps.push_back(MI.getOperand(i));
4033       MI.RemoveOperand(i);
4034     }
4035 
4036     // Remove the last MO in the list, which is ZERO operand in fact.
4037     MOps.pop_back();
4038     // Add the imm operand.
4039     MI.addOperand(*ImmMO);
4040     // Now add the rest back.
4041     for (auto &MO : MOps)
4042       MI.addOperand(MO);
4043   }
4044 
4045   // Update the opcode.
4046   MI.setDesc(get(III.ImmOpcode));
4047 
4048   // Fix up killed/dead flag after transformation.
4049   // Pattern 1:
4050   // x = ADD KilledFwdFeederReg, imm
4051   // n = opn KilledFwdFeederReg(killed), regn
4052   // y = XOP 0, x
4053   // Pattern 2:
4054   // x = ADD reg(killed), imm
4055   // y = XOP 0, x
4056   if (IsFwdFeederRegKilled || RegMO->isKill())
4057     fixupIsDeadOrKill(DefMI, MI, RegMO->getReg());
4058   // Pattern 3:
4059   // ForwardKilledOperandReg = ADD reg, imm
4060   // y = XOP 0, ForwardKilledOperandReg(killed)
4061   if (ForwardKilledOperandReg != ~0U)
4062     fixupIsDeadOrKill(DefMI, MI, ForwardKilledOperandReg);
4063 
4064   LLVM_DEBUG(dbgs() << "With:\n");
4065   LLVM_DEBUG(MI.dump());
4066 
4067   return true;
4068 }
4069 
4070 bool PPCInstrInfo::transformToImmFormFedByLI(MachineInstr &MI,
4071                                              const ImmInstrInfo &III,
4072                                              unsigned ConstantOpNo,
4073                                              MachineInstr &DefMI) const {
4074   // DefMI must be LI or LI8.
4075   if ((DefMI.getOpcode() != PPC::LI && DefMI.getOpcode() != PPC::LI8) ||
4076       !DefMI.getOperand(1).isImm())
4077     return false;
4078 
4079   // Get Imm operand and Sign-extend to 64-bits.
4080   int64_t Imm = SignExtend64<16>(DefMI.getOperand(1).getImm());
4081 
4082   MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
4083   bool PostRA = !MRI.isSSA();
4084   // Exit early if we can't convert this.
4085   if ((ConstantOpNo != III.OpNoForForwarding) && !III.IsCommutative)
4086     return false;
4087   if (Imm % III.ImmMustBeMultipleOf)
4088     return false;
4089   if (III.TruncateImmTo)
4090     Imm &= ((1 << III.TruncateImmTo) - 1);
4091   if (III.SignedImm) {
4092     APInt ActualValue(64, Imm, true);
4093     if (!ActualValue.isSignedIntN(III.ImmWidth))
4094       return false;
4095   } else {
4096     uint64_t UnsignedMax = (1 << III.ImmWidth) - 1;
4097     if ((uint64_t)Imm > UnsignedMax)
4098       return false;
4099   }
4100 
4101   // If we're post-RA, the instructions don't agree on whether register zero is
4102   // special, we can transform this as long as the register operand that will
4103   // end up in the location where zero is special isn't R0.
4104   if (PostRA && III.ZeroIsSpecialOrig != III.ZeroIsSpecialNew) {
4105     unsigned PosForOrigZero = III.ZeroIsSpecialOrig ? III.ZeroIsSpecialOrig :
4106       III.ZeroIsSpecialNew + 1;
4107     Register OrigZeroReg = MI.getOperand(PosForOrigZero).getReg();
4108     Register NewZeroReg = MI.getOperand(III.ZeroIsSpecialNew).getReg();
4109     // If R0 is in the operand where zero is special for the new instruction,
4110     // it is unsafe to transform if the constant operand isn't that operand.
4111     if ((NewZeroReg == PPC::R0 || NewZeroReg == PPC::X0) &&
4112         ConstantOpNo != III.ZeroIsSpecialNew)
4113       return false;
4114     if ((OrigZeroReg == PPC::R0 || OrigZeroReg == PPC::X0) &&
4115         ConstantOpNo != PosForOrigZero)
4116       return false;
4117   }
4118 
4119   // Get killed info in case fixup needed after transformation.
4120   unsigned ForwardKilledOperandReg = ~0U;
4121   if (PostRA && MI.getOperand(ConstantOpNo).isKill())
4122     ForwardKilledOperandReg = MI.getOperand(ConstantOpNo).getReg();
4123 
4124   unsigned Opc = MI.getOpcode();
4125   bool SpecialShift32 = Opc == PPC::SLW || Opc == PPC::SLW_rec ||
4126                         Opc == PPC::SRW || Opc == PPC::SRW_rec ||
4127                         Opc == PPC::SLW8 || Opc == PPC::SLW8_rec ||
4128                         Opc == PPC::SRW8 || Opc == PPC::SRW8_rec;
4129   bool SpecialShift64 = Opc == PPC::SLD || Opc == PPC::SLD_rec ||
4130                         Opc == PPC::SRD || Opc == PPC::SRD_rec;
4131   bool SetCR = Opc == PPC::SLW_rec || Opc == PPC::SRW_rec ||
4132                Opc == PPC::SLD_rec || Opc == PPC::SRD_rec;
4133   bool RightShift = Opc == PPC::SRW || Opc == PPC::SRW_rec || Opc == PPC::SRD ||
4134                     Opc == PPC::SRD_rec;
4135 
4136   MI.setDesc(get(III.ImmOpcode));
4137   if (ConstantOpNo == III.OpNoForForwarding) {
4138     // Converting shifts to immediate form is a bit tricky since they may do
4139     // one of three things:
4140     // 1. If the shift amount is between OpSize and 2*OpSize, the result is zero
4141     // 2. If the shift amount is zero, the result is unchanged (save for maybe
4142     //    setting CR0)
4143     // 3. If the shift amount is in [1, OpSize), it's just a shift
4144     if (SpecialShift32 || SpecialShift64) {
4145       LoadImmediateInfo LII;
4146       LII.Imm = 0;
4147       LII.SetCR = SetCR;
4148       LII.Is64Bit = SpecialShift64;
4149       uint64_t ShAmt = Imm & (SpecialShift32 ? 0x1F : 0x3F);
4150       if (Imm & (SpecialShift32 ? 0x20 : 0x40))
4151         replaceInstrWithLI(MI, LII);
4152       // Shifts by zero don't change the value. If we don't need to set CR0,
4153       // just convert this to a COPY. Can't do this post-RA since we've already
4154       // cleaned up the copies.
4155       else if (!SetCR && ShAmt == 0 && !PostRA) {
4156         MI.RemoveOperand(2);
4157         MI.setDesc(get(PPC::COPY));
4158       } else {
4159         // The 32 bit and 64 bit instructions are quite different.
4160         if (SpecialShift32) {
4161           // Left shifts use (N, 0, 31-N).
4162           // Right shifts use (32-N, N, 31) if 0 < N < 32.
4163           //              use (0, 0, 31)    if N == 0.
4164           uint64_t SH = ShAmt == 0 ? 0 : RightShift ? 32 - ShAmt : ShAmt;
4165           uint64_t MB = RightShift ? ShAmt : 0;
4166           uint64_t ME = RightShift ? 31 : 31 - ShAmt;
4167           replaceInstrOperandWithImm(MI, III.OpNoForForwarding, SH);
4168           MachineInstrBuilder(*MI.getParent()->getParent(), MI).addImm(MB)
4169             .addImm(ME);
4170         } else {
4171           // Left shifts use (N, 63-N).
4172           // Right shifts use (64-N, N) if 0 < N < 64.
4173           //              use (0, 0)    if N == 0.
4174           uint64_t SH = ShAmt == 0 ? 0 : RightShift ? 64 - ShAmt : ShAmt;
4175           uint64_t ME = RightShift ? ShAmt : 63 - ShAmt;
4176           replaceInstrOperandWithImm(MI, III.OpNoForForwarding, SH);
4177           MachineInstrBuilder(*MI.getParent()->getParent(), MI).addImm(ME);
4178         }
4179       }
4180     } else
4181       replaceInstrOperandWithImm(MI, ConstantOpNo, Imm);
4182   }
4183   // Convert commutative instructions (switch the operands and convert the
4184   // desired one to an immediate.
4185   else if (III.IsCommutative) {
4186     replaceInstrOperandWithImm(MI, ConstantOpNo, Imm);
4187     swapMIOperands(MI, ConstantOpNo, III.OpNoForForwarding);
4188   } else
4189     llvm_unreachable("Should have exited early!");
4190 
4191   // For instructions for which the constant register replaces a different
4192   // operand than where the immediate goes, we need to swap them.
4193   if (III.OpNoForForwarding != III.ImmOpNo)
4194     swapMIOperands(MI, III.OpNoForForwarding, III.ImmOpNo);
4195 
4196   // If the special R0/X0 register index are different for original instruction
4197   // and new instruction, we need to fix up the register class in new
4198   // instruction.
4199   if (!PostRA && III.ZeroIsSpecialOrig != III.ZeroIsSpecialNew) {
4200     if (III.ZeroIsSpecialNew) {
4201       // If operand at III.ZeroIsSpecialNew is physical reg(eg: ZERO/ZERO8), no
4202       // need to fix up register class.
4203       Register RegToModify = MI.getOperand(III.ZeroIsSpecialNew).getReg();
4204       if (Register::isVirtualRegister(RegToModify)) {
4205         const TargetRegisterClass *NewRC =
4206           MRI.getRegClass(RegToModify)->hasSuperClassEq(&PPC::GPRCRegClass) ?
4207           &PPC::GPRC_and_GPRC_NOR0RegClass : &PPC::G8RC_and_G8RC_NOX0RegClass;
4208         MRI.setRegClass(RegToModify, NewRC);
4209       }
4210     }
4211   }
4212 
4213   // Fix up killed/dead flag after transformation.
4214   // Pattern:
4215   // ForwardKilledOperandReg = LI imm
4216   // y = XOP reg, ForwardKilledOperandReg(killed)
4217   if (ForwardKilledOperandReg != ~0U)
4218     fixupIsDeadOrKill(DefMI, MI, ForwardKilledOperandReg);
4219   return true;
4220 }
4221 
4222 const TargetRegisterClass *
4223 PPCInstrInfo::updatedRC(const TargetRegisterClass *RC) const {
4224   if (Subtarget.hasVSX() && RC == &PPC::VRRCRegClass)
4225     return &PPC::VSRCRegClass;
4226   return RC;
4227 }
4228 
4229 int PPCInstrInfo::getRecordFormOpcode(unsigned Opcode) {
4230   return PPC::getRecordFormOpcode(Opcode);
4231 }
4232 
4233 // This function returns true if the machine instruction
4234 // always outputs a value by sign-extending a 32 bit value,
4235 // i.e. 0 to 31-th bits are same as 32-th bit.
4236 static bool isSignExtendingOp(const MachineInstr &MI) {
4237   int Opcode = MI.getOpcode();
4238   if (Opcode == PPC::LI || Opcode == PPC::LI8 || Opcode == PPC::LIS ||
4239       Opcode == PPC::LIS8 || Opcode == PPC::SRAW || Opcode == PPC::SRAW_rec ||
4240       Opcode == PPC::SRAWI || Opcode == PPC::SRAWI_rec || Opcode == PPC::LWA ||
4241       Opcode == PPC::LWAX || Opcode == PPC::LWA_32 || Opcode == PPC::LWAX_32 ||
4242       Opcode == PPC::LHA || Opcode == PPC::LHAX || Opcode == PPC::LHA8 ||
4243       Opcode == PPC::LHAX8 || Opcode == PPC::LBZ || Opcode == PPC::LBZX ||
4244       Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 || Opcode == PPC::LBZU ||
4245       Opcode == PPC::LBZUX || Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 ||
4246       Opcode == PPC::LHZ || Opcode == PPC::LHZX || Opcode == PPC::LHZ8 ||
4247       Opcode == PPC::LHZX8 || Opcode == PPC::LHZU || Opcode == PPC::LHZUX ||
4248       Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8 || Opcode == PPC::EXTSB ||
4249       Opcode == PPC::EXTSB_rec || Opcode == PPC::EXTSH ||
4250       Opcode == PPC::EXTSH_rec || Opcode == PPC::EXTSB8 ||
4251       Opcode == PPC::EXTSH8 || Opcode == PPC::EXTSW ||
4252       Opcode == PPC::EXTSW_rec || Opcode == PPC::SETB || Opcode == PPC::SETB8 ||
4253       Opcode == PPC::EXTSH8_32_64 || Opcode == PPC::EXTSW_32_64 ||
4254       Opcode == PPC::EXTSB8_32_64)
4255     return true;
4256 
4257   if (Opcode == PPC::RLDICL && MI.getOperand(3).getImm() >= 33)
4258     return true;
4259 
4260   if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINM_rec ||
4261        Opcode == PPC::RLWNM || Opcode == PPC::RLWNM_rec) &&
4262       MI.getOperand(3).getImm() > 0 &&
4263       MI.getOperand(3).getImm() <= MI.getOperand(4).getImm())
4264     return true;
4265 
4266   return false;
4267 }
4268 
4269 // This function returns true if the machine instruction
4270 // always outputs zeros in higher 32 bits.
4271 static bool isZeroExtendingOp(const MachineInstr &MI) {
4272   int Opcode = MI.getOpcode();
4273   // The 16-bit immediate is sign-extended in li/lis.
4274   // If the most significant bit is zero, all higher bits are zero.
4275   if (Opcode == PPC::LI  || Opcode == PPC::LI8 ||
4276       Opcode == PPC::LIS || Opcode == PPC::LIS8) {
4277     int64_t Imm = MI.getOperand(1).getImm();
4278     if (((uint64_t)Imm & ~0x7FFFuLL) == 0)
4279       return true;
4280   }
4281 
4282   // We have some variations of rotate-and-mask instructions
4283   // that clear higher 32-bits.
4284   if ((Opcode == PPC::RLDICL || Opcode == PPC::RLDICL_rec ||
4285        Opcode == PPC::RLDCL || Opcode == PPC::RLDCL_rec ||
4286        Opcode == PPC::RLDICL_32_64) &&
4287       MI.getOperand(3).getImm() >= 32)
4288     return true;
4289 
4290   if ((Opcode == PPC::RLDIC || Opcode == PPC::RLDIC_rec) &&
4291       MI.getOperand(3).getImm() >= 32 &&
4292       MI.getOperand(3).getImm() <= 63 - MI.getOperand(2).getImm())
4293     return true;
4294 
4295   if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINM_rec ||
4296        Opcode == PPC::RLWNM || Opcode == PPC::RLWNM_rec ||
4297        Opcode == PPC::RLWINM8 || Opcode == PPC::RLWNM8) &&
4298       MI.getOperand(3).getImm() <= MI.getOperand(4).getImm())
4299     return true;
4300 
4301   // There are other instructions that clear higher 32-bits.
4302   if (Opcode == PPC::CNTLZW || Opcode == PPC::CNTLZW_rec ||
4303       Opcode == PPC::CNTTZW || Opcode == PPC::CNTTZW_rec ||
4304       Opcode == PPC::CNTLZW8 || Opcode == PPC::CNTTZW8 ||
4305       Opcode == PPC::CNTLZD || Opcode == PPC::CNTLZD_rec ||
4306       Opcode == PPC::CNTTZD || Opcode == PPC::CNTTZD_rec ||
4307       Opcode == PPC::POPCNTD || Opcode == PPC::POPCNTW || Opcode == PPC::SLW ||
4308       Opcode == PPC::SLW_rec || Opcode == PPC::SRW || Opcode == PPC::SRW_rec ||
4309       Opcode == PPC::SLW8 || Opcode == PPC::SRW8 || Opcode == PPC::SLWI ||
4310       Opcode == PPC::SLWI_rec || Opcode == PPC::SRWI ||
4311       Opcode == PPC::SRWI_rec || Opcode == PPC::LWZ || Opcode == PPC::LWZX ||
4312       Opcode == PPC::LWZU || Opcode == PPC::LWZUX || Opcode == PPC::LWBRX ||
4313       Opcode == PPC::LHBRX || Opcode == PPC::LHZ || Opcode == PPC::LHZX ||
4314       Opcode == PPC::LHZU || Opcode == PPC::LHZUX || Opcode == PPC::LBZ ||
4315       Opcode == PPC::LBZX || Opcode == PPC::LBZU || Opcode == PPC::LBZUX ||
4316       Opcode == PPC::LWZ8 || Opcode == PPC::LWZX8 || Opcode == PPC::LWZU8 ||
4317       Opcode == PPC::LWZUX8 || Opcode == PPC::LWBRX8 || Opcode == PPC::LHBRX8 ||
4318       Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 || Opcode == PPC::LHZU8 ||
4319       Opcode == PPC::LHZUX8 || Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 ||
4320       Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8 ||
4321       Opcode == PPC::ANDI_rec || Opcode == PPC::ANDIS_rec ||
4322       Opcode == PPC::ROTRWI || Opcode == PPC::ROTRWI_rec ||
4323       Opcode == PPC::EXTLWI || Opcode == PPC::EXTLWI_rec ||
4324       Opcode == PPC::MFVSRWZ)
4325     return true;
4326 
4327   return false;
4328 }
4329 
4330 // This function returns true if the input MachineInstr is a TOC save
4331 // instruction.
4332 bool PPCInstrInfo::isTOCSaveMI(const MachineInstr &MI) const {
4333   if (!MI.getOperand(1).isImm() || !MI.getOperand(2).isReg())
4334     return false;
4335   unsigned TOCSaveOffset = Subtarget.getFrameLowering()->getTOCSaveOffset();
4336   unsigned StackOffset = MI.getOperand(1).getImm();
4337   Register StackReg = MI.getOperand(2).getReg();
4338   if (StackReg == PPC::X1 && StackOffset == TOCSaveOffset)
4339     return true;
4340 
4341   return false;
4342 }
4343 
4344 // We limit the max depth to track incoming values of PHIs or binary ops
4345 // (e.g. AND) to avoid excessive cost.
4346 const unsigned MAX_DEPTH = 1;
4347 
4348 bool
4349 PPCInstrInfo::isSignOrZeroExtended(const MachineInstr &MI, bool SignExt,
4350                                    const unsigned Depth) const {
4351   const MachineFunction *MF = MI.getParent()->getParent();
4352   const MachineRegisterInfo *MRI = &MF->getRegInfo();
4353 
4354   // If we know this instruction returns sign- or zero-extended result,
4355   // return true.
4356   if (SignExt ? isSignExtendingOp(MI):
4357                 isZeroExtendingOp(MI))
4358     return true;
4359 
4360   switch (MI.getOpcode()) {
4361   case PPC::COPY: {
4362     Register SrcReg = MI.getOperand(1).getReg();
4363 
4364     // In both ELFv1 and v2 ABI, method parameters and the return value
4365     // are sign- or zero-extended.
4366     if (MF->getSubtarget<PPCSubtarget>().isSVR4ABI()) {
4367       const PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
4368       // We check the ZExt/SExt flags for a method parameter.
4369       if (MI.getParent()->getBasicBlock() ==
4370           &MF->getFunction().getEntryBlock()) {
4371         Register VReg = MI.getOperand(0).getReg();
4372         if (MF->getRegInfo().isLiveIn(VReg))
4373           return SignExt ? FuncInfo->isLiveInSExt(VReg) :
4374                            FuncInfo->isLiveInZExt(VReg);
4375       }
4376 
4377       // For a method return value, we check the ZExt/SExt flags in attribute.
4378       // We assume the following code sequence for method call.
4379       //   ADJCALLSTACKDOWN 32, implicit dead %r1, implicit %r1
4380       //   BL8_NOP @func,...
4381       //   ADJCALLSTACKUP 32, 0, implicit dead %r1, implicit %r1
4382       //   %5 = COPY %x3; G8RC:%5
4383       if (SrcReg == PPC::X3) {
4384         const MachineBasicBlock *MBB = MI.getParent();
4385         MachineBasicBlock::const_instr_iterator II =
4386           MachineBasicBlock::const_instr_iterator(&MI);
4387         if (II != MBB->instr_begin() &&
4388             (--II)->getOpcode() == PPC::ADJCALLSTACKUP) {
4389           const MachineInstr &CallMI = *(--II);
4390           if (CallMI.isCall() && CallMI.getOperand(0).isGlobal()) {
4391             const Function *CalleeFn =
4392               dyn_cast<Function>(CallMI.getOperand(0).getGlobal());
4393             if (!CalleeFn)
4394               return false;
4395             const IntegerType *IntTy =
4396               dyn_cast<IntegerType>(CalleeFn->getReturnType());
4397             const AttributeSet &Attrs =
4398               CalleeFn->getAttributes().getRetAttributes();
4399             if (IntTy && IntTy->getBitWidth() <= 32)
4400               return Attrs.hasAttribute(SignExt ? Attribute::SExt :
4401                                                   Attribute::ZExt);
4402           }
4403         }
4404       }
4405     }
4406 
4407     // If this is a copy from another register, we recursively check source.
4408     if (!Register::isVirtualRegister(SrcReg))
4409       return false;
4410     const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
4411     if (SrcMI != NULL)
4412       return isSignOrZeroExtended(*SrcMI, SignExt, Depth);
4413 
4414     return false;
4415   }
4416 
4417   case PPC::ANDI_rec:
4418   case PPC::ANDIS_rec:
4419   case PPC::ORI:
4420   case PPC::ORIS:
4421   case PPC::XORI:
4422   case PPC::XORIS:
4423   case PPC::ANDI8_rec:
4424   case PPC::ANDIS8_rec:
4425   case PPC::ORI8:
4426   case PPC::ORIS8:
4427   case PPC::XORI8:
4428   case PPC::XORIS8: {
4429     // logical operation with 16-bit immediate does not change the upper bits.
4430     // So, we track the operand register as we do for register copy.
4431     Register SrcReg = MI.getOperand(1).getReg();
4432     if (!Register::isVirtualRegister(SrcReg))
4433       return false;
4434     const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
4435     if (SrcMI != NULL)
4436       return isSignOrZeroExtended(*SrcMI, SignExt, Depth);
4437 
4438     return false;
4439   }
4440 
4441   // If all incoming values are sign-/zero-extended,
4442   // the output of OR, ISEL or PHI is also sign-/zero-extended.
4443   case PPC::OR:
4444   case PPC::OR8:
4445   case PPC::ISEL:
4446   case PPC::PHI: {
4447     if (Depth >= MAX_DEPTH)
4448       return false;
4449 
4450     // The input registers for PHI are operand 1, 3, ...
4451     // The input registers for others are operand 1 and 2.
4452     unsigned E = 3, D = 1;
4453     if (MI.getOpcode() == PPC::PHI) {
4454       E = MI.getNumOperands();
4455       D = 2;
4456     }
4457 
4458     for (unsigned I = 1; I != E; I += D) {
4459       if (MI.getOperand(I).isReg()) {
4460         Register SrcReg = MI.getOperand(I).getReg();
4461         if (!Register::isVirtualRegister(SrcReg))
4462           return false;
4463         const MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
4464         if (SrcMI == NULL || !isSignOrZeroExtended(*SrcMI, SignExt, Depth+1))
4465           return false;
4466       }
4467       else
4468         return false;
4469     }
4470     return true;
4471   }
4472 
4473   // If at least one of the incoming values of an AND is zero extended
4474   // then the output is also zero-extended. If both of the incoming values
4475   // are sign-extended then the output is also sign extended.
4476   case PPC::AND:
4477   case PPC::AND8: {
4478     if (Depth >= MAX_DEPTH)
4479        return false;
4480 
4481     assert(MI.getOperand(1).isReg() && MI.getOperand(2).isReg());
4482 
4483     Register SrcReg1 = MI.getOperand(1).getReg();
4484     Register SrcReg2 = MI.getOperand(2).getReg();
4485 
4486     if (!Register::isVirtualRegister(SrcReg1) ||
4487         !Register::isVirtualRegister(SrcReg2))
4488       return false;
4489 
4490     const MachineInstr *MISrc1 = MRI->getVRegDef(SrcReg1);
4491     const MachineInstr *MISrc2 = MRI->getVRegDef(SrcReg2);
4492     if (!MISrc1 || !MISrc2)
4493         return false;
4494 
4495     if(SignExt)
4496         return isSignOrZeroExtended(*MISrc1, SignExt, Depth+1) &&
4497                isSignOrZeroExtended(*MISrc2, SignExt, Depth+1);
4498     else
4499         return isSignOrZeroExtended(*MISrc1, SignExt, Depth+1) ||
4500                isSignOrZeroExtended(*MISrc2, SignExt, Depth+1);
4501   }
4502 
4503   default:
4504     break;
4505   }
4506   return false;
4507 }
4508 
4509 bool PPCInstrInfo::isBDNZ(unsigned Opcode) const {
4510   return (Opcode == (Subtarget.isPPC64() ? PPC::BDNZ8 : PPC::BDNZ));
4511 }
4512 
4513 namespace {
4514 class PPCPipelinerLoopInfo : public TargetInstrInfo::PipelinerLoopInfo {
4515   MachineInstr *Loop, *EndLoop, *LoopCount;
4516   MachineFunction *MF;
4517   const TargetInstrInfo *TII;
4518   int64_t TripCount;
4519 
4520 public:
4521   PPCPipelinerLoopInfo(MachineInstr *Loop, MachineInstr *EndLoop,
4522                        MachineInstr *LoopCount)
4523       : Loop(Loop), EndLoop(EndLoop), LoopCount(LoopCount),
4524         MF(Loop->getParent()->getParent()),
4525         TII(MF->getSubtarget().getInstrInfo()) {
4526     // Inspect the Loop instruction up-front, as it may be deleted when we call
4527     // createTripCountGreaterCondition.
4528     if (LoopCount->getOpcode() == PPC::LI8 || LoopCount->getOpcode() == PPC::LI)
4529       TripCount = LoopCount->getOperand(1).getImm();
4530     else
4531       TripCount = -1;
4532   }
4533 
4534   bool shouldIgnoreForPipelining(const MachineInstr *MI) const override {
4535     // Only ignore the terminator.
4536     return MI == EndLoop;
4537   }
4538 
4539   Optional<bool>
4540   createTripCountGreaterCondition(int TC, MachineBasicBlock &MBB,
4541                                   SmallVectorImpl<MachineOperand> &Cond) override {
4542     if (TripCount == -1) {
4543       // Since BDZ/BDZ8 that we will insert will also decrease the ctr by 1,
4544       // so we don't need to generate any thing here.
4545       Cond.push_back(MachineOperand::CreateImm(0));
4546       Cond.push_back(MachineOperand::CreateReg(
4547           MF->getSubtarget<PPCSubtarget>().isPPC64() ? PPC::CTR8 : PPC::CTR,
4548           true));
4549       return {};
4550     }
4551 
4552     return TripCount > TC;
4553   }
4554 
4555   void setPreheader(MachineBasicBlock *NewPreheader) override {
4556     // Do nothing. We want the LOOP setup instruction to stay in the *old*
4557     // preheader, so we can use BDZ in the prologs to adapt the loop trip count.
4558   }
4559 
4560   void adjustTripCount(int TripCountAdjust) override {
4561     // If the loop trip count is a compile-time value, then just change the
4562     // value.
4563     if (LoopCount->getOpcode() == PPC::LI8 ||
4564         LoopCount->getOpcode() == PPC::LI) {
4565       int64_t TripCount = LoopCount->getOperand(1).getImm() + TripCountAdjust;
4566       LoopCount->getOperand(1).setImm(TripCount);
4567       return;
4568     }
4569 
4570     // Since BDZ/BDZ8 that we will insert will also decrease the ctr by 1,
4571     // so we don't need to generate any thing here.
4572   }
4573 
4574   void disposed() override {
4575     Loop->eraseFromParent();
4576     // Ensure the loop setup instruction is deleted too.
4577     LoopCount->eraseFromParent();
4578   }
4579 };
4580 } // namespace
4581 
4582 std::unique_ptr<TargetInstrInfo::PipelinerLoopInfo>
4583 PPCInstrInfo::analyzeLoopForPipelining(MachineBasicBlock *LoopBB) const {
4584   // We really "analyze" only hardware loops right now.
4585   MachineBasicBlock::iterator I = LoopBB->getFirstTerminator();
4586   MachineBasicBlock *Preheader = *LoopBB->pred_begin();
4587   if (Preheader == LoopBB)
4588     Preheader = *std::next(LoopBB->pred_begin());
4589   MachineFunction *MF = Preheader->getParent();
4590 
4591   if (I != LoopBB->end() && isBDNZ(I->getOpcode())) {
4592     SmallPtrSet<MachineBasicBlock *, 8> Visited;
4593     if (MachineInstr *LoopInst = findLoopInstr(*Preheader, Visited)) {
4594       Register LoopCountReg = LoopInst->getOperand(0).getReg();
4595       MachineRegisterInfo &MRI = MF->getRegInfo();
4596       MachineInstr *LoopCount = MRI.getUniqueVRegDef(LoopCountReg);
4597       return std::make_unique<PPCPipelinerLoopInfo>(LoopInst, &*I, LoopCount);
4598     }
4599   }
4600   return nullptr;
4601 }
4602 
4603 MachineInstr *PPCInstrInfo::findLoopInstr(
4604     MachineBasicBlock &PreHeader,
4605     SmallPtrSet<MachineBasicBlock *, 8> &Visited) const {
4606 
4607   unsigned LOOPi = (Subtarget.isPPC64() ? PPC::MTCTR8loop : PPC::MTCTRloop);
4608 
4609   // The loop set-up instruction should be in preheader
4610   for (auto &I : PreHeader.instrs())
4611     if (I.getOpcode() == LOOPi)
4612       return &I;
4613   return nullptr;
4614 }
4615 
4616 // Return true if get the base operand, byte offset of an instruction and the
4617 // memory width. Width is the size of memory that is being loaded/stored.
4618 bool PPCInstrInfo::getMemOperandWithOffsetWidth(
4619     const MachineInstr &LdSt, const MachineOperand *&BaseReg, int64_t &Offset,
4620     unsigned &Width, const TargetRegisterInfo *TRI) const {
4621   if (!LdSt.mayLoadOrStore())
4622     return false;
4623 
4624   // Handle only loads/stores with base register followed by immediate offset.
4625   if (LdSt.getNumExplicitOperands() != 3)
4626     return false;
4627   if (!LdSt.getOperand(1).isImm() || !LdSt.getOperand(2).isReg())
4628     return false;
4629 
4630   if (!LdSt.hasOneMemOperand())
4631     return false;
4632 
4633   Width = (*LdSt.memoperands_begin())->getSize();
4634   Offset = LdSt.getOperand(1).getImm();
4635   BaseReg = &LdSt.getOperand(2);
4636   return true;
4637 }
4638 
4639 bool PPCInstrInfo::areMemAccessesTriviallyDisjoint(
4640     const MachineInstr &MIa, const MachineInstr &MIb) const {
4641   assert(MIa.mayLoadOrStore() && "MIa must be a load or store.");
4642   assert(MIb.mayLoadOrStore() && "MIb must be a load or store.");
4643 
4644   if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
4645       MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
4646     return false;
4647 
4648   // Retrieve the base register, offset from the base register and width. Width
4649   // is the size of memory that is being loaded/stored (e.g. 1, 2, 4).  If
4650   // base registers are identical, and the offset of a lower memory access +
4651   // the width doesn't overlap the offset of a higher memory access,
4652   // then the memory accesses are different.
4653   const TargetRegisterInfo *TRI = &getRegisterInfo();
4654   const MachineOperand *BaseOpA = nullptr, *BaseOpB = nullptr;
4655   int64_t OffsetA = 0, OffsetB = 0;
4656   unsigned int WidthA = 0, WidthB = 0;
4657   if (getMemOperandWithOffsetWidth(MIa, BaseOpA, OffsetA, WidthA, TRI) &&
4658       getMemOperandWithOffsetWidth(MIb, BaseOpB, OffsetB, WidthB, TRI)) {
4659     if (BaseOpA->isIdenticalTo(*BaseOpB)) {
4660       int LowOffset = std::min(OffsetA, OffsetB);
4661       int HighOffset = std::max(OffsetA, OffsetB);
4662       int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
4663       if (LowOffset + LowWidth <= HighOffset)
4664         return true;
4665     }
4666   }
4667   return false;
4668 }
4669