blob: c28384a78a2b8b69ae01f2cc3958f0e14a2dfac4 [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
74 private:
Shih-wei Liao21d28f52012-06-12 05:55:00 -070075 //----------------------------------------------------------------------------
Logan Chien75e4b602012-07-23 14:24:12 -070076 // Constant for GBC expansion
77 //----------------------------------------------------------------------------
78 enum IntegerShiftKind {
79 kIntegerSHL,
80 kIntegerSHR,
81 kIntegerUSHR,
82 };
83
84 private:
85 //----------------------------------------------------------------------------
Shih-wei Liao21d28f52012-06-12 05:55:00 -070086 // Helper function for GBC expansion
87 //----------------------------------------------------------------------------
88
Shih-wei Liao21d28f52012-06-12 05:55:00 -070089 llvm::Value* ExpandToRuntime(runtime_support::RuntimeId rt,
90 llvm::CallInst& inst);
91
TDYa1275e869b62012-07-25 00:45:39 -070092 uint64_t LV2UInt(llvm::Value* lv) {
93 return llvm::cast<llvm::ConstantInt>(lv)->getZExtValue();
94 }
95
96 int64_t LV2SInt(llvm::Value* lv) {
97 return llvm::cast<llvm::ConstantInt>(lv)->getSExtValue();
98 }
99
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700100 private:
101 // TODO: Almost all Emit* are directly copy-n-paste from MethodCompiler.
102 // Refactor these utility functions from MethodCompiler to avoid forking.
103
104 bool EmitStackOverflowCheck(llvm::Instruction* first_non_alloca);
105
106 //----------------------------------------------------------------------------
107 // Dex cache code generation helper function
108 //----------------------------------------------------------------------------
109 llvm::Value* EmitLoadDexCacheAddr(MemberOffset dex_cache_offset);
110
111 llvm::Value* EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx);
112
113 llvm::Value* EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx);
114
115 llvm::Value* EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx);
116
117 llvm::Value* EmitLoadDexCacheStringFieldAddr(uint32_t string_idx);
118
119 //----------------------------------------------------------------------------
120 // Code generation helper function
121 //----------------------------------------------------------------------------
122 llvm::Value* EmitLoadMethodObjectAddr();
123
124 llvm::Value* EmitLoadArrayLength(llvm::Value* array);
125
126 llvm::Value* EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx);
127
128 llvm::Value* EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
129 llvm::Value* this_addr);
130
131 llvm::Value* EmitArrayGEP(llvm::Value* array_addr,
132 llvm::Value* index_value,
133 JType elem_jty);
134
135 private:
136 //----------------------------------------------------------------------------
137 // Expand Greenland intrinsics
138 //----------------------------------------------------------------------------
139 void Expand_TestSuspend(llvm::CallInst& call_inst);
140
TDYa1279a129452012-07-19 03:10:08 -0700141 void Expand_MarkGCCard(llvm::CallInst& call_inst);
142
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700143 llvm::Value* Expand_GetException();
144
145 llvm::Value* Expand_LoadStringFromDexCache(llvm::Value* string_idx_value);
146
147 llvm::Value* Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value);
148
149 void Expand_LockObject(llvm::Value* obj);
150
151 void Expand_UnlockObject(llvm::Value* obj);
152
153 llvm::Value* Expand_ArrayGet(llvm::Value* array_addr,
154 llvm::Value* index_value,
155 JType elem_jty);
156
157 void Expand_ArrayPut(llvm::Value* new_value,
158 llvm::Value* array_addr,
159 llvm::Value* index_value,
160 JType elem_jty);
161
162 void Expand_FilledNewArray(llvm::CallInst& call_inst);
163
164 llvm::Value* Expand_IGetFast(llvm::Value* field_offset_value,
165 llvm::Value* is_volatile_value,
166 llvm::Value* object_addr,
167 JType field_jty);
168
169 void Expand_IPutFast(llvm::Value* field_offset_value,
170 llvm::Value* is_volatile_value,
171 llvm::Value* object_addr,
172 llvm::Value* new_value,
173 JType field_jty);
174
175 llvm::Value* Expand_SGetFast(llvm::Value* static_storage_addr,
176 llvm::Value* field_offset_value,
177 llvm::Value* is_volatile_value,
178 JType field_jty);
179
180 void Expand_SPutFast(llvm::Value* static_storage_addr,
181 llvm::Value* field_offset_value,
182 llvm::Value* is_volatile_value,
183 llvm::Value* new_value,
184 JType field_jty);
185
186 llvm::Value* Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr);
187
188 llvm::Value* Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value);
189
190 llvm::Value*
191 Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value);
192
193 llvm::Value*
194 Expand_GetVirtualCalleeMethodObjAddrFast(llvm::Value* vtable_idx_value,
195 llvm::Value* this_addr);
196
197 llvm::Value* Expand_Invoke(llvm::CallInst& call_inst);
198
TDYa1274ec8ccd2012-08-11 07:04:57 -0700199 llvm::Value* Expand_DivRem(llvm::CallInst& call_inst, bool is_div, JType op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700200
201 void Expand_AllocaShadowFrame(llvm::Value* num_entry_value);
202
203 void Expand_SetShadowFrameEntry(llvm::Value* obj, llvm::Value* entry_idx);
204
205 void Expand_PopShadowFrame();
206
207 void Expand_UpdateDexPC(llvm::Value* dex_pc_value);
208
TDYa127a1b21852012-07-23 03:20:39 -0700209 //----------------------------------------------------------------------------
210 // Quick
211 //----------------------------------------------------------------------------
212
213 llvm::Value* Expand_FPCompare(llvm::Value* src1_value,
214 llvm::Value* src2_value,
215 bool gt_bias);
216
217 llvm::Value* Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value);
218
219 llvm::Value* EmitCompareResultSelection(llvm::Value* cmp_eq,
220 llvm::Value* cmp_lt);
221
TDYa1275e869b62012-07-25 00:45:39 -0700222 class ScopedExpandToBasicBlock {
223 public:
224 ScopedExpandToBasicBlock(IRBuilder& irb, llvm::Instruction* expand_inst)
225 : irb_(irb), expand_inst_(expand_inst) {
226 llvm::Function* func = expand_inst_->getParent()->getParent();
227 begin_bb_ = llvm::BasicBlock::Create(irb_.getContext(),
228 "",
229 func);
230 irb_.SetInsertPoint(begin_bb_);
231 }
232
233 ~ScopedExpandToBasicBlock() {
234 llvm::BasicBlock* end_bb = irb_.GetInsertBlock();
235 SplitAndInsertBasicBlocksAfter(*expand_inst_, begin_bb_, end_bb);
236 }
237
238 private:
239 // Split the basic block containing INST at INST and insert a sequence of
240 // basic blocks with a single entry at BEGIN_BB and a single exit at END_BB
241 // before INST.
242 llvm::BasicBlock*
243 SplitAndInsertBasicBlocksAfter(llvm::BasicBlock::iterator inst,
244 llvm::BasicBlock* begin_bb,
245 llvm::BasicBlock* end_bb);
246 private:
247 IRBuilder& irb_;
248 llvm::Instruction* expand_inst_;
249 llvm::BasicBlock* begin_bb_;
250 };
251
TDYa127f71bf5a2012-07-29 20:09:52 -0700252 llvm::Value* EmitLoadConstantClass(uint32_t dex_pc, uint32_t type_idx);
TDYa1275a26d442012-07-26 18:58:38 -0700253 llvm::Value* EmitLoadStaticStorage(uint32_t dex_pc, uint32_t type_idx);
254
TDYa1275e869b62012-07-25 00:45:39 -0700255 llvm::Value* Expand_HLIGet(llvm::CallInst& call_inst, JType field_jty);
256 void Expand_HLIPut(llvm::CallInst& call_inst, JType field_jty);
257
TDYa1275a26d442012-07-26 18:58:38 -0700258 llvm::Value* Expand_HLSget(llvm::CallInst& call_inst, JType field_jty);
259 void Expand_HLSput(llvm::CallInst& call_inst, JType field_jty);
260
261 llvm::Value* Expand_HLArrayGet(llvm::CallInst& call_inst, JType field_jty);
262 void Expand_HLArrayPut(llvm::CallInst& call_inst, JType field_jty);
263
TDYa127f71bf5a2012-07-29 20:09:52 -0700264 llvm::Value* Expand_ConstString(llvm::CallInst& call_inst);
265 llvm::Value* Expand_ConstClass(llvm::CallInst& call_inst);
266
267 void Expand_MonitorEnter(llvm::CallInst& call_inst);
268 void Expand_MonitorExit(llvm::CallInst& call_inst);
269
270 void Expand_HLCheckCast(llvm::CallInst& call_inst);
271 llvm::Value* Expand_InstanceOf(llvm::CallInst& call_inst);
272
273 llvm::Value* Expand_NewInstance(llvm::CallInst& call_inst);
274
275 llvm::Value* Expand_HLInvoke(llvm::CallInst& call_inst);
276
277 llvm::Value* Expand_OptArrayLength(llvm::CallInst& call_inst);
278 llvm::Value* Expand_NewArray(llvm::CallInst& call_inst);
279 llvm::Value* Expand_HLFilledNewArray(llvm::CallInst& call_inst);
280 void Expand_HLFillArrayData(llvm::CallInst& call_inst);
281
282 llvm::Value* EmitAllocNewArray(uint32_t dex_pc,
283 llvm::Value* array_length_value,
284 uint32_t type_idx,
285 bool is_filled_new_array);
286
287 llvm::Value* EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
288 InvokeType invoke_type,
289 llvm::Value* this_addr,
290 uint32_t dex_pc,
291 bool is_fast_path);
292
TDYa1275e869b62012-07-25 00:45:39 -0700293 void EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr);
294
295 void EmitUpdateDexPC(uint32_t dex_pc);
296
297 void EmitGuard_DivZeroException(uint32_t dex_pc,
298 llvm::Value* denominator,
299 JType op_jty);
300
301 void EmitGuard_NullPointerException(uint32_t dex_pc,
302 llvm::Value* object);
303
304 void EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
305 llvm::Value* array,
306 llvm::Value* index);
307
308 void EmitGuard_ArrayException(uint32_t dex_pc,
309 llvm::Value* array,
310 llvm::Value* index);
311
312 llvm::FunctionType* GetFunctionType(uint32_t method_idx, bool is_static);
313
314 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
315
316 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
317 const char* postfix);
318
319 int32_t GetTryItemOffset(uint32_t dex_pc);
320
321 llvm::BasicBlock* GetLandingPadBasicBlock(uint32_t dex_pc);
322
323 llvm::BasicBlock* GetUnwindBasicBlock();
324
325 void EmitGuard_ExceptionLandingPad(uint32_t dex_pc);
326
327 void EmitBranchExceptionLandingPad(uint32_t dex_pc);
328
Logan Chien75e4b602012-07-23 14:24:12 -0700329 //----------------------------------------------------------------------------
330 // Expand Arithmetic Helper Intrinsics
331 //----------------------------------------------------------------------------
332
333 llvm::Value* Expand_IntegerShift(llvm::Value* src1_value,
334 llvm::Value* src2_value,
335 IntegerShiftKind kind,
336 JType op_jty);
337
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700338 public:
339 static char ID;
340
341 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb)
342 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
Logan Chiene5b8f8b2012-08-13 11:45:05 +0800343 context_(irb.getContext()), rtb_(irb.Runtime()),
344
345 // TODO: Initialize these fields correctly.
346 shadow_frame_(NULL), old_shadow_frame_(NULL), shadow_frame_size_(0),
347 compiler_(NULL), dex_file_(NULL), dex_cache_(NULL), code_item_(NULL),
348 oat_compilation_unit_(NULL), method_idx_(-1u), func_(NULL)
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700349 { }
350
351 bool runOnFunction(llvm::Function& func);
352
353 private:
354 bool InsertStackOverflowCheck(llvm::Function& func);
355
356 llvm::Value* ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
357 llvm::CallInst& call_inst);
358
359};
360
361char GBCExpanderPass::ID = 0;
362
363bool GBCExpanderPass::runOnFunction(llvm::Function& func) {
TDYa127b672d1e2012-06-28 21:21:45 -0700364 // Runtime support or stub
365 if (func.getName().startswith("art_") || func.getName().startswith("Art")) {
366 return false;
367 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700368 bool changed;
369
TDYa127b672d1e2012-06-28 21:21:45 -0700370 // TODO: Use intrinsic.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700371 changed = InsertStackOverflowCheck(func);
372
373 std::list<std::pair<llvm::CallInst*,
374 IntrinsicHelper::IntrinsicId> > work_list;
375
376 for (llvm::inst_iterator inst_iter = llvm::inst_begin(func),
377 inst_end = llvm::inst_end(func); inst_iter != inst_end; inst_iter++) {
378 // Only CallInst with its called function is dexlang intrinsic need to
379 // process
380 llvm::Instruction* inst = &*inst_iter;
381 if (llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst)) {
382 const llvm::Function* callee = call_inst->getCalledFunction();
383
384 if (callee != NULL) {
385 IntrinsicHelper::IntrinsicId intr_id =
386 intrinsic_helper_.GetIntrinsicId(callee);
387
388 if (intr_id != IntrinsicHelper::UnknownId) {
389 work_list.push_back(std::make_pair(call_inst, intr_id));
390 }
391 }
392 }
393 }
394
395 changed |= !work_list.empty();
396
397 shadow_frame_ = NULL;
398 old_shadow_frame_ = NULL;
399 shadow_frame_size_ = 0;
TDYa1275e869b62012-07-25 00:45:39 -0700400 func_ = &func;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700401
402 // Remove the instruction containing in the work_list
403 while (!work_list.empty()) {
404 llvm::CallInst* intr_inst = work_list.front().first;
405 IntrinsicHelper::IntrinsicId intr_id = work_list.front().second;
406
407 // Remove the instruction from work list
408 work_list.pop_front();
409
410 // Move the IRBuilder insert pointer
411 irb_.SetInsertPoint(intr_inst);
412
413 // Process the expansion
414 llvm::Value* new_value = ExpandIntrinsic(intr_id, *intr_inst);
415
416 // Use the new value from the expansion
417 if (new_value != NULL) {
418 intr_inst->replaceAllUsesWith(new_value);
419 }
420
421 // Remove the intrinsic instruction
422 intr_inst->eraseFromParent();
423 }
424
425 VERIFY_LLVM_FUNCTION(func);
426
427 return changed;
428}
429
430llvm::BasicBlock*
TDYa1275e869b62012-07-25 00:45:39 -0700431GBCExpanderPass::ScopedExpandToBasicBlock::
432SplitAndInsertBasicBlocksAfter(llvm::BasicBlock::iterator inst,
433 llvm::BasicBlock* begin_bb,
434 llvm::BasicBlock* end_bb) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700435 llvm::BasicBlock* original = inst->getParent();
436 llvm::Function* parent = original->getParent();
437
438 // 1. Create a new basic block A after ORIGINAL
439 llvm::BasicBlock *insert_before =
440 llvm::next(llvm::Function::iterator(original)).getNodePtrUnchecked();
441 llvm::BasicBlock* a =
TDYa1275e869b62012-07-25 00:45:39 -0700442 llvm::BasicBlock::Create(irb_.getContext(), "", parent, insert_before);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700443
444 // 2. Move all instructions in ORIGINAL after INST (included) to A
445 a->getInstList().splice(a->end(), original->getInstList(),
446 inst, original->end());
447
448 // 3. Add an unconditional branch in ORIGINAL to begin_bb
449 llvm::BranchInst::Create(begin_bb, original);
450
451 // 4. Add an unconditional branch in END_BB to A
452 llvm::BranchInst::Create(a, end_bb);
453
454 // 5. Update the PHI nodes in the successors of A. Update the PHI node entry
455 // with incoming basic block from ORIGINAL to A
456 for (llvm::succ_iterator succ_iter = llvm::succ_begin(a),
457 succ_end = llvm::succ_end(a); succ_iter != succ_end; succ_iter++) {
458 llvm::BasicBlock* succ = *succ_iter;
459 llvm::PHINode* phi;
460 for (llvm::BasicBlock::iterator inst_iter = succ->begin();
461 (phi = llvm::dyn_cast<llvm::PHINode>(inst_iter)); ++inst_iter) {
462 int idx;
463 while ((idx = phi->getBasicBlockIndex(original)) != -1) {
464 phi->setIncomingBlock(static_cast<unsigned>(idx), a);
465 }
466 }
467 }
468
469 return a;
470}
471
472llvm::Value* GBCExpanderPass::ExpandToRuntime(runtime_support::RuntimeId rt,
473 llvm::CallInst& inst) {
474 // Some GBC intrinsic can directly replace with IBC runtime. "Directly" means
475 // the arguments passed to the GBC intrinsic are as the same as IBC runtime
476 // function, therefore only called function is needed to change.
477 unsigned num_args = inst.getNumArgOperands();
478
479 if (num_args <= 0) {
480 return irb_.CreateCall(irb_.GetRuntime(rt));
481 } else {
482 std::vector<llvm::Value*> args;
483 for (unsigned i = 0; i < num_args; i++) {
484 args.push_back(inst.getArgOperand(i));
485 }
486
487 return irb_.CreateCall(irb_.GetRuntime(rt), args);
488 }
489}
490
491bool
492GBCExpanderPass::EmitStackOverflowCheck(llvm::Instruction* first_non_alloca) {
TDYa1275e869b62012-07-25 00:45:39 -0700493 ScopedExpandToBasicBlock eb(irb_, first_non_alloca);
494
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700495 llvm::Function* func = first_non_alloca->getParent()->getParent();
496 llvm::Module* module = func->getParent();
497
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700498 // Call llvm intrinsic function to get frame address.
499 llvm::Function* frameaddress =
500 llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
501
502 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
503 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
504
505 // Cast i8* to int
506 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
507
508 // Get thread.stack_end_
509 llvm::Value* stack_end =
510 irb_.Runtime().EmitLoadFromThreadOffset(Thread::StackEndOffset().Int32Value(),
511 irb_.getPtrEquivIntTy(),
512 kTBAARuntimeInfo);
513
514 // Check the frame address < thread.stack_end_ ?
515 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
516
517 llvm::BasicBlock* block_exception =
518 llvm::BasicBlock::Create(context_, "stack_overflow", func);
519
520 llvm::BasicBlock* block_continue =
521 llvm::BasicBlock::Create(context_, "stack_overflow_cont", func);
522
523 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
524
525 // If stack overflow, throw exception.
526 irb_.SetInsertPoint(block_exception);
527 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowStackOverflowException));
528
529 // Unwind.
530 llvm::Type* ret_type = func->getReturnType();
531 if (ret_type->isVoidTy()) {
532 irb_.CreateRetVoid();
533 } else {
534 // The return value is ignored when there's an exception. MethodCompiler
535 // returns zero value under the the corresponding return type in this case.
536 // GBCExpander returns LLVM undef value here for brevity
537 irb_.CreateRet(llvm::UndefValue::get(ret_type));
538 }
539
540 irb_.SetInsertPoint(block_continue);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700541 return true;
542}
543
544llvm::Value* GBCExpanderPass::EmitLoadDexCacheAddr(MemberOffset offset) {
545 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
546
547 return irb_.LoadFromObjectOffset(method_object_addr,
548 offset.Int32Value(),
549 irb_.getJObjectTy(),
550 kTBAAConstJObject);
551}
552
553llvm::Value*
554GBCExpanderPass::EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
555 llvm::Value* static_storage_dex_cache_addr =
556 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
557
558 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
559
560 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
561}
562
563llvm::Value*
564GBCExpanderPass::EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
565 llvm::Value* resolved_type_dex_cache_addr =
566 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
567
568 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
569
570 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
571}
572
573llvm::Value* GBCExpanderPass::
574EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
575 llvm::Value* resolved_method_dex_cache_addr =
576 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
577
578 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
579
580 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
581}
582
583llvm::Value* GBCExpanderPass::
584EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
585 llvm::Value* string_dex_cache_addr =
586 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
587
588 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
589
590 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
591}
592
593llvm::Value* GBCExpanderPass::EmitLoadMethodObjectAddr() {
594 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
595 return parent_func->arg_begin();
596}
597
598llvm::Value* GBCExpanderPass::EmitLoadArrayLength(llvm::Value* array) {
599 // Load array length
600 return irb_.LoadFromObjectOffset(array,
601 Array::LengthOffset().Int32Value(),
602 irb_.getJIntTy(),
603 kTBAAConstJObject);
604
605}
606
607llvm::Value*
608GBCExpanderPass::EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
609 llvm::Value* callee_method_object_field_addr =
610 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
611
612 return irb_.CreateLoad(callee_method_object_field_addr, kTBAAJRuntime);
613}
614
615llvm::Value* GBCExpanderPass::
616EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, llvm::Value* this_addr) {
617 // Load class object of *this* pointer
618 llvm::Value* class_object_addr =
619 irb_.LoadFromObjectOffset(this_addr,
620 Object::ClassOffset().Int32Value(),
621 irb_.getJObjectTy(),
622 kTBAAConstJObject);
623
624 // Load vtable address
625 llvm::Value* vtable_addr =
626 irb_.LoadFromObjectOffset(class_object_addr,
627 Class::VTableOffset().Int32Value(),
628 irb_.getJObjectTy(),
629 kTBAAConstJObject);
630
631 // Load callee method object
632 llvm::Value* vtable_idx_value =
633 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
634
635 llvm::Value* method_field_addr =
636 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
637
638 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
639}
640
641// Emit Array GetElementPtr
642llvm::Value* GBCExpanderPass::EmitArrayGEP(llvm::Value* array_addr,
643 llvm::Value* index_value,
644 JType elem_jty) {
645
646 int data_offset;
647 if (elem_jty == kLong || elem_jty == kDouble ||
648 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
649 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
650 } else {
651 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
652 }
653
654 llvm::Constant* data_offset_value =
655 irb_.getPtrEquivInt(data_offset);
656
657 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
658
659 llvm::Value* array_data_addr =
660 irb_.CreatePtrDisp(array_addr, data_offset_value,
661 elem_type->getPointerTo());
662
663 return irb_.CreateGEP(array_data_addr, index_value);
664}
665
666void GBCExpanderPass::Expand_TestSuspend(llvm::CallInst& call_inst) {
TDYa1275e869b62012-07-25 00:45:39 -0700667 ScopedExpandToBasicBlock eb(irb_, &call_inst);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700668 irb_.Runtime().EmitTestSuspend();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700669 return;
670}
671
TDYa1279a129452012-07-19 03:10:08 -0700672void GBCExpanderPass::Expand_MarkGCCard(llvm::CallInst& call_inst) {
TDYa1275e869b62012-07-25 00:45:39 -0700673 ScopedExpandToBasicBlock eb(irb_, &call_inst);
TDYa1279a129452012-07-19 03:10:08 -0700674 irb_.Runtime().EmitMarkGCCard(call_inst.getArgOperand(0), call_inst.getArgOperand(1));
TDYa1279a129452012-07-19 03:10:08 -0700675 return;
676}
677
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700678llvm::Value* GBCExpanderPass::Expand_GetException() {
679 // Get thread-local exception field address
680 llvm::Value* exception_object_addr =
681 irb_.Runtime().EmitLoadFromThreadOffset(Thread::ExceptionOffset().Int32Value(),
682 irb_.getJObjectTy(),
683 kTBAAJRuntime);
684
685 // Set thread-local exception field address to NULL
686 irb_.Runtime().EmitStoreToThreadOffset(Thread::ExceptionOffset().Int32Value(),
687 irb_.getJNull(),
688 kTBAAJRuntime);
689
690 return exception_object_addr;
691}
692
693llvm::Value*
694GBCExpanderPass::Expand_LoadStringFromDexCache(llvm::Value* string_idx_value) {
695 uint32_t string_idx =
696 llvm::cast<llvm::ConstantInt>(string_idx_value)->getZExtValue();
697
698 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
699
700 return irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
701}
702
703llvm::Value*
704GBCExpanderPass::Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value) {
705 uint32_t type_idx =
706 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
707
708 llvm::Value* type_field_addr =
709 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
710
711 return irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
712}
713
714void GBCExpanderPass::Expand_LockObject(llvm::Value* obj) {
TDYa1275e869b62012-07-25 00:45:39 -0700715 ScopedExpandToBasicBlock eb(irb_, irb_.GetInsertPoint());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700716 rtb_.EmitLockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700717 return;
718}
719
720void GBCExpanderPass::Expand_UnlockObject(llvm::Value* obj) {
TDYa1275e869b62012-07-25 00:45:39 -0700721 ScopedExpandToBasicBlock eb(irb_, irb_.GetInsertPoint());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700722 rtb_.EmitUnlockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700723 return;
724}
725
726llvm::Value* GBCExpanderPass::Expand_ArrayGet(llvm::Value* array_addr,
727 llvm::Value* index_value,
728 JType elem_jty) {
729 llvm::Value* array_elem_addr =
730 EmitArrayGEP(array_addr, index_value, elem_jty);
731
732 return irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
733}
734
735void GBCExpanderPass::Expand_ArrayPut(llvm::Value* new_value,
736 llvm::Value* array_addr,
737 llvm::Value* index_value,
738 JType elem_jty) {
739 llvm::Value* array_elem_addr =
740 EmitArrayGEP(array_addr, index_value, elem_jty);
741
742 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
743
744 return;
745}
746
747void GBCExpanderPass::Expand_FilledNewArray(llvm::CallInst& call_inst) {
748 // Most of the codes refer to MethodCompiler::EmitInsn_FilledNewArray
749 llvm::Value* array = call_inst.getArgOperand(0);
750
751 uint32_t element_jty =
752 llvm::cast<llvm::ConstantInt>(call_inst.getArgOperand(1))->getZExtValue();
753
754 DCHECK(call_inst.getNumArgOperands() > 2);
755 unsigned num_elements = (call_inst.getNumArgOperands() - 2);
756
757 bool is_elem_int_ty = (static_cast<JType>(element_jty) == kInt);
758
759 uint32_t alignment;
760 llvm::Constant* elem_size;
761 llvm::PointerType* field_type;
762
763 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
764 // as the element, thus we are only checking 2 cases: primitive int and
765 // non-primitive type.
766 if (is_elem_int_ty) {
767 alignment = sizeof(int32_t);
768 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
769 field_type = irb_.getJIntTy()->getPointerTo();
770 } else {
771 alignment = irb_.getSizeOfPtrEquivInt();
772 elem_size = irb_.getSizeOfPtrEquivIntValue();
773 field_type = irb_.getJObjectTy()->getPointerTo();
774 }
775
776 llvm::Value* data_field_offset =
777 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
778
779 llvm::Value* data_field_addr =
780 irb_.CreatePtrDisp(array, data_field_offset, field_type);
781
782 for (unsigned i = 0; i < num_elements; ++i) {
783 // Values to fill the array begin at the 3rd argument
784 llvm::Value* reg_value = call_inst.getArgOperand(2 + i);
785
786 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
787
788 data_field_addr =
789 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
790 }
791
792 return;
793}
794
795llvm::Value* GBCExpanderPass::Expand_IGetFast(llvm::Value* field_offset_value,
796 llvm::Value* /*is_volatile_value*/,
797 llvm::Value* object_addr,
798 JType field_jty) {
799 int field_offset =
800 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
801
802 DCHECK_GE(field_offset, 0);
803
804 llvm::PointerType* field_type =
805 irb_.getJType(field_jty, kField)->getPointerTo();
806
807 field_offset_value = irb_.getPtrEquivInt(field_offset);
808
809 llvm::Value* field_addr =
810 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
811
812 // TODO: Check is_volatile. We need to generate atomic load instruction
813 // when is_volatile is true.
814 return irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
815}
816
817void GBCExpanderPass::Expand_IPutFast(llvm::Value* field_offset_value,
818 llvm::Value* /* is_volatile_value */,
819 llvm::Value* object_addr,
820 llvm::Value* new_value,
821 JType field_jty) {
822 int field_offset =
823 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
824
825 DCHECK_GE(field_offset, 0);
826
827 llvm::PointerType* field_type =
828 irb_.getJType(field_jty, kField)->getPointerTo();
829
830 field_offset_value = irb_.getPtrEquivInt(field_offset);
831
832 llvm::Value* field_addr =
833 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
834
835 // TODO: Check is_volatile. We need to generate atomic store instruction
836 // when is_volatile is true.
837 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
838
839 return;
840}
841
842llvm::Value* GBCExpanderPass::Expand_SGetFast(llvm::Value* static_storage_addr,
843 llvm::Value* field_offset_value,
844 llvm::Value* /*is_volatile_value*/,
845 JType field_jty) {
846 int field_offset =
847 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
848
849 DCHECK_GE(field_offset, 0);
850
851 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
852
853 llvm::Value* static_field_addr =
854 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
855 irb_.getJType(field_jty, kField)->getPointerTo());
856
857 // TODO: Check is_volatile. We need to generate atomic store instruction
858 // when is_volatile is true.
859 return irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
860}
861
862void GBCExpanderPass::Expand_SPutFast(llvm::Value* static_storage_addr,
863 llvm::Value* field_offset_value,
864 llvm::Value* /* is_volatile_value */,
865 llvm::Value* new_value,
866 JType field_jty) {
867 int field_offset =
868 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
869
870 DCHECK_GE(field_offset, 0);
871
872 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
873
874 llvm::Value* static_field_addr =
875 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
876 irb_.getJType(field_jty, kField)->getPointerTo());
877
878 // TODO: Check is_volatile. We need to generate atomic store instruction
879 // when is_volatile is true.
880 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
881
882 return;
883}
884
885llvm::Value*
886GBCExpanderPass::Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr) {
887 return irb_.LoadFromObjectOffset(method_object_addr,
888 Method::DeclaringClassOffset().Int32Value(),
889 irb_.getJObjectTy(),
890 kTBAAConstJObject);
891}
892
893llvm::Value*
894GBCExpanderPass::Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value) {
895 uint32_t type_idx =
896 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
897
898 llvm::Value* storage_field_addr =
899 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
900
901 return irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
902}
903
904llvm::Value*
905GBCExpanderPass::Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value) {
906 uint32_t callee_method_idx =
907 llvm::cast<llvm::ConstantInt>(callee_method_idx_value)->getZExtValue();
908
909 return EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
910}
911
912llvm::Value* GBCExpanderPass::Expand_GetVirtualCalleeMethodObjAddrFast(
913 llvm::Value* vtable_idx_value,
914 llvm::Value* this_addr) {
915 int vtable_idx =
916 llvm::cast<llvm::ConstantInt>(vtable_idx_value)->getSExtValue();
917
918 return EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
919}
920
921llvm::Value* GBCExpanderPass::Expand_Invoke(llvm::CallInst& call_inst) {
922 // Most of the codes refer to MethodCompiler::EmitInsn_Invoke
923 llvm::Value* callee_method_object_addr = call_inst.getArgOperand(0);
924 unsigned num_args = call_inst.getNumArgOperands();
925 llvm::Type* ret_type = call_inst.getType();
926
927 // Determine the function type of the callee method
928 std::vector<llvm::Type*> args_type;
929 std::vector<llvm::Value*> args;
930 for (unsigned i = 0; i < num_args; i++) {
931 args.push_back(call_inst.getArgOperand(i));
932 args_type.push_back(args[i]->getType());
933 }
934
935 llvm::FunctionType* callee_method_type =
936 llvm::FunctionType::get(ret_type, args_type, false);
937
938 llvm::Value* code_addr =
939 irb_.LoadFromObjectOffset(callee_method_object_addr,
940 Method::GetCodeOffset().Int32Value(),
941 callee_method_type->getPointerTo(),
942 kTBAAJRuntime);
943
944 // Invoke callee
945 llvm::Value* retval = irb_.CreateCall(code_addr, args);
946
947 return retval;
948}
949
TDYa1274ec8ccd2012-08-11 07:04:57 -0700950llvm::Value* GBCExpanderPass::Expand_DivRem(llvm::CallInst& call_inst,
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700951 bool is_div, JType op_jty) {
TDYa1274ec8ccd2012-08-11 07:04:57 -0700952 llvm::Value* dividend = call_inst.getArgOperand(0);
953 llvm::Value* divisor = call_inst.getArgOperand(1);
954#if defined(ART_USE_QUICK_COMPILER)
955 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
956 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
957#endif
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700958 // Most of the codes refer to MethodCompiler::EmitIntDivRemResultComputation
959
960 // Check the special case: MININT / -1 = MININT
961 // That case will cause overflow, which is undefined behavior in llvm.
962 // So we check the divisor is -1 or not, if the divisor is -1, we do
963 // the special path to avoid undefined behavior.
964 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
965 llvm::Value* zero = irb_.getJZero(op_jty);
966 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
967
TDYa1275e869b62012-07-25 00:45:39 -0700968 ScopedExpandToBasicBlock eb(irb_, irb_.GetInsertPoint());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700969
TDYa1275e869b62012-07-25 00:45:39 -0700970 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700971 llvm::BasicBlock* eq_neg_one = llvm::BasicBlock::Create(context_, "", parent);
972 llvm::BasicBlock* ne_neg_one = llvm::BasicBlock::Create(context_, "", parent);
973 llvm::BasicBlock* neg_one_cont =
974 llvm::BasicBlock::Create(context_, "", parent);
975
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700976 llvm::Value* is_equal_neg_one = irb_.CreateICmpEQ(divisor, neg_one);
977 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
978
979 // If divisor == -1
980 irb_.SetInsertPoint(eq_neg_one);
981 llvm::Value* eq_result;
982 if (is_div) {
983 // We can just change from "dividend div -1" to "neg dividend". The sub
984 // don't care the sign/unsigned because of two's complement representation.
985 // And the behavior is what we want:
986 // -(2^n) (2^n)-1
987 // MININT < k <= MAXINT -> mul k -1 = -k
988 // MININT == k -> mul k -1 = k
989 //
990 // LLVM use sub to represent 'neg'
991 eq_result = irb_.CreateSub(zero, dividend);
992 } else {
993 // Everything modulo -1 will be 0.
994 eq_result = zero;
995 }
996 irb_.CreateBr(neg_one_cont);
997
998 // If divisor != -1, just do the division.
999 irb_.SetInsertPoint(ne_neg_one);
1000 llvm::Value* ne_result;
1001 if (is_div) {
1002 ne_result = irb_.CreateSDiv(dividend, divisor);
1003 } else {
1004 ne_result = irb_.CreateSRem(dividend, divisor);
1005 }
1006 irb_.CreateBr(neg_one_cont);
1007
1008 irb_.SetInsertPoint(neg_one_cont);
1009 llvm::PHINode* result = irb_.CreatePHI(op_type, 2);
1010 result->addIncoming(eq_result, eq_neg_one);
1011 result->addIncoming(ne_result, ne_neg_one);
1012
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001013 return result;
1014}
1015
1016void GBCExpanderPass::Expand_AllocaShadowFrame(llvm::Value* num_entry_value) {
1017 // Most of the codes refer to MethodCompiler::EmitPrologueAllocShadowFrame and
1018 // MethodCompiler::EmitPushShadowFrame
1019 shadow_frame_size_ =
1020 llvm::cast<llvm::ConstantInt>(num_entry_value)->getZExtValue();
1021
1022 llvm::StructType* shadow_frame_type =
1023 irb_.getShadowFrameTy(shadow_frame_size_);
1024
1025 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
1026
1027 // Alloca a pointer to old shadow frame
1028 old_shadow_frame_ =
1029 irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
1030
1031 // Zero-initialization of the shadow frame table
1032 llvm::Value* shadow_frame_table =
1033 irb_.CreateConstGEP2_32(shadow_frame_, 0, 1);
1034 llvm::Type* table_type = shadow_frame_type->getElementType(1);
1035
1036 llvm::ConstantAggregateZero* zero_initializer =
1037 llvm::ConstantAggregateZero::get(table_type);
1038
1039 irb_.CreateStore(zero_initializer, shadow_frame_table, kTBAAShadowFrame);
1040
1041 // Push the shadow frame
1042 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1043
1044 // Push the shadow frame
1045 llvm::Value* shadow_frame_upcast =
1046 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
1047
1048 llvm::Value* result = rtb_.EmitPushShadowFrame(shadow_frame_upcast,
1049 method_object_addr,
1050 shadow_frame_size_);
1051
1052 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
1053
1054 return;
1055}
1056
1057void GBCExpanderPass::Expand_SetShadowFrameEntry(llvm::Value* obj,
1058 llvm::Value* entry_idx) {
1059 DCHECK(shadow_frame_ != NULL);
1060
1061 llvm::Value* gep_index[] = {
1062 irb_.getInt32(0), // No pointer displacement
1063 irb_.getInt32(1), // SIRT
1064 entry_idx // Pointer field
1065 };
1066
1067 llvm::Value* entry_addr = irb_.CreateGEP(shadow_frame_, gep_index);
1068 irb_.CreateStore(obj, entry_addr, kTBAAShadowFrame);
1069 return;
1070}
1071
1072void GBCExpanderPass::Expand_PopShadowFrame() {
1073 rtb_.EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
1074 return;
1075}
1076
1077void GBCExpanderPass::Expand_UpdateDexPC(llvm::Value* dex_pc_value) {
1078 irb_.StoreToObjectOffset(shadow_frame_,
1079 ShadowFrame::DexPCOffset(),
1080 dex_pc_value,
1081 kTBAAShadowFrame);
1082 return;
1083}
1084
1085bool GBCExpanderPass::InsertStackOverflowCheck(llvm::Function& func) {
1086 // DexLang generates all alloca instruction in the first basic block of the
1087 // FUNC and also there's no any alloca instructions after the first non-alloca
1088 // instruction
1089
1090 llvm::BasicBlock::iterator first_non_alloca = func.front().begin();
1091 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1092 ++first_non_alloca;
1093 }
1094
1095 // Insert stack overflow check codes before first_non_alloca (i.e., after all
1096 // alloca instructions)
1097 return EmitStackOverflowCheck(&*first_non_alloca);
1098}
1099
TDYa1275e869b62012-07-25 00:45:39 -07001100// ==== High-level intrinsic expander ==========================================
1101
TDYa127a1b21852012-07-23 03:20:39 -07001102llvm::Value* GBCExpanderPass::Expand_FPCompare(llvm::Value* src1_value,
1103 llvm::Value* src2_value,
1104 bool gt_bias) {
1105 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1106 llvm::Value* cmp_lt;
1107
1108 if (gt_bias) {
1109 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1110 } else {
1111 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1112 }
1113
1114 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1115}
1116
1117llvm::Value* GBCExpanderPass::Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value) {
1118 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1119 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1120
1121 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1122}
1123
1124llvm::Value* GBCExpanderPass::EmitCompareResultSelection(llvm::Value* cmp_eq,
1125 llvm::Value* cmp_lt) {
1126
1127 llvm::Constant* zero = irb_.getJInt(0);
1128 llvm::Constant* pos1 = irb_.getJInt(1);
1129 llvm::Constant* neg1 = irb_.getJInt(-1);
1130
1131 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1132 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1133
1134 return result_eq;
1135}
1136
Logan Chien75e4b602012-07-23 14:24:12 -07001137llvm::Value* GBCExpanderPass::Expand_IntegerShift(llvm::Value* src1_value,
1138 llvm::Value* src2_value,
1139 IntegerShiftKind kind,
1140 JType op_jty) {
1141 DCHECK(op_jty == kInt || op_jty == kLong);
1142
1143 // Mask and zero-extend RHS properly
1144 if (op_jty == kInt) {
1145 src2_value = irb_.CreateAnd(src2_value, 0x1f);
1146 } else {
1147 llvm::Value* masked_src2_value = irb_.CreateAnd(src2_value, 0x3f);
1148 src2_value = irb_.CreateZExt(masked_src2_value, irb_.getJLongTy());
1149 }
1150
1151 // Create integer shift llvm instruction
1152 switch (kind) {
1153 case kIntegerSHL:
1154 return irb_.CreateShl(src1_value, src2_value);
1155
1156 case kIntegerSHR:
1157 return irb_.CreateAShr(src1_value, src2_value);
1158
1159 case kIntegerUSHR:
1160 return irb_.CreateLShr(src1_value, src2_value);
1161
1162 default:
1163 LOG(FATAL) << "Unknown integer shift kind: " << kind;
1164 return NULL;
1165 }
1166}
1167
TDYa1275a26d442012-07-26 18:58:38 -07001168llvm::Value* GBCExpanderPass::Expand_HLArrayGet(llvm::CallInst& call_inst,
1169 JType elem_jty) {
1170 ScopedExpandToBasicBlock eb(irb_, &call_inst);
1171
1172 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1173 llvm::Value* array_addr = call_inst.getArgOperand(1);
1174 llvm::Value* index_value = call_inst.getArgOperand(2);
1175
1176 // TODO: opt_flags
1177 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
1178
1179 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1180
1181 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
1182
1183 switch (elem_jty) {
1184 case kVoid:
1185 break;
1186
1187 case kBoolean:
1188 case kChar:
1189 array_elem_value = irb_.CreateZExt(array_elem_value, irb_.getJType(elem_jty, kReg));
1190 break;
1191
1192 case kByte:
1193 case kShort:
1194 array_elem_value = irb_.CreateSExt(array_elem_value, irb_.getJType(elem_jty, kReg));
1195 break;
1196
1197 case kInt:
1198 case kLong:
1199 case kFloat:
1200 case kDouble:
1201 case kObject:
1202 break;
1203
1204 default:
1205 LOG(FATAL) << "Unknown java type: " << elem_jty;
1206 }
1207
1208 return array_elem_value;
1209}
1210
1211
1212void GBCExpanderPass::Expand_HLArrayPut(llvm::CallInst& call_inst,
1213 JType elem_jty) {
1214 ScopedExpandToBasicBlock eb(irb_, &call_inst);
1215
1216 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1217 llvm::Value* new_value = call_inst.getArgOperand(1);
1218 llvm::Value* array_addr = call_inst.getArgOperand(2);
1219 llvm::Value* index_value = call_inst.getArgOperand(3);
1220
1221 // TODO: opt_flags
1222 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
1223
1224 switch (elem_jty) {
1225 case kVoid:
1226 break;
1227
1228 case kBoolean:
1229 case kChar:
1230 new_value = irb_.CreateTrunc(new_value, irb_.getJType(elem_jty, kArray));
1231 break;
1232
1233 case kInt:
1234 case kLong:
1235 case kFloat:
1236 case kDouble:
1237 case kObject:
1238 break;
1239
1240 default:
1241 LOG(FATAL) << "Unknown java type: " << elem_jty;
1242 }
1243
1244 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1245
1246 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
1247 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::CheckPutArrayElement);
1248
1249 irb_.CreateCall2(runtime_func, new_value, array_addr);
1250
1251 EmitGuard_ExceptionLandingPad(dex_pc);
1252
1253 EmitMarkGCCard(new_value, array_addr);
1254 }
1255
1256 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1257
1258 return;
1259}
1260
TDYa1275e869b62012-07-25 00:45:39 -07001261llvm::Value* GBCExpanderPass::Expand_HLIGet(llvm::CallInst& call_inst,
1262 JType field_jty) {
1263 ScopedExpandToBasicBlock eb(irb_, &call_inst);
1264
1265 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1266 llvm::Value* object_addr = call_inst.getArgOperand(1);
1267 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(2));
1268
1269 // TODO: opt_flags
1270 EmitGuard_NullPointerException(dex_pc, object_addr);
1271
1272 llvm::Value* field_value;
1273
1274 int field_offset;
1275 bool is_volatile;
1276 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
1277 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
1278
1279 if (!is_fast_path) {
1280 llvm::Function* runtime_func;
1281
1282 if (field_jty == kObject) {
1283 runtime_func = irb_.GetRuntime(runtime_support::GetObjectInstance);
1284 } else if (field_jty == kLong || field_jty == kDouble) {
1285 runtime_func = irb_.GetRuntime(runtime_support::Get64Instance);
1286 } else {
1287 runtime_func = irb_.GetRuntime(runtime_support::Get32Instance);
1288 }
1289
1290 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
1291
1292 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1293
1294 EmitUpdateDexPC(dex_pc);
1295
1296 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
1297 method_object_addr, object_addr);
1298
1299 EmitGuard_ExceptionLandingPad(dex_pc);
1300
1301 } else {
1302 DCHECK_GE(field_offset, 0);
1303
1304 llvm::PointerType* field_type =
1305 irb_.getJType(field_jty, kField)->getPointerTo();
1306
1307 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
1308
1309 llvm::Value* field_addr =
1310 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1311
1312 // TODO: Check is_volatile. We need to generate atomic load instruction
1313 // when is_volatile is true.
1314 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
1315 }
1316
1317 if (field_jty == kFloat || field_jty == kDouble) {
TDYa1275a26d442012-07-26 18:58:38 -07001318 field_value = irb_.CreateBitCast(field_value, irb_.getJType(field_jty, kAccurate));
TDYa1275e869b62012-07-25 00:45:39 -07001319 }
1320
1321 return field_value;
1322}
1323
1324void GBCExpanderPass::Expand_HLIPut(llvm::CallInst& call_inst,
1325 JType field_jty) {
1326 ScopedExpandToBasicBlock eb(irb_, &call_inst);
1327
1328 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1329 llvm::Value* object_addr = call_inst.getArgOperand(1);
1330 llvm::Value* new_value = call_inst.getArgOperand(2);
1331 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(3));
1332
1333 if (field_jty == kFloat || field_jty == kDouble) {
TDYa1275a26d442012-07-26 18:58:38 -07001334 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty, kField));
TDYa1275e869b62012-07-25 00:45:39 -07001335 }
1336
1337 // TODO: opt_flags
1338 EmitGuard_NullPointerException(dex_pc, object_addr);
1339
1340 int field_offset;
1341 bool is_volatile;
1342 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
1343 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
1344
1345 if (!is_fast_path) {
1346 llvm::Function* runtime_func;
1347
1348 if (field_jty == kObject) {
1349 runtime_func = irb_.GetRuntime(runtime_support::SetObjectInstance);
1350 } else if (field_jty == kLong || field_jty == kDouble) {
1351 runtime_func = irb_.GetRuntime(runtime_support::Set64Instance);
1352 } else {
1353 runtime_func = irb_.GetRuntime(runtime_support::Set32Instance);
1354 }
1355
1356 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1357
1358 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1359
1360 EmitUpdateDexPC(dex_pc);
1361
1362 irb_.CreateCall4(runtime_func, field_idx_value,
1363 method_object_addr, object_addr, new_value);
1364
1365 EmitGuard_ExceptionLandingPad(dex_pc);
1366
1367 } else {
1368 DCHECK_GE(field_offset, 0);
1369
1370 llvm::PointerType* field_type =
1371 irb_.getJType(field_jty, kField)->getPointerTo();
1372
1373 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
1374
1375 llvm::Value* field_addr =
1376 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1377
1378 // TODO: Check is_volatile. We need to generate atomic store instruction
1379 // when is_volatile is true.
1380 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1381
1382 if (field_jty == kObject) { // If put an object, mark the GC card table.
1383 EmitMarkGCCard(new_value, object_addr);
1384 }
1385 }
1386
1387 return;
1388}
1389
TDYa127f71bf5a2012-07-29 20:09:52 -07001390llvm::Value* GBCExpanderPass::EmitLoadConstantClass(uint32_t dex_pc,
1391 uint32_t type_idx) {
1392 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1393 *dex_file_, type_idx)) {
1394 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1395
1396 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1397
1398 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1399
1400 llvm::Function* runtime_func =
1401 irb_.GetRuntime(runtime_support::InitializeTypeAndVerifyAccess);
1402
1403 EmitUpdateDexPC(dex_pc);
1404
1405 llvm::Value* type_object_addr =
1406 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1407
1408 EmitGuard_ExceptionLandingPad(dex_pc);
1409
1410 return type_object_addr;
1411
1412 } else {
1413 // Try to load the class (type) object from the test cache.
1414 llvm::Value* type_field_addr =
1415 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1416
1417 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
1418
1419 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1420 return type_object_addr;
1421 }
1422
1423 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1424
1425 // Test whether class (type) object is in the dex cache or not
1426 llvm::Value* equal_null =
1427 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1428
1429 llvm::BasicBlock* block_cont =
1430 CreateBasicBlockWithDexPC(dex_pc, "cont");
1431
1432 llvm::BasicBlock* block_load_class =
1433 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1434
1435 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
1436
1437 // Failback routine to load the class object
1438 irb_.SetInsertPoint(block_load_class);
1439
1440 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeType);
1441
1442 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1443
1444 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1445
1446 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1447
1448 EmitUpdateDexPC(dex_pc);
1449
1450 llvm::Value* loaded_type_object_addr =
1451 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1452
1453 EmitGuard_ExceptionLandingPad(dex_pc);
1454
1455 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1456
1457 irb_.CreateBr(block_cont);
1458
1459 // Now the class object must be loaded
1460 irb_.SetInsertPoint(block_cont);
1461
1462 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1463
1464 phi->addIncoming(type_object_addr, block_original);
1465 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1466
1467 return phi;
1468 }
1469}
1470
TDYa1275a26d442012-07-26 18:58:38 -07001471llvm::Value* GBCExpanderPass::EmitLoadStaticStorage(uint32_t dex_pc,
1472 uint32_t type_idx) {
1473 llvm::BasicBlock* block_load_static =
1474 CreateBasicBlockWithDexPC(dex_pc, "load_static");
1475
1476 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1477
1478 // Load static storage from dex cache
1479 llvm::Value* storage_field_addr =
1480 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1481
1482 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
1483
1484 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1485
1486 // Test: Is the static storage of this class initialized?
1487 llvm::Value* equal_null =
1488 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
1489
1490 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
1491
1492 // Failback routine to load the class object
1493 irb_.SetInsertPoint(block_load_static);
1494
1495 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeStaticStorage);
1496
1497 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1498
1499 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1500
1501 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1502
1503 EmitUpdateDexPC(dex_pc);
1504
1505 llvm::Value* loaded_storage_object_addr =
1506 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1507
1508 EmitGuard_ExceptionLandingPad(dex_pc);
1509
1510 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
1511
1512 irb_.CreateBr(block_cont);
1513
1514 // Now the class object must be loaded
1515 irb_.SetInsertPoint(block_cont);
1516
1517 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1518
1519 phi->addIncoming(storage_object_addr, block_original);
1520 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
1521
1522 return phi;
1523}
1524
1525llvm::Value* GBCExpanderPass::Expand_HLSget(llvm::CallInst& call_inst,
1526 JType field_jty) {
1527 ScopedExpandToBasicBlock eb(irb_, &call_inst);
1528
1529 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1530 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1531
1532 int field_offset;
1533 int ssb_index;
1534 bool is_referrers_class;
1535 bool is_volatile;
1536
1537 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
1538 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1539 is_referrers_class, is_volatile, false);
1540
1541 llvm::Value* static_field_value;
1542
1543 if (!is_fast_path) {
1544 llvm::Function* runtime_func;
1545
1546 if (field_jty == kObject) {
1547 runtime_func = irb_.GetRuntime(runtime_support::GetObjectStatic);
1548 } else if (field_jty == kLong || field_jty == kDouble) {
1549 runtime_func = irb_.GetRuntime(runtime_support::Get64Static);
1550 } else {
1551 runtime_func = irb_.GetRuntime(runtime_support::Get32Static);
1552 }
1553
1554 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1555
1556 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1557
1558 EmitUpdateDexPC(dex_pc);
1559
1560 static_field_value =
1561 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
1562
1563 EmitGuard_ExceptionLandingPad(dex_pc);
1564
1565 } else {
1566 DCHECK_GE(field_offset, 0);
1567
1568 llvm::Value* static_storage_addr = NULL;
1569
1570 if (is_referrers_class) {
1571 // Fast path, static storage base is this method's class
1572 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1573
1574 static_storage_addr =
1575 irb_.LoadFromObjectOffset(method_object_addr,
1576 Method::DeclaringClassOffset().Int32Value(),
1577 irb_.getJObjectTy(),
1578 kTBAAConstJObject);
1579 } else {
1580 // Medium path, static storage base in a different class which
1581 // requires checks that the other class is initialized
1582 DCHECK_GE(ssb_index, 0);
1583 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1584 }
1585
1586 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1587
1588 llvm::Value* static_field_addr =
1589 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1590 irb_.getJType(field_jty, kField)->getPointerTo());
1591
1592 // TODO: Check is_volatile. We need to generate atomic load instruction
1593 // when is_volatile is true.
1594 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
1595 }
1596
1597 if (field_jty == kFloat || field_jty == kDouble) {
1598 static_field_value =
1599 irb_.CreateBitCast(static_field_value, irb_.getJType(field_jty, kAccurate));
1600 }
1601
1602 return static_field_value;
1603}
1604
1605void GBCExpanderPass::Expand_HLSput(llvm::CallInst& call_inst,
1606 JType field_jty) {
1607 ScopedExpandToBasicBlock eb(irb_, &call_inst);
1608
1609 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1610 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1611 llvm::Value* new_value = call_inst.getArgOperand(1);
1612
1613 if (field_jty == kFloat || field_jty == kDouble) {
1614 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty, kField));
1615 }
1616
1617 int field_offset;
1618 int ssb_index;
1619 bool is_referrers_class;
1620 bool is_volatile;
1621
1622 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
1623 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1624 is_referrers_class, is_volatile, true);
1625
1626 if (!is_fast_path) {
1627 llvm::Function* runtime_func;
1628
1629 if (field_jty == kObject) {
1630 runtime_func = irb_.GetRuntime(runtime_support::SetObjectStatic);
1631 } else if (field_jty == kLong || field_jty == kDouble) {
1632 runtime_func = irb_.GetRuntime(runtime_support::Set64Static);
1633 } else {
1634 runtime_func = irb_.GetRuntime(runtime_support::Set32Static);
1635 }
1636
1637 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1638
1639 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1640
1641 EmitUpdateDexPC(dex_pc);
1642
1643 irb_.CreateCall3(runtime_func, field_idx_value,
1644 method_object_addr, new_value);
1645
1646 EmitGuard_ExceptionLandingPad(dex_pc);
1647
1648 } else {
1649 DCHECK_GE(field_offset, 0);
1650
1651 llvm::Value* static_storage_addr = NULL;
1652
1653 if (is_referrers_class) {
1654 // Fast path, static storage base is this method's class
1655 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1656
1657 static_storage_addr =
1658 irb_.LoadFromObjectOffset(method_object_addr,
1659 Method::DeclaringClassOffset().Int32Value(),
1660 irb_.getJObjectTy(),
1661 kTBAAConstJObject);
1662 } else {
1663 // Medium path, static storage base in a different class which
1664 // requires checks that the other class is initialized
1665 DCHECK_GE(ssb_index, 0);
1666 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1667 }
1668
1669 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1670
1671 llvm::Value* static_field_addr =
1672 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1673 irb_.getJType(field_jty, kField)->getPointerTo());
1674
1675 // TODO: Check is_volatile. We need to generate atomic store instruction
1676 // when is_volatile is true.
1677 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1678
1679 if (field_jty == kObject) { // If put an object, mark the GC card table.
1680 EmitMarkGCCard(new_value, static_storage_addr);
1681 }
1682 }
1683
1684 return;
1685}
1686
TDYa127f71bf5a2012-07-29 20:09:52 -07001687llvm::Value* GBCExpanderPass::Expand_ConstString(llvm::CallInst& call_inst) {
1688 ScopedExpandToBasicBlock eb(irb_, &call_inst);
1689
1690 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) {
1748 ScopedExpandToBasicBlock eb(irb_, &call_inst);
1749
1750 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1751 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1752
1753 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
1754
1755 return type_object_addr;
1756}
1757
1758void GBCExpanderPass::Expand_MonitorEnter(llvm::CallInst& call_inst) {
1759 ScopedExpandToBasicBlock eb(irb_, &call_inst);
1760
1761 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1762 llvm::Value* object_addr = call_inst.getArgOperand(1);
1763
1764 // TODO: opt_flags
1765 EmitGuard_NullPointerException(dex_pc, object_addr);
1766
1767 irb_.Runtime().EmitLockObject(object_addr);
1768
1769 return;
1770}
1771
1772void GBCExpanderPass::Expand_MonitorExit(llvm::CallInst& call_inst) {
1773 ScopedExpandToBasicBlock eb(irb_, &call_inst);
1774
1775 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1776 llvm::Value* object_addr = call_inst.getArgOperand(1);
1777
1778 // TODO: opt_flags
1779 EmitGuard_NullPointerException(dex_pc, object_addr);
1780
1781 EmitUpdateDexPC(dex_pc);
1782
1783 irb_.Runtime().EmitUnlockObject(object_addr);
1784
1785 EmitGuard_ExceptionLandingPad(dex_pc);
1786
1787 return;
1788}
1789
1790void GBCExpanderPass::Expand_HLCheckCast(llvm::CallInst& call_inst) {
1791 ScopedExpandToBasicBlock eb(irb_, &call_inst);
1792
1793 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1794 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1795 llvm::Value* object_addr = call_inst.getArgOperand(1);
1796
1797 llvm::BasicBlock* block_test_class =
1798 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1799
1800 llvm::BasicBlock* block_test_sub_class =
1801 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1802
1803 llvm::BasicBlock* block_cont =
1804 CreateBasicBlockWithDexPC(dex_pc, "checkcast_cont");
1805
1806 // Test: Is the reference equal to null? Act as no-op when it is null.
1807 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1808
1809 irb_.CreateCondBr(equal_null,
1810 block_cont,
1811 block_test_class);
1812
1813 // Test: Is the object instantiated from the given class?
1814 irb_.SetInsertPoint(block_test_class);
1815 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
1816 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1817
1818 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1819
1820 llvm::Value* object_type_field_addr =
1821 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1822
1823 llvm::Value* object_type_object_addr =
1824 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
1825
1826 llvm::Value* equal_class =
1827 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1828
1829 irb_.CreateCondBr(equal_class,
1830 block_cont,
1831 block_test_sub_class);
1832
1833 // Test: Is the object instantiated from the subclass of the given class?
1834 irb_.SetInsertPoint(block_test_sub_class);
1835
1836 EmitUpdateDexPC(dex_pc);
1837
1838 irb_.CreateCall2(irb_.GetRuntime(runtime_support::CheckCast),
1839 type_object_addr, object_type_object_addr);
1840
1841 EmitGuard_ExceptionLandingPad(dex_pc);
1842
1843 irb_.CreateBr(block_cont);
1844
1845 irb_.SetInsertPoint(block_cont);
1846
1847 return;
1848}
1849
1850llvm::Value* GBCExpanderPass::Expand_InstanceOf(llvm::CallInst& call_inst) {
1851 ScopedExpandToBasicBlock eb(irb_, &call_inst);
1852
1853 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1854 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1855 llvm::Value* object_addr = call_inst.getArgOperand(1);
1856
1857 llvm::BasicBlock* block_nullp =
1858 CreateBasicBlockWithDexPC(dex_pc, "nullp");
1859
1860 llvm::BasicBlock* block_test_class =
1861 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1862
1863 llvm::BasicBlock* block_class_equals =
1864 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1865
1866 llvm::BasicBlock* block_test_sub_class =
1867 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1868
1869 llvm::BasicBlock* block_cont =
1870 CreateBasicBlockWithDexPC(dex_pc, "instance_of_cont");
1871
1872 // Overview of the following code :
1873 // We check for null, if so, then false, otherwise check for class == . If so
1874 // then true, otherwise do callout slowpath.
1875 //
1876 // Test: Is the reference equal to null? Set 0 when it is null.
1877 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1878
1879 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1880
1881 irb_.SetInsertPoint(block_nullp);
1882 irb_.CreateBr(block_cont);
1883
1884 // Test: Is the object instantiated from the given class?
1885 irb_.SetInsertPoint(block_test_class);
1886 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
1887 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1888
1889 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1890
1891 llvm::Value* object_type_field_addr =
1892 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1893
1894 llvm::Value* object_type_object_addr =
1895 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
1896
1897 llvm::Value* equal_class =
1898 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1899
1900 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1901
1902 irb_.SetInsertPoint(block_class_equals);
1903 irb_.CreateBr(block_cont);
1904
1905 // Test: Is the object instantiated from the subclass of the given class?
1906 irb_.SetInsertPoint(block_test_sub_class);
1907 llvm::Value* result =
1908 irb_.CreateCall2(irb_.GetRuntime(runtime_support::IsAssignable),
1909 type_object_addr, object_type_object_addr);
1910 irb_.CreateBr(block_cont);
1911
1912 irb_.SetInsertPoint(block_cont);
1913
1914 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJIntTy(), 3);
1915
1916 phi->addIncoming(irb_.getJInt(0), block_nullp);
1917 phi->addIncoming(irb_.getJInt(1), block_class_equals);
1918 phi->addIncoming(result, block_test_sub_class);
1919
1920 return phi;
1921}
1922
1923llvm::Value* GBCExpanderPass::Expand_NewInstance(llvm::CallInst& call_inst) {
1924 ScopedExpandToBasicBlock eb(irb_, &call_inst);
1925
1926 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1927 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1928
1929 llvm::Function* runtime_func;
1930 if (compiler_->CanAccessInstantiableTypeWithoutChecks(
1931 method_idx_, dex_cache_, *dex_file_, type_idx)) {
1932 runtime_func = irb_.GetRuntime(runtime_support::AllocObject);
1933 } else {
1934 runtime_func = irb_.GetRuntime(runtime_support::AllocObjectWithAccessCheck);
1935 }
1936
1937 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1938
1939 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1940
1941 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1942
1943 EmitUpdateDexPC(dex_pc);
1944
1945 llvm::Value* object_addr =
1946 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
1947
1948 EmitGuard_ExceptionLandingPad(dex_pc);
1949
1950 return object_addr;
1951}
1952
1953llvm::Value* GBCExpanderPass::Expand_HLInvoke(llvm::CallInst& call_inst) {
1954 ScopedExpandToBasicBlock eb(irb_, &call_inst);
1955
1956 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1957 InvokeType invoke_type = static_cast<InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
1958 bool is_static = (invoke_type == kStatic);
1959 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
1960
1961 // Compute invoke related information for compiler decision
1962 int vtable_idx = -1;
1963 uintptr_t direct_code = 0;
1964 uintptr_t direct_method = 0;
1965 bool is_fast_path = compiler_->
1966 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
1967 invoke_type, vtable_idx, direct_code, direct_method);
1968
1969 // Load *this* actual parameter
1970 llvm::Value* this_addr = NULL;
1971
1972 if (!is_static) {
1973 // Test: Is *this* parameter equal to null?
1974 this_addr = call_inst.getArgOperand(3);
1975 }
1976
1977 // Load the method object
1978 llvm::Value* callee_method_object_addr = NULL;
1979
1980 if (!is_fast_path) {
1981 callee_method_object_addr =
1982 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
1983 this_addr, dex_pc, is_fast_path);
1984
1985 // TODO: opt_flags
1986 if (!is_static) {
1987 EmitGuard_NullPointerException(dex_pc, this_addr);
1988 }
1989 } else {
1990 // TODO: opt_flags
1991 if (!is_static) {
1992 EmitGuard_NullPointerException(dex_pc, this_addr);
1993 }
1994
1995 switch (invoke_type) {
1996 case kStatic:
1997 case kDirect:
1998 if (direct_method != 0u &&
1999 direct_method != static_cast<uintptr_t>(-1)) {
2000 callee_method_object_addr =
2001 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2002 irb_.getJObjectTy());
2003 } else {
2004 callee_method_object_addr =
2005 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2006 }
2007 break;
2008
2009 case kVirtual:
2010 DCHECK(vtable_idx != -1);
2011 callee_method_object_addr =
2012 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2013 break;
2014
2015 case kSuper:
2016 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2017 "the fast path.";
2018 break;
2019
2020 case kInterface:
2021 callee_method_object_addr =
2022 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2023 invoke_type, this_addr,
2024 dex_pc, is_fast_path);
2025 break;
2026 }
2027 }
2028
2029 // Load the actual parameter
2030 std::vector<llvm::Value*> args;
2031
2032 args.push_back(callee_method_object_addr); // method object for callee
2033
2034 for (uint32_t i = 3; i < call_inst.getNumArgOperands(); ++i) {
2035 args.push_back(call_inst.getArgOperand(i));
2036 }
2037
2038 llvm::Value* code_addr;
2039 if (direct_code != 0u &&
2040 direct_code != static_cast<uintptr_t>(-1)) {
2041 code_addr =
2042 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_code),
2043 GetFunctionType(callee_method_idx, is_static)->getPointerTo());
2044 } else {
2045 code_addr =
2046 irb_.LoadFromObjectOffset(callee_method_object_addr,
2047 Method::GetCodeOffset().Int32Value(),
2048 GetFunctionType(callee_method_idx, is_static)->getPointerTo(),
2049 kTBAAJRuntime);
2050 }
2051
2052 // Invoke callee
2053 EmitUpdateDexPC(dex_pc);
2054 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2055 EmitGuard_ExceptionLandingPad(dex_pc);
2056
2057 return retval;
2058}
2059
2060llvm::Value* GBCExpanderPass::Expand_OptArrayLength(llvm::CallInst& call_inst) {
2061 ScopedExpandToBasicBlock eb(irb_, &call_inst);
2062
2063 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2064 // Get the array object address
2065 llvm::Value* array_addr = call_inst.getArgOperand(1);
2066
2067 // TODO: opt_flags
2068 EmitGuard_NullPointerException(dex_pc, array_addr);
2069
2070 // Get the array length and store it to the register
2071 return EmitLoadArrayLength(array_addr);
2072}
2073
2074llvm::Value* GBCExpanderPass::Expand_NewArray(llvm::CallInst& call_inst) {
2075 ScopedExpandToBasicBlock eb(irb_, &call_inst);
2076
2077 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2078 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2079 llvm::Value* length = call_inst.getArgOperand(1);
2080
2081 return EmitAllocNewArray(dex_pc, length, type_idx, false);
2082}
2083
2084llvm::Value* GBCExpanderPass::Expand_HLFilledNewArray(llvm::CallInst& call_inst) {
2085 ScopedExpandToBasicBlock eb(irb_, &call_inst);
2086
2087 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2088 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(1));
2089 uint32_t length = call_inst.getNumArgOperands() - 3;
2090
2091 llvm::Value* object_addr =
2092 EmitAllocNewArray(dex_pc, irb_.getInt32(length), type_idx, true);
2093
2094 if (length > 0) {
2095 // Check for the element type
2096 uint32_t type_desc_len = 0;
2097 const char* type_desc =
2098 dex_file_->StringByTypeIdx(type_idx, &type_desc_len);
2099
2100 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
2101 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
2102 bool is_elem_int_ty = (type_desc[1] == 'I');
2103
2104 uint32_t alignment;
2105 llvm::Constant* elem_size;
2106 llvm::PointerType* field_type;
2107
2108 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
2109 // as the element, thus we are only checking 2 cases: primitive int and
2110 // non-primitive type.
2111 if (is_elem_int_ty) {
2112 alignment = sizeof(int32_t);
2113 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
2114 field_type = irb_.getJIntTy()->getPointerTo();
2115 } else {
2116 alignment = irb_.getSizeOfPtrEquivInt();
2117 elem_size = irb_.getSizeOfPtrEquivIntValue();
2118 field_type = irb_.getJObjectTy()->getPointerTo();
2119 }
2120
2121 llvm::Value* data_field_offset =
2122 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
2123
2124 llvm::Value* data_field_addr =
2125 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
2126
2127 // TODO: Tune this code. Currently we are generating one instruction for
2128 // one element which may be very space consuming. Maybe changing to use
2129 // memcpy may help; however, since we can't guarantee that the alloca of
2130 // dalvik register are continuous, we can't perform such optimization yet.
2131 for (uint32_t i = 0; i < length; ++i) {
2132 llvm::Value* reg_value = call_inst.getArgOperand(i+3);
2133
2134 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
2135
2136 data_field_addr =
2137 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
2138 }
2139 }
2140
2141 return object_addr;
2142}
2143
2144void GBCExpanderPass::Expand_HLFillArrayData(llvm::CallInst& call_inst) {
2145 ScopedExpandToBasicBlock eb(irb_, &call_inst);
2146
2147 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2148 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
2149 LV2SInt(call_inst.getArgOperand(0));
2150 llvm::Value* array_addr = call_inst.getArgOperand(1);
2151
2152 const Instruction::ArrayDataPayload* payload =
2153 reinterpret_cast<const Instruction::ArrayDataPayload*>(
2154 code_item_->insns_ + payload_offset);
2155
2156 if (payload->element_count == 0) {
2157 // When the number of the elements in the payload is zero, we don't have
2158 // to copy any numbers. However, we should check whether the array object
2159 // address is equal to null or not.
2160 EmitGuard_NullPointerException(dex_pc, array_addr);
2161 } else {
2162 // To save the code size, we are going to call the runtime function to
2163 // copy the content from DexFile.
2164
2165 // NOTE: We will check for the NullPointerException in the runtime.
2166
2167 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::FillArrayData);
2168
2169 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2170
2171 EmitUpdateDexPC(dex_pc);
2172
2173 irb_.CreateCall4(runtime_func,
2174 method_object_addr, irb_.getInt32(dex_pc),
2175 array_addr, irb_.getInt32(payload_offset));
2176
2177 EmitGuard_ExceptionLandingPad(dex_pc);
2178 }
2179
2180 return;
2181}
2182
2183llvm::Value* GBCExpanderPass::EmitAllocNewArray(uint32_t dex_pc,
2184 llvm::Value* array_length_value,
2185 uint32_t type_idx,
2186 bool is_filled_new_array) {
2187 llvm::Function* runtime_func;
2188
2189 bool skip_access_check =
2190 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
2191 *dex_file_, type_idx);
2192
2193
2194 if (is_filled_new_array) {
2195 runtime_func = skip_access_check ?
2196 irb_.GetRuntime(runtime_support::CheckAndAllocArray) :
2197 irb_.GetRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck);
2198 } else {
2199 runtime_func = skip_access_check ?
2200 irb_.GetRuntime(runtime_support::AllocArray) :
2201 irb_.GetRuntime(runtime_support::AllocArrayWithAccessCheck);
2202 }
2203
2204 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2205
2206 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2207
2208 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2209
2210 EmitUpdateDexPC(dex_pc);
2211
2212 llvm::Value* object_addr =
2213 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
2214 array_length_value, thread_object_addr);
2215
2216 EmitGuard_ExceptionLandingPad(dex_pc);
2217
2218 return object_addr;
2219}
2220
2221llvm::Value* GBCExpanderPass::
2222EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
2223 InvokeType invoke_type,
2224 llvm::Value* this_addr,
2225 uint32_t dex_pc,
2226 bool is_fast_path) {
2227
2228 llvm::Function* runtime_func = NULL;
2229
2230 switch (invoke_type) {
2231 case kStatic:
2232 runtime_func = irb_.GetRuntime(runtime_support::FindStaticMethodWithAccessCheck);
2233 break;
2234
2235 case kDirect:
2236 runtime_func = irb_.GetRuntime(runtime_support::FindDirectMethodWithAccessCheck);
2237 break;
2238
2239 case kVirtual:
2240 runtime_func = irb_.GetRuntime(runtime_support::FindVirtualMethodWithAccessCheck);
2241 break;
2242
2243 case kSuper:
2244 runtime_func = irb_.GetRuntime(runtime_support::FindSuperMethodWithAccessCheck);
2245 break;
2246
2247 case kInterface:
2248 if (is_fast_path) {
2249 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethod);
2250 } else {
2251 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethodWithAccessCheck);
2252 }
2253 break;
2254 }
2255
2256 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2257
2258 if (this_addr == NULL) {
2259 DCHECK_EQ(invoke_type, kStatic);
2260 this_addr = irb_.getJNull();
2261 }
2262
2263 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2264
2265 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2266
2267 EmitUpdateDexPC(dex_pc);
2268
2269 llvm::Value* callee_method_object_addr =
2270 irb_.CreateCall4(runtime_func,
2271 callee_method_idx_value,
2272 this_addr,
2273 caller_method_object_addr,
2274 thread_object_addr);
2275
2276 EmitGuard_ExceptionLandingPad(dex_pc);
2277
2278 return callee_method_object_addr;
2279}
2280
TDYa1275e869b62012-07-25 00:45:39 -07002281void GBCExpanderPass::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2282 // Using runtime support, let the target can override by InlineAssembly.
2283 irb_.Runtime().EmitMarkGCCard(value, target_addr);
2284}
2285
2286void GBCExpanderPass::EmitUpdateDexPC(uint32_t dex_pc) {
2287 irb_.StoreToObjectOffset(shadow_frame_,
2288 ShadowFrame::DexPCOffset(),
2289 irb_.getInt32(dex_pc),
2290 kTBAAShadowFrame);
2291}
2292
2293void GBCExpanderPass::EmitGuard_DivZeroException(uint32_t dex_pc,
2294 llvm::Value* denominator,
2295 JType op_jty) {
2296 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2297
2298 llvm::Constant* zero = irb_.getJZero(op_jty);
2299
2300 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2301
2302 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2303
2304 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2305
2306 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
2307
2308 irb_.SetInsertPoint(block_exception);
2309 EmitUpdateDexPC(dex_pc);
2310 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowDivZeroException));
2311 EmitBranchExceptionLandingPad(dex_pc);
2312
2313 irb_.SetInsertPoint(block_continue);
2314}
2315
2316void GBCExpanderPass::EmitGuard_NullPointerException(uint32_t dex_pc,
2317 llvm::Value* object) {
2318 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
2319
2320 llvm::BasicBlock* block_exception =
2321 CreateBasicBlockWithDexPC(dex_pc, "nullp");
2322
2323 llvm::BasicBlock* block_continue =
2324 CreateBasicBlockWithDexPC(dex_pc, "cont");
2325
2326 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
2327
2328 irb_.SetInsertPoint(block_exception);
2329 EmitUpdateDexPC(dex_pc);
2330 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowNullPointerException),
2331 irb_.getInt32(dex_pc));
2332 EmitBranchExceptionLandingPad(dex_pc);
2333
2334 irb_.SetInsertPoint(block_continue);
2335}
2336
2337void
2338GBCExpanderPass::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2339 llvm::Value* array,
2340 llvm::Value* index) {
2341 llvm::Value* array_len = EmitLoadArrayLength(array);
2342
2343 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2344
2345 llvm::BasicBlock* block_exception =
2346 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2347
2348 llvm::BasicBlock* block_continue =
2349 CreateBasicBlockWithDexPC(dex_pc, "cont");
2350
2351 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
2352
2353 irb_.SetInsertPoint(block_exception);
2354
2355 EmitUpdateDexPC(dex_pc);
2356 irb_.CreateCall2(irb_.GetRuntime(runtime_support::ThrowIndexOutOfBounds), index, array_len);
2357 EmitBranchExceptionLandingPad(dex_pc);
2358
2359 irb_.SetInsertPoint(block_continue);
2360}
2361
2362void GBCExpanderPass::EmitGuard_ArrayException(uint32_t dex_pc,
2363 llvm::Value* array,
2364 llvm::Value* index) {
2365 EmitGuard_NullPointerException(dex_pc, array);
2366 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2367}
2368
2369llvm::FunctionType* GBCExpanderPass::GetFunctionType(uint32_t method_idx,
2370 bool is_static) {
2371 // Get method signature
2372 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
2373
2374 uint32_t shorty_size;
2375 const char* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
2376 CHECK_GE(shorty_size, 1u);
2377
2378 // Get return type
2379 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
2380
2381 // Get argument type
2382 std::vector<llvm::Type*> args_type;
2383
2384 args_type.push_back(irb_.getJObjectTy()); // method object pointer
2385
2386 if (!is_static) {
2387 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
2388 }
2389
2390 for (uint32_t i = 1; i < shorty_size; ++i) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002391#if defined(ART_USE_QUICK_COMPILER)
2392 char shorty_type = shorty[i];
2393 switch(shorty_type) {
2394 case 'Z' : shorty_type = 'I'; break;
2395 case 'B' : shorty_type = 'I'; break;
2396 case 'S' : shorty_type = 'I'; break;
2397 case 'C' : shorty_type = 'I'; break;
2398 default: break;
2399 }
2400 args_type.push_back(irb_.getJType(shorty_type, kAccurate));
2401#else
TDYa1275e869b62012-07-25 00:45:39 -07002402 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
TDYa127f71bf5a2012-07-29 20:09:52 -07002403#endif
TDYa1275e869b62012-07-25 00:45:39 -07002404 }
2405
2406 return llvm::FunctionType::get(ret_type, args_type, false);
2407}
2408
2409
2410llvm::BasicBlock* GBCExpanderPass::
2411CreateBasicBlockWithDexPC(uint32_t dex_pc, const char* postfix) {
2412 std::string name;
2413
2414#if !defined(NDEBUG)
2415 StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
2416#endif
2417
2418 return llvm::BasicBlock::Create(context_, name, func_);
2419}
2420
2421llvm::BasicBlock* GBCExpanderPass::GetBasicBlock(uint32_t dex_pc) {
2422 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
2423
2424 return basic_blocks_[dex_pc];
2425}
2426
2427int32_t GBCExpanderPass::GetTryItemOffset(uint32_t dex_pc) {
2428 int32_t min = 0;
2429 int32_t max = code_item_->tries_size_ - 1;
2430
2431 while (min <= max) {
2432 int32_t mid = min + (max - min) / 2;
2433
2434 const DexFile::TryItem* ti = DexFile::GetTryItems(*code_item_, mid);
2435 uint32_t start = ti->start_addr_;
2436 uint32_t end = start + ti->insn_count_;
2437
2438 if (dex_pc < start) {
2439 max = mid - 1;
2440 } else if (dex_pc >= end) {
2441 min = mid + 1;
2442 } else {
2443 return mid; // found
2444 }
2445 }
2446
2447 return -1; // not found
2448}
2449
2450llvm::BasicBlock* GBCExpanderPass::GetLandingPadBasicBlock(uint32_t dex_pc) {
2451 // Find the try item for this address in this method
2452 int32_t ti_offset = GetTryItemOffset(dex_pc);
2453
2454 if (ti_offset == -1) {
2455 return NULL; // No landing pad is available for this address.
2456 }
2457
2458 // Check for the existing landing pad basic block
2459 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2460 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2461
2462 if (block_lpad) {
2463 // We have generated landing pad for this try item already. Return the
2464 // same basic block.
2465 return block_lpad;
2466 }
2467
2468 // Get try item from code item
2469 const DexFile::TryItem* ti = DexFile::GetTryItems(*code_item_, ti_offset);
2470
2471 std::string lpadname;
2472
2473#if !defined(NDEBUG)
2474 StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
2475#endif
2476
2477 // Create landing pad basic block
2478 block_lpad = llvm::BasicBlock::Create(context_, lpadname, func_);
2479
2480 // Change IRBuilder insert point
2481 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2482 irb_.SetInsertPoint(block_lpad);
2483
2484 // Find catch block with matching type
2485 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2486
2487 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
2488
2489 llvm::Value* catch_handler_index_value =
2490 irb_.CreateCall2(irb_.GetRuntime(runtime_support::FindCatchBlock),
2491 method_object_addr, ti_offset_value);
2492
2493 // Switch instruction (Go to unwind basic block by default)
2494 llvm::SwitchInst* sw =
2495 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2496
2497 // Cases with matched catch block
2498 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
2499
2500 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2501 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2502 }
2503
2504 // Restore the orignal insert point for IRBuilder
2505 irb_.restoreIP(irb_ip_original);
2506
2507 // Cache this landing pad
2508 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2509 basic_block_landing_pads_[ti_offset] = block_lpad;
2510
2511 return block_lpad;
2512}
2513
2514llvm::BasicBlock* GBCExpanderPass::GetUnwindBasicBlock() {
2515 // Check the existing unwinding baisc block block
2516 if (basic_block_unwind_ != NULL) {
2517 return basic_block_unwind_;
2518 }
2519
2520 // Create new basic block for unwinding
2521 basic_block_unwind_ =
2522 llvm::BasicBlock::Create(context_, "exception_unwind", func_);
2523
2524 // Change IRBuilder insert point
2525 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2526 irb_.SetInsertPoint(basic_block_unwind_);
2527
2528 // Pop the shadow frame
2529 Expand_PopShadowFrame();
2530
2531 // Emit the code to return default value (zero) for the given return type.
2532 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
2533 if (ret_shorty == 'V') {
2534 irb_.CreateRetVoid();
2535 } else {
2536 irb_.CreateRet(irb_.getJZero(ret_shorty));
2537 }
2538
2539 // Restore the orignal insert point for IRBuilder
2540 irb_.restoreIP(irb_ip_original);
2541
2542 return basic_block_unwind_;
2543}
2544
2545void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2546 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
2547 irb_.CreateBr(lpad);
2548 } else {
2549 irb_.CreateBr(GetUnwindBasicBlock());
2550 }
2551}
2552
2553void GBCExpanderPass::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
2554 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
2555
2556 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2557
2558 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
2559 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
2560 } else {
2561 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
2562 }
2563
2564 irb_.SetInsertPoint(block_cont);
2565}
2566
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002567llvm::Value*
2568GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
2569 llvm::CallInst& call_inst) {
2570 switch (intr_id) {
2571 //==- Thread -----------------------------------------------------------==//
2572 case IntrinsicHelper::GetCurrentThread: {
TDYa127b672d1e2012-06-28 21:21:45 -07002573 return irb_.Runtime().EmitGetCurrentThread();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002574 }
Logan Chiend54a23d2012-07-24 11:19:23 -07002575 case IntrinsicHelper::TestSuspend:
Logan Chien75e4b602012-07-23 14:24:12 -07002576 case IntrinsicHelper::CheckSuspend: {
Logan Chiend54a23d2012-07-24 11:19:23 -07002577 Expand_TestSuspend(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002578 return NULL;
2579 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002580 case IntrinsicHelper::MarkGCCard: {
TDYa1279a129452012-07-19 03:10:08 -07002581 Expand_MarkGCCard(call_inst);
2582 return NULL;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002583 }
Logan Chien75e4b602012-07-23 14:24:12 -07002584
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002585 //==- Exception --------------------------------------------------------==//
2586 case IntrinsicHelper::ThrowException: {
2587 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
2588 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002589 case IntrinsicHelper::HLThrowException: {
2590 ScopedExpandToBasicBlock eb(irb_, &call_inst);
2591
2592 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2593
2594 EmitUpdateDexPC(dex_pc);
2595
2596 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowException),
2597 call_inst.getArgOperand(0));
2598
2599 EmitGuard_ExceptionLandingPad(dex_pc);
2600 return NULL;
2601 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002602 case IntrinsicHelper::GetException: {
2603 return Expand_GetException();
2604 }
2605 case IntrinsicHelper::IsExceptionPending: {
2606 return irb_.Runtime().EmitIsExceptionPending();
2607 }
2608 case IntrinsicHelper::FindCatchBlock: {
2609 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
2610 }
2611 case IntrinsicHelper::ThrowDivZeroException: {
2612 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
2613 }
2614 case IntrinsicHelper::ThrowNullPointerException: {
2615 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
2616 }
2617 case IntrinsicHelper::ThrowIndexOutOfBounds: {
2618 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
2619 }
Logan Chien75e4b602012-07-23 14:24:12 -07002620
2621 //==- Const String -----------------------------------------------------==//
2622 case IntrinsicHelper::ConstString: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002623 return Expand_ConstString(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002624 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002625 case IntrinsicHelper::LoadStringFromDexCache: {
2626 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
2627 }
2628 case IntrinsicHelper::ResolveString: {
2629 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
2630 }
Logan Chien75e4b602012-07-23 14:24:12 -07002631
2632 //==- Const Class ------------------------------------------------------==//
2633 case IntrinsicHelper::ConstClass: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002634 return Expand_ConstClass(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002635 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002636 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
2637 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
2638 }
2639 case IntrinsicHelper::LoadTypeFromDexCache: {
2640 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
2641 }
2642 case IntrinsicHelper::InitializeType: {
2643 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
2644 }
Logan Chien75e4b602012-07-23 14:24:12 -07002645
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002646 //==- Lock -------------------------------------------------------------==//
2647 case IntrinsicHelper::LockObject: {
2648 Expand_LockObject(call_inst.getArgOperand(0));
2649 return NULL;
2650 }
2651 case IntrinsicHelper::UnlockObject: {
2652 Expand_UnlockObject(call_inst.getArgOperand(0));
2653 return NULL;
2654 }
Logan Chien75e4b602012-07-23 14:24:12 -07002655
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002656 //==- Cast -------------------------------------------------------------==//
2657 case IntrinsicHelper::CheckCast: {
2658 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
2659 }
Logan Chien75e4b602012-07-23 14:24:12 -07002660 case IntrinsicHelper::HLCheckCast: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002661 Expand_HLCheckCast(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002662 return NULL;
2663 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002664 case IntrinsicHelper::IsAssignable: {
2665 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
2666 }
Logan Chien75e4b602012-07-23 14:24:12 -07002667
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002668 //==- Alloc ------------------------------------------------------------==//
2669 case IntrinsicHelper::AllocObject: {
2670 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
2671 }
2672 case IntrinsicHelper::AllocObjectWithAccessCheck: {
2673 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
2674 }
Logan Chien75e4b602012-07-23 14:24:12 -07002675
2676 //==- Instance ---------------------------------------------------------==//
2677 case IntrinsicHelper::NewInstance: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002678 return Expand_NewInstance(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002679 }
2680 case IntrinsicHelper::InstanceOf: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002681 return Expand_InstanceOf(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002682 }
2683
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002684 //==- Array ------------------------------------------------------------==//
Logan Chien75e4b602012-07-23 14:24:12 -07002685 case IntrinsicHelper::NewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002686 return Expand_NewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002687 }
2688 case IntrinsicHelper::OptArrayLength: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002689 return Expand_OptArrayLength(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002690 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002691 case IntrinsicHelper::ArrayLength: {
2692 return EmitLoadArrayLength(call_inst.getArgOperand(0));
2693 }
2694 case IntrinsicHelper::AllocArray: {
2695 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
2696 }
2697 case IntrinsicHelper::AllocArrayWithAccessCheck: {
2698 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
2699 call_inst);
2700 }
2701 case IntrinsicHelper::CheckAndAllocArray: {
2702 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
2703 }
2704 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
2705 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
2706 call_inst);
2707 }
2708 case IntrinsicHelper::ArrayGet: {
2709 return Expand_ArrayGet(call_inst.getArgOperand(0),
2710 call_inst.getArgOperand(1),
2711 kInt);
2712 }
2713 case IntrinsicHelper::ArrayGetWide: {
2714 return Expand_ArrayGet(call_inst.getArgOperand(0),
2715 call_inst.getArgOperand(1),
2716 kLong);
2717 }
2718 case IntrinsicHelper::ArrayGetObject: {
2719 return Expand_ArrayGet(call_inst.getArgOperand(0),
2720 call_inst.getArgOperand(1),
2721 kObject);
2722 }
2723 case IntrinsicHelper::ArrayGetBoolean: {
2724 return Expand_ArrayGet(call_inst.getArgOperand(0),
2725 call_inst.getArgOperand(1),
2726 kBoolean);
2727 }
2728 case IntrinsicHelper::ArrayGetByte: {
2729 return Expand_ArrayGet(call_inst.getArgOperand(0),
2730 call_inst.getArgOperand(1),
2731 kByte);
2732 }
2733 case IntrinsicHelper::ArrayGetChar: {
2734 return Expand_ArrayGet(call_inst.getArgOperand(0),
2735 call_inst.getArgOperand(1),
2736 kChar);
2737 }
2738 case IntrinsicHelper::ArrayGetShort: {
2739 return Expand_ArrayGet(call_inst.getArgOperand(0),
2740 call_inst.getArgOperand(1),
2741 kShort);
2742 }
2743 case IntrinsicHelper::ArrayPut: {
2744 Expand_ArrayPut(call_inst.getArgOperand(0),
2745 call_inst.getArgOperand(1),
2746 call_inst.getArgOperand(2),
2747 kInt);
2748 return NULL;
2749 }
2750 case IntrinsicHelper::ArrayPutWide: {
2751 Expand_ArrayPut(call_inst.getArgOperand(0),
2752 call_inst.getArgOperand(1),
2753 call_inst.getArgOperand(2),
2754 kLong);
2755 return NULL;
2756 }
2757 case IntrinsicHelper::ArrayPutObject: {
2758 Expand_ArrayPut(call_inst.getArgOperand(0),
2759 call_inst.getArgOperand(1),
2760 call_inst.getArgOperand(2),
2761 kObject);
2762 return NULL;
2763 }
2764 case IntrinsicHelper::ArrayPutBoolean: {
2765 Expand_ArrayPut(call_inst.getArgOperand(0),
2766 call_inst.getArgOperand(1),
2767 call_inst.getArgOperand(2),
2768 kBoolean);
2769 return NULL;
2770 }
2771 case IntrinsicHelper::ArrayPutByte: {
2772 Expand_ArrayPut(call_inst.getArgOperand(0),
2773 call_inst.getArgOperand(1),
2774 call_inst.getArgOperand(2),
2775 kByte);
2776 return NULL;
2777 }
2778 case IntrinsicHelper::ArrayPutChar: {
2779 Expand_ArrayPut(call_inst.getArgOperand(0),
2780 call_inst.getArgOperand(1),
2781 call_inst.getArgOperand(2),
2782 kChar);
2783 return NULL;
2784 }
2785 case IntrinsicHelper::ArrayPutShort: {
2786 Expand_ArrayPut(call_inst.getArgOperand(0),
2787 call_inst.getArgOperand(1),
2788 call_inst.getArgOperand(2),
2789 kShort);
2790 return NULL;
2791 }
2792 case IntrinsicHelper::CheckPutArrayElement: {
2793 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
2794 }
2795 case IntrinsicHelper::FilledNewArray: {
2796 Expand_FilledNewArray(call_inst);
2797 return NULL;
2798 }
2799 case IntrinsicHelper::FillArrayData: {
2800 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
2801 }
Logan Chien75e4b602012-07-23 14:24:12 -07002802 case IntrinsicHelper::HLFillArrayData: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002803 Expand_HLFillArrayData(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002804 return NULL;
2805 }
2806 case IntrinsicHelper::HLFilledNewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002807 return Expand_HLFilledNewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002808 }
2809
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002810 //==- Instance Field ---------------------------------------------------==//
2811 case IntrinsicHelper::InstanceFieldGet:
2812 case IntrinsicHelper::InstanceFieldGetBoolean:
2813 case IntrinsicHelper::InstanceFieldGetByte:
2814 case IntrinsicHelper::InstanceFieldGetChar:
2815 case IntrinsicHelper::InstanceFieldGetShort: {
2816 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
2817 }
2818 case IntrinsicHelper::InstanceFieldGetWide: {
2819 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
2820 }
2821 case IntrinsicHelper::InstanceFieldGetObject: {
2822 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
2823 }
2824 case IntrinsicHelper::InstanceFieldGetFast: {
2825 return Expand_IGetFast(call_inst.getArgOperand(0),
2826 call_inst.getArgOperand(1),
2827 call_inst.getArgOperand(2),
2828 kInt);
2829 }
2830 case IntrinsicHelper::InstanceFieldGetWideFast: {
2831 return Expand_IGetFast(call_inst.getArgOperand(0),
2832 call_inst.getArgOperand(1),
2833 call_inst.getArgOperand(2),
2834 kLong);
2835 }
2836 case IntrinsicHelper::InstanceFieldGetObjectFast: {
2837 return Expand_IGetFast(call_inst.getArgOperand(0),
2838 call_inst.getArgOperand(1),
2839 call_inst.getArgOperand(2),
2840 kObject);
2841 }
2842 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
2843 return Expand_IGetFast(call_inst.getArgOperand(0),
2844 call_inst.getArgOperand(1),
2845 call_inst.getArgOperand(2),
2846 kBoolean);
2847 }
2848 case IntrinsicHelper::InstanceFieldGetByteFast: {
2849 return Expand_IGetFast(call_inst.getArgOperand(0),
2850 call_inst.getArgOperand(1),
2851 call_inst.getArgOperand(2),
2852 kByte);
2853 }
2854 case IntrinsicHelper::InstanceFieldGetCharFast: {
2855 return Expand_IGetFast(call_inst.getArgOperand(0),
2856 call_inst.getArgOperand(1),
2857 call_inst.getArgOperand(2),
2858 kChar);
2859 }
2860 case IntrinsicHelper::InstanceFieldGetShortFast: {
2861 return Expand_IGetFast(call_inst.getArgOperand(0),
2862 call_inst.getArgOperand(1),
2863 call_inst.getArgOperand(2),
2864 kShort);
2865 }
2866 case IntrinsicHelper::InstanceFieldPut:
2867 case IntrinsicHelper::InstanceFieldPutBoolean:
2868 case IntrinsicHelper::InstanceFieldPutByte:
2869 case IntrinsicHelper::InstanceFieldPutChar:
2870 case IntrinsicHelper::InstanceFieldPutShort: {
2871 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
2872 }
2873 case IntrinsicHelper::InstanceFieldPutWide: {
2874 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
2875 }
2876 case IntrinsicHelper::InstanceFieldPutObject: {
2877 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
2878 }
2879 case IntrinsicHelper::InstanceFieldPutFast: {
2880 Expand_IPutFast(call_inst.getArgOperand(0),
2881 call_inst.getArgOperand(1),
2882 call_inst.getArgOperand(2),
2883 call_inst.getArgOperand(3),
2884 kInt);
2885 return NULL;
2886 }
2887 case IntrinsicHelper::InstanceFieldPutWideFast: {
2888 Expand_IPutFast(call_inst.getArgOperand(0),
2889 call_inst.getArgOperand(1),
2890 call_inst.getArgOperand(2),
2891 call_inst.getArgOperand(3),
2892 kLong);
2893 return NULL;
2894 }
2895 case IntrinsicHelper::InstanceFieldPutObjectFast: {
2896 Expand_IPutFast(call_inst.getArgOperand(0),
2897 call_inst.getArgOperand(1),
2898 call_inst.getArgOperand(2),
2899 call_inst.getArgOperand(3),
2900 kObject);
2901 return NULL;
2902 }
2903 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
2904 Expand_IPutFast(call_inst.getArgOperand(0),
2905 call_inst.getArgOperand(1),
2906 call_inst.getArgOperand(2),
2907 call_inst.getArgOperand(3),
2908 kBoolean);
2909 return NULL;
2910 }
2911 case IntrinsicHelper::InstanceFieldPutByteFast: {
2912 Expand_IPutFast(call_inst.getArgOperand(0),
2913 call_inst.getArgOperand(1),
2914 call_inst.getArgOperand(2),
2915 call_inst.getArgOperand(3),
2916 kByte);
2917 return NULL;
2918 }
2919 case IntrinsicHelper::InstanceFieldPutCharFast: {
2920 Expand_IPutFast(call_inst.getArgOperand(0),
2921 call_inst.getArgOperand(1),
2922 call_inst.getArgOperand(2),
2923 call_inst.getArgOperand(3),
2924 kChar);
2925 return NULL;
2926 }
2927 case IntrinsicHelper::InstanceFieldPutShortFast: {
2928 Expand_IPutFast(call_inst.getArgOperand(0),
2929 call_inst.getArgOperand(1),
2930 call_inst.getArgOperand(2),
2931 call_inst.getArgOperand(3),
2932 kShort);
2933 return NULL;
2934 }
Logan Chien75e4b602012-07-23 14:24:12 -07002935
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002936 //==- Static Field -----------------------------------------------------==//
2937 case IntrinsicHelper::StaticFieldGet:
2938 case IntrinsicHelper::StaticFieldGetBoolean:
2939 case IntrinsicHelper::StaticFieldGetByte:
2940 case IntrinsicHelper::StaticFieldGetChar:
2941 case IntrinsicHelper::StaticFieldGetShort: {
2942 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
2943 }
2944 case IntrinsicHelper::StaticFieldGetWide: {
2945 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
2946 }
2947 case IntrinsicHelper::StaticFieldGetObject: {
2948 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
2949 }
2950 case IntrinsicHelper::StaticFieldGetFast: {
2951 return Expand_SGetFast(call_inst.getArgOperand(0),
2952 call_inst.getArgOperand(1),
2953 call_inst.getArgOperand(2),
2954 kInt);
2955 }
2956 case IntrinsicHelper::StaticFieldGetWideFast: {
2957 return Expand_SGetFast(call_inst.getArgOperand(0),
2958 call_inst.getArgOperand(1),
2959 call_inst.getArgOperand(2),
2960 kLong);
2961 }
2962 case IntrinsicHelper::StaticFieldGetObjectFast: {
2963 return Expand_SGetFast(call_inst.getArgOperand(0),
2964 call_inst.getArgOperand(1),
2965 call_inst.getArgOperand(2),
2966 kObject);
2967 }
2968 case IntrinsicHelper::StaticFieldGetBooleanFast: {
2969 return Expand_SGetFast(call_inst.getArgOperand(0),
2970 call_inst.getArgOperand(1),
2971 call_inst.getArgOperand(2),
2972 kBoolean);
2973 }
2974 case IntrinsicHelper::StaticFieldGetByteFast: {
2975 return Expand_SGetFast(call_inst.getArgOperand(0),
2976 call_inst.getArgOperand(1),
2977 call_inst.getArgOperand(2),
2978 kByte);
2979 }
2980 case IntrinsicHelper::StaticFieldGetCharFast: {
2981 return Expand_SGetFast(call_inst.getArgOperand(0),
2982 call_inst.getArgOperand(1),
2983 call_inst.getArgOperand(2),
2984 kChar);
2985 }
2986 case IntrinsicHelper::StaticFieldGetShortFast: {
2987 return Expand_SGetFast(call_inst.getArgOperand(0),
2988 call_inst.getArgOperand(1),
2989 call_inst.getArgOperand(2),
2990 kShort);
2991 }
2992 case IntrinsicHelper::StaticFieldPut:
2993 case IntrinsicHelper::StaticFieldPutBoolean:
2994 case IntrinsicHelper::StaticFieldPutByte:
2995 case IntrinsicHelper::StaticFieldPutChar:
2996 case IntrinsicHelper::StaticFieldPutShort: {
2997 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
2998 }
2999 case IntrinsicHelper::StaticFieldPutWide: {
3000 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
3001 }
3002 case IntrinsicHelper::StaticFieldPutObject: {
3003 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
3004 }
3005 case IntrinsicHelper::StaticFieldPutFast: {
3006 Expand_SPutFast(call_inst.getArgOperand(0),
3007 call_inst.getArgOperand(1),
3008 call_inst.getArgOperand(2),
3009 call_inst.getArgOperand(3),
3010 kInt);
3011 return NULL;
3012 }
3013 case IntrinsicHelper::StaticFieldPutWideFast: {
3014 Expand_SPutFast(call_inst.getArgOperand(0),
3015 call_inst.getArgOperand(1),
3016 call_inst.getArgOperand(2),
3017 call_inst.getArgOperand(3),
3018 kLong);
3019 return NULL;
3020 }
3021 case IntrinsicHelper::StaticFieldPutObjectFast: {
3022 Expand_SPutFast(call_inst.getArgOperand(0),
3023 call_inst.getArgOperand(1),
3024 call_inst.getArgOperand(2),
3025 call_inst.getArgOperand(3),
3026 kObject);
3027 return NULL;
3028 }
3029 case IntrinsicHelper::StaticFieldPutBooleanFast: {
3030 Expand_SPutFast(call_inst.getArgOperand(0),
3031 call_inst.getArgOperand(1),
3032 call_inst.getArgOperand(2),
3033 call_inst.getArgOperand(3),
3034 kBoolean);
3035 return NULL;
3036 }
3037 case IntrinsicHelper::StaticFieldPutByteFast: {
3038 Expand_SPutFast(call_inst.getArgOperand(0),
3039 call_inst.getArgOperand(1),
3040 call_inst.getArgOperand(2),
3041 call_inst.getArgOperand(3),
3042 kByte);
3043 return NULL;
3044 }
3045 case IntrinsicHelper::StaticFieldPutCharFast: {
3046 Expand_SPutFast(call_inst.getArgOperand(0),
3047 call_inst.getArgOperand(1),
3048 call_inst.getArgOperand(2),
3049 call_inst.getArgOperand(3),
3050 kChar);
3051 return NULL;
3052 }
3053 case IntrinsicHelper::StaticFieldPutShortFast: {
3054 Expand_SPutFast(call_inst.getArgOperand(0),
3055 call_inst.getArgOperand(1),
3056 call_inst.getArgOperand(2),
3057 call_inst.getArgOperand(3),
3058 kShort);
3059 return NULL;
3060 }
3061 case IntrinsicHelper::LoadDeclaringClassSSB: {
3062 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
3063 }
3064 case IntrinsicHelper::LoadClassSSBFromDexCache: {
3065 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
3066 }
3067 case IntrinsicHelper::InitializeAndLoadClassSSB: {
3068 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
3069 }
Logan Chien75e4b602012-07-23 14:24:12 -07003070
3071 //==- High-level Array -------------------------------------------------==//
3072 case IntrinsicHelper::HLArrayGet: {
TDYa1275a26d442012-07-26 18:58:38 -07003073 return Expand_HLArrayGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003074 }
3075 case IntrinsicHelper::HLArrayGetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003076 return Expand_HLArrayGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003077 }
3078 case IntrinsicHelper::HLArrayGetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003079 return Expand_HLArrayGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003080 }
3081 case IntrinsicHelper::HLArrayGetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003082 return Expand_HLArrayGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003083 }
3084 case IntrinsicHelper::HLArrayGetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003085 return Expand_HLArrayGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003086 }
3087 case IntrinsicHelper::HLArrayGetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003088 return Expand_HLArrayGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003089 }
3090 case IntrinsicHelper::HLArrayGetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003091 return Expand_HLArrayGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003092 }
3093 case IntrinsicHelper::HLArrayGetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003094 return Expand_HLArrayGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003095 }
3096 case IntrinsicHelper::HLArrayGetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003097 return Expand_HLArrayGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003098 }
3099 case IntrinsicHelper::HLArrayPut: {
TDYa1275a26d442012-07-26 18:58:38 -07003100 Expand_HLArrayPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003101 return NULL;
3102 }
3103 case IntrinsicHelper::HLArrayPutBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003104 Expand_HLArrayPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003105 return NULL;
3106 }
3107 case IntrinsicHelper::HLArrayPutByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003108 Expand_HLArrayPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003109 return NULL;
3110 }
3111 case IntrinsicHelper::HLArrayPutChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003112 Expand_HLArrayPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003113 return NULL;
3114 }
3115 case IntrinsicHelper::HLArrayPutShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003116 Expand_HLArrayPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003117 return NULL;
3118 }
3119 case IntrinsicHelper::HLArrayPutFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003120 Expand_HLArrayPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003121 return NULL;
3122 }
3123 case IntrinsicHelper::HLArrayPutWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003124 Expand_HLArrayPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003125 return NULL;
3126 }
3127 case IntrinsicHelper::HLArrayPutDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003128 Expand_HLArrayPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003129 return NULL;
3130 }
3131 case IntrinsicHelper::HLArrayPutObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003132 Expand_HLArrayPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003133 return NULL;
3134 }
3135
3136 //==- High-level Instance ----------------------------------------------==//
3137 case IntrinsicHelper::HLIGet: {
TDYa1275e869b62012-07-25 00:45:39 -07003138 return Expand_HLIGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003139 }
3140 case IntrinsicHelper::HLIGetBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003141 return Expand_HLIGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003142 }
3143 case IntrinsicHelper::HLIGetByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003144 return Expand_HLIGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003145 }
3146 case IntrinsicHelper::HLIGetChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003147 return Expand_HLIGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003148 }
3149 case IntrinsicHelper::HLIGetShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003150 return Expand_HLIGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003151 }
3152 case IntrinsicHelper::HLIGetFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003153 return Expand_HLIGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003154 }
3155 case IntrinsicHelper::HLIGetWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003156 return Expand_HLIGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003157 }
3158 case IntrinsicHelper::HLIGetDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003159 return Expand_HLIGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003160 }
3161 case IntrinsicHelper::HLIGetObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003162 return Expand_HLIGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003163 }
3164 case IntrinsicHelper::HLIPut: {
TDYa1275e869b62012-07-25 00:45:39 -07003165 Expand_HLIPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003166 return NULL;
3167 }
3168 case IntrinsicHelper::HLIPutBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003169 Expand_HLIPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003170 return NULL;
3171 }
3172 case IntrinsicHelper::HLIPutByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003173 Expand_HLIPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003174 return NULL;
3175 }
3176 case IntrinsicHelper::HLIPutChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003177 Expand_HLIPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003178 return NULL;
3179 }
3180 case IntrinsicHelper::HLIPutShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003181 Expand_HLIPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003182 return NULL;
3183 }
3184 case IntrinsicHelper::HLIPutFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003185 Expand_HLIPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003186 return NULL;
3187 }
3188 case IntrinsicHelper::HLIPutWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003189 Expand_HLIPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003190 return NULL;
3191 }
3192 case IntrinsicHelper::HLIPutDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003193 Expand_HLIPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003194 return NULL;
3195 }
3196 case IntrinsicHelper::HLIPutObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003197 Expand_HLIPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003198 return NULL;
3199 }
3200
3201 //==- High-level Invoke ------------------------------------------------==//
TDYa127f71bf5a2012-07-29 20:09:52 -07003202 case IntrinsicHelper::HLInvokeVoid:
3203 case IntrinsicHelper::HLInvokeObj:
3204 case IntrinsicHelper::HLInvokeInt:
3205 case IntrinsicHelper::HLInvokeFloat:
3206 case IntrinsicHelper::HLInvokeLong:
Logan Chien75e4b602012-07-23 14:24:12 -07003207 case IntrinsicHelper::HLInvokeDouble: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003208 return Expand_HLInvoke(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003209 }
3210
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003211 //==- Invoke -----------------------------------------------------------==//
3212 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
3213 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
3214 }
3215 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
3216 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
3217 }
3218 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
3219 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
3220 }
3221 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
3222 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
3223 }
3224 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
3225 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
3226 }
3227 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
3228 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
3229 }
3230 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
3231 return Expand_GetVirtualCalleeMethodObjAddrFast(
3232 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
3233 }
3234 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
3235 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
3236 }
3237 case IntrinsicHelper::InvokeRetVoid:
3238 case IntrinsicHelper::InvokeRetBoolean:
3239 case IntrinsicHelper::InvokeRetByte:
3240 case IntrinsicHelper::InvokeRetChar:
3241 case IntrinsicHelper::InvokeRetShort:
3242 case IntrinsicHelper::InvokeRetInt:
3243 case IntrinsicHelper::InvokeRetLong:
3244 case IntrinsicHelper::InvokeRetFloat:
3245 case IntrinsicHelper::InvokeRetDouble:
3246 case IntrinsicHelper::InvokeRetObject: {
3247 return Expand_Invoke(call_inst);
3248 }
Logan Chien75e4b602012-07-23 14:24:12 -07003249
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003250 //==- Math -------------------------------------------------------------==//
3251 case IntrinsicHelper::DivInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003252 return Expand_DivRem(call_inst, /* is_div */true, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003253 }
3254 case IntrinsicHelper::RemInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003255 return Expand_DivRem(call_inst, /* is_div */false, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003256 }
3257 case IntrinsicHelper::DivLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003258 return Expand_DivRem(call_inst, /* is_div */true, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003259 }
3260 case IntrinsicHelper::RemLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003261 return Expand_DivRem(call_inst, /* is_div */false, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003262 }
3263 case IntrinsicHelper::D2L: {
3264 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
3265 }
3266 case IntrinsicHelper::D2I: {
3267 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
3268 }
3269 case IntrinsicHelper::F2L: {
3270 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
3271 }
3272 case IntrinsicHelper::F2I: {
3273 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
3274 }
Logan Chien75e4b602012-07-23 14:24:12 -07003275
3276 //==- High-level Static ------------------------------------------------==//
3277 case IntrinsicHelper::HLSget: {
TDYa1275a26d442012-07-26 18:58:38 -07003278 return Expand_HLSget(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003279 }
3280 case IntrinsicHelper::HLSgetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003281 return Expand_HLSget(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003282 }
3283 case IntrinsicHelper::HLSgetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003284 return Expand_HLSget(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003285 }
3286 case IntrinsicHelper::HLSgetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003287 return Expand_HLSget(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003288 }
3289 case IntrinsicHelper::HLSgetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003290 return Expand_HLSget(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003291 }
3292 case IntrinsicHelper::HLSgetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003293 return Expand_HLSget(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003294 }
3295 case IntrinsicHelper::HLSgetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003296 return Expand_HLSget(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003297 }
3298 case IntrinsicHelper::HLSgetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003299 return Expand_HLSget(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003300 }
3301 case IntrinsicHelper::HLSgetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003302 return Expand_HLSget(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003303 }
3304 case IntrinsicHelper::HLSput: {
TDYa1275a26d442012-07-26 18:58:38 -07003305 Expand_HLSput(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003306 return NULL;
3307 }
3308 case IntrinsicHelper::HLSputBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003309 Expand_HLSput(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003310 return NULL;
3311 }
3312 case IntrinsicHelper::HLSputByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003313 Expand_HLSput(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003314 return NULL;
3315 }
3316 case IntrinsicHelper::HLSputChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003317 Expand_HLSput(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003318 return NULL;
3319 }
3320 case IntrinsicHelper::HLSputShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003321 Expand_HLSput(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003322 return NULL;
3323 }
3324 case IntrinsicHelper::HLSputFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003325 Expand_HLSput(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003326 return NULL;
3327 }
3328 case IntrinsicHelper::HLSputWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003329 Expand_HLSput(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003330 return NULL;
3331 }
3332 case IntrinsicHelper::HLSputDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003333 Expand_HLSput(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003334 return NULL;
3335 }
3336 case IntrinsicHelper::HLSputObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003337 Expand_HLSput(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003338 return NULL;
3339 }
3340
3341 //==- High-level Monitor -----------------------------------------------==//
3342 case IntrinsicHelper::MonitorEnter: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003343 Expand_MonitorEnter(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003344 return NULL;
3345 }
3346 case IntrinsicHelper::MonitorExit: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003347 Expand_MonitorExit(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003348 return NULL;
3349 }
3350
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003351 //==- Shadow Frame -----------------------------------------------------==//
3352 case IntrinsicHelper::AllocaShadowFrame: {
3353 Expand_AllocaShadowFrame(call_inst.getArgOperand(0));
3354 return NULL;
3355 }
3356 case IntrinsicHelper::SetShadowFrameEntry: {
3357 Expand_SetShadowFrameEntry(call_inst.getArgOperand(0),
3358 call_inst.getArgOperand(1));
3359 return NULL;
3360 }
3361 case IntrinsicHelper::PopShadowFrame: {
3362 Expand_PopShadowFrame();
3363 return NULL;
3364 }
3365 case IntrinsicHelper::UpdateDexPC: {
3366 Expand_UpdateDexPC(call_inst.getArgOperand(0));
3367 return NULL;
3368 }
TDYa127a1b21852012-07-23 03:20:39 -07003369
Logan Chien75e4b602012-07-23 14:24:12 -07003370 //==- Comparison -------------------------------------------------------==//
3371 case IntrinsicHelper::CmplFloat:
3372 case IntrinsicHelper::CmplDouble: {
3373 return Expand_FPCompare(call_inst.getArgOperand(0),
3374 call_inst.getArgOperand(1),
3375 false);
3376 }
3377 case IntrinsicHelper::CmpgFloat:
3378 case IntrinsicHelper::CmpgDouble: {
3379 return Expand_FPCompare(call_inst.getArgOperand(0),
3380 call_inst.getArgOperand(1),
3381 true);
3382 }
3383 case IntrinsicHelper::CmpLong: {
3384 return Expand_LongCompare(call_inst.getArgOperand(0),
3385 call_inst.getArgOperand(1));
3386 }
TDYa127a1b21852012-07-23 03:20:39 -07003387
Logan Chien75e4b602012-07-23 14:24:12 -07003388 //==- Switch -----------------------------------------------------------==//
3389 case greenland::IntrinsicHelper::SparseSwitch: {
TDYa1275e869b62012-07-25 00:45:39 -07003390 // Nothing to be done.
Logan Chien75e4b602012-07-23 14:24:12 -07003391 return NULL;
3392 }
3393 case greenland::IntrinsicHelper::PackedSwitch: {
TDYa1275e869b62012-07-25 00:45:39 -07003394 // Nothing to be done.
Logan Chien75e4b602012-07-23 14:24:12 -07003395 return NULL;
3396 }
3397
3398 //==- Const ------------------------------------------------------------==//
Logan Chiend54a23d2012-07-24 11:19:23 -07003399 case greenland::IntrinsicHelper::ConstInt:
Logan Chien75e4b602012-07-23 14:24:12 -07003400 case greenland::IntrinsicHelper::ConstLong: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003401 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003402 }
3403 case greenland::IntrinsicHelper::ConstFloat: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003404 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3405 irb_.getJFloatTy());
Logan Chien75e4b602012-07-23 14:24:12 -07003406 }
3407 case greenland::IntrinsicHelper::ConstDouble: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003408 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3409 irb_.getJDoubleTy());
3410 }
3411 case greenland::IntrinsicHelper::ConstObj: {
3412 LOG(FATAL) << "ConstObj should not occur at all";
Logan Chien75e4b602012-07-23 14:24:12 -07003413 return NULL;
3414 }
3415
3416 //==- Method Info ------------------------------------------------------==//
3417 case greenland::IntrinsicHelper::MethodInfo: {
TDYa1275e869b62012-07-25 00:45:39 -07003418 // Nothing to be done.
Logan Chien75e4b602012-07-23 14:24:12 -07003419 return NULL;
3420 }
3421
3422 //==- Copy -------------------------------------------------------------==//
Logan Chiend54a23d2012-07-24 11:19:23 -07003423 case greenland::IntrinsicHelper::CopyInt:
3424 case greenland::IntrinsicHelper::CopyFloat:
3425 case greenland::IntrinsicHelper::CopyLong:
TDYa1275e869b62012-07-25 00:45:39 -07003426 case greenland::IntrinsicHelper::CopyDouble:
Logan Chien75e4b602012-07-23 14:24:12 -07003427 case greenland::IntrinsicHelper::CopyObj: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003428 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003429 }
3430
3431 //==- Shift ------------------------------------------------------------==//
3432 case greenland::IntrinsicHelper::SHLLong: {
3433 return Expand_IntegerShift(call_inst.getArgOperand(0),
3434 call_inst.getArgOperand(1),
3435 kIntegerSHL, kLong);
3436 }
3437 case greenland::IntrinsicHelper::SHRLong: {
3438 return Expand_IntegerShift(call_inst.getArgOperand(0),
3439 call_inst.getArgOperand(1),
3440 kIntegerSHR, kLong);
3441 }
3442 case greenland::IntrinsicHelper::USHRLong: {
3443 return Expand_IntegerShift(call_inst.getArgOperand(0),
3444 call_inst.getArgOperand(1),
3445 kIntegerUSHR, kLong);
3446 }
3447 case greenland::IntrinsicHelper::SHLInt: {
3448 return Expand_IntegerShift(call_inst.getArgOperand(0),
3449 call_inst.getArgOperand(1),
3450 kIntegerSHL, kInt);
3451 }
3452 case greenland::IntrinsicHelper::SHRInt: {
3453 return Expand_IntegerShift(call_inst.getArgOperand(0),
3454 call_inst.getArgOperand(1),
3455 kIntegerSHR, kInt);
3456 }
3457 case greenland::IntrinsicHelper::USHRInt: {
3458 return Expand_IntegerShift(call_inst.getArgOperand(0),
3459 call_inst.getArgOperand(1),
3460 kIntegerUSHR, kInt);
3461 }
3462
3463 //==- Conversion -------------------------------------------------------==//
TDYa127a1b21852012-07-23 03:20:39 -07003464 case IntrinsicHelper::IntToChar: {
3465 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
3466 irb_.getJIntTy());
3467 }
3468 case IntrinsicHelper::IntToShort: {
3469 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
3470 irb_.getJIntTy());
3471 }
3472 case IntrinsicHelper::IntToByte: {
3473 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
3474 irb_.getJIntTy());
3475 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003476
Logan Chien75e4b602012-07-23 14:24:12 -07003477 //==- Unknown Cases ----------------------------------------------------==//
3478 case IntrinsicHelper::MaxIntrinsicId:
3479 case IntrinsicHelper::UnknownId:
3480 //default:
3481 // NOTE: "default" is intentionally commented so that C/C++ compiler will
3482 // give some warning on unmatched cases.
3483 // NOTE: We should not implement these cases.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003484 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003485 }
Logan Chien75e4b602012-07-23 14:24:12 -07003486 UNIMPLEMENTED(FATAL) << "Unexpected GBC intrinsic: " << static_cast<int>(intr_id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003487 return NULL;
3488}
3489
3490} // anonymous namespace
3491
3492namespace art {
3493namespace compiler_llvm {
3494
3495llvm::FunctionPass*
3496CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb) {
3497 return new GBCExpanderPass(intrinsic_helper, irb);
3498}
3499
3500} // namespace compiler_llvm
3501} // namespace art