1 //===-- ARMBaseInstrInfo.h - ARM Base 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 Base ARM implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #ifndef ARMBASEINSTRUCTIONINFO_H
15 #define ARMBASEINSTRUCTIONINFO_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/Target/TargetInstrInfo.h"
23 #define GET_INSTRINFO_HEADER
24 #include "ARMGenInstrInfo.inc"
28 class ARMBaseRegisterInfo;
30 class ARMBaseInstrInfo : public ARMGenInstrInfo {
31 const ARMSubtarget &Subtarget;
34 // Can be only subclassed.
35 explicit ARMBaseInstrInfo(const ARMSubtarget &STI);
38 // Return whether the target has an explicit NOP encoding.
41 // Return the non-pre/post incrementing version of 'Opc'. Return 0
42 // if there is not such an opcode.
43 virtual unsigned getUnindexedOpcode(unsigned Opc) const =0;
45 MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
46 MachineBasicBlock::iterator &MBBI,
47 LiveVariables *LV) const override;
49 virtual const ARMBaseRegisterInfo &getRegisterInfo() const = 0;
50 const ARMSubtarget &getSubtarget() const { return Subtarget; }
52 ScheduleHazardRecognizer *
53 CreateTargetHazardRecognizer(const TargetMachine *TM,
54 const ScheduleDAG *DAG) const override;
56 ScheduleHazardRecognizer *
57 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
58 const ScheduleDAG *DAG) const override;
61 bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
62 MachineBasicBlock *&FBB,
63 SmallVectorImpl<MachineOperand> &Cond,
64 bool AllowModify = false) const override;
65 unsigned RemoveBranch(MachineBasicBlock &MBB) const override;
66 unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
67 MachineBasicBlock *FBB,
68 const SmallVectorImpl<MachineOperand> &Cond,
69 DebugLoc DL) const override;
72 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
74 // Predication support.
75 bool isPredicated(const MachineInstr *MI) const override;
77 ARMCC::CondCodes getPredicate(const MachineInstr *MI) const {
78 int PIdx = MI->findFirstPredOperandIdx();
79 return PIdx != -1 ? (ARMCC::CondCodes)MI->getOperand(PIdx).getImm()
83 bool PredicateInstruction(MachineInstr *MI,
84 const SmallVectorImpl<MachineOperand> &Pred) const override;
86 bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
87 const SmallVectorImpl<MachineOperand> &Pred2) const override;
89 bool DefinesPredicate(MachineInstr *MI,
90 std::vector<MachineOperand> &Pred) const override;
92 bool isPredicable(MachineInstr *MI) const override;
94 /// GetInstSize - Returns the size of the specified MachineInstr.
96 virtual unsigned GetInstSizeInBytes(const MachineInstr* MI) const;
98 unsigned isLoadFromStackSlot(const MachineInstr *MI,
99 int &FrameIndex) const override;
100 unsigned isStoreToStackSlot(const MachineInstr *MI,
101 int &FrameIndex) const override;
102 unsigned isLoadFromStackSlotPostFE(const MachineInstr *MI,
103 int &FrameIndex) const override;
104 unsigned isStoreToStackSlotPostFE(const MachineInstr *MI,
105 int &FrameIndex) const override;
107 void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
108 DebugLoc DL, unsigned DestReg, unsigned SrcReg,
109 bool KillSrc) const override;
111 void storeRegToStackSlot(MachineBasicBlock &MBB,
112 MachineBasicBlock::iterator MBBI,
113 unsigned SrcReg, bool isKill, int FrameIndex,
114 const TargetRegisterClass *RC,
115 const TargetRegisterInfo *TRI) const override;
117 void loadRegFromStackSlot(MachineBasicBlock &MBB,
118 MachineBasicBlock::iterator MBBI,
119 unsigned DestReg, int FrameIndex,
120 const TargetRegisterClass *RC,
121 const TargetRegisterInfo *TRI) const override;
123 bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const override;
125 void reMaterialize(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
126 unsigned DestReg, unsigned SubIdx,
127 const MachineInstr *Orig,
128 const TargetRegisterInfo &TRI) const override;
130 MachineInstr *duplicate(MachineInstr *Orig,
131 MachineFunction &MF) const override;
133 MachineInstr *commuteInstruction(MachineInstr*,
134 bool=false) const override;
136 const MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
137 unsigned SubIdx, unsigned State,
138 const TargetRegisterInfo *TRI) const;
140 bool produceSameValue(const MachineInstr *MI0, const MachineInstr *MI1,
141 const MachineRegisterInfo *MRI) const override;
143 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
144 /// determine if two loads are loading from the same base address. It should
145 /// only return true if the base pointers are the same and the only
146 /// differences between the two addresses is the offset. It also returns the
147 /// offsets by reference.
148 bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2, int64_t &Offset1,
149 int64_t &Offset2) const override;
151 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
152 /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads
153 /// should be scheduled togther. On some targets if two loads are loading from
154 /// addresses in the same cache line, it's better if they are scheduled
155 /// together. This function takes two integers that represent the load offsets
156 /// from the common base address. It returns true if it decides it's desirable
157 /// to schedule the two loads together. "NumLoads" is the number of loads that
158 /// have already been scheduled after Load1.
159 bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
160 int64_t Offset1, int64_t Offset2,
161 unsigned NumLoads) const override;
163 bool isSchedulingBoundary(const MachineInstr *MI,
164 const MachineBasicBlock *MBB,
165 const MachineFunction &MF) const override;
167 bool isProfitableToIfCvt(MachineBasicBlock &MBB,
168 unsigned NumCycles, unsigned ExtraPredCycles,
169 const BranchProbability &Probability) const override;
171 bool isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumT,
172 unsigned ExtraT, MachineBasicBlock &FMBB,
173 unsigned NumF, unsigned ExtraF,
174 const BranchProbability &Probability) const override;
176 bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
177 const BranchProbability &Probability) const override {
178 return NumCycles == 1;
181 bool isProfitableToUnpredicate(MachineBasicBlock &TMBB,
182 MachineBasicBlock &FMBB) const override;
184 /// analyzeCompare - For a comparison instruction, return the source registers
185 /// in SrcReg and SrcReg2 if having two register operands, and the value it
186 /// compares against in CmpValue. Return true if the comparison instruction
188 bool analyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
189 unsigned &SrcReg2, int &CmpMask,
190 int &CmpValue) const override;
192 /// optimizeCompareInstr - Convert the instruction to set the zero flag so
193 /// that we can remove a "comparison with zero"; Remove a redundant CMP
194 /// instruction if the flags can be updated in the same way by an earlier
195 /// instruction such as SUB.
196 bool optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg,
197 unsigned SrcReg2, int CmpMask, int CmpValue,
198 const MachineRegisterInfo *MRI) const override;
200 bool analyzeSelect(const MachineInstr *MI,
201 SmallVectorImpl<MachineOperand> &Cond,
202 unsigned &TrueOp, unsigned &FalseOp,
203 bool &Optimizable) const override;
205 MachineInstr *optimizeSelect(MachineInstr *MI, bool) const override;
207 /// FoldImmediate - 'Reg' is known to be defined by a move immediate
208 /// instruction, try to fold the immediate into the use instruction.
209 bool FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
210 unsigned Reg, MachineRegisterInfo *MRI) const override;
212 unsigned getNumMicroOps(const InstrItineraryData *ItinData,
213 const MachineInstr *MI) const override;
215 int getOperandLatency(const InstrItineraryData *ItinData,
216 const MachineInstr *DefMI, unsigned DefIdx,
217 const MachineInstr *UseMI,
218 unsigned UseIdx) const override;
219 int getOperandLatency(const InstrItineraryData *ItinData,
220 SDNode *DefNode, unsigned DefIdx,
221 SDNode *UseNode, unsigned UseIdx) const override;
223 /// VFP/NEON execution domains.
224 std::pair<uint16_t, uint16_t>
225 getExecutionDomain(const MachineInstr *MI) const override;
226 void setExecutionDomain(MachineInstr *MI, unsigned Domain) const override;
228 unsigned getPartialRegUpdateClearance(const MachineInstr*, unsigned,
229 const TargetRegisterInfo*) const override;
230 void breakPartialRegDependency(MachineBasicBlock::iterator, unsigned,
231 const TargetRegisterInfo *TRI) const override;
232 /// Get the number of addresses by LDM or VLDM or zero for unknown.
233 unsigned getNumLDMAddresses(const MachineInstr *MI) const;
236 unsigned getInstBundleLength(const MachineInstr *MI) const;
238 int getVLDMDefCycle(const InstrItineraryData *ItinData,
239 const MCInstrDesc &DefMCID,
241 unsigned DefIdx, unsigned DefAlign) const;
242 int getLDMDefCycle(const InstrItineraryData *ItinData,
243 const MCInstrDesc &DefMCID,
245 unsigned DefIdx, unsigned DefAlign) const;
246 int getVSTMUseCycle(const InstrItineraryData *ItinData,
247 const MCInstrDesc &UseMCID,
249 unsigned UseIdx, unsigned UseAlign) const;
250 int getSTMUseCycle(const InstrItineraryData *ItinData,
251 const MCInstrDesc &UseMCID,
253 unsigned UseIdx, unsigned UseAlign) const;
254 int getOperandLatency(const InstrItineraryData *ItinData,
255 const MCInstrDesc &DefMCID,
256 unsigned DefIdx, unsigned DefAlign,
257 const MCInstrDesc &UseMCID,
258 unsigned UseIdx, unsigned UseAlign) const;
260 unsigned getPredicationCost(const MachineInstr *MI) const override;
262 unsigned getInstrLatency(const InstrItineraryData *ItinData,
263 const MachineInstr *MI,
264 unsigned *PredCost = 0) const override;
266 int getInstrLatency(const InstrItineraryData *ItinData,
267 SDNode *Node) const override;
269 bool hasHighOperandLatency(const InstrItineraryData *ItinData,
270 const MachineRegisterInfo *MRI,
271 const MachineInstr *DefMI, unsigned DefIdx,
272 const MachineInstr *UseMI,
273 unsigned UseIdx) const override;
274 bool hasLowDefLatency(const InstrItineraryData *ItinData,
275 const MachineInstr *DefMI,
276 unsigned DefIdx) const override;
278 /// verifyInstruction - Perform target specific instruction verification.
279 bool verifyInstruction(const MachineInstr *MI,
280 StringRef &ErrInfo) const override;
283 /// Modeling special VFP / NEON fp MLA / MLS hazards.
285 /// MLxEntryMap - Map fp MLA / MLS to the corresponding entry in the internal
287 DenseMap<unsigned, unsigned> MLxEntryMap;
289 /// MLxHazardOpcodes - Set of add / sub and multiply opcodes that would cause
290 /// stalls when scheduled together with fp MLA / MLS opcodes.
291 SmallSet<unsigned, 16> MLxHazardOpcodes;
294 /// isFpMLxInstruction - Return true if the specified opcode is a fp MLA / MLS
296 bool isFpMLxInstruction(unsigned Opcode) const {
297 return MLxEntryMap.count(Opcode);
300 /// isFpMLxInstruction - This version also returns the multiply opcode and the
301 /// addition / subtraction opcode to expand to. Return true for 'HasLane' for
302 /// the MLX instructions with an extra lane operand.
303 bool isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
304 unsigned &AddSubOpc, bool &NegAcc,
305 bool &HasLane) const;
307 /// canCauseFpMLxStall - Return true if an instruction of the specified opcode
308 /// will cause stalls when scheduled after (within 4-cycle window) a fp
309 /// MLA / MLS instruction.
310 bool canCauseFpMLxStall(unsigned Opcode) const {
311 return MLxHazardOpcodes.count(Opcode);
314 /// Returns true if the instruction has a shift by immediate that can be
315 /// executed in one cycle less.
316 bool isSwiftFastImmShift(const MachineInstr *MI) const;
320 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
321 return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
325 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
326 return MIB.addReg(0);
330 const MachineInstrBuilder &AddDefaultT1CC(const MachineInstrBuilder &MIB,
331 bool isDead = false) {
332 return MIB.addReg(ARM::CPSR, getDefRegState(true) | getDeadRegState(isDead));
336 const MachineInstrBuilder &AddNoT1CC(const MachineInstrBuilder &MIB) {
337 return MIB.addReg(0);
341 bool isUncondBranchOpcode(int Opc) {
342 return Opc == ARM::B || Opc == ARM::tB || Opc == ARM::t2B;
346 bool isCondBranchOpcode(int Opc) {
347 return Opc == ARM::Bcc || Opc == ARM::tBcc || Opc == ARM::t2Bcc;
351 bool isJumpTableBranchOpcode(int Opc) {
352 return Opc == ARM::BR_JTr || Opc == ARM::BR_JTm || Opc == ARM::BR_JTadd ||
353 Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT;
357 bool isIndirectBranchOpcode(int Opc) {
358 return Opc == ARM::BX || Opc == ARM::MOVPCRX || Opc == ARM::tBRIND;
361 static inline bool isPopOpcode(int Opc) {
362 return Opc == ARM::tPOP_RET || Opc == ARM::LDMIA_RET ||
363 Opc == ARM::t2LDMIA_RET || Opc == ARM::tPOP || Opc == ARM::LDMIA_UPD ||
364 Opc == ARM::t2LDMIA_UPD || Opc == ARM::VLDMDIA_UPD;
367 static inline bool isPushOpcode(int Opc) {
368 return Opc == ARM::tPUSH || Opc == ARM::t2STMDB_UPD ||
369 Opc == ARM::STMDB_UPD || Opc == ARM::VSTMDDB_UPD;
372 /// getInstrPredicate - If instruction is predicated, returns its predicate
373 /// condition, otherwise returns AL. It also returns the condition code
374 /// register by reference.
375 ARMCC::CondCodes getInstrPredicate(const MachineInstr *MI, unsigned &PredReg);
377 int getMatchingCondBranchOpcode(int Opc);
379 /// Determine if MI can be folded into an ARM MOVCC instruction, and return the
380 /// opcode of the SSA instruction representing the conditional MI.
381 unsigned canFoldARMInstrIntoMOVCC(unsigned Reg,
383 const MachineRegisterInfo &MRI);
385 /// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether
386 /// the instruction is encoded with an 'S' bit is determined by the optional
387 /// CPSR def operand.
388 unsigned convertAddSubFlagsOpcode(unsigned OldOpc);
390 /// emitARMRegPlusImmediate / emitT2RegPlusImmediate - Emits a series of
391 /// instructions to materializea destreg = basereg + immediate in ARM / Thumb2
393 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
394 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
395 unsigned DestReg, unsigned BaseReg, int NumBytes,
396 ARMCC::CondCodes Pred, unsigned PredReg,
397 const ARMBaseInstrInfo &TII, unsigned MIFlags = 0);
399 void emitT2RegPlusImmediate(MachineBasicBlock &MBB,
400 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
401 unsigned DestReg, unsigned BaseReg, int NumBytes,
402 ARMCC::CondCodes Pred, unsigned PredReg,
403 const ARMBaseInstrInfo &TII, unsigned MIFlags = 0);
404 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
405 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
406 unsigned DestReg, unsigned BaseReg,
407 int NumBytes, const TargetInstrInfo &TII,
408 const ARMBaseRegisterInfo& MRI,
409 unsigned MIFlags = 0);
411 /// Tries to add registers to the reglist of a given base-updating
412 /// push/pop instruction to adjust the stack by an additional
413 /// NumBytes. This can save a few bytes per function in code-size, but
414 /// obviously generates more memory traffic. As such, it only takes
415 /// effect in functions being optimised for size.
416 bool tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
417 MachineFunction &MF, MachineInstr *MI,
420 /// rewriteARMFrameIndex / rewriteT2FrameIndex -
421 /// Rewrite MI to access 'Offset' bytes from the FP. Return false if the
422 /// offset could not be handled directly in MI, and return the left-over
423 /// portion by reference.
424 bool rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
425 unsigned FrameReg, int &Offset,
426 const ARMBaseInstrInfo &TII);
428 bool rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
429 unsigned FrameReg, int &Offset,
430 const ARMBaseInstrInfo &TII);
432 } // End llvm namespace