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