blob: f29cab1fa2d6ac2887dee401916b72e123d798c2 [file] [log] [blame]
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ir_builder.h"
18#include "utils_llvm.h"
19
20#include "greenland/intrinsic_helper.h"
21#include "object.h"
22#include "thread.h"
23
24#include <llvm/ADT/STLExtras.h>
25#include <llvm/Intrinsics.h>
26#include <llvm/Pass.h>
27#include <llvm/Support/CFG.h>
28#include <llvm/Support/InstIterator.h>
29
30#include <vector>
31
32using namespace art;
33using namespace compiler_llvm;
34
35using art::greenland::IntrinsicHelper;
36
37namespace {
38
39class GBCExpanderPass : public llvm::FunctionPass {
40 private:
41 const IntrinsicHelper& intrinsic_helper_;
42 IRBuilder& irb_;
43
44 llvm::LLVMContext& context_;
45 RuntimeSupportBuilder& rtb_;
46
47 private:
48 llvm::AllocaInst* shadow_frame_;
49 llvm::Value* old_shadow_frame_;
50 uint32_t shadow_frame_size_;
51
52 private:
53 //----------------------------------------------------------------------------
54 // Helper function for GBC expansion
55 //----------------------------------------------------------------------------
56
57 // Split the basic block containing INST at INST and insert a sequence of
58 // basic blocks with a single entry at BEGIN_BB and a single exit at END_BB
59 // before INST.
60 llvm::BasicBlock*
61 SplitAndInsertBasicBlocksAfter(llvm::BasicBlock::iterator inst,
62 llvm::BasicBlock* begin_bb,
63 llvm::BasicBlock* end_bb);
64
65 llvm::Value* ExpandToRuntime(runtime_support::RuntimeId rt,
66 llvm::CallInst& inst);
67
68 private:
69 // TODO: Almost all Emit* are directly copy-n-paste from MethodCompiler.
70 // Refactor these utility functions from MethodCompiler to avoid forking.
71
72 bool EmitStackOverflowCheck(llvm::Instruction* first_non_alloca);
73
74 //----------------------------------------------------------------------------
75 // Dex cache code generation helper function
76 //----------------------------------------------------------------------------
77 llvm::Value* EmitLoadDexCacheAddr(MemberOffset dex_cache_offset);
78
79 llvm::Value* EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx);
80
81 llvm::Value* EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx);
82
83 llvm::Value* EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx);
84
85 llvm::Value* EmitLoadDexCacheStringFieldAddr(uint32_t string_idx);
86
87 //----------------------------------------------------------------------------
88 // Code generation helper function
89 //----------------------------------------------------------------------------
90 llvm::Value* EmitLoadMethodObjectAddr();
91
92 llvm::Value* EmitLoadArrayLength(llvm::Value* array);
93
94 llvm::Value* EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx);
95
96 llvm::Value* EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
97 llvm::Value* this_addr);
98
99 llvm::Value* EmitArrayGEP(llvm::Value* array_addr,
100 llvm::Value* index_value,
101 JType elem_jty);
102
103 private:
104 //----------------------------------------------------------------------------
105 // Expand Greenland intrinsics
106 //----------------------------------------------------------------------------
107 void Expand_TestSuspend(llvm::CallInst& call_inst);
108
TDYa1279a129452012-07-19 03:10:08 -0700109 void Expand_MarkGCCard(llvm::CallInst& call_inst);
110
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700111 llvm::Value* Expand_GetException();
112
113 llvm::Value* Expand_LoadStringFromDexCache(llvm::Value* string_idx_value);
114
115 llvm::Value* Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value);
116
117 void Expand_LockObject(llvm::Value* obj);
118
119 void Expand_UnlockObject(llvm::Value* obj);
120
121 llvm::Value* Expand_ArrayGet(llvm::Value* array_addr,
122 llvm::Value* index_value,
123 JType elem_jty);
124
125 void Expand_ArrayPut(llvm::Value* new_value,
126 llvm::Value* array_addr,
127 llvm::Value* index_value,
128 JType elem_jty);
129
130 void Expand_FilledNewArray(llvm::CallInst& call_inst);
131
132 llvm::Value* Expand_IGetFast(llvm::Value* field_offset_value,
133 llvm::Value* is_volatile_value,
134 llvm::Value* object_addr,
135 JType field_jty);
136
137 void Expand_IPutFast(llvm::Value* field_offset_value,
138 llvm::Value* is_volatile_value,
139 llvm::Value* object_addr,
140 llvm::Value* new_value,
141 JType field_jty);
142
143 llvm::Value* Expand_SGetFast(llvm::Value* static_storage_addr,
144 llvm::Value* field_offset_value,
145 llvm::Value* is_volatile_value,
146 JType field_jty);
147
148 void Expand_SPutFast(llvm::Value* static_storage_addr,
149 llvm::Value* field_offset_value,
150 llvm::Value* is_volatile_value,
151 llvm::Value* new_value,
152 JType field_jty);
153
154 llvm::Value* Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr);
155
156 llvm::Value* Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value);
157
158 llvm::Value*
159 Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value);
160
161 llvm::Value*
162 Expand_GetVirtualCalleeMethodObjAddrFast(llvm::Value* vtable_idx_value,
163 llvm::Value* this_addr);
164
165 llvm::Value* Expand_Invoke(llvm::CallInst& call_inst);
166
167 llvm::Value* Expand_DivRem(llvm::Value* dividend, llvm::Value* divisor,
168 bool is_div, JType op_jty);
169
170 void Expand_AllocaShadowFrame(llvm::Value* num_entry_value);
171
172 void Expand_SetShadowFrameEntry(llvm::Value* obj, llvm::Value* entry_idx);
173
174 void Expand_PopShadowFrame();
175
176 void Expand_UpdateDexPC(llvm::Value* dex_pc_value);
177
TDYa127a1b21852012-07-23 03:20:39 -0700178 //----------------------------------------------------------------------------
179 // Quick
180 //----------------------------------------------------------------------------
181
182 llvm::Value* Expand_FPCompare(llvm::Value* src1_value,
183 llvm::Value* src2_value,
184 bool gt_bias);
185
186 llvm::Value* Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value);
187
188 llvm::Value* EmitCompareResultSelection(llvm::Value* cmp_eq,
189 llvm::Value* cmp_lt);
190
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700191 public:
192 static char ID;
193
194 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb)
195 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
196 context_(irb.getContext()), rtb_(irb.Runtime())
197 { }
198
199 bool runOnFunction(llvm::Function& func);
200
201 private:
202 bool InsertStackOverflowCheck(llvm::Function& func);
203
204 llvm::Value* ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
205 llvm::CallInst& call_inst);
206
207};
208
209char GBCExpanderPass::ID = 0;
210
211bool GBCExpanderPass::runOnFunction(llvm::Function& func) {
TDYa127b672d1e2012-06-28 21:21:45 -0700212 // Runtime support or stub
213 if (func.getName().startswith("art_") || func.getName().startswith("Art")) {
214 return false;
215 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700216 bool changed;
217
TDYa127b672d1e2012-06-28 21:21:45 -0700218 // TODO: Use intrinsic.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700219 changed = InsertStackOverflowCheck(func);
220
221 std::list<std::pair<llvm::CallInst*,
222 IntrinsicHelper::IntrinsicId> > work_list;
223
224 for (llvm::inst_iterator inst_iter = llvm::inst_begin(func),
225 inst_end = llvm::inst_end(func); inst_iter != inst_end; inst_iter++) {
226 // Only CallInst with its called function is dexlang intrinsic need to
227 // process
228 llvm::Instruction* inst = &*inst_iter;
229 if (llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst)) {
230 const llvm::Function* callee = call_inst->getCalledFunction();
231
232 if (callee != NULL) {
233 IntrinsicHelper::IntrinsicId intr_id =
234 intrinsic_helper_.GetIntrinsicId(callee);
235
236 if (intr_id != IntrinsicHelper::UnknownId) {
237 work_list.push_back(std::make_pair(call_inst, intr_id));
238 }
239 }
240 }
241 }
242
243 changed |= !work_list.empty();
244
245 shadow_frame_ = NULL;
246 old_shadow_frame_ = NULL;
247 shadow_frame_size_ = 0;
248
249 // Remove the instruction containing in the work_list
250 while (!work_list.empty()) {
251 llvm::CallInst* intr_inst = work_list.front().first;
252 IntrinsicHelper::IntrinsicId intr_id = work_list.front().second;
253
254 // Remove the instruction from work list
255 work_list.pop_front();
256
257 // Move the IRBuilder insert pointer
258 irb_.SetInsertPoint(intr_inst);
259
260 // Process the expansion
261 llvm::Value* new_value = ExpandIntrinsic(intr_id, *intr_inst);
262
263 // Use the new value from the expansion
264 if (new_value != NULL) {
265 intr_inst->replaceAllUsesWith(new_value);
266 }
267
268 // Remove the intrinsic instruction
269 intr_inst->eraseFromParent();
270 }
271
272 VERIFY_LLVM_FUNCTION(func);
273
274 return changed;
275}
276
277llvm::BasicBlock*
278GBCExpanderPass::SplitAndInsertBasicBlocksAfter(llvm::BasicBlock::iterator inst,
279 llvm::BasicBlock* begin_bb,
280 llvm::BasicBlock* end_bb) {
281 llvm::BasicBlock* original = inst->getParent();
282 llvm::Function* parent = original->getParent();
283
284 // 1. Create a new basic block A after ORIGINAL
285 llvm::BasicBlock *insert_before =
286 llvm::next(llvm::Function::iterator(original)).getNodePtrUnchecked();
287 llvm::BasicBlock* a =
288 llvm::BasicBlock::Create(context_, "", parent, insert_before);
289
290 // 2. Move all instructions in ORIGINAL after INST (included) to A
291 a->getInstList().splice(a->end(), original->getInstList(),
292 inst, original->end());
293
294 // 3. Add an unconditional branch in ORIGINAL to begin_bb
295 llvm::BranchInst::Create(begin_bb, original);
296
297 // 4. Add an unconditional branch in END_BB to A
298 llvm::BranchInst::Create(a, end_bb);
299
300 // 5. Update the PHI nodes in the successors of A. Update the PHI node entry
301 // with incoming basic block from ORIGINAL to A
302 for (llvm::succ_iterator succ_iter = llvm::succ_begin(a),
303 succ_end = llvm::succ_end(a); succ_iter != succ_end; succ_iter++) {
304 llvm::BasicBlock* succ = *succ_iter;
305 llvm::PHINode* phi;
306 for (llvm::BasicBlock::iterator inst_iter = succ->begin();
307 (phi = llvm::dyn_cast<llvm::PHINode>(inst_iter)); ++inst_iter) {
308 int idx;
309 while ((idx = phi->getBasicBlockIndex(original)) != -1) {
310 phi->setIncomingBlock(static_cast<unsigned>(idx), a);
311 }
312 }
313 }
314
315 return a;
316}
317
318llvm::Value* GBCExpanderPass::ExpandToRuntime(runtime_support::RuntimeId rt,
319 llvm::CallInst& inst) {
320 // Some GBC intrinsic can directly replace with IBC runtime. "Directly" means
321 // the arguments passed to the GBC intrinsic are as the same as IBC runtime
322 // function, therefore only called function is needed to change.
323 unsigned num_args = inst.getNumArgOperands();
324
325 if (num_args <= 0) {
326 return irb_.CreateCall(irb_.GetRuntime(rt));
327 } else {
328 std::vector<llvm::Value*> args;
329 for (unsigned i = 0; i < num_args; i++) {
330 args.push_back(inst.getArgOperand(i));
331 }
332
333 return irb_.CreateCall(irb_.GetRuntime(rt), args);
334 }
335}
336
337bool
338GBCExpanderPass::EmitStackOverflowCheck(llvm::Instruction* first_non_alloca) {
339 llvm::Function* func = first_non_alloca->getParent()->getParent();
340 llvm::Module* module = func->getParent();
341
342 llvm::BasicBlock* block_entry =
343 llvm::BasicBlock::Create(context_, "stack_overflow_entry", func);
344
345 irb_.SetInsertPoint(block_entry);
346
347 // Call llvm intrinsic function to get frame address.
348 llvm::Function* frameaddress =
349 llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
350
351 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
352 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
353
354 // Cast i8* to int
355 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
356
357 // Get thread.stack_end_
358 llvm::Value* stack_end =
359 irb_.Runtime().EmitLoadFromThreadOffset(Thread::StackEndOffset().Int32Value(),
360 irb_.getPtrEquivIntTy(),
361 kTBAARuntimeInfo);
362
363 // Check the frame address < thread.stack_end_ ?
364 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
365
366 llvm::BasicBlock* block_exception =
367 llvm::BasicBlock::Create(context_, "stack_overflow", func);
368
369 llvm::BasicBlock* block_continue =
370 llvm::BasicBlock::Create(context_, "stack_overflow_cont", func);
371
372 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
373
374 // If stack overflow, throw exception.
375 irb_.SetInsertPoint(block_exception);
376 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowStackOverflowException));
377
378 // Unwind.
379 llvm::Type* ret_type = func->getReturnType();
380 if (ret_type->isVoidTy()) {
381 irb_.CreateRetVoid();
382 } else {
383 // The return value is ignored when there's an exception. MethodCompiler
384 // returns zero value under the the corresponding return type in this case.
385 // GBCExpander returns LLVM undef value here for brevity
386 irb_.CreateRet(llvm::UndefValue::get(ret_type));
387 }
388
389 irb_.SetInsertPoint(block_continue);
390
391 SplitAndInsertBasicBlocksAfter(*first_non_alloca, block_entry, block_continue);
392
393 return true;
394}
395
396llvm::Value* GBCExpanderPass::EmitLoadDexCacheAddr(MemberOffset offset) {
397 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
398
399 return irb_.LoadFromObjectOffset(method_object_addr,
400 offset.Int32Value(),
401 irb_.getJObjectTy(),
402 kTBAAConstJObject);
403}
404
405llvm::Value*
406GBCExpanderPass::EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
407 llvm::Value* static_storage_dex_cache_addr =
408 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
409
410 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
411
412 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
413}
414
415llvm::Value*
416GBCExpanderPass::EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
417 llvm::Value* resolved_type_dex_cache_addr =
418 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
419
420 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
421
422 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
423}
424
425llvm::Value* GBCExpanderPass::
426EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
427 llvm::Value* resolved_method_dex_cache_addr =
428 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
429
430 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
431
432 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
433}
434
435llvm::Value* GBCExpanderPass::
436EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
437 llvm::Value* string_dex_cache_addr =
438 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
439
440 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
441
442 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
443}
444
445llvm::Value* GBCExpanderPass::EmitLoadMethodObjectAddr() {
446 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
447 return parent_func->arg_begin();
448}
449
450llvm::Value* GBCExpanderPass::EmitLoadArrayLength(llvm::Value* array) {
451 // Load array length
452 return irb_.LoadFromObjectOffset(array,
453 Array::LengthOffset().Int32Value(),
454 irb_.getJIntTy(),
455 kTBAAConstJObject);
456
457}
458
459llvm::Value*
460GBCExpanderPass::EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
461 llvm::Value* callee_method_object_field_addr =
462 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
463
464 return irb_.CreateLoad(callee_method_object_field_addr, kTBAAJRuntime);
465}
466
467llvm::Value* GBCExpanderPass::
468EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, llvm::Value* this_addr) {
469 // Load class object of *this* pointer
470 llvm::Value* class_object_addr =
471 irb_.LoadFromObjectOffset(this_addr,
472 Object::ClassOffset().Int32Value(),
473 irb_.getJObjectTy(),
474 kTBAAConstJObject);
475
476 // Load vtable address
477 llvm::Value* vtable_addr =
478 irb_.LoadFromObjectOffset(class_object_addr,
479 Class::VTableOffset().Int32Value(),
480 irb_.getJObjectTy(),
481 kTBAAConstJObject);
482
483 // Load callee method object
484 llvm::Value* vtable_idx_value =
485 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
486
487 llvm::Value* method_field_addr =
488 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
489
490 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
491}
492
493// Emit Array GetElementPtr
494llvm::Value* GBCExpanderPass::EmitArrayGEP(llvm::Value* array_addr,
495 llvm::Value* index_value,
496 JType elem_jty) {
497
498 int data_offset;
499 if (elem_jty == kLong || elem_jty == kDouble ||
500 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
501 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
502 } else {
503 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
504 }
505
506 llvm::Constant* data_offset_value =
507 irb_.getPtrEquivInt(data_offset);
508
509 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
510
511 llvm::Value* array_data_addr =
512 irb_.CreatePtrDisp(array_addr, data_offset_value,
513 elem_type->getPointerTo());
514
515 return irb_.CreateGEP(array_data_addr, index_value);
516}
517
518void GBCExpanderPass::Expand_TestSuspend(llvm::CallInst& call_inst) {
519 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
520 llvm::BasicBlock* suspend_test_begin_bb =
521 llvm::BasicBlock::Create(context_, "suspend_test", parent_func);
522
523 irb_.SetInsertPoint(suspend_test_begin_bb);
524 irb_.Runtime().EmitTestSuspend();
525
526 llvm::BasicBlock* suspend_test_end_bb = irb_.GetInsertBlock();
527
528 SplitAndInsertBasicBlocksAfter(call_inst, suspend_test_begin_bb,
529 suspend_test_end_bb);
530 return;
531}
532
TDYa1279a129452012-07-19 03:10:08 -0700533void GBCExpanderPass::Expand_MarkGCCard(llvm::CallInst& call_inst) {
534 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
535 llvm::BasicBlock* begin_bb =
536 llvm::BasicBlock::Create(context_, "mark_gc_card", parent_func);
537
538 irb_.SetInsertPoint(begin_bb);
539 irb_.Runtime().EmitMarkGCCard(call_inst.getArgOperand(0), call_inst.getArgOperand(1));
540
541 llvm::BasicBlock* end_bb = irb_.GetInsertBlock();
542
543 SplitAndInsertBasicBlocksAfter(call_inst, begin_bb, end_bb);
544 return;
545}
546
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700547llvm::Value* GBCExpanderPass::Expand_GetException() {
548 // Get thread-local exception field address
549 llvm::Value* exception_object_addr =
550 irb_.Runtime().EmitLoadFromThreadOffset(Thread::ExceptionOffset().Int32Value(),
551 irb_.getJObjectTy(),
552 kTBAAJRuntime);
553
554 // Set thread-local exception field address to NULL
555 irb_.Runtime().EmitStoreToThreadOffset(Thread::ExceptionOffset().Int32Value(),
556 irb_.getJNull(),
557 kTBAAJRuntime);
558
559 return exception_object_addr;
560}
561
562llvm::Value*
563GBCExpanderPass::Expand_LoadStringFromDexCache(llvm::Value* string_idx_value) {
564 uint32_t string_idx =
565 llvm::cast<llvm::ConstantInt>(string_idx_value)->getZExtValue();
566
567 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
568
569 return irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
570}
571
572llvm::Value*
573GBCExpanderPass::Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value) {
574 uint32_t type_idx =
575 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
576
577 llvm::Value* type_field_addr =
578 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
579
580 return irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
581}
582
583void GBCExpanderPass::Expand_LockObject(llvm::Value* obj) {
584 llvm::BasicBlock::iterator lock_obj_inst = irb_.GetInsertPoint();
585 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
586
587 llvm::BasicBlock* lock_obj_begin_bb =
588 llvm::BasicBlock::Create(context_, "", parent);
589
590 irb_.SetInsertPoint(lock_obj_begin_bb);
591 rtb_.EmitLockObject(obj);
592
593 llvm::BasicBlock* lock_obj_end_bb = irb_.GetInsertBlock();
594
595 SplitAndInsertBasicBlocksAfter(lock_obj_inst, lock_obj_begin_bb,
596 lock_obj_end_bb);
597
598 return;
599}
600
601void GBCExpanderPass::Expand_UnlockObject(llvm::Value* obj) {
602 llvm::BasicBlock::iterator unlock_obj_inst = irb_.GetInsertPoint();
603 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
604
605 llvm::BasicBlock* unlock_obj_begin_bb =
606 llvm::BasicBlock::Create(context_, "", parent);
607
608 irb_.SetInsertPoint(unlock_obj_begin_bb);
609 rtb_.EmitUnlockObject(obj);
610
611 llvm::BasicBlock* unlock_obj_end_bb = irb_.GetInsertBlock();
612
613 SplitAndInsertBasicBlocksAfter(unlock_obj_inst, unlock_obj_begin_bb,
614 unlock_obj_end_bb);
615
616 return;
617}
618
619llvm::Value* GBCExpanderPass::Expand_ArrayGet(llvm::Value* array_addr,
620 llvm::Value* index_value,
621 JType elem_jty) {
622 llvm::Value* array_elem_addr =
623 EmitArrayGEP(array_addr, index_value, elem_jty);
624
625 return irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
626}
627
628void GBCExpanderPass::Expand_ArrayPut(llvm::Value* new_value,
629 llvm::Value* array_addr,
630 llvm::Value* index_value,
631 JType elem_jty) {
632 llvm::Value* array_elem_addr =
633 EmitArrayGEP(array_addr, index_value, elem_jty);
634
635 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
636
637 return;
638}
639
640void GBCExpanderPass::Expand_FilledNewArray(llvm::CallInst& call_inst) {
641 // Most of the codes refer to MethodCompiler::EmitInsn_FilledNewArray
642 llvm::Value* array = call_inst.getArgOperand(0);
643
644 uint32_t element_jty =
645 llvm::cast<llvm::ConstantInt>(call_inst.getArgOperand(1))->getZExtValue();
646
647 DCHECK(call_inst.getNumArgOperands() > 2);
648 unsigned num_elements = (call_inst.getNumArgOperands() - 2);
649
650 bool is_elem_int_ty = (static_cast<JType>(element_jty) == kInt);
651
652 uint32_t alignment;
653 llvm::Constant* elem_size;
654 llvm::PointerType* field_type;
655
656 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
657 // as the element, thus we are only checking 2 cases: primitive int and
658 // non-primitive type.
659 if (is_elem_int_ty) {
660 alignment = sizeof(int32_t);
661 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
662 field_type = irb_.getJIntTy()->getPointerTo();
663 } else {
664 alignment = irb_.getSizeOfPtrEquivInt();
665 elem_size = irb_.getSizeOfPtrEquivIntValue();
666 field_type = irb_.getJObjectTy()->getPointerTo();
667 }
668
669 llvm::Value* data_field_offset =
670 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
671
672 llvm::Value* data_field_addr =
673 irb_.CreatePtrDisp(array, data_field_offset, field_type);
674
675 for (unsigned i = 0; i < num_elements; ++i) {
676 // Values to fill the array begin at the 3rd argument
677 llvm::Value* reg_value = call_inst.getArgOperand(2 + i);
678
679 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
680
681 data_field_addr =
682 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
683 }
684
685 return;
686}
687
688llvm::Value* GBCExpanderPass::Expand_IGetFast(llvm::Value* field_offset_value,
689 llvm::Value* /*is_volatile_value*/,
690 llvm::Value* object_addr,
691 JType field_jty) {
692 int field_offset =
693 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
694
695 DCHECK_GE(field_offset, 0);
696
697 llvm::PointerType* field_type =
698 irb_.getJType(field_jty, kField)->getPointerTo();
699
700 field_offset_value = irb_.getPtrEquivInt(field_offset);
701
702 llvm::Value* field_addr =
703 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
704
705 // TODO: Check is_volatile. We need to generate atomic load instruction
706 // when is_volatile is true.
707 return irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
708}
709
710void GBCExpanderPass::Expand_IPutFast(llvm::Value* field_offset_value,
711 llvm::Value* /* is_volatile_value */,
712 llvm::Value* object_addr,
713 llvm::Value* new_value,
714 JType field_jty) {
715 int field_offset =
716 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
717
718 DCHECK_GE(field_offset, 0);
719
720 llvm::PointerType* field_type =
721 irb_.getJType(field_jty, kField)->getPointerTo();
722
723 field_offset_value = irb_.getPtrEquivInt(field_offset);
724
725 llvm::Value* field_addr =
726 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
727
728 // TODO: Check is_volatile. We need to generate atomic store instruction
729 // when is_volatile is true.
730 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
731
732 return;
733}
734
735llvm::Value* GBCExpanderPass::Expand_SGetFast(llvm::Value* static_storage_addr,
736 llvm::Value* field_offset_value,
737 llvm::Value* /*is_volatile_value*/,
738 JType field_jty) {
739 int field_offset =
740 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
741
742 DCHECK_GE(field_offset, 0);
743
744 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
745
746 llvm::Value* static_field_addr =
747 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
748 irb_.getJType(field_jty, kField)->getPointerTo());
749
750 // TODO: Check is_volatile. We need to generate atomic store instruction
751 // when is_volatile is true.
752 return irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
753}
754
755void GBCExpanderPass::Expand_SPutFast(llvm::Value* static_storage_addr,
756 llvm::Value* field_offset_value,
757 llvm::Value* /* is_volatile_value */,
758 llvm::Value* new_value,
759 JType field_jty) {
760 int field_offset =
761 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
762
763 DCHECK_GE(field_offset, 0);
764
765 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
766
767 llvm::Value* static_field_addr =
768 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
769 irb_.getJType(field_jty, kField)->getPointerTo());
770
771 // TODO: Check is_volatile. We need to generate atomic store instruction
772 // when is_volatile is true.
773 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
774
775 return;
776}
777
778llvm::Value*
779GBCExpanderPass::Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr) {
780 return irb_.LoadFromObjectOffset(method_object_addr,
781 Method::DeclaringClassOffset().Int32Value(),
782 irb_.getJObjectTy(),
783 kTBAAConstJObject);
784}
785
786llvm::Value*
787GBCExpanderPass::Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value) {
788 uint32_t type_idx =
789 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
790
791 llvm::Value* storage_field_addr =
792 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
793
794 return irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
795}
796
797llvm::Value*
798GBCExpanderPass::Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value) {
799 uint32_t callee_method_idx =
800 llvm::cast<llvm::ConstantInt>(callee_method_idx_value)->getZExtValue();
801
802 return EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
803}
804
805llvm::Value* GBCExpanderPass::Expand_GetVirtualCalleeMethodObjAddrFast(
806 llvm::Value* vtable_idx_value,
807 llvm::Value* this_addr) {
808 int vtable_idx =
809 llvm::cast<llvm::ConstantInt>(vtable_idx_value)->getSExtValue();
810
811 return EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
812}
813
814llvm::Value* GBCExpanderPass::Expand_Invoke(llvm::CallInst& call_inst) {
815 // Most of the codes refer to MethodCompiler::EmitInsn_Invoke
816 llvm::Value* callee_method_object_addr = call_inst.getArgOperand(0);
817 unsigned num_args = call_inst.getNumArgOperands();
818 llvm::Type* ret_type = call_inst.getType();
819
820 // Determine the function type of the callee method
821 std::vector<llvm::Type*> args_type;
822 std::vector<llvm::Value*> args;
823 for (unsigned i = 0; i < num_args; i++) {
824 args.push_back(call_inst.getArgOperand(i));
825 args_type.push_back(args[i]->getType());
826 }
827
828 llvm::FunctionType* callee_method_type =
829 llvm::FunctionType::get(ret_type, args_type, false);
830
831 llvm::Value* code_addr =
832 irb_.LoadFromObjectOffset(callee_method_object_addr,
833 Method::GetCodeOffset().Int32Value(),
834 callee_method_type->getPointerTo(),
835 kTBAAJRuntime);
836
837 // Invoke callee
838 llvm::Value* retval = irb_.CreateCall(code_addr, args);
839
840 return retval;
841}
842
843llvm::Value* GBCExpanderPass::Expand_DivRem(llvm::Value* dividend,
844 llvm::Value* divisor,
845 bool is_div, JType op_jty) {
846 // Most of the codes refer to MethodCompiler::EmitIntDivRemResultComputation
847
848 // Check the special case: MININT / -1 = MININT
849 // That case will cause overflow, which is undefined behavior in llvm.
850 // So we check the divisor is -1 or not, if the divisor is -1, we do
851 // the special path to avoid undefined behavior.
852 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
853 llvm::Value* zero = irb_.getJZero(op_jty);
854 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
855
856 llvm::BasicBlock::iterator div_rem_inst = irb_.GetInsertPoint();
857 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
858
859 llvm::BasicBlock* begin_div_rem =
860 llvm::BasicBlock::Create(context_, "", parent);
861 llvm::BasicBlock* eq_neg_one = llvm::BasicBlock::Create(context_, "", parent);
862 llvm::BasicBlock* ne_neg_one = llvm::BasicBlock::Create(context_, "", parent);
863 llvm::BasicBlock* neg_one_cont =
864 llvm::BasicBlock::Create(context_, "", parent);
865
866 irb_.SetInsertPoint(begin_div_rem);
867 llvm::Value* is_equal_neg_one = irb_.CreateICmpEQ(divisor, neg_one);
868 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
869
870 // If divisor == -1
871 irb_.SetInsertPoint(eq_neg_one);
872 llvm::Value* eq_result;
873 if (is_div) {
874 // We can just change from "dividend div -1" to "neg dividend". The sub
875 // don't care the sign/unsigned because of two's complement representation.
876 // And the behavior is what we want:
877 // -(2^n) (2^n)-1
878 // MININT < k <= MAXINT -> mul k -1 = -k
879 // MININT == k -> mul k -1 = k
880 //
881 // LLVM use sub to represent 'neg'
882 eq_result = irb_.CreateSub(zero, dividend);
883 } else {
884 // Everything modulo -1 will be 0.
885 eq_result = zero;
886 }
887 irb_.CreateBr(neg_one_cont);
888
889 // If divisor != -1, just do the division.
890 irb_.SetInsertPoint(ne_neg_one);
891 llvm::Value* ne_result;
892 if (is_div) {
893 ne_result = irb_.CreateSDiv(dividend, divisor);
894 } else {
895 ne_result = irb_.CreateSRem(dividend, divisor);
896 }
897 irb_.CreateBr(neg_one_cont);
898
899 irb_.SetInsertPoint(neg_one_cont);
900 llvm::PHINode* result = irb_.CreatePHI(op_type, 2);
901 result->addIncoming(eq_result, eq_neg_one);
902 result->addIncoming(ne_result, ne_neg_one);
903
904 SplitAndInsertBasicBlocksAfter(div_rem_inst, begin_div_rem, neg_one_cont);
905
906 return result;
907}
908
909void GBCExpanderPass::Expand_AllocaShadowFrame(llvm::Value* num_entry_value) {
910 // Most of the codes refer to MethodCompiler::EmitPrologueAllocShadowFrame and
911 // MethodCompiler::EmitPushShadowFrame
912 shadow_frame_size_ =
913 llvm::cast<llvm::ConstantInt>(num_entry_value)->getZExtValue();
914
915 llvm::StructType* shadow_frame_type =
916 irb_.getShadowFrameTy(shadow_frame_size_);
917
918 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
919
920 // Alloca a pointer to old shadow frame
921 old_shadow_frame_ =
922 irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
923
924 // Zero-initialization of the shadow frame table
925 llvm::Value* shadow_frame_table =
926 irb_.CreateConstGEP2_32(shadow_frame_, 0, 1);
927 llvm::Type* table_type = shadow_frame_type->getElementType(1);
928
929 llvm::ConstantAggregateZero* zero_initializer =
930 llvm::ConstantAggregateZero::get(table_type);
931
932 irb_.CreateStore(zero_initializer, shadow_frame_table, kTBAAShadowFrame);
933
934 // Push the shadow frame
935 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
936
937 // Push the shadow frame
938 llvm::Value* shadow_frame_upcast =
939 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
940
941 llvm::Value* result = rtb_.EmitPushShadowFrame(shadow_frame_upcast,
942 method_object_addr,
943 shadow_frame_size_);
944
945 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
946
947 return;
948}
949
950void GBCExpanderPass::Expand_SetShadowFrameEntry(llvm::Value* obj,
951 llvm::Value* entry_idx) {
952 DCHECK(shadow_frame_ != NULL);
953
954 llvm::Value* gep_index[] = {
955 irb_.getInt32(0), // No pointer displacement
956 irb_.getInt32(1), // SIRT
957 entry_idx // Pointer field
958 };
959
960 llvm::Value* entry_addr = irb_.CreateGEP(shadow_frame_, gep_index);
961 irb_.CreateStore(obj, entry_addr, kTBAAShadowFrame);
962 return;
963}
964
965void GBCExpanderPass::Expand_PopShadowFrame() {
966 rtb_.EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
967 return;
968}
969
970void GBCExpanderPass::Expand_UpdateDexPC(llvm::Value* dex_pc_value) {
971 irb_.StoreToObjectOffset(shadow_frame_,
972 ShadowFrame::DexPCOffset(),
973 dex_pc_value,
974 kTBAAShadowFrame);
975 return;
976}
977
978bool GBCExpanderPass::InsertStackOverflowCheck(llvm::Function& func) {
979 // DexLang generates all alloca instruction in the first basic block of the
980 // FUNC and also there's no any alloca instructions after the first non-alloca
981 // instruction
982
983 llvm::BasicBlock::iterator first_non_alloca = func.front().begin();
984 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
985 ++first_non_alloca;
986 }
987
988 // Insert stack overflow check codes before first_non_alloca (i.e., after all
989 // alloca instructions)
990 return EmitStackOverflowCheck(&*first_non_alloca);
991}
992
TDYa127a1b21852012-07-23 03:20:39 -0700993llvm::Value* GBCExpanderPass::Expand_FPCompare(llvm::Value* src1_value,
994 llvm::Value* src2_value,
995 bool gt_bias) {
996 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
997 llvm::Value* cmp_lt;
998
999 if (gt_bias) {
1000 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1001 } else {
1002 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1003 }
1004
1005 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1006}
1007
1008llvm::Value* GBCExpanderPass::Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value) {
1009 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1010 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1011
1012 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1013}
1014
1015llvm::Value* GBCExpanderPass::EmitCompareResultSelection(llvm::Value* cmp_eq,
1016 llvm::Value* cmp_lt) {
1017
1018 llvm::Constant* zero = irb_.getJInt(0);
1019 llvm::Constant* pos1 = irb_.getJInt(1);
1020 llvm::Constant* neg1 = irb_.getJInt(-1);
1021
1022 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1023 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1024
1025 return result_eq;
1026}
1027
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001028llvm::Value*
1029GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
1030 llvm::CallInst& call_inst) {
1031 switch (intr_id) {
1032 //==- Thread -----------------------------------------------------------==//
1033 case IntrinsicHelper::GetCurrentThread: {
TDYa127b672d1e2012-06-28 21:21:45 -07001034 return irb_.Runtime().EmitGetCurrentThread();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001035 }
1036 case IntrinsicHelper::TestSuspend: {
1037 Expand_TestSuspend(call_inst);
1038 return NULL;
1039 }
1040 case IntrinsicHelper::MarkGCCard: {
TDYa1279a129452012-07-19 03:10:08 -07001041 Expand_MarkGCCard(call_inst);
1042 return NULL;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001043 }
1044 //==- Exception --------------------------------------------------------==//
1045 case IntrinsicHelper::ThrowException: {
1046 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
1047 }
1048 case IntrinsicHelper::GetException: {
1049 return Expand_GetException();
1050 }
1051 case IntrinsicHelper::IsExceptionPending: {
1052 return irb_.Runtime().EmitIsExceptionPending();
1053 }
1054 case IntrinsicHelper::FindCatchBlock: {
1055 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
1056 }
1057 case IntrinsicHelper::ThrowDivZeroException: {
1058 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
1059 }
1060 case IntrinsicHelper::ThrowNullPointerException: {
1061 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
1062 }
1063 case IntrinsicHelper::ThrowIndexOutOfBounds: {
1064 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
1065 }
1066 //==- ConstString ------------------------------------------------------==//
1067 case IntrinsicHelper::LoadStringFromDexCache: {
1068 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
1069 }
1070 case IntrinsicHelper::ResolveString: {
1071 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
1072 }
1073 //==- ConstClass -------------------------------------------------------==//
1074 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
1075 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
1076 }
1077 case IntrinsicHelper::LoadTypeFromDexCache: {
1078 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
1079 }
1080 case IntrinsicHelper::InitializeType: {
1081 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
1082 }
1083 //==- Lock -------------------------------------------------------------==//
1084 case IntrinsicHelper::LockObject: {
1085 Expand_LockObject(call_inst.getArgOperand(0));
1086 return NULL;
1087 }
1088 case IntrinsicHelper::UnlockObject: {
1089 Expand_UnlockObject(call_inst.getArgOperand(0));
1090 return NULL;
1091 }
1092 //==- Cast -------------------------------------------------------------==//
1093 case IntrinsicHelper::CheckCast: {
1094 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
1095 }
1096 case IntrinsicHelper::IsAssignable: {
1097 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
1098 }
1099 //==- Alloc ------------------------------------------------------------==//
1100 case IntrinsicHelper::AllocObject: {
1101 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
1102 }
1103 case IntrinsicHelper::AllocObjectWithAccessCheck: {
1104 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
1105 }
1106 //==- Array ------------------------------------------------------------==//
1107 case IntrinsicHelper::ArrayLength: {
1108 return EmitLoadArrayLength(call_inst.getArgOperand(0));
1109 }
1110 case IntrinsicHelper::AllocArray: {
1111 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
1112 }
1113 case IntrinsicHelper::AllocArrayWithAccessCheck: {
1114 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
1115 call_inst);
1116 }
1117 case IntrinsicHelper::CheckAndAllocArray: {
1118 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
1119 }
1120 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
1121 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
1122 call_inst);
1123 }
1124 case IntrinsicHelper::ArrayGet: {
1125 return Expand_ArrayGet(call_inst.getArgOperand(0),
1126 call_inst.getArgOperand(1),
1127 kInt);
1128 }
1129 case IntrinsicHelper::ArrayGetWide: {
1130 return Expand_ArrayGet(call_inst.getArgOperand(0),
1131 call_inst.getArgOperand(1),
1132 kLong);
1133 }
1134 case IntrinsicHelper::ArrayGetObject: {
1135 return Expand_ArrayGet(call_inst.getArgOperand(0),
1136 call_inst.getArgOperand(1),
1137 kObject);
1138 }
1139 case IntrinsicHelper::ArrayGetBoolean: {
1140 return Expand_ArrayGet(call_inst.getArgOperand(0),
1141 call_inst.getArgOperand(1),
1142 kBoolean);
1143 }
1144 case IntrinsicHelper::ArrayGetByte: {
1145 return Expand_ArrayGet(call_inst.getArgOperand(0),
1146 call_inst.getArgOperand(1),
1147 kByte);
1148 }
1149 case IntrinsicHelper::ArrayGetChar: {
1150 return Expand_ArrayGet(call_inst.getArgOperand(0),
1151 call_inst.getArgOperand(1),
1152 kChar);
1153 }
1154 case IntrinsicHelper::ArrayGetShort: {
1155 return Expand_ArrayGet(call_inst.getArgOperand(0),
1156 call_inst.getArgOperand(1),
1157 kShort);
1158 }
1159 case IntrinsicHelper::ArrayPut: {
1160 Expand_ArrayPut(call_inst.getArgOperand(0),
1161 call_inst.getArgOperand(1),
1162 call_inst.getArgOperand(2),
1163 kInt);
1164 return NULL;
1165 }
1166 case IntrinsicHelper::ArrayPutWide: {
1167 Expand_ArrayPut(call_inst.getArgOperand(0),
1168 call_inst.getArgOperand(1),
1169 call_inst.getArgOperand(2),
1170 kLong);
1171 return NULL;
1172 }
1173 case IntrinsicHelper::ArrayPutObject: {
1174 Expand_ArrayPut(call_inst.getArgOperand(0),
1175 call_inst.getArgOperand(1),
1176 call_inst.getArgOperand(2),
1177 kObject);
1178 return NULL;
1179 }
1180 case IntrinsicHelper::ArrayPutBoolean: {
1181 Expand_ArrayPut(call_inst.getArgOperand(0),
1182 call_inst.getArgOperand(1),
1183 call_inst.getArgOperand(2),
1184 kBoolean);
1185 return NULL;
1186 }
1187 case IntrinsicHelper::ArrayPutByte: {
1188 Expand_ArrayPut(call_inst.getArgOperand(0),
1189 call_inst.getArgOperand(1),
1190 call_inst.getArgOperand(2),
1191 kByte);
1192 return NULL;
1193 }
1194 case IntrinsicHelper::ArrayPutChar: {
1195 Expand_ArrayPut(call_inst.getArgOperand(0),
1196 call_inst.getArgOperand(1),
1197 call_inst.getArgOperand(2),
1198 kChar);
1199 return NULL;
1200 }
1201 case IntrinsicHelper::ArrayPutShort: {
1202 Expand_ArrayPut(call_inst.getArgOperand(0),
1203 call_inst.getArgOperand(1),
1204 call_inst.getArgOperand(2),
1205 kShort);
1206 return NULL;
1207 }
1208 case IntrinsicHelper::CheckPutArrayElement: {
1209 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
1210 }
1211 case IntrinsicHelper::FilledNewArray: {
1212 Expand_FilledNewArray(call_inst);
1213 return NULL;
1214 }
1215 case IntrinsicHelper::FillArrayData: {
1216 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
1217 }
1218 //==- Instance Field ---------------------------------------------------==//
1219 case IntrinsicHelper::InstanceFieldGet:
1220 case IntrinsicHelper::InstanceFieldGetBoolean:
1221 case IntrinsicHelper::InstanceFieldGetByte:
1222 case IntrinsicHelper::InstanceFieldGetChar:
1223 case IntrinsicHelper::InstanceFieldGetShort: {
1224 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
1225 }
1226 case IntrinsicHelper::InstanceFieldGetWide: {
1227 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
1228 }
1229 case IntrinsicHelper::InstanceFieldGetObject: {
1230 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
1231 }
1232 case IntrinsicHelper::InstanceFieldGetFast: {
1233 return Expand_IGetFast(call_inst.getArgOperand(0),
1234 call_inst.getArgOperand(1),
1235 call_inst.getArgOperand(2),
1236 kInt);
1237 }
1238 case IntrinsicHelper::InstanceFieldGetWideFast: {
1239 return Expand_IGetFast(call_inst.getArgOperand(0),
1240 call_inst.getArgOperand(1),
1241 call_inst.getArgOperand(2),
1242 kLong);
1243 }
1244 case IntrinsicHelper::InstanceFieldGetObjectFast: {
1245 return Expand_IGetFast(call_inst.getArgOperand(0),
1246 call_inst.getArgOperand(1),
1247 call_inst.getArgOperand(2),
1248 kObject);
1249 }
1250 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
1251 return Expand_IGetFast(call_inst.getArgOperand(0),
1252 call_inst.getArgOperand(1),
1253 call_inst.getArgOperand(2),
1254 kBoolean);
1255 }
1256 case IntrinsicHelper::InstanceFieldGetByteFast: {
1257 return Expand_IGetFast(call_inst.getArgOperand(0),
1258 call_inst.getArgOperand(1),
1259 call_inst.getArgOperand(2),
1260 kByte);
1261 }
1262 case IntrinsicHelper::InstanceFieldGetCharFast: {
1263 return Expand_IGetFast(call_inst.getArgOperand(0),
1264 call_inst.getArgOperand(1),
1265 call_inst.getArgOperand(2),
1266 kChar);
1267 }
1268 case IntrinsicHelper::InstanceFieldGetShortFast: {
1269 return Expand_IGetFast(call_inst.getArgOperand(0),
1270 call_inst.getArgOperand(1),
1271 call_inst.getArgOperand(2),
1272 kShort);
1273 }
1274 case IntrinsicHelper::InstanceFieldPut:
1275 case IntrinsicHelper::InstanceFieldPutBoolean:
1276 case IntrinsicHelper::InstanceFieldPutByte:
1277 case IntrinsicHelper::InstanceFieldPutChar:
1278 case IntrinsicHelper::InstanceFieldPutShort: {
1279 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
1280 }
1281 case IntrinsicHelper::InstanceFieldPutWide: {
1282 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
1283 }
1284 case IntrinsicHelper::InstanceFieldPutObject: {
1285 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
1286 }
1287 case IntrinsicHelper::InstanceFieldPutFast: {
1288 Expand_IPutFast(call_inst.getArgOperand(0),
1289 call_inst.getArgOperand(1),
1290 call_inst.getArgOperand(2),
1291 call_inst.getArgOperand(3),
1292 kInt);
1293 return NULL;
1294 }
1295 case IntrinsicHelper::InstanceFieldPutWideFast: {
1296 Expand_IPutFast(call_inst.getArgOperand(0),
1297 call_inst.getArgOperand(1),
1298 call_inst.getArgOperand(2),
1299 call_inst.getArgOperand(3),
1300 kLong);
1301 return NULL;
1302 }
1303 case IntrinsicHelper::InstanceFieldPutObjectFast: {
1304 Expand_IPutFast(call_inst.getArgOperand(0),
1305 call_inst.getArgOperand(1),
1306 call_inst.getArgOperand(2),
1307 call_inst.getArgOperand(3),
1308 kObject);
1309 return NULL;
1310 }
1311 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
1312 Expand_IPutFast(call_inst.getArgOperand(0),
1313 call_inst.getArgOperand(1),
1314 call_inst.getArgOperand(2),
1315 call_inst.getArgOperand(3),
1316 kBoolean);
1317 return NULL;
1318 }
1319 case IntrinsicHelper::InstanceFieldPutByteFast: {
1320 Expand_IPutFast(call_inst.getArgOperand(0),
1321 call_inst.getArgOperand(1),
1322 call_inst.getArgOperand(2),
1323 call_inst.getArgOperand(3),
1324 kByte);
1325 return NULL;
1326 }
1327 case IntrinsicHelper::InstanceFieldPutCharFast: {
1328 Expand_IPutFast(call_inst.getArgOperand(0),
1329 call_inst.getArgOperand(1),
1330 call_inst.getArgOperand(2),
1331 call_inst.getArgOperand(3),
1332 kChar);
1333 return NULL;
1334 }
1335 case IntrinsicHelper::InstanceFieldPutShortFast: {
1336 Expand_IPutFast(call_inst.getArgOperand(0),
1337 call_inst.getArgOperand(1),
1338 call_inst.getArgOperand(2),
1339 call_inst.getArgOperand(3),
1340 kShort);
1341 return NULL;
1342 }
1343 //==- Static Field -----------------------------------------------------==//
1344 case IntrinsicHelper::StaticFieldGet:
1345 case IntrinsicHelper::StaticFieldGetBoolean:
1346 case IntrinsicHelper::StaticFieldGetByte:
1347 case IntrinsicHelper::StaticFieldGetChar:
1348 case IntrinsicHelper::StaticFieldGetShort: {
1349 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
1350 }
1351 case IntrinsicHelper::StaticFieldGetWide: {
1352 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
1353 }
1354 case IntrinsicHelper::StaticFieldGetObject: {
1355 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
1356 }
1357 case IntrinsicHelper::StaticFieldGetFast: {
1358 return Expand_SGetFast(call_inst.getArgOperand(0),
1359 call_inst.getArgOperand(1),
1360 call_inst.getArgOperand(2),
1361 kInt);
1362 }
1363 case IntrinsicHelper::StaticFieldGetWideFast: {
1364 return Expand_SGetFast(call_inst.getArgOperand(0),
1365 call_inst.getArgOperand(1),
1366 call_inst.getArgOperand(2),
1367 kLong);
1368 }
1369 case IntrinsicHelper::StaticFieldGetObjectFast: {
1370 return Expand_SGetFast(call_inst.getArgOperand(0),
1371 call_inst.getArgOperand(1),
1372 call_inst.getArgOperand(2),
1373 kObject);
1374 }
1375 case IntrinsicHelper::StaticFieldGetBooleanFast: {
1376 return Expand_SGetFast(call_inst.getArgOperand(0),
1377 call_inst.getArgOperand(1),
1378 call_inst.getArgOperand(2),
1379 kBoolean);
1380 }
1381 case IntrinsicHelper::StaticFieldGetByteFast: {
1382 return Expand_SGetFast(call_inst.getArgOperand(0),
1383 call_inst.getArgOperand(1),
1384 call_inst.getArgOperand(2),
1385 kByte);
1386 }
1387 case IntrinsicHelper::StaticFieldGetCharFast: {
1388 return Expand_SGetFast(call_inst.getArgOperand(0),
1389 call_inst.getArgOperand(1),
1390 call_inst.getArgOperand(2),
1391 kChar);
1392 }
1393 case IntrinsicHelper::StaticFieldGetShortFast: {
1394 return Expand_SGetFast(call_inst.getArgOperand(0),
1395 call_inst.getArgOperand(1),
1396 call_inst.getArgOperand(2),
1397 kShort);
1398 }
1399 case IntrinsicHelper::StaticFieldPut:
1400 case IntrinsicHelper::StaticFieldPutBoolean:
1401 case IntrinsicHelper::StaticFieldPutByte:
1402 case IntrinsicHelper::StaticFieldPutChar:
1403 case IntrinsicHelper::StaticFieldPutShort: {
1404 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
1405 }
1406 case IntrinsicHelper::StaticFieldPutWide: {
1407 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
1408 }
1409 case IntrinsicHelper::StaticFieldPutObject: {
1410 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
1411 }
1412 case IntrinsicHelper::StaticFieldPutFast: {
1413 Expand_SPutFast(call_inst.getArgOperand(0),
1414 call_inst.getArgOperand(1),
1415 call_inst.getArgOperand(2),
1416 call_inst.getArgOperand(3),
1417 kInt);
1418 return NULL;
1419 }
1420 case IntrinsicHelper::StaticFieldPutWideFast: {
1421 Expand_SPutFast(call_inst.getArgOperand(0),
1422 call_inst.getArgOperand(1),
1423 call_inst.getArgOperand(2),
1424 call_inst.getArgOperand(3),
1425 kLong);
1426 return NULL;
1427 }
1428 case IntrinsicHelper::StaticFieldPutObjectFast: {
1429 Expand_SPutFast(call_inst.getArgOperand(0),
1430 call_inst.getArgOperand(1),
1431 call_inst.getArgOperand(2),
1432 call_inst.getArgOperand(3),
1433 kObject);
1434 return NULL;
1435 }
1436 case IntrinsicHelper::StaticFieldPutBooleanFast: {
1437 Expand_SPutFast(call_inst.getArgOperand(0),
1438 call_inst.getArgOperand(1),
1439 call_inst.getArgOperand(2),
1440 call_inst.getArgOperand(3),
1441 kBoolean);
1442 return NULL;
1443 }
1444 case IntrinsicHelper::StaticFieldPutByteFast: {
1445 Expand_SPutFast(call_inst.getArgOperand(0),
1446 call_inst.getArgOperand(1),
1447 call_inst.getArgOperand(2),
1448 call_inst.getArgOperand(3),
1449 kByte);
1450 return NULL;
1451 }
1452 case IntrinsicHelper::StaticFieldPutCharFast: {
1453 Expand_SPutFast(call_inst.getArgOperand(0),
1454 call_inst.getArgOperand(1),
1455 call_inst.getArgOperand(2),
1456 call_inst.getArgOperand(3),
1457 kChar);
1458 return NULL;
1459 }
1460 case IntrinsicHelper::StaticFieldPutShortFast: {
1461 Expand_SPutFast(call_inst.getArgOperand(0),
1462 call_inst.getArgOperand(1),
1463 call_inst.getArgOperand(2),
1464 call_inst.getArgOperand(3),
1465 kShort);
1466 return NULL;
1467 }
1468 case IntrinsicHelper::LoadDeclaringClassSSB: {
1469 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
1470 }
1471 case IntrinsicHelper::LoadClassSSBFromDexCache: {
1472 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
1473 }
1474 case IntrinsicHelper::InitializeAndLoadClassSSB: {
1475 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
1476 }
1477 //==- Invoke -----------------------------------------------------------==//
1478 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
1479 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
1480 }
1481 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
1482 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
1483 }
1484 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
1485 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
1486 }
1487 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
1488 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
1489 }
1490 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
1491 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
1492 }
1493 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
1494 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
1495 }
1496 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
1497 return Expand_GetVirtualCalleeMethodObjAddrFast(
1498 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
1499 }
1500 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
1501 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
1502 }
1503 case IntrinsicHelper::InvokeRetVoid:
1504 case IntrinsicHelper::InvokeRetBoolean:
1505 case IntrinsicHelper::InvokeRetByte:
1506 case IntrinsicHelper::InvokeRetChar:
1507 case IntrinsicHelper::InvokeRetShort:
1508 case IntrinsicHelper::InvokeRetInt:
1509 case IntrinsicHelper::InvokeRetLong:
1510 case IntrinsicHelper::InvokeRetFloat:
1511 case IntrinsicHelper::InvokeRetDouble:
1512 case IntrinsicHelper::InvokeRetObject: {
1513 return Expand_Invoke(call_inst);
1514 }
1515 //==- Math -------------------------------------------------------------==//
1516 case IntrinsicHelper::DivInt: {
1517 return Expand_DivRem(call_inst.getArgOperand(0),
1518 call_inst.getArgOperand(1),
1519 /* is_div */true, kInt);
1520 }
1521 case IntrinsicHelper::RemInt: {
1522 return Expand_DivRem(call_inst.getArgOperand(0),
1523 call_inst.getArgOperand(1),
1524 /* is_div */false, kInt);
1525 }
1526 case IntrinsicHelper::DivLong: {
1527 return Expand_DivRem(call_inst.getArgOperand(0),
1528 call_inst.getArgOperand(1),
1529 /* is_div */true, kLong);
1530 }
1531 case IntrinsicHelper::RemLong: {
1532 return Expand_DivRem(call_inst.getArgOperand(0),
1533 call_inst.getArgOperand(1),
1534 /* is_div */false, kLong);
1535 }
1536 case IntrinsicHelper::D2L: {
1537 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
1538 }
1539 case IntrinsicHelper::D2I: {
1540 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
1541 }
1542 case IntrinsicHelper::F2L: {
1543 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
1544 }
1545 case IntrinsicHelper::F2I: {
1546 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
1547 }
1548 //==- Shadow Frame -----------------------------------------------------==//
1549 case IntrinsicHelper::AllocaShadowFrame: {
1550 Expand_AllocaShadowFrame(call_inst.getArgOperand(0));
1551 return NULL;
1552 }
1553 case IntrinsicHelper::SetShadowFrameEntry: {
1554 Expand_SetShadowFrameEntry(call_inst.getArgOperand(0),
1555 call_inst.getArgOperand(1));
1556 return NULL;
1557 }
1558 case IntrinsicHelper::PopShadowFrame: {
1559 Expand_PopShadowFrame();
1560 return NULL;
1561 }
1562 case IntrinsicHelper::UpdateDexPC: {
1563 Expand_UpdateDexPC(call_inst.getArgOperand(0));
1564 return NULL;
1565 }
TDYa127a1b21852012-07-23 03:20:39 -07001566
1567
1568 //==- Quick ------------------------------------------------------------==//
1569 case IntrinsicHelper::IntToChar: {
1570 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
1571 irb_.getJIntTy());
1572 }
1573 case IntrinsicHelper::IntToShort: {
1574 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
1575 irb_.getJIntTy());
1576 }
1577 case IntrinsicHelper::IntToByte: {
1578 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
1579 irb_.getJIntTy());
1580 }
1581 case IntrinsicHelper::CmplFloat:
1582 case IntrinsicHelper::CmplDouble: {
1583 return Expand_FPCompare(call_inst.getArgOperand(0), call_inst.getArgOperand(0), false);
1584 }
1585 case IntrinsicHelper::CmpgFloat:
1586 case IntrinsicHelper::CmpgDouble: {
1587 return Expand_FPCompare(call_inst.getArgOperand(0), call_inst.getArgOperand(0), true);
1588 }
1589 case IntrinsicHelper::CmpLong: {
1590 return Expand_LongCompare(call_inst.getArgOperand(0), call_inst.getArgOperand(0));
1591 }
1592 case greenland::IntrinsicHelper::SHLLong: {
1593 }
1594 case greenland::IntrinsicHelper::SHRLong: {
1595 }
1596 case greenland::IntrinsicHelper::USHRLong: {
1597 }
1598 case greenland::IntrinsicHelper::SHLInt: {
1599 }
1600 case greenland::IntrinsicHelper::SHRInt: {
1601 }
1602 case greenland::IntrinsicHelper::USHRInt: {
1603 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001604 default: {
1605 const IntrinsicHelper::IntrinsicInfo& intr_info =
1606 IntrinsicHelper::GetInfo(intr_id);
1607
1608 UNIMPLEMENTED(FATAL) << "expand DexLang intrinsic: " << intr_info.name_;
1609 break;
1610 }
1611 }
1612 return NULL;
1613}
1614
1615} // anonymous namespace
1616
1617namespace art {
1618namespace compiler_llvm {
1619
1620llvm::FunctionPass*
1621CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb) {
1622 return new GBCExpanderPass(intrinsic_helper, irb);
1623}
1624
1625} // namespace compiler_llvm
1626} // namespace art