blob: 94cc9731aaaafc0ecccdcddd6218cc0d8639c4e3 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -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 "dex_file-inl.h"
18#include "driver/compiler_driver.h"
19#include "driver/dex_compilation_unit.h"
20#include "intrinsic_helper.h"
21#include "ir_builder.h"
22#include "method_reference.h"
23#include "mirror/abstract_method.h"
24#include "mirror/array.h"
25#include "mirror/string.h"
26#include "thread.h"
27#include "utils_llvm.h"
28#include "verifier/method_verifier.h"
29
30#include "dex/compiler_ir.h"
31#include "dex/mir_graph.h"
32#include "dex/quick/mir_to_lir.h"
33using art::kMIRIgnoreNullCheck;
34using art::kMIRIgnoreRangeCheck;
35
36#include <llvm/ADT/STLExtras.h>
37#include <llvm/IR/Intrinsics.h>
38#include <llvm/IR/Metadata.h>
39#include <llvm/Pass.h>
40#include <llvm/Support/CFG.h>
41#include <llvm/Support/InstIterator.h>
42
43#include <vector>
44#include <map>
45#include <utility>
46
47using namespace art::llvm;
48
49using art::llvm::IntrinsicHelper;
50
51namespace art {
52extern char RemapShorty(char shortyType);
53};
54
55namespace {
56
57class GBCExpanderPass : public llvm::FunctionPass {
58 private:
59 const IntrinsicHelper& intrinsic_helper_;
60 IRBuilder& irb_;
61
62 llvm::LLVMContext& context_;
63 RuntimeSupportBuilder& rtb_;
64
65 private:
66 llvm::AllocaInst* shadow_frame_;
67 llvm::Value* old_shadow_frame_;
68
69 private:
70 art::CompilerDriver* const driver_;
71
72 const art::DexCompilationUnit* const dex_compilation_unit_;
73
74 llvm::Function* func_;
75
76 std::vector<llvm::BasicBlock*> basic_blocks_;
77
78 std::vector<llvm::BasicBlock*> basic_block_landing_pads_;
79 llvm::BasicBlock* current_bb_;
80 std::map<llvm::BasicBlock*, std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> > >
81 landing_pad_phi_mapping_;
82 llvm::BasicBlock* basic_block_unwind_;
83
84 // Maps each vreg to its shadow frame address.
85 std::vector<llvm::Value*> shadow_frame_vreg_addresses_;
86
87 bool changed_;
88
89 private:
90 //----------------------------------------------------------------------------
91 // Constant for GBC expansion
92 //----------------------------------------------------------------------------
93 enum IntegerShiftKind {
94 kIntegerSHL,
95 kIntegerSHR,
96 kIntegerUSHR,
97 };
98
99 private:
100 //----------------------------------------------------------------------------
101 // Helper function for GBC expansion
102 //----------------------------------------------------------------------------
103
104 llvm::Value* ExpandToRuntime(runtime_support::RuntimeId rt,
105 llvm::CallInst& inst);
106
107 uint64_t LV2UInt(llvm::Value* lv) {
108 return llvm::cast<llvm::ConstantInt>(lv)->getZExtValue();
109 }
110
111 int64_t LV2SInt(llvm::Value* lv) {
112 return llvm::cast<llvm::ConstantInt>(lv)->getSExtValue();
113 }
114
115 private:
116 // TODO: Almost all Emit* are directly copy-n-paste from MethodCompiler.
117 // Refactor these utility functions from MethodCompiler to avoid forking.
118
119 void EmitStackOverflowCheck(llvm::Instruction* first_non_alloca);
120
121 void RewriteFunction();
122
123 void RewriteBasicBlock(llvm::BasicBlock* original_block);
124
125 void UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
126 llvm::BasicBlock* new_basic_block);
127
128
129 // Sign or zero extend category 1 types < 32bits in size to 32bits.
130 llvm::Value* SignOrZeroExtendCat1Types(llvm::Value* value, JType jty);
131
132 // Truncate category 1 types from 32bits to the given JType size.
133 llvm::Value* TruncateCat1Types(llvm::Value* value, JType jty);
134
135 //----------------------------------------------------------------------------
136 // Dex cache code generation helper function
137 //----------------------------------------------------------------------------
138 llvm::Value* EmitLoadDexCacheAddr(art::MemberOffset dex_cache_offset);
139
140 llvm::Value* EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx);
141
142 llvm::Value* EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx);
143
144 llvm::Value* EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx);
145
146 llvm::Value* EmitLoadDexCacheStringFieldAddr(uint32_t string_idx);
147
148 //----------------------------------------------------------------------------
149 // Code generation helper function
150 //----------------------------------------------------------------------------
151 llvm::Value* EmitLoadMethodObjectAddr();
152
153 llvm::Value* EmitLoadArrayLength(llvm::Value* array);
154
155 llvm::Value* EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx);
156
157 llvm::Value* EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
158 llvm::Value* this_addr);
159
160 llvm::Value* EmitArrayGEP(llvm::Value* array_addr,
161 llvm::Value* index_value,
162 JType elem_jty);
163
164 //----------------------------------------------------------------------------
165 // Invoke helper function
166 //----------------------------------------------------------------------------
167 llvm::Value* EmitInvoke(llvm::CallInst& call_inst);
168
169 //----------------------------------------------------------------------------
170 // Inlining helper functions
171 //----------------------------------------------------------------------------
172 bool EmitIntrinsic(llvm::CallInst& call_inst, llvm::Value** result);
173
174 bool EmitIntrinsicStringLengthOrIsEmpty(llvm::CallInst& call_inst,
175 llvm::Value** result, bool is_empty);
176
177 private:
178 //----------------------------------------------------------------------------
179 // Expand Greenland intrinsics
180 //----------------------------------------------------------------------------
181 void Expand_TestSuspend(llvm::CallInst& call_inst);
182
183 void Expand_MarkGCCard(llvm::CallInst& call_inst);
184
185 llvm::Value* Expand_LoadStringFromDexCache(llvm::Value* string_idx_value);
186
187 llvm::Value* Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value);
188
189 void Expand_LockObject(llvm::Value* obj);
190
191 void Expand_UnlockObject(llvm::Value* obj);
192
193 llvm::Value* Expand_ArrayGet(llvm::Value* array_addr,
194 llvm::Value* index_value,
195 JType elem_jty);
196
197 void Expand_ArrayPut(llvm::Value* new_value,
198 llvm::Value* array_addr,
199 llvm::Value* index_value,
200 JType elem_jty);
201
202 void Expand_FilledNewArray(llvm::CallInst& call_inst);
203
204 llvm::Value* Expand_IGetFast(llvm::Value* field_offset_value,
205 llvm::Value* is_volatile_value,
206 llvm::Value* object_addr,
207 JType field_jty);
208
209 void Expand_IPutFast(llvm::Value* field_offset_value,
210 llvm::Value* is_volatile_value,
211 llvm::Value* object_addr,
212 llvm::Value* new_value,
213 JType field_jty);
214
215 llvm::Value* Expand_SGetFast(llvm::Value* static_storage_addr,
216 llvm::Value* field_offset_value,
217 llvm::Value* is_volatile_value,
218 JType field_jty);
219
220 void Expand_SPutFast(llvm::Value* static_storage_addr,
221 llvm::Value* field_offset_value,
222 llvm::Value* is_volatile_value,
223 llvm::Value* new_value,
224 JType field_jty);
225
226 llvm::Value* Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr);
227
228 llvm::Value* Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value);
229
230 llvm::Value*
231 Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value);
232
233 llvm::Value*
234 Expand_GetVirtualCalleeMethodObjAddrFast(llvm::Value* vtable_idx_value,
235 llvm::Value* this_addr);
236
237 llvm::Value* Expand_Invoke(llvm::CallInst& call_inst);
238
239 llvm::Value* Expand_DivRem(llvm::CallInst& call_inst, bool is_div, JType op_jty);
240
241 void Expand_AllocaShadowFrame(llvm::Value* num_vregs_value);
242
243 void Expand_SetVReg(llvm::Value* entry_idx, llvm::Value* obj);
244
245 void Expand_PopShadowFrame();
246
247 void Expand_UpdateDexPC(llvm::Value* dex_pc_value);
248
249 //----------------------------------------------------------------------------
250 // Quick
251 //----------------------------------------------------------------------------
252
253 llvm::Value* Expand_FPCompare(llvm::Value* src1_value,
254 llvm::Value* src2_value,
255 bool gt_bias);
256
257 llvm::Value* Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value);
258
259 llvm::Value* EmitCompareResultSelection(llvm::Value* cmp_eq,
260 llvm::Value* cmp_lt);
261
262 llvm::Value* EmitLoadConstantClass(uint32_t dex_pc, uint32_t type_idx);
263 llvm::Value* EmitLoadStaticStorage(uint32_t dex_pc, uint32_t type_idx);
264
265 llvm::Value* Expand_HLIGet(llvm::CallInst& call_inst, JType field_jty);
266 void Expand_HLIPut(llvm::CallInst& call_inst, JType field_jty);
267
268 llvm::Value* Expand_HLSget(llvm::CallInst& call_inst, JType field_jty);
269 void Expand_HLSput(llvm::CallInst& call_inst, JType field_jty);
270
271 llvm::Value* Expand_HLArrayGet(llvm::CallInst& call_inst, JType field_jty);
272 void Expand_HLArrayPut(llvm::CallInst& call_inst, JType field_jty);
273
274 llvm::Value* Expand_ConstString(llvm::CallInst& call_inst);
275 llvm::Value* Expand_ConstClass(llvm::CallInst& call_inst);
276
277 void Expand_MonitorEnter(llvm::CallInst& call_inst);
278 void Expand_MonitorExit(llvm::CallInst& call_inst);
279
280 void Expand_HLCheckCast(llvm::CallInst& call_inst);
281 llvm::Value* Expand_InstanceOf(llvm::CallInst& call_inst);
282
283 llvm::Value* Expand_NewInstance(llvm::CallInst& call_inst);
284
285 llvm::Value* Expand_HLInvoke(llvm::CallInst& call_inst);
286
287 llvm::Value* Expand_OptArrayLength(llvm::CallInst& call_inst);
288 llvm::Value* Expand_NewArray(llvm::CallInst& call_inst);
289 llvm::Value* Expand_HLFilledNewArray(llvm::CallInst& call_inst);
290 void Expand_HLFillArrayData(llvm::CallInst& call_inst);
291
292 llvm::Value* EmitAllocNewArray(uint32_t dex_pc,
293 llvm::Value* array_length_value,
294 uint32_t type_idx,
295 bool is_filled_new_array);
296
297 llvm::Value* EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
298 art::InvokeType invoke_type,
299 llvm::Value* this_addr,
300 uint32_t dex_pc,
301 bool is_fast_path);
302
303 void EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr);
304
305 void EmitUpdateDexPC(uint32_t dex_pc);
306
307 void EmitGuard_DivZeroException(uint32_t dex_pc,
308 llvm::Value* denominator,
309 JType op_jty);
310
311 void EmitGuard_NullPointerException(uint32_t dex_pc, llvm::Value* object,
312 int opt_flags);
313
314 void EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
315 llvm::Value* array,
316 llvm::Value* index,
317 int opt_flags);
318
319 llvm::FunctionType* GetFunctionType(llvm::Type* ret_type, uint32_t method_idx, bool is_static);
320
321 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
322
323 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
324 const char* postfix);
325
326 int32_t GetTryItemOffset(uint32_t dex_pc);
327
328 llvm::BasicBlock* GetLandingPadBasicBlock(uint32_t dex_pc);
329
330 llvm::BasicBlock* GetUnwindBasicBlock();
331
332 void EmitGuard_ExceptionLandingPad(uint32_t dex_pc);
333
334 void EmitBranchExceptionLandingPad(uint32_t dex_pc);
335
336 //----------------------------------------------------------------------------
337 // Expand Arithmetic Helper Intrinsics
338 //----------------------------------------------------------------------------
339
340 llvm::Value* Expand_IntegerShift(llvm::Value* src1_value,
341 llvm::Value* src2_value,
342 IntegerShiftKind kind,
343 JType op_jty);
344
345 public:
346 static char ID;
347
348 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
349 art::CompilerDriver* driver, const art::DexCompilationUnit* dex_compilation_unit)
350 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
351 context_(irb.getContext()), rtb_(irb.Runtime()),
352 shadow_frame_(NULL), old_shadow_frame_(NULL),
353 driver_(driver),
354 dex_compilation_unit_(dex_compilation_unit),
355 func_(NULL), current_bb_(NULL), basic_block_unwind_(NULL), changed_(false) {}
356
357 bool runOnFunction(llvm::Function& func);
358
359 private:
360 void InsertStackOverflowCheck(llvm::Function& func);
361
362 llvm::Value* ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
363 llvm::CallInst& call_inst);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700364};
365
366char GBCExpanderPass::ID = 0;
367
368bool GBCExpanderPass::runOnFunction(llvm::Function& func) {
369 VLOG(compiler) << "GBC expansion on " << func.getName().str();
370
371 // Runtime support or stub
372 if (dex_compilation_unit_ == NULL) {
373 return false;
374 }
375
376 // Setup rewrite context
377 shadow_frame_ = NULL;
378 old_shadow_frame_ = NULL;
379 func_ = &func;
380 changed_ = false; // Assume unchanged
381
382 shadow_frame_vreg_addresses_.resize(dex_compilation_unit_->GetCodeItem()->registers_size_, NULL);
383 basic_blocks_.resize(dex_compilation_unit_->GetCodeItem()->insns_size_in_code_units_);
384 basic_block_landing_pads_.resize(dex_compilation_unit_->GetCodeItem()->tries_size_, NULL);
385 basic_block_unwind_ = NULL;
386 for (llvm::Function::iterator bb_iter = func_->begin(), bb_end = func_->end();
387 bb_iter != bb_end;
388 ++bb_iter) {
389 if (bb_iter->begin()->getMetadata("DexOff") == NULL) {
390 continue;
391 }
392 uint32_t dex_pc = LV2UInt(bb_iter->begin()->getMetadata("DexOff")->getOperand(0));
393 basic_blocks_[dex_pc] = bb_iter;
394 }
395
396 // Insert stack overflow check
397 InsertStackOverflowCheck(func); // TODO: Use intrinsic.
398
399 // Rewrite the intrinsics
400 RewriteFunction();
401
402 VERIFY_LLVM_FUNCTION(func);
403
404 return changed_;
405}
406
407void GBCExpanderPass::RewriteBasicBlock(llvm::BasicBlock* original_block) {
408 llvm::BasicBlock* curr_basic_block = original_block;
409
410 llvm::BasicBlock::iterator inst_iter = original_block->begin();
411 llvm::BasicBlock::iterator inst_end = original_block->end();
412
413 while (inst_iter != inst_end) {
414 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst_iter);
415 IntrinsicHelper::IntrinsicId intr_id = IntrinsicHelper::UnknownId;
416
417 if (call_inst) {
418 llvm::Function* callee_func = call_inst->getCalledFunction();
419 intr_id = intrinsic_helper_.GetIntrinsicId(callee_func);
420 }
421
422 if (intr_id == IntrinsicHelper::UnknownId) {
423 // This is not intrinsic call. Skip this instruction.
424 ++inst_iter;
425 continue;
426 }
427
428 // Rewrite the intrinsic and change the function
429 changed_ = true;
430 irb_.SetInsertPoint(inst_iter);
431
432 // Expand the intrinsic
433 if (llvm::Value* new_value = ExpandIntrinsic(intr_id, *call_inst)) {
434 inst_iter->replaceAllUsesWith(new_value);
435 }
436
437 // Remove the old intrinsic call instruction
438 llvm::BasicBlock::iterator old_inst = inst_iter++;
439 old_inst->eraseFromParent();
440
441 // Splice the instruction to the new basic block
442 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
443 if (next_basic_block != curr_basic_block) {
444 next_basic_block->getInstList().splice(
445 irb_.GetInsertPoint(), curr_basic_block->getInstList(),
446 inst_iter, inst_end);
447 curr_basic_block = next_basic_block;
448 inst_end = curr_basic_block->end();
449 }
450 }
451}
452
453
454void GBCExpanderPass::RewriteFunction() {
455 size_t num_basic_blocks = func_->getBasicBlockList().size();
456 // NOTE: We are not using (bb_iter != bb_end) as the for-loop condition,
457 // because we will create new basic block while expanding the intrinsics.
458 // We only want to iterate through the input basic blocks.
459
460 landing_pad_phi_mapping_.clear();
461
462 for (llvm::Function::iterator bb_iter = func_->begin();
463 num_basic_blocks > 0; ++bb_iter, --num_basic_blocks) {
464 // Set insert point to current basic block.
465 irb_.SetInsertPoint(bb_iter);
466
467 current_bb_ = bb_iter;
468
469 // Rewrite the basic block
470 RewriteBasicBlock(bb_iter);
471
472 // Update the phi-instructions in the successor basic block
473 llvm::BasicBlock* last_block = irb_.GetInsertBlock();
474 if (last_block != bb_iter) {
475 UpdatePhiInstruction(bb_iter, last_block);
476 }
477 }
478
479 typedef std::map<llvm::PHINode*, llvm::PHINode*> HandlerPHIMap;
480 HandlerPHIMap handler_phi;
481 // Iterate every used landing pad basic block
482 for (size_t i = 0, ei = basic_block_landing_pads_.size(); i != ei; ++i) {
483 llvm::BasicBlock* lbb = basic_block_landing_pads_[i];
484 if (lbb == NULL) {
485 continue;
486 }
487
488 llvm::TerminatorInst* term_inst = lbb->getTerminator();
489 std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> >& rewrite_pair
490 = landing_pad_phi_mapping_[lbb];
491 irb_.SetInsertPoint(lbb->begin());
492
493 // Iterate every succeeding basic block (catch block)
494 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
495 succ_iter != succ_end; ++succ_iter) {
496 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
497
498 // Iterate every phi instructions in the succeeding basic block
499 for (llvm::BasicBlock::iterator
500 inst_iter = succ_basic_block->begin(),
501 inst_end = succ_basic_block->end();
502 inst_iter != inst_end; ++inst_iter) {
503 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
504
505 if (!phi) {
506 break; // Meet non-phi instruction. Done.
507 }
508
509 if (handler_phi[phi] == NULL) {
510 handler_phi[phi] = llvm::PHINode::Create(phi->getType(), 1);
511 }
512
513 // Create new_phi in landing pad
514 llvm::PHINode* new_phi = irb_.CreatePHI(phi->getType(), rewrite_pair.size());
515 // Insert all incoming value into new_phi by rewrite_pair
516 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
517 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
518 llvm::BasicBlock* new_bb = rewrite_pair[j].second;
519 new_phi->addIncoming(phi->getIncomingValueForBlock(old_bb), new_bb);
520 }
521 // Delete all incoming value from phi by rewrite_pair
522 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
523 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
524 int old_bb_idx = phi->getBasicBlockIndex(old_bb);
525 if (old_bb_idx >= 0) {
526 phi->removeIncomingValue(old_bb_idx, false);
527 }
528 }
529 // Insert new_phi into new handler phi
530 handler_phi[phi]->addIncoming(new_phi, lbb);
531 }
532 }
533 }
534
535 // Replace all handler phi
536 // We can't just use the old handler phi, because some exception edges will disappear after we
537 // compute fast-path.
538 for (HandlerPHIMap::iterator it = handler_phi.begin(); it != handler_phi.end(); ++it) {
539 llvm::PHINode* old_phi = it->first;
540 llvm::PHINode* new_phi = it->second;
541 new_phi->insertBefore(old_phi);
542 old_phi->replaceAllUsesWith(new_phi);
543 old_phi->eraseFromParent();
544 }
545}
546
547void GBCExpanderPass::UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
548 llvm::BasicBlock* new_basic_block) {
549 llvm::TerminatorInst* term_inst = new_basic_block->getTerminator();
550
551 if (!term_inst) {
552 return; // No terminating instruction in new_basic_block. Nothing to do.
553 }
554
555 // Iterate every succeeding basic block
556 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
557 succ_iter != succ_end; ++succ_iter) {
558 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
559
560 // Iterate every phi instructions in the succeeding basic block
561 for (llvm::BasicBlock::iterator
562 inst_iter = succ_basic_block->begin(),
563 inst_end = succ_basic_block->end();
564 inst_iter != inst_end; ++inst_iter) {
565 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
566
567 if (!phi) {
568 break; // Meet non-phi instruction. Done.
569 }
570
571 // Update the incoming block of this phi instruction
572 for (llvm::PHINode::block_iterator
573 ibb_iter = phi->block_begin(), ibb_end = phi->block_end();
574 ibb_iter != ibb_end; ++ibb_iter) {
575 if (*ibb_iter == old_basic_block) {
576 *ibb_iter = new_basic_block;
577 }
578 }
579 }
580 }
581}
582
583llvm::Value* GBCExpanderPass::ExpandToRuntime(runtime_support::RuntimeId rt,
584 llvm::CallInst& inst) {
585 // Some GBC intrinsic can directly replace with IBC runtime. "Directly" means
586 // the arguments passed to the GBC intrinsic are as the same as IBC runtime
587 // function, therefore only called function is needed to change.
588 unsigned num_args = inst.getNumArgOperands();
589
590 if (num_args <= 0) {
591 return irb_.CreateCall(irb_.GetRuntime(rt));
592 } else {
593 std::vector<llvm::Value*> args;
594 for (unsigned i = 0; i < num_args; i++) {
595 args.push_back(inst.getArgOperand(i));
596 }
597
598 return irb_.CreateCall(irb_.GetRuntime(rt), args);
599 }
600}
601
602void
603GBCExpanderPass::EmitStackOverflowCheck(llvm::Instruction* first_non_alloca) {
604 llvm::Function* func = first_non_alloca->getParent()->getParent();
605 llvm::Module* module = func->getParent();
606
607 // Call llvm intrinsic function to get frame address.
608 llvm::Function* frameaddress =
609 llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
610
611 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
612 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
613
614 // Cast i8* to int
615 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
616
617 // Get thread.stack_end_
618 llvm::Value* stack_end =
619 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::StackEndOffset().Int32Value(),
620 irb_.getPtrEquivIntTy(),
621 kTBAARuntimeInfo);
622
623 // Check the frame address < thread.stack_end_ ?
624 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
625
626 llvm::BasicBlock* block_exception =
627 llvm::BasicBlock::Create(context_, "stack_overflow", func);
628
629 llvm::BasicBlock* block_continue =
630 llvm::BasicBlock::Create(context_, "stack_overflow_cont", func);
631
632 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
633
634 // If stack overflow, throw exception.
635 irb_.SetInsertPoint(block_exception);
636 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowStackOverflowException));
637
638 // Unwind.
639 llvm::Type* ret_type = func->getReturnType();
640 if (ret_type->isVoidTy()) {
641 irb_.CreateRetVoid();
642 } else {
643 // The return value is ignored when there's an exception. MethodCompiler
644 // returns zero value under the the corresponding return type in this case.
645 // GBCExpander returns LLVM undef value here for brevity
646 irb_.CreateRet(llvm::UndefValue::get(ret_type));
647 }
648
649 irb_.SetInsertPoint(block_continue);
650}
651
652llvm::Value* GBCExpanderPass::EmitLoadDexCacheAddr(art::MemberOffset offset) {
653 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
654
655 return irb_.LoadFromObjectOffset(method_object_addr,
656 offset.Int32Value(),
657 irb_.getJObjectTy(),
658 kTBAAConstJObject);
659}
660
661llvm::Value*
662GBCExpanderPass::EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
663 llvm::Value* static_storage_dex_cache_addr =
664 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheInitializedStaticStorageOffset());
665
666 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
667
668 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
669}
670
671llvm::Value*
672GBCExpanderPass::EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
673 llvm::Value* resolved_type_dex_cache_addr =
674 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedTypesOffset());
675
676 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
677
678 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
679}
680
681llvm::Value* GBCExpanderPass::
682EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
683 llvm::Value* resolved_method_dex_cache_addr =
684 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedMethodsOffset());
685
686 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
687
688 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
689}
690
691llvm::Value* GBCExpanderPass::
692EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
693 llvm::Value* string_dex_cache_addr =
694 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheStringsOffset());
695
696 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
697
698 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
699}
700
701llvm::Value* GBCExpanderPass::EmitLoadMethodObjectAddr() {
702 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
703 return parent_func->arg_begin();
704}
705
706llvm::Value* GBCExpanderPass::EmitLoadArrayLength(llvm::Value* array) {
707 // Load array length
708 return irb_.LoadFromObjectOffset(array,
709 art::mirror::Array::LengthOffset().Int32Value(),
710 irb_.getJIntTy(),
711 kTBAAConstJObject);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712}
713
714llvm::Value*
715GBCExpanderPass::EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
716 llvm::Value* callee_method_object_field_addr =
717 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
718
719 return irb_.CreateLoad(callee_method_object_field_addr, kTBAARuntimeInfo);
720}
721
722llvm::Value* GBCExpanderPass::
723EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, llvm::Value* this_addr) {
724 // Load class object of *this* pointer
725 llvm::Value* class_object_addr =
726 irb_.LoadFromObjectOffset(this_addr,
727 art::mirror::Object::ClassOffset().Int32Value(),
728 irb_.getJObjectTy(),
729 kTBAAConstJObject);
730
731 // Load vtable address
732 llvm::Value* vtable_addr =
733 irb_.LoadFromObjectOffset(class_object_addr,
734 art::mirror::Class::VTableOffset().Int32Value(),
735 irb_.getJObjectTy(),
736 kTBAAConstJObject);
737
738 // Load callee method object
739 llvm::Value* vtable_idx_value =
740 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
741
742 llvm::Value* method_field_addr =
743 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
744
745 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
746}
747
748// Emit Array GetElementPtr
749llvm::Value* GBCExpanderPass::EmitArrayGEP(llvm::Value* array_addr,
750 llvm::Value* index_value,
751 JType elem_jty) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700752 int data_offset;
753 if (elem_jty == kLong || elem_jty == kDouble ||
754 (elem_jty == kObject && sizeof(uint64_t) == sizeof(art::mirror::Object*))) {
755 data_offset = art::mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
756 } else {
757 data_offset = art::mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
758 }
759
760 llvm::Constant* data_offset_value =
761 irb_.getPtrEquivInt(data_offset);
762
763 llvm::Type* elem_type = irb_.getJType(elem_jty);
764
765 llvm::Value* array_data_addr =
766 irb_.CreatePtrDisp(array_addr, data_offset_value,
767 elem_type->getPointerTo());
768
769 return irb_.CreateGEP(array_data_addr, index_value);
770}
771
772llvm::Value* GBCExpanderPass::EmitInvoke(llvm::CallInst& call_inst) {
773 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
774 art::InvokeType invoke_type =
775 static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
776 bool is_static = (invoke_type == art::kStatic);
777 art::MethodReference target_method(dex_compilation_unit_->GetDexFile(),
778 LV2UInt(call_inst.getArgOperand(1)));
779
780 // Load *this* actual parameter
781 llvm::Value* this_addr = (!is_static) ? call_inst.getArgOperand(3) : NULL;
782
783 // Compute invoke related information for compiler decision
784 int vtable_idx = -1;
785 uintptr_t direct_code = 0;
786 uintptr_t direct_method = 0;
787 bool is_fast_path = driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc,
788 invoke_type, target_method,
789 vtable_idx,
790 direct_code, direct_method,
791 true);
792 // Load the method object
793 llvm::Value* callee_method_object_addr = NULL;
794
795 if (!is_fast_path) {
796 callee_method_object_addr =
797 EmitCallRuntimeForCalleeMethodObjectAddr(target_method.dex_method_index, invoke_type,
798 this_addr, dex_pc, is_fast_path);
799 } else {
800 switch (invoke_type) {
801 case art::kStatic:
802 case art::kDirect:
803 if (direct_method != 0u &&
804 direct_method != static_cast<uintptr_t>(-1)) {
805 callee_method_object_addr =
806 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
807 irb_.getJObjectTy());
808 } else {
809 callee_method_object_addr =
810 EmitLoadSDCalleeMethodObjectAddr(target_method.dex_method_index);
811 }
812 break;
813
814 case art::kVirtual:
815 DCHECK(vtable_idx != -1);
816 callee_method_object_addr =
817 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
818 break;
819
820 case art::kSuper:
821 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
822 "the fast path.";
823 break;
824
825 case art::kInterface:
826 callee_method_object_addr =
827 EmitCallRuntimeForCalleeMethodObjectAddr(target_method.dex_method_index,
828 invoke_type, this_addr,
829 dex_pc, is_fast_path);
830 break;
831 }
832 }
833
834 // Load the actual parameter
835 std::vector<llvm::Value*> args;
836
837 args.push_back(callee_method_object_addr); // method object for callee
838
839 for (uint32_t i = 3; i < call_inst.getNumArgOperands(); ++i) {
840 args.push_back(call_inst.getArgOperand(i));
841 }
842
843 llvm::Value* code_addr;
844 llvm::Type* func_type = GetFunctionType(call_inst.getType(),
845 target_method.dex_method_index, is_static);
846 if (direct_code != 0u && direct_code != static_cast<uintptr_t>(-1)) {
847 code_addr =
848 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_code),
849 func_type->getPointerTo());
850 } else {
851 code_addr =
852 irb_.LoadFromObjectOffset(callee_method_object_addr,
853 art::mirror::AbstractMethod::GetEntryPointFromCompiledCodeOffset().Int32Value(),
854 func_type->getPointerTo(), kTBAARuntimeInfo);
855 }
856
857 // Invoke callee
858 EmitUpdateDexPC(dex_pc);
859 llvm::Value* retval = irb_.CreateCall(code_addr, args);
860 EmitGuard_ExceptionLandingPad(dex_pc);
861
862 return retval;
863}
864
865bool GBCExpanderPass::EmitIntrinsic(llvm::CallInst& call_inst,
866 llvm::Value** result) {
867 DCHECK(result != NULL);
868
869 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
870 std::string callee_method_name(
871 PrettyMethod(callee_method_idx, *dex_compilation_unit_->GetDexFile()));
872
873 if (callee_method_name == "int java.lang.String.length()") {
874 return EmitIntrinsicStringLengthOrIsEmpty(call_inst, result,
875 false /* is_empty */);
876 }
877 if (callee_method_name == "boolean java.lang.String.isEmpty()") {
878 return EmitIntrinsicStringLengthOrIsEmpty(call_inst, result,
879 true /* is_empty */);
880 }
881
882 *result = NULL;
883 return false;
884}
885
886bool GBCExpanderPass::EmitIntrinsicStringLengthOrIsEmpty(llvm::CallInst& call_inst,
887 llvm::Value** result,
888 bool is_empty) {
889 art::InvokeType invoke_type =
890 static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
891 DCHECK_NE(invoke_type, art::kStatic);
892 DCHECK_EQ(call_inst.getNumArgOperands(), 4U);
893
894 llvm::Value* this_object = call_inst.getArgOperand(3);
895 llvm::Value* string_count =
896 irb_.LoadFromObjectOffset(this_object,
897 art::mirror::String::CountOffset().Int32Value(),
898 irb_.getJIntTy(),
899 kTBAAConstJObject);
900 if (is_empty) {
901 llvm::Value* count_equals_zero = irb_.CreateICmpEQ(string_count,
902 irb_.getJInt(0));
903 llvm::Value* is_empty = irb_.CreateSelect(count_equals_zero,
904 irb_.getJBoolean(true),
905 irb_.getJBoolean(false));
906 is_empty = SignOrZeroExtendCat1Types(is_empty, kBoolean);
907 *result = is_empty;
908 } else {
909 *result = string_count;
910 }
911 return true;
912}
913
914void GBCExpanderPass::Expand_TestSuspend(llvm::CallInst& call_inst) {
915 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
916
917 llvm::Value* suspend_count =
918 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::ThreadFlagsOffset().Int32Value(),
919 irb_.getInt16Ty(),
920 kTBAARuntimeInfo);
921 llvm::Value* is_suspend = irb_.CreateICmpNE(suspend_count, irb_.getInt16(0));
922
923 llvm::BasicBlock* basic_block_suspend = CreateBasicBlockWithDexPC(dex_pc, "suspend");
924 llvm::BasicBlock* basic_block_cont = CreateBasicBlockWithDexPC(dex_pc, "suspend_cont");
925
926 irb_.CreateCondBr(is_suspend, basic_block_suspend, basic_block_cont, kUnlikely);
927
928 irb_.SetInsertPoint(basic_block_suspend);
929 if (dex_pc != art::DexFile::kDexNoIndex) {
930 EmitUpdateDexPC(dex_pc);
931 }
932 irb_.Runtime().EmitTestSuspend();
933
934 llvm::BasicBlock* basic_block_exception = CreateBasicBlockWithDexPC(dex_pc, "exception");
935 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
936 irb_.CreateCondBr(exception_pending, basic_block_exception, basic_block_cont, kUnlikely);
937
938 irb_.SetInsertPoint(basic_block_exception);
939 llvm::Type* ret_type = call_inst.getParent()->getParent()->getReturnType();
940 if (ret_type->isVoidTy()) {
941 irb_.CreateRetVoid();
942 } else {
943 // The return value is ignored when there's an exception.
944 irb_.CreateRet(llvm::UndefValue::get(ret_type));
945 }
946
947 irb_.SetInsertPoint(basic_block_cont);
948 return;
949}
950
951void GBCExpanderPass::Expand_MarkGCCard(llvm::CallInst& call_inst) {
952 irb_.Runtime().EmitMarkGCCard(call_inst.getArgOperand(0), call_inst.getArgOperand(1));
953 return;
954}
955
956llvm::Value*
957GBCExpanderPass::Expand_LoadStringFromDexCache(llvm::Value* string_idx_value) {
958 uint32_t string_idx =
959 llvm::cast<llvm::ConstantInt>(string_idx_value)->getZExtValue();
960
961 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
962
963 return irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
964}
965
966llvm::Value*
967GBCExpanderPass::Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value) {
968 uint32_t type_idx =
969 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
970
971 llvm::Value* type_field_addr =
972 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
973
974 return irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
975}
976
977void GBCExpanderPass::Expand_LockObject(llvm::Value* obj) {
978 rtb_.EmitLockObject(obj);
979 return;
980}
981
982void GBCExpanderPass::Expand_UnlockObject(llvm::Value* obj) {
983 rtb_.EmitUnlockObject(obj);
984 return;
985}
986
987llvm::Value* GBCExpanderPass::Expand_ArrayGet(llvm::Value* array_addr,
988 llvm::Value* index_value,
989 JType elem_jty) {
990 llvm::Value* array_elem_addr =
991 EmitArrayGEP(array_addr, index_value, elem_jty);
992
993 return irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
994}
995
996void GBCExpanderPass::Expand_ArrayPut(llvm::Value* new_value,
997 llvm::Value* array_addr,
998 llvm::Value* index_value,
999 JType elem_jty) {
1000 llvm::Value* array_elem_addr =
1001 EmitArrayGEP(array_addr, index_value, elem_jty);
1002
1003 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1004
1005 return;
1006}
1007
1008void GBCExpanderPass::Expand_FilledNewArray(llvm::CallInst& call_inst) {
1009 // Most of the codes refer to MethodCompiler::EmitInsn_FilledNewArray
1010 llvm::Value* array = call_inst.getArgOperand(0);
1011
1012 uint32_t element_jty =
1013 llvm::cast<llvm::ConstantInt>(call_inst.getArgOperand(1))->getZExtValue();
1014
1015 DCHECK(call_inst.getNumArgOperands() > 2);
1016 unsigned num_elements = (call_inst.getNumArgOperands() - 2);
1017
1018 bool is_elem_int_ty = (static_cast<JType>(element_jty) == kInt);
1019
1020 uint32_t alignment;
1021 llvm::Constant* elem_size;
1022 llvm::PointerType* field_type;
1023
1024 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1025 // as the element, thus we are only checking 2 cases: primitive int and
1026 // non-primitive type.
1027 if (is_elem_int_ty) {
1028 alignment = sizeof(int32_t);
1029 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
1030 field_type = irb_.getJIntTy()->getPointerTo();
1031 } else {
1032 alignment = irb_.getSizeOfPtrEquivInt();
1033 elem_size = irb_.getSizeOfPtrEquivIntValue();
1034 field_type = irb_.getJObjectTy()->getPointerTo();
1035 }
1036
1037 llvm::Value* data_field_offset =
1038 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
1039
1040 llvm::Value* data_field_addr =
1041 irb_.CreatePtrDisp(array, data_field_offset, field_type);
1042
1043 for (unsigned i = 0; i < num_elements; ++i) {
1044 // Values to fill the array begin at the 3rd argument
1045 llvm::Value* reg_value = call_inst.getArgOperand(2 + i);
1046
1047 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
1048
1049 data_field_addr =
1050 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
1051 }
1052
1053 return;
1054}
1055
1056llvm::Value* GBCExpanderPass::Expand_IGetFast(llvm::Value* field_offset_value,
1057 llvm::Value* /*is_volatile_value*/,
1058 llvm::Value* object_addr,
1059 JType field_jty) {
1060 int field_offset =
1061 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1062
1063 DCHECK_GE(field_offset, 0);
1064
1065 llvm::PointerType* field_type =
1066 irb_.getJType(field_jty)->getPointerTo();
1067
1068 field_offset_value = irb_.getPtrEquivInt(field_offset);
1069
1070 llvm::Value* field_addr =
1071 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1072
1073 // TODO: Check is_volatile. We need to generate atomic load instruction
1074 // when is_volatile is true.
1075 return irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
1076}
1077
1078void GBCExpanderPass::Expand_IPutFast(llvm::Value* field_offset_value,
1079 llvm::Value* /* is_volatile_value */,
1080 llvm::Value* object_addr,
1081 llvm::Value* new_value,
1082 JType field_jty) {
1083 int field_offset =
1084 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1085
1086 DCHECK_GE(field_offset, 0);
1087
1088 llvm::PointerType* field_type =
1089 irb_.getJType(field_jty)->getPointerTo();
1090
1091 field_offset_value = irb_.getPtrEquivInt(field_offset);
1092
1093 llvm::Value* field_addr =
1094 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1095
1096 // TODO: Check is_volatile. We need to generate atomic store instruction
1097 // when is_volatile is true.
1098 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1099
1100 return;
1101}
1102
1103llvm::Value* GBCExpanderPass::Expand_SGetFast(llvm::Value* static_storage_addr,
1104 llvm::Value* field_offset_value,
1105 llvm::Value* /*is_volatile_value*/,
1106 JType field_jty) {
1107 int field_offset =
1108 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1109
1110 DCHECK_GE(field_offset, 0);
1111
1112 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1113
1114 llvm::Value* static_field_addr =
1115 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1116 irb_.getJType(field_jty)->getPointerTo());
1117
1118 // TODO: Check is_volatile. We need to generate atomic store instruction
1119 // when is_volatile is true.
1120 return irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
1121}
1122
1123void GBCExpanderPass::Expand_SPutFast(llvm::Value* static_storage_addr,
1124 llvm::Value* field_offset_value,
1125 llvm::Value* /* is_volatile_value */,
1126 llvm::Value* new_value,
1127 JType field_jty) {
1128 int field_offset =
1129 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1130
1131 DCHECK_GE(field_offset, 0);
1132
1133 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1134
1135 llvm::Value* static_field_addr =
1136 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1137 irb_.getJType(field_jty)->getPointerTo());
1138
1139 // TODO: Check is_volatile. We need to generate atomic store instruction
1140 // when is_volatile is true.
1141 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1142
1143 return;
1144}
1145
1146llvm::Value*
1147GBCExpanderPass::Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr) {
1148 return irb_.LoadFromObjectOffset(method_object_addr,
1149 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
1150 irb_.getJObjectTy(),
1151 kTBAAConstJObject);
1152}
1153
1154llvm::Value*
1155GBCExpanderPass::Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value) {
1156 uint32_t type_idx =
1157 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
1158
1159 llvm::Value* storage_field_addr =
1160 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1161
1162 return irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
1163}
1164
1165llvm::Value*
1166GBCExpanderPass::Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value) {
1167 uint32_t callee_method_idx =
1168 llvm::cast<llvm::ConstantInt>(callee_method_idx_value)->getZExtValue();
1169
1170 return EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
1171}
1172
1173llvm::Value* GBCExpanderPass::Expand_GetVirtualCalleeMethodObjAddrFast(
1174 llvm::Value* vtable_idx_value,
1175 llvm::Value* this_addr) {
1176 int vtable_idx =
1177 llvm::cast<llvm::ConstantInt>(vtable_idx_value)->getSExtValue();
1178
1179 return EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
1180}
1181
1182llvm::Value* GBCExpanderPass::Expand_Invoke(llvm::CallInst& call_inst) {
1183 // Most of the codes refer to MethodCompiler::EmitInsn_Invoke
1184 llvm::Value* callee_method_object_addr = call_inst.getArgOperand(0);
1185 unsigned num_args = call_inst.getNumArgOperands();
1186 llvm::Type* ret_type = call_inst.getType();
1187
1188 // Determine the function type of the callee method
1189 std::vector<llvm::Type*> args_type;
1190 std::vector<llvm::Value*> args;
1191 for (unsigned i = 0; i < num_args; i++) {
1192 args.push_back(call_inst.getArgOperand(i));
1193 args_type.push_back(args[i]->getType());
1194 }
1195
1196 llvm::FunctionType* callee_method_type =
1197 llvm::FunctionType::get(ret_type, args_type, false);
1198
1199 llvm::Value* code_addr =
1200 irb_.LoadFromObjectOffset(callee_method_object_addr,
1201 art::mirror::AbstractMethod::GetEntryPointFromCompiledCodeOffset().Int32Value(),
1202 callee_method_type->getPointerTo(),
1203 kTBAARuntimeInfo);
1204
1205 // Invoke callee
1206 llvm::Value* retval = irb_.CreateCall(code_addr, args);
1207
1208 return retval;
1209}
1210
1211llvm::Value* GBCExpanderPass::Expand_DivRem(llvm::CallInst& call_inst,
1212 bool is_div, JType op_jty) {
1213 llvm::Value* dividend = call_inst.getArgOperand(0);
1214 llvm::Value* divisor = call_inst.getArgOperand(1);
1215 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1216 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
1217 // Most of the codes refer to MethodCompiler::EmitIntDivRemResultComputation
1218
1219 // Check the special case: MININT / -1 = MININT
1220 // That case will cause overflow, which is undefined behavior in llvm.
1221 // So we check the divisor is -1 or not, if the divisor is -1, we do
1222 // the special path to avoid undefined behavior.
1223 llvm::Type* op_type = irb_.getJType(op_jty);
1224 llvm::Value* zero = irb_.getJZero(op_jty);
1225 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
1226
1227 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
1228 llvm::BasicBlock* eq_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1229 llvm::BasicBlock* ne_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1230 llvm::BasicBlock* neg_one_cont =
1231 llvm::BasicBlock::Create(context_, "", parent);
1232
1233 llvm::Value* is_equal_neg_one = irb_.CreateICmpEQ(divisor, neg_one);
1234 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
1235
1236 // If divisor == -1
1237 irb_.SetInsertPoint(eq_neg_one);
1238 llvm::Value* eq_result;
1239 if (is_div) {
1240 // We can just change from "dividend div -1" to "neg dividend". The sub
1241 // don't care the sign/unsigned because of two's complement representation.
1242 // And the behavior is what we want:
1243 // -(2^n) (2^n)-1
1244 // MININT < k <= MAXINT -> mul k -1 = -k
1245 // MININT == k -> mul k -1 = k
1246 //
1247 // LLVM use sub to represent 'neg'
1248 eq_result = irb_.CreateSub(zero, dividend);
1249 } else {
1250 // Everything modulo -1 will be 0.
1251 eq_result = zero;
1252 }
1253 irb_.CreateBr(neg_one_cont);
1254
1255 // If divisor != -1, just do the division.
1256 irb_.SetInsertPoint(ne_neg_one);
1257 llvm::Value* ne_result;
1258 if (is_div) {
1259 ne_result = irb_.CreateSDiv(dividend, divisor);
1260 } else {
1261 ne_result = irb_.CreateSRem(dividend, divisor);
1262 }
1263 irb_.CreateBr(neg_one_cont);
1264
1265 irb_.SetInsertPoint(neg_one_cont);
1266 llvm::PHINode* result = irb_.CreatePHI(op_type, 2);
1267 result->addIncoming(eq_result, eq_neg_one);
1268 result->addIncoming(ne_result, ne_neg_one);
1269
1270 return result;
1271}
1272
1273void GBCExpanderPass::Expand_AllocaShadowFrame(llvm::Value* num_vregs_value) {
1274 // Most of the codes refer to MethodCompiler::EmitPrologueAllocShadowFrame and
1275 // MethodCompiler::EmitPushShadowFrame
1276 uint16_t num_vregs =
1277 llvm::cast<llvm::ConstantInt>(num_vregs_value)->getZExtValue();
1278
1279 llvm::StructType* shadow_frame_type =
1280 irb_.getShadowFrameTy(num_vregs);
1281
1282 // Create allocas at the start of entry block.
1283 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1284 llvm::BasicBlock* entry_block = &func_->front();
1285 irb_.SetInsertPoint(&entry_block->front());
1286
1287 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
1288
1289 // Alloca a pointer to old shadow frame
1290 old_shadow_frame_ =
1291 irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
1292
1293 irb_.restoreIP(irb_ip_original);
1294
1295 // Push the shadow frame
1296 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1297
1298 llvm::Value* shadow_frame_upcast =
1299 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
1300
1301 llvm::Value* result = rtb_.EmitPushShadowFrame(shadow_frame_upcast,
1302 method_object_addr,
1303 num_vregs);
1304
1305 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
1306
1307 return;
1308}
1309
1310void GBCExpanderPass::Expand_SetVReg(llvm::Value* entry_idx,
1311 llvm::Value* value) {
1312 unsigned vreg_idx = LV2UInt(entry_idx);
1313 DCHECK_LT(vreg_idx, dex_compilation_unit_->GetCodeItem()->registers_size_);
1314
1315 llvm::Value* vreg_addr = shadow_frame_vreg_addresses_[vreg_idx];
1316 if (UNLIKELY(vreg_addr == NULL)) {
1317 DCHECK(shadow_frame_ != NULL);
1318
1319 llvm::Value* gep_index[] = {
1320 irb_.getInt32(0), // No pointer displacement
1321 irb_.getInt32(1), // VRegs
1322 entry_idx // Pointer field
1323 };
1324
1325 // A shadow frame address must dominate every use in the function so we
1326 // place it in the entry block right after the allocas.
1327 llvm::BasicBlock::iterator first_non_alloca = func_->getEntryBlock().begin();
1328 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1329 ++first_non_alloca;
1330 }
1331
1332 llvm::IRBuilderBase::InsertPoint ip = irb_.saveIP();
1333 irb_.SetInsertPoint(static_cast<llvm::Instruction*>(first_non_alloca));
1334 vreg_addr = irb_.CreateGEP(shadow_frame_, gep_index);
1335 shadow_frame_vreg_addresses_[vreg_idx] = vreg_addr;
1336 irb_.restoreIP(ip);
1337 }
1338
1339 irb_.CreateStore(value,
1340 irb_.CreateBitCast(vreg_addr, value->getType()->getPointerTo()),
1341 kTBAAShadowFrame);
1342 return;
1343}
1344
1345void GBCExpanderPass::Expand_PopShadowFrame() {
1346 if (old_shadow_frame_ == NULL) {
1347 return;
1348 }
1349 rtb_.EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
1350 return;
1351}
1352
1353void GBCExpanderPass::Expand_UpdateDexPC(llvm::Value* dex_pc_value) {
1354 irb_.StoreToObjectOffset(shadow_frame_,
1355 art::ShadowFrame::DexPCOffset(),
1356 dex_pc_value,
1357 kTBAAShadowFrame);
1358 return;
1359}
1360
1361void GBCExpanderPass::InsertStackOverflowCheck(llvm::Function& func) {
1362 // All alloca instructions are generated in the first basic block of the
1363 // function, and there are no alloca instructions after the first non-alloca
1364 // instruction.
1365
1366 llvm::BasicBlock* first_basic_block = &func.front();
1367
1368 // Look for first non-alloca instruction
1369 llvm::BasicBlock::iterator first_non_alloca = first_basic_block->begin();
1370 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1371 ++first_non_alloca;
1372 }
1373
1374 irb_.SetInsertPoint(first_non_alloca);
1375
1376 // Insert stack overflow check codes before first_non_alloca (i.e., after all
1377 // alloca instructions)
1378 EmitStackOverflowCheck(&*first_non_alloca);
1379
1380 irb_.Runtime().EmitTestSuspend();
1381
1382 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
1383 if (next_basic_block != first_basic_block) {
1384 // Splice the rest of the instruction to the continuing basic block
1385 next_basic_block->getInstList().splice(
1386 irb_.GetInsertPoint(), first_basic_block->getInstList(),
1387 first_non_alloca, first_basic_block->end());
1388
1389 // Rewrite the basic block
1390 RewriteBasicBlock(next_basic_block);
1391
1392 // Update the phi-instructions in the successor basic block
1393 UpdatePhiInstruction(first_basic_block, irb_.GetInsertBlock());
1394 }
1395
1396 // We have changed the basic block
1397 changed_ = true;
1398}
1399
1400// ==== High-level intrinsic expander ==========================================
1401
1402llvm::Value* GBCExpanderPass::Expand_FPCompare(llvm::Value* src1_value,
1403 llvm::Value* src2_value,
1404 bool gt_bias) {
1405 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1406 llvm::Value* cmp_lt;
1407
1408 if (gt_bias) {
1409 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1410 } else {
1411 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1412 }
1413
1414 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1415}
1416
1417llvm::Value* GBCExpanderPass::Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value) {
1418 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1419 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1420
1421 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1422}
1423
1424llvm::Value* GBCExpanderPass::EmitCompareResultSelection(llvm::Value* cmp_eq,
1425 llvm::Value* cmp_lt) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001426 llvm::Constant* zero = irb_.getJInt(0);
1427 llvm::Constant* pos1 = irb_.getJInt(1);
1428 llvm::Constant* neg1 = irb_.getJInt(-1);
1429
1430 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1431 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1432
1433 return result_eq;
1434}
1435
1436llvm::Value* GBCExpanderPass::Expand_IntegerShift(llvm::Value* src1_value,
1437 llvm::Value* src2_value,
1438 IntegerShiftKind kind,
1439 JType op_jty) {
1440 DCHECK(op_jty == kInt || op_jty == kLong);
1441
1442 // Mask and zero-extend RHS properly
1443 if (op_jty == kInt) {
1444 src2_value = irb_.CreateAnd(src2_value, 0x1f);
1445 } else {
1446 llvm::Value* masked_src2_value = irb_.CreateAnd(src2_value, 0x3f);
1447 src2_value = irb_.CreateZExt(masked_src2_value, irb_.getJLongTy());
1448 }
1449
1450 // Create integer shift llvm instruction
1451 switch (kind) {
1452 case kIntegerSHL:
1453 return irb_.CreateShl(src1_value, src2_value);
1454
1455 case kIntegerSHR:
1456 return irb_.CreateAShr(src1_value, src2_value);
1457
1458 case kIntegerUSHR:
1459 return irb_.CreateLShr(src1_value, src2_value);
1460
1461 default:
1462 LOG(FATAL) << "Unknown integer shift kind: " << kind;
1463 return NULL;
1464 }
1465}
1466
1467llvm::Value* GBCExpanderPass::SignOrZeroExtendCat1Types(llvm::Value* value, JType jty) {
1468 switch (jty) {
1469 case kBoolean:
1470 case kChar:
1471 return irb_.CreateZExt(value, irb_.getJType(kInt));
1472 case kByte:
1473 case kShort:
1474 return irb_.CreateSExt(value, irb_.getJType(kInt));
1475 case kVoid:
1476 case kInt:
1477 case kLong:
1478 case kFloat:
1479 case kDouble:
1480 case kObject:
1481 return value; // Nothing to do.
1482 default:
1483 LOG(FATAL) << "Unknown java type: " << jty;
1484 return NULL;
1485 }
1486}
1487
1488llvm::Value* GBCExpanderPass::TruncateCat1Types(llvm::Value* value, JType jty) {
1489 switch (jty) {
1490 case kBoolean:
1491 case kChar:
1492 case kByte:
1493 case kShort:
1494 return irb_.CreateTrunc(value, irb_.getJType(jty));
1495 case kVoid:
1496 case kInt:
1497 case kLong:
1498 case kFloat:
1499 case kDouble:
1500 case kObject:
1501 return value; // Nothing to do.
1502 default:
1503 LOG(FATAL) << "Unknown java type: " << jty;
1504 return NULL;
1505 }
1506}
1507
1508llvm::Value* GBCExpanderPass::Expand_HLArrayGet(llvm::CallInst& call_inst,
1509 JType elem_jty) {
1510 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1511 llvm::Value* array_addr = call_inst.getArgOperand(1);
1512 llvm::Value* index_value = call_inst.getArgOperand(2);
1513 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
1514
1515 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1516 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1517 opt_flags);
1518
1519 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1520
1521 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
1522
1523 return SignOrZeroExtendCat1Types(array_elem_value, elem_jty);
1524}
1525
1526
1527void GBCExpanderPass::Expand_HLArrayPut(llvm::CallInst& call_inst,
1528 JType elem_jty) {
1529 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1530 llvm::Value* new_value = call_inst.getArgOperand(1);
1531 llvm::Value* array_addr = call_inst.getArgOperand(2);
1532 llvm::Value* index_value = call_inst.getArgOperand(3);
1533 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
1534
1535 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1536 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1537 opt_flags);
1538
1539 new_value = TruncateCat1Types(new_value, elem_jty);
1540
1541 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1542
1543 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
1544 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::CheckPutArrayElement);
1545
1546 irb_.CreateCall2(runtime_func, new_value, array_addr);
1547
1548 EmitGuard_ExceptionLandingPad(dex_pc);
1549
1550 EmitMarkGCCard(new_value, array_addr);
1551 }
1552
1553 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1554
1555 return;
1556}
1557
1558llvm::Value* GBCExpanderPass::Expand_HLIGet(llvm::CallInst& call_inst,
1559 JType field_jty) {
1560 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1561 llvm::Value* object_addr = call_inst.getArgOperand(1);
1562 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(2));
1563 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
1564
1565 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
1566
1567 llvm::Value* field_value;
1568
1569 int field_offset;
1570 bool is_volatile;
1571 bool is_fast_path = driver_->ComputeInstanceFieldInfo(
1572 field_idx, dex_compilation_unit_, field_offset, is_volatile, false);
1573
1574 if (!is_fast_path) {
1575 llvm::Function* runtime_func;
1576
1577 if (field_jty == kObject) {
1578 runtime_func = irb_.GetRuntime(runtime_support::GetObjectInstance);
1579 } else if (field_jty == kLong || field_jty == kDouble) {
1580 runtime_func = irb_.GetRuntime(runtime_support::Get64Instance);
1581 } else {
1582 runtime_func = irb_.GetRuntime(runtime_support::Get32Instance);
1583 }
1584
1585 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
1586
1587 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1588
1589 EmitUpdateDexPC(dex_pc);
1590
1591 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
1592 method_object_addr, object_addr);
1593
1594 EmitGuard_ExceptionLandingPad(dex_pc);
1595
1596 if (field_jty == kFloat || field_jty == kDouble) {
1597 field_value = irb_.CreateBitCast(field_value, irb_.getJType(field_jty));
1598 }
1599 } else {
1600 DCHECK_GE(field_offset, 0);
1601
1602 llvm::PointerType* field_type =
1603 irb_.getJType(field_jty)->getPointerTo();
1604
1605 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
1606
1607 llvm::Value* field_addr =
1608 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1609
1610 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
1611 field_value = SignOrZeroExtendCat1Types(field_value, field_jty);
1612
1613 if (is_volatile) {
1614 irb_.CreateMemoryBarrier(art::kLoadLoad);
1615 }
1616 }
1617
1618 return field_value;
1619}
1620
1621void GBCExpanderPass::Expand_HLIPut(llvm::CallInst& call_inst,
1622 JType field_jty) {
1623 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1624 llvm::Value* new_value = call_inst.getArgOperand(1);
1625 llvm::Value* object_addr = call_inst.getArgOperand(2);
1626 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(3));
1627 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
1628
1629 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
1630
1631 int field_offset;
1632 bool is_volatile;
1633 bool is_fast_path = driver_->ComputeInstanceFieldInfo(
1634 field_idx, dex_compilation_unit_, field_offset, is_volatile, true);
1635
1636 if (!is_fast_path) {
1637 llvm::Function* runtime_func;
1638
1639 if (field_jty == kFloat) {
1640 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1641 } else if (field_jty == kDouble) {
1642 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1643 }
1644
1645 if (field_jty == kObject) {
1646 runtime_func = irb_.GetRuntime(runtime_support::SetObjectInstance);
1647 } else if (field_jty == kLong || field_jty == kDouble) {
1648 runtime_func = irb_.GetRuntime(runtime_support::Set64Instance);
1649 } else {
1650 runtime_func = irb_.GetRuntime(runtime_support::Set32Instance);
1651 }
1652
1653 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1654
1655 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1656
1657 EmitUpdateDexPC(dex_pc);
1658
1659 irb_.CreateCall4(runtime_func, field_idx_value,
1660 method_object_addr, object_addr, new_value);
1661
1662 EmitGuard_ExceptionLandingPad(dex_pc);
1663
1664 } else {
1665 DCHECK_GE(field_offset, 0);
1666
1667 if (is_volatile) {
1668 irb_.CreateMemoryBarrier(art::kStoreStore);
1669 }
1670
1671 llvm::PointerType* field_type =
1672 irb_.getJType(field_jty)->getPointerTo();
1673
1674 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
1675
1676 llvm::Value* field_addr =
1677 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1678
1679 new_value = TruncateCat1Types(new_value, field_jty);
1680 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1681
1682 if (is_volatile) {
1683 irb_.CreateMemoryBarrier(art::kLoadLoad);
1684 }
1685
1686 if (field_jty == kObject) { // If put an object, mark the GC card table.
1687 EmitMarkGCCard(new_value, object_addr);
1688 }
1689 }
1690
1691 return;
1692}
1693
1694llvm::Value* GBCExpanderPass::EmitLoadConstantClass(uint32_t dex_pc,
1695 uint32_t type_idx) {
1696 if (!driver_->CanAccessTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
1697 *dex_compilation_unit_->GetDexFile(), type_idx)) {
1698 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1699
1700 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1701
1702 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1703
1704 llvm::Function* runtime_func =
1705 irb_.GetRuntime(runtime_support::InitializeTypeAndVerifyAccess);
1706
1707 EmitUpdateDexPC(dex_pc);
1708
1709 llvm::Value* type_object_addr =
1710 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1711
1712 EmitGuard_ExceptionLandingPad(dex_pc);
1713
1714 return type_object_addr;
1715
1716 } else {
1717 // Try to load the class (type) object from the test cache.
1718 llvm::Value* type_field_addr =
1719 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1720
1721 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
1722
1723 if (driver_->CanAssumeTypeIsPresentInDexCache(*dex_compilation_unit_->GetDexFile(), type_idx)) {
1724 return type_object_addr;
1725 }
1726
1727 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1728
1729 // Test whether class (type) object is in the dex cache or not
1730 llvm::Value* equal_null =
1731 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1732
1733 llvm::BasicBlock* block_cont =
1734 CreateBasicBlockWithDexPC(dex_pc, "cont");
1735
1736 llvm::BasicBlock* block_load_class =
1737 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1738
1739 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
1740
1741 // Failback routine to load the class object
1742 irb_.SetInsertPoint(block_load_class);
1743
1744 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeType);
1745
1746 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1747
1748 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1749
1750 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1751
1752 EmitUpdateDexPC(dex_pc);
1753
1754 llvm::Value* loaded_type_object_addr =
1755 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1756
1757 EmitGuard_ExceptionLandingPad(dex_pc);
1758
1759 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1760
1761 irb_.CreateBr(block_cont);
1762
1763 // Now the class object must be loaded
1764 irb_.SetInsertPoint(block_cont);
1765
1766 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1767
1768 phi->addIncoming(type_object_addr, block_original);
1769 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1770
1771 return phi;
1772 }
1773}
1774
1775llvm::Value* GBCExpanderPass::EmitLoadStaticStorage(uint32_t dex_pc,
1776 uint32_t type_idx) {
1777 llvm::BasicBlock* block_load_static =
1778 CreateBasicBlockWithDexPC(dex_pc, "load_static");
1779
1780 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1781
1782 // Load static storage from dex cache
1783 llvm::Value* storage_field_addr =
1784 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1785
1786 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
1787
1788 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1789
1790 // Test: Is the static storage of this class initialized?
1791 llvm::Value* equal_null =
1792 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
1793
1794 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
1795
1796 // Failback routine to load the class object
1797 irb_.SetInsertPoint(block_load_static);
1798
1799 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeStaticStorage);
1800
1801 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1802
1803 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1804
1805 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1806
1807 EmitUpdateDexPC(dex_pc);
1808
1809 llvm::Value* loaded_storage_object_addr =
1810 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1811
1812 EmitGuard_ExceptionLandingPad(dex_pc);
1813
1814 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
1815
1816 irb_.CreateBr(block_cont);
1817
1818 // Now the class object must be loaded
1819 irb_.SetInsertPoint(block_cont);
1820
1821 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1822
1823 phi->addIncoming(storage_object_addr, block_original);
1824 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
1825
1826 return phi;
1827}
1828
1829llvm::Value* GBCExpanderPass::Expand_HLSget(llvm::CallInst& call_inst,
1830 JType field_jty) {
1831 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1832 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1833
1834 int field_offset;
1835 int ssb_index;
1836 bool is_referrers_class;
1837 bool is_volatile;
1838
1839 bool is_fast_path = driver_->ComputeStaticFieldInfo(
1840 field_idx, dex_compilation_unit_, field_offset, ssb_index,
1841 is_referrers_class, is_volatile, false);
1842
1843 llvm::Value* static_field_value;
1844
1845 if (!is_fast_path) {
1846 llvm::Function* runtime_func;
1847
1848 if (field_jty == kObject) {
1849 runtime_func = irb_.GetRuntime(runtime_support::GetObjectStatic);
1850 } else if (field_jty == kLong || field_jty == kDouble) {
1851 runtime_func = irb_.GetRuntime(runtime_support::Get64Static);
1852 } else {
1853 runtime_func = irb_.GetRuntime(runtime_support::Get32Static);
1854 }
1855
1856 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1857
1858 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1859
1860 EmitUpdateDexPC(dex_pc);
1861
1862 static_field_value =
1863 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
1864
1865 EmitGuard_ExceptionLandingPad(dex_pc);
1866
1867 if (field_jty == kFloat || field_jty == kDouble) {
1868 static_field_value = irb_.CreateBitCast(static_field_value, irb_.getJType(field_jty));
1869 }
1870 } else {
1871 DCHECK_GE(field_offset, 0);
1872
1873 llvm::Value* static_storage_addr = NULL;
1874
1875 if (is_referrers_class) {
1876 // Fast path, static storage base is this method's class
1877 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1878
1879 static_storage_addr =
1880 irb_.LoadFromObjectOffset(method_object_addr,
1881 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
1882 irb_.getJObjectTy(),
1883 kTBAAConstJObject);
1884 } else {
1885 // Medium path, static storage base in a different class which
1886 // requires checks that the other class is initialized
1887 DCHECK_GE(ssb_index, 0);
1888 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1889 }
1890
1891 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1892
1893 llvm::Value* static_field_addr =
1894 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1895 irb_.getJType(field_jty)->getPointerTo());
1896
1897 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
1898 static_field_value = SignOrZeroExtendCat1Types(static_field_value, field_jty);
1899
1900 if (is_volatile) {
1901 irb_.CreateMemoryBarrier(art::kLoadLoad);
1902 }
1903 }
1904
1905 return static_field_value;
1906}
1907
1908void GBCExpanderPass::Expand_HLSput(llvm::CallInst& call_inst,
1909 JType field_jty) {
1910 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1911 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1912 llvm::Value* new_value = call_inst.getArgOperand(1);
1913
1914 if (field_jty == kFloat || field_jty == kDouble) {
1915 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty));
1916 }
1917
1918 int field_offset;
1919 int ssb_index;
1920 bool is_referrers_class;
1921 bool is_volatile;
1922
1923 bool is_fast_path = driver_->ComputeStaticFieldInfo(
1924 field_idx, dex_compilation_unit_, field_offset, ssb_index,
1925 is_referrers_class, is_volatile, true);
1926
1927 if (!is_fast_path) {
1928 llvm::Function* runtime_func;
1929
1930 if (field_jty == kObject) {
1931 runtime_func = irb_.GetRuntime(runtime_support::SetObjectStatic);
1932 } else if (field_jty == kLong || field_jty == kDouble) {
1933 runtime_func = irb_.GetRuntime(runtime_support::Set64Static);
1934 } else {
1935 runtime_func = irb_.GetRuntime(runtime_support::Set32Static);
1936 }
1937
1938 if (field_jty == kFloat) {
1939 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1940 } else if (field_jty == kDouble) {
1941 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1942 }
1943
1944 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1945
1946 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1947
1948 EmitUpdateDexPC(dex_pc);
1949
1950 irb_.CreateCall3(runtime_func, field_idx_value,
1951 method_object_addr, new_value);
1952
1953 EmitGuard_ExceptionLandingPad(dex_pc);
1954
1955 } else {
1956 DCHECK_GE(field_offset, 0);
1957
1958 llvm::Value* static_storage_addr = NULL;
1959
1960 if (is_referrers_class) {
1961 // Fast path, static storage base is this method's class
1962 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1963
1964 static_storage_addr =
1965 irb_.LoadFromObjectOffset(method_object_addr,
1966 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
1967 irb_.getJObjectTy(),
1968 kTBAAConstJObject);
1969 } else {
1970 // Medium path, static storage base in a different class which
1971 // requires checks that the other class is initialized
1972 DCHECK_GE(ssb_index, 0);
1973 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1974 }
1975
1976 if (is_volatile) {
1977 irb_.CreateMemoryBarrier(art::kStoreStore);
1978 }
1979
1980 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1981
1982 llvm::Value* static_field_addr =
1983 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1984 irb_.getJType(field_jty)->getPointerTo());
1985
1986 new_value = TruncateCat1Types(new_value, field_jty);
1987 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1988
1989 if (is_volatile) {
1990 irb_.CreateMemoryBarrier(art::kStoreLoad);
1991 }
1992
1993 if (field_jty == kObject) { // If put an object, mark the GC card table.
1994 EmitMarkGCCard(new_value, static_storage_addr);
1995 }
1996 }
1997
1998 return;
1999}
2000
2001llvm::Value* GBCExpanderPass::Expand_ConstString(llvm::CallInst& call_inst) {
2002 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2003 uint32_t string_idx = LV2UInt(call_inst.getArgOperand(0));
2004
2005 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
2006
2007 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
2008
2009 if (!driver_->CanAssumeStringIsPresentInDexCache(*dex_compilation_unit_->GetDexFile(),
2010 string_idx)) {
2011 llvm::BasicBlock* block_str_exist =
2012 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
2013
2014 llvm::BasicBlock* block_str_resolve =
2015 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
2016
2017 llvm::BasicBlock* block_cont =
2018 CreateBasicBlockWithDexPC(dex_pc, "str_cont");
2019
2020 // Test: Is the string resolved and in the dex cache?
2021 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
2022
2023 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
2024
2025 // String is resolved, go to next basic block.
2026 irb_.SetInsertPoint(block_str_exist);
2027 irb_.CreateBr(block_cont);
2028
2029 // String is not resolved yet, resolve it now.
2030 irb_.SetInsertPoint(block_str_resolve);
2031
2032 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::ResolveString);
2033
2034 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2035
2036 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
2037
2038 EmitUpdateDexPC(dex_pc);
2039
2040 llvm::Value* result = irb_.CreateCall2(runtime_func, method_object_addr,
2041 string_idx_value);
2042
2043 EmitGuard_ExceptionLandingPad(dex_pc);
2044
2045 irb_.CreateBr(block_cont);
2046
2047
2048 llvm::BasicBlock* block_pre_cont = irb_.GetInsertBlock();
2049
2050 irb_.SetInsertPoint(block_cont);
2051
2052 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2053
2054 phi->addIncoming(string_addr, block_str_exist);
2055 phi->addIncoming(result, block_pre_cont);
2056
2057 string_addr = phi;
2058 }
2059
2060 return string_addr;
2061}
2062
2063llvm::Value* GBCExpanderPass::Expand_ConstClass(llvm::CallInst& call_inst) {
2064 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2065 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2066
2067 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
2068
2069 return type_object_addr;
2070}
2071
2072void GBCExpanderPass::Expand_MonitorEnter(llvm::CallInst& call_inst) {
2073 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2074 llvm::Value* object_addr = call_inst.getArgOperand(1);
2075 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
2076
2077 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
2078
2079 EmitUpdateDexPC(dex_pc);
2080
2081 irb_.Runtime().EmitLockObject(object_addr);
2082
2083 return;
2084}
2085
2086void GBCExpanderPass::Expand_MonitorExit(llvm::CallInst& call_inst) {
2087 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2088 llvm::Value* object_addr = call_inst.getArgOperand(1);
2089 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
2090
2091 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
2092
2093 EmitUpdateDexPC(dex_pc);
2094
2095 irb_.Runtime().EmitUnlockObject(object_addr);
2096
2097 EmitGuard_ExceptionLandingPad(dex_pc);
2098
2099 return;
2100}
2101
2102void GBCExpanderPass::Expand_HLCheckCast(llvm::CallInst& call_inst) {
2103 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2104 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2105 llvm::Value* object_addr = call_inst.getArgOperand(1);
2106
2107 llvm::BasicBlock* block_test_class =
2108 CreateBasicBlockWithDexPC(dex_pc, "test_class");
2109
2110 llvm::BasicBlock* block_test_sub_class =
2111 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
2112
2113 llvm::BasicBlock* block_cont =
2114 CreateBasicBlockWithDexPC(dex_pc, "checkcast_cont");
2115
2116 // Test: Is the reference equal to null? Act as no-op when it is null.
2117 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2118
2119 irb_.CreateCondBr(equal_null, block_cont, block_test_class, kUnlikely);
2120
2121 // Test: Is the object instantiated from the given class?
2122 irb_.SetInsertPoint(block_test_class);
2123 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
2124 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
2125
2126 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2127
2128 llvm::Value* object_type_field_addr =
2129 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2130
2131 llvm::Value* object_type_object_addr =
2132 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2133
2134 llvm::Value* equal_class =
2135 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2136
2137 irb_.CreateCondBr(equal_class, block_cont, block_test_sub_class, kLikely);
2138
2139 // Test: Is the object instantiated from the subclass of the given class?
2140 irb_.SetInsertPoint(block_test_sub_class);
2141
2142 EmitUpdateDexPC(dex_pc);
2143
2144 irb_.CreateCall2(irb_.GetRuntime(runtime_support::CheckCast),
2145 type_object_addr, object_type_object_addr);
2146
2147 EmitGuard_ExceptionLandingPad(dex_pc);
2148
2149 irb_.CreateBr(block_cont);
2150
2151 irb_.SetInsertPoint(block_cont);
2152
2153 return;
2154}
2155
2156llvm::Value* GBCExpanderPass::Expand_InstanceOf(llvm::CallInst& call_inst) {
2157 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2158 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2159 llvm::Value* object_addr = call_inst.getArgOperand(1);
2160
2161 llvm::BasicBlock* block_nullp =
2162 CreateBasicBlockWithDexPC(dex_pc, "nullp");
2163
2164 llvm::BasicBlock* block_test_class =
2165 CreateBasicBlockWithDexPC(dex_pc, "test_class");
2166
2167 llvm::BasicBlock* block_class_equals =
2168 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
2169
2170 llvm::BasicBlock* block_test_sub_class =
2171 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
2172
2173 llvm::BasicBlock* block_cont =
2174 CreateBasicBlockWithDexPC(dex_pc, "instance_of_cont");
2175
2176 // Overview of the following code :
2177 // We check for null, if so, then false, otherwise check for class == . If so
2178 // then true, otherwise do callout slowpath.
2179 //
2180 // Test: Is the reference equal to null? Set 0 when it is null.
2181 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2182
2183 irb_.CreateCondBr(equal_null, block_nullp, block_test_class, kUnlikely);
2184
2185 irb_.SetInsertPoint(block_nullp);
2186 irb_.CreateBr(block_cont);
2187
2188 // Test: Is the object instantiated from the given class?
2189 irb_.SetInsertPoint(block_test_class);
2190 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
2191 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
2192
2193 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2194
2195 llvm::Value* object_type_field_addr =
2196 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2197
2198 llvm::Value* object_type_object_addr =
2199 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2200
2201 llvm::Value* equal_class =
2202 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2203
2204 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class, kLikely);
2205
2206 irb_.SetInsertPoint(block_class_equals);
2207 irb_.CreateBr(block_cont);
2208
2209 // Test: Is the object instantiated from the subclass of the given class?
2210 irb_.SetInsertPoint(block_test_sub_class);
2211 llvm::Value* result =
2212 irb_.CreateCall2(irb_.GetRuntime(runtime_support::IsAssignable),
2213 type_object_addr, object_type_object_addr);
2214 irb_.CreateBr(block_cont);
2215
2216 irb_.SetInsertPoint(block_cont);
2217
2218 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJIntTy(), 3);
2219
2220 phi->addIncoming(irb_.getJInt(0), block_nullp);
2221 phi->addIncoming(irb_.getJInt(1), block_class_equals);
2222 phi->addIncoming(result, block_test_sub_class);
2223
2224 return phi;
2225}
2226
2227llvm::Value* GBCExpanderPass::Expand_NewInstance(llvm::CallInst& call_inst) {
2228 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2229 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2230
2231 llvm::Function* runtime_func;
2232 if (driver_->CanAccessInstantiableTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
2233 *dex_compilation_unit_->GetDexFile(),
2234 type_idx)) {
2235 runtime_func = irb_.GetRuntime(runtime_support::AllocObject);
2236 } else {
2237 runtime_func = irb_.GetRuntime(runtime_support::AllocObjectWithAccessCheck);
2238 }
2239
2240 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2241
2242 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2243
2244 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2245
2246 EmitUpdateDexPC(dex_pc);
2247
2248 llvm::Value* object_addr =
2249 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
2250
2251 EmitGuard_ExceptionLandingPad(dex_pc);
2252
2253 return object_addr;
2254}
2255
2256llvm::Value* GBCExpanderPass::Expand_HLInvoke(llvm::CallInst& call_inst) {
2257 art::InvokeType invoke_type = static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
2258 bool is_static = (invoke_type == art::kStatic);
2259
2260 if (!is_static) {
2261 // Test: Is *this* parameter equal to null?
2262 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2263 llvm::Value* this_addr = call_inst.getArgOperand(3);
2264 int opt_flags = LV2UInt(call_inst.getArgOperand(2));
2265
2266 EmitGuard_NullPointerException(dex_pc, this_addr, opt_flags);
2267 }
2268
2269 llvm::Value* result = NULL;
2270 if (EmitIntrinsic(call_inst, &result)) {
2271 return result;
2272 }
2273
2274 return EmitInvoke(call_inst);
2275}
2276
2277llvm::Value* GBCExpanderPass::Expand_OptArrayLength(llvm::CallInst& call_inst) {
2278 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2279 // Get the array object address
2280 llvm::Value* array_addr = call_inst.getArgOperand(1);
2281 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
2282
2283 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
2284
2285 // Get the array length and store it to the register
2286 return EmitLoadArrayLength(array_addr);
2287}
2288
2289llvm::Value* GBCExpanderPass::Expand_NewArray(llvm::CallInst& call_inst) {
2290 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2291 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2292 llvm::Value* length = call_inst.getArgOperand(1);
2293
2294 return EmitAllocNewArray(dex_pc, length, type_idx, false);
2295}
2296
2297llvm::Value* GBCExpanderPass::Expand_HLFilledNewArray(llvm::CallInst& call_inst) {
2298 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2299 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(1));
2300 uint32_t length = call_inst.getNumArgOperands() - 3;
2301
2302 llvm::Value* object_addr =
2303 EmitAllocNewArray(dex_pc, irb_.getInt32(length), type_idx, true);
2304
2305 if (length > 0) {
2306 // Check for the element type
2307 uint32_t type_desc_len = 0;
2308 const char* type_desc =
2309 dex_compilation_unit_->GetDexFile()->StringByTypeIdx(type_idx, &type_desc_len);
2310
2311 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
2312 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
2313 bool is_elem_int_ty = (type_desc[1] == 'I');
2314
2315 uint32_t alignment;
2316 llvm::Constant* elem_size;
2317 llvm::PointerType* field_type;
2318
2319 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
2320 // as the element, thus we are only checking 2 cases: primitive int and
2321 // non-primitive type.
2322 if (is_elem_int_ty) {
2323 alignment = sizeof(int32_t);
2324 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
2325 field_type = irb_.getJIntTy()->getPointerTo();
2326 } else {
2327 alignment = irb_.getSizeOfPtrEquivInt();
2328 elem_size = irb_.getSizeOfPtrEquivIntValue();
2329 field_type = irb_.getJObjectTy()->getPointerTo();
2330 }
2331
2332 llvm::Value* data_field_offset =
2333 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
2334
2335 llvm::Value* data_field_addr =
2336 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
2337
2338 // TODO: Tune this code. Currently we are generating one instruction for
2339 // one element which may be very space consuming. Maybe changing to use
2340 // memcpy may help; however, since we can't guarantee that the alloca of
2341 // dalvik register are continuous, we can't perform such optimization yet.
2342 for (uint32_t i = 0; i < length; ++i) {
2343 llvm::Value* reg_value = call_inst.getArgOperand(i+3);
2344
2345 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
2346
2347 data_field_addr =
2348 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
2349 }
2350 }
2351
2352 return object_addr;
2353}
2354
2355void GBCExpanderPass::Expand_HLFillArrayData(llvm::CallInst& call_inst) {
2356 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2357 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
2358 LV2SInt(call_inst.getArgOperand(0));
2359 llvm::Value* array_addr = call_inst.getArgOperand(1);
2360
2361 const art::Instruction::ArrayDataPayload* payload =
2362 reinterpret_cast<const art::Instruction::ArrayDataPayload*>(
2363 dex_compilation_unit_->GetCodeItem()->insns_ + payload_offset);
2364
2365 if (payload->element_count == 0) {
2366 // When the number of the elements in the payload is zero, we don't have
2367 // to copy any numbers. However, we should check whether the array object
2368 // address is equal to null or not.
2369 EmitGuard_NullPointerException(dex_pc, array_addr, 0);
2370 } else {
2371 // To save the code size, we are going to call the runtime function to
2372 // copy the content from DexFile.
2373
2374 // NOTE: We will check for the NullPointerException in the runtime.
2375
2376 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::FillArrayData);
2377
2378 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2379
2380 EmitUpdateDexPC(dex_pc);
2381
2382 irb_.CreateCall4(runtime_func,
2383 method_object_addr, irb_.getInt32(dex_pc),
2384 array_addr, irb_.getInt32(payload_offset));
2385
2386 EmitGuard_ExceptionLandingPad(dex_pc);
2387 }
2388
2389 return;
2390}
2391
2392llvm::Value* GBCExpanderPass::EmitAllocNewArray(uint32_t dex_pc,
2393 llvm::Value* array_length_value,
2394 uint32_t type_idx,
2395 bool is_filled_new_array) {
2396 llvm::Function* runtime_func;
2397
2398 bool skip_access_check =
2399 driver_->CanAccessTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
2400 *dex_compilation_unit_->GetDexFile(), type_idx);
2401
2402
2403 if (is_filled_new_array) {
2404 runtime_func = skip_access_check ?
2405 irb_.GetRuntime(runtime_support::CheckAndAllocArray) :
2406 irb_.GetRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck);
2407 } else {
2408 runtime_func = skip_access_check ?
2409 irb_.GetRuntime(runtime_support::AllocArray) :
2410 irb_.GetRuntime(runtime_support::AllocArrayWithAccessCheck);
2411 }
2412
2413 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2414
2415 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2416
2417 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2418
2419 EmitUpdateDexPC(dex_pc);
2420
2421 llvm::Value* object_addr =
2422 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
2423 array_length_value, thread_object_addr);
2424
2425 EmitGuard_ExceptionLandingPad(dex_pc);
2426
2427 return object_addr;
2428}
2429
2430llvm::Value* GBCExpanderPass::
2431EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
2432 art::InvokeType invoke_type,
2433 llvm::Value* this_addr,
2434 uint32_t dex_pc,
2435 bool is_fast_path) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002436 llvm::Function* runtime_func = NULL;
2437
2438 switch (invoke_type) {
2439 case art::kStatic:
2440 runtime_func = irb_.GetRuntime(runtime_support::FindStaticMethodWithAccessCheck);
2441 break;
2442
2443 case art::kDirect:
2444 runtime_func = irb_.GetRuntime(runtime_support::FindDirectMethodWithAccessCheck);
2445 break;
2446
2447 case art::kVirtual:
2448 runtime_func = irb_.GetRuntime(runtime_support::FindVirtualMethodWithAccessCheck);
2449 break;
2450
2451 case art::kSuper:
2452 runtime_func = irb_.GetRuntime(runtime_support::FindSuperMethodWithAccessCheck);
2453 break;
2454
2455 case art::kInterface:
2456 if (is_fast_path) {
2457 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethod);
2458 } else {
2459 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethodWithAccessCheck);
2460 }
2461 break;
2462 }
2463
2464 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2465
2466 if (this_addr == NULL) {
2467 DCHECK_EQ(invoke_type, art::kStatic);
2468 this_addr = irb_.getJNull();
2469 }
2470
2471 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2472
2473 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2474
2475 EmitUpdateDexPC(dex_pc);
2476
2477 llvm::Value* callee_method_object_addr =
2478 irb_.CreateCall4(runtime_func,
2479 callee_method_idx_value,
2480 this_addr,
2481 caller_method_object_addr,
2482 thread_object_addr);
2483
2484 EmitGuard_ExceptionLandingPad(dex_pc);
2485
2486 return callee_method_object_addr;
2487}
2488
2489void GBCExpanderPass::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2490 // Using runtime support, let the target can override by InlineAssembly.
2491 irb_.Runtime().EmitMarkGCCard(value, target_addr);
2492}
2493
2494void GBCExpanderPass::EmitUpdateDexPC(uint32_t dex_pc) {
2495 if (shadow_frame_ == NULL) {
2496 return;
2497 }
2498 irb_.StoreToObjectOffset(shadow_frame_,
2499 art::ShadowFrame::DexPCOffset(),
2500 irb_.getInt32(dex_pc),
2501 kTBAAShadowFrame);
2502}
2503
2504void GBCExpanderPass::EmitGuard_DivZeroException(uint32_t dex_pc,
2505 llvm::Value* denominator,
2506 JType op_jty) {
2507 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2508
2509 llvm::Constant* zero = irb_.getJZero(op_jty);
2510
2511 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2512
2513 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2514
2515 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2516
2517 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
2518
2519 irb_.SetInsertPoint(block_exception);
2520 EmitUpdateDexPC(dex_pc);
2521 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowDivZeroException));
2522 EmitBranchExceptionLandingPad(dex_pc);
2523
2524 irb_.SetInsertPoint(block_continue);
2525}
2526
2527void GBCExpanderPass::EmitGuard_NullPointerException(uint32_t dex_pc,
2528 llvm::Value* object,
2529 int opt_flags) {
2530 bool ignore_null_check = ((opt_flags & MIR_IGNORE_NULL_CHECK) != 0);
2531 if (ignore_null_check) {
2532 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2533 if (lpad) {
2534 // There is at least one catch: create a "fake" conditional branch to
2535 // keep the exception edge to the catch block.
2536 landing_pad_phi_mapping_[lpad].push_back(
2537 std::make_pair(current_bb_->getUniquePredecessor(),
2538 irb_.GetInsertBlock()));
2539
2540 llvm::BasicBlock* block_continue =
2541 CreateBasicBlockWithDexPC(dex_pc, "cont");
2542
2543 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
2544
2545 irb_.SetInsertPoint(block_continue);
2546 }
2547 } else {
2548 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
2549
2550 llvm::BasicBlock* block_exception =
2551 CreateBasicBlockWithDexPC(dex_pc, "nullp");
2552
2553 llvm::BasicBlock* block_continue =
2554 CreateBasicBlockWithDexPC(dex_pc, "cont");
2555
2556 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
2557
2558 irb_.SetInsertPoint(block_exception);
2559 EmitUpdateDexPC(dex_pc);
2560 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowNullPointerException),
2561 irb_.getInt32(dex_pc));
2562 EmitBranchExceptionLandingPad(dex_pc);
2563
2564 irb_.SetInsertPoint(block_continue);
2565 }
2566}
2567
2568void
2569GBCExpanderPass::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2570 llvm::Value* array,
2571 llvm::Value* index,
2572 int opt_flags) {
2573 bool ignore_range_check = ((opt_flags & MIR_IGNORE_RANGE_CHECK) != 0);
2574 if (ignore_range_check) {
2575 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2576 if (lpad) {
2577 // There is at least one catch: create a "fake" conditional branch to
2578 // keep the exception edge to the catch block.
2579 landing_pad_phi_mapping_[lpad].push_back(
2580 std::make_pair(current_bb_->getUniquePredecessor(),
2581 irb_.GetInsertBlock()));
2582
2583 llvm::BasicBlock* block_continue =
2584 CreateBasicBlockWithDexPC(dex_pc, "cont");
2585
2586 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
2587
2588 irb_.SetInsertPoint(block_continue);
2589 }
2590 } else {
2591 llvm::Value* array_len = EmitLoadArrayLength(array);
2592
2593 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2594
2595 llvm::BasicBlock* block_exception =
2596 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2597
2598 llvm::BasicBlock* block_continue =
2599 CreateBasicBlockWithDexPC(dex_pc, "cont");
2600
2601 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
2602
2603 irb_.SetInsertPoint(block_exception);
2604
2605 EmitUpdateDexPC(dex_pc);
2606 irb_.CreateCall2(irb_.GetRuntime(runtime_support::ThrowIndexOutOfBounds), index, array_len);
2607 EmitBranchExceptionLandingPad(dex_pc);
2608
2609 irb_.SetInsertPoint(block_continue);
2610 }
2611}
2612
2613llvm::FunctionType* GBCExpanderPass::GetFunctionType(llvm::Type* ret_type, uint32_t method_idx,
2614 bool is_static) {
2615 // Get method signature
2616 art::DexFile::MethodId const& method_id =
2617 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx);
2618
2619 uint32_t shorty_size;
2620 const char* shorty = dex_compilation_unit_->GetDexFile()->GetMethodShorty(method_id, &shorty_size);
2621 CHECK_GE(shorty_size, 1u);
2622
2623 // Get argument type
2624 std::vector<llvm::Type*> args_type;
2625
2626 args_type.push_back(irb_.getJObjectTy()); // method object pointer
2627
2628 if (!is_static) {
2629 args_type.push_back(irb_.getJType('L')); // "this" object pointer
2630 }
2631
2632 for (uint32_t i = 1; i < shorty_size; ++i) {
2633 char shorty_type = art::RemapShorty(shorty[i]);
2634 args_type.push_back(irb_.getJType(shorty_type));
2635 }
2636
2637 return llvm::FunctionType::get(ret_type, args_type, false);
2638}
2639
2640
2641llvm::BasicBlock* GBCExpanderPass::
2642CreateBasicBlockWithDexPC(uint32_t dex_pc, const char* postfix) {
2643 std::string name;
2644
2645#if !defined(NDEBUG)
2646 art::StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
2647#endif
2648
2649 return llvm::BasicBlock::Create(context_, name, func_);
2650}
2651
2652llvm::BasicBlock* GBCExpanderPass::GetBasicBlock(uint32_t dex_pc) {
2653 DCHECK(dex_pc < dex_compilation_unit_->GetCodeItem()->insns_size_in_code_units_);
2654 CHECK(basic_blocks_[dex_pc] != NULL);
2655 return basic_blocks_[dex_pc];
2656}
2657
2658int32_t GBCExpanderPass::GetTryItemOffset(uint32_t dex_pc) {
2659 int32_t min = 0;
2660 int32_t max = dex_compilation_unit_->GetCodeItem()->tries_size_ - 1;
2661
2662 while (min <= max) {
2663 int32_t mid = min + (max - min) / 2;
2664
2665 const art::DexFile::TryItem* ti =
2666 art::DexFile::GetTryItems(*dex_compilation_unit_->GetCodeItem(), mid);
2667 uint32_t start = ti->start_addr_;
2668 uint32_t end = start + ti->insn_count_;
2669
2670 if (dex_pc < start) {
2671 max = mid - 1;
2672 } else if (dex_pc >= end) {
2673 min = mid + 1;
2674 } else {
2675 return mid; // found
2676 }
2677 }
2678
2679 return -1; // not found
2680}
2681
2682llvm::BasicBlock* GBCExpanderPass::GetLandingPadBasicBlock(uint32_t dex_pc) {
2683 // Find the try item for this address in this method
2684 int32_t ti_offset = GetTryItemOffset(dex_pc);
2685
2686 if (ti_offset == -1) {
2687 return NULL; // No landing pad is available for this address.
2688 }
2689
2690 // Check for the existing landing pad basic block
2691 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2692 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2693
2694 if (block_lpad) {
2695 // We have generated landing pad for this try item already. Return the
2696 // same basic block.
2697 return block_lpad;
2698 }
2699
2700 // Get try item from code item
2701 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*dex_compilation_unit_->GetCodeItem(),
2702 ti_offset);
2703
2704 std::string lpadname;
2705
2706#if !defined(NDEBUG)
2707 art::StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
2708#endif
2709
2710 // Create landing pad basic block
2711 block_lpad = llvm::BasicBlock::Create(context_, lpadname, func_);
2712
2713 // Change IRBuilder insert point
2714 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2715 irb_.SetInsertPoint(block_lpad);
2716
2717 // Find catch block with matching type
2718 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2719
2720 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
2721
2722 llvm::Value* catch_handler_index_value =
2723 irb_.CreateCall2(irb_.GetRuntime(runtime_support::FindCatchBlock),
2724 method_object_addr, ti_offset_value);
2725
2726 // Switch instruction (Go to unwind basic block by default)
2727 llvm::SwitchInst* sw =
2728 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2729
2730 // Cases with matched catch block
2731 art::CatchHandlerIterator iter(*dex_compilation_unit_->GetCodeItem(), ti->start_addr_);
2732
2733 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2734 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2735 }
2736
2737 // Restore the orignal insert point for IRBuilder
2738 irb_.restoreIP(irb_ip_original);
2739
2740 // Cache this landing pad
2741 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2742 basic_block_landing_pads_[ti_offset] = block_lpad;
2743
2744 return block_lpad;
2745}
2746
2747llvm::BasicBlock* GBCExpanderPass::GetUnwindBasicBlock() {
2748 // Check the existing unwinding baisc block block
2749 if (basic_block_unwind_ != NULL) {
2750 return basic_block_unwind_;
2751 }
2752
2753 // Create new basic block for unwinding
2754 basic_block_unwind_ =
2755 llvm::BasicBlock::Create(context_, "exception_unwind", func_);
2756
2757 // Change IRBuilder insert point
2758 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2759 irb_.SetInsertPoint(basic_block_unwind_);
2760
2761 // Pop the shadow frame
2762 Expand_PopShadowFrame();
2763
2764 // Emit the code to return default value (zero) for the given return type.
2765 char ret_shorty = dex_compilation_unit_->GetShorty()[0];
2766 ret_shorty = art::RemapShorty(ret_shorty);
2767 if (ret_shorty == 'V') {
2768 irb_.CreateRetVoid();
2769 } else {
2770 irb_.CreateRet(irb_.getJZero(ret_shorty));
2771 }
2772
2773 // Restore the orignal insert point for IRBuilder
2774 irb_.restoreIP(irb_ip_original);
2775
2776 return basic_block_unwind_;
2777}
2778
2779void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2780 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
2781 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
2782 irb_.GetInsertBlock()));
2783 irb_.CreateBr(lpad);
2784 } else {
2785 irb_.CreateBr(GetUnwindBasicBlock());
2786 }
2787}
2788
2789void GBCExpanderPass::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
2790 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
2791
2792 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2793
2794 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
2795 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
2796 irb_.GetInsertBlock()));
2797 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
2798 } else {
2799 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
2800 }
2801
2802 irb_.SetInsertPoint(block_cont);
2803}
2804
2805llvm::Value*
2806GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
2807 llvm::CallInst& call_inst) {
2808 switch (intr_id) {
2809 //==- Thread -----------------------------------------------------------==//
2810 case IntrinsicHelper::GetCurrentThread: {
2811 return irb_.Runtime().EmitGetCurrentThread();
2812 }
2813 case IntrinsicHelper::CheckSuspend: {
2814 Expand_TestSuspend(call_inst);
2815 return NULL;
2816 }
2817 case IntrinsicHelper::TestSuspend: {
2818 Expand_TestSuspend(call_inst);
2819 return NULL;
2820 }
2821 case IntrinsicHelper::MarkGCCard: {
2822 Expand_MarkGCCard(call_inst);
2823 return NULL;
2824 }
2825
2826 //==- Exception --------------------------------------------------------==//
2827 case IntrinsicHelper::ThrowException: {
2828 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
2829 }
2830 case IntrinsicHelper::HLThrowException: {
2831 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2832
2833 EmitUpdateDexPC(dex_pc);
2834
2835 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowException),
2836 call_inst.getArgOperand(0));
2837
2838 EmitGuard_ExceptionLandingPad(dex_pc);
2839 return NULL;
2840 }
2841 case IntrinsicHelper::GetException: {
2842 return irb_.Runtime().EmitGetAndClearException();
2843 }
2844 case IntrinsicHelper::IsExceptionPending: {
2845 return irb_.Runtime().EmitIsExceptionPending();
2846 }
2847 case IntrinsicHelper::FindCatchBlock: {
2848 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
2849 }
2850 case IntrinsicHelper::ThrowDivZeroException: {
2851 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
2852 }
2853 case IntrinsicHelper::ThrowNullPointerException: {
2854 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
2855 }
2856 case IntrinsicHelper::ThrowIndexOutOfBounds: {
2857 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
2858 }
2859
2860 //==- Const String -----------------------------------------------------==//
2861 case IntrinsicHelper::ConstString: {
2862 return Expand_ConstString(call_inst);
2863 }
2864 case IntrinsicHelper::LoadStringFromDexCache: {
2865 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
2866 }
2867 case IntrinsicHelper::ResolveString: {
2868 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
2869 }
2870
2871 //==- Const Class ------------------------------------------------------==//
2872 case IntrinsicHelper::ConstClass: {
2873 return Expand_ConstClass(call_inst);
2874 }
2875 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
2876 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
2877 }
2878 case IntrinsicHelper::LoadTypeFromDexCache: {
2879 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
2880 }
2881 case IntrinsicHelper::InitializeType: {
2882 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
2883 }
2884
2885 //==- Lock -------------------------------------------------------------==//
2886 case IntrinsicHelper::LockObject: {
2887 Expand_LockObject(call_inst.getArgOperand(0));
2888 return NULL;
2889 }
2890 case IntrinsicHelper::UnlockObject: {
2891 Expand_UnlockObject(call_inst.getArgOperand(0));
2892 return NULL;
2893 }
2894
2895 //==- Cast -------------------------------------------------------------==//
2896 case IntrinsicHelper::CheckCast: {
2897 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
2898 }
2899 case IntrinsicHelper::HLCheckCast: {
2900 Expand_HLCheckCast(call_inst);
2901 return NULL;
2902 }
2903 case IntrinsicHelper::IsAssignable: {
2904 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
2905 }
2906
2907 //==- Alloc ------------------------------------------------------------==//
2908 case IntrinsicHelper::AllocObject: {
2909 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
2910 }
2911 case IntrinsicHelper::AllocObjectWithAccessCheck: {
2912 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
2913 }
2914
2915 //==- Instance ---------------------------------------------------------==//
2916 case IntrinsicHelper::NewInstance: {
2917 return Expand_NewInstance(call_inst);
2918 }
2919 case IntrinsicHelper::InstanceOf: {
2920 return Expand_InstanceOf(call_inst);
2921 }
2922
2923 //==- Array ------------------------------------------------------------==//
2924 case IntrinsicHelper::NewArray: {
2925 return Expand_NewArray(call_inst);
2926 }
2927 case IntrinsicHelper::OptArrayLength: {
2928 return Expand_OptArrayLength(call_inst);
2929 }
2930 case IntrinsicHelper::ArrayLength: {
2931 return EmitLoadArrayLength(call_inst.getArgOperand(0));
2932 }
2933 case IntrinsicHelper::AllocArray: {
2934 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
2935 }
2936 case IntrinsicHelper::AllocArrayWithAccessCheck: {
2937 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
2938 call_inst);
2939 }
2940 case IntrinsicHelper::CheckAndAllocArray: {
2941 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
2942 }
2943 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
2944 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
2945 call_inst);
2946 }
2947 case IntrinsicHelper::ArrayGet: {
2948 return Expand_ArrayGet(call_inst.getArgOperand(0),
2949 call_inst.getArgOperand(1),
2950 kInt);
2951 }
2952 case IntrinsicHelper::ArrayGetWide: {
2953 return Expand_ArrayGet(call_inst.getArgOperand(0),
2954 call_inst.getArgOperand(1),
2955 kLong);
2956 }
2957 case IntrinsicHelper::ArrayGetObject: {
2958 return Expand_ArrayGet(call_inst.getArgOperand(0),
2959 call_inst.getArgOperand(1),
2960 kObject);
2961 }
2962 case IntrinsicHelper::ArrayGetBoolean: {
2963 return Expand_ArrayGet(call_inst.getArgOperand(0),
2964 call_inst.getArgOperand(1),
2965 kBoolean);
2966 }
2967 case IntrinsicHelper::ArrayGetByte: {
2968 return Expand_ArrayGet(call_inst.getArgOperand(0),
2969 call_inst.getArgOperand(1),
2970 kByte);
2971 }
2972 case IntrinsicHelper::ArrayGetChar: {
2973 return Expand_ArrayGet(call_inst.getArgOperand(0),
2974 call_inst.getArgOperand(1),
2975 kChar);
2976 }
2977 case IntrinsicHelper::ArrayGetShort: {
2978 return Expand_ArrayGet(call_inst.getArgOperand(0),
2979 call_inst.getArgOperand(1),
2980 kShort);
2981 }
2982 case IntrinsicHelper::ArrayPut: {
2983 Expand_ArrayPut(call_inst.getArgOperand(0),
2984 call_inst.getArgOperand(1),
2985 call_inst.getArgOperand(2),
2986 kInt);
2987 return NULL;
2988 }
2989 case IntrinsicHelper::ArrayPutWide: {
2990 Expand_ArrayPut(call_inst.getArgOperand(0),
2991 call_inst.getArgOperand(1),
2992 call_inst.getArgOperand(2),
2993 kLong);
2994 return NULL;
2995 }
2996 case IntrinsicHelper::ArrayPutObject: {
2997 Expand_ArrayPut(call_inst.getArgOperand(0),
2998 call_inst.getArgOperand(1),
2999 call_inst.getArgOperand(2),
3000 kObject);
3001 return NULL;
3002 }
3003 case IntrinsicHelper::ArrayPutBoolean: {
3004 Expand_ArrayPut(call_inst.getArgOperand(0),
3005 call_inst.getArgOperand(1),
3006 call_inst.getArgOperand(2),
3007 kBoolean);
3008 return NULL;
3009 }
3010 case IntrinsicHelper::ArrayPutByte: {
3011 Expand_ArrayPut(call_inst.getArgOperand(0),
3012 call_inst.getArgOperand(1),
3013 call_inst.getArgOperand(2),
3014 kByte);
3015 return NULL;
3016 }
3017 case IntrinsicHelper::ArrayPutChar: {
3018 Expand_ArrayPut(call_inst.getArgOperand(0),
3019 call_inst.getArgOperand(1),
3020 call_inst.getArgOperand(2),
3021 kChar);
3022 return NULL;
3023 }
3024 case IntrinsicHelper::ArrayPutShort: {
3025 Expand_ArrayPut(call_inst.getArgOperand(0),
3026 call_inst.getArgOperand(1),
3027 call_inst.getArgOperand(2),
3028 kShort);
3029 return NULL;
3030 }
3031 case IntrinsicHelper::CheckPutArrayElement: {
3032 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
3033 }
3034 case IntrinsicHelper::FilledNewArray: {
3035 Expand_FilledNewArray(call_inst);
3036 return NULL;
3037 }
3038 case IntrinsicHelper::FillArrayData: {
3039 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
3040 }
3041 case IntrinsicHelper::HLFillArrayData: {
3042 Expand_HLFillArrayData(call_inst);
3043 return NULL;
3044 }
3045 case IntrinsicHelper::HLFilledNewArray: {
3046 return Expand_HLFilledNewArray(call_inst);
3047 }
3048
3049 //==- Instance Field ---------------------------------------------------==//
3050 case IntrinsicHelper::InstanceFieldGet:
3051 case IntrinsicHelper::InstanceFieldGetBoolean:
3052 case IntrinsicHelper::InstanceFieldGetByte:
3053 case IntrinsicHelper::InstanceFieldGetChar:
3054 case IntrinsicHelper::InstanceFieldGetShort: {
3055 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
3056 }
3057 case IntrinsicHelper::InstanceFieldGetWide: {
3058 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
3059 }
3060 case IntrinsicHelper::InstanceFieldGetObject: {
3061 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
3062 }
3063 case IntrinsicHelper::InstanceFieldGetFast: {
3064 return Expand_IGetFast(call_inst.getArgOperand(0),
3065 call_inst.getArgOperand(1),
3066 call_inst.getArgOperand(2),
3067 kInt);
3068 }
3069 case IntrinsicHelper::InstanceFieldGetWideFast: {
3070 return Expand_IGetFast(call_inst.getArgOperand(0),
3071 call_inst.getArgOperand(1),
3072 call_inst.getArgOperand(2),
3073 kLong);
3074 }
3075 case IntrinsicHelper::InstanceFieldGetObjectFast: {
3076 return Expand_IGetFast(call_inst.getArgOperand(0),
3077 call_inst.getArgOperand(1),
3078 call_inst.getArgOperand(2),
3079 kObject);
3080 }
3081 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
3082 return Expand_IGetFast(call_inst.getArgOperand(0),
3083 call_inst.getArgOperand(1),
3084 call_inst.getArgOperand(2),
3085 kBoolean);
3086 }
3087 case IntrinsicHelper::InstanceFieldGetByteFast: {
3088 return Expand_IGetFast(call_inst.getArgOperand(0),
3089 call_inst.getArgOperand(1),
3090 call_inst.getArgOperand(2),
3091 kByte);
3092 }
3093 case IntrinsicHelper::InstanceFieldGetCharFast: {
3094 return Expand_IGetFast(call_inst.getArgOperand(0),
3095 call_inst.getArgOperand(1),
3096 call_inst.getArgOperand(2),
3097 kChar);
3098 }
3099 case IntrinsicHelper::InstanceFieldGetShortFast: {
3100 return Expand_IGetFast(call_inst.getArgOperand(0),
3101 call_inst.getArgOperand(1),
3102 call_inst.getArgOperand(2),
3103 kShort);
3104 }
3105 case IntrinsicHelper::InstanceFieldPut:
3106 case IntrinsicHelper::InstanceFieldPutBoolean:
3107 case IntrinsicHelper::InstanceFieldPutByte:
3108 case IntrinsicHelper::InstanceFieldPutChar:
3109 case IntrinsicHelper::InstanceFieldPutShort: {
3110 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
3111 }
3112 case IntrinsicHelper::InstanceFieldPutWide: {
3113 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
3114 }
3115 case IntrinsicHelper::InstanceFieldPutObject: {
3116 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
3117 }
3118 case IntrinsicHelper::InstanceFieldPutFast: {
3119 Expand_IPutFast(call_inst.getArgOperand(0),
3120 call_inst.getArgOperand(1),
3121 call_inst.getArgOperand(2),
3122 call_inst.getArgOperand(3),
3123 kInt);
3124 return NULL;
3125 }
3126 case IntrinsicHelper::InstanceFieldPutWideFast: {
3127 Expand_IPutFast(call_inst.getArgOperand(0),
3128 call_inst.getArgOperand(1),
3129 call_inst.getArgOperand(2),
3130 call_inst.getArgOperand(3),
3131 kLong);
3132 return NULL;
3133 }
3134 case IntrinsicHelper::InstanceFieldPutObjectFast: {
3135 Expand_IPutFast(call_inst.getArgOperand(0),
3136 call_inst.getArgOperand(1),
3137 call_inst.getArgOperand(2),
3138 call_inst.getArgOperand(3),
3139 kObject);
3140 return NULL;
3141 }
3142 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
3143 Expand_IPutFast(call_inst.getArgOperand(0),
3144 call_inst.getArgOperand(1),
3145 call_inst.getArgOperand(2),
3146 call_inst.getArgOperand(3),
3147 kBoolean);
3148 return NULL;
3149 }
3150 case IntrinsicHelper::InstanceFieldPutByteFast: {
3151 Expand_IPutFast(call_inst.getArgOperand(0),
3152 call_inst.getArgOperand(1),
3153 call_inst.getArgOperand(2),
3154 call_inst.getArgOperand(3),
3155 kByte);
3156 return NULL;
3157 }
3158 case IntrinsicHelper::InstanceFieldPutCharFast: {
3159 Expand_IPutFast(call_inst.getArgOperand(0),
3160 call_inst.getArgOperand(1),
3161 call_inst.getArgOperand(2),
3162 call_inst.getArgOperand(3),
3163 kChar);
3164 return NULL;
3165 }
3166 case IntrinsicHelper::InstanceFieldPutShortFast: {
3167 Expand_IPutFast(call_inst.getArgOperand(0),
3168 call_inst.getArgOperand(1),
3169 call_inst.getArgOperand(2),
3170 call_inst.getArgOperand(3),
3171 kShort);
3172 return NULL;
3173 }
3174
3175 //==- Static Field -----------------------------------------------------==//
3176 case IntrinsicHelper::StaticFieldGet:
3177 case IntrinsicHelper::StaticFieldGetBoolean:
3178 case IntrinsicHelper::StaticFieldGetByte:
3179 case IntrinsicHelper::StaticFieldGetChar:
3180 case IntrinsicHelper::StaticFieldGetShort: {
3181 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
3182 }
3183 case IntrinsicHelper::StaticFieldGetWide: {
3184 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
3185 }
3186 case IntrinsicHelper::StaticFieldGetObject: {
3187 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
3188 }
3189 case IntrinsicHelper::StaticFieldGetFast: {
3190 return Expand_SGetFast(call_inst.getArgOperand(0),
3191 call_inst.getArgOperand(1),
3192 call_inst.getArgOperand(2),
3193 kInt);
3194 }
3195 case IntrinsicHelper::StaticFieldGetWideFast: {
3196 return Expand_SGetFast(call_inst.getArgOperand(0),
3197 call_inst.getArgOperand(1),
3198 call_inst.getArgOperand(2),
3199 kLong);
3200 }
3201 case IntrinsicHelper::StaticFieldGetObjectFast: {
3202 return Expand_SGetFast(call_inst.getArgOperand(0),
3203 call_inst.getArgOperand(1),
3204 call_inst.getArgOperand(2),
3205 kObject);
3206 }
3207 case IntrinsicHelper::StaticFieldGetBooleanFast: {
3208 return Expand_SGetFast(call_inst.getArgOperand(0),
3209 call_inst.getArgOperand(1),
3210 call_inst.getArgOperand(2),
3211 kBoolean);
3212 }
3213 case IntrinsicHelper::StaticFieldGetByteFast: {
3214 return Expand_SGetFast(call_inst.getArgOperand(0),
3215 call_inst.getArgOperand(1),
3216 call_inst.getArgOperand(2),
3217 kByte);
3218 }
3219 case IntrinsicHelper::StaticFieldGetCharFast: {
3220 return Expand_SGetFast(call_inst.getArgOperand(0),
3221 call_inst.getArgOperand(1),
3222 call_inst.getArgOperand(2),
3223 kChar);
3224 }
3225 case IntrinsicHelper::StaticFieldGetShortFast: {
3226 return Expand_SGetFast(call_inst.getArgOperand(0),
3227 call_inst.getArgOperand(1),
3228 call_inst.getArgOperand(2),
3229 kShort);
3230 }
3231 case IntrinsicHelper::StaticFieldPut:
3232 case IntrinsicHelper::StaticFieldPutBoolean:
3233 case IntrinsicHelper::StaticFieldPutByte:
3234 case IntrinsicHelper::StaticFieldPutChar:
3235 case IntrinsicHelper::StaticFieldPutShort: {
3236 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
3237 }
3238 case IntrinsicHelper::StaticFieldPutWide: {
3239 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
3240 }
3241 case IntrinsicHelper::StaticFieldPutObject: {
3242 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
3243 }
3244 case IntrinsicHelper::StaticFieldPutFast: {
3245 Expand_SPutFast(call_inst.getArgOperand(0),
3246 call_inst.getArgOperand(1),
3247 call_inst.getArgOperand(2),
3248 call_inst.getArgOperand(3),
3249 kInt);
3250 return NULL;
3251 }
3252 case IntrinsicHelper::StaticFieldPutWideFast: {
3253 Expand_SPutFast(call_inst.getArgOperand(0),
3254 call_inst.getArgOperand(1),
3255 call_inst.getArgOperand(2),
3256 call_inst.getArgOperand(3),
3257 kLong);
3258 return NULL;
3259 }
3260 case IntrinsicHelper::StaticFieldPutObjectFast: {
3261 Expand_SPutFast(call_inst.getArgOperand(0),
3262 call_inst.getArgOperand(1),
3263 call_inst.getArgOperand(2),
3264 call_inst.getArgOperand(3),
3265 kObject);
3266 return NULL;
3267 }
3268 case IntrinsicHelper::StaticFieldPutBooleanFast: {
3269 Expand_SPutFast(call_inst.getArgOperand(0),
3270 call_inst.getArgOperand(1),
3271 call_inst.getArgOperand(2),
3272 call_inst.getArgOperand(3),
3273 kBoolean);
3274 return NULL;
3275 }
3276 case IntrinsicHelper::StaticFieldPutByteFast: {
3277 Expand_SPutFast(call_inst.getArgOperand(0),
3278 call_inst.getArgOperand(1),
3279 call_inst.getArgOperand(2),
3280 call_inst.getArgOperand(3),
3281 kByte);
3282 return NULL;
3283 }
3284 case IntrinsicHelper::StaticFieldPutCharFast: {
3285 Expand_SPutFast(call_inst.getArgOperand(0),
3286 call_inst.getArgOperand(1),
3287 call_inst.getArgOperand(2),
3288 call_inst.getArgOperand(3),
3289 kChar);
3290 return NULL;
3291 }
3292 case IntrinsicHelper::StaticFieldPutShortFast: {
3293 Expand_SPutFast(call_inst.getArgOperand(0),
3294 call_inst.getArgOperand(1),
3295 call_inst.getArgOperand(2),
3296 call_inst.getArgOperand(3),
3297 kShort);
3298 return NULL;
3299 }
3300 case IntrinsicHelper::LoadDeclaringClassSSB: {
3301 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
3302 }
3303 case IntrinsicHelper::LoadClassSSBFromDexCache: {
3304 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
3305 }
3306 case IntrinsicHelper::InitializeAndLoadClassSSB: {
3307 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
3308 }
3309
3310 //==- High-level Array -------------------------------------------------==//
3311 case IntrinsicHelper::HLArrayGet: {
3312 return Expand_HLArrayGet(call_inst, kInt);
3313 }
3314 case IntrinsicHelper::HLArrayGetBoolean: {
3315 return Expand_HLArrayGet(call_inst, kBoolean);
3316 }
3317 case IntrinsicHelper::HLArrayGetByte: {
3318 return Expand_HLArrayGet(call_inst, kByte);
3319 }
3320 case IntrinsicHelper::HLArrayGetChar: {
3321 return Expand_HLArrayGet(call_inst, kChar);
3322 }
3323 case IntrinsicHelper::HLArrayGetShort: {
3324 return Expand_HLArrayGet(call_inst, kShort);
3325 }
3326 case IntrinsicHelper::HLArrayGetFloat: {
3327 return Expand_HLArrayGet(call_inst, kFloat);
3328 }
3329 case IntrinsicHelper::HLArrayGetWide: {
3330 return Expand_HLArrayGet(call_inst, kLong);
3331 }
3332 case IntrinsicHelper::HLArrayGetDouble: {
3333 return Expand_HLArrayGet(call_inst, kDouble);
3334 }
3335 case IntrinsicHelper::HLArrayGetObject: {
3336 return Expand_HLArrayGet(call_inst, kObject);
3337 }
3338 case IntrinsicHelper::HLArrayPut: {
3339 Expand_HLArrayPut(call_inst, kInt);
3340 return NULL;
3341 }
3342 case IntrinsicHelper::HLArrayPutBoolean: {
3343 Expand_HLArrayPut(call_inst, kBoolean);
3344 return NULL;
3345 }
3346 case IntrinsicHelper::HLArrayPutByte: {
3347 Expand_HLArrayPut(call_inst, kByte);
3348 return NULL;
3349 }
3350 case IntrinsicHelper::HLArrayPutChar: {
3351 Expand_HLArrayPut(call_inst, kChar);
3352 return NULL;
3353 }
3354 case IntrinsicHelper::HLArrayPutShort: {
3355 Expand_HLArrayPut(call_inst, kShort);
3356 return NULL;
3357 }
3358 case IntrinsicHelper::HLArrayPutFloat: {
3359 Expand_HLArrayPut(call_inst, kFloat);
3360 return NULL;
3361 }
3362 case IntrinsicHelper::HLArrayPutWide: {
3363 Expand_HLArrayPut(call_inst, kLong);
3364 return NULL;
3365 }
3366 case IntrinsicHelper::HLArrayPutDouble: {
3367 Expand_HLArrayPut(call_inst, kDouble);
3368 return NULL;
3369 }
3370 case IntrinsicHelper::HLArrayPutObject: {
3371 Expand_HLArrayPut(call_inst, kObject);
3372 return NULL;
3373 }
3374
3375 //==- High-level Instance ----------------------------------------------==//
3376 case IntrinsicHelper::HLIGet: {
3377 return Expand_HLIGet(call_inst, kInt);
3378 }
3379 case IntrinsicHelper::HLIGetBoolean: {
3380 return Expand_HLIGet(call_inst, kBoolean);
3381 }
3382 case IntrinsicHelper::HLIGetByte: {
3383 return Expand_HLIGet(call_inst, kByte);
3384 }
3385 case IntrinsicHelper::HLIGetChar: {
3386 return Expand_HLIGet(call_inst, kChar);
3387 }
3388 case IntrinsicHelper::HLIGetShort: {
3389 return Expand_HLIGet(call_inst, kShort);
3390 }
3391 case IntrinsicHelper::HLIGetFloat: {
3392 return Expand_HLIGet(call_inst, kFloat);
3393 }
3394 case IntrinsicHelper::HLIGetWide: {
3395 return Expand_HLIGet(call_inst, kLong);
3396 }
3397 case IntrinsicHelper::HLIGetDouble: {
3398 return Expand_HLIGet(call_inst, kDouble);
3399 }
3400 case IntrinsicHelper::HLIGetObject: {
3401 return Expand_HLIGet(call_inst, kObject);
3402 }
3403 case IntrinsicHelper::HLIPut: {
3404 Expand_HLIPut(call_inst, kInt);
3405 return NULL;
3406 }
3407 case IntrinsicHelper::HLIPutBoolean: {
3408 Expand_HLIPut(call_inst, kBoolean);
3409 return NULL;
3410 }
3411 case IntrinsicHelper::HLIPutByte: {
3412 Expand_HLIPut(call_inst, kByte);
3413 return NULL;
3414 }
3415 case IntrinsicHelper::HLIPutChar: {
3416 Expand_HLIPut(call_inst, kChar);
3417 return NULL;
3418 }
3419 case IntrinsicHelper::HLIPutShort: {
3420 Expand_HLIPut(call_inst, kShort);
3421 return NULL;
3422 }
3423 case IntrinsicHelper::HLIPutFloat: {
3424 Expand_HLIPut(call_inst, kFloat);
3425 return NULL;
3426 }
3427 case IntrinsicHelper::HLIPutWide: {
3428 Expand_HLIPut(call_inst, kLong);
3429 return NULL;
3430 }
3431 case IntrinsicHelper::HLIPutDouble: {
3432 Expand_HLIPut(call_inst, kDouble);
3433 return NULL;
3434 }
3435 case IntrinsicHelper::HLIPutObject: {
3436 Expand_HLIPut(call_inst, kObject);
3437 return NULL;
3438 }
3439
3440 //==- High-level Invoke ------------------------------------------------==//
3441 case IntrinsicHelper::HLInvokeVoid:
3442 case IntrinsicHelper::HLInvokeObj:
3443 case IntrinsicHelper::HLInvokeInt:
3444 case IntrinsicHelper::HLInvokeFloat:
3445 case IntrinsicHelper::HLInvokeLong:
3446 case IntrinsicHelper::HLInvokeDouble: {
3447 return Expand_HLInvoke(call_inst);
3448 }
3449
3450 //==- Invoke -----------------------------------------------------------==//
3451 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
3452 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
3453 }
3454 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
3455 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
3456 }
3457 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
3458 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
3459 }
3460 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
3461 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
3462 }
3463 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
3464 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
3465 }
3466 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
3467 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
3468 }
3469 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
3470 return Expand_GetVirtualCalleeMethodObjAddrFast(
3471 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
3472 }
3473 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
3474 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
3475 }
3476 case IntrinsicHelper::InvokeRetVoid:
3477 case IntrinsicHelper::InvokeRetBoolean:
3478 case IntrinsicHelper::InvokeRetByte:
3479 case IntrinsicHelper::InvokeRetChar:
3480 case IntrinsicHelper::InvokeRetShort:
3481 case IntrinsicHelper::InvokeRetInt:
3482 case IntrinsicHelper::InvokeRetLong:
3483 case IntrinsicHelper::InvokeRetFloat:
3484 case IntrinsicHelper::InvokeRetDouble:
3485 case IntrinsicHelper::InvokeRetObject: {
3486 return Expand_Invoke(call_inst);
3487 }
3488
3489 //==- Math -------------------------------------------------------------==//
3490 case IntrinsicHelper::DivInt: {
3491 return Expand_DivRem(call_inst, /* is_div */true, kInt);
3492 }
3493 case IntrinsicHelper::RemInt: {
3494 return Expand_DivRem(call_inst, /* is_div */false, kInt);
3495 }
3496 case IntrinsicHelper::DivLong: {
3497 return Expand_DivRem(call_inst, /* is_div */true, kLong);
3498 }
3499 case IntrinsicHelper::RemLong: {
3500 return Expand_DivRem(call_inst, /* is_div */false, kLong);
3501 }
3502 case IntrinsicHelper::D2L: {
3503 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
3504 }
3505 case IntrinsicHelper::D2I: {
3506 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
3507 }
3508 case IntrinsicHelper::F2L: {
3509 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
3510 }
3511 case IntrinsicHelper::F2I: {
3512 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
3513 }
3514
3515 //==- High-level Static ------------------------------------------------==//
3516 case IntrinsicHelper::HLSget: {
3517 return Expand_HLSget(call_inst, kInt);
3518 }
3519 case IntrinsicHelper::HLSgetBoolean: {
3520 return Expand_HLSget(call_inst, kBoolean);
3521 }
3522 case IntrinsicHelper::HLSgetByte: {
3523 return Expand_HLSget(call_inst, kByte);
3524 }
3525 case IntrinsicHelper::HLSgetChar: {
3526 return Expand_HLSget(call_inst, kChar);
3527 }
3528 case IntrinsicHelper::HLSgetShort: {
3529 return Expand_HLSget(call_inst, kShort);
3530 }
3531 case IntrinsicHelper::HLSgetFloat: {
3532 return Expand_HLSget(call_inst, kFloat);
3533 }
3534 case IntrinsicHelper::HLSgetWide: {
3535 return Expand_HLSget(call_inst, kLong);
3536 }
3537 case IntrinsicHelper::HLSgetDouble: {
3538 return Expand_HLSget(call_inst, kDouble);
3539 }
3540 case IntrinsicHelper::HLSgetObject: {
3541 return Expand_HLSget(call_inst, kObject);
3542 }
3543 case IntrinsicHelper::HLSput: {
3544 Expand_HLSput(call_inst, kInt);
3545 return NULL;
3546 }
3547 case IntrinsicHelper::HLSputBoolean: {
3548 Expand_HLSput(call_inst, kBoolean);
3549 return NULL;
3550 }
3551 case IntrinsicHelper::HLSputByte: {
3552 Expand_HLSput(call_inst, kByte);
3553 return NULL;
3554 }
3555 case IntrinsicHelper::HLSputChar: {
3556 Expand_HLSput(call_inst, kChar);
3557 return NULL;
3558 }
3559 case IntrinsicHelper::HLSputShort: {
3560 Expand_HLSput(call_inst, kShort);
3561 return NULL;
3562 }
3563 case IntrinsicHelper::HLSputFloat: {
3564 Expand_HLSput(call_inst, kFloat);
3565 return NULL;
3566 }
3567 case IntrinsicHelper::HLSputWide: {
3568 Expand_HLSput(call_inst, kLong);
3569 return NULL;
3570 }
3571 case IntrinsicHelper::HLSputDouble: {
3572 Expand_HLSput(call_inst, kDouble);
3573 return NULL;
3574 }
3575 case IntrinsicHelper::HLSputObject: {
3576 Expand_HLSput(call_inst, kObject);
3577 return NULL;
3578 }
3579
3580 //==- High-level Monitor -----------------------------------------------==//
3581 case IntrinsicHelper::MonitorEnter: {
3582 Expand_MonitorEnter(call_inst);
3583 return NULL;
3584 }
3585 case IntrinsicHelper::MonitorExit: {
3586 Expand_MonitorExit(call_inst);
3587 return NULL;
3588 }
3589
3590 //==- Shadow Frame -----------------------------------------------------==//
3591 case IntrinsicHelper::AllocaShadowFrame: {
3592 Expand_AllocaShadowFrame(call_inst.getArgOperand(0));
3593 return NULL;
3594 }
3595 case IntrinsicHelper::SetVReg: {
3596 Expand_SetVReg(call_inst.getArgOperand(0),
3597 call_inst.getArgOperand(1));
3598 return NULL;
3599 }
3600 case IntrinsicHelper::PopShadowFrame: {
3601 Expand_PopShadowFrame();
3602 return NULL;
3603 }
3604 case IntrinsicHelper::UpdateDexPC: {
3605 Expand_UpdateDexPC(call_inst.getArgOperand(0));
3606 return NULL;
3607 }
3608
3609 //==- Comparison -------------------------------------------------------==//
3610 case IntrinsicHelper::CmplFloat:
3611 case IntrinsicHelper::CmplDouble: {
3612 return Expand_FPCompare(call_inst.getArgOperand(0),
3613 call_inst.getArgOperand(1),
3614 false);
3615 }
3616 case IntrinsicHelper::CmpgFloat:
3617 case IntrinsicHelper::CmpgDouble: {
3618 return Expand_FPCompare(call_inst.getArgOperand(0),
3619 call_inst.getArgOperand(1),
3620 true);
3621 }
3622 case IntrinsicHelper::CmpLong: {
3623 return Expand_LongCompare(call_inst.getArgOperand(0),
3624 call_inst.getArgOperand(1));
3625 }
3626
3627 //==- Const ------------------------------------------------------------==//
3628 case IntrinsicHelper::ConstInt:
3629 case IntrinsicHelper::ConstLong: {
3630 return call_inst.getArgOperand(0);
3631 }
3632 case IntrinsicHelper::ConstFloat: {
3633 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3634 irb_.getJFloatTy());
3635 }
3636 case IntrinsicHelper::ConstDouble: {
3637 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3638 irb_.getJDoubleTy());
3639 }
3640 case IntrinsicHelper::ConstObj: {
3641 CHECK(LV2UInt(call_inst.getArgOperand(0)) == 0);
3642 return irb_.getJNull();
3643 }
3644
3645 //==- Method Info ------------------------------------------------------==//
3646 case IntrinsicHelper::MethodInfo: {
3647 // Nothing to be done, because MethodInfo carries optional hints that are
3648 // not needed by the portable path.
3649 return NULL;
3650 }
3651
3652 //==- Copy -------------------------------------------------------------==//
3653 case IntrinsicHelper::CopyInt:
3654 case IntrinsicHelper::CopyFloat:
3655 case IntrinsicHelper::CopyLong:
3656 case IntrinsicHelper::CopyDouble:
3657 case IntrinsicHelper::CopyObj: {
3658 return call_inst.getArgOperand(0);
3659 }
3660
3661 //==- Shift ------------------------------------------------------------==//
3662 case IntrinsicHelper::SHLLong: {
3663 return Expand_IntegerShift(call_inst.getArgOperand(0),
3664 call_inst.getArgOperand(1),
3665 kIntegerSHL, kLong);
3666 }
3667 case IntrinsicHelper::SHRLong: {
3668 return Expand_IntegerShift(call_inst.getArgOperand(0),
3669 call_inst.getArgOperand(1),
3670 kIntegerSHR, kLong);
3671 }
3672 case IntrinsicHelper::USHRLong: {
3673 return Expand_IntegerShift(call_inst.getArgOperand(0),
3674 call_inst.getArgOperand(1),
3675 kIntegerUSHR, kLong);
3676 }
3677 case IntrinsicHelper::SHLInt: {
3678 return Expand_IntegerShift(call_inst.getArgOperand(0),
3679 call_inst.getArgOperand(1),
3680 kIntegerSHL, kInt);
3681 }
3682 case IntrinsicHelper::SHRInt: {
3683 return Expand_IntegerShift(call_inst.getArgOperand(0),
3684 call_inst.getArgOperand(1),
3685 kIntegerSHR, kInt);
3686 }
3687 case IntrinsicHelper::USHRInt: {
3688 return Expand_IntegerShift(call_inst.getArgOperand(0),
3689 call_inst.getArgOperand(1),
3690 kIntegerUSHR, kInt);
3691 }
3692
3693 //==- Conversion -------------------------------------------------------==//
3694 case IntrinsicHelper::IntToChar: {
3695 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
3696 irb_.getJIntTy());
3697 }
3698 case IntrinsicHelper::IntToShort: {
3699 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
3700 irb_.getJIntTy());
3701 }
3702 case IntrinsicHelper::IntToByte: {
3703 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
3704 irb_.getJIntTy());
3705 }
3706
3707 //==- Exception --------------------------------------------------------==//
3708 case IntrinsicHelper::CatchTargets: {
3709 UpdatePhiInstruction(current_bb_, irb_.GetInsertBlock());
3710 llvm::SwitchInst* si = llvm::dyn_cast<llvm::SwitchInst>(call_inst.getNextNode());
3711 CHECK(si != NULL);
3712 irb_.CreateBr(si->getDefaultDest());
3713 si->eraseFromParent();
3714 return call_inst.getArgOperand(0);
3715 }
3716
3717 //==- Constructor barrier-----------------------------------------------==//
3718 case IntrinsicHelper::ConstructorBarrier: {
3719 irb_.CreateMemoryBarrier(art::kStoreStore);
3720 return NULL;
3721 }
3722
3723 //==- Unknown Cases ----------------------------------------------------==//
3724 case IntrinsicHelper::MaxIntrinsicId:
3725 case IntrinsicHelper::UnknownId:
3726 //default:
3727 // NOTE: "default" is intentionally commented so that C/C++ compiler will
3728 // give some warning on unmatched cases.
3729 // NOTE: We should not implement these cases.
3730 break;
3731 }
3732 UNIMPLEMENTED(FATAL) << "Unexpected GBC intrinsic: " << static_cast<int>(intr_id);
3733 return NULL;
3734}
3735
3736} // anonymous namespace
3737
3738namespace art {
3739namespace llvm {
3740
3741::llvm::FunctionPass*
3742CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
3743 CompilerDriver* driver, const DexCompilationUnit* dex_compilation_unit) {
3744 return new GBCExpanderPass(intrinsic_helper, irb, driver, dex_compilation_unit);
3745}
3746
3747} // namespace llvm
3748} // namespace art