Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | #include "Dalvik.h" |
| 18 | #include "vm/compiler/CompilerInternals.h" |
Bill Buzbee | 89efc3d | 2009-07-28 11:22:22 -0700 | [diff] [blame] | 19 | #include "ArmLIR.h" |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 20 | |
Bill Buzbee | 7ea0f64 | 2009-08-10 17:06:51 -0700 | [diff] [blame] | 21 | ArmLIR* dvmCompilerGenCopy(CompilationUnit *cUnit, int rDest, int rSrc); |
| 22 | |
| 23 | /* Is this a Dalvik register access? */ |
| 24 | static inline bool isDalvikLoad(ArmLIR *lir) |
| 25 | { |
| 26 | return ((lir->operands[1] == rFP) && |
| 27 | ((lir->opCode == THUMB_LDR_RRI5) || |
| 28 | (lir->opCode == THUMB2_LDR_RRI12) || |
| 29 | (lir->opCode == THUMB2_VLDRS) || |
| 30 | (lir->opCode == THUMB2_VLDRD))); |
| 31 | } |
| 32 | |
| 33 | static inline bool isDalvikStore(ArmLIR *lir) |
| 34 | { |
| 35 | return ((lir->operands[1] == rFP) && |
| 36 | ((lir->opCode == THUMB_STR_RRI5) || |
| 37 | (lir->opCode == THUMB2_STR_RRI12) || |
| 38 | (lir->opCode == THUMB2_VSTRS) || |
| 39 | (lir->opCode == THUMB2_VSTRD))); |
| 40 | } |
| 41 | |
Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame^] | 42 | /* Double regs overlap float regs. Return true if collision */ |
| 43 | static bool regClobber(int reg1, int reg2) |
| 44 | { |
| 45 | int reg1a, reg1b; |
| 46 | int reg2a, reg2b; |
| 47 | if (!FPREG(reg1) || !FPREG(reg2)) |
| 48 | return (reg1 == reg2); |
| 49 | if (DOUBLEREG(reg1)) { |
| 50 | reg1a = reg1 & FP_REG_MASK; |
| 51 | reg1b = reg1a + 1; |
| 52 | } else { |
| 53 | reg1a = reg1b = reg1 & FP_REG_MASK; |
| 54 | } |
| 55 | if (DOUBLEREG(reg2)) { |
| 56 | reg2a = reg2 & FP_REG_MASK; |
| 57 | reg2b = reg2a + 1; |
| 58 | } else { |
| 59 | reg2a = reg2b = reg2 & FP_REG_MASK; |
| 60 | } |
| 61 | return (reg1a == reg2a) || (reg1a == reg2b) || |
| 62 | (reg1b == reg2a) || (reg1b == reg2b); |
| 63 | } |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 64 | /* |
| 65 | * Perform a pass of top-down walk to |
| 66 | * 1) Eliminate redundant loads and stores |
| 67 | * 2) Sink stores to latest possible slot |
| 68 | */ |
| 69 | static void applyLoadStoreElimination(CompilationUnit *cUnit, |
Bill Buzbee | 89efc3d | 2009-07-28 11:22:22 -0700 | [diff] [blame] | 70 | ArmLIR *headLIR, |
| 71 | ArmLIR *tailLIR) |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 72 | { |
Bill Buzbee | 89efc3d | 2009-07-28 11:22:22 -0700 | [diff] [blame] | 73 | ArmLIR *thisLIR; |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 74 | |
| 75 | cUnit->optRound++; |
| 76 | for (thisLIR = headLIR; |
| 77 | thisLIR != tailLIR; |
| 78 | thisLIR = NEXT_LIR(thisLIR)) { |
| 79 | /* Skip newly added instructions */ |
| 80 | if (thisLIR->age >= cUnit->optRound) { |
| 81 | continue; |
| 82 | } |
Bill Buzbee | 7ea0f64 | 2009-08-10 17:06:51 -0700 | [diff] [blame] | 83 | if (isDalvikStore(thisLIR)) { |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 84 | int dRegId = thisLIR->operands[2]; |
| 85 | int nativeRegId = thisLIR->operands[0]; |
Bill Buzbee | 89efc3d | 2009-07-28 11:22:22 -0700 | [diff] [blame] | 86 | ArmLIR *checkLIR; |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 87 | int sinkDistance = 0; |
| 88 | |
| 89 | for (checkLIR = NEXT_LIR(thisLIR); |
| 90 | checkLIR != tailLIR; |
| 91 | checkLIR = NEXT_LIR(checkLIR)) { |
| 92 | |
| 93 | /* Check if a Dalvik register load is redundant */ |
Bill Buzbee | 7ea0f64 | 2009-08-10 17:06:51 -0700 | [diff] [blame] | 94 | if (isDalvikLoad(checkLIR) && |
| 95 | checkLIR->operands[2] == dRegId ) { |
| 96 | if (FPREG(nativeRegId) != FPREG(checkLIR->operands[0])) { |
| 97 | break; // TODO: handle gen<=>float copies |
| 98 | } |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 99 | /* Insert a move to replace the load */ |
| 100 | if (checkLIR->operands[0] != nativeRegId) { |
Bill Buzbee | 7ea0f64 | 2009-08-10 17:06:51 -0700 | [diff] [blame] | 101 | ArmLIR *moveLIR; |
| 102 | moveLIR = dvmCompilerRegCopy(cUnit, |
| 103 | checkLIR->operands[0], |
| 104 | nativeRegId); |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 105 | /* |
| 106 | * Insertion is guaranteed to succeed since checkLIR |
| 107 | * is never the first LIR on the list |
| 108 | */ |
| 109 | dvmCompilerInsertLIRBefore((LIR *) checkLIR, |
| 110 | (LIR *) moveLIR); |
| 111 | } |
| 112 | checkLIR->isNop = true; |
| 113 | continue; |
| 114 | |
| 115 | /* Found a true output dependency - nuke the previous store */ |
Bill Buzbee | 7ea0f64 | 2009-08-10 17:06:51 -0700 | [diff] [blame] | 116 | } else if (isDalvikStore(checkLIR) && |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 117 | checkLIR->operands[2] == dRegId) { |
| 118 | thisLIR->isNop = true; |
| 119 | break; |
| 120 | /* Find out the latest slot that the store can be sunk into */ |
| 121 | } else { |
| 122 | bool stopHere = false; |
| 123 | |
| 124 | /* Last instruction reached */ |
| 125 | stopHere |= checkLIR->generic.next == NULL; |
| 126 | |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 127 | /* |
| 128 | * Conservatively assume there is a memory dependency |
| 129 | * for st/ld multiples and reg+reg address mode |
| 130 | */ |
Bill Buzbee | 89efc3d | 2009-07-28 11:22:22 -0700 | [diff] [blame] | 131 | stopHere |= checkLIR->opCode == THUMB_STMIA || |
| 132 | checkLIR->opCode == THUMB_LDMIA || |
| 133 | checkLIR->opCode == THUMB_STR_RRR || |
Bill Buzbee | 7ea0f64 | 2009-08-10 17:06:51 -0700 | [diff] [blame] | 134 | checkLIR->opCode == THUMB_LDR_RRR || |
Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame^] | 135 | checkLIR->opCode == THUMB2_STR_RRR || |
| 136 | checkLIR->opCode == THUMB2_LDR_RRR || |
| 137 | checkLIR->opCode == THUMB2_STMIA || |
| 138 | checkLIR->opCode == THUMB2_LDMIA || |
Bill Buzbee | 7ea0f64 | 2009-08-10 17:06:51 -0700 | [diff] [blame] | 139 | checkLIR->opCode == THUMB2_VLDRD || |
| 140 | checkLIR->opCode == THUMB2_VSTRD; |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 141 | |
Bill Buzbee | 7ea0f64 | 2009-08-10 17:06:51 -0700 | [diff] [blame] | 142 | if (!isPseudoOpCode(checkLIR->opCode)) { |
Bill Buzbee | 9bc3df3 | 2009-07-30 10:52:29 -0700 | [diff] [blame] | 143 | |
Bill Buzbee | 7ea0f64 | 2009-08-10 17:06:51 -0700 | [diff] [blame] | 144 | /* Store data is clobbered */ |
| 145 | stopHere |= (EncodingMap[checkLIR->opCode].flags & |
| 146 | CLOBBER_DEST) != 0 && |
Bill Buzbee | 270c1d6 | 2009-08-13 16:58:07 -0700 | [diff] [blame^] | 147 | regClobber(checkLIR->operands[0], |
| 148 | nativeRegId); |
Bill Buzbee | 7ea0f64 | 2009-08-10 17:06:51 -0700 | [diff] [blame] | 149 | |
| 150 | stopHere |= (EncodingMap[checkLIR->opCode].flags & |
| 151 | IS_BRANCH) != 0; |
| 152 | } |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 153 | |
| 154 | /* Found a new place to put the store - move it here */ |
| 155 | if (stopHere == true) { |
| 156 | |
| 157 | /* The store can be sunk for at least one cycle */ |
| 158 | if (sinkDistance != 0) { |
Bill Buzbee | 89efc3d | 2009-07-28 11:22:22 -0700 | [diff] [blame] | 159 | ArmLIR *newStoreLIR = |
| 160 | dvmCompilerNew(sizeof(ArmLIR), true); |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 161 | *newStoreLIR = *thisLIR; |
| 162 | newStoreLIR->age = cUnit->optRound; |
| 163 | /* |
| 164 | * Insertion is guaranteed to succeed since checkLIR |
| 165 | * is never the first LIR on the list |
| 166 | */ |
| 167 | dvmCompilerInsertLIRBefore((LIR *) checkLIR, |
| 168 | (LIR *) newStoreLIR); |
| 169 | thisLIR->isNop = true; |
| 170 | } |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Saw a real instruction that the store can be sunk after |
| 176 | */ |
| 177 | if (!isPseudoOpCode(checkLIR->opCode)) { |
| 178 | sinkDistance++; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | void dvmCompilerApplyLocalOptimizations(CompilationUnit *cUnit, LIR *headLIR, |
| 187 | LIR *tailLIR) |
| 188 | { |
| 189 | applyLoadStoreElimination(cUnit, |
Bill Buzbee | 89efc3d | 2009-07-28 11:22:22 -0700 | [diff] [blame] | 190 | (ArmLIR *) headLIR, |
| 191 | (ArmLIR *) tailLIR); |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 192 | } |