When compiled with GCC 4.0, a latent bug was exposed where both SparcV9
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9RegInfo.h
1 //===-- SparcV9RegInfo.h - SparcV9 Target Register Info ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is used to describe the register file of the SparcV9 target to
11 // its register allocator.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef SPARCV9REGINFO_H
16 #define SPARCV9REGINFO_H
17
18 #include "llvm/ADT/hash_map"
19 #include <string>
20 #include <cassert>
21
22 namespace llvm {
23
24 class TargetMachine;
25 class IGNode;
26 class Type;
27 class Value;
28 class LiveRangeInfo;
29 class Function;
30 class V9LiveRange;
31 class AddedInstrns;
32 class MachineInstr;
33 class BasicBlock;
34 class SparcV9TargetMachine;
35
36
37 ///----------------------------------------------------------------------------
38 ///   Interface to description of machine register class (e.g., int reg class
39 ///   float reg class etc)
40 ///
41 class TargetRegClassInfo {
42 protected:
43   const unsigned RegClassID;        // integer ID of a reg class
44   const unsigned NumOfAvailRegs;    // # of avail for coloring -without SP etc.
45   const unsigned NumOfAllRegs;      // # of all registers -including SP,g0 etc.
46
47 public:
48   virtual ~TargetRegClassInfo() {}
49
50   inline unsigned getRegClassID()     const { return RegClassID; }
51   inline unsigned getNumOfAvailRegs() const { return NumOfAvailRegs; }
52   inline unsigned getNumOfAllRegs()   const { return NumOfAllRegs; }
53
54   // This method marks the registers used for a given register number.
55   // This defaults to marking a single register but may mark multiple
56   // registers when a single number denotes paired registers.
57   //
58   virtual void markColorsUsed(unsigned RegInClass,
59                               int UserRegType,
60                               int RegTypeWanted,
61                               std::vector<bool> &IsColorUsedArr) const {
62     assert(RegInClass < NumOfAllRegs && RegInClass < IsColorUsedArr.size());
63     assert(UserRegType == RegTypeWanted &&
64        "Default method is probably incorrect for class with multiple types.");
65     IsColorUsedArr[RegInClass] = true;
66   }
67
68   // This method finds unused registers of the specified register type,
69   // using the given "used" flag array IsColorUsedArr.  It defaults to
70   // checking a single entry in the array directly, but that can be overridden
71   // for paired registers and other such silliness.
72   // It returns -1 if no unused color is found.
73   //
74   virtual int findUnusedColor(int RegTypeWanted,
75                           const std::vector<bool> &IsColorUsedArr) const {
76     // find first unused color in the IsColorUsedArr directly
77     unsigned NC = this->getNumOfAvailRegs();
78     assert(IsColorUsedArr.size() >= NC && "Invalid colors-used array");
79     for (unsigned c = 0; c < NC; c++)
80       if (!IsColorUsedArr[c])
81         return c;
82     return -1;
83   }
84
85   // This method should find a color which is not used by neighbors
86   // (i.e., a false position in IsColorUsedArr) and
87   virtual void colorIGNode(IGNode *Node,
88                            const std::vector<bool> &IsColorUsedArr) const = 0;
89
90   // Check whether a specific register is volatile, i.e., whether it is not
91   // preserved across calls
92   virtual bool isRegVolatile(int Reg) const = 0;
93
94   // Check whether a specific register is modified as a side-effect of the
95   // call instruction itself,
96   virtual bool modifiedByCall(int Reg) const { return false; }
97
98   virtual const char* const getRegName(unsigned reg) const = 0;
99
100   TargetRegClassInfo(unsigned ID, unsigned NVR, unsigned NAR)
101     : RegClassID(ID), NumOfAvailRegs(NVR), NumOfAllRegs(NAR) {}
102 };
103
104 /// SparcV9RegInfo - Interface to register info of SparcV9 target machine
105 ///
106 class SparcV9RegInfo {
107   SparcV9RegInfo(const SparcV9RegInfo &);  // DO NOT IMPLEMENT
108   void operator=(const SparcV9RegInfo &); // DO NOT IMPLEMENT
109 protected:
110   // A vector of all machine register classes
111   //
112   std::vector<const TargetRegClassInfo *> MachineRegClassArr;
113
114 public:
115   const TargetMachine &target;
116
117   // A register can be initialized to an invalid number. That number can
118   // be obtained using this method.
119   //
120   static int getInvalidRegNum() { return -1; }
121
122
123   // According the definition of a MachineOperand class, a Value in a
124   // machine instruction can go into either a normal register or a
125   // condition code register. If isCCReg is true below, the ID of the condition
126   // code register class will be returned. Otherwise, the normal register
127   // class (eg. int, float) must be returned.
128
129   // To find the register class used for a specified Type
130   //
131   unsigned getRegClassIDOfType  (const Type *type,
132                                          bool isCCReg = false) const;
133
134   // To find the register class to which a specified register belongs
135   //
136   unsigned getRegClassIDOfRegType(int regType) const;
137
138   unsigned getRegClassIDOfReg(int unifiedRegNum) const {
139     unsigned classId = 0;
140     (void) getClassRegNum(unifiedRegNum, classId);
141     return classId;
142   }
143
144   unsigned int getNumOfRegClasses() const {
145     return MachineRegClassArr.size();
146   }
147
148   const TargetRegClassInfo *getMachineRegClass(unsigned i) const {
149     return MachineRegClassArr[i];
150   }
151
152   // getZeroRegNum - returns the register that is hardwired to always contain
153   // zero, if any (-1 if none). This is the unified register number.
154   //
155   unsigned getZeroRegNum() const;
156
157   // The following methods are used to color special live ranges (e.g.
158   // method args and return values etc.) with specific hardware registers
159   // as required. See SparcRegInfo.cpp for the implementation for Sparc.
160   //
161   void suggestRegs4MethodArgs(const Function *Func,
162                                       LiveRangeInfo& LRI) const;
163
164   void suggestRegs4CallArgs(MachineInstr *CallI,
165                                     LiveRangeInfo& LRI) const;
166
167   void suggestReg4RetValue(MachineInstr *RetI,
168                                    LiveRangeInfo& LRI) const;
169
170   void colorMethodArgs(const Function *Func,
171                            LiveRangeInfo &LRI,
172                            std::vector<MachineInstr*>& InstrnsBefore,
173                            std::vector<MachineInstr*>& InstrnsAfter) const;
174
175
176   // Check whether a specific register is volatile, i.e., whether it is not
177   // preserved across calls
178   inline bool isRegVolatile(int RegClassID, int Reg) const {
179     return MachineRegClassArr[RegClassID]->isRegVolatile(Reg);
180   }
181
182   // Check whether a specific register is modified as a side-effect of the
183   // call instruction itself,
184   inline bool modifiedByCall(int RegClassID, int Reg) const {
185     return MachineRegClassArr[RegClassID]->modifiedByCall(Reg);
186   }
187
188   // getCallAddressReg - Returns the reg used for pushing the address
189   // when a method is called. This can be used for other purposes
190   // between calls
191   //
192   unsigned getCallAddressReg() const;
193
194   // Each register class has a separate space for register IDs. To convert
195   // a regId in a register class to a common Id, or vice versa,
196   // we use the folloing two methods.
197   //
198   // This method converts from class reg. number to unified register number.
199   int getUnifiedRegNum(unsigned regClassID, int reg) const {
200     if (reg == getInvalidRegNum()) { return getInvalidRegNum(); }
201     assert(regClassID < getNumOfRegClasses() && "Invalid register class");
202     int totalRegs = 0;
203     for (unsigned rcid = 0; rcid < regClassID; ++rcid)
204       totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
205     return reg + totalRegs;
206   }
207
208   // This method converts the unified number to the number in its class,
209   // and returns the class ID in regClassID.
210   int getClassRegNum(int uRegNum, unsigned& regClassID) const {
211     if (uRegNum == getInvalidRegNum()) { return getInvalidRegNum(); }
212
213     int totalRegs = 0, rcid = 0, NC = getNumOfRegClasses();
214     while (rcid < NC &&
215            uRegNum>= totalRegs+(int)MachineRegClassArr[rcid]->getNumOfAllRegs())
216     {
217       totalRegs += MachineRegClassArr[rcid]->getNumOfAllRegs();
218       rcid++;
219     }
220     if (rcid == NC) {
221       assert(0 && "getClassRegNum(): Invalid register number");
222       return getInvalidRegNum();
223     }
224     regClassID = rcid;
225     return uRegNum - totalRegs;
226   }
227
228   // Returns the assembly-language name of the specified machine register.
229   //
230   const char * const getUnifiedRegName(int UnifiedRegNum) const {
231     unsigned regClassID = getNumOfRegClasses(); // initialize to invalid value
232     int regNumInClass = getClassRegNum(UnifiedRegNum, regClassID);
233     return MachineRegClassArr[regClassID]->getRegName(regNumInClass);
234   }
235
236   // This method gives the the number of bytes of stack space allocated
237   // to a register when it is spilled to the stack, according to its
238   // register type.
239   //
240   // For SparcV9, currently we allocate 8 bytes on stack for all
241   // register types. We can optimize this later if necessary to save stack
242   // space (However, should make sure that stack alignment is correct)
243   //
244   int getSpilledRegSize(int RegType) const {
245     return 8;
246   }
247
248 private:
249   // Number of registers used for passing int args (usually 6: %o0 - %o5)
250   //
251   unsigned const NumOfIntArgRegs;
252
253   // Number of registers used for passing float args (usually 32: %f0 - %f31)
254   //
255   unsigned const NumOfFloatArgRegs;
256
257   // The following methods are used to color special live ranges (e.g.
258   // function args and return values etc.) with specific hardware registers
259   // as required. See SparcV9RegInfo.cpp for the implementation.
260   //
261   void suggestReg4RetAddr(MachineInstr *RetMI,
262                           LiveRangeInfo &LRI) const;
263
264   void suggestReg4CallAddr(MachineInstr *CallMI, LiveRangeInfo &LRI) const;
265
266   // Helper used by the all the getRegType() functions.
267   int getRegTypeForClassAndType(unsigned regClassID, const Type* type) const;
268
269 public:
270   // Type of registers available in SparcV9. There can be several reg types
271   // in the same class. For instace, the float reg class has Single/Double
272   // types
273   //
274   enum RegTypes {
275     IntRegType,
276     FPSingleRegType,
277     FPDoubleRegType,
278     IntCCRegType,
279     FloatCCRegType,
280     SpecialRegType
281   };
282
283   // The actual register classes in the SparcV9
284   //
285   // **** WARNING: If this enum order is changed, also modify
286   // getRegisterClassOfValue method below since it assumes this particular
287   // order for efficiency.
288   //
289   enum RegClassIDs {
290     IntRegClassID,                      // Integer
291     FloatRegClassID,                    // Float (both single/double)
292     IntCCRegClassID,                    // Int Condition Code
293     FloatCCRegClassID,                  // Float Condition code
294     SpecialRegClassID                   // Special (unallocated) registers
295   };
296
297   SparcV9RegInfo(const SparcV9TargetMachine &tgt);
298
299   ~SparcV9RegInfo() {
300     for (unsigned i = 0, e = MachineRegClassArr.size(); i != e; ++i)
301       delete MachineRegClassArr[i];
302   }
303
304   // Returns the register containing the return address.
305   // It should be made sure that this  register contains the return
306   // value when a return instruction is reached.
307   //
308   unsigned getReturnAddressReg() const;
309
310   // Number of registers used for passing int args (usually 6: %o0 - %o5)
311   // and float args (usually 32: %f0 - %f31)
312   //
313   unsigned const getNumOfIntArgRegs() const   { return NumOfIntArgRegs; }
314   unsigned const getNumOfFloatArgRegs() const { return NumOfFloatArgRegs; }
315
316   // Compute which register can be used for an argument, if any
317   //
318   int regNumForIntArg(bool inCallee, bool isVarArgsCall,
319                       unsigned argNo, unsigned& regClassId) const;
320
321   int regNumForFPArg(unsigned RegType, bool inCallee, bool isVarArgsCall,
322                      unsigned argNo, unsigned& regClassId) const;
323
324
325   // method used for printing a register for debugging purposes
326   //
327   void printReg(const V9LiveRange *LR) const;
328
329   // To obtain the return value and the indirect call address (if any)
330   // contained in a CALL machine instruction
331   //
332   const Value * getCallInstRetVal(const MachineInstr *CallMI) const;
333   const Value * getCallInstIndirectAddrVal(const MachineInstr *CallMI) const;
334
335   // The following methods are used to generate "copy" machine instructions
336   // for an architecture. Currently they are used in TargetRegClass
337   // interface. However, they can be moved to TargetInstrInfo interface if
338   // necessary.
339   //
340   // The function regTypeNeedsScratchReg() can be used to check whether a
341   // scratch register is needed to copy a register of type `regType' to
342   // or from memory.  If so, such a scratch register can be provided by
343   // the caller (e.g., if it knows which regsiters are free); otherwise
344   // an arbitrary one will be chosen and spilled by the copy instructions.
345   // If a scratch reg is needed, the reg. type that must be used
346   // for scratch registers is returned in scratchRegType.
347   //
348
349   bool regTypeNeedsScratchReg(int RegType,
350                               int& scratchRegClassId) const;
351
352   void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
353                    unsigned SrcReg, unsigned DestReg,
354                    int RegType) const;
355
356   void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
357                    unsigned SrcReg, unsigned DestPtrReg,
358                    int Offset, int RegType, int scratchReg = -1) const;
359
360   void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
361                    unsigned SrcPtrReg, int Offset, unsigned DestReg,
362                    int RegType, int scratchReg = -1) const;
363
364   void cpValue2Value(Value *Src, Value *Dest,
365                      std::vector<MachineInstr*>& mvec) const;
366
367   // Get the register type for a register identified different ways.
368   // Note that getRegTypeForLR(LR) != getRegTypeForDataType(LR->getType())!
369   // The reg class of a LR depends both on the Value types in it and whether
370   // they are CC registers or not (for example).
371   int getRegTypeForDataType(const Type* type) const;
372   int getRegTypeForLR(const V9LiveRange *LR) const;
373   int getRegType(int unifiedRegNum) const;
374
375   unsigned getFramePointer() const;
376   unsigned getStackPointer() const;
377 };
378
379 } // End llvm namespace
380
381 #endif // SPARCV9REGINFO_H