blob: 484dd77e3a3078d05cc37550f6ead12e5709552a [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
TDYa127920be7c2012-09-10 17:13:22 -070027#include "compiler/CompilerIR.h"
28using art::kMIRIgnoreNullCheck;
29using art::kMIRIgnoreRangeCheck;
30
Shih-wei Liao21d28f52012-06-12 05:55:00 -070031#include <llvm/ADT/STLExtras.h>
32#include <llvm/Intrinsics.h>
Logan Chiend36a2ac2012-08-04 00:37:30 +080033#include <llvm/Metadata.h>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070034#include <llvm/Pass.h>
35#include <llvm/Support/CFG.h>
36#include <llvm/Support/InstIterator.h>
37
38#include <vector>
TDYa127aa558872012-08-16 05:11:07 -070039#include <map>
40#include <utility>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070041
TDYa127920be7c2012-09-10 17:13:22 -070042using namespace art::compiler_llvm;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070043
44using art::greenland::IntrinsicHelper;
45
Shih-wei Liaob2596522012-09-14 16:36:11 -070046namespace art {
47extern char remapShorty(char shortyType);
48};
49
Shih-wei Liao21d28f52012-06-12 05:55:00 -070050namespace {
51
52class GBCExpanderPass : public llvm::FunctionPass {
53 private:
54 const IntrinsicHelper& intrinsic_helper_;
55 IRBuilder& irb_;
56
57 llvm::LLVMContext& context_;
58 RuntimeSupportBuilder& rtb_;
59
60 private:
61 llvm::AllocaInst* shadow_frame_;
62 llvm::Value* old_shadow_frame_;
Shih-wei Liao6c279682012-10-16 18:07:43 -070063 uint16_t num_shadow_frame_refs_;
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
220 void Expand_AllocaShadowFrame(llvm::Value* num_entry_value);
221
222 void Expand_SetShadowFrameEntry(llvm::Value* obj, llvm::Value* entry_idx);
223
224 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()),
Shih-wei Liao6c279682012-10-16 18:07:43 -0700329 shadow_frame_(NULL), old_shadow_frame_(NULL), num_shadow_frame_refs_(0),
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()),
Shih-wei Liao6c279682012-10-16 18:07:43 -0700339 shadow_frame_(NULL), old_shadow_frame_(NULL), num_shadow_frame_refs_(0),
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;
Shih-wei Liao6c279682012-10-16 18:07:43 -0700369 num_shadow_frame_refs_ = 0;
TDYa1275e869b62012-07-25 00:45:39 -0700370 func_ = &func;
Logan Chien67645d82012-08-17 09:10:54 +0800371 changed_ = false; // Assume unchanged
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700372
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700373#if defined(ART_USE_QUICK_COMPILER)
374 basic_blocks_.resize(code_item_->insns_size_in_code_units_);
375 basic_block_landing_pads_.resize(code_item_->tries_size_, NULL);
376 basic_block_unwind_ = NULL;
377 for (llvm::Function::iterator bb_iter = func_->begin(), bb_end = func_->end();
378 bb_iter != bb_end;
379 ++bb_iter) {
380 if (bb_iter->begin()->getMetadata("DexOff") == NULL) {
381 continue;
382 }
383 uint32_t dex_pc = LV2UInt(bb_iter->begin()->getMetadata("DexOff")->getOperand(0));
384 basic_blocks_[dex_pc] = bb_iter;
385 }
386#endif
387
Logan Chien67645d82012-08-17 09:10:54 +0800388 // Insert stack overflow check
389 InsertStackOverflowCheck(func); // TODO: Use intrinsic.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700390
Logan Chien67645d82012-08-17 09:10:54 +0800391 // Rewrite the intrinsics
392 RewriteFunction();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700393
394 VERIFY_LLVM_FUNCTION(func);
395
Logan Chien67645d82012-08-17 09:10:54 +0800396 return changed_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700397}
398
Logan Chien67645d82012-08-17 09:10:54 +0800399void GBCExpanderPass::RewriteBasicBlock(llvm::BasicBlock* original_block) {
400 llvm::BasicBlock* curr_basic_block = original_block;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700401
Logan Chien67645d82012-08-17 09:10:54 +0800402 llvm::BasicBlock::iterator inst_iter = original_block->begin();
403 llvm::BasicBlock::iterator inst_end = original_block->end();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700404
Logan Chien67645d82012-08-17 09:10:54 +0800405 while (inst_iter != inst_end) {
406 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst_iter);
407 IntrinsicHelper::IntrinsicId intr_id = IntrinsicHelper::UnknownId;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700408
Logan Chien67645d82012-08-17 09:10:54 +0800409 if (call_inst) {
410 llvm::Function* callee_func = call_inst->getCalledFunction();
411 intr_id = intrinsic_helper_.GetIntrinsicId(callee_func);
412 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700413
Logan Chien67645d82012-08-17 09:10:54 +0800414 if (intr_id == IntrinsicHelper::UnknownId) {
415 // This is not intrinsic call. Skip this instruction.
416 ++inst_iter;
417 continue;
418 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700419
Logan Chien67645d82012-08-17 09:10:54 +0800420 // Rewrite the intrinsic and change the function
421 changed_ = true;
422 irb_.SetInsertPoint(inst_iter);
423
424 // Expand the intrinsic
425 if (llvm::Value* new_value = ExpandIntrinsic(intr_id, *call_inst)) {
426 inst_iter->replaceAllUsesWith(new_value);
427 }
428
429 // Remove the old intrinsic call instruction
430 llvm::BasicBlock::iterator old_inst = inst_iter++;
431 old_inst->eraseFromParent();
432
433 // Splice the instruction to the new basic block
434 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
435 if (next_basic_block != curr_basic_block) {
436 next_basic_block->getInstList().splice(
437 irb_.GetInsertPoint(), curr_basic_block->getInstList(),
438 inst_iter, inst_end);
439 curr_basic_block = next_basic_block;
440 inst_end = curr_basic_block->end();
441 }
442 }
443}
444
445
446void GBCExpanderPass::RewriteFunction() {
447 size_t num_basic_blocks = func_->getBasicBlockList().size();
448 // NOTE: We are not using (bb_iter != bb_end) as the for-loop condition,
449 // because we will create new basic block while expanding the intrinsics.
450 // We only want to iterate through the input basic blocks.
451
TDYa127aa558872012-08-16 05:11:07 -0700452 landing_pad_phi_mapping_.clear();
453
Logan Chien67645d82012-08-17 09:10:54 +0800454 for (llvm::Function::iterator bb_iter = func_->begin();
455 num_basic_blocks > 0; ++bb_iter, --num_basic_blocks) {
Shih-wei Liao627d8c42012-08-24 08:31:18 -0700456 // Set insert point to current basic block.
457 irb_.SetInsertPoint(bb_iter);
Logan Chien67645d82012-08-17 09:10:54 +0800458
TDYa12755e5e6c2012-09-11 15:14:42 -0700459 current_bb_ = bb_iter;
TDYa127aa558872012-08-16 05:11:07 -0700460
Logan Chien67645d82012-08-17 09:10:54 +0800461 // Rewrite the basic block
462 RewriteBasicBlock(bb_iter);
463
464 // Update the phi-instructions in the successor basic block
465 llvm::BasicBlock* last_block = irb_.GetInsertBlock();
466 if (last_block != bb_iter) {
467 UpdatePhiInstruction(bb_iter, last_block);
468 }
469 }
TDYa127aa558872012-08-16 05:11:07 -0700470
471 typedef std::map<llvm::PHINode*, llvm::PHINode*> HandlerPHIMap;
472 HandlerPHIMap handler_phi;
473 // Iterate every used landing pad basic block
474 for (size_t i = 0, ei = basic_block_landing_pads_.size(); i != ei; ++i) {
475 llvm::BasicBlock* lbb = basic_block_landing_pads_[i];
476 if (lbb == NULL) {
477 continue;
478 }
479
480 llvm::TerminatorInst* term_inst = lbb->getTerminator();
481 std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> >& rewrite_pair
482 = landing_pad_phi_mapping_[lbb];
483 irb_.SetInsertPoint(lbb->begin());
484
485 // Iterate every succeeding basic block (catch block)
486 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
487 succ_iter != succ_end; ++succ_iter) {
488 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
489
490 // Iterate every phi instructions in the succeeding basic block
491 for (llvm::BasicBlock::iterator
492 inst_iter = succ_basic_block->begin(),
493 inst_end = succ_basic_block->end();
494 inst_iter != inst_end; ++inst_iter) {
495 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
496
497 if (!phi) {
498 break; // Meet non-phi instruction. Done.
499 }
500
501 if (handler_phi[phi] == NULL) {
502 handler_phi[phi] = llvm::PHINode::Create(phi->getType(), 1);
503 }
504
505 // Create new_phi in landing pad
506 llvm::PHINode* new_phi = irb_.CreatePHI(phi->getType(), rewrite_pair.size());
507 // Insert all incoming value into new_phi by rewrite_pair
508 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
509 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
510 llvm::BasicBlock* new_bb = rewrite_pair[j].second;
511 new_phi->addIncoming(phi->getIncomingValueForBlock(old_bb), new_bb);
512 }
513 // Delete all incoming value from phi by rewrite_pair
514 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
515 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
516 int old_bb_idx = phi->getBasicBlockIndex(old_bb);
517 if (old_bb_idx >= 0) {
518 phi->removeIncomingValue(old_bb_idx, false);
519 }
520 }
521 // Insert new_phi into new handler phi
522 handler_phi[phi]->addIncoming(new_phi, lbb);
523 }
524 }
525 }
526
527 // Replace all handler phi
528 // We can't just use the old handler phi, because some exception edges will disappear after we
529 // compute fast-path.
530 for (HandlerPHIMap::iterator it = handler_phi.begin(); it != handler_phi.end(); ++it) {
531 llvm::PHINode* old_phi = it->first;
532 llvm::PHINode* new_phi = it->second;
533 new_phi->insertBefore(old_phi);
534 old_phi->replaceAllUsesWith(new_phi);
535 old_phi->eraseFromParent();
536 }
Logan Chien67645d82012-08-17 09:10:54 +0800537}
538
539void GBCExpanderPass::UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
540 llvm::BasicBlock* new_basic_block) {
541 llvm::TerminatorInst* term_inst = new_basic_block->getTerminator();
542
543 if (!term_inst) {
544 return; // No terminating instruction in new_basic_block. Nothing to do.
545 }
546
547 // Iterate every succeeding basic block
548 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
549 succ_iter != succ_end; ++succ_iter) {
550 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
551
552 // Iterate every phi instructions in the succeeding basic block
553 for (llvm::BasicBlock::iterator
554 inst_iter = succ_basic_block->begin(),
555 inst_end = succ_basic_block->end();
556 inst_iter != inst_end; ++inst_iter) {
557 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
558
559 if (!phi) {
560 break; // Meet non-phi instruction. Done.
561 }
562
563 // Update the incoming block of this phi instruction
564 for (llvm::PHINode::block_iterator
565 ibb_iter = phi->block_begin(), ibb_end = phi->block_end();
566 ibb_iter != ibb_end; ++ibb_iter) {
567 if (*ibb_iter == old_basic_block) {
568 *ibb_iter = new_basic_block;
569 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700570 }
571 }
572 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700573}
574
575llvm::Value* GBCExpanderPass::ExpandToRuntime(runtime_support::RuntimeId rt,
576 llvm::CallInst& inst) {
577 // Some GBC intrinsic can directly replace with IBC runtime. "Directly" means
578 // the arguments passed to the GBC intrinsic are as the same as IBC runtime
579 // function, therefore only called function is needed to change.
580 unsigned num_args = inst.getNumArgOperands();
581
582 if (num_args <= 0) {
583 return irb_.CreateCall(irb_.GetRuntime(rt));
584 } else {
585 std::vector<llvm::Value*> args;
586 for (unsigned i = 0; i < num_args; i++) {
587 args.push_back(inst.getArgOperand(i));
588 }
589
590 return irb_.CreateCall(irb_.GetRuntime(rt), args);
591 }
592}
593
Logan Chien67645d82012-08-17 09:10:54 +0800594void
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700595GBCExpanderPass::EmitStackOverflowCheck(llvm::Instruction* first_non_alloca) {
596 llvm::Function* func = first_non_alloca->getParent()->getParent();
597 llvm::Module* module = func->getParent();
598
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700599 // Call llvm intrinsic function to get frame address.
600 llvm::Function* frameaddress =
601 llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
602
603 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
604 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
605
606 // Cast i8* to int
607 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
608
609 // Get thread.stack_end_
610 llvm::Value* stack_end =
TDYa127920be7c2012-09-10 17:13:22 -0700611 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::StackEndOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700612 irb_.getPtrEquivIntTy(),
613 kTBAARuntimeInfo);
614
615 // Check the frame address < thread.stack_end_ ?
616 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
617
618 llvm::BasicBlock* block_exception =
619 llvm::BasicBlock::Create(context_, "stack_overflow", func);
620
621 llvm::BasicBlock* block_continue =
622 llvm::BasicBlock::Create(context_, "stack_overflow_cont", func);
623
624 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
625
626 // If stack overflow, throw exception.
627 irb_.SetInsertPoint(block_exception);
628 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowStackOverflowException));
629
630 // Unwind.
631 llvm::Type* ret_type = func->getReturnType();
632 if (ret_type->isVoidTy()) {
633 irb_.CreateRetVoid();
634 } else {
635 // The return value is ignored when there's an exception. MethodCompiler
636 // returns zero value under the the corresponding return type in this case.
637 // GBCExpander returns LLVM undef value here for brevity
638 irb_.CreateRet(llvm::UndefValue::get(ret_type));
639 }
640
641 irb_.SetInsertPoint(block_continue);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700642}
643
TDYa127920be7c2012-09-10 17:13:22 -0700644llvm::Value* GBCExpanderPass::EmitLoadDexCacheAddr(art::MemberOffset offset) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700645 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
646
647 return irb_.LoadFromObjectOffset(method_object_addr,
648 offset.Int32Value(),
649 irb_.getJObjectTy(),
650 kTBAAConstJObject);
651}
652
653llvm::Value*
654GBCExpanderPass::EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
655 llvm::Value* static_storage_dex_cache_addr =
Mathieu Chartier66f19252012-09-18 08:57:04 -0700656 EmitLoadDexCacheAddr(art::AbstractMethod::DexCacheInitializedStaticStorageOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700657
658 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
659
660 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
661}
662
663llvm::Value*
664GBCExpanderPass::EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
665 llvm::Value* resolved_type_dex_cache_addr =
Mathieu Chartier66f19252012-09-18 08:57:04 -0700666 EmitLoadDexCacheAddr(art::AbstractMethod::DexCacheResolvedTypesOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700667
668 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
669
670 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
671}
672
673llvm::Value* GBCExpanderPass::
674EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
675 llvm::Value* resolved_method_dex_cache_addr =
Mathieu Chartier66f19252012-09-18 08:57:04 -0700676 EmitLoadDexCacheAddr(art::AbstractMethod::DexCacheResolvedMethodsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700677
678 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
679
680 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
681}
682
683llvm::Value* GBCExpanderPass::
684EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
685 llvm::Value* string_dex_cache_addr =
Mathieu Chartier66f19252012-09-18 08:57:04 -0700686 EmitLoadDexCacheAddr(art::AbstractMethod::DexCacheStringsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700687
688 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
689
690 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
691}
692
693llvm::Value* GBCExpanderPass::EmitLoadMethodObjectAddr() {
694 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
695 return parent_func->arg_begin();
696}
697
698llvm::Value* GBCExpanderPass::EmitLoadArrayLength(llvm::Value* array) {
699 // Load array length
700 return irb_.LoadFromObjectOffset(array,
TDYa127920be7c2012-09-10 17:13:22 -0700701 art::Array::LengthOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700702 irb_.getJIntTy(),
703 kTBAAConstJObject);
704
705}
706
707llvm::Value*
708GBCExpanderPass::EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
709 llvm::Value* callee_method_object_field_addr =
710 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
711
712 return irb_.CreateLoad(callee_method_object_field_addr, kTBAAJRuntime);
713}
714
715llvm::Value* GBCExpanderPass::
716EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, llvm::Value* this_addr) {
717 // Load class object of *this* pointer
718 llvm::Value* class_object_addr =
719 irb_.LoadFromObjectOffset(this_addr,
TDYa127920be7c2012-09-10 17:13:22 -0700720 art::Object::ClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700721 irb_.getJObjectTy(),
722 kTBAAConstJObject);
723
724 // Load vtable address
725 llvm::Value* vtable_addr =
726 irb_.LoadFromObjectOffset(class_object_addr,
TDYa127920be7c2012-09-10 17:13:22 -0700727 art::Class::VTableOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700728 irb_.getJObjectTy(),
729 kTBAAConstJObject);
730
731 // Load callee method object
732 llvm::Value* vtable_idx_value =
733 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
734
735 llvm::Value* method_field_addr =
736 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
737
738 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
739}
740
741// Emit Array GetElementPtr
742llvm::Value* GBCExpanderPass::EmitArrayGEP(llvm::Value* array_addr,
743 llvm::Value* index_value,
744 JType elem_jty) {
745
746 int data_offset;
747 if (elem_jty == kLong || elem_jty == kDouble ||
TDYa127920be7c2012-09-10 17:13:22 -0700748 (elem_jty == kObject && sizeof(uint64_t) == sizeof(art::Object*))) {
749 data_offset = art::Array::DataOffset(sizeof(int64_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700750 } else {
TDYa127920be7c2012-09-10 17:13:22 -0700751 data_offset = art::Array::DataOffset(sizeof(int32_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700752 }
753
754 llvm::Constant* data_offset_value =
755 irb_.getPtrEquivInt(data_offset);
756
757 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
758
759 llvm::Value* array_data_addr =
760 irb_.CreatePtrDisp(array_addr, data_offset_value,
761 elem_type->getPointerTo());
762
763 return irb_.CreateGEP(array_data_addr, index_value);
764}
765
766void GBCExpanderPass::Expand_TestSuspend(llvm::CallInst& call_inst) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700767 irb_.Runtime().EmitTestSuspend();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700768 return;
769}
770
TDYa1279a129452012-07-19 03:10:08 -0700771void GBCExpanderPass::Expand_MarkGCCard(llvm::CallInst& call_inst) {
TDYa1279a129452012-07-19 03:10:08 -0700772 irb_.Runtime().EmitMarkGCCard(call_inst.getArgOperand(0), call_inst.getArgOperand(1));
TDYa1279a129452012-07-19 03:10:08 -0700773 return;
774}
775
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700776llvm::Value*
777GBCExpanderPass::Expand_LoadStringFromDexCache(llvm::Value* string_idx_value) {
778 uint32_t string_idx =
779 llvm::cast<llvm::ConstantInt>(string_idx_value)->getZExtValue();
780
781 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
782
783 return irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
784}
785
786llvm::Value*
787GBCExpanderPass::Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value) {
788 uint32_t type_idx =
789 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
790
791 llvm::Value* type_field_addr =
792 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
793
794 return irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
795}
796
797void GBCExpanderPass::Expand_LockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700798 rtb_.EmitLockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700799 return;
800}
801
802void GBCExpanderPass::Expand_UnlockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700803 rtb_.EmitUnlockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700804 return;
805}
806
807llvm::Value* GBCExpanderPass::Expand_ArrayGet(llvm::Value* array_addr,
808 llvm::Value* index_value,
809 JType elem_jty) {
810 llvm::Value* array_elem_addr =
811 EmitArrayGEP(array_addr, index_value, elem_jty);
812
813 return irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
814}
815
816void GBCExpanderPass::Expand_ArrayPut(llvm::Value* new_value,
817 llvm::Value* array_addr,
818 llvm::Value* index_value,
819 JType elem_jty) {
820 llvm::Value* array_elem_addr =
821 EmitArrayGEP(array_addr, index_value, elem_jty);
822
823 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
824
825 return;
826}
827
828void GBCExpanderPass::Expand_FilledNewArray(llvm::CallInst& call_inst) {
829 // Most of the codes refer to MethodCompiler::EmitInsn_FilledNewArray
830 llvm::Value* array = call_inst.getArgOperand(0);
831
832 uint32_t element_jty =
833 llvm::cast<llvm::ConstantInt>(call_inst.getArgOperand(1))->getZExtValue();
834
835 DCHECK(call_inst.getNumArgOperands() > 2);
836 unsigned num_elements = (call_inst.getNumArgOperands() - 2);
837
838 bool is_elem_int_ty = (static_cast<JType>(element_jty) == kInt);
839
840 uint32_t alignment;
841 llvm::Constant* elem_size;
842 llvm::PointerType* field_type;
843
844 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
845 // as the element, thus we are only checking 2 cases: primitive int and
846 // non-primitive type.
847 if (is_elem_int_ty) {
848 alignment = sizeof(int32_t);
849 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
850 field_type = irb_.getJIntTy()->getPointerTo();
851 } else {
852 alignment = irb_.getSizeOfPtrEquivInt();
853 elem_size = irb_.getSizeOfPtrEquivIntValue();
854 field_type = irb_.getJObjectTy()->getPointerTo();
855 }
856
857 llvm::Value* data_field_offset =
TDYa127920be7c2012-09-10 17:13:22 -0700858 irb_.getPtrEquivInt(art::Array::DataOffset(alignment).Int32Value());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700859
860 llvm::Value* data_field_addr =
861 irb_.CreatePtrDisp(array, data_field_offset, field_type);
862
863 for (unsigned i = 0; i < num_elements; ++i) {
864 // Values to fill the array begin at the 3rd argument
865 llvm::Value* reg_value = call_inst.getArgOperand(2 + i);
866
867 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
868
869 data_field_addr =
870 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
871 }
872
873 return;
874}
875
876llvm::Value* GBCExpanderPass::Expand_IGetFast(llvm::Value* field_offset_value,
877 llvm::Value* /*is_volatile_value*/,
878 llvm::Value* object_addr,
879 JType field_jty) {
880 int field_offset =
881 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
882
883 DCHECK_GE(field_offset, 0);
884
885 llvm::PointerType* field_type =
886 irb_.getJType(field_jty, kField)->getPointerTo();
887
888 field_offset_value = irb_.getPtrEquivInt(field_offset);
889
890 llvm::Value* field_addr =
891 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
892
893 // TODO: Check is_volatile. We need to generate atomic load instruction
894 // when is_volatile is true.
895 return irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
896}
897
898void GBCExpanderPass::Expand_IPutFast(llvm::Value* field_offset_value,
899 llvm::Value* /* is_volatile_value */,
900 llvm::Value* object_addr,
901 llvm::Value* new_value,
902 JType field_jty) {
903 int field_offset =
904 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
905
906 DCHECK_GE(field_offset, 0);
907
908 llvm::PointerType* field_type =
909 irb_.getJType(field_jty, kField)->getPointerTo();
910
911 field_offset_value = irb_.getPtrEquivInt(field_offset);
912
913 llvm::Value* field_addr =
914 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
915
916 // TODO: Check is_volatile. We need to generate atomic store instruction
917 // when is_volatile is true.
918 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
919
920 return;
921}
922
923llvm::Value* GBCExpanderPass::Expand_SGetFast(llvm::Value* static_storage_addr,
924 llvm::Value* field_offset_value,
925 llvm::Value* /*is_volatile_value*/,
926 JType field_jty) {
927 int field_offset =
928 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
929
930 DCHECK_GE(field_offset, 0);
931
932 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
933
934 llvm::Value* static_field_addr =
935 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
936 irb_.getJType(field_jty, kField)->getPointerTo());
937
938 // TODO: Check is_volatile. We need to generate atomic store instruction
939 // when is_volatile is true.
940 return irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
941}
942
943void GBCExpanderPass::Expand_SPutFast(llvm::Value* static_storage_addr,
944 llvm::Value* field_offset_value,
945 llvm::Value* /* is_volatile_value */,
946 llvm::Value* new_value,
947 JType field_jty) {
948 int field_offset =
949 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
950
951 DCHECK_GE(field_offset, 0);
952
953 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
954
955 llvm::Value* static_field_addr =
956 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
957 irb_.getJType(field_jty, kField)->getPointerTo());
958
959 // TODO: Check is_volatile. We need to generate atomic store instruction
960 // when is_volatile is true.
961 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
962
963 return;
964}
965
966llvm::Value*
967GBCExpanderPass::Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr) {
968 return irb_.LoadFromObjectOffset(method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700969 art::AbstractMethod::DeclaringClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700970 irb_.getJObjectTy(),
971 kTBAAConstJObject);
972}
973
974llvm::Value*
975GBCExpanderPass::Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value) {
976 uint32_t type_idx =
977 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
978
979 llvm::Value* storage_field_addr =
980 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
981
982 return irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
983}
984
985llvm::Value*
986GBCExpanderPass::Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value) {
987 uint32_t callee_method_idx =
988 llvm::cast<llvm::ConstantInt>(callee_method_idx_value)->getZExtValue();
989
990 return EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
991}
992
993llvm::Value* GBCExpanderPass::Expand_GetVirtualCalleeMethodObjAddrFast(
994 llvm::Value* vtable_idx_value,
995 llvm::Value* this_addr) {
996 int vtable_idx =
997 llvm::cast<llvm::ConstantInt>(vtable_idx_value)->getSExtValue();
998
999 return EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
1000}
1001
1002llvm::Value* GBCExpanderPass::Expand_Invoke(llvm::CallInst& call_inst) {
1003 // Most of the codes refer to MethodCompiler::EmitInsn_Invoke
1004 llvm::Value* callee_method_object_addr = call_inst.getArgOperand(0);
1005 unsigned num_args = call_inst.getNumArgOperands();
1006 llvm::Type* ret_type = call_inst.getType();
1007
1008 // Determine the function type of the callee method
1009 std::vector<llvm::Type*> args_type;
1010 std::vector<llvm::Value*> args;
1011 for (unsigned i = 0; i < num_args; i++) {
1012 args.push_back(call_inst.getArgOperand(i));
1013 args_type.push_back(args[i]->getType());
1014 }
1015
1016 llvm::FunctionType* callee_method_type =
1017 llvm::FunctionType::get(ret_type, args_type, false);
1018
1019 llvm::Value* code_addr =
1020 irb_.LoadFromObjectOffset(callee_method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001021 art::AbstractMethod::GetCodeOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001022 callee_method_type->getPointerTo(),
1023 kTBAAJRuntime);
1024
1025 // Invoke callee
1026 llvm::Value* retval = irb_.CreateCall(code_addr, args);
1027
1028 return retval;
1029}
1030
TDYa1274ec8ccd2012-08-11 07:04:57 -07001031llvm::Value* GBCExpanderPass::Expand_DivRem(llvm::CallInst& call_inst,
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001032 bool is_div, JType op_jty) {
TDYa1274ec8ccd2012-08-11 07:04:57 -07001033 llvm::Value* dividend = call_inst.getArgOperand(0);
1034 llvm::Value* divisor = call_inst.getArgOperand(1);
1035#if defined(ART_USE_QUICK_COMPILER)
1036 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1037 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
1038#endif
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001039 // Most of the codes refer to MethodCompiler::EmitIntDivRemResultComputation
1040
1041 // Check the special case: MININT / -1 = MININT
1042 // That case will cause overflow, which is undefined behavior in llvm.
1043 // So we check the divisor is -1 or not, if the divisor is -1, we do
1044 // the special path to avoid undefined behavior.
1045 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
1046 llvm::Value* zero = irb_.getJZero(op_jty);
1047 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
1048
TDYa1275e869b62012-07-25 00:45:39 -07001049 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001050 llvm::BasicBlock* eq_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1051 llvm::BasicBlock* ne_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1052 llvm::BasicBlock* neg_one_cont =
1053 llvm::BasicBlock::Create(context_, "", parent);
1054
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001055 llvm::Value* is_equal_neg_one = irb_.CreateICmpEQ(divisor, neg_one);
1056 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
1057
1058 // If divisor == -1
1059 irb_.SetInsertPoint(eq_neg_one);
1060 llvm::Value* eq_result;
1061 if (is_div) {
1062 // We can just change from "dividend div -1" to "neg dividend". The sub
1063 // don't care the sign/unsigned because of two's complement representation.
1064 // And the behavior is what we want:
1065 // -(2^n) (2^n)-1
1066 // MININT < k <= MAXINT -> mul k -1 = -k
1067 // MININT == k -> mul k -1 = k
1068 //
1069 // LLVM use sub to represent 'neg'
1070 eq_result = irb_.CreateSub(zero, dividend);
1071 } else {
1072 // Everything modulo -1 will be 0.
1073 eq_result = zero;
1074 }
1075 irb_.CreateBr(neg_one_cont);
1076
1077 // If divisor != -1, just do the division.
1078 irb_.SetInsertPoint(ne_neg_one);
1079 llvm::Value* ne_result;
1080 if (is_div) {
1081 ne_result = irb_.CreateSDiv(dividend, divisor);
1082 } else {
1083 ne_result = irb_.CreateSRem(dividend, divisor);
1084 }
1085 irb_.CreateBr(neg_one_cont);
1086
1087 irb_.SetInsertPoint(neg_one_cont);
1088 llvm::PHINode* result = irb_.CreatePHI(op_type, 2);
1089 result->addIncoming(eq_result, eq_neg_one);
1090 result->addIncoming(ne_result, ne_neg_one);
1091
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001092 return result;
1093}
1094
1095void GBCExpanderPass::Expand_AllocaShadowFrame(llvm::Value* num_entry_value) {
1096 // Most of the codes refer to MethodCompiler::EmitPrologueAllocShadowFrame and
1097 // MethodCompiler::EmitPushShadowFrame
Shih-wei Liao6c279682012-10-16 18:07:43 -07001098 num_shadow_frame_refs_ =
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001099 llvm::cast<llvm::ConstantInt>(num_entry_value)->getZExtValue();
1100
1101 llvm::StructType* shadow_frame_type =
Shih-wei Liao6c279682012-10-16 18:07:43 -07001102 irb_.getShadowFrameTy(num_shadow_frame_refs_);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001103
1104 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
1105
1106 // Alloca a pointer to old shadow frame
1107 old_shadow_frame_ =
1108 irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
1109
1110 // Zero-initialization of the shadow frame table
1111 llvm::Value* shadow_frame_table =
1112 irb_.CreateConstGEP2_32(shadow_frame_, 0, 1);
1113 llvm::Type* table_type = shadow_frame_type->getElementType(1);
1114
1115 llvm::ConstantAggregateZero* zero_initializer =
1116 llvm::ConstantAggregateZero::get(table_type);
1117
1118 irb_.CreateStore(zero_initializer, shadow_frame_table, kTBAAShadowFrame);
1119
1120 // Push the shadow frame
1121 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1122
1123 // Push the shadow frame
1124 llvm::Value* shadow_frame_upcast =
1125 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
1126
1127 llvm::Value* result = rtb_.EmitPushShadowFrame(shadow_frame_upcast,
1128 method_object_addr,
Shih-wei Liao6c279682012-10-16 18:07:43 -07001129 num_shadow_frame_refs_,
1130 0);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001131
1132 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
1133
1134 return;
1135}
1136
1137void GBCExpanderPass::Expand_SetShadowFrameEntry(llvm::Value* obj,
1138 llvm::Value* entry_idx) {
1139 DCHECK(shadow_frame_ != NULL);
1140
1141 llvm::Value* gep_index[] = {
1142 irb_.getInt32(0), // No pointer displacement
1143 irb_.getInt32(1), // SIRT
1144 entry_idx // Pointer field
1145 };
1146
1147 llvm::Value* entry_addr = irb_.CreateGEP(shadow_frame_, gep_index);
TDYa127347166a2012-08-23 12:23:44 -07001148#if defined(ART_USE_QUICK_COMPILER)
1149 if (obj->getType() != irb_.getJObjectTy()) {
1150 obj = irb_.getJNull();
1151 }
1152#endif
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001153 irb_.CreateStore(obj, entry_addr, kTBAAShadowFrame);
1154 return;
1155}
1156
1157void GBCExpanderPass::Expand_PopShadowFrame() {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001158#if defined(ART_USE_QUICK_COMPILER)
1159 if (old_shadow_frame_ == NULL) {
1160 return;
1161 }
1162#endif
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001163 rtb_.EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
1164 return;
1165}
1166
1167void GBCExpanderPass::Expand_UpdateDexPC(llvm::Value* dex_pc_value) {
1168 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07001169 art::ShadowFrame::DexPCOffset(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001170 dex_pc_value,
1171 kTBAAShadowFrame);
1172 return;
1173}
1174
Logan Chien67645d82012-08-17 09:10:54 +08001175void GBCExpanderPass::InsertStackOverflowCheck(llvm::Function& func) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001176 // DexLang generates all alloca instruction in the first basic block of the
1177 // FUNC and also there's no any alloca instructions after the first non-alloca
1178 // instruction
1179
Logan Chien67645d82012-08-17 09:10:54 +08001180 llvm::BasicBlock* first_basic_block = &func.front();
1181
1182 // Look for first non-alloca instruction
1183 llvm::BasicBlock::iterator first_non_alloca = first_basic_block->begin();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001184 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1185 ++first_non_alloca;
1186 }
1187
Logan Chien67645d82012-08-17 09:10:54 +08001188 irb_.SetInsertPoint(first_non_alloca);
1189
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001190 // Insert stack overflow check codes before first_non_alloca (i.e., after all
1191 // alloca instructions)
Logan Chien67645d82012-08-17 09:10:54 +08001192 EmitStackOverflowCheck(&*first_non_alloca);
1193
TDYa127890ea892012-08-22 10:49:42 -07001194#if defined(ART_USE_QUICK_COMPILER)
1195 irb_.Runtime().EmitTestSuspend();
1196#endif
1197
Logan Chien67645d82012-08-17 09:10:54 +08001198 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
1199 if (next_basic_block != first_basic_block) {
1200 // Splice the rest of the instruction to the continuing basic block
1201 next_basic_block->getInstList().splice(
1202 irb_.GetInsertPoint(), first_basic_block->getInstList(),
1203 first_non_alloca, first_basic_block->end());
1204
1205 // Rewrite the basic block
1206 RewriteBasicBlock(next_basic_block);
1207
1208 // Update the phi-instructions in the successor basic block
1209 UpdatePhiInstruction(first_basic_block, irb_.GetInsertBlock());
1210 }
1211
1212 // We have changed the basic block
1213 changed_ = true;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001214}
1215
TDYa1275e869b62012-07-25 00:45:39 -07001216// ==== High-level intrinsic expander ==========================================
1217
TDYa127a1b21852012-07-23 03:20:39 -07001218llvm::Value* GBCExpanderPass::Expand_FPCompare(llvm::Value* src1_value,
1219 llvm::Value* src2_value,
1220 bool gt_bias) {
1221 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1222 llvm::Value* cmp_lt;
1223
1224 if (gt_bias) {
1225 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1226 } else {
1227 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1228 }
1229
1230 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1231}
1232
1233llvm::Value* GBCExpanderPass::Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value) {
1234 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1235 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1236
1237 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1238}
1239
1240llvm::Value* GBCExpanderPass::EmitCompareResultSelection(llvm::Value* cmp_eq,
1241 llvm::Value* cmp_lt) {
1242
1243 llvm::Constant* zero = irb_.getJInt(0);
1244 llvm::Constant* pos1 = irb_.getJInt(1);
1245 llvm::Constant* neg1 = irb_.getJInt(-1);
1246
1247 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1248 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1249
1250 return result_eq;
1251}
1252
Logan Chien75e4b602012-07-23 14:24:12 -07001253llvm::Value* GBCExpanderPass::Expand_IntegerShift(llvm::Value* src1_value,
1254 llvm::Value* src2_value,
1255 IntegerShiftKind kind,
1256 JType op_jty) {
1257 DCHECK(op_jty == kInt || op_jty == kLong);
1258
1259 // Mask and zero-extend RHS properly
1260 if (op_jty == kInt) {
1261 src2_value = irb_.CreateAnd(src2_value, 0x1f);
1262 } else {
1263 llvm::Value* masked_src2_value = irb_.CreateAnd(src2_value, 0x3f);
1264 src2_value = irb_.CreateZExt(masked_src2_value, irb_.getJLongTy());
1265 }
1266
1267 // Create integer shift llvm instruction
1268 switch (kind) {
1269 case kIntegerSHL:
1270 return irb_.CreateShl(src1_value, src2_value);
1271
1272 case kIntegerSHR:
1273 return irb_.CreateAShr(src1_value, src2_value);
1274
1275 case kIntegerUSHR:
1276 return irb_.CreateLShr(src1_value, src2_value);
1277
1278 default:
1279 LOG(FATAL) << "Unknown integer shift kind: " << kind;
1280 return NULL;
1281 }
1282}
1283
TDYa1275a26d442012-07-26 18:58:38 -07001284llvm::Value* GBCExpanderPass::Expand_HLArrayGet(llvm::CallInst& call_inst,
1285 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001286 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1287 llvm::Value* array_addr = call_inst.getArgOperand(1);
1288 llvm::Value* index_value = call_inst.getArgOperand(2);
TDYa127920be7c2012-09-10 17:13:22 -07001289 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001290
TDYa127920be7c2012-09-10 17:13:22 -07001291 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1292 EmitGuard_NullPointerException(dex_pc, array_addr);
1293 }
1294 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
1295 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value);
1296 }
TDYa1275a26d442012-07-26 18:58:38 -07001297
1298 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1299
1300 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
1301
1302 switch (elem_jty) {
1303 case kVoid:
1304 break;
1305
1306 case kBoolean:
1307 case kChar:
1308 array_elem_value = irb_.CreateZExt(array_elem_value, irb_.getJType(elem_jty, kReg));
1309 break;
1310
1311 case kByte:
1312 case kShort:
1313 array_elem_value = irb_.CreateSExt(array_elem_value, irb_.getJType(elem_jty, kReg));
1314 break;
1315
1316 case kInt:
1317 case kLong:
1318 case kFloat:
1319 case kDouble:
1320 case kObject:
1321 break;
1322
1323 default:
1324 LOG(FATAL) << "Unknown java type: " << elem_jty;
1325 }
1326
1327 return array_elem_value;
1328}
1329
1330
1331void GBCExpanderPass::Expand_HLArrayPut(llvm::CallInst& call_inst,
1332 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001333 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1334 llvm::Value* new_value = call_inst.getArgOperand(1);
1335 llvm::Value* array_addr = call_inst.getArgOperand(2);
1336 llvm::Value* index_value = call_inst.getArgOperand(3);
TDYa127920be7c2012-09-10 17:13:22 -07001337 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001338
TDYa127920be7c2012-09-10 17:13:22 -07001339 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1340 EmitGuard_NullPointerException(dex_pc, array_addr);
1341 }
1342 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
1343 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value);
1344 }
TDYa1275a26d442012-07-26 18:58:38 -07001345
1346 switch (elem_jty) {
1347 case kVoid:
1348 break;
1349
1350 case kBoolean:
1351 case kChar:
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001352 case kByte:
1353 case kShort:
TDYa1275a26d442012-07-26 18:58:38 -07001354 new_value = irb_.CreateTrunc(new_value, irb_.getJType(elem_jty, kArray));
1355 break;
1356
1357 case kInt:
1358 case kLong:
1359 case kFloat:
1360 case kDouble:
1361 case kObject:
1362 break;
1363
1364 default:
1365 LOG(FATAL) << "Unknown java type: " << elem_jty;
1366 }
1367
1368 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1369
1370 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
1371 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::CheckPutArrayElement);
1372
1373 irb_.CreateCall2(runtime_func, new_value, array_addr);
1374
1375 EmitGuard_ExceptionLandingPad(dex_pc);
1376
1377 EmitMarkGCCard(new_value, array_addr);
1378 }
1379
1380 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1381
1382 return;
1383}
1384
TDYa1275e869b62012-07-25 00:45:39 -07001385llvm::Value* GBCExpanderPass::Expand_HLIGet(llvm::CallInst& call_inst,
1386 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001387 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1388 llvm::Value* object_addr = call_inst.getArgOperand(1);
1389 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(2));
TDYa127920be7c2012-09-10 17:13:22 -07001390 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001391
TDYa127920be7c2012-09-10 17:13:22 -07001392 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1393 EmitGuard_NullPointerException(dex_pc, object_addr);
1394 }
TDYa1275e869b62012-07-25 00:45:39 -07001395
1396 llvm::Value* field_value;
1397
1398 int field_offset;
1399 bool is_volatile;
1400 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
1401 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
1402
1403 if (!is_fast_path) {
1404 llvm::Function* runtime_func;
1405
1406 if (field_jty == kObject) {
1407 runtime_func = irb_.GetRuntime(runtime_support::GetObjectInstance);
1408 } else if (field_jty == kLong || field_jty == kDouble) {
1409 runtime_func = irb_.GetRuntime(runtime_support::Get64Instance);
1410 } else {
1411 runtime_func = irb_.GetRuntime(runtime_support::Get32Instance);
1412 }
1413
1414 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
1415
1416 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1417
1418 EmitUpdateDexPC(dex_pc);
1419
1420 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
1421 method_object_addr, object_addr);
1422
1423 EmitGuard_ExceptionLandingPad(dex_pc);
1424
1425 } else {
1426 DCHECK_GE(field_offset, 0);
1427
1428 llvm::PointerType* field_type =
1429 irb_.getJType(field_jty, kField)->getPointerTo();
1430
1431 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
1432
1433 llvm::Value* field_addr =
1434 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1435
1436 // TODO: Check is_volatile. We need to generate atomic load instruction
1437 // when is_volatile is true.
1438 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
1439 }
1440
1441 if (field_jty == kFloat || field_jty == kDouble) {
TDYa1275a26d442012-07-26 18:58:38 -07001442 field_value = irb_.CreateBitCast(field_value, irb_.getJType(field_jty, kAccurate));
TDYa1275e869b62012-07-25 00:45:39 -07001443 }
1444
1445 return field_value;
1446}
1447
1448void GBCExpanderPass::Expand_HLIPut(llvm::CallInst& call_inst,
1449 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001450 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001451 llvm::Value* new_value = call_inst.getArgOperand(1);
1452 llvm::Value* object_addr = call_inst.getArgOperand(2);
TDYa1275e869b62012-07-25 00:45:39 -07001453 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(3));
TDYa127920be7c2012-09-10 17:13:22 -07001454 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001455
1456 if (field_jty == kFloat || field_jty == kDouble) {
TDYa1275a26d442012-07-26 18:58:38 -07001457 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty, kField));
TDYa1275e869b62012-07-25 00:45:39 -07001458 }
1459
TDYa127920be7c2012-09-10 17:13:22 -07001460 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1461 EmitGuard_NullPointerException(dex_pc, object_addr);
1462 }
TDYa1275e869b62012-07-25 00:45:39 -07001463
1464 int field_offset;
1465 bool is_volatile;
1466 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
1467 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
1468
1469 if (!is_fast_path) {
1470 llvm::Function* runtime_func;
1471
1472 if (field_jty == kObject) {
1473 runtime_func = irb_.GetRuntime(runtime_support::SetObjectInstance);
1474 } else if (field_jty == kLong || field_jty == kDouble) {
1475 runtime_func = irb_.GetRuntime(runtime_support::Set64Instance);
1476 } else {
1477 runtime_func = irb_.GetRuntime(runtime_support::Set32Instance);
1478 }
1479
1480 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1481
1482 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1483
1484 EmitUpdateDexPC(dex_pc);
1485
1486 irb_.CreateCall4(runtime_func, field_idx_value,
1487 method_object_addr, object_addr, new_value);
1488
1489 EmitGuard_ExceptionLandingPad(dex_pc);
1490
1491 } else {
1492 DCHECK_GE(field_offset, 0);
1493
1494 llvm::PointerType* field_type =
1495 irb_.getJType(field_jty, kField)->getPointerTo();
1496
1497 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
1498
1499 llvm::Value* field_addr =
1500 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1501
1502 // TODO: Check is_volatile. We need to generate atomic store instruction
1503 // when is_volatile is true.
1504 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1505
1506 if (field_jty == kObject) { // If put an object, mark the GC card table.
1507 EmitMarkGCCard(new_value, object_addr);
1508 }
1509 }
1510
1511 return;
1512}
1513
TDYa127f71bf5a2012-07-29 20:09:52 -07001514llvm::Value* GBCExpanderPass::EmitLoadConstantClass(uint32_t dex_pc,
1515 uint32_t type_idx) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001516 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, *dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001517 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1518
1519 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1520
1521 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1522
1523 llvm::Function* runtime_func =
1524 irb_.GetRuntime(runtime_support::InitializeTypeAndVerifyAccess);
1525
1526 EmitUpdateDexPC(dex_pc);
1527
1528 llvm::Value* type_object_addr =
1529 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1530
1531 EmitGuard_ExceptionLandingPad(dex_pc);
1532
1533 return type_object_addr;
1534
1535 } else {
1536 // Try to load the class (type) object from the test cache.
1537 llvm::Value* type_field_addr =
1538 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1539
1540 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
1541
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001542 if (compiler_->CanAssumeTypeIsPresentInDexCache(*dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001543 return type_object_addr;
1544 }
1545
1546 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1547
1548 // Test whether class (type) object is in the dex cache or not
1549 llvm::Value* equal_null =
1550 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1551
1552 llvm::BasicBlock* block_cont =
1553 CreateBasicBlockWithDexPC(dex_pc, "cont");
1554
1555 llvm::BasicBlock* block_load_class =
1556 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1557
1558 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
1559
1560 // Failback routine to load the class object
1561 irb_.SetInsertPoint(block_load_class);
1562
1563 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeType);
1564
1565 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1566
1567 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1568
1569 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1570
1571 EmitUpdateDexPC(dex_pc);
1572
1573 llvm::Value* loaded_type_object_addr =
1574 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1575
1576 EmitGuard_ExceptionLandingPad(dex_pc);
1577
1578 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1579
1580 irb_.CreateBr(block_cont);
1581
1582 // Now the class object must be loaded
1583 irb_.SetInsertPoint(block_cont);
1584
1585 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1586
1587 phi->addIncoming(type_object_addr, block_original);
1588 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1589
1590 return phi;
1591 }
1592}
1593
TDYa1275a26d442012-07-26 18:58:38 -07001594llvm::Value* GBCExpanderPass::EmitLoadStaticStorage(uint32_t dex_pc,
1595 uint32_t type_idx) {
1596 llvm::BasicBlock* block_load_static =
1597 CreateBasicBlockWithDexPC(dex_pc, "load_static");
1598
1599 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1600
1601 // Load static storage from dex cache
1602 llvm::Value* storage_field_addr =
1603 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1604
1605 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
1606
1607 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1608
1609 // Test: Is the static storage of this class initialized?
1610 llvm::Value* equal_null =
1611 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
1612
1613 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
1614
1615 // Failback routine to load the class object
1616 irb_.SetInsertPoint(block_load_static);
1617
1618 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeStaticStorage);
1619
1620 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1621
1622 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1623
1624 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1625
1626 EmitUpdateDexPC(dex_pc);
1627
1628 llvm::Value* loaded_storage_object_addr =
1629 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1630
1631 EmitGuard_ExceptionLandingPad(dex_pc);
1632
1633 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
1634
1635 irb_.CreateBr(block_cont);
1636
1637 // Now the class object must be loaded
1638 irb_.SetInsertPoint(block_cont);
1639
1640 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1641
1642 phi->addIncoming(storage_object_addr, block_original);
1643 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
1644
1645 return phi;
1646}
1647
1648llvm::Value* GBCExpanderPass::Expand_HLSget(llvm::CallInst& call_inst,
1649 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001650 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1651 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1652
1653 int field_offset;
1654 int ssb_index;
1655 bool is_referrers_class;
1656 bool is_volatile;
1657
1658 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
1659 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1660 is_referrers_class, is_volatile, false);
1661
1662 llvm::Value* static_field_value;
1663
1664 if (!is_fast_path) {
1665 llvm::Function* runtime_func;
1666
1667 if (field_jty == kObject) {
1668 runtime_func = irb_.GetRuntime(runtime_support::GetObjectStatic);
1669 } else if (field_jty == kLong || field_jty == kDouble) {
1670 runtime_func = irb_.GetRuntime(runtime_support::Get64Static);
1671 } else {
1672 runtime_func = irb_.GetRuntime(runtime_support::Get32Static);
1673 }
1674
1675 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1676
1677 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1678
1679 EmitUpdateDexPC(dex_pc);
1680
1681 static_field_value =
1682 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
1683
1684 EmitGuard_ExceptionLandingPad(dex_pc);
1685
1686 } else {
1687 DCHECK_GE(field_offset, 0);
1688
1689 llvm::Value* static_storage_addr = NULL;
1690
1691 if (is_referrers_class) {
1692 // Fast path, static storage base is this method's class
1693 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1694
1695 static_storage_addr =
1696 irb_.LoadFromObjectOffset(method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001697 art::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001698 irb_.getJObjectTy(),
1699 kTBAAConstJObject);
1700 } else {
1701 // Medium path, static storage base in a different class which
1702 // requires checks that the other class is initialized
1703 DCHECK_GE(ssb_index, 0);
1704 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1705 }
1706
1707 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1708
1709 llvm::Value* static_field_addr =
1710 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1711 irb_.getJType(field_jty, kField)->getPointerTo());
1712
1713 // TODO: Check is_volatile. We need to generate atomic load instruction
1714 // when is_volatile is true.
1715 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
1716 }
1717
1718 if (field_jty == kFloat || field_jty == kDouble) {
1719 static_field_value =
1720 irb_.CreateBitCast(static_field_value, irb_.getJType(field_jty, kAccurate));
1721 }
1722
1723 return static_field_value;
1724}
1725
1726void GBCExpanderPass::Expand_HLSput(llvm::CallInst& call_inst,
1727 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001728 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1729 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1730 llvm::Value* new_value = call_inst.getArgOperand(1);
1731
1732 if (field_jty == kFloat || field_jty == kDouble) {
1733 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty, kField));
1734 }
1735
1736 int field_offset;
1737 int ssb_index;
1738 bool is_referrers_class;
1739 bool is_volatile;
1740
1741 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
1742 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1743 is_referrers_class, is_volatile, true);
1744
1745 if (!is_fast_path) {
1746 llvm::Function* runtime_func;
1747
1748 if (field_jty == kObject) {
1749 runtime_func = irb_.GetRuntime(runtime_support::SetObjectStatic);
1750 } else if (field_jty == kLong || field_jty == kDouble) {
1751 runtime_func = irb_.GetRuntime(runtime_support::Set64Static);
1752 } else {
1753 runtime_func = irb_.GetRuntime(runtime_support::Set32Static);
1754 }
1755
1756 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1757
1758 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1759
1760 EmitUpdateDexPC(dex_pc);
1761
1762 irb_.CreateCall3(runtime_func, field_idx_value,
1763 method_object_addr, new_value);
1764
1765 EmitGuard_ExceptionLandingPad(dex_pc);
1766
1767 } else {
1768 DCHECK_GE(field_offset, 0);
1769
1770 llvm::Value* static_storage_addr = NULL;
1771
1772 if (is_referrers_class) {
1773 // Fast path, static storage base is this method's class
1774 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1775
1776 static_storage_addr =
1777 irb_.LoadFromObjectOffset(method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001778 art::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001779 irb_.getJObjectTy(),
1780 kTBAAConstJObject);
1781 } else {
1782 // Medium path, static storage base in a different class which
1783 // requires checks that the other class is initialized
1784 DCHECK_GE(ssb_index, 0);
1785 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1786 }
1787
1788 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1789
1790 llvm::Value* static_field_addr =
1791 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1792 irb_.getJType(field_jty, kField)->getPointerTo());
1793
1794 // TODO: Check is_volatile. We need to generate atomic store instruction
1795 // when is_volatile is true.
1796 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1797
1798 if (field_jty == kObject) { // If put an object, mark the GC card table.
1799 EmitMarkGCCard(new_value, static_storage_addr);
1800 }
1801 }
1802
1803 return;
1804}
1805
TDYa127f71bf5a2012-07-29 20:09:52 -07001806llvm::Value* GBCExpanderPass::Expand_ConstString(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001807 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1808 uint32_t string_idx = LV2UInt(call_inst.getArgOperand(0));
1809
1810 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1811
1812 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
1813
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001814 if (!compiler_->CanAssumeStringIsPresentInDexCache(*dex_file_, string_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001815 llvm::BasicBlock* block_str_exist =
1816 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1817
1818 llvm::BasicBlock* block_str_resolve =
1819 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1820
1821 llvm::BasicBlock* block_cont =
1822 CreateBasicBlockWithDexPC(dex_pc, "str_cont");
1823
1824 // Test: Is the string resolved and in the dex cache?
1825 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1826
1827 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
1828
1829 // String is resolved, go to next basic block.
1830 irb_.SetInsertPoint(block_str_exist);
1831 irb_.CreateBr(block_cont);
1832
1833 // String is not resolved yet, resolve it now.
1834 irb_.SetInsertPoint(block_str_resolve);
1835
1836 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::ResolveString);
1837
1838 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1839
1840 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1841
1842 EmitUpdateDexPC(dex_pc);
1843
1844 llvm::Value* result = irb_.CreateCall2(runtime_func, method_object_addr,
1845 string_idx_value);
1846
1847 EmitGuard_ExceptionLandingPad(dex_pc);
1848
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001849 irb_.CreateBr(block_cont);
1850
1851
TDYa127f71bf5a2012-07-29 20:09:52 -07001852 llvm::BasicBlock* block_pre_cont = irb_.GetInsertBlock();
1853
1854 irb_.SetInsertPoint(block_cont);
1855
1856 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1857
1858 phi->addIncoming(string_addr, block_str_exist);
1859 phi->addIncoming(result, block_pre_cont);
1860
1861 string_addr = phi;
1862 }
1863
1864 return string_addr;
1865}
1866
1867llvm::Value* GBCExpanderPass::Expand_ConstClass(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001868 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1869 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1870
1871 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
1872
1873 return type_object_addr;
1874}
1875
1876void GBCExpanderPass::Expand_MonitorEnter(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001877 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1878 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07001879 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07001880
TDYa127920be7c2012-09-10 17:13:22 -07001881 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1882 EmitGuard_NullPointerException(dex_pc, object_addr);
1883 }
TDYa127f71bf5a2012-07-29 20:09:52 -07001884
1885 irb_.Runtime().EmitLockObject(object_addr);
1886
1887 return;
1888}
1889
1890void GBCExpanderPass::Expand_MonitorExit(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001891 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1892 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07001893 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07001894
TDYa127920be7c2012-09-10 17:13:22 -07001895 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1896 EmitGuard_NullPointerException(dex_pc, object_addr);
1897 }
TDYa127f71bf5a2012-07-29 20:09:52 -07001898
1899 EmitUpdateDexPC(dex_pc);
1900
1901 irb_.Runtime().EmitUnlockObject(object_addr);
1902
1903 EmitGuard_ExceptionLandingPad(dex_pc);
1904
1905 return;
1906}
1907
1908void GBCExpanderPass::Expand_HLCheckCast(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001909 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1910 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1911 llvm::Value* object_addr = call_inst.getArgOperand(1);
1912
1913 llvm::BasicBlock* block_test_class =
1914 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1915
1916 llvm::BasicBlock* block_test_sub_class =
1917 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1918
1919 llvm::BasicBlock* block_cont =
1920 CreateBasicBlockWithDexPC(dex_pc, "checkcast_cont");
1921
1922 // Test: Is the reference equal to null? Act as no-op when it is null.
1923 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1924
1925 irb_.CreateCondBr(equal_null,
1926 block_cont,
1927 block_test_class);
1928
1929 // Test: Is the object instantiated from the given class?
1930 irb_.SetInsertPoint(block_test_class);
1931 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
TDYa127920be7c2012-09-10 17:13:22 -07001932 DCHECK_EQ(art::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07001933
1934 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1935
1936 llvm::Value* object_type_field_addr =
1937 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1938
1939 llvm::Value* object_type_object_addr =
1940 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
1941
1942 llvm::Value* equal_class =
1943 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1944
1945 irb_.CreateCondBr(equal_class,
1946 block_cont,
1947 block_test_sub_class);
1948
1949 // Test: Is the object instantiated from the subclass of the given class?
1950 irb_.SetInsertPoint(block_test_sub_class);
1951
1952 EmitUpdateDexPC(dex_pc);
1953
1954 irb_.CreateCall2(irb_.GetRuntime(runtime_support::CheckCast),
1955 type_object_addr, object_type_object_addr);
1956
1957 EmitGuard_ExceptionLandingPad(dex_pc);
1958
1959 irb_.CreateBr(block_cont);
1960
1961 irb_.SetInsertPoint(block_cont);
1962
1963 return;
1964}
1965
1966llvm::Value* GBCExpanderPass::Expand_InstanceOf(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001967 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1968 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1969 llvm::Value* object_addr = call_inst.getArgOperand(1);
1970
1971 llvm::BasicBlock* block_nullp =
1972 CreateBasicBlockWithDexPC(dex_pc, "nullp");
1973
1974 llvm::BasicBlock* block_test_class =
1975 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1976
1977 llvm::BasicBlock* block_class_equals =
1978 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1979
1980 llvm::BasicBlock* block_test_sub_class =
1981 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1982
1983 llvm::BasicBlock* block_cont =
1984 CreateBasicBlockWithDexPC(dex_pc, "instance_of_cont");
1985
1986 // Overview of the following code :
1987 // We check for null, if so, then false, otherwise check for class == . If so
1988 // then true, otherwise do callout slowpath.
1989 //
1990 // Test: Is the reference equal to null? Set 0 when it is null.
1991 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1992
1993 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1994
1995 irb_.SetInsertPoint(block_nullp);
1996 irb_.CreateBr(block_cont);
1997
1998 // Test: Is the object instantiated from the given class?
1999 irb_.SetInsertPoint(block_test_class);
2000 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
TDYa127920be7c2012-09-10 17:13:22 -07002001 DCHECK_EQ(art::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002002
2003 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2004
2005 llvm::Value* object_type_field_addr =
2006 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2007
2008 llvm::Value* object_type_object_addr =
2009 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2010
2011 llvm::Value* equal_class =
2012 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2013
2014 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
2015
2016 irb_.SetInsertPoint(block_class_equals);
2017 irb_.CreateBr(block_cont);
2018
2019 // Test: Is the object instantiated from the subclass of the given class?
2020 irb_.SetInsertPoint(block_test_sub_class);
2021 llvm::Value* result =
2022 irb_.CreateCall2(irb_.GetRuntime(runtime_support::IsAssignable),
2023 type_object_addr, object_type_object_addr);
2024 irb_.CreateBr(block_cont);
2025
2026 irb_.SetInsertPoint(block_cont);
2027
2028 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJIntTy(), 3);
2029
2030 phi->addIncoming(irb_.getJInt(0), block_nullp);
2031 phi->addIncoming(irb_.getJInt(1), block_class_equals);
2032 phi->addIncoming(result, block_test_sub_class);
2033
2034 return phi;
2035}
2036
2037llvm::Value* GBCExpanderPass::Expand_NewInstance(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002038 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2039 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2040
2041 llvm::Function* runtime_func;
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002042 if (compiler_->CanAccessInstantiableTypeWithoutChecks(method_idx_, *dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002043 runtime_func = irb_.GetRuntime(runtime_support::AllocObject);
2044 } else {
2045 runtime_func = irb_.GetRuntime(runtime_support::AllocObjectWithAccessCheck);
2046 }
2047
2048 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2049
2050 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2051
2052 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2053
2054 EmitUpdateDexPC(dex_pc);
2055
2056 llvm::Value* object_addr =
2057 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
2058
2059 EmitGuard_ExceptionLandingPad(dex_pc);
2060
2061 return object_addr;
2062}
2063
2064llvm::Value* GBCExpanderPass::Expand_HLInvoke(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002065 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
TDYa127920be7c2012-09-10 17:13:22 -07002066 art::InvokeType invoke_type = static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
2067 bool is_static = (invoke_type == art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002068 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
TDYa127920be7c2012-09-10 17:13:22 -07002069 int opt_flags = LV2UInt(call_inst.getArgOperand(2));
TDYa127f71bf5a2012-07-29 20:09:52 -07002070
2071 // Compute invoke related information for compiler decision
2072 int vtable_idx = -1;
2073 uintptr_t direct_code = 0;
2074 uintptr_t direct_method = 0;
2075 bool is_fast_path = compiler_->
2076 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
2077 invoke_type, vtable_idx, direct_code, direct_method);
2078
2079 // Load *this* actual parameter
2080 llvm::Value* this_addr = NULL;
2081
2082 if (!is_static) {
2083 // Test: Is *this* parameter equal to null?
2084 this_addr = call_inst.getArgOperand(3);
2085 }
2086
2087 // Load the method object
2088 llvm::Value* callee_method_object_addr = NULL;
2089
2090 if (!is_fast_path) {
2091 callee_method_object_addr =
2092 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2093 this_addr, dex_pc, is_fast_path);
2094
TDYa127920be7c2012-09-10 17:13:22 -07002095 if (!is_static && !(opt_flags & MIR_IGNORE_NULL_CHECK)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002096 EmitGuard_NullPointerException(dex_pc, this_addr);
2097 }
2098 } else {
TDYa127920be7c2012-09-10 17:13:22 -07002099 if (!is_static && !(opt_flags & MIR_IGNORE_NULL_CHECK)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002100 EmitGuard_NullPointerException(dex_pc, this_addr);
2101 }
2102
2103 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002104 case art::kStatic:
2105 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002106 if (direct_method != 0u &&
2107 direct_method != static_cast<uintptr_t>(-1)) {
2108 callee_method_object_addr =
2109 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2110 irb_.getJObjectTy());
2111 } else {
2112 callee_method_object_addr =
2113 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2114 }
2115 break;
2116
TDYa127920be7c2012-09-10 17:13:22 -07002117 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002118 DCHECK(vtable_idx != -1);
2119 callee_method_object_addr =
2120 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2121 break;
2122
TDYa127920be7c2012-09-10 17:13:22 -07002123 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002124 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2125 "the fast path.";
2126 break;
2127
TDYa127920be7c2012-09-10 17:13:22 -07002128 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002129 callee_method_object_addr =
2130 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2131 invoke_type, this_addr,
2132 dex_pc, is_fast_path);
2133 break;
2134 }
2135 }
2136
2137 // Load the actual parameter
2138 std::vector<llvm::Value*> args;
2139
2140 args.push_back(callee_method_object_addr); // method object for callee
2141
2142 for (uint32_t i = 3; i < call_inst.getNumArgOperands(); ++i) {
2143 args.push_back(call_inst.getArgOperand(i));
2144 }
2145
2146 llvm::Value* code_addr;
2147 if (direct_code != 0u &&
2148 direct_code != static_cast<uintptr_t>(-1)) {
2149 code_addr =
2150 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_code),
2151 GetFunctionType(callee_method_idx, is_static)->getPointerTo());
2152 } else {
2153 code_addr =
2154 irb_.LoadFromObjectOffset(callee_method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -07002155 art::AbstractMethod::GetCodeOffset().Int32Value(),
TDYa127f71bf5a2012-07-29 20:09:52 -07002156 GetFunctionType(callee_method_idx, is_static)->getPointerTo(),
2157 kTBAAJRuntime);
2158 }
2159
2160 // Invoke callee
2161 EmitUpdateDexPC(dex_pc);
2162 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2163 EmitGuard_ExceptionLandingPad(dex_pc);
2164
2165 return retval;
2166}
2167
2168llvm::Value* GBCExpanderPass::Expand_OptArrayLength(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002169 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2170 // Get the array object address
2171 llvm::Value* array_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002172 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002173
TDYa127920be7c2012-09-10 17:13:22 -07002174 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
2175 EmitGuard_NullPointerException(dex_pc, array_addr);
2176 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002177
2178 // Get the array length and store it to the register
2179 return EmitLoadArrayLength(array_addr);
2180}
2181
2182llvm::Value* GBCExpanderPass::Expand_NewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002183 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2184 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2185 llvm::Value* length = call_inst.getArgOperand(1);
2186
2187 return EmitAllocNewArray(dex_pc, length, type_idx, false);
2188}
2189
2190llvm::Value* GBCExpanderPass::Expand_HLFilledNewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002191 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2192 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(1));
2193 uint32_t length = call_inst.getNumArgOperands() - 3;
2194
2195 llvm::Value* object_addr =
2196 EmitAllocNewArray(dex_pc, irb_.getInt32(length), type_idx, true);
2197
2198 if (length > 0) {
2199 // Check for the element type
2200 uint32_t type_desc_len = 0;
2201 const char* type_desc =
2202 dex_file_->StringByTypeIdx(type_idx, &type_desc_len);
2203
2204 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
2205 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
2206 bool is_elem_int_ty = (type_desc[1] == 'I');
2207
2208 uint32_t alignment;
2209 llvm::Constant* elem_size;
2210 llvm::PointerType* field_type;
2211
2212 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
2213 // as the element, thus we are only checking 2 cases: primitive int and
2214 // non-primitive type.
2215 if (is_elem_int_ty) {
2216 alignment = sizeof(int32_t);
2217 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
2218 field_type = irb_.getJIntTy()->getPointerTo();
2219 } else {
2220 alignment = irb_.getSizeOfPtrEquivInt();
2221 elem_size = irb_.getSizeOfPtrEquivIntValue();
2222 field_type = irb_.getJObjectTy()->getPointerTo();
2223 }
2224
2225 llvm::Value* data_field_offset =
TDYa127920be7c2012-09-10 17:13:22 -07002226 irb_.getPtrEquivInt(art::Array::DataOffset(alignment).Int32Value());
TDYa127f71bf5a2012-07-29 20:09:52 -07002227
2228 llvm::Value* data_field_addr =
2229 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
2230
2231 // TODO: Tune this code. Currently we are generating one instruction for
2232 // one element which may be very space consuming. Maybe changing to use
2233 // memcpy may help; however, since we can't guarantee that the alloca of
2234 // dalvik register are continuous, we can't perform such optimization yet.
2235 for (uint32_t i = 0; i < length; ++i) {
2236 llvm::Value* reg_value = call_inst.getArgOperand(i+3);
2237
2238 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
2239
2240 data_field_addr =
2241 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
2242 }
2243 }
2244
2245 return object_addr;
2246}
2247
2248void GBCExpanderPass::Expand_HLFillArrayData(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002249 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2250 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
2251 LV2SInt(call_inst.getArgOperand(0));
2252 llvm::Value* array_addr = call_inst.getArgOperand(1);
2253
TDYa127920be7c2012-09-10 17:13:22 -07002254 const art::Instruction::ArrayDataPayload* payload =
2255 reinterpret_cast<const art::Instruction::ArrayDataPayload*>(
TDYa127f71bf5a2012-07-29 20:09:52 -07002256 code_item_->insns_ + payload_offset);
2257
2258 if (payload->element_count == 0) {
2259 // When the number of the elements in the payload is zero, we don't have
2260 // to copy any numbers. However, we should check whether the array object
2261 // address is equal to null or not.
2262 EmitGuard_NullPointerException(dex_pc, array_addr);
2263 } else {
2264 // To save the code size, we are going to call the runtime function to
2265 // copy the content from DexFile.
2266
2267 // NOTE: We will check for the NullPointerException in the runtime.
2268
2269 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::FillArrayData);
2270
2271 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2272
2273 EmitUpdateDexPC(dex_pc);
2274
2275 irb_.CreateCall4(runtime_func,
2276 method_object_addr, irb_.getInt32(dex_pc),
2277 array_addr, irb_.getInt32(payload_offset));
2278
2279 EmitGuard_ExceptionLandingPad(dex_pc);
2280 }
2281
2282 return;
2283}
2284
2285llvm::Value* GBCExpanderPass::EmitAllocNewArray(uint32_t dex_pc,
2286 llvm::Value* array_length_value,
2287 uint32_t type_idx,
2288 bool is_filled_new_array) {
2289 llvm::Function* runtime_func;
2290
2291 bool skip_access_check =
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002292 compiler_->CanAccessTypeWithoutChecks(method_idx_, *dex_file_, type_idx);
TDYa127f71bf5a2012-07-29 20:09:52 -07002293
2294
2295 if (is_filled_new_array) {
2296 runtime_func = skip_access_check ?
2297 irb_.GetRuntime(runtime_support::CheckAndAllocArray) :
2298 irb_.GetRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck);
2299 } else {
2300 runtime_func = skip_access_check ?
2301 irb_.GetRuntime(runtime_support::AllocArray) :
2302 irb_.GetRuntime(runtime_support::AllocArrayWithAccessCheck);
2303 }
2304
2305 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2306
2307 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2308
2309 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2310
2311 EmitUpdateDexPC(dex_pc);
2312
2313 llvm::Value* object_addr =
2314 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
2315 array_length_value, thread_object_addr);
2316
2317 EmitGuard_ExceptionLandingPad(dex_pc);
2318
2319 return object_addr;
2320}
2321
2322llvm::Value* GBCExpanderPass::
2323EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -07002324 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -07002325 llvm::Value* this_addr,
2326 uint32_t dex_pc,
2327 bool is_fast_path) {
2328
2329 llvm::Function* runtime_func = NULL;
2330
2331 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002332 case art::kStatic:
TDYa127f71bf5a2012-07-29 20:09:52 -07002333 runtime_func = irb_.GetRuntime(runtime_support::FindStaticMethodWithAccessCheck);
2334 break;
2335
TDYa127920be7c2012-09-10 17:13:22 -07002336 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002337 runtime_func = irb_.GetRuntime(runtime_support::FindDirectMethodWithAccessCheck);
2338 break;
2339
TDYa127920be7c2012-09-10 17:13:22 -07002340 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002341 runtime_func = irb_.GetRuntime(runtime_support::FindVirtualMethodWithAccessCheck);
2342 break;
2343
TDYa127920be7c2012-09-10 17:13:22 -07002344 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002345 runtime_func = irb_.GetRuntime(runtime_support::FindSuperMethodWithAccessCheck);
2346 break;
2347
TDYa127920be7c2012-09-10 17:13:22 -07002348 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002349 if (is_fast_path) {
2350 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethod);
2351 } else {
2352 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethodWithAccessCheck);
2353 }
2354 break;
2355 }
2356
2357 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2358
2359 if (this_addr == NULL) {
TDYa127920be7c2012-09-10 17:13:22 -07002360 DCHECK_EQ(invoke_type, art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002361 this_addr = irb_.getJNull();
2362 }
2363
2364 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2365
2366 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2367
2368 EmitUpdateDexPC(dex_pc);
2369
2370 llvm::Value* callee_method_object_addr =
2371 irb_.CreateCall4(runtime_func,
2372 callee_method_idx_value,
2373 this_addr,
2374 caller_method_object_addr,
2375 thread_object_addr);
2376
2377 EmitGuard_ExceptionLandingPad(dex_pc);
2378
2379 return callee_method_object_addr;
2380}
2381
TDYa1275e869b62012-07-25 00:45:39 -07002382void GBCExpanderPass::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2383 // Using runtime support, let the target can override by InlineAssembly.
2384 irb_.Runtime().EmitMarkGCCard(value, target_addr);
2385}
2386
2387void GBCExpanderPass::EmitUpdateDexPC(uint32_t dex_pc) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002388#if defined(ART_USE_QUICK_COMPILER)
2389 if (shadow_frame_ == NULL) {
2390 return;
2391 }
2392#endif
TDYa1275e869b62012-07-25 00:45:39 -07002393 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07002394 art::ShadowFrame::DexPCOffset(),
TDYa1275e869b62012-07-25 00:45:39 -07002395 irb_.getInt32(dex_pc),
2396 kTBAAShadowFrame);
2397}
2398
2399void GBCExpanderPass::EmitGuard_DivZeroException(uint32_t dex_pc,
2400 llvm::Value* denominator,
2401 JType op_jty) {
2402 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2403
2404 llvm::Constant* zero = irb_.getJZero(op_jty);
2405
2406 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2407
2408 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2409
2410 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2411
2412 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
2413
2414 irb_.SetInsertPoint(block_exception);
2415 EmitUpdateDexPC(dex_pc);
2416 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowDivZeroException));
2417 EmitBranchExceptionLandingPad(dex_pc);
2418
2419 irb_.SetInsertPoint(block_continue);
2420}
2421
2422void GBCExpanderPass::EmitGuard_NullPointerException(uint32_t dex_pc,
2423 llvm::Value* object) {
2424 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
2425
2426 llvm::BasicBlock* block_exception =
2427 CreateBasicBlockWithDexPC(dex_pc, "nullp");
2428
2429 llvm::BasicBlock* block_continue =
2430 CreateBasicBlockWithDexPC(dex_pc, "cont");
2431
2432 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
2433
2434 irb_.SetInsertPoint(block_exception);
2435 EmitUpdateDexPC(dex_pc);
2436 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowNullPointerException),
2437 irb_.getInt32(dex_pc));
2438 EmitBranchExceptionLandingPad(dex_pc);
2439
2440 irb_.SetInsertPoint(block_continue);
2441}
2442
2443void
2444GBCExpanderPass::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2445 llvm::Value* array,
2446 llvm::Value* index) {
2447 llvm::Value* array_len = EmitLoadArrayLength(array);
2448
2449 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2450
2451 llvm::BasicBlock* block_exception =
2452 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2453
2454 llvm::BasicBlock* block_continue =
2455 CreateBasicBlockWithDexPC(dex_pc, "cont");
2456
2457 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
2458
2459 irb_.SetInsertPoint(block_exception);
2460
2461 EmitUpdateDexPC(dex_pc);
2462 irb_.CreateCall2(irb_.GetRuntime(runtime_support::ThrowIndexOutOfBounds), index, array_len);
2463 EmitBranchExceptionLandingPad(dex_pc);
2464
2465 irb_.SetInsertPoint(block_continue);
2466}
2467
TDYa1275e869b62012-07-25 00:45:39 -07002468llvm::FunctionType* GBCExpanderPass::GetFunctionType(uint32_t method_idx,
2469 bool is_static) {
2470 // Get method signature
TDYa127920be7c2012-09-10 17:13:22 -07002471 art::DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
TDYa1275e869b62012-07-25 00:45:39 -07002472
2473 uint32_t shorty_size;
2474 const char* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
2475 CHECK_GE(shorty_size, 1u);
2476
2477 // Get return type
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002478
2479 char ret_shorty = shorty[0];
2480#if defined(ART_USE_QUICK_COMPILER)
Shih-wei Liaob2596522012-09-14 16:36:11 -07002481 ret_shorty = art::remapShorty(ret_shorty);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002482#endif
2483 llvm::Type* ret_type = irb_.getJType(ret_shorty, kAccurate);
TDYa1275e869b62012-07-25 00:45:39 -07002484
2485 // Get argument type
2486 std::vector<llvm::Type*> args_type;
2487
2488 args_type.push_back(irb_.getJObjectTy()); // method object pointer
2489
2490 if (!is_static) {
2491 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
2492 }
2493
2494 for (uint32_t i = 1; i < shorty_size; ++i) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002495#if defined(ART_USE_QUICK_COMPILER)
Shih-wei Liaob2596522012-09-14 16:36:11 -07002496 char shorty_type = art::remapShorty(shorty[i]);
TDYa127f71bf5a2012-07-29 20:09:52 -07002497 args_type.push_back(irb_.getJType(shorty_type, kAccurate));
2498#else
TDYa1275e869b62012-07-25 00:45:39 -07002499 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
TDYa127f71bf5a2012-07-29 20:09:52 -07002500#endif
TDYa1275e869b62012-07-25 00:45:39 -07002501 }
2502
2503 return llvm::FunctionType::get(ret_type, args_type, false);
2504}
2505
2506
2507llvm::BasicBlock* GBCExpanderPass::
2508CreateBasicBlockWithDexPC(uint32_t dex_pc, const char* postfix) {
2509 std::string name;
2510
2511#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002512 art::StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
TDYa1275e869b62012-07-25 00:45:39 -07002513#endif
2514
2515 return llvm::BasicBlock::Create(context_, name, func_);
2516}
2517
2518llvm::BasicBlock* GBCExpanderPass::GetBasicBlock(uint32_t dex_pc) {
2519 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002520 CHECK(basic_blocks_[dex_pc] != NULL);
TDYa1275e869b62012-07-25 00:45:39 -07002521 return basic_blocks_[dex_pc];
2522}
2523
2524int32_t GBCExpanderPass::GetTryItemOffset(uint32_t dex_pc) {
2525 int32_t min = 0;
2526 int32_t max = code_item_->tries_size_ - 1;
2527
2528 while (min <= max) {
2529 int32_t mid = min + (max - min) / 2;
2530
TDYa127920be7c2012-09-10 17:13:22 -07002531 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*code_item_, mid);
TDYa1275e869b62012-07-25 00:45:39 -07002532 uint32_t start = ti->start_addr_;
2533 uint32_t end = start + ti->insn_count_;
2534
2535 if (dex_pc < start) {
2536 max = mid - 1;
2537 } else if (dex_pc >= end) {
2538 min = mid + 1;
2539 } else {
2540 return mid; // found
2541 }
2542 }
2543
2544 return -1; // not found
2545}
2546
2547llvm::BasicBlock* GBCExpanderPass::GetLandingPadBasicBlock(uint32_t dex_pc) {
2548 // Find the try item for this address in this method
2549 int32_t ti_offset = GetTryItemOffset(dex_pc);
2550
2551 if (ti_offset == -1) {
2552 return NULL; // No landing pad is available for this address.
2553 }
2554
2555 // Check for the existing landing pad basic block
2556 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2557 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2558
2559 if (block_lpad) {
2560 // We have generated landing pad for this try item already. Return the
2561 // same basic block.
2562 return block_lpad;
2563 }
2564
2565 // Get try item from code item
TDYa127920be7c2012-09-10 17:13:22 -07002566 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*code_item_, ti_offset);
TDYa1275e869b62012-07-25 00:45:39 -07002567
2568 std::string lpadname;
2569
2570#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002571 art::StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
TDYa1275e869b62012-07-25 00:45:39 -07002572#endif
2573
2574 // Create landing pad basic block
2575 block_lpad = llvm::BasicBlock::Create(context_, lpadname, func_);
2576
2577 // Change IRBuilder insert point
2578 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2579 irb_.SetInsertPoint(block_lpad);
2580
2581 // Find catch block with matching type
2582 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2583
2584 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
2585
2586 llvm::Value* catch_handler_index_value =
2587 irb_.CreateCall2(irb_.GetRuntime(runtime_support::FindCatchBlock),
2588 method_object_addr, ti_offset_value);
2589
2590 // Switch instruction (Go to unwind basic block by default)
2591 llvm::SwitchInst* sw =
2592 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2593
2594 // Cases with matched catch block
TDYa127920be7c2012-09-10 17:13:22 -07002595 art::CatchHandlerIterator iter(*code_item_, ti->start_addr_);
TDYa1275e869b62012-07-25 00:45:39 -07002596
2597 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2598 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2599 }
2600
2601 // Restore the orignal insert point for IRBuilder
2602 irb_.restoreIP(irb_ip_original);
2603
2604 // Cache this landing pad
2605 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2606 basic_block_landing_pads_[ti_offset] = block_lpad;
2607
2608 return block_lpad;
2609}
2610
2611llvm::BasicBlock* GBCExpanderPass::GetUnwindBasicBlock() {
2612 // Check the existing unwinding baisc block block
2613 if (basic_block_unwind_ != NULL) {
2614 return basic_block_unwind_;
2615 }
2616
2617 // Create new basic block for unwinding
2618 basic_block_unwind_ =
2619 llvm::BasicBlock::Create(context_, "exception_unwind", func_);
2620
2621 // Change IRBuilder insert point
2622 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2623 irb_.SetInsertPoint(basic_block_unwind_);
2624
2625 // Pop the shadow frame
2626 Expand_PopShadowFrame();
2627
2628 // Emit the code to return default value (zero) for the given return type.
2629 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002630#if defined(ART_USE_QUICK_COMPILER)
Shih-wei Liaob2596522012-09-14 16:36:11 -07002631 ret_shorty = art::remapShorty(ret_shorty);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002632#endif
TDYa1275e869b62012-07-25 00:45:39 -07002633 if (ret_shorty == 'V') {
2634 irb_.CreateRetVoid();
2635 } else {
2636 irb_.CreateRet(irb_.getJZero(ret_shorty));
2637 }
2638
2639 // Restore the orignal insert point for IRBuilder
2640 irb_.restoreIP(irb_ip_original);
2641
2642 return basic_block_unwind_;
2643}
2644
2645void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2646 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002647 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002648 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002649 irb_.CreateBr(lpad);
2650 } else {
2651 irb_.CreateBr(GetUnwindBasicBlock());
2652 }
2653}
2654
2655void GBCExpanderPass::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
2656 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
2657
2658 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2659
2660 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002661 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002662 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002663 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
2664 } else {
2665 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
2666 }
2667
2668 irb_.SetInsertPoint(block_cont);
2669}
2670
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002671llvm::Value*
2672GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
2673 llvm::CallInst& call_inst) {
2674 switch (intr_id) {
2675 //==- Thread -----------------------------------------------------------==//
2676 case IntrinsicHelper::GetCurrentThread: {
TDYa127b672d1e2012-06-28 21:21:45 -07002677 return irb_.Runtime().EmitGetCurrentThread();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002678 }
Logan Chien75e4b602012-07-23 14:24:12 -07002679 case IntrinsicHelper::CheckSuspend: {
TDYa127890ea892012-08-22 10:49:42 -07002680 // We will add suspend by ourselves.
2681 return NULL;
2682 }
2683 case IntrinsicHelper::TestSuspend: {
Logan Chiend54a23d2012-07-24 11:19:23 -07002684 Expand_TestSuspend(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002685 return NULL;
2686 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002687 case IntrinsicHelper::MarkGCCard: {
TDYa1279a129452012-07-19 03:10:08 -07002688 Expand_MarkGCCard(call_inst);
2689 return NULL;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002690 }
Logan Chien75e4b602012-07-23 14:24:12 -07002691
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002692 //==- Exception --------------------------------------------------------==//
2693 case IntrinsicHelper::ThrowException: {
2694 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
2695 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002696 case IntrinsicHelper::HLThrowException: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002697 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2698
2699 EmitUpdateDexPC(dex_pc);
2700
2701 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowException),
2702 call_inst.getArgOperand(0));
2703
2704 EmitGuard_ExceptionLandingPad(dex_pc);
2705 return NULL;
2706 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002707 case IntrinsicHelper::GetException: {
TDYa127823433d2012-09-26 16:03:51 -07002708 return irb_.Runtime().EmitGetAndClearException();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002709 }
2710 case IntrinsicHelper::IsExceptionPending: {
2711 return irb_.Runtime().EmitIsExceptionPending();
2712 }
2713 case IntrinsicHelper::FindCatchBlock: {
2714 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
2715 }
2716 case IntrinsicHelper::ThrowDivZeroException: {
2717 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
2718 }
2719 case IntrinsicHelper::ThrowNullPointerException: {
2720 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
2721 }
2722 case IntrinsicHelper::ThrowIndexOutOfBounds: {
2723 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
2724 }
Logan Chien75e4b602012-07-23 14:24:12 -07002725
2726 //==- Const String -----------------------------------------------------==//
2727 case IntrinsicHelper::ConstString: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002728 return Expand_ConstString(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002729 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002730 case IntrinsicHelper::LoadStringFromDexCache: {
2731 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
2732 }
2733 case IntrinsicHelper::ResolveString: {
2734 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
2735 }
Logan Chien75e4b602012-07-23 14:24:12 -07002736
2737 //==- Const Class ------------------------------------------------------==//
2738 case IntrinsicHelper::ConstClass: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002739 return Expand_ConstClass(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002740 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002741 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
2742 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
2743 }
2744 case IntrinsicHelper::LoadTypeFromDexCache: {
2745 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
2746 }
2747 case IntrinsicHelper::InitializeType: {
2748 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
2749 }
Logan Chien75e4b602012-07-23 14:24:12 -07002750
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002751 //==- Lock -------------------------------------------------------------==//
2752 case IntrinsicHelper::LockObject: {
2753 Expand_LockObject(call_inst.getArgOperand(0));
2754 return NULL;
2755 }
2756 case IntrinsicHelper::UnlockObject: {
2757 Expand_UnlockObject(call_inst.getArgOperand(0));
2758 return NULL;
2759 }
Logan Chien75e4b602012-07-23 14:24:12 -07002760
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002761 //==- Cast -------------------------------------------------------------==//
2762 case IntrinsicHelper::CheckCast: {
2763 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
2764 }
Logan Chien75e4b602012-07-23 14:24:12 -07002765 case IntrinsicHelper::HLCheckCast: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002766 Expand_HLCheckCast(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002767 return NULL;
2768 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002769 case IntrinsicHelper::IsAssignable: {
2770 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
2771 }
Logan Chien75e4b602012-07-23 14:24:12 -07002772
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002773 //==- Alloc ------------------------------------------------------------==//
2774 case IntrinsicHelper::AllocObject: {
2775 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
2776 }
2777 case IntrinsicHelper::AllocObjectWithAccessCheck: {
2778 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
2779 }
Logan Chien75e4b602012-07-23 14:24:12 -07002780
2781 //==- Instance ---------------------------------------------------------==//
2782 case IntrinsicHelper::NewInstance: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002783 return Expand_NewInstance(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002784 }
2785 case IntrinsicHelper::InstanceOf: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002786 return Expand_InstanceOf(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002787 }
2788
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002789 //==- Array ------------------------------------------------------------==//
Logan Chien75e4b602012-07-23 14:24:12 -07002790 case IntrinsicHelper::NewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002791 return Expand_NewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002792 }
2793 case IntrinsicHelper::OptArrayLength: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002794 return Expand_OptArrayLength(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002795 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002796 case IntrinsicHelper::ArrayLength: {
2797 return EmitLoadArrayLength(call_inst.getArgOperand(0));
2798 }
2799 case IntrinsicHelper::AllocArray: {
2800 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
2801 }
2802 case IntrinsicHelper::AllocArrayWithAccessCheck: {
2803 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
2804 call_inst);
2805 }
2806 case IntrinsicHelper::CheckAndAllocArray: {
2807 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
2808 }
2809 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
2810 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
2811 call_inst);
2812 }
2813 case IntrinsicHelper::ArrayGet: {
2814 return Expand_ArrayGet(call_inst.getArgOperand(0),
2815 call_inst.getArgOperand(1),
2816 kInt);
2817 }
2818 case IntrinsicHelper::ArrayGetWide: {
2819 return Expand_ArrayGet(call_inst.getArgOperand(0),
2820 call_inst.getArgOperand(1),
2821 kLong);
2822 }
2823 case IntrinsicHelper::ArrayGetObject: {
2824 return Expand_ArrayGet(call_inst.getArgOperand(0),
2825 call_inst.getArgOperand(1),
2826 kObject);
2827 }
2828 case IntrinsicHelper::ArrayGetBoolean: {
2829 return Expand_ArrayGet(call_inst.getArgOperand(0),
2830 call_inst.getArgOperand(1),
2831 kBoolean);
2832 }
2833 case IntrinsicHelper::ArrayGetByte: {
2834 return Expand_ArrayGet(call_inst.getArgOperand(0),
2835 call_inst.getArgOperand(1),
2836 kByte);
2837 }
2838 case IntrinsicHelper::ArrayGetChar: {
2839 return Expand_ArrayGet(call_inst.getArgOperand(0),
2840 call_inst.getArgOperand(1),
2841 kChar);
2842 }
2843 case IntrinsicHelper::ArrayGetShort: {
2844 return Expand_ArrayGet(call_inst.getArgOperand(0),
2845 call_inst.getArgOperand(1),
2846 kShort);
2847 }
2848 case IntrinsicHelper::ArrayPut: {
2849 Expand_ArrayPut(call_inst.getArgOperand(0),
2850 call_inst.getArgOperand(1),
2851 call_inst.getArgOperand(2),
2852 kInt);
2853 return NULL;
2854 }
2855 case IntrinsicHelper::ArrayPutWide: {
2856 Expand_ArrayPut(call_inst.getArgOperand(0),
2857 call_inst.getArgOperand(1),
2858 call_inst.getArgOperand(2),
2859 kLong);
2860 return NULL;
2861 }
2862 case IntrinsicHelper::ArrayPutObject: {
2863 Expand_ArrayPut(call_inst.getArgOperand(0),
2864 call_inst.getArgOperand(1),
2865 call_inst.getArgOperand(2),
2866 kObject);
2867 return NULL;
2868 }
2869 case IntrinsicHelper::ArrayPutBoolean: {
2870 Expand_ArrayPut(call_inst.getArgOperand(0),
2871 call_inst.getArgOperand(1),
2872 call_inst.getArgOperand(2),
2873 kBoolean);
2874 return NULL;
2875 }
2876 case IntrinsicHelper::ArrayPutByte: {
2877 Expand_ArrayPut(call_inst.getArgOperand(0),
2878 call_inst.getArgOperand(1),
2879 call_inst.getArgOperand(2),
2880 kByte);
2881 return NULL;
2882 }
2883 case IntrinsicHelper::ArrayPutChar: {
2884 Expand_ArrayPut(call_inst.getArgOperand(0),
2885 call_inst.getArgOperand(1),
2886 call_inst.getArgOperand(2),
2887 kChar);
2888 return NULL;
2889 }
2890 case IntrinsicHelper::ArrayPutShort: {
2891 Expand_ArrayPut(call_inst.getArgOperand(0),
2892 call_inst.getArgOperand(1),
2893 call_inst.getArgOperand(2),
2894 kShort);
2895 return NULL;
2896 }
2897 case IntrinsicHelper::CheckPutArrayElement: {
2898 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
2899 }
2900 case IntrinsicHelper::FilledNewArray: {
2901 Expand_FilledNewArray(call_inst);
2902 return NULL;
2903 }
2904 case IntrinsicHelper::FillArrayData: {
2905 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
2906 }
Logan Chien75e4b602012-07-23 14:24:12 -07002907 case IntrinsicHelper::HLFillArrayData: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002908 Expand_HLFillArrayData(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002909 return NULL;
2910 }
2911 case IntrinsicHelper::HLFilledNewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002912 return Expand_HLFilledNewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002913 }
2914
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002915 //==- Instance Field ---------------------------------------------------==//
2916 case IntrinsicHelper::InstanceFieldGet:
2917 case IntrinsicHelper::InstanceFieldGetBoolean:
2918 case IntrinsicHelper::InstanceFieldGetByte:
2919 case IntrinsicHelper::InstanceFieldGetChar:
2920 case IntrinsicHelper::InstanceFieldGetShort: {
2921 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
2922 }
2923 case IntrinsicHelper::InstanceFieldGetWide: {
2924 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
2925 }
2926 case IntrinsicHelper::InstanceFieldGetObject: {
2927 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
2928 }
2929 case IntrinsicHelper::InstanceFieldGetFast: {
2930 return Expand_IGetFast(call_inst.getArgOperand(0),
2931 call_inst.getArgOperand(1),
2932 call_inst.getArgOperand(2),
2933 kInt);
2934 }
2935 case IntrinsicHelper::InstanceFieldGetWideFast: {
2936 return Expand_IGetFast(call_inst.getArgOperand(0),
2937 call_inst.getArgOperand(1),
2938 call_inst.getArgOperand(2),
2939 kLong);
2940 }
2941 case IntrinsicHelper::InstanceFieldGetObjectFast: {
2942 return Expand_IGetFast(call_inst.getArgOperand(0),
2943 call_inst.getArgOperand(1),
2944 call_inst.getArgOperand(2),
2945 kObject);
2946 }
2947 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
2948 return Expand_IGetFast(call_inst.getArgOperand(0),
2949 call_inst.getArgOperand(1),
2950 call_inst.getArgOperand(2),
2951 kBoolean);
2952 }
2953 case IntrinsicHelper::InstanceFieldGetByteFast: {
2954 return Expand_IGetFast(call_inst.getArgOperand(0),
2955 call_inst.getArgOperand(1),
2956 call_inst.getArgOperand(2),
2957 kByte);
2958 }
2959 case IntrinsicHelper::InstanceFieldGetCharFast: {
2960 return Expand_IGetFast(call_inst.getArgOperand(0),
2961 call_inst.getArgOperand(1),
2962 call_inst.getArgOperand(2),
2963 kChar);
2964 }
2965 case IntrinsicHelper::InstanceFieldGetShortFast: {
2966 return Expand_IGetFast(call_inst.getArgOperand(0),
2967 call_inst.getArgOperand(1),
2968 call_inst.getArgOperand(2),
2969 kShort);
2970 }
2971 case IntrinsicHelper::InstanceFieldPut:
2972 case IntrinsicHelper::InstanceFieldPutBoolean:
2973 case IntrinsicHelper::InstanceFieldPutByte:
2974 case IntrinsicHelper::InstanceFieldPutChar:
2975 case IntrinsicHelper::InstanceFieldPutShort: {
2976 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
2977 }
2978 case IntrinsicHelper::InstanceFieldPutWide: {
2979 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
2980 }
2981 case IntrinsicHelper::InstanceFieldPutObject: {
2982 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
2983 }
2984 case IntrinsicHelper::InstanceFieldPutFast: {
2985 Expand_IPutFast(call_inst.getArgOperand(0),
2986 call_inst.getArgOperand(1),
2987 call_inst.getArgOperand(2),
2988 call_inst.getArgOperand(3),
2989 kInt);
2990 return NULL;
2991 }
2992 case IntrinsicHelper::InstanceFieldPutWideFast: {
2993 Expand_IPutFast(call_inst.getArgOperand(0),
2994 call_inst.getArgOperand(1),
2995 call_inst.getArgOperand(2),
2996 call_inst.getArgOperand(3),
2997 kLong);
2998 return NULL;
2999 }
3000 case IntrinsicHelper::InstanceFieldPutObjectFast: {
3001 Expand_IPutFast(call_inst.getArgOperand(0),
3002 call_inst.getArgOperand(1),
3003 call_inst.getArgOperand(2),
3004 call_inst.getArgOperand(3),
3005 kObject);
3006 return NULL;
3007 }
3008 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
3009 Expand_IPutFast(call_inst.getArgOperand(0),
3010 call_inst.getArgOperand(1),
3011 call_inst.getArgOperand(2),
3012 call_inst.getArgOperand(3),
3013 kBoolean);
3014 return NULL;
3015 }
3016 case IntrinsicHelper::InstanceFieldPutByteFast: {
3017 Expand_IPutFast(call_inst.getArgOperand(0),
3018 call_inst.getArgOperand(1),
3019 call_inst.getArgOperand(2),
3020 call_inst.getArgOperand(3),
3021 kByte);
3022 return NULL;
3023 }
3024 case IntrinsicHelper::InstanceFieldPutCharFast: {
3025 Expand_IPutFast(call_inst.getArgOperand(0),
3026 call_inst.getArgOperand(1),
3027 call_inst.getArgOperand(2),
3028 call_inst.getArgOperand(3),
3029 kChar);
3030 return NULL;
3031 }
3032 case IntrinsicHelper::InstanceFieldPutShortFast: {
3033 Expand_IPutFast(call_inst.getArgOperand(0),
3034 call_inst.getArgOperand(1),
3035 call_inst.getArgOperand(2),
3036 call_inst.getArgOperand(3),
3037 kShort);
3038 return NULL;
3039 }
Logan Chien75e4b602012-07-23 14:24:12 -07003040
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003041 //==- Static Field -----------------------------------------------------==//
3042 case IntrinsicHelper::StaticFieldGet:
3043 case IntrinsicHelper::StaticFieldGetBoolean:
3044 case IntrinsicHelper::StaticFieldGetByte:
3045 case IntrinsicHelper::StaticFieldGetChar:
3046 case IntrinsicHelper::StaticFieldGetShort: {
3047 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
3048 }
3049 case IntrinsicHelper::StaticFieldGetWide: {
3050 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
3051 }
3052 case IntrinsicHelper::StaticFieldGetObject: {
3053 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
3054 }
3055 case IntrinsicHelper::StaticFieldGetFast: {
3056 return Expand_SGetFast(call_inst.getArgOperand(0),
3057 call_inst.getArgOperand(1),
3058 call_inst.getArgOperand(2),
3059 kInt);
3060 }
3061 case IntrinsicHelper::StaticFieldGetWideFast: {
3062 return Expand_SGetFast(call_inst.getArgOperand(0),
3063 call_inst.getArgOperand(1),
3064 call_inst.getArgOperand(2),
3065 kLong);
3066 }
3067 case IntrinsicHelper::StaticFieldGetObjectFast: {
3068 return Expand_SGetFast(call_inst.getArgOperand(0),
3069 call_inst.getArgOperand(1),
3070 call_inst.getArgOperand(2),
3071 kObject);
3072 }
3073 case IntrinsicHelper::StaticFieldGetBooleanFast: {
3074 return Expand_SGetFast(call_inst.getArgOperand(0),
3075 call_inst.getArgOperand(1),
3076 call_inst.getArgOperand(2),
3077 kBoolean);
3078 }
3079 case IntrinsicHelper::StaticFieldGetByteFast: {
3080 return Expand_SGetFast(call_inst.getArgOperand(0),
3081 call_inst.getArgOperand(1),
3082 call_inst.getArgOperand(2),
3083 kByte);
3084 }
3085 case IntrinsicHelper::StaticFieldGetCharFast: {
3086 return Expand_SGetFast(call_inst.getArgOperand(0),
3087 call_inst.getArgOperand(1),
3088 call_inst.getArgOperand(2),
3089 kChar);
3090 }
3091 case IntrinsicHelper::StaticFieldGetShortFast: {
3092 return Expand_SGetFast(call_inst.getArgOperand(0),
3093 call_inst.getArgOperand(1),
3094 call_inst.getArgOperand(2),
3095 kShort);
3096 }
3097 case IntrinsicHelper::StaticFieldPut:
3098 case IntrinsicHelper::StaticFieldPutBoolean:
3099 case IntrinsicHelper::StaticFieldPutByte:
3100 case IntrinsicHelper::StaticFieldPutChar:
3101 case IntrinsicHelper::StaticFieldPutShort: {
3102 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
3103 }
3104 case IntrinsicHelper::StaticFieldPutWide: {
3105 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
3106 }
3107 case IntrinsicHelper::StaticFieldPutObject: {
3108 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
3109 }
3110 case IntrinsicHelper::StaticFieldPutFast: {
3111 Expand_SPutFast(call_inst.getArgOperand(0),
3112 call_inst.getArgOperand(1),
3113 call_inst.getArgOperand(2),
3114 call_inst.getArgOperand(3),
3115 kInt);
3116 return NULL;
3117 }
3118 case IntrinsicHelper::StaticFieldPutWideFast: {
3119 Expand_SPutFast(call_inst.getArgOperand(0),
3120 call_inst.getArgOperand(1),
3121 call_inst.getArgOperand(2),
3122 call_inst.getArgOperand(3),
3123 kLong);
3124 return NULL;
3125 }
3126 case IntrinsicHelper::StaticFieldPutObjectFast: {
3127 Expand_SPutFast(call_inst.getArgOperand(0),
3128 call_inst.getArgOperand(1),
3129 call_inst.getArgOperand(2),
3130 call_inst.getArgOperand(3),
3131 kObject);
3132 return NULL;
3133 }
3134 case IntrinsicHelper::StaticFieldPutBooleanFast: {
3135 Expand_SPutFast(call_inst.getArgOperand(0),
3136 call_inst.getArgOperand(1),
3137 call_inst.getArgOperand(2),
3138 call_inst.getArgOperand(3),
3139 kBoolean);
3140 return NULL;
3141 }
3142 case IntrinsicHelper::StaticFieldPutByteFast: {
3143 Expand_SPutFast(call_inst.getArgOperand(0),
3144 call_inst.getArgOperand(1),
3145 call_inst.getArgOperand(2),
3146 call_inst.getArgOperand(3),
3147 kByte);
3148 return NULL;
3149 }
3150 case IntrinsicHelper::StaticFieldPutCharFast: {
3151 Expand_SPutFast(call_inst.getArgOperand(0),
3152 call_inst.getArgOperand(1),
3153 call_inst.getArgOperand(2),
3154 call_inst.getArgOperand(3),
3155 kChar);
3156 return NULL;
3157 }
3158 case IntrinsicHelper::StaticFieldPutShortFast: {
3159 Expand_SPutFast(call_inst.getArgOperand(0),
3160 call_inst.getArgOperand(1),
3161 call_inst.getArgOperand(2),
3162 call_inst.getArgOperand(3),
3163 kShort);
3164 return NULL;
3165 }
3166 case IntrinsicHelper::LoadDeclaringClassSSB: {
3167 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
3168 }
3169 case IntrinsicHelper::LoadClassSSBFromDexCache: {
3170 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
3171 }
3172 case IntrinsicHelper::InitializeAndLoadClassSSB: {
3173 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
3174 }
Logan Chien75e4b602012-07-23 14:24:12 -07003175
3176 //==- High-level Array -------------------------------------------------==//
3177 case IntrinsicHelper::HLArrayGet: {
TDYa1275a26d442012-07-26 18:58:38 -07003178 return Expand_HLArrayGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003179 }
3180 case IntrinsicHelper::HLArrayGetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003181 return Expand_HLArrayGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003182 }
3183 case IntrinsicHelper::HLArrayGetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003184 return Expand_HLArrayGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003185 }
3186 case IntrinsicHelper::HLArrayGetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003187 return Expand_HLArrayGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003188 }
3189 case IntrinsicHelper::HLArrayGetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003190 return Expand_HLArrayGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003191 }
3192 case IntrinsicHelper::HLArrayGetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003193 return Expand_HLArrayGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003194 }
3195 case IntrinsicHelper::HLArrayGetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003196 return Expand_HLArrayGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003197 }
3198 case IntrinsicHelper::HLArrayGetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003199 return Expand_HLArrayGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003200 }
3201 case IntrinsicHelper::HLArrayGetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003202 return Expand_HLArrayGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003203 }
3204 case IntrinsicHelper::HLArrayPut: {
TDYa1275a26d442012-07-26 18:58:38 -07003205 Expand_HLArrayPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003206 return NULL;
3207 }
3208 case IntrinsicHelper::HLArrayPutBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003209 Expand_HLArrayPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003210 return NULL;
3211 }
3212 case IntrinsicHelper::HLArrayPutByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003213 Expand_HLArrayPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003214 return NULL;
3215 }
3216 case IntrinsicHelper::HLArrayPutChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003217 Expand_HLArrayPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003218 return NULL;
3219 }
3220 case IntrinsicHelper::HLArrayPutShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003221 Expand_HLArrayPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003222 return NULL;
3223 }
3224 case IntrinsicHelper::HLArrayPutFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003225 Expand_HLArrayPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003226 return NULL;
3227 }
3228 case IntrinsicHelper::HLArrayPutWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003229 Expand_HLArrayPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003230 return NULL;
3231 }
3232 case IntrinsicHelper::HLArrayPutDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003233 Expand_HLArrayPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003234 return NULL;
3235 }
3236 case IntrinsicHelper::HLArrayPutObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003237 Expand_HLArrayPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003238 return NULL;
3239 }
3240
3241 //==- High-level Instance ----------------------------------------------==//
3242 case IntrinsicHelper::HLIGet: {
TDYa1275e869b62012-07-25 00:45:39 -07003243 return Expand_HLIGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003244 }
3245 case IntrinsicHelper::HLIGetBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003246 return Expand_HLIGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003247 }
3248 case IntrinsicHelper::HLIGetByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003249 return Expand_HLIGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003250 }
3251 case IntrinsicHelper::HLIGetChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003252 return Expand_HLIGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003253 }
3254 case IntrinsicHelper::HLIGetShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003255 return Expand_HLIGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003256 }
3257 case IntrinsicHelper::HLIGetFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003258 return Expand_HLIGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003259 }
3260 case IntrinsicHelper::HLIGetWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003261 return Expand_HLIGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003262 }
3263 case IntrinsicHelper::HLIGetDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003264 return Expand_HLIGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003265 }
3266 case IntrinsicHelper::HLIGetObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003267 return Expand_HLIGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003268 }
3269 case IntrinsicHelper::HLIPut: {
TDYa1275e869b62012-07-25 00:45:39 -07003270 Expand_HLIPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003271 return NULL;
3272 }
3273 case IntrinsicHelper::HLIPutBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003274 Expand_HLIPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003275 return NULL;
3276 }
3277 case IntrinsicHelper::HLIPutByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003278 Expand_HLIPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003279 return NULL;
3280 }
3281 case IntrinsicHelper::HLIPutChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003282 Expand_HLIPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003283 return NULL;
3284 }
3285 case IntrinsicHelper::HLIPutShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003286 Expand_HLIPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003287 return NULL;
3288 }
3289 case IntrinsicHelper::HLIPutFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003290 Expand_HLIPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003291 return NULL;
3292 }
3293 case IntrinsicHelper::HLIPutWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003294 Expand_HLIPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003295 return NULL;
3296 }
3297 case IntrinsicHelper::HLIPutDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003298 Expand_HLIPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003299 return NULL;
3300 }
3301 case IntrinsicHelper::HLIPutObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003302 Expand_HLIPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003303 return NULL;
3304 }
3305
3306 //==- High-level Invoke ------------------------------------------------==//
TDYa127f71bf5a2012-07-29 20:09:52 -07003307 case IntrinsicHelper::HLInvokeVoid:
3308 case IntrinsicHelper::HLInvokeObj:
3309 case IntrinsicHelper::HLInvokeInt:
3310 case IntrinsicHelper::HLInvokeFloat:
3311 case IntrinsicHelper::HLInvokeLong:
Logan Chien75e4b602012-07-23 14:24:12 -07003312 case IntrinsicHelper::HLInvokeDouble: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003313 return Expand_HLInvoke(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003314 }
3315
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003316 //==- Invoke -----------------------------------------------------------==//
3317 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
3318 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
3319 }
3320 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
3321 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
3322 }
3323 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
3324 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
3325 }
3326 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
3327 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
3328 }
3329 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
3330 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
3331 }
3332 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
3333 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
3334 }
3335 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
3336 return Expand_GetVirtualCalleeMethodObjAddrFast(
3337 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
3338 }
3339 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
3340 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
3341 }
3342 case IntrinsicHelper::InvokeRetVoid:
3343 case IntrinsicHelper::InvokeRetBoolean:
3344 case IntrinsicHelper::InvokeRetByte:
3345 case IntrinsicHelper::InvokeRetChar:
3346 case IntrinsicHelper::InvokeRetShort:
3347 case IntrinsicHelper::InvokeRetInt:
3348 case IntrinsicHelper::InvokeRetLong:
3349 case IntrinsicHelper::InvokeRetFloat:
3350 case IntrinsicHelper::InvokeRetDouble:
3351 case IntrinsicHelper::InvokeRetObject: {
3352 return Expand_Invoke(call_inst);
3353 }
Logan Chien75e4b602012-07-23 14:24:12 -07003354
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003355 //==- Math -------------------------------------------------------------==//
3356 case IntrinsicHelper::DivInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003357 return Expand_DivRem(call_inst, /* is_div */true, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003358 }
3359 case IntrinsicHelper::RemInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003360 return Expand_DivRem(call_inst, /* is_div */false, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003361 }
3362 case IntrinsicHelper::DivLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003363 return Expand_DivRem(call_inst, /* is_div */true, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003364 }
3365 case IntrinsicHelper::RemLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003366 return Expand_DivRem(call_inst, /* is_div */false, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003367 }
3368 case IntrinsicHelper::D2L: {
3369 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
3370 }
3371 case IntrinsicHelper::D2I: {
3372 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
3373 }
3374 case IntrinsicHelper::F2L: {
3375 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
3376 }
3377 case IntrinsicHelper::F2I: {
3378 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
3379 }
Logan Chien75e4b602012-07-23 14:24:12 -07003380
3381 //==- High-level Static ------------------------------------------------==//
3382 case IntrinsicHelper::HLSget: {
TDYa1275a26d442012-07-26 18:58:38 -07003383 return Expand_HLSget(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003384 }
3385 case IntrinsicHelper::HLSgetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003386 return Expand_HLSget(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003387 }
3388 case IntrinsicHelper::HLSgetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003389 return Expand_HLSget(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003390 }
3391 case IntrinsicHelper::HLSgetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003392 return Expand_HLSget(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003393 }
3394 case IntrinsicHelper::HLSgetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003395 return Expand_HLSget(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003396 }
3397 case IntrinsicHelper::HLSgetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003398 return Expand_HLSget(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003399 }
3400 case IntrinsicHelper::HLSgetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003401 return Expand_HLSget(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003402 }
3403 case IntrinsicHelper::HLSgetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003404 return Expand_HLSget(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003405 }
3406 case IntrinsicHelper::HLSgetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003407 return Expand_HLSget(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003408 }
3409 case IntrinsicHelper::HLSput: {
TDYa1275a26d442012-07-26 18:58:38 -07003410 Expand_HLSput(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003411 return NULL;
3412 }
3413 case IntrinsicHelper::HLSputBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003414 Expand_HLSput(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003415 return NULL;
3416 }
3417 case IntrinsicHelper::HLSputByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003418 Expand_HLSput(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003419 return NULL;
3420 }
3421 case IntrinsicHelper::HLSputChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003422 Expand_HLSput(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003423 return NULL;
3424 }
3425 case IntrinsicHelper::HLSputShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003426 Expand_HLSput(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003427 return NULL;
3428 }
3429 case IntrinsicHelper::HLSputFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003430 Expand_HLSput(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003431 return NULL;
3432 }
3433 case IntrinsicHelper::HLSputWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003434 Expand_HLSput(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003435 return NULL;
3436 }
3437 case IntrinsicHelper::HLSputDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003438 Expand_HLSput(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003439 return NULL;
3440 }
3441 case IntrinsicHelper::HLSputObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003442 Expand_HLSput(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003443 return NULL;
3444 }
3445
3446 //==- High-level Monitor -----------------------------------------------==//
3447 case IntrinsicHelper::MonitorEnter: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003448 Expand_MonitorEnter(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003449 return NULL;
3450 }
3451 case IntrinsicHelper::MonitorExit: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003452 Expand_MonitorExit(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003453 return NULL;
3454 }
3455
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003456 //==- Shadow Frame -----------------------------------------------------==//
3457 case IntrinsicHelper::AllocaShadowFrame: {
3458 Expand_AllocaShadowFrame(call_inst.getArgOperand(0));
3459 return NULL;
3460 }
3461 case IntrinsicHelper::SetShadowFrameEntry: {
3462 Expand_SetShadowFrameEntry(call_inst.getArgOperand(0),
3463 call_inst.getArgOperand(1));
3464 return NULL;
3465 }
3466 case IntrinsicHelper::PopShadowFrame: {
3467 Expand_PopShadowFrame();
3468 return NULL;
3469 }
3470 case IntrinsicHelper::UpdateDexPC: {
3471 Expand_UpdateDexPC(call_inst.getArgOperand(0));
3472 return NULL;
3473 }
TDYa127a1b21852012-07-23 03:20:39 -07003474
Logan Chien75e4b602012-07-23 14:24:12 -07003475 //==- Comparison -------------------------------------------------------==//
3476 case IntrinsicHelper::CmplFloat:
3477 case IntrinsicHelper::CmplDouble: {
3478 return Expand_FPCompare(call_inst.getArgOperand(0),
3479 call_inst.getArgOperand(1),
3480 false);
3481 }
3482 case IntrinsicHelper::CmpgFloat:
3483 case IntrinsicHelper::CmpgDouble: {
3484 return Expand_FPCompare(call_inst.getArgOperand(0),
3485 call_inst.getArgOperand(1),
3486 true);
3487 }
3488 case IntrinsicHelper::CmpLong: {
3489 return Expand_LongCompare(call_inst.getArgOperand(0),
3490 call_inst.getArgOperand(1));
3491 }
TDYa127a1b21852012-07-23 03:20:39 -07003492
Logan Chien75e4b602012-07-23 14:24:12 -07003493 //==- Const ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003494 case IntrinsicHelper::ConstInt:
3495 case IntrinsicHelper::ConstLong: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003496 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003497 }
TDYa127920be7c2012-09-10 17:13:22 -07003498 case IntrinsicHelper::ConstFloat: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003499 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3500 irb_.getJFloatTy());
Logan Chien75e4b602012-07-23 14:24:12 -07003501 }
TDYa127920be7c2012-09-10 17:13:22 -07003502 case IntrinsicHelper::ConstDouble: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003503 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3504 irb_.getJDoubleTy());
3505 }
TDYa127920be7c2012-09-10 17:13:22 -07003506 case IntrinsicHelper::ConstObj: {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003507 CHECK(LV2UInt(call_inst.getArgOperand(0)) == 0);
3508 return irb_.getJNull();
Logan Chien75e4b602012-07-23 14:24:12 -07003509 }
3510
3511 //==- Method Info ------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003512 case IntrinsicHelper::MethodInfo: {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003513 // Nothing to be done, because MethodInfo carries optional hints that are
3514 // not needed by the portable path.
Logan Chien75e4b602012-07-23 14:24:12 -07003515 return NULL;
3516 }
3517
3518 //==- Copy -------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003519 case IntrinsicHelper::CopyInt:
3520 case IntrinsicHelper::CopyFloat:
3521 case IntrinsicHelper::CopyLong:
3522 case IntrinsicHelper::CopyDouble:
3523 case IntrinsicHelper::CopyObj: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003524 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003525 }
3526
3527 //==- Shift ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003528 case IntrinsicHelper::SHLLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003529 return Expand_IntegerShift(call_inst.getArgOperand(0),
3530 call_inst.getArgOperand(1),
3531 kIntegerSHL, kLong);
3532 }
TDYa127920be7c2012-09-10 17:13:22 -07003533 case IntrinsicHelper::SHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003534 return Expand_IntegerShift(call_inst.getArgOperand(0),
3535 call_inst.getArgOperand(1),
3536 kIntegerSHR, kLong);
3537 }
TDYa127920be7c2012-09-10 17:13:22 -07003538 case IntrinsicHelper::USHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003539 return Expand_IntegerShift(call_inst.getArgOperand(0),
3540 call_inst.getArgOperand(1),
3541 kIntegerUSHR, kLong);
3542 }
TDYa127920be7c2012-09-10 17:13:22 -07003543 case IntrinsicHelper::SHLInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003544 return Expand_IntegerShift(call_inst.getArgOperand(0),
3545 call_inst.getArgOperand(1),
3546 kIntegerSHL, kInt);
3547 }
TDYa127920be7c2012-09-10 17:13:22 -07003548 case IntrinsicHelper::SHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003549 return Expand_IntegerShift(call_inst.getArgOperand(0),
3550 call_inst.getArgOperand(1),
3551 kIntegerSHR, kInt);
3552 }
TDYa127920be7c2012-09-10 17:13:22 -07003553 case IntrinsicHelper::USHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003554 return Expand_IntegerShift(call_inst.getArgOperand(0),
3555 call_inst.getArgOperand(1),
3556 kIntegerUSHR, kInt);
3557 }
3558
3559 //==- Conversion -------------------------------------------------------==//
TDYa127a1b21852012-07-23 03:20:39 -07003560 case IntrinsicHelper::IntToChar: {
3561 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
3562 irb_.getJIntTy());
3563 }
3564 case IntrinsicHelper::IntToShort: {
3565 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
3566 irb_.getJIntTy());
3567 }
3568 case IntrinsicHelper::IntToByte: {
3569 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
3570 irb_.getJIntTy());
3571 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003572
TDYa12787caa7e2012-08-25 23:23:27 -07003573 //==- Exception --------------------------------------------------------==//
3574 case IntrinsicHelper::CatchTargets: {
TDYa12755e5e6c2012-09-11 15:14:42 -07003575 UpdatePhiInstruction(current_bb_, irb_.GetInsertBlock());
TDYa12787caa7e2012-08-25 23:23:27 -07003576 llvm::SwitchInst* si = llvm::dyn_cast<llvm::SwitchInst>(call_inst.getNextNode());
3577 CHECK(si != NULL);
3578 irb_.CreateBr(si->getDefaultDest());
3579 si->eraseFromParent();
3580 return call_inst.getArgOperand(0);
3581 }
3582
Logan Chien75e4b602012-07-23 14:24:12 -07003583 //==- Unknown Cases ----------------------------------------------------==//
3584 case IntrinsicHelper::MaxIntrinsicId:
3585 case IntrinsicHelper::UnknownId:
3586 //default:
3587 // NOTE: "default" is intentionally commented so that C/C++ compiler will
3588 // give some warning on unmatched cases.
3589 // NOTE: We should not implement these cases.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003590 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003591 }
Logan Chien75e4b602012-07-23 14:24:12 -07003592 UNIMPLEMENTED(FATAL) << "Unexpected GBC intrinsic: " << static_cast<int>(intr_id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003593 return NULL;
3594}
3595
3596} // anonymous namespace
3597
3598namespace art {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003599
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003600namespace compiler_llvm {
3601
3602llvm::FunctionPass*
3603CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb) {
3604 return new GBCExpanderPass(intrinsic_helper, irb);
3605}
3606
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003607llvm::FunctionPass*
3608CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
3609 Compiler* compiler, OatCompilationUnit* oat_compilation_unit) {
3610 if (compiler != NULL) {
3611 return new GBCExpanderPass(intrinsic_helper, irb,
3612 compiler, oat_compilation_unit);
3613 } else {
3614 return new GBCExpanderPass(intrinsic_helper, irb);
3615 }
3616}
3617
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003618} // namespace compiler_llvm
3619} // namespace art