blob: c0943978d0f85601c64509b74517edce5fb6e3e2 [file] [log] [blame]
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001/*
2 * Copyright (C) 2012 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 "ir_builder.h"
18#include "utils_llvm.h"
19
TDYa1275e869b62012-07-25 00:45:39 -070020#include "compiler.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070021#include "greenland/intrinsic_helper.h"
TDYa1275e869b62012-07-25 00:45:39 -070022#include "oat_compilation_unit.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070023#include "object.h"
24#include "thread.h"
TDYa1275e869b62012-07-25 00:45:39 -070025#include "verifier/method_verifier.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070026
TDYa127920be7c2012-09-10 17:13:22 -070027#include "compiler/CompilerIR.h"
28using art::kMIRIgnoreNullCheck;
29using art::kMIRIgnoreRangeCheck;
30
Shih-wei Liao21d28f52012-06-12 05:55:00 -070031#include <llvm/ADT/STLExtras.h>
32#include <llvm/Intrinsics.h>
Logan Chiend36a2ac2012-08-04 00:37:30 +080033#include <llvm/Metadata.h>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070034#include <llvm/Pass.h>
35#include <llvm/Support/CFG.h>
36#include <llvm/Support/InstIterator.h>
37
38#include <vector>
TDYa127aa558872012-08-16 05:11:07 -070039#include <map>
40#include <utility>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070041
TDYa127920be7c2012-09-10 17:13:22 -070042using namespace art::compiler_llvm;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070043
44using art::greenland::IntrinsicHelper;
45
Shih-wei Liaob2596522012-09-14 16:36:11 -070046namespace art {
47extern char remapShorty(char shortyType);
48};
49
Shih-wei Liao21d28f52012-06-12 05:55:00 -070050namespace {
51
52class GBCExpanderPass : public llvm::FunctionPass {
53 private:
54 const IntrinsicHelper& intrinsic_helper_;
55 IRBuilder& irb_;
56
57 llvm::LLVMContext& context_;
58 RuntimeSupportBuilder& rtb_;
59
60 private:
61 llvm::AllocaInst* shadow_frame_;
62 llvm::Value* old_shadow_frame_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070063
64 private:
TDYa127920be7c2012-09-10 17:13:22 -070065 art::Compiler* compiler_;
TDYa1275e869b62012-07-25 00:45:39 -070066
TDYa127920be7c2012-09-10 17:13:22 -070067 const art::DexFile* dex_file_;
68 const art::DexFile::CodeItem* code_item_;
TDYa1275e869b62012-07-25 00:45:39 -070069
TDYa127920be7c2012-09-10 17:13:22 -070070 art::OatCompilationUnit* oat_compilation_unit_;
TDYa1275e869b62012-07-25 00:45:39 -070071
72 uint32_t method_idx_;
73
74 llvm::Function* func_;
75
76 std::vector<llvm::BasicBlock*> basic_blocks_;
77
78 std::vector<llvm::BasicBlock*> basic_block_landing_pads_;
TDYa12755e5e6c2012-09-11 15:14:42 -070079 llvm::BasicBlock* current_bb_;
TDYa127aa558872012-08-16 05:11:07 -070080 std::map<llvm::BasicBlock*, std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> > >
81 landing_pad_phi_mapping_;
TDYa1275e869b62012-07-25 00:45:39 -070082 llvm::BasicBlock* basic_block_unwind_;
83
Logan Chien67645d82012-08-17 09:10:54 +080084 bool changed_;
85
TDYa1275e869b62012-07-25 00:45:39 -070086 private:
Shih-wei Liao21d28f52012-06-12 05:55:00 -070087 //----------------------------------------------------------------------------
Logan Chien75e4b602012-07-23 14:24:12 -070088 // Constant for GBC expansion
89 //----------------------------------------------------------------------------
90 enum IntegerShiftKind {
91 kIntegerSHL,
92 kIntegerSHR,
93 kIntegerUSHR,
94 };
95
96 private:
97 //----------------------------------------------------------------------------
Shih-wei Liao21d28f52012-06-12 05:55:00 -070098 // Helper function for GBC expansion
99 //----------------------------------------------------------------------------
100
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700101 llvm::Value* ExpandToRuntime(runtime_support::RuntimeId rt,
102 llvm::CallInst& inst);
103
TDYa1275e869b62012-07-25 00:45:39 -0700104 uint64_t LV2UInt(llvm::Value* lv) {
105 return llvm::cast<llvm::ConstantInt>(lv)->getZExtValue();
106 }
107
108 int64_t LV2SInt(llvm::Value* lv) {
109 return llvm::cast<llvm::ConstantInt>(lv)->getSExtValue();
110 }
111
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700112 private:
113 // TODO: Almost all Emit* are directly copy-n-paste from MethodCompiler.
114 // Refactor these utility functions from MethodCompiler to avoid forking.
115
Logan Chien67645d82012-08-17 09:10:54 +0800116 void EmitStackOverflowCheck(llvm::Instruction* first_non_alloca);
117
118 void RewriteFunction();
119
120 void RewriteBasicBlock(llvm::BasicBlock* original_block);
121
122 void UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
123 llvm::BasicBlock* new_basic_block);
124
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700125
126 //----------------------------------------------------------------------------
127 // Dex cache code generation helper function
128 //----------------------------------------------------------------------------
TDYa127920be7c2012-09-10 17:13:22 -0700129 llvm::Value* EmitLoadDexCacheAddr(art::MemberOffset dex_cache_offset);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700130
131 llvm::Value* EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx);
132
133 llvm::Value* EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx);
134
135 llvm::Value* EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx);
136
137 llvm::Value* EmitLoadDexCacheStringFieldAddr(uint32_t string_idx);
138
139 //----------------------------------------------------------------------------
140 // Code generation helper function
141 //----------------------------------------------------------------------------
142 llvm::Value* EmitLoadMethodObjectAddr();
143
144 llvm::Value* EmitLoadArrayLength(llvm::Value* array);
145
146 llvm::Value* EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx);
147
148 llvm::Value* EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
149 llvm::Value* this_addr);
150
151 llvm::Value* EmitArrayGEP(llvm::Value* array_addr,
152 llvm::Value* index_value,
153 JType elem_jty);
154
155 private:
156 //----------------------------------------------------------------------------
157 // Expand Greenland intrinsics
158 //----------------------------------------------------------------------------
159 void Expand_TestSuspend(llvm::CallInst& call_inst);
160
TDYa1279a129452012-07-19 03:10:08 -0700161 void Expand_MarkGCCard(llvm::CallInst& call_inst);
162
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700163 llvm::Value* Expand_LoadStringFromDexCache(llvm::Value* string_idx_value);
164
165 llvm::Value* Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value);
166
167 void Expand_LockObject(llvm::Value* obj);
168
169 void Expand_UnlockObject(llvm::Value* obj);
170
171 llvm::Value* Expand_ArrayGet(llvm::Value* array_addr,
172 llvm::Value* index_value,
173 JType elem_jty);
174
175 void Expand_ArrayPut(llvm::Value* new_value,
176 llvm::Value* array_addr,
177 llvm::Value* index_value,
178 JType elem_jty);
179
180 void Expand_FilledNewArray(llvm::CallInst& call_inst);
181
182 llvm::Value* Expand_IGetFast(llvm::Value* field_offset_value,
183 llvm::Value* is_volatile_value,
184 llvm::Value* object_addr,
185 JType field_jty);
186
187 void Expand_IPutFast(llvm::Value* field_offset_value,
188 llvm::Value* is_volatile_value,
189 llvm::Value* object_addr,
190 llvm::Value* new_value,
191 JType field_jty);
192
193 llvm::Value* Expand_SGetFast(llvm::Value* static_storage_addr,
194 llvm::Value* field_offset_value,
195 llvm::Value* is_volatile_value,
196 JType field_jty);
197
198 void Expand_SPutFast(llvm::Value* static_storage_addr,
199 llvm::Value* field_offset_value,
200 llvm::Value* is_volatile_value,
201 llvm::Value* new_value,
202 JType field_jty);
203
204 llvm::Value* Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr);
205
206 llvm::Value* Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value);
207
208 llvm::Value*
209 Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value);
210
211 llvm::Value*
212 Expand_GetVirtualCalleeMethodObjAddrFast(llvm::Value* vtable_idx_value,
213 llvm::Value* this_addr);
214
215 llvm::Value* Expand_Invoke(llvm::CallInst& call_inst);
216
TDYa1274ec8ccd2012-08-11 07:04:57 -0700217 llvm::Value* Expand_DivRem(llvm::CallInst& call_inst, bool is_div, JType op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700218
TDYa1278e950c12012-11-02 09:58:19 -0700219 void Expand_AllocaShadowFrame(llvm::Value* num_entry_value, llvm::Value* num_vregs_value);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700220
221 void Expand_SetShadowFrameEntry(llvm::Value* obj, llvm::Value* entry_idx);
222
TDYa1278e950c12012-11-02 09:58:19 -0700223 void Expand_SetVReg(llvm::Value* entry_idx, llvm::Value* obj);
224
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700225 void Expand_PopShadowFrame();
226
227 void Expand_UpdateDexPC(llvm::Value* dex_pc_value);
228
TDYa127a1b21852012-07-23 03:20:39 -0700229 //----------------------------------------------------------------------------
230 // Quick
231 //----------------------------------------------------------------------------
232
233 llvm::Value* Expand_FPCompare(llvm::Value* src1_value,
234 llvm::Value* src2_value,
235 bool gt_bias);
236
237 llvm::Value* Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value);
238
239 llvm::Value* EmitCompareResultSelection(llvm::Value* cmp_eq,
240 llvm::Value* cmp_lt);
241
TDYa127f71bf5a2012-07-29 20:09:52 -0700242 llvm::Value* EmitLoadConstantClass(uint32_t dex_pc, uint32_t type_idx);
TDYa1275a26d442012-07-26 18:58:38 -0700243 llvm::Value* EmitLoadStaticStorage(uint32_t dex_pc, uint32_t type_idx);
244
TDYa1275e869b62012-07-25 00:45:39 -0700245 llvm::Value* Expand_HLIGet(llvm::CallInst& call_inst, JType field_jty);
246 void Expand_HLIPut(llvm::CallInst& call_inst, JType field_jty);
247
TDYa1275a26d442012-07-26 18:58:38 -0700248 llvm::Value* Expand_HLSget(llvm::CallInst& call_inst, JType field_jty);
249 void Expand_HLSput(llvm::CallInst& call_inst, JType field_jty);
250
251 llvm::Value* Expand_HLArrayGet(llvm::CallInst& call_inst, JType field_jty);
252 void Expand_HLArrayPut(llvm::CallInst& call_inst, JType field_jty);
253
TDYa127f71bf5a2012-07-29 20:09:52 -0700254 llvm::Value* Expand_ConstString(llvm::CallInst& call_inst);
255 llvm::Value* Expand_ConstClass(llvm::CallInst& call_inst);
256
257 void Expand_MonitorEnter(llvm::CallInst& call_inst);
258 void Expand_MonitorExit(llvm::CallInst& call_inst);
259
260 void Expand_HLCheckCast(llvm::CallInst& call_inst);
261 llvm::Value* Expand_InstanceOf(llvm::CallInst& call_inst);
262
263 llvm::Value* Expand_NewInstance(llvm::CallInst& call_inst);
264
265 llvm::Value* Expand_HLInvoke(llvm::CallInst& call_inst);
266
267 llvm::Value* Expand_OptArrayLength(llvm::CallInst& call_inst);
268 llvm::Value* Expand_NewArray(llvm::CallInst& call_inst);
269 llvm::Value* Expand_HLFilledNewArray(llvm::CallInst& call_inst);
270 void Expand_HLFillArrayData(llvm::CallInst& call_inst);
271
272 llvm::Value* EmitAllocNewArray(uint32_t dex_pc,
273 llvm::Value* array_length_value,
274 uint32_t type_idx,
275 bool is_filled_new_array);
276
277 llvm::Value* EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -0700278 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -0700279 llvm::Value* this_addr,
280 uint32_t dex_pc,
281 bool is_fast_path);
282
TDYa1275e869b62012-07-25 00:45:39 -0700283 void EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr);
284
285 void EmitUpdateDexPC(uint32_t dex_pc);
286
287 void EmitGuard_DivZeroException(uint32_t dex_pc,
288 llvm::Value* denominator,
289 JType op_jty);
290
291 void EmitGuard_NullPointerException(uint32_t dex_pc,
292 llvm::Value* object);
293
294 void EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
295 llvm::Value* array,
296 llvm::Value* index);
297
TDYa1275e869b62012-07-25 00:45:39 -0700298 llvm::FunctionType* GetFunctionType(uint32_t method_idx, bool is_static);
299
300 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
301
302 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
303 const char* postfix);
304
305 int32_t GetTryItemOffset(uint32_t dex_pc);
306
307 llvm::BasicBlock* GetLandingPadBasicBlock(uint32_t dex_pc);
308
309 llvm::BasicBlock* GetUnwindBasicBlock();
310
311 void EmitGuard_ExceptionLandingPad(uint32_t dex_pc);
312
313 void EmitBranchExceptionLandingPad(uint32_t dex_pc);
314
Logan Chien75e4b602012-07-23 14:24:12 -0700315 //----------------------------------------------------------------------------
316 // Expand Arithmetic Helper Intrinsics
317 //----------------------------------------------------------------------------
318
319 llvm::Value* Expand_IntegerShift(llvm::Value* src1_value,
320 llvm::Value* src2_value,
321 IntegerShiftKind kind,
322 JType op_jty);
323
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700324 public:
325 static char ID;
326
327 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb)
328 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
Logan Chiene5b8f8b2012-08-13 11:45:05 +0800329 context_(irb.getContext()), rtb_(irb.Runtime()),
TDYa1278e950c12012-11-02 09:58:19 -0700330 shadow_frame_(NULL), old_shadow_frame_(NULL),
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700331 compiler_(NULL), dex_file_(NULL), code_item_(NULL),
Logan Chien67645d82012-08-17 09:10:54 +0800332 oat_compilation_unit_(NULL), method_idx_(-1u), func_(NULL),
333 changed_(false)
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700334 { }
335
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700336 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
TDYa127920be7c2012-09-10 17:13:22 -0700337 art::Compiler* compiler, art::OatCompilationUnit* oat_compilation_unit)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700338 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
339 context_(irb.getContext()), rtb_(irb.Runtime()),
TDYa1278e950c12012-11-02 09:58:19 -0700340 shadow_frame_(NULL), old_shadow_frame_(NULL),
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700341 compiler_(compiler),
342 dex_file_(oat_compilation_unit->GetDexFile()),
343 code_item_(oat_compilation_unit->GetCodeItem()),
344 oat_compilation_unit_(oat_compilation_unit),
345 method_idx_(oat_compilation_unit->GetDexMethodIndex()),
346 func_(NULL), changed_(false)
347 { }
348
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700349 bool runOnFunction(llvm::Function& func);
350
351 private:
Logan Chien67645d82012-08-17 09:10:54 +0800352 void InsertStackOverflowCheck(llvm::Function& func);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700353
354 llvm::Value* ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
355 llvm::CallInst& call_inst);
356
357};
358
359char GBCExpanderPass::ID = 0;
360
361bool GBCExpanderPass::runOnFunction(llvm::Function& func) {
TDYa127b672d1e2012-06-28 21:21:45 -0700362 // Runtime support or stub
363 if (func.getName().startswith("art_") || func.getName().startswith("Art")) {
364 return false;
365 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700366
Logan Chien67645d82012-08-17 09:10:54 +0800367 // Setup rewrite context
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700368 shadow_frame_ = NULL;
369 old_shadow_frame_ = NULL;
TDYa1275e869b62012-07-25 00:45:39 -0700370 func_ = &func;
Logan Chien67645d82012-08-17 09:10:54 +0800371 changed_ = false; // Assume unchanged
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700372
buzbeec531cef2012-10-18 07:09:20 -0700373#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700374 basic_blocks_.resize(code_item_->insns_size_in_code_units_);
375 basic_block_landing_pads_.resize(code_item_->tries_size_, NULL);
376 basic_block_unwind_ = NULL;
377 for (llvm::Function::iterator bb_iter = func_->begin(), bb_end = func_->end();
378 bb_iter != bb_end;
379 ++bb_iter) {
380 if (bb_iter->begin()->getMetadata("DexOff") == NULL) {
381 continue;
382 }
383 uint32_t dex_pc = LV2UInt(bb_iter->begin()->getMetadata("DexOff")->getOperand(0));
384 basic_blocks_[dex_pc] = bb_iter;
385 }
386#endif
387
Logan Chien67645d82012-08-17 09:10:54 +0800388 // Insert stack overflow check
389 InsertStackOverflowCheck(func); // TODO: Use intrinsic.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700390
Logan Chien67645d82012-08-17 09:10:54 +0800391 // Rewrite the intrinsics
392 RewriteFunction();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700393
394 VERIFY_LLVM_FUNCTION(func);
395
Logan Chien67645d82012-08-17 09:10:54 +0800396 return changed_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700397}
398
Logan Chien67645d82012-08-17 09:10:54 +0800399void GBCExpanderPass::RewriteBasicBlock(llvm::BasicBlock* original_block) {
400 llvm::BasicBlock* curr_basic_block = original_block;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700401
Logan Chien67645d82012-08-17 09:10:54 +0800402 llvm::BasicBlock::iterator inst_iter = original_block->begin();
403 llvm::BasicBlock::iterator inst_end = original_block->end();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700404
Logan Chien67645d82012-08-17 09:10:54 +0800405 while (inst_iter != inst_end) {
406 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst_iter);
407 IntrinsicHelper::IntrinsicId intr_id = IntrinsicHelper::UnknownId;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700408
Logan Chien67645d82012-08-17 09:10:54 +0800409 if (call_inst) {
410 llvm::Function* callee_func = call_inst->getCalledFunction();
411 intr_id = intrinsic_helper_.GetIntrinsicId(callee_func);
412 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700413
Logan Chien67645d82012-08-17 09:10:54 +0800414 if (intr_id == IntrinsicHelper::UnknownId) {
415 // This is not intrinsic call. Skip this instruction.
416 ++inst_iter;
417 continue;
418 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700419
Logan Chien67645d82012-08-17 09:10:54 +0800420 // Rewrite the intrinsic and change the function
421 changed_ = true;
422 irb_.SetInsertPoint(inst_iter);
423
424 // Expand the intrinsic
425 if (llvm::Value* new_value = ExpandIntrinsic(intr_id, *call_inst)) {
426 inst_iter->replaceAllUsesWith(new_value);
427 }
428
429 // Remove the old intrinsic call instruction
430 llvm::BasicBlock::iterator old_inst = inst_iter++;
431 old_inst->eraseFromParent();
432
433 // Splice the instruction to the new basic block
434 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
435 if (next_basic_block != curr_basic_block) {
436 next_basic_block->getInstList().splice(
437 irb_.GetInsertPoint(), curr_basic_block->getInstList(),
438 inst_iter, inst_end);
439 curr_basic_block = next_basic_block;
440 inst_end = curr_basic_block->end();
441 }
442 }
443}
444
445
446void GBCExpanderPass::RewriteFunction() {
447 size_t num_basic_blocks = func_->getBasicBlockList().size();
448 // NOTE: We are not using (bb_iter != bb_end) as the for-loop condition,
449 // because we will create new basic block while expanding the intrinsics.
450 // We only want to iterate through the input basic blocks.
451
TDYa127aa558872012-08-16 05:11:07 -0700452 landing_pad_phi_mapping_.clear();
453
Logan Chien67645d82012-08-17 09:10:54 +0800454 for (llvm::Function::iterator bb_iter = func_->begin();
455 num_basic_blocks > 0; ++bb_iter, --num_basic_blocks) {
Shih-wei Liao627d8c42012-08-24 08:31:18 -0700456 // Set insert point to current basic block.
457 irb_.SetInsertPoint(bb_iter);
Logan Chien67645d82012-08-17 09:10:54 +0800458
TDYa12755e5e6c2012-09-11 15:14:42 -0700459 current_bb_ = bb_iter;
TDYa127aa558872012-08-16 05:11:07 -0700460
Logan Chien67645d82012-08-17 09:10:54 +0800461 // Rewrite the basic block
462 RewriteBasicBlock(bb_iter);
463
464 // Update the phi-instructions in the successor basic block
465 llvm::BasicBlock* last_block = irb_.GetInsertBlock();
466 if (last_block != bb_iter) {
467 UpdatePhiInstruction(bb_iter, last_block);
468 }
469 }
TDYa127aa558872012-08-16 05:11:07 -0700470
471 typedef std::map<llvm::PHINode*, llvm::PHINode*> HandlerPHIMap;
472 HandlerPHIMap handler_phi;
473 // Iterate every used landing pad basic block
474 for (size_t i = 0, ei = basic_block_landing_pads_.size(); i != ei; ++i) {
475 llvm::BasicBlock* lbb = basic_block_landing_pads_[i];
476 if (lbb == NULL) {
477 continue;
478 }
479
480 llvm::TerminatorInst* term_inst = lbb->getTerminator();
481 std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> >& rewrite_pair
482 = landing_pad_phi_mapping_[lbb];
483 irb_.SetInsertPoint(lbb->begin());
484
485 // Iterate every succeeding basic block (catch block)
486 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
487 succ_iter != succ_end; ++succ_iter) {
488 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
489
490 // Iterate every phi instructions in the succeeding basic block
491 for (llvm::BasicBlock::iterator
492 inst_iter = succ_basic_block->begin(),
493 inst_end = succ_basic_block->end();
494 inst_iter != inst_end; ++inst_iter) {
495 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
496
497 if (!phi) {
498 break; // Meet non-phi instruction. Done.
499 }
500
501 if (handler_phi[phi] == NULL) {
502 handler_phi[phi] = llvm::PHINode::Create(phi->getType(), 1);
503 }
504
505 // Create new_phi in landing pad
506 llvm::PHINode* new_phi = irb_.CreatePHI(phi->getType(), rewrite_pair.size());
507 // Insert all incoming value into new_phi by rewrite_pair
508 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
509 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
510 llvm::BasicBlock* new_bb = rewrite_pair[j].second;
511 new_phi->addIncoming(phi->getIncomingValueForBlock(old_bb), new_bb);
512 }
513 // Delete all incoming value from phi by rewrite_pair
514 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
515 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
516 int old_bb_idx = phi->getBasicBlockIndex(old_bb);
517 if (old_bb_idx >= 0) {
518 phi->removeIncomingValue(old_bb_idx, false);
519 }
520 }
521 // Insert new_phi into new handler phi
522 handler_phi[phi]->addIncoming(new_phi, lbb);
523 }
524 }
525 }
526
527 // Replace all handler phi
528 // We can't just use the old handler phi, because some exception edges will disappear after we
529 // compute fast-path.
530 for (HandlerPHIMap::iterator it = handler_phi.begin(); it != handler_phi.end(); ++it) {
531 llvm::PHINode* old_phi = it->first;
532 llvm::PHINode* new_phi = it->second;
533 new_phi->insertBefore(old_phi);
534 old_phi->replaceAllUsesWith(new_phi);
535 old_phi->eraseFromParent();
536 }
Logan Chien67645d82012-08-17 09:10:54 +0800537}
538
539void GBCExpanderPass::UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
540 llvm::BasicBlock* new_basic_block) {
541 llvm::TerminatorInst* term_inst = new_basic_block->getTerminator();
542
543 if (!term_inst) {
544 return; // No terminating instruction in new_basic_block. Nothing to do.
545 }
546
547 // Iterate every succeeding basic block
548 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
549 succ_iter != succ_end; ++succ_iter) {
550 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
551
552 // Iterate every phi instructions in the succeeding basic block
553 for (llvm::BasicBlock::iterator
554 inst_iter = succ_basic_block->begin(),
555 inst_end = succ_basic_block->end();
556 inst_iter != inst_end; ++inst_iter) {
557 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
558
559 if (!phi) {
560 break; // Meet non-phi instruction. Done.
561 }
562
563 // Update the incoming block of this phi instruction
564 for (llvm::PHINode::block_iterator
565 ibb_iter = phi->block_begin(), ibb_end = phi->block_end();
566 ibb_iter != ibb_end; ++ibb_iter) {
567 if (*ibb_iter == old_basic_block) {
568 *ibb_iter = new_basic_block;
569 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700570 }
571 }
572 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700573}
574
575llvm::Value* GBCExpanderPass::ExpandToRuntime(runtime_support::RuntimeId rt,
576 llvm::CallInst& inst) {
577 // Some GBC intrinsic can directly replace with IBC runtime. "Directly" means
578 // the arguments passed to the GBC intrinsic are as the same as IBC runtime
579 // function, therefore only called function is needed to change.
580 unsigned num_args = inst.getNumArgOperands();
581
582 if (num_args <= 0) {
583 return irb_.CreateCall(irb_.GetRuntime(rt));
584 } else {
585 std::vector<llvm::Value*> args;
586 for (unsigned i = 0; i < num_args; i++) {
587 args.push_back(inst.getArgOperand(i));
588 }
589
590 return irb_.CreateCall(irb_.GetRuntime(rt), args);
591 }
592}
593
Logan Chien67645d82012-08-17 09:10:54 +0800594void
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700595GBCExpanderPass::EmitStackOverflowCheck(llvm::Instruction* first_non_alloca) {
596 llvm::Function* func = first_non_alloca->getParent()->getParent();
597 llvm::Module* module = func->getParent();
598
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700599 // Call llvm intrinsic function to get frame address.
600 llvm::Function* frameaddress =
601 llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
602
603 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
604 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
605
606 // Cast i8* to int
607 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
608
609 // Get thread.stack_end_
610 llvm::Value* stack_end =
TDYa127920be7c2012-09-10 17:13:22 -0700611 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::StackEndOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700612 irb_.getPtrEquivIntTy(),
613 kTBAARuntimeInfo);
614
615 // Check the frame address < thread.stack_end_ ?
616 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
617
618 llvm::BasicBlock* block_exception =
619 llvm::BasicBlock::Create(context_, "stack_overflow", func);
620
621 llvm::BasicBlock* block_continue =
622 llvm::BasicBlock::Create(context_, "stack_overflow_cont", func);
623
624 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
625
626 // If stack overflow, throw exception.
627 irb_.SetInsertPoint(block_exception);
628 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowStackOverflowException));
629
630 // Unwind.
631 llvm::Type* ret_type = func->getReturnType();
632 if (ret_type->isVoidTy()) {
633 irb_.CreateRetVoid();
634 } else {
635 // The return value is ignored when there's an exception. MethodCompiler
636 // returns zero value under the the corresponding return type in this case.
637 // GBCExpander returns LLVM undef value here for brevity
638 irb_.CreateRet(llvm::UndefValue::get(ret_type));
639 }
640
641 irb_.SetInsertPoint(block_continue);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700642}
643
TDYa127920be7c2012-09-10 17:13:22 -0700644llvm::Value* GBCExpanderPass::EmitLoadDexCacheAddr(art::MemberOffset offset) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700645 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
646
647 return irb_.LoadFromObjectOffset(method_object_addr,
648 offset.Int32Value(),
649 irb_.getJObjectTy(),
650 kTBAAConstJObject);
651}
652
653llvm::Value*
654GBCExpanderPass::EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
655 llvm::Value* static_storage_dex_cache_addr =
Mathieu Chartier66f19252012-09-18 08:57:04 -0700656 EmitLoadDexCacheAddr(art::AbstractMethod::DexCacheInitializedStaticStorageOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700657
658 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
659
660 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
661}
662
663llvm::Value*
664GBCExpanderPass::EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
665 llvm::Value* resolved_type_dex_cache_addr =
Mathieu Chartier66f19252012-09-18 08:57:04 -0700666 EmitLoadDexCacheAddr(art::AbstractMethod::DexCacheResolvedTypesOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700667
668 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
669
670 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
671}
672
673llvm::Value* GBCExpanderPass::
674EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
675 llvm::Value* resolved_method_dex_cache_addr =
Mathieu Chartier66f19252012-09-18 08:57:04 -0700676 EmitLoadDexCacheAddr(art::AbstractMethod::DexCacheResolvedMethodsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700677
678 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
679
680 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
681}
682
683llvm::Value* GBCExpanderPass::
684EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
685 llvm::Value* string_dex_cache_addr =
Mathieu Chartier66f19252012-09-18 08:57:04 -0700686 EmitLoadDexCacheAddr(art::AbstractMethod::DexCacheStringsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700687
688 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
689
690 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
691}
692
693llvm::Value* GBCExpanderPass::EmitLoadMethodObjectAddr() {
694 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
695 return parent_func->arg_begin();
696}
697
698llvm::Value* GBCExpanderPass::EmitLoadArrayLength(llvm::Value* array) {
699 // Load array length
700 return irb_.LoadFromObjectOffset(array,
TDYa127920be7c2012-09-10 17:13:22 -0700701 art::Array::LengthOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700702 irb_.getJIntTy(),
703 kTBAAConstJObject);
704
705}
706
707llvm::Value*
708GBCExpanderPass::EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
709 llvm::Value* callee_method_object_field_addr =
710 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
711
712 return irb_.CreateLoad(callee_method_object_field_addr, kTBAAJRuntime);
713}
714
715llvm::Value* GBCExpanderPass::
716EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, llvm::Value* this_addr) {
717 // Load class object of *this* pointer
718 llvm::Value* class_object_addr =
719 irb_.LoadFromObjectOffset(this_addr,
TDYa127920be7c2012-09-10 17:13:22 -0700720 art::Object::ClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700721 irb_.getJObjectTy(),
722 kTBAAConstJObject);
723
724 // Load vtable address
725 llvm::Value* vtable_addr =
726 irb_.LoadFromObjectOffset(class_object_addr,
TDYa127920be7c2012-09-10 17:13:22 -0700727 art::Class::VTableOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700728 irb_.getJObjectTy(),
729 kTBAAConstJObject);
730
731 // Load callee method object
732 llvm::Value* vtable_idx_value =
733 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
734
735 llvm::Value* method_field_addr =
736 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
737
738 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
739}
740
741// Emit Array GetElementPtr
742llvm::Value* GBCExpanderPass::EmitArrayGEP(llvm::Value* array_addr,
743 llvm::Value* index_value,
744 JType elem_jty) {
745
746 int data_offset;
747 if (elem_jty == kLong || elem_jty == kDouble ||
TDYa127920be7c2012-09-10 17:13:22 -0700748 (elem_jty == kObject && sizeof(uint64_t) == sizeof(art::Object*))) {
749 data_offset = art::Array::DataOffset(sizeof(int64_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700750 } else {
TDYa127920be7c2012-09-10 17:13:22 -0700751 data_offset = art::Array::DataOffset(sizeof(int32_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700752 }
753
754 llvm::Constant* data_offset_value =
755 irb_.getPtrEquivInt(data_offset);
756
757 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
758
759 llvm::Value* array_data_addr =
760 irb_.CreatePtrDisp(array_addr, data_offset_value,
761 elem_type->getPointerTo());
762
763 return irb_.CreateGEP(array_data_addr, index_value);
764}
765
766void GBCExpanderPass::Expand_TestSuspend(llvm::CallInst& call_inst) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700767 irb_.Runtime().EmitTestSuspend();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700768 return;
769}
770
TDYa1279a129452012-07-19 03:10:08 -0700771void GBCExpanderPass::Expand_MarkGCCard(llvm::CallInst& call_inst) {
TDYa1279a129452012-07-19 03:10:08 -0700772 irb_.Runtime().EmitMarkGCCard(call_inst.getArgOperand(0), call_inst.getArgOperand(1));
TDYa1279a129452012-07-19 03:10:08 -0700773 return;
774}
775
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700776llvm::Value*
777GBCExpanderPass::Expand_LoadStringFromDexCache(llvm::Value* string_idx_value) {
778 uint32_t string_idx =
779 llvm::cast<llvm::ConstantInt>(string_idx_value)->getZExtValue();
780
781 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
782
783 return irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
784}
785
786llvm::Value*
787GBCExpanderPass::Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value) {
788 uint32_t type_idx =
789 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
790
791 llvm::Value* type_field_addr =
792 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
793
794 return irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
795}
796
797void GBCExpanderPass::Expand_LockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700798 rtb_.EmitLockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700799 return;
800}
801
802void GBCExpanderPass::Expand_UnlockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700803 rtb_.EmitUnlockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700804 return;
805}
806
807llvm::Value* GBCExpanderPass::Expand_ArrayGet(llvm::Value* array_addr,
808 llvm::Value* index_value,
809 JType elem_jty) {
810 llvm::Value* array_elem_addr =
811 EmitArrayGEP(array_addr, index_value, elem_jty);
812
813 return irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
814}
815
816void GBCExpanderPass::Expand_ArrayPut(llvm::Value* new_value,
817 llvm::Value* array_addr,
818 llvm::Value* index_value,
819 JType elem_jty) {
820 llvm::Value* array_elem_addr =
821 EmitArrayGEP(array_addr, index_value, elem_jty);
822
823 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
824
825 return;
826}
827
828void GBCExpanderPass::Expand_FilledNewArray(llvm::CallInst& call_inst) {
829 // Most of the codes refer to MethodCompiler::EmitInsn_FilledNewArray
830 llvm::Value* array = call_inst.getArgOperand(0);
831
832 uint32_t element_jty =
833 llvm::cast<llvm::ConstantInt>(call_inst.getArgOperand(1))->getZExtValue();
834
835 DCHECK(call_inst.getNumArgOperands() > 2);
836 unsigned num_elements = (call_inst.getNumArgOperands() - 2);
837
838 bool is_elem_int_ty = (static_cast<JType>(element_jty) == kInt);
839
840 uint32_t alignment;
841 llvm::Constant* elem_size;
842 llvm::PointerType* field_type;
843
844 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
845 // as the element, thus we are only checking 2 cases: primitive int and
846 // non-primitive type.
847 if (is_elem_int_ty) {
848 alignment = sizeof(int32_t);
849 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
850 field_type = irb_.getJIntTy()->getPointerTo();
851 } else {
852 alignment = irb_.getSizeOfPtrEquivInt();
853 elem_size = irb_.getSizeOfPtrEquivIntValue();
854 field_type = irb_.getJObjectTy()->getPointerTo();
855 }
856
857 llvm::Value* data_field_offset =
TDYa127920be7c2012-09-10 17:13:22 -0700858 irb_.getPtrEquivInt(art::Array::DataOffset(alignment).Int32Value());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700859
860 llvm::Value* data_field_addr =
861 irb_.CreatePtrDisp(array, data_field_offset, field_type);
862
863 for (unsigned i = 0; i < num_elements; ++i) {
864 // Values to fill the array begin at the 3rd argument
865 llvm::Value* reg_value = call_inst.getArgOperand(2 + i);
866
867 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
868
869 data_field_addr =
870 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
871 }
872
873 return;
874}
875
876llvm::Value* GBCExpanderPass::Expand_IGetFast(llvm::Value* field_offset_value,
877 llvm::Value* /*is_volatile_value*/,
878 llvm::Value* object_addr,
879 JType field_jty) {
880 int field_offset =
881 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
882
883 DCHECK_GE(field_offset, 0);
884
885 llvm::PointerType* field_type =
886 irb_.getJType(field_jty, kField)->getPointerTo();
887
888 field_offset_value = irb_.getPtrEquivInt(field_offset);
889
890 llvm::Value* field_addr =
891 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
892
893 // TODO: Check is_volatile. We need to generate atomic load instruction
894 // when is_volatile is true.
895 return irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
896}
897
898void GBCExpanderPass::Expand_IPutFast(llvm::Value* field_offset_value,
899 llvm::Value* /* is_volatile_value */,
900 llvm::Value* object_addr,
901 llvm::Value* new_value,
902 JType field_jty) {
903 int field_offset =
904 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
905
906 DCHECK_GE(field_offset, 0);
907
908 llvm::PointerType* field_type =
909 irb_.getJType(field_jty, kField)->getPointerTo();
910
911 field_offset_value = irb_.getPtrEquivInt(field_offset);
912
913 llvm::Value* field_addr =
914 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
915
916 // TODO: Check is_volatile. We need to generate atomic store instruction
917 // when is_volatile is true.
918 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
919
920 return;
921}
922
923llvm::Value* GBCExpanderPass::Expand_SGetFast(llvm::Value* static_storage_addr,
924 llvm::Value* field_offset_value,
925 llvm::Value* /*is_volatile_value*/,
926 JType field_jty) {
927 int field_offset =
928 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
929
930 DCHECK_GE(field_offset, 0);
931
932 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
933
934 llvm::Value* static_field_addr =
935 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
936 irb_.getJType(field_jty, kField)->getPointerTo());
937
938 // TODO: Check is_volatile. We need to generate atomic store instruction
939 // when is_volatile is true.
940 return irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
941}
942
943void GBCExpanderPass::Expand_SPutFast(llvm::Value* static_storage_addr,
944 llvm::Value* field_offset_value,
945 llvm::Value* /* is_volatile_value */,
946 llvm::Value* new_value,
947 JType field_jty) {
948 int field_offset =
949 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
950
951 DCHECK_GE(field_offset, 0);
952
953 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
954
955 llvm::Value* static_field_addr =
956 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
957 irb_.getJType(field_jty, kField)->getPointerTo());
958
959 // TODO: Check is_volatile. We need to generate atomic store instruction
960 // when is_volatile is true.
961 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
962
963 return;
964}
965
966llvm::Value*
967GBCExpanderPass::Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr) {
968 return irb_.LoadFromObjectOffset(method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700969 art::AbstractMethod::DeclaringClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700970 irb_.getJObjectTy(),
971 kTBAAConstJObject);
972}
973
974llvm::Value*
975GBCExpanderPass::Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value) {
976 uint32_t type_idx =
977 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
978
979 llvm::Value* storage_field_addr =
980 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
981
982 return irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
983}
984
985llvm::Value*
986GBCExpanderPass::Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value) {
987 uint32_t callee_method_idx =
988 llvm::cast<llvm::ConstantInt>(callee_method_idx_value)->getZExtValue();
989
990 return EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
991}
992
993llvm::Value* GBCExpanderPass::Expand_GetVirtualCalleeMethodObjAddrFast(
994 llvm::Value* vtable_idx_value,
995 llvm::Value* this_addr) {
996 int vtable_idx =
997 llvm::cast<llvm::ConstantInt>(vtable_idx_value)->getSExtValue();
998
999 return EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
1000}
1001
1002llvm::Value* GBCExpanderPass::Expand_Invoke(llvm::CallInst& call_inst) {
1003 // Most of the codes refer to MethodCompiler::EmitInsn_Invoke
1004 llvm::Value* callee_method_object_addr = call_inst.getArgOperand(0);
1005 unsigned num_args = call_inst.getNumArgOperands();
1006 llvm::Type* ret_type = call_inst.getType();
1007
1008 // Determine the function type of the callee method
1009 std::vector<llvm::Type*> args_type;
1010 std::vector<llvm::Value*> args;
1011 for (unsigned i = 0; i < num_args; i++) {
1012 args.push_back(call_inst.getArgOperand(i));
1013 args_type.push_back(args[i]->getType());
1014 }
1015
1016 llvm::FunctionType* callee_method_type =
1017 llvm::FunctionType::get(ret_type, args_type, false);
1018
1019 llvm::Value* code_addr =
1020 irb_.LoadFromObjectOffset(callee_method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001021 art::AbstractMethod::GetCodeOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001022 callee_method_type->getPointerTo(),
1023 kTBAAJRuntime);
1024
1025 // Invoke callee
1026 llvm::Value* retval = irb_.CreateCall(code_addr, args);
1027
1028 return retval;
1029}
1030
TDYa1274ec8ccd2012-08-11 07:04:57 -07001031llvm::Value* GBCExpanderPass::Expand_DivRem(llvm::CallInst& call_inst,
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001032 bool is_div, JType op_jty) {
TDYa1274ec8ccd2012-08-11 07:04:57 -07001033 llvm::Value* dividend = call_inst.getArgOperand(0);
1034 llvm::Value* divisor = call_inst.getArgOperand(1);
buzbeec531cef2012-10-18 07:09:20 -07001035#if defined(ART_USE_PORTABLE_COMPILER)
TDYa1274ec8ccd2012-08-11 07:04:57 -07001036 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1037 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
1038#endif
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001039 // Most of the codes refer to MethodCompiler::EmitIntDivRemResultComputation
1040
1041 // Check the special case: MININT / -1 = MININT
1042 // That case will cause overflow, which is undefined behavior in llvm.
1043 // So we check the divisor is -1 or not, if the divisor is -1, we do
1044 // the special path to avoid undefined behavior.
1045 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
1046 llvm::Value* zero = irb_.getJZero(op_jty);
1047 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
1048
TDYa1275e869b62012-07-25 00:45:39 -07001049 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001050 llvm::BasicBlock* eq_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1051 llvm::BasicBlock* ne_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1052 llvm::BasicBlock* neg_one_cont =
1053 llvm::BasicBlock::Create(context_, "", parent);
1054
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001055 llvm::Value* is_equal_neg_one = irb_.CreateICmpEQ(divisor, neg_one);
1056 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
1057
1058 // If divisor == -1
1059 irb_.SetInsertPoint(eq_neg_one);
1060 llvm::Value* eq_result;
1061 if (is_div) {
1062 // We can just change from "dividend div -1" to "neg dividend". The sub
1063 // don't care the sign/unsigned because of two's complement representation.
1064 // And the behavior is what we want:
1065 // -(2^n) (2^n)-1
1066 // MININT < k <= MAXINT -> mul k -1 = -k
1067 // MININT == k -> mul k -1 = k
1068 //
1069 // LLVM use sub to represent 'neg'
1070 eq_result = irb_.CreateSub(zero, dividend);
1071 } else {
1072 // Everything modulo -1 will be 0.
1073 eq_result = zero;
1074 }
1075 irb_.CreateBr(neg_one_cont);
1076
1077 // If divisor != -1, just do the division.
1078 irb_.SetInsertPoint(ne_neg_one);
1079 llvm::Value* ne_result;
1080 if (is_div) {
1081 ne_result = irb_.CreateSDiv(dividend, divisor);
1082 } else {
1083 ne_result = irb_.CreateSRem(dividend, divisor);
1084 }
1085 irb_.CreateBr(neg_one_cont);
1086
1087 irb_.SetInsertPoint(neg_one_cont);
1088 llvm::PHINode* result = irb_.CreatePHI(op_type, 2);
1089 result->addIncoming(eq_result, eq_neg_one);
1090 result->addIncoming(ne_result, ne_neg_one);
1091
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001092 return result;
1093}
1094
TDYa1278e950c12012-11-02 09:58:19 -07001095void GBCExpanderPass::Expand_AllocaShadowFrame(llvm::Value* num_entry_value,
1096 llvm::Value* num_vregs_value) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001097 // Most of the codes refer to MethodCompiler::EmitPrologueAllocShadowFrame and
1098 // MethodCompiler::EmitPushShadowFrame
TDYa1278e950c12012-11-02 09:58:19 -07001099 uint16_t num_shadow_frame_refs =
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001100 llvm::cast<llvm::ConstantInt>(num_entry_value)->getZExtValue();
TDYa1278e950c12012-11-02 09:58:19 -07001101 uint16_t num_vregs =
1102 llvm::cast<llvm::ConstantInt>(num_vregs_value)->getZExtValue();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001103
1104 llvm::StructType* shadow_frame_type =
TDYa1278e950c12012-11-02 09:58:19 -07001105 irb_.getShadowFrameTy(num_shadow_frame_refs, num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001106
1107 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
1108
1109 // Alloca a pointer to old shadow frame
1110 old_shadow_frame_ =
1111 irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
1112
1113 // Zero-initialization of the shadow frame table
1114 llvm::Value* shadow_frame_table =
1115 irb_.CreateConstGEP2_32(shadow_frame_, 0, 1);
1116 llvm::Type* table_type = shadow_frame_type->getElementType(1);
1117
1118 llvm::ConstantAggregateZero* zero_initializer =
1119 llvm::ConstantAggregateZero::get(table_type);
1120
1121 irb_.CreateStore(zero_initializer, shadow_frame_table, kTBAAShadowFrame);
1122
1123 // Push the shadow frame
1124 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1125
1126 // Push the shadow frame
1127 llvm::Value* shadow_frame_upcast =
1128 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
1129
1130 llvm::Value* result = rtb_.EmitPushShadowFrame(shadow_frame_upcast,
1131 method_object_addr,
TDYa1278e950c12012-11-02 09:58:19 -07001132 num_shadow_frame_refs,
1133 num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001134
1135 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
1136
1137 return;
1138}
1139
TDYa1278e950c12012-11-02 09:58:19 -07001140// TODO: We will remove ShadowFrameEntry later, so I just copy/paste from ShadowFrameEntry.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001141void GBCExpanderPass::Expand_SetShadowFrameEntry(llvm::Value* obj,
1142 llvm::Value* entry_idx) {
1143 DCHECK(shadow_frame_ != NULL);
1144
1145 llvm::Value* gep_index[] = {
1146 irb_.getInt32(0), // No pointer displacement
1147 irb_.getInt32(1), // SIRT
1148 entry_idx // Pointer field
1149 };
1150
1151 llvm::Value* entry_addr = irb_.CreateGEP(shadow_frame_, gep_index);
buzbeec531cef2012-10-18 07:09:20 -07001152#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127347166a2012-08-23 12:23:44 -07001153 if (obj->getType() != irb_.getJObjectTy()) {
1154 obj = irb_.getJNull();
1155 }
1156#endif
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001157 irb_.CreateStore(obj, entry_addr, kTBAAShadowFrame);
1158 return;
1159}
1160
TDYa1278e950c12012-11-02 09:58:19 -07001161void GBCExpanderPass::Expand_SetVReg(llvm::Value* entry_idx,
1162 llvm::Value* value) {
1163 DCHECK(shadow_frame_ != NULL);
1164
1165 llvm::Value* gep_index[] = {
1166 irb_.getInt32(0), // No pointer displacement
1167 irb_.getInt32(2), // VRegs
1168 entry_idx // Pointer field
1169 };
1170
1171 llvm::Value* vreg_addr = irb_.CreateGEP(shadow_frame_, gep_index);
1172
1173 irb_.CreateStore(value,
1174 irb_.CreateBitCast(vreg_addr, value->getType()->getPointerTo()),
1175 kTBAAShadowFrame);
1176 return;
1177}
1178
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001179void GBCExpanderPass::Expand_PopShadowFrame() {
buzbeec531cef2012-10-18 07:09:20 -07001180#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001181 if (old_shadow_frame_ == NULL) {
1182 return;
1183 }
1184#endif
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001185 rtb_.EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
1186 return;
1187}
1188
1189void GBCExpanderPass::Expand_UpdateDexPC(llvm::Value* dex_pc_value) {
1190 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07001191 art::ShadowFrame::DexPCOffset(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001192 dex_pc_value,
1193 kTBAAShadowFrame);
1194 return;
1195}
1196
Logan Chien67645d82012-08-17 09:10:54 +08001197void GBCExpanderPass::InsertStackOverflowCheck(llvm::Function& func) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001198 // DexLang generates all alloca instruction in the first basic block of the
1199 // FUNC and also there's no any alloca instructions after the first non-alloca
1200 // instruction
1201
Logan Chien67645d82012-08-17 09:10:54 +08001202 llvm::BasicBlock* first_basic_block = &func.front();
1203
1204 // Look for first non-alloca instruction
1205 llvm::BasicBlock::iterator first_non_alloca = first_basic_block->begin();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001206 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1207 ++first_non_alloca;
1208 }
1209
Logan Chien67645d82012-08-17 09:10:54 +08001210 irb_.SetInsertPoint(first_non_alloca);
1211
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001212 // Insert stack overflow check codes before first_non_alloca (i.e., after all
1213 // alloca instructions)
Logan Chien67645d82012-08-17 09:10:54 +08001214 EmitStackOverflowCheck(&*first_non_alloca);
1215
buzbeec531cef2012-10-18 07:09:20 -07001216#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127890ea892012-08-22 10:49:42 -07001217 irb_.Runtime().EmitTestSuspend();
1218#endif
1219
Logan Chien67645d82012-08-17 09:10:54 +08001220 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
1221 if (next_basic_block != first_basic_block) {
1222 // Splice the rest of the instruction to the continuing basic block
1223 next_basic_block->getInstList().splice(
1224 irb_.GetInsertPoint(), first_basic_block->getInstList(),
1225 first_non_alloca, first_basic_block->end());
1226
1227 // Rewrite the basic block
1228 RewriteBasicBlock(next_basic_block);
1229
1230 // Update the phi-instructions in the successor basic block
1231 UpdatePhiInstruction(first_basic_block, irb_.GetInsertBlock());
1232 }
1233
1234 // We have changed the basic block
1235 changed_ = true;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001236}
1237
TDYa1275e869b62012-07-25 00:45:39 -07001238// ==== High-level intrinsic expander ==========================================
1239
TDYa127a1b21852012-07-23 03:20:39 -07001240llvm::Value* GBCExpanderPass::Expand_FPCompare(llvm::Value* src1_value,
1241 llvm::Value* src2_value,
1242 bool gt_bias) {
1243 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1244 llvm::Value* cmp_lt;
1245
1246 if (gt_bias) {
1247 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1248 } else {
1249 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1250 }
1251
1252 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1253}
1254
1255llvm::Value* GBCExpanderPass::Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value) {
1256 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1257 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1258
1259 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1260}
1261
1262llvm::Value* GBCExpanderPass::EmitCompareResultSelection(llvm::Value* cmp_eq,
1263 llvm::Value* cmp_lt) {
1264
1265 llvm::Constant* zero = irb_.getJInt(0);
1266 llvm::Constant* pos1 = irb_.getJInt(1);
1267 llvm::Constant* neg1 = irb_.getJInt(-1);
1268
1269 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1270 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1271
1272 return result_eq;
1273}
1274
Logan Chien75e4b602012-07-23 14:24:12 -07001275llvm::Value* GBCExpanderPass::Expand_IntegerShift(llvm::Value* src1_value,
1276 llvm::Value* src2_value,
1277 IntegerShiftKind kind,
1278 JType op_jty) {
1279 DCHECK(op_jty == kInt || op_jty == kLong);
1280
1281 // Mask and zero-extend RHS properly
1282 if (op_jty == kInt) {
1283 src2_value = irb_.CreateAnd(src2_value, 0x1f);
1284 } else {
1285 llvm::Value* masked_src2_value = irb_.CreateAnd(src2_value, 0x3f);
1286 src2_value = irb_.CreateZExt(masked_src2_value, irb_.getJLongTy());
1287 }
1288
1289 // Create integer shift llvm instruction
1290 switch (kind) {
1291 case kIntegerSHL:
1292 return irb_.CreateShl(src1_value, src2_value);
1293
1294 case kIntegerSHR:
1295 return irb_.CreateAShr(src1_value, src2_value);
1296
1297 case kIntegerUSHR:
1298 return irb_.CreateLShr(src1_value, src2_value);
1299
1300 default:
1301 LOG(FATAL) << "Unknown integer shift kind: " << kind;
1302 return NULL;
1303 }
1304}
1305
TDYa1275a26d442012-07-26 18:58:38 -07001306llvm::Value* GBCExpanderPass::Expand_HLArrayGet(llvm::CallInst& call_inst,
1307 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001308 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1309 llvm::Value* array_addr = call_inst.getArgOperand(1);
1310 llvm::Value* index_value = call_inst.getArgOperand(2);
TDYa127920be7c2012-09-10 17:13:22 -07001311 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001312
TDYa127920be7c2012-09-10 17:13:22 -07001313 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1314 EmitGuard_NullPointerException(dex_pc, array_addr);
1315 }
1316 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
1317 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value);
1318 }
TDYa1275a26d442012-07-26 18:58:38 -07001319
1320 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1321
1322 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
1323
1324 switch (elem_jty) {
1325 case kVoid:
1326 break;
1327
1328 case kBoolean:
1329 case kChar:
1330 array_elem_value = irb_.CreateZExt(array_elem_value, irb_.getJType(elem_jty, kReg));
1331 break;
1332
1333 case kByte:
1334 case kShort:
1335 array_elem_value = irb_.CreateSExt(array_elem_value, irb_.getJType(elem_jty, kReg));
1336 break;
1337
1338 case kInt:
1339 case kLong:
1340 case kFloat:
1341 case kDouble:
1342 case kObject:
1343 break;
1344
1345 default:
1346 LOG(FATAL) << "Unknown java type: " << elem_jty;
1347 }
1348
1349 return array_elem_value;
1350}
1351
1352
1353void GBCExpanderPass::Expand_HLArrayPut(llvm::CallInst& call_inst,
1354 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001355 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1356 llvm::Value* new_value = call_inst.getArgOperand(1);
1357 llvm::Value* array_addr = call_inst.getArgOperand(2);
1358 llvm::Value* index_value = call_inst.getArgOperand(3);
TDYa127920be7c2012-09-10 17:13:22 -07001359 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001360
TDYa127920be7c2012-09-10 17:13:22 -07001361 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1362 EmitGuard_NullPointerException(dex_pc, array_addr);
1363 }
1364 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
1365 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value);
1366 }
TDYa1275a26d442012-07-26 18:58:38 -07001367
1368 switch (elem_jty) {
1369 case kVoid:
1370 break;
1371
1372 case kBoolean:
1373 case kChar:
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001374 case kByte:
1375 case kShort:
TDYa1275a26d442012-07-26 18:58:38 -07001376 new_value = irb_.CreateTrunc(new_value, irb_.getJType(elem_jty, kArray));
1377 break;
1378
1379 case kInt:
1380 case kLong:
1381 case kFloat:
1382 case kDouble:
1383 case kObject:
1384 break;
1385
1386 default:
1387 LOG(FATAL) << "Unknown java type: " << elem_jty;
1388 }
1389
1390 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1391
1392 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
1393 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::CheckPutArrayElement);
1394
1395 irb_.CreateCall2(runtime_func, new_value, array_addr);
1396
1397 EmitGuard_ExceptionLandingPad(dex_pc);
1398
1399 EmitMarkGCCard(new_value, array_addr);
1400 }
1401
1402 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1403
1404 return;
1405}
1406
TDYa1275e869b62012-07-25 00:45:39 -07001407llvm::Value* GBCExpanderPass::Expand_HLIGet(llvm::CallInst& call_inst,
1408 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001409 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1410 llvm::Value* object_addr = call_inst.getArgOperand(1);
1411 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(2));
TDYa127920be7c2012-09-10 17:13:22 -07001412 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001413
TDYa127920be7c2012-09-10 17:13:22 -07001414 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1415 EmitGuard_NullPointerException(dex_pc, object_addr);
1416 }
TDYa1275e869b62012-07-25 00:45:39 -07001417
1418 llvm::Value* field_value;
1419
1420 int field_offset;
1421 bool is_volatile;
1422 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
1423 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
1424
1425 if (!is_fast_path) {
1426 llvm::Function* runtime_func;
1427
1428 if (field_jty == kObject) {
1429 runtime_func = irb_.GetRuntime(runtime_support::GetObjectInstance);
1430 } else if (field_jty == kLong || field_jty == kDouble) {
1431 runtime_func = irb_.GetRuntime(runtime_support::Get64Instance);
1432 } else {
1433 runtime_func = irb_.GetRuntime(runtime_support::Get32Instance);
1434 }
1435
1436 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
1437
1438 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1439
1440 EmitUpdateDexPC(dex_pc);
1441
1442 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
1443 method_object_addr, object_addr);
1444
1445 EmitGuard_ExceptionLandingPad(dex_pc);
1446
1447 } else {
1448 DCHECK_GE(field_offset, 0);
1449
1450 llvm::PointerType* field_type =
1451 irb_.getJType(field_jty, kField)->getPointerTo();
1452
1453 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
1454
1455 llvm::Value* field_addr =
1456 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1457
1458 // TODO: Check is_volatile. We need to generate atomic load instruction
1459 // when is_volatile is true.
1460 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
1461 }
1462
1463 if (field_jty == kFloat || field_jty == kDouble) {
TDYa1275a26d442012-07-26 18:58:38 -07001464 field_value = irb_.CreateBitCast(field_value, irb_.getJType(field_jty, kAccurate));
TDYa1275e869b62012-07-25 00:45:39 -07001465 }
1466
1467 return field_value;
1468}
1469
1470void GBCExpanderPass::Expand_HLIPut(llvm::CallInst& call_inst,
1471 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001472 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001473 llvm::Value* new_value = call_inst.getArgOperand(1);
1474 llvm::Value* object_addr = call_inst.getArgOperand(2);
TDYa1275e869b62012-07-25 00:45:39 -07001475 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(3));
TDYa127920be7c2012-09-10 17:13:22 -07001476 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001477
1478 if (field_jty == kFloat || field_jty == kDouble) {
TDYa1275a26d442012-07-26 18:58:38 -07001479 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty, kField));
TDYa1275e869b62012-07-25 00:45:39 -07001480 }
1481
TDYa127920be7c2012-09-10 17:13:22 -07001482 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1483 EmitGuard_NullPointerException(dex_pc, object_addr);
1484 }
TDYa1275e869b62012-07-25 00:45:39 -07001485
1486 int field_offset;
1487 bool is_volatile;
1488 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
1489 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
1490
1491 if (!is_fast_path) {
1492 llvm::Function* runtime_func;
1493
1494 if (field_jty == kObject) {
1495 runtime_func = irb_.GetRuntime(runtime_support::SetObjectInstance);
1496 } else if (field_jty == kLong || field_jty == kDouble) {
1497 runtime_func = irb_.GetRuntime(runtime_support::Set64Instance);
1498 } else {
1499 runtime_func = irb_.GetRuntime(runtime_support::Set32Instance);
1500 }
1501
1502 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1503
1504 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1505
1506 EmitUpdateDexPC(dex_pc);
1507
1508 irb_.CreateCall4(runtime_func, field_idx_value,
1509 method_object_addr, object_addr, new_value);
1510
1511 EmitGuard_ExceptionLandingPad(dex_pc);
1512
1513 } else {
1514 DCHECK_GE(field_offset, 0);
1515
1516 llvm::PointerType* field_type =
1517 irb_.getJType(field_jty, kField)->getPointerTo();
1518
1519 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
1520
1521 llvm::Value* field_addr =
1522 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1523
1524 // TODO: Check is_volatile. We need to generate atomic store instruction
1525 // when is_volatile is true.
1526 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1527
1528 if (field_jty == kObject) { // If put an object, mark the GC card table.
1529 EmitMarkGCCard(new_value, object_addr);
1530 }
1531 }
1532
1533 return;
1534}
1535
TDYa127f71bf5a2012-07-29 20:09:52 -07001536llvm::Value* GBCExpanderPass::EmitLoadConstantClass(uint32_t dex_pc,
1537 uint32_t type_idx) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001538 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, *dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001539 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1540
1541 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1542
1543 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1544
1545 llvm::Function* runtime_func =
1546 irb_.GetRuntime(runtime_support::InitializeTypeAndVerifyAccess);
1547
1548 EmitUpdateDexPC(dex_pc);
1549
1550 llvm::Value* type_object_addr =
1551 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1552
1553 EmitGuard_ExceptionLandingPad(dex_pc);
1554
1555 return type_object_addr;
1556
1557 } else {
1558 // Try to load the class (type) object from the test cache.
1559 llvm::Value* type_field_addr =
1560 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1561
1562 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
1563
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001564 if (compiler_->CanAssumeTypeIsPresentInDexCache(*dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001565 return type_object_addr;
1566 }
1567
1568 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1569
1570 // Test whether class (type) object is in the dex cache or not
1571 llvm::Value* equal_null =
1572 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1573
1574 llvm::BasicBlock* block_cont =
1575 CreateBasicBlockWithDexPC(dex_pc, "cont");
1576
1577 llvm::BasicBlock* block_load_class =
1578 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1579
1580 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
1581
1582 // Failback routine to load the class object
1583 irb_.SetInsertPoint(block_load_class);
1584
1585 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeType);
1586
1587 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1588
1589 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1590
1591 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1592
1593 EmitUpdateDexPC(dex_pc);
1594
1595 llvm::Value* loaded_type_object_addr =
1596 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1597
1598 EmitGuard_ExceptionLandingPad(dex_pc);
1599
1600 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1601
1602 irb_.CreateBr(block_cont);
1603
1604 // Now the class object must be loaded
1605 irb_.SetInsertPoint(block_cont);
1606
1607 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1608
1609 phi->addIncoming(type_object_addr, block_original);
1610 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1611
1612 return phi;
1613 }
1614}
1615
TDYa1275a26d442012-07-26 18:58:38 -07001616llvm::Value* GBCExpanderPass::EmitLoadStaticStorage(uint32_t dex_pc,
1617 uint32_t type_idx) {
1618 llvm::BasicBlock* block_load_static =
1619 CreateBasicBlockWithDexPC(dex_pc, "load_static");
1620
1621 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1622
1623 // Load static storage from dex cache
1624 llvm::Value* storage_field_addr =
1625 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1626
1627 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
1628
1629 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1630
1631 // Test: Is the static storage of this class initialized?
1632 llvm::Value* equal_null =
1633 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
1634
1635 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
1636
1637 // Failback routine to load the class object
1638 irb_.SetInsertPoint(block_load_static);
1639
1640 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeStaticStorage);
1641
1642 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1643
1644 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1645
1646 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1647
1648 EmitUpdateDexPC(dex_pc);
1649
1650 llvm::Value* loaded_storage_object_addr =
1651 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1652
1653 EmitGuard_ExceptionLandingPad(dex_pc);
1654
1655 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
1656
1657 irb_.CreateBr(block_cont);
1658
1659 // Now the class object must be loaded
1660 irb_.SetInsertPoint(block_cont);
1661
1662 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1663
1664 phi->addIncoming(storage_object_addr, block_original);
1665 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
1666
1667 return phi;
1668}
1669
1670llvm::Value* GBCExpanderPass::Expand_HLSget(llvm::CallInst& call_inst,
1671 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001672 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1673 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1674
1675 int field_offset;
1676 int ssb_index;
1677 bool is_referrers_class;
1678 bool is_volatile;
1679
1680 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
1681 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1682 is_referrers_class, is_volatile, false);
1683
1684 llvm::Value* static_field_value;
1685
1686 if (!is_fast_path) {
1687 llvm::Function* runtime_func;
1688
1689 if (field_jty == kObject) {
1690 runtime_func = irb_.GetRuntime(runtime_support::GetObjectStatic);
1691 } else if (field_jty == kLong || field_jty == kDouble) {
1692 runtime_func = irb_.GetRuntime(runtime_support::Get64Static);
1693 } else {
1694 runtime_func = irb_.GetRuntime(runtime_support::Get32Static);
1695 }
1696
1697 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1698
1699 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1700
1701 EmitUpdateDexPC(dex_pc);
1702
1703 static_field_value =
1704 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
1705
1706 EmitGuard_ExceptionLandingPad(dex_pc);
1707
1708 } else {
1709 DCHECK_GE(field_offset, 0);
1710
1711 llvm::Value* static_storage_addr = NULL;
1712
1713 if (is_referrers_class) {
1714 // Fast path, static storage base is this method's class
1715 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1716
1717 static_storage_addr =
1718 irb_.LoadFromObjectOffset(method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001719 art::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001720 irb_.getJObjectTy(),
1721 kTBAAConstJObject);
1722 } else {
1723 // Medium path, static storage base in a different class which
1724 // requires checks that the other class is initialized
1725 DCHECK_GE(ssb_index, 0);
1726 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1727 }
1728
1729 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1730
1731 llvm::Value* static_field_addr =
1732 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1733 irb_.getJType(field_jty, kField)->getPointerTo());
1734
1735 // TODO: Check is_volatile. We need to generate atomic load instruction
1736 // when is_volatile is true.
1737 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
1738 }
1739
1740 if (field_jty == kFloat || field_jty == kDouble) {
1741 static_field_value =
1742 irb_.CreateBitCast(static_field_value, irb_.getJType(field_jty, kAccurate));
1743 }
1744
1745 return static_field_value;
1746}
1747
1748void GBCExpanderPass::Expand_HLSput(llvm::CallInst& call_inst,
1749 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001750 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1751 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1752 llvm::Value* new_value = call_inst.getArgOperand(1);
1753
1754 if (field_jty == kFloat || field_jty == kDouble) {
1755 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty, kField));
1756 }
1757
1758 int field_offset;
1759 int ssb_index;
1760 bool is_referrers_class;
1761 bool is_volatile;
1762
1763 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
1764 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1765 is_referrers_class, is_volatile, true);
1766
1767 if (!is_fast_path) {
1768 llvm::Function* runtime_func;
1769
1770 if (field_jty == kObject) {
1771 runtime_func = irb_.GetRuntime(runtime_support::SetObjectStatic);
1772 } else if (field_jty == kLong || field_jty == kDouble) {
1773 runtime_func = irb_.GetRuntime(runtime_support::Set64Static);
1774 } else {
1775 runtime_func = irb_.GetRuntime(runtime_support::Set32Static);
1776 }
1777
1778 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1779
1780 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1781
1782 EmitUpdateDexPC(dex_pc);
1783
1784 irb_.CreateCall3(runtime_func, field_idx_value,
1785 method_object_addr, new_value);
1786
1787 EmitGuard_ExceptionLandingPad(dex_pc);
1788
1789 } else {
1790 DCHECK_GE(field_offset, 0);
1791
1792 llvm::Value* static_storage_addr = NULL;
1793
1794 if (is_referrers_class) {
1795 // Fast path, static storage base is this method's class
1796 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1797
1798 static_storage_addr =
1799 irb_.LoadFromObjectOffset(method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001800 art::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001801 irb_.getJObjectTy(),
1802 kTBAAConstJObject);
1803 } else {
1804 // Medium path, static storage base in a different class which
1805 // requires checks that the other class is initialized
1806 DCHECK_GE(ssb_index, 0);
1807 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1808 }
1809
1810 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1811
1812 llvm::Value* static_field_addr =
1813 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1814 irb_.getJType(field_jty, kField)->getPointerTo());
1815
1816 // TODO: Check is_volatile. We need to generate atomic store instruction
1817 // when is_volatile is true.
1818 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1819
1820 if (field_jty == kObject) { // If put an object, mark the GC card table.
1821 EmitMarkGCCard(new_value, static_storage_addr);
1822 }
1823 }
1824
1825 return;
1826}
1827
TDYa127f71bf5a2012-07-29 20:09:52 -07001828llvm::Value* GBCExpanderPass::Expand_ConstString(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001829 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1830 uint32_t string_idx = LV2UInt(call_inst.getArgOperand(0));
1831
1832 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1833
1834 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
1835
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001836 if (!compiler_->CanAssumeStringIsPresentInDexCache(*dex_file_, string_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001837 llvm::BasicBlock* block_str_exist =
1838 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1839
1840 llvm::BasicBlock* block_str_resolve =
1841 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1842
1843 llvm::BasicBlock* block_cont =
1844 CreateBasicBlockWithDexPC(dex_pc, "str_cont");
1845
1846 // Test: Is the string resolved and in the dex cache?
1847 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1848
1849 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
1850
1851 // String is resolved, go to next basic block.
1852 irb_.SetInsertPoint(block_str_exist);
1853 irb_.CreateBr(block_cont);
1854
1855 // String is not resolved yet, resolve it now.
1856 irb_.SetInsertPoint(block_str_resolve);
1857
1858 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::ResolveString);
1859
1860 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1861
1862 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1863
1864 EmitUpdateDexPC(dex_pc);
1865
1866 llvm::Value* result = irb_.CreateCall2(runtime_func, method_object_addr,
1867 string_idx_value);
1868
1869 EmitGuard_ExceptionLandingPad(dex_pc);
1870
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001871 irb_.CreateBr(block_cont);
1872
1873
TDYa127f71bf5a2012-07-29 20:09:52 -07001874 llvm::BasicBlock* block_pre_cont = irb_.GetInsertBlock();
1875
1876 irb_.SetInsertPoint(block_cont);
1877
1878 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1879
1880 phi->addIncoming(string_addr, block_str_exist);
1881 phi->addIncoming(result, block_pre_cont);
1882
1883 string_addr = phi;
1884 }
1885
1886 return string_addr;
1887}
1888
1889llvm::Value* GBCExpanderPass::Expand_ConstClass(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001890 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1891 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1892
1893 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
1894
1895 return type_object_addr;
1896}
1897
1898void GBCExpanderPass::Expand_MonitorEnter(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001899 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1900 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07001901 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07001902
TDYa127920be7c2012-09-10 17:13:22 -07001903 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1904 EmitGuard_NullPointerException(dex_pc, object_addr);
1905 }
TDYa127f71bf5a2012-07-29 20:09:52 -07001906
1907 irb_.Runtime().EmitLockObject(object_addr);
1908
1909 return;
1910}
1911
1912void GBCExpanderPass::Expand_MonitorExit(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001913 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1914 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07001915 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07001916
TDYa127920be7c2012-09-10 17:13:22 -07001917 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1918 EmitGuard_NullPointerException(dex_pc, object_addr);
1919 }
TDYa127f71bf5a2012-07-29 20:09:52 -07001920
1921 EmitUpdateDexPC(dex_pc);
1922
1923 irb_.Runtime().EmitUnlockObject(object_addr);
1924
1925 EmitGuard_ExceptionLandingPad(dex_pc);
1926
1927 return;
1928}
1929
1930void GBCExpanderPass::Expand_HLCheckCast(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001931 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1932 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1933 llvm::Value* object_addr = call_inst.getArgOperand(1);
1934
1935 llvm::BasicBlock* block_test_class =
1936 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1937
1938 llvm::BasicBlock* block_test_sub_class =
1939 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1940
1941 llvm::BasicBlock* block_cont =
1942 CreateBasicBlockWithDexPC(dex_pc, "checkcast_cont");
1943
1944 // Test: Is the reference equal to null? Act as no-op when it is null.
1945 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1946
1947 irb_.CreateCondBr(equal_null,
1948 block_cont,
1949 block_test_class);
1950
1951 // Test: Is the object instantiated from the given class?
1952 irb_.SetInsertPoint(block_test_class);
1953 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
TDYa127920be7c2012-09-10 17:13:22 -07001954 DCHECK_EQ(art::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07001955
1956 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1957
1958 llvm::Value* object_type_field_addr =
1959 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1960
1961 llvm::Value* object_type_object_addr =
1962 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
1963
1964 llvm::Value* equal_class =
1965 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1966
1967 irb_.CreateCondBr(equal_class,
1968 block_cont,
1969 block_test_sub_class);
1970
1971 // Test: Is the object instantiated from the subclass of the given class?
1972 irb_.SetInsertPoint(block_test_sub_class);
1973
1974 EmitUpdateDexPC(dex_pc);
1975
1976 irb_.CreateCall2(irb_.GetRuntime(runtime_support::CheckCast),
1977 type_object_addr, object_type_object_addr);
1978
1979 EmitGuard_ExceptionLandingPad(dex_pc);
1980
1981 irb_.CreateBr(block_cont);
1982
1983 irb_.SetInsertPoint(block_cont);
1984
1985 return;
1986}
1987
1988llvm::Value* GBCExpanderPass::Expand_InstanceOf(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001989 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1990 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1991 llvm::Value* object_addr = call_inst.getArgOperand(1);
1992
1993 llvm::BasicBlock* block_nullp =
1994 CreateBasicBlockWithDexPC(dex_pc, "nullp");
1995
1996 llvm::BasicBlock* block_test_class =
1997 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1998
1999 llvm::BasicBlock* block_class_equals =
2000 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
2001
2002 llvm::BasicBlock* block_test_sub_class =
2003 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
2004
2005 llvm::BasicBlock* block_cont =
2006 CreateBasicBlockWithDexPC(dex_pc, "instance_of_cont");
2007
2008 // Overview of the following code :
2009 // We check for null, if so, then false, otherwise check for class == . If so
2010 // then true, otherwise do callout slowpath.
2011 //
2012 // Test: Is the reference equal to null? Set 0 when it is null.
2013 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2014
2015 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
2016
2017 irb_.SetInsertPoint(block_nullp);
2018 irb_.CreateBr(block_cont);
2019
2020 // Test: Is the object instantiated from the given class?
2021 irb_.SetInsertPoint(block_test_class);
2022 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
TDYa127920be7c2012-09-10 17:13:22 -07002023 DCHECK_EQ(art::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002024
2025 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2026
2027 llvm::Value* object_type_field_addr =
2028 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2029
2030 llvm::Value* object_type_object_addr =
2031 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2032
2033 llvm::Value* equal_class =
2034 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2035
2036 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
2037
2038 irb_.SetInsertPoint(block_class_equals);
2039 irb_.CreateBr(block_cont);
2040
2041 // Test: Is the object instantiated from the subclass of the given class?
2042 irb_.SetInsertPoint(block_test_sub_class);
2043 llvm::Value* result =
2044 irb_.CreateCall2(irb_.GetRuntime(runtime_support::IsAssignable),
2045 type_object_addr, object_type_object_addr);
2046 irb_.CreateBr(block_cont);
2047
2048 irb_.SetInsertPoint(block_cont);
2049
2050 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJIntTy(), 3);
2051
2052 phi->addIncoming(irb_.getJInt(0), block_nullp);
2053 phi->addIncoming(irb_.getJInt(1), block_class_equals);
2054 phi->addIncoming(result, block_test_sub_class);
2055
2056 return phi;
2057}
2058
2059llvm::Value* GBCExpanderPass::Expand_NewInstance(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002060 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2061 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2062
2063 llvm::Function* runtime_func;
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002064 if (compiler_->CanAccessInstantiableTypeWithoutChecks(method_idx_, *dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002065 runtime_func = irb_.GetRuntime(runtime_support::AllocObject);
2066 } else {
2067 runtime_func = irb_.GetRuntime(runtime_support::AllocObjectWithAccessCheck);
2068 }
2069
2070 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2071
2072 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2073
2074 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2075
2076 EmitUpdateDexPC(dex_pc);
2077
2078 llvm::Value* object_addr =
2079 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
2080
2081 EmitGuard_ExceptionLandingPad(dex_pc);
2082
2083 return object_addr;
2084}
2085
2086llvm::Value* GBCExpanderPass::Expand_HLInvoke(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002087 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
TDYa127920be7c2012-09-10 17:13:22 -07002088 art::InvokeType invoke_type = static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
2089 bool is_static = (invoke_type == art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002090 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
TDYa127920be7c2012-09-10 17:13:22 -07002091 int opt_flags = LV2UInt(call_inst.getArgOperand(2));
TDYa127f71bf5a2012-07-29 20:09:52 -07002092
2093 // Compute invoke related information for compiler decision
2094 int vtable_idx = -1;
2095 uintptr_t direct_code = 0;
2096 uintptr_t direct_method = 0;
2097 bool is_fast_path = compiler_->
2098 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
2099 invoke_type, vtable_idx, direct_code, direct_method);
2100
2101 // Load *this* actual parameter
2102 llvm::Value* this_addr = NULL;
2103
2104 if (!is_static) {
2105 // Test: Is *this* parameter equal to null?
2106 this_addr = call_inst.getArgOperand(3);
2107 }
2108
2109 // Load the method object
2110 llvm::Value* callee_method_object_addr = NULL;
2111
2112 if (!is_fast_path) {
2113 callee_method_object_addr =
2114 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2115 this_addr, dex_pc, is_fast_path);
2116
TDYa127920be7c2012-09-10 17:13:22 -07002117 if (!is_static && !(opt_flags & MIR_IGNORE_NULL_CHECK)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002118 EmitGuard_NullPointerException(dex_pc, this_addr);
2119 }
2120 } else {
TDYa127920be7c2012-09-10 17:13:22 -07002121 if (!is_static && !(opt_flags & MIR_IGNORE_NULL_CHECK)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002122 EmitGuard_NullPointerException(dex_pc, this_addr);
2123 }
2124
2125 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002126 case art::kStatic:
2127 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002128 if (direct_method != 0u &&
2129 direct_method != static_cast<uintptr_t>(-1)) {
2130 callee_method_object_addr =
2131 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2132 irb_.getJObjectTy());
2133 } else {
2134 callee_method_object_addr =
2135 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2136 }
2137 break;
2138
TDYa127920be7c2012-09-10 17:13:22 -07002139 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002140 DCHECK(vtable_idx != -1);
2141 callee_method_object_addr =
2142 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2143 break;
2144
TDYa127920be7c2012-09-10 17:13:22 -07002145 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002146 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2147 "the fast path.";
2148 break;
2149
TDYa127920be7c2012-09-10 17:13:22 -07002150 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002151 callee_method_object_addr =
2152 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2153 invoke_type, this_addr,
2154 dex_pc, is_fast_path);
2155 break;
2156 }
2157 }
2158
2159 // Load the actual parameter
2160 std::vector<llvm::Value*> args;
2161
2162 args.push_back(callee_method_object_addr); // method object for callee
2163
2164 for (uint32_t i = 3; i < call_inst.getNumArgOperands(); ++i) {
2165 args.push_back(call_inst.getArgOperand(i));
2166 }
2167
2168 llvm::Value* code_addr;
2169 if (direct_code != 0u &&
2170 direct_code != static_cast<uintptr_t>(-1)) {
2171 code_addr =
2172 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_code),
2173 GetFunctionType(callee_method_idx, is_static)->getPointerTo());
2174 } else {
2175 code_addr =
2176 irb_.LoadFromObjectOffset(callee_method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -07002177 art::AbstractMethod::GetCodeOffset().Int32Value(),
TDYa127f71bf5a2012-07-29 20:09:52 -07002178 GetFunctionType(callee_method_idx, is_static)->getPointerTo(),
2179 kTBAAJRuntime);
2180 }
2181
2182 // Invoke callee
2183 EmitUpdateDexPC(dex_pc);
2184 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2185 EmitGuard_ExceptionLandingPad(dex_pc);
2186
2187 return retval;
2188}
2189
2190llvm::Value* GBCExpanderPass::Expand_OptArrayLength(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002191 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2192 // Get the array object address
2193 llvm::Value* array_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002194 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002195
TDYa127920be7c2012-09-10 17:13:22 -07002196 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
2197 EmitGuard_NullPointerException(dex_pc, array_addr);
2198 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002199
2200 // Get the array length and store it to the register
2201 return EmitLoadArrayLength(array_addr);
2202}
2203
2204llvm::Value* GBCExpanderPass::Expand_NewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002205 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2206 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2207 llvm::Value* length = call_inst.getArgOperand(1);
2208
2209 return EmitAllocNewArray(dex_pc, length, type_idx, false);
2210}
2211
2212llvm::Value* GBCExpanderPass::Expand_HLFilledNewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002213 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2214 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(1));
2215 uint32_t length = call_inst.getNumArgOperands() - 3;
2216
2217 llvm::Value* object_addr =
2218 EmitAllocNewArray(dex_pc, irb_.getInt32(length), type_idx, true);
2219
2220 if (length > 0) {
2221 // Check for the element type
2222 uint32_t type_desc_len = 0;
2223 const char* type_desc =
2224 dex_file_->StringByTypeIdx(type_idx, &type_desc_len);
2225
2226 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
2227 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
2228 bool is_elem_int_ty = (type_desc[1] == 'I');
2229
2230 uint32_t alignment;
2231 llvm::Constant* elem_size;
2232 llvm::PointerType* field_type;
2233
2234 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
2235 // as the element, thus we are only checking 2 cases: primitive int and
2236 // non-primitive type.
2237 if (is_elem_int_ty) {
2238 alignment = sizeof(int32_t);
2239 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
2240 field_type = irb_.getJIntTy()->getPointerTo();
2241 } else {
2242 alignment = irb_.getSizeOfPtrEquivInt();
2243 elem_size = irb_.getSizeOfPtrEquivIntValue();
2244 field_type = irb_.getJObjectTy()->getPointerTo();
2245 }
2246
2247 llvm::Value* data_field_offset =
TDYa127920be7c2012-09-10 17:13:22 -07002248 irb_.getPtrEquivInt(art::Array::DataOffset(alignment).Int32Value());
TDYa127f71bf5a2012-07-29 20:09:52 -07002249
2250 llvm::Value* data_field_addr =
2251 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
2252
2253 // TODO: Tune this code. Currently we are generating one instruction for
2254 // one element which may be very space consuming. Maybe changing to use
2255 // memcpy may help; however, since we can't guarantee that the alloca of
2256 // dalvik register are continuous, we can't perform such optimization yet.
2257 for (uint32_t i = 0; i < length; ++i) {
2258 llvm::Value* reg_value = call_inst.getArgOperand(i+3);
2259
2260 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
2261
2262 data_field_addr =
2263 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
2264 }
2265 }
2266
2267 return object_addr;
2268}
2269
2270void GBCExpanderPass::Expand_HLFillArrayData(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002271 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2272 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
2273 LV2SInt(call_inst.getArgOperand(0));
2274 llvm::Value* array_addr = call_inst.getArgOperand(1);
2275
TDYa127920be7c2012-09-10 17:13:22 -07002276 const art::Instruction::ArrayDataPayload* payload =
2277 reinterpret_cast<const art::Instruction::ArrayDataPayload*>(
TDYa127f71bf5a2012-07-29 20:09:52 -07002278 code_item_->insns_ + payload_offset);
2279
2280 if (payload->element_count == 0) {
2281 // When the number of the elements in the payload is zero, we don't have
2282 // to copy any numbers. However, we should check whether the array object
2283 // address is equal to null or not.
2284 EmitGuard_NullPointerException(dex_pc, array_addr);
2285 } else {
2286 // To save the code size, we are going to call the runtime function to
2287 // copy the content from DexFile.
2288
2289 // NOTE: We will check for the NullPointerException in the runtime.
2290
2291 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::FillArrayData);
2292
2293 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2294
2295 EmitUpdateDexPC(dex_pc);
2296
2297 irb_.CreateCall4(runtime_func,
2298 method_object_addr, irb_.getInt32(dex_pc),
2299 array_addr, irb_.getInt32(payload_offset));
2300
2301 EmitGuard_ExceptionLandingPad(dex_pc);
2302 }
2303
2304 return;
2305}
2306
2307llvm::Value* GBCExpanderPass::EmitAllocNewArray(uint32_t dex_pc,
2308 llvm::Value* array_length_value,
2309 uint32_t type_idx,
2310 bool is_filled_new_array) {
2311 llvm::Function* runtime_func;
2312
2313 bool skip_access_check =
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002314 compiler_->CanAccessTypeWithoutChecks(method_idx_, *dex_file_, type_idx);
TDYa127f71bf5a2012-07-29 20:09:52 -07002315
2316
2317 if (is_filled_new_array) {
2318 runtime_func = skip_access_check ?
2319 irb_.GetRuntime(runtime_support::CheckAndAllocArray) :
2320 irb_.GetRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck);
2321 } else {
2322 runtime_func = skip_access_check ?
2323 irb_.GetRuntime(runtime_support::AllocArray) :
2324 irb_.GetRuntime(runtime_support::AllocArrayWithAccessCheck);
2325 }
2326
2327 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2328
2329 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2330
2331 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2332
2333 EmitUpdateDexPC(dex_pc);
2334
2335 llvm::Value* object_addr =
2336 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
2337 array_length_value, thread_object_addr);
2338
2339 EmitGuard_ExceptionLandingPad(dex_pc);
2340
2341 return object_addr;
2342}
2343
2344llvm::Value* GBCExpanderPass::
2345EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -07002346 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -07002347 llvm::Value* this_addr,
2348 uint32_t dex_pc,
2349 bool is_fast_path) {
2350
2351 llvm::Function* runtime_func = NULL;
2352
2353 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002354 case art::kStatic:
TDYa127f71bf5a2012-07-29 20:09:52 -07002355 runtime_func = irb_.GetRuntime(runtime_support::FindStaticMethodWithAccessCheck);
2356 break;
2357
TDYa127920be7c2012-09-10 17:13:22 -07002358 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002359 runtime_func = irb_.GetRuntime(runtime_support::FindDirectMethodWithAccessCheck);
2360 break;
2361
TDYa127920be7c2012-09-10 17:13:22 -07002362 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002363 runtime_func = irb_.GetRuntime(runtime_support::FindVirtualMethodWithAccessCheck);
2364 break;
2365
TDYa127920be7c2012-09-10 17:13:22 -07002366 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002367 runtime_func = irb_.GetRuntime(runtime_support::FindSuperMethodWithAccessCheck);
2368 break;
2369
TDYa127920be7c2012-09-10 17:13:22 -07002370 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002371 if (is_fast_path) {
2372 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethod);
2373 } else {
2374 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethodWithAccessCheck);
2375 }
2376 break;
2377 }
2378
2379 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2380
2381 if (this_addr == NULL) {
TDYa127920be7c2012-09-10 17:13:22 -07002382 DCHECK_EQ(invoke_type, art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002383 this_addr = irb_.getJNull();
2384 }
2385
2386 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2387
2388 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2389
2390 EmitUpdateDexPC(dex_pc);
2391
2392 llvm::Value* callee_method_object_addr =
2393 irb_.CreateCall4(runtime_func,
2394 callee_method_idx_value,
2395 this_addr,
2396 caller_method_object_addr,
2397 thread_object_addr);
2398
2399 EmitGuard_ExceptionLandingPad(dex_pc);
2400
2401 return callee_method_object_addr;
2402}
2403
TDYa1275e869b62012-07-25 00:45:39 -07002404void GBCExpanderPass::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2405 // Using runtime support, let the target can override by InlineAssembly.
2406 irb_.Runtime().EmitMarkGCCard(value, target_addr);
2407}
2408
2409void GBCExpanderPass::EmitUpdateDexPC(uint32_t dex_pc) {
buzbeec531cef2012-10-18 07:09:20 -07002410#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002411 if (shadow_frame_ == NULL) {
2412 return;
2413 }
2414#endif
TDYa1275e869b62012-07-25 00:45:39 -07002415 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07002416 art::ShadowFrame::DexPCOffset(),
TDYa1275e869b62012-07-25 00:45:39 -07002417 irb_.getInt32(dex_pc),
2418 kTBAAShadowFrame);
2419}
2420
2421void GBCExpanderPass::EmitGuard_DivZeroException(uint32_t dex_pc,
2422 llvm::Value* denominator,
2423 JType op_jty) {
2424 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2425
2426 llvm::Constant* zero = irb_.getJZero(op_jty);
2427
2428 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2429
2430 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2431
2432 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2433
2434 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
2435
2436 irb_.SetInsertPoint(block_exception);
2437 EmitUpdateDexPC(dex_pc);
2438 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowDivZeroException));
2439 EmitBranchExceptionLandingPad(dex_pc);
2440
2441 irb_.SetInsertPoint(block_continue);
2442}
2443
2444void GBCExpanderPass::EmitGuard_NullPointerException(uint32_t dex_pc,
2445 llvm::Value* object) {
2446 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
2447
2448 llvm::BasicBlock* block_exception =
2449 CreateBasicBlockWithDexPC(dex_pc, "nullp");
2450
2451 llvm::BasicBlock* block_continue =
2452 CreateBasicBlockWithDexPC(dex_pc, "cont");
2453
2454 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
2455
2456 irb_.SetInsertPoint(block_exception);
2457 EmitUpdateDexPC(dex_pc);
2458 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowNullPointerException),
2459 irb_.getInt32(dex_pc));
2460 EmitBranchExceptionLandingPad(dex_pc);
2461
2462 irb_.SetInsertPoint(block_continue);
2463}
2464
2465void
2466GBCExpanderPass::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2467 llvm::Value* array,
2468 llvm::Value* index) {
2469 llvm::Value* array_len = EmitLoadArrayLength(array);
2470
2471 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2472
2473 llvm::BasicBlock* block_exception =
2474 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2475
2476 llvm::BasicBlock* block_continue =
2477 CreateBasicBlockWithDexPC(dex_pc, "cont");
2478
2479 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
2480
2481 irb_.SetInsertPoint(block_exception);
2482
2483 EmitUpdateDexPC(dex_pc);
2484 irb_.CreateCall2(irb_.GetRuntime(runtime_support::ThrowIndexOutOfBounds), index, array_len);
2485 EmitBranchExceptionLandingPad(dex_pc);
2486
2487 irb_.SetInsertPoint(block_continue);
2488}
2489
TDYa1275e869b62012-07-25 00:45:39 -07002490llvm::FunctionType* GBCExpanderPass::GetFunctionType(uint32_t method_idx,
2491 bool is_static) {
2492 // Get method signature
TDYa127920be7c2012-09-10 17:13:22 -07002493 art::DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
TDYa1275e869b62012-07-25 00:45:39 -07002494
2495 uint32_t shorty_size;
2496 const char* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
2497 CHECK_GE(shorty_size, 1u);
2498
2499 // Get return type
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002500
2501 char ret_shorty = shorty[0];
buzbeec531cef2012-10-18 07:09:20 -07002502#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaob2596522012-09-14 16:36:11 -07002503 ret_shorty = art::remapShorty(ret_shorty);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002504#endif
2505 llvm::Type* ret_type = irb_.getJType(ret_shorty, kAccurate);
TDYa1275e869b62012-07-25 00:45:39 -07002506
2507 // Get argument type
2508 std::vector<llvm::Type*> args_type;
2509
2510 args_type.push_back(irb_.getJObjectTy()); // method object pointer
2511
2512 if (!is_static) {
2513 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
2514 }
2515
2516 for (uint32_t i = 1; i < shorty_size; ++i) {
buzbeec531cef2012-10-18 07:09:20 -07002517#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaob2596522012-09-14 16:36:11 -07002518 char shorty_type = art::remapShorty(shorty[i]);
TDYa127f71bf5a2012-07-29 20:09:52 -07002519 args_type.push_back(irb_.getJType(shorty_type, kAccurate));
2520#else
TDYa1275e869b62012-07-25 00:45:39 -07002521 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
TDYa127f71bf5a2012-07-29 20:09:52 -07002522#endif
TDYa1275e869b62012-07-25 00:45:39 -07002523 }
2524
2525 return llvm::FunctionType::get(ret_type, args_type, false);
2526}
2527
2528
2529llvm::BasicBlock* GBCExpanderPass::
2530CreateBasicBlockWithDexPC(uint32_t dex_pc, const char* postfix) {
2531 std::string name;
2532
2533#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002534 art::StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
TDYa1275e869b62012-07-25 00:45:39 -07002535#endif
2536
2537 return llvm::BasicBlock::Create(context_, name, func_);
2538}
2539
2540llvm::BasicBlock* GBCExpanderPass::GetBasicBlock(uint32_t dex_pc) {
2541 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002542 CHECK(basic_blocks_[dex_pc] != NULL);
TDYa1275e869b62012-07-25 00:45:39 -07002543 return basic_blocks_[dex_pc];
2544}
2545
2546int32_t GBCExpanderPass::GetTryItemOffset(uint32_t dex_pc) {
2547 int32_t min = 0;
2548 int32_t max = code_item_->tries_size_ - 1;
2549
2550 while (min <= max) {
2551 int32_t mid = min + (max - min) / 2;
2552
TDYa127920be7c2012-09-10 17:13:22 -07002553 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*code_item_, mid);
TDYa1275e869b62012-07-25 00:45:39 -07002554 uint32_t start = ti->start_addr_;
2555 uint32_t end = start + ti->insn_count_;
2556
2557 if (dex_pc < start) {
2558 max = mid - 1;
2559 } else if (dex_pc >= end) {
2560 min = mid + 1;
2561 } else {
2562 return mid; // found
2563 }
2564 }
2565
2566 return -1; // not found
2567}
2568
2569llvm::BasicBlock* GBCExpanderPass::GetLandingPadBasicBlock(uint32_t dex_pc) {
2570 // Find the try item for this address in this method
2571 int32_t ti_offset = GetTryItemOffset(dex_pc);
2572
2573 if (ti_offset == -1) {
2574 return NULL; // No landing pad is available for this address.
2575 }
2576
2577 // Check for the existing landing pad basic block
2578 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2579 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2580
2581 if (block_lpad) {
2582 // We have generated landing pad for this try item already. Return the
2583 // same basic block.
2584 return block_lpad;
2585 }
2586
2587 // Get try item from code item
TDYa127920be7c2012-09-10 17:13:22 -07002588 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*code_item_, ti_offset);
TDYa1275e869b62012-07-25 00:45:39 -07002589
2590 std::string lpadname;
2591
2592#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002593 art::StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
TDYa1275e869b62012-07-25 00:45:39 -07002594#endif
2595
2596 // Create landing pad basic block
2597 block_lpad = llvm::BasicBlock::Create(context_, lpadname, func_);
2598
2599 // Change IRBuilder insert point
2600 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2601 irb_.SetInsertPoint(block_lpad);
2602
2603 // Find catch block with matching type
2604 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2605
2606 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
2607
2608 llvm::Value* catch_handler_index_value =
2609 irb_.CreateCall2(irb_.GetRuntime(runtime_support::FindCatchBlock),
2610 method_object_addr, ti_offset_value);
2611
2612 // Switch instruction (Go to unwind basic block by default)
2613 llvm::SwitchInst* sw =
2614 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2615
2616 // Cases with matched catch block
TDYa127920be7c2012-09-10 17:13:22 -07002617 art::CatchHandlerIterator iter(*code_item_, ti->start_addr_);
TDYa1275e869b62012-07-25 00:45:39 -07002618
2619 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2620 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2621 }
2622
2623 // Restore the orignal insert point for IRBuilder
2624 irb_.restoreIP(irb_ip_original);
2625
2626 // Cache this landing pad
2627 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2628 basic_block_landing_pads_[ti_offset] = block_lpad;
2629
2630 return block_lpad;
2631}
2632
2633llvm::BasicBlock* GBCExpanderPass::GetUnwindBasicBlock() {
2634 // Check the existing unwinding baisc block block
2635 if (basic_block_unwind_ != NULL) {
2636 return basic_block_unwind_;
2637 }
2638
2639 // Create new basic block for unwinding
2640 basic_block_unwind_ =
2641 llvm::BasicBlock::Create(context_, "exception_unwind", func_);
2642
2643 // Change IRBuilder insert point
2644 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2645 irb_.SetInsertPoint(basic_block_unwind_);
2646
2647 // Pop the shadow frame
2648 Expand_PopShadowFrame();
2649
2650 // Emit the code to return default value (zero) for the given return type.
2651 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
buzbeec531cef2012-10-18 07:09:20 -07002652#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaob2596522012-09-14 16:36:11 -07002653 ret_shorty = art::remapShorty(ret_shorty);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002654#endif
TDYa1275e869b62012-07-25 00:45:39 -07002655 if (ret_shorty == 'V') {
2656 irb_.CreateRetVoid();
2657 } else {
2658 irb_.CreateRet(irb_.getJZero(ret_shorty));
2659 }
2660
2661 // Restore the orignal insert point for IRBuilder
2662 irb_.restoreIP(irb_ip_original);
2663
2664 return basic_block_unwind_;
2665}
2666
2667void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2668 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002669 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002670 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002671 irb_.CreateBr(lpad);
2672 } else {
2673 irb_.CreateBr(GetUnwindBasicBlock());
2674 }
2675}
2676
2677void GBCExpanderPass::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
2678 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
2679
2680 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2681
2682 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002683 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002684 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002685 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
2686 } else {
2687 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
2688 }
2689
2690 irb_.SetInsertPoint(block_cont);
2691}
2692
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002693llvm::Value*
2694GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
2695 llvm::CallInst& call_inst) {
2696 switch (intr_id) {
2697 //==- Thread -----------------------------------------------------------==//
2698 case IntrinsicHelper::GetCurrentThread: {
TDYa127b672d1e2012-06-28 21:21:45 -07002699 return irb_.Runtime().EmitGetCurrentThread();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002700 }
Logan Chien75e4b602012-07-23 14:24:12 -07002701 case IntrinsicHelper::CheckSuspend: {
TDYa127890ea892012-08-22 10:49:42 -07002702 // We will add suspend by ourselves.
2703 return NULL;
2704 }
2705 case IntrinsicHelper::TestSuspend: {
Logan Chiend54a23d2012-07-24 11:19:23 -07002706 Expand_TestSuspend(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002707 return NULL;
2708 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002709 case IntrinsicHelper::MarkGCCard: {
TDYa1279a129452012-07-19 03:10:08 -07002710 Expand_MarkGCCard(call_inst);
2711 return NULL;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002712 }
Logan Chien75e4b602012-07-23 14:24:12 -07002713
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002714 //==- Exception --------------------------------------------------------==//
2715 case IntrinsicHelper::ThrowException: {
2716 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
2717 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002718 case IntrinsicHelper::HLThrowException: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002719 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2720
2721 EmitUpdateDexPC(dex_pc);
2722
2723 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowException),
2724 call_inst.getArgOperand(0));
2725
2726 EmitGuard_ExceptionLandingPad(dex_pc);
2727 return NULL;
2728 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002729 case IntrinsicHelper::GetException: {
TDYa127823433d2012-09-26 16:03:51 -07002730 return irb_.Runtime().EmitGetAndClearException();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002731 }
2732 case IntrinsicHelper::IsExceptionPending: {
2733 return irb_.Runtime().EmitIsExceptionPending();
2734 }
2735 case IntrinsicHelper::FindCatchBlock: {
2736 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
2737 }
2738 case IntrinsicHelper::ThrowDivZeroException: {
2739 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
2740 }
2741 case IntrinsicHelper::ThrowNullPointerException: {
2742 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
2743 }
2744 case IntrinsicHelper::ThrowIndexOutOfBounds: {
2745 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
2746 }
Logan Chien75e4b602012-07-23 14:24:12 -07002747
2748 //==- Const String -----------------------------------------------------==//
2749 case IntrinsicHelper::ConstString: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002750 return Expand_ConstString(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002751 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002752 case IntrinsicHelper::LoadStringFromDexCache: {
2753 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
2754 }
2755 case IntrinsicHelper::ResolveString: {
2756 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
2757 }
Logan Chien75e4b602012-07-23 14:24:12 -07002758
2759 //==- Const Class ------------------------------------------------------==//
2760 case IntrinsicHelper::ConstClass: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002761 return Expand_ConstClass(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002762 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002763 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
2764 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
2765 }
2766 case IntrinsicHelper::LoadTypeFromDexCache: {
2767 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
2768 }
2769 case IntrinsicHelper::InitializeType: {
2770 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
2771 }
Logan Chien75e4b602012-07-23 14:24:12 -07002772
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002773 //==- Lock -------------------------------------------------------------==//
2774 case IntrinsicHelper::LockObject: {
2775 Expand_LockObject(call_inst.getArgOperand(0));
2776 return NULL;
2777 }
2778 case IntrinsicHelper::UnlockObject: {
2779 Expand_UnlockObject(call_inst.getArgOperand(0));
2780 return NULL;
2781 }
Logan Chien75e4b602012-07-23 14:24:12 -07002782
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002783 //==- Cast -------------------------------------------------------------==//
2784 case IntrinsicHelper::CheckCast: {
2785 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
2786 }
Logan Chien75e4b602012-07-23 14:24:12 -07002787 case IntrinsicHelper::HLCheckCast: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002788 Expand_HLCheckCast(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002789 return NULL;
2790 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002791 case IntrinsicHelper::IsAssignable: {
2792 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
2793 }
Logan Chien75e4b602012-07-23 14:24:12 -07002794
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002795 //==- Alloc ------------------------------------------------------------==//
2796 case IntrinsicHelper::AllocObject: {
2797 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
2798 }
2799 case IntrinsicHelper::AllocObjectWithAccessCheck: {
2800 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
2801 }
Logan Chien75e4b602012-07-23 14:24:12 -07002802
2803 //==- Instance ---------------------------------------------------------==//
2804 case IntrinsicHelper::NewInstance: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002805 return Expand_NewInstance(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002806 }
2807 case IntrinsicHelper::InstanceOf: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002808 return Expand_InstanceOf(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002809 }
2810
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002811 //==- Array ------------------------------------------------------------==//
Logan Chien75e4b602012-07-23 14:24:12 -07002812 case IntrinsicHelper::NewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002813 return Expand_NewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002814 }
2815 case IntrinsicHelper::OptArrayLength: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002816 return Expand_OptArrayLength(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002817 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002818 case IntrinsicHelper::ArrayLength: {
2819 return EmitLoadArrayLength(call_inst.getArgOperand(0));
2820 }
2821 case IntrinsicHelper::AllocArray: {
2822 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
2823 }
2824 case IntrinsicHelper::AllocArrayWithAccessCheck: {
2825 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
2826 call_inst);
2827 }
2828 case IntrinsicHelper::CheckAndAllocArray: {
2829 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
2830 }
2831 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
2832 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
2833 call_inst);
2834 }
2835 case IntrinsicHelper::ArrayGet: {
2836 return Expand_ArrayGet(call_inst.getArgOperand(0),
2837 call_inst.getArgOperand(1),
2838 kInt);
2839 }
2840 case IntrinsicHelper::ArrayGetWide: {
2841 return Expand_ArrayGet(call_inst.getArgOperand(0),
2842 call_inst.getArgOperand(1),
2843 kLong);
2844 }
2845 case IntrinsicHelper::ArrayGetObject: {
2846 return Expand_ArrayGet(call_inst.getArgOperand(0),
2847 call_inst.getArgOperand(1),
2848 kObject);
2849 }
2850 case IntrinsicHelper::ArrayGetBoolean: {
2851 return Expand_ArrayGet(call_inst.getArgOperand(0),
2852 call_inst.getArgOperand(1),
2853 kBoolean);
2854 }
2855 case IntrinsicHelper::ArrayGetByte: {
2856 return Expand_ArrayGet(call_inst.getArgOperand(0),
2857 call_inst.getArgOperand(1),
2858 kByte);
2859 }
2860 case IntrinsicHelper::ArrayGetChar: {
2861 return Expand_ArrayGet(call_inst.getArgOperand(0),
2862 call_inst.getArgOperand(1),
2863 kChar);
2864 }
2865 case IntrinsicHelper::ArrayGetShort: {
2866 return Expand_ArrayGet(call_inst.getArgOperand(0),
2867 call_inst.getArgOperand(1),
2868 kShort);
2869 }
2870 case IntrinsicHelper::ArrayPut: {
2871 Expand_ArrayPut(call_inst.getArgOperand(0),
2872 call_inst.getArgOperand(1),
2873 call_inst.getArgOperand(2),
2874 kInt);
2875 return NULL;
2876 }
2877 case IntrinsicHelper::ArrayPutWide: {
2878 Expand_ArrayPut(call_inst.getArgOperand(0),
2879 call_inst.getArgOperand(1),
2880 call_inst.getArgOperand(2),
2881 kLong);
2882 return NULL;
2883 }
2884 case IntrinsicHelper::ArrayPutObject: {
2885 Expand_ArrayPut(call_inst.getArgOperand(0),
2886 call_inst.getArgOperand(1),
2887 call_inst.getArgOperand(2),
2888 kObject);
2889 return NULL;
2890 }
2891 case IntrinsicHelper::ArrayPutBoolean: {
2892 Expand_ArrayPut(call_inst.getArgOperand(0),
2893 call_inst.getArgOperand(1),
2894 call_inst.getArgOperand(2),
2895 kBoolean);
2896 return NULL;
2897 }
2898 case IntrinsicHelper::ArrayPutByte: {
2899 Expand_ArrayPut(call_inst.getArgOperand(0),
2900 call_inst.getArgOperand(1),
2901 call_inst.getArgOperand(2),
2902 kByte);
2903 return NULL;
2904 }
2905 case IntrinsicHelper::ArrayPutChar: {
2906 Expand_ArrayPut(call_inst.getArgOperand(0),
2907 call_inst.getArgOperand(1),
2908 call_inst.getArgOperand(2),
2909 kChar);
2910 return NULL;
2911 }
2912 case IntrinsicHelper::ArrayPutShort: {
2913 Expand_ArrayPut(call_inst.getArgOperand(0),
2914 call_inst.getArgOperand(1),
2915 call_inst.getArgOperand(2),
2916 kShort);
2917 return NULL;
2918 }
2919 case IntrinsicHelper::CheckPutArrayElement: {
2920 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
2921 }
2922 case IntrinsicHelper::FilledNewArray: {
2923 Expand_FilledNewArray(call_inst);
2924 return NULL;
2925 }
2926 case IntrinsicHelper::FillArrayData: {
2927 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
2928 }
Logan Chien75e4b602012-07-23 14:24:12 -07002929 case IntrinsicHelper::HLFillArrayData: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002930 Expand_HLFillArrayData(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002931 return NULL;
2932 }
2933 case IntrinsicHelper::HLFilledNewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002934 return Expand_HLFilledNewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002935 }
2936
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002937 //==- Instance Field ---------------------------------------------------==//
2938 case IntrinsicHelper::InstanceFieldGet:
2939 case IntrinsicHelper::InstanceFieldGetBoolean:
2940 case IntrinsicHelper::InstanceFieldGetByte:
2941 case IntrinsicHelper::InstanceFieldGetChar:
2942 case IntrinsicHelper::InstanceFieldGetShort: {
2943 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
2944 }
2945 case IntrinsicHelper::InstanceFieldGetWide: {
2946 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
2947 }
2948 case IntrinsicHelper::InstanceFieldGetObject: {
2949 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
2950 }
2951 case IntrinsicHelper::InstanceFieldGetFast: {
2952 return Expand_IGetFast(call_inst.getArgOperand(0),
2953 call_inst.getArgOperand(1),
2954 call_inst.getArgOperand(2),
2955 kInt);
2956 }
2957 case IntrinsicHelper::InstanceFieldGetWideFast: {
2958 return Expand_IGetFast(call_inst.getArgOperand(0),
2959 call_inst.getArgOperand(1),
2960 call_inst.getArgOperand(2),
2961 kLong);
2962 }
2963 case IntrinsicHelper::InstanceFieldGetObjectFast: {
2964 return Expand_IGetFast(call_inst.getArgOperand(0),
2965 call_inst.getArgOperand(1),
2966 call_inst.getArgOperand(2),
2967 kObject);
2968 }
2969 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
2970 return Expand_IGetFast(call_inst.getArgOperand(0),
2971 call_inst.getArgOperand(1),
2972 call_inst.getArgOperand(2),
2973 kBoolean);
2974 }
2975 case IntrinsicHelper::InstanceFieldGetByteFast: {
2976 return Expand_IGetFast(call_inst.getArgOperand(0),
2977 call_inst.getArgOperand(1),
2978 call_inst.getArgOperand(2),
2979 kByte);
2980 }
2981 case IntrinsicHelper::InstanceFieldGetCharFast: {
2982 return Expand_IGetFast(call_inst.getArgOperand(0),
2983 call_inst.getArgOperand(1),
2984 call_inst.getArgOperand(2),
2985 kChar);
2986 }
2987 case IntrinsicHelper::InstanceFieldGetShortFast: {
2988 return Expand_IGetFast(call_inst.getArgOperand(0),
2989 call_inst.getArgOperand(1),
2990 call_inst.getArgOperand(2),
2991 kShort);
2992 }
2993 case IntrinsicHelper::InstanceFieldPut:
2994 case IntrinsicHelper::InstanceFieldPutBoolean:
2995 case IntrinsicHelper::InstanceFieldPutByte:
2996 case IntrinsicHelper::InstanceFieldPutChar:
2997 case IntrinsicHelper::InstanceFieldPutShort: {
2998 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
2999 }
3000 case IntrinsicHelper::InstanceFieldPutWide: {
3001 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
3002 }
3003 case IntrinsicHelper::InstanceFieldPutObject: {
3004 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
3005 }
3006 case IntrinsicHelper::InstanceFieldPutFast: {
3007 Expand_IPutFast(call_inst.getArgOperand(0),
3008 call_inst.getArgOperand(1),
3009 call_inst.getArgOperand(2),
3010 call_inst.getArgOperand(3),
3011 kInt);
3012 return NULL;
3013 }
3014 case IntrinsicHelper::InstanceFieldPutWideFast: {
3015 Expand_IPutFast(call_inst.getArgOperand(0),
3016 call_inst.getArgOperand(1),
3017 call_inst.getArgOperand(2),
3018 call_inst.getArgOperand(3),
3019 kLong);
3020 return NULL;
3021 }
3022 case IntrinsicHelper::InstanceFieldPutObjectFast: {
3023 Expand_IPutFast(call_inst.getArgOperand(0),
3024 call_inst.getArgOperand(1),
3025 call_inst.getArgOperand(2),
3026 call_inst.getArgOperand(3),
3027 kObject);
3028 return NULL;
3029 }
3030 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
3031 Expand_IPutFast(call_inst.getArgOperand(0),
3032 call_inst.getArgOperand(1),
3033 call_inst.getArgOperand(2),
3034 call_inst.getArgOperand(3),
3035 kBoolean);
3036 return NULL;
3037 }
3038 case IntrinsicHelper::InstanceFieldPutByteFast: {
3039 Expand_IPutFast(call_inst.getArgOperand(0),
3040 call_inst.getArgOperand(1),
3041 call_inst.getArgOperand(2),
3042 call_inst.getArgOperand(3),
3043 kByte);
3044 return NULL;
3045 }
3046 case IntrinsicHelper::InstanceFieldPutCharFast: {
3047 Expand_IPutFast(call_inst.getArgOperand(0),
3048 call_inst.getArgOperand(1),
3049 call_inst.getArgOperand(2),
3050 call_inst.getArgOperand(3),
3051 kChar);
3052 return NULL;
3053 }
3054 case IntrinsicHelper::InstanceFieldPutShortFast: {
3055 Expand_IPutFast(call_inst.getArgOperand(0),
3056 call_inst.getArgOperand(1),
3057 call_inst.getArgOperand(2),
3058 call_inst.getArgOperand(3),
3059 kShort);
3060 return NULL;
3061 }
Logan Chien75e4b602012-07-23 14:24:12 -07003062
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003063 //==- Static Field -----------------------------------------------------==//
3064 case IntrinsicHelper::StaticFieldGet:
3065 case IntrinsicHelper::StaticFieldGetBoolean:
3066 case IntrinsicHelper::StaticFieldGetByte:
3067 case IntrinsicHelper::StaticFieldGetChar:
3068 case IntrinsicHelper::StaticFieldGetShort: {
3069 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
3070 }
3071 case IntrinsicHelper::StaticFieldGetWide: {
3072 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
3073 }
3074 case IntrinsicHelper::StaticFieldGetObject: {
3075 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
3076 }
3077 case IntrinsicHelper::StaticFieldGetFast: {
3078 return Expand_SGetFast(call_inst.getArgOperand(0),
3079 call_inst.getArgOperand(1),
3080 call_inst.getArgOperand(2),
3081 kInt);
3082 }
3083 case IntrinsicHelper::StaticFieldGetWideFast: {
3084 return Expand_SGetFast(call_inst.getArgOperand(0),
3085 call_inst.getArgOperand(1),
3086 call_inst.getArgOperand(2),
3087 kLong);
3088 }
3089 case IntrinsicHelper::StaticFieldGetObjectFast: {
3090 return Expand_SGetFast(call_inst.getArgOperand(0),
3091 call_inst.getArgOperand(1),
3092 call_inst.getArgOperand(2),
3093 kObject);
3094 }
3095 case IntrinsicHelper::StaticFieldGetBooleanFast: {
3096 return Expand_SGetFast(call_inst.getArgOperand(0),
3097 call_inst.getArgOperand(1),
3098 call_inst.getArgOperand(2),
3099 kBoolean);
3100 }
3101 case IntrinsicHelper::StaticFieldGetByteFast: {
3102 return Expand_SGetFast(call_inst.getArgOperand(0),
3103 call_inst.getArgOperand(1),
3104 call_inst.getArgOperand(2),
3105 kByte);
3106 }
3107 case IntrinsicHelper::StaticFieldGetCharFast: {
3108 return Expand_SGetFast(call_inst.getArgOperand(0),
3109 call_inst.getArgOperand(1),
3110 call_inst.getArgOperand(2),
3111 kChar);
3112 }
3113 case IntrinsicHelper::StaticFieldGetShortFast: {
3114 return Expand_SGetFast(call_inst.getArgOperand(0),
3115 call_inst.getArgOperand(1),
3116 call_inst.getArgOperand(2),
3117 kShort);
3118 }
3119 case IntrinsicHelper::StaticFieldPut:
3120 case IntrinsicHelper::StaticFieldPutBoolean:
3121 case IntrinsicHelper::StaticFieldPutByte:
3122 case IntrinsicHelper::StaticFieldPutChar:
3123 case IntrinsicHelper::StaticFieldPutShort: {
3124 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
3125 }
3126 case IntrinsicHelper::StaticFieldPutWide: {
3127 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
3128 }
3129 case IntrinsicHelper::StaticFieldPutObject: {
3130 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
3131 }
3132 case IntrinsicHelper::StaticFieldPutFast: {
3133 Expand_SPutFast(call_inst.getArgOperand(0),
3134 call_inst.getArgOperand(1),
3135 call_inst.getArgOperand(2),
3136 call_inst.getArgOperand(3),
3137 kInt);
3138 return NULL;
3139 }
3140 case IntrinsicHelper::StaticFieldPutWideFast: {
3141 Expand_SPutFast(call_inst.getArgOperand(0),
3142 call_inst.getArgOperand(1),
3143 call_inst.getArgOperand(2),
3144 call_inst.getArgOperand(3),
3145 kLong);
3146 return NULL;
3147 }
3148 case IntrinsicHelper::StaticFieldPutObjectFast: {
3149 Expand_SPutFast(call_inst.getArgOperand(0),
3150 call_inst.getArgOperand(1),
3151 call_inst.getArgOperand(2),
3152 call_inst.getArgOperand(3),
3153 kObject);
3154 return NULL;
3155 }
3156 case IntrinsicHelper::StaticFieldPutBooleanFast: {
3157 Expand_SPutFast(call_inst.getArgOperand(0),
3158 call_inst.getArgOperand(1),
3159 call_inst.getArgOperand(2),
3160 call_inst.getArgOperand(3),
3161 kBoolean);
3162 return NULL;
3163 }
3164 case IntrinsicHelper::StaticFieldPutByteFast: {
3165 Expand_SPutFast(call_inst.getArgOperand(0),
3166 call_inst.getArgOperand(1),
3167 call_inst.getArgOperand(2),
3168 call_inst.getArgOperand(3),
3169 kByte);
3170 return NULL;
3171 }
3172 case IntrinsicHelper::StaticFieldPutCharFast: {
3173 Expand_SPutFast(call_inst.getArgOperand(0),
3174 call_inst.getArgOperand(1),
3175 call_inst.getArgOperand(2),
3176 call_inst.getArgOperand(3),
3177 kChar);
3178 return NULL;
3179 }
3180 case IntrinsicHelper::StaticFieldPutShortFast: {
3181 Expand_SPutFast(call_inst.getArgOperand(0),
3182 call_inst.getArgOperand(1),
3183 call_inst.getArgOperand(2),
3184 call_inst.getArgOperand(3),
3185 kShort);
3186 return NULL;
3187 }
3188 case IntrinsicHelper::LoadDeclaringClassSSB: {
3189 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
3190 }
3191 case IntrinsicHelper::LoadClassSSBFromDexCache: {
3192 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
3193 }
3194 case IntrinsicHelper::InitializeAndLoadClassSSB: {
3195 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
3196 }
Logan Chien75e4b602012-07-23 14:24:12 -07003197
3198 //==- High-level Array -------------------------------------------------==//
3199 case IntrinsicHelper::HLArrayGet: {
TDYa1275a26d442012-07-26 18:58:38 -07003200 return Expand_HLArrayGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003201 }
3202 case IntrinsicHelper::HLArrayGetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003203 return Expand_HLArrayGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003204 }
3205 case IntrinsicHelper::HLArrayGetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003206 return Expand_HLArrayGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003207 }
3208 case IntrinsicHelper::HLArrayGetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003209 return Expand_HLArrayGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003210 }
3211 case IntrinsicHelper::HLArrayGetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003212 return Expand_HLArrayGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003213 }
3214 case IntrinsicHelper::HLArrayGetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003215 return Expand_HLArrayGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003216 }
3217 case IntrinsicHelper::HLArrayGetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003218 return Expand_HLArrayGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003219 }
3220 case IntrinsicHelper::HLArrayGetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003221 return Expand_HLArrayGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003222 }
3223 case IntrinsicHelper::HLArrayGetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003224 return Expand_HLArrayGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003225 }
3226 case IntrinsicHelper::HLArrayPut: {
TDYa1275a26d442012-07-26 18:58:38 -07003227 Expand_HLArrayPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003228 return NULL;
3229 }
3230 case IntrinsicHelper::HLArrayPutBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003231 Expand_HLArrayPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003232 return NULL;
3233 }
3234 case IntrinsicHelper::HLArrayPutByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003235 Expand_HLArrayPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003236 return NULL;
3237 }
3238 case IntrinsicHelper::HLArrayPutChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003239 Expand_HLArrayPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003240 return NULL;
3241 }
3242 case IntrinsicHelper::HLArrayPutShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003243 Expand_HLArrayPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003244 return NULL;
3245 }
3246 case IntrinsicHelper::HLArrayPutFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003247 Expand_HLArrayPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003248 return NULL;
3249 }
3250 case IntrinsicHelper::HLArrayPutWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003251 Expand_HLArrayPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003252 return NULL;
3253 }
3254 case IntrinsicHelper::HLArrayPutDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003255 Expand_HLArrayPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003256 return NULL;
3257 }
3258 case IntrinsicHelper::HLArrayPutObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003259 Expand_HLArrayPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003260 return NULL;
3261 }
3262
3263 //==- High-level Instance ----------------------------------------------==//
3264 case IntrinsicHelper::HLIGet: {
TDYa1275e869b62012-07-25 00:45:39 -07003265 return Expand_HLIGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003266 }
3267 case IntrinsicHelper::HLIGetBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003268 return Expand_HLIGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003269 }
3270 case IntrinsicHelper::HLIGetByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003271 return Expand_HLIGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003272 }
3273 case IntrinsicHelper::HLIGetChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003274 return Expand_HLIGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003275 }
3276 case IntrinsicHelper::HLIGetShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003277 return Expand_HLIGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003278 }
3279 case IntrinsicHelper::HLIGetFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003280 return Expand_HLIGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003281 }
3282 case IntrinsicHelper::HLIGetWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003283 return Expand_HLIGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003284 }
3285 case IntrinsicHelper::HLIGetDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003286 return Expand_HLIGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003287 }
3288 case IntrinsicHelper::HLIGetObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003289 return Expand_HLIGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003290 }
3291 case IntrinsicHelper::HLIPut: {
TDYa1275e869b62012-07-25 00:45:39 -07003292 Expand_HLIPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003293 return NULL;
3294 }
3295 case IntrinsicHelper::HLIPutBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003296 Expand_HLIPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003297 return NULL;
3298 }
3299 case IntrinsicHelper::HLIPutByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003300 Expand_HLIPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003301 return NULL;
3302 }
3303 case IntrinsicHelper::HLIPutChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003304 Expand_HLIPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003305 return NULL;
3306 }
3307 case IntrinsicHelper::HLIPutShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003308 Expand_HLIPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003309 return NULL;
3310 }
3311 case IntrinsicHelper::HLIPutFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003312 Expand_HLIPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003313 return NULL;
3314 }
3315 case IntrinsicHelper::HLIPutWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003316 Expand_HLIPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003317 return NULL;
3318 }
3319 case IntrinsicHelper::HLIPutDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003320 Expand_HLIPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003321 return NULL;
3322 }
3323 case IntrinsicHelper::HLIPutObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003324 Expand_HLIPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003325 return NULL;
3326 }
3327
3328 //==- High-level Invoke ------------------------------------------------==//
TDYa127f71bf5a2012-07-29 20:09:52 -07003329 case IntrinsicHelper::HLInvokeVoid:
3330 case IntrinsicHelper::HLInvokeObj:
3331 case IntrinsicHelper::HLInvokeInt:
3332 case IntrinsicHelper::HLInvokeFloat:
3333 case IntrinsicHelper::HLInvokeLong:
Logan Chien75e4b602012-07-23 14:24:12 -07003334 case IntrinsicHelper::HLInvokeDouble: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003335 return Expand_HLInvoke(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003336 }
3337
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003338 //==- Invoke -----------------------------------------------------------==//
3339 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
3340 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
3341 }
3342 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
3343 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
3344 }
3345 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
3346 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
3347 }
3348 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
3349 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
3350 }
3351 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
3352 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
3353 }
3354 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
3355 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
3356 }
3357 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
3358 return Expand_GetVirtualCalleeMethodObjAddrFast(
3359 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
3360 }
3361 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
3362 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
3363 }
3364 case IntrinsicHelper::InvokeRetVoid:
3365 case IntrinsicHelper::InvokeRetBoolean:
3366 case IntrinsicHelper::InvokeRetByte:
3367 case IntrinsicHelper::InvokeRetChar:
3368 case IntrinsicHelper::InvokeRetShort:
3369 case IntrinsicHelper::InvokeRetInt:
3370 case IntrinsicHelper::InvokeRetLong:
3371 case IntrinsicHelper::InvokeRetFloat:
3372 case IntrinsicHelper::InvokeRetDouble:
3373 case IntrinsicHelper::InvokeRetObject: {
3374 return Expand_Invoke(call_inst);
3375 }
Logan Chien75e4b602012-07-23 14:24:12 -07003376
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003377 //==- Math -------------------------------------------------------------==//
3378 case IntrinsicHelper::DivInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003379 return Expand_DivRem(call_inst, /* is_div */true, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003380 }
3381 case IntrinsicHelper::RemInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003382 return Expand_DivRem(call_inst, /* is_div */false, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003383 }
3384 case IntrinsicHelper::DivLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003385 return Expand_DivRem(call_inst, /* is_div */true, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003386 }
3387 case IntrinsicHelper::RemLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003388 return Expand_DivRem(call_inst, /* is_div */false, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003389 }
3390 case IntrinsicHelper::D2L: {
3391 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
3392 }
3393 case IntrinsicHelper::D2I: {
3394 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
3395 }
3396 case IntrinsicHelper::F2L: {
3397 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
3398 }
3399 case IntrinsicHelper::F2I: {
3400 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
3401 }
Logan Chien75e4b602012-07-23 14:24:12 -07003402
3403 //==- High-level Static ------------------------------------------------==//
3404 case IntrinsicHelper::HLSget: {
TDYa1275a26d442012-07-26 18:58:38 -07003405 return Expand_HLSget(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003406 }
3407 case IntrinsicHelper::HLSgetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003408 return Expand_HLSget(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003409 }
3410 case IntrinsicHelper::HLSgetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003411 return Expand_HLSget(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003412 }
3413 case IntrinsicHelper::HLSgetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003414 return Expand_HLSget(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003415 }
3416 case IntrinsicHelper::HLSgetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003417 return Expand_HLSget(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003418 }
3419 case IntrinsicHelper::HLSgetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003420 return Expand_HLSget(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003421 }
3422 case IntrinsicHelper::HLSgetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003423 return Expand_HLSget(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003424 }
3425 case IntrinsicHelper::HLSgetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003426 return Expand_HLSget(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003427 }
3428 case IntrinsicHelper::HLSgetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003429 return Expand_HLSget(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003430 }
3431 case IntrinsicHelper::HLSput: {
TDYa1275a26d442012-07-26 18:58:38 -07003432 Expand_HLSput(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003433 return NULL;
3434 }
3435 case IntrinsicHelper::HLSputBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003436 Expand_HLSput(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003437 return NULL;
3438 }
3439 case IntrinsicHelper::HLSputByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003440 Expand_HLSput(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003441 return NULL;
3442 }
3443 case IntrinsicHelper::HLSputChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003444 Expand_HLSput(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003445 return NULL;
3446 }
3447 case IntrinsicHelper::HLSputShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003448 Expand_HLSput(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003449 return NULL;
3450 }
3451 case IntrinsicHelper::HLSputFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003452 Expand_HLSput(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003453 return NULL;
3454 }
3455 case IntrinsicHelper::HLSputWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003456 Expand_HLSput(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003457 return NULL;
3458 }
3459 case IntrinsicHelper::HLSputDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003460 Expand_HLSput(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003461 return NULL;
3462 }
3463 case IntrinsicHelper::HLSputObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003464 Expand_HLSput(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003465 return NULL;
3466 }
3467
3468 //==- High-level Monitor -----------------------------------------------==//
3469 case IntrinsicHelper::MonitorEnter: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003470 Expand_MonitorEnter(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003471 return NULL;
3472 }
3473 case IntrinsicHelper::MonitorExit: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003474 Expand_MonitorExit(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003475 return NULL;
3476 }
3477
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003478 //==- Shadow Frame -----------------------------------------------------==//
3479 case IntrinsicHelper::AllocaShadowFrame: {
TDYa1278e950c12012-11-02 09:58:19 -07003480 Expand_AllocaShadowFrame(call_inst.getArgOperand(0),
3481 call_inst.getArgOperand(1));
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003482 return NULL;
3483 }
3484 case IntrinsicHelper::SetShadowFrameEntry: {
3485 Expand_SetShadowFrameEntry(call_inst.getArgOperand(0),
3486 call_inst.getArgOperand(1));
3487 return NULL;
3488 }
TDYa1278e950c12012-11-02 09:58:19 -07003489 case IntrinsicHelper::SetVReg: {
3490 Expand_SetVReg(call_inst.getArgOperand(0),
3491 call_inst.getArgOperand(1));
3492 return NULL;
3493 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003494 case IntrinsicHelper::PopShadowFrame: {
3495 Expand_PopShadowFrame();
3496 return NULL;
3497 }
3498 case IntrinsicHelper::UpdateDexPC: {
3499 Expand_UpdateDexPC(call_inst.getArgOperand(0));
3500 return NULL;
3501 }
TDYa127a1b21852012-07-23 03:20:39 -07003502
Logan Chien75e4b602012-07-23 14:24:12 -07003503 //==- Comparison -------------------------------------------------------==//
3504 case IntrinsicHelper::CmplFloat:
3505 case IntrinsicHelper::CmplDouble: {
3506 return Expand_FPCompare(call_inst.getArgOperand(0),
3507 call_inst.getArgOperand(1),
3508 false);
3509 }
3510 case IntrinsicHelper::CmpgFloat:
3511 case IntrinsicHelper::CmpgDouble: {
3512 return Expand_FPCompare(call_inst.getArgOperand(0),
3513 call_inst.getArgOperand(1),
3514 true);
3515 }
3516 case IntrinsicHelper::CmpLong: {
3517 return Expand_LongCompare(call_inst.getArgOperand(0),
3518 call_inst.getArgOperand(1));
3519 }
TDYa127a1b21852012-07-23 03:20:39 -07003520
Logan Chien75e4b602012-07-23 14:24:12 -07003521 //==- Const ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003522 case IntrinsicHelper::ConstInt:
3523 case IntrinsicHelper::ConstLong: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003524 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003525 }
TDYa127920be7c2012-09-10 17:13:22 -07003526 case IntrinsicHelper::ConstFloat: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003527 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3528 irb_.getJFloatTy());
Logan Chien75e4b602012-07-23 14:24:12 -07003529 }
TDYa127920be7c2012-09-10 17:13:22 -07003530 case IntrinsicHelper::ConstDouble: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003531 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3532 irb_.getJDoubleTy());
3533 }
TDYa127920be7c2012-09-10 17:13:22 -07003534 case IntrinsicHelper::ConstObj: {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003535 CHECK(LV2UInt(call_inst.getArgOperand(0)) == 0);
3536 return irb_.getJNull();
Logan Chien75e4b602012-07-23 14:24:12 -07003537 }
3538
3539 //==- Method Info ------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003540 case IntrinsicHelper::MethodInfo: {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003541 // Nothing to be done, because MethodInfo carries optional hints that are
3542 // not needed by the portable path.
Logan Chien75e4b602012-07-23 14:24:12 -07003543 return NULL;
3544 }
3545
3546 //==- Copy -------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003547 case IntrinsicHelper::CopyInt:
3548 case IntrinsicHelper::CopyFloat:
3549 case IntrinsicHelper::CopyLong:
3550 case IntrinsicHelper::CopyDouble:
3551 case IntrinsicHelper::CopyObj: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003552 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003553 }
3554
3555 //==- Shift ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003556 case IntrinsicHelper::SHLLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003557 return Expand_IntegerShift(call_inst.getArgOperand(0),
3558 call_inst.getArgOperand(1),
3559 kIntegerSHL, kLong);
3560 }
TDYa127920be7c2012-09-10 17:13:22 -07003561 case IntrinsicHelper::SHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003562 return Expand_IntegerShift(call_inst.getArgOperand(0),
3563 call_inst.getArgOperand(1),
3564 kIntegerSHR, kLong);
3565 }
TDYa127920be7c2012-09-10 17:13:22 -07003566 case IntrinsicHelper::USHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003567 return Expand_IntegerShift(call_inst.getArgOperand(0),
3568 call_inst.getArgOperand(1),
3569 kIntegerUSHR, kLong);
3570 }
TDYa127920be7c2012-09-10 17:13:22 -07003571 case IntrinsicHelper::SHLInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003572 return Expand_IntegerShift(call_inst.getArgOperand(0),
3573 call_inst.getArgOperand(1),
3574 kIntegerSHL, kInt);
3575 }
TDYa127920be7c2012-09-10 17:13:22 -07003576 case IntrinsicHelper::SHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003577 return Expand_IntegerShift(call_inst.getArgOperand(0),
3578 call_inst.getArgOperand(1),
3579 kIntegerSHR, kInt);
3580 }
TDYa127920be7c2012-09-10 17:13:22 -07003581 case IntrinsicHelper::USHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003582 return Expand_IntegerShift(call_inst.getArgOperand(0),
3583 call_inst.getArgOperand(1),
3584 kIntegerUSHR, kInt);
3585 }
3586
3587 //==- Conversion -------------------------------------------------------==//
TDYa127a1b21852012-07-23 03:20:39 -07003588 case IntrinsicHelper::IntToChar: {
3589 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
3590 irb_.getJIntTy());
3591 }
3592 case IntrinsicHelper::IntToShort: {
3593 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
3594 irb_.getJIntTy());
3595 }
3596 case IntrinsicHelper::IntToByte: {
3597 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
3598 irb_.getJIntTy());
3599 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003600
TDYa12787caa7e2012-08-25 23:23:27 -07003601 //==- Exception --------------------------------------------------------==//
3602 case IntrinsicHelper::CatchTargets: {
TDYa12755e5e6c2012-09-11 15:14:42 -07003603 UpdatePhiInstruction(current_bb_, irb_.GetInsertBlock());
TDYa12787caa7e2012-08-25 23:23:27 -07003604 llvm::SwitchInst* si = llvm::dyn_cast<llvm::SwitchInst>(call_inst.getNextNode());
3605 CHECK(si != NULL);
3606 irb_.CreateBr(si->getDefaultDest());
3607 si->eraseFromParent();
3608 return call_inst.getArgOperand(0);
3609 }
3610
Logan Chien75e4b602012-07-23 14:24:12 -07003611 //==- Unknown Cases ----------------------------------------------------==//
3612 case IntrinsicHelper::MaxIntrinsicId:
3613 case IntrinsicHelper::UnknownId:
3614 //default:
3615 // NOTE: "default" is intentionally commented so that C/C++ compiler will
3616 // give some warning on unmatched cases.
3617 // NOTE: We should not implement these cases.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003618 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003619 }
Logan Chien75e4b602012-07-23 14:24:12 -07003620 UNIMPLEMENTED(FATAL) << "Unexpected GBC intrinsic: " << static_cast<int>(intr_id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003621 return NULL;
3622}
3623
3624} // anonymous namespace
3625
3626namespace art {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003627
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003628namespace compiler_llvm {
3629
3630llvm::FunctionPass*
3631CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb) {
3632 return new GBCExpanderPass(intrinsic_helper, irb);
3633}
3634
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003635llvm::FunctionPass*
3636CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
3637 Compiler* compiler, OatCompilationUnit* oat_compilation_unit) {
3638 if (compiler != NULL) {
3639 return new GBCExpanderPass(intrinsic_helper, irb,
3640 compiler, oat_compilation_unit);
3641 } else {
3642 return new GBCExpanderPass(intrinsic_helper, irb);
3643 }
3644}
3645
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003646} // namespace compiler_llvm
3647} // namespace art