blob: 57a13d56d31fea58697cf90fb8bbe90cd5c870d4 [file] [log] [blame]
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ir_builder.h"
18#include "utils_llvm.h"
19
TDYa1275e869b62012-07-25 00:45:39 -070020#include "compiler.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070021#include "greenland/intrinsic_helper.h"
TDYa1275e869b62012-07-25 00:45:39 -070022#include "oat_compilation_unit.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070023#include "object.h"
24#include "thread.h"
TDYa1275e869b62012-07-25 00:45:39 -070025#include "verifier/method_verifier.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070026
buzbeeefc63692012-11-14 16:31:52 -080027#include "compiler/compiler_ir.h"
buzbee449a49b2012-11-25 19:25:44 -080028#include "compiler/codegen/codegen.h"
TDYa127920be7c2012-09-10 17:13:22 -070029using art::kMIRIgnoreNullCheck;
30using art::kMIRIgnoreRangeCheck;
31
Shih-wei Liao21d28f52012-06-12 05:55:00 -070032#include <llvm/ADT/STLExtras.h>
33#include <llvm/Intrinsics.h>
Logan Chiend36a2ac2012-08-04 00:37:30 +080034#include <llvm/Metadata.h>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070035#include <llvm/Pass.h>
36#include <llvm/Support/CFG.h>
37#include <llvm/Support/InstIterator.h>
38
39#include <vector>
TDYa127aa558872012-08-16 05:11:07 -070040#include <map>
41#include <utility>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070042
TDYa127920be7c2012-09-10 17:13:22 -070043using namespace art::compiler_llvm;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070044
45using art::greenland::IntrinsicHelper;
46
Shih-wei Liaob2596522012-09-14 16:36:11 -070047namespace art {
48extern char remapShorty(char shortyType);
49};
50
Shih-wei Liao21d28f52012-06-12 05:55:00 -070051namespace {
52
53class GBCExpanderPass : public llvm::FunctionPass {
54 private:
55 const IntrinsicHelper& intrinsic_helper_;
56 IRBuilder& irb_;
57
58 llvm::LLVMContext& context_;
59 RuntimeSupportBuilder& rtb_;
60
61 private:
62 llvm::AllocaInst* shadow_frame_;
63 llvm::Value* old_shadow_frame_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070064
65 private:
TDYa127920be7c2012-09-10 17:13:22 -070066 art::Compiler* compiler_;
TDYa1275e869b62012-07-25 00:45:39 -070067
TDYa127920be7c2012-09-10 17:13:22 -070068 const art::DexFile* dex_file_;
69 const art::DexFile::CodeItem* code_item_;
TDYa1275e869b62012-07-25 00:45:39 -070070
TDYa127920be7c2012-09-10 17:13:22 -070071 art::OatCompilationUnit* oat_compilation_unit_;
TDYa1275e869b62012-07-25 00:45:39 -070072
73 uint32_t method_idx_;
74
75 llvm::Function* func_;
76
77 std::vector<llvm::BasicBlock*> basic_blocks_;
78
79 std::vector<llvm::BasicBlock*> basic_block_landing_pads_;
TDYa12755e5e6c2012-09-11 15:14:42 -070080 llvm::BasicBlock* current_bb_;
TDYa127aa558872012-08-16 05:11:07 -070081 std::map<llvm::BasicBlock*, std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> > >
82 landing_pad_phi_mapping_;
TDYa1275e869b62012-07-25 00:45:39 -070083 llvm::BasicBlock* basic_block_unwind_;
84
Logan Chien67645d82012-08-17 09:10:54 +080085 bool changed_;
86
TDYa1275e869b62012-07-25 00:45:39 -070087 private:
Shih-wei Liao21d28f52012-06-12 05:55:00 -070088 //----------------------------------------------------------------------------
Logan Chien75e4b602012-07-23 14:24:12 -070089 // Constant for GBC expansion
90 //----------------------------------------------------------------------------
91 enum IntegerShiftKind {
92 kIntegerSHL,
93 kIntegerSHR,
94 kIntegerUSHR,
95 };
96
97 private:
98 //----------------------------------------------------------------------------
Shih-wei Liao21d28f52012-06-12 05:55:00 -070099 // Helper function for GBC expansion
100 //----------------------------------------------------------------------------
101
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700102 llvm::Value* ExpandToRuntime(runtime_support::RuntimeId rt,
103 llvm::CallInst& inst);
104
TDYa1275e869b62012-07-25 00:45:39 -0700105 uint64_t LV2UInt(llvm::Value* lv) {
106 return llvm::cast<llvm::ConstantInt>(lv)->getZExtValue();
107 }
108
109 int64_t LV2SInt(llvm::Value* lv) {
110 return llvm::cast<llvm::ConstantInt>(lv)->getSExtValue();
111 }
112
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700113 private:
114 // TODO: Almost all Emit* are directly copy-n-paste from MethodCompiler.
115 // Refactor these utility functions from MethodCompiler to avoid forking.
116
Logan Chien67645d82012-08-17 09:10:54 +0800117 void EmitStackOverflowCheck(llvm::Instruction* first_non_alloca);
118
119 void RewriteFunction();
120
121 void RewriteBasicBlock(llvm::BasicBlock* original_block);
122
123 void UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
124 llvm::BasicBlock* new_basic_block);
125
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700126
127 //----------------------------------------------------------------------------
128 // Dex cache code generation helper function
129 //----------------------------------------------------------------------------
TDYa127920be7c2012-09-10 17:13:22 -0700130 llvm::Value* EmitLoadDexCacheAddr(art::MemberOffset dex_cache_offset);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700131
132 llvm::Value* EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx);
133
134 llvm::Value* EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx);
135
136 llvm::Value* EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx);
137
138 llvm::Value* EmitLoadDexCacheStringFieldAddr(uint32_t string_idx);
139
140 //----------------------------------------------------------------------------
141 // Code generation helper function
142 //----------------------------------------------------------------------------
143 llvm::Value* EmitLoadMethodObjectAddr();
144
145 llvm::Value* EmitLoadArrayLength(llvm::Value* array);
146
147 llvm::Value* EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx);
148
149 llvm::Value* EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
150 llvm::Value* this_addr);
151
152 llvm::Value* EmitArrayGEP(llvm::Value* array_addr,
153 llvm::Value* index_value,
154 JType elem_jty);
155
156 private:
157 //----------------------------------------------------------------------------
158 // Expand Greenland intrinsics
159 //----------------------------------------------------------------------------
160 void Expand_TestSuspend(llvm::CallInst& call_inst);
161
TDYa1279a129452012-07-19 03:10:08 -0700162 void Expand_MarkGCCard(llvm::CallInst& call_inst);
163
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700164 llvm::Value* Expand_LoadStringFromDexCache(llvm::Value* string_idx_value);
165
166 llvm::Value* Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value);
167
168 void Expand_LockObject(llvm::Value* obj);
169
170 void Expand_UnlockObject(llvm::Value* obj);
171
172 llvm::Value* Expand_ArrayGet(llvm::Value* array_addr,
173 llvm::Value* index_value,
174 JType elem_jty);
175
176 void Expand_ArrayPut(llvm::Value* new_value,
177 llvm::Value* array_addr,
178 llvm::Value* index_value,
179 JType elem_jty);
180
181 void Expand_FilledNewArray(llvm::CallInst& call_inst);
182
183 llvm::Value* Expand_IGetFast(llvm::Value* field_offset_value,
184 llvm::Value* is_volatile_value,
185 llvm::Value* object_addr,
186 JType field_jty);
187
188 void Expand_IPutFast(llvm::Value* field_offset_value,
189 llvm::Value* is_volatile_value,
190 llvm::Value* object_addr,
191 llvm::Value* new_value,
192 JType field_jty);
193
194 llvm::Value* Expand_SGetFast(llvm::Value* static_storage_addr,
195 llvm::Value* field_offset_value,
196 llvm::Value* is_volatile_value,
197 JType field_jty);
198
199 void Expand_SPutFast(llvm::Value* static_storage_addr,
200 llvm::Value* field_offset_value,
201 llvm::Value* is_volatile_value,
202 llvm::Value* new_value,
203 JType field_jty);
204
205 llvm::Value* Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr);
206
207 llvm::Value* Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value);
208
209 llvm::Value*
210 Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value);
211
212 llvm::Value*
213 Expand_GetVirtualCalleeMethodObjAddrFast(llvm::Value* vtable_idx_value,
214 llvm::Value* this_addr);
215
216 llvm::Value* Expand_Invoke(llvm::CallInst& call_inst);
217
TDYa1274ec8ccd2012-08-11 07:04:57 -0700218 llvm::Value* Expand_DivRem(llvm::CallInst& call_inst, bool is_div, JType op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700219
TDYa1278e950c12012-11-02 09:58:19 -0700220 void Expand_AllocaShadowFrame(llvm::Value* num_entry_value, llvm::Value* num_vregs_value);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700221
222 void Expand_SetShadowFrameEntry(llvm::Value* obj, llvm::Value* entry_idx);
223
TDYa1278e950c12012-11-02 09:58:19 -0700224 void Expand_SetVReg(llvm::Value* entry_idx, llvm::Value* obj);
225
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700226 void Expand_PopShadowFrame();
227
228 void Expand_UpdateDexPC(llvm::Value* dex_pc_value);
229
TDYa127a1b21852012-07-23 03:20:39 -0700230 //----------------------------------------------------------------------------
231 // Quick
232 //----------------------------------------------------------------------------
233
234 llvm::Value* Expand_FPCompare(llvm::Value* src1_value,
235 llvm::Value* src2_value,
236 bool gt_bias);
237
238 llvm::Value* Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value);
239
240 llvm::Value* EmitCompareResultSelection(llvm::Value* cmp_eq,
241 llvm::Value* cmp_lt);
242
TDYa127f71bf5a2012-07-29 20:09:52 -0700243 llvm::Value* EmitLoadConstantClass(uint32_t dex_pc, uint32_t type_idx);
TDYa1275a26d442012-07-26 18:58:38 -0700244 llvm::Value* EmitLoadStaticStorage(uint32_t dex_pc, uint32_t type_idx);
245
TDYa1275e869b62012-07-25 00:45:39 -0700246 llvm::Value* Expand_HLIGet(llvm::CallInst& call_inst, JType field_jty);
247 void Expand_HLIPut(llvm::CallInst& call_inst, JType field_jty);
248
TDYa1275a26d442012-07-26 18:58:38 -0700249 llvm::Value* Expand_HLSget(llvm::CallInst& call_inst, JType field_jty);
250 void Expand_HLSput(llvm::CallInst& call_inst, JType field_jty);
251
252 llvm::Value* Expand_HLArrayGet(llvm::CallInst& call_inst, JType field_jty);
253 void Expand_HLArrayPut(llvm::CallInst& call_inst, JType field_jty);
254
TDYa127f71bf5a2012-07-29 20:09:52 -0700255 llvm::Value* Expand_ConstString(llvm::CallInst& call_inst);
256 llvm::Value* Expand_ConstClass(llvm::CallInst& call_inst);
257
258 void Expand_MonitorEnter(llvm::CallInst& call_inst);
259 void Expand_MonitorExit(llvm::CallInst& call_inst);
260
261 void Expand_HLCheckCast(llvm::CallInst& call_inst);
262 llvm::Value* Expand_InstanceOf(llvm::CallInst& call_inst);
263
264 llvm::Value* Expand_NewInstance(llvm::CallInst& call_inst);
265
266 llvm::Value* Expand_HLInvoke(llvm::CallInst& call_inst);
267
268 llvm::Value* Expand_OptArrayLength(llvm::CallInst& call_inst);
269 llvm::Value* Expand_NewArray(llvm::CallInst& call_inst);
270 llvm::Value* Expand_HLFilledNewArray(llvm::CallInst& call_inst);
271 void Expand_HLFillArrayData(llvm::CallInst& call_inst);
272
273 llvm::Value* EmitAllocNewArray(uint32_t dex_pc,
274 llvm::Value* array_length_value,
275 uint32_t type_idx,
276 bool is_filled_new_array);
277
278 llvm::Value* EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -0700279 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -0700280 llvm::Value* this_addr,
281 uint32_t dex_pc,
282 bool is_fast_path);
283
TDYa1275e869b62012-07-25 00:45:39 -0700284 void EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr);
285
286 void EmitUpdateDexPC(uint32_t dex_pc);
287
288 void EmitGuard_DivZeroException(uint32_t dex_pc,
289 llvm::Value* denominator,
290 JType op_jty);
291
292 void EmitGuard_NullPointerException(uint32_t dex_pc,
293 llvm::Value* object);
294
295 void EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
296 llvm::Value* array,
297 llvm::Value* index);
298
TDYa1275e869b62012-07-25 00:45:39 -0700299 llvm::FunctionType* GetFunctionType(uint32_t method_idx, bool is_static);
300
301 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
302
303 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
304 const char* postfix);
305
306 int32_t GetTryItemOffset(uint32_t dex_pc);
307
308 llvm::BasicBlock* GetLandingPadBasicBlock(uint32_t dex_pc);
309
310 llvm::BasicBlock* GetUnwindBasicBlock();
311
312 void EmitGuard_ExceptionLandingPad(uint32_t dex_pc);
313
314 void EmitBranchExceptionLandingPad(uint32_t dex_pc);
315
Logan Chien75e4b602012-07-23 14:24:12 -0700316 //----------------------------------------------------------------------------
317 // Expand Arithmetic Helper Intrinsics
318 //----------------------------------------------------------------------------
319
320 llvm::Value* Expand_IntegerShift(llvm::Value* src1_value,
321 llvm::Value* src2_value,
322 IntegerShiftKind kind,
323 JType op_jty);
324
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700325 public:
326 static char ID;
327
328 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb)
329 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
Logan Chiene5b8f8b2012-08-13 11:45:05 +0800330 context_(irb.getContext()), rtb_(irb.Runtime()),
TDYa1278e950c12012-11-02 09:58:19 -0700331 shadow_frame_(NULL), old_shadow_frame_(NULL),
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700332 compiler_(NULL), dex_file_(NULL), code_item_(NULL),
Logan Chien67645d82012-08-17 09:10:54 +0800333 oat_compilation_unit_(NULL), method_idx_(-1u), func_(NULL),
334 changed_(false)
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700335 { }
336
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700337 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
TDYa127920be7c2012-09-10 17:13:22 -0700338 art::Compiler* compiler, art::OatCompilationUnit* oat_compilation_unit)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700339 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
340 context_(irb.getContext()), rtb_(irb.Runtime()),
TDYa1278e950c12012-11-02 09:58:19 -0700341 shadow_frame_(NULL), old_shadow_frame_(NULL),
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700342 compiler_(compiler),
343 dex_file_(oat_compilation_unit->GetDexFile()),
344 code_item_(oat_compilation_unit->GetCodeItem()),
345 oat_compilation_unit_(oat_compilation_unit),
346 method_idx_(oat_compilation_unit->GetDexMethodIndex()),
347 func_(NULL), changed_(false)
348 { }
349
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700350 bool runOnFunction(llvm::Function& func);
351
352 private:
Logan Chien67645d82012-08-17 09:10:54 +0800353 void InsertStackOverflowCheck(llvm::Function& func);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700354
355 llvm::Value* ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
356 llvm::CallInst& call_inst);
357
358};
359
360char GBCExpanderPass::ID = 0;
361
362bool GBCExpanderPass::runOnFunction(llvm::Function& func) {
TDYa127b672d1e2012-06-28 21:21:45 -0700363 // Runtime support or stub
364 if (func.getName().startswith("art_") || func.getName().startswith("Art")) {
365 return false;
366 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700367
Logan Chien67645d82012-08-17 09:10:54 +0800368 // Setup rewrite context
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700369 shadow_frame_ = NULL;
370 old_shadow_frame_ = NULL;
TDYa1275e869b62012-07-25 00:45:39 -0700371 func_ = &func;
Logan Chien67645d82012-08-17 09:10:54 +0800372 changed_ = false; // Assume unchanged
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700373
buzbeec531cef2012-10-18 07:09:20 -0700374#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700375 basic_blocks_.resize(code_item_->insns_size_in_code_units_);
376 basic_block_landing_pads_.resize(code_item_->tries_size_, NULL);
377 basic_block_unwind_ = NULL;
378 for (llvm::Function::iterator bb_iter = func_->begin(), bb_end = func_->end();
379 bb_iter != bb_end;
380 ++bb_iter) {
381 if (bb_iter->begin()->getMetadata("DexOff") == NULL) {
382 continue;
383 }
384 uint32_t dex_pc = LV2UInt(bb_iter->begin()->getMetadata("DexOff")->getOperand(0));
385 basic_blocks_[dex_pc] = bb_iter;
386 }
387#endif
388
Logan Chien67645d82012-08-17 09:10:54 +0800389 // Insert stack overflow check
390 InsertStackOverflowCheck(func); // TODO: Use intrinsic.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700391
Logan Chien67645d82012-08-17 09:10:54 +0800392 // Rewrite the intrinsics
393 RewriteFunction();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700394
395 VERIFY_LLVM_FUNCTION(func);
396
Logan Chien67645d82012-08-17 09:10:54 +0800397 return changed_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700398}
399
Logan Chien67645d82012-08-17 09:10:54 +0800400void GBCExpanderPass::RewriteBasicBlock(llvm::BasicBlock* original_block) {
401 llvm::BasicBlock* curr_basic_block = original_block;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700402
Logan Chien67645d82012-08-17 09:10:54 +0800403 llvm::BasicBlock::iterator inst_iter = original_block->begin();
404 llvm::BasicBlock::iterator inst_end = original_block->end();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700405
Logan Chien67645d82012-08-17 09:10:54 +0800406 while (inst_iter != inst_end) {
407 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst_iter);
408 IntrinsicHelper::IntrinsicId intr_id = IntrinsicHelper::UnknownId;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700409
Logan Chien67645d82012-08-17 09:10:54 +0800410 if (call_inst) {
411 llvm::Function* callee_func = call_inst->getCalledFunction();
412 intr_id = intrinsic_helper_.GetIntrinsicId(callee_func);
413 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700414
Logan Chien67645d82012-08-17 09:10:54 +0800415 if (intr_id == IntrinsicHelper::UnknownId) {
416 // This is not intrinsic call. Skip this instruction.
417 ++inst_iter;
418 continue;
419 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700420
Logan Chien67645d82012-08-17 09:10:54 +0800421 // Rewrite the intrinsic and change the function
422 changed_ = true;
423 irb_.SetInsertPoint(inst_iter);
424
425 // Expand the intrinsic
426 if (llvm::Value* new_value = ExpandIntrinsic(intr_id, *call_inst)) {
427 inst_iter->replaceAllUsesWith(new_value);
428 }
429
430 // Remove the old intrinsic call instruction
431 llvm::BasicBlock::iterator old_inst = inst_iter++;
432 old_inst->eraseFromParent();
433
434 // Splice the instruction to the new basic block
435 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
436 if (next_basic_block != curr_basic_block) {
437 next_basic_block->getInstList().splice(
438 irb_.GetInsertPoint(), curr_basic_block->getInstList(),
439 inst_iter, inst_end);
440 curr_basic_block = next_basic_block;
441 inst_end = curr_basic_block->end();
442 }
443 }
444}
445
446
447void GBCExpanderPass::RewriteFunction() {
448 size_t num_basic_blocks = func_->getBasicBlockList().size();
449 // NOTE: We are not using (bb_iter != bb_end) as the for-loop condition,
450 // because we will create new basic block while expanding the intrinsics.
451 // We only want to iterate through the input basic blocks.
452
TDYa127aa558872012-08-16 05:11:07 -0700453 landing_pad_phi_mapping_.clear();
454
Logan Chien67645d82012-08-17 09:10:54 +0800455 for (llvm::Function::iterator bb_iter = func_->begin();
456 num_basic_blocks > 0; ++bb_iter, --num_basic_blocks) {
Shih-wei Liao627d8c42012-08-24 08:31:18 -0700457 // Set insert point to current basic block.
458 irb_.SetInsertPoint(bb_iter);
Logan Chien67645d82012-08-17 09:10:54 +0800459
TDYa12755e5e6c2012-09-11 15:14:42 -0700460 current_bb_ = bb_iter;
TDYa127aa558872012-08-16 05:11:07 -0700461
Logan Chien67645d82012-08-17 09:10:54 +0800462 // Rewrite the basic block
463 RewriteBasicBlock(bb_iter);
464
465 // Update the phi-instructions in the successor basic block
466 llvm::BasicBlock* last_block = irb_.GetInsertBlock();
467 if (last_block != bb_iter) {
468 UpdatePhiInstruction(bb_iter, last_block);
469 }
470 }
TDYa127aa558872012-08-16 05:11:07 -0700471
472 typedef std::map<llvm::PHINode*, llvm::PHINode*> HandlerPHIMap;
473 HandlerPHIMap handler_phi;
474 // Iterate every used landing pad basic block
475 for (size_t i = 0, ei = basic_block_landing_pads_.size(); i != ei; ++i) {
476 llvm::BasicBlock* lbb = basic_block_landing_pads_[i];
477 if (lbb == NULL) {
478 continue;
479 }
480
481 llvm::TerminatorInst* term_inst = lbb->getTerminator();
482 std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> >& rewrite_pair
483 = landing_pad_phi_mapping_[lbb];
484 irb_.SetInsertPoint(lbb->begin());
485
486 // Iterate every succeeding basic block (catch block)
487 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
488 succ_iter != succ_end; ++succ_iter) {
489 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
490
491 // Iterate every phi instructions in the succeeding basic block
492 for (llvm::BasicBlock::iterator
493 inst_iter = succ_basic_block->begin(),
494 inst_end = succ_basic_block->end();
495 inst_iter != inst_end; ++inst_iter) {
496 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
497
498 if (!phi) {
499 break; // Meet non-phi instruction. Done.
500 }
501
502 if (handler_phi[phi] == NULL) {
503 handler_phi[phi] = llvm::PHINode::Create(phi->getType(), 1);
504 }
505
506 // Create new_phi in landing pad
507 llvm::PHINode* new_phi = irb_.CreatePHI(phi->getType(), rewrite_pair.size());
508 // Insert all incoming value into new_phi by rewrite_pair
509 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
510 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
511 llvm::BasicBlock* new_bb = rewrite_pair[j].second;
512 new_phi->addIncoming(phi->getIncomingValueForBlock(old_bb), new_bb);
513 }
514 // Delete all incoming value from phi by rewrite_pair
515 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
516 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
517 int old_bb_idx = phi->getBasicBlockIndex(old_bb);
518 if (old_bb_idx >= 0) {
519 phi->removeIncomingValue(old_bb_idx, false);
520 }
521 }
522 // Insert new_phi into new handler phi
523 handler_phi[phi]->addIncoming(new_phi, lbb);
524 }
525 }
526 }
527
528 // Replace all handler phi
529 // We can't just use the old handler phi, because some exception edges will disappear after we
530 // compute fast-path.
531 for (HandlerPHIMap::iterator it = handler_phi.begin(); it != handler_phi.end(); ++it) {
532 llvm::PHINode* old_phi = it->first;
533 llvm::PHINode* new_phi = it->second;
534 new_phi->insertBefore(old_phi);
535 old_phi->replaceAllUsesWith(new_phi);
536 old_phi->eraseFromParent();
537 }
Logan Chien67645d82012-08-17 09:10:54 +0800538}
539
540void GBCExpanderPass::UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
541 llvm::BasicBlock* new_basic_block) {
542 llvm::TerminatorInst* term_inst = new_basic_block->getTerminator();
543
544 if (!term_inst) {
545 return; // No terminating instruction in new_basic_block. Nothing to do.
546 }
547
548 // Iterate every succeeding basic block
549 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
550 succ_iter != succ_end; ++succ_iter) {
551 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
552
553 // Iterate every phi instructions in the succeeding basic block
554 for (llvm::BasicBlock::iterator
555 inst_iter = succ_basic_block->begin(),
556 inst_end = succ_basic_block->end();
557 inst_iter != inst_end; ++inst_iter) {
558 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
559
560 if (!phi) {
561 break; // Meet non-phi instruction. Done.
562 }
563
564 // Update the incoming block of this phi instruction
565 for (llvm::PHINode::block_iterator
566 ibb_iter = phi->block_begin(), ibb_end = phi->block_end();
567 ibb_iter != ibb_end; ++ibb_iter) {
568 if (*ibb_iter == old_basic_block) {
569 *ibb_iter = new_basic_block;
570 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700571 }
572 }
573 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700574}
575
576llvm::Value* GBCExpanderPass::ExpandToRuntime(runtime_support::RuntimeId rt,
577 llvm::CallInst& inst) {
578 // Some GBC intrinsic can directly replace with IBC runtime. "Directly" means
579 // the arguments passed to the GBC intrinsic are as the same as IBC runtime
580 // function, therefore only called function is needed to change.
581 unsigned num_args = inst.getNumArgOperands();
582
583 if (num_args <= 0) {
584 return irb_.CreateCall(irb_.GetRuntime(rt));
585 } else {
586 std::vector<llvm::Value*> args;
587 for (unsigned i = 0; i < num_args; i++) {
588 args.push_back(inst.getArgOperand(i));
589 }
590
591 return irb_.CreateCall(irb_.GetRuntime(rt), args);
592 }
593}
594
Logan Chien67645d82012-08-17 09:10:54 +0800595void
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700596GBCExpanderPass::EmitStackOverflowCheck(llvm::Instruction* first_non_alloca) {
597 llvm::Function* func = first_non_alloca->getParent()->getParent();
598 llvm::Module* module = func->getParent();
599
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700600 // Call llvm intrinsic function to get frame address.
601 llvm::Function* frameaddress =
602 llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
603
604 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
605 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
606
607 // Cast i8* to int
608 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
609
610 // Get thread.stack_end_
611 llvm::Value* stack_end =
TDYa127920be7c2012-09-10 17:13:22 -0700612 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::StackEndOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700613 irb_.getPtrEquivIntTy(),
614 kTBAARuntimeInfo);
615
616 // Check the frame address < thread.stack_end_ ?
617 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
618
619 llvm::BasicBlock* block_exception =
620 llvm::BasicBlock::Create(context_, "stack_overflow", func);
621
622 llvm::BasicBlock* block_continue =
623 llvm::BasicBlock::Create(context_, "stack_overflow_cont", func);
624
625 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
626
627 // If stack overflow, throw exception.
628 irb_.SetInsertPoint(block_exception);
629 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowStackOverflowException));
630
631 // Unwind.
632 llvm::Type* ret_type = func->getReturnType();
633 if (ret_type->isVoidTy()) {
634 irb_.CreateRetVoid();
635 } else {
636 // The return value is ignored when there's an exception. MethodCompiler
637 // returns zero value under the the corresponding return type in this case.
638 // GBCExpander returns LLVM undef value here for brevity
639 irb_.CreateRet(llvm::UndefValue::get(ret_type));
640 }
641
642 irb_.SetInsertPoint(block_continue);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700643}
644
TDYa127920be7c2012-09-10 17:13:22 -0700645llvm::Value* GBCExpanderPass::EmitLoadDexCacheAddr(art::MemberOffset offset) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700646 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
647
648 return irb_.LoadFromObjectOffset(method_object_addr,
649 offset.Int32Value(),
650 irb_.getJObjectTy(),
651 kTBAAConstJObject);
652}
653
654llvm::Value*
655GBCExpanderPass::EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
656 llvm::Value* static_storage_dex_cache_addr =
Mathieu Chartier66f19252012-09-18 08:57:04 -0700657 EmitLoadDexCacheAddr(art::AbstractMethod::DexCacheInitializedStaticStorageOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700658
659 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
660
661 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
662}
663
664llvm::Value*
665GBCExpanderPass::EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
666 llvm::Value* resolved_type_dex_cache_addr =
Mathieu Chartier66f19252012-09-18 08:57:04 -0700667 EmitLoadDexCacheAddr(art::AbstractMethod::DexCacheResolvedTypesOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700668
669 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
670
671 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
672}
673
674llvm::Value* GBCExpanderPass::
675EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
676 llvm::Value* resolved_method_dex_cache_addr =
Mathieu Chartier66f19252012-09-18 08:57:04 -0700677 EmitLoadDexCacheAddr(art::AbstractMethod::DexCacheResolvedMethodsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700678
679 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
680
681 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
682}
683
684llvm::Value* GBCExpanderPass::
685EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
686 llvm::Value* string_dex_cache_addr =
Mathieu Chartier66f19252012-09-18 08:57:04 -0700687 EmitLoadDexCacheAddr(art::AbstractMethod::DexCacheStringsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700688
689 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
690
691 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
692}
693
694llvm::Value* GBCExpanderPass::EmitLoadMethodObjectAddr() {
695 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
696 return parent_func->arg_begin();
697}
698
699llvm::Value* GBCExpanderPass::EmitLoadArrayLength(llvm::Value* array) {
700 // Load array length
701 return irb_.LoadFromObjectOffset(array,
TDYa127920be7c2012-09-10 17:13:22 -0700702 art::Array::LengthOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700703 irb_.getJIntTy(),
704 kTBAAConstJObject);
705
706}
707
708llvm::Value*
709GBCExpanderPass::EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
710 llvm::Value* callee_method_object_field_addr =
711 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
712
713 return irb_.CreateLoad(callee_method_object_field_addr, kTBAAJRuntime);
714}
715
716llvm::Value* GBCExpanderPass::
717EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, llvm::Value* this_addr) {
718 // Load class object of *this* pointer
719 llvm::Value* class_object_addr =
720 irb_.LoadFromObjectOffset(this_addr,
TDYa127920be7c2012-09-10 17:13:22 -0700721 art::Object::ClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700722 irb_.getJObjectTy(),
723 kTBAAConstJObject);
724
725 // Load vtable address
726 llvm::Value* vtable_addr =
727 irb_.LoadFromObjectOffset(class_object_addr,
TDYa127920be7c2012-09-10 17:13:22 -0700728 art::Class::VTableOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700729 irb_.getJObjectTy(),
730 kTBAAConstJObject);
731
732 // Load callee method object
733 llvm::Value* vtable_idx_value =
734 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
735
736 llvm::Value* method_field_addr =
737 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
738
739 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
740}
741
742// Emit Array GetElementPtr
743llvm::Value* GBCExpanderPass::EmitArrayGEP(llvm::Value* array_addr,
744 llvm::Value* index_value,
745 JType elem_jty) {
746
747 int data_offset;
748 if (elem_jty == kLong || elem_jty == kDouble ||
TDYa127920be7c2012-09-10 17:13:22 -0700749 (elem_jty == kObject && sizeof(uint64_t) == sizeof(art::Object*))) {
750 data_offset = art::Array::DataOffset(sizeof(int64_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700751 } else {
TDYa127920be7c2012-09-10 17:13:22 -0700752 data_offset = art::Array::DataOffset(sizeof(int32_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700753 }
754
755 llvm::Constant* data_offset_value =
756 irb_.getPtrEquivInt(data_offset);
757
758 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
759
760 llvm::Value* array_data_addr =
761 irb_.CreatePtrDisp(array_addr, data_offset_value,
762 elem_type->getPointerTo());
763
764 return irb_.CreateGEP(array_data_addr, index_value);
765}
766
767void GBCExpanderPass::Expand_TestSuspend(llvm::CallInst& call_inst) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700768 irb_.Runtime().EmitTestSuspend();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700769 return;
770}
771
TDYa1279a129452012-07-19 03:10:08 -0700772void GBCExpanderPass::Expand_MarkGCCard(llvm::CallInst& call_inst) {
TDYa1279a129452012-07-19 03:10:08 -0700773 irb_.Runtime().EmitMarkGCCard(call_inst.getArgOperand(0), call_inst.getArgOperand(1));
TDYa1279a129452012-07-19 03:10:08 -0700774 return;
775}
776
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700777llvm::Value*
778GBCExpanderPass::Expand_LoadStringFromDexCache(llvm::Value* string_idx_value) {
779 uint32_t string_idx =
780 llvm::cast<llvm::ConstantInt>(string_idx_value)->getZExtValue();
781
782 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
783
784 return irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
785}
786
787llvm::Value*
788GBCExpanderPass::Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value) {
789 uint32_t type_idx =
790 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
791
792 llvm::Value* type_field_addr =
793 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
794
795 return irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
796}
797
798void GBCExpanderPass::Expand_LockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700799 rtb_.EmitLockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700800 return;
801}
802
803void GBCExpanderPass::Expand_UnlockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700804 rtb_.EmitUnlockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700805 return;
806}
807
808llvm::Value* GBCExpanderPass::Expand_ArrayGet(llvm::Value* array_addr,
809 llvm::Value* index_value,
810 JType elem_jty) {
811 llvm::Value* array_elem_addr =
812 EmitArrayGEP(array_addr, index_value, elem_jty);
813
814 return irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
815}
816
817void GBCExpanderPass::Expand_ArrayPut(llvm::Value* new_value,
818 llvm::Value* array_addr,
819 llvm::Value* index_value,
820 JType elem_jty) {
821 llvm::Value* array_elem_addr =
822 EmitArrayGEP(array_addr, index_value, elem_jty);
823
824 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
825
826 return;
827}
828
829void GBCExpanderPass::Expand_FilledNewArray(llvm::CallInst& call_inst) {
830 // Most of the codes refer to MethodCompiler::EmitInsn_FilledNewArray
831 llvm::Value* array = call_inst.getArgOperand(0);
832
833 uint32_t element_jty =
834 llvm::cast<llvm::ConstantInt>(call_inst.getArgOperand(1))->getZExtValue();
835
836 DCHECK(call_inst.getNumArgOperands() > 2);
837 unsigned num_elements = (call_inst.getNumArgOperands() - 2);
838
839 bool is_elem_int_ty = (static_cast<JType>(element_jty) == kInt);
840
841 uint32_t alignment;
842 llvm::Constant* elem_size;
843 llvm::PointerType* field_type;
844
845 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
846 // as the element, thus we are only checking 2 cases: primitive int and
847 // non-primitive type.
848 if (is_elem_int_ty) {
849 alignment = sizeof(int32_t);
850 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
851 field_type = irb_.getJIntTy()->getPointerTo();
852 } else {
853 alignment = irb_.getSizeOfPtrEquivInt();
854 elem_size = irb_.getSizeOfPtrEquivIntValue();
855 field_type = irb_.getJObjectTy()->getPointerTo();
856 }
857
858 llvm::Value* data_field_offset =
TDYa127920be7c2012-09-10 17:13:22 -0700859 irb_.getPtrEquivInt(art::Array::DataOffset(alignment).Int32Value());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700860
861 llvm::Value* data_field_addr =
862 irb_.CreatePtrDisp(array, data_field_offset, field_type);
863
864 for (unsigned i = 0; i < num_elements; ++i) {
865 // Values to fill the array begin at the 3rd argument
866 llvm::Value* reg_value = call_inst.getArgOperand(2 + i);
867
868 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
869
870 data_field_addr =
871 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
872 }
873
874 return;
875}
876
877llvm::Value* GBCExpanderPass::Expand_IGetFast(llvm::Value* field_offset_value,
878 llvm::Value* /*is_volatile_value*/,
879 llvm::Value* object_addr,
880 JType field_jty) {
881 int field_offset =
882 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
883
884 DCHECK_GE(field_offset, 0);
885
886 llvm::PointerType* field_type =
887 irb_.getJType(field_jty, kField)->getPointerTo();
888
889 field_offset_value = irb_.getPtrEquivInt(field_offset);
890
891 llvm::Value* field_addr =
892 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
893
894 // TODO: Check is_volatile. We need to generate atomic load instruction
895 // when is_volatile is true.
896 return irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
897}
898
899void GBCExpanderPass::Expand_IPutFast(llvm::Value* field_offset_value,
900 llvm::Value* /* is_volatile_value */,
901 llvm::Value* object_addr,
902 llvm::Value* new_value,
903 JType field_jty) {
904 int field_offset =
905 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
906
907 DCHECK_GE(field_offset, 0);
908
909 llvm::PointerType* field_type =
910 irb_.getJType(field_jty, kField)->getPointerTo();
911
912 field_offset_value = irb_.getPtrEquivInt(field_offset);
913
914 llvm::Value* field_addr =
915 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
916
917 // TODO: Check is_volatile. We need to generate atomic store instruction
918 // when is_volatile is true.
919 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
920
921 return;
922}
923
924llvm::Value* GBCExpanderPass::Expand_SGetFast(llvm::Value* static_storage_addr,
925 llvm::Value* field_offset_value,
926 llvm::Value* /*is_volatile_value*/,
927 JType field_jty) {
928 int field_offset =
929 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
930
931 DCHECK_GE(field_offset, 0);
932
933 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
934
935 llvm::Value* static_field_addr =
936 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
937 irb_.getJType(field_jty, kField)->getPointerTo());
938
939 // TODO: Check is_volatile. We need to generate atomic store instruction
940 // when is_volatile is true.
941 return irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
942}
943
944void GBCExpanderPass::Expand_SPutFast(llvm::Value* static_storage_addr,
945 llvm::Value* field_offset_value,
946 llvm::Value* /* is_volatile_value */,
947 llvm::Value* new_value,
948 JType field_jty) {
949 int field_offset =
950 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
951
952 DCHECK_GE(field_offset, 0);
953
954 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
955
956 llvm::Value* static_field_addr =
957 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
958 irb_.getJType(field_jty, kField)->getPointerTo());
959
960 // TODO: Check is_volatile. We need to generate atomic store instruction
961 // when is_volatile is true.
962 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
963
964 return;
965}
966
967llvm::Value*
968GBCExpanderPass::Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr) {
969 return irb_.LoadFromObjectOffset(method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700970 art::AbstractMethod::DeclaringClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700971 irb_.getJObjectTy(),
972 kTBAAConstJObject);
973}
974
975llvm::Value*
976GBCExpanderPass::Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value) {
977 uint32_t type_idx =
978 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
979
980 llvm::Value* storage_field_addr =
981 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
982
983 return irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
984}
985
986llvm::Value*
987GBCExpanderPass::Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value) {
988 uint32_t callee_method_idx =
989 llvm::cast<llvm::ConstantInt>(callee_method_idx_value)->getZExtValue();
990
991 return EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
992}
993
994llvm::Value* GBCExpanderPass::Expand_GetVirtualCalleeMethodObjAddrFast(
995 llvm::Value* vtable_idx_value,
996 llvm::Value* this_addr) {
997 int vtable_idx =
998 llvm::cast<llvm::ConstantInt>(vtable_idx_value)->getSExtValue();
999
1000 return EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
1001}
1002
1003llvm::Value* GBCExpanderPass::Expand_Invoke(llvm::CallInst& call_inst) {
1004 // Most of the codes refer to MethodCompiler::EmitInsn_Invoke
1005 llvm::Value* callee_method_object_addr = call_inst.getArgOperand(0);
1006 unsigned num_args = call_inst.getNumArgOperands();
1007 llvm::Type* ret_type = call_inst.getType();
1008
1009 // Determine the function type of the callee method
1010 std::vector<llvm::Type*> args_type;
1011 std::vector<llvm::Value*> args;
1012 for (unsigned i = 0; i < num_args; i++) {
1013 args.push_back(call_inst.getArgOperand(i));
1014 args_type.push_back(args[i]->getType());
1015 }
1016
1017 llvm::FunctionType* callee_method_type =
1018 llvm::FunctionType::get(ret_type, args_type, false);
1019
1020 llvm::Value* code_addr =
1021 irb_.LoadFromObjectOffset(callee_method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001022 art::AbstractMethod::GetCodeOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001023 callee_method_type->getPointerTo(),
1024 kTBAAJRuntime);
1025
1026 // Invoke callee
1027 llvm::Value* retval = irb_.CreateCall(code_addr, args);
1028
1029 return retval;
1030}
1031
TDYa1274ec8ccd2012-08-11 07:04:57 -07001032llvm::Value* GBCExpanderPass::Expand_DivRem(llvm::CallInst& call_inst,
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001033 bool is_div, JType op_jty) {
TDYa1274ec8ccd2012-08-11 07:04:57 -07001034 llvm::Value* dividend = call_inst.getArgOperand(0);
1035 llvm::Value* divisor = call_inst.getArgOperand(1);
buzbeec531cef2012-10-18 07:09:20 -07001036#if defined(ART_USE_PORTABLE_COMPILER)
TDYa1274ec8ccd2012-08-11 07:04:57 -07001037 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1038 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
1039#endif
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001040 // Most of the codes refer to MethodCompiler::EmitIntDivRemResultComputation
1041
1042 // Check the special case: MININT / -1 = MININT
1043 // That case will cause overflow, which is undefined behavior in llvm.
1044 // So we check the divisor is -1 or not, if the divisor is -1, we do
1045 // the special path to avoid undefined behavior.
1046 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
1047 llvm::Value* zero = irb_.getJZero(op_jty);
1048 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
1049
TDYa1275e869b62012-07-25 00:45:39 -07001050 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001051 llvm::BasicBlock* eq_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1052 llvm::BasicBlock* ne_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1053 llvm::BasicBlock* neg_one_cont =
1054 llvm::BasicBlock::Create(context_, "", parent);
1055
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001056 llvm::Value* is_equal_neg_one = irb_.CreateICmpEQ(divisor, neg_one);
1057 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
1058
1059 // If divisor == -1
1060 irb_.SetInsertPoint(eq_neg_one);
1061 llvm::Value* eq_result;
1062 if (is_div) {
1063 // We can just change from "dividend div -1" to "neg dividend". The sub
1064 // don't care the sign/unsigned because of two's complement representation.
1065 // And the behavior is what we want:
1066 // -(2^n) (2^n)-1
1067 // MININT < k <= MAXINT -> mul k -1 = -k
1068 // MININT == k -> mul k -1 = k
1069 //
1070 // LLVM use sub to represent 'neg'
1071 eq_result = irb_.CreateSub(zero, dividend);
1072 } else {
1073 // Everything modulo -1 will be 0.
1074 eq_result = zero;
1075 }
1076 irb_.CreateBr(neg_one_cont);
1077
1078 // If divisor != -1, just do the division.
1079 irb_.SetInsertPoint(ne_neg_one);
1080 llvm::Value* ne_result;
1081 if (is_div) {
1082 ne_result = irb_.CreateSDiv(dividend, divisor);
1083 } else {
1084 ne_result = irb_.CreateSRem(dividend, divisor);
1085 }
1086 irb_.CreateBr(neg_one_cont);
1087
1088 irb_.SetInsertPoint(neg_one_cont);
1089 llvm::PHINode* result = irb_.CreatePHI(op_type, 2);
1090 result->addIncoming(eq_result, eq_neg_one);
1091 result->addIncoming(ne_result, ne_neg_one);
1092
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001093 return result;
1094}
1095
TDYa1278e950c12012-11-02 09:58:19 -07001096void GBCExpanderPass::Expand_AllocaShadowFrame(llvm::Value* num_entry_value,
1097 llvm::Value* num_vregs_value) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001098 // Most of the codes refer to MethodCompiler::EmitPrologueAllocShadowFrame and
1099 // MethodCompiler::EmitPushShadowFrame
TDYa1278e950c12012-11-02 09:58:19 -07001100 uint16_t num_shadow_frame_refs =
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001101 llvm::cast<llvm::ConstantInt>(num_entry_value)->getZExtValue();
TDYa1278e950c12012-11-02 09:58:19 -07001102 uint16_t num_vregs =
1103 llvm::cast<llvm::ConstantInt>(num_vregs_value)->getZExtValue();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001104
1105 llvm::StructType* shadow_frame_type =
TDYa1278e950c12012-11-02 09:58:19 -07001106 irb_.getShadowFrameTy(num_shadow_frame_refs, num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001107
1108 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
1109
1110 // Alloca a pointer to old shadow frame
1111 old_shadow_frame_ =
1112 irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
1113
1114 // Zero-initialization of the shadow frame table
1115 llvm::Value* shadow_frame_table =
1116 irb_.CreateConstGEP2_32(shadow_frame_, 0, 1);
1117 llvm::Type* table_type = shadow_frame_type->getElementType(1);
1118
1119 llvm::ConstantAggregateZero* zero_initializer =
1120 llvm::ConstantAggregateZero::get(table_type);
1121
1122 irb_.CreateStore(zero_initializer, shadow_frame_table, kTBAAShadowFrame);
1123
1124 // Push the shadow frame
1125 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1126
1127 // Push the shadow frame
1128 llvm::Value* shadow_frame_upcast =
1129 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
1130
1131 llvm::Value* result = rtb_.EmitPushShadowFrame(shadow_frame_upcast,
1132 method_object_addr,
TDYa1278e950c12012-11-02 09:58:19 -07001133 num_shadow_frame_refs,
1134 num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001135
1136 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
1137
1138 return;
1139}
1140
TDYa1278e950c12012-11-02 09:58:19 -07001141// TODO: We will remove ShadowFrameEntry later, so I just copy/paste from ShadowFrameEntry.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001142void GBCExpanderPass::Expand_SetShadowFrameEntry(llvm::Value* obj,
1143 llvm::Value* entry_idx) {
1144 DCHECK(shadow_frame_ != NULL);
1145
1146 llvm::Value* gep_index[] = {
1147 irb_.getInt32(0), // No pointer displacement
1148 irb_.getInt32(1), // SIRT
1149 entry_idx // Pointer field
1150 };
1151
1152 llvm::Value* entry_addr = irb_.CreateGEP(shadow_frame_, gep_index);
buzbeec531cef2012-10-18 07:09:20 -07001153#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127347166a2012-08-23 12:23:44 -07001154 if (obj->getType() != irb_.getJObjectTy()) {
1155 obj = irb_.getJNull();
1156 }
1157#endif
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001158 irb_.CreateStore(obj, entry_addr, kTBAAShadowFrame);
1159 return;
1160}
1161
TDYa1278e950c12012-11-02 09:58:19 -07001162void GBCExpanderPass::Expand_SetVReg(llvm::Value* entry_idx,
1163 llvm::Value* value) {
1164 DCHECK(shadow_frame_ != NULL);
1165
1166 llvm::Value* gep_index[] = {
1167 irb_.getInt32(0), // No pointer displacement
1168 irb_.getInt32(2), // VRegs
1169 entry_idx // Pointer field
1170 };
1171
1172 llvm::Value* vreg_addr = irb_.CreateGEP(shadow_frame_, gep_index);
1173
1174 irb_.CreateStore(value,
1175 irb_.CreateBitCast(vreg_addr, value->getType()->getPointerTo()),
1176 kTBAAShadowFrame);
1177 return;
1178}
1179
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001180void GBCExpanderPass::Expand_PopShadowFrame() {
buzbeec531cef2012-10-18 07:09:20 -07001181#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001182 if (old_shadow_frame_ == NULL) {
1183 return;
1184 }
1185#endif
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001186 rtb_.EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
1187 return;
1188}
1189
1190void GBCExpanderPass::Expand_UpdateDexPC(llvm::Value* dex_pc_value) {
1191 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07001192 art::ShadowFrame::DexPCOffset(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001193 dex_pc_value,
1194 kTBAAShadowFrame);
1195 return;
1196}
1197
Logan Chien67645d82012-08-17 09:10:54 +08001198void GBCExpanderPass::InsertStackOverflowCheck(llvm::Function& func) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001199 // DexLang generates all alloca instruction in the first basic block of the
1200 // FUNC and also there's no any alloca instructions after the first non-alloca
1201 // instruction
1202
Logan Chien67645d82012-08-17 09:10:54 +08001203 llvm::BasicBlock* first_basic_block = &func.front();
1204
1205 // Look for first non-alloca instruction
1206 llvm::BasicBlock::iterator first_non_alloca = first_basic_block->begin();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001207 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1208 ++first_non_alloca;
1209 }
1210
Logan Chien67645d82012-08-17 09:10:54 +08001211 irb_.SetInsertPoint(first_non_alloca);
1212
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001213 // Insert stack overflow check codes before first_non_alloca (i.e., after all
1214 // alloca instructions)
Logan Chien67645d82012-08-17 09:10:54 +08001215 EmitStackOverflowCheck(&*first_non_alloca);
1216
buzbeec531cef2012-10-18 07:09:20 -07001217#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127890ea892012-08-22 10:49:42 -07001218 irb_.Runtime().EmitTestSuspend();
1219#endif
1220
Logan Chien67645d82012-08-17 09:10:54 +08001221 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
1222 if (next_basic_block != first_basic_block) {
1223 // Splice the rest of the instruction to the continuing basic block
1224 next_basic_block->getInstList().splice(
1225 irb_.GetInsertPoint(), first_basic_block->getInstList(),
1226 first_non_alloca, first_basic_block->end());
1227
1228 // Rewrite the basic block
1229 RewriteBasicBlock(next_basic_block);
1230
1231 // Update the phi-instructions in the successor basic block
1232 UpdatePhiInstruction(first_basic_block, irb_.GetInsertBlock());
1233 }
1234
1235 // We have changed the basic block
1236 changed_ = true;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001237}
1238
TDYa1275e869b62012-07-25 00:45:39 -07001239// ==== High-level intrinsic expander ==========================================
1240
TDYa127a1b21852012-07-23 03:20:39 -07001241llvm::Value* GBCExpanderPass::Expand_FPCompare(llvm::Value* src1_value,
1242 llvm::Value* src2_value,
1243 bool gt_bias) {
1244 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1245 llvm::Value* cmp_lt;
1246
1247 if (gt_bias) {
1248 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1249 } else {
1250 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1251 }
1252
1253 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1254}
1255
1256llvm::Value* GBCExpanderPass::Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value) {
1257 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1258 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1259
1260 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1261}
1262
1263llvm::Value* GBCExpanderPass::EmitCompareResultSelection(llvm::Value* cmp_eq,
1264 llvm::Value* cmp_lt) {
1265
1266 llvm::Constant* zero = irb_.getJInt(0);
1267 llvm::Constant* pos1 = irb_.getJInt(1);
1268 llvm::Constant* neg1 = irb_.getJInt(-1);
1269
1270 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1271 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1272
1273 return result_eq;
1274}
1275
Logan Chien75e4b602012-07-23 14:24:12 -07001276llvm::Value* GBCExpanderPass::Expand_IntegerShift(llvm::Value* src1_value,
1277 llvm::Value* src2_value,
1278 IntegerShiftKind kind,
1279 JType op_jty) {
1280 DCHECK(op_jty == kInt || op_jty == kLong);
1281
1282 // Mask and zero-extend RHS properly
1283 if (op_jty == kInt) {
1284 src2_value = irb_.CreateAnd(src2_value, 0x1f);
1285 } else {
1286 llvm::Value* masked_src2_value = irb_.CreateAnd(src2_value, 0x3f);
1287 src2_value = irb_.CreateZExt(masked_src2_value, irb_.getJLongTy());
1288 }
1289
1290 // Create integer shift llvm instruction
1291 switch (kind) {
1292 case kIntegerSHL:
1293 return irb_.CreateShl(src1_value, src2_value);
1294
1295 case kIntegerSHR:
1296 return irb_.CreateAShr(src1_value, src2_value);
1297
1298 case kIntegerUSHR:
1299 return irb_.CreateLShr(src1_value, src2_value);
1300
1301 default:
1302 LOG(FATAL) << "Unknown integer shift kind: " << kind;
1303 return NULL;
1304 }
1305}
1306
TDYa1275a26d442012-07-26 18:58:38 -07001307llvm::Value* GBCExpanderPass::Expand_HLArrayGet(llvm::CallInst& call_inst,
1308 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001309 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1310 llvm::Value* array_addr = call_inst.getArgOperand(1);
1311 llvm::Value* index_value = call_inst.getArgOperand(2);
TDYa127920be7c2012-09-10 17:13:22 -07001312 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001313
TDYa127920be7c2012-09-10 17:13:22 -07001314 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1315 EmitGuard_NullPointerException(dex_pc, array_addr);
1316 }
1317 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
1318 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value);
1319 }
TDYa1275a26d442012-07-26 18:58:38 -07001320
1321 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1322
1323 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
1324
1325 switch (elem_jty) {
1326 case kVoid:
1327 break;
1328
1329 case kBoolean:
1330 case kChar:
1331 array_elem_value = irb_.CreateZExt(array_elem_value, irb_.getJType(elem_jty, kReg));
1332 break;
1333
1334 case kByte:
1335 case kShort:
1336 array_elem_value = irb_.CreateSExt(array_elem_value, irb_.getJType(elem_jty, kReg));
1337 break;
1338
1339 case kInt:
1340 case kLong:
1341 case kFloat:
1342 case kDouble:
1343 case kObject:
1344 break;
1345
1346 default:
1347 LOG(FATAL) << "Unknown java type: " << elem_jty;
1348 }
1349
1350 return array_elem_value;
1351}
1352
1353
1354void GBCExpanderPass::Expand_HLArrayPut(llvm::CallInst& call_inst,
1355 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001356 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1357 llvm::Value* new_value = call_inst.getArgOperand(1);
1358 llvm::Value* array_addr = call_inst.getArgOperand(2);
1359 llvm::Value* index_value = call_inst.getArgOperand(3);
TDYa127920be7c2012-09-10 17:13:22 -07001360 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001361
TDYa127920be7c2012-09-10 17:13:22 -07001362 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1363 EmitGuard_NullPointerException(dex_pc, array_addr);
1364 }
1365 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
1366 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value);
1367 }
TDYa1275a26d442012-07-26 18:58:38 -07001368
1369 switch (elem_jty) {
1370 case kVoid:
1371 break;
1372
1373 case kBoolean:
1374 case kChar:
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001375 case kByte:
1376 case kShort:
TDYa1275a26d442012-07-26 18:58:38 -07001377 new_value = irb_.CreateTrunc(new_value, irb_.getJType(elem_jty, kArray));
1378 break;
1379
1380 case kInt:
1381 case kLong:
1382 case kFloat:
1383 case kDouble:
1384 case kObject:
1385 break;
1386
1387 default:
1388 LOG(FATAL) << "Unknown java type: " << elem_jty;
1389 }
1390
1391 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1392
1393 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
1394 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::CheckPutArrayElement);
1395
1396 irb_.CreateCall2(runtime_func, new_value, array_addr);
1397
1398 EmitGuard_ExceptionLandingPad(dex_pc);
1399
1400 EmitMarkGCCard(new_value, array_addr);
1401 }
1402
1403 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1404
1405 return;
1406}
1407
TDYa1275e869b62012-07-25 00:45:39 -07001408llvm::Value* GBCExpanderPass::Expand_HLIGet(llvm::CallInst& call_inst,
1409 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001410 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1411 llvm::Value* object_addr = call_inst.getArgOperand(1);
1412 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(2));
TDYa127920be7c2012-09-10 17:13:22 -07001413 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001414
TDYa127920be7c2012-09-10 17:13:22 -07001415 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1416 EmitGuard_NullPointerException(dex_pc, object_addr);
1417 }
TDYa1275e869b62012-07-25 00:45:39 -07001418
1419 llvm::Value* field_value;
1420
1421 int field_offset;
1422 bool is_volatile;
1423 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
1424 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
1425
1426 if (!is_fast_path) {
1427 llvm::Function* runtime_func;
1428
1429 if (field_jty == kObject) {
1430 runtime_func = irb_.GetRuntime(runtime_support::GetObjectInstance);
1431 } else if (field_jty == kLong || field_jty == kDouble) {
1432 runtime_func = irb_.GetRuntime(runtime_support::Get64Instance);
1433 } else {
1434 runtime_func = irb_.GetRuntime(runtime_support::Get32Instance);
1435 }
1436
1437 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
1438
1439 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1440
1441 EmitUpdateDexPC(dex_pc);
1442
1443 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
1444 method_object_addr, object_addr);
1445
1446 EmitGuard_ExceptionLandingPad(dex_pc);
1447
1448 } else {
1449 DCHECK_GE(field_offset, 0);
1450
1451 llvm::PointerType* field_type =
1452 irb_.getJType(field_jty, kField)->getPointerTo();
1453
1454 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
1455
1456 llvm::Value* field_addr =
1457 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1458
1459 // TODO: Check is_volatile. We need to generate atomic load instruction
1460 // when is_volatile is true.
1461 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
1462 }
1463
1464 if (field_jty == kFloat || field_jty == kDouble) {
TDYa1275a26d442012-07-26 18:58:38 -07001465 field_value = irb_.CreateBitCast(field_value, irb_.getJType(field_jty, kAccurate));
TDYa1275e869b62012-07-25 00:45:39 -07001466 }
1467
1468 return field_value;
1469}
1470
1471void GBCExpanderPass::Expand_HLIPut(llvm::CallInst& call_inst,
1472 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001473 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001474 llvm::Value* new_value = call_inst.getArgOperand(1);
1475 llvm::Value* object_addr = call_inst.getArgOperand(2);
TDYa1275e869b62012-07-25 00:45:39 -07001476 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(3));
TDYa127920be7c2012-09-10 17:13:22 -07001477 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001478
1479 if (field_jty == kFloat || field_jty == kDouble) {
TDYa1275a26d442012-07-26 18:58:38 -07001480 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty, kField));
TDYa1275e869b62012-07-25 00:45:39 -07001481 }
1482
TDYa127920be7c2012-09-10 17:13:22 -07001483 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1484 EmitGuard_NullPointerException(dex_pc, object_addr);
1485 }
TDYa1275e869b62012-07-25 00:45:39 -07001486
1487 int field_offset;
1488 bool is_volatile;
1489 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
1490 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
1491
1492 if (!is_fast_path) {
1493 llvm::Function* runtime_func;
1494
1495 if (field_jty == kObject) {
1496 runtime_func = irb_.GetRuntime(runtime_support::SetObjectInstance);
1497 } else if (field_jty == kLong || field_jty == kDouble) {
1498 runtime_func = irb_.GetRuntime(runtime_support::Set64Instance);
1499 } else {
1500 runtime_func = irb_.GetRuntime(runtime_support::Set32Instance);
1501 }
1502
1503 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1504
1505 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1506
1507 EmitUpdateDexPC(dex_pc);
1508
1509 irb_.CreateCall4(runtime_func, field_idx_value,
1510 method_object_addr, object_addr, new_value);
1511
1512 EmitGuard_ExceptionLandingPad(dex_pc);
1513
1514 } else {
1515 DCHECK_GE(field_offset, 0);
1516
1517 llvm::PointerType* field_type =
1518 irb_.getJType(field_jty, kField)->getPointerTo();
1519
1520 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
1521
1522 llvm::Value* field_addr =
1523 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1524
1525 // TODO: Check is_volatile. We need to generate atomic store instruction
1526 // when is_volatile is true.
1527 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1528
1529 if (field_jty == kObject) { // If put an object, mark the GC card table.
1530 EmitMarkGCCard(new_value, object_addr);
1531 }
1532 }
1533
1534 return;
1535}
1536
TDYa127f71bf5a2012-07-29 20:09:52 -07001537llvm::Value* GBCExpanderPass::EmitLoadConstantClass(uint32_t dex_pc,
1538 uint32_t type_idx) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001539 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, *dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001540 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1541
1542 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1543
1544 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1545
1546 llvm::Function* runtime_func =
1547 irb_.GetRuntime(runtime_support::InitializeTypeAndVerifyAccess);
1548
1549 EmitUpdateDexPC(dex_pc);
1550
1551 llvm::Value* type_object_addr =
1552 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1553
1554 EmitGuard_ExceptionLandingPad(dex_pc);
1555
1556 return type_object_addr;
1557
1558 } else {
1559 // Try to load the class (type) object from the test cache.
1560 llvm::Value* type_field_addr =
1561 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1562
1563 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
1564
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001565 if (compiler_->CanAssumeTypeIsPresentInDexCache(*dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001566 return type_object_addr;
1567 }
1568
1569 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1570
1571 // Test whether class (type) object is in the dex cache or not
1572 llvm::Value* equal_null =
1573 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1574
1575 llvm::BasicBlock* block_cont =
1576 CreateBasicBlockWithDexPC(dex_pc, "cont");
1577
1578 llvm::BasicBlock* block_load_class =
1579 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1580
1581 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
1582
1583 // Failback routine to load the class object
1584 irb_.SetInsertPoint(block_load_class);
1585
1586 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeType);
1587
1588 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1589
1590 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1591
1592 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1593
1594 EmitUpdateDexPC(dex_pc);
1595
1596 llvm::Value* loaded_type_object_addr =
1597 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1598
1599 EmitGuard_ExceptionLandingPad(dex_pc);
1600
1601 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1602
1603 irb_.CreateBr(block_cont);
1604
1605 // Now the class object must be loaded
1606 irb_.SetInsertPoint(block_cont);
1607
1608 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1609
1610 phi->addIncoming(type_object_addr, block_original);
1611 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1612
1613 return phi;
1614 }
1615}
1616
TDYa1275a26d442012-07-26 18:58:38 -07001617llvm::Value* GBCExpanderPass::EmitLoadStaticStorage(uint32_t dex_pc,
1618 uint32_t type_idx) {
1619 llvm::BasicBlock* block_load_static =
1620 CreateBasicBlockWithDexPC(dex_pc, "load_static");
1621
1622 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1623
1624 // Load static storage from dex cache
1625 llvm::Value* storage_field_addr =
1626 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1627
1628 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
1629
1630 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1631
1632 // Test: Is the static storage of this class initialized?
1633 llvm::Value* equal_null =
1634 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
1635
1636 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
1637
1638 // Failback routine to load the class object
1639 irb_.SetInsertPoint(block_load_static);
1640
1641 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeStaticStorage);
1642
1643 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1644
1645 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1646
1647 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1648
1649 EmitUpdateDexPC(dex_pc);
1650
1651 llvm::Value* loaded_storage_object_addr =
1652 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1653
1654 EmitGuard_ExceptionLandingPad(dex_pc);
1655
1656 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
1657
1658 irb_.CreateBr(block_cont);
1659
1660 // Now the class object must be loaded
1661 irb_.SetInsertPoint(block_cont);
1662
1663 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1664
1665 phi->addIncoming(storage_object_addr, block_original);
1666 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
1667
1668 return phi;
1669}
1670
1671llvm::Value* GBCExpanderPass::Expand_HLSget(llvm::CallInst& call_inst,
1672 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001673 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1674 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1675
1676 int field_offset;
1677 int ssb_index;
1678 bool is_referrers_class;
1679 bool is_volatile;
1680
1681 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
1682 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1683 is_referrers_class, is_volatile, false);
1684
1685 llvm::Value* static_field_value;
1686
1687 if (!is_fast_path) {
1688 llvm::Function* runtime_func;
1689
1690 if (field_jty == kObject) {
1691 runtime_func = irb_.GetRuntime(runtime_support::GetObjectStatic);
1692 } else if (field_jty == kLong || field_jty == kDouble) {
1693 runtime_func = irb_.GetRuntime(runtime_support::Get64Static);
1694 } else {
1695 runtime_func = irb_.GetRuntime(runtime_support::Get32Static);
1696 }
1697
1698 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1699
1700 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1701
1702 EmitUpdateDexPC(dex_pc);
1703
1704 static_field_value =
1705 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
1706
1707 EmitGuard_ExceptionLandingPad(dex_pc);
1708
1709 } else {
1710 DCHECK_GE(field_offset, 0);
1711
1712 llvm::Value* static_storage_addr = NULL;
1713
1714 if (is_referrers_class) {
1715 // Fast path, static storage base is this method's class
1716 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1717
1718 static_storage_addr =
1719 irb_.LoadFromObjectOffset(method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001720 art::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001721 irb_.getJObjectTy(),
1722 kTBAAConstJObject);
1723 } else {
1724 // Medium path, static storage base in a different class which
1725 // requires checks that the other class is initialized
1726 DCHECK_GE(ssb_index, 0);
1727 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1728 }
1729
1730 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1731
1732 llvm::Value* static_field_addr =
1733 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1734 irb_.getJType(field_jty, kField)->getPointerTo());
1735
1736 // TODO: Check is_volatile. We need to generate atomic load instruction
1737 // when is_volatile is true.
1738 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
1739 }
1740
1741 if (field_jty == kFloat || field_jty == kDouble) {
1742 static_field_value =
1743 irb_.CreateBitCast(static_field_value, irb_.getJType(field_jty, kAccurate));
1744 }
1745
1746 return static_field_value;
1747}
1748
1749void GBCExpanderPass::Expand_HLSput(llvm::CallInst& call_inst,
1750 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001751 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1752 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1753 llvm::Value* new_value = call_inst.getArgOperand(1);
1754
1755 if (field_jty == kFloat || field_jty == kDouble) {
1756 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty, kField));
1757 }
1758
1759 int field_offset;
1760 int ssb_index;
1761 bool is_referrers_class;
1762 bool is_volatile;
1763
1764 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
1765 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1766 is_referrers_class, is_volatile, true);
1767
1768 if (!is_fast_path) {
1769 llvm::Function* runtime_func;
1770
1771 if (field_jty == kObject) {
1772 runtime_func = irb_.GetRuntime(runtime_support::SetObjectStatic);
1773 } else if (field_jty == kLong || field_jty == kDouble) {
1774 runtime_func = irb_.GetRuntime(runtime_support::Set64Static);
1775 } else {
1776 runtime_func = irb_.GetRuntime(runtime_support::Set32Static);
1777 }
1778
1779 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1780
1781 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1782
1783 EmitUpdateDexPC(dex_pc);
1784
1785 irb_.CreateCall3(runtime_func, field_idx_value,
1786 method_object_addr, new_value);
1787
1788 EmitGuard_ExceptionLandingPad(dex_pc);
1789
1790 } else {
1791 DCHECK_GE(field_offset, 0);
1792
1793 llvm::Value* static_storage_addr = NULL;
1794
1795 if (is_referrers_class) {
1796 // Fast path, static storage base is this method's class
1797 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1798
1799 static_storage_addr =
1800 irb_.LoadFromObjectOffset(method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -07001801 art::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001802 irb_.getJObjectTy(),
1803 kTBAAConstJObject);
1804 } else {
1805 // Medium path, static storage base in a different class which
1806 // requires checks that the other class is initialized
1807 DCHECK_GE(ssb_index, 0);
1808 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1809 }
1810
1811 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1812
1813 llvm::Value* static_field_addr =
1814 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1815 irb_.getJType(field_jty, kField)->getPointerTo());
1816
1817 // TODO: Check is_volatile. We need to generate atomic store instruction
1818 // when is_volatile is true.
1819 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1820
1821 if (field_jty == kObject) { // If put an object, mark the GC card table.
1822 EmitMarkGCCard(new_value, static_storage_addr);
1823 }
1824 }
1825
1826 return;
1827}
1828
TDYa127f71bf5a2012-07-29 20:09:52 -07001829llvm::Value* GBCExpanderPass::Expand_ConstString(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001830 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1831 uint32_t string_idx = LV2UInt(call_inst.getArgOperand(0));
1832
1833 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1834
1835 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
1836
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001837 if (!compiler_->CanAssumeStringIsPresentInDexCache(*dex_file_, string_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001838 llvm::BasicBlock* block_str_exist =
1839 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1840
1841 llvm::BasicBlock* block_str_resolve =
1842 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1843
1844 llvm::BasicBlock* block_cont =
1845 CreateBasicBlockWithDexPC(dex_pc, "str_cont");
1846
1847 // Test: Is the string resolved and in the dex cache?
1848 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1849
1850 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
1851
1852 // String is resolved, go to next basic block.
1853 irb_.SetInsertPoint(block_str_exist);
1854 irb_.CreateBr(block_cont);
1855
1856 // String is not resolved yet, resolve it now.
1857 irb_.SetInsertPoint(block_str_resolve);
1858
1859 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::ResolveString);
1860
1861 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1862
1863 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1864
1865 EmitUpdateDexPC(dex_pc);
1866
1867 llvm::Value* result = irb_.CreateCall2(runtime_func, method_object_addr,
1868 string_idx_value);
1869
1870 EmitGuard_ExceptionLandingPad(dex_pc);
1871
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001872 irb_.CreateBr(block_cont);
1873
1874
TDYa127f71bf5a2012-07-29 20:09:52 -07001875 llvm::BasicBlock* block_pre_cont = irb_.GetInsertBlock();
1876
1877 irb_.SetInsertPoint(block_cont);
1878
1879 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1880
1881 phi->addIncoming(string_addr, block_str_exist);
1882 phi->addIncoming(result, block_pre_cont);
1883
1884 string_addr = phi;
1885 }
1886
1887 return string_addr;
1888}
1889
1890llvm::Value* GBCExpanderPass::Expand_ConstClass(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001891 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1892 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1893
1894 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
1895
1896 return type_object_addr;
1897}
1898
1899void GBCExpanderPass::Expand_MonitorEnter(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001900 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1901 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07001902 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07001903
TDYa127920be7c2012-09-10 17:13:22 -07001904 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1905 EmitGuard_NullPointerException(dex_pc, object_addr);
1906 }
TDYa127f71bf5a2012-07-29 20:09:52 -07001907
1908 irb_.Runtime().EmitLockObject(object_addr);
1909
1910 return;
1911}
1912
1913void GBCExpanderPass::Expand_MonitorExit(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001914 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1915 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07001916 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07001917
TDYa127920be7c2012-09-10 17:13:22 -07001918 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1919 EmitGuard_NullPointerException(dex_pc, object_addr);
1920 }
TDYa127f71bf5a2012-07-29 20:09:52 -07001921
1922 EmitUpdateDexPC(dex_pc);
1923
1924 irb_.Runtime().EmitUnlockObject(object_addr);
1925
1926 EmitGuard_ExceptionLandingPad(dex_pc);
1927
1928 return;
1929}
1930
1931void GBCExpanderPass::Expand_HLCheckCast(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001932 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1933 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1934 llvm::Value* object_addr = call_inst.getArgOperand(1);
1935
1936 llvm::BasicBlock* block_test_class =
1937 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1938
1939 llvm::BasicBlock* block_test_sub_class =
1940 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1941
1942 llvm::BasicBlock* block_cont =
1943 CreateBasicBlockWithDexPC(dex_pc, "checkcast_cont");
1944
1945 // Test: Is the reference equal to null? Act as no-op when it is null.
1946 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1947
1948 irb_.CreateCondBr(equal_null,
1949 block_cont,
1950 block_test_class);
1951
1952 // Test: Is the object instantiated from the given class?
1953 irb_.SetInsertPoint(block_test_class);
1954 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
TDYa127920be7c2012-09-10 17:13:22 -07001955 DCHECK_EQ(art::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07001956
1957 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1958
1959 llvm::Value* object_type_field_addr =
1960 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1961
1962 llvm::Value* object_type_object_addr =
1963 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
1964
1965 llvm::Value* equal_class =
1966 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1967
1968 irb_.CreateCondBr(equal_class,
1969 block_cont,
1970 block_test_sub_class);
1971
1972 // Test: Is the object instantiated from the subclass of the given class?
1973 irb_.SetInsertPoint(block_test_sub_class);
1974
1975 EmitUpdateDexPC(dex_pc);
1976
1977 irb_.CreateCall2(irb_.GetRuntime(runtime_support::CheckCast),
1978 type_object_addr, object_type_object_addr);
1979
1980 EmitGuard_ExceptionLandingPad(dex_pc);
1981
1982 irb_.CreateBr(block_cont);
1983
1984 irb_.SetInsertPoint(block_cont);
1985
1986 return;
1987}
1988
1989llvm::Value* GBCExpanderPass::Expand_InstanceOf(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001990 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1991 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1992 llvm::Value* object_addr = call_inst.getArgOperand(1);
1993
1994 llvm::BasicBlock* block_nullp =
1995 CreateBasicBlockWithDexPC(dex_pc, "nullp");
1996
1997 llvm::BasicBlock* block_test_class =
1998 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1999
2000 llvm::BasicBlock* block_class_equals =
2001 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
2002
2003 llvm::BasicBlock* block_test_sub_class =
2004 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
2005
2006 llvm::BasicBlock* block_cont =
2007 CreateBasicBlockWithDexPC(dex_pc, "instance_of_cont");
2008
2009 // Overview of the following code :
2010 // We check for null, if so, then false, otherwise check for class == . If so
2011 // then true, otherwise do callout slowpath.
2012 //
2013 // Test: Is the reference equal to null? Set 0 when it is null.
2014 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2015
2016 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
2017
2018 irb_.SetInsertPoint(block_nullp);
2019 irb_.CreateBr(block_cont);
2020
2021 // Test: Is the object instantiated from the given class?
2022 irb_.SetInsertPoint(block_test_class);
2023 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
TDYa127920be7c2012-09-10 17:13:22 -07002024 DCHECK_EQ(art::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002025
2026 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2027
2028 llvm::Value* object_type_field_addr =
2029 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2030
2031 llvm::Value* object_type_object_addr =
2032 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2033
2034 llvm::Value* equal_class =
2035 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2036
2037 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
2038
2039 irb_.SetInsertPoint(block_class_equals);
2040 irb_.CreateBr(block_cont);
2041
2042 // Test: Is the object instantiated from the subclass of the given class?
2043 irb_.SetInsertPoint(block_test_sub_class);
2044 llvm::Value* result =
2045 irb_.CreateCall2(irb_.GetRuntime(runtime_support::IsAssignable),
2046 type_object_addr, object_type_object_addr);
2047 irb_.CreateBr(block_cont);
2048
2049 irb_.SetInsertPoint(block_cont);
2050
2051 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJIntTy(), 3);
2052
2053 phi->addIncoming(irb_.getJInt(0), block_nullp);
2054 phi->addIncoming(irb_.getJInt(1), block_class_equals);
2055 phi->addIncoming(result, block_test_sub_class);
2056
2057 return phi;
2058}
2059
2060llvm::Value* GBCExpanderPass::Expand_NewInstance(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002061 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2062 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2063
2064 llvm::Function* runtime_func;
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002065 if (compiler_->CanAccessInstantiableTypeWithoutChecks(method_idx_, *dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002066 runtime_func = irb_.GetRuntime(runtime_support::AllocObject);
2067 } else {
2068 runtime_func = irb_.GetRuntime(runtime_support::AllocObjectWithAccessCheck);
2069 }
2070
2071 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2072
2073 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2074
2075 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2076
2077 EmitUpdateDexPC(dex_pc);
2078
2079 llvm::Value* object_addr =
2080 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
2081
2082 EmitGuard_ExceptionLandingPad(dex_pc);
2083
2084 return object_addr;
2085}
2086
2087llvm::Value* GBCExpanderPass::Expand_HLInvoke(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002088 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
TDYa127920be7c2012-09-10 17:13:22 -07002089 art::InvokeType invoke_type = static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
2090 bool is_static = (invoke_type == art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002091 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
TDYa127920be7c2012-09-10 17:13:22 -07002092 int opt_flags = LV2UInt(call_inst.getArgOperand(2));
TDYa127f71bf5a2012-07-29 20:09:52 -07002093
2094 // Compute invoke related information for compiler decision
2095 int vtable_idx = -1;
2096 uintptr_t direct_code = 0;
2097 uintptr_t direct_method = 0;
2098 bool is_fast_path = compiler_->
2099 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
2100 invoke_type, vtable_idx, direct_code, direct_method);
2101
2102 // Load *this* actual parameter
2103 llvm::Value* this_addr = NULL;
2104
2105 if (!is_static) {
2106 // Test: Is *this* parameter equal to null?
2107 this_addr = call_inst.getArgOperand(3);
2108 }
2109
2110 // Load the method object
2111 llvm::Value* callee_method_object_addr = NULL;
2112
2113 if (!is_fast_path) {
2114 callee_method_object_addr =
2115 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2116 this_addr, dex_pc, is_fast_path);
2117
TDYa127920be7c2012-09-10 17:13:22 -07002118 if (!is_static && !(opt_flags & MIR_IGNORE_NULL_CHECK)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002119 EmitGuard_NullPointerException(dex_pc, this_addr);
2120 }
2121 } else {
TDYa127920be7c2012-09-10 17:13:22 -07002122 if (!is_static && !(opt_flags & MIR_IGNORE_NULL_CHECK)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002123 EmitGuard_NullPointerException(dex_pc, this_addr);
2124 }
2125
2126 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002127 case art::kStatic:
2128 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002129 if (direct_method != 0u &&
2130 direct_method != static_cast<uintptr_t>(-1)) {
2131 callee_method_object_addr =
2132 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2133 irb_.getJObjectTy());
2134 } else {
2135 callee_method_object_addr =
2136 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2137 }
2138 break;
2139
TDYa127920be7c2012-09-10 17:13:22 -07002140 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002141 DCHECK(vtable_idx != -1);
2142 callee_method_object_addr =
2143 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2144 break;
2145
TDYa127920be7c2012-09-10 17:13:22 -07002146 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002147 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2148 "the fast path.";
2149 break;
2150
TDYa127920be7c2012-09-10 17:13:22 -07002151 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002152 callee_method_object_addr =
2153 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2154 invoke_type, this_addr,
2155 dex_pc, is_fast_path);
2156 break;
2157 }
2158 }
2159
2160 // Load the actual parameter
2161 std::vector<llvm::Value*> args;
2162
2163 args.push_back(callee_method_object_addr); // method object for callee
2164
2165 for (uint32_t i = 3; i < call_inst.getNumArgOperands(); ++i) {
2166 args.push_back(call_inst.getArgOperand(i));
2167 }
2168
2169 llvm::Value* code_addr;
2170 if (direct_code != 0u &&
2171 direct_code != static_cast<uintptr_t>(-1)) {
2172 code_addr =
2173 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_code),
2174 GetFunctionType(callee_method_idx, is_static)->getPointerTo());
2175 } else {
2176 code_addr =
2177 irb_.LoadFromObjectOffset(callee_method_object_addr,
Mathieu Chartier66f19252012-09-18 08:57:04 -07002178 art::AbstractMethod::GetCodeOffset().Int32Value(),
TDYa127f71bf5a2012-07-29 20:09:52 -07002179 GetFunctionType(callee_method_idx, is_static)->getPointerTo(),
2180 kTBAAJRuntime);
2181 }
2182
2183 // Invoke callee
2184 EmitUpdateDexPC(dex_pc);
2185 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2186 EmitGuard_ExceptionLandingPad(dex_pc);
2187
2188 return retval;
2189}
2190
2191llvm::Value* GBCExpanderPass::Expand_OptArrayLength(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002192 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2193 // Get the array object address
2194 llvm::Value* array_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002195 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002196
TDYa127920be7c2012-09-10 17:13:22 -07002197 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
2198 EmitGuard_NullPointerException(dex_pc, array_addr);
2199 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002200
2201 // Get the array length and store it to the register
2202 return EmitLoadArrayLength(array_addr);
2203}
2204
2205llvm::Value* GBCExpanderPass::Expand_NewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002206 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2207 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2208 llvm::Value* length = call_inst.getArgOperand(1);
2209
2210 return EmitAllocNewArray(dex_pc, length, type_idx, false);
2211}
2212
2213llvm::Value* GBCExpanderPass::Expand_HLFilledNewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002214 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2215 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(1));
2216 uint32_t length = call_inst.getNumArgOperands() - 3;
2217
2218 llvm::Value* object_addr =
2219 EmitAllocNewArray(dex_pc, irb_.getInt32(length), type_idx, true);
2220
2221 if (length > 0) {
2222 // Check for the element type
2223 uint32_t type_desc_len = 0;
2224 const char* type_desc =
2225 dex_file_->StringByTypeIdx(type_idx, &type_desc_len);
2226
2227 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
2228 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
2229 bool is_elem_int_ty = (type_desc[1] == 'I');
2230
2231 uint32_t alignment;
2232 llvm::Constant* elem_size;
2233 llvm::PointerType* field_type;
2234
2235 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
2236 // as the element, thus we are only checking 2 cases: primitive int and
2237 // non-primitive type.
2238 if (is_elem_int_ty) {
2239 alignment = sizeof(int32_t);
2240 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
2241 field_type = irb_.getJIntTy()->getPointerTo();
2242 } else {
2243 alignment = irb_.getSizeOfPtrEquivInt();
2244 elem_size = irb_.getSizeOfPtrEquivIntValue();
2245 field_type = irb_.getJObjectTy()->getPointerTo();
2246 }
2247
2248 llvm::Value* data_field_offset =
TDYa127920be7c2012-09-10 17:13:22 -07002249 irb_.getPtrEquivInt(art::Array::DataOffset(alignment).Int32Value());
TDYa127f71bf5a2012-07-29 20:09:52 -07002250
2251 llvm::Value* data_field_addr =
2252 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
2253
2254 // TODO: Tune this code. Currently we are generating one instruction for
2255 // one element which may be very space consuming. Maybe changing to use
2256 // memcpy may help; however, since we can't guarantee that the alloca of
2257 // dalvik register are continuous, we can't perform such optimization yet.
2258 for (uint32_t i = 0; i < length; ++i) {
2259 llvm::Value* reg_value = call_inst.getArgOperand(i+3);
2260
2261 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
2262
2263 data_field_addr =
2264 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
2265 }
2266 }
2267
2268 return object_addr;
2269}
2270
2271void GBCExpanderPass::Expand_HLFillArrayData(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002272 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2273 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
2274 LV2SInt(call_inst.getArgOperand(0));
2275 llvm::Value* array_addr = call_inst.getArgOperand(1);
2276
TDYa127920be7c2012-09-10 17:13:22 -07002277 const art::Instruction::ArrayDataPayload* payload =
2278 reinterpret_cast<const art::Instruction::ArrayDataPayload*>(
TDYa127f71bf5a2012-07-29 20:09:52 -07002279 code_item_->insns_ + payload_offset);
2280
2281 if (payload->element_count == 0) {
2282 // When the number of the elements in the payload is zero, we don't have
2283 // to copy any numbers. However, we should check whether the array object
2284 // address is equal to null or not.
2285 EmitGuard_NullPointerException(dex_pc, array_addr);
2286 } else {
2287 // To save the code size, we are going to call the runtime function to
2288 // copy the content from DexFile.
2289
2290 // NOTE: We will check for the NullPointerException in the runtime.
2291
2292 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::FillArrayData);
2293
2294 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2295
2296 EmitUpdateDexPC(dex_pc);
2297
2298 irb_.CreateCall4(runtime_func,
2299 method_object_addr, irb_.getInt32(dex_pc),
2300 array_addr, irb_.getInt32(payload_offset));
2301
2302 EmitGuard_ExceptionLandingPad(dex_pc);
2303 }
2304
2305 return;
2306}
2307
2308llvm::Value* GBCExpanderPass::EmitAllocNewArray(uint32_t dex_pc,
2309 llvm::Value* array_length_value,
2310 uint32_t type_idx,
2311 bool is_filled_new_array) {
2312 llvm::Function* runtime_func;
2313
2314 bool skip_access_check =
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002315 compiler_->CanAccessTypeWithoutChecks(method_idx_, *dex_file_, type_idx);
TDYa127f71bf5a2012-07-29 20:09:52 -07002316
2317
2318 if (is_filled_new_array) {
2319 runtime_func = skip_access_check ?
2320 irb_.GetRuntime(runtime_support::CheckAndAllocArray) :
2321 irb_.GetRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck);
2322 } else {
2323 runtime_func = skip_access_check ?
2324 irb_.GetRuntime(runtime_support::AllocArray) :
2325 irb_.GetRuntime(runtime_support::AllocArrayWithAccessCheck);
2326 }
2327
2328 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2329
2330 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2331
2332 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2333
2334 EmitUpdateDexPC(dex_pc);
2335
2336 llvm::Value* object_addr =
2337 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
2338 array_length_value, thread_object_addr);
2339
2340 EmitGuard_ExceptionLandingPad(dex_pc);
2341
2342 return object_addr;
2343}
2344
2345llvm::Value* GBCExpanderPass::
2346EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -07002347 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -07002348 llvm::Value* this_addr,
2349 uint32_t dex_pc,
2350 bool is_fast_path) {
2351
2352 llvm::Function* runtime_func = NULL;
2353
2354 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002355 case art::kStatic:
TDYa127f71bf5a2012-07-29 20:09:52 -07002356 runtime_func = irb_.GetRuntime(runtime_support::FindStaticMethodWithAccessCheck);
2357 break;
2358
TDYa127920be7c2012-09-10 17:13:22 -07002359 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002360 runtime_func = irb_.GetRuntime(runtime_support::FindDirectMethodWithAccessCheck);
2361 break;
2362
TDYa127920be7c2012-09-10 17:13:22 -07002363 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002364 runtime_func = irb_.GetRuntime(runtime_support::FindVirtualMethodWithAccessCheck);
2365 break;
2366
TDYa127920be7c2012-09-10 17:13:22 -07002367 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002368 runtime_func = irb_.GetRuntime(runtime_support::FindSuperMethodWithAccessCheck);
2369 break;
2370
TDYa127920be7c2012-09-10 17:13:22 -07002371 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002372 if (is_fast_path) {
2373 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethod);
2374 } else {
2375 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethodWithAccessCheck);
2376 }
2377 break;
2378 }
2379
2380 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2381
2382 if (this_addr == NULL) {
TDYa127920be7c2012-09-10 17:13:22 -07002383 DCHECK_EQ(invoke_type, art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002384 this_addr = irb_.getJNull();
2385 }
2386
2387 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2388
2389 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2390
2391 EmitUpdateDexPC(dex_pc);
2392
2393 llvm::Value* callee_method_object_addr =
2394 irb_.CreateCall4(runtime_func,
2395 callee_method_idx_value,
2396 this_addr,
2397 caller_method_object_addr,
2398 thread_object_addr);
2399
2400 EmitGuard_ExceptionLandingPad(dex_pc);
2401
2402 return callee_method_object_addr;
2403}
2404
TDYa1275e869b62012-07-25 00:45:39 -07002405void GBCExpanderPass::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2406 // Using runtime support, let the target can override by InlineAssembly.
2407 irb_.Runtime().EmitMarkGCCard(value, target_addr);
2408}
2409
2410void GBCExpanderPass::EmitUpdateDexPC(uint32_t dex_pc) {
buzbeec531cef2012-10-18 07:09:20 -07002411#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002412 if (shadow_frame_ == NULL) {
2413 return;
2414 }
2415#endif
TDYa1275e869b62012-07-25 00:45:39 -07002416 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07002417 art::ShadowFrame::DexPCOffset(),
TDYa1275e869b62012-07-25 00:45:39 -07002418 irb_.getInt32(dex_pc),
2419 kTBAAShadowFrame);
2420}
2421
2422void GBCExpanderPass::EmitGuard_DivZeroException(uint32_t dex_pc,
2423 llvm::Value* denominator,
2424 JType op_jty) {
2425 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2426
2427 llvm::Constant* zero = irb_.getJZero(op_jty);
2428
2429 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2430
2431 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2432
2433 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2434
2435 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
2436
2437 irb_.SetInsertPoint(block_exception);
2438 EmitUpdateDexPC(dex_pc);
2439 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowDivZeroException));
2440 EmitBranchExceptionLandingPad(dex_pc);
2441
2442 irb_.SetInsertPoint(block_continue);
2443}
2444
2445void GBCExpanderPass::EmitGuard_NullPointerException(uint32_t dex_pc,
2446 llvm::Value* object) {
2447 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
2448
2449 llvm::BasicBlock* block_exception =
2450 CreateBasicBlockWithDexPC(dex_pc, "nullp");
2451
2452 llvm::BasicBlock* block_continue =
2453 CreateBasicBlockWithDexPC(dex_pc, "cont");
2454
2455 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
2456
2457 irb_.SetInsertPoint(block_exception);
2458 EmitUpdateDexPC(dex_pc);
2459 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowNullPointerException),
2460 irb_.getInt32(dex_pc));
2461 EmitBranchExceptionLandingPad(dex_pc);
2462
2463 irb_.SetInsertPoint(block_continue);
2464}
2465
2466void
2467GBCExpanderPass::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2468 llvm::Value* array,
2469 llvm::Value* index) {
2470 llvm::Value* array_len = EmitLoadArrayLength(array);
2471
2472 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2473
2474 llvm::BasicBlock* block_exception =
2475 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2476
2477 llvm::BasicBlock* block_continue =
2478 CreateBasicBlockWithDexPC(dex_pc, "cont");
2479
2480 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
2481
2482 irb_.SetInsertPoint(block_exception);
2483
2484 EmitUpdateDexPC(dex_pc);
2485 irb_.CreateCall2(irb_.GetRuntime(runtime_support::ThrowIndexOutOfBounds), index, array_len);
2486 EmitBranchExceptionLandingPad(dex_pc);
2487
2488 irb_.SetInsertPoint(block_continue);
2489}
2490
TDYa1275e869b62012-07-25 00:45:39 -07002491llvm::FunctionType* GBCExpanderPass::GetFunctionType(uint32_t method_idx,
2492 bool is_static) {
2493 // Get method signature
TDYa127920be7c2012-09-10 17:13:22 -07002494 art::DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
TDYa1275e869b62012-07-25 00:45:39 -07002495
2496 uint32_t shorty_size;
2497 const char* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
2498 CHECK_GE(shorty_size, 1u);
2499
2500 // Get return type
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002501
2502 char ret_shorty = shorty[0];
buzbeec531cef2012-10-18 07:09:20 -07002503#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaob2596522012-09-14 16:36:11 -07002504 ret_shorty = art::remapShorty(ret_shorty);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002505#endif
2506 llvm::Type* ret_type = irb_.getJType(ret_shorty, kAccurate);
TDYa1275e869b62012-07-25 00:45:39 -07002507
2508 // Get argument type
2509 std::vector<llvm::Type*> args_type;
2510
2511 args_type.push_back(irb_.getJObjectTy()); // method object pointer
2512
2513 if (!is_static) {
2514 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
2515 }
2516
2517 for (uint32_t i = 1; i < shorty_size; ++i) {
buzbeec531cef2012-10-18 07:09:20 -07002518#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaob2596522012-09-14 16:36:11 -07002519 char shorty_type = art::remapShorty(shorty[i]);
TDYa127f71bf5a2012-07-29 20:09:52 -07002520 args_type.push_back(irb_.getJType(shorty_type, kAccurate));
2521#else
TDYa1275e869b62012-07-25 00:45:39 -07002522 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
TDYa127f71bf5a2012-07-29 20:09:52 -07002523#endif
TDYa1275e869b62012-07-25 00:45:39 -07002524 }
2525
2526 return llvm::FunctionType::get(ret_type, args_type, false);
2527}
2528
2529
2530llvm::BasicBlock* GBCExpanderPass::
2531CreateBasicBlockWithDexPC(uint32_t dex_pc, const char* postfix) {
2532 std::string name;
2533
2534#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002535 art::StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
TDYa1275e869b62012-07-25 00:45:39 -07002536#endif
2537
2538 return llvm::BasicBlock::Create(context_, name, func_);
2539}
2540
2541llvm::BasicBlock* GBCExpanderPass::GetBasicBlock(uint32_t dex_pc) {
2542 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002543 CHECK(basic_blocks_[dex_pc] != NULL);
TDYa1275e869b62012-07-25 00:45:39 -07002544 return basic_blocks_[dex_pc];
2545}
2546
2547int32_t GBCExpanderPass::GetTryItemOffset(uint32_t dex_pc) {
2548 int32_t min = 0;
2549 int32_t max = code_item_->tries_size_ - 1;
2550
2551 while (min <= max) {
2552 int32_t mid = min + (max - min) / 2;
2553
TDYa127920be7c2012-09-10 17:13:22 -07002554 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*code_item_, mid);
TDYa1275e869b62012-07-25 00:45:39 -07002555 uint32_t start = ti->start_addr_;
2556 uint32_t end = start + ti->insn_count_;
2557
2558 if (dex_pc < start) {
2559 max = mid - 1;
2560 } else if (dex_pc >= end) {
2561 min = mid + 1;
2562 } else {
2563 return mid; // found
2564 }
2565 }
2566
2567 return -1; // not found
2568}
2569
2570llvm::BasicBlock* GBCExpanderPass::GetLandingPadBasicBlock(uint32_t dex_pc) {
2571 // Find the try item for this address in this method
2572 int32_t ti_offset = GetTryItemOffset(dex_pc);
2573
2574 if (ti_offset == -1) {
2575 return NULL; // No landing pad is available for this address.
2576 }
2577
2578 // Check for the existing landing pad basic block
2579 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2580 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2581
2582 if (block_lpad) {
2583 // We have generated landing pad for this try item already. Return the
2584 // same basic block.
2585 return block_lpad;
2586 }
2587
2588 // Get try item from code item
TDYa127920be7c2012-09-10 17:13:22 -07002589 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*code_item_, ti_offset);
TDYa1275e869b62012-07-25 00:45:39 -07002590
2591 std::string lpadname;
2592
2593#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002594 art::StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
TDYa1275e869b62012-07-25 00:45:39 -07002595#endif
2596
2597 // Create landing pad basic block
2598 block_lpad = llvm::BasicBlock::Create(context_, lpadname, func_);
2599
2600 // Change IRBuilder insert point
2601 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2602 irb_.SetInsertPoint(block_lpad);
2603
2604 // Find catch block with matching type
2605 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2606
2607 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
2608
2609 llvm::Value* catch_handler_index_value =
2610 irb_.CreateCall2(irb_.GetRuntime(runtime_support::FindCatchBlock),
2611 method_object_addr, ti_offset_value);
2612
2613 // Switch instruction (Go to unwind basic block by default)
2614 llvm::SwitchInst* sw =
2615 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2616
2617 // Cases with matched catch block
TDYa127920be7c2012-09-10 17:13:22 -07002618 art::CatchHandlerIterator iter(*code_item_, ti->start_addr_);
TDYa1275e869b62012-07-25 00:45:39 -07002619
2620 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2621 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2622 }
2623
2624 // Restore the orignal insert point for IRBuilder
2625 irb_.restoreIP(irb_ip_original);
2626
2627 // Cache this landing pad
2628 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2629 basic_block_landing_pads_[ti_offset] = block_lpad;
2630
2631 return block_lpad;
2632}
2633
2634llvm::BasicBlock* GBCExpanderPass::GetUnwindBasicBlock() {
2635 // Check the existing unwinding baisc block block
2636 if (basic_block_unwind_ != NULL) {
2637 return basic_block_unwind_;
2638 }
2639
2640 // Create new basic block for unwinding
2641 basic_block_unwind_ =
2642 llvm::BasicBlock::Create(context_, "exception_unwind", func_);
2643
2644 // Change IRBuilder insert point
2645 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2646 irb_.SetInsertPoint(basic_block_unwind_);
2647
2648 // Pop the shadow frame
2649 Expand_PopShadowFrame();
2650
2651 // Emit the code to return default value (zero) for the given return type.
2652 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
buzbeec531cef2012-10-18 07:09:20 -07002653#if defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaob2596522012-09-14 16:36:11 -07002654 ret_shorty = art::remapShorty(ret_shorty);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002655#endif
TDYa1275e869b62012-07-25 00:45:39 -07002656 if (ret_shorty == 'V') {
2657 irb_.CreateRetVoid();
2658 } else {
2659 irb_.CreateRet(irb_.getJZero(ret_shorty));
2660 }
2661
2662 // Restore the orignal insert point for IRBuilder
2663 irb_.restoreIP(irb_ip_original);
2664
2665 return basic_block_unwind_;
2666}
2667
2668void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2669 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002670 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002671 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002672 irb_.CreateBr(lpad);
2673 } else {
2674 irb_.CreateBr(GetUnwindBasicBlock());
2675 }
2676}
2677
2678void GBCExpanderPass::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
2679 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
2680
2681 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2682
2683 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002684 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002685 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002686 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
2687 } else {
2688 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
2689 }
2690
2691 irb_.SetInsertPoint(block_cont);
2692}
2693
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002694llvm::Value*
2695GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
2696 llvm::CallInst& call_inst) {
2697 switch (intr_id) {
2698 //==- Thread -----------------------------------------------------------==//
2699 case IntrinsicHelper::GetCurrentThread: {
TDYa127b672d1e2012-06-28 21:21:45 -07002700 return irb_.Runtime().EmitGetCurrentThread();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002701 }
Logan Chien75e4b602012-07-23 14:24:12 -07002702 case IntrinsicHelper::CheckSuspend: {
TDYa127890ea892012-08-22 10:49:42 -07002703 // We will add suspend by ourselves.
2704 return NULL;
2705 }
2706 case IntrinsicHelper::TestSuspend: {
Logan Chiend54a23d2012-07-24 11:19:23 -07002707 Expand_TestSuspend(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002708 return NULL;
2709 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002710 case IntrinsicHelper::MarkGCCard: {
TDYa1279a129452012-07-19 03:10:08 -07002711 Expand_MarkGCCard(call_inst);
2712 return NULL;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002713 }
Logan Chien75e4b602012-07-23 14:24:12 -07002714
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002715 //==- Exception --------------------------------------------------------==//
2716 case IntrinsicHelper::ThrowException: {
2717 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
2718 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002719 case IntrinsicHelper::HLThrowException: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002720 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2721
2722 EmitUpdateDexPC(dex_pc);
2723
2724 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowException),
2725 call_inst.getArgOperand(0));
2726
2727 EmitGuard_ExceptionLandingPad(dex_pc);
2728 return NULL;
2729 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002730 case IntrinsicHelper::GetException: {
TDYa127823433d2012-09-26 16:03:51 -07002731 return irb_.Runtime().EmitGetAndClearException();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002732 }
2733 case IntrinsicHelper::IsExceptionPending: {
2734 return irb_.Runtime().EmitIsExceptionPending();
2735 }
2736 case IntrinsicHelper::FindCatchBlock: {
2737 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
2738 }
2739 case IntrinsicHelper::ThrowDivZeroException: {
2740 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
2741 }
2742 case IntrinsicHelper::ThrowNullPointerException: {
2743 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
2744 }
2745 case IntrinsicHelper::ThrowIndexOutOfBounds: {
2746 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
2747 }
Logan Chien75e4b602012-07-23 14:24:12 -07002748
2749 //==- Const String -----------------------------------------------------==//
2750 case IntrinsicHelper::ConstString: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002751 return Expand_ConstString(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002752 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002753 case IntrinsicHelper::LoadStringFromDexCache: {
2754 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
2755 }
2756 case IntrinsicHelper::ResolveString: {
2757 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
2758 }
Logan Chien75e4b602012-07-23 14:24:12 -07002759
2760 //==- Const Class ------------------------------------------------------==//
2761 case IntrinsicHelper::ConstClass: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002762 return Expand_ConstClass(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002763 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002764 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
2765 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
2766 }
2767 case IntrinsicHelper::LoadTypeFromDexCache: {
2768 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
2769 }
2770 case IntrinsicHelper::InitializeType: {
2771 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
2772 }
Logan Chien75e4b602012-07-23 14:24:12 -07002773
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002774 //==- Lock -------------------------------------------------------------==//
2775 case IntrinsicHelper::LockObject: {
2776 Expand_LockObject(call_inst.getArgOperand(0));
2777 return NULL;
2778 }
2779 case IntrinsicHelper::UnlockObject: {
2780 Expand_UnlockObject(call_inst.getArgOperand(0));
2781 return NULL;
2782 }
Logan Chien75e4b602012-07-23 14:24:12 -07002783
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002784 //==- Cast -------------------------------------------------------------==//
2785 case IntrinsicHelper::CheckCast: {
2786 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
2787 }
Logan Chien75e4b602012-07-23 14:24:12 -07002788 case IntrinsicHelper::HLCheckCast: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002789 Expand_HLCheckCast(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002790 return NULL;
2791 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002792 case IntrinsicHelper::IsAssignable: {
2793 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
2794 }
Logan Chien75e4b602012-07-23 14:24:12 -07002795
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002796 //==- Alloc ------------------------------------------------------------==//
2797 case IntrinsicHelper::AllocObject: {
2798 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
2799 }
2800 case IntrinsicHelper::AllocObjectWithAccessCheck: {
2801 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
2802 }
Logan Chien75e4b602012-07-23 14:24:12 -07002803
2804 //==- Instance ---------------------------------------------------------==//
2805 case IntrinsicHelper::NewInstance: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002806 return Expand_NewInstance(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002807 }
2808 case IntrinsicHelper::InstanceOf: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002809 return Expand_InstanceOf(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002810 }
2811
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002812 //==- Array ------------------------------------------------------------==//
Logan Chien75e4b602012-07-23 14:24:12 -07002813 case IntrinsicHelper::NewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002814 return Expand_NewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002815 }
2816 case IntrinsicHelper::OptArrayLength: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002817 return Expand_OptArrayLength(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002818 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002819 case IntrinsicHelper::ArrayLength: {
2820 return EmitLoadArrayLength(call_inst.getArgOperand(0));
2821 }
2822 case IntrinsicHelper::AllocArray: {
2823 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
2824 }
2825 case IntrinsicHelper::AllocArrayWithAccessCheck: {
2826 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
2827 call_inst);
2828 }
2829 case IntrinsicHelper::CheckAndAllocArray: {
2830 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
2831 }
2832 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
2833 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
2834 call_inst);
2835 }
2836 case IntrinsicHelper::ArrayGet: {
2837 return Expand_ArrayGet(call_inst.getArgOperand(0),
2838 call_inst.getArgOperand(1),
2839 kInt);
2840 }
2841 case IntrinsicHelper::ArrayGetWide: {
2842 return Expand_ArrayGet(call_inst.getArgOperand(0),
2843 call_inst.getArgOperand(1),
2844 kLong);
2845 }
2846 case IntrinsicHelper::ArrayGetObject: {
2847 return Expand_ArrayGet(call_inst.getArgOperand(0),
2848 call_inst.getArgOperand(1),
2849 kObject);
2850 }
2851 case IntrinsicHelper::ArrayGetBoolean: {
2852 return Expand_ArrayGet(call_inst.getArgOperand(0),
2853 call_inst.getArgOperand(1),
2854 kBoolean);
2855 }
2856 case IntrinsicHelper::ArrayGetByte: {
2857 return Expand_ArrayGet(call_inst.getArgOperand(0),
2858 call_inst.getArgOperand(1),
2859 kByte);
2860 }
2861 case IntrinsicHelper::ArrayGetChar: {
2862 return Expand_ArrayGet(call_inst.getArgOperand(0),
2863 call_inst.getArgOperand(1),
2864 kChar);
2865 }
2866 case IntrinsicHelper::ArrayGetShort: {
2867 return Expand_ArrayGet(call_inst.getArgOperand(0),
2868 call_inst.getArgOperand(1),
2869 kShort);
2870 }
2871 case IntrinsicHelper::ArrayPut: {
2872 Expand_ArrayPut(call_inst.getArgOperand(0),
2873 call_inst.getArgOperand(1),
2874 call_inst.getArgOperand(2),
2875 kInt);
2876 return NULL;
2877 }
2878 case IntrinsicHelper::ArrayPutWide: {
2879 Expand_ArrayPut(call_inst.getArgOperand(0),
2880 call_inst.getArgOperand(1),
2881 call_inst.getArgOperand(2),
2882 kLong);
2883 return NULL;
2884 }
2885 case IntrinsicHelper::ArrayPutObject: {
2886 Expand_ArrayPut(call_inst.getArgOperand(0),
2887 call_inst.getArgOperand(1),
2888 call_inst.getArgOperand(2),
2889 kObject);
2890 return NULL;
2891 }
2892 case IntrinsicHelper::ArrayPutBoolean: {
2893 Expand_ArrayPut(call_inst.getArgOperand(0),
2894 call_inst.getArgOperand(1),
2895 call_inst.getArgOperand(2),
2896 kBoolean);
2897 return NULL;
2898 }
2899 case IntrinsicHelper::ArrayPutByte: {
2900 Expand_ArrayPut(call_inst.getArgOperand(0),
2901 call_inst.getArgOperand(1),
2902 call_inst.getArgOperand(2),
2903 kByte);
2904 return NULL;
2905 }
2906 case IntrinsicHelper::ArrayPutChar: {
2907 Expand_ArrayPut(call_inst.getArgOperand(0),
2908 call_inst.getArgOperand(1),
2909 call_inst.getArgOperand(2),
2910 kChar);
2911 return NULL;
2912 }
2913 case IntrinsicHelper::ArrayPutShort: {
2914 Expand_ArrayPut(call_inst.getArgOperand(0),
2915 call_inst.getArgOperand(1),
2916 call_inst.getArgOperand(2),
2917 kShort);
2918 return NULL;
2919 }
2920 case IntrinsicHelper::CheckPutArrayElement: {
2921 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
2922 }
2923 case IntrinsicHelper::FilledNewArray: {
2924 Expand_FilledNewArray(call_inst);
2925 return NULL;
2926 }
2927 case IntrinsicHelper::FillArrayData: {
2928 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
2929 }
Logan Chien75e4b602012-07-23 14:24:12 -07002930 case IntrinsicHelper::HLFillArrayData: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002931 Expand_HLFillArrayData(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002932 return NULL;
2933 }
2934 case IntrinsicHelper::HLFilledNewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002935 return Expand_HLFilledNewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002936 }
2937
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002938 //==- Instance Field ---------------------------------------------------==//
2939 case IntrinsicHelper::InstanceFieldGet:
2940 case IntrinsicHelper::InstanceFieldGetBoolean:
2941 case IntrinsicHelper::InstanceFieldGetByte:
2942 case IntrinsicHelper::InstanceFieldGetChar:
2943 case IntrinsicHelper::InstanceFieldGetShort: {
2944 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
2945 }
2946 case IntrinsicHelper::InstanceFieldGetWide: {
2947 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
2948 }
2949 case IntrinsicHelper::InstanceFieldGetObject: {
2950 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
2951 }
2952 case IntrinsicHelper::InstanceFieldGetFast: {
2953 return Expand_IGetFast(call_inst.getArgOperand(0),
2954 call_inst.getArgOperand(1),
2955 call_inst.getArgOperand(2),
2956 kInt);
2957 }
2958 case IntrinsicHelper::InstanceFieldGetWideFast: {
2959 return Expand_IGetFast(call_inst.getArgOperand(0),
2960 call_inst.getArgOperand(1),
2961 call_inst.getArgOperand(2),
2962 kLong);
2963 }
2964 case IntrinsicHelper::InstanceFieldGetObjectFast: {
2965 return Expand_IGetFast(call_inst.getArgOperand(0),
2966 call_inst.getArgOperand(1),
2967 call_inst.getArgOperand(2),
2968 kObject);
2969 }
2970 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
2971 return Expand_IGetFast(call_inst.getArgOperand(0),
2972 call_inst.getArgOperand(1),
2973 call_inst.getArgOperand(2),
2974 kBoolean);
2975 }
2976 case IntrinsicHelper::InstanceFieldGetByteFast: {
2977 return Expand_IGetFast(call_inst.getArgOperand(0),
2978 call_inst.getArgOperand(1),
2979 call_inst.getArgOperand(2),
2980 kByte);
2981 }
2982 case IntrinsicHelper::InstanceFieldGetCharFast: {
2983 return Expand_IGetFast(call_inst.getArgOperand(0),
2984 call_inst.getArgOperand(1),
2985 call_inst.getArgOperand(2),
2986 kChar);
2987 }
2988 case IntrinsicHelper::InstanceFieldGetShortFast: {
2989 return Expand_IGetFast(call_inst.getArgOperand(0),
2990 call_inst.getArgOperand(1),
2991 call_inst.getArgOperand(2),
2992 kShort);
2993 }
2994 case IntrinsicHelper::InstanceFieldPut:
2995 case IntrinsicHelper::InstanceFieldPutBoolean:
2996 case IntrinsicHelper::InstanceFieldPutByte:
2997 case IntrinsicHelper::InstanceFieldPutChar:
2998 case IntrinsicHelper::InstanceFieldPutShort: {
2999 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
3000 }
3001 case IntrinsicHelper::InstanceFieldPutWide: {
3002 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
3003 }
3004 case IntrinsicHelper::InstanceFieldPutObject: {
3005 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
3006 }
3007 case IntrinsicHelper::InstanceFieldPutFast: {
3008 Expand_IPutFast(call_inst.getArgOperand(0),
3009 call_inst.getArgOperand(1),
3010 call_inst.getArgOperand(2),
3011 call_inst.getArgOperand(3),
3012 kInt);
3013 return NULL;
3014 }
3015 case IntrinsicHelper::InstanceFieldPutWideFast: {
3016 Expand_IPutFast(call_inst.getArgOperand(0),
3017 call_inst.getArgOperand(1),
3018 call_inst.getArgOperand(2),
3019 call_inst.getArgOperand(3),
3020 kLong);
3021 return NULL;
3022 }
3023 case IntrinsicHelper::InstanceFieldPutObjectFast: {
3024 Expand_IPutFast(call_inst.getArgOperand(0),
3025 call_inst.getArgOperand(1),
3026 call_inst.getArgOperand(2),
3027 call_inst.getArgOperand(3),
3028 kObject);
3029 return NULL;
3030 }
3031 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
3032 Expand_IPutFast(call_inst.getArgOperand(0),
3033 call_inst.getArgOperand(1),
3034 call_inst.getArgOperand(2),
3035 call_inst.getArgOperand(3),
3036 kBoolean);
3037 return NULL;
3038 }
3039 case IntrinsicHelper::InstanceFieldPutByteFast: {
3040 Expand_IPutFast(call_inst.getArgOperand(0),
3041 call_inst.getArgOperand(1),
3042 call_inst.getArgOperand(2),
3043 call_inst.getArgOperand(3),
3044 kByte);
3045 return NULL;
3046 }
3047 case IntrinsicHelper::InstanceFieldPutCharFast: {
3048 Expand_IPutFast(call_inst.getArgOperand(0),
3049 call_inst.getArgOperand(1),
3050 call_inst.getArgOperand(2),
3051 call_inst.getArgOperand(3),
3052 kChar);
3053 return NULL;
3054 }
3055 case IntrinsicHelper::InstanceFieldPutShortFast: {
3056 Expand_IPutFast(call_inst.getArgOperand(0),
3057 call_inst.getArgOperand(1),
3058 call_inst.getArgOperand(2),
3059 call_inst.getArgOperand(3),
3060 kShort);
3061 return NULL;
3062 }
Logan Chien75e4b602012-07-23 14:24:12 -07003063
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003064 //==- Static Field -----------------------------------------------------==//
3065 case IntrinsicHelper::StaticFieldGet:
3066 case IntrinsicHelper::StaticFieldGetBoolean:
3067 case IntrinsicHelper::StaticFieldGetByte:
3068 case IntrinsicHelper::StaticFieldGetChar:
3069 case IntrinsicHelper::StaticFieldGetShort: {
3070 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
3071 }
3072 case IntrinsicHelper::StaticFieldGetWide: {
3073 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
3074 }
3075 case IntrinsicHelper::StaticFieldGetObject: {
3076 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
3077 }
3078 case IntrinsicHelper::StaticFieldGetFast: {
3079 return Expand_SGetFast(call_inst.getArgOperand(0),
3080 call_inst.getArgOperand(1),
3081 call_inst.getArgOperand(2),
3082 kInt);
3083 }
3084 case IntrinsicHelper::StaticFieldGetWideFast: {
3085 return Expand_SGetFast(call_inst.getArgOperand(0),
3086 call_inst.getArgOperand(1),
3087 call_inst.getArgOperand(2),
3088 kLong);
3089 }
3090 case IntrinsicHelper::StaticFieldGetObjectFast: {
3091 return Expand_SGetFast(call_inst.getArgOperand(0),
3092 call_inst.getArgOperand(1),
3093 call_inst.getArgOperand(2),
3094 kObject);
3095 }
3096 case IntrinsicHelper::StaticFieldGetBooleanFast: {
3097 return Expand_SGetFast(call_inst.getArgOperand(0),
3098 call_inst.getArgOperand(1),
3099 call_inst.getArgOperand(2),
3100 kBoolean);
3101 }
3102 case IntrinsicHelper::StaticFieldGetByteFast: {
3103 return Expand_SGetFast(call_inst.getArgOperand(0),
3104 call_inst.getArgOperand(1),
3105 call_inst.getArgOperand(2),
3106 kByte);
3107 }
3108 case IntrinsicHelper::StaticFieldGetCharFast: {
3109 return Expand_SGetFast(call_inst.getArgOperand(0),
3110 call_inst.getArgOperand(1),
3111 call_inst.getArgOperand(2),
3112 kChar);
3113 }
3114 case IntrinsicHelper::StaticFieldGetShortFast: {
3115 return Expand_SGetFast(call_inst.getArgOperand(0),
3116 call_inst.getArgOperand(1),
3117 call_inst.getArgOperand(2),
3118 kShort);
3119 }
3120 case IntrinsicHelper::StaticFieldPut:
3121 case IntrinsicHelper::StaticFieldPutBoolean:
3122 case IntrinsicHelper::StaticFieldPutByte:
3123 case IntrinsicHelper::StaticFieldPutChar:
3124 case IntrinsicHelper::StaticFieldPutShort: {
3125 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
3126 }
3127 case IntrinsicHelper::StaticFieldPutWide: {
3128 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
3129 }
3130 case IntrinsicHelper::StaticFieldPutObject: {
3131 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
3132 }
3133 case IntrinsicHelper::StaticFieldPutFast: {
3134 Expand_SPutFast(call_inst.getArgOperand(0),
3135 call_inst.getArgOperand(1),
3136 call_inst.getArgOperand(2),
3137 call_inst.getArgOperand(3),
3138 kInt);
3139 return NULL;
3140 }
3141 case IntrinsicHelper::StaticFieldPutWideFast: {
3142 Expand_SPutFast(call_inst.getArgOperand(0),
3143 call_inst.getArgOperand(1),
3144 call_inst.getArgOperand(2),
3145 call_inst.getArgOperand(3),
3146 kLong);
3147 return NULL;
3148 }
3149 case IntrinsicHelper::StaticFieldPutObjectFast: {
3150 Expand_SPutFast(call_inst.getArgOperand(0),
3151 call_inst.getArgOperand(1),
3152 call_inst.getArgOperand(2),
3153 call_inst.getArgOperand(3),
3154 kObject);
3155 return NULL;
3156 }
3157 case IntrinsicHelper::StaticFieldPutBooleanFast: {
3158 Expand_SPutFast(call_inst.getArgOperand(0),
3159 call_inst.getArgOperand(1),
3160 call_inst.getArgOperand(2),
3161 call_inst.getArgOperand(3),
3162 kBoolean);
3163 return NULL;
3164 }
3165 case IntrinsicHelper::StaticFieldPutByteFast: {
3166 Expand_SPutFast(call_inst.getArgOperand(0),
3167 call_inst.getArgOperand(1),
3168 call_inst.getArgOperand(2),
3169 call_inst.getArgOperand(3),
3170 kByte);
3171 return NULL;
3172 }
3173 case IntrinsicHelper::StaticFieldPutCharFast: {
3174 Expand_SPutFast(call_inst.getArgOperand(0),
3175 call_inst.getArgOperand(1),
3176 call_inst.getArgOperand(2),
3177 call_inst.getArgOperand(3),
3178 kChar);
3179 return NULL;
3180 }
3181 case IntrinsicHelper::StaticFieldPutShortFast: {
3182 Expand_SPutFast(call_inst.getArgOperand(0),
3183 call_inst.getArgOperand(1),
3184 call_inst.getArgOperand(2),
3185 call_inst.getArgOperand(3),
3186 kShort);
3187 return NULL;
3188 }
3189 case IntrinsicHelper::LoadDeclaringClassSSB: {
3190 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
3191 }
3192 case IntrinsicHelper::LoadClassSSBFromDexCache: {
3193 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
3194 }
3195 case IntrinsicHelper::InitializeAndLoadClassSSB: {
3196 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
3197 }
Logan Chien75e4b602012-07-23 14:24:12 -07003198
3199 //==- High-level Array -------------------------------------------------==//
3200 case IntrinsicHelper::HLArrayGet: {
TDYa1275a26d442012-07-26 18:58:38 -07003201 return Expand_HLArrayGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003202 }
3203 case IntrinsicHelper::HLArrayGetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003204 return Expand_HLArrayGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003205 }
3206 case IntrinsicHelper::HLArrayGetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003207 return Expand_HLArrayGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003208 }
3209 case IntrinsicHelper::HLArrayGetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003210 return Expand_HLArrayGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003211 }
3212 case IntrinsicHelper::HLArrayGetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003213 return Expand_HLArrayGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003214 }
3215 case IntrinsicHelper::HLArrayGetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003216 return Expand_HLArrayGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003217 }
3218 case IntrinsicHelper::HLArrayGetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003219 return Expand_HLArrayGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003220 }
3221 case IntrinsicHelper::HLArrayGetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003222 return Expand_HLArrayGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003223 }
3224 case IntrinsicHelper::HLArrayGetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003225 return Expand_HLArrayGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003226 }
3227 case IntrinsicHelper::HLArrayPut: {
TDYa1275a26d442012-07-26 18:58:38 -07003228 Expand_HLArrayPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003229 return NULL;
3230 }
3231 case IntrinsicHelper::HLArrayPutBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003232 Expand_HLArrayPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003233 return NULL;
3234 }
3235 case IntrinsicHelper::HLArrayPutByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003236 Expand_HLArrayPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003237 return NULL;
3238 }
3239 case IntrinsicHelper::HLArrayPutChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003240 Expand_HLArrayPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003241 return NULL;
3242 }
3243 case IntrinsicHelper::HLArrayPutShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003244 Expand_HLArrayPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003245 return NULL;
3246 }
3247 case IntrinsicHelper::HLArrayPutFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003248 Expand_HLArrayPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003249 return NULL;
3250 }
3251 case IntrinsicHelper::HLArrayPutWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003252 Expand_HLArrayPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003253 return NULL;
3254 }
3255 case IntrinsicHelper::HLArrayPutDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003256 Expand_HLArrayPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003257 return NULL;
3258 }
3259 case IntrinsicHelper::HLArrayPutObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003260 Expand_HLArrayPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003261 return NULL;
3262 }
3263
3264 //==- High-level Instance ----------------------------------------------==//
3265 case IntrinsicHelper::HLIGet: {
TDYa1275e869b62012-07-25 00:45:39 -07003266 return Expand_HLIGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003267 }
3268 case IntrinsicHelper::HLIGetBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003269 return Expand_HLIGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003270 }
3271 case IntrinsicHelper::HLIGetByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003272 return Expand_HLIGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003273 }
3274 case IntrinsicHelper::HLIGetChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003275 return Expand_HLIGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003276 }
3277 case IntrinsicHelper::HLIGetShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003278 return Expand_HLIGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003279 }
3280 case IntrinsicHelper::HLIGetFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003281 return Expand_HLIGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003282 }
3283 case IntrinsicHelper::HLIGetWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003284 return Expand_HLIGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003285 }
3286 case IntrinsicHelper::HLIGetDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003287 return Expand_HLIGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003288 }
3289 case IntrinsicHelper::HLIGetObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003290 return Expand_HLIGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003291 }
3292 case IntrinsicHelper::HLIPut: {
TDYa1275e869b62012-07-25 00:45:39 -07003293 Expand_HLIPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003294 return NULL;
3295 }
3296 case IntrinsicHelper::HLIPutBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003297 Expand_HLIPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003298 return NULL;
3299 }
3300 case IntrinsicHelper::HLIPutByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003301 Expand_HLIPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003302 return NULL;
3303 }
3304 case IntrinsicHelper::HLIPutChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003305 Expand_HLIPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003306 return NULL;
3307 }
3308 case IntrinsicHelper::HLIPutShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003309 Expand_HLIPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003310 return NULL;
3311 }
3312 case IntrinsicHelper::HLIPutFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003313 Expand_HLIPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003314 return NULL;
3315 }
3316 case IntrinsicHelper::HLIPutWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003317 Expand_HLIPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003318 return NULL;
3319 }
3320 case IntrinsicHelper::HLIPutDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003321 Expand_HLIPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003322 return NULL;
3323 }
3324 case IntrinsicHelper::HLIPutObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003325 Expand_HLIPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003326 return NULL;
3327 }
3328
3329 //==- High-level Invoke ------------------------------------------------==//
TDYa127f71bf5a2012-07-29 20:09:52 -07003330 case IntrinsicHelper::HLInvokeVoid:
3331 case IntrinsicHelper::HLInvokeObj:
3332 case IntrinsicHelper::HLInvokeInt:
3333 case IntrinsicHelper::HLInvokeFloat:
3334 case IntrinsicHelper::HLInvokeLong:
Logan Chien75e4b602012-07-23 14:24:12 -07003335 case IntrinsicHelper::HLInvokeDouble: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003336 return Expand_HLInvoke(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003337 }
3338
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003339 //==- Invoke -----------------------------------------------------------==//
3340 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
3341 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
3342 }
3343 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
3344 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
3345 }
3346 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
3347 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
3348 }
3349 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
3350 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
3351 }
3352 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
3353 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
3354 }
3355 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
3356 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
3357 }
3358 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
3359 return Expand_GetVirtualCalleeMethodObjAddrFast(
3360 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
3361 }
3362 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
3363 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
3364 }
3365 case IntrinsicHelper::InvokeRetVoid:
3366 case IntrinsicHelper::InvokeRetBoolean:
3367 case IntrinsicHelper::InvokeRetByte:
3368 case IntrinsicHelper::InvokeRetChar:
3369 case IntrinsicHelper::InvokeRetShort:
3370 case IntrinsicHelper::InvokeRetInt:
3371 case IntrinsicHelper::InvokeRetLong:
3372 case IntrinsicHelper::InvokeRetFloat:
3373 case IntrinsicHelper::InvokeRetDouble:
3374 case IntrinsicHelper::InvokeRetObject: {
3375 return Expand_Invoke(call_inst);
3376 }
Logan Chien75e4b602012-07-23 14:24:12 -07003377
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003378 //==- Math -------------------------------------------------------------==//
3379 case IntrinsicHelper::DivInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003380 return Expand_DivRem(call_inst, /* is_div */true, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003381 }
3382 case IntrinsicHelper::RemInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003383 return Expand_DivRem(call_inst, /* is_div */false, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003384 }
3385 case IntrinsicHelper::DivLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003386 return Expand_DivRem(call_inst, /* is_div */true, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003387 }
3388 case IntrinsicHelper::RemLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003389 return Expand_DivRem(call_inst, /* is_div */false, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003390 }
3391 case IntrinsicHelper::D2L: {
3392 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
3393 }
3394 case IntrinsicHelper::D2I: {
3395 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
3396 }
3397 case IntrinsicHelper::F2L: {
3398 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
3399 }
3400 case IntrinsicHelper::F2I: {
3401 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
3402 }
Logan Chien75e4b602012-07-23 14:24:12 -07003403
3404 //==- High-level Static ------------------------------------------------==//
3405 case IntrinsicHelper::HLSget: {
TDYa1275a26d442012-07-26 18:58:38 -07003406 return Expand_HLSget(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003407 }
3408 case IntrinsicHelper::HLSgetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003409 return Expand_HLSget(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003410 }
3411 case IntrinsicHelper::HLSgetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003412 return Expand_HLSget(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003413 }
3414 case IntrinsicHelper::HLSgetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003415 return Expand_HLSget(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003416 }
3417 case IntrinsicHelper::HLSgetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003418 return Expand_HLSget(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003419 }
3420 case IntrinsicHelper::HLSgetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003421 return Expand_HLSget(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003422 }
3423 case IntrinsicHelper::HLSgetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003424 return Expand_HLSget(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003425 }
3426 case IntrinsicHelper::HLSgetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003427 return Expand_HLSget(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003428 }
3429 case IntrinsicHelper::HLSgetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003430 return Expand_HLSget(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003431 }
3432 case IntrinsicHelper::HLSput: {
TDYa1275a26d442012-07-26 18:58:38 -07003433 Expand_HLSput(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003434 return NULL;
3435 }
3436 case IntrinsicHelper::HLSputBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003437 Expand_HLSput(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003438 return NULL;
3439 }
3440 case IntrinsicHelper::HLSputByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003441 Expand_HLSput(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003442 return NULL;
3443 }
3444 case IntrinsicHelper::HLSputChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003445 Expand_HLSput(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003446 return NULL;
3447 }
3448 case IntrinsicHelper::HLSputShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003449 Expand_HLSput(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003450 return NULL;
3451 }
3452 case IntrinsicHelper::HLSputFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003453 Expand_HLSput(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003454 return NULL;
3455 }
3456 case IntrinsicHelper::HLSputWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003457 Expand_HLSput(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003458 return NULL;
3459 }
3460 case IntrinsicHelper::HLSputDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003461 Expand_HLSput(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003462 return NULL;
3463 }
3464 case IntrinsicHelper::HLSputObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003465 Expand_HLSput(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003466 return NULL;
3467 }
3468
3469 //==- High-level Monitor -----------------------------------------------==//
3470 case IntrinsicHelper::MonitorEnter: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003471 Expand_MonitorEnter(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003472 return NULL;
3473 }
3474 case IntrinsicHelper::MonitorExit: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003475 Expand_MonitorExit(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003476 return NULL;
3477 }
3478
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003479 //==- Shadow Frame -----------------------------------------------------==//
3480 case IntrinsicHelper::AllocaShadowFrame: {
TDYa1278e950c12012-11-02 09:58:19 -07003481 Expand_AllocaShadowFrame(call_inst.getArgOperand(0),
3482 call_inst.getArgOperand(1));
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003483 return NULL;
3484 }
3485 case IntrinsicHelper::SetShadowFrameEntry: {
3486 Expand_SetShadowFrameEntry(call_inst.getArgOperand(0),
3487 call_inst.getArgOperand(1));
3488 return NULL;
3489 }
TDYa1278e950c12012-11-02 09:58:19 -07003490 case IntrinsicHelper::SetVReg: {
3491 Expand_SetVReg(call_inst.getArgOperand(0),
3492 call_inst.getArgOperand(1));
3493 return NULL;
3494 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003495 case IntrinsicHelper::PopShadowFrame: {
3496 Expand_PopShadowFrame();
3497 return NULL;
3498 }
3499 case IntrinsicHelper::UpdateDexPC: {
3500 Expand_UpdateDexPC(call_inst.getArgOperand(0));
3501 return NULL;
3502 }
TDYa127a1b21852012-07-23 03:20:39 -07003503
Logan Chien75e4b602012-07-23 14:24:12 -07003504 //==- Comparison -------------------------------------------------------==//
3505 case IntrinsicHelper::CmplFloat:
3506 case IntrinsicHelper::CmplDouble: {
3507 return Expand_FPCompare(call_inst.getArgOperand(0),
3508 call_inst.getArgOperand(1),
3509 false);
3510 }
3511 case IntrinsicHelper::CmpgFloat:
3512 case IntrinsicHelper::CmpgDouble: {
3513 return Expand_FPCompare(call_inst.getArgOperand(0),
3514 call_inst.getArgOperand(1),
3515 true);
3516 }
3517 case IntrinsicHelper::CmpLong: {
3518 return Expand_LongCompare(call_inst.getArgOperand(0),
3519 call_inst.getArgOperand(1));
3520 }
TDYa127a1b21852012-07-23 03:20:39 -07003521
Logan Chien75e4b602012-07-23 14:24:12 -07003522 //==- Const ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003523 case IntrinsicHelper::ConstInt:
3524 case IntrinsicHelper::ConstLong: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003525 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003526 }
TDYa127920be7c2012-09-10 17:13:22 -07003527 case IntrinsicHelper::ConstFloat: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003528 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3529 irb_.getJFloatTy());
Logan Chien75e4b602012-07-23 14:24:12 -07003530 }
TDYa127920be7c2012-09-10 17:13:22 -07003531 case IntrinsicHelper::ConstDouble: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003532 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3533 irb_.getJDoubleTy());
3534 }
TDYa127920be7c2012-09-10 17:13:22 -07003535 case IntrinsicHelper::ConstObj: {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003536 CHECK(LV2UInt(call_inst.getArgOperand(0)) == 0);
3537 return irb_.getJNull();
Logan Chien75e4b602012-07-23 14:24:12 -07003538 }
3539
3540 //==- Method Info ------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003541 case IntrinsicHelper::MethodInfo: {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003542 // Nothing to be done, because MethodInfo carries optional hints that are
3543 // not needed by the portable path.
Logan Chien75e4b602012-07-23 14:24:12 -07003544 return NULL;
3545 }
3546
3547 //==- Copy -------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003548 case IntrinsicHelper::CopyInt:
3549 case IntrinsicHelper::CopyFloat:
3550 case IntrinsicHelper::CopyLong:
3551 case IntrinsicHelper::CopyDouble:
3552 case IntrinsicHelper::CopyObj: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003553 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003554 }
3555
3556 //==- Shift ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003557 case IntrinsicHelper::SHLLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003558 return Expand_IntegerShift(call_inst.getArgOperand(0),
3559 call_inst.getArgOperand(1),
3560 kIntegerSHL, kLong);
3561 }
TDYa127920be7c2012-09-10 17:13:22 -07003562 case IntrinsicHelper::SHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003563 return Expand_IntegerShift(call_inst.getArgOperand(0),
3564 call_inst.getArgOperand(1),
3565 kIntegerSHR, kLong);
3566 }
TDYa127920be7c2012-09-10 17:13:22 -07003567 case IntrinsicHelper::USHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003568 return Expand_IntegerShift(call_inst.getArgOperand(0),
3569 call_inst.getArgOperand(1),
3570 kIntegerUSHR, kLong);
3571 }
TDYa127920be7c2012-09-10 17:13:22 -07003572 case IntrinsicHelper::SHLInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003573 return Expand_IntegerShift(call_inst.getArgOperand(0),
3574 call_inst.getArgOperand(1),
3575 kIntegerSHL, kInt);
3576 }
TDYa127920be7c2012-09-10 17:13:22 -07003577 case IntrinsicHelper::SHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003578 return Expand_IntegerShift(call_inst.getArgOperand(0),
3579 call_inst.getArgOperand(1),
3580 kIntegerSHR, kInt);
3581 }
TDYa127920be7c2012-09-10 17:13:22 -07003582 case IntrinsicHelper::USHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003583 return Expand_IntegerShift(call_inst.getArgOperand(0),
3584 call_inst.getArgOperand(1),
3585 kIntegerUSHR, kInt);
3586 }
3587
3588 //==- Conversion -------------------------------------------------------==//
TDYa127a1b21852012-07-23 03:20:39 -07003589 case IntrinsicHelper::IntToChar: {
3590 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
3591 irb_.getJIntTy());
3592 }
3593 case IntrinsicHelper::IntToShort: {
3594 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
3595 irb_.getJIntTy());
3596 }
3597 case IntrinsicHelper::IntToByte: {
3598 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
3599 irb_.getJIntTy());
3600 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003601
TDYa12787caa7e2012-08-25 23:23:27 -07003602 //==- Exception --------------------------------------------------------==//
3603 case IntrinsicHelper::CatchTargets: {
TDYa12755e5e6c2012-09-11 15:14:42 -07003604 UpdatePhiInstruction(current_bb_, irb_.GetInsertBlock());
TDYa12787caa7e2012-08-25 23:23:27 -07003605 llvm::SwitchInst* si = llvm::dyn_cast<llvm::SwitchInst>(call_inst.getNextNode());
3606 CHECK(si != NULL);
3607 irb_.CreateBr(si->getDefaultDest());
3608 si->eraseFromParent();
3609 return call_inst.getArgOperand(0);
3610 }
3611
Logan Chien75e4b602012-07-23 14:24:12 -07003612 //==- Unknown Cases ----------------------------------------------------==//
3613 case IntrinsicHelper::MaxIntrinsicId:
3614 case IntrinsicHelper::UnknownId:
3615 //default:
3616 // NOTE: "default" is intentionally commented so that C/C++ compiler will
3617 // give some warning on unmatched cases.
3618 // NOTE: We should not implement these cases.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003619 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003620 }
Logan Chien75e4b602012-07-23 14:24:12 -07003621 UNIMPLEMENTED(FATAL) << "Unexpected GBC intrinsic: " << static_cast<int>(intr_id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003622 return NULL;
3623}
3624
3625} // anonymous namespace
3626
3627namespace art {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003628
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003629namespace compiler_llvm {
3630
3631llvm::FunctionPass*
3632CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb) {
3633 return new GBCExpanderPass(intrinsic_helper, irb);
3634}
3635
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003636llvm::FunctionPass*
3637CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
3638 Compiler* compiler, OatCompilationUnit* oat_compilation_unit) {
3639 if (compiler != NULL) {
3640 return new GBCExpanderPass(intrinsic_helper, irb,
3641 compiler, oat_compilation_unit);
3642 } else {
3643 return new GBCExpanderPass(intrinsic_helper, irb);
3644 }
3645}
3646
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003647} // namespace compiler_llvm
3648} // namespace art