blob: 11214cf9715162ccf17dd3e2954340a378e7de0e [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#ifndef ART_SRC_COMPILER_COMPILER_H_
18#define ART_SRC_COMPILER_COMPILER_H_
19
Ian Rogers0571d352011-11-03 19:51:38 -070020#include "dex_file.h"
Elliott Hughesadb8c672012-03-06 16:49:32 -080021#include "dex_instruction.h"
Ian Rogers0571d352011-11-03 19:51:38 -070022
buzbee692be802012-08-29 15:52:59 -070023namespace llvm {
24 class Module;
25 class LLVMContext;
26}
buzbee692be802012-08-29 15:52:59 -070027
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080028namespace art {
buzbee692be802012-08-29 15:52:59 -070029namespace greenland {
30 class IntrinsicHelper;
31 class IRBuilder;
32}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080033
buzbee67bf8852011-08-17 17:51:35 -070034#define COMPILER_TRACED(X)
35#define COMPILER_TRACEE(X)
36
buzbee44b412b2012-02-04 08:50:53 -080037/*
38 * Special offsets to denote method entry/exit for debugger update.
39 * NOTE: bit pattern must be loadable using 1 instruction and must
40 * not be a valid Dalvik offset.
41 */
42#define DEBUGGER_METHOD_ENTRY -1
43#define DEBUGGER_METHOD_EXIT -2
44
buzbeee3acd072012-02-25 17:03:10 -080045/*
46 * Assembly is an iterative process, and usually terminates within
47 * two or three passes. This should be high enough to handle bizarre
48 * cases, but detect an infinite loop bug.
49 */
50#define MAX_ASSEMBLER_RETRIES 50
51
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080052/* Suppress optimization if corresponding bit set */
buzbeece302932011-10-04 14:32:18 -070053enum optControlVector {
Bill Buzbeea114add2012-05-03 15:00:40 -070054 kLoadStoreElimination = 0,
55 kLoadHoisting,
56 kSuppressLoads,
57 kNullCheckElimination,
58 kPromoteRegs,
59 kTrackLiveTemps,
60 kSkipLargeMethodOptimization,
61 kSafeOptimizations,
62 kBBOpt,
63 kMatch,
64 kPromoteCompilerTemps,
buzbeece302932011-10-04 14:32:18 -070065};
66
buzbee5abfa3e2012-01-31 17:01:43 -080067/* Type of allocation for memory tuning */
68enum oatAllocKind {
Bill Buzbeea114add2012-05-03 15:00:40 -070069 kAllocMisc,
70 kAllocBB,
71 kAllocLIR,
72 kAllocMIR,
73 kAllocDFInfo,
74 kAllocGrowableList,
75 kAllocGrowableBitMap,
76 kAllocDalvikToSSAMap,
77 kAllocDebugInfo,
78 kAllocSuccessor,
79 kAllocRegAlloc,
80 kAllocData,
81 kAllocPredecessors,
82 kNumAllocKinds
buzbee5abfa3e2012-01-31 17:01:43 -080083};
84
85/* Type of growable list for memory tuning */
86enum oatListKind {
Bill Buzbeea114add2012-05-03 15:00:40 -070087 kListMisc = 0,
88 kListBlockList,
89 kListSSAtoDalvikMap,
90 kListDfsOrder,
91 kListDfsPostOrder,
92 kListDomPostOrderTraversal,
93 kListThrowLaunchPads,
94 kListSuspendLaunchPads,
95 kListSwitchTables,
96 kListFillArrayData,
97 kListSuccessorBlocks,
98 kListPredecessors,
99 kNumListKinds
buzbee5abfa3e2012-01-31 17:01:43 -0800100};
101
102/* Type of growable bitmap for memory tuning */
103enum oatBitMapKind {
Bill Buzbeea114add2012-05-03 15:00:40 -0700104 kBitMapMisc = 0,
105 kBitMapUse,
106 kBitMapDef,
107 kBitMapLiveIn,
108 kBitMapBMatrix,
109 kBitMapDominators,
110 kBitMapIDominated,
111 kBitMapDomFrontier,
112 kBitMapPhi,
113 kBitMapTmpBlocks,
114 kBitMapInputBlocks,
115 kBitMapRegisterV,
116 kBitMapTempSSARegisterV,
117 kBitMapNullCheck,
118 kBitMapTmpBlockV,
119 kBitMapPredecessors,
120 kNumBitMapKinds
buzbee5abfa3e2012-01-31 17:01:43 -0800121};
122
buzbeece302932011-10-04 14:32:18 -0700123/* Force code generation paths for testing */
124enum debugControlVector {
Bill Buzbeea114add2012-05-03 15:00:40 -0700125 kDebugDisplayMissingTargets,
126 kDebugVerbose,
127 kDebugDumpCFG,
128 kDebugSlowFieldPath,
129 kDebugSlowInvokePath,
130 kDebugSlowStringPath,
131 kDebugSlowTypePath,
132 kDebugSlowestFieldPath,
133 kDebugSlowestStringPath,
134 kDebugExerciseResolveMethod,
135 kDebugVerifyDataflow,
136 kDebugShowMemoryUsage,
137 kDebugShowNops,
138 kDebugCountOpcodes,
buzbeed1643e42012-09-05 14:06:51 -0700139 kDebugDumpCheckStats,
buzbeead8f15e2012-06-18 14:49:45 -0700140 kDebugDumpBitcodeFile,
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700141 kDebugVerifyBitcode,
buzbeece302932011-10-04 14:32:18 -0700142};
143
Elliott Hughes719ace42012-03-09 18:06:03 -0800144enum OatMethodAttributes {
Bill Buzbeea114add2012-05-03 15:00:40 -0700145 kIsCallee = 0, /* Code is part of a callee (invoked by a hot trace) */
146 kIsHot, /* Code is part of a hot trace */
147 kIsLeaf, /* Method is leaf */
148 kIsEmpty, /* Method is empty */
149 kIsThrowFree, /* Method doesn't throw */
150 kIsGetter, /* Method fits the getter pattern */
151 kIsSetter, /* Method fits the setter pattern */
152 kCannotCompile, /* Method cannot be compiled */
Elliott Hughes719ace42012-03-09 18:06:03 -0800153};
buzbee67bf8852011-08-17 17:51:35 -0700154
155#define METHOD_IS_CALLEE (1 << kIsCallee)
156#define METHOD_IS_HOT (1 << kIsHot)
157#define METHOD_IS_LEAF (1 << kIsLeaf)
158#define METHOD_IS_EMPTY (1 << kIsEmpty)
159#define METHOD_IS_THROW_FREE (1 << kIsThrowFree)
160#define METHOD_IS_GETTER (1 << kIsGetter)
161#define METHOD_IS_SETTER (1 << kIsSetter)
162#define METHOD_CANNOT_COMPILE (1 << kCannotCompile)
163
164/* Customized node traversal orders for different needs */
Elliott Hughes719ace42012-03-09 18:06:03 -0800165enum DataFlowAnalysisMode {
Bill Buzbeea114add2012-05-03 15:00:40 -0700166 kAllNodes = 0, // All nodes
167 kReachableNodes, // All reachable nodes
168 kPreOrderDFSTraversal, // Depth-First-Search / Pre-Order
169 kPostOrderDFSTraversal, // Depth-First-Search / Post-Order
170 kPostOrderDOMTraversal, // Dominator tree / Post-Order
171 kReversePostOrderTraversal, // Depth-First-Search / reverse Post-Order
Elliott Hughes719ace42012-03-09 18:06:03 -0800172};
buzbee67bf8852011-08-17 17:51:35 -0700173
buzbee4df2bbd2012-10-11 14:46:06 -0700174class LLVMInfo {
buzbee692be802012-08-29 15:52:59 -0700175 public:
buzbee4df2bbd2012-10-11 14:46:06 -0700176 LLVMInfo();
177 ~LLVMInfo();
buzbee692be802012-08-29 15:52:59 -0700178
buzbee692be802012-08-29 15:52:59 -0700179 llvm::LLVMContext* GetLLVMContext() {
180 return llvm_context_.get();
181 }
182
183 llvm::Module* GetLLVMModule() {
TDYa12755e5e6c2012-09-11 15:14:42 -0700184 return llvm_module_;
buzbee692be802012-08-29 15:52:59 -0700185 }
186
187 art::greenland::IntrinsicHelper* GetIntrinsicHelper() {
188 return intrinsic_helper_.get();
189 }
190
191 art::greenland::IRBuilder* GetIRBuilder() {
192 return ir_builder_.get();
193 }
194
195 private:
buzbee692be802012-08-29 15:52:59 -0700196 UniquePtr<llvm::LLVMContext> llvm_context_;
TDYa12755e5e6c2012-09-11 15:14:42 -0700197 llvm::Module* llvm_module_; // Managed by context_
buzbee692be802012-08-29 15:52:59 -0700198 UniquePtr<art::greenland::IntrinsicHelper> intrinsic_helper_;
199 UniquePtr<art::greenland::IRBuilder> ir_builder_;
200};
buzbee692be802012-08-29 15:52:59 -0700201
buzbee67bf8852011-08-17 17:51:35 -0700202struct CompilationUnit;
203struct BasicBlock;
204struct SSARepresentation;
205struct GrowableList;
206struct MIR;
207
buzbeeba938cb2012-02-03 14:47:55 -0800208void oatInit(CompilationUnit* cUnit, const Compiler& compiler);
buzbee67bf8852011-08-17 17:51:35 -0700209bool oatArchInit(void);
buzbee67bf8852011-08-17 17:51:35 -0700210bool oatStartup(void);
211void oatShutdown(void);
buzbee67bf8852011-08-17 17:51:35 -0700212void oatScanAllClassPointers(void (*callback)(void* ptr));
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700213void oatInitializeSSAConversion(CompilationUnit* cUnit);
214int SRegToVReg(const CompilationUnit* cUnit, int ssaReg);
215int SRegToSubscript(const CompilationUnit* cUnit, int ssaReg);
216bool oatFindLocalLiveIn(CompilationUnit* cUnit, BasicBlock* bb);
217bool oatDoSSAConversion(CompilationUnit* cUnit, BasicBlock* bb);
218bool oatDoConstantPropagation(CompilationUnit* cUnit, BasicBlock* bb);
219bool oatFindInductionVariables(CompilationUnit* cUnit, BasicBlock* bb);
buzbee67bf8852011-08-17 17:51:35 -0700220/* Clear the visited flag for each BB */
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700221bool oatClearVisitedFlag(CompilationUnit* cUnit, BasicBlock* bb);
222char* oatGetDalvikDisassembly(CompilationUnit* cUnit, const DecodedInstruction& insn,
buzbeeba938cb2012-02-03 14:47:55 -0800223 const char* note);
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700224char* oatFullDisassembler(CompilationUnit* cUnit, const MIR* mir);
225char* oatGetSSAString(CompilationUnit* cUnit, SSARepresentation* ssaRep);
226void oatDataFlowAnalysisDispatcher(CompilationUnit* cUnit,
Bill Buzbeea114add2012-05-03 15:00:40 -0700227 bool (*func)(CompilationUnit* , BasicBlock*),
228 DataFlowAnalysisMode dfaMode,
229 bool isIterative);
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700230void oatMethodSSATransformation(CompilationUnit* cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700231u8 oatGetRegResourceMask(int reg);
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700232void oatDumpCFG(CompilationUnit* cUnit, const char* dirPrefix);
buzbee67bf8852011-08-17 17:51:35 -0700233void oatProcessSwitchTables(CompilationUnit* cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800234bool oatIsFpReg(int reg);
235uint32_t oatFpRegMask(void);
buzbeead8f15e2012-06-18 14:49:45 -0700236void oatReplaceSpecialChars(std::string& str);
buzbeef58c12c2012-07-03 15:06:29 -0700237BasicBlock* oatFindBlock(CompilationUnit* cUnit, unsigned int codeOffset);
buzbee67bf8852011-08-17 17:51:35 -0700238
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800239} // namespace art
240
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700241extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler,
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800242 const art::DexFile::CodeItem* code_item,
Ian Rogers08f753d2012-08-24 14:35:25 -0700243 uint32_t access_flags,
244 art::InvokeType invoke_type,
245 uint32_t method_idx,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246 jobject class_loader,
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800247 const art::DexFile& dex_file);
248
buzbee67bf8852011-08-17 17:51:35 -0700249#endif // ART_SRC_COMPILER_COMPILER_H_