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 LLVM_LIB_TARGET_ARM_ARMBASEINSTRINFO_H
15 #define LLVM_LIB_TARGET_ARM_ARMBASEINSTRINFO_H
17 #include "MCTargetDesc/ARMBaseInfo.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/Support/CodeGen.h"
22 #include "llvm/Target/TargetInstrInfo.h"
24 #define GET_INSTRINFO_HEADER
25 #include "ARMGenInstrInfo.inc"
29 class ARMBaseRegisterInfo;
31 class ARMBaseInstrInfo : public ARMGenInstrInfo {
32 const ARMSubtarget &Subtarget;
35 // Can be only subclassed.
36 explicit ARMBaseInstrInfo(const ARMSubtarget &STI);
38 void expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
39 unsigned LoadImmOpc, unsigned LoadOpc) const;
41 /// Build the equivalent inputs of a REG_SEQUENCE for the given \p MI
43 /// \p [out] InputRegs of the equivalent REG_SEQUENCE. Each element of
44 /// the list is modeled as <Reg:SubReg, SubIdx>.
45 /// E.g., REG_SEQUENCE vreg1:sub1, sub0, vreg2, sub1 would produce
47 /// - vreg1:sub1, sub0
50 /// \returns true if it is possible to build such an input sequence
51 /// with the pair \p MI, \p DefIdx. False otherwise.
53 /// \pre MI.isRegSequenceLike().
54 bool getRegSequenceLikeInputs(
55 const MachineInstr &MI, unsigned DefIdx,
56 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const override;
58 /// Build the equivalent inputs of a EXTRACT_SUBREG for the given \p MI
60 /// \p [out] InputReg of the equivalent EXTRACT_SUBREG.
61 /// E.g., EXTRACT_SUBREG vreg1:sub1, sub0, sub1 would produce:
62 /// - vreg1:sub1, sub0
64 /// \returns true if it is possible to build such an input sequence
65 /// with the pair \p MI, \p DefIdx. False otherwise.
67 /// \pre MI.isExtractSubregLike().
68 bool getExtractSubregLikeInputs(const MachineInstr &MI, unsigned DefIdx,
69 RegSubRegPairAndIdx &InputReg) const override;
71 /// Build the equivalent inputs of a INSERT_SUBREG for the given \p MI
73 /// \p [out] BaseReg and \p [out] InsertedReg contain
74 /// the equivalent inputs of INSERT_SUBREG.
75 /// E.g., INSERT_SUBREG vreg0:sub0, vreg1:sub1, sub3 would produce:
76 /// - BaseReg: vreg0:sub0
77 /// - InsertedReg: vreg1:sub1, sub3
79 /// \returns true if it is possible to build such an input sequence
80 /// with the pair \p MI, \p DefIdx. False otherwise.
82 /// \pre MI.isInsertSubregLike().
84 getInsertSubregLikeInputs(const MachineInstr &MI, unsigned DefIdx,
85 RegSubRegPair &BaseReg,
86 RegSubRegPairAndIdx &InsertedReg) const override;
88 /// Commutes the operands in the given instruction.
89 /// The commutable operands are specified by their indices OpIdx1 and OpIdx2.
91 /// Do not call this method for a non-commutable instruction or for
92 /// non-commutable pair of operand indices OpIdx1 and OpIdx2.
93 /// Even though the instruction is commutable, the method may still
94 /// fail to commute the operands, null pointer is returned in such cases.
95 MachineInstr *commuteInstructionImpl(MachineInstr &MI, bool NewMI,
97 unsigned OpIdx2) const override;
100 // Return whether the target has an explicit NOP encoding.
103 virtual void getNoopForElfTarget(MCInst &NopInst) const {
104 getNoopForMachoTarget(NopInst);
107 // Return the non-pre/post incrementing version of 'Opc'. Return 0
108 // if there is not such an opcode.
109 virtual unsigned getUnindexedOpcode(unsigned Opc) const =0;
111 MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
113 LiveVariables *LV) const override;
115 virtual const ARMBaseRegisterInfo &getRegisterInfo() const = 0;
116 const ARMSubtarget &getSubtarget() const { return Subtarget; }
118 ScheduleHazardRecognizer *
119 CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
120 const ScheduleDAG *DAG) const override;
122 ScheduleHazardRecognizer *
123 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
124 const ScheduleDAG *DAG) const override;
127 bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
128 MachineBasicBlock *&FBB,
129 SmallVectorImpl<MachineOperand> &Cond,
130 bool AllowModify = false) const override;
131 unsigned removeBranch(MachineBasicBlock &MBB,
132 int *BytesRemoved = nullptr) const override;
133 unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
134 MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
136 int *BytesAdded = nullptr) const override;
139 reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
141 // Predication support.
142 bool isPredicated(const MachineInstr &MI) const override;
144 ARMCC::CondCodes getPredicate(const MachineInstr &MI) const {
145 int PIdx = MI.findFirstPredOperandIdx();
146 return PIdx != -1 ? (ARMCC::CondCodes)MI.getOperand(PIdx).getImm()
150 bool PredicateInstruction(MachineInstr &MI,
151 ArrayRef<MachineOperand> Pred) const override;
153 bool SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
154 ArrayRef<MachineOperand> Pred2) const override;
156 bool DefinesPredicate(MachineInstr &MI,
157 std::vector<MachineOperand> &Pred) const override;
159 bool isPredicable(MachineInstr &MI) const override;
161 /// GetInstSize - Returns the size of the specified MachineInstr.
163 unsigned getInstSizeInBytes(const MachineInstr &MI) const override;
165 unsigned isLoadFromStackSlot(const MachineInstr &MI,
166 int &FrameIndex) const override;
167 unsigned isStoreToStackSlot(const MachineInstr &MI,
168 int &FrameIndex) const override;
169 unsigned isLoadFromStackSlotPostFE(const MachineInstr &MI,
170 int &FrameIndex) const override;
171 unsigned isStoreToStackSlotPostFE(const MachineInstr &MI,
172 int &FrameIndex) const override;
174 void copyToCPSR(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
175 unsigned SrcReg, bool KillSrc,
176 const ARMSubtarget &Subtarget) const;
177 void copyFromCPSR(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
178 unsigned DestReg, bool KillSrc,
179 const ARMSubtarget &Subtarget) const;
181 void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
182 const DebugLoc &DL, unsigned DestReg, unsigned SrcReg,
183 bool KillSrc) const override;
185 void storeRegToStackSlot(MachineBasicBlock &MBB,
186 MachineBasicBlock::iterator MBBI,
187 unsigned SrcReg, bool isKill, int FrameIndex,
188 const TargetRegisterClass *RC,
189 const TargetRegisterInfo *TRI) const override;
191 void loadRegFromStackSlot(MachineBasicBlock &MBB,
192 MachineBasicBlock::iterator MBBI,
193 unsigned DestReg, int FrameIndex,
194 const TargetRegisterClass *RC,
195 const TargetRegisterInfo *TRI) const override;
197 bool expandPostRAPseudo(MachineInstr &MI) const override;
199 void reMaterialize(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
200 unsigned DestReg, unsigned SubIdx,
201 const MachineInstr &Orig,
202 const TargetRegisterInfo &TRI) const override;
204 MachineInstr *duplicate(MachineInstr &Orig,
205 MachineFunction &MF) const override;
207 const MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
208 unsigned SubIdx, unsigned State,
209 const TargetRegisterInfo *TRI) const;
211 bool produceSameValue(const MachineInstr &MI0, const MachineInstr &MI1,
212 const MachineRegisterInfo *MRI) const override;
214 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
215 /// determine if two loads are loading from the same base address. It should
216 /// only return true if the base pointers are the same and the only
217 /// differences between the two addresses is the offset. It also returns the
218 /// offsets by reference.
219 bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2, int64_t &Offset1,
220 int64_t &Offset2) const override;
222 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
223 /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads
224 /// should be scheduled togther. On some targets if two loads are loading from
225 /// addresses in the same cache line, it's better if they are scheduled
226 /// together. This function takes two integers that represent the load offsets
227 /// from the common base address. It returns true if it decides it's desirable
228 /// to schedule the two loads together. "NumLoads" is the number of loads that
229 /// have already been scheduled after Load1.
230 bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
231 int64_t Offset1, int64_t Offset2,
232 unsigned NumLoads) const override;
234 bool isSchedulingBoundary(const MachineInstr &MI,
235 const MachineBasicBlock *MBB,
236 const MachineFunction &MF) const override;
238 bool isProfitableToIfCvt(MachineBasicBlock &MBB,
239 unsigned NumCycles, unsigned ExtraPredCycles,
240 BranchProbability Probability) const override;
242 bool isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumT,
243 unsigned ExtraT, MachineBasicBlock &FMBB,
244 unsigned NumF, unsigned ExtraF,
245 BranchProbability Probability) const override;
247 bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
248 BranchProbability Probability) const override {
249 return NumCycles == 1;
252 bool isProfitableToUnpredicate(MachineBasicBlock &TMBB,
253 MachineBasicBlock &FMBB) const override;
255 /// analyzeCompare - For a comparison instruction, return the source registers
256 /// in SrcReg and SrcReg2 if having two register operands, and the value it
257 /// compares against in CmpValue. Return true if the comparison instruction
259 bool analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
260 unsigned &SrcReg2, int &CmpMask,
261 int &CmpValue) const override;
263 /// optimizeCompareInstr - Convert the instruction to set the zero flag so
264 /// that we can remove a "comparison with zero"; Remove a redundant CMP
265 /// instruction if the flags can be updated in the same way by an earlier
266 /// instruction such as SUB.
267 bool optimizeCompareInstr(MachineInstr &CmpInstr, unsigned SrcReg,
268 unsigned SrcReg2, int CmpMask, int CmpValue,
269 const MachineRegisterInfo *MRI) const override;
271 bool analyzeSelect(const MachineInstr &MI,
272 SmallVectorImpl<MachineOperand> &Cond, unsigned &TrueOp,
273 unsigned &FalseOp, bool &Optimizable) const override;
275 MachineInstr *optimizeSelect(MachineInstr &MI,
276 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
277 bool) const override;
279 /// FoldImmediate - 'Reg' is known to be defined by a move immediate
280 /// instruction, try to fold the immediate into the use instruction.
281 bool FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, unsigned Reg,
282 MachineRegisterInfo *MRI) const override;
284 unsigned getNumMicroOps(const InstrItineraryData *ItinData,
285 const MachineInstr &MI) const override;
287 int getOperandLatency(const InstrItineraryData *ItinData,
288 const MachineInstr &DefMI, unsigned DefIdx,
289 const MachineInstr &UseMI,
290 unsigned UseIdx) const override;
291 int getOperandLatency(const InstrItineraryData *ItinData,
292 SDNode *DefNode, unsigned DefIdx,
293 SDNode *UseNode, unsigned UseIdx) const override;
295 /// VFP/NEON execution domains.
296 std::pair<uint16_t, uint16_t>
297 getExecutionDomain(const MachineInstr &MI) const override;
298 void setExecutionDomain(MachineInstr &MI, unsigned Domain) const override;
301 getPartialRegUpdateClearance(const MachineInstr &, unsigned,
302 const TargetRegisterInfo *) const override;
303 void breakPartialRegDependency(MachineInstr &, unsigned,
304 const TargetRegisterInfo *TRI) const override;
306 /// Get the number of addresses by LDM or VLDM or zero for unknown.
307 unsigned getNumLDMAddresses(const MachineInstr &MI) const;
310 unsigned getInstBundleLength(const MachineInstr &MI) const;
312 int getVLDMDefCycle(const InstrItineraryData *ItinData,
313 const MCInstrDesc &DefMCID,
315 unsigned DefIdx, unsigned DefAlign) const;
316 int getLDMDefCycle(const InstrItineraryData *ItinData,
317 const MCInstrDesc &DefMCID,
319 unsigned DefIdx, unsigned DefAlign) const;
320 int getVSTMUseCycle(const InstrItineraryData *ItinData,
321 const MCInstrDesc &UseMCID,
323 unsigned UseIdx, unsigned UseAlign) const;
324 int getSTMUseCycle(const InstrItineraryData *ItinData,
325 const MCInstrDesc &UseMCID,
327 unsigned UseIdx, unsigned UseAlign) const;
328 int getOperandLatency(const InstrItineraryData *ItinData,
329 const MCInstrDesc &DefMCID,
330 unsigned DefIdx, unsigned DefAlign,
331 const MCInstrDesc &UseMCID,
332 unsigned UseIdx, unsigned UseAlign) const;
334 int getOperandLatencyImpl(const InstrItineraryData *ItinData,
335 const MachineInstr &DefMI, unsigned DefIdx,
336 const MCInstrDesc &DefMCID, unsigned DefAdj,
337 const MachineOperand &DefMO, unsigned Reg,
338 const MachineInstr &UseMI, unsigned UseIdx,
339 const MCInstrDesc &UseMCID, unsigned UseAdj) const;
341 unsigned getPredicationCost(const MachineInstr &MI) const override;
343 unsigned getInstrLatency(const InstrItineraryData *ItinData,
344 const MachineInstr &MI,
345 unsigned *PredCost = nullptr) const override;
347 int getInstrLatency(const InstrItineraryData *ItinData,
348 SDNode *Node) const override;
350 bool hasHighOperandLatency(const TargetSchedModel &SchedModel,
351 const MachineRegisterInfo *MRI,
352 const MachineInstr &DefMI, unsigned DefIdx,
353 const MachineInstr &UseMI,
354 unsigned UseIdx) const override;
355 bool hasLowDefLatency(const TargetSchedModel &SchedModel,
356 const MachineInstr &DefMI,
357 unsigned DefIdx) const override;
359 /// verifyInstruction - Perform target specific instruction verification.
360 bool verifyInstruction(const MachineInstr &MI,
361 StringRef &ErrInfo) const override;
363 virtual void expandLoadStackGuard(MachineBasicBlock::iterator MI) const = 0;
365 void expandMEMCPY(MachineBasicBlock::iterator) const;
368 /// Modeling special VFP / NEON fp MLA / MLS hazards.
370 /// MLxEntryMap - Map fp MLA / MLS to the corresponding entry in the internal
372 DenseMap<unsigned, unsigned> MLxEntryMap;
374 /// MLxHazardOpcodes - Set of add / sub and multiply opcodes that would cause
375 /// stalls when scheduled together with fp MLA / MLS opcodes.
376 SmallSet<unsigned, 16> MLxHazardOpcodes;
379 /// isFpMLxInstruction - Return true if the specified opcode is a fp MLA / MLS
381 bool isFpMLxInstruction(unsigned Opcode) const {
382 return MLxEntryMap.count(Opcode);
385 /// isFpMLxInstruction - This version also returns the multiply opcode and the
386 /// addition / subtraction opcode to expand to. Return true for 'HasLane' for
387 /// the MLX instructions with an extra lane operand.
388 bool isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
389 unsigned &AddSubOpc, bool &NegAcc,
390 bool &HasLane) const;
392 /// canCauseFpMLxStall - Return true if an instruction of the specified opcode
393 /// will cause stalls when scheduled after (within 4-cycle window) a fp
394 /// MLA / MLS instruction.
395 bool canCauseFpMLxStall(unsigned Opcode) const {
396 return MLxHazardOpcodes.count(Opcode);
399 /// Returns true if the instruction has a shift by immediate that can be
400 /// executed in one cycle less.
401 bool isSwiftFastImmShift(const MachineInstr *MI) const;
405 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
406 return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
410 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
411 return MIB.addReg(0);
415 const MachineInstrBuilder &AddDefaultT1CC(const MachineInstrBuilder &MIB,
416 bool isDead = false) {
417 return MIB.addReg(ARM::CPSR, getDefRegState(true) | getDeadRegState(isDead));
421 const MachineInstrBuilder &AddNoT1CC(const MachineInstrBuilder &MIB) {
422 return MIB.addReg(0);
426 bool isUncondBranchOpcode(int Opc) {
427 return Opc == ARM::B || Opc == ARM::tB || Opc == ARM::t2B;
431 bool isCondBranchOpcode(int Opc) {
432 return Opc == ARM::Bcc || Opc == ARM::tBcc || Opc == ARM::t2Bcc;
436 bool isJumpTableBranchOpcode(int Opc) {
437 return Opc == ARM::BR_JTr || Opc == ARM::BR_JTm || Opc == ARM::BR_JTadd ||
438 Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT;
442 bool isIndirectBranchOpcode(int Opc) {
443 return Opc == ARM::BX || Opc == ARM::MOVPCRX || Opc == ARM::tBRIND;
446 static inline bool isPopOpcode(int Opc) {
447 return Opc == ARM::tPOP_RET || Opc == ARM::LDMIA_RET ||
448 Opc == ARM::t2LDMIA_RET || Opc == ARM::tPOP || Opc == ARM::LDMIA_UPD ||
449 Opc == ARM::t2LDMIA_UPD || Opc == ARM::VLDMDIA_UPD;
452 static inline bool isPushOpcode(int Opc) {
453 return Opc == ARM::tPUSH || Opc == ARM::t2STMDB_UPD ||
454 Opc == ARM::STMDB_UPD || Opc == ARM::VSTMDDB_UPD;
457 /// getInstrPredicate - If instruction is predicated, returns its predicate
458 /// condition, otherwise returns AL. It also returns the condition code
459 /// register by reference.
460 ARMCC::CondCodes getInstrPredicate(const MachineInstr &MI, unsigned &PredReg);
462 unsigned getMatchingCondBranchOpcode(unsigned Opc);
464 /// Determine if MI can be folded into an ARM MOVCC instruction, and return the
465 /// opcode of the SSA instruction representing the conditional MI.
466 unsigned canFoldARMInstrIntoMOVCC(unsigned Reg,
468 const MachineRegisterInfo &MRI);
470 /// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether
471 /// the instruction is encoded with an 'S' bit is determined by the optional
472 /// CPSR def operand.
473 unsigned convertAddSubFlagsOpcode(unsigned OldOpc);
475 /// emitARMRegPlusImmediate / emitT2RegPlusImmediate - Emits a series of
476 /// instructions to materializea destreg = basereg + immediate in ARM / Thumb2
478 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
479 MachineBasicBlock::iterator &MBBI,
480 const DebugLoc &dl, unsigned DestReg,
481 unsigned BaseReg, int NumBytes,
482 ARMCC::CondCodes Pred, unsigned PredReg,
483 const ARMBaseInstrInfo &TII, unsigned MIFlags = 0);
485 void emitT2RegPlusImmediate(MachineBasicBlock &MBB,
486 MachineBasicBlock::iterator &MBBI,
487 const DebugLoc &dl, unsigned DestReg,
488 unsigned BaseReg, int NumBytes,
489 ARMCC::CondCodes Pred, unsigned PredReg,
490 const ARMBaseInstrInfo &TII, unsigned MIFlags = 0);
491 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
492 MachineBasicBlock::iterator &MBBI,
493 const DebugLoc &dl, unsigned DestReg,
494 unsigned BaseReg, int NumBytes,
495 const TargetInstrInfo &TII,
496 const ARMBaseRegisterInfo &MRI,
497 unsigned MIFlags = 0);
499 /// Tries to add registers to the reglist of a given base-updating
500 /// push/pop instruction to adjust the stack by an additional
501 /// NumBytes. This can save a few bytes per function in code-size, but
502 /// obviously generates more memory traffic. As such, it only takes
503 /// effect in functions being optimised for size.
504 bool tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
505 MachineFunction &MF, MachineInstr *MI,
508 /// rewriteARMFrameIndex / rewriteT2FrameIndex -
509 /// Rewrite MI to access 'Offset' bytes from the FP. Return false if the
510 /// offset could not be handled directly in MI, and return the left-over
511 /// portion by reference.
512 bool rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
513 unsigned FrameReg, int &Offset,
514 const ARMBaseInstrInfo &TII);
516 bool rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
517 unsigned FrameReg, int &Offset,
518 const ARMBaseInstrInfo &TII);
520 } // End llvm namespace