blob: 88505eec71b30a0812efb4a702f5084d9c769bf0 [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
27#include <llvm/ADT/STLExtras.h>
28#include <llvm/Intrinsics.h>
Logan Chiend36a2ac2012-08-04 00:37:30 +080029#include <llvm/Metadata.h>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070030#include <llvm/Pass.h>
31#include <llvm/Support/CFG.h>
32#include <llvm/Support/InstIterator.h>
33
34#include <vector>
35
36using namespace art;
37using namespace compiler_llvm;
38
39using art::greenland::IntrinsicHelper;
40
41namespace {
42
43class GBCExpanderPass : public llvm::FunctionPass {
44 private:
45 const IntrinsicHelper& intrinsic_helper_;
46 IRBuilder& irb_;
47
48 llvm::LLVMContext& context_;
49 RuntimeSupportBuilder& rtb_;
50
51 private:
52 llvm::AllocaInst* shadow_frame_;
53 llvm::Value* old_shadow_frame_;
54 uint32_t shadow_frame_size_;
55
56 private:
TDYa1275e869b62012-07-25 00:45:39 -070057 Compiler* compiler_;
58
59 const DexFile* dex_file_;
60 DexCache* dex_cache_;
61 const DexFile::CodeItem* code_item_;
62
63 OatCompilationUnit* oat_compilation_unit_;
64
65 uint32_t method_idx_;
66
67 llvm::Function* func_;
68
69 std::vector<llvm::BasicBlock*> basic_blocks_;
70
71 std::vector<llvm::BasicBlock*> basic_block_landing_pads_;
72 llvm::BasicBlock* basic_block_unwind_;
73
Logan Chien67645d82012-08-17 09:10:54 +080074 bool changed_;
75
TDYa1275e869b62012-07-25 00:45:39 -070076 private:
Shih-wei Liao21d28f52012-06-12 05:55:00 -070077 //----------------------------------------------------------------------------
Logan Chien75e4b602012-07-23 14:24:12 -070078 // Constant for GBC expansion
79 //----------------------------------------------------------------------------
80 enum IntegerShiftKind {
81 kIntegerSHL,
82 kIntegerSHR,
83 kIntegerUSHR,
84 };
85
86 private:
87 //----------------------------------------------------------------------------
Shih-wei Liao21d28f52012-06-12 05:55:00 -070088 // Helper function for GBC expansion
89 //----------------------------------------------------------------------------
90
Shih-wei Liao21d28f52012-06-12 05:55:00 -070091 llvm::Value* ExpandToRuntime(runtime_support::RuntimeId rt,
92 llvm::CallInst& inst);
93
TDYa1275e869b62012-07-25 00:45:39 -070094 uint64_t LV2UInt(llvm::Value* lv) {
95 return llvm::cast<llvm::ConstantInt>(lv)->getZExtValue();
96 }
97
98 int64_t LV2SInt(llvm::Value* lv) {
99 return llvm::cast<llvm::ConstantInt>(lv)->getSExtValue();
100 }
101
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700102 private:
103 // TODO: Almost all Emit* are directly copy-n-paste from MethodCompiler.
104 // Refactor these utility functions from MethodCompiler to avoid forking.
105
Logan Chien67645d82012-08-17 09:10:54 +0800106 void EmitStackOverflowCheck(llvm::Instruction* first_non_alloca);
107
108 void RewriteFunction();
109
110 void RewriteBasicBlock(llvm::BasicBlock* original_block);
111
112 void UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
113 llvm::BasicBlock* new_basic_block);
114
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700115
116 //----------------------------------------------------------------------------
117 // Dex cache code generation helper function
118 //----------------------------------------------------------------------------
119 llvm::Value* EmitLoadDexCacheAddr(MemberOffset dex_cache_offset);
120
121 llvm::Value* EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx);
122
123 llvm::Value* EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx);
124
125 llvm::Value* EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx);
126
127 llvm::Value* EmitLoadDexCacheStringFieldAddr(uint32_t string_idx);
128
129 //----------------------------------------------------------------------------
130 // Code generation helper function
131 //----------------------------------------------------------------------------
132 llvm::Value* EmitLoadMethodObjectAddr();
133
134 llvm::Value* EmitLoadArrayLength(llvm::Value* array);
135
136 llvm::Value* EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx);
137
138 llvm::Value* EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
139 llvm::Value* this_addr);
140
141 llvm::Value* EmitArrayGEP(llvm::Value* array_addr,
142 llvm::Value* index_value,
143 JType elem_jty);
144
145 private:
146 //----------------------------------------------------------------------------
147 // Expand Greenland intrinsics
148 //----------------------------------------------------------------------------
149 void Expand_TestSuspend(llvm::CallInst& call_inst);
150
TDYa1279a129452012-07-19 03:10:08 -0700151 void Expand_MarkGCCard(llvm::CallInst& call_inst);
152
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700153 llvm::Value* Expand_GetException();
154
155 llvm::Value* Expand_LoadStringFromDexCache(llvm::Value* string_idx_value);
156
157 llvm::Value* Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value);
158
159 void Expand_LockObject(llvm::Value* obj);
160
161 void Expand_UnlockObject(llvm::Value* obj);
162
163 llvm::Value* Expand_ArrayGet(llvm::Value* array_addr,
164 llvm::Value* index_value,
165 JType elem_jty);
166
167 void Expand_ArrayPut(llvm::Value* new_value,
168 llvm::Value* array_addr,
169 llvm::Value* index_value,
170 JType elem_jty);
171
172 void Expand_FilledNewArray(llvm::CallInst& call_inst);
173
174 llvm::Value* Expand_IGetFast(llvm::Value* field_offset_value,
175 llvm::Value* is_volatile_value,
176 llvm::Value* object_addr,
177 JType field_jty);
178
179 void Expand_IPutFast(llvm::Value* field_offset_value,
180 llvm::Value* is_volatile_value,
181 llvm::Value* object_addr,
182 llvm::Value* new_value,
183 JType field_jty);
184
185 llvm::Value* Expand_SGetFast(llvm::Value* static_storage_addr,
186 llvm::Value* field_offset_value,
187 llvm::Value* is_volatile_value,
188 JType field_jty);
189
190 void Expand_SPutFast(llvm::Value* static_storage_addr,
191 llvm::Value* field_offset_value,
192 llvm::Value* is_volatile_value,
193 llvm::Value* new_value,
194 JType field_jty);
195
196 llvm::Value* Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr);
197
198 llvm::Value* Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value);
199
200 llvm::Value*
201 Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value);
202
203 llvm::Value*
204 Expand_GetVirtualCalleeMethodObjAddrFast(llvm::Value* vtable_idx_value,
205 llvm::Value* this_addr);
206
207 llvm::Value* Expand_Invoke(llvm::CallInst& call_inst);
208
TDYa1274ec8ccd2012-08-11 07:04:57 -0700209 llvm::Value* Expand_DivRem(llvm::CallInst& call_inst, bool is_div, JType op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700210
211 void Expand_AllocaShadowFrame(llvm::Value* num_entry_value);
212
213 void Expand_SetShadowFrameEntry(llvm::Value* obj, llvm::Value* entry_idx);
214
215 void Expand_PopShadowFrame();
216
217 void Expand_UpdateDexPC(llvm::Value* dex_pc_value);
218
TDYa127a1b21852012-07-23 03:20:39 -0700219 //----------------------------------------------------------------------------
220 // Quick
221 //----------------------------------------------------------------------------
222
223 llvm::Value* Expand_FPCompare(llvm::Value* src1_value,
224 llvm::Value* src2_value,
225 bool gt_bias);
226
227 llvm::Value* Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value);
228
229 llvm::Value* EmitCompareResultSelection(llvm::Value* cmp_eq,
230 llvm::Value* cmp_lt);
231
TDYa127f71bf5a2012-07-29 20:09:52 -0700232 llvm::Value* EmitLoadConstantClass(uint32_t dex_pc, uint32_t type_idx);
TDYa1275a26d442012-07-26 18:58:38 -0700233 llvm::Value* EmitLoadStaticStorage(uint32_t dex_pc, uint32_t type_idx);
234
TDYa1275e869b62012-07-25 00:45:39 -0700235 llvm::Value* Expand_HLIGet(llvm::CallInst& call_inst, JType field_jty);
236 void Expand_HLIPut(llvm::CallInst& call_inst, JType field_jty);
237
TDYa1275a26d442012-07-26 18:58:38 -0700238 llvm::Value* Expand_HLSget(llvm::CallInst& call_inst, JType field_jty);
239 void Expand_HLSput(llvm::CallInst& call_inst, JType field_jty);
240
241 llvm::Value* Expand_HLArrayGet(llvm::CallInst& call_inst, JType field_jty);
242 void Expand_HLArrayPut(llvm::CallInst& call_inst, JType field_jty);
243
TDYa127f71bf5a2012-07-29 20:09:52 -0700244 llvm::Value* Expand_ConstString(llvm::CallInst& call_inst);
245 llvm::Value* Expand_ConstClass(llvm::CallInst& call_inst);
246
247 void Expand_MonitorEnter(llvm::CallInst& call_inst);
248 void Expand_MonitorExit(llvm::CallInst& call_inst);
249
250 void Expand_HLCheckCast(llvm::CallInst& call_inst);
251 llvm::Value* Expand_InstanceOf(llvm::CallInst& call_inst);
252
253 llvm::Value* Expand_NewInstance(llvm::CallInst& call_inst);
254
255 llvm::Value* Expand_HLInvoke(llvm::CallInst& call_inst);
256
257 llvm::Value* Expand_OptArrayLength(llvm::CallInst& call_inst);
258 llvm::Value* Expand_NewArray(llvm::CallInst& call_inst);
259 llvm::Value* Expand_HLFilledNewArray(llvm::CallInst& call_inst);
260 void Expand_HLFillArrayData(llvm::CallInst& call_inst);
261
262 llvm::Value* EmitAllocNewArray(uint32_t dex_pc,
263 llvm::Value* array_length_value,
264 uint32_t type_idx,
265 bool is_filled_new_array);
266
267 llvm::Value* EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
268 InvokeType invoke_type,
269 llvm::Value* this_addr,
270 uint32_t dex_pc,
271 bool is_fast_path);
272
TDYa1275e869b62012-07-25 00:45:39 -0700273 void EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr);
274
275 void EmitUpdateDexPC(uint32_t dex_pc);
276
277 void EmitGuard_DivZeroException(uint32_t dex_pc,
278 llvm::Value* denominator,
279 JType op_jty);
280
281 void EmitGuard_NullPointerException(uint32_t dex_pc,
282 llvm::Value* object);
283
284 void EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
285 llvm::Value* array,
286 llvm::Value* index);
287
288 void EmitGuard_ArrayException(uint32_t dex_pc,
289 llvm::Value* array,
290 llvm::Value* index);
291
292 llvm::FunctionType* GetFunctionType(uint32_t method_idx, bool is_static);
293
294 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
295
296 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
297 const char* postfix);
298
299 int32_t GetTryItemOffset(uint32_t dex_pc);
300
301 llvm::BasicBlock* GetLandingPadBasicBlock(uint32_t dex_pc);
302
303 llvm::BasicBlock* GetUnwindBasicBlock();
304
305 void EmitGuard_ExceptionLandingPad(uint32_t dex_pc);
306
307 void EmitBranchExceptionLandingPad(uint32_t dex_pc);
308
Logan Chien75e4b602012-07-23 14:24:12 -0700309 //----------------------------------------------------------------------------
310 // Expand Arithmetic Helper Intrinsics
311 //----------------------------------------------------------------------------
312
313 llvm::Value* Expand_IntegerShift(llvm::Value* src1_value,
314 llvm::Value* src2_value,
315 IntegerShiftKind kind,
316 JType op_jty);
317
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700318 public:
319 static char ID;
320
321 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb)
322 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
Logan Chiene5b8f8b2012-08-13 11:45:05 +0800323 context_(irb.getContext()), rtb_(irb.Runtime()),
324
325 // TODO: Initialize these fields correctly.
326 shadow_frame_(NULL), old_shadow_frame_(NULL), shadow_frame_size_(0),
327 compiler_(NULL), dex_file_(NULL), dex_cache_(NULL), code_item_(NULL),
Logan Chien67645d82012-08-17 09:10:54 +0800328 oat_compilation_unit_(NULL), method_idx_(-1u), func_(NULL),
329 changed_(false)
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700330 { }
331
332 bool runOnFunction(llvm::Function& func);
333
334 private:
Logan Chien67645d82012-08-17 09:10:54 +0800335 void InsertStackOverflowCheck(llvm::Function& func);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700336
337 llvm::Value* ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
338 llvm::CallInst& call_inst);
339
340};
341
342char GBCExpanderPass::ID = 0;
343
344bool GBCExpanderPass::runOnFunction(llvm::Function& func) {
TDYa127b672d1e2012-06-28 21:21:45 -0700345 // Runtime support or stub
346 if (func.getName().startswith("art_") || func.getName().startswith("Art")) {
347 return false;
348 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700349
Logan Chien67645d82012-08-17 09:10:54 +0800350 // Setup rewrite context
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700351 shadow_frame_ = NULL;
352 old_shadow_frame_ = NULL;
353 shadow_frame_size_ = 0;
TDYa1275e869b62012-07-25 00:45:39 -0700354 func_ = &func;
Logan Chien67645d82012-08-17 09:10:54 +0800355 changed_ = false; // Assume unchanged
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700356
Logan Chien67645d82012-08-17 09:10:54 +0800357 // Insert stack overflow check
358 InsertStackOverflowCheck(func); // TODO: Use intrinsic.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700359
Logan Chien67645d82012-08-17 09:10:54 +0800360 // Rewrite the intrinsics
361 RewriteFunction();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700362
363 VERIFY_LLVM_FUNCTION(func);
364
Logan Chien67645d82012-08-17 09:10:54 +0800365 return changed_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700366}
367
Logan Chien67645d82012-08-17 09:10:54 +0800368void GBCExpanderPass::RewriteBasicBlock(llvm::BasicBlock* original_block) {
369 llvm::BasicBlock* curr_basic_block = original_block;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700370
Logan Chien67645d82012-08-17 09:10:54 +0800371 llvm::BasicBlock::iterator inst_iter = original_block->begin();
372 llvm::BasicBlock::iterator inst_end = original_block->end();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700373
Logan Chien67645d82012-08-17 09:10:54 +0800374 while (inst_iter != inst_end) {
375 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst_iter);
376 IntrinsicHelper::IntrinsicId intr_id = IntrinsicHelper::UnknownId;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700377
Logan Chien67645d82012-08-17 09:10:54 +0800378 if (call_inst) {
379 llvm::Function* callee_func = call_inst->getCalledFunction();
380 intr_id = intrinsic_helper_.GetIntrinsicId(callee_func);
381 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700382
Logan Chien67645d82012-08-17 09:10:54 +0800383 if (intr_id == IntrinsicHelper::UnknownId) {
384 // This is not intrinsic call. Skip this instruction.
385 ++inst_iter;
386 continue;
387 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700388
Logan Chien67645d82012-08-17 09:10:54 +0800389 // Rewrite the intrinsic and change the function
390 changed_ = true;
391 irb_.SetInsertPoint(inst_iter);
392
393 // Expand the intrinsic
394 if (llvm::Value* new_value = ExpandIntrinsic(intr_id, *call_inst)) {
395 inst_iter->replaceAllUsesWith(new_value);
396 }
397
398 // Remove the old intrinsic call instruction
399 llvm::BasicBlock::iterator old_inst = inst_iter++;
400 old_inst->eraseFromParent();
401
402 // Splice the instruction to the new basic block
403 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
404 if (next_basic_block != curr_basic_block) {
405 next_basic_block->getInstList().splice(
406 irb_.GetInsertPoint(), curr_basic_block->getInstList(),
407 inst_iter, inst_end);
408 curr_basic_block = next_basic_block;
409 inst_end = curr_basic_block->end();
410 }
411 }
412}
413
414
415void GBCExpanderPass::RewriteFunction() {
416 size_t num_basic_blocks = func_->getBasicBlockList().size();
417 // NOTE: We are not using (bb_iter != bb_end) as the for-loop condition,
418 // because we will create new basic block while expanding the intrinsics.
419 // We only want to iterate through the input basic blocks.
420
421 for (llvm::Function::iterator bb_iter = func_->begin();
422 num_basic_blocks > 0; ++bb_iter, --num_basic_blocks) {
Shih-wei Liao627d8c42012-08-24 08:31:18 -0700423 // Set insert point to current basic block.
424 irb_.SetInsertPoint(bb_iter);
Logan Chien67645d82012-08-17 09:10:54 +0800425
426 // Rewrite the basic block
427 RewriteBasicBlock(bb_iter);
428
429 // Update the phi-instructions in the successor basic block
430 llvm::BasicBlock* last_block = irb_.GetInsertBlock();
431 if (last_block != bb_iter) {
432 UpdatePhiInstruction(bb_iter, last_block);
433 }
434 }
435}
436
437void GBCExpanderPass::UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
438 llvm::BasicBlock* new_basic_block) {
439 llvm::TerminatorInst* term_inst = new_basic_block->getTerminator();
440
441 if (!term_inst) {
442 return; // No terminating instruction in new_basic_block. Nothing to do.
443 }
444
445 // Iterate every succeeding basic block
446 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
447 succ_iter != succ_end; ++succ_iter) {
448 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
449
450 // Iterate every phi instructions in the succeeding basic block
451 for (llvm::BasicBlock::iterator
452 inst_iter = succ_basic_block->begin(),
453 inst_end = succ_basic_block->end();
454 inst_iter != inst_end; ++inst_iter) {
455 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
456
457 if (!phi) {
458 break; // Meet non-phi instruction. Done.
459 }
460
461 // Update the incoming block of this phi instruction
462 for (llvm::PHINode::block_iterator
463 ibb_iter = phi->block_begin(), ibb_end = phi->block_end();
464 ibb_iter != ibb_end; ++ibb_iter) {
465 if (*ibb_iter == old_basic_block) {
466 *ibb_iter = new_basic_block;
467 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700468 }
469 }
470 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700471}
472
473llvm::Value* GBCExpanderPass::ExpandToRuntime(runtime_support::RuntimeId rt,
474 llvm::CallInst& inst) {
475 // Some GBC intrinsic can directly replace with IBC runtime. "Directly" means
476 // the arguments passed to the GBC intrinsic are as the same as IBC runtime
477 // function, therefore only called function is needed to change.
478 unsigned num_args = inst.getNumArgOperands();
479
480 if (num_args <= 0) {
481 return irb_.CreateCall(irb_.GetRuntime(rt));
482 } else {
483 std::vector<llvm::Value*> args;
484 for (unsigned i = 0; i < num_args; i++) {
485 args.push_back(inst.getArgOperand(i));
486 }
487
488 return irb_.CreateCall(irb_.GetRuntime(rt), args);
489 }
490}
491
Logan Chien67645d82012-08-17 09:10:54 +0800492void
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700493GBCExpanderPass::EmitStackOverflowCheck(llvm::Instruction* first_non_alloca) {
494 llvm::Function* func = first_non_alloca->getParent()->getParent();
495 llvm::Module* module = func->getParent();
496
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700497 // Call llvm intrinsic function to get frame address.
498 llvm::Function* frameaddress =
499 llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
500
501 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
502 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
503
504 // Cast i8* to int
505 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
506
507 // Get thread.stack_end_
508 llvm::Value* stack_end =
509 irb_.Runtime().EmitLoadFromThreadOffset(Thread::StackEndOffset().Int32Value(),
510 irb_.getPtrEquivIntTy(),
511 kTBAARuntimeInfo);
512
513 // Check the frame address < thread.stack_end_ ?
514 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
515
516 llvm::BasicBlock* block_exception =
517 llvm::BasicBlock::Create(context_, "stack_overflow", func);
518
519 llvm::BasicBlock* block_continue =
520 llvm::BasicBlock::Create(context_, "stack_overflow_cont", func);
521
522 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
523
524 // If stack overflow, throw exception.
525 irb_.SetInsertPoint(block_exception);
526 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowStackOverflowException));
527
528 // Unwind.
529 llvm::Type* ret_type = func->getReturnType();
530 if (ret_type->isVoidTy()) {
531 irb_.CreateRetVoid();
532 } else {
533 // The return value is ignored when there's an exception. MethodCompiler
534 // returns zero value under the the corresponding return type in this case.
535 // GBCExpander returns LLVM undef value here for brevity
536 irb_.CreateRet(llvm::UndefValue::get(ret_type));
537 }
538
539 irb_.SetInsertPoint(block_continue);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700540}
541
542llvm::Value* GBCExpanderPass::EmitLoadDexCacheAddr(MemberOffset offset) {
543 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
544
545 return irb_.LoadFromObjectOffset(method_object_addr,
546 offset.Int32Value(),
547 irb_.getJObjectTy(),
548 kTBAAConstJObject);
549}
550
551llvm::Value*
552GBCExpanderPass::EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
553 llvm::Value* static_storage_dex_cache_addr =
554 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
555
556 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
557
558 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
559}
560
561llvm::Value*
562GBCExpanderPass::EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
563 llvm::Value* resolved_type_dex_cache_addr =
564 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
565
566 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
567
568 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
569}
570
571llvm::Value* GBCExpanderPass::
572EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
573 llvm::Value* resolved_method_dex_cache_addr =
574 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
575
576 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
577
578 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
579}
580
581llvm::Value* GBCExpanderPass::
582EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
583 llvm::Value* string_dex_cache_addr =
584 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
585
586 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
587
588 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
589}
590
591llvm::Value* GBCExpanderPass::EmitLoadMethodObjectAddr() {
592 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
593 return parent_func->arg_begin();
594}
595
596llvm::Value* GBCExpanderPass::EmitLoadArrayLength(llvm::Value* array) {
597 // Load array length
598 return irb_.LoadFromObjectOffset(array,
599 Array::LengthOffset().Int32Value(),
600 irb_.getJIntTy(),
601 kTBAAConstJObject);
602
603}
604
605llvm::Value*
606GBCExpanderPass::EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
607 llvm::Value* callee_method_object_field_addr =
608 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
609
610 return irb_.CreateLoad(callee_method_object_field_addr, kTBAAJRuntime);
611}
612
613llvm::Value* GBCExpanderPass::
614EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, llvm::Value* this_addr) {
615 // Load class object of *this* pointer
616 llvm::Value* class_object_addr =
617 irb_.LoadFromObjectOffset(this_addr,
618 Object::ClassOffset().Int32Value(),
619 irb_.getJObjectTy(),
620 kTBAAConstJObject);
621
622 // Load vtable address
623 llvm::Value* vtable_addr =
624 irb_.LoadFromObjectOffset(class_object_addr,
625 Class::VTableOffset().Int32Value(),
626 irb_.getJObjectTy(),
627 kTBAAConstJObject);
628
629 // Load callee method object
630 llvm::Value* vtable_idx_value =
631 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
632
633 llvm::Value* method_field_addr =
634 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
635
636 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
637}
638
639// Emit Array GetElementPtr
640llvm::Value* GBCExpanderPass::EmitArrayGEP(llvm::Value* array_addr,
641 llvm::Value* index_value,
642 JType elem_jty) {
643
644 int data_offset;
645 if (elem_jty == kLong || elem_jty == kDouble ||
646 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
647 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
648 } else {
649 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
650 }
651
652 llvm::Constant* data_offset_value =
653 irb_.getPtrEquivInt(data_offset);
654
655 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
656
657 llvm::Value* array_data_addr =
658 irb_.CreatePtrDisp(array_addr, data_offset_value,
659 elem_type->getPointerTo());
660
661 return irb_.CreateGEP(array_data_addr, index_value);
662}
663
664void GBCExpanderPass::Expand_TestSuspend(llvm::CallInst& call_inst) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700665 irb_.Runtime().EmitTestSuspend();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700666 return;
667}
668
TDYa1279a129452012-07-19 03:10:08 -0700669void GBCExpanderPass::Expand_MarkGCCard(llvm::CallInst& call_inst) {
TDYa1279a129452012-07-19 03:10:08 -0700670 irb_.Runtime().EmitMarkGCCard(call_inst.getArgOperand(0), call_inst.getArgOperand(1));
TDYa1279a129452012-07-19 03:10:08 -0700671 return;
672}
673
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700674llvm::Value* GBCExpanderPass::Expand_GetException() {
675 // Get thread-local exception field address
676 llvm::Value* exception_object_addr =
677 irb_.Runtime().EmitLoadFromThreadOffset(Thread::ExceptionOffset().Int32Value(),
678 irb_.getJObjectTy(),
679 kTBAAJRuntime);
680
681 // Set thread-local exception field address to NULL
682 irb_.Runtime().EmitStoreToThreadOffset(Thread::ExceptionOffset().Int32Value(),
683 irb_.getJNull(),
684 kTBAAJRuntime);
685
686 return exception_object_addr;
687}
688
689llvm::Value*
690GBCExpanderPass::Expand_LoadStringFromDexCache(llvm::Value* string_idx_value) {
691 uint32_t string_idx =
692 llvm::cast<llvm::ConstantInt>(string_idx_value)->getZExtValue();
693
694 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
695
696 return irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
697}
698
699llvm::Value*
700GBCExpanderPass::Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value) {
701 uint32_t type_idx =
702 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
703
704 llvm::Value* type_field_addr =
705 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
706
707 return irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
708}
709
710void GBCExpanderPass::Expand_LockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700711 rtb_.EmitLockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700712 return;
713}
714
715void GBCExpanderPass::Expand_UnlockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700716 rtb_.EmitUnlockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700717 return;
718}
719
720llvm::Value* GBCExpanderPass::Expand_ArrayGet(llvm::Value* array_addr,
721 llvm::Value* index_value,
722 JType elem_jty) {
723 llvm::Value* array_elem_addr =
724 EmitArrayGEP(array_addr, index_value, elem_jty);
725
726 return irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
727}
728
729void GBCExpanderPass::Expand_ArrayPut(llvm::Value* new_value,
730 llvm::Value* array_addr,
731 llvm::Value* index_value,
732 JType elem_jty) {
733 llvm::Value* array_elem_addr =
734 EmitArrayGEP(array_addr, index_value, elem_jty);
735
736 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
737
738 return;
739}
740
741void GBCExpanderPass::Expand_FilledNewArray(llvm::CallInst& call_inst) {
742 // Most of the codes refer to MethodCompiler::EmitInsn_FilledNewArray
743 llvm::Value* array = call_inst.getArgOperand(0);
744
745 uint32_t element_jty =
746 llvm::cast<llvm::ConstantInt>(call_inst.getArgOperand(1))->getZExtValue();
747
748 DCHECK(call_inst.getNumArgOperands() > 2);
749 unsigned num_elements = (call_inst.getNumArgOperands() - 2);
750
751 bool is_elem_int_ty = (static_cast<JType>(element_jty) == kInt);
752
753 uint32_t alignment;
754 llvm::Constant* elem_size;
755 llvm::PointerType* field_type;
756
757 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
758 // as the element, thus we are only checking 2 cases: primitive int and
759 // non-primitive type.
760 if (is_elem_int_ty) {
761 alignment = sizeof(int32_t);
762 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
763 field_type = irb_.getJIntTy()->getPointerTo();
764 } else {
765 alignment = irb_.getSizeOfPtrEquivInt();
766 elem_size = irb_.getSizeOfPtrEquivIntValue();
767 field_type = irb_.getJObjectTy()->getPointerTo();
768 }
769
770 llvm::Value* data_field_offset =
771 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
772
773 llvm::Value* data_field_addr =
774 irb_.CreatePtrDisp(array, data_field_offset, field_type);
775
776 for (unsigned i = 0; i < num_elements; ++i) {
777 // Values to fill the array begin at the 3rd argument
778 llvm::Value* reg_value = call_inst.getArgOperand(2 + i);
779
780 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
781
782 data_field_addr =
783 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
784 }
785
786 return;
787}
788
789llvm::Value* GBCExpanderPass::Expand_IGetFast(llvm::Value* field_offset_value,
790 llvm::Value* /*is_volatile_value*/,
791 llvm::Value* object_addr,
792 JType field_jty) {
793 int field_offset =
794 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
795
796 DCHECK_GE(field_offset, 0);
797
798 llvm::PointerType* field_type =
799 irb_.getJType(field_jty, kField)->getPointerTo();
800
801 field_offset_value = irb_.getPtrEquivInt(field_offset);
802
803 llvm::Value* field_addr =
804 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
805
806 // TODO: Check is_volatile. We need to generate atomic load instruction
807 // when is_volatile is true.
808 return irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
809}
810
811void GBCExpanderPass::Expand_IPutFast(llvm::Value* field_offset_value,
812 llvm::Value* /* is_volatile_value */,
813 llvm::Value* object_addr,
814 llvm::Value* new_value,
815 JType field_jty) {
816 int field_offset =
817 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
818
819 DCHECK_GE(field_offset, 0);
820
821 llvm::PointerType* field_type =
822 irb_.getJType(field_jty, kField)->getPointerTo();
823
824 field_offset_value = irb_.getPtrEquivInt(field_offset);
825
826 llvm::Value* field_addr =
827 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
828
829 // TODO: Check is_volatile. We need to generate atomic store instruction
830 // when is_volatile is true.
831 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
832
833 return;
834}
835
836llvm::Value* GBCExpanderPass::Expand_SGetFast(llvm::Value* static_storage_addr,
837 llvm::Value* field_offset_value,
838 llvm::Value* /*is_volatile_value*/,
839 JType field_jty) {
840 int field_offset =
841 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
842
843 DCHECK_GE(field_offset, 0);
844
845 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
846
847 llvm::Value* static_field_addr =
848 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
849 irb_.getJType(field_jty, kField)->getPointerTo());
850
851 // TODO: Check is_volatile. We need to generate atomic store instruction
852 // when is_volatile is true.
853 return irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
854}
855
856void GBCExpanderPass::Expand_SPutFast(llvm::Value* static_storage_addr,
857 llvm::Value* field_offset_value,
858 llvm::Value* /* is_volatile_value */,
859 llvm::Value* new_value,
860 JType field_jty) {
861 int field_offset =
862 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
863
864 DCHECK_GE(field_offset, 0);
865
866 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
867
868 llvm::Value* static_field_addr =
869 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
870 irb_.getJType(field_jty, kField)->getPointerTo());
871
872 // TODO: Check is_volatile. We need to generate atomic store instruction
873 // when is_volatile is true.
874 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
875
876 return;
877}
878
879llvm::Value*
880GBCExpanderPass::Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr) {
881 return irb_.LoadFromObjectOffset(method_object_addr,
882 Method::DeclaringClassOffset().Int32Value(),
883 irb_.getJObjectTy(),
884 kTBAAConstJObject);
885}
886
887llvm::Value*
888GBCExpanderPass::Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value) {
889 uint32_t type_idx =
890 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
891
892 llvm::Value* storage_field_addr =
893 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
894
895 return irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
896}
897
898llvm::Value*
899GBCExpanderPass::Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value) {
900 uint32_t callee_method_idx =
901 llvm::cast<llvm::ConstantInt>(callee_method_idx_value)->getZExtValue();
902
903 return EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
904}
905
906llvm::Value* GBCExpanderPass::Expand_GetVirtualCalleeMethodObjAddrFast(
907 llvm::Value* vtable_idx_value,
908 llvm::Value* this_addr) {
909 int vtable_idx =
910 llvm::cast<llvm::ConstantInt>(vtable_idx_value)->getSExtValue();
911
912 return EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
913}
914
915llvm::Value* GBCExpanderPass::Expand_Invoke(llvm::CallInst& call_inst) {
916 // Most of the codes refer to MethodCompiler::EmitInsn_Invoke
917 llvm::Value* callee_method_object_addr = call_inst.getArgOperand(0);
918 unsigned num_args = call_inst.getNumArgOperands();
919 llvm::Type* ret_type = call_inst.getType();
920
921 // Determine the function type of the callee method
922 std::vector<llvm::Type*> args_type;
923 std::vector<llvm::Value*> args;
924 for (unsigned i = 0; i < num_args; i++) {
925 args.push_back(call_inst.getArgOperand(i));
926 args_type.push_back(args[i]->getType());
927 }
928
929 llvm::FunctionType* callee_method_type =
930 llvm::FunctionType::get(ret_type, args_type, false);
931
932 llvm::Value* code_addr =
933 irb_.LoadFromObjectOffset(callee_method_object_addr,
934 Method::GetCodeOffset().Int32Value(),
935 callee_method_type->getPointerTo(),
936 kTBAAJRuntime);
937
938 // Invoke callee
939 llvm::Value* retval = irb_.CreateCall(code_addr, args);
940
941 return retval;
942}
943
TDYa1274ec8ccd2012-08-11 07:04:57 -0700944llvm::Value* GBCExpanderPass::Expand_DivRem(llvm::CallInst& call_inst,
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700945 bool is_div, JType op_jty) {
TDYa1274ec8ccd2012-08-11 07:04:57 -0700946 llvm::Value* dividend = call_inst.getArgOperand(0);
947 llvm::Value* divisor = call_inst.getArgOperand(1);
948#if defined(ART_USE_QUICK_COMPILER)
949 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
950 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
951#endif
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700952 // Most of the codes refer to MethodCompiler::EmitIntDivRemResultComputation
953
954 // Check the special case: MININT / -1 = MININT
955 // That case will cause overflow, which is undefined behavior in llvm.
956 // So we check the divisor is -1 or not, if the divisor is -1, we do
957 // the special path to avoid undefined behavior.
958 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
959 llvm::Value* zero = irb_.getJZero(op_jty);
960 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
961
TDYa1275e869b62012-07-25 00:45:39 -0700962 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700963 llvm::BasicBlock* eq_neg_one = llvm::BasicBlock::Create(context_, "", parent);
964 llvm::BasicBlock* ne_neg_one = llvm::BasicBlock::Create(context_, "", parent);
965 llvm::BasicBlock* neg_one_cont =
966 llvm::BasicBlock::Create(context_, "", parent);
967
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700968 llvm::Value* is_equal_neg_one = irb_.CreateICmpEQ(divisor, neg_one);
969 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
970
971 // If divisor == -1
972 irb_.SetInsertPoint(eq_neg_one);
973 llvm::Value* eq_result;
974 if (is_div) {
975 // We can just change from "dividend div -1" to "neg dividend". The sub
976 // don't care the sign/unsigned because of two's complement representation.
977 // And the behavior is what we want:
978 // -(2^n) (2^n)-1
979 // MININT < k <= MAXINT -> mul k -1 = -k
980 // MININT == k -> mul k -1 = k
981 //
982 // LLVM use sub to represent 'neg'
983 eq_result = irb_.CreateSub(zero, dividend);
984 } else {
985 // Everything modulo -1 will be 0.
986 eq_result = zero;
987 }
988 irb_.CreateBr(neg_one_cont);
989
990 // If divisor != -1, just do the division.
991 irb_.SetInsertPoint(ne_neg_one);
992 llvm::Value* ne_result;
993 if (is_div) {
994 ne_result = irb_.CreateSDiv(dividend, divisor);
995 } else {
996 ne_result = irb_.CreateSRem(dividend, divisor);
997 }
998 irb_.CreateBr(neg_one_cont);
999
1000 irb_.SetInsertPoint(neg_one_cont);
1001 llvm::PHINode* result = irb_.CreatePHI(op_type, 2);
1002 result->addIncoming(eq_result, eq_neg_one);
1003 result->addIncoming(ne_result, ne_neg_one);
1004
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001005 return result;
1006}
1007
1008void GBCExpanderPass::Expand_AllocaShadowFrame(llvm::Value* num_entry_value) {
1009 // Most of the codes refer to MethodCompiler::EmitPrologueAllocShadowFrame and
1010 // MethodCompiler::EmitPushShadowFrame
1011 shadow_frame_size_ =
1012 llvm::cast<llvm::ConstantInt>(num_entry_value)->getZExtValue();
1013
1014 llvm::StructType* shadow_frame_type =
1015 irb_.getShadowFrameTy(shadow_frame_size_);
1016
1017 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
1018
1019 // Alloca a pointer to old shadow frame
1020 old_shadow_frame_ =
1021 irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
1022
1023 // Zero-initialization of the shadow frame table
1024 llvm::Value* shadow_frame_table =
1025 irb_.CreateConstGEP2_32(shadow_frame_, 0, 1);
1026 llvm::Type* table_type = shadow_frame_type->getElementType(1);
1027
1028 llvm::ConstantAggregateZero* zero_initializer =
1029 llvm::ConstantAggregateZero::get(table_type);
1030
1031 irb_.CreateStore(zero_initializer, shadow_frame_table, kTBAAShadowFrame);
1032
1033 // Push the shadow frame
1034 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1035
1036 // Push the shadow frame
1037 llvm::Value* shadow_frame_upcast =
1038 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
1039
1040 llvm::Value* result = rtb_.EmitPushShadowFrame(shadow_frame_upcast,
1041 method_object_addr,
1042 shadow_frame_size_);
1043
1044 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
1045
1046 return;
1047}
1048
1049void GBCExpanderPass::Expand_SetShadowFrameEntry(llvm::Value* obj,
1050 llvm::Value* entry_idx) {
1051 DCHECK(shadow_frame_ != NULL);
1052
1053 llvm::Value* gep_index[] = {
1054 irb_.getInt32(0), // No pointer displacement
1055 irb_.getInt32(1), // SIRT
1056 entry_idx // Pointer field
1057 };
1058
1059 llvm::Value* entry_addr = irb_.CreateGEP(shadow_frame_, gep_index);
1060 irb_.CreateStore(obj, entry_addr, kTBAAShadowFrame);
1061 return;
1062}
1063
1064void GBCExpanderPass::Expand_PopShadowFrame() {
1065 rtb_.EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
1066 return;
1067}
1068
1069void GBCExpanderPass::Expand_UpdateDexPC(llvm::Value* dex_pc_value) {
1070 irb_.StoreToObjectOffset(shadow_frame_,
1071 ShadowFrame::DexPCOffset(),
1072 dex_pc_value,
1073 kTBAAShadowFrame);
1074 return;
1075}
1076
Logan Chien67645d82012-08-17 09:10:54 +08001077void GBCExpanderPass::InsertStackOverflowCheck(llvm::Function& func) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001078 // DexLang generates all alloca instruction in the first basic block of the
1079 // FUNC and also there's no any alloca instructions after the first non-alloca
1080 // instruction
1081
Logan Chien67645d82012-08-17 09:10:54 +08001082 llvm::BasicBlock* first_basic_block = &func.front();
1083
1084 // Look for first non-alloca instruction
1085 llvm::BasicBlock::iterator first_non_alloca = first_basic_block->begin();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001086 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1087 ++first_non_alloca;
1088 }
1089
Logan Chien67645d82012-08-17 09:10:54 +08001090 irb_.SetInsertPoint(first_non_alloca);
1091
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001092 // Insert stack overflow check codes before first_non_alloca (i.e., after all
1093 // alloca instructions)
Logan Chien67645d82012-08-17 09:10:54 +08001094 EmitStackOverflowCheck(&*first_non_alloca);
1095
1096 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
1097 if (next_basic_block != first_basic_block) {
1098 // Splice the rest of the instruction to the continuing basic block
1099 next_basic_block->getInstList().splice(
1100 irb_.GetInsertPoint(), first_basic_block->getInstList(),
1101 first_non_alloca, first_basic_block->end());
1102
1103 // Rewrite the basic block
1104 RewriteBasicBlock(next_basic_block);
1105
1106 // Update the phi-instructions in the successor basic block
1107 UpdatePhiInstruction(first_basic_block, irb_.GetInsertBlock());
1108 }
1109
1110 // We have changed the basic block
1111 changed_ = true;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001112}
1113
TDYa1275e869b62012-07-25 00:45:39 -07001114// ==== High-level intrinsic expander ==========================================
1115
TDYa127a1b21852012-07-23 03:20:39 -07001116llvm::Value* GBCExpanderPass::Expand_FPCompare(llvm::Value* src1_value,
1117 llvm::Value* src2_value,
1118 bool gt_bias) {
1119 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1120 llvm::Value* cmp_lt;
1121
1122 if (gt_bias) {
1123 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1124 } else {
1125 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1126 }
1127
1128 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1129}
1130
1131llvm::Value* GBCExpanderPass::Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value) {
1132 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1133 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1134
1135 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1136}
1137
1138llvm::Value* GBCExpanderPass::EmitCompareResultSelection(llvm::Value* cmp_eq,
1139 llvm::Value* cmp_lt) {
1140
1141 llvm::Constant* zero = irb_.getJInt(0);
1142 llvm::Constant* pos1 = irb_.getJInt(1);
1143 llvm::Constant* neg1 = irb_.getJInt(-1);
1144
1145 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1146 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1147
1148 return result_eq;
1149}
1150
Logan Chien75e4b602012-07-23 14:24:12 -07001151llvm::Value* GBCExpanderPass::Expand_IntegerShift(llvm::Value* src1_value,
1152 llvm::Value* src2_value,
1153 IntegerShiftKind kind,
1154 JType op_jty) {
1155 DCHECK(op_jty == kInt || op_jty == kLong);
1156
1157 // Mask and zero-extend RHS properly
1158 if (op_jty == kInt) {
1159 src2_value = irb_.CreateAnd(src2_value, 0x1f);
1160 } else {
1161 llvm::Value* masked_src2_value = irb_.CreateAnd(src2_value, 0x3f);
1162 src2_value = irb_.CreateZExt(masked_src2_value, irb_.getJLongTy());
1163 }
1164
1165 // Create integer shift llvm instruction
1166 switch (kind) {
1167 case kIntegerSHL:
1168 return irb_.CreateShl(src1_value, src2_value);
1169
1170 case kIntegerSHR:
1171 return irb_.CreateAShr(src1_value, src2_value);
1172
1173 case kIntegerUSHR:
1174 return irb_.CreateLShr(src1_value, src2_value);
1175
1176 default:
1177 LOG(FATAL) << "Unknown integer shift kind: " << kind;
1178 return NULL;
1179 }
1180}
1181
TDYa1275a26d442012-07-26 18:58:38 -07001182llvm::Value* GBCExpanderPass::Expand_HLArrayGet(llvm::CallInst& call_inst,
1183 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001184 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1185 llvm::Value* array_addr = call_inst.getArgOperand(1);
1186 llvm::Value* index_value = call_inst.getArgOperand(2);
1187
1188 // TODO: opt_flags
1189 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
1190
1191 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1192
1193 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
1194
1195 switch (elem_jty) {
1196 case kVoid:
1197 break;
1198
1199 case kBoolean:
1200 case kChar:
1201 array_elem_value = irb_.CreateZExt(array_elem_value, irb_.getJType(elem_jty, kReg));
1202 break;
1203
1204 case kByte:
1205 case kShort:
1206 array_elem_value = irb_.CreateSExt(array_elem_value, irb_.getJType(elem_jty, kReg));
1207 break;
1208
1209 case kInt:
1210 case kLong:
1211 case kFloat:
1212 case kDouble:
1213 case kObject:
1214 break;
1215
1216 default:
1217 LOG(FATAL) << "Unknown java type: " << elem_jty;
1218 }
1219
1220 return array_elem_value;
1221}
1222
1223
1224void GBCExpanderPass::Expand_HLArrayPut(llvm::CallInst& call_inst,
1225 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001226 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1227 llvm::Value* new_value = call_inst.getArgOperand(1);
1228 llvm::Value* array_addr = call_inst.getArgOperand(2);
1229 llvm::Value* index_value = call_inst.getArgOperand(3);
1230
1231 // TODO: opt_flags
1232 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
1233
1234 switch (elem_jty) {
1235 case kVoid:
1236 break;
1237
1238 case kBoolean:
1239 case kChar:
1240 new_value = irb_.CreateTrunc(new_value, irb_.getJType(elem_jty, kArray));
1241 break;
1242
1243 case kInt:
1244 case kLong:
1245 case kFloat:
1246 case kDouble:
1247 case kObject:
1248 break;
1249
1250 default:
1251 LOG(FATAL) << "Unknown java type: " << elem_jty;
1252 }
1253
1254 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1255
1256 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
1257 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::CheckPutArrayElement);
1258
1259 irb_.CreateCall2(runtime_func, new_value, array_addr);
1260
1261 EmitGuard_ExceptionLandingPad(dex_pc);
1262
1263 EmitMarkGCCard(new_value, array_addr);
1264 }
1265
1266 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1267
1268 return;
1269}
1270
TDYa1275e869b62012-07-25 00:45:39 -07001271llvm::Value* GBCExpanderPass::Expand_HLIGet(llvm::CallInst& call_inst,
1272 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001273 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1274 llvm::Value* object_addr = call_inst.getArgOperand(1);
1275 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(2));
1276
1277 // TODO: opt_flags
1278 EmitGuard_NullPointerException(dex_pc, object_addr);
1279
1280 llvm::Value* field_value;
1281
1282 int field_offset;
1283 bool is_volatile;
1284 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
1285 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
1286
1287 if (!is_fast_path) {
1288 llvm::Function* runtime_func;
1289
1290 if (field_jty == kObject) {
1291 runtime_func = irb_.GetRuntime(runtime_support::GetObjectInstance);
1292 } else if (field_jty == kLong || field_jty == kDouble) {
1293 runtime_func = irb_.GetRuntime(runtime_support::Get64Instance);
1294 } else {
1295 runtime_func = irb_.GetRuntime(runtime_support::Get32Instance);
1296 }
1297
1298 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
1299
1300 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1301
1302 EmitUpdateDexPC(dex_pc);
1303
1304 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
1305 method_object_addr, object_addr);
1306
1307 EmitGuard_ExceptionLandingPad(dex_pc);
1308
1309 } else {
1310 DCHECK_GE(field_offset, 0);
1311
1312 llvm::PointerType* field_type =
1313 irb_.getJType(field_jty, kField)->getPointerTo();
1314
1315 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
1316
1317 llvm::Value* field_addr =
1318 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1319
1320 // TODO: Check is_volatile. We need to generate atomic load instruction
1321 // when is_volatile is true.
1322 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
1323 }
1324
1325 if (field_jty == kFloat || field_jty == kDouble) {
TDYa1275a26d442012-07-26 18:58:38 -07001326 field_value = irb_.CreateBitCast(field_value, irb_.getJType(field_jty, kAccurate));
TDYa1275e869b62012-07-25 00:45:39 -07001327 }
1328
1329 return field_value;
1330}
1331
1332void GBCExpanderPass::Expand_HLIPut(llvm::CallInst& call_inst,
1333 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001334 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1335 llvm::Value* object_addr = call_inst.getArgOperand(1);
1336 llvm::Value* new_value = call_inst.getArgOperand(2);
1337 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(3));
1338
1339 if (field_jty == kFloat || field_jty == kDouble) {
TDYa1275a26d442012-07-26 18:58:38 -07001340 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty, kField));
TDYa1275e869b62012-07-25 00:45:39 -07001341 }
1342
1343 // TODO: opt_flags
1344 EmitGuard_NullPointerException(dex_pc, object_addr);
1345
1346 int field_offset;
1347 bool is_volatile;
1348 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
1349 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
1350
1351 if (!is_fast_path) {
1352 llvm::Function* runtime_func;
1353
1354 if (field_jty == kObject) {
1355 runtime_func = irb_.GetRuntime(runtime_support::SetObjectInstance);
1356 } else if (field_jty == kLong || field_jty == kDouble) {
1357 runtime_func = irb_.GetRuntime(runtime_support::Set64Instance);
1358 } else {
1359 runtime_func = irb_.GetRuntime(runtime_support::Set32Instance);
1360 }
1361
1362 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1363
1364 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1365
1366 EmitUpdateDexPC(dex_pc);
1367
1368 irb_.CreateCall4(runtime_func, field_idx_value,
1369 method_object_addr, object_addr, new_value);
1370
1371 EmitGuard_ExceptionLandingPad(dex_pc);
1372
1373 } else {
1374 DCHECK_GE(field_offset, 0);
1375
1376 llvm::PointerType* field_type =
1377 irb_.getJType(field_jty, kField)->getPointerTo();
1378
1379 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
1380
1381 llvm::Value* field_addr =
1382 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1383
1384 // TODO: Check is_volatile. We need to generate atomic store instruction
1385 // when is_volatile is true.
1386 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1387
1388 if (field_jty == kObject) { // If put an object, mark the GC card table.
1389 EmitMarkGCCard(new_value, object_addr);
1390 }
1391 }
1392
1393 return;
1394}
1395
TDYa127f71bf5a2012-07-29 20:09:52 -07001396llvm::Value* GBCExpanderPass::EmitLoadConstantClass(uint32_t dex_pc,
1397 uint32_t type_idx) {
1398 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1399 *dex_file_, type_idx)) {
1400 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1401
1402 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1403
1404 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1405
1406 llvm::Function* runtime_func =
1407 irb_.GetRuntime(runtime_support::InitializeTypeAndVerifyAccess);
1408
1409 EmitUpdateDexPC(dex_pc);
1410
1411 llvm::Value* type_object_addr =
1412 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1413
1414 EmitGuard_ExceptionLandingPad(dex_pc);
1415
1416 return type_object_addr;
1417
1418 } else {
1419 // Try to load the class (type) object from the test cache.
1420 llvm::Value* type_field_addr =
1421 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1422
1423 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
1424
1425 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1426 return type_object_addr;
1427 }
1428
1429 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1430
1431 // Test whether class (type) object is in the dex cache or not
1432 llvm::Value* equal_null =
1433 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1434
1435 llvm::BasicBlock* block_cont =
1436 CreateBasicBlockWithDexPC(dex_pc, "cont");
1437
1438 llvm::BasicBlock* block_load_class =
1439 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1440
1441 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
1442
1443 // Failback routine to load the class object
1444 irb_.SetInsertPoint(block_load_class);
1445
1446 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeType);
1447
1448 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1449
1450 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1451
1452 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1453
1454 EmitUpdateDexPC(dex_pc);
1455
1456 llvm::Value* loaded_type_object_addr =
1457 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1458
1459 EmitGuard_ExceptionLandingPad(dex_pc);
1460
1461 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1462
1463 irb_.CreateBr(block_cont);
1464
1465 // Now the class object must be loaded
1466 irb_.SetInsertPoint(block_cont);
1467
1468 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1469
1470 phi->addIncoming(type_object_addr, block_original);
1471 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1472
1473 return phi;
1474 }
1475}
1476
TDYa1275a26d442012-07-26 18:58:38 -07001477llvm::Value* GBCExpanderPass::EmitLoadStaticStorage(uint32_t dex_pc,
1478 uint32_t type_idx) {
1479 llvm::BasicBlock* block_load_static =
1480 CreateBasicBlockWithDexPC(dex_pc, "load_static");
1481
1482 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1483
1484 // Load static storage from dex cache
1485 llvm::Value* storage_field_addr =
1486 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1487
1488 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
1489
1490 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1491
1492 // Test: Is the static storage of this class initialized?
1493 llvm::Value* equal_null =
1494 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
1495
1496 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
1497
1498 // Failback routine to load the class object
1499 irb_.SetInsertPoint(block_load_static);
1500
1501 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeStaticStorage);
1502
1503 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1504
1505 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1506
1507 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1508
1509 EmitUpdateDexPC(dex_pc);
1510
1511 llvm::Value* loaded_storage_object_addr =
1512 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1513
1514 EmitGuard_ExceptionLandingPad(dex_pc);
1515
1516 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
1517
1518 irb_.CreateBr(block_cont);
1519
1520 // Now the class object must be loaded
1521 irb_.SetInsertPoint(block_cont);
1522
1523 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1524
1525 phi->addIncoming(storage_object_addr, block_original);
1526 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
1527
1528 return phi;
1529}
1530
1531llvm::Value* GBCExpanderPass::Expand_HLSget(llvm::CallInst& call_inst,
1532 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001533 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1534 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1535
1536 int field_offset;
1537 int ssb_index;
1538 bool is_referrers_class;
1539 bool is_volatile;
1540
1541 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
1542 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1543 is_referrers_class, is_volatile, false);
1544
1545 llvm::Value* static_field_value;
1546
1547 if (!is_fast_path) {
1548 llvm::Function* runtime_func;
1549
1550 if (field_jty == kObject) {
1551 runtime_func = irb_.GetRuntime(runtime_support::GetObjectStatic);
1552 } else if (field_jty == kLong || field_jty == kDouble) {
1553 runtime_func = irb_.GetRuntime(runtime_support::Get64Static);
1554 } else {
1555 runtime_func = irb_.GetRuntime(runtime_support::Get32Static);
1556 }
1557
1558 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1559
1560 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1561
1562 EmitUpdateDexPC(dex_pc);
1563
1564 static_field_value =
1565 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
1566
1567 EmitGuard_ExceptionLandingPad(dex_pc);
1568
1569 } else {
1570 DCHECK_GE(field_offset, 0);
1571
1572 llvm::Value* static_storage_addr = NULL;
1573
1574 if (is_referrers_class) {
1575 // Fast path, static storage base is this method's class
1576 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1577
1578 static_storage_addr =
1579 irb_.LoadFromObjectOffset(method_object_addr,
1580 Method::DeclaringClassOffset().Int32Value(),
1581 irb_.getJObjectTy(),
1582 kTBAAConstJObject);
1583 } else {
1584 // Medium path, static storage base in a different class which
1585 // requires checks that the other class is initialized
1586 DCHECK_GE(ssb_index, 0);
1587 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1588 }
1589
1590 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1591
1592 llvm::Value* static_field_addr =
1593 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1594 irb_.getJType(field_jty, kField)->getPointerTo());
1595
1596 // TODO: Check is_volatile. We need to generate atomic load instruction
1597 // when is_volatile is true.
1598 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
1599 }
1600
1601 if (field_jty == kFloat || field_jty == kDouble) {
1602 static_field_value =
1603 irb_.CreateBitCast(static_field_value, irb_.getJType(field_jty, kAccurate));
1604 }
1605
1606 return static_field_value;
1607}
1608
1609void GBCExpanderPass::Expand_HLSput(llvm::CallInst& call_inst,
1610 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001611 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1612 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1613 llvm::Value* new_value = call_inst.getArgOperand(1);
1614
1615 if (field_jty == kFloat || field_jty == kDouble) {
1616 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty, kField));
1617 }
1618
1619 int field_offset;
1620 int ssb_index;
1621 bool is_referrers_class;
1622 bool is_volatile;
1623
1624 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
1625 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1626 is_referrers_class, is_volatile, true);
1627
1628 if (!is_fast_path) {
1629 llvm::Function* runtime_func;
1630
1631 if (field_jty == kObject) {
1632 runtime_func = irb_.GetRuntime(runtime_support::SetObjectStatic);
1633 } else if (field_jty == kLong || field_jty == kDouble) {
1634 runtime_func = irb_.GetRuntime(runtime_support::Set64Static);
1635 } else {
1636 runtime_func = irb_.GetRuntime(runtime_support::Set32Static);
1637 }
1638
1639 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1640
1641 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1642
1643 EmitUpdateDexPC(dex_pc);
1644
1645 irb_.CreateCall3(runtime_func, field_idx_value,
1646 method_object_addr, new_value);
1647
1648 EmitGuard_ExceptionLandingPad(dex_pc);
1649
1650 } else {
1651 DCHECK_GE(field_offset, 0);
1652
1653 llvm::Value* static_storage_addr = NULL;
1654
1655 if (is_referrers_class) {
1656 // Fast path, static storage base is this method's class
1657 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1658
1659 static_storage_addr =
1660 irb_.LoadFromObjectOffset(method_object_addr,
1661 Method::DeclaringClassOffset().Int32Value(),
1662 irb_.getJObjectTy(),
1663 kTBAAConstJObject);
1664 } else {
1665 // Medium path, static storage base in a different class which
1666 // requires checks that the other class is initialized
1667 DCHECK_GE(ssb_index, 0);
1668 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1669 }
1670
1671 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1672
1673 llvm::Value* static_field_addr =
1674 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1675 irb_.getJType(field_jty, kField)->getPointerTo());
1676
1677 // TODO: Check is_volatile. We need to generate atomic store instruction
1678 // when is_volatile is true.
1679 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1680
1681 if (field_jty == kObject) { // If put an object, mark the GC card table.
1682 EmitMarkGCCard(new_value, static_storage_addr);
1683 }
1684 }
1685
1686 return;
1687}
1688
TDYa127f71bf5a2012-07-29 20:09:52 -07001689llvm::Value* GBCExpanderPass::Expand_ConstString(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001690 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1691 uint32_t string_idx = LV2UInt(call_inst.getArgOperand(0));
1692
1693 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1694
1695 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
1696
1697 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) {
1698 llvm::BasicBlock* block_str_exist =
1699 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1700
1701 llvm::BasicBlock* block_str_resolve =
1702 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1703
1704 llvm::BasicBlock* block_cont =
1705 CreateBasicBlockWithDexPC(dex_pc, "str_cont");
1706
1707 // Test: Is the string resolved and in the dex cache?
1708 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1709
1710 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
1711
1712 // String is resolved, go to next basic block.
1713 irb_.SetInsertPoint(block_str_exist);
1714 irb_.CreateBr(block_cont);
1715
1716 // String is not resolved yet, resolve it now.
1717 irb_.SetInsertPoint(block_str_resolve);
1718
1719 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::ResolveString);
1720
1721 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1722
1723 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1724
1725 EmitUpdateDexPC(dex_pc);
1726
1727 llvm::Value* result = irb_.CreateCall2(runtime_func, method_object_addr,
1728 string_idx_value);
1729
1730 EmitGuard_ExceptionLandingPad(dex_pc);
1731
1732 llvm::BasicBlock* block_pre_cont = irb_.GetInsertBlock();
1733
1734 irb_.SetInsertPoint(block_cont);
1735
1736 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1737
1738 phi->addIncoming(string_addr, block_str_exist);
1739 phi->addIncoming(result, block_pre_cont);
1740
1741 string_addr = phi;
1742 }
1743
1744 return string_addr;
1745}
1746
1747llvm::Value* GBCExpanderPass::Expand_ConstClass(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001748 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1749 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1750
1751 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
1752
1753 return type_object_addr;
1754}
1755
1756void GBCExpanderPass::Expand_MonitorEnter(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001757 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1758 llvm::Value* object_addr = call_inst.getArgOperand(1);
1759
1760 // TODO: opt_flags
1761 EmitGuard_NullPointerException(dex_pc, object_addr);
1762
1763 irb_.Runtime().EmitLockObject(object_addr);
1764
1765 return;
1766}
1767
1768void GBCExpanderPass::Expand_MonitorExit(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001769 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1770 llvm::Value* object_addr = call_inst.getArgOperand(1);
1771
1772 // TODO: opt_flags
1773 EmitGuard_NullPointerException(dex_pc, object_addr);
1774
1775 EmitUpdateDexPC(dex_pc);
1776
1777 irb_.Runtime().EmitUnlockObject(object_addr);
1778
1779 EmitGuard_ExceptionLandingPad(dex_pc);
1780
1781 return;
1782}
1783
1784void GBCExpanderPass::Expand_HLCheckCast(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001785 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1786 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1787 llvm::Value* object_addr = call_inst.getArgOperand(1);
1788
1789 llvm::BasicBlock* block_test_class =
1790 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1791
1792 llvm::BasicBlock* block_test_sub_class =
1793 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1794
1795 llvm::BasicBlock* block_cont =
1796 CreateBasicBlockWithDexPC(dex_pc, "checkcast_cont");
1797
1798 // Test: Is the reference equal to null? Act as no-op when it is null.
1799 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1800
1801 irb_.CreateCondBr(equal_null,
1802 block_cont,
1803 block_test_class);
1804
1805 // Test: Is the object instantiated from the given class?
1806 irb_.SetInsertPoint(block_test_class);
1807 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
1808 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1809
1810 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1811
1812 llvm::Value* object_type_field_addr =
1813 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1814
1815 llvm::Value* object_type_object_addr =
1816 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
1817
1818 llvm::Value* equal_class =
1819 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1820
1821 irb_.CreateCondBr(equal_class,
1822 block_cont,
1823 block_test_sub_class);
1824
1825 // Test: Is the object instantiated from the subclass of the given class?
1826 irb_.SetInsertPoint(block_test_sub_class);
1827
1828 EmitUpdateDexPC(dex_pc);
1829
1830 irb_.CreateCall2(irb_.GetRuntime(runtime_support::CheckCast),
1831 type_object_addr, object_type_object_addr);
1832
1833 EmitGuard_ExceptionLandingPad(dex_pc);
1834
1835 irb_.CreateBr(block_cont);
1836
1837 irb_.SetInsertPoint(block_cont);
1838
1839 return;
1840}
1841
1842llvm::Value* GBCExpanderPass::Expand_InstanceOf(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001843 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1844 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1845 llvm::Value* object_addr = call_inst.getArgOperand(1);
1846
1847 llvm::BasicBlock* block_nullp =
1848 CreateBasicBlockWithDexPC(dex_pc, "nullp");
1849
1850 llvm::BasicBlock* block_test_class =
1851 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1852
1853 llvm::BasicBlock* block_class_equals =
1854 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1855
1856 llvm::BasicBlock* block_test_sub_class =
1857 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1858
1859 llvm::BasicBlock* block_cont =
1860 CreateBasicBlockWithDexPC(dex_pc, "instance_of_cont");
1861
1862 // Overview of the following code :
1863 // We check for null, if so, then false, otherwise check for class == . If so
1864 // then true, otherwise do callout slowpath.
1865 //
1866 // Test: Is the reference equal to null? Set 0 when it is null.
1867 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1868
1869 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1870
1871 irb_.SetInsertPoint(block_nullp);
1872 irb_.CreateBr(block_cont);
1873
1874 // Test: Is the object instantiated from the given class?
1875 irb_.SetInsertPoint(block_test_class);
1876 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
1877 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1878
1879 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1880
1881 llvm::Value* object_type_field_addr =
1882 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1883
1884 llvm::Value* object_type_object_addr =
1885 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
1886
1887 llvm::Value* equal_class =
1888 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1889
1890 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1891
1892 irb_.SetInsertPoint(block_class_equals);
1893 irb_.CreateBr(block_cont);
1894
1895 // Test: Is the object instantiated from the subclass of the given class?
1896 irb_.SetInsertPoint(block_test_sub_class);
1897 llvm::Value* result =
1898 irb_.CreateCall2(irb_.GetRuntime(runtime_support::IsAssignable),
1899 type_object_addr, object_type_object_addr);
1900 irb_.CreateBr(block_cont);
1901
1902 irb_.SetInsertPoint(block_cont);
1903
1904 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJIntTy(), 3);
1905
1906 phi->addIncoming(irb_.getJInt(0), block_nullp);
1907 phi->addIncoming(irb_.getJInt(1), block_class_equals);
1908 phi->addIncoming(result, block_test_sub_class);
1909
1910 return phi;
1911}
1912
1913llvm::Value* GBCExpanderPass::Expand_NewInstance(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001914 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1915 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1916
1917 llvm::Function* runtime_func;
1918 if (compiler_->CanAccessInstantiableTypeWithoutChecks(
1919 method_idx_, dex_cache_, *dex_file_, type_idx)) {
1920 runtime_func = irb_.GetRuntime(runtime_support::AllocObject);
1921 } else {
1922 runtime_func = irb_.GetRuntime(runtime_support::AllocObjectWithAccessCheck);
1923 }
1924
1925 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1926
1927 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1928
1929 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1930
1931 EmitUpdateDexPC(dex_pc);
1932
1933 llvm::Value* object_addr =
1934 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
1935
1936 EmitGuard_ExceptionLandingPad(dex_pc);
1937
1938 return object_addr;
1939}
1940
1941llvm::Value* GBCExpanderPass::Expand_HLInvoke(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001942 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1943 InvokeType invoke_type = static_cast<InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
1944 bool is_static = (invoke_type == kStatic);
1945 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
1946
1947 // Compute invoke related information for compiler decision
1948 int vtable_idx = -1;
1949 uintptr_t direct_code = 0;
1950 uintptr_t direct_method = 0;
1951 bool is_fast_path = compiler_->
1952 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
1953 invoke_type, vtable_idx, direct_code, direct_method);
1954
1955 // Load *this* actual parameter
1956 llvm::Value* this_addr = NULL;
1957
1958 if (!is_static) {
1959 // Test: Is *this* parameter equal to null?
1960 this_addr = call_inst.getArgOperand(3);
1961 }
1962
1963 // Load the method object
1964 llvm::Value* callee_method_object_addr = NULL;
1965
1966 if (!is_fast_path) {
1967 callee_method_object_addr =
1968 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
1969 this_addr, dex_pc, is_fast_path);
1970
1971 // TODO: opt_flags
1972 if (!is_static) {
1973 EmitGuard_NullPointerException(dex_pc, this_addr);
1974 }
1975 } else {
1976 // TODO: opt_flags
1977 if (!is_static) {
1978 EmitGuard_NullPointerException(dex_pc, this_addr);
1979 }
1980
1981 switch (invoke_type) {
1982 case kStatic:
1983 case kDirect:
1984 if (direct_method != 0u &&
1985 direct_method != static_cast<uintptr_t>(-1)) {
1986 callee_method_object_addr =
1987 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
1988 irb_.getJObjectTy());
1989 } else {
1990 callee_method_object_addr =
1991 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
1992 }
1993 break;
1994
1995 case kVirtual:
1996 DCHECK(vtable_idx != -1);
1997 callee_method_object_addr =
1998 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
1999 break;
2000
2001 case kSuper:
2002 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2003 "the fast path.";
2004 break;
2005
2006 case kInterface:
2007 callee_method_object_addr =
2008 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2009 invoke_type, this_addr,
2010 dex_pc, is_fast_path);
2011 break;
2012 }
2013 }
2014
2015 // Load the actual parameter
2016 std::vector<llvm::Value*> args;
2017
2018 args.push_back(callee_method_object_addr); // method object for callee
2019
2020 for (uint32_t i = 3; i < call_inst.getNumArgOperands(); ++i) {
2021 args.push_back(call_inst.getArgOperand(i));
2022 }
2023
2024 llvm::Value* code_addr;
2025 if (direct_code != 0u &&
2026 direct_code != static_cast<uintptr_t>(-1)) {
2027 code_addr =
2028 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_code),
2029 GetFunctionType(callee_method_idx, is_static)->getPointerTo());
2030 } else {
2031 code_addr =
2032 irb_.LoadFromObjectOffset(callee_method_object_addr,
2033 Method::GetCodeOffset().Int32Value(),
2034 GetFunctionType(callee_method_idx, is_static)->getPointerTo(),
2035 kTBAAJRuntime);
2036 }
2037
2038 // Invoke callee
2039 EmitUpdateDexPC(dex_pc);
2040 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2041 EmitGuard_ExceptionLandingPad(dex_pc);
2042
2043 return retval;
2044}
2045
2046llvm::Value* GBCExpanderPass::Expand_OptArrayLength(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002047 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2048 // Get the array object address
2049 llvm::Value* array_addr = call_inst.getArgOperand(1);
2050
2051 // TODO: opt_flags
2052 EmitGuard_NullPointerException(dex_pc, array_addr);
2053
2054 // Get the array length and store it to the register
2055 return EmitLoadArrayLength(array_addr);
2056}
2057
2058llvm::Value* GBCExpanderPass::Expand_NewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002059 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2060 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2061 llvm::Value* length = call_inst.getArgOperand(1);
2062
2063 return EmitAllocNewArray(dex_pc, length, type_idx, false);
2064}
2065
2066llvm::Value* GBCExpanderPass::Expand_HLFilledNewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002067 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2068 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(1));
2069 uint32_t length = call_inst.getNumArgOperands() - 3;
2070
2071 llvm::Value* object_addr =
2072 EmitAllocNewArray(dex_pc, irb_.getInt32(length), type_idx, true);
2073
2074 if (length > 0) {
2075 // Check for the element type
2076 uint32_t type_desc_len = 0;
2077 const char* type_desc =
2078 dex_file_->StringByTypeIdx(type_idx, &type_desc_len);
2079
2080 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
2081 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
2082 bool is_elem_int_ty = (type_desc[1] == 'I');
2083
2084 uint32_t alignment;
2085 llvm::Constant* elem_size;
2086 llvm::PointerType* field_type;
2087
2088 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
2089 // as the element, thus we are only checking 2 cases: primitive int and
2090 // non-primitive type.
2091 if (is_elem_int_ty) {
2092 alignment = sizeof(int32_t);
2093 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
2094 field_type = irb_.getJIntTy()->getPointerTo();
2095 } else {
2096 alignment = irb_.getSizeOfPtrEquivInt();
2097 elem_size = irb_.getSizeOfPtrEquivIntValue();
2098 field_type = irb_.getJObjectTy()->getPointerTo();
2099 }
2100
2101 llvm::Value* data_field_offset =
2102 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
2103
2104 llvm::Value* data_field_addr =
2105 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
2106
2107 // TODO: Tune this code. Currently we are generating one instruction for
2108 // one element which may be very space consuming. Maybe changing to use
2109 // memcpy may help; however, since we can't guarantee that the alloca of
2110 // dalvik register are continuous, we can't perform such optimization yet.
2111 for (uint32_t i = 0; i < length; ++i) {
2112 llvm::Value* reg_value = call_inst.getArgOperand(i+3);
2113
2114 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
2115
2116 data_field_addr =
2117 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
2118 }
2119 }
2120
2121 return object_addr;
2122}
2123
2124void GBCExpanderPass::Expand_HLFillArrayData(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002125 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2126 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
2127 LV2SInt(call_inst.getArgOperand(0));
2128 llvm::Value* array_addr = call_inst.getArgOperand(1);
2129
2130 const Instruction::ArrayDataPayload* payload =
2131 reinterpret_cast<const Instruction::ArrayDataPayload*>(
2132 code_item_->insns_ + payload_offset);
2133
2134 if (payload->element_count == 0) {
2135 // When the number of the elements in the payload is zero, we don't have
2136 // to copy any numbers. However, we should check whether the array object
2137 // address is equal to null or not.
2138 EmitGuard_NullPointerException(dex_pc, array_addr);
2139 } else {
2140 // To save the code size, we are going to call the runtime function to
2141 // copy the content from DexFile.
2142
2143 // NOTE: We will check for the NullPointerException in the runtime.
2144
2145 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::FillArrayData);
2146
2147 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2148
2149 EmitUpdateDexPC(dex_pc);
2150
2151 irb_.CreateCall4(runtime_func,
2152 method_object_addr, irb_.getInt32(dex_pc),
2153 array_addr, irb_.getInt32(payload_offset));
2154
2155 EmitGuard_ExceptionLandingPad(dex_pc);
2156 }
2157
2158 return;
2159}
2160
2161llvm::Value* GBCExpanderPass::EmitAllocNewArray(uint32_t dex_pc,
2162 llvm::Value* array_length_value,
2163 uint32_t type_idx,
2164 bool is_filled_new_array) {
2165 llvm::Function* runtime_func;
2166
2167 bool skip_access_check =
2168 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
2169 *dex_file_, type_idx);
2170
2171
2172 if (is_filled_new_array) {
2173 runtime_func = skip_access_check ?
2174 irb_.GetRuntime(runtime_support::CheckAndAllocArray) :
2175 irb_.GetRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck);
2176 } else {
2177 runtime_func = skip_access_check ?
2178 irb_.GetRuntime(runtime_support::AllocArray) :
2179 irb_.GetRuntime(runtime_support::AllocArrayWithAccessCheck);
2180 }
2181
2182 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2183
2184 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2185
2186 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2187
2188 EmitUpdateDexPC(dex_pc);
2189
2190 llvm::Value* object_addr =
2191 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
2192 array_length_value, thread_object_addr);
2193
2194 EmitGuard_ExceptionLandingPad(dex_pc);
2195
2196 return object_addr;
2197}
2198
2199llvm::Value* GBCExpanderPass::
2200EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
2201 InvokeType invoke_type,
2202 llvm::Value* this_addr,
2203 uint32_t dex_pc,
2204 bool is_fast_path) {
2205
2206 llvm::Function* runtime_func = NULL;
2207
2208 switch (invoke_type) {
2209 case kStatic:
2210 runtime_func = irb_.GetRuntime(runtime_support::FindStaticMethodWithAccessCheck);
2211 break;
2212
2213 case kDirect:
2214 runtime_func = irb_.GetRuntime(runtime_support::FindDirectMethodWithAccessCheck);
2215 break;
2216
2217 case kVirtual:
2218 runtime_func = irb_.GetRuntime(runtime_support::FindVirtualMethodWithAccessCheck);
2219 break;
2220
2221 case kSuper:
2222 runtime_func = irb_.GetRuntime(runtime_support::FindSuperMethodWithAccessCheck);
2223 break;
2224
2225 case kInterface:
2226 if (is_fast_path) {
2227 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethod);
2228 } else {
2229 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethodWithAccessCheck);
2230 }
2231 break;
2232 }
2233
2234 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2235
2236 if (this_addr == NULL) {
2237 DCHECK_EQ(invoke_type, kStatic);
2238 this_addr = irb_.getJNull();
2239 }
2240
2241 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2242
2243 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2244
2245 EmitUpdateDexPC(dex_pc);
2246
2247 llvm::Value* callee_method_object_addr =
2248 irb_.CreateCall4(runtime_func,
2249 callee_method_idx_value,
2250 this_addr,
2251 caller_method_object_addr,
2252 thread_object_addr);
2253
2254 EmitGuard_ExceptionLandingPad(dex_pc);
2255
2256 return callee_method_object_addr;
2257}
2258
TDYa1275e869b62012-07-25 00:45:39 -07002259void GBCExpanderPass::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2260 // Using runtime support, let the target can override by InlineAssembly.
2261 irb_.Runtime().EmitMarkGCCard(value, target_addr);
2262}
2263
2264void GBCExpanderPass::EmitUpdateDexPC(uint32_t dex_pc) {
2265 irb_.StoreToObjectOffset(shadow_frame_,
2266 ShadowFrame::DexPCOffset(),
2267 irb_.getInt32(dex_pc),
2268 kTBAAShadowFrame);
2269}
2270
2271void GBCExpanderPass::EmitGuard_DivZeroException(uint32_t dex_pc,
2272 llvm::Value* denominator,
2273 JType op_jty) {
2274 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2275
2276 llvm::Constant* zero = irb_.getJZero(op_jty);
2277
2278 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2279
2280 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2281
2282 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2283
2284 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
2285
2286 irb_.SetInsertPoint(block_exception);
2287 EmitUpdateDexPC(dex_pc);
2288 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowDivZeroException));
2289 EmitBranchExceptionLandingPad(dex_pc);
2290
2291 irb_.SetInsertPoint(block_continue);
2292}
2293
2294void GBCExpanderPass::EmitGuard_NullPointerException(uint32_t dex_pc,
2295 llvm::Value* object) {
2296 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
2297
2298 llvm::BasicBlock* block_exception =
2299 CreateBasicBlockWithDexPC(dex_pc, "nullp");
2300
2301 llvm::BasicBlock* block_continue =
2302 CreateBasicBlockWithDexPC(dex_pc, "cont");
2303
2304 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
2305
2306 irb_.SetInsertPoint(block_exception);
2307 EmitUpdateDexPC(dex_pc);
2308 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowNullPointerException),
2309 irb_.getInt32(dex_pc));
2310 EmitBranchExceptionLandingPad(dex_pc);
2311
2312 irb_.SetInsertPoint(block_continue);
2313}
2314
2315void
2316GBCExpanderPass::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2317 llvm::Value* array,
2318 llvm::Value* index) {
2319 llvm::Value* array_len = EmitLoadArrayLength(array);
2320
2321 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2322
2323 llvm::BasicBlock* block_exception =
2324 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2325
2326 llvm::BasicBlock* block_continue =
2327 CreateBasicBlockWithDexPC(dex_pc, "cont");
2328
2329 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
2330
2331 irb_.SetInsertPoint(block_exception);
2332
2333 EmitUpdateDexPC(dex_pc);
2334 irb_.CreateCall2(irb_.GetRuntime(runtime_support::ThrowIndexOutOfBounds), index, array_len);
2335 EmitBranchExceptionLandingPad(dex_pc);
2336
2337 irb_.SetInsertPoint(block_continue);
2338}
2339
2340void GBCExpanderPass::EmitGuard_ArrayException(uint32_t dex_pc,
2341 llvm::Value* array,
2342 llvm::Value* index) {
2343 EmitGuard_NullPointerException(dex_pc, array);
2344 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2345}
2346
2347llvm::FunctionType* GBCExpanderPass::GetFunctionType(uint32_t method_idx,
2348 bool is_static) {
2349 // Get method signature
2350 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
2351
2352 uint32_t shorty_size;
2353 const char* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
2354 CHECK_GE(shorty_size, 1u);
2355
2356 // Get return type
2357 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
2358
2359 // Get argument type
2360 std::vector<llvm::Type*> args_type;
2361
2362 args_type.push_back(irb_.getJObjectTy()); // method object pointer
2363
2364 if (!is_static) {
2365 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
2366 }
2367
2368 for (uint32_t i = 1; i < shorty_size; ++i) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002369#if defined(ART_USE_QUICK_COMPILER)
2370 char shorty_type = shorty[i];
2371 switch(shorty_type) {
2372 case 'Z' : shorty_type = 'I'; break;
2373 case 'B' : shorty_type = 'I'; break;
2374 case 'S' : shorty_type = 'I'; break;
2375 case 'C' : shorty_type = 'I'; break;
2376 default: break;
2377 }
2378 args_type.push_back(irb_.getJType(shorty_type, kAccurate));
2379#else
TDYa1275e869b62012-07-25 00:45:39 -07002380 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
TDYa127f71bf5a2012-07-29 20:09:52 -07002381#endif
TDYa1275e869b62012-07-25 00:45:39 -07002382 }
2383
2384 return llvm::FunctionType::get(ret_type, args_type, false);
2385}
2386
2387
2388llvm::BasicBlock* GBCExpanderPass::
2389CreateBasicBlockWithDexPC(uint32_t dex_pc, const char* postfix) {
2390 std::string name;
2391
2392#if !defined(NDEBUG)
2393 StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
2394#endif
2395
2396 return llvm::BasicBlock::Create(context_, name, func_);
2397}
2398
2399llvm::BasicBlock* GBCExpanderPass::GetBasicBlock(uint32_t dex_pc) {
2400 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
2401
2402 return basic_blocks_[dex_pc];
2403}
2404
2405int32_t GBCExpanderPass::GetTryItemOffset(uint32_t dex_pc) {
2406 int32_t min = 0;
2407 int32_t max = code_item_->tries_size_ - 1;
2408
2409 while (min <= max) {
2410 int32_t mid = min + (max - min) / 2;
2411
2412 const DexFile::TryItem* ti = DexFile::GetTryItems(*code_item_, mid);
2413 uint32_t start = ti->start_addr_;
2414 uint32_t end = start + ti->insn_count_;
2415
2416 if (dex_pc < start) {
2417 max = mid - 1;
2418 } else if (dex_pc >= end) {
2419 min = mid + 1;
2420 } else {
2421 return mid; // found
2422 }
2423 }
2424
2425 return -1; // not found
2426}
2427
2428llvm::BasicBlock* GBCExpanderPass::GetLandingPadBasicBlock(uint32_t dex_pc) {
2429 // Find the try item for this address in this method
2430 int32_t ti_offset = GetTryItemOffset(dex_pc);
2431
2432 if (ti_offset == -1) {
2433 return NULL; // No landing pad is available for this address.
2434 }
2435
2436 // Check for the existing landing pad basic block
2437 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2438 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2439
2440 if (block_lpad) {
2441 // We have generated landing pad for this try item already. Return the
2442 // same basic block.
2443 return block_lpad;
2444 }
2445
2446 // Get try item from code item
2447 const DexFile::TryItem* ti = DexFile::GetTryItems(*code_item_, ti_offset);
2448
2449 std::string lpadname;
2450
2451#if !defined(NDEBUG)
2452 StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
2453#endif
2454
2455 // Create landing pad basic block
2456 block_lpad = llvm::BasicBlock::Create(context_, lpadname, func_);
2457
2458 // Change IRBuilder insert point
2459 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2460 irb_.SetInsertPoint(block_lpad);
2461
2462 // Find catch block with matching type
2463 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2464
2465 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
2466
2467 llvm::Value* catch_handler_index_value =
2468 irb_.CreateCall2(irb_.GetRuntime(runtime_support::FindCatchBlock),
2469 method_object_addr, ti_offset_value);
2470
2471 // Switch instruction (Go to unwind basic block by default)
2472 llvm::SwitchInst* sw =
2473 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2474
2475 // Cases with matched catch block
2476 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
2477
2478 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2479 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2480 }
2481
2482 // Restore the orignal insert point for IRBuilder
2483 irb_.restoreIP(irb_ip_original);
2484
2485 // Cache this landing pad
2486 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2487 basic_block_landing_pads_[ti_offset] = block_lpad;
2488
2489 return block_lpad;
2490}
2491
2492llvm::BasicBlock* GBCExpanderPass::GetUnwindBasicBlock() {
2493 // Check the existing unwinding baisc block block
2494 if (basic_block_unwind_ != NULL) {
2495 return basic_block_unwind_;
2496 }
2497
2498 // Create new basic block for unwinding
2499 basic_block_unwind_ =
2500 llvm::BasicBlock::Create(context_, "exception_unwind", func_);
2501
2502 // Change IRBuilder insert point
2503 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2504 irb_.SetInsertPoint(basic_block_unwind_);
2505
2506 // Pop the shadow frame
2507 Expand_PopShadowFrame();
2508
2509 // Emit the code to return default value (zero) for the given return type.
2510 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
2511 if (ret_shorty == 'V') {
2512 irb_.CreateRetVoid();
2513 } else {
2514 irb_.CreateRet(irb_.getJZero(ret_shorty));
2515 }
2516
2517 // Restore the orignal insert point for IRBuilder
2518 irb_.restoreIP(irb_ip_original);
2519
2520 return basic_block_unwind_;
2521}
2522
2523void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2524 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
2525 irb_.CreateBr(lpad);
2526 } else {
2527 irb_.CreateBr(GetUnwindBasicBlock());
2528 }
2529}
2530
2531void GBCExpanderPass::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
2532 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
2533
2534 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2535
2536 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
2537 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
2538 } else {
2539 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
2540 }
2541
2542 irb_.SetInsertPoint(block_cont);
2543}
2544
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002545llvm::Value*
2546GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
2547 llvm::CallInst& call_inst) {
2548 switch (intr_id) {
2549 //==- Thread -----------------------------------------------------------==//
2550 case IntrinsicHelper::GetCurrentThread: {
TDYa127b672d1e2012-06-28 21:21:45 -07002551 return irb_.Runtime().EmitGetCurrentThread();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002552 }
Logan Chiend54a23d2012-07-24 11:19:23 -07002553 case IntrinsicHelper::TestSuspend:
Logan Chien75e4b602012-07-23 14:24:12 -07002554 case IntrinsicHelper::CheckSuspend: {
Logan Chiend54a23d2012-07-24 11:19:23 -07002555 Expand_TestSuspend(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002556 return NULL;
2557 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002558 case IntrinsicHelper::MarkGCCard: {
TDYa1279a129452012-07-19 03:10:08 -07002559 Expand_MarkGCCard(call_inst);
2560 return NULL;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002561 }
Logan Chien75e4b602012-07-23 14:24:12 -07002562
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002563 //==- Exception --------------------------------------------------------==//
2564 case IntrinsicHelper::ThrowException: {
2565 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
2566 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002567 case IntrinsicHelper::HLThrowException: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002568 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2569
2570 EmitUpdateDexPC(dex_pc);
2571
2572 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowException),
2573 call_inst.getArgOperand(0));
2574
2575 EmitGuard_ExceptionLandingPad(dex_pc);
2576 return NULL;
2577 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002578 case IntrinsicHelper::GetException: {
2579 return Expand_GetException();
2580 }
2581 case IntrinsicHelper::IsExceptionPending: {
2582 return irb_.Runtime().EmitIsExceptionPending();
2583 }
2584 case IntrinsicHelper::FindCatchBlock: {
2585 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
2586 }
2587 case IntrinsicHelper::ThrowDivZeroException: {
2588 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
2589 }
2590 case IntrinsicHelper::ThrowNullPointerException: {
2591 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
2592 }
2593 case IntrinsicHelper::ThrowIndexOutOfBounds: {
2594 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
2595 }
Logan Chien75e4b602012-07-23 14:24:12 -07002596
2597 //==- Const String -----------------------------------------------------==//
2598 case IntrinsicHelper::ConstString: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002599 return Expand_ConstString(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002600 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002601 case IntrinsicHelper::LoadStringFromDexCache: {
2602 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
2603 }
2604 case IntrinsicHelper::ResolveString: {
2605 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
2606 }
Logan Chien75e4b602012-07-23 14:24:12 -07002607
2608 //==- Const Class ------------------------------------------------------==//
2609 case IntrinsicHelper::ConstClass: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002610 return Expand_ConstClass(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002611 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002612 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
2613 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
2614 }
2615 case IntrinsicHelper::LoadTypeFromDexCache: {
2616 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
2617 }
2618 case IntrinsicHelper::InitializeType: {
2619 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
2620 }
Logan Chien75e4b602012-07-23 14:24:12 -07002621
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002622 //==- Lock -------------------------------------------------------------==//
2623 case IntrinsicHelper::LockObject: {
2624 Expand_LockObject(call_inst.getArgOperand(0));
2625 return NULL;
2626 }
2627 case IntrinsicHelper::UnlockObject: {
2628 Expand_UnlockObject(call_inst.getArgOperand(0));
2629 return NULL;
2630 }
Logan Chien75e4b602012-07-23 14:24:12 -07002631
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002632 //==- Cast -------------------------------------------------------------==//
2633 case IntrinsicHelper::CheckCast: {
2634 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
2635 }
Logan Chien75e4b602012-07-23 14:24:12 -07002636 case IntrinsicHelper::HLCheckCast: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002637 Expand_HLCheckCast(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002638 return NULL;
2639 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002640 case IntrinsicHelper::IsAssignable: {
2641 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
2642 }
Logan Chien75e4b602012-07-23 14:24:12 -07002643
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002644 //==- Alloc ------------------------------------------------------------==//
2645 case IntrinsicHelper::AllocObject: {
2646 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
2647 }
2648 case IntrinsicHelper::AllocObjectWithAccessCheck: {
2649 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
2650 }
Logan Chien75e4b602012-07-23 14:24:12 -07002651
2652 //==- Instance ---------------------------------------------------------==//
2653 case IntrinsicHelper::NewInstance: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002654 return Expand_NewInstance(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002655 }
2656 case IntrinsicHelper::InstanceOf: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002657 return Expand_InstanceOf(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002658 }
2659
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002660 //==- Array ------------------------------------------------------------==//
Logan Chien75e4b602012-07-23 14:24:12 -07002661 case IntrinsicHelper::NewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002662 return Expand_NewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002663 }
2664 case IntrinsicHelper::OptArrayLength: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002665 return Expand_OptArrayLength(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002666 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002667 case IntrinsicHelper::ArrayLength: {
2668 return EmitLoadArrayLength(call_inst.getArgOperand(0));
2669 }
2670 case IntrinsicHelper::AllocArray: {
2671 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
2672 }
2673 case IntrinsicHelper::AllocArrayWithAccessCheck: {
2674 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
2675 call_inst);
2676 }
2677 case IntrinsicHelper::CheckAndAllocArray: {
2678 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
2679 }
2680 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
2681 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
2682 call_inst);
2683 }
2684 case IntrinsicHelper::ArrayGet: {
2685 return Expand_ArrayGet(call_inst.getArgOperand(0),
2686 call_inst.getArgOperand(1),
2687 kInt);
2688 }
2689 case IntrinsicHelper::ArrayGetWide: {
2690 return Expand_ArrayGet(call_inst.getArgOperand(0),
2691 call_inst.getArgOperand(1),
2692 kLong);
2693 }
2694 case IntrinsicHelper::ArrayGetObject: {
2695 return Expand_ArrayGet(call_inst.getArgOperand(0),
2696 call_inst.getArgOperand(1),
2697 kObject);
2698 }
2699 case IntrinsicHelper::ArrayGetBoolean: {
2700 return Expand_ArrayGet(call_inst.getArgOperand(0),
2701 call_inst.getArgOperand(1),
2702 kBoolean);
2703 }
2704 case IntrinsicHelper::ArrayGetByte: {
2705 return Expand_ArrayGet(call_inst.getArgOperand(0),
2706 call_inst.getArgOperand(1),
2707 kByte);
2708 }
2709 case IntrinsicHelper::ArrayGetChar: {
2710 return Expand_ArrayGet(call_inst.getArgOperand(0),
2711 call_inst.getArgOperand(1),
2712 kChar);
2713 }
2714 case IntrinsicHelper::ArrayGetShort: {
2715 return Expand_ArrayGet(call_inst.getArgOperand(0),
2716 call_inst.getArgOperand(1),
2717 kShort);
2718 }
2719 case IntrinsicHelper::ArrayPut: {
2720 Expand_ArrayPut(call_inst.getArgOperand(0),
2721 call_inst.getArgOperand(1),
2722 call_inst.getArgOperand(2),
2723 kInt);
2724 return NULL;
2725 }
2726 case IntrinsicHelper::ArrayPutWide: {
2727 Expand_ArrayPut(call_inst.getArgOperand(0),
2728 call_inst.getArgOperand(1),
2729 call_inst.getArgOperand(2),
2730 kLong);
2731 return NULL;
2732 }
2733 case IntrinsicHelper::ArrayPutObject: {
2734 Expand_ArrayPut(call_inst.getArgOperand(0),
2735 call_inst.getArgOperand(1),
2736 call_inst.getArgOperand(2),
2737 kObject);
2738 return NULL;
2739 }
2740 case IntrinsicHelper::ArrayPutBoolean: {
2741 Expand_ArrayPut(call_inst.getArgOperand(0),
2742 call_inst.getArgOperand(1),
2743 call_inst.getArgOperand(2),
2744 kBoolean);
2745 return NULL;
2746 }
2747 case IntrinsicHelper::ArrayPutByte: {
2748 Expand_ArrayPut(call_inst.getArgOperand(0),
2749 call_inst.getArgOperand(1),
2750 call_inst.getArgOperand(2),
2751 kByte);
2752 return NULL;
2753 }
2754 case IntrinsicHelper::ArrayPutChar: {
2755 Expand_ArrayPut(call_inst.getArgOperand(0),
2756 call_inst.getArgOperand(1),
2757 call_inst.getArgOperand(2),
2758 kChar);
2759 return NULL;
2760 }
2761 case IntrinsicHelper::ArrayPutShort: {
2762 Expand_ArrayPut(call_inst.getArgOperand(0),
2763 call_inst.getArgOperand(1),
2764 call_inst.getArgOperand(2),
2765 kShort);
2766 return NULL;
2767 }
2768 case IntrinsicHelper::CheckPutArrayElement: {
2769 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
2770 }
2771 case IntrinsicHelper::FilledNewArray: {
2772 Expand_FilledNewArray(call_inst);
2773 return NULL;
2774 }
2775 case IntrinsicHelper::FillArrayData: {
2776 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
2777 }
Logan Chien75e4b602012-07-23 14:24:12 -07002778 case IntrinsicHelper::HLFillArrayData: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002779 Expand_HLFillArrayData(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002780 return NULL;
2781 }
2782 case IntrinsicHelper::HLFilledNewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002783 return Expand_HLFilledNewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002784 }
2785
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002786 //==- Instance Field ---------------------------------------------------==//
2787 case IntrinsicHelper::InstanceFieldGet:
2788 case IntrinsicHelper::InstanceFieldGetBoolean:
2789 case IntrinsicHelper::InstanceFieldGetByte:
2790 case IntrinsicHelper::InstanceFieldGetChar:
2791 case IntrinsicHelper::InstanceFieldGetShort: {
2792 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
2793 }
2794 case IntrinsicHelper::InstanceFieldGetWide: {
2795 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
2796 }
2797 case IntrinsicHelper::InstanceFieldGetObject: {
2798 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
2799 }
2800 case IntrinsicHelper::InstanceFieldGetFast: {
2801 return Expand_IGetFast(call_inst.getArgOperand(0),
2802 call_inst.getArgOperand(1),
2803 call_inst.getArgOperand(2),
2804 kInt);
2805 }
2806 case IntrinsicHelper::InstanceFieldGetWideFast: {
2807 return Expand_IGetFast(call_inst.getArgOperand(0),
2808 call_inst.getArgOperand(1),
2809 call_inst.getArgOperand(2),
2810 kLong);
2811 }
2812 case IntrinsicHelper::InstanceFieldGetObjectFast: {
2813 return Expand_IGetFast(call_inst.getArgOperand(0),
2814 call_inst.getArgOperand(1),
2815 call_inst.getArgOperand(2),
2816 kObject);
2817 }
2818 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
2819 return Expand_IGetFast(call_inst.getArgOperand(0),
2820 call_inst.getArgOperand(1),
2821 call_inst.getArgOperand(2),
2822 kBoolean);
2823 }
2824 case IntrinsicHelper::InstanceFieldGetByteFast: {
2825 return Expand_IGetFast(call_inst.getArgOperand(0),
2826 call_inst.getArgOperand(1),
2827 call_inst.getArgOperand(2),
2828 kByte);
2829 }
2830 case IntrinsicHelper::InstanceFieldGetCharFast: {
2831 return Expand_IGetFast(call_inst.getArgOperand(0),
2832 call_inst.getArgOperand(1),
2833 call_inst.getArgOperand(2),
2834 kChar);
2835 }
2836 case IntrinsicHelper::InstanceFieldGetShortFast: {
2837 return Expand_IGetFast(call_inst.getArgOperand(0),
2838 call_inst.getArgOperand(1),
2839 call_inst.getArgOperand(2),
2840 kShort);
2841 }
2842 case IntrinsicHelper::InstanceFieldPut:
2843 case IntrinsicHelper::InstanceFieldPutBoolean:
2844 case IntrinsicHelper::InstanceFieldPutByte:
2845 case IntrinsicHelper::InstanceFieldPutChar:
2846 case IntrinsicHelper::InstanceFieldPutShort: {
2847 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
2848 }
2849 case IntrinsicHelper::InstanceFieldPutWide: {
2850 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
2851 }
2852 case IntrinsicHelper::InstanceFieldPutObject: {
2853 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
2854 }
2855 case IntrinsicHelper::InstanceFieldPutFast: {
2856 Expand_IPutFast(call_inst.getArgOperand(0),
2857 call_inst.getArgOperand(1),
2858 call_inst.getArgOperand(2),
2859 call_inst.getArgOperand(3),
2860 kInt);
2861 return NULL;
2862 }
2863 case IntrinsicHelper::InstanceFieldPutWideFast: {
2864 Expand_IPutFast(call_inst.getArgOperand(0),
2865 call_inst.getArgOperand(1),
2866 call_inst.getArgOperand(2),
2867 call_inst.getArgOperand(3),
2868 kLong);
2869 return NULL;
2870 }
2871 case IntrinsicHelper::InstanceFieldPutObjectFast: {
2872 Expand_IPutFast(call_inst.getArgOperand(0),
2873 call_inst.getArgOperand(1),
2874 call_inst.getArgOperand(2),
2875 call_inst.getArgOperand(3),
2876 kObject);
2877 return NULL;
2878 }
2879 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
2880 Expand_IPutFast(call_inst.getArgOperand(0),
2881 call_inst.getArgOperand(1),
2882 call_inst.getArgOperand(2),
2883 call_inst.getArgOperand(3),
2884 kBoolean);
2885 return NULL;
2886 }
2887 case IntrinsicHelper::InstanceFieldPutByteFast: {
2888 Expand_IPutFast(call_inst.getArgOperand(0),
2889 call_inst.getArgOperand(1),
2890 call_inst.getArgOperand(2),
2891 call_inst.getArgOperand(3),
2892 kByte);
2893 return NULL;
2894 }
2895 case IntrinsicHelper::InstanceFieldPutCharFast: {
2896 Expand_IPutFast(call_inst.getArgOperand(0),
2897 call_inst.getArgOperand(1),
2898 call_inst.getArgOperand(2),
2899 call_inst.getArgOperand(3),
2900 kChar);
2901 return NULL;
2902 }
2903 case IntrinsicHelper::InstanceFieldPutShortFast: {
2904 Expand_IPutFast(call_inst.getArgOperand(0),
2905 call_inst.getArgOperand(1),
2906 call_inst.getArgOperand(2),
2907 call_inst.getArgOperand(3),
2908 kShort);
2909 return NULL;
2910 }
Logan Chien75e4b602012-07-23 14:24:12 -07002911
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002912 //==- Static Field -----------------------------------------------------==//
2913 case IntrinsicHelper::StaticFieldGet:
2914 case IntrinsicHelper::StaticFieldGetBoolean:
2915 case IntrinsicHelper::StaticFieldGetByte:
2916 case IntrinsicHelper::StaticFieldGetChar:
2917 case IntrinsicHelper::StaticFieldGetShort: {
2918 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
2919 }
2920 case IntrinsicHelper::StaticFieldGetWide: {
2921 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
2922 }
2923 case IntrinsicHelper::StaticFieldGetObject: {
2924 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
2925 }
2926 case IntrinsicHelper::StaticFieldGetFast: {
2927 return Expand_SGetFast(call_inst.getArgOperand(0),
2928 call_inst.getArgOperand(1),
2929 call_inst.getArgOperand(2),
2930 kInt);
2931 }
2932 case IntrinsicHelper::StaticFieldGetWideFast: {
2933 return Expand_SGetFast(call_inst.getArgOperand(0),
2934 call_inst.getArgOperand(1),
2935 call_inst.getArgOperand(2),
2936 kLong);
2937 }
2938 case IntrinsicHelper::StaticFieldGetObjectFast: {
2939 return Expand_SGetFast(call_inst.getArgOperand(0),
2940 call_inst.getArgOperand(1),
2941 call_inst.getArgOperand(2),
2942 kObject);
2943 }
2944 case IntrinsicHelper::StaticFieldGetBooleanFast: {
2945 return Expand_SGetFast(call_inst.getArgOperand(0),
2946 call_inst.getArgOperand(1),
2947 call_inst.getArgOperand(2),
2948 kBoolean);
2949 }
2950 case IntrinsicHelper::StaticFieldGetByteFast: {
2951 return Expand_SGetFast(call_inst.getArgOperand(0),
2952 call_inst.getArgOperand(1),
2953 call_inst.getArgOperand(2),
2954 kByte);
2955 }
2956 case IntrinsicHelper::StaticFieldGetCharFast: {
2957 return Expand_SGetFast(call_inst.getArgOperand(0),
2958 call_inst.getArgOperand(1),
2959 call_inst.getArgOperand(2),
2960 kChar);
2961 }
2962 case IntrinsicHelper::StaticFieldGetShortFast: {
2963 return Expand_SGetFast(call_inst.getArgOperand(0),
2964 call_inst.getArgOperand(1),
2965 call_inst.getArgOperand(2),
2966 kShort);
2967 }
2968 case IntrinsicHelper::StaticFieldPut:
2969 case IntrinsicHelper::StaticFieldPutBoolean:
2970 case IntrinsicHelper::StaticFieldPutByte:
2971 case IntrinsicHelper::StaticFieldPutChar:
2972 case IntrinsicHelper::StaticFieldPutShort: {
2973 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
2974 }
2975 case IntrinsicHelper::StaticFieldPutWide: {
2976 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
2977 }
2978 case IntrinsicHelper::StaticFieldPutObject: {
2979 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
2980 }
2981 case IntrinsicHelper::StaticFieldPutFast: {
2982 Expand_SPutFast(call_inst.getArgOperand(0),
2983 call_inst.getArgOperand(1),
2984 call_inst.getArgOperand(2),
2985 call_inst.getArgOperand(3),
2986 kInt);
2987 return NULL;
2988 }
2989 case IntrinsicHelper::StaticFieldPutWideFast: {
2990 Expand_SPutFast(call_inst.getArgOperand(0),
2991 call_inst.getArgOperand(1),
2992 call_inst.getArgOperand(2),
2993 call_inst.getArgOperand(3),
2994 kLong);
2995 return NULL;
2996 }
2997 case IntrinsicHelper::StaticFieldPutObjectFast: {
2998 Expand_SPutFast(call_inst.getArgOperand(0),
2999 call_inst.getArgOperand(1),
3000 call_inst.getArgOperand(2),
3001 call_inst.getArgOperand(3),
3002 kObject);
3003 return NULL;
3004 }
3005 case IntrinsicHelper::StaticFieldPutBooleanFast: {
3006 Expand_SPutFast(call_inst.getArgOperand(0),
3007 call_inst.getArgOperand(1),
3008 call_inst.getArgOperand(2),
3009 call_inst.getArgOperand(3),
3010 kBoolean);
3011 return NULL;
3012 }
3013 case IntrinsicHelper::StaticFieldPutByteFast: {
3014 Expand_SPutFast(call_inst.getArgOperand(0),
3015 call_inst.getArgOperand(1),
3016 call_inst.getArgOperand(2),
3017 call_inst.getArgOperand(3),
3018 kByte);
3019 return NULL;
3020 }
3021 case IntrinsicHelper::StaticFieldPutCharFast: {
3022 Expand_SPutFast(call_inst.getArgOperand(0),
3023 call_inst.getArgOperand(1),
3024 call_inst.getArgOperand(2),
3025 call_inst.getArgOperand(3),
3026 kChar);
3027 return NULL;
3028 }
3029 case IntrinsicHelper::StaticFieldPutShortFast: {
3030 Expand_SPutFast(call_inst.getArgOperand(0),
3031 call_inst.getArgOperand(1),
3032 call_inst.getArgOperand(2),
3033 call_inst.getArgOperand(3),
3034 kShort);
3035 return NULL;
3036 }
3037 case IntrinsicHelper::LoadDeclaringClassSSB: {
3038 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
3039 }
3040 case IntrinsicHelper::LoadClassSSBFromDexCache: {
3041 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
3042 }
3043 case IntrinsicHelper::InitializeAndLoadClassSSB: {
3044 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
3045 }
Logan Chien75e4b602012-07-23 14:24:12 -07003046
3047 //==- High-level Array -------------------------------------------------==//
3048 case IntrinsicHelper::HLArrayGet: {
TDYa1275a26d442012-07-26 18:58:38 -07003049 return Expand_HLArrayGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003050 }
3051 case IntrinsicHelper::HLArrayGetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003052 return Expand_HLArrayGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003053 }
3054 case IntrinsicHelper::HLArrayGetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003055 return Expand_HLArrayGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003056 }
3057 case IntrinsicHelper::HLArrayGetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003058 return Expand_HLArrayGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003059 }
3060 case IntrinsicHelper::HLArrayGetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003061 return Expand_HLArrayGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003062 }
3063 case IntrinsicHelper::HLArrayGetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003064 return Expand_HLArrayGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003065 }
3066 case IntrinsicHelper::HLArrayGetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003067 return Expand_HLArrayGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003068 }
3069 case IntrinsicHelper::HLArrayGetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003070 return Expand_HLArrayGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003071 }
3072 case IntrinsicHelper::HLArrayGetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003073 return Expand_HLArrayGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003074 }
3075 case IntrinsicHelper::HLArrayPut: {
TDYa1275a26d442012-07-26 18:58:38 -07003076 Expand_HLArrayPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003077 return NULL;
3078 }
3079 case IntrinsicHelper::HLArrayPutBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003080 Expand_HLArrayPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003081 return NULL;
3082 }
3083 case IntrinsicHelper::HLArrayPutByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003084 Expand_HLArrayPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003085 return NULL;
3086 }
3087 case IntrinsicHelper::HLArrayPutChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003088 Expand_HLArrayPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003089 return NULL;
3090 }
3091 case IntrinsicHelper::HLArrayPutShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003092 Expand_HLArrayPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003093 return NULL;
3094 }
3095 case IntrinsicHelper::HLArrayPutFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003096 Expand_HLArrayPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003097 return NULL;
3098 }
3099 case IntrinsicHelper::HLArrayPutWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003100 Expand_HLArrayPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003101 return NULL;
3102 }
3103 case IntrinsicHelper::HLArrayPutDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003104 Expand_HLArrayPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003105 return NULL;
3106 }
3107 case IntrinsicHelper::HLArrayPutObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003108 Expand_HLArrayPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003109 return NULL;
3110 }
3111
3112 //==- High-level Instance ----------------------------------------------==//
3113 case IntrinsicHelper::HLIGet: {
TDYa1275e869b62012-07-25 00:45:39 -07003114 return Expand_HLIGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003115 }
3116 case IntrinsicHelper::HLIGetBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003117 return Expand_HLIGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003118 }
3119 case IntrinsicHelper::HLIGetByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003120 return Expand_HLIGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003121 }
3122 case IntrinsicHelper::HLIGetChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003123 return Expand_HLIGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003124 }
3125 case IntrinsicHelper::HLIGetShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003126 return Expand_HLIGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003127 }
3128 case IntrinsicHelper::HLIGetFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003129 return Expand_HLIGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003130 }
3131 case IntrinsicHelper::HLIGetWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003132 return Expand_HLIGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003133 }
3134 case IntrinsicHelper::HLIGetDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003135 return Expand_HLIGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003136 }
3137 case IntrinsicHelper::HLIGetObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003138 return Expand_HLIGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003139 }
3140 case IntrinsicHelper::HLIPut: {
TDYa1275e869b62012-07-25 00:45:39 -07003141 Expand_HLIPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003142 return NULL;
3143 }
3144 case IntrinsicHelper::HLIPutBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003145 Expand_HLIPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003146 return NULL;
3147 }
3148 case IntrinsicHelper::HLIPutByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003149 Expand_HLIPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003150 return NULL;
3151 }
3152 case IntrinsicHelper::HLIPutChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003153 Expand_HLIPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003154 return NULL;
3155 }
3156 case IntrinsicHelper::HLIPutShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003157 Expand_HLIPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003158 return NULL;
3159 }
3160 case IntrinsicHelper::HLIPutFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003161 Expand_HLIPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003162 return NULL;
3163 }
3164 case IntrinsicHelper::HLIPutWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003165 Expand_HLIPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003166 return NULL;
3167 }
3168 case IntrinsicHelper::HLIPutDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003169 Expand_HLIPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003170 return NULL;
3171 }
3172 case IntrinsicHelper::HLIPutObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003173 Expand_HLIPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003174 return NULL;
3175 }
3176
3177 //==- High-level Invoke ------------------------------------------------==//
TDYa127f71bf5a2012-07-29 20:09:52 -07003178 case IntrinsicHelper::HLInvokeVoid:
3179 case IntrinsicHelper::HLInvokeObj:
3180 case IntrinsicHelper::HLInvokeInt:
3181 case IntrinsicHelper::HLInvokeFloat:
3182 case IntrinsicHelper::HLInvokeLong:
Logan Chien75e4b602012-07-23 14:24:12 -07003183 case IntrinsicHelper::HLInvokeDouble: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003184 return Expand_HLInvoke(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003185 }
3186
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003187 //==- Invoke -----------------------------------------------------------==//
3188 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
3189 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
3190 }
3191 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
3192 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
3193 }
3194 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
3195 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
3196 }
3197 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
3198 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
3199 }
3200 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
3201 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
3202 }
3203 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
3204 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
3205 }
3206 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
3207 return Expand_GetVirtualCalleeMethodObjAddrFast(
3208 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
3209 }
3210 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
3211 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
3212 }
3213 case IntrinsicHelper::InvokeRetVoid:
3214 case IntrinsicHelper::InvokeRetBoolean:
3215 case IntrinsicHelper::InvokeRetByte:
3216 case IntrinsicHelper::InvokeRetChar:
3217 case IntrinsicHelper::InvokeRetShort:
3218 case IntrinsicHelper::InvokeRetInt:
3219 case IntrinsicHelper::InvokeRetLong:
3220 case IntrinsicHelper::InvokeRetFloat:
3221 case IntrinsicHelper::InvokeRetDouble:
3222 case IntrinsicHelper::InvokeRetObject: {
3223 return Expand_Invoke(call_inst);
3224 }
Logan Chien75e4b602012-07-23 14:24:12 -07003225
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003226 //==- Math -------------------------------------------------------------==//
3227 case IntrinsicHelper::DivInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003228 return Expand_DivRem(call_inst, /* is_div */true, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003229 }
3230 case IntrinsicHelper::RemInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003231 return Expand_DivRem(call_inst, /* is_div */false, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003232 }
3233 case IntrinsicHelper::DivLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003234 return Expand_DivRem(call_inst, /* is_div */true, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003235 }
3236 case IntrinsicHelper::RemLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003237 return Expand_DivRem(call_inst, /* is_div */false, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003238 }
3239 case IntrinsicHelper::D2L: {
3240 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
3241 }
3242 case IntrinsicHelper::D2I: {
3243 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
3244 }
3245 case IntrinsicHelper::F2L: {
3246 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
3247 }
3248 case IntrinsicHelper::F2I: {
3249 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
3250 }
Logan Chien75e4b602012-07-23 14:24:12 -07003251
3252 //==- High-level Static ------------------------------------------------==//
3253 case IntrinsicHelper::HLSget: {
TDYa1275a26d442012-07-26 18:58:38 -07003254 return Expand_HLSget(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003255 }
3256 case IntrinsicHelper::HLSgetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003257 return Expand_HLSget(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003258 }
3259 case IntrinsicHelper::HLSgetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003260 return Expand_HLSget(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003261 }
3262 case IntrinsicHelper::HLSgetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003263 return Expand_HLSget(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003264 }
3265 case IntrinsicHelper::HLSgetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003266 return Expand_HLSget(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003267 }
3268 case IntrinsicHelper::HLSgetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003269 return Expand_HLSget(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003270 }
3271 case IntrinsicHelper::HLSgetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003272 return Expand_HLSget(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003273 }
3274 case IntrinsicHelper::HLSgetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003275 return Expand_HLSget(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003276 }
3277 case IntrinsicHelper::HLSgetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003278 return Expand_HLSget(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003279 }
3280 case IntrinsicHelper::HLSput: {
TDYa1275a26d442012-07-26 18:58:38 -07003281 Expand_HLSput(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003282 return NULL;
3283 }
3284 case IntrinsicHelper::HLSputBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003285 Expand_HLSput(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003286 return NULL;
3287 }
3288 case IntrinsicHelper::HLSputByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003289 Expand_HLSput(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003290 return NULL;
3291 }
3292 case IntrinsicHelper::HLSputChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003293 Expand_HLSput(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003294 return NULL;
3295 }
3296 case IntrinsicHelper::HLSputShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003297 Expand_HLSput(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003298 return NULL;
3299 }
3300 case IntrinsicHelper::HLSputFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003301 Expand_HLSput(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003302 return NULL;
3303 }
3304 case IntrinsicHelper::HLSputWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003305 Expand_HLSput(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003306 return NULL;
3307 }
3308 case IntrinsicHelper::HLSputDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003309 Expand_HLSput(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003310 return NULL;
3311 }
3312 case IntrinsicHelper::HLSputObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003313 Expand_HLSput(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003314 return NULL;
3315 }
3316
3317 //==- High-level Monitor -----------------------------------------------==//
3318 case IntrinsicHelper::MonitorEnter: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003319 Expand_MonitorEnter(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003320 return NULL;
3321 }
3322 case IntrinsicHelper::MonitorExit: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003323 Expand_MonitorExit(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003324 return NULL;
3325 }
3326
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003327 //==- Shadow Frame -----------------------------------------------------==//
3328 case IntrinsicHelper::AllocaShadowFrame: {
3329 Expand_AllocaShadowFrame(call_inst.getArgOperand(0));
3330 return NULL;
3331 }
3332 case IntrinsicHelper::SetShadowFrameEntry: {
3333 Expand_SetShadowFrameEntry(call_inst.getArgOperand(0),
3334 call_inst.getArgOperand(1));
3335 return NULL;
3336 }
3337 case IntrinsicHelper::PopShadowFrame: {
3338 Expand_PopShadowFrame();
3339 return NULL;
3340 }
3341 case IntrinsicHelper::UpdateDexPC: {
3342 Expand_UpdateDexPC(call_inst.getArgOperand(0));
3343 return NULL;
3344 }
TDYa127a1b21852012-07-23 03:20:39 -07003345
Logan Chien75e4b602012-07-23 14:24:12 -07003346 //==- Comparison -------------------------------------------------------==//
3347 case IntrinsicHelper::CmplFloat:
3348 case IntrinsicHelper::CmplDouble: {
3349 return Expand_FPCompare(call_inst.getArgOperand(0),
3350 call_inst.getArgOperand(1),
3351 false);
3352 }
3353 case IntrinsicHelper::CmpgFloat:
3354 case IntrinsicHelper::CmpgDouble: {
3355 return Expand_FPCompare(call_inst.getArgOperand(0),
3356 call_inst.getArgOperand(1),
3357 true);
3358 }
3359 case IntrinsicHelper::CmpLong: {
3360 return Expand_LongCompare(call_inst.getArgOperand(0),
3361 call_inst.getArgOperand(1));
3362 }
TDYa127a1b21852012-07-23 03:20:39 -07003363
Logan Chien75e4b602012-07-23 14:24:12 -07003364 //==- Switch -----------------------------------------------------------==//
3365 case greenland::IntrinsicHelper::SparseSwitch: {
TDYa1275e869b62012-07-25 00:45:39 -07003366 // Nothing to be done.
Logan Chien75e4b602012-07-23 14:24:12 -07003367 return NULL;
3368 }
3369 case greenland::IntrinsicHelper::PackedSwitch: {
TDYa1275e869b62012-07-25 00:45:39 -07003370 // Nothing to be done.
Logan Chien75e4b602012-07-23 14:24:12 -07003371 return NULL;
3372 }
3373
3374 //==- Const ------------------------------------------------------------==//
Logan Chiend54a23d2012-07-24 11:19:23 -07003375 case greenland::IntrinsicHelper::ConstInt:
Logan Chien75e4b602012-07-23 14:24:12 -07003376 case greenland::IntrinsicHelper::ConstLong: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003377 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003378 }
3379 case greenland::IntrinsicHelper::ConstFloat: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003380 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3381 irb_.getJFloatTy());
Logan Chien75e4b602012-07-23 14:24:12 -07003382 }
3383 case greenland::IntrinsicHelper::ConstDouble: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003384 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3385 irb_.getJDoubleTy());
3386 }
3387 case greenland::IntrinsicHelper::ConstObj: {
3388 LOG(FATAL) << "ConstObj should not occur at all";
Logan Chien75e4b602012-07-23 14:24:12 -07003389 return NULL;
3390 }
3391
3392 //==- Method Info ------------------------------------------------------==//
3393 case greenland::IntrinsicHelper::MethodInfo: {
TDYa1275e869b62012-07-25 00:45:39 -07003394 // Nothing to be done.
Logan Chien75e4b602012-07-23 14:24:12 -07003395 return NULL;
3396 }
3397
3398 //==- Copy -------------------------------------------------------------==//
Logan Chiend54a23d2012-07-24 11:19:23 -07003399 case greenland::IntrinsicHelper::CopyInt:
3400 case greenland::IntrinsicHelper::CopyFloat:
3401 case greenland::IntrinsicHelper::CopyLong:
TDYa1275e869b62012-07-25 00:45:39 -07003402 case greenland::IntrinsicHelper::CopyDouble:
Logan Chien75e4b602012-07-23 14:24:12 -07003403 case greenland::IntrinsicHelper::CopyObj: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003404 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003405 }
3406
3407 //==- Shift ------------------------------------------------------------==//
3408 case greenland::IntrinsicHelper::SHLLong: {
3409 return Expand_IntegerShift(call_inst.getArgOperand(0),
3410 call_inst.getArgOperand(1),
3411 kIntegerSHL, kLong);
3412 }
3413 case greenland::IntrinsicHelper::SHRLong: {
3414 return Expand_IntegerShift(call_inst.getArgOperand(0),
3415 call_inst.getArgOperand(1),
3416 kIntegerSHR, kLong);
3417 }
3418 case greenland::IntrinsicHelper::USHRLong: {
3419 return Expand_IntegerShift(call_inst.getArgOperand(0),
3420 call_inst.getArgOperand(1),
3421 kIntegerUSHR, kLong);
3422 }
3423 case greenland::IntrinsicHelper::SHLInt: {
3424 return Expand_IntegerShift(call_inst.getArgOperand(0),
3425 call_inst.getArgOperand(1),
3426 kIntegerSHL, kInt);
3427 }
3428 case greenland::IntrinsicHelper::SHRInt: {
3429 return Expand_IntegerShift(call_inst.getArgOperand(0),
3430 call_inst.getArgOperand(1),
3431 kIntegerSHR, kInt);
3432 }
3433 case greenland::IntrinsicHelper::USHRInt: {
3434 return Expand_IntegerShift(call_inst.getArgOperand(0),
3435 call_inst.getArgOperand(1),
3436 kIntegerUSHR, kInt);
3437 }
3438
3439 //==- Conversion -------------------------------------------------------==//
TDYa127a1b21852012-07-23 03:20:39 -07003440 case IntrinsicHelper::IntToChar: {
3441 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
3442 irb_.getJIntTy());
3443 }
3444 case IntrinsicHelper::IntToShort: {
3445 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
3446 irb_.getJIntTy());
3447 }
3448 case IntrinsicHelper::IntToByte: {
3449 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
3450 irb_.getJIntTy());
3451 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003452
Logan Chien75e4b602012-07-23 14:24:12 -07003453 //==- Unknown Cases ----------------------------------------------------==//
3454 case IntrinsicHelper::MaxIntrinsicId:
3455 case IntrinsicHelper::UnknownId:
3456 //default:
3457 // NOTE: "default" is intentionally commented so that C/C++ compiler will
3458 // give some warning on unmatched cases.
3459 // NOTE: We should not implement these cases.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003460 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003461 }
Logan Chien75e4b602012-07-23 14:24:12 -07003462 UNIMPLEMENTED(FATAL) << "Unexpected GBC intrinsic: " << static_cast<int>(intr_id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003463 return NULL;
3464}
3465
3466} // anonymous namespace
3467
3468namespace art {
3469namespace compiler_llvm {
3470
3471llvm::FunctionPass*
3472CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb) {
3473 return new GBCExpanderPass(intrinsic_helper, irb);
3474}
3475
3476} // namespace compiler_llvm
3477} // namespace art