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] = { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 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", |
Ian Rogers | 680b1bd | 2012-03-07 20:18:49 -0800 | [diff] [blame] | 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 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 71 | BasicBlock* bb = (BasicBlock* )oatNew(cUnit, sizeof(BasicBlock), true, |
| 72 | kAllocBB); |
| 73 | bb->blockType = blockType; |
| 74 | bb->id = blockId; |
| 75 | bb->predecessors = (GrowableList*) oatNew(cUnit, sizeof(GrowableList), |
| 76 | false, kAllocPredecessors); |
| 77 | oatInitGrowableList(cUnit, bb->predecessors, |
| 78 | (blockType == kExitBlock) ? 2048 : 2, |
| 79 | kListPredecessors); |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 80 | cUnit->blockIdMap.Put(blockId, blockId); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 81 | return bb; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* Insert an MIR instruction to the end of a basic block */ |
| 85 | void oatAppendMIR(BasicBlock* bb, MIR* mir) |
| 86 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 87 | if (bb->firstMIRInsn == NULL) { |
| 88 | DCHECK(bb->lastMIRInsn == NULL); |
| 89 | bb->lastMIRInsn = bb->firstMIRInsn = mir; |
| 90 | mir->prev = mir->next = NULL; |
| 91 | } else { |
| 92 | bb->lastMIRInsn->next = mir; |
| 93 | mir->prev = bb->lastMIRInsn; |
| 94 | mir->next = NULL; |
| 95 | bb->lastMIRInsn = mir; |
| 96 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* Insert an MIR instruction to the head of a basic block */ |
| 100 | void oatPrependMIR(BasicBlock* bb, MIR* mir) |
| 101 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 102 | if (bb->firstMIRInsn == NULL) { |
| 103 | DCHECK(bb->lastMIRInsn == NULL); |
| 104 | bb->lastMIRInsn = bb->firstMIRInsn = mir; |
| 105 | mir->prev = mir->next = NULL; |
| 106 | } else { |
| 107 | bb->firstMIRInsn->prev = mir; |
| 108 | mir->next = bb->firstMIRInsn; |
| 109 | mir->prev = NULL; |
| 110 | bb->firstMIRInsn = mir; |
| 111 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 112 | } |
| 113 | |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 114 | /* Insert a MIR instruction after the specified MIR */ |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 115 | void oatInsertMIRAfter(BasicBlock* bb, MIR* currentMIR, MIR* newMIR) |
| 116 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 117 | newMIR->prev = currentMIR; |
| 118 | newMIR->next = currentMIR->next; |
| 119 | currentMIR->next = newMIR; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 120 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 121 | if (newMIR->next) { |
| 122 | /* Is not the last MIR in the block */ |
| 123 | newMIR->next->prev = newMIR; |
| 124 | } else { |
| 125 | /* Is the last MIR in the block */ |
| 126 | bb->lastMIRInsn = newMIR; |
| 127 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Append an LIR instruction to the LIR list maintained by a compilation |
| 132 | * unit |
| 133 | */ |
| 134 | void oatAppendLIR(CompilationUnit *cUnit, LIR* lir) |
| 135 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 136 | if (cUnit->firstLIRInsn == NULL) { |
| 137 | DCHECK(cUnit->lastLIRInsn == NULL); |
| 138 | cUnit->lastLIRInsn = cUnit->firstLIRInsn = lir; |
| 139 | lir->prev = lir->next = NULL; |
| 140 | } else { |
| 141 | cUnit->lastLIRInsn->next = lir; |
| 142 | lir->prev = cUnit->lastLIRInsn; |
| 143 | lir->next = NULL; |
| 144 | cUnit->lastLIRInsn = lir; |
| 145 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Insert an LIR instruction before the current instruction, which cannot be the |
| 150 | * first instruction. |
| 151 | * |
| 152 | * prevLIR <-> newLIR <-> currentLIR |
| 153 | */ |
| 154 | void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR) |
| 155 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 156 | DCHECK(currentLIR->prev != NULL); |
| 157 | LIR *prevLIR = currentLIR->prev; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 158 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 159 | prevLIR->next = newLIR; |
| 160 | newLIR->prev = prevLIR; |
| 161 | newLIR->next = currentLIR; |
| 162 | currentLIR->prev = newLIR; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Insert an LIR instruction after the current instruction, which cannot be the |
| 167 | * first instruction. |
| 168 | * |
| 169 | * currentLIR -> newLIR -> oldNext |
| 170 | */ |
| 171 | void oatInsertLIRAfter(LIR* currentLIR, LIR* newLIR) |
| 172 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 173 | newLIR->prev = currentLIR; |
| 174 | newLIR->next = currentLIR->next; |
| 175 | currentLIR->next = newLIR; |
| 176 | newLIR->next->prev = newLIR; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 177 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 178 | |
| 179 | } // namespace art |