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 | |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 17 | #ifndef ART_SRC_COMPILER_CODEGEN_RALLOCUTIL_H_ |
| 18 | #define ART_SRC_COMPILER_CODEGEN_RALLOCUTIL_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 | |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 24 | #include "../compiler_utility.h" |
| 25 | #include "../compiler_ir.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 { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 32 | int count; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 33 | int s_reg; |
| 34 | bool double_start; // Starting v_reg 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 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 38 | /* |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 39 | * Get the "real" sreg number associated with an s_reg slot. In general, |
| 40 | * s_reg values passed through codegen are the SSA names created by |
| 41 | * dataflow analysis and refer to slot numbers in the cu->reg_location |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 42 | * array. However, renaming is accomplished by simply replacing RegLocation |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 43 | * entries in the cu->reglocation[] array. Therefore, when location |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 44 | * records for operands are first created, we need to ask the locRecord |
| 45 | * identified by the dataflow pass what it's new name is. |
| 46 | */ |
| 47 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 48 | inline int GetSRegHi(int lowSreg) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 49 | return (lowSreg == INVALID_SREG) ? INVALID_SREG : lowSreg + 1; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 53 | inline bool oat_live_out(CompilationUnit* cu, int s_reg) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 54 | //For now. |
| 55 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 58 | inline int oatSSASrc(MIR* mir, int num) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 59 | DCHECK_GT(mir->ssa_rep->num_uses, num); |
| 60 | return mir->ssa_rep->uses[num]; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 61 | } |
| 62 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 63 | void ClobberSReg(CompilationUnit* cu, int s_reg); |
| 64 | RegLocation EvalLoc(CompilationUnit* cu, RegLocation loc, |
| 65 | int reg_class, bool update); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 66 | /* Mark a temp register as dead. Does not affect allocation state. */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 67 | void Clobber(CompilationUnit* cu, int reg); |
| 68 | RegLocation UpdateLoc(CompilationUnit* cu, RegLocation loc); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 69 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 70 | /* see comments for update_loc */ |
| 71 | RegLocation UpdateLocWide(CompilationUnit* cu, RegLocation loc); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 72 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 73 | RegLocation UpdateRawLoc(CompilationUnit* cu, RegLocation loc); |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 74 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 75 | void MarkLive(CompilationUnit* cu, int reg, int s_reg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 76 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 77 | void MarkTemp(CompilationUnit* cu, int reg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 78 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 79 | void UnmarkTemp(CompilationUnit* cu, int reg); |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 80 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 81 | void MarkDirty(CompilationUnit* cu, RegLocation loc); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 82 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 83 | void MarkPair(CompilationUnit* cu, int low_reg, int high_reg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 84 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 85 | void MarkClean(CompilationUnit* cu, RegLocation loc); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 86 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 87 | void ResetDef(CompilationUnit* cu, int reg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 88 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 89 | void ResetDefLoc(CompilationUnit* cu, RegLocation rl); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 90 | |
| 91 | /* Set up temp & preserved register pools specialized by target */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 92 | void CompilerInitPool(RegisterInfo* regs, int* reg_nums, int num); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * Mark the beginning and end LIR of a def sequence. Note that |
| 96 | * on entry start points to the LIR prior to the beginning of the |
| 97 | * sequence. |
| 98 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 99 | void MarkDef(CompilationUnit* cu, RegLocation rl, LIR* start, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 100 | LIR* finish); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 101 | /* |
| 102 | * Mark the beginning and end LIR of a def sequence. Note that |
| 103 | * on entry start points to the LIR prior to the beginning of the |
| 104 | * sequence. |
| 105 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 106 | void MarkDefWide(CompilationUnit* cu, RegLocation rl, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 107 | LIR* start, LIR* finish); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 108 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 109 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 110 | // Get the LocRecord associated with an SSA name use. |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 111 | RegLocation GetSrc(CompilationUnit* cu, MIR* mir, int num); |
| 112 | RegLocation GetSrcWide(CompilationUnit* cu, MIR* mir, int low); |
buzbee | 15bf980 | 2012-06-12 17:49:27 -0700 | [diff] [blame] | 113 | // Non-width checking version |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 114 | RegLocation GetRawSrc(CompilationUnit* cu, MIR* mir, int num); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 115 | |
| 116 | // Get the LocRecord associated with an SSA name def. |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 117 | RegLocation GetDest(CompilationUnit* cu, MIR* mir); |
| 118 | RegLocation GetDestWide(CompilationUnit* cu, MIR* mir); |
buzbee | 15bf980 | 2012-06-12 17:49:27 -0700 | [diff] [blame] | 119 | // Non-width checking version |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 120 | RegLocation GetRawDest(CompilationUnit* cu, MIR* mir); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 121 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 122 | RegLocation GetReturnWide(CompilationUnit* cu, bool is_double); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 123 | |
| 124 | /* Clobber all regs that might be used by an external C call */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 125 | void ClobberCalleeSave(CompilationUnit* cu); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 126 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 127 | RegisterInfo *IsTemp(CompilationUnit* cu, int reg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 128 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 129 | RegisterInfo *IsPromoted(CompilationUnit* cu, int reg); |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 130 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 131 | bool IsDirty(CompilationUnit* cu, int reg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 132 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 133 | void MarkInUse(CompilationUnit* cu, int reg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 134 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 135 | int AllocTemp(CompilationUnit* cu); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 136 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 137 | int AllocTempFloat(CompilationUnit* cu); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 138 | |
| 139 | //REDO: too many assumptions. |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 140 | int AllocTempDouble(CompilationUnit* cu); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 141 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 142 | void FreeTemp(CompilationUnit* cu, int reg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 143 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 144 | void ResetDefLocWide(CompilationUnit* cu, RegLocation rl); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 145 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 146 | void ResetDefTracking(CompilationUnit* cu); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 147 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 148 | RegisterInfo *IsLive(CompilationUnit* cu, int reg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 149 | |
| 150 | /* To be used when explicitly managing register use */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 151 | void LockCallTemps(CompilationUnit* cu); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 152 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 153 | void FreeCallTemps(CompilationUnit* cu); |
buzbee | 0d966cf | 2011-09-08 17:34:58 -0700 | [diff] [blame] | 154 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 155 | void FlushAllRegs(CompilationUnit* cu); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 156 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 157 | RegLocation GetReturnWideAlt(CompilationUnit* cu); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 158 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 159 | RegLocation GetReturn(CompilationUnit* cu, bool is_float); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 160 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 161 | RegLocation GetReturnAlt(CompilationUnit* cu); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 162 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 163 | /* Clobber any temp associated with an s_reg. Could be in either class */ |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 164 | |
| 165 | /* Return a temp if one is available, -1 otherwise */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 166 | int AllocFreeTemp(CompilationUnit* cu); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 167 | |
| 168 | /* Attempt to allocate a callee-save register */ |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 169 | /* |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 170 | * Similar to AllocTemp(), but forces the allocation of a specific |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 171 | * register. No check is made to see if the register was previously |
| 172 | * allocated. Use with caution. |
| 173 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 174 | void LockTemp(CompilationUnit* cu, int reg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 175 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 176 | RegLocation WideToNarrow(CompilationUnit* cu, RegLocation rl); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | * Free all allocated temps in the temp pools. Note that this does |
| 180 | * not affect the "liveness" of a temp register, which will stay |
| 181 | * live until it is either explicitly killed or reallocated. |
| 182 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 183 | void ResetRegPool(CompilationUnit* cu); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 184 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 185 | void ClobberAllRegs(CompilationUnit* cu); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 186 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 187 | void FlushRegWide(CompilationUnit* cu, int reg1, int reg2); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 188 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 189 | void FlushReg(CompilationUnit* cu, int reg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 190 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 191 | void DoPromotion(CompilationUnit* cu); |
| 192 | int VRegOffset(CompilationUnit* cu, int reg); |
| 193 | int SRegOffset(CompilationUnit* cu, int reg); |
| 194 | void RecordCorePromotion(CompilationUnit* cu, int reg, int s_reg); |
| 195 | void RecordFpPromotion(CompilationUnit* cu, int reg, int s_reg); |
buzbee | ca7a5e4 | 2012-08-20 11:12:18 -0700 | [diff] [blame] | 196 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 197 | |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 198 | /* Architecture-dependent register allocation routines. */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 199 | int AllocTypedTempPair(CompilationUnit* cu, |
| 200 | bool fp_hint, int reg_class); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 201 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 202 | int AllocTypedTemp(CompilationUnit* cu, bool fp_hint, int reg_class); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 203 | |
buzbee | aad9438 | 2012-11-21 07:40:50 -0800 | [diff] [blame] | 204 | void oatDumpFPRegPool(CompilationUnit* cUint); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 205 | RegisterInfo* GetRegInfo(CompilationUnit* cu, int reg); |
buzbee | aad9438 | 2012-11-21 07:40:50 -0800 | [diff] [blame] | 206 | void NopLIR(LIR* lir); |
| 207 | bool oatIsFPReg(int reg); |
| 208 | uint32_t oatFPRegMask(void); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 209 | void AdjustSpillMask(CompilationUnit* cu); |
| 210 | void MarkPreservedSingle(CompilationUnit* cu, int v_reg, int reg); |
| 211 | int ComputeFrameSize(CompilationUnit* cu); |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 212 | |
| 213 | } // namespace art |
| 214 | |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 215 | #endif // ART_SRC_COMPILER_CODEGEN_RALLOCUTIL_H_ |