blob: 1567fccf1a5ec4fa52208811ccc989751cfc0b17 [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{
buzbeeba938cb2012-02-03 14:47:55 -080025 BasicBlock* bb = (BasicBlock* )oatNew(cUnit, sizeof(BasicBlock), true,
26 kAllocBB);
buzbee67bf8852011-08-17 17:51:35 -070027 bb->blockType = blockType;
28 bb->id = blockId;
buzbeeba938cb2012-02-03 14:47:55 -080029 bb->predecessors = (GrowableList*) oatNew(cUnit, sizeof(GrowableList),
30 false, kAllocPredecessors);
31 oatInitGrowableList(cUnit, bb->predecessors,
32 (blockType == kExitBlock) ? 2048 : 2,
buzbee5abfa3e2012-01-31 17:01:43 -080033 kListPredecessors);
buzbee67bf8852011-08-17 17:51:35 -070034 return bb;
35}
36
37/* Insert an MIR instruction to the end of a basic block */
38void oatAppendMIR(BasicBlock* bb, MIR* mir)
39{
40 if (bb->firstMIRInsn == NULL) {
buzbeeed3e9302011-09-23 17:34:19 -070041 DCHECK(bb->lastMIRInsn == NULL);
buzbee67bf8852011-08-17 17:51:35 -070042 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 */
53void oatPrependMIR(BasicBlock* bb, MIR* mir)
54{
55 if (bb->firstMIRInsn == NULL) {
buzbeeed3e9302011-09-23 17:34:19 -070056 DCHECK(bb->lastMIRInsn == NULL);
buzbee67bf8852011-08-17 17:51:35 -070057 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 */
68void 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 */
87void oatAppendLIR(CompilationUnit *cUnit, LIR* lir)
88{
89 if (cUnit->firstLIRInsn == NULL) {
buzbeeed3e9302011-09-23 17:34:19 -070090 DCHECK(cUnit->lastLIRInsn == NULL);
buzbee67bf8852011-08-17 17:51:35 -070091 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 */
107void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR)
108{
buzbeeed3e9302011-09-23 17:34:19 -0700109 DCHECK(currentLIR->prev != NULL);
buzbee67bf8852011-08-17 17:51:35 -0700110 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 */
124void 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 Hughes11d1b0c2012-01-23 16:57:47 -0800131
132} // namespace art