blob: 93d7d48ab3001b6a2ca3a3a75493a2bc5a073bfd [file] [log] [blame]
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ir_builder.h"
18#include "utils_llvm.h"
19
TDYa1275e869b62012-07-25 00:45:39 -070020#include "compiler.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070021#include "greenland/intrinsic_helper.h"
TDYa1275e869b62012-07-25 00:45:39 -070022#include "oat_compilation_unit.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070023#include "object.h"
24#include "thread.h"
TDYa1275e869b62012-07-25 00:45:39 -070025#include "verifier/method_verifier.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070026
buzbeeefc63692012-11-14 16:31:52 -080027#include "compiler/compiler_ir.h"
buzbee449a49b2012-11-25 19:25:44 -080028#include "compiler/codegen/codegen.h"
TDYa127920be7c2012-09-10 17:13:22 -070029using art::kMIRIgnoreNullCheck;
30using art::kMIRIgnoreRangeCheck;
31
Shih-wei Liao21d28f52012-06-12 05:55:00 -070032#include <llvm/ADT/STLExtras.h>
33#include <llvm/Intrinsics.h>
Logan Chiend36a2ac2012-08-04 00:37:30 +080034#include <llvm/Metadata.h>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070035#include <llvm/Pass.h>
36#include <llvm/Support/CFG.h>
37#include <llvm/Support/InstIterator.h>
38
39#include <vector>
TDYa127aa558872012-08-16 05:11:07 -070040#include <map>
41#include <utility>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070042
TDYa127920be7c2012-09-10 17:13:22 -070043using namespace art::compiler_llvm;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070044
45using art::greenland::IntrinsicHelper;
46
Shih-wei Liaob2596522012-09-14 16:36:11 -070047namespace art {
48extern char remapShorty(char shortyType);
49};
50
Shih-wei Liao21d28f52012-06-12 05:55:00 -070051namespace {
52
53class GBCExpanderPass : public llvm::FunctionPass {
54 private:
55 const IntrinsicHelper& intrinsic_helper_;
56 IRBuilder& irb_;
57
58 llvm::LLVMContext& context_;
59 RuntimeSupportBuilder& rtb_;
60
61 private:
62 llvm::AllocaInst* shadow_frame_;
63 llvm::Value* old_shadow_frame_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070064
65 private:
TDYa127920be7c2012-09-10 17:13:22 -070066 art::Compiler* compiler_;
TDYa1275e869b62012-07-25 00:45:39 -070067
TDYa127920be7c2012-09-10 17:13:22 -070068 const art::DexFile* dex_file_;
69 const art::DexFile::CodeItem* code_item_;
TDYa1275e869b62012-07-25 00:45:39 -070070
TDYa127920be7c2012-09-10 17:13:22 -070071 art::OatCompilationUnit* oat_compilation_unit_;
TDYa1275e869b62012-07-25 00:45:39 -070072
73 uint32_t method_idx_;
74
75 llvm::Function* func_;
76
77 std::vector<llvm::BasicBlock*> basic_blocks_;
78
79 std::vector<llvm::BasicBlock*> basic_block_landing_pads_;
TDYa12755e5e6c2012-09-11 15:14:42 -070080 llvm::BasicBlock* current_bb_;
TDYa127aa558872012-08-16 05:11:07 -070081 std::map<llvm::BasicBlock*, std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> > >
82 landing_pad_phi_mapping_;
TDYa1275e869b62012-07-25 00:45:39 -070083 llvm::BasicBlock* basic_block_unwind_;
84
Logan Chien67645d82012-08-17 09:10:54 +080085 bool changed_;
86
TDYa1275e869b62012-07-25 00:45:39 -070087 private:
Shih-wei Liao21d28f52012-06-12 05:55:00 -070088 //----------------------------------------------------------------------------
Logan Chien75e4b602012-07-23 14:24:12 -070089 // Constant for GBC expansion
90 //----------------------------------------------------------------------------
91 enum IntegerShiftKind {
92 kIntegerSHL,
93 kIntegerSHR,
94 kIntegerUSHR,
95 };
96
97 private:
98 //----------------------------------------------------------------------------
Shih-wei Liao21d28f52012-06-12 05:55:00 -070099 // Helper function for GBC expansion
100 //----------------------------------------------------------------------------
101
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700102 llvm::Value* ExpandToRuntime(runtime_support::RuntimeId rt,
103 llvm::CallInst& inst);
104
TDYa1275e869b62012-07-25 00:45:39 -0700105 uint64_t LV2UInt(llvm::Value* lv) {
106 return llvm::cast<llvm::ConstantInt>(lv)->getZExtValue();
107 }
108
109 int64_t LV2SInt(llvm::Value* lv) {
110 return llvm::cast<llvm::ConstantInt>(lv)->getSExtValue();
111 }
112
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700113 private:
114 // TODO: Almost all Emit* are directly copy-n-paste from MethodCompiler.
115 // Refactor these utility functions from MethodCompiler to avoid forking.
116
Logan Chien67645d82012-08-17 09:10:54 +0800117 void EmitStackOverflowCheck(llvm::Instruction* first_non_alloca);
118
119 void RewriteFunction();
120
121 void RewriteBasicBlock(llvm::BasicBlock* original_block);
122
123 void UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
124 llvm::BasicBlock* new_basic_block);
125
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700126
127 //----------------------------------------------------------------------------
128 // Dex cache code generation helper function
129 //----------------------------------------------------------------------------
TDYa127920be7c2012-09-10 17:13:22 -0700130 llvm::Value* EmitLoadDexCacheAddr(art::MemberOffset dex_cache_offset);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700131
132 llvm::Value* EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx);
133
134 llvm::Value* EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx);
135
136 llvm::Value* EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx);
137
138 llvm::Value* EmitLoadDexCacheStringFieldAddr(uint32_t string_idx);
139
140 //----------------------------------------------------------------------------
141 // Code generation helper function
142 //----------------------------------------------------------------------------
143 llvm::Value* EmitLoadMethodObjectAddr();
144
145 llvm::Value* EmitLoadArrayLength(llvm::Value* array);
146
147 llvm::Value* EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx);
148
149 llvm::Value* EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
150 llvm::Value* this_addr);
151
152 llvm::Value* EmitArrayGEP(llvm::Value* array_addr,
153 llvm::Value* index_value,
154 JType elem_jty);
155
156 private:
157 //----------------------------------------------------------------------------
158 // Expand Greenland intrinsics
159 //----------------------------------------------------------------------------
160 void Expand_TestSuspend(llvm::CallInst& call_inst);
161
TDYa1279a129452012-07-19 03:10:08 -0700162 void Expand_MarkGCCard(llvm::CallInst& call_inst);
163
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700164 llvm::Value* Expand_LoadStringFromDexCache(llvm::Value* string_idx_value);
165
166 llvm::Value* Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value);
167
168 void Expand_LockObject(llvm::Value* obj);
169
170 void Expand_UnlockObject(llvm::Value* obj);
171
172 llvm::Value* Expand_ArrayGet(llvm::Value* array_addr,
173 llvm::Value* index_value,
174 JType elem_jty);
175
176 void Expand_ArrayPut(llvm::Value* new_value,
177 llvm::Value* array_addr,
178 llvm::Value* index_value,
179 JType elem_jty);
180
181 void Expand_FilledNewArray(llvm::CallInst& call_inst);
182
183 llvm::Value* Expand_IGetFast(llvm::Value* field_offset_value,
184 llvm::Value* is_volatile_value,
185 llvm::Value* object_addr,
186 JType field_jty);
187
188 void Expand_IPutFast(llvm::Value* field_offset_value,
189 llvm::Value* is_volatile_value,
190 llvm::Value* object_addr,
191 llvm::Value* new_value,
192 JType field_jty);
193
194 llvm::Value* Expand_SGetFast(llvm::Value* static_storage_addr,
195 llvm::Value* field_offset_value,
196 llvm::Value* is_volatile_value,
197 JType field_jty);
198
199 void Expand_SPutFast(llvm::Value* static_storage_addr,
200 llvm::Value* field_offset_value,
201 llvm::Value* is_volatile_value,
202 llvm::Value* new_value,
203 JType field_jty);
204
205 llvm::Value* Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr);
206
207 llvm::Value* Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value);
208
209 llvm::Value*
210 Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value);
211
212 llvm::Value*
213 Expand_GetVirtualCalleeMethodObjAddrFast(llvm::Value* vtable_idx_value,
214 llvm::Value* this_addr);
215
216 llvm::Value* Expand_Invoke(llvm::CallInst& call_inst);
217
TDYa1274ec8ccd2012-08-11 07:04:57 -0700218 llvm::Value* Expand_DivRem(llvm::CallInst& call_inst, bool is_div, JType op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700219
TDYa127ce4cc0d2012-11-18 16:59:53 -0800220 void Expand_AllocaShadowFrame(llvm::Value* num_vregs_value);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700221
TDYa1278e950c12012-11-02 09:58:19 -0700222 void Expand_SetVReg(llvm::Value* entry_idx, llvm::Value* obj);
223
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700224 void Expand_PopShadowFrame();
225
226 void Expand_UpdateDexPC(llvm::Value* dex_pc_value);
227
TDYa127a1b21852012-07-23 03:20:39 -0700228 //----------------------------------------------------------------------------
229 // Quick
230 //----------------------------------------------------------------------------
231
232 llvm::Value* Expand_FPCompare(llvm::Value* src1_value,
233 llvm::Value* src2_value,
234 bool gt_bias);
235
236 llvm::Value* Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value);
237
238 llvm::Value* EmitCompareResultSelection(llvm::Value* cmp_eq,
239 llvm::Value* cmp_lt);
240
TDYa127f71bf5a2012-07-29 20:09:52 -0700241 llvm::Value* EmitLoadConstantClass(uint32_t dex_pc, uint32_t type_idx);
TDYa1275a26d442012-07-26 18:58:38 -0700242 llvm::Value* EmitLoadStaticStorage(uint32_t dex_pc, uint32_t type_idx);
243
TDYa1275e869b62012-07-25 00:45:39 -0700244 llvm::Value* Expand_HLIGet(llvm::CallInst& call_inst, JType field_jty);
245 void Expand_HLIPut(llvm::CallInst& call_inst, JType field_jty);
246
TDYa1275a26d442012-07-26 18:58:38 -0700247 llvm::Value* Expand_HLSget(llvm::CallInst& call_inst, JType field_jty);
248 void Expand_HLSput(llvm::CallInst& call_inst, JType field_jty);
249
250 llvm::Value* Expand_HLArrayGet(llvm::CallInst& call_inst, JType field_jty);
251 void Expand_HLArrayPut(llvm::CallInst& call_inst, JType field_jty);
252
TDYa127f71bf5a2012-07-29 20:09:52 -0700253 llvm::Value* Expand_ConstString(llvm::CallInst& call_inst);
254 llvm::Value* Expand_ConstClass(llvm::CallInst& call_inst);
255
256 void Expand_MonitorEnter(llvm::CallInst& call_inst);
257 void Expand_MonitorExit(llvm::CallInst& call_inst);
258
259 void Expand_HLCheckCast(llvm::CallInst& call_inst);
260 llvm::Value* Expand_InstanceOf(llvm::CallInst& call_inst);
261
262 llvm::Value* Expand_NewInstance(llvm::CallInst& call_inst);
263
264 llvm::Value* Expand_HLInvoke(llvm::CallInst& call_inst);
265
266 llvm::Value* Expand_OptArrayLength(llvm::CallInst& call_inst);
267 llvm::Value* Expand_NewArray(llvm::CallInst& call_inst);
268 llvm::Value* Expand_HLFilledNewArray(llvm::CallInst& call_inst);
269 void Expand_HLFillArrayData(llvm::CallInst& call_inst);
270
271 llvm::Value* EmitAllocNewArray(uint32_t dex_pc,
272 llvm::Value* array_length_value,
273 uint32_t type_idx,
274 bool is_filled_new_array);
275
276 llvm::Value* EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -0700277 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -0700278 llvm::Value* this_addr,
279 uint32_t dex_pc,
280 bool is_fast_path);
281
TDYa1275e869b62012-07-25 00:45:39 -0700282 void EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr);
283
284 void EmitUpdateDexPC(uint32_t dex_pc);
285
286 void EmitGuard_DivZeroException(uint32_t dex_pc,
287 llvm::Value* denominator,
288 JType op_jty);
289
290 void EmitGuard_NullPointerException(uint32_t dex_pc,
291 llvm::Value* object);
292
293 void EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
294 llvm::Value* array,
295 llvm::Value* index);
296
TDYa1275e869b62012-07-25 00:45:39 -0700297 llvm::FunctionType* GetFunctionType(uint32_t method_idx, bool is_static);
298
299 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
300
301 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
302 const char* postfix);
303
304 int32_t GetTryItemOffset(uint32_t dex_pc);
305
306 llvm::BasicBlock* GetLandingPadBasicBlock(uint32_t dex_pc);
307
308 llvm::BasicBlock* GetUnwindBasicBlock();
309
310 void EmitGuard_ExceptionLandingPad(uint32_t dex_pc);
311
312 void EmitBranchExceptionLandingPad(uint32_t dex_pc);
313
Logan Chien75e4b602012-07-23 14:24:12 -0700314 //----------------------------------------------------------------------------
315 // Expand Arithmetic Helper Intrinsics
316 //----------------------------------------------------------------------------
317
318 llvm::Value* Expand_IntegerShift(llvm::Value* src1_value,
319 llvm::Value* src2_value,
320 IntegerShiftKind kind,
321 JType op_jty);
322
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700323 public:
324 static char ID;
325
326 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb)
327 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
Logan Chiene5b8f8b2012-08-13 11:45:05 +0800328 context_(irb.getContext()), rtb_(irb.Runtime()),
TDYa1278e950c12012-11-02 09:58:19 -0700329 shadow_frame_(NULL), old_shadow_frame_(NULL),
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700330 compiler_(NULL), dex_file_(NULL), code_item_(NULL),
Logan Chien67645d82012-08-17 09:10:54 +0800331 oat_compilation_unit_(NULL), method_idx_(-1u), func_(NULL),
332 changed_(false)
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700333 { }
334
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700335 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
TDYa127920be7c2012-09-10 17:13:22 -0700336 art::Compiler* compiler, art::OatCompilationUnit* oat_compilation_unit)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700337 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
338 context_(irb.getContext()), rtb_(irb.Runtime()),
TDYa1278e950c12012-11-02 09:58:19 -0700339 shadow_frame_(NULL), old_shadow_frame_(NULL),
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700340 compiler_(compiler),
341 dex_file_(oat_compilation_unit->GetDexFile()),
342 code_item_(oat_compilation_unit->GetCodeItem()),
343 oat_compilation_unit_(oat_compilation_unit),
344 method_idx_(oat_compilation_unit->GetDexMethodIndex()),
345 func_(NULL), changed_(false)
346 { }
347
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700348 bool runOnFunction(llvm::Function& func);
349
350 private:
Logan Chien67645d82012-08-17 09:10:54 +0800351 void InsertStackOverflowCheck(llvm::Function& func);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700352
353 llvm::Value* ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
354 llvm::CallInst& call_inst);
355
356};
357
358char GBCExpanderPass::ID = 0;
359
360bool GBCExpanderPass::runOnFunction(llvm::Function& func) {
TDYa127b672d1e2012-06-28 21:21:45 -0700361 // Runtime support or stub
362 if (func.getName().startswith("art_") || func.getName().startswith("Art")) {
363 return false;
364 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700365
Logan Chien67645d82012-08-17 09:10:54 +0800366 // Setup rewrite context
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700367 shadow_frame_ = NULL;
368 old_shadow_frame_ = NULL;
TDYa1275e869b62012-07-25 00:45:39 -0700369 func_ = &func;
Logan Chien67645d82012-08-17 09:10:54 +0800370 changed_ = false; // Assume unchanged
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700371
buzbeec531cef2012-10-18 07:09:20 -0700372#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700373 basic_blocks_.resize(code_item_->insns_size_in_code_units_);
374 basic_block_landing_pads_.resize(code_item_->tries_size_, NULL);
375 basic_block_unwind_ = NULL;
376 for (llvm::Function::iterator bb_iter = func_->begin(), bb_end = func_->end();
377 bb_iter != bb_end;
378 ++bb_iter) {
379 if (bb_iter->begin()->getMetadata("DexOff") == NULL) {
380 continue;
381 }
382 uint32_t dex_pc = LV2UInt(bb_iter->begin()->getMetadata("DexOff")->getOperand(0));
383 basic_blocks_[dex_pc] = bb_iter;
384 }
385#endif
386
Logan Chien67645d82012-08-17 09:10:54 +0800387 // Insert stack overflow check
388 InsertStackOverflowCheck(func); // TODO: Use intrinsic.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700389
Logan Chien67645d82012-08-17 09:10:54 +0800390 // Rewrite the intrinsics
391 RewriteFunction();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700392
393 VERIFY_LLVM_FUNCTION(func);
394
Logan Chien67645d82012-08-17 09:10:54 +0800395 return changed_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700396}
397
Logan Chien67645d82012-08-17 09:10:54 +0800398void GBCExpanderPass::RewriteBasicBlock(llvm::BasicBlock* original_block) {
399 llvm::BasicBlock* curr_basic_block = original_block;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700400
Logan Chien67645d82012-08-17 09:10:54 +0800401 llvm::BasicBlock::iterator inst_iter = original_block->begin();
402 llvm::BasicBlock::iterator inst_end = original_block->end();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700403
Logan Chien67645d82012-08-17 09:10:54 +0800404 while (inst_iter != inst_end) {
405 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst_iter);
406 IntrinsicHelper::IntrinsicId intr_id = IntrinsicHelper::UnknownId;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700407
Logan Chien67645d82012-08-17 09:10:54 +0800408 if (call_inst) {
409 llvm::Function* callee_func = call_inst->getCalledFunction();
410 intr_id = intrinsic_helper_.GetIntrinsicId(callee_func);
411 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700412
Logan Chien67645d82012-08-17 09:10:54 +0800413 if (intr_id == IntrinsicHelper::UnknownId) {
414 // This is not intrinsic call. Skip this instruction.
415 ++inst_iter;
416 continue;
417 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700418
Logan Chien67645d82012-08-17 09:10:54 +0800419 // Rewrite the intrinsic and change the function
420 changed_ = true;
421 irb_.SetInsertPoint(inst_iter);
422
423 // Expand the intrinsic
424 if (llvm::Value* new_value = ExpandIntrinsic(intr_id, *call_inst)) {
425 inst_iter->replaceAllUsesWith(new_value);
426 }
427
428 // Remove the old intrinsic call instruction
429 llvm::BasicBlock::iterator old_inst = inst_iter++;
430 old_inst->eraseFromParent();
431
432 // Splice the instruction to the new basic block
433 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
434 if (next_basic_block != curr_basic_block) {
435 next_basic_block->getInstList().splice(
436 irb_.GetInsertPoint(), curr_basic_block->getInstList(),
437 inst_iter, inst_end);
438 curr_basic_block = next_basic_block;
439 inst_end = curr_basic_block->end();
440 }
441 }
442}
443
444
445void GBCExpanderPass::RewriteFunction() {
446 size_t num_basic_blocks = func_->getBasicBlockList().size();
447 // NOTE: We are not using (bb_iter != bb_end) as the for-loop condition,
448 // because we will create new basic block while expanding the intrinsics.
449 // We only want to iterate through the input basic blocks.
450
TDYa127aa558872012-08-16 05:11:07 -0700451 landing_pad_phi_mapping_.clear();
452
Logan Chien67645d82012-08-17 09:10:54 +0800453 for (llvm::Function::iterator bb_iter = func_->begin();
454 num_basic_blocks > 0; ++bb_iter, --num_basic_blocks) {
Shih-wei Liao627d8c42012-08-24 08:31:18 -0700455 // Set insert point to current basic block.
456 irb_.SetInsertPoint(bb_iter);
Logan Chien67645d82012-08-17 09:10:54 +0800457
TDYa12755e5e6c2012-09-11 15:14:42 -0700458 current_bb_ = bb_iter;
TDYa127aa558872012-08-16 05:11:07 -0700459
Logan Chien67645d82012-08-17 09:10:54 +0800460 // Rewrite the basic block
461 RewriteBasicBlock(bb_iter);
462
463 // Update the phi-instructions in the successor basic block
464 llvm::BasicBlock* last_block = irb_.GetInsertBlock();
465 if (last_block != bb_iter) {
466 UpdatePhiInstruction(bb_iter, last_block);
467 }
468 }
TDYa127aa558872012-08-16 05:11:07 -0700469
470 typedef std::map<llvm::PHINode*, llvm::PHINode*> HandlerPHIMap;
471 HandlerPHIMap handler_phi;
472 // Iterate every used landing pad basic block
473 for (size_t i = 0, ei = basic_block_landing_pads_.size(); i != ei; ++i) {
474 llvm::BasicBlock* lbb = basic_block_landing_pads_[i];
475 if (lbb == NULL) {
476 continue;
477 }
478
479 llvm::TerminatorInst* term_inst = lbb->getTerminator();
480 std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> >& rewrite_pair
481 = landing_pad_phi_mapping_[lbb];
482 irb_.SetInsertPoint(lbb->begin());
483
484 // Iterate every succeeding basic block (catch block)
485 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
486 succ_iter != succ_end; ++succ_iter) {
487 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
488
489 // Iterate every phi instructions in the succeeding basic block
490 for (llvm::BasicBlock::iterator
491 inst_iter = succ_basic_block->begin(),
492 inst_end = succ_basic_block->end();
493 inst_iter != inst_end; ++inst_iter) {
494 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
495
496 if (!phi) {
497 break; // Meet non-phi instruction. Done.
498 }
499
500 if (handler_phi[phi] == NULL) {
501 handler_phi[phi] = llvm::PHINode::Create(phi->getType(), 1);
502 }
503
504 // Create new_phi in landing pad
505 llvm::PHINode* new_phi = irb_.CreatePHI(phi->getType(), rewrite_pair.size());
506 // Insert all incoming value into new_phi by rewrite_pair
507 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
508 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
509 llvm::BasicBlock* new_bb = rewrite_pair[j].second;
510 new_phi->addIncoming(phi->getIncomingValueForBlock(old_bb), new_bb);
511 }
512 // Delete all incoming value from phi by rewrite_pair
513 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
514 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
515 int old_bb_idx = phi->getBasicBlockIndex(old_bb);
516 if (old_bb_idx >= 0) {
517 phi->removeIncomingValue(old_bb_idx, false);
518 }
519 }
520 // Insert new_phi into new handler phi
521 handler_phi[phi]->addIncoming(new_phi, lbb);
522 }
523 }
524 }
525
526 // Replace all handler phi
527 // We can't just use the old handler phi, because some exception edges will disappear after we
528 // compute fast-path.
529 for (HandlerPHIMap::iterator it = handler_phi.begin(); it != handler_phi.end(); ++it) {
530 llvm::PHINode* old_phi = it->first;
531 llvm::PHINode* new_phi = it->second;
532 new_phi->insertBefore(old_phi);
533 old_phi->replaceAllUsesWith(new_phi);
534 old_phi->eraseFromParent();
535 }
Logan Chien67645d82012-08-17 09:10:54 +0800536}
537
538void GBCExpanderPass::UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
539 llvm::BasicBlock* new_basic_block) {
540 llvm::TerminatorInst* term_inst = new_basic_block->getTerminator();
541
542 if (!term_inst) {
543 return; // No terminating instruction in new_basic_block. Nothing to do.
544 }
545
546 // Iterate every succeeding basic block
547 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
548 succ_iter != succ_end; ++succ_iter) {
549 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
550
551 // Iterate every phi instructions in the succeeding basic block
552 for (llvm::BasicBlock::iterator
553 inst_iter = succ_basic_block->begin(),
554 inst_end = succ_basic_block->end();
555 inst_iter != inst_end; ++inst_iter) {
556 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
557
558 if (!phi) {
559 break; // Meet non-phi instruction. Done.
560 }
561
562 // Update the incoming block of this phi instruction
563 for (llvm::PHINode::block_iterator
564 ibb_iter = phi->block_begin(), ibb_end = phi->block_end();
565 ibb_iter != ibb_end; ++ibb_iter) {
566 if (*ibb_iter == old_basic_block) {
567 *ibb_iter = new_basic_block;
568 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700569 }
570 }
571 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700572}
573
574llvm::Value* GBCExpanderPass::ExpandToRuntime(runtime_support::RuntimeId rt,
575 llvm::CallInst& inst) {
576 // Some GBC intrinsic can directly replace with IBC runtime. "Directly" means
577 // the arguments passed to the GBC intrinsic are as the same as IBC runtime
578 // function, therefore only called function is needed to change.
579 unsigned num_args = inst.getNumArgOperands();
580
581 if (num_args <= 0) {
582 return irb_.CreateCall(irb_.GetRuntime(rt));
583 } else {
584 std::vector<llvm::Value*> args;
585 for (unsigned i = 0; i < num_args; i++) {
586 args.push_back(inst.getArgOperand(i));
587 }
588
589 return irb_.CreateCall(irb_.GetRuntime(rt), args);
590 }
591}
592
Logan Chien67645d82012-08-17 09:10:54 +0800593void
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700594GBCExpanderPass::EmitStackOverflowCheck(llvm::Instruction* first_non_alloca) {
595 llvm::Function* func = first_non_alloca->getParent()->getParent();
596 llvm::Module* module = func->getParent();
597
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700598 // Call llvm intrinsic function to get frame address.
599 llvm::Function* frameaddress =
600 llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
601
602 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
603 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
604
605 // Cast i8* to int
606 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
607
608 // Get thread.stack_end_
609 llvm::Value* stack_end =
TDYa127920be7c2012-09-10 17:13:22 -0700610 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::StackEndOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700611 irb_.getPtrEquivIntTy(),
612 kTBAARuntimeInfo);
613
614 // Check the frame address < thread.stack_end_ ?
615 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
616
617 llvm::BasicBlock* block_exception =
618 llvm::BasicBlock::Create(context_, "stack_overflow", func);
619
620 llvm::BasicBlock* block_continue =
621 llvm::BasicBlock::Create(context_, "stack_overflow_cont", func);
622
623 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
624
625 // If stack overflow, throw exception.
626 irb_.SetInsertPoint(block_exception);
627 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowStackOverflowException));
628
629 // Unwind.
630 llvm::Type* ret_type = func->getReturnType();
631 if (ret_type->isVoidTy()) {
632 irb_.CreateRetVoid();
633 } else {
634 // The return value is ignored when there's an exception. MethodCompiler
635 // returns zero value under the the corresponding return type in this case.
636 // GBCExpander returns LLVM undef value here for brevity
637 irb_.CreateRet(llvm::UndefValue::get(ret_type));
638 }
639
640 irb_.SetInsertPoint(block_continue);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700641}
642
TDYa127920be7c2012-09-10 17:13:22 -0700643llvm::Value* GBCExpanderPass::EmitLoadDexCacheAddr(art::MemberOffset offset) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700644 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
645
646 return irb_.LoadFromObjectOffset(method_object_addr,
647 offset.Int32Value(),
648 irb_.getJObjectTy(),
649 kTBAAConstJObject);
650}
651
652llvm::Value*
653GBCExpanderPass::EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
654 llvm::Value* static_storage_dex_cache_addr =
Mathieu Chartier66f19252012-09-18 08:57:04 -0700655 EmitLoadDexCacheAddr(art::AbstractMethod::DexCacheInitializedStaticStorageOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700656
657 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
658
659 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
660}
661
662llvm::Value*
663GBCExpanderPass::EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
664 llvm::Value* resolved_type_dex_cache_addr =
Mathieu Chartier66f19252012-09-18 08:57:04 -0700665 EmitLoadDexCacheAddr(art::AbstractMethod::DexCacheResolvedTypesOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700666
667 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
668
669 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
670}
671
672llvm::Value* GBCExpanderPass::
673EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
674 llvm::Value* resolved_method_dex_cache_addr =
Mathieu Chartier66f19252012-09-18 08:57:04 -0700675 EmitLoadDexCacheAddr(art::AbstractMethod::DexCacheResolvedMethodsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700676
677 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
678
679 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
680}
681
682llvm::Value* GBCExpanderPass::
683EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
684 llvm::Value* string_dex_cache_addr =
Mathieu Chartier66f19252012-09-18 08:57:04 -0700685 EmitLoadDexCacheAddr(art::AbstractMethod::DexCacheStringsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700686
687 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
688
689 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
690}
691
692llvm::Value* GBCExpanderPass::EmitLoadMethodObjectAddr() {
693 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
694 return parent_func->arg_begin();
695}
696
697llvm::Value* GBCExpanderPass::EmitLoadArrayLength(llvm::Value* array) {
698 // Load array length
699 return irb_.LoadFromObjectOffset(array,
TDYa127920be7c2012-09-10 17:13:22 -0700700 art::Array::LengthOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700701 irb_.getJIntTy(),
702 kTBAAConstJObject);
703
704}
705
706llvm::Value*
707GBCExpanderPass::EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
708 llvm::Value* callee_method_object_field_addr =
709 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
710
TDYa127ce4cc0d2012-11-18 16:59:53 -0800711 return irb_.CreateLoad(callee_method_object_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700712}
713
714llvm::Value* GBCExpanderPass::
715EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, llvm::Value* this_addr) {
716 // Load class object of *this* pointer
717 llvm::Value* class_object_addr =
718 irb_.LoadFromObjectOffset(this_addr,
TDYa127920be7c2012-09-10 17:13:22 -0700719 art::Object::ClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700720 irb_.getJObjectTy(),
721 kTBAAConstJObject);
722
723 // Load vtable address
724 llvm::Value* vtable_addr =
725 irb_.LoadFromObjectOffset(class_object_addr,
TDYa127920be7c2012-09-10 17:13:22 -0700726 art::Class::VTableOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700727 irb_.getJObjectTy(),
728 kTBAAConstJObject);
729
730 // Load callee method object
731 llvm::Value* vtable_idx_value =
732 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
733
734 llvm::Value* method_field_addr =
735 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
736
737 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
738}
739
740// Emit Array GetElementPtr
741llvm::Value* GBCExpanderPass::EmitArrayGEP(llvm::Value* array_addr,
742 llvm::Value* index_value,
743 JType elem_jty) {
744
745 int data_offset;
746 if (elem_jty == kLong || elem_jty == kDouble ||
TDYa127920be7c2012-09-10 17:13:22 -0700747 (elem_jty == kObject && sizeof(uint64_t) == sizeof(art::Object*))) {
748 data_offset = art::Array::DataOffset(sizeof(int64_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700749 } else {
TDYa127920be7c2012-09-10 17:13:22 -0700750 data_offset = art::Array::DataOffset(sizeof(int32_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700751 }
752
753 llvm::Constant* data_offset_value =
754 irb_.getPtrEquivInt(data_offset);
755
756 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
757
758 llvm::Value* array_data_addr =
759 irb_.CreatePtrDisp(array_addr, data_offset_value,
760 elem_type->getPointerTo());
761
762 return irb_.CreateGEP(array_data_addr, index_value);
763}
764
765void GBCExpanderPass::Expand_TestSuspend(llvm::CallInst& call_inst) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800766 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
767
768 llvm::Value* suspend_count =
769 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::ThreadFlagsOffset().Int32Value(),
770 irb_.getInt16Ty(),
771 kTBAARuntimeInfo);
772 llvm::Value* is_suspend = irb_.CreateICmpNE(suspend_count, irb_.getInt16(0));
773
774 llvm::BasicBlock* basic_block_suspend = CreateBasicBlockWithDexPC(dex_pc, "suspend");
775 llvm::BasicBlock* basic_block_cont = CreateBasicBlockWithDexPC(dex_pc, "suspend_cont");
776
777 irb_.CreateCondBr(is_suspend, basic_block_suspend, basic_block_cont, kUnlikely);
778
779 irb_.SetInsertPoint(basic_block_suspend);
780 if (dex_pc != art::DexFile::kDexNoIndex) {
781 EmitUpdateDexPC(dex_pc);
782 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700783 irb_.Runtime().EmitTestSuspend();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800784 irb_.CreateBr(basic_block_cont);
785
786 irb_.SetInsertPoint(basic_block_cont);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700787 return;
788}
789
TDYa1279a129452012-07-19 03:10:08 -0700790void GBCExpanderPass::Expand_MarkGCCard(llvm::CallInst& call_inst) {
TDYa1279a129452012-07-19 03:10:08 -0700791 irb_.Runtime().EmitMarkGCCard(call_inst.getArgOperand(0), call_inst.getArgOperand(1));
TDYa1279a129452012-07-19 03:10:08 -0700792 return;
793}
794
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700795llvm::Value*
796GBCExpanderPass::Expand_LoadStringFromDexCache(llvm::Value* string_idx_value) {
797 uint32_t string_idx =
798 llvm::cast<llvm::ConstantInt>(string_idx_value)->getZExtValue();
799
800 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
801
TDYa127ce4cc0d2012-11-18 16:59:53 -0800802 return irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700803}
804
805llvm::Value*
806GBCExpanderPass::Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value) {
807 uint32_t type_idx =
808 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
809
810 llvm::Value* type_field_addr =
811 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
812
TDYa127ce4cc0d2012-11-18 16:59:53 -0800813 return irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700814}
815
816void GBCExpanderPass::Expand_LockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700817 rtb_.EmitLockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700818 return;
819}
820
821void GBCExpanderPass::Expand_UnlockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700822 rtb_.EmitUnlockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700823 return;
824}
825
826llvm::Value* GBCExpanderPass::Expand_ArrayGet(llvm::Value* array_addr,
827 llvm::Value* index_value,
828 JType elem_jty) {
829 llvm::Value* array_elem_addr =
830 EmitArrayGEP(array_addr, index_value, elem_jty);
831
832 return irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
833}
834
835void GBCExpanderPass::Expand_ArrayPut(llvm::Value* new_value,
836 llvm::Value* array_addr,
837 llvm::Value* index_value,
838 JType elem_jty) {
839 llvm::Value* array_elem_addr =
840 EmitArrayGEP(array_addr, index_value, elem_jty);
841
842 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
843
844 return;
845}
846
847void GBCExpanderPass::Expand_FilledNewArray(llvm::CallInst& call_inst) {
848 // Most of the codes refer to MethodCompiler::EmitInsn_FilledNewArray
849 llvm::Value* array = call_inst.getArgOperand(0);
850
851 uint32_t element_jty =
852 llvm::cast<llvm::ConstantInt>(call_inst.getArgOperand(1))->getZExtValue();
853
854 DCHECK(call_inst.getNumArgOperands() > 2);
855 unsigned num_elements = (call_inst.getNumArgOperands() - 2);
856
857 bool is_elem_int_ty = (static_cast<JType>(element_jty) == kInt);
858
859 uint32_t alignment;
860 llvm::Constant* elem_size;
861 llvm::PointerType* field_type;
862
863 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
864 // as the element, thus we are only checking 2 cases: primitive int and
865 // non-primitive type.
866 if (is_elem_int_ty) {
867 alignment = sizeof(int32_t);
868 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
869 field_type = irb_.getJIntTy()->getPointerTo();
870 } else {
871 alignment = irb_.getSizeOfPtrEquivInt();
872 elem_size = irb_.getSizeOfPtrEquivIntValue();
873 field_type = irb_.getJObjectTy()->getPointerTo();
874 }
875
876 llvm::Value* data_field_offset =
TDYa127920be7c2012-09-10 17:13:22 -0700877 irb_.getPtrEquivInt(art::Array::DataOffset(alignment).Int32Value());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700878
879 llvm::Value* data_field_addr =
880 irb_.CreatePtrDisp(array, data_field_offset, field_type);
881
882 for (unsigned i = 0; i < num_elements; ++i) {
883 // Values to fill the array begin at the 3rd argument
884 llvm::Value* reg_value = call_inst.getArgOperand(2 + i);
885
886 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
887
888 data_field_addr =
889 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
890 }
891
892 return;
893}
894
895llvm::Value* GBCExpanderPass::Expand_IGetFast(llvm::Value* field_offset_value,
896 llvm::Value* /*is_volatile_value*/,
897 llvm::Value* object_addr,
898 JType field_jty) {
899 int field_offset =
900 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
901
902 DCHECK_GE(field_offset, 0);
903
904 llvm::PointerType* field_type =
905 irb_.getJType(field_jty, kField)->getPointerTo();
906
907 field_offset_value = irb_.getPtrEquivInt(field_offset);
908
909 llvm::Value* field_addr =
910 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
911
912 // TODO: Check is_volatile. We need to generate atomic load instruction
913 // when is_volatile is true.
914 return irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
915}
916
917void GBCExpanderPass::Expand_IPutFast(llvm::Value* field_offset_value,
918 llvm::Value* /* is_volatile_value */,
919 llvm::Value* object_addr,
920 llvm::Value* new_value,
921 JType field_jty) {
922 int field_offset =
923 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
924
925 DCHECK_GE(field_offset, 0);
926
927 llvm::PointerType* field_type =
928 irb_.getJType(field_jty, kField)->getPointerTo();
929
930 field_offset_value = irb_.getPtrEquivInt(field_offset);
931
932 llvm::Value* field_addr =
933 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
934
935 // TODO: Check is_volatile. We need to generate atomic store instruction
936 // when is_volatile is true.
937 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
938
939 return;
940}
941
942llvm::Value* GBCExpanderPass::Expand_SGetFast(llvm::Value* static_storage_addr,
943 llvm::Value* field_offset_value,
944 llvm::Value* /*is_volatile_value*/,
945 JType field_jty) {
946 int field_offset =
947 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
948
949 DCHECK_GE(field_offset, 0);
950
951 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
952
953 llvm::Value* static_field_addr =
954 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
955 irb_.getJType(field_jty, kField)->getPointerTo());
956
957 // TODO: Check is_volatile. We need to generate atomic store instruction
958 // when is_volatile is true.
959 return irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
960}
961
962void GBCExpanderPass::Expand_SPutFast(llvm::Value* static_storage_addr,
963 llvm::Value* field_offset_value,
964 llvm::Value* /* is_volatile_value */,
965 llvm::Value* new_value,
966 JType field_jty) {
967 int field_offset =
968 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
969
970 DCHECK_GE(field_offset, 0);
971
972 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
973
974 llvm::Value* static_field_addr =
975 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
976 irb_.getJType(field_jty, kField)->getPointerTo());
977
978 // TODO: Check is_volatile. We need to generate atomic store instruction
979 // when is_volatile is true.
980 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
981
982 return;
983}
984
985llvm::Value*
986GBCExpanderPass::Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr) {
987 return irb_.LoadFromObjectOffset(method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700988 art::AbstractMethod::DeclaringClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700989 irb_.getJObjectTy(),
990 kTBAAConstJObject);
991}
992
993llvm::Value*
994GBCExpanderPass::Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value) {
995 uint32_t type_idx =
996 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
997
998 llvm::Value* storage_field_addr =
999 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1000
TDYa127ce4cc0d2012-11-18 16:59:53 -08001001 return irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001002}
1003
1004llvm::Value*
1005GBCExpanderPass::Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value) {
1006 uint32_t callee_method_idx =
1007 llvm::cast<llvm::ConstantInt>(callee_method_idx_value)->getZExtValue();
1008
1009 return EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
1010}
1011
1012llvm::Value* GBCExpanderPass::Expand_GetVirtualCalleeMethodObjAddrFast(
1013 llvm::Value* vtable_idx_value,
1014 llvm::Value* this_addr) {
1015 int vtable_idx =
1016 llvm::cast<llvm::ConstantInt>(vtable_idx_value)->getSExtValue();
1017
1018 return EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
1019}
1020
1021llvm::Value* GBCExpanderPass::Expand_Invoke(llvm::CallInst& call_inst) {
1022 // Most of the codes refer to MethodCompiler::EmitInsn_Invoke
1023 llvm::Value* callee_method_object_addr = call_inst.getArgOperand(0);
1024 unsigned num_args = call_inst.getNumArgOperands();
1025 llvm::Type* ret_type = call_inst.getType();
1026
1027 // Determine the function type of the callee method
1028 std::vector<llvm::Type*> args_type;
1029 std::vector<llvm::Value*> args;
1030 for (unsigned i = 0; i < num_args; i++) {
1031 args.push_back(call_inst.getArgOperand(i));
1032 args_type.push_back(args[i]->getType());
1033 }
1034
1035 llvm::FunctionType* callee_method_type =
1036 llvm::FunctionType::get(ret_type, args_type, false);
1037
1038 llvm::Value* code_addr =
1039 irb_.LoadFromObjectOffset(callee_method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001040 art::AbstractMethod::GetCodeOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001041 callee_method_type->getPointerTo(),
TDYa127ce4cc0d2012-11-18 16:59:53 -08001042 kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001043
1044 // Invoke callee
1045 llvm::Value* retval = irb_.CreateCall(code_addr, args);
1046
1047 return retval;
1048}
1049
TDYa1274ec8ccd2012-08-11 07:04:57 -07001050llvm::Value* GBCExpanderPass::Expand_DivRem(llvm::CallInst& call_inst,
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001051 bool is_div, JType op_jty) {
TDYa1274ec8ccd2012-08-11 07:04:57 -07001052 llvm::Value* dividend = call_inst.getArgOperand(0);
1053 llvm::Value* divisor = call_inst.getArgOperand(1);
buzbeec531cef2012-10-18 07:09:20 -07001054#if defined(ART_USE_PORTABLE_COMPILER)
TDYa1274ec8ccd2012-08-11 07:04:57 -07001055 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1056 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
1057#endif
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001058 // Most of the codes refer to MethodCompiler::EmitIntDivRemResultComputation
1059
1060 // Check the special case: MININT / -1 = MININT
1061 // That case will cause overflow, which is undefined behavior in llvm.
1062 // So we check the divisor is -1 or not, if the divisor is -1, we do
1063 // the special path to avoid undefined behavior.
1064 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
1065 llvm::Value* zero = irb_.getJZero(op_jty);
1066 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
1067
TDYa1275e869b62012-07-25 00:45:39 -07001068 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001069 llvm::BasicBlock* eq_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1070 llvm::BasicBlock* ne_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1071 llvm::BasicBlock* neg_one_cont =
1072 llvm::BasicBlock::Create(context_, "", parent);
1073
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001074 llvm::Value* is_equal_neg_one = irb_.CreateICmpEQ(divisor, neg_one);
1075 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
1076
1077 // If divisor == -1
1078 irb_.SetInsertPoint(eq_neg_one);
1079 llvm::Value* eq_result;
1080 if (is_div) {
1081 // We can just change from "dividend div -1" to "neg dividend". The sub
1082 // don't care the sign/unsigned because of two's complement representation.
1083 // And the behavior is what we want:
1084 // -(2^n) (2^n)-1
1085 // MININT < k <= MAXINT -> mul k -1 = -k
1086 // MININT == k -> mul k -1 = k
1087 //
1088 // LLVM use sub to represent 'neg'
1089 eq_result = irb_.CreateSub(zero, dividend);
1090 } else {
1091 // Everything modulo -1 will be 0.
1092 eq_result = zero;
1093 }
1094 irb_.CreateBr(neg_one_cont);
1095
1096 // If divisor != -1, just do the division.
1097 irb_.SetInsertPoint(ne_neg_one);
1098 llvm::Value* ne_result;
1099 if (is_div) {
1100 ne_result = irb_.CreateSDiv(dividend, divisor);
1101 } else {
1102 ne_result = irb_.CreateSRem(dividend, divisor);
1103 }
1104 irb_.CreateBr(neg_one_cont);
1105
1106 irb_.SetInsertPoint(neg_one_cont);
1107 llvm::PHINode* result = irb_.CreatePHI(op_type, 2);
1108 result->addIncoming(eq_result, eq_neg_one);
1109 result->addIncoming(ne_result, ne_neg_one);
1110
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001111 return result;
1112}
1113
TDYa127ce4cc0d2012-11-18 16:59:53 -08001114void GBCExpanderPass::Expand_AllocaShadowFrame(llvm::Value* num_vregs_value) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001115 // Most of the codes refer to MethodCompiler::EmitPrologueAllocShadowFrame and
1116 // MethodCompiler::EmitPushShadowFrame
TDYa1278e950c12012-11-02 09:58:19 -07001117 uint16_t num_vregs =
1118 llvm::cast<llvm::ConstantInt>(num_vregs_value)->getZExtValue();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001119
1120 llvm::StructType* shadow_frame_type =
TDYa127ce4cc0d2012-11-18 16:59:53 -08001121 irb_.getShadowFrameTy(num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001122
1123 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
1124
1125 // Alloca a pointer to old shadow frame
1126 old_shadow_frame_ =
1127 irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
1128
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001129 // Push the shadow frame
1130 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1131
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001132 llvm::Value* shadow_frame_upcast =
1133 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
1134
1135 llvm::Value* result = rtb_.EmitPushShadowFrame(shadow_frame_upcast,
1136 method_object_addr,
TDYa1278e950c12012-11-02 09:58:19 -07001137 num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001138
1139 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
1140
1141 return;
1142}
1143
TDYa1278e950c12012-11-02 09:58:19 -07001144void GBCExpanderPass::Expand_SetVReg(llvm::Value* entry_idx,
1145 llvm::Value* value) {
1146 DCHECK(shadow_frame_ != NULL);
1147
1148 llvm::Value* gep_index[] = {
1149 irb_.getInt32(0), // No pointer displacement
TDYa127ce4cc0d2012-11-18 16:59:53 -08001150 irb_.getInt32(1), // VRegs
TDYa1278e950c12012-11-02 09:58:19 -07001151 entry_idx // Pointer field
1152 };
1153
1154 llvm::Value* vreg_addr = irb_.CreateGEP(shadow_frame_, gep_index);
1155
1156 irb_.CreateStore(value,
1157 irb_.CreateBitCast(vreg_addr, value->getType()->getPointerTo()),
1158 kTBAAShadowFrame);
1159 return;
1160}
1161
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001162void GBCExpanderPass::Expand_PopShadowFrame() {
buzbeec531cef2012-10-18 07:09:20 -07001163#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001164 if (old_shadow_frame_ == NULL) {
1165 return;
1166 }
1167#endif
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001168 rtb_.EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
1169 return;
1170}
1171
1172void GBCExpanderPass::Expand_UpdateDexPC(llvm::Value* dex_pc_value) {
1173 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07001174 art::ShadowFrame::DexPCOffset(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001175 dex_pc_value,
1176 kTBAAShadowFrame);
1177 return;
1178}
1179
Logan Chien67645d82012-08-17 09:10:54 +08001180void GBCExpanderPass::InsertStackOverflowCheck(llvm::Function& func) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001181 // DexLang generates all alloca instruction in the first basic block of the
1182 // FUNC and also there's no any alloca instructions after the first non-alloca
1183 // instruction
1184
Logan Chien67645d82012-08-17 09:10:54 +08001185 llvm::BasicBlock* first_basic_block = &func.front();
1186
1187 // Look for first non-alloca instruction
1188 llvm::BasicBlock::iterator first_non_alloca = first_basic_block->begin();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001189 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1190 ++first_non_alloca;
1191 }
1192
Logan Chien67645d82012-08-17 09:10:54 +08001193 irb_.SetInsertPoint(first_non_alloca);
1194
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001195 // Insert stack overflow check codes before first_non_alloca (i.e., after all
1196 // alloca instructions)
Logan Chien67645d82012-08-17 09:10:54 +08001197 EmitStackOverflowCheck(&*first_non_alloca);
1198
buzbeec531cef2012-10-18 07:09:20 -07001199#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127890ea892012-08-22 10:49:42 -07001200 irb_.Runtime().EmitTestSuspend();
1201#endif
1202
Logan Chien67645d82012-08-17 09:10:54 +08001203 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
1204 if (next_basic_block != first_basic_block) {
1205 // Splice the rest of the instruction to the continuing basic block
1206 next_basic_block->getInstList().splice(
1207 irb_.GetInsertPoint(), first_basic_block->getInstList(),
1208 first_non_alloca, first_basic_block->end());
1209
1210 // Rewrite the basic block
1211 RewriteBasicBlock(next_basic_block);
1212
1213 // Update the phi-instructions in the successor basic block
1214 UpdatePhiInstruction(first_basic_block, irb_.GetInsertBlock());
1215 }
1216
1217 // We have changed the basic block
1218 changed_ = true;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001219}
1220
TDYa1275e869b62012-07-25 00:45:39 -07001221// ==== High-level intrinsic expander ==========================================
1222
TDYa127a1b21852012-07-23 03:20:39 -07001223llvm::Value* GBCExpanderPass::Expand_FPCompare(llvm::Value* src1_value,
1224 llvm::Value* src2_value,
1225 bool gt_bias) {
1226 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1227 llvm::Value* cmp_lt;
1228
1229 if (gt_bias) {
1230 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1231 } else {
1232 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1233 }
1234
1235 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1236}
1237
1238llvm::Value* GBCExpanderPass::Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value) {
1239 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1240 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1241
1242 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1243}
1244
1245llvm::Value* GBCExpanderPass::EmitCompareResultSelection(llvm::Value* cmp_eq,
1246 llvm::Value* cmp_lt) {
1247
1248 llvm::Constant* zero = irb_.getJInt(0);
1249 llvm::Constant* pos1 = irb_.getJInt(1);
1250 llvm::Constant* neg1 = irb_.getJInt(-1);
1251
1252 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1253 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1254
1255 return result_eq;
1256}
1257
Logan Chien75e4b602012-07-23 14:24:12 -07001258llvm::Value* GBCExpanderPass::Expand_IntegerShift(llvm::Value* src1_value,
1259 llvm::Value* src2_value,
1260 IntegerShiftKind kind,
1261 JType op_jty) {
1262 DCHECK(op_jty == kInt || op_jty == kLong);
1263
1264 // Mask and zero-extend RHS properly
1265 if (op_jty == kInt) {
1266 src2_value = irb_.CreateAnd(src2_value, 0x1f);
1267 } else {
1268 llvm::Value* masked_src2_value = irb_.CreateAnd(src2_value, 0x3f);
1269 src2_value = irb_.CreateZExt(masked_src2_value, irb_.getJLongTy());
1270 }
1271
1272 // Create integer shift llvm instruction
1273 switch (kind) {
1274 case kIntegerSHL:
1275 return irb_.CreateShl(src1_value, src2_value);
1276
1277 case kIntegerSHR:
1278 return irb_.CreateAShr(src1_value, src2_value);
1279
1280 case kIntegerUSHR:
1281 return irb_.CreateLShr(src1_value, src2_value);
1282
1283 default:
1284 LOG(FATAL) << "Unknown integer shift kind: " << kind;
1285 return NULL;
1286 }
1287}
1288
TDYa1275a26d442012-07-26 18:58:38 -07001289llvm::Value* GBCExpanderPass::Expand_HLArrayGet(llvm::CallInst& call_inst,
1290 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001291 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1292 llvm::Value* array_addr = call_inst.getArgOperand(1);
1293 llvm::Value* index_value = call_inst.getArgOperand(2);
TDYa127920be7c2012-09-10 17:13:22 -07001294 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001295
TDYa127920be7c2012-09-10 17:13:22 -07001296 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1297 EmitGuard_NullPointerException(dex_pc, array_addr);
1298 }
1299 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
1300 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value);
1301 }
TDYa1275a26d442012-07-26 18:58:38 -07001302
1303 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1304
1305 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
1306
1307 switch (elem_jty) {
1308 case kVoid:
1309 break;
1310
1311 case kBoolean:
1312 case kChar:
1313 array_elem_value = irb_.CreateZExt(array_elem_value, irb_.getJType(elem_jty, kReg));
1314 break;
1315
1316 case kByte:
1317 case kShort:
1318 array_elem_value = irb_.CreateSExt(array_elem_value, irb_.getJType(elem_jty, kReg));
1319 break;
1320
1321 case kInt:
1322 case kLong:
1323 case kFloat:
1324 case kDouble:
1325 case kObject:
1326 break;
1327
1328 default:
1329 LOG(FATAL) << "Unknown java type: " << elem_jty;
1330 }
1331
1332 return array_elem_value;
1333}
1334
1335
1336void GBCExpanderPass::Expand_HLArrayPut(llvm::CallInst& call_inst,
1337 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001338 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1339 llvm::Value* new_value = call_inst.getArgOperand(1);
1340 llvm::Value* array_addr = call_inst.getArgOperand(2);
1341 llvm::Value* index_value = call_inst.getArgOperand(3);
TDYa127920be7c2012-09-10 17:13:22 -07001342 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001343
TDYa127920be7c2012-09-10 17:13:22 -07001344 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1345 EmitGuard_NullPointerException(dex_pc, array_addr);
1346 }
1347 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
1348 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value);
1349 }
TDYa1275a26d442012-07-26 18:58:38 -07001350
1351 switch (elem_jty) {
1352 case kVoid:
1353 break;
1354
1355 case kBoolean:
1356 case kChar:
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001357 case kByte:
1358 case kShort:
TDYa1275a26d442012-07-26 18:58:38 -07001359 new_value = irb_.CreateTrunc(new_value, irb_.getJType(elem_jty, kArray));
1360 break;
1361
1362 case kInt:
1363 case kLong:
1364 case kFloat:
1365 case kDouble:
1366 case kObject:
1367 break;
1368
1369 default:
1370 LOG(FATAL) << "Unknown java type: " << elem_jty;
1371 }
1372
1373 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1374
1375 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
1376 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::CheckPutArrayElement);
1377
1378 irb_.CreateCall2(runtime_func, new_value, array_addr);
1379
1380 EmitGuard_ExceptionLandingPad(dex_pc);
1381
1382 EmitMarkGCCard(new_value, array_addr);
1383 }
1384
1385 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1386
1387 return;
1388}
1389
TDYa1275e869b62012-07-25 00:45:39 -07001390llvm::Value* GBCExpanderPass::Expand_HLIGet(llvm::CallInst& call_inst,
1391 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001392 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1393 llvm::Value* object_addr = call_inst.getArgOperand(1);
1394 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(2));
TDYa127920be7c2012-09-10 17:13:22 -07001395 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001396
TDYa127920be7c2012-09-10 17:13:22 -07001397 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1398 EmitGuard_NullPointerException(dex_pc, object_addr);
1399 }
TDYa1275e869b62012-07-25 00:45:39 -07001400
1401 llvm::Value* field_value;
1402
1403 int field_offset;
1404 bool is_volatile;
1405 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
1406 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
1407
1408 if (!is_fast_path) {
1409 llvm::Function* runtime_func;
1410
1411 if (field_jty == kObject) {
1412 runtime_func = irb_.GetRuntime(runtime_support::GetObjectInstance);
1413 } else if (field_jty == kLong || field_jty == kDouble) {
1414 runtime_func = irb_.GetRuntime(runtime_support::Get64Instance);
1415 } else {
1416 runtime_func = irb_.GetRuntime(runtime_support::Get32Instance);
1417 }
1418
1419 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
1420
1421 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1422
1423 EmitUpdateDexPC(dex_pc);
1424
1425 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
1426 method_object_addr, object_addr);
1427
1428 EmitGuard_ExceptionLandingPad(dex_pc);
1429
1430 } else {
1431 DCHECK_GE(field_offset, 0);
1432
1433 llvm::PointerType* field_type =
1434 irb_.getJType(field_jty, kField)->getPointerTo();
1435
1436 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
1437
1438 llvm::Value* field_addr =
1439 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1440
1441 // TODO: Check is_volatile. We need to generate atomic load instruction
1442 // when is_volatile is true.
1443 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
1444 }
1445
1446 if (field_jty == kFloat || field_jty == kDouble) {
TDYa1275a26d442012-07-26 18:58:38 -07001447 field_value = irb_.CreateBitCast(field_value, irb_.getJType(field_jty, kAccurate));
TDYa1275e869b62012-07-25 00:45:39 -07001448 }
1449
1450 return field_value;
1451}
1452
1453void GBCExpanderPass::Expand_HLIPut(llvm::CallInst& call_inst,
1454 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001455 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001456 llvm::Value* new_value = call_inst.getArgOperand(1);
1457 llvm::Value* object_addr = call_inst.getArgOperand(2);
TDYa1275e869b62012-07-25 00:45:39 -07001458 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(3));
TDYa127920be7c2012-09-10 17:13:22 -07001459 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001460
1461 if (field_jty == kFloat || field_jty == kDouble) {
TDYa1275a26d442012-07-26 18:58:38 -07001462 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty, kField));
TDYa1275e869b62012-07-25 00:45:39 -07001463 }
1464
TDYa127920be7c2012-09-10 17:13:22 -07001465 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1466 EmitGuard_NullPointerException(dex_pc, object_addr);
1467 }
TDYa1275e869b62012-07-25 00:45:39 -07001468
1469 int field_offset;
1470 bool is_volatile;
1471 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
1472 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
1473
1474 if (!is_fast_path) {
1475 llvm::Function* runtime_func;
1476
1477 if (field_jty == kObject) {
1478 runtime_func = irb_.GetRuntime(runtime_support::SetObjectInstance);
1479 } else if (field_jty == kLong || field_jty == kDouble) {
1480 runtime_func = irb_.GetRuntime(runtime_support::Set64Instance);
1481 } else {
1482 runtime_func = irb_.GetRuntime(runtime_support::Set32Instance);
1483 }
1484
1485 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1486
1487 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1488
1489 EmitUpdateDexPC(dex_pc);
1490
1491 irb_.CreateCall4(runtime_func, field_idx_value,
1492 method_object_addr, object_addr, new_value);
1493
1494 EmitGuard_ExceptionLandingPad(dex_pc);
1495
1496 } else {
1497 DCHECK_GE(field_offset, 0);
1498
1499 llvm::PointerType* field_type =
1500 irb_.getJType(field_jty, kField)->getPointerTo();
1501
1502 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
1503
1504 llvm::Value* field_addr =
1505 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1506
1507 // TODO: Check is_volatile. We need to generate atomic store instruction
1508 // when is_volatile is true.
1509 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1510
1511 if (field_jty == kObject) { // If put an object, mark the GC card table.
1512 EmitMarkGCCard(new_value, object_addr);
1513 }
1514 }
1515
1516 return;
1517}
1518
TDYa127f71bf5a2012-07-29 20:09:52 -07001519llvm::Value* GBCExpanderPass::EmitLoadConstantClass(uint32_t dex_pc,
1520 uint32_t type_idx) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001521 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, *dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001522 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1523
1524 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1525
1526 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1527
1528 llvm::Function* runtime_func =
1529 irb_.GetRuntime(runtime_support::InitializeTypeAndVerifyAccess);
1530
1531 EmitUpdateDexPC(dex_pc);
1532
1533 llvm::Value* type_object_addr =
1534 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1535
1536 EmitGuard_ExceptionLandingPad(dex_pc);
1537
1538 return type_object_addr;
1539
1540 } else {
1541 // Try to load the class (type) object from the test cache.
1542 llvm::Value* type_field_addr =
1543 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1544
TDYa127ce4cc0d2012-11-18 16:59:53 -08001545 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07001546
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001547 if (compiler_->CanAssumeTypeIsPresentInDexCache(*dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001548 return type_object_addr;
1549 }
1550
1551 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1552
1553 // Test whether class (type) object is in the dex cache or not
1554 llvm::Value* equal_null =
1555 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1556
1557 llvm::BasicBlock* block_cont =
1558 CreateBasicBlockWithDexPC(dex_pc, "cont");
1559
1560 llvm::BasicBlock* block_load_class =
1561 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1562
1563 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
1564
1565 // Failback routine to load the class object
1566 irb_.SetInsertPoint(block_load_class);
1567
1568 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeType);
1569
1570 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1571
1572 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1573
1574 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1575
1576 EmitUpdateDexPC(dex_pc);
1577
1578 llvm::Value* loaded_type_object_addr =
1579 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1580
1581 EmitGuard_ExceptionLandingPad(dex_pc);
1582
1583 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1584
1585 irb_.CreateBr(block_cont);
1586
1587 // Now the class object must be loaded
1588 irb_.SetInsertPoint(block_cont);
1589
1590 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1591
1592 phi->addIncoming(type_object_addr, block_original);
1593 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1594
1595 return phi;
1596 }
1597}
1598
TDYa1275a26d442012-07-26 18:58:38 -07001599llvm::Value* GBCExpanderPass::EmitLoadStaticStorage(uint32_t dex_pc,
1600 uint32_t type_idx) {
1601 llvm::BasicBlock* block_load_static =
1602 CreateBasicBlockWithDexPC(dex_pc, "load_static");
1603
1604 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1605
1606 // Load static storage from dex cache
1607 llvm::Value* storage_field_addr =
1608 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1609
TDYa127ce4cc0d2012-11-18 16:59:53 -08001610 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
TDYa1275a26d442012-07-26 18:58:38 -07001611
1612 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1613
1614 // Test: Is the static storage of this class initialized?
1615 llvm::Value* equal_null =
1616 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
1617
1618 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
1619
1620 // Failback routine to load the class object
1621 irb_.SetInsertPoint(block_load_static);
1622
1623 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeStaticStorage);
1624
1625 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1626
1627 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1628
1629 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1630
1631 EmitUpdateDexPC(dex_pc);
1632
1633 llvm::Value* loaded_storage_object_addr =
1634 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1635
1636 EmitGuard_ExceptionLandingPad(dex_pc);
1637
1638 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
1639
1640 irb_.CreateBr(block_cont);
1641
1642 // Now the class object must be loaded
1643 irb_.SetInsertPoint(block_cont);
1644
1645 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1646
1647 phi->addIncoming(storage_object_addr, block_original);
1648 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
1649
1650 return phi;
1651}
1652
1653llvm::Value* GBCExpanderPass::Expand_HLSget(llvm::CallInst& call_inst,
1654 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001655 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1656 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1657
1658 int field_offset;
1659 int ssb_index;
1660 bool is_referrers_class;
1661 bool is_volatile;
1662
1663 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
1664 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1665 is_referrers_class, is_volatile, false);
1666
1667 llvm::Value* static_field_value;
1668
1669 if (!is_fast_path) {
1670 llvm::Function* runtime_func;
1671
1672 if (field_jty == kObject) {
1673 runtime_func = irb_.GetRuntime(runtime_support::GetObjectStatic);
1674 } else if (field_jty == kLong || field_jty == kDouble) {
1675 runtime_func = irb_.GetRuntime(runtime_support::Get64Static);
1676 } else {
1677 runtime_func = irb_.GetRuntime(runtime_support::Get32Static);
1678 }
1679
1680 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1681
1682 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1683
1684 EmitUpdateDexPC(dex_pc);
1685
1686 static_field_value =
1687 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
1688
1689 EmitGuard_ExceptionLandingPad(dex_pc);
1690
1691 } else {
1692 DCHECK_GE(field_offset, 0);
1693
1694 llvm::Value* static_storage_addr = NULL;
1695
1696 if (is_referrers_class) {
1697 // Fast path, static storage base is this method's class
1698 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1699
1700 static_storage_addr =
1701 irb_.LoadFromObjectOffset(method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001702 art::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001703 irb_.getJObjectTy(),
1704 kTBAAConstJObject);
1705 } else {
1706 // Medium path, static storage base in a different class which
1707 // requires checks that the other class is initialized
1708 DCHECK_GE(ssb_index, 0);
1709 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1710 }
1711
1712 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1713
1714 llvm::Value* static_field_addr =
1715 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1716 irb_.getJType(field_jty, kField)->getPointerTo());
1717
1718 // TODO: Check is_volatile. We need to generate atomic load instruction
1719 // when is_volatile is true.
1720 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
1721 }
1722
1723 if (field_jty == kFloat || field_jty == kDouble) {
1724 static_field_value =
1725 irb_.CreateBitCast(static_field_value, irb_.getJType(field_jty, kAccurate));
1726 }
1727
1728 return static_field_value;
1729}
1730
1731void GBCExpanderPass::Expand_HLSput(llvm::CallInst& call_inst,
1732 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001733 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1734 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1735 llvm::Value* new_value = call_inst.getArgOperand(1);
1736
1737 if (field_jty == kFloat || field_jty == kDouble) {
1738 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty, kField));
1739 }
1740
1741 int field_offset;
1742 int ssb_index;
1743 bool is_referrers_class;
1744 bool is_volatile;
1745
1746 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
1747 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1748 is_referrers_class, is_volatile, true);
1749
1750 if (!is_fast_path) {
1751 llvm::Function* runtime_func;
1752
1753 if (field_jty == kObject) {
1754 runtime_func = irb_.GetRuntime(runtime_support::SetObjectStatic);
1755 } else if (field_jty == kLong || field_jty == kDouble) {
1756 runtime_func = irb_.GetRuntime(runtime_support::Set64Static);
1757 } else {
1758 runtime_func = irb_.GetRuntime(runtime_support::Set32Static);
1759 }
1760
1761 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1762
1763 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1764
1765 EmitUpdateDexPC(dex_pc);
1766
1767 irb_.CreateCall3(runtime_func, field_idx_value,
1768 method_object_addr, new_value);
1769
1770 EmitGuard_ExceptionLandingPad(dex_pc);
1771
1772 } else {
1773 DCHECK_GE(field_offset, 0);
1774
1775 llvm::Value* static_storage_addr = NULL;
1776
1777 if (is_referrers_class) {
1778 // Fast path, static storage base is this method's class
1779 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1780
1781 static_storage_addr =
1782 irb_.LoadFromObjectOffset(method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001783 art::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001784 irb_.getJObjectTy(),
1785 kTBAAConstJObject);
1786 } else {
1787 // Medium path, static storage base in a different class which
1788 // requires checks that the other class is initialized
1789 DCHECK_GE(ssb_index, 0);
1790 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1791 }
1792
1793 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1794
1795 llvm::Value* static_field_addr =
1796 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1797 irb_.getJType(field_jty, kField)->getPointerTo());
1798
1799 // TODO: Check is_volatile. We need to generate atomic store instruction
1800 // when is_volatile is true.
1801 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1802
1803 if (field_jty == kObject) { // If put an object, mark the GC card table.
1804 EmitMarkGCCard(new_value, static_storage_addr);
1805 }
1806 }
1807
1808 return;
1809}
1810
TDYa127f71bf5a2012-07-29 20:09:52 -07001811llvm::Value* GBCExpanderPass::Expand_ConstString(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001812 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1813 uint32_t string_idx = LV2UInt(call_inst.getArgOperand(0));
1814
1815 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1816
TDYa127ce4cc0d2012-11-18 16:59:53 -08001817 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07001818
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001819 if (!compiler_->CanAssumeStringIsPresentInDexCache(*dex_file_, string_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001820 llvm::BasicBlock* block_str_exist =
1821 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1822
1823 llvm::BasicBlock* block_str_resolve =
1824 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1825
1826 llvm::BasicBlock* block_cont =
1827 CreateBasicBlockWithDexPC(dex_pc, "str_cont");
1828
1829 // Test: Is the string resolved and in the dex cache?
1830 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1831
1832 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
1833
1834 // String is resolved, go to next basic block.
1835 irb_.SetInsertPoint(block_str_exist);
1836 irb_.CreateBr(block_cont);
1837
1838 // String is not resolved yet, resolve it now.
1839 irb_.SetInsertPoint(block_str_resolve);
1840
1841 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::ResolveString);
1842
1843 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1844
1845 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1846
1847 EmitUpdateDexPC(dex_pc);
1848
1849 llvm::Value* result = irb_.CreateCall2(runtime_func, method_object_addr,
1850 string_idx_value);
1851
1852 EmitGuard_ExceptionLandingPad(dex_pc);
1853
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001854 irb_.CreateBr(block_cont);
1855
1856
TDYa127f71bf5a2012-07-29 20:09:52 -07001857 llvm::BasicBlock* block_pre_cont = irb_.GetInsertBlock();
1858
1859 irb_.SetInsertPoint(block_cont);
1860
1861 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1862
1863 phi->addIncoming(string_addr, block_str_exist);
1864 phi->addIncoming(result, block_pre_cont);
1865
1866 string_addr = phi;
1867 }
1868
1869 return string_addr;
1870}
1871
1872llvm::Value* GBCExpanderPass::Expand_ConstClass(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001873 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1874 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1875
1876 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
1877
1878 return type_object_addr;
1879}
1880
1881void GBCExpanderPass::Expand_MonitorEnter(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001882 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1883 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07001884 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07001885
TDYa127920be7c2012-09-10 17:13:22 -07001886 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1887 EmitGuard_NullPointerException(dex_pc, object_addr);
1888 }
TDYa127f71bf5a2012-07-29 20:09:52 -07001889
TDYa127ce4cc0d2012-11-18 16:59:53 -08001890 EmitUpdateDexPC(dex_pc);
1891
TDYa127f71bf5a2012-07-29 20:09:52 -07001892 irb_.Runtime().EmitLockObject(object_addr);
1893
1894 return;
1895}
1896
1897void GBCExpanderPass::Expand_MonitorExit(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001898 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1899 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07001900 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07001901
TDYa127920be7c2012-09-10 17:13:22 -07001902 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1903 EmitGuard_NullPointerException(dex_pc, object_addr);
1904 }
TDYa127f71bf5a2012-07-29 20:09:52 -07001905
1906 EmitUpdateDexPC(dex_pc);
1907
1908 irb_.Runtime().EmitUnlockObject(object_addr);
1909
1910 EmitGuard_ExceptionLandingPad(dex_pc);
1911
1912 return;
1913}
1914
1915void GBCExpanderPass::Expand_HLCheckCast(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001916 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1917 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1918 llvm::Value* object_addr = call_inst.getArgOperand(1);
1919
1920 llvm::BasicBlock* block_test_class =
1921 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1922
1923 llvm::BasicBlock* block_test_sub_class =
1924 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1925
1926 llvm::BasicBlock* block_cont =
1927 CreateBasicBlockWithDexPC(dex_pc, "checkcast_cont");
1928
1929 // Test: Is the reference equal to null? Act as no-op when it is null.
1930 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1931
1932 irb_.CreateCondBr(equal_null,
1933 block_cont,
1934 block_test_class);
1935
1936 // Test: Is the object instantiated from the given class?
1937 irb_.SetInsertPoint(block_test_class);
1938 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
TDYa127920be7c2012-09-10 17:13:22 -07001939 DCHECK_EQ(art::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07001940
1941 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1942
1943 llvm::Value* object_type_field_addr =
1944 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1945
1946 llvm::Value* object_type_object_addr =
1947 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
1948
1949 llvm::Value* equal_class =
1950 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1951
1952 irb_.CreateCondBr(equal_class,
1953 block_cont,
1954 block_test_sub_class);
1955
1956 // Test: Is the object instantiated from the subclass of the given class?
1957 irb_.SetInsertPoint(block_test_sub_class);
1958
1959 EmitUpdateDexPC(dex_pc);
1960
1961 irb_.CreateCall2(irb_.GetRuntime(runtime_support::CheckCast),
1962 type_object_addr, object_type_object_addr);
1963
1964 EmitGuard_ExceptionLandingPad(dex_pc);
1965
1966 irb_.CreateBr(block_cont);
1967
1968 irb_.SetInsertPoint(block_cont);
1969
1970 return;
1971}
1972
1973llvm::Value* GBCExpanderPass::Expand_InstanceOf(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001974 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1975 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1976 llvm::Value* object_addr = call_inst.getArgOperand(1);
1977
1978 llvm::BasicBlock* block_nullp =
1979 CreateBasicBlockWithDexPC(dex_pc, "nullp");
1980
1981 llvm::BasicBlock* block_test_class =
1982 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1983
1984 llvm::BasicBlock* block_class_equals =
1985 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1986
1987 llvm::BasicBlock* block_test_sub_class =
1988 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1989
1990 llvm::BasicBlock* block_cont =
1991 CreateBasicBlockWithDexPC(dex_pc, "instance_of_cont");
1992
1993 // Overview of the following code :
1994 // We check for null, if so, then false, otherwise check for class == . If so
1995 // then true, otherwise do callout slowpath.
1996 //
1997 // Test: Is the reference equal to null? Set 0 when it is null.
1998 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1999
2000 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
2001
2002 irb_.SetInsertPoint(block_nullp);
2003 irb_.CreateBr(block_cont);
2004
2005 // Test: Is the object instantiated from the given class?
2006 irb_.SetInsertPoint(block_test_class);
2007 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
TDYa127920be7c2012-09-10 17:13:22 -07002008 DCHECK_EQ(art::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002009
2010 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2011
2012 llvm::Value* object_type_field_addr =
2013 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2014
2015 llvm::Value* object_type_object_addr =
2016 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2017
2018 llvm::Value* equal_class =
2019 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2020
2021 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
2022
2023 irb_.SetInsertPoint(block_class_equals);
2024 irb_.CreateBr(block_cont);
2025
2026 // Test: Is the object instantiated from the subclass of the given class?
2027 irb_.SetInsertPoint(block_test_sub_class);
2028 llvm::Value* result =
2029 irb_.CreateCall2(irb_.GetRuntime(runtime_support::IsAssignable),
2030 type_object_addr, object_type_object_addr);
2031 irb_.CreateBr(block_cont);
2032
2033 irb_.SetInsertPoint(block_cont);
2034
2035 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJIntTy(), 3);
2036
2037 phi->addIncoming(irb_.getJInt(0), block_nullp);
2038 phi->addIncoming(irb_.getJInt(1), block_class_equals);
2039 phi->addIncoming(result, block_test_sub_class);
2040
2041 return phi;
2042}
2043
2044llvm::Value* GBCExpanderPass::Expand_NewInstance(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002045 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2046 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2047
2048 llvm::Function* runtime_func;
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002049 if (compiler_->CanAccessInstantiableTypeWithoutChecks(method_idx_, *dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002050 runtime_func = irb_.GetRuntime(runtime_support::AllocObject);
2051 } else {
2052 runtime_func = irb_.GetRuntime(runtime_support::AllocObjectWithAccessCheck);
2053 }
2054
2055 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2056
2057 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2058
2059 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2060
2061 EmitUpdateDexPC(dex_pc);
2062
2063 llvm::Value* object_addr =
2064 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
2065
2066 EmitGuard_ExceptionLandingPad(dex_pc);
2067
2068 return object_addr;
2069}
2070
2071llvm::Value* GBCExpanderPass::Expand_HLInvoke(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002072 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
TDYa127920be7c2012-09-10 17:13:22 -07002073 art::InvokeType invoke_type = static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
2074 bool is_static = (invoke_type == art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002075 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
TDYa127920be7c2012-09-10 17:13:22 -07002076 int opt_flags = LV2UInt(call_inst.getArgOperand(2));
TDYa127f71bf5a2012-07-29 20:09:52 -07002077
2078 // Compute invoke related information for compiler decision
2079 int vtable_idx = -1;
2080 uintptr_t direct_code = 0;
2081 uintptr_t direct_method = 0;
2082 bool is_fast_path = compiler_->
2083 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
2084 invoke_type, vtable_idx, direct_code, direct_method);
2085
2086 // Load *this* actual parameter
2087 llvm::Value* this_addr = NULL;
2088
2089 if (!is_static) {
2090 // Test: Is *this* parameter equal to null?
2091 this_addr = call_inst.getArgOperand(3);
2092 }
2093
2094 // Load the method object
2095 llvm::Value* callee_method_object_addr = NULL;
2096
2097 if (!is_fast_path) {
2098 callee_method_object_addr =
2099 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2100 this_addr, dex_pc, is_fast_path);
2101
TDYa127920be7c2012-09-10 17:13:22 -07002102 if (!is_static && !(opt_flags & MIR_IGNORE_NULL_CHECK)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002103 EmitGuard_NullPointerException(dex_pc, this_addr);
2104 }
2105 } else {
TDYa127920be7c2012-09-10 17:13:22 -07002106 if (!is_static && !(opt_flags & MIR_IGNORE_NULL_CHECK)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002107 EmitGuard_NullPointerException(dex_pc, this_addr);
2108 }
2109
2110 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002111 case art::kStatic:
2112 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002113 if (direct_method != 0u &&
2114 direct_method != static_cast<uintptr_t>(-1)) {
2115 callee_method_object_addr =
2116 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2117 irb_.getJObjectTy());
2118 } else {
2119 callee_method_object_addr =
2120 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2121 }
2122 break;
2123
TDYa127920be7c2012-09-10 17:13:22 -07002124 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002125 DCHECK(vtable_idx != -1);
2126 callee_method_object_addr =
2127 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2128 break;
2129
TDYa127920be7c2012-09-10 17:13:22 -07002130 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002131 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2132 "the fast path.";
2133 break;
2134
TDYa127920be7c2012-09-10 17:13:22 -07002135 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002136 callee_method_object_addr =
2137 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2138 invoke_type, this_addr,
2139 dex_pc, is_fast_path);
2140 break;
2141 }
2142 }
2143
2144 // Load the actual parameter
2145 std::vector<llvm::Value*> args;
2146
2147 args.push_back(callee_method_object_addr); // method object for callee
2148
2149 for (uint32_t i = 3; i < call_inst.getNumArgOperands(); ++i) {
2150 args.push_back(call_inst.getArgOperand(i));
2151 }
2152
2153 llvm::Value* code_addr;
2154 if (direct_code != 0u &&
2155 direct_code != static_cast<uintptr_t>(-1)) {
2156 code_addr =
2157 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_code),
2158 GetFunctionType(callee_method_idx, is_static)->getPointerTo());
2159 } else {
2160 code_addr =
2161 irb_.LoadFromObjectOffset(callee_method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -07002162 art::AbstractMethod::GetCodeOffset().Int32Value(),
TDYa127f71bf5a2012-07-29 20:09:52 -07002163 GetFunctionType(callee_method_idx, is_static)->getPointerTo(),
TDYa127ce4cc0d2012-11-18 16:59:53 -08002164 kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07002165 }
2166
2167 // Invoke callee
2168 EmitUpdateDexPC(dex_pc);
2169 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2170 EmitGuard_ExceptionLandingPad(dex_pc);
2171
2172 return retval;
2173}
2174
2175llvm::Value* GBCExpanderPass::Expand_OptArrayLength(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002176 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2177 // Get the array object address
2178 llvm::Value* array_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002179 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002180
TDYa127920be7c2012-09-10 17:13:22 -07002181 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
2182 EmitGuard_NullPointerException(dex_pc, array_addr);
2183 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002184
2185 // Get the array length and store it to the register
2186 return EmitLoadArrayLength(array_addr);
2187}
2188
2189llvm::Value* GBCExpanderPass::Expand_NewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002190 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2191 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2192 llvm::Value* length = call_inst.getArgOperand(1);
2193
2194 return EmitAllocNewArray(dex_pc, length, type_idx, false);
2195}
2196
2197llvm::Value* GBCExpanderPass::Expand_HLFilledNewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002198 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2199 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(1));
2200 uint32_t length = call_inst.getNumArgOperands() - 3;
2201
2202 llvm::Value* object_addr =
2203 EmitAllocNewArray(dex_pc, irb_.getInt32(length), type_idx, true);
2204
2205 if (length > 0) {
2206 // Check for the element type
2207 uint32_t type_desc_len = 0;
2208 const char* type_desc =
2209 dex_file_->StringByTypeIdx(type_idx, &type_desc_len);
2210
2211 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
2212 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
2213 bool is_elem_int_ty = (type_desc[1] == 'I');
2214
2215 uint32_t alignment;
2216 llvm::Constant* elem_size;
2217 llvm::PointerType* field_type;
2218
2219 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
2220 // as the element, thus we are only checking 2 cases: primitive int and
2221 // non-primitive type.
2222 if (is_elem_int_ty) {
2223 alignment = sizeof(int32_t);
2224 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
2225 field_type = irb_.getJIntTy()->getPointerTo();
2226 } else {
2227 alignment = irb_.getSizeOfPtrEquivInt();
2228 elem_size = irb_.getSizeOfPtrEquivIntValue();
2229 field_type = irb_.getJObjectTy()->getPointerTo();
2230 }
2231
2232 llvm::Value* data_field_offset =
TDYa127920be7c2012-09-10 17:13:22 -07002233 irb_.getPtrEquivInt(art::Array::DataOffset(alignment).Int32Value());
TDYa127f71bf5a2012-07-29 20:09:52 -07002234
2235 llvm::Value* data_field_addr =
2236 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
2237
2238 // TODO: Tune this code. Currently we are generating one instruction for
2239 // one element which may be very space consuming. Maybe changing to use
2240 // memcpy may help; however, since we can't guarantee that the alloca of
2241 // dalvik register are continuous, we can't perform such optimization yet.
2242 for (uint32_t i = 0; i < length; ++i) {
2243 llvm::Value* reg_value = call_inst.getArgOperand(i+3);
2244
2245 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
2246
2247 data_field_addr =
2248 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
2249 }
2250 }
2251
2252 return object_addr;
2253}
2254
2255void GBCExpanderPass::Expand_HLFillArrayData(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002256 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2257 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
2258 LV2SInt(call_inst.getArgOperand(0));
2259 llvm::Value* array_addr = call_inst.getArgOperand(1);
2260
TDYa127920be7c2012-09-10 17:13:22 -07002261 const art::Instruction::ArrayDataPayload* payload =
2262 reinterpret_cast<const art::Instruction::ArrayDataPayload*>(
TDYa127f71bf5a2012-07-29 20:09:52 -07002263 code_item_->insns_ + payload_offset);
2264
2265 if (payload->element_count == 0) {
2266 // When the number of the elements in the payload is zero, we don't have
2267 // to copy any numbers. However, we should check whether the array object
2268 // address is equal to null or not.
2269 EmitGuard_NullPointerException(dex_pc, array_addr);
2270 } else {
2271 // To save the code size, we are going to call the runtime function to
2272 // copy the content from DexFile.
2273
2274 // NOTE: We will check for the NullPointerException in the runtime.
2275
2276 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::FillArrayData);
2277
2278 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2279
2280 EmitUpdateDexPC(dex_pc);
2281
2282 irb_.CreateCall4(runtime_func,
2283 method_object_addr, irb_.getInt32(dex_pc),
2284 array_addr, irb_.getInt32(payload_offset));
2285
2286 EmitGuard_ExceptionLandingPad(dex_pc);
2287 }
2288
2289 return;
2290}
2291
2292llvm::Value* GBCExpanderPass::EmitAllocNewArray(uint32_t dex_pc,
2293 llvm::Value* array_length_value,
2294 uint32_t type_idx,
2295 bool is_filled_new_array) {
2296 llvm::Function* runtime_func;
2297
2298 bool skip_access_check =
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002299 compiler_->CanAccessTypeWithoutChecks(method_idx_, *dex_file_, type_idx);
TDYa127f71bf5a2012-07-29 20:09:52 -07002300
2301
2302 if (is_filled_new_array) {
2303 runtime_func = skip_access_check ?
2304 irb_.GetRuntime(runtime_support::CheckAndAllocArray) :
2305 irb_.GetRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck);
2306 } else {
2307 runtime_func = skip_access_check ?
2308 irb_.GetRuntime(runtime_support::AllocArray) :
2309 irb_.GetRuntime(runtime_support::AllocArrayWithAccessCheck);
2310 }
2311
2312 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2313
2314 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2315
2316 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2317
2318 EmitUpdateDexPC(dex_pc);
2319
2320 llvm::Value* object_addr =
2321 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
2322 array_length_value, thread_object_addr);
2323
2324 EmitGuard_ExceptionLandingPad(dex_pc);
2325
2326 return object_addr;
2327}
2328
2329llvm::Value* GBCExpanderPass::
2330EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -07002331 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -07002332 llvm::Value* this_addr,
2333 uint32_t dex_pc,
2334 bool is_fast_path) {
2335
2336 llvm::Function* runtime_func = NULL;
2337
2338 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002339 case art::kStatic:
TDYa127f71bf5a2012-07-29 20:09:52 -07002340 runtime_func = irb_.GetRuntime(runtime_support::FindStaticMethodWithAccessCheck);
2341 break;
2342
TDYa127920be7c2012-09-10 17:13:22 -07002343 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002344 runtime_func = irb_.GetRuntime(runtime_support::FindDirectMethodWithAccessCheck);
2345 break;
2346
TDYa127920be7c2012-09-10 17:13:22 -07002347 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002348 runtime_func = irb_.GetRuntime(runtime_support::FindVirtualMethodWithAccessCheck);
2349 break;
2350
TDYa127920be7c2012-09-10 17:13:22 -07002351 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002352 runtime_func = irb_.GetRuntime(runtime_support::FindSuperMethodWithAccessCheck);
2353 break;
2354
TDYa127920be7c2012-09-10 17:13:22 -07002355 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002356 if (is_fast_path) {
2357 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethod);
2358 } else {
2359 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethodWithAccessCheck);
2360 }
2361 break;
2362 }
2363
2364 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2365
2366 if (this_addr == NULL) {
TDYa127920be7c2012-09-10 17:13:22 -07002367 DCHECK_EQ(invoke_type, art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002368 this_addr = irb_.getJNull();
2369 }
2370
2371 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2372
2373 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2374
2375 EmitUpdateDexPC(dex_pc);
2376
2377 llvm::Value* callee_method_object_addr =
2378 irb_.CreateCall4(runtime_func,
2379 callee_method_idx_value,
2380 this_addr,
2381 caller_method_object_addr,
2382 thread_object_addr);
2383
2384 EmitGuard_ExceptionLandingPad(dex_pc);
2385
2386 return callee_method_object_addr;
2387}
2388
TDYa1275e869b62012-07-25 00:45:39 -07002389void GBCExpanderPass::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2390 // Using runtime support, let the target can override by InlineAssembly.
2391 irb_.Runtime().EmitMarkGCCard(value, target_addr);
2392}
2393
2394void GBCExpanderPass::EmitUpdateDexPC(uint32_t dex_pc) {
buzbeec531cef2012-10-18 07:09:20 -07002395#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002396 if (shadow_frame_ == NULL) {
2397 return;
2398 }
2399#endif
TDYa1275e869b62012-07-25 00:45:39 -07002400 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07002401 art::ShadowFrame::DexPCOffset(),
TDYa1275e869b62012-07-25 00:45:39 -07002402 irb_.getInt32(dex_pc),
2403 kTBAAShadowFrame);
2404}
2405
2406void GBCExpanderPass::EmitGuard_DivZeroException(uint32_t dex_pc,
2407 llvm::Value* denominator,
2408 JType op_jty) {
2409 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2410
2411 llvm::Constant* zero = irb_.getJZero(op_jty);
2412
2413 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2414
2415 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2416
2417 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2418
2419 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
2420
2421 irb_.SetInsertPoint(block_exception);
2422 EmitUpdateDexPC(dex_pc);
2423 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowDivZeroException));
2424 EmitBranchExceptionLandingPad(dex_pc);
2425
2426 irb_.SetInsertPoint(block_continue);
2427}
2428
2429void GBCExpanderPass::EmitGuard_NullPointerException(uint32_t dex_pc,
2430 llvm::Value* object) {
2431 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
2432
2433 llvm::BasicBlock* block_exception =
2434 CreateBasicBlockWithDexPC(dex_pc, "nullp");
2435
2436 llvm::BasicBlock* block_continue =
2437 CreateBasicBlockWithDexPC(dex_pc, "cont");
2438
2439 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
2440
2441 irb_.SetInsertPoint(block_exception);
2442 EmitUpdateDexPC(dex_pc);
2443 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowNullPointerException),
2444 irb_.getInt32(dex_pc));
2445 EmitBranchExceptionLandingPad(dex_pc);
2446
2447 irb_.SetInsertPoint(block_continue);
2448}
2449
2450void
2451GBCExpanderPass::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2452 llvm::Value* array,
2453 llvm::Value* index) {
2454 llvm::Value* array_len = EmitLoadArrayLength(array);
2455
2456 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2457
2458 llvm::BasicBlock* block_exception =
2459 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2460
2461 llvm::BasicBlock* block_continue =
2462 CreateBasicBlockWithDexPC(dex_pc, "cont");
2463
2464 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
2465
2466 irb_.SetInsertPoint(block_exception);
2467
2468 EmitUpdateDexPC(dex_pc);
2469 irb_.CreateCall2(irb_.GetRuntime(runtime_support::ThrowIndexOutOfBounds), index, array_len);
2470 EmitBranchExceptionLandingPad(dex_pc);
2471
2472 irb_.SetInsertPoint(block_continue);
2473}
2474
TDYa1275e869b62012-07-25 00:45:39 -07002475llvm::FunctionType* GBCExpanderPass::GetFunctionType(uint32_t method_idx,
2476 bool is_static) {
2477 // Get method signature
TDYa127920be7c2012-09-10 17:13:22 -07002478 art::DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
TDYa1275e869b62012-07-25 00:45:39 -07002479
2480 uint32_t shorty_size;
2481 const char* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
2482 CHECK_GE(shorty_size, 1u);
2483
2484 // Get return type
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002485
2486 char ret_shorty = shorty[0];
buzbeec531cef2012-10-18 07:09:20 -07002487#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaob2596522012-09-14 16:36:11 -07002488 ret_shorty = art::remapShorty(ret_shorty);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002489#endif
2490 llvm::Type* ret_type = irb_.getJType(ret_shorty, kAccurate);
TDYa1275e869b62012-07-25 00:45:39 -07002491
2492 // Get argument type
2493 std::vector<llvm::Type*> args_type;
2494
2495 args_type.push_back(irb_.getJObjectTy()); // method object pointer
2496
2497 if (!is_static) {
2498 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
2499 }
2500
2501 for (uint32_t i = 1; i < shorty_size; ++i) {
buzbeec531cef2012-10-18 07:09:20 -07002502#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaob2596522012-09-14 16:36:11 -07002503 char shorty_type = art::remapShorty(shorty[i]);
TDYa127f71bf5a2012-07-29 20:09:52 -07002504 args_type.push_back(irb_.getJType(shorty_type, kAccurate));
2505#else
TDYa1275e869b62012-07-25 00:45:39 -07002506 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
TDYa127f71bf5a2012-07-29 20:09:52 -07002507#endif
TDYa1275e869b62012-07-25 00:45:39 -07002508 }
2509
2510 return llvm::FunctionType::get(ret_type, args_type, false);
2511}
2512
2513
2514llvm::BasicBlock* GBCExpanderPass::
2515CreateBasicBlockWithDexPC(uint32_t dex_pc, const char* postfix) {
2516 std::string name;
2517
2518#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002519 art::StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
TDYa1275e869b62012-07-25 00:45:39 -07002520#endif
2521
2522 return llvm::BasicBlock::Create(context_, name, func_);
2523}
2524
2525llvm::BasicBlock* GBCExpanderPass::GetBasicBlock(uint32_t dex_pc) {
2526 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002527 CHECK(basic_blocks_[dex_pc] != NULL);
TDYa1275e869b62012-07-25 00:45:39 -07002528 return basic_blocks_[dex_pc];
2529}
2530
2531int32_t GBCExpanderPass::GetTryItemOffset(uint32_t dex_pc) {
2532 int32_t min = 0;
2533 int32_t max = code_item_->tries_size_ - 1;
2534
2535 while (min <= max) {
2536 int32_t mid = min + (max - min) / 2;
2537
TDYa127920be7c2012-09-10 17:13:22 -07002538 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*code_item_, mid);
TDYa1275e869b62012-07-25 00:45:39 -07002539 uint32_t start = ti->start_addr_;
2540 uint32_t end = start + ti->insn_count_;
2541
2542 if (dex_pc < start) {
2543 max = mid - 1;
2544 } else if (dex_pc >= end) {
2545 min = mid + 1;
2546 } else {
2547 return mid; // found
2548 }
2549 }
2550
2551 return -1; // not found
2552}
2553
2554llvm::BasicBlock* GBCExpanderPass::GetLandingPadBasicBlock(uint32_t dex_pc) {
2555 // Find the try item for this address in this method
2556 int32_t ti_offset = GetTryItemOffset(dex_pc);
2557
2558 if (ti_offset == -1) {
2559 return NULL; // No landing pad is available for this address.
2560 }
2561
2562 // Check for the existing landing pad basic block
2563 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2564 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2565
2566 if (block_lpad) {
2567 // We have generated landing pad for this try item already. Return the
2568 // same basic block.
2569 return block_lpad;
2570 }
2571
2572 // Get try item from code item
TDYa127920be7c2012-09-10 17:13:22 -07002573 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*code_item_, ti_offset);
TDYa1275e869b62012-07-25 00:45:39 -07002574
2575 std::string lpadname;
2576
2577#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002578 art::StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
TDYa1275e869b62012-07-25 00:45:39 -07002579#endif
2580
2581 // Create landing pad basic block
2582 block_lpad = llvm::BasicBlock::Create(context_, lpadname, func_);
2583
2584 // Change IRBuilder insert point
2585 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2586 irb_.SetInsertPoint(block_lpad);
2587
2588 // Find catch block with matching type
2589 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2590
2591 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
2592
2593 llvm::Value* catch_handler_index_value =
2594 irb_.CreateCall2(irb_.GetRuntime(runtime_support::FindCatchBlock),
2595 method_object_addr, ti_offset_value);
2596
2597 // Switch instruction (Go to unwind basic block by default)
2598 llvm::SwitchInst* sw =
2599 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2600
2601 // Cases with matched catch block
TDYa127920be7c2012-09-10 17:13:22 -07002602 art::CatchHandlerIterator iter(*code_item_, ti->start_addr_);
TDYa1275e869b62012-07-25 00:45:39 -07002603
2604 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2605 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2606 }
2607
2608 // Restore the orignal insert point for IRBuilder
2609 irb_.restoreIP(irb_ip_original);
2610
2611 // Cache this landing pad
2612 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2613 basic_block_landing_pads_[ti_offset] = block_lpad;
2614
2615 return block_lpad;
2616}
2617
2618llvm::BasicBlock* GBCExpanderPass::GetUnwindBasicBlock() {
2619 // Check the existing unwinding baisc block block
2620 if (basic_block_unwind_ != NULL) {
2621 return basic_block_unwind_;
2622 }
2623
2624 // Create new basic block for unwinding
2625 basic_block_unwind_ =
2626 llvm::BasicBlock::Create(context_, "exception_unwind", func_);
2627
2628 // Change IRBuilder insert point
2629 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2630 irb_.SetInsertPoint(basic_block_unwind_);
2631
2632 // Pop the shadow frame
2633 Expand_PopShadowFrame();
2634
2635 // Emit the code to return default value (zero) for the given return type.
2636 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
buzbeec531cef2012-10-18 07:09:20 -07002637#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaob2596522012-09-14 16:36:11 -07002638 ret_shorty = art::remapShorty(ret_shorty);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002639#endif
TDYa1275e869b62012-07-25 00:45:39 -07002640 if (ret_shorty == 'V') {
2641 irb_.CreateRetVoid();
2642 } else {
2643 irb_.CreateRet(irb_.getJZero(ret_shorty));
2644 }
2645
2646 // Restore the orignal insert point for IRBuilder
2647 irb_.restoreIP(irb_ip_original);
2648
2649 return basic_block_unwind_;
2650}
2651
2652void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2653 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002654 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002655 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002656 irb_.CreateBr(lpad);
2657 } else {
2658 irb_.CreateBr(GetUnwindBasicBlock());
2659 }
2660}
2661
2662void GBCExpanderPass::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
2663 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
2664
2665 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2666
2667 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002668 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002669 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002670 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
2671 } else {
2672 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
2673 }
2674
2675 irb_.SetInsertPoint(block_cont);
2676}
2677
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002678llvm::Value*
2679GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
2680 llvm::CallInst& call_inst) {
2681 switch (intr_id) {
2682 //==- Thread -----------------------------------------------------------==//
2683 case IntrinsicHelper::GetCurrentThread: {
TDYa127b672d1e2012-06-28 21:21:45 -07002684 return irb_.Runtime().EmitGetCurrentThread();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002685 }
Logan Chien75e4b602012-07-23 14:24:12 -07002686 case IntrinsicHelper::CheckSuspend: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08002687 Expand_TestSuspend(call_inst);
TDYa127890ea892012-08-22 10:49:42 -07002688 return NULL;
2689 }
2690 case IntrinsicHelper::TestSuspend: {
Logan Chiend54a23d2012-07-24 11:19:23 -07002691 Expand_TestSuspend(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002692 return NULL;
2693 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002694 case IntrinsicHelper::MarkGCCard: {
TDYa1279a129452012-07-19 03:10:08 -07002695 Expand_MarkGCCard(call_inst);
2696 return NULL;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002697 }
Logan Chien75e4b602012-07-23 14:24:12 -07002698
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002699 //==- Exception --------------------------------------------------------==//
2700 case IntrinsicHelper::ThrowException: {
2701 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
2702 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002703 case IntrinsicHelper::HLThrowException: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002704 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2705
2706 EmitUpdateDexPC(dex_pc);
2707
2708 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowException),
2709 call_inst.getArgOperand(0));
2710
2711 EmitGuard_ExceptionLandingPad(dex_pc);
2712 return NULL;
2713 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002714 case IntrinsicHelper::GetException: {
TDYa127823433d2012-09-26 16:03:51 -07002715 return irb_.Runtime().EmitGetAndClearException();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002716 }
2717 case IntrinsicHelper::IsExceptionPending: {
2718 return irb_.Runtime().EmitIsExceptionPending();
2719 }
2720 case IntrinsicHelper::FindCatchBlock: {
2721 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
2722 }
2723 case IntrinsicHelper::ThrowDivZeroException: {
2724 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
2725 }
2726 case IntrinsicHelper::ThrowNullPointerException: {
2727 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
2728 }
2729 case IntrinsicHelper::ThrowIndexOutOfBounds: {
2730 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
2731 }
Logan Chien75e4b602012-07-23 14:24:12 -07002732
2733 //==- Const String -----------------------------------------------------==//
2734 case IntrinsicHelper::ConstString: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002735 return Expand_ConstString(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002736 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002737 case IntrinsicHelper::LoadStringFromDexCache: {
2738 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
2739 }
2740 case IntrinsicHelper::ResolveString: {
2741 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
2742 }
Logan Chien75e4b602012-07-23 14:24:12 -07002743
2744 //==- Const Class ------------------------------------------------------==//
2745 case IntrinsicHelper::ConstClass: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002746 return Expand_ConstClass(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002747 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002748 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
2749 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
2750 }
2751 case IntrinsicHelper::LoadTypeFromDexCache: {
2752 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
2753 }
2754 case IntrinsicHelper::InitializeType: {
2755 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
2756 }
Logan Chien75e4b602012-07-23 14:24:12 -07002757
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002758 //==- Lock -------------------------------------------------------------==//
2759 case IntrinsicHelper::LockObject: {
2760 Expand_LockObject(call_inst.getArgOperand(0));
2761 return NULL;
2762 }
2763 case IntrinsicHelper::UnlockObject: {
2764 Expand_UnlockObject(call_inst.getArgOperand(0));
2765 return NULL;
2766 }
Logan Chien75e4b602012-07-23 14:24:12 -07002767
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002768 //==- Cast -------------------------------------------------------------==//
2769 case IntrinsicHelper::CheckCast: {
2770 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
2771 }
Logan Chien75e4b602012-07-23 14:24:12 -07002772 case IntrinsicHelper::HLCheckCast: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002773 Expand_HLCheckCast(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002774 return NULL;
2775 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002776 case IntrinsicHelper::IsAssignable: {
2777 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
2778 }
Logan Chien75e4b602012-07-23 14:24:12 -07002779
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002780 //==- Alloc ------------------------------------------------------------==//
2781 case IntrinsicHelper::AllocObject: {
2782 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
2783 }
2784 case IntrinsicHelper::AllocObjectWithAccessCheck: {
2785 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
2786 }
Logan Chien75e4b602012-07-23 14:24:12 -07002787
2788 //==- Instance ---------------------------------------------------------==//
2789 case IntrinsicHelper::NewInstance: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002790 return Expand_NewInstance(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002791 }
2792 case IntrinsicHelper::InstanceOf: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002793 return Expand_InstanceOf(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002794 }
2795
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002796 //==- Array ------------------------------------------------------------==//
Logan Chien75e4b602012-07-23 14:24:12 -07002797 case IntrinsicHelper::NewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002798 return Expand_NewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002799 }
2800 case IntrinsicHelper::OptArrayLength: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002801 return Expand_OptArrayLength(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002802 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002803 case IntrinsicHelper::ArrayLength: {
2804 return EmitLoadArrayLength(call_inst.getArgOperand(0));
2805 }
2806 case IntrinsicHelper::AllocArray: {
2807 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
2808 }
2809 case IntrinsicHelper::AllocArrayWithAccessCheck: {
2810 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
2811 call_inst);
2812 }
2813 case IntrinsicHelper::CheckAndAllocArray: {
2814 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
2815 }
2816 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
2817 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
2818 call_inst);
2819 }
2820 case IntrinsicHelper::ArrayGet: {
2821 return Expand_ArrayGet(call_inst.getArgOperand(0),
2822 call_inst.getArgOperand(1),
2823 kInt);
2824 }
2825 case IntrinsicHelper::ArrayGetWide: {
2826 return Expand_ArrayGet(call_inst.getArgOperand(0),
2827 call_inst.getArgOperand(1),
2828 kLong);
2829 }
2830 case IntrinsicHelper::ArrayGetObject: {
2831 return Expand_ArrayGet(call_inst.getArgOperand(0),
2832 call_inst.getArgOperand(1),
2833 kObject);
2834 }
2835 case IntrinsicHelper::ArrayGetBoolean: {
2836 return Expand_ArrayGet(call_inst.getArgOperand(0),
2837 call_inst.getArgOperand(1),
2838 kBoolean);
2839 }
2840 case IntrinsicHelper::ArrayGetByte: {
2841 return Expand_ArrayGet(call_inst.getArgOperand(0),
2842 call_inst.getArgOperand(1),
2843 kByte);
2844 }
2845 case IntrinsicHelper::ArrayGetChar: {
2846 return Expand_ArrayGet(call_inst.getArgOperand(0),
2847 call_inst.getArgOperand(1),
2848 kChar);
2849 }
2850 case IntrinsicHelper::ArrayGetShort: {
2851 return Expand_ArrayGet(call_inst.getArgOperand(0),
2852 call_inst.getArgOperand(1),
2853 kShort);
2854 }
2855 case IntrinsicHelper::ArrayPut: {
2856 Expand_ArrayPut(call_inst.getArgOperand(0),
2857 call_inst.getArgOperand(1),
2858 call_inst.getArgOperand(2),
2859 kInt);
2860 return NULL;
2861 }
2862 case IntrinsicHelper::ArrayPutWide: {
2863 Expand_ArrayPut(call_inst.getArgOperand(0),
2864 call_inst.getArgOperand(1),
2865 call_inst.getArgOperand(2),
2866 kLong);
2867 return NULL;
2868 }
2869 case IntrinsicHelper::ArrayPutObject: {
2870 Expand_ArrayPut(call_inst.getArgOperand(0),
2871 call_inst.getArgOperand(1),
2872 call_inst.getArgOperand(2),
2873 kObject);
2874 return NULL;
2875 }
2876 case IntrinsicHelper::ArrayPutBoolean: {
2877 Expand_ArrayPut(call_inst.getArgOperand(0),
2878 call_inst.getArgOperand(1),
2879 call_inst.getArgOperand(2),
2880 kBoolean);
2881 return NULL;
2882 }
2883 case IntrinsicHelper::ArrayPutByte: {
2884 Expand_ArrayPut(call_inst.getArgOperand(0),
2885 call_inst.getArgOperand(1),
2886 call_inst.getArgOperand(2),
2887 kByte);
2888 return NULL;
2889 }
2890 case IntrinsicHelper::ArrayPutChar: {
2891 Expand_ArrayPut(call_inst.getArgOperand(0),
2892 call_inst.getArgOperand(1),
2893 call_inst.getArgOperand(2),
2894 kChar);
2895 return NULL;
2896 }
2897 case IntrinsicHelper::ArrayPutShort: {
2898 Expand_ArrayPut(call_inst.getArgOperand(0),
2899 call_inst.getArgOperand(1),
2900 call_inst.getArgOperand(2),
2901 kShort);
2902 return NULL;
2903 }
2904 case IntrinsicHelper::CheckPutArrayElement: {
2905 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
2906 }
2907 case IntrinsicHelper::FilledNewArray: {
2908 Expand_FilledNewArray(call_inst);
2909 return NULL;
2910 }
2911 case IntrinsicHelper::FillArrayData: {
2912 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
2913 }
Logan Chien75e4b602012-07-23 14:24:12 -07002914 case IntrinsicHelper::HLFillArrayData: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002915 Expand_HLFillArrayData(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002916 return NULL;
2917 }
2918 case IntrinsicHelper::HLFilledNewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002919 return Expand_HLFilledNewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002920 }
2921
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002922 //==- Instance Field ---------------------------------------------------==//
2923 case IntrinsicHelper::InstanceFieldGet:
2924 case IntrinsicHelper::InstanceFieldGetBoolean:
2925 case IntrinsicHelper::InstanceFieldGetByte:
2926 case IntrinsicHelper::InstanceFieldGetChar:
2927 case IntrinsicHelper::InstanceFieldGetShort: {
2928 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
2929 }
2930 case IntrinsicHelper::InstanceFieldGetWide: {
2931 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
2932 }
2933 case IntrinsicHelper::InstanceFieldGetObject: {
2934 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
2935 }
2936 case IntrinsicHelper::InstanceFieldGetFast: {
2937 return Expand_IGetFast(call_inst.getArgOperand(0),
2938 call_inst.getArgOperand(1),
2939 call_inst.getArgOperand(2),
2940 kInt);
2941 }
2942 case IntrinsicHelper::InstanceFieldGetWideFast: {
2943 return Expand_IGetFast(call_inst.getArgOperand(0),
2944 call_inst.getArgOperand(1),
2945 call_inst.getArgOperand(2),
2946 kLong);
2947 }
2948 case IntrinsicHelper::InstanceFieldGetObjectFast: {
2949 return Expand_IGetFast(call_inst.getArgOperand(0),
2950 call_inst.getArgOperand(1),
2951 call_inst.getArgOperand(2),
2952 kObject);
2953 }
2954 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
2955 return Expand_IGetFast(call_inst.getArgOperand(0),
2956 call_inst.getArgOperand(1),
2957 call_inst.getArgOperand(2),
2958 kBoolean);
2959 }
2960 case IntrinsicHelper::InstanceFieldGetByteFast: {
2961 return Expand_IGetFast(call_inst.getArgOperand(0),
2962 call_inst.getArgOperand(1),
2963 call_inst.getArgOperand(2),
2964 kByte);
2965 }
2966 case IntrinsicHelper::InstanceFieldGetCharFast: {
2967 return Expand_IGetFast(call_inst.getArgOperand(0),
2968 call_inst.getArgOperand(1),
2969 call_inst.getArgOperand(2),
2970 kChar);
2971 }
2972 case IntrinsicHelper::InstanceFieldGetShortFast: {
2973 return Expand_IGetFast(call_inst.getArgOperand(0),
2974 call_inst.getArgOperand(1),
2975 call_inst.getArgOperand(2),
2976 kShort);
2977 }
2978 case IntrinsicHelper::InstanceFieldPut:
2979 case IntrinsicHelper::InstanceFieldPutBoolean:
2980 case IntrinsicHelper::InstanceFieldPutByte:
2981 case IntrinsicHelper::InstanceFieldPutChar:
2982 case IntrinsicHelper::InstanceFieldPutShort: {
2983 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
2984 }
2985 case IntrinsicHelper::InstanceFieldPutWide: {
2986 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
2987 }
2988 case IntrinsicHelper::InstanceFieldPutObject: {
2989 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
2990 }
2991 case IntrinsicHelper::InstanceFieldPutFast: {
2992 Expand_IPutFast(call_inst.getArgOperand(0),
2993 call_inst.getArgOperand(1),
2994 call_inst.getArgOperand(2),
2995 call_inst.getArgOperand(3),
2996 kInt);
2997 return NULL;
2998 }
2999 case IntrinsicHelper::InstanceFieldPutWideFast: {
3000 Expand_IPutFast(call_inst.getArgOperand(0),
3001 call_inst.getArgOperand(1),
3002 call_inst.getArgOperand(2),
3003 call_inst.getArgOperand(3),
3004 kLong);
3005 return NULL;
3006 }
3007 case IntrinsicHelper::InstanceFieldPutObjectFast: {
3008 Expand_IPutFast(call_inst.getArgOperand(0),
3009 call_inst.getArgOperand(1),
3010 call_inst.getArgOperand(2),
3011 call_inst.getArgOperand(3),
3012 kObject);
3013 return NULL;
3014 }
3015 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
3016 Expand_IPutFast(call_inst.getArgOperand(0),
3017 call_inst.getArgOperand(1),
3018 call_inst.getArgOperand(2),
3019 call_inst.getArgOperand(3),
3020 kBoolean);
3021 return NULL;
3022 }
3023 case IntrinsicHelper::InstanceFieldPutByteFast: {
3024 Expand_IPutFast(call_inst.getArgOperand(0),
3025 call_inst.getArgOperand(1),
3026 call_inst.getArgOperand(2),
3027 call_inst.getArgOperand(3),
3028 kByte);
3029 return NULL;
3030 }
3031 case IntrinsicHelper::InstanceFieldPutCharFast: {
3032 Expand_IPutFast(call_inst.getArgOperand(0),
3033 call_inst.getArgOperand(1),
3034 call_inst.getArgOperand(2),
3035 call_inst.getArgOperand(3),
3036 kChar);
3037 return NULL;
3038 }
3039 case IntrinsicHelper::InstanceFieldPutShortFast: {
3040 Expand_IPutFast(call_inst.getArgOperand(0),
3041 call_inst.getArgOperand(1),
3042 call_inst.getArgOperand(2),
3043 call_inst.getArgOperand(3),
3044 kShort);
3045 return NULL;
3046 }
Logan Chien75e4b602012-07-23 14:24:12 -07003047
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003048 //==- Static Field -----------------------------------------------------==//
3049 case IntrinsicHelper::StaticFieldGet:
3050 case IntrinsicHelper::StaticFieldGetBoolean:
3051 case IntrinsicHelper::StaticFieldGetByte:
3052 case IntrinsicHelper::StaticFieldGetChar:
3053 case IntrinsicHelper::StaticFieldGetShort: {
3054 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
3055 }
3056 case IntrinsicHelper::StaticFieldGetWide: {
3057 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
3058 }
3059 case IntrinsicHelper::StaticFieldGetObject: {
3060 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
3061 }
3062 case IntrinsicHelper::StaticFieldGetFast: {
3063 return Expand_SGetFast(call_inst.getArgOperand(0),
3064 call_inst.getArgOperand(1),
3065 call_inst.getArgOperand(2),
3066 kInt);
3067 }
3068 case IntrinsicHelper::StaticFieldGetWideFast: {
3069 return Expand_SGetFast(call_inst.getArgOperand(0),
3070 call_inst.getArgOperand(1),
3071 call_inst.getArgOperand(2),
3072 kLong);
3073 }
3074 case IntrinsicHelper::StaticFieldGetObjectFast: {
3075 return Expand_SGetFast(call_inst.getArgOperand(0),
3076 call_inst.getArgOperand(1),
3077 call_inst.getArgOperand(2),
3078 kObject);
3079 }
3080 case IntrinsicHelper::StaticFieldGetBooleanFast: {
3081 return Expand_SGetFast(call_inst.getArgOperand(0),
3082 call_inst.getArgOperand(1),
3083 call_inst.getArgOperand(2),
3084 kBoolean);
3085 }
3086 case IntrinsicHelper::StaticFieldGetByteFast: {
3087 return Expand_SGetFast(call_inst.getArgOperand(0),
3088 call_inst.getArgOperand(1),
3089 call_inst.getArgOperand(2),
3090 kByte);
3091 }
3092 case IntrinsicHelper::StaticFieldGetCharFast: {
3093 return Expand_SGetFast(call_inst.getArgOperand(0),
3094 call_inst.getArgOperand(1),
3095 call_inst.getArgOperand(2),
3096 kChar);
3097 }
3098 case IntrinsicHelper::StaticFieldGetShortFast: {
3099 return Expand_SGetFast(call_inst.getArgOperand(0),
3100 call_inst.getArgOperand(1),
3101 call_inst.getArgOperand(2),
3102 kShort);
3103 }
3104 case IntrinsicHelper::StaticFieldPut:
3105 case IntrinsicHelper::StaticFieldPutBoolean:
3106 case IntrinsicHelper::StaticFieldPutByte:
3107 case IntrinsicHelper::StaticFieldPutChar:
3108 case IntrinsicHelper::StaticFieldPutShort: {
3109 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
3110 }
3111 case IntrinsicHelper::StaticFieldPutWide: {
3112 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
3113 }
3114 case IntrinsicHelper::StaticFieldPutObject: {
3115 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
3116 }
3117 case IntrinsicHelper::StaticFieldPutFast: {
3118 Expand_SPutFast(call_inst.getArgOperand(0),
3119 call_inst.getArgOperand(1),
3120 call_inst.getArgOperand(2),
3121 call_inst.getArgOperand(3),
3122 kInt);
3123 return NULL;
3124 }
3125 case IntrinsicHelper::StaticFieldPutWideFast: {
3126 Expand_SPutFast(call_inst.getArgOperand(0),
3127 call_inst.getArgOperand(1),
3128 call_inst.getArgOperand(2),
3129 call_inst.getArgOperand(3),
3130 kLong);
3131 return NULL;
3132 }
3133 case IntrinsicHelper::StaticFieldPutObjectFast: {
3134 Expand_SPutFast(call_inst.getArgOperand(0),
3135 call_inst.getArgOperand(1),
3136 call_inst.getArgOperand(2),
3137 call_inst.getArgOperand(3),
3138 kObject);
3139 return NULL;
3140 }
3141 case IntrinsicHelper::StaticFieldPutBooleanFast: {
3142 Expand_SPutFast(call_inst.getArgOperand(0),
3143 call_inst.getArgOperand(1),
3144 call_inst.getArgOperand(2),
3145 call_inst.getArgOperand(3),
3146 kBoolean);
3147 return NULL;
3148 }
3149 case IntrinsicHelper::StaticFieldPutByteFast: {
3150 Expand_SPutFast(call_inst.getArgOperand(0),
3151 call_inst.getArgOperand(1),
3152 call_inst.getArgOperand(2),
3153 call_inst.getArgOperand(3),
3154 kByte);
3155 return NULL;
3156 }
3157 case IntrinsicHelper::StaticFieldPutCharFast: {
3158 Expand_SPutFast(call_inst.getArgOperand(0),
3159 call_inst.getArgOperand(1),
3160 call_inst.getArgOperand(2),
3161 call_inst.getArgOperand(3),
3162 kChar);
3163 return NULL;
3164 }
3165 case IntrinsicHelper::StaticFieldPutShortFast: {
3166 Expand_SPutFast(call_inst.getArgOperand(0),
3167 call_inst.getArgOperand(1),
3168 call_inst.getArgOperand(2),
3169 call_inst.getArgOperand(3),
3170 kShort);
3171 return NULL;
3172 }
3173 case IntrinsicHelper::LoadDeclaringClassSSB: {
3174 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
3175 }
3176 case IntrinsicHelper::LoadClassSSBFromDexCache: {
3177 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
3178 }
3179 case IntrinsicHelper::InitializeAndLoadClassSSB: {
3180 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
3181 }
Logan Chien75e4b602012-07-23 14:24:12 -07003182
3183 //==- High-level Array -------------------------------------------------==//
3184 case IntrinsicHelper::HLArrayGet: {
TDYa1275a26d442012-07-26 18:58:38 -07003185 return Expand_HLArrayGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003186 }
3187 case IntrinsicHelper::HLArrayGetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003188 return Expand_HLArrayGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003189 }
3190 case IntrinsicHelper::HLArrayGetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003191 return Expand_HLArrayGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003192 }
3193 case IntrinsicHelper::HLArrayGetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003194 return Expand_HLArrayGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003195 }
3196 case IntrinsicHelper::HLArrayGetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003197 return Expand_HLArrayGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003198 }
3199 case IntrinsicHelper::HLArrayGetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003200 return Expand_HLArrayGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003201 }
3202 case IntrinsicHelper::HLArrayGetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003203 return Expand_HLArrayGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003204 }
3205 case IntrinsicHelper::HLArrayGetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003206 return Expand_HLArrayGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003207 }
3208 case IntrinsicHelper::HLArrayGetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003209 return Expand_HLArrayGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003210 }
3211 case IntrinsicHelper::HLArrayPut: {
TDYa1275a26d442012-07-26 18:58:38 -07003212 Expand_HLArrayPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003213 return NULL;
3214 }
3215 case IntrinsicHelper::HLArrayPutBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003216 Expand_HLArrayPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003217 return NULL;
3218 }
3219 case IntrinsicHelper::HLArrayPutByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003220 Expand_HLArrayPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003221 return NULL;
3222 }
3223 case IntrinsicHelper::HLArrayPutChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003224 Expand_HLArrayPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003225 return NULL;
3226 }
3227 case IntrinsicHelper::HLArrayPutShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003228 Expand_HLArrayPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003229 return NULL;
3230 }
3231 case IntrinsicHelper::HLArrayPutFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003232 Expand_HLArrayPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003233 return NULL;
3234 }
3235 case IntrinsicHelper::HLArrayPutWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003236 Expand_HLArrayPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003237 return NULL;
3238 }
3239 case IntrinsicHelper::HLArrayPutDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003240 Expand_HLArrayPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003241 return NULL;
3242 }
3243 case IntrinsicHelper::HLArrayPutObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003244 Expand_HLArrayPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003245 return NULL;
3246 }
3247
3248 //==- High-level Instance ----------------------------------------------==//
3249 case IntrinsicHelper::HLIGet: {
TDYa1275e869b62012-07-25 00:45:39 -07003250 return Expand_HLIGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003251 }
3252 case IntrinsicHelper::HLIGetBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003253 return Expand_HLIGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003254 }
3255 case IntrinsicHelper::HLIGetByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003256 return Expand_HLIGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003257 }
3258 case IntrinsicHelper::HLIGetChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003259 return Expand_HLIGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003260 }
3261 case IntrinsicHelper::HLIGetShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003262 return Expand_HLIGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003263 }
3264 case IntrinsicHelper::HLIGetFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003265 return Expand_HLIGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003266 }
3267 case IntrinsicHelper::HLIGetWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003268 return Expand_HLIGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003269 }
3270 case IntrinsicHelper::HLIGetDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003271 return Expand_HLIGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003272 }
3273 case IntrinsicHelper::HLIGetObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003274 return Expand_HLIGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003275 }
3276 case IntrinsicHelper::HLIPut: {
TDYa1275e869b62012-07-25 00:45:39 -07003277 Expand_HLIPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003278 return NULL;
3279 }
3280 case IntrinsicHelper::HLIPutBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003281 Expand_HLIPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003282 return NULL;
3283 }
3284 case IntrinsicHelper::HLIPutByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003285 Expand_HLIPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003286 return NULL;
3287 }
3288 case IntrinsicHelper::HLIPutChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003289 Expand_HLIPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003290 return NULL;
3291 }
3292 case IntrinsicHelper::HLIPutShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003293 Expand_HLIPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003294 return NULL;
3295 }
3296 case IntrinsicHelper::HLIPutFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003297 Expand_HLIPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003298 return NULL;
3299 }
3300 case IntrinsicHelper::HLIPutWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003301 Expand_HLIPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003302 return NULL;
3303 }
3304 case IntrinsicHelper::HLIPutDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003305 Expand_HLIPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003306 return NULL;
3307 }
3308 case IntrinsicHelper::HLIPutObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003309 Expand_HLIPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003310 return NULL;
3311 }
3312
3313 //==- High-level Invoke ------------------------------------------------==//
TDYa127f71bf5a2012-07-29 20:09:52 -07003314 case IntrinsicHelper::HLInvokeVoid:
3315 case IntrinsicHelper::HLInvokeObj:
3316 case IntrinsicHelper::HLInvokeInt:
3317 case IntrinsicHelper::HLInvokeFloat:
3318 case IntrinsicHelper::HLInvokeLong:
Logan Chien75e4b602012-07-23 14:24:12 -07003319 case IntrinsicHelper::HLInvokeDouble: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003320 return Expand_HLInvoke(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003321 }
3322
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003323 //==- Invoke -----------------------------------------------------------==//
3324 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
3325 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
3326 }
3327 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
3328 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
3329 }
3330 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
3331 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
3332 }
3333 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
3334 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
3335 }
3336 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
3337 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
3338 }
3339 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
3340 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
3341 }
3342 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
3343 return Expand_GetVirtualCalleeMethodObjAddrFast(
3344 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
3345 }
3346 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
3347 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
3348 }
3349 case IntrinsicHelper::InvokeRetVoid:
3350 case IntrinsicHelper::InvokeRetBoolean:
3351 case IntrinsicHelper::InvokeRetByte:
3352 case IntrinsicHelper::InvokeRetChar:
3353 case IntrinsicHelper::InvokeRetShort:
3354 case IntrinsicHelper::InvokeRetInt:
3355 case IntrinsicHelper::InvokeRetLong:
3356 case IntrinsicHelper::InvokeRetFloat:
3357 case IntrinsicHelper::InvokeRetDouble:
3358 case IntrinsicHelper::InvokeRetObject: {
3359 return Expand_Invoke(call_inst);
3360 }
Logan Chien75e4b602012-07-23 14:24:12 -07003361
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003362 //==- Math -------------------------------------------------------------==//
3363 case IntrinsicHelper::DivInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003364 return Expand_DivRem(call_inst, /* is_div */true, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003365 }
3366 case IntrinsicHelper::RemInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003367 return Expand_DivRem(call_inst, /* is_div */false, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003368 }
3369 case IntrinsicHelper::DivLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003370 return Expand_DivRem(call_inst, /* is_div */true, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003371 }
3372 case IntrinsicHelper::RemLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003373 return Expand_DivRem(call_inst, /* is_div */false, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003374 }
3375 case IntrinsicHelper::D2L: {
3376 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
3377 }
3378 case IntrinsicHelper::D2I: {
3379 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
3380 }
3381 case IntrinsicHelper::F2L: {
3382 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
3383 }
3384 case IntrinsicHelper::F2I: {
3385 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
3386 }
Logan Chien75e4b602012-07-23 14:24:12 -07003387
3388 //==- High-level Static ------------------------------------------------==//
3389 case IntrinsicHelper::HLSget: {
TDYa1275a26d442012-07-26 18:58:38 -07003390 return Expand_HLSget(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003391 }
3392 case IntrinsicHelper::HLSgetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003393 return Expand_HLSget(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003394 }
3395 case IntrinsicHelper::HLSgetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003396 return Expand_HLSget(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003397 }
3398 case IntrinsicHelper::HLSgetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003399 return Expand_HLSget(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003400 }
3401 case IntrinsicHelper::HLSgetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003402 return Expand_HLSget(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003403 }
3404 case IntrinsicHelper::HLSgetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003405 return Expand_HLSget(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003406 }
3407 case IntrinsicHelper::HLSgetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003408 return Expand_HLSget(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003409 }
3410 case IntrinsicHelper::HLSgetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003411 return Expand_HLSget(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003412 }
3413 case IntrinsicHelper::HLSgetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003414 return Expand_HLSget(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003415 }
3416 case IntrinsicHelper::HLSput: {
TDYa1275a26d442012-07-26 18:58:38 -07003417 Expand_HLSput(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003418 return NULL;
3419 }
3420 case IntrinsicHelper::HLSputBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003421 Expand_HLSput(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003422 return NULL;
3423 }
3424 case IntrinsicHelper::HLSputByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003425 Expand_HLSput(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003426 return NULL;
3427 }
3428 case IntrinsicHelper::HLSputChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003429 Expand_HLSput(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003430 return NULL;
3431 }
3432 case IntrinsicHelper::HLSputShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003433 Expand_HLSput(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003434 return NULL;
3435 }
3436 case IntrinsicHelper::HLSputFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003437 Expand_HLSput(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003438 return NULL;
3439 }
3440 case IntrinsicHelper::HLSputWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003441 Expand_HLSput(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003442 return NULL;
3443 }
3444 case IntrinsicHelper::HLSputDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003445 Expand_HLSput(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003446 return NULL;
3447 }
3448 case IntrinsicHelper::HLSputObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003449 Expand_HLSput(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003450 return NULL;
3451 }
3452
3453 //==- High-level Monitor -----------------------------------------------==//
3454 case IntrinsicHelper::MonitorEnter: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003455 Expand_MonitorEnter(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003456 return NULL;
3457 }
3458 case IntrinsicHelper::MonitorExit: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003459 Expand_MonitorExit(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003460 return NULL;
3461 }
3462
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003463 //==- Shadow Frame -----------------------------------------------------==//
3464 case IntrinsicHelper::AllocaShadowFrame: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08003465 Expand_AllocaShadowFrame(call_inst.getArgOperand(0));
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003466 return NULL;
3467 }
TDYa1278e950c12012-11-02 09:58:19 -07003468 case IntrinsicHelper::SetVReg: {
3469 Expand_SetVReg(call_inst.getArgOperand(0),
3470 call_inst.getArgOperand(1));
3471 return NULL;
3472 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003473 case IntrinsicHelper::PopShadowFrame: {
3474 Expand_PopShadowFrame();
3475 return NULL;
3476 }
3477 case IntrinsicHelper::UpdateDexPC: {
3478 Expand_UpdateDexPC(call_inst.getArgOperand(0));
3479 return NULL;
3480 }
TDYa127a1b21852012-07-23 03:20:39 -07003481
Logan Chien75e4b602012-07-23 14:24:12 -07003482 //==- Comparison -------------------------------------------------------==//
3483 case IntrinsicHelper::CmplFloat:
3484 case IntrinsicHelper::CmplDouble: {
3485 return Expand_FPCompare(call_inst.getArgOperand(0),
3486 call_inst.getArgOperand(1),
3487 false);
3488 }
3489 case IntrinsicHelper::CmpgFloat:
3490 case IntrinsicHelper::CmpgDouble: {
3491 return Expand_FPCompare(call_inst.getArgOperand(0),
3492 call_inst.getArgOperand(1),
3493 true);
3494 }
3495 case IntrinsicHelper::CmpLong: {
3496 return Expand_LongCompare(call_inst.getArgOperand(0),
3497 call_inst.getArgOperand(1));
3498 }
TDYa127a1b21852012-07-23 03:20:39 -07003499
Logan Chien75e4b602012-07-23 14:24:12 -07003500 //==- Const ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003501 case IntrinsicHelper::ConstInt:
3502 case IntrinsicHelper::ConstLong: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003503 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003504 }
TDYa127920be7c2012-09-10 17:13:22 -07003505 case IntrinsicHelper::ConstFloat: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003506 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3507 irb_.getJFloatTy());
Logan Chien75e4b602012-07-23 14:24:12 -07003508 }
TDYa127920be7c2012-09-10 17:13:22 -07003509 case IntrinsicHelper::ConstDouble: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003510 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3511 irb_.getJDoubleTy());
3512 }
TDYa127920be7c2012-09-10 17:13:22 -07003513 case IntrinsicHelper::ConstObj: {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003514 CHECK(LV2UInt(call_inst.getArgOperand(0)) == 0);
3515 return irb_.getJNull();
Logan Chien75e4b602012-07-23 14:24:12 -07003516 }
3517
3518 //==- Method Info ------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003519 case IntrinsicHelper::MethodInfo: {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003520 // Nothing to be done, because MethodInfo carries optional hints that are
3521 // not needed by the portable path.
Logan Chien75e4b602012-07-23 14:24:12 -07003522 return NULL;
3523 }
3524
3525 //==- Copy -------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003526 case IntrinsicHelper::CopyInt:
3527 case IntrinsicHelper::CopyFloat:
3528 case IntrinsicHelper::CopyLong:
3529 case IntrinsicHelper::CopyDouble:
3530 case IntrinsicHelper::CopyObj: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003531 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003532 }
3533
3534 //==- Shift ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003535 case IntrinsicHelper::SHLLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003536 return Expand_IntegerShift(call_inst.getArgOperand(0),
3537 call_inst.getArgOperand(1),
3538 kIntegerSHL, kLong);
3539 }
TDYa127920be7c2012-09-10 17:13:22 -07003540 case IntrinsicHelper::SHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003541 return Expand_IntegerShift(call_inst.getArgOperand(0),
3542 call_inst.getArgOperand(1),
3543 kIntegerSHR, kLong);
3544 }
TDYa127920be7c2012-09-10 17:13:22 -07003545 case IntrinsicHelper::USHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003546 return Expand_IntegerShift(call_inst.getArgOperand(0),
3547 call_inst.getArgOperand(1),
3548 kIntegerUSHR, kLong);
3549 }
TDYa127920be7c2012-09-10 17:13:22 -07003550 case IntrinsicHelper::SHLInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003551 return Expand_IntegerShift(call_inst.getArgOperand(0),
3552 call_inst.getArgOperand(1),
3553 kIntegerSHL, kInt);
3554 }
TDYa127920be7c2012-09-10 17:13:22 -07003555 case IntrinsicHelper::SHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003556 return Expand_IntegerShift(call_inst.getArgOperand(0),
3557 call_inst.getArgOperand(1),
3558 kIntegerSHR, kInt);
3559 }
TDYa127920be7c2012-09-10 17:13:22 -07003560 case IntrinsicHelper::USHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003561 return Expand_IntegerShift(call_inst.getArgOperand(0),
3562 call_inst.getArgOperand(1),
3563 kIntegerUSHR, kInt);
3564 }
3565
3566 //==- Conversion -------------------------------------------------------==//
TDYa127a1b21852012-07-23 03:20:39 -07003567 case IntrinsicHelper::IntToChar: {
3568 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
3569 irb_.getJIntTy());
3570 }
3571 case IntrinsicHelper::IntToShort: {
3572 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
3573 irb_.getJIntTy());
3574 }
3575 case IntrinsicHelper::IntToByte: {
3576 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
3577 irb_.getJIntTy());
3578 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003579
TDYa12787caa7e2012-08-25 23:23:27 -07003580 //==- Exception --------------------------------------------------------==//
3581 case IntrinsicHelper::CatchTargets: {
TDYa12755e5e6c2012-09-11 15:14:42 -07003582 UpdatePhiInstruction(current_bb_, irb_.GetInsertBlock());
TDYa12787caa7e2012-08-25 23:23:27 -07003583 llvm::SwitchInst* si = llvm::dyn_cast<llvm::SwitchInst>(call_inst.getNextNode());
3584 CHECK(si != NULL);
3585 irb_.CreateBr(si->getDefaultDest());
3586 si->eraseFromParent();
3587 return call_inst.getArgOperand(0);
3588 }
3589
Logan Chien75e4b602012-07-23 14:24:12 -07003590 //==- Unknown Cases ----------------------------------------------------==//
3591 case IntrinsicHelper::MaxIntrinsicId:
3592 case IntrinsicHelper::UnknownId:
3593 //default:
3594 // NOTE: "default" is intentionally commented so that C/C++ compiler will
3595 // give some warning on unmatched cases.
3596 // NOTE: We should not implement these cases.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003597 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003598 }
Logan Chien75e4b602012-07-23 14:24:12 -07003599 UNIMPLEMENTED(FATAL) << "Unexpected GBC intrinsic: " << static_cast<int>(intr_id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003600 return NULL;
3601}
3602
3603} // anonymous namespace
3604
3605namespace art {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003606
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003607namespace compiler_llvm {
3608
3609llvm::FunctionPass*
3610CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb) {
3611 return new GBCExpanderPass(intrinsic_helper, irb);
3612}
3613
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003614llvm::FunctionPass*
3615CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
3616 Compiler* compiler, OatCompilationUnit* oat_compilation_unit) {
3617 if (compiler != NULL) {
3618 return new GBCExpanderPass(intrinsic_helper, irb,
3619 compiler, oat_compilation_unit);
3620 } else {
3621 return new GBCExpanderPass(intrinsic_helper, irb);
3622 }
3623}
3624
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003625} // namespace compiler_llvm
3626} // namespace art