blob: c39aed3e2d9122f8fb5b871090ac8af0f264437b [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 */
23BasicBlock* oatNewBB(BBType blockType, int blockId)
24{
25 BasicBlock* bb = (BasicBlock* )oatNew(sizeof(BasicBlock), true);
26 bb->blockType = blockType;
27 bb->id = blockId;
28 bb->predecessors = oatAllocBitVector(blockId > 32 ? blockId : 32,
29 true /* expandable */);
30 return bb;
31}
32
33/* Insert an MIR instruction to the end of a basic block */
34void oatAppendMIR(BasicBlock* bb, MIR* mir)
35{
36 if (bb->firstMIRInsn == NULL) {
buzbeeed3e9302011-09-23 17:34:19 -070037 DCHECK(bb->lastMIRInsn == NULL);
buzbee67bf8852011-08-17 17:51:35 -070038 bb->lastMIRInsn = bb->firstMIRInsn = mir;
39 mir->prev = mir->next = NULL;
40 } else {
41 bb->lastMIRInsn->next = mir;
42 mir->prev = bb->lastMIRInsn;
43 mir->next = NULL;
44 bb->lastMIRInsn = mir;
45 }
46}
47
48/* Insert an MIR instruction to the head of a basic block */
49void oatPrependMIR(BasicBlock* bb, MIR* mir)
50{
51 if (bb->firstMIRInsn == NULL) {
buzbeeed3e9302011-09-23 17:34:19 -070052 DCHECK(bb->lastMIRInsn == NULL);
buzbee67bf8852011-08-17 17:51:35 -070053 bb->lastMIRInsn = bb->firstMIRInsn = mir;
54 mir->prev = mir->next = NULL;
55 } else {
56 bb->firstMIRInsn->prev = mir;
57 mir->next = bb->firstMIRInsn;
58 mir->prev = NULL;
59 bb->firstMIRInsn = mir;
60 }
61}
62
63/* Insert an MIR instruction after the specified MIR */
64void oatInsertMIRAfter(BasicBlock* bb, MIR* currentMIR, MIR* newMIR)
65{
66 newMIR->prev = currentMIR;
67 newMIR->next = currentMIR->next;
68 currentMIR->next = newMIR;
69
70 if (newMIR->next) {
71 /* Is not the last MIR in the block */
72 newMIR->next->prev = newMIR;
73 } else {
74 /* Is the last MIR in the block */
75 bb->lastMIRInsn = newMIR;
76 }
77}
78
79/*
80 * Append an LIR instruction to the LIR list maintained by a compilation
81 * unit
82 */
83void oatAppendLIR(CompilationUnit *cUnit, LIR* lir)
84{
85 if (cUnit->firstLIRInsn == NULL) {
buzbeeed3e9302011-09-23 17:34:19 -070086 DCHECK(cUnit->lastLIRInsn == NULL);
buzbee67bf8852011-08-17 17:51:35 -070087 cUnit->lastLIRInsn = cUnit->firstLIRInsn = lir;
88 lir->prev = lir->next = NULL;
89 } else {
90 cUnit->lastLIRInsn->next = lir;
91 lir->prev = cUnit->lastLIRInsn;
92 lir->next = NULL;
93 cUnit->lastLIRInsn = lir;
94 }
95}
96
97/*
98 * Insert an LIR instruction before the current instruction, which cannot be the
99 * first instruction.
100 *
101 * prevLIR <-> newLIR <-> currentLIR
102 */
103void oatInsertLIRBefore(LIR* currentLIR, LIR* newLIR)
104{
buzbeeed3e9302011-09-23 17:34:19 -0700105 DCHECK(currentLIR->prev != NULL);
buzbee67bf8852011-08-17 17:51:35 -0700106 LIR *prevLIR = currentLIR->prev;
107
108 prevLIR->next = newLIR;
109 newLIR->prev = prevLIR;
110 newLIR->next = currentLIR;
111 currentLIR->prev = newLIR;
112}
113
114/*
115 * Insert an LIR instruction after the current instruction, which cannot be the
116 * first instruction.
117 *
118 * currentLIR -> newLIR -> oldNext
119 */
120void oatInsertLIRAfter(LIR* currentLIR, LIR* newLIR)
121{
122 newLIR->prev = currentLIR;
123 newLIR->next = currentLIR->next;
124 currentLIR->next = newLIR;
125 newLIR->next->prev = newLIR;
126}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800127
128} // namespace art