blob: 6cb65802833cf79cc9c4d9e4d4cba6dc4e9eaf81 [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
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 Hughes11d1b0c2012-01-23 16:57:47 -080020namespace art {
21
buzbee67bf8852011-08-17 17:51:35 -070022/* Allocate a new basic block */
buzbee5abfa3e2012-01-31 17:01:43 -080023BasicBlock* oatNewBB(CompilationUnit* cUnit, BBType blockType, int blockId)
buzbee67bf8852011-08-17 17:51:35 -070024{
buzbee5abfa3e2012-01-31 17:01:43 -080025 BasicBlock* bb = (BasicBlock* )oatNew(sizeof(BasicBlock), true, kAllocBB);
buzbee67bf8852011-08-17 17:51:35 -070026 bb->blockType = blockType;
27 bb->id = blockId;
buzbee5abfa3e2012-01-31 17:01:43 -080028 bb->predecessors = (GrowableList*) oatNew(sizeof(GrowableList), false,
29 kAllocPredecessors);
30 oatInitGrowableList(bb->predecessors, (blockType == kExitBlock) ? 2048 : 2,
31 kListPredecessors);
buzbee67bf8852011-08-17 17:51:35 -070032 return bb;
33}
34
35/* Insert an MIR instruction to the end of a basic block */
36void oatAppendMIR(BasicBlock* bb, MIR* mir)
37{
38 if (bb->firstMIRInsn == NULL) {
buzbeeed3e9302011-09-23 17:34:19 -070039 DCHECK(bb->lastMIRInsn == NULL);
buzbee67bf8852011-08-17 17:51:35 -070040 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 */
51void oatPrependMIR(BasicBlock* bb, MIR* mir)
52{
53 if (bb->firstMIRInsn == NULL) {
buzbeeed3e9302011-09-23 17:34:19 -070054 DCHECK(bb->lastMIRInsn == NULL);
buzbee67bf8852011-08-17 17:51:35 -070055 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 */
66void 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 */
85void oatAppendLIR(CompilationUnit *cUnit, LIR* lir)
86{
87 if (cUnit->firstLIRInsn == NULL) {
buzbeeed3e9302011-09-23 17:34:19 -070088 DCHECK(cUnit->lastLIRInsn == NULL);
buzbee67bf8852011-08-17 17:51:35 -070089 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 */
105void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR)
106{
buzbeeed3e9302011-09-23 17:34:19 -0700107 DCHECK(currentLIR->prev != NULL);
buzbee67bf8852011-08-17 17:51:35 -0700108 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 */
122void 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 Hughes11d1b0c2012-01-23 16:57:47 -0800129
130} // namespace art