1 //===-- X86InstrInfo.h - X86 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 X86 implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_TARGET_X86_X86INSTRINFO_H
15 #define LLVM_LIB_TARGET_X86_X86INSTRINFO_H
17 #include "MCTargetDesc/X86BaseInfo.h"
18 #include "X86RegisterInfo.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Target/TargetInstrInfo.h"
22 #define GET_INSTRINFO_HEADER
23 #include "X86GenInstrInfo.inc"
26 class X86RegisterInfo;
30 // X86 specific condition code. These correspond to X86_*_COND in
31 // X86InstrInfo.td. They must be kept in synch.
49 LAST_VALID_COND = COND_S,
51 // Artificial condition codes. These are used by AnalyzeBranch
52 // to indicate a block terminated with two conditional branches to
53 // the same location. This occurs in code using FCMP_OEQ or FCMP_UNE,
54 // which can't be represented on x86 with a single condition. These
55 // are never used in MachineInstrs.
62 // Turn condition code into conditional branch opcode.
63 unsigned GetCondBranchFromCond(CondCode CC);
65 /// \brief Return a set opcode for the given condition and whether it has
67 unsigned getSETFromCond(CondCode CC, bool HasMemoryOperand = false);
69 /// \brief Return a cmov opcode for the given condition, register size in
70 /// bytes, and operand type.
71 unsigned getCMovFromCond(CondCode CC, unsigned RegBytes,
72 bool HasMemoryOperand = false);
74 // Turn CMov opcode into condition code.
75 CondCode getCondFromCMovOpc(unsigned Opc);
77 /// GetOppositeBranchCondition - Return the inverse of the specified cond,
78 /// e.g. turning COND_E to COND_NE.
79 CondCode GetOppositeBranchCondition(CondCode CC);
80 } // end namespace X86;
83 /// isGlobalStubReference - Return true if the specified TargetFlag operand is
84 /// a reference to a stub for a global, not the global itself.
85 inline static bool isGlobalStubReference(unsigned char TargetFlag) {
87 case X86II::MO_DLLIMPORT: // dllimport stub.
88 case X86II::MO_GOTPCREL: // rip-relative GOT reference.
89 case X86II::MO_GOT: // normal GOT reference.
90 case X86II::MO_DARWIN_NONLAZY_PIC_BASE: // Normal $non_lazy_ptr ref.
91 case X86II::MO_DARWIN_NONLAZY: // Normal $non_lazy_ptr ref.
92 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE: // Hidden $non_lazy_ptr ref.
99 /// isGlobalRelativeToPICBase - Return true if the specified global value
100 /// reference is relative to a 32-bit PIC base (X86ISD::GlobalBaseReg). If this
101 /// is true, the addressing mode has the PIC base register added in (e.g. EBX).
102 inline static bool isGlobalRelativeToPICBase(unsigned char TargetFlag) {
103 switch (TargetFlag) {
104 case X86II::MO_GOTOFF: // isPICStyleGOT: local global.
105 case X86II::MO_GOT: // isPICStyleGOT: other global.
106 case X86II::MO_PIC_BASE_OFFSET: // Darwin local global.
107 case X86II::MO_DARWIN_NONLAZY_PIC_BASE: // Darwin/32 external global.
108 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE: // Darwin/32 hidden global.
109 case X86II::MO_TLVP: // ??? Pretty sure..
116 inline static bool isScale(const MachineOperand &MO) {
118 (MO.getImm() == 1 || MO.getImm() == 2 ||
119 MO.getImm() == 4 || MO.getImm() == 8);
122 inline static bool isLeaMem(const MachineInstr *MI, unsigned Op) {
123 if (MI->getOperand(Op).isFI()) return true;
124 return Op+X86::AddrSegmentReg <= MI->getNumOperands() &&
125 MI->getOperand(Op+X86::AddrBaseReg).isReg() &&
126 isScale(MI->getOperand(Op+X86::AddrScaleAmt)) &&
127 MI->getOperand(Op+X86::AddrIndexReg).isReg() &&
128 (MI->getOperand(Op+X86::AddrDisp).isImm() ||
129 MI->getOperand(Op+X86::AddrDisp).isGlobal() ||
130 MI->getOperand(Op+X86::AddrDisp).isCPI() ||
131 MI->getOperand(Op+X86::AddrDisp).isJTI());
134 inline static bool isMem(const MachineInstr *MI, unsigned Op) {
135 if (MI->getOperand(Op).isFI()) return true;
136 return Op+X86::AddrNumOperands <= MI->getNumOperands() &&
137 MI->getOperand(Op+X86::AddrSegmentReg).isReg() &&
141 class X86InstrInfo final : public X86GenInstrInfo {
142 X86Subtarget &Subtarget;
143 const X86RegisterInfo RI;
145 /// RegOp2MemOpTable3Addr, RegOp2MemOpTable0, RegOp2MemOpTable1,
146 /// RegOp2MemOpTable2, RegOp2MemOpTable3 - Load / store folding opcode maps.
148 typedef DenseMap<unsigned,
149 std::pair<unsigned, unsigned> > RegOp2MemOpTableType;
150 RegOp2MemOpTableType RegOp2MemOpTable2Addr;
151 RegOp2MemOpTableType RegOp2MemOpTable0;
152 RegOp2MemOpTableType RegOp2MemOpTable1;
153 RegOp2MemOpTableType RegOp2MemOpTable2;
154 RegOp2MemOpTableType RegOp2MemOpTable3;
155 RegOp2MemOpTableType RegOp2MemOpTable4;
157 /// MemOp2RegOpTable - Load / store unfolding opcode map.
159 typedef DenseMap<unsigned,
160 std::pair<unsigned, unsigned> > MemOp2RegOpTableType;
161 MemOp2RegOpTableType MemOp2RegOpTable;
163 static void AddTableEntry(RegOp2MemOpTableType &R2MTable,
164 MemOp2RegOpTableType &M2RTable,
165 unsigned RegOp, unsigned MemOp, unsigned Flags);
167 virtual void anchor();
169 bool AnalyzeBranchImpl(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
170 MachineBasicBlock *&FBB,
171 SmallVectorImpl<MachineOperand> &Cond,
172 SmallVectorImpl<MachineInstr *> &CondBranches,
173 bool AllowModify) const;
176 explicit X86InstrInfo(X86Subtarget &STI);
178 /// getRegisterInfo - TargetInstrInfo is a superset of MRegister info. As
179 /// such, whenever a client has an instance of instruction info, it should
180 /// always be able to get register info as well (through this method).
182 const X86RegisterInfo &getRegisterInfo() const { return RI; }
184 /// getSPAdjust - This returns the stack pointer adjustment made by
185 /// this instruction. For x86, we need to handle more complex call
186 /// sequences involving PUSHes.
187 int getSPAdjust(const MachineInstr *MI) const override;
189 /// isCoalescableExtInstr - Return true if the instruction is a "coalescable"
190 /// extension instruction. That is, it's like a copy where it's legal for the
191 /// source to overlap the destination. e.g. X86::MOVSX64rr32. If this returns
192 /// true, then it's expected the pre-extension value is available as a subreg
193 /// of the result register. This also returns the sub-register index in
195 bool isCoalescableExtInstr(const MachineInstr &MI,
196 unsigned &SrcReg, unsigned &DstReg,
197 unsigned &SubIdx) const override;
199 unsigned isLoadFromStackSlot(const MachineInstr *MI,
200 int &FrameIndex) const override;
201 /// isLoadFromStackSlotPostFE - Check for post-frame ptr elimination
202 /// stack locations as well. This uses a heuristic so it isn't
203 /// reliable for correctness.
204 unsigned isLoadFromStackSlotPostFE(const MachineInstr *MI,
205 int &FrameIndex) const override;
207 unsigned isStoreToStackSlot(const MachineInstr *MI,
208 int &FrameIndex) const override;
209 /// isStoreToStackSlotPostFE - Check for post-frame ptr elimination
210 /// stack locations as well. This uses a heuristic so it isn't
211 /// reliable for correctness.
212 unsigned isStoreToStackSlotPostFE(const MachineInstr *MI,
213 int &FrameIndex) const override;
215 bool isReallyTriviallyReMaterializable(const MachineInstr *MI,
216 AliasAnalysis *AA) const override;
217 void reMaterialize(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
218 unsigned DestReg, unsigned SubIdx,
219 const MachineInstr *Orig,
220 const TargetRegisterInfo &TRI) const override;
222 /// Given an operand within a MachineInstr, insert preceding code to put it
223 /// into the right format for a particular kind of LEA instruction. This may
224 /// involve using an appropriate super-register instead (with an implicit use
225 /// of the original) or creating a new virtual register and inserting COPY
226 /// instructions to get the data into the right class.
228 /// Reference parameters are set to indicate how caller should add this
229 /// operand to the LEA instruction.
230 bool classifyLEAReg(MachineInstr *MI, const MachineOperand &Src,
231 unsigned LEAOpcode, bool AllowSP,
232 unsigned &NewSrc, bool &isKill,
233 bool &isUndef, MachineOperand &ImplicitOp) const;
235 /// convertToThreeAddress - This method must be implemented by targets that
236 /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
237 /// may be able to convert a two-address instruction into a true
238 /// three-address instruction on demand. This allows the X86 target (for
239 /// example) to convert ADD and SHL instructions into LEA instructions if they
240 /// would require register copies due to two-addressness.
242 /// This method returns a null pointer if the transformation cannot be
243 /// performed, otherwise it returns the new instruction.
245 MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
246 MachineBasicBlock::iterator &MBBI,
247 LiveVariables *LV) const override;
249 /// Returns true iff the routine could find two commutable operands in the
250 /// given machine instruction.
251 /// The 'SrcOpIdx1' and 'SrcOpIdx2' are INPUT and OUTPUT arguments. Their
252 /// input values can be re-defined in this method only if the input values
253 /// are not pre-defined, which is designated by the special value
254 /// 'CommuteAnyOperandIndex' assigned to it.
255 /// If both of indices are pre-defined and refer to some operands, then the
256 /// method simply returns true if the corresponding operands are commutable
257 /// and returns false otherwise.
259 /// For example, calling this method this way:
260 /// unsigned Op1 = 1, Op2 = CommuteAnyOperandIndex;
261 /// findCommutedOpIndices(MI, Op1, Op2);
262 /// can be interpreted as a query asking to find an operand that would be
263 /// commutable with the operand#1.
264 bool findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
265 unsigned &SrcOpIdx2) const override;
268 bool isUnpredicatedTerminator(const MachineInstr* MI) const override;
269 bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
270 MachineBasicBlock *&FBB,
271 SmallVectorImpl<MachineOperand> &Cond,
272 bool AllowModify) const override;
274 bool getMemOpBaseRegImmOfs(MachineInstr *LdSt, unsigned &BaseReg,
276 const TargetRegisterInfo *TRI) const override;
277 bool AnalyzeBranchPredicate(MachineBasicBlock &MBB,
278 TargetInstrInfo::MachineBranchPredicate &MBP,
279 bool AllowModify = false) const override;
281 unsigned RemoveBranch(MachineBasicBlock &MBB) const override;
282 unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
283 MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
284 DebugLoc DL) const override;
285 bool canInsertSelect(const MachineBasicBlock&, ArrayRef<MachineOperand> Cond,
286 unsigned, unsigned, int&, int&, int&) const override;
287 void insertSelect(MachineBasicBlock &MBB,
288 MachineBasicBlock::iterator MI, DebugLoc DL,
289 unsigned DstReg, ArrayRef<MachineOperand> Cond,
290 unsigned TrueReg, unsigned FalseReg) const override;
291 void copyPhysReg(MachineBasicBlock &MBB,
292 MachineBasicBlock::iterator MI, DebugLoc DL,
293 unsigned DestReg, unsigned SrcReg,
294 bool KillSrc) const override;
295 void storeRegToStackSlot(MachineBasicBlock &MBB,
296 MachineBasicBlock::iterator MI,
297 unsigned SrcReg, bool isKill, int FrameIndex,
298 const TargetRegisterClass *RC,
299 const TargetRegisterInfo *TRI) const override;
301 void storeRegToAddr(MachineFunction &MF, unsigned SrcReg, bool isKill,
302 SmallVectorImpl<MachineOperand> &Addr,
303 const TargetRegisterClass *RC,
304 MachineInstr::mmo_iterator MMOBegin,
305 MachineInstr::mmo_iterator MMOEnd,
306 SmallVectorImpl<MachineInstr*> &NewMIs) const;
308 void loadRegFromStackSlot(MachineBasicBlock &MBB,
309 MachineBasicBlock::iterator MI,
310 unsigned DestReg, int FrameIndex,
311 const TargetRegisterClass *RC,
312 const TargetRegisterInfo *TRI) const override;
314 void loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
315 SmallVectorImpl<MachineOperand> &Addr,
316 const TargetRegisterClass *RC,
317 MachineInstr::mmo_iterator MMOBegin,
318 MachineInstr::mmo_iterator MMOEnd,
319 SmallVectorImpl<MachineInstr*> &NewMIs) const;
321 bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const override;
323 /// foldMemoryOperand - If this target supports it, fold a load or store of
324 /// the specified stack slot into the specified machine instruction for the
325 /// specified operand(s). If this is possible, the target should perform the
326 /// folding and return true, otherwise it should return false. If it folds
327 /// the instruction, it is likely that the MachineInstruction the iterator
328 /// references has been changed.
329 MachineInstr *foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
330 ArrayRef<unsigned> Ops,
331 MachineBasicBlock::iterator InsertPt,
332 int FrameIndex) const override;
334 /// foldMemoryOperand - Same as the previous version except it allows folding
335 /// of any load and store from / to any address, not just from a specific
337 MachineInstr *foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
338 ArrayRef<unsigned> Ops,
339 MachineBasicBlock::iterator InsertPt,
340 MachineInstr *LoadMI) const override;
342 /// unfoldMemoryOperand - Separate a single instruction which folded a load or
343 /// a store or a load and a store into two or more instruction. If this is
344 /// possible, returns true as well as the new instructions by reference.
345 bool unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
346 unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
347 SmallVectorImpl<MachineInstr*> &NewMIs) const override;
349 bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
350 SmallVectorImpl<SDNode*> &NewNodes) const override;
352 /// getOpcodeAfterMemoryUnfold - Returns the opcode of the would be new
353 /// instruction after load / store are unfolded from an instruction of the
354 /// specified opcode. It returns zero if the specified unfolding is not
355 /// possible. If LoadRegIndex is non-null, it is filled in with the operand
356 /// index of the operand which will hold the register holding the loaded
358 unsigned getOpcodeAfterMemoryUnfold(unsigned Opc,
359 bool UnfoldLoad, bool UnfoldStore,
360 unsigned *LoadRegIndex = nullptr) const override;
362 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler
363 /// to determine if two loads are loading from the same base address. It
364 /// should only return true if the base pointers are the same and the
365 /// only differences between the two addresses are the offset. It also returns
366 /// the offsets by reference.
367 bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2, int64_t &Offset1,
368 int64_t &Offset2) const override;
370 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
371 /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
372 /// be scheduled togther. On some targets if two loads are loading from
373 /// addresses in the same cache line, it's better if they are scheduled
374 /// together. This function takes two integers that represent the load offsets
375 /// from the common base address. It returns true if it decides it's desirable
376 /// to schedule the two loads together. "NumLoads" is the number of loads that
377 /// have already been scheduled after Load1.
378 bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
379 int64_t Offset1, int64_t Offset2,
380 unsigned NumLoads) const override;
382 bool shouldScheduleAdjacent(MachineInstr* First,
383 MachineInstr *Second) const override;
385 void getNoopForMachoTarget(MCInst &NopInst) const override;
388 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
390 /// isSafeToMoveRegClassDefs - Return true if it's safe to move a machine
391 /// instruction that defines the specified register class.
392 bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const override;
394 /// isSafeToClobberEFLAGS - Return true if it's safe insert an instruction tha
395 /// would clobber the EFLAGS condition register. Note the result may be
396 /// conservative. If it cannot definitely determine the safety after visiting
397 /// a few instructions in each direction it assumes it's not safe.
398 bool isSafeToClobberEFLAGS(MachineBasicBlock &MBB,
399 MachineBasicBlock::iterator I) const;
401 /// True if MI has a condition code def, e.g. EFLAGS, that is
403 bool hasLiveCondCodeDef(MachineInstr *MI) const;
405 /// getGlobalBaseReg - Return a virtual register initialized with the
406 /// the global base register value. Output instructions required to
407 /// initialize the register in the function entry block, if necessary.
409 unsigned getGlobalBaseReg(MachineFunction *MF) const;
411 std::pair<uint16_t, uint16_t>
412 getExecutionDomain(const MachineInstr *MI) const override;
414 void setExecutionDomain(MachineInstr *MI, unsigned Domain) const override;
417 getPartialRegUpdateClearance(const MachineInstr *MI, unsigned OpNum,
418 const TargetRegisterInfo *TRI) const override;
419 unsigned getUndefRegClearance(const MachineInstr *MI, unsigned &OpNum,
420 const TargetRegisterInfo *TRI) const override;
421 void breakPartialRegDependency(MachineBasicBlock::iterator MI, unsigned OpNum,
422 const TargetRegisterInfo *TRI) const override;
424 MachineInstr *foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
426 ArrayRef<MachineOperand> MOs,
427 MachineBasicBlock::iterator InsertPt,
428 unsigned Size, unsigned Alignment,
429 bool AllowCommute) const;
432 getUnconditionalBranch(MCInst &Branch,
433 const MCSymbolRefExpr *BranchTarget) const override;
435 void getTrap(MCInst &MI) const override;
437 unsigned getJumpInstrTableEntryBound() const override;
439 bool isHighLatencyDef(int opc) const override;
441 bool hasHighOperandLatency(const TargetSchedModel &SchedModel,
442 const MachineRegisterInfo *MRI,
443 const MachineInstr *DefMI, unsigned DefIdx,
444 const MachineInstr *UseMI,
445 unsigned UseIdx) const override;
447 bool useMachineCombiner() const override {
451 bool isAssociativeAndCommutative(const MachineInstr &Inst) const override;
453 bool hasReassociableOperands(const MachineInstr &Inst,
454 const MachineBasicBlock *MBB) const override;
456 void setSpecialOperandAttr(MachineInstr &OldMI1, MachineInstr &OldMI2,
457 MachineInstr &NewMI1,
458 MachineInstr &NewMI2) const override;
460 /// analyzeCompare - For a comparison instruction, return the source registers
461 /// in SrcReg and SrcReg2 if having two register operands, and the value it
462 /// compares against in CmpValue. Return true if the comparison instruction
464 bool analyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
465 unsigned &SrcReg2, int &CmpMask,
466 int &CmpValue) const override;
468 /// optimizeCompareInstr - Check if there exists an earlier instruction that
469 /// operates on the same source operands and sets flags in the same way as
470 /// Compare; remove Compare if possible.
471 bool optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg,
472 unsigned SrcReg2, int CmpMask, int CmpValue,
473 const MachineRegisterInfo *MRI) const override;
475 /// optimizeLoadInstr - Try to remove the load by folding it to a register
476 /// operand at the use. We fold the load instructions if and only if the
477 /// def and use are in the same BB. We only look at one load and see
478 /// whether it can be folded into MI. FoldAsLoadDefReg is the virtual register
479 /// defined by the load we are trying to fold. DefMI returns the machine
480 /// instruction that defines FoldAsLoadDefReg, and the function returns
481 /// the machine instruction generated due to folding.
482 MachineInstr* optimizeLoadInstr(MachineInstr *MI,
483 const MachineRegisterInfo *MRI,
484 unsigned &FoldAsLoadDefReg,
485 MachineInstr *&DefMI) const override;
487 std::pair<unsigned, unsigned>
488 decomposeMachineOperandsTargetFlags(unsigned TF) const override;
490 ArrayRef<std::pair<unsigned, const char *>>
491 getSerializableDirectMachineOperandTargetFlags() const override;
494 /// Commutes the operands in the given instruction by changing the operands
495 /// order and/or changing the instruction's opcode and/or the immediate value
498 /// The arguments 'CommuteOpIdx1' and 'CommuteOpIdx2' specify the operands
501 /// Do not call this method for a non-commutable instruction or
502 /// non-commutable operands.
503 /// Even though the instruction is commutable, the method may still
504 /// fail to commute the operands, null pointer is returned in such cases.
505 MachineInstr *commuteInstructionImpl(MachineInstr *MI, bool NewMI,
506 unsigned CommuteOpIdx1,
507 unsigned CommuteOpIdx2) const override;
510 MachineInstr * convertToThreeAddressWithLEA(unsigned MIOpc,
511 MachineFunction::iterator &MFI,
512 MachineBasicBlock::iterator &MBBI,
513 LiveVariables *LV) const;
515 /// isFrameOperand - Return true and the FrameIndex if the specified
516 /// operand and follow operands form a reference to the stack frame.
517 bool isFrameOperand(const MachineInstr *MI, unsigned int Op,
518 int &FrameIndex) const;
521 } // End llvm namespace