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 | #include "Dalvik.h" |
| 18 | #include "CompilerInternals.h" |
| 19 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 20 | namespace art { |
| 21 | |
Ian Rogers | 680b1bd | 2012-03-07 20:18:49 -0800 | [diff] [blame] | 22 | static const char* gOpKindNames[kOpInvalid + 1] = { |
| 23 | "OpMov", |
| 24 | "OpMvn", |
| 25 | "OpCmp", |
| 26 | "OpLsl", |
| 27 | "OpLsr", |
| 28 | "OpAsr", |
| 29 | "OpRor", |
| 30 | "OpNot", |
| 31 | "OpAnd", |
| 32 | "OpOr", |
| 33 | "OpXor", |
| 34 | "OpNeg", |
| 35 | "OpAdd", |
| 36 | "OpAdc", |
| 37 | "OpSub", |
| 38 | "OpSbc", |
| 39 | "OpRsub", |
| 40 | "OpMul", |
| 41 | "OpDiv", |
| 42 | "OpRem", |
| 43 | "OpBic", |
| 44 | "OpCmn", |
| 45 | "OpTst", |
| 46 | "OpBkpt", |
| 47 | "OpBlx", |
| 48 | "OpPush", |
| 49 | "OpPop", |
| 50 | "Op2Char", |
| 51 | "Op2Short", |
| 52 | "Op2Byte", |
| 53 | "OpCondBr", |
| 54 | "OpUncondBr", |
| 55 | "OpBx", |
| 56 | "OpInvalid", |
| 57 | }; |
| 58 | |
| 59 | std::ostream& operator<<(std::ostream& os, const OpKind& kind) { |
| 60 | if (kind >= kOpMov && kind <= kOpInvalid) { |
| 61 | os << gOpKindNames[kind]; |
| 62 | } else { |
| 63 | os << "Unknown Op " << static_cast<int>(kind); |
| 64 | } |
| 65 | return os; |
| 66 | } |
| 67 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 68 | /* Allocate a new basic block */ |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 69 | BasicBlock* oatNewBB(CompilationUnit* cUnit, BBType blockType, int blockId) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 70 | { |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 71 | BasicBlock* bb = (BasicBlock* )oatNew(cUnit, sizeof(BasicBlock), true, |
| 72 | kAllocBB); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 73 | bb->blockType = blockType; |
| 74 | bb->id = blockId; |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 75 | bb->predecessors = (GrowableList*) oatNew(cUnit, sizeof(GrowableList), |
| 76 | false, kAllocPredecessors); |
| 77 | oatInitGrowableList(cUnit, bb->predecessors, |
| 78 | (blockType == kExitBlock) ? 2048 : 2, |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 79 | kListPredecessors); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 80 | return bb; |
| 81 | } |
| 82 | |
| 83 | /* Insert an MIR instruction to the end of a basic block */ |
| 84 | void oatAppendMIR(BasicBlock* bb, MIR* mir) |
| 85 | { |
| 86 | if (bb->firstMIRInsn == NULL) { |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 87 | DCHECK(bb->lastMIRInsn == NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 88 | bb->lastMIRInsn = bb->firstMIRInsn = mir; |
| 89 | mir->prev = mir->next = NULL; |
| 90 | } else { |
| 91 | bb->lastMIRInsn->next = mir; |
| 92 | mir->prev = bb->lastMIRInsn; |
| 93 | mir->next = NULL; |
| 94 | bb->lastMIRInsn = mir; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /* Insert an MIR instruction to the head of a basic block */ |
| 99 | void oatPrependMIR(BasicBlock* bb, MIR* mir) |
| 100 | { |
| 101 | if (bb->firstMIRInsn == NULL) { |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 102 | DCHECK(bb->lastMIRInsn == NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 103 | bb->lastMIRInsn = bb->firstMIRInsn = mir; |
| 104 | mir->prev = mir->next = NULL; |
| 105 | } else { |
| 106 | bb->firstMIRInsn->prev = mir; |
| 107 | mir->next = bb->firstMIRInsn; |
| 108 | mir->prev = NULL; |
| 109 | bb->firstMIRInsn = mir; |
| 110 | } |
| 111 | } |
| 112 | |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame^] | 113 | /* Insert a MIR instruction after the specified MIR */ |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 114 | void oatInsertMIRAfter(BasicBlock* bb, MIR* currentMIR, MIR* newMIR) |
| 115 | { |
| 116 | newMIR->prev = currentMIR; |
| 117 | newMIR->next = currentMIR->next; |
| 118 | currentMIR->next = newMIR; |
| 119 | |
| 120 | if (newMIR->next) { |
| 121 | /* Is not the last MIR in the block */ |
| 122 | newMIR->next->prev = newMIR; |
| 123 | } else { |
| 124 | /* Is the last MIR in the block */ |
| 125 | bb->lastMIRInsn = newMIR; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Append an LIR instruction to the LIR list maintained by a compilation |
| 131 | * unit |
| 132 | */ |
| 133 | void oatAppendLIR(CompilationUnit *cUnit, LIR* lir) |
| 134 | { |
| 135 | if (cUnit->firstLIRInsn == NULL) { |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 136 | DCHECK(cUnit->lastLIRInsn == NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 137 | cUnit->lastLIRInsn = cUnit->firstLIRInsn = lir; |
| 138 | lir->prev = lir->next = NULL; |
| 139 | } else { |
| 140 | cUnit->lastLIRInsn->next = lir; |
| 141 | lir->prev = cUnit->lastLIRInsn; |
| 142 | lir->next = NULL; |
| 143 | cUnit->lastLIRInsn = lir; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Insert an LIR instruction before the current instruction, which cannot be the |
| 149 | * first instruction. |
| 150 | * |
| 151 | * prevLIR <-> newLIR <-> currentLIR |
| 152 | */ |
| 153 | void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR) |
| 154 | { |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 155 | DCHECK(currentLIR->prev != NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 156 | LIR *prevLIR = currentLIR->prev; |
| 157 | |
| 158 | prevLIR->next = newLIR; |
| 159 | newLIR->prev = prevLIR; |
| 160 | newLIR->next = currentLIR; |
| 161 | currentLIR->prev = newLIR; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Insert an LIR instruction after the current instruction, which cannot be the |
| 166 | * first instruction. |
| 167 | * |
| 168 | * currentLIR -> newLIR -> oldNext |
| 169 | */ |
| 170 | void oatInsertLIRAfter(LIR* currentLIR, LIR* newLIR) |
| 171 | { |
| 172 | newLIR->prev = currentLIR; |
| 173 | newLIR->next = currentLIR->next; |
| 174 | currentLIR->next = newLIR; |
| 175 | newLIR->next->prev = newLIR; |
| 176 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 177 | |
| 178 | } // namespace art |