blob: 17c22bfdb8217418b7aac9bc383fec4e96a0ea91 [file] [log] [blame]
Chris Lattner128aff42002-12-28 20:32:54 +00001//===- X86RegisterInfo.h - X86 Register Information Impl --------*- C++ -*-===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
John Criswell856ba762003-10-21 15:17:13 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner72614082002-10-25 22:55:53 +00009//
10// This file contains the X86 implementation of the MRegisterInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef X86REGISTERINFO_H
15#define X86REGISTERINFO_H
16
Evan Cheng7f3394f2007-10-01 23:44:33 +000017#include "llvm/ADT/DenseMap.h"
Evan Chengf4c3a592007-08-30 05:54:07 +000018#include "llvm/ADT/SmallVector.h"
Chris Lattner72614082002-10-25 22:55:53 +000019#include "llvm/Target/MRegisterInfo.h"
Chris Lattner7ad3e062003-08-03 15:48:14 +000020#include "X86GenRegisterInfo.h.inc"
21
Brian Gaeked0fde302003-11-11 22:41:34 +000022namespace llvm {
Chris Lattner29268692006-09-05 02:12:02 +000023 class Type;
24 class TargetInstrInfo;
Evan Cheng25ab6902006-09-08 06:48:29 +000025 class X86TargetMachine;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Duncan Sandsee465742007-08-29 19:01:20 +000027/// N86 namespace - Native X86 register numbers
28///
29namespace N86 {
30 enum {
31 EAX = 0, ECX = 1, EDX = 2, EBX = 3, ESP = 4, EBP = 5, ESI = 6, EDI = 7
32 };
33}
34
Anton Korobeynikovf191c802007-11-11 19:50:10 +000035/// DWARFFlavour - Flavour of dwarf regnumbers
36///
37namespace DWARFFlavour {
38 enum {
39 X86_64 = 0, X86_32_Darwin = 1, X86_32_ELF = 2
40 };
41}
42
Jeff Cohend41b30d2006-11-05 19:31:28 +000043class X86RegisterInfo : public X86GenRegisterInfo {
44public:
Evan Cheng25ab6902006-09-08 06:48:29 +000045 X86TargetMachine &TM;
Chris Lattner29268692006-09-05 02:12:02 +000046 const TargetInstrInfo &TII;
Jeff Cohend41b30d2006-11-05 19:31:28 +000047
Evan Cheng25ab6902006-09-08 06:48:29 +000048private:
49 /// Is64Bit - Is the target 64-bits.
Evan Cheng7f3394f2007-10-01 23:44:33 +000050 ///
Evan Cheng25ab6902006-09-08 06:48:29 +000051 bool Is64Bit;
52
53 /// SlotSize - Stack slot size in bytes.
Evan Cheng7f3394f2007-10-01 23:44:33 +000054 ///
Evan Cheng25ab6902006-09-08 06:48:29 +000055 unsigned SlotSize;
56
Evan Chengdb807ed2007-11-05 07:30:01 +000057 /// StackAlign - Default stack alignment.
58 ///
59 unsigned StackAlign;
60
Evan Cheng25ab6902006-09-08 06:48:29 +000061 /// StackPtr - X86 physical register used as stack ptr.
Evan Cheng7f3394f2007-10-01 23:44:33 +000062 ///
Evan Cheng25ab6902006-09-08 06:48:29 +000063 unsigned StackPtr;
64
65 /// FramePtr - X86 physical register used as frame ptr.
Evan Cheng7f3394f2007-10-01 23:44:33 +000066 ///
Evan Cheng25ab6902006-09-08 06:48:29 +000067 unsigned FramePtr;
68
Evan Cheng7f3394f2007-10-01 23:44:33 +000069 /// RegOp2MemOpTable2Addr, RegOp2MemOpTable0, RegOp2MemOpTable1,
70 /// RegOp2MemOpTable2 - Load / store folding opcode maps.
71 ///
72 DenseMap<unsigned*, unsigned> RegOp2MemOpTable2Addr;
73 DenseMap<unsigned*, unsigned> RegOp2MemOpTable0;
74 DenseMap<unsigned*, unsigned> RegOp2MemOpTable1;
75 DenseMap<unsigned*, unsigned> RegOp2MemOpTable2;
76
77 /// MemOp2RegOpTable - Load / store unfolding opcode map.
78 ///
Evan Cheng75b4e462007-10-05 01:34:55 +000079 DenseMap<unsigned*, std::pair<unsigned, unsigned> > MemOp2RegOpTable;
Evan Cheng7f3394f2007-10-01 23:44:33 +000080
Evan Cheng25ab6902006-09-08 06:48:29 +000081public:
82 X86RegisterInfo(X86TargetMachine &tm, const TargetInstrInfo &tii);
Chris Lattner128aff42002-12-28 20:32:54 +000083
Duncan Sandsee465742007-08-29 19:01:20 +000084 /// getX86RegNum - Returns the native X86 register number for the given LLVM
85 /// register identifier.
86 unsigned getX86RegNum(unsigned RegNo);
87
Dale Johannesen483ec212007-11-07 00:25:05 +000088 /// getDwarfRegNum - allows modification of X86GenRegisterInfo::getDwarfRegNum
89 /// (created by TableGen) for target dependencies.
Dale Johannesenb97aec62007-11-13 19:13:01 +000090 int getDwarfRegNum(unsigned RegNum, bool isEH) const;
Dale Johannesen483ec212007-11-07 00:25:05 +000091
Chris Lattner128aff42002-12-28 20:32:54 +000092 /// Code Generation virtual methods...
Evan Cheng7f3394f2007-10-01 23:44:33 +000093 ///
Evan Cheng89d16592007-07-17 07:59:08 +000094 bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
95 MachineBasicBlock::iterator MI,
96 const std::vector<CalleeSavedInfo> &CSI) const;
97
98 bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
99 MachineBasicBlock::iterator MI,
100 const std::vector<CalleeSavedInfo> &CSI) const;
101
Chris Lattner01d0efb2004-08-15 22:15:11 +0000102 void storeRegToStackSlot(MachineBasicBlock &MBB,
Alkis Evlogimenos024126e2004-02-12 08:11:04 +0000103 MachineBasicBlock::iterator MI,
Evan Chengd64b5c82007-12-05 03:14:33 +0000104 unsigned SrcReg, bool isKill, int FrameIndex,
Chris Lattner97d5e642005-09-30 01:29:42 +0000105 const TargetRegisterClass *RC) const;
Chris Lattner01d0efb2004-08-15 22:15:11 +0000106
Evan Chengd64b5c82007-12-05 03:14:33 +0000107 void storeRegToAddr(MachineFunction &MF, unsigned SrcReg, bool isKill,
Evan Chengf0a0cdd2007-10-18 22:40:57 +0000108 SmallVectorImpl<MachineOperand> &Addr,
Evan Cheng75b4e462007-10-05 01:34:55 +0000109 const TargetRegisterClass *RC,
Evan Cheng58184e62007-10-18 21:29:24 +0000110 SmallVectorImpl<MachineInstr*> &NewMIs) const;
Evan Cheng75b4e462007-10-05 01:34:55 +0000111
Chris Lattner01d0efb2004-08-15 22:15:11 +0000112 void loadRegFromStackSlot(MachineBasicBlock &MBB,
113 MachineBasicBlock::iterator MI,
Chris Lattner97d5e642005-09-30 01:29:42 +0000114 unsigned DestReg, int FrameIndex,
115 const TargetRegisterClass *RC) const;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000116
Evan Cheng75b4e462007-10-05 01:34:55 +0000117 void loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Evan Chengf0a0cdd2007-10-18 22:40:57 +0000118 SmallVectorImpl<MachineOperand> &Addr,
Evan Cheng75b4e462007-10-05 01:34:55 +0000119 const TargetRegisterClass *RC,
Evan Cheng58184e62007-10-18 21:29:24 +0000120 SmallVectorImpl<MachineInstr*> &NewMIs) const;
Evan Cheng75b4e462007-10-05 01:34:55 +0000121
Chris Lattner01d0efb2004-08-15 22:15:11 +0000122 void copyRegToReg(MachineBasicBlock &MBB,
123 MachineBasicBlock::iterator MI,
124 unsigned DestReg, unsigned SrcReg,
Evan Cheng9efce632007-09-26 06:25:56 +0000125 const TargetRegisterClass *DestRC,
126 const TargetRegisterClass *SrcRC) const;
Evan Chengbf2c8b32007-03-20 08:09:38 +0000127
Evan Chengff110262007-09-26 21:31:07 +0000128 const TargetRegisterClass *
129 getCrossCopyRegClass(const TargetRegisterClass *RC) const;
130
Evan Chengbf2c8b32007-03-20 08:09:38 +0000131 void reMaterialize(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
132 unsigned DestReg, const MachineInstr *Orig) const;
Chris Lattner128aff42002-12-28 20:32:54 +0000133
Chris Lattner5a051f62004-02-17 05:54:57 +0000134 /// foldMemoryOperand - If this target supports it, fold a load or store of
135 /// the specified stack slot into the specified machine instruction for the
Evan Chengaee4af62007-12-02 08:30:39 +0000136 /// specified operand(s). If this is possible, the target should perform the
Chris Lattner5a051f62004-02-17 05:54:57 +0000137 /// folding and return true, otherwise it should return false. If it folds
138 /// the instruction, it is likely that the MachineInstruction the iterator
139 /// references has been changed.
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000140 MachineInstr* foldMemoryOperand(MachineInstr* MI,
Evan Chengaee4af62007-12-02 08:30:39 +0000141 SmallVectorImpl<unsigned> &Ops,
Evan Chenge62f97c2007-12-01 02:07:52 +0000142 int FrameIndex) const;
143
Evan Chengf4c3a592007-08-30 05:54:07 +0000144 /// foldMemoryOperand - Same as the previous version except it allows folding
145 /// of any load and store from / to any address, not just from a specific
146 /// stack slot.
147 MachineInstr* foldMemoryOperand(MachineInstr* MI,
Evan Chengaee4af62007-12-02 08:30:39 +0000148 SmallVectorImpl<unsigned> &Ops,
Evan Chenge62f97c2007-12-01 02:07:52 +0000149 MachineInstr* LoadMI) const;
150
Evan Chengd64b5c82007-12-05 03:14:33 +0000151 /// canFoldMemoryOperand - Returns true if the specified load / store is
152 /// folding is possible.
153 bool canFoldMemoryOperand(MachineInstr*, SmallVectorImpl<unsigned> &) const;
Evan Cheng66f71632007-10-19 21:23:22 +0000154
Christopher Lamb91ee18c2007-10-18 19:28:55 +0000155 /// unfoldMemoryOperand - Separate a single instruction which folded a load or
Evan Cheng75b4e462007-10-05 01:34:55 +0000156 /// a store or a load and a store into two or more instruction. If this is
157 /// possible, returns true as well as the new instructions by reference.
158 bool unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
Evan Cheng106e8022007-10-13 02:35:06 +0000159 unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
Evan Cheng58184e62007-10-18 21:29:24 +0000160 SmallVectorImpl<MachineInstr*> &NewMIs) const;
Evan Cheng75b4e462007-10-05 01:34:55 +0000161
162 bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
Evan Cheng58184e62007-10-18 21:29:24 +0000163 SmallVectorImpl<SDNode*> &NewNodes) const;
Evan Cheng75b4e462007-10-05 01:34:55 +0000164
Evan Chengf0a0cdd2007-10-18 22:40:57 +0000165 /// getOpcodeAfterMemoryUnfold - Returns the opcode of the would be new
Evan Cheng66f71632007-10-19 21:23:22 +0000166 /// instruction after load / store are unfolded from an instruction of the
167 /// specified opcode. It returns zero if the specified unfolding is not
168 /// possible.
Evan Chengf0a0cdd2007-10-18 22:40:57 +0000169 unsigned getOpcodeAfterMemoryUnfold(unsigned Opc,
170 bool UnfoldLoad, bool UnfoldStore) const;
171
Evan Chengc2b861d2007-01-02 21:33:40 +0000172 /// getCalleeSavedRegs - Return a null-terminated list of all of the
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000173 /// callee-save registers on this target.
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000174 const unsigned *getCalleeSavedRegs(const MachineFunction* MF = 0) const;
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000175
Evan Chengc2b861d2007-01-02 21:33:40 +0000176 /// getCalleeSavedRegClasses - Return a null-terminated list of the preferred
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000177 /// register classes to spill each callee-saved register with. The order and
Evan Chengc2b861d2007-01-02 21:33:40 +0000178 /// length of this list match the getCalleeSavedRegs() list.
Evan Cheng64d80e32007-07-19 01:14:50 +0000179 const TargetRegisterClass* const*
180 getCalleeSavedRegClasses(const MachineFunction *MF = 0) const;
Alkis Evlogimenosb4998662004-02-17 04:33:18 +0000181
Evan Chengb371f452007-02-19 21:49:54 +0000182 /// getReservedRegs - Returns a bitset indexed by physical register number
183 /// indicating if a register is a special register that has particular uses and
184 /// should be considered unavailable at all times, e.g. SP, RA. This is used by
185 /// register scavenger to determine what registers are free.
186 BitVector getReservedRegs(const MachineFunction &MF) const;
187
Evan Chengdc775402007-01-23 00:57:47 +0000188 bool hasFP(const MachineFunction &MF) const;
189
Evan Cheng7e7bbf82007-07-19 00:42:05 +0000190 bool hasReservedCallFrame(MachineFunction &MF) const;
191
Chris Lattnerbb07ef92004-02-14 19:49:54 +0000192 void eliminateCallFramePseudoInstr(MachineFunction &MF,
193 MachineBasicBlock &MBB,
194 MachineBasicBlock::iterator MI) const;
Chris Lattner128aff42002-12-28 20:32:54 +0000195
Evan Cheng5e6df462007-02-28 00:21:17 +0000196 void eliminateFrameIndex(MachineBasicBlock::iterator MI,
Evan Cheng97de9132007-05-01 09:13:03 +0000197 int SPAdj, RegScavenger *RS = NULL) const;
Chris Lattner128aff42002-12-28 20:32:54 +0000198
Chris Lattnerbb07ef92004-02-14 19:49:54 +0000199 void processFunctionBeforeFrameFinalized(MachineFunction &MF) const;
Chris Lattner128aff42002-12-28 20:32:54 +0000200
Chris Lattnerbb07ef92004-02-14 19:49:54 +0000201 void emitPrologue(MachineFunction &MF) const;
202 void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const;
Jim Laskeyf1d78e82006-03-23 18:12:57 +0000203
Jim Laskeya9979182006-03-28 13:48:33 +0000204 // Debug information queries.
Jim Laskey41886992006-04-07 16:34:46 +0000205 unsigned getRARegister() const;
Jim Laskeya9979182006-03-28 13:48:33 +0000206 unsigned getFrameRegister(MachineFunction &MF) const;
Jim Laskey0e410942007-01-24 19:15:24 +0000207 void getInitialFrameState(std::vector<MachineMove> &Moves) const;
Jim Laskey62819f32007-02-21 22:54:50 +0000208
209 // Exception handling queries.
210 unsigned getEHExceptionRegister() const;
211 unsigned getEHHandlerRegister() const;
Evan Chengf4c3a592007-08-30 05:54:07 +0000212
213private:
214 MachineInstr* foldMemoryOperand(MachineInstr* MI,
215 unsigned OpNum,
216 SmallVector<MachineOperand,4> &MOs) const;
Chris Lattner72614082002-10-25 22:55:53 +0000217};
218
Evan Cheng8f7f7122006-05-05 05:40:20 +0000219// getX86SubSuperRegister - X86 utility function. It returns the sub or super
220// register of a specific X86 register.
221// e.g. getX86SubSuperRegister(X86::EAX, MVT::i16) return X86:AX
222unsigned getX86SubSuperRegister(unsigned, MVT::ValueType, bool High=false);
223
Brian Gaeked0fde302003-11-11 22:41:34 +0000224} // End llvm namespace
225
Chris Lattner72614082002-10-25 22:55:53 +0000226#endif