buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_SRC_COMPILER_RALLOC_H_ |
| 18 | #define ART_SRC_COMPILER_RALLOC_H_ |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 19 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 20 | /* |
| 21 | * This file contains target independent register alloction support. |
| 22 | */ |
| 23 | |
| 24 | #include "../CompilerUtility.h" |
| 25 | #include "../CompilerIR.h" |
| 26 | #include "../Dataflow.h" |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 27 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 28 | namespace art { |
| 29 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 30 | /* Static register use counts */ |
Elliott Hughes | 719ace4 | 2012-03-09 18:06:03 -0800 | [diff] [blame^] | 31 | struct RefCounts { |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 32 | int count; |
| 33 | int sReg; |
| 34 | bool doubleStart; // Starting vReg for a double |
Elliott Hughes | 719ace4 | 2012-03-09 18:06:03 -0800 | [diff] [blame^] | 35 | }; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 36 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 37 | |
| 38 | inline int oatS2VReg(CompilationUnit* cUnit, int sReg) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 39 | { |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 40 | DCHECK_NE(sReg, INVALID_SREG); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 41 | return DECODE_REG(oatConvertSSARegToDalvik(cUnit, sReg)); |
| 42 | } |
| 43 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 44 | /* |
| 45 | * Get the "real" sreg number associated with an sReg slot. In general, |
| 46 | * sReg values passed through codegen are the SSA names created by |
| 47 | * dataflow analysis and refer to slot numbers in the cUnit->regLocation |
| 48 | * array. However, renaming is accomplished by simply replacing RegLocation |
| 49 | * entries in the cUnit->reglocation[] array. Therefore, when location |
| 50 | * records for operands are first created, we need to ask the locRecord |
| 51 | * identified by the dataflow pass what it's new name is. |
| 52 | */ |
| 53 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 54 | inline int oatSRegHi(int lowSreg) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 55 | return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1; |
| 56 | } |
| 57 | |
| 58 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 59 | inline bool oatLiveOut(CompilationUnit* cUnit, int sReg) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 60 | { |
| 61 | //For now. |
| 62 | return true; |
| 63 | } |
| 64 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 65 | inline int oatSSASrc(MIR* mir, int num) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 66 | { |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 67 | DCHECK_GT(mir->ssaRep->numUses, num); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 68 | return mir->ssaRep->uses[num]; |
| 69 | } |
| 70 | |
| 71 | extern RegLocation oatEvalLoc(CompilationUnit* cUnit, RegLocation loc, |
| 72 | int regClass, bool update); |
| 73 | /* Mark a temp register as dead. Does not affect allocation state. */ |
| 74 | extern void oatClobber(CompilationUnit* cUnit, int reg); |
| 75 | |
| 76 | extern RegLocation oatUpdateLoc(CompilationUnit* cUnit, |
| 77 | RegLocation loc); |
| 78 | |
| 79 | /* see comments for updateLoc */ |
| 80 | extern RegLocation oatUpdateLocWide(CompilationUnit* cUnit, |
| 81 | RegLocation loc); |
| 82 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 83 | extern RegLocation oatUpdateRawLoc(CompilationUnit* cUnit, |
| 84 | RegLocation loc); |
| 85 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 86 | extern void oatMarkLive(CompilationUnit* cUnit, int reg, int sReg); |
| 87 | |
| 88 | extern void oatMarkTemp(CompilationUnit* cUnit, int reg); |
| 89 | |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 90 | extern void oatUnmarkTemp(CompilationUnit* cUnit, int reg); |
| 91 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 92 | extern void oatMarkDirty(CompilationUnit* cUnit, RegLocation loc); |
| 93 | |
| 94 | extern void oatMarkPair(CompilationUnit* cUnit, int lowReg, |
| 95 | int highReg); |
| 96 | |
| 97 | extern void oatMarkClean(CompilationUnit* cUnit, RegLocation loc); |
| 98 | |
| 99 | extern void oatResetDef(CompilationUnit* cUnit, int reg); |
| 100 | |
| 101 | extern void oatResetDefLoc(CompilationUnit* cUnit, RegLocation rl); |
| 102 | |
| 103 | /* Set up temp & preserved register pools specialized by target */ |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 104 | extern void oatInitPool(struct RegisterInfo* regs, int* regNums, int num); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | * Mark the beginning and end LIR of a def sequence. Note that |
| 108 | * on entry start points to the LIR prior to the beginning of the |
| 109 | * sequence. |
| 110 | */ |
| 111 | extern void oatMarkDef(CompilationUnit* cUnit, RegLocation rl, |
| 112 | LIR* start, LIR* finish); |
| 113 | /* |
| 114 | * Mark the beginning and end LIR of a def sequence. Note that |
| 115 | * on entry start points to the LIR prior to the beginning of the |
| 116 | * sequence. |
| 117 | */ |
| 118 | extern void oatMarkDefWide(CompilationUnit* cUnit, RegLocation rl, |
| 119 | LIR* start, LIR* finish); |
| 120 | |
| 121 | extern RegLocation oatGetSrcWide(CompilationUnit* cUnit, MIR* mir, |
| 122 | int low, int high); |
| 123 | |
| 124 | extern RegLocation oatGetDestWide(CompilationUnit* cUnit, MIR* mir, |
| 125 | int low, int high); |
| 126 | // Get the LocRecord associated with an SSA name use. |
| 127 | extern RegLocation oatGetSrc(CompilationUnit* cUnit, MIR* mir, int num); |
buzbee | e9a72f6 | 2011-09-04 17:59:07 -0700 | [diff] [blame] | 128 | extern RegLocation oatGetRawSrc(CompilationUnit* cUnit, MIR* mir, int num); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 129 | |
| 130 | // Get the LocRecord associated with an SSA name def. |
| 131 | extern RegLocation oatGetDest(CompilationUnit* cUnit, MIR* mir, int num); |
| 132 | |
| 133 | extern RegLocation oatGetReturnWide(CompilationUnit* cUnit); |
| 134 | |
| 135 | /* Clobber all regs that might be used by an external C call */ |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 136 | extern void oatClobberCalleeSave(CompilationUnit* cUnit); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 137 | |
| 138 | extern RegisterInfo *oatIsTemp(CompilationUnit* cUnit, int reg); |
| 139 | |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 140 | extern RegisterInfo *oatIsPromoted(CompilationUnit* cUnit, int reg); |
| 141 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 142 | extern bool oatIsDirty(CompilationUnit* cUnit, int reg); |
| 143 | |
| 144 | extern void oatMarkInUse(CompilationUnit* cUnit, int reg); |
| 145 | |
| 146 | extern int oatAllocTemp(CompilationUnit* cUnit); |
| 147 | |
| 148 | extern int oatAllocTempFloat(CompilationUnit* cUnit); |
| 149 | |
| 150 | //REDO: too many assumptions. |
| 151 | extern int oatAllocTempDouble(CompilationUnit* cUnit); |
| 152 | |
| 153 | extern void oatFreeTemp(CompilationUnit* cUnit, int reg); |
| 154 | |
| 155 | extern void oatResetDefLocWide(CompilationUnit* cUnit, RegLocation rl); |
| 156 | |
| 157 | extern void oatResetDefTracking(CompilationUnit* cUnit); |
| 158 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 159 | extern RegisterInfo *oatIsLive(CompilationUnit* cUnit, int reg); |
| 160 | |
| 161 | /* To be used when explicitly managing register use */ |
buzbee | 2e748f3 | 2011-08-29 21:02:19 -0700 | [diff] [blame] | 162 | extern void oatLockCallTemps(CompilationUnit* cUnit); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 163 | |
buzbee | 0d966cf | 2011-09-08 17:34:58 -0700 | [diff] [blame] | 164 | extern void oatFreeCallTemps(CompilationUnit* cUnit); |
| 165 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 166 | extern void oatFlushAllRegs(CompilationUnit* cUnit); |
| 167 | |
| 168 | extern RegLocation oatGetReturnWideAlt(CompilationUnit* cUnit); |
| 169 | |
| 170 | extern RegLocation oatGetReturn(CompilationUnit* cUnit); |
| 171 | |
| 172 | extern RegLocation oatGetReturnAlt(CompilationUnit* cUnit); |
| 173 | |
| 174 | /* Clobber any temp associated with an sReg. Could be in either class */ |
| 175 | extern void oatClobberSReg(CompilationUnit* cUnit, int sReg); |
| 176 | |
| 177 | /* Return a temp if one is available, -1 otherwise */ |
| 178 | extern int oatAllocFreeTemp(CompilationUnit* cUnit); |
| 179 | |
| 180 | /* Attempt to allocate a callee-save register */ |
| 181 | extern int oatAllocPreservedCoreReg(CompilationUnit* cUnit, int sreg); |
| 182 | extern int oatAllocPreservedFPReg(CompilationUnit* cUnit, int sReg, |
| 183 | bool doubleStart); |
| 184 | |
| 185 | /* |
| 186 | * Similar to oatAllocTemp(), but forces the allocation of a specific |
| 187 | * register. No check is made to see if the register was previously |
| 188 | * allocated. Use with caution. |
| 189 | */ |
| 190 | extern void oatLockTemp(CompilationUnit* cUnit, int reg); |
| 191 | |
| 192 | extern RegLocation oatWideToNarrow(CompilationUnit* cUnit, |
| 193 | RegLocation rl); |
| 194 | |
| 195 | /* |
| 196 | * Free all allocated temps in the temp pools. Note that this does |
| 197 | * not affect the "liveness" of a temp register, which will stay |
| 198 | * live until it is either explicitly killed or reallocated. |
| 199 | */ |
| 200 | extern void oatResetRegPool(CompilationUnit* cUnit); |
| 201 | |
| 202 | extern void oatClobberAllRegs(CompilationUnit* cUnit); |
| 203 | |
| 204 | extern void oatFlushRegWide(CompilationUnit* cUnit, int reg1, int reg2); |
| 205 | |
| 206 | extern void oatFlushReg(CompilationUnit* cUnit, int reg); |
| 207 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 208 | extern void oatDoPromotion(CompilationUnit* cUnit); |
| 209 | extern int oatVRegOffset(CompilationUnit* cUnit, int reg); |
| 210 | extern int oatSRegOffset(CompilationUnit* cUnit, int reg); |
| 211 | extern void oatCountRefs(CompilationUnit*, BasicBlock*, RefCounts*, RefCounts*); |
| 212 | extern int oatSortCounts(const void *val1, const void *val2); |
| 213 | extern void oatDumpCounts(const RefCounts* arr, int size, const char* msg); |
| 214 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 215 | /* |
| 216 | * Architecture-dependent register allocation routines implemented in |
| 217 | * ${TARGET_ARCH}/${TARGET_ARCH_VARIANT}/Ralloc.c |
| 218 | */ |
| 219 | extern int oatAllocTypedTempPair(CompilationUnit* cUnit, |
| 220 | bool fpHint, int regClass); |
| 221 | |
| 222 | extern int oatAllocTypedTemp(CompilationUnit* cUnit, bool fpHint, |
| 223 | int regClass); |
| 224 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 225 | extern void oatRegCopyWide(CompilationUnit* cUnit, int destLo, |
| 226 | int destHi, int srcLo, int srcHi); |
| 227 | |
| 228 | extern void oatFlushRegImpl(CompilationUnit* cUnit, int rBase, |
| 229 | int displacement, int rSrc, OpSize size); |
| 230 | |
| 231 | extern void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase, |
| 232 | int displacement, int rSrcLo, int rSrcHi); |
| 233 | |
buzbee | 6181f79 | 2011-09-29 11:14:04 -0700 | [diff] [blame] | 234 | extern void oatDumpCoreRegPool(CompilationUnit* cUint); |
| 235 | extern void oatDumpFPRegPool(CompilationUnit* cUint); |
| 236 | extern bool oatCheckCorePoolSanity(CompilationUnit* cUnit); |
buzbee | 6825326 | 2011-10-07 14:02:25 -0700 | [diff] [blame] | 237 | extern RegisterInfo* oatGetRegInfo(CompilationUnit* cUnit, int reg); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 238 | extern void oatNopLIR(LIR* lir); |
| 239 | extern bool oatIsFPReg(int reg); |
| 240 | extern uint32_t oatFPRegMask(void); |
| 241 | extern void oatAdjustSpillMask(CompilationUnit* cUnit); |
| 242 | void oatMarkPreservedSingle(CompilationUnit* cUnit, int sReg, int reg); |
| 243 | void oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc); |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 244 | |
| 245 | } // namespace art |
| 246 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 247 | #endif // ART_SRC_COMPILER_RALLOC_H_ |