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 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 22 | /* Allocate a new basic block */ |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 23 | BasicBlock* oatNewBB(CompilationUnit* cUnit, BBType blockType, int blockId) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 24 | { |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 25 | BasicBlock* bb = (BasicBlock* )oatNew(cUnit, sizeof(BasicBlock), true, |
| 26 | kAllocBB); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 27 | bb->blockType = blockType; |
| 28 | bb->id = blockId; |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 29 | bb->predecessors = (GrowableList*) oatNew(cUnit, sizeof(GrowableList), |
| 30 | false, kAllocPredecessors); |
| 31 | oatInitGrowableList(cUnit, bb->predecessors, |
| 32 | (blockType == kExitBlock) ? 2048 : 2, |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 33 | kListPredecessors); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 34 | return bb; |
| 35 | } |
| 36 | |
| 37 | /* Insert an MIR instruction to the end of a basic block */ |
| 38 | void oatAppendMIR(BasicBlock* bb, MIR* mir) |
| 39 | { |
| 40 | if (bb->firstMIRInsn == NULL) { |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 41 | DCHECK(bb->lastMIRInsn == NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 42 | bb->lastMIRInsn = bb->firstMIRInsn = mir; |
| 43 | mir->prev = mir->next = NULL; |
| 44 | } else { |
| 45 | bb->lastMIRInsn->next = mir; |
| 46 | mir->prev = bb->lastMIRInsn; |
| 47 | mir->next = NULL; |
| 48 | bb->lastMIRInsn = mir; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /* Insert an MIR instruction to the head of a basic block */ |
| 53 | void oatPrependMIR(BasicBlock* bb, MIR* mir) |
| 54 | { |
| 55 | if (bb->firstMIRInsn == NULL) { |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 56 | DCHECK(bb->lastMIRInsn == NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 57 | bb->lastMIRInsn = bb->firstMIRInsn = mir; |
| 58 | mir->prev = mir->next = NULL; |
| 59 | } else { |
| 60 | bb->firstMIRInsn->prev = mir; |
| 61 | mir->next = bb->firstMIRInsn; |
| 62 | mir->prev = NULL; |
| 63 | bb->firstMIRInsn = mir; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* Insert an MIR instruction after the specified MIR */ |
| 68 | void oatInsertMIRAfter(BasicBlock* bb, MIR* currentMIR, MIR* newMIR) |
| 69 | { |
| 70 | newMIR->prev = currentMIR; |
| 71 | newMIR->next = currentMIR->next; |
| 72 | currentMIR->next = newMIR; |
| 73 | |
| 74 | if (newMIR->next) { |
| 75 | /* Is not the last MIR in the block */ |
| 76 | newMIR->next->prev = newMIR; |
| 77 | } else { |
| 78 | /* Is the last MIR in the block */ |
| 79 | bb->lastMIRInsn = newMIR; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Append an LIR instruction to the LIR list maintained by a compilation |
| 85 | * unit |
| 86 | */ |
| 87 | void oatAppendLIR(CompilationUnit *cUnit, LIR* lir) |
| 88 | { |
| 89 | if (cUnit->firstLIRInsn == NULL) { |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 90 | DCHECK(cUnit->lastLIRInsn == NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 91 | cUnit->lastLIRInsn = cUnit->firstLIRInsn = lir; |
| 92 | lir->prev = lir->next = NULL; |
| 93 | } else { |
| 94 | cUnit->lastLIRInsn->next = lir; |
| 95 | lir->prev = cUnit->lastLIRInsn; |
| 96 | lir->next = NULL; |
| 97 | cUnit->lastLIRInsn = lir; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Insert an LIR instruction before the current instruction, which cannot be the |
| 103 | * first instruction. |
| 104 | * |
| 105 | * prevLIR <-> newLIR <-> currentLIR |
| 106 | */ |
| 107 | void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR) |
| 108 | { |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 109 | DCHECK(currentLIR->prev != NULL); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 110 | LIR *prevLIR = currentLIR->prev; |
| 111 | |
| 112 | prevLIR->next = newLIR; |
| 113 | newLIR->prev = prevLIR; |
| 114 | newLIR->next = currentLIR; |
| 115 | currentLIR->prev = newLIR; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Insert an LIR instruction after the current instruction, which cannot be the |
| 120 | * first instruction. |
| 121 | * |
| 122 | * currentLIR -> newLIR -> oldNext |
| 123 | */ |
| 124 | void oatInsertLIRAfter(LIR* currentLIR, LIR* newLIR) |
| 125 | { |
| 126 | newLIR->prev = currentLIR; |
| 127 | newLIR->next = currentLIR->next; |
| 128 | currentLIR->next = newLIR; |
| 129 | newLIR->next->prev = newLIR; |
| 130 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 131 | |
| 132 | } // namespace art |