blob: e52bd8a4912d6ee745751342024129c8a1fc75ad [file] [log] [blame]
Ben Chenge9695e52009-06-16 16:11:47 -07001/*
2 * Copyright (C) 2009 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 "vm/compiler/CompilerInternals.h"
Bill Buzbee89efc3d2009-07-28 11:22:22 -070019#include "ArmLIR.h"
Ben Chenge9695e52009-06-16 16:11:47 -070020
21/*
22 * Identify unconditional branches that jump to the immediate successor of the
23 * branch itself.
24 */
25static void applyRedundantBranchElimination(CompilationUnit *cUnit)
26{
Bill Buzbee89efc3d2009-07-28 11:22:22 -070027 ArmLIR *thisLIR;
Ben Chenge9695e52009-06-16 16:11:47 -070028
Bill Buzbee89efc3d2009-07-28 11:22:22 -070029 for (thisLIR = (ArmLIR *) cUnit->firstLIRInsn;
30 thisLIR != (ArmLIR *) cUnit->lastLIRInsn;
Ben Chenge9695e52009-06-16 16:11:47 -070031 thisLIR = NEXT_LIR(thisLIR)) {
32
33 /* Branch to the next instruction */
Dan Bornstein9a1f8162010-12-01 17:02:26 -080034 if (thisLIR->opcode == kThumbBUncond) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -070035 ArmLIR *nextLIR = thisLIR;
Ben Chenge9695e52009-06-16 16:11:47 -070036
37 while (true) {
38 nextLIR = NEXT_LIR(nextLIR);
39
40 /*
41 * Is the branch target the next instruction?
42 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -070043 if (nextLIR == (ArmLIR *) thisLIR->generic.target) {
Ben Chengd72564c2011-02-08 17:09:25 -080044 thisLIR->flags.isNop = true;
Ben Chenge9695e52009-06-16 16:11:47 -070045 break;
46 }
47
48 /*
Ben Cheng20d7e6c2011-02-18 17:12:42 -080049 * Found real useful stuff between the branch and the target.
50 * Need to explicitly check the lastLIRInsn here since with
51 * method-based JIT the branch might be the last real
52 * instruction.
Ben Chenge9695e52009-06-16 16:11:47 -070053 */
Ben Cheng20d7e6c2011-02-18 17:12:42 -080054 if (!isPseudoOpcode(nextLIR->opcode) ||
55 (nextLIR = (ArmLIR *) cUnit->lastLIRInsn))
Ben Chenge9695e52009-06-16 16:11:47 -070056 break;
57 }
58 }
59 }
60}
61
62void dvmCompilerApplyGlobalOptimizations(CompilationUnit *cUnit)
63{
64 applyRedundantBranchElimination(cUnit);
65}