blob: d482526621247d14d627ac28c46b9f0ccdf4c484 [file] [log] [blame]
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ir_builder.h"
18#include "utils_llvm.h"
19
TDYa1275e869b62012-07-25 00:45:39 -070020#include "compiler.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070021#include "greenland/intrinsic_helper.h"
TDYa1275e869b62012-07-25 00:45:39 -070022#include "oat_compilation_unit.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070023#include "object.h"
24#include "thread.h"
TDYa1275e869b62012-07-25 00:45:39 -070025#include "verifier/method_verifier.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070026
TDYa127920be7c2012-09-10 17:13:22 -070027#include "compiler/CompilerIR.h"
28using art::kMIRIgnoreNullCheck;
29using art::kMIRIgnoreRangeCheck;
30
Shih-wei Liao21d28f52012-06-12 05:55:00 -070031#include <llvm/ADT/STLExtras.h>
32#include <llvm/Intrinsics.h>
Logan Chiend36a2ac2012-08-04 00:37:30 +080033#include <llvm/Metadata.h>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070034#include <llvm/Pass.h>
35#include <llvm/Support/CFG.h>
36#include <llvm/Support/InstIterator.h>
37
38#include <vector>
TDYa127aa558872012-08-16 05:11:07 -070039#include <map>
40#include <utility>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070041
TDYa127920be7c2012-09-10 17:13:22 -070042using namespace art::compiler_llvm;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070043
44using art::greenland::IntrinsicHelper;
45
Shih-wei Liaob2596522012-09-14 16:36:11 -070046namespace art {
47extern char remapShorty(char shortyType);
48};
49
Shih-wei Liao21d28f52012-06-12 05:55:00 -070050namespace {
51
52class GBCExpanderPass : public llvm::FunctionPass {
53 private:
54 const IntrinsicHelper& intrinsic_helper_;
55 IRBuilder& irb_;
56
57 llvm::LLVMContext& context_;
58 RuntimeSupportBuilder& rtb_;
59
60 private:
61 llvm::AllocaInst* shadow_frame_;
62 llvm::Value* old_shadow_frame_;
63 uint32_t shadow_frame_size_;
64
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_GetException();
165
166 llvm::Value* Expand_LoadStringFromDexCache(llvm::Value* string_idx_value);
167
168 llvm::Value* Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value);
169
170 void Expand_LockObject(llvm::Value* obj);
171
172 void Expand_UnlockObject(llvm::Value* obj);
173
174 llvm::Value* Expand_ArrayGet(llvm::Value* array_addr,
175 llvm::Value* index_value,
176 JType elem_jty);
177
178 void Expand_ArrayPut(llvm::Value* new_value,
179 llvm::Value* array_addr,
180 llvm::Value* index_value,
181 JType elem_jty);
182
183 void Expand_FilledNewArray(llvm::CallInst& call_inst);
184
185 llvm::Value* Expand_IGetFast(llvm::Value* field_offset_value,
186 llvm::Value* is_volatile_value,
187 llvm::Value* object_addr,
188 JType field_jty);
189
190 void Expand_IPutFast(llvm::Value* field_offset_value,
191 llvm::Value* is_volatile_value,
192 llvm::Value* object_addr,
193 llvm::Value* new_value,
194 JType field_jty);
195
196 llvm::Value* Expand_SGetFast(llvm::Value* static_storage_addr,
197 llvm::Value* field_offset_value,
198 llvm::Value* is_volatile_value,
199 JType field_jty);
200
201 void Expand_SPutFast(llvm::Value* static_storage_addr,
202 llvm::Value* field_offset_value,
203 llvm::Value* is_volatile_value,
204 llvm::Value* new_value,
205 JType field_jty);
206
207 llvm::Value* Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr);
208
209 llvm::Value* Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value);
210
211 llvm::Value*
212 Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value);
213
214 llvm::Value*
215 Expand_GetVirtualCalleeMethodObjAddrFast(llvm::Value* vtable_idx_value,
216 llvm::Value* this_addr);
217
218 llvm::Value* Expand_Invoke(llvm::CallInst& call_inst);
219
TDYa1274ec8ccd2012-08-11 07:04:57 -0700220 llvm::Value* Expand_DivRem(llvm::CallInst& call_inst, bool is_div, JType op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700221
222 void Expand_AllocaShadowFrame(llvm::Value* num_entry_value);
223
224 void Expand_SetShadowFrameEntry(llvm::Value* obj, llvm::Value* entry_idx);
225
226 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()),
Logan Chiene5b8f8b2012-08-13 11:45:05 +0800331 shadow_frame_(NULL), old_shadow_frame_(NULL), shadow_frame_size_(0),
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()),
341 shadow_frame_(NULL), old_shadow_frame_(NULL), shadow_frame_size_(0),
342 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;
371 shadow_frame_size_ = 0;
TDYa1275e869b62012-07-25 00:45:39 -0700372 func_ = &func;
Logan Chien67645d82012-08-17 09:10:54 +0800373 changed_ = false; // Assume unchanged
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700374
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700375#if defined(ART_USE_QUICK_COMPILER)
376 basic_blocks_.resize(code_item_->insns_size_in_code_units_);
377 basic_block_landing_pads_.resize(code_item_->tries_size_, NULL);
378 basic_block_unwind_ = NULL;
379 for (llvm::Function::iterator bb_iter = func_->begin(), bb_end = func_->end();
380 bb_iter != bb_end;
381 ++bb_iter) {
382 if (bb_iter->begin()->getMetadata("DexOff") == NULL) {
383 continue;
384 }
385 uint32_t dex_pc = LV2UInt(bb_iter->begin()->getMetadata("DexOff")->getOperand(0));
386 basic_blocks_[dex_pc] = bb_iter;
387 }
388#endif
389
Logan Chien67645d82012-08-17 09:10:54 +0800390 // Insert stack overflow check
391 InsertStackOverflowCheck(func); // TODO: Use intrinsic.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700392
Logan Chien67645d82012-08-17 09:10:54 +0800393 // Rewrite the intrinsics
394 RewriteFunction();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700395
396 VERIFY_LLVM_FUNCTION(func);
397
Logan Chien67645d82012-08-17 09:10:54 +0800398 return changed_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700399}
400
Logan Chien67645d82012-08-17 09:10:54 +0800401void GBCExpanderPass::RewriteBasicBlock(llvm::BasicBlock* original_block) {
402 llvm::BasicBlock* curr_basic_block = original_block;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700403
Logan Chien67645d82012-08-17 09:10:54 +0800404 llvm::BasicBlock::iterator inst_iter = original_block->begin();
405 llvm::BasicBlock::iterator inst_end = original_block->end();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700406
Logan Chien67645d82012-08-17 09:10:54 +0800407 while (inst_iter != inst_end) {
408 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst_iter);
409 IntrinsicHelper::IntrinsicId intr_id = IntrinsicHelper::UnknownId;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700410
Logan Chien67645d82012-08-17 09:10:54 +0800411 if (call_inst) {
412 llvm::Function* callee_func = call_inst->getCalledFunction();
413 intr_id = intrinsic_helper_.GetIntrinsicId(callee_func);
414 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700415
Logan Chien67645d82012-08-17 09:10:54 +0800416 if (intr_id == IntrinsicHelper::UnknownId) {
417 // This is not intrinsic call. Skip this instruction.
418 ++inst_iter;
419 continue;
420 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700421
Logan Chien67645d82012-08-17 09:10:54 +0800422 // Rewrite the intrinsic and change the function
423 changed_ = true;
424 irb_.SetInsertPoint(inst_iter);
425
426 // Expand the intrinsic
427 if (llvm::Value* new_value = ExpandIntrinsic(intr_id, *call_inst)) {
428 inst_iter->replaceAllUsesWith(new_value);
429 }
430
431 // Remove the old intrinsic call instruction
432 llvm::BasicBlock::iterator old_inst = inst_iter++;
433 old_inst->eraseFromParent();
434
435 // Splice the instruction to the new basic block
436 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
437 if (next_basic_block != curr_basic_block) {
438 next_basic_block->getInstList().splice(
439 irb_.GetInsertPoint(), curr_basic_block->getInstList(),
440 inst_iter, inst_end);
441 curr_basic_block = next_basic_block;
442 inst_end = curr_basic_block->end();
443 }
444 }
445}
446
447
448void GBCExpanderPass::RewriteFunction() {
449 size_t num_basic_blocks = func_->getBasicBlockList().size();
450 // NOTE: We are not using (bb_iter != bb_end) as the for-loop condition,
451 // because we will create new basic block while expanding the intrinsics.
452 // We only want to iterate through the input basic blocks.
453
TDYa127aa558872012-08-16 05:11:07 -0700454 landing_pad_phi_mapping_.clear();
455
Logan Chien67645d82012-08-17 09:10:54 +0800456 for (llvm::Function::iterator bb_iter = func_->begin();
457 num_basic_blocks > 0; ++bb_iter, --num_basic_blocks) {
Shih-wei Liao627d8c42012-08-24 08:31:18 -0700458 // Set insert point to current basic block.
459 irb_.SetInsertPoint(bb_iter);
Logan Chien67645d82012-08-17 09:10:54 +0800460
TDYa12755e5e6c2012-09-11 15:14:42 -0700461 current_bb_ = bb_iter;
TDYa127aa558872012-08-16 05:11:07 -0700462
Logan Chien67645d82012-08-17 09:10:54 +0800463 // Rewrite the basic block
464 RewriteBasicBlock(bb_iter);
465
466 // Update the phi-instructions in the successor basic block
467 llvm::BasicBlock* last_block = irb_.GetInsertBlock();
468 if (last_block != bb_iter) {
469 UpdatePhiInstruction(bb_iter, last_block);
470 }
471 }
TDYa127aa558872012-08-16 05:11:07 -0700472
473 typedef std::map<llvm::PHINode*, llvm::PHINode*> HandlerPHIMap;
474 HandlerPHIMap handler_phi;
475 // Iterate every used landing pad basic block
476 for (size_t i = 0, ei = basic_block_landing_pads_.size(); i != ei; ++i) {
477 llvm::BasicBlock* lbb = basic_block_landing_pads_[i];
478 if (lbb == NULL) {
479 continue;
480 }
481
482 llvm::TerminatorInst* term_inst = lbb->getTerminator();
483 std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> >& rewrite_pair
484 = landing_pad_phi_mapping_[lbb];
485 irb_.SetInsertPoint(lbb->begin());
486
487 // Iterate every succeeding basic block (catch block)
488 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
489 succ_iter != succ_end; ++succ_iter) {
490 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
491
492 // Iterate every phi instructions in the succeeding basic block
493 for (llvm::BasicBlock::iterator
494 inst_iter = succ_basic_block->begin(),
495 inst_end = succ_basic_block->end();
496 inst_iter != inst_end; ++inst_iter) {
497 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
498
499 if (!phi) {
500 break; // Meet non-phi instruction. Done.
501 }
502
503 if (handler_phi[phi] == NULL) {
504 handler_phi[phi] = llvm::PHINode::Create(phi->getType(), 1);
505 }
506
507 // Create new_phi in landing pad
508 llvm::PHINode* new_phi = irb_.CreatePHI(phi->getType(), rewrite_pair.size());
509 // Insert all incoming value into new_phi by rewrite_pair
510 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
511 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
512 llvm::BasicBlock* new_bb = rewrite_pair[j].second;
513 new_phi->addIncoming(phi->getIncomingValueForBlock(old_bb), new_bb);
514 }
515 // Delete all incoming value from phi by rewrite_pair
516 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
517 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
518 int old_bb_idx = phi->getBasicBlockIndex(old_bb);
519 if (old_bb_idx >= 0) {
520 phi->removeIncomingValue(old_bb_idx, false);
521 }
522 }
523 // Insert new_phi into new handler phi
524 handler_phi[phi]->addIncoming(new_phi, lbb);
525 }
526 }
527 }
528
529 // Replace all handler phi
530 // We can't just use the old handler phi, because some exception edges will disappear after we
531 // compute fast-path.
532 for (HandlerPHIMap::iterator it = handler_phi.begin(); it != handler_phi.end(); ++it) {
533 llvm::PHINode* old_phi = it->first;
534 llvm::PHINode* new_phi = it->second;
535 new_phi->insertBefore(old_phi);
536 old_phi->replaceAllUsesWith(new_phi);
537 old_phi->eraseFromParent();
538 }
Logan Chien67645d82012-08-17 09:10:54 +0800539}
540
541void GBCExpanderPass::UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
542 llvm::BasicBlock* new_basic_block) {
543 llvm::TerminatorInst* term_inst = new_basic_block->getTerminator();
544
545 if (!term_inst) {
546 return; // No terminating instruction in new_basic_block. Nothing to do.
547 }
548
549 // Iterate every succeeding basic block
550 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
551 succ_iter != succ_end; ++succ_iter) {
552 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
553
554 // Iterate every phi instructions in the succeeding basic block
555 for (llvm::BasicBlock::iterator
556 inst_iter = succ_basic_block->begin(),
557 inst_end = succ_basic_block->end();
558 inst_iter != inst_end; ++inst_iter) {
559 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
560
561 if (!phi) {
562 break; // Meet non-phi instruction. Done.
563 }
564
565 // Update the incoming block of this phi instruction
566 for (llvm::PHINode::block_iterator
567 ibb_iter = phi->block_begin(), ibb_end = phi->block_end();
568 ibb_iter != ibb_end; ++ibb_iter) {
569 if (*ibb_iter == old_basic_block) {
570 *ibb_iter = new_basic_block;
571 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700572 }
573 }
574 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700575}
576
577llvm::Value* GBCExpanderPass::ExpandToRuntime(runtime_support::RuntimeId rt,
578 llvm::CallInst& inst) {
579 // Some GBC intrinsic can directly replace with IBC runtime. "Directly" means
580 // the arguments passed to the GBC intrinsic are as the same as IBC runtime
581 // function, therefore only called function is needed to change.
582 unsigned num_args = inst.getNumArgOperands();
583
584 if (num_args <= 0) {
585 return irb_.CreateCall(irb_.GetRuntime(rt));
586 } else {
587 std::vector<llvm::Value*> args;
588 for (unsigned i = 0; i < num_args; i++) {
589 args.push_back(inst.getArgOperand(i));
590 }
591
592 return irb_.CreateCall(irb_.GetRuntime(rt), args);
593 }
594}
595
Logan Chien67645d82012-08-17 09:10:54 +0800596void
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700597GBCExpanderPass::EmitStackOverflowCheck(llvm::Instruction* first_non_alloca) {
598 llvm::Function* func = first_non_alloca->getParent()->getParent();
599 llvm::Module* module = func->getParent();
600
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700601 // Call llvm intrinsic function to get frame address.
602 llvm::Function* frameaddress =
603 llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
604
605 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
606 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
607
608 // Cast i8* to int
609 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
610
611 // Get thread.stack_end_
612 llvm::Value* stack_end =
TDYa127920be7c2012-09-10 17:13:22 -0700613 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::StackEndOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700614 irb_.getPtrEquivIntTy(),
615 kTBAARuntimeInfo);
616
617 // Check the frame address < thread.stack_end_ ?
618 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
619
620 llvm::BasicBlock* block_exception =
621 llvm::BasicBlock::Create(context_, "stack_overflow", func);
622
623 llvm::BasicBlock* block_continue =
624 llvm::BasicBlock::Create(context_, "stack_overflow_cont", func);
625
626 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
627
628 // If stack overflow, throw exception.
629 irb_.SetInsertPoint(block_exception);
630 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowStackOverflowException));
631
632 // Unwind.
633 llvm::Type* ret_type = func->getReturnType();
634 if (ret_type->isVoidTy()) {
635 irb_.CreateRetVoid();
636 } else {
637 // The return value is ignored when there's an exception. MethodCompiler
638 // returns zero value under the the corresponding return type in this case.
639 // GBCExpander returns LLVM undef value here for brevity
640 irb_.CreateRet(llvm::UndefValue::get(ret_type));
641 }
642
643 irb_.SetInsertPoint(block_continue);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700644}
645
TDYa127920be7c2012-09-10 17:13:22 -0700646llvm::Value* GBCExpanderPass::EmitLoadDexCacheAddr(art::MemberOffset offset) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700647 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
648
649 return irb_.LoadFromObjectOffset(method_object_addr,
650 offset.Int32Value(),
651 irb_.getJObjectTy(),
652 kTBAAConstJObject);
653}
654
655llvm::Value*
656GBCExpanderPass::EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
657 llvm::Value* static_storage_dex_cache_addr =
TDYa127920be7c2012-09-10 17:13:22 -0700658 EmitLoadDexCacheAddr(art::Method::DexCacheInitializedStaticStorageOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700659
660 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
661
662 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
663}
664
665llvm::Value*
666GBCExpanderPass::EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
667 llvm::Value* resolved_type_dex_cache_addr =
TDYa127920be7c2012-09-10 17:13:22 -0700668 EmitLoadDexCacheAddr(art::Method::DexCacheResolvedTypesOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700669
670 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
671
672 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
673}
674
675llvm::Value* GBCExpanderPass::
676EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
677 llvm::Value* resolved_method_dex_cache_addr =
TDYa127920be7c2012-09-10 17:13:22 -0700678 EmitLoadDexCacheAddr(art::Method::DexCacheResolvedMethodsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700679
680 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
681
682 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
683}
684
685llvm::Value* GBCExpanderPass::
686EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
687 llvm::Value* string_dex_cache_addr =
TDYa127920be7c2012-09-10 17:13:22 -0700688 EmitLoadDexCacheAddr(art::Method::DexCacheStringsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700689
690 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
691
692 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
693}
694
695llvm::Value* GBCExpanderPass::EmitLoadMethodObjectAddr() {
696 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
697 return parent_func->arg_begin();
698}
699
700llvm::Value* GBCExpanderPass::EmitLoadArrayLength(llvm::Value* array) {
701 // Load array length
702 return irb_.LoadFromObjectOffset(array,
TDYa127920be7c2012-09-10 17:13:22 -0700703 art::Array::LengthOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700704 irb_.getJIntTy(),
705 kTBAAConstJObject);
706
707}
708
709llvm::Value*
710GBCExpanderPass::EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
711 llvm::Value* callee_method_object_field_addr =
712 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
713
714 return irb_.CreateLoad(callee_method_object_field_addr, kTBAAJRuntime);
715}
716
717llvm::Value* GBCExpanderPass::
718EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, llvm::Value* this_addr) {
719 // Load class object of *this* pointer
720 llvm::Value* class_object_addr =
721 irb_.LoadFromObjectOffset(this_addr,
TDYa127920be7c2012-09-10 17:13:22 -0700722 art::Object::ClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700723 irb_.getJObjectTy(),
724 kTBAAConstJObject);
725
726 // Load vtable address
727 llvm::Value* vtable_addr =
728 irb_.LoadFromObjectOffset(class_object_addr,
TDYa127920be7c2012-09-10 17:13:22 -0700729 art::Class::VTableOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700730 irb_.getJObjectTy(),
731 kTBAAConstJObject);
732
733 // Load callee method object
734 llvm::Value* vtable_idx_value =
735 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
736
737 llvm::Value* method_field_addr =
738 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
739
740 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
741}
742
743// Emit Array GetElementPtr
744llvm::Value* GBCExpanderPass::EmitArrayGEP(llvm::Value* array_addr,
745 llvm::Value* index_value,
746 JType elem_jty) {
747
748 int data_offset;
749 if (elem_jty == kLong || elem_jty == kDouble ||
TDYa127920be7c2012-09-10 17:13:22 -0700750 (elem_jty == kObject && sizeof(uint64_t) == sizeof(art::Object*))) {
751 data_offset = art::Array::DataOffset(sizeof(int64_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700752 } else {
TDYa127920be7c2012-09-10 17:13:22 -0700753 data_offset = art::Array::DataOffset(sizeof(int32_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700754 }
755
756 llvm::Constant* data_offset_value =
757 irb_.getPtrEquivInt(data_offset);
758
759 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
760
761 llvm::Value* array_data_addr =
762 irb_.CreatePtrDisp(array_addr, data_offset_value,
763 elem_type->getPointerTo());
764
765 return irb_.CreateGEP(array_data_addr, index_value);
766}
767
768void GBCExpanderPass::Expand_TestSuspend(llvm::CallInst& call_inst) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700769 irb_.Runtime().EmitTestSuspend();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700770 return;
771}
772
TDYa1279a129452012-07-19 03:10:08 -0700773void GBCExpanderPass::Expand_MarkGCCard(llvm::CallInst& call_inst) {
TDYa1279a129452012-07-19 03:10:08 -0700774 irb_.Runtime().EmitMarkGCCard(call_inst.getArgOperand(0), call_inst.getArgOperand(1));
TDYa1279a129452012-07-19 03:10:08 -0700775 return;
776}
777
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700778llvm::Value* GBCExpanderPass::Expand_GetException() {
779 // Get thread-local exception field address
780 llvm::Value* exception_object_addr =
TDYa127920be7c2012-09-10 17:13:22 -0700781 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::ExceptionOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700782 irb_.getJObjectTy(),
783 kTBAAJRuntime);
784
785 // Set thread-local exception field address to NULL
TDYa127920be7c2012-09-10 17:13:22 -0700786 irb_.Runtime().EmitStoreToThreadOffset(art::Thread::ExceptionOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700787 irb_.getJNull(),
788 kTBAAJRuntime);
789
790 return exception_object_addr;
791}
792
793llvm::Value*
794GBCExpanderPass::Expand_LoadStringFromDexCache(llvm::Value* string_idx_value) {
795 uint32_t string_idx =
796 llvm::cast<llvm::ConstantInt>(string_idx_value)->getZExtValue();
797
798 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
799
800 return irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
801}
802
803llvm::Value*
804GBCExpanderPass::Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value) {
805 uint32_t type_idx =
806 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
807
808 llvm::Value* type_field_addr =
809 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
810
811 return irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
812}
813
814void GBCExpanderPass::Expand_LockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700815 rtb_.EmitLockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700816 return;
817}
818
819void GBCExpanderPass::Expand_UnlockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700820 rtb_.EmitUnlockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700821 return;
822}
823
824llvm::Value* GBCExpanderPass::Expand_ArrayGet(llvm::Value* array_addr,
825 llvm::Value* index_value,
826 JType elem_jty) {
827 llvm::Value* array_elem_addr =
828 EmitArrayGEP(array_addr, index_value, elem_jty);
829
830 return irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
831}
832
833void GBCExpanderPass::Expand_ArrayPut(llvm::Value* new_value,
834 llvm::Value* array_addr,
835 llvm::Value* index_value,
836 JType elem_jty) {
837 llvm::Value* array_elem_addr =
838 EmitArrayGEP(array_addr, index_value, elem_jty);
839
840 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
841
842 return;
843}
844
845void GBCExpanderPass::Expand_FilledNewArray(llvm::CallInst& call_inst) {
846 // Most of the codes refer to MethodCompiler::EmitInsn_FilledNewArray
847 llvm::Value* array = call_inst.getArgOperand(0);
848
849 uint32_t element_jty =
850 llvm::cast<llvm::ConstantInt>(call_inst.getArgOperand(1))->getZExtValue();
851
852 DCHECK(call_inst.getNumArgOperands() > 2);
853 unsigned num_elements = (call_inst.getNumArgOperands() - 2);
854
855 bool is_elem_int_ty = (static_cast<JType>(element_jty) == kInt);
856
857 uint32_t alignment;
858 llvm::Constant* elem_size;
859 llvm::PointerType* field_type;
860
861 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
862 // as the element, thus we are only checking 2 cases: primitive int and
863 // non-primitive type.
864 if (is_elem_int_ty) {
865 alignment = sizeof(int32_t);
866 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
867 field_type = irb_.getJIntTy()->getPointerTo();
868 } else {
869 alignment = irb_.getSizeOfPtrEquivInt();
870 elem_size = irb_.getSizeOfPtrEquivIntValue();
871 field_type = irb_.getJObjectTy()->getPointerTo();
872 }
873
874 llvm::Value* data_field_offset =
TDYa127920be7c2012-09-10 17:13:22 -0700875 irb_.getPtrEquivInt(art::Array::DataOffset(alignment).Int32Value());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700876
877 llvm::Value* data_field_addr =
878 irb_.CreatePtrDisp(array, data_field_offset, field_type);
879
880 for (unsigned i = 0; i < num_elements; ++i) {
881 // Values to fill the array begin at the 3rd argument
882 llvm::Value* reg_value = call_inst.getArgOperand(2 + i);
883
884 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
885
886 data_field_addr =
887 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
888 }
889
890 return;
891}
892
893llvm::Value* GBCExpanderPass::Expand_IGetFast(llvm::Value* field_offset_value,
894 llvm::Value* /*is_volatile_value*/,
895 llvm::Value* object_addr,
896 JType field_jty) {
897 int field_offset =
898 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
899
900 DCHECK_GE(field_offset, 0);
901
902 llvm::PointerType* field_type =
903 irb_.getJType(field_jty, kField)->getPointerTo();
904
905 field_offset_value = irb_.getPtrEquivInt(field_offset);
906
907 llvm::Value* field_addr =
908 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
909
910 // TODO: Check is_volatile. We need to generate atomic load instruction
911 // when is_volatile is true.
912 return irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
913}
914
915void GBCExpanderPass::Expand_IPutFast(llvm::Value* field_offset_value,
916 llvm::Value* /* is_volatile_value */,
917 llvm::Value* object_addr,
918 llvm::Value* new_value,
919 JType field_jty) {
920 int field_offset =
921 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
922
923 DCHECK_GE(field_offset, 0);
924
925 llvm::PointerType* field_type =
926 irb_.getJType(field_jty, kField)->getPointerTo();
927
928 field_offset_value = irb_.getPtrEquivInt(field_offset);
929
930 llvm::Value* field_addr =
931 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
932
933 // TODO: Check is_volatile. We need to generate atomic store instruction
934 // when is_volatile is true.
935 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
936
937 return;
938}
939
940llvm::Value* GBCExpanderPass::Expand_SGetFast(llvm::Value* static_storage_addr,
941 llvm::Value* field_offset_value,
942 llvm::Value* /*is_volatile_value*/,
943 JType field_jty) {
944 int field_offset =
945 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
946
947 DCHECK_GE(field_offset, 0);
948
949 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
950
951 llvm::Value* static_field_addr =
952 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
953 irb_.getJType(field_jty, kField)->getPointerTo());
954
955 // TODO: Check is_volatile. We need to generate atomic store instruction
956 // when is_volatile is true.
957 return irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
958}
959
960void GBCExpanderPass::Expand_SPutFast(llvm::Value* static_storage_addr,
961 llvm::Value* field_offset_value,
962 llvm::Value* /* is_volatile_value */,
963 llvm::Value* new_value,
964 JType field_jty) {
965 int field_offset =
966 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
967
968 DCHECK_GE(field_offset, 0);
969
970 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
971
972 llvm::Value* static_field_addr =
973 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
974 irb_.getJType(field_jty, kField)->getPointerTo());
975
976 // TODO: Check is_volatile. We need to generate atomic store instruction
977 // when is_volatile is true.
978 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
979
980 return;
981}
982
983llvm::Value*
984GBCExpanderPass::Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr) {
985 return irb_.LoadFromObjectOffset(method_object_addr,
TDYa127920be7c2012-09-10 17:13:22 -0700986 art::Method::DeclaringClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700987 irb_.getJObjectTy(),
988 kTBAAConstJObject);
989}
990
991llvm::Value*
992GBCExpanderPass::Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value) {
993 uint32_t type_idx =
994 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
995
996 llvm::Value* storage_field_addr =
997 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
998
999 return irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
1000}
1001
1002llvm::Value*
1003GBCExpanderPass::Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value) {
1004 uint32_t callee_method_idx =
1005 llvm::cast<llvm::ConstantInt>(callee_method_idx_value)->getZExtValue();
1006
1007 return EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
1008}
1009
1010llvm::Value* GBCExpanderPass::Expand_GetVirtualCalleeMethodObjAddrFast(
1011 llvm::Value* vtable_idx_value,
1012 llvm::Value* this_addr) {
1013 int vtable_idx =
1014 llvm::cast<llvm::ConstantInt>(vtable_idx_value)->getSExtValue();
1015
1016 return EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
1017}
1018
1019llvm::Value* GBCExpanderPass::Expand_Invoke(llvm::CallInst& call_inst) {
1020 // Most of the codes refer to MethodCompiler::EmitInsn_Invoke
1021 llvm::Value* callee_method_object_addr = call_inst.getArgOperand(0);
1022 unsigned num_args = call_inst.getNumArgOperands();
1023 llvm::Type* ret_type = call_inst.getType();
1024
1025 // Determine the function type of the callee method
1026 std::vector<llvm::Type*> args_type;
1027 std::vector<llvm::Value*> args;
1028 for (unsigned i = 0; i < num_args; i++) {
1029 args.push_back(call_inst.getArgOperand(i));
1030 args_type.push_back(args[i]->getType());
1031 }
1032
1033 llvm::FunctionType* callee_method_type =
1034 llvm::FunctionType::get(ret_type, args_type, false);
1035
1036 llvm::Value* code_addr =
1037 irb_.LoadFromObjectOffset(callee_method_object_addr,
TDYa127920be7c2012-09-10 17:13:22 -07001038 art::Method::GetCodeOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001039 callee_method_type->getPointerTo(),
1040 kTBAAJRuntime);
1041
1042 // Invoke callee
1043 llvm::Value* retval = irb_.CreateCall(code_addr, args);
1044
1045 return retval;
1046}
1047
TDYa1274ec8ccd2012-08-11 07:04:57 -07001048llvm::Value* GBCExpanderPass::Expand_DivRem(llvm::CallInst& call_inst,
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001049 bool is_div, JType op_jty) {
TDYa1274ec8ccd2012-08-11 07:04:57 -07001050 llvm::Value* dividend = call_inst.getArgOperand(0);
1051 llvm::Value* divisor = call_inst.getArgOperand(1);
1052#if defined(ART_USE_QUICK_COMPILER)
1053 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1054 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
1055#endif
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001056 // Most of the codes refer to MethodCompiler::EmitIntDivRemResultComputation
1057
1058 // Check the special case: MININT / -1 = MININT
1059 // That case will cause overflow, which is undefined behavior in llvm.
1060 // So we check the divisor is -1 or not, if the divisor is -1, we do
1061 // the special path to avoid undefined behavior.
1062 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
1063 llvm::Value* zero = irb_.getJZero(op_jty);
1064 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
1065
TDYa1275e869b62012-07-25 00:45:39 -07001066 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001067 llvm::BasicBlock* eq_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1068 llvm::BasicBlock* ne_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1069 llvm::BasicBlock* neg_one_cont =
1070 llvm::BasicBlock::Create(context_, "", parent);
1071
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001072 llvm::Value* is_equal_neg_one = irb_.CreateICmpEQ(divisor, neg_one);
1073 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
1074
1075 // If divisor == -1
1076 irb_.SetInsertPoint(eq_neg_one);
1077 llvm::Value* eq_result;
1078 if (is_div) {
1079 // We can just change from "dividend div -1" to "neg dividend". The sub
1080 // don't care the sign/unsigned because of two's complement representation.
1081 // And the behavior is what we want:
1082 // -(2^n) (2^n)-1
1083 // MININT < k <= MAXINT -> mul k -1 = -k
1084 // MININT == k -> mul k -1 = k
1085 //
1086 // LLVM use sub to represent 'neg'
1087 eq_result = irb_.CreateSub(zero, dividend);
1088 } else {
1089 // Everything modulo -1 will be 0.
1090 eq_result = zero;
1091 }
1092 irb_.CreateBr(neg_one_cont);
1093
1094 // If divisor != -1, just do the division.
1095 irb_.SetInsertPoint(ne_neg_one);
1096 llvm::Value* ne_result;
1097 if (is_div) {
1098 ne_result = irb_.CreateSDiv(dividend, divisor);
1099 } else {
1100 ne_result = irb_.CreateSRem(dividend, divisor);
1101 }
1102 irb_.CreateBr(neg_one_cont);
1103
1104 irb_.SetInsertPoint(neg_one_cont);
1105 llvm::PHINode* result = irb_.CreatePHI(op_type, 2);
1106 result->addIncoming(eq_result, eq_neg_one);
1107 result->addIncoming(ne_result, ne_neg_one);
1108
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001109 return result;
1110}
1111
1112void GBCExpanderPass::Expand_AllocaShadowFrame(llvm::Value* num_entry_value) {
1113 // Most of the codes refer to MethodCompiler::EmitPrologueAllocShadowFrame and
1114 // MethodCompiler::EmitPushShadowFrame
1115 shadow_frame_size_ =
1116 llvm::cast<llvm::ConstantInt>(num_entry_value)->getZExtValue();
1117
1118 llvm::StructType* shadow_frame_type =
1119 irb_.getShadowFrameTy(shadow_frame_size_);
1120
1121 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
1122
1123 // Alloca a pointer to old shadow frame
1124 old_shadow_frame_ =
1125 irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
1126
1127 // Zero-initialization of the shadow frame table
1128 llvm::Value* shadow_frame_table =
1129 irb_.CreateConstGEP2_32(shadow_frame_, 0, 1);
1130 llvm::Type* table_type = shadow_frame_type->getElementType(1);
1131
1132 llvm::ConstantAggregateZero* zero_initializer =
1133 llvm::ConstantAggregateZero::get(table_type);
1134
1135 irb_.CreateStore(zero_initializer, shadow_frame_table, kTBAAShadowFrame);
1136
1137 // Push the shadow frame
1138 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1139
1140 // Push the shadow frame
1141 llvm::Value* shadow_frame_upcast =
1142 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
1143
1144 llvm::Value* result = rtb_.EmitPushShadowFrame(shadow_frame_upcast,
1145 method_object_addr,
1146 shadow_frame_size_);
1147
1148 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
1149
1150 return;
1151}
1152
1153void GBCExpanderPass::Expand_SetShadowFrameEntry(llvm::Value* obj,
1154 llvm::Value* entry_idx) {
1155 DCHECK(shadow_frame_ != NULL);
1156
1157 llvm::Value* gep_index[] = {
1158 irb_.getInt32(0), // No pointer displacement
1159 irb_.getInt32(1), // SIRT
1160 entry_idx // Pointer field
1161 };
1162
1163 llvm::Value* entry_addr = irb_.CreateGEP(shadow_frame_, gep_index);
TDYa127347166a2012-08-23 12:23:44 -07001164#if defined(ART_USE_QUICK_COMPILER)
1165 if (obj->getType() != irb_.getJObjectTy()) {
1166 obj = irb_.getJNull();
1167 }
1168#endif
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001169 irb_.CreateStore(obj, entry_addr, kTBAAShadowFrame);
1170 return;
1171}
1172
1173void GBCExpanderPass::Expand_PopShadowFrame() {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001174#if defined(ART_USE_QUICK_COMPILER)
1175 if (old_shadow_frame_ == NULL) {
1176 return;
1177 }
1178#endif
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001179 rtb_.EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
1180 return;
1181}
1182
1183void GBCExpanderPass::Expand_UpdateDexPC(llvm::Value* dex_pc_value) {
1184 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07001185 art::ShadowFrame::DexPCOffset(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001186 dex_pc_value,
1187 kTBAAShadowFrame);
1188 return;
1189}
1190
Logan Chien67645d82012-08-17 09:10:54 +08001191void GBCExpanderPass::InsertStackOverflowCheck(llvm::Function& func) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001192 // DexLang generates all alloca instruction in the first basic block of the
1193 // FUNC and also there's no any alloca instructions after the first non-alloca
1194 // instruction
1195
Logan Chien67645d82012-08-17 09:10:54 +08001196 llvm::BasicBlock* first_basic_block = &func.front();
1197
1198 // Look for first non-alloca instruction
1199 llvm::BasicBlock::iterator first_non_alloca = first_basic_block->begin();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001200 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1201 ++first_non_alloca;
1202 }
1203
Logan Chien67645d82012-08-17 09:10:54 +08001204 irb_.SetInsertPoint(first_non_alloca);
1205
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001206 // Insert stack overflow check codes before first_non_alloca (i.e., after all
1207 // alloca instructions)
Logan Chien67645d82012-08-17 09:10:54 +08001208 EmitStackOverflowCheck(&*first_non_alloca);
1209
TDYa127890ea892012-08-22 10:49:42 -07001210#if defined(ART_USE_QUICK_COMPILER)
1211 irb_.Runtime().EmitTestSuspend();
1212#endif
1213
Logan Chien67645d82012-08-17 09:10:54 +08001214 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
1215 if (next_basic_block != first_basic_block) {
1216 // Splice the rest of the instruction to the continuing basic block
1217 next_basic_block->getInstList().splice(
1218 irb_.GetInsertPoint(), first_basic_block->getInstList(),
1219 first_non_alloca, first_basic_block->end());
1220
1221 // Rewrite the basic block
1222 RewriteBasicBlock(next_basic_block);
1223
1224 // Update the phi-instructions in the successor basic block
1225 UpdatePhiInstruction(first_basic_block, irb_.GetInsertBlock());
1226 }
1227
1228 // We have changed the basic block
1229 changed_ = true;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001230}
1231
TDYa1275e869b62012-07-25 00:45:39 -07001232// ==== High-level intrinsic expander ==========================================
1233
TDYa127a1b21852012-07-23 03:20:39 -07001234llvm::Value* GBCExpanderPass::Expand_FPCompare(llvm::Value* src1_value,
1235 llvm::Value* src2_value,
1236 bool gt_bias) {
1237 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1238 llvm::Value* cmp_lt;
1239
1240 if (gt_bias) {
1241 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1242 } else {
1243 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1244 }
1245
1246 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1247}
1248
1249llvm::Value* GBCExpanderPass::Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value) {
1250 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1251 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1252
1253 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1254}
1255
1256llvm::Value* GBCExpanderPass::EmitCompareResultSelection(llvm::Value* cmp_eq,
1257 llvm::Value* cmp_lt) {
1258
1259 llvm::Constant* zero = irb_.getJInt(0);
1260 llvm::Constant* pos1 = irb_.getJInt(1);
1261 llvm::Constant* neg1 = irb_.getJInt(-1);
1262
1263 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1264 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1265
1266 return result_eq;
1267}
1268
Logan Chien75e4b602012-07-23 14:24:12 -07001269llvm::Value* GBCExpanderPass::Expand_IntegerShift(llvm::Value* src1_value,
1270 llvm::Value* src2_value,
1271 IntegerShiftKind kind,
1272 JType op_jty) {
1273 DCHECK(op_jty == kInt || op_jty == kLong);
1274
1275 // Mask and zero-extend RHS properly
1276 if (op_jty == kInt) {
1277 src2_value = irb_.CreateAnd(src2_value, 0x1f);
1278 } else {
1279 llvm::Value* masked_src2_value = irb_.CreateAnd(src2_value, 0x3f);
1280 src2_value = irb_.CreateZExt(masked_src2_value, irb_.getJLongTy());
1281 }
1282
1283 // Create integer shift llvm instruction
1284 switch (kind) {
1285 case kIntegerSHL:
1286 return irb_.CreateShl(src1_value, src2_value);
1287
1288 case kIntegerSHR:
1289 return irb_.CreateAShr(src1_value, src2_value);
1290
1291 case kIntegerUSHR:
1292 return irb_.CreateLShr(src1_value, src2_value);
1293
1294 default:
1295 LOG(FATAL) << "Unknown integer shift kind: " << kind;
1296 return NULL;
1297 }
1298}
1299
TDYa1275a26d442012-07-26 18:58:38 -07001300llvm::Value* GBCExpanderPass::Expand_HLArrayGet(llvm::CallInst& call_inst,
1301 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001302 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1303 llvm::Value* array_addr = call_inst.getArgOperand(1);
1304 llvm::Value* index_value = call_inst.getArgOperand(2);
TDYa127920be7c2012-09-10 17:13:22 -07001305 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001306
TDYa127920be7c2012-09-10 17:13:22 -07001307 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1308 EmitGuard_NullPointerException(dex_pc, array_addr);
1309 }
1310 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
1311 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value);
1312 }
TDYa1275a26d442012-07-26 18:58:38 -07001313
1314 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1315
1316 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
1317
1318 switch (elem_jty) {
1319 case kVoid:
1320 break;
1321
1322 case kBoolean:
1323 case kChar:
1324 array_elem_value = irb_.CreateZExt(array_elem_value, irb_.getJType(elem_jty, kReg));
1325 break;
1326
1327 case kByte:
1328 case kShort:
1329 array_elem_value = irb_.CreateSExt(array_elem_value, irb_.getJType(elem_jty, kReg));
1330 break;
1331
1332 case kInt:
1333 case kLong:
1334 case kFloat:
1335 case kDouble:
1336 case kObject:
1337 break;
1338
1339 default:
1340 LOG(FATAL) << "Unknown java type: " << elem_jty;
1341 }
1342
1343 return array_elem_value;
1344}
1345
1346
1347void GBCExpanderPass::Expand_HLArrayPut(llvm::CallInst& call_inst,
1348 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001349 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1350 llvm::Value* new_value = call_inst.getArgOperand(1);
1351 llvm::Value* array_addr = call_inst.getArgOperand(2);
1352 llvm::Value* index_value = call_inst.getArgOperand(3);
TDYa127920be7c2012-09-10 17:13:22 -07001353 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001354
TDYa127920be7c2012-09-10 17:13:22 -07001355 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1356 EmitGuard_NullPointerException(dex_pc, array_addr);
1357 }
1358 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
1359 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value);
1360 }
TDYa1275a26d442012-07-26 18:58:38 -07001361
1362 switch (elem_jty) {
1363 case kVoid:
1364 break;
1365
1366 case kBoolean:
1367 case kChar:
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001368 case kByte:
1369 case kShort:
TDYa1275a26d442012-07-26 18:58:38 -07001370 new_value = irb_.CreateTrunc(new_value, irb_.getJType(elem_jty, kArray));
1371 break;
1372
1373 case kInt:
1374 case kLong:
1375 case kFloat:
1376 case kDouble:
1377 case kObject:
1378 break;
1379
1380 default:
1381 LOG(FATAL) << "Unknown java type: " << elem_jty;
1382 }
1383
1384 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1385
1386 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
1387 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::CheckPutArrayElement);
1388
1389 irb_.CreateCall2(runtime_func, new_value, array_addr);
1390
1391 EmitGuard_ExceptionLandingPad(dex_pc);
1392
1393 EmitMarkGCCard(new_value, array_addr);
1394 }
1395
1396 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1397
1398 return;
1399}
1400
TDYa1275e869b62012-07-25 00:45:39 -07001401llvm::Value* GBCExpanderPass::Expand_HLIGet(llvm::CallInst& call_inst,
1402 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001403 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1404 llvm::Value* object_addr = call_inst.getArgOperand(1);
1405 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(2));
TDYa127920be7c2012-09-10 17:13:22 -07001406 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001407
TDYa127920be7c2012-09-10 17:13:22 -07001408 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1409 EmitGuard_NullPointerException(dex_pc, object_addr);
1410 }
TDYa1275e869b62012-07-25 00:45:39 -07001411
1412 llvm::Value* field_value;
1413
1414 int field_offset;
1415 bool is_volatile;
1416 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
1417 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
1418
1419 if (!is_fast_path) {
1420 llvm::Function* runtime_func;
1421
1422 if (field_jty == kObject) {
1423 runtime_func = irb_.GetRuntime(runtime_support::GetObjectInstance);
1424 } else if (field_jty == kLong || field_jty == kDouble) {
1425 runtime_func = irb_.GetRuntime(runtime_support::Get64Instance);
1426 } else {
1427 runtime_func = irb_.GetRuntime(runtime_support::Get32Instance);
1428 }
1429
1430 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
1431
1432 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1433
1434 EmitUpdateDexPC(dex_pc);
1435
1436 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
1437 method_object_addr, object_addr);
1438
1439 EmitGuard_ExceptionLandingPad(dex_pc);
1440
1441 } else {
1442 DCHECK_GE(field_offset, 0);
1443
1444 llvm::PointerType* field_type =
1445 irb_.getJType(field_jty, kField)->getPointerTo();
1446
1447 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
1448
1449 llvm::Value* field_addr =
1450 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1451
1452 // TODO: Check is_volatile. We need to generate atomic load instruction
1453 // when is_volatile is true.
1454 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
1455 }
1456
1457 if (field_jty == kFloat || field_jty == kDouble) {
TDYa1275a26d442012-07-26 18:58:38 -07001458 field_value = irb_.CreateBitCast(field_value, irb_.getJType(field_jty, kAccurate));
TDYa1275e869b62012-07-25 00:45:39 -07001459 }
1460
1461 return field_value;
1462}
1463
1464void GBCExpanderPass::Expand_HLIPut(llvm::CallInst& call_inst,
1465 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001466 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001467 llvm::Value* new_value = call_inst.getArgOperand(1);
1468 llvm::Value* object_addr = call_inst.getArgOperand(2);
TDYa1275e869b62012-07-25 00:45:39 -07001469 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(3));
TDYa127920be7c2012-09-10 17:13:22 -07001470 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001471
1472 if (field_jty == kFloat || field_jty == kDouble) {
TDYa1275a26d442012-07-26 18:58:38 -07001473 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty, kField));
TDYa1275e869b62012-07-25 00:45:39 -07001474 }
1475
TDYa127920be7c2012-09-10 17:13:22 -07001476 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1477 EmitGuard_NullPointerException(dex_pc, object_addr);
1478 }
TDYa1275e869b62012-07-25 00:45:39 -07001479
1480 int field_offset;
1481 bool is_volatile;
1482 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
1483 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
1484
1485 if (!is_fast_path) {
1486 llvm::Function* runtime_func;
1487
1488 if (field_jty == kObject) {
1489 runtime_func = irb_.GetRuntime(runtime_support::SetObjectInstance);
1490 } else if (field_jty == kLong || field_jty == kDouble) {
1491 runtime_func = irb_.GetRuntime(runtime_support::Set64Instance);
1492 } else {
1493 runtime_func = irb_.GetRuntime(runtime_support::Set32Instance);
1494 }
1495
1496 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1497
1498 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1499
1500 EmitUpdateDexPC(dex_pc);
1501
1502 irb_.CreateCall4(runtime_func, field_idx_value,
1503 method_object_addr, object_addr, new_value);
1504
1505 EmitGuard_ExceptionLandingPad(dex_pc);
1506
1507 } else {
1508 DCHECK_GE(field_offset, 0);
1509
1510 llvm::PointerType* field_type =
1511 irb_.getJType(field_jty, kField)->getPointerTo();
1512
1513 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
1514
1515 llvm::Value* field_addr =
1516 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1517
1518 // TODO: Check is_volatile. We need to generate atomic store instruction
1519 // when is_volatile is true.
1520 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1521
1522 if (field_jty == kObject) { // If put an object, mark the GC card table.
1523 EmitMarkGCCard(new_value, object_addr);
1524 }
1525 }
1526
1527 return;
1528}
1529
TDYa127f71bf5a2012-07-29 20:09:52 -07001530llvm::Value* GBCExpanderPass::EmitLoadConstantClass(uint32_t dex_pc,
1531 uint32_t type_idx) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001532 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, *dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001533 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1534
1535 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1536
1537 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1538
1539 llvm::Function* runtime_func =
1540 irb_.GetRuntime(runtime_support::InitializeTypeAndVerifyAccess);
1541
1542 EmitUpdateDexPC(dex_pc);
1543
1544 llvm::Value* type_object_addr =
1545 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1546
1547 EmitGuard_ExceptionLandingPad(dex_pc);
1548
1549 return type_object_addr;
1550
1551 } else {
1552 // Try to load the class (type) object from the test cache.
1553 llvm::Value* type_field_addr =
1554 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1555
1556 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
1557
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001558 if (compiler_->CanAssumeTypeIsPresentInDexCache(*dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001559 return type_object_addr;
1560 }
1561
1562 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1563
1564 // Test whether class (type) object is in the dex cache or not
1565 llvm::Value* equal_null =
1566 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1567
1568 llvm::BasicBlock* block_cont =
1569 CreateBasicBlockWithDexPC(dex_pc, "cont");
1570
1571 llvm::BasicBlock* block_load_class =
1572 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1573
1574 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
1575
1576 // Failback routine to load the class object
1577 irb_.SetInsertPoint(block_load_class);
1578
1579 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeType);
1580
1581 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1582
1583 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1584
1585 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1586
1587 EmitUpdateDexPC(dex_pc);
1588
1589 llvm::Value* loaded_type_object_addr =
1590 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1591
1592 EmitGuard_ExceptionLandingPad(dex_pc);
1593
1594 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1595
1596 irb_.CreateBr(block_cont);
1597
1598 // Now the class object must be loaded
1599 irb_.SetInsertPoint(block_cont);
1600
1601 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1602
1603 phi->addIncoming(type_object_addr, block_original);
1604 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1605
1606 return phi;
1607 }
1608}
1609
TDYa1275a26d442012-07-26 18:58:38 -07001610llvm::Value* GBCExpanderPass::EmitLoadStaticStorage(uint32_t dex_pc,
1611 uint32_t type_idx) {
1612 llvm::BasicBlock* block_load_static =
1613 CreateBasicBlockWithDexPC(dex_pc, "load_static");
1614
1615 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1616
1617 // Load static storage from dex cache
1618 llvm::Value* storage_field_addr =
1619 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1620
1621 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
1622
1623 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1624
1625 // Test: Is the static storage of this class initialized?
1626 llvm::Value* equal_null =
1627 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
1628
1629 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
1630
1631 // Failback routine to load the class object
1632 irb_.SetInsertPoint(block_load_static);
1633
1634 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeStaticStorage);
1635
1636 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1637
1638 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1639
1640 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1641
1642 EmitUpdateDexPC(dex_pc);
1643
1644 llvm::Value* loaded_storage_object_addr =
1645 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1646
1647 EmitGuard_ExceptionLandingPad(dex_pc);
1648
1649 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
1650
1651 irb_.CreateBr(block_cont);
1652
1653 // Now the class object must be loaded
1654 irb_.SetInsertPoint(block_cont);
1655
1656 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1657
1658 phi->addIncoming(storage_object_addr, block_original);
1659 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
1660
1661 return phi;
1662}
1663
1664llvm::Value* GBCExpanderPass::Expand_HLSget(llvm::CallInst& call_inst,
1665 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001666 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1667 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1668
1669 int field_offset;
1670 int ssb_index;
1671 bool is_referrers_class;
1672 bool is_volatile;
1673
1674 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
1675 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1676 is_referrers_class, is_volatile, false);
1677
1678 llvm::Value* static_field_value;
1679
1680 if (!is_fast_path) {
1681 llvm::Function* runtime_func;
1682
1683 if (field_jty == kObject) {
1684 runtime_func = irb_.GetRuntime(runtime_support::GetObjectStatic);
1685 } else if (field_jty == kLong || field_jty == kDouble) {
1686 runtime_func = irb_.GetRuntime(runtime_support::Get64Static);
1687 } else {
1688 runtime_func = irb_.GetRuntime(runtime_support::Get32Static);
1689 }
1690
1691 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1692
1693 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1694
1695 EmitUpdateDexPC(dex_pc);
1696
1697 static_field_value =
1698 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
1699
1700 EmitGuard_ExceptionLandingPad(dex_pc);
1701
1702 } else {
1703 DCHECK_GE(field_offset, 0);
1704
1705 llvm::Value* static_storage_addr = NULL;
1706
1707 if (is_referrers_class) {
1708 // Fast path, static storage base is this method's class
1709 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1710
1711 static_storage_addr =
1712 irb_.LoadFromObjectOffset(method_object_addr,
TDYa127920be7c2012-09-10 17:13:22 -07001713 art::Method::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001714 irb_.getJObjectTy(),
1715 kTBAAConstJObject);
1716 } else {
1717 // Medium path, static storage base in a different class which
1718 // requires checks that the other class is initialized
1719 DCHECK_GE(ssb_index, 0);
1720 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1721 }
1722
1723 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1724
1725 llvm::Value* static_field_addr =
1726 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1727 irb_.getJType(field_jty, kField)->getPointerTo());
1728
1729 // TODO: Check is_volatile. We need to generate atomic load instruction
1730 // when is_volatile is true.
1731 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
1732 }
1733
1734 if (field_jty == kFloat || field_jty == kDouble) {
1735 static_field_value =
1736 irb_.CreateBitCast(static_field_value, irb_.getJType(field_jty, kAccurate));
1737 }
1738
1739 return static_field_value;
1740}
1741
1742void GBCExpanderPass::Expand_HLSput(llvm::CallInst& call_inst,
1743 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001744 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1745 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1746 llvm::Value* new_value = call_inst.getArgOperand(1);
1747
1748 if (field_jty == kFloat || field_jty == kDouble) {
1749 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty, kField));
1750 }
1751
1752 int field_offset;
1753 int ssb_index;
1754 bool is_referrers_class;
1755 bool is_volatile;
1756
1757 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
1758 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1759 is_referrers_class, is_volatile, true);
1760
1761 if (!is_fast_path) {
1762 llvm::Function* runtime_func;
1763
1764 if (field_jty == kObject) {
1765 runtime_func = irb_.GetRuntime(runtime_support::SetObjectStatic);
1766 } else if (field_jty == kLong || field_jty == kDouble) {
1767 runtime_func = irb_.GetRuntime(runtime_support::Set64Static);
1768 } else {
1769 runtime_func = irb_.GetRuntime(runtime_support::Set32Static);
1770 }
1771
1772 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1773
1774 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1775
1776 EmitUpdateDexPC(dex_pc);
1777
1778 irb_.CreateCall3(runtime_func, field_idx_value,
1779 method_object_addr, new_value);
1780
1781 EmitGuard_ExceptionLandingPad(dex_pc);
1782
1783 } else {
1784 DCHECK_GE(field_offset, 0);
1785
1786 llvm::Value* static_storage_addr = NULL;
1787
1788 if (is_referrers_class) {
1789 // Fast path, static storage base is this method's class
1790 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1791
1792 static_storage_addr =
1793 irb_.LoadFromObjectOffset(method_object_addr,
TDYa127920be7c2012-09-10 17:13:22 -07001794 art::Method::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001795 irb_.getJObjectTy(),
1796 kTBAAConstJObject);
1797 } else {
1798 // Medium path, static storage base in a different class which
1799 // requires checks that the other class is initialized
1800 DCHECK_GE(ssb_index, 0);
1801 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1802 }
1803
1804 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1805
1806 llvm::Value* static_field_addr =
1807 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
1808 irb_.getJType(field_jty, kField)->getPointerTo());
1809
1810 // TODO: Check is_volatile. We need to generate atomic store instruction
1811 // when is_volatile is true.
1812 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1813
1814 if (field_jty == kObject) { // If put an object, mark the GC card table.
1815 EmitMarkGCCard(new_value, static_storage_addr);
1816 }
1817 }
1818
1819 return;
1820}
1821
TDYa127f71bf5a2012-07-29 20:09:52 -07001822llvm::Value* GBCExpanderPass::Expand_ConstString(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001823 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1824 uint32_t string_idx = LV2UInt(call_inst.getArgOperand(0));
1825
1826 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1827
1828 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
1829
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001830 if (!compiler_->CanAssumeStringIsPresentInDexCache(*dex_file_, string_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001831 llvm::BasicBlock* block_str_exist =
1832 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1833
1834 llvm::BasicBlock* block_str_resolve =
1835 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1836
1837 llvm::BasicBlock* block_cont =
1838 CreateBasicBlockWithDexPC(dex_pc, "str_cont");
1839
1840 // Test: Is the string resolved and in the dex cache?
1841 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1842
1843 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
1844
1845 // String is resolved, go to next basic block.
1846 irb_.SetInsertPoint(block_str_exist);
1847 irb_.CreateBr(block_cont);
1848
1849 // String is not resolved yet, resolve it now.
1850 irb_.SetInsertPoint(block_str_resolve);
1851
1852 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::ResolveString);
1853
1854 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1855
1856 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1857
1858 EmitUpdateDexPC(dex_pc);
1859
1860 llvm::Value* result = irb_.CreateCall2(runtime_func, method_object_addr,
1861 string_idx_value);
1862
1863 EmitGuard_ExceptionLandingPad(dex_pc);
1864
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001865 irb_.CreateBr(block_cont);
1866
1867
TDYa127f71bf5a2012-07-29 20:09:52 -07001868 llvm::BasicBlock* block_pre_cont = irb_.GetInsertBlock();
1869
1870 irb_.SetInsertPoint(block_cont);
1871
1872 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1873
1874 phi->addIncoming(string_addr, block_str_exist);
1875 phi->addIncoming(result, block_pre_cont);
1876
1877 string_addr = phi;
1878 }
1879
1880 return string_addr;
1881}
1882
1883llvm::Value* GBCExpanderPass::Expand_ConstClass(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001884 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1885 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1886
1887 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
1888
1889 return type_object_addr;
1890}
1891
1892void GBCExpanderPass::Expand_MonitorEnter(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001893 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1894 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07001895 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07001896
TDYa127920be7c2012-09-10 17:13:22 -07001897 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1898 EmitGuard_NullPointerException(dex_pc, object_addr);
1899 }
TDYa127f71bf5a2012-07-29 20:09:52 -07001900
1901 irb_.Runtime().EmitLockObject(object_addr);
1902
1903 return;
1904}
1905
1906void GBCExpanderPass::Expand_MonitorExit(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001907 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1908 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07001909 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07001910
TDYa127920be7c2012-09-10 17:13:22 -07001911 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
1912 EmitGuard_NullPointerException(dex_pc, object_addr);
1913 }
TDYa127f71bf5a2012-07-29 20:09:52 -07001914
1915 EmitUpdateDexPC(dex_pc);
1916
1917 irb_.Runtime().EmitUnlockObject(object_addr);
1918
1919 EmitGuard_ExceptionLandingPad(dex_pc);
1920
1921 return;
1922}
1923
1924void GBCExpanderPass::Expand_HLCheckCast(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001925 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1926 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1927 llvm::Value* object_addr = call_inst.getArgOperand(1);
1928
1929 llvm::BasicBlock* block_test_class =
1930 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1931
1932 llvm::BasicBlock* block_test_sub_class =
1933 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1934
1935 llvm::BasicBlock* block_cont =
1936 CreateBasicBlockWithDexPC(dex_pc, "checkcast_cont");
1937
1938 // Test: Is the reference equal to null? Act as no-op when it is null.
1939 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1940
1941 irb_.CreateCondBr(equal_null,
1942 block_cont,
1943 block_test_class);
1944
1945 // Test: Is the object instantiated from the given class?
1946 irb_.SetInsertPoint(block_test_class);
1947 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
TDYa127920be7c2012-09-10 17:13:22 -07001948 DCHECK_EQ(art::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07001949
1950 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1951
1952 llvm::Value* object_type_field_addr =
1953 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1954
1955 llvm::Value* object_type_object_addr =
1956 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
1957
1958 llvm::Value* equal_class =
1959 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1960
1961 irb_.CreateCondBr(equal_class,
1962 block_cont,
1963 block_test_sub_class);
1964
1965 // Test: Is the object instantiated from the subclass of the given class?
1966 irb_.SetInsertPoint(block_test_sub_class);
1967
1968 EmitUpdateDexPC(dex_pc);
1969
1970 irb_.CreateCall2(irb_.GetRuntime(runtime_support::CheckCast),
1971 type_object_addr, object_type_object_addr);
1972
1973 EmitGuard_ExceptionLandingPad(dex_pc);
1974
1975 irb_.CreateBr(block_cont);
1976
1977 irb_.SetInsertPoint(block_cont);
1978
1979 return;
1980}
1981
1982llvm::Value* GBCExpanderPass::Expand_InstanceOf(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001983 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1984 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1985 llvm::Value* object_addr = call_inst.getArgOperand(1);
1986
1987 llvm::BasicBlock* block_nullp =
1988 CreateBasicBlockWithDexPC(dex_pc, "nullp");
1989
1990 llvm::BasicBlock* block_test_class =
1991 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1992
1993 llvm::BasicBlock* block_class_equals =
1994 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1995
1996 llvm::BasicBlock* block_test_sub_class =
1997 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1998
1999 llvm::BasicBlock* block_cont =
2000 CreateBasicBlockWithDexPC(dex_pc, "instance_of_cont");
2001
2002 // Overview of the following code :
2003 // We check for null, if so, then false, otherwise check for class == . If so
2004 // then true, otherwise do callout slowpath.
2005 //
2006 // Test: Is the reference equal to null? Set 0 when it is null.
2007 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2008
2009 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
2010
2011 irb_.SetInsertPoint(block_nullp);
2012 irb_.CreateBr(block_cont);
2013
2014 // Test: Is the object instantiated from the given class?
2015 irb_.SetInsertPoint(block_test_class);
2016 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
TDYa127920be7c2012-09-10 17:13:22 -07002017 DCHECK_EQ(art::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002018
2019 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2020
2021 llvm::Value* object_type_field_addr =
2022 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2023
2024 llvm::Value* object_type_object_addr =
2025 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2026
2027 llvm::Value* equal_class =
2028 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2029
2030 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
2031
2032 irb_.SetInsertPoint(block_class_equals);
2033 irb_.CreateBr(block_cont);
2034
2035 // Test: Is the object instantiated from the subclass of the given class?
2036 irb_.SetInsertPoint(block_test_sub_class);
2037 llvm::Value* result =
2038 irb_.CreateCall2(irb_.GetRuntime(runtime_support::IsAssignable),
2039 type_object_addr, object_type_object_addr);
2040 irb_.CreateBr(block_cont);
2041
2042 irb_.SetInsertPoint(block_cont);
2043
2044 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJIntTy(), 3);
2045
2046 phi->addIncoming(irb_.getJInt(0), block_nullp);
2047 phi->addIncoming(irb_.getJInt(1), block_class_equals);
2048 phi->addIncoming(result, block_test_sub_class);
2049
2050 return phi;
2051}
2052
2053llvm::Value* GBCExpanderPass::Expand_NewInstance(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002054 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2055 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2056
2057 llvm::Function* runtime_func;
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002058 if (compiler_->CanAccessInstantiableTypeWithoutChecks(method_idx_, *dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002059 runtime_func = irb_.GetRuntime(runtime_support::AllocObject);
2060 } else {
2061 runtime_func = irb_.GetRuntime(runtime_support::AllocObjectWithAccessCheck);
2062 }
2063
2064 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2065
2066 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2067
2068 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2069
2070 EmitUpdateDexPC(dex_pc);
2071
2072 llvm::Value* object_addr =
2073 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
2074
2075 EmitGuard_ExceptionLandingPad(dex_pc);
2076
2077 return object_addr;
2078}
2079
2080llvm::Value* GBCExpanderPass::Expand_HLInvoke(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002081 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
TDYa127920be7c2012-09-10 17:13:22 -07002082 art::InvokeType invoke_type = static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
2083 bool is_static = (invoke_type == art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002084 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
TDYa127920be7c2012-09-10 17:13:22 -07002085 int opt_flags = LV2UInt(call_inst.getArgOperand(2));
TDYa127f71bf5a2012-07-29 20:09:52 -07002086
2087 // Compute invoke related information for compiler decision
2088 int vtable_idx = -1;
2089 uintptr_t direct_code = 0;
2090 uintptr_t direct_method = 0;
2091 bool is_fast_path = compiler_->
2092 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
2093 invoke_type, vtable_idx, direct_code, direct_method);
2094
2095 // Load *this* actual parameter
2096 llvm::Value* this_addr = NULL;
2097
2098 if (!is_static) {
2099 // Test: Is *this* parameter equal to null?
2100 this_addr = call_inst.getArgOperand(3);
2101 }
2102
2103 // Load the method object
2104 llvm::Value* callee_method_object_addr = NULL;
2105
2106 if (!is_fast_path) {
2107 callee_method_object_addr =
2108 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2109 this_addr, dex_pc, is_fast_path);
2110
TDYa127920be7c2012-09-10 17:13:22 -07002111 if (!is_static && !(opt_flags & MIR_IGNORE_NULL_CHECK)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002112 EmitGuard_NullPointerException(dex_pc, this_addr);
2113 }
2114 } else {
TDYa127920be7c2012-09-10 17:13:22 -07002115 if (!is_static && !(opt_flags & MIR_IGNORE_NULL_CHECK)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002116 EmitGuard_NullPointerException(dex_pc, this_addr);
2117 }
2118
2119 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002120 case art::kStatic:
2121 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002122 if (direct_method != 0u &&
2123 direct_method != static_cast<uintptr_t>(-1)) {
2124 callee_method_object_addr =
2125 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2126 irb_.getJObjectTy());
2127 } else {
2128 callee_method_object_addr =
2129 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2130 }
2131 break;
2132
TDYa127920be7c2012-09-10 17:13:22 -07002133 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002134 DCHECK(vtable_idx != -1);
2135 callee_method_object_addr =
2136 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2137 break;
2138
TDYa127920be7c2012-09-10 17:13:22 -07002139 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002140 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2141 "the fast path.";
2142 break;
2143
TDYa127920be7c2012-09-10 17:13:22 -07002144 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002145 callee_method_object_addr =
2146 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2147 invoke_type, this_addr,
2148 dex_pc, is_fast_path);
2149 break;
2150 }
2151 }
2152
2153 // Load the actual parameter
2154 std::vector<llvm::Value*> args;
2155
2156 args.push_back(callee_method_object_addr); // method object for callee
2157
2158 for (uint32_t i = 3; i < call_inst.getNumArgOperands(); ++i) {
2159 args.push_back(call_inst.getArgOperand(i));
2160 }
2161
2162 llvm::Value* code_addr;
2163 if (direct_code != 0u &&
2164 direct_code != static_cast<uintptr_t>(-1)) {
2165 code_addr =
2166 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_code),
2167 GetFunctionType(callee_method_idx, is_static)->getPointerTo());
2168 } else {
2169 code_addr =
2170 irb_.LoadFromObjectOffset(callee_method_object_addr,
TDYa127920be7c2012-09-10 17:13:22 -07002171 art::Method::GetCodeOffset().Int32Value(),
TDYa127f71bf5a2012-07-29 20:09:52 -07002172 GetFunctionType(callee_method_idx, is_static)->getPointerTo(),
2173 kTBAAJRuntime);
2174 }
2175
2176 // Invoke callee
2177 EmitUpdateDexPC(dex_pc);
2178 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2179 EmitGuard_ExceptionLandingPad(dex_pc);
2180
2181 return retval;
2182}
2183
2184llvm::Value* GBCExpanderPass::Expand_OptArrayLength(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002185 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2186 // Get the array object address
2187 llvm::Value* array_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002188 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002189
TDYa127920be7c2012-09-10 17:13:22 -07002190 if (!(opt_flags & MIR_IGNORE_NULL_CHECK)) {
2191 EmitGuard_NullPointerException(dex_pc, array_addr);
2192 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002193
2194 // Get the array length and store it to the register
2195 return EmitLoadArrayLength(array_addr);
2196}
2197
2198llvm::Value* GBCExpanderPass::Expand_NewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002199 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2200 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2201 llvm::Value* length = call_inst.getArgOperand(1);
2202
2203 return EmitAllocNewArray(dex_pc, length, type_idx, false);
2204}
2205
2206llvm::Value* GBCExpanderPass::Expand_HLFilledNewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002207 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2208 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(1));
2209 uint32_t length = call_inst.getNumArgOperands() - 3;
2210
2211 llvm::Value* object_addr =
2212 EmitAllocNewArray(dex_pc, irb_.getInt32(length), type_idx, true);
2213
2214 if (length > 0) {
2215 // Check for the element type
2216 uint32_t type_desc_len = 0;
2217 const char* type_desc =
2218 dex_file_->StringByTypeIdx(type_idx, &type_desc_len);
2219
2220 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
2221 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
2222 bool is_elem_int_ty = (type_desc[1] == 'I');
2223
2224 uint32_t alignment;
2225 llvm::Constant* elem_size;
2226 llvm::PointerType* field_type;
2227
2228 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
2229 // as the element, thus we are only checking 2 cases: primitive int and
2230 // non-primitive type.
2231 if (is_elem_int_ty) {
2232 alignment = sizeof(int32_t);
2233 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
2234 field_type = irb_.getJIntTy()->getPointerTo();
2235 } else {
2236 alignment = irb_.getSizeOfPtrEquivInt();
2237 elem_size = irb_.getSizeOfPtrEquivIntValue();
2238 field_type = irb_.getJObjectTy()->getPointerTo();
2239 }
2240
2241 llvm::Value* data_field_offset =
TDYa127920be7c2012-09-10 17:13:22 -07002242 irb_.getPtrEquivInt(art::Array::DataOffset(alignment).Int32Value());
TDYa127f71bf5a2012-07-29 20:09:52 -07002243
2244 llvm::Value* data_field_addr =
2245 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
2246
2247 // TODO: Tune this code. Currently we are generating one instruction for
2248 // one element which may be very space consuming. Maybe changing to use
2249 // memcpy may help; however, since we can't guarantee that the alloca of
2250 // dalvik register are continuous, we can't perform such optimization yet.
2251 for (uint32_t i = 0; i < length; ++i) {
2252 llvm::Value* reg_value = call_inst.getArgOperand(i+3);
2253
2254 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
2255
2256 data_field_addr =
2257 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
2258 }
2259 }
2260
2261 return object_addr;
2262}
2263
2264void GBCExpanderPass::Expand_HLFillArrayData(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002265 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2266 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
2267 LV2SInt(call_inst.getArgOperand(0));
2268 llvm::Value* array_addr = call_inst.getArgOperand(1);
2269
TDYa127920be7c2012-09-10 17:13:22 -07002270 const art::Instruction::ArrayDataPayload* payload =
2271 reinterpret_cast<const art::Instruction::ArrayDataPayload*>(
TDYa127f71bf5a2012-07-29 20:09:52 -07002272 code_item_->insns_ + payload_offset);
2273
2274 if (payload->element_count == 0) {
2275 // When the number of the elements in the payload is zero, we don't have
2276 // to copy any numbers. However, we should check whether the array object
2277 // address is equal to null or not.
2278 EmitGuard_NullPointerException(dex_pc, array_addr);
2279 } else {
2280 // To save the code size, we are going to call the runtime function to
2281 // copy the content from DexFile.
2282
2283 // NOTE: We will check for the NullPointerException in the runtime.
2284
2285 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::FillArrayData);
2286
2287 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2288
2289 EmitUpdateDexPC(dex_pc);
2290
2291 irb_.CreateCall4(runtime_func,
2292 method_object_addr, irb_.getInt32(dex_pc),
2293 array_addr, irb_.getInt32(payload_offset));
2294
2295 EmitGuard_ExceptionLandingPad(dex_pc);
2296 }
2297
2298 return;
2299}
2300
2301llvm::Value* GBCExpanderPass::EmitAllocNewArray(uint32_t dex_pc,
2302 llvm::Value* array_length_value,
2303 uint32_t type_idx,
2304 bool is_filled_new_array) {
2305 llvm::Function* runtime_func;
2306
2307 bool skip_access_check =
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002308 compiler_->CanAccessTypeWithoutChecks(method_idx_, *dex_file_, type_idx);
TDYa127f71bf5a2012-07-29 20:09:52 -07002309
2310
2311 if (is_filled_new_array) {
2312 runtime_func = skip_access_check ?
2313 irb_.GetRuntime(runtime_support::CheckAndAllocArray) :
2314 irb_.GetRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck);
2315 } else {
2316 runtime_func = skip_access_check ?
2317 irb_.GetRuntime(runtime_support::AllocArray) :
2318 irb_.GetRuntime(runtime_support::AllocArrayWithAccessCheck);
2319 }
2320
2321 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2322
2323 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2324
2325 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2326
2327 EmitUpdateDexPC(dex_pc);
2328
2329 llvm::Value* object_addr =
2330 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
2331 array_length_value, thread_object_addr);
2332
2333 EmitGuard_ExceptionLandingPad(dex_pc);
2334
2335 return object_addr;
2336}
2337
2338llvm::Value* GBCExpanderPass::
2339EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -07002340 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -07002341 llvm::Value* this_addr,
2342 uint32_t dex_pc,
2343 bool is_fast_path) {
2344
2345 llvm::Function* runtime_func = NULL;
2346
2347 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002348 case art::kStatic:
TDYa127f71bf5a2012-07-29 20:09:52 -07002349 runtime_func = irb_.GetRuntime(runtime_support::FindStaticMethodWithAccessCheck);
2350 break;
2351
TDYa127920be7c2012-09-10 17:13:22 -07002352 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002353 runtime_func = irb_.GetRuntime(runtime_support::FindDirectMethodWithAccessCheck);
2354 break;
2355
TDYa127920be7c2012-09-10 17:13:22 -07002356 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002357 runtime_func = irb_.GetRuntime(runtime_support::FindVirtualMethodWithAccessCheck);
2358 break;
2359
TDYa127920be7c2012-09-10 17:13:22 -07002360 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002361 runtime_func = irb_.GetRuntime(runtime_support::FindSuperMethodWithAccessCheck);
2362 break;
2363
TDYa127920be7c2012-09-10 17:13:22 -07002364 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002365 if (is_fast_path) {
2366 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethod);
2367 } else {
2368 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethodWithAccessCheck);
2369 }
2370 break;
2371 }
2372
2373 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2374
2375 if (this_addr == NULL) {
TDYa127920be7c2012-09-10 17:13:22 -07002376 DCHECK_EQ(invoke_type, art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002377 this_addr = irb_.getJNull();
2378 }
2379
2380 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2381
2382 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2383
2384 EmitUpdateDexPC(dex_pc);
2385
2386 llvm::Value* callee_method_object_addr =
2387 irb_.CreateCall4(runtime_func,
2388 callee_method_idx_value,
2389 this_addr,
2390 caller_method_object_addr,
2391 thread_object_addr);
2392
2393 EmitGuard_ExceptionLandingPad(dex_pc);
2394
2395 return callee_method_object_addr;
2396}
2397
TDYa1275e869b62012-07-25 00:45:39 -07002398void GBCExpanderPass::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2399 // Using runtime support, let the target can override by InlineAssembly.
2400 irb_.Runtime().EmitMarkGCCard(value, target_addr);
2401}
2402
2403void GBCExpanderPass::EmitUpdateDexPC(uint32_t dex_pc) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002404#if defined(ART_USE_QUICK_COMPILER)
2405 if (shadow_frame_ == NULL) {
2406 return;
2407 }
2408#endif
TDYa1275e869b62012-07-25 00:45:39 -07002409 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07002410 art::ShadowFrame::DexPCOffset(),
TDYa1275e869b62012-07-25 00:45:39 -07002411 irb_.getInt32(dex_pc),
2412 kTBAAShadowFrame);
2413}
2414
2415void GBCExpanderPass::EmitGuard_DivZeroException(uint32_t dex_pc,
2416 llvm::Value* denominator,
2417 JType op_jty) {
2418 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2419
2420 llvm::Constant* zero = irb_.getJZero(op_jty);
2421
2422 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2423
2424 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2425
2426 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2427
2428 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
2429
2430 irb_.SetInsertPoint(block_exception);
2431 EmitUpdateDexPC(dex_pc);
2432 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowDivZeroException));
2433 EmitBranchExceptionLandingPad(dex_pc);
2434
2435 irb_.SetInsertPoint(block_continue);
2436}
2437
2438void GBCExpanderPass::EmitGuard_NullPointerException(uint32_t dex_pc,
2439 llvm::Value* object) {
2440 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
2441
2442 llvm::BasicBlock* block_exception =
2443 CreateBasicBlockWithDexPC(dex_pc, "nullp");
2444
2445 llvm::BasicBlock* block_continue =
2446 CreateBasicBlockWithDexPC(dex_pc, "cont");
2447
2448 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
2449
2450 irb_.SetInsertPoint(block_exception);
2451 EmitUpdateDexPC(dex_pc);
2452 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowNullPointerException),
2453 irb_.getInt32(dex_pc));
2454 EmitBranchExceptionLandingPad(dex_pc);
2455
2456 irb_.SetInsertPoint(block_continue);
2457}
2458
2459void
2460GBCExpanderPass::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2461 llvm::Value* array,
2462 llvm::Value* index) {
2463 llvm::Value* array_len = EmitLoadArrayLength(array);
2464
2465 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2466
2467 llvm::BasicBlock* block_exception =
2468 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2469
2470 llvm::BasicBlock* block_continue =
2471 CreateBasicBlockWithDexPC(dex_pc, "cont");
2472
2473 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
2474
2475 irb_.SetInsertPoint(block_exception);
2476
2477 EmitUpdateDexPC(dex_pc);
2478 irb_.CreateCall2(irb_.GetRuntime(runtime_support::ThrowIndexOutOfBounds), index, array_len);
2479 EmitBranchExceptionLandingPad(dex_pc);
2480
2481 irb_.SetInsertPoint(block_continue);
2482}
2483
TDYa1275e869b62012-07-25 00:45:39 -07002484llvm::FunctionType* GBCExpanderPass::GetFunctionType(uint32_t method_idx,
2485 bool is_static) {
2486 // Get method signature
TDYa127920be7c2012-09-10 17:13:22 -07002487 art::DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
TDYa1275e869b62012-07-25 00:45:39 -07002488
2489 uint32_t shorty_size;
2490 const char* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
2491 CHECK_GE(shorty_size, 1u);
2492
2493 // Get return type
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002494
2495 char ret_shorty = shorty[0];
2496#if defined(ART_USE_QUICK_COMPILER)
Shih-wei Liaob2596522012-09-14 16:36:11 -07002497 ret_shorty = art::remapShorty(ret_shorty);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002498#endif
2499 llvm::Type* ret_type = irb_.getJType(ret_shorty, kAccurate);
TDYa1275e869b62012-07-25 00:45:39 -07002500
2501 // Get argument type
2502 std::vector<llvm::Type*> args_type;
2503
2504 args_type.push_back(irb_.getJObjectTy()); // method object pointer
2505
2506 if (!is_static) {
2507 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
2508 }
2509
2510 for (uint32_t i = 1; i < shorty_size; ++i) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002511#if defined(ART_USE_QUICK_COMPILER)
Shih-wei Liaob2596522012-09-14 16:36:11 -07002512 char shorty_type = art::remapShorty(shorty[i]);
TDYa127f71bf5a2012-07-29 20:09:52 -07002513 args_type.push_back(irb_.getJType(shorty_type, kAccurate));
2514#else
TDYa1275e869b62012-07-25 00:45:39 -07002515 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
TDYa127f71bf5a2012-07-29 20:09:52 -07002516#endif
TDYa1275e869b62012-07-25 00:45:39 -07002517 }
2518
2519 return llvm::FunctionType::get(ret_type, args_type, false);
2520}
2521
2522
2523llvm::BasicBlock* GBCExpanderPass::
2524CreateBasicBlockWithDexPC(uint32_t dex_pc, const char* postfix) {
2525 std::string name;
2526
2527#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002528 art::StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
TDYa1275e869b62012-07-25 00:45:39 -07002529#endif
2530
2531 return llvm::BasicBlock::Create(context_, name, func_);
2532}
2533
2534llvm::BasicBlock* GBCExpanderPass::GetBasicBlock(uint32_t dex_pc) {
2535 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002536 CHECK(basic_blocks_[dex_pc] != NULL);
TDYa1275e869b62012-07-25 00:45:39 -07002537 return basic_blocks_[dex_pc];
2538}
2539
2540int32_t GBCExpanderPass::GetTryItemOffset(uint32_t dex_pc) {
2541 int32_t min = 0;
2542 int32_t max = code_item_->tries_size_ - 1;
2543
2544 while (min <= max) {
2545 int32_t mid = min + (max - min) / 2;
2546
TDYa127920be7c2012-09-10 17:13:22 -07002547 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*code_item_, mid);
TDYa1275e869b62012-07-25 00:45:39 -07002548 uint32_t start = ti->start_addr_;
2549 uint32_t end = start + ti->insn_count_;
2550
2551 if (dex_pc < start) {
2552 max = mid - 1;
2553 } else if (dex_pc >= end) {
2554 min = mid + 1;
2555 } else {
2556 return mid; // found
2557 }
2558 }
2559
2560 return -1; // not found
2561}
2562
2563llvm::BasicBlock* GBCExpanderPass::GetLandingPadBasicBlock(uint32_t dex_pc) {
2564 // Find the try item for this address in this method
2565 int32_t ti_offset = GetTryItemOffset(dex_pc);
2566
2567 if (ti_offset == -1) {
2568 return NULL; // No landing pad is available for this address.
2569 }
2570
2571 // Check for the existing landing pad basic block
2572 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2573 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2574
2575 if (block_lpad) {
2576 // We have generated landing pad for this try item already. Return the
2577 // same basic block.
2578 return block_lpad;
2579 }
2580
2581 // Get try item from code item
TDYa127920be7c2012-09-10 17:13:22 -07002582 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*code_item_, ti_offset);
TDYa1275e869b62012-07-25 00:45:39 -07002583
2584 std::string lpadname;
2585
2586#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002587 art::StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
TDYa1275e869b62012-07-25 00:45:39 -07002588#endif
2589
2590 // Create landing pad basic block
2591 block_lpad = llvm::BasicBlock::Create(context_, lpadname, func_);
2592
2593 // Change IRBuilder insert point
2594 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2595 irb_.SetInsertPoint(block_lpad);
2596
2597 // Find catch block with matching type
2598 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2599
2600 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
2601
2602 llvm::Value* catch_handler_index_value =
2603 irb_.CreateCall2(irb_.GetRuntime(runtime_support::FindCatchBlock),
2604 method_object_addr, ti_offset_value);
2605
2606 // Switch instruction (Go to unwind basic block by default)
2607 llvm::SwitchInst* sw =
2608 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2609
2610 // Cases with matched catch block
TDYa127920be7c2012-09-10 17:13:22 -07002611 art::CatchHandlerIterator iter(*code_item_, ti->start_addr_);
TDYa1275e869b62012-07-25 00:45:39 -07002612
2613 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2614 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2615 }
2616
2617 // Restore the orignal insert point for IRBuilder
2618 irb_.restoreIP(irb_ip_original);
2619
2620 // Cache this landing pad
2621 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2622 basic_block_landing_pads_[ti_offset] = block_lpad;
2623
2624 return block_lpad;
2625}
2626
2627llvm::BasicBlock* GBCExpanderPass::GetUnwindBasicBlock() {
2628 // Check the existing unwinding baisc block block
2629 if (basic_block_unwind_ != NULL) {
2630 return basic_block_unwind_;
2631 }
2632
2633 // Create new basic block for unwinding
2634 basic_block_unwind_ =
2635 llvm::BasicBlock::Create(context_, "exception_unwind", func_);
2636
2637 // Change IRBuilder insert point
2638 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2639 irb_.SetInsertPoint(basic_block_unwind_);
2640
2641 // Pop the shadow frame
2642 Expand_PopShadowFrame();
2643
2644 // Emit the code to return default value (zero) for the given return type.
2645 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002646#if defined(ART_USE_QUICK_COMPILER)
Shih-wei Liaob2596522012-09-14 16:36:11 -07002647 ret_shorty = art::remapShorty(ret_shorty);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002648#endif
TDYa1275e869b62012-07-25 00:45:39 -07002649 if (ret_shorty == 'V') {
2650 irb_.CreateRetVoid();
2651 } else {
2652 irb_.CreateRet(irb_.getJZero(ret_shorty));
2653 }
2654
2655 // Restore the orignal insert point for IRBuilder
2656 irb_.restoreIP(irb_ip_original);
2657
2658 return basic_block_unwind_;
2659}
2660
2661void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2662 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002663 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002664 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002665 irb_.CreateBr(lpad);
2666 } else {
2667 irb_.CreateBr(GetUnwindBasicBlock());
2668 }
2669}
2670
2671void GBCExpanderPass::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
2672 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
2673
2674 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2675
2676 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002677 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002678 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002679 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
2680 } else {
2681 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
2682 }
2683
2684 irb_.SetInsertPoint(block_cont);
2685}
2686
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002687llvm::Value*
2688GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
2689 llvm::CallInst& call_inst) {
2690 switch (intr_id) {
2691 //==- Thread -----------------------------------------------------------==//
2692 case IntrinsicHelper::GetCurrentThread: {
TDYa127b672d1e2012-06-28 21:21:45 -07002693 return irb_.Runtime().EmitGetCurrentThread();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002694 }
Logan Chien75e4b602012-07-23 14:24:12 -07002695 case IntrinsicHelper::CheckSuspend: {
TDYa127890ea892012-08-22 10:49:42 -07002696 // We will add suspend by ourselves.
2697 return NULL;
2698 }
2699 case IntrinsicHelper::TestSuspend: {
Logan Chiend54a23d2012-07-24 11:19:23 -07002700 Expand_TestSuspend(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002701 return NULL;
2702 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002703 case IntrinsicHelper::MarkGCCard: {
TDYa1279a129452012-07-19 03:10:08 -07002704 Expand_MarkGCCard(call_inst);
2705 return NULL;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002706 }
Logan Chien75e4b602012-07-23 14:24:12 -07002707
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002708 //==- Exception --------------------------------------------------------==//
2709 case IntrinsicHelper::ThrowException: {
2710 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
2711 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002712 case IntrinsicHelper::HLThrowException: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002713 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2714
2715 EmitUpdateDexPC(dex_pc);
2716
2717 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowException),
2718 call_inst.getArgOperand(0));
2719
2720 EmitGuard_ExceptionLandingPad(dex_pc);
2721 return NULL;
2722 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002723 case IntrinsicHelper::GetException: {
2724 return Expand_GetException();
2725 }
2726 case IntrinsicHelper::IsExceptionPending: {
2727 return irb_.Runtime().EmitIsExceptionPending();
2728 }
2729 case IntrinsicHelper::FindCatchBlock: {
2730 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
2731 }
2732 case IntrinsicHelper::ThrowDivZeroException: {
2733 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
2734 }
2735 case IntrinsicHelper::ThrowNullPointerException: {
2736 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
2737 }
2738 case IntrinsicHelper::ThrowIndexOutOfBounds: {
2739 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
2740 }
Logan Chien75e4b602012-07-23 14:24:12 -07002741
2742 //==- Const String -----------------------------------------------------==//
2743 case IntrinsicHelper::ConstString: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002744 return Expand_ConstString(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002745 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002746 case IntrinsicHelper::LoadStringFromDexCache: {
2747 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
2748 }
2749 case IntrinsicHelper::ResolveString: {
2750 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
2751 }
Logan Chien75e4b602012-07-23 14:24:12 -07002752
2753 //==- Const Class ------------------------------------------------------==//
2754 case IntrinsicHelper::ConstClass: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002755 return Expand_ConstClass(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002756 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002757 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
2758 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
2759 }
2760 case IntrinsicHelper::LoadTypeFromDexCache: {
2761 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
2762 }
2763 case IntrinsicHelper::InitializeType: {
2764 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
2765 }
Logan Chien75e4b602012-07-23 14:24:12 -07002766
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002767 //==- Lock -------------------------------------------------------------==//
2768 case IntrinsicHelper::LockObject: {
2769 Expand_LockObject(call_inst.getArgOperand(0));
2770 return NULL;
2771 }
2772 case IntrinsicHelper::UnlockObject: {
2773 Expand_UnlockObject(call_inst.getArgOperand(0));
2774 return NULL;
2775 }
Logan Chien75e4b602012-07-23 14:24:12 -07002776
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002777 //==- Cast -------------------------------------------------------------==//
2778 case IntrinsicHelper::CheckCast: {
2779 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
2780 }
Logan Chien75e4b602012-07-23 14:24:12 -07002781 case IntrinsicHelper::HLCheckCast: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002782 Expand_HLCheckCast(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002783 return NULL;
2784 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002785 case IntrinsicHelper::IsAssignable: {
2786 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
2787 }
Logan Chien75e4b602012-07-23 14:24:12 -07002788
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002789 //==- Alloc ------------------------------------------------------------==//
2790 case IntrinsicHelper::AllocObject: {
2791 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
2792 }
2793 case IntrinsicHelper::AllocObjectWithAccessCheck: {
2794 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
2795 }
Logan Chien75e4b602012-07-23 14:24:12 -07002796
2797 //==- Instance ---------------------------------------------------------==//
2798 case IntrinsicHelper::NewInstance: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002799 return Expand_NewInstance(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002800 }
2801 case IntrinsicHelper::InstanceOf: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002802 return Expand_InstanceOf(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002803 }
2804
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002805 //==- Array ------------------------------------------------------------==//
Logan Chien75e4b602012-07-23 14:24:12 -07002806 case IntrinsicHelper::NewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002807 return Expand_NewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002808 }
2809 case IntrinsicHelper::OptArrayLength: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002810 return Expand_OptArrayLength(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002811 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002812 case IntrinsicHelper::ArrayLength: {
2813 return EmitLoadArrayLength(call_inst.getArgOperand(0));
2814 }
2815 case IntrinsicHelper::AllocArray: {
2816 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
2817 }
2818 case IntrinsicHelper::AllocArrayWithAccessCheck: {
2819 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
2820 call_inst);
2821 }
2822 case IntrinsicHelper::CheckAndAllocArray: {
2823 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
2824 }
2825 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
2826 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
2827 call_inst);
2828 }
2829 case IntrinsicHelper::ArrayGet: {
2830 return Expand_ArrayGet(call_inst.getArgOperand(0),
2831 call_inst.getArgOperand(1),
2832 kInt);
2833 }
2834 case IntrinsicHelper::ArrayGetWide: {
2835 return Expand_ArrayGet(call_inst.getArgOperand(0),
2836 call_inst.getArgOperand(1),
2837 kLong);
2838 }
2839 case IntrinsicHelper::ArrayGetObject: {
2840 return Expand_ArrayGet(call_inst.getArgOperand(0),
2841 call_inst.getArgOperand(1),
2842 kObject);
2843 }
2844 case IntrinsicHelper::ArrayGetBoolean: {
2845 return Expand_ArrayGet(call_inst.getArgOperand(0),
2846 call_inst.getArgOperand(1),
2847 kBoolean);
2848 }
2849 case IntrinsicHelper::ArrayGetByte: {
2850 return Expand_ArrayGet(call_inst.getArgOperand(0),
2851 call_inst.getArgOperand(1),
2852 kByte);
2853 }
2854 case IntrinsicHelper::ArrayGetChar: {
2855 return Expand_ArrayGet(call_inst.getArgOperand(0),
2856 call_inst.getArgOperand(1),
2857 kChar);
2858 }
2859 case IntrinsicHelper::ArrayGetShort: {
2860 return Expand_ArrayGet(call_inst.getArgOperand(0),
2861 call_inst.getArgOperand(1),
2862 kShort);
2863 }
2864 case IntrinsicHelper::ArrayPut: {
2865 Expand_ArrayPut(call_inst.getArgOperand(0),
2866 call_inst.getArgOperand(1),
2867 call_inst.getArgOperand(2),
2868 kInt);
2869 return NULL;
2870 }
2871 case IntrinsicHelper::ArrayPutWide: {
2872 Expand_ArrayPut(call_inst.getArgOperand(0),
2873 call_inst.getArgOperand(1),
2874 call_inst.getArgOperand(2),
2875 kLong);
2876 return NULL;
2877 }
2878 case IntrinsicHelper::ArrayPutObject: {
2879 Expand_ArrayPut(call_inst.getArgOperand(0),
2880 call_inst.getArgOperand(1),
2881 call_inst.getArgOperand(2),
2882 kObject);
2883 return NULL;
2884 }
2885 case IntrinsicHelper::ArrayPutBoolean: {
2886 Expand_ArrayPut(call_inst.getArgOperand(0),
2887 call_inst.getArgOperand(1),
2888 call_inst.getArgOperand(2),
2889 kBoolean);
2890 return NULL;
2891 }
2892 case IntrinsicHelper::ArrayPutByte: {
2893 Expand_ArrayPut(call_inst.getArgOperand(0),
2894 call_inst.getArgOperand(1),
2895 call_inst.getArgOperand(2),
2896 kByte);
2897 return NULL;
2898 }
2899 case IntrinsicHelper::ArrayPutChar: {
2900 Expand_ArrayPut(call_inst.getArgOperand(0),
2901 call_inst.getArgOperand(1),
2902 call_inst.getArgOperand(2),
2903 kChar);
2904 return NULL;
2905 }
2906 case IntrinsicHelper::ArrayPutShort: {
2907 Expand_ArrayPut(call_inst.getArgOperand(0),
2908 call_inst.getArgOperand(1),
2909 call_inst.getArgOperand(2),
2910 kShort);
2911 return NULL;
2912 }
2913 case IntrinsicHelper::CheckPutArrayElement: {
2914 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
2915 }
2916 case IntrinsicHelper::FilledNewArray: {
2917 Expand_FilledNewArray(call_inst);
2918 return NULL;
2919 }
2920 case IntrinsicHelper::FillArrayData: {
2921 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
2922 }
Logan Chien75e4b602012-07-23 14:24:12 -07002923 case IntrinsicHelper::HLFillArrayData: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002924 Expand_HLFillArrayData(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002925 return NULL;
2926 }
2927 case IntrinsicHelper::HLFilledNewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002928 return Expand_HLFilledNewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002929 }
2930
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002931 //==- Instance Field ---------------------------------------------------==//
2932 case IntrinsicHelper::InstanceFieldGet:
2933 case IntrinsicHelper::InstanceFieldGetBoolean:
2934 case IntrinsicHelper::InstanceFieldGetByte:
2935 case IntrinsicHelper::InstanceFieldGetChar:
2936 case IntrinsicHelper::InstanceFieldGetShort: {
2937 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
2938 }
2939 case IntrinsicHelper::InstanceFieldGetWide: {
2940 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
2941 }
2942 case IntrinsicHelper::InstanceFieldGetObject: {
2943 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
2944 }
2945 case IntrinsicHelper::InstanceFieldGetFast: {
2946 return Expand_IGetFast(call_inst.getArgOperand(0),
2947 call_inst.getArgOperand(1),
2948 call_inst.getArgOperand(2),
2949 kInt);
2950 }
2951 case IntrinsicHelper::InstanceFieldGetWideFast: {
2952 return Expand_IGetFast(call_inst.getArgOperand(0),
2953 call_inst.getArgOperand(1),
2954 call_inst.getArgOperand(2),
2955 kLong);
2956 }
2957 case IntrinsicHelper::InstanceFieldGetObjectFast: {
2958 return Expand_IGetFast(call_inst.getArgOperand(0),
2959 call_inst.getArgOperand(1),
2960 call_inst.getArgOperand(2),
2961 kObject);
2962 }
2963 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
2964 return Expand_IGetFast(call_inst.getArgOperand(0),
2965 call_inst.getArgOperand(1),
2966 call_inst.getArgOperand(2),
2967 kBoolean);
2968 }
2969 case IntrinsicHelper::InstanceFieldGetByteFast: {
2970 return Expand_IGetFast(call_inst.getArgOperand(0),
2971 call_inst.getArgOperand(1),
2972 call_inst.getArgOperand(2),
2973 kByte);
2974 }
2975 case IntrinsicHelper::InstanceFieldGetCharFast: {
2976 return Expand_IGetFast(call_inst.getArgOperand(0),
2977 call_inst.getArgOperand(1),
2978 call_inst.getArgOperand(2),
2979 kChar);
2980 }
2981 case IntrinsicHelper::InstanceFieldGetShortFast: {
2982 return Expand_IGetFast(call_inst.getArgOperand(0),
2983 call_inst.getArgOperand(1),
2984 call_inst.getArgOperand(2),
2985 kShort);
2986 }
2987 case IntrinsicHelper::InstanceFieldPut:
2988 case IntrinsicHelper::InstanceFieldPutBoolean:
2989 case IntrinsicHelper::InstanceFieldPutByte:
2990 case IntrinsicHelper::InstanceFieldPutChar:
2991 case IntrinsicHelper::InstanceFieldPutShort: {
2992 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
2993 }
2994 case IntrinsicHelper::InstanceFieldPutWide: {
2995 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
2996 }
2997 case IntrinsicHelper::InstanceFieldPutObject: {
2998 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
2999 }
3000 case IntrinsicHelper::InstanceFieldPutFast: {
3001 Expand_IPutFast(call_inst.getArgOperand(0),
3002 call_inst.getArgOperand(1),
3003 call_inst.getArgOperand(2),
3004 call_inst.getArgOperand(3),
3005 kInt);
3006 return NULL;
3007 }
3008 case IntrinsicHelper::InstanceFieldPutWideFast: {
3009 Expand_IPutFast(call_inst.getArgOperand(0),
3010 call_inst.getArgOperand(1),
3011 call_inst.getArgOperand(2),
3012 call_inst.getArgOperand(3),
3013 kLong);
3014 return NULL;
3015 }
3016 case IntrinsicHelper::InstanceFieldPutObjectFast: {
3017 Expand_IPutFast(call_inst.getArgOperand(0),
3018 call_inst.getArgOperand(1),
3019 call_inst.getArgOperand(2),
3020 call_inst.getArgOperand(3),
3021 kObject);
3022 return NULL;
3023 }
3024 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
3025 Expand_IPutFast(call_inst.getArgOperand(0),
3026 call_inst.getArgOperand(1),
3027 call_inst.getArgOperand(2),
3028 call_inst.getArgOperand(3),
3029 kBoolean);
3030 return NULL;
3031 }
3032 case IntrinsicHelper::InstanceFieldPutByteFast: {
3033 Expand_IPutFast(call_inst.getArgOperand(0),
3034 call_inst.getArgOperand(1),
3035 call_inst.getArgOperand(2),
3036 call_inst.getArgOperand(3),
3037 kByte);
3038 return NULL;
3039 }
3040 case IntrinsicHelper::InstanceFieldPutCharFast: {
3041 Expand_IPutFast(call_inst.getArgOperand(0),
3042 call_inst.getArgOperand(1),
3043 call_inst.getArgOperand(2),
3044 call_inst.getArgOperand(3),
3045 kChar);
3046 return NULL;
3047 }
3048 case IntrinsicHelper::InstanceFieldPutShortFast: {
3049 Expand_IPutFast(call_inst.getArgOperand(0),
3050 call_inst.getArgOperand(1),
3051 call_inst.getArgOperand(2),
3052 call_inst.getArgOperand(3),
3053 kShort);
3054 return NULL;
3055 }
Logan Chien75e4b602012-07-23 14:24:12 -07003056
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003057 //==- Static Field -----------------------------------------------------==//
3058 case IntrinsicHelper::StaticFieldGet:
3059 case IntrinsicHelper::StaticFieldGetBoolean:
3060 case IntrinsicHelper::StaticFieldGetByte:
3061 case IntrinsicHelper::StaticFieldGetChar:
3062 case IntrinsicHelper::StaticFieldGetShort: {
3063 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
3064 }
3065 case IntrinsicHelper::StaticFieldGetWide: {
3066 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
3067 }
3068 case IntrinsicHelper::StaticFieldGetObject: {
3069 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
3070 }
3071 case IntrinsicHelper::StaticFieldGetFast: {
3072 return Expand_SGetFast(call_inst.getArgOperand(0),
3073 call_inst.getArgOperand(1),
3074 call_inst.getArgOperand(2),
3075 kInt);
3076 }
3077 case IntrinsicHelper::StaticFieldGetWideFast: {
3078 return Expand_SGetFast(call_inst.getArgOperand(0),
3079 call_inst.getArgOperand(1),
3080 call_inst.getArgOperand(2),
3081 kLong);
3082 }
3083 case IntrinsicHelper::StaticFieldGetObjectFast: {
3084 return Expand_SGetFast(call_inst.getArgOperand(0),
3085 call_inst.getArgOperand(1),
3086 call_inst.getArgOperand(2),
3087 kObject);
3088 }
3089 case IntrinsicHelper::StaticFieldGetBooleanFast: {
3090 return Expand_SGetFast(call_inst.getArgOperand(0),
3091 call_inst.getArgOperand(1),
3092 call_inst.getArgOperand(2),
3093 kBoolean);
3094 }
3095 case IntrinsicHelper::StaticFieldGetByteFast: {
3096 return Expand_SGetFast(call_inst.getArgOperand(0),
3097 call_inst.getArgOperand(1),
3098 call_inst.getArgOperand(2),
3099 kByte);
3100 }
3101 case IntrinsicHelper::StaticFieldGetCharFast: {
3102 return Expand_SGetFast(call_inst.getArgOperand(0),
3103 call_inst.getArgOperand(1),
3104 call_inst.getArgOperand(2),
3105 kChar);
3106 }
3107 case IntrinsicHelper::StaticFieldGetShortFast: {
3108 return Expand_SGetFast(call_inst.getArgOperand(0),
3109 call_inst.getArgOperand(1),
3110 call_inst.getArgOperand(2),
3111 kShort);
3112 }
3113 case IntrinsicHelper::StaticFieldPut:
3114 case IntrinsicHelper::StaticFieldPutBoolean:
3115 case IntrinsicHelper::StaticFieldPutByte:
3116 case IntrinsicHelper::StaticFieldPutChar:
3117 case IntrinsicHelper::StaticFieldPutShort: {
3118 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
3119 }
3120 case IntrinsicHelper::StaticFieldPutWide: {
3121 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
3122 }
3123 case IntrinsicHelper::StaticFieldPutObject: {
3124 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
3125 }
3126 case IntrinsicHelper::StaticFieldPutFast: {
3127 Expand_SPutFast(call_inst.getArgOperand(0),
3128 call_inst.getArgOperand(1),
3129 call_inst.getArgOperand(2),
3130 call_inst.getArgOperand(3),
3131 kInt);
3132 return NULL;
3133 }
3134 case IntrinsicHelper::StaticFieldPutWideFast: {
3135 Expand_SPutFast(call_inst.getArgOperand(0),
3136 call_inst.getArgOperand(1),
3137 call_inst.getArgOperand(2),
3138 call_inst.getArgOperand(3),
3139 kLong);
3140 return NULL;
3141 }
3142 case IntrinsicHelper::StaticFieldPutObjectFast: {
3143 Expand_SPutFast(call_inst.getArgOperand(0),
3144 call_inst.getArgOperand(1),
3145 call_inst.getArgOperand(2),
3146 call_inst.getArgOperand(3),
3147 kObject);
3148 return NULL;
3149 }
3150 case IntrinsicHelper::StaticFieldPutBooleanFast: {
3151 Expand_SPutFast(call_inst.getArgOperand(0),
3152 call_inst.getArgOperand(1),
3153 call_inst.getArgOperand(2),
3154 call_inst.getArgOperand(3),
3155 kBoolean);
3156 return NULL;
3157 }
3158 case IntrinsicHelper::StaticFieldPutByteFast: {
3159 Expand_SPutFast(call_inst.getArgOperand(0),
3160 call_inst.getArgOperand(1),
3161 call_inst.getArgOperand(2),
3162 call_inst.getArgOperand(3),
3163 kByte);
3164 return NULL;
3165 }
3166 case IntrinsicHelper::StaticFieldPutCharFast: {
3167 Expand_SPutFast(call_inst.getArgOperand(0),
3168 call_inst.getArgOperand(1),
3169 call_inst.getArgOperand(2),
3170 call_inst.getArgOperand(3),
3171 kChar);
3172 return NULL;
3173 }
3174 case IntrinsicHelper::StaticFieldPutShortFast: {
3175 Expand_SPutFast(call_inst.getArgOperand(0),
3176 call_inst.getArgOperand(1),
3177 call_inst.getArgOperand(2),
3178 call_inst.getArgOperand(3),
3179 kShort);
3180 return NULL;
3181 }
3182 case IntrinsicHelper::LoadDeclaringClassSSB: {
3183 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
3184 }
3185 case IntrinsicHelper::LoadClassSSBFromDexCache: {
3186 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
3187 }
3188 case IntrinsicHelper::InitializeAndLoadClassSSB: {
3189 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
3190 }
Logan Chien75e4b602012-07-23 14:24:12 -07003191
3192 //==- High-level Array -------------------------------------------------==//
3193 case IntrinsicHelper::HLArrayGet: {
TDYa1275a26d442012-07-26 18:58:38 -07003194 return Expand_HLArrayGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003195 }
3196 case IntrinsicHelper::HLArrayGetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003197 return Expand_HLArrayGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003198 }
3199 case IntrinsicHelper::HLArrayGetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003200 return Expand_HLArrayGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003201 }
3202 case IntrinsicHelper::HLArrayGetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003203 return Expand_HLArrayGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003204 }
3205 case IntrinsicHelper::HLArrayGetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003206 return Expand_HLArrayGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003207 }
3208 case IntrinsicHelper::HLArrayGetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003209 return Expand_HLArrayGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003210 }
3211 case IntrinsicHelper::HLArrayGetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003212 return Expand_HLArrayGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003213 }
3214 case IntrinsicHelper::HLArrayGetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003215 return Expand_HLArrayGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003216 }
3217 case IntrinsicHelper::HLArrayGetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003218 return Expand_HLArrayGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003219 }
3220 case IntrinsicHelper::HLArrayPut: {
TDYa1275a26d442012-07-26 18:58:38 -07003221 Expand_HLArrayPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003222 return NULL;
3223 }
3224 case IntrinsicHelper::HLArrayPutBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003225 Expand_HLArrayPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003226 return NULL;
3227 }
3228 case IntrinsicHelper::HLArrayPutByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003229 Expand_HLArrayPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003230 return NULL;
3231 }
3232 case IntrinsicHelper::HLArrayPutChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003233 Expand_HLArrayPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003234 return NULL;
3235 }
3236 case IntrinsicHelper::HLArrayPutShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003237 Expand_HLArrayPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003238 return NULL;
3239 }
3240 case IntrinsicHelper::HLArrayPutFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003241 Expand_HLArrayPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003242 return NULL;
3243 }
3244 case IntrinsicHelper::HLArrayPutWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003245 Expand_HLArrayPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003246 return NULL;
3247 }
3248 case IntrinsicHelper::HLArrayPutDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003249 Expand_HLArrayPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003250 return NULL;
3251 }
3252 case IntrinsicHelper::HLArrayPutObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003253 Expand_HLArrayPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003254 return NULL;
3255 }
3256
3257 //==- High-level Instance ----------------------------------------------==//
3258 case IntrinsicHelper::HLIGet: {
TDYa1275e869b62012-07-25 00:45:39 -07003259 return Expand_HLIGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003260 }
3261 case IntrinsicHelper::HLIGetBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003262 return Expand_HLIGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003263 }
3264 case IntrinsicHelper::HLIGetByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003265 return Expand_HLIGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003266 }
3267 case IntrinsicHelper::HLIGetChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003268 return Expand_HLIGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003269 }
3270 case IntrinsicHelper::HLIGetShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003271 return Expand_HLIGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003272 }
3273 case IntrinsicHelper::HLIGetFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003274 return Expand_HLIGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003275 }
3276 case IntrinsicHelper::HLIGetWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003277 return Expand_HLIGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003278 }
3279 case IntrinsicHelper::HLIGetDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003280 return Expand_HLIGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003281 }
3282 case IntrinsicHelper::HLIGetObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003283 return Expand_HLIGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003284 }
3285 case IntrinsicHelper::HLIPut: {
TDYa1275e869b62012-07-25 00:45:39 -07003286 Expand_HLIPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003287 return NULL;
3288 }
3289 case IntrinsicHelper::HLIPutBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003290 Expand_HLIPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003291 return NULL;
3292 }
3293 case IntrinsicHelper::HLIPutByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003294 Expand_HLIPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003295 return NULL;
3296 }
3297 case IntrinsicHelper::HLIPutChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003298 Expand_HLIPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003299 return NULL;
3300 }
3301 case IntrinsicHelper::HLIPutShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003302 Expand_HLIPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003303 return NULL;
3304 }
3305 case IntrinsicHelper::HLIPutFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003306 Expand_HLIPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003307 return NULL;
3308 }
3309 case IntrinsicHelper::HLIPutWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003310 Expand_HLIPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003311 return NULL;
3312 }
3313 case IntrinsicHelper::HLIPutDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003314 Expand_HLIPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003315 return NULL;
3316 }
3317 case IntrinsicHelper::HLIPutObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003318 Expand_HLIPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003319 return NULL;
3320 }
3321
3322 //==- High-level Invoke ------------------------------------------------==//
TDYa127f71bf5a2012-07-29 20:09:52 -07003323 case IntrinsicHelper::HLInvokeVoid:
3324 case IntrinsicHelper::HLInvokeObj:
3325 case IntrinsicHelper::HLInvokeInt:
3326 case IntrinsicHelper::HLInvokeFloat:
3327 case IntrinsicHelper::HLInvokeLong:
Logan Chien75e4b602012-07-23 14:24:12 -07003328 case IntrinsicHelper::HLInvokeDouble: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003329 return Expand_HLInvoke(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003330 }
3331
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003332 //==- Invoke -----------------------------------------------------------==//
3333 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
3334 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
3335 }
3336 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
3337 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
3338 }
3339 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
3340 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
3341 }
3342 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
3343 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
3344 }
3345 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
3346 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
3347 }
3348 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
3349 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
3350 }
3351 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
3352 return Expand_GetVirtualCalleeMethodObjAddrFast(
3353 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
3354 }
3355 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
3356 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
3357 }
3358 case IntrinsicHelper::InvokeRetVoid:
3359 case IntrinsicHelper::InvokeRetBoolean:
3360 case IntrinsicHelper::InvokeRetByte:
3361 case IntrinsicHelper::InvokeRetChar:
3362 case IntrinsicHelper::InvokeRetShort:
3363 case IntrinsicHelper::InvokeRetInt:
3364 case IntrinsicHelper::InvokeRetLong:
3365 case IntrinsicHelper::InvokeRetFloat:
3366 case IntrinsicHelper::InvokeRetDouble:
3367 case IntrinsicHelper::InvokeRetObject: {
3368 return Expand_Invoke(call_inst);
3369 }
Logan Chien75e4b602012-07-23 14:24:12 -07003370
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003371 //==- Math -------------------------------------------------------------==//
3372 case IntrinsicHelper::DivInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003373 return Expand_DivRem(call_inst, /* is_div */true, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003374 }
3375 case IntrinsicHelper::RemInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003376 return Expand_DivRem(call_inst, /* is_div */false, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003377 }
3378 case IntrinsicHelper::DivLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003379 return Expand_DivRem(call_inst, /* is_div */true, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003380 }
3381 case IntrinsicHelper::RemLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003382 return Expand_DivRem(call_inst, /* is_div */false, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003383 }
3384 case IntrinsicHelper::D2L: {
3385 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
3386 }
3387 case IntrinsicHelper::D2I: {
3388 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
3389 }
3390 case IntrinsicHelper::F2L: {
3391 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
3392 }
3393 case IntrinsicHelper::F2I: {
3394 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
3395 }
Logan Chien75e4b602012-07-23 14:24:12 -07003396
3397 //==- High-level Static ------------------------------------------------==//
3398 case IntrinsicHelper::HLSget: {
TDYa1275a26d442012-07-26 18:58:38 -07003399 return Expand_HLSget(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003400 }
3401 case IntrinsicHelper::HLSgetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003402 return Expand_HLSget(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003403 }
3404 case IntrinsicHelper::HLSgetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003405 return Expand_HLSget(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003406 }
3407 case IntrinsicHelper::HLSgetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003408 return Expand_HLSget(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003409 }
3410 case IntrinsicHelper::HLSgetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003411 return Expand_HLSget(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003412 }
3413 case IntrinsicHelper::HLSgetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003414 return Expand_HLSget(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003415 }
3416 case IntrinsicHelper::HLSgetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003417 return Expand_HLSget(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003418 }
3419 case IntrinsicHelper::HLSgetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003420 return Expand_HLSget(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003421 }
3422 case IntrinsicHelper::HLSgetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003423 return Expand_HLSget(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003424 }
3425 case IntrinsicHelper::HLSput: {
TDYa1275a26d442012-07-26 18:58:38 -07003426 Expand_HLSput(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003427 return NULL;
3428 }
3429 case IntrinsicHelper::HLSputBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003430 Expand_HLSput(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003431 return NULL;
3432 }
3433 case IntrinsicHelper::HLSputByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003434 Expand_HLSput(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003435 return NULL;
3436 }
3437 case IntrinsicHelper::HLSputChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003438 Expand_HLSput(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003439 return NULL;
3440 }
3441 case IntrinsicHelper::HLSputShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003442 Expand_HLSput(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003443 return NULL;
3444 }
3445 case IntrinsicHelper::HLSputFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003446 Expand_HLSput(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003447 return NULL;
3448 }
3449 case IntrinsicHelper::HLSputWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003450 Expand_HLSput(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003451 return NULL;
3452 }
3453 case IntrinsicHelper::HLSputDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003454 Expand_HLSput(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003455 return NULL;
3456 }
3457 case IntrinsicHelper::HLSputObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003458 Expand_HLSput(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003459 return NULL;
3460 }
3461
3462 //==- High-level Monitor -----------------------------------------------==//
3463 case IntrinsicHelper::MonitorEnter: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003464 Expand_MonitorEnter(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003465 return NULL;
3466 }
3467 case IntrinsicHelper::MonitorExit: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003468 Expand_MonitorExit(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003469 return NULL;
3470 }
3471
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003472 //==- Shadow Frame -----------------------------------------------------==//
3473 case IntrinsicHelper::AllocaShadowFrame: {
3474 Expand_AllocaShadowFrame(call_inst.getArgOperand(0));
3475 return NULL;
3476 }
3477 case IntrinsicHelper::SetShadowFrameEntry: {
3478 Expand_SetShadowFrameEntry(call_inst.getArgOperand(0),
3479 call_inst.getArgOperand(1));
3480 return NULL;
3481 }
3482 case IntrinsicHelper::PopShadowFrame: {
3483 Expand_PopShadowFrame();
3484 return NULL;
3485 }
3486 case IntrinsicHelper::UpdateDexPC: {
3487 Expand_UpdateDexPC(call_inst.getArgOperand(0));
3488 return NULL;
3489 }
TDYa127a1b21852012-07-23 03:20:39 -07003490
Logan Chien75e4b602012-07-23 14:24:12 -07003491 //==- Comparison -------------------------------------------------------==//
3492 case IntrinsicHelper::CmplFloat:
3493 case IntrinsicHelper::CmplDouble: {
3494 return Expand_FPCompare(call_inst.getArgOperand(0),
3495 call_inst.getArgOperand(1),
3496 false);
3497 }
3498 case IntrinsicHelper::CmpgFloat:
3499 case IntrinsicHelper::CmpgDouble: {
3500 return Expand_FPCompare(call_inst.getArgOperand(0),
3501 call_inst.getArgOperand(1),
3502 true);
3503 }
3504 case IntrinsicHelper::CmpLong: {
3505 return Expand_LongCompare(call_inst.getArgOperand(0),
3506 call_inst.getArgOperand(1));
3507 }
TDYa127a1b21852012-07-23 03:20:39 -07003508
Logan Chien75e4b602012-07-23 14:24:12 -07003509 //==- Const ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003510 case IntrinsicHelper::ConstInt:
3511 case IntrinsicHelper::ConstLong: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003512 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003513 }
TDYa127920be7c2012-09-10 17:13:22 -07003514 case IntrinsicHelper::ConstFloat: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003515 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3516 irb_.getJFloatTy());
Logan Chien75e4b602012-07-23 14:24:12 -07003517 }
TDYa127920be7c2012-09-10 17:13:22 -07003518 case IntrinsicHelper::ConstDouble: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003519 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3520 irb_.getJDoubleTy());
3521 }
TDYa127920be7c2012-09-10 17:13:22 -07003522 case IntrinsicHelper::ConstObj: {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003523 CHECK(LV2UInt(call_inst.getArgOperand(0)) == 0);
3524 return irb_.getJNull();
Logan Chien75e4b602012-07-23 14:24:12 -07003525 }
3526
3527 //==- Method Info ------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003528 case IntrinsicHelper::MethodInfo: {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003529 // Nothing to be done, because MethodInfo carries optional hints that are
3530 // not needed by the portable path.
Logan Chien75e4b602012-07-23 14:24:12 -07003531 return NULL;
3532 }
3533
3534 //==- Copy -------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003535 case IntrinsicHelper::CopyInt:
3536 case IntrinsicHelper::CopyFloat:
3537 case IntrinsicHelper::CopyLong:
3538 case IntrinsicHelper::CopyDouble:
3539 case IntrinsicHelper::CopyObj: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003540 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003541 }
3542
3543 //==- Shift ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003544 case IntrinsicHelper::SHLLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003545 return Expand_IntegerShift(call_inst.getArgOperand(0),
3546 call_inst.getArgOperand(1),
3547 kIntegerSHL, kLong);
3548 }
TDYa127920be7c2012-09-10 17:13:22 -07003549 case IntrinsicHelper::SHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003550 return Expand_IntegerShift(call_inst.getArgOperand(0),
3551 call_inst.getArgOperand(1),
3552 kIntegerSHR, kLong);
3553 }
TDYa127920be7c2012-09-10 17:13:22 -07003554 case IntrinsicHelper::USHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003555 return Expand_IntegerShift(call_inst.getArgOperand(0),
3556 call_inst.getArgOperand(1),
3557 kIntegerUSHR, kLong);
3558 }
TDYa127920be7c2012-09-10 17:13:22 -07003559 case IntrinsicHelper::SHLInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003560 return Expand_IntegerShift(call_inst.getArgOperand(0),
3561 call_inst.getArgOperand(1),
3562 kIntegerSHL, kInt);
3563 }
TDYa127920be7c2012-09-10 17:13:22 -07003564 case IntrinsicHelper::SHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003565 return Expand_IntegerShift(call_inst.getArgOperand(0),
3566 call_inst.getArgOperand(1),
3567 kIntegerSHR, kInt);
3568 }
TDYa127920be7c2012-09-10 17:13:22 -07003569 case IntrinsicHelper::USHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003570 return Expand_IntegerShift(call_inst.getArgOperand(0),
3571 call_inst.getArgOperand(1),
3572 kIntegerUSHR, kInt);
3573 }
3574
3575 //==- Conversion -------------------------------------------------------==//
TDYa127a1b21852012-07-23 03:20:39 -07003576 case IntrinsicHelper::IntToChar: {
3577 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
3578 irb_.getJIntTy());
3579 }
3580 case IntrinsicHelper::IntToShort: {
3581 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
3582 irb_.getJIntTy());
3583 }
3584 case IntrinsicHelper::IntToByte: {
3585 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
3586 irb_.getJIntTy());
3587 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003588
TDYa12787caa7e2012-08-25 23:23:27 -07003589 //==- Exception --------------------------------------------------------==//
3590 case IntrinsicHelper::CatchTargets: {
TDYa12755e5e6c2012-09-11 15:14:42 -07003591 UpdatePhiInstruction(current_bb_, irb_.GetInsertBlock());
TDYa12787caa7e2012-08-25 23:23:27 -07003592 llvm::SwitchInst* si = llvm::dyn_cast<llvm::SwitchInst>(call_inst.getNextNode());
3593 CHECK(si != NULL);
3594 irb_.CreateBr(si->getDefaultDest());
3595 si->eraseFromParent();
3596 return call_inst.getArgOperand(0);
3597 }
3598
Logan Chien75e4b602012-07-23 14:24:12 -07003599 //==- Unknown Cases ----------------------------------------------------==//
3600 case IntrinsicHelper::MaxIntrinsicId:
3601 case IntrinsicHelper::UnknownId:
3602 //default:
3603 // NOTE: "default" is intentionally commented so that C/C++ compiler will
3604 // give some warning on unmatched cases.
3605 // NOTE: We should not implement these cases.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003606 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003607 }
Logan Chien75e4b602012-07-23 14:24:12 -07003608 UNIMPLEMENTED(FATAL) << "Unexpected GBC intrinsic: " << static_cast<int>(intr_id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003609 return NULL;
3610}
3611
3612} // anonymous namespace
3613
3614namespace art {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003615
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003616namespace compiler_llvm {
3617
3618llvm::FunctionPass*
3619CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb) {
3620 return new GBCExpanderPass(intrinsic_helper, irb);
3621}
3622
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003623llvm::FunctionPass*
3624CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
3625 Compiler* compiler, OatCompilationUnit* oat_compilation_unit) {
3626 if (compiler != NULL) {
3627 return new GBCExpanderPass(intrinsic_helper, irb,
3628 compiler, oat_compilation_unit);
3629 } else {
3630 return new GBCExpanderPass(intrinsic_helper, irb);
3631 }
3632}
3633
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003634} // namespace compiler_llvm
3635} // namespace art