1 //===-- PPCInstrInfo.h - PowerPC Instruction Information --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the PowerPC implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_TARGET_POWERPC_PPCINSTRINFO_H
15 #define LLVM_LIB_TARGET_POWERPC_PPCINSTRINFO_H
18 #include "PPCRegisterInfo.h"
19 #include "llvm/CodeGen/TargetInstrInfo.h"
21 #define GET_INSTRINFO_HEADER
22 #include "PPCGenInstrInfo.inc"
26 /// PPCII - This namespace holds all of the PowerPC target-specific
27 /// per-instruction flags. These must match the corresponding definitions in
28 /// PPC.td and PPCInstrFormats.td.
31 // PPC970 Instruction Flags. These flags describe the characteristics of the
32 // PowerPC 970 (aka G5) dispatch groups and how they are formed out of
33 // raw machine instructions.
35 /// PPC970_First - This instruction starts a new dispatch group, so it will
36 /// always be the first one in the group.
39 /// PPC970_Single - This instruction starts a new dispatch group and
40 /// terminates it, so it will be the sole instruction in the group.
43 /// PPC970_Cracked - This instruction is cracked into two pieces, requiring
44 /// two dispatch pipes to be available to issue.
47 /// PPC970_Mask/Shift - This is a bitmask that selects the pipeline type that
48 /// an instruction is issued to.
50 PPC970_Mask = 0x07 << PPC970_Shift
53 /// These are the various PPC970 execution unit pipelines. Each instruction
55 PPC970_Pseudo = 0 << PPC970_Shift, // Pseudo instruction
56 PPC970_FXU = 1 << PPC970_Shift, // Fixed Point (aka Integer/ALU) Unit
57 PPC970_LSU = 2 << PPC970_Shift, // Load Store Unit
58 PPC970_FPU = 3 << PPC970_Shift, // Floating Point Unit
59 PPC970_CRU = 4 << PPC970_Shift, // Control Register Unit
60 PPC970_VALU = 5 << PPC970_Shift, // Vector ALU
61 PPC970_VPERM = 6 << PPC970_Shift, // Vector Permute Unit
62 PPC970_BRU = 7 << PPC970_Shift // Branch Unit
66 /// Shift count to bypass PPC970 flags
69 /// The VSX instruction that uses VSX register (vs0-vs63), instead of VMX
70 /// register (v0-v31).
71 UseVSXReg = 0x1 << NewDef_Shift,
72 /// This instruction is an X-Form memory operation.
73 XFormMemOp = 0x1 << (NewDef_Shift+1)
75 } // end namespace PPCII
77 // Instructions that have an immediate form might be convertible to that
78 // form if the correct input is a result of a load immediate. In order to
79 // know whether the transformation is special, we might need to know some
80 // of the details of the two forms.
82 // Is the immediate field in the immediate form signed or unsigned?
83 uint64_t SignedImm : 1;
84 // Does the immediate need to be a multiple of some value?
85 uint64_t ImmMustBeMultipleOf : 5;
86 // Is R0/X0 treated specially by the original r+r instruction?
87 // If so, in which operand?
88 uint64_t ZeroIsSpecialOrig : 3;
89 // Is R0/X0 treated specially by the new r+i instruction?
90 // If so, in which operand?
91 uint64_t ZeroIsSpecialNew : 3;
92 // Is the operation commutative?
93 uint64_t IsCommutative : 1;
94 // The operand number to check for load immediate.
95 uint64_t ConstantOpNo : 3;
96 // The operand number for the immediate.
98 // The opcode of the new instruction.
99 uint64_t ImmOpcode : 16;
100 // The size of the immediate.
101 uint64_t ImmWidth : 5;
102 // The immediate should be truncated to N bits.
103 uint64_t TruncateImmTo : 5;
106 // Information required to convert an instruction to just a materialized
108 struct LoadImmediateInfo {
110 unsigned Is64Bit : 1;
115 class PPCInstrInfo : public PPCGenInstrInfo {
116 PPCSubtarget &Subtarget;
117 const PPCRegisterInfo RI;
119 void StoreRegToStackSlot(MachineFunction &MF, unsigned SrcReg, bool isKill,
120 int FrameIdx, const TargetRegisterClass *RC,
121 SmallVectorImpl<MachineInstr *> &NewMIs) const;
122 void LoadRegFromStackSlot(MachineFunction &MF, const DebugLoc &DL,
123 unsigned DestReg, int FrameIdx,
124 const TargetRegisterClass *RC,
125 SmallVectorImpl<MachineInstr *> &NewMIs) const;
126 bool transformToImmForm(MachineInstr &MI, const ImmInstrInfo &III,
127 unsigned ConstantOpNo, int64_t Imm) const;
128 MachineInstr *getConstantDefMI(MachineInstr &MI, unsigned &ConstOp,
129 bool &SeenIntermediateUse) const;
130 const unsigned *getStoreOpcodesForSpillArray() const;
131 const unsigned *getLoadOpcodesForSpillArray() const;
132 virtual void anchor();
135 /// Commutes the operands in the given instruction.
136 /// The commutable operands are specified by their indices OpIdx1 and OpIdx2.
138 /// Do not call this method for a non-commutable instruction or for
139 /// non-commutable pair of operand indices OpIdx1 and OpIdx2.
140 /// Even though the instruction is commutable, the method may still
141 /// fail to commute the operands, null pointer is returned in such cases.
143 /// For example, we can commute rlwimi instructions, but only if the
144 /// rotate amt is zero. We also have to munge the immediates a bit.
145 MachineInstr *commuteInstructionImpl(MachineInstr &MI, bool NewMI,
147 unsigned OpIdx2) const override;
150 explicit PPCInstrInfo(PPCSubtarget &STI);
152 /// getRegisterInfo - TargetInstrInfo is a superset of MRegister info. As
153 /// such, whenever a client has an instance of instruction info, it should
154 /// always be able to get register info as well (through this method).
156 const PPCRegisterInfo &getRegisterInfo() const { return RI; }
158 bool isXFormMemOp(unsigned Opcode) const {
159 return get(Opcode).TSFlags & PPCII::XFormMemOp;
162 ScheduleHazardRecognizer *
163 CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
164 const ScheduleDAG *DAG) const override;
165 ScheduleHazardRecognizer *
166 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
167 const ScheduleDAG *DAG) const override;
169 unsigned getInstrLatency(const InstrItineraryData *ItinData,
170 const MachineInstr &MI,
171 unsigned *PredCost = nullptr) const override;
173 int getOperandLatency(const InstrItineraryData *ItinData,
174 const MachineInstr &DefMI, unsigned DefIdx,
175 const MachineInstr &UseMI,
176 unsigned UseIdx) const override;
177 int getOperandLatency(const InstrItineraryData *ItinData,
178 SDNode *DefNode, unsigned DefIdx,
179 SDNode *UseNode, unsigned UseIdx) const override {
180 return PPCGenInstrInfo::getOperandLatency(ItinData, DefNode, DefIdx,
184 bool hasLowDefLatency(const TargetSchedModel &SchedModel,
185 const MachineInstr &DefMI,
186 unsigned DefIdx) const override {
187 // Machine LICM should hoist all instructions in low-register-pressure
188 // situations; none are sufficiently free to justify leaving in a loop
193 bool useMachineCombiner() const override {
197 /// Return true when there is potentially a faster code sequence
198 /// for an instruction chain ending in <Root>. All potential patterns are
199 /// output in the <Pattern> array.
200 bool getMachineCombinerPatterns(
202 SmallVectorImpl<MachineCombinerPattern> &P) const override;
204 bool isAssociativeAndCommutative(const MachineInstr &Inst) const override;
206 bool isCoalescableExtInstr(const MachineInstr &MI,
207 unsigned &SrcReg, unsigned &DstReg,
208 unsigned &SubIdx) const override;
209 unsigned isLoadFromStackSlot(const MachineInstr &MI,
210 int &FrameIndex) const override;
211 bool isReallyTriviallyReMaterializable(const MachineInstr &MI,
212 AliasAnalysis *AA) const override;
213 unsigned isStoreToStackSlot(const MachineInstr &MI,
214 int &FrameIndex) const override;
216 bool findCommutedOpIndices(MachineInstr &MI, unsigned &SrcOpIdx1,
217 unsigned &SrcOpIdx2) const override;
219 void insertNoop(MachineBasicBlock &MBB,
220 MachineBasicBlock::iterator MI) const override;
224 bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
225 MachineBasicBlock *&FBB,
226 SmallVectorImpl<MachineOperand> &Cond,
227 bool AllowModify) const override;
228 unsigned removeBranch(MachineBasicBlock &MBB,
229 int *BytesRemoved = nullptr) const override;
230 unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
231 MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
233 int *BytesAdded = nullptr) const override;
236 bool canInsertSelect(const MachineBasicBlock &, ArrayRef<MachineOperand> Cond,
237 unsigned, unsigned, int &, int &, int &) const override;
238 void insertSelect(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
239 const DebugLoc &DL, unsigned DstReg,
240 ArrayRef<MachineOperand> Cond, unsigned TrueReg,
241 unsigned FalseReg) const override;
243 void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
244 const DebugLoc &DL, unsigned DestReg, unsigned SrcReg,
245 bool KillSrc) const override;
247 void storeRegToStackSlot(MachineBasicBlock &MBB,
248 MachineBasicBlock::iterator MBBI,
249 unsigned SrcReg, bool isKill, int FrameIndex,
250 const TargetRegisterClass *RC,
251 const TargetRegisterInfo *TRI) const override;
253 void loadRegFromStackSlot(MachineBasicBlock &MBB,
254 MachineBasicBlock::iterator MBBI,
255 unsigned DestReg, int FrameIndex,
256 const TargetRegisterClass *RC,
257 const TargetRegisterInfo *TRI) const override;
259 unsigned getStoreOpcodeForSpill(unsigned Reg,
260 const TargetRegisterClass *RC = nullptr) const;
262 unsigned getLoadOpcodeForSpill(unsigned Reg,
263 const TargetRegisterClass *RC = nullptr) const;
266 reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
268 bool FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, unsigned Reg,
269 MachineRegisterInfo *MRI) const override;
271 // If conversion by predication (only supported by some branch instructions).
272 // All of the profitability checks always return true; it is always
273 // profitable to use the predicated branches.
274 bool isProfitableToIfCvt(MachineBasicBlock &MBB,
275 unsigned NumCycles, unsigned ExtraPredCycles,
276 BranchProbability Probability) const override {
280 bool isProfitableToIfCvt(MachineBasicBlock &TMBB,
281 unsigned NumT, unsigned ExtraT,
282 MachineBasicBlock &FMBB,
283 unsigned NumF, unsigned ExtraF,
284 BranchProbability Probability) const override;
286 bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
287 BranchProbability Probability) const override {
291 bool isProfitableToUnpredicate(MachineBasicBlock &TMBB,
292 MachineBasicBlock &FMBB) const override {
296 // Predication support.
297 bool isPredicated(const MachineInstr &MI) const override;
299 bool isUnpredicatedTerminator(const MachineInstr &MI) const override;
301 bool PredicateInstruction(MachineInstr &MI,
302 ArrayRef<MachineOperand> Pred) const override;
304 bool SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
305 ArrayRef<MachineOperand> Pred2) const override;
307 bool DefinesPredicate(MachineInstr &MI,
308 std::vector<MachineOperand> &Pred) const override;
310 bool isPredicable(const MachineInstr &MI) const override;
312 // Comparison optimization.
314 bool analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
315 unsigned &SrcReg2, int &Mask, int &Value) const override;
317 bool optimizeCompareInstr(MachineInstr &CmpInstr, unsigned SrcReg,
318 unsigned SrcReg2, int Mask, int Value,
319 const MachineRegisterInfo *MRI) const override;
321 /// GetInstSize - Return the number of bytes of code the specified
322 /// instruction may be. This returns the maximum number of bytes.
324 unsigned getInstSizeInBytes(const MachineInstr &MI) const override;
326 void getNoop(MCInst &NopInst) const override;
328 std::pair<unsigned, unsigned>
329 decomposeMachineOperandsTargetFlags(unsigned TF) const override;
331 ArrayRef<std::pair<unsigned, const char *>>
332 getSerializableDirectMachineOperandTargetFlags() const override;
334 ArrayRef<std::pair<unsigned, const char *>>
335 getSerializableBitmaskMachineOperandTargetFlags() const override;
337 // Expand VSX Memory Pseudo instruction to either a VSX or a FP instruction.
338 bool expandVSXMemPseudo(MachineInstr &MI) const;
340 // Lower pseudo instructions after register allocation.
341 bool expandPostRAPseudo(MachineInstr &MI) const override;
343 static bool isVFRegister(unsigned Reg) {
344 return Reg >= PPC::VF0 && Reg <= PPC::VF31;
346 static bool isVRRegister(unsigned Reg) {
347 return Reg >= PPC::V0 && Reg <= PPC::V31;
349 const TargetRegisterClass *updatedRC(const TargetRegisterClass *RC) const;
350 static int getRecordFormOpcode(unsigned Opcode);
352 bool isTOCSaveMI(const MachineInstr &MI) const;
354 bool isSignOrZeroExtended(const MachineInstr &MI, bool SignExt,
355 const unsigned PhiDepth) const;
357 /// Return true if the output of the instruction is always a sign-extended,
358 /// i.e. 0 to 31-th bits are same as 32-th bit.
359 bool isSignExtended(const MachineInstr &MI, const unsigned depth = 0) const {
360 return isSignOrZeroExtended(MI, true, depth);
363 /// Return true if the output of the instruction is always zero-extended,
364 /// i.e. 0 to 31-th bits are all zeros
365 bool isZeroExtended(const MachineInstr &MI, const unsigned depth = 0) const {
366 return isSignOrZeroExtended(MI, false, depth);
369 bool convertToImmediateForm(MachineInstr &MI,
370 MachineInstr **KilledDef = nullptr) const;
371 void replaceInstrWithLI(MachineInstr &MI, const LoadImmediateInfo &LII) const;
373 bool instrHasImmForm(const MachineInstr &MI, ImmInstrInfo &III) const;