blob: df83ddcf60535b6614150fb657fb35902624fbd7 [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"
Ian Rogers76ae4fe2013-02-27 16:03:41 -080021#include "intrinsic_helper.h"
Ian Rogers98573f92013-01-30 17:26:32 -080022#include "mirror/abstract_method.h"
23#include "mirror/array.h"
TDYa1275e869b62012-07-25 00:45:39 -070024#include "oat_compilation_unit.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070025#include "thread.h"
TDYa1275e869b62012-07-25 00:45:39 -070026#include "verifier/method_verifier.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070027
buzbee395116c2013-02-27 14:30:25 -080028#include "compiler/dex/compiler_ir.h"
29#include "compiler/dex/quick/codegen.h"
TDYa127920be7c2012-09-10 17:13:22 -070030using art::kMIRIgnoreNullCheck;
31using art::kMIRIgnoreRangeCheck;
32
Shih-wei Liao21d28f52012-06-12 05:55:00 -070033#include <llvm/ADT/STLExtras.h>
34#include <llvm/Intrinsics.h>
Logan Chiend36a2ac2012-08-04 00:37:30 +080035#include <llvm/Metadata.h>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070036#include <llvm/Pass.h>
37#include <llvm/Support/CFG.h>
38#include <llvm/Support/InstIterator.h>
39
40#include <vector>
TDYa127aa558872012-08-16 05:11:07 -070041#include <map>
42#include <utility>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070043
TDYa127920be7c2012-09-10 17:13:22 -070044using namespace art::compiler_llvm;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070045
Ian Rogers76ae4fe2013-02-27 16:03:41 -080046using art::compiler_llvm::IntrinsicHelper;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070047
Shih-wei Liaob2596522012-09-14 16:36:11 -070048namespace art {
buzbee26f10ee2012-12-21 11:16:29 -080049extern char RemapShorty(char shortyType);
Shih-wei Liaob2596522012-09-14 16:36:11 -070050};
51
Shih-wei Liao21d28f52012-06-12 05:55:00 -070052namespace {
53
54class GBCExpanderPass : public llvm::FunctionPass {
55 private:
56 const IntrinsicHelper& intrinsic_helper_;
57 IRBuilder& irb_;
58
59 llvm::LLVMContext& context_;
60 RuntimeSupportBuilder& rtb_;
61
62 private:
63 llvm::AllocaInst* shadow_frame_;
64 llvm::Value* old_shadow_frame_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070065
66 private:
TDYa127920be7c2012-09-10 17:13:22 -070067 art::Compiler* compiler_;
TDYa1275e869b62012-07-25 00:45:39 -070068
TDYa127920be7c2012-09-10 17:13:22 -070069 const art::DexFile* dex_file_;
70 const art::DexFile::CodeItem* code_item_;
TDYa1275e869b62012-07-25 00:45:39 -070071
TDYa127920be7c2012-09-10 17:13:22 -070072 art::OatCompilationUnit* oat_compilation_unit_;
TDYa1275e869b62012-07-25 00:45:39 -070073
74 uint32_t method_idx_;
75
76 llvm::Function* func_;
77
78 std::vector<llvm::BasicBlock*> basic_blocks_;
79
80 std::vector<llvm::BasicBlock*> basic_block_landing_pads_;
TDYa12755e5e6c2012-09-11 15:14:42 -070081 llvm::BasicBlock* current_bb_;
TDYa127aa558872012-08-16 05:11:07 -070082 std::map<llvm::BasicBlock*, std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> > >
83 landing_pad_phi_mapping_;
TDYa1275e869b62012-07-25 00:45:39 -070084 llvm::BasicBlock* basic_block_unwind_;
85
Logan Chien67645d82012-08-17 09:10:54 +080086 bool changed_;
87
TDYa1275e869b62012-07-25 00:45:39 -070088 private:
Shih-wei Liao21d28f52012-06-12 05:55:00 -070089 //----------------------------------------------------------------------------
Logan Chien75e4b602012-07-23 14:24:12 -070090 // Constant for GBC expansion
91 //----------------------------------------------------------------------------
92 enum IntegerShiftKind {
93 kIntegerSHL,
94 kIntegerSHR,
95 kIntegerUSHR,
96 };
97
98 private:
99 //----------------------------------------------------------------------------
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700100 // Helper function for GBC expansion
101 //----------------------------------------------------------------------------
102
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700103 llvm::Value* ExpandToRuntime(runtime_support::RuntimeId rt,
104 llvm::CallInst& inst);
105
TDYa1275e869b62012-07-25 00:45:39 -0700106 uint64_t LV2UInt(llvm::Value* lv) {
107 return llvm::cast<llvm::ConstantInt>(lv)->getZExtValue();
108 }
109
110 int64_t LV2SInt(llvm::Value* lv) {
111 return llvm::cast<llvm::ConstantInt>(lv)->getSExtValue();
112 }
113
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700114 private:
115 // TODO: Almost all Emit* are directly copy-n-paste from MethodCompiler.
116 // Refactor these utility functions from MethodCompiler to avoid forking.
117
Logan Chien67645d82012-08-17 09:10:54 +0800118 void EmitStackOverflowCheck(llvm::Instruction* first_non_alloca);
119
120 void RewriteFunction();
121
122 void RewriteBasicBlock(llvm::BasicBlock* original_block);
123
124 void UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
125 llvm::BasicBlock* new_basic_block);
126
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700127
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800128 // Sign or zero extend category 1 types < 32bits in size to 32bits.
129 llvm::Value* SignOrZeroExtendCat1Types(llvm::Value* value, JType jty);
130
131 // Truncate category 1 types from 32bits to the given JType size.
132 llvm::Value* TruncateCat1Types(llvm::Value* value, JType jty);
133
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700134 //----------------------------------------------------------------------------
135 // Dex cache code generation helper function
136 //----------------------------------------------------------------------------
TDYa127920be7c2012-09-10 17:13:22 -0700137 llvm::Value* EmitLoadDexCacheAddr(art::MemberOffset dex_cache_offset);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700138
139 llvm::Value* EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx);
140
141 llvm::Value* EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx);
142
143 llvm::Value* EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx);
144
145 llvm::Value* EmitLoadDexCacheStringFieldAddr(uint32_t string_idx);
146
147 //----------------------------------------------------------------------------
148 // Code generation helper function
149 //----------------------------------------------------------------------------
150 llvm::Value* EmitLoadMethodObjectAddr();
151
152 llvm::Value* EmitLoadArrayLength(llvm::Value* array);
153
154 llvm::Value* EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx);
155
156 llvm::Value* EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
157 llvm::Value* this_addr);
158
159 llvm::Value* EmitArrayGEP(llvm::Value* array_addr,
160 llvm::Value* index_value,
161 JType elem_jty);
162
163 private:
164 //----------------------------------------------------------------------------
165 // Expand Greenland intrinsics
166 //----------------------------------------------------------------------------
167 void Expand_TestSuspend(llvm::CallInst& call_inst);
168
TDYa1279a129452012-07-19 03:10:08 -0700169 void Expand_MarkGCCard(llvm::CallInst& call_inst);
170
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700171 llvm::Value* Expand_LoadStringFromDexCache(llvm::Value* string_idx_value);
172
173 llvm::Value* Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value);
174
175 void Expand_LockObject(llvm::Value* obj);
176
177 void Expand_UnlockObject(llvm::Value* obj);
178
179 llvm::Value* Expand_ArrayGet(llvm::Value* array_addr,
180 llvm::Value* index_value,
181 JType elem_jty);
182
183 void Expand_ArrayPut(llvm::Value* new_value,
184 llvm::Value* array_addr,
185 llvm::Value* index_value,
186 JType elem_jty);
187
188 void Expand_FilledNewArray(llvm::CallInst& call_inst);
189
190 llvm::Value* Expand_IGetFast(llvm::Value* field_offset_value,
191 llvm::Value* is_volatile_value,
192 llvm::Value* object_addr,
193 JType field_jty);
194
195 void Expand_IPutFast(llvm::Value* field_offset_value,
196 llvm::Value* is_volatile_value,
197 llvm::Value* object_addr,
198 llvm::Value* new_value,
199 JType field_jty);
200
201 llvm::Value* Expand_SGetFast(llvm::Value* static_storage_addr,
202 llvm::Value* field_offset_value,
203 llvm::Value* is_volatile_value,
204 JType field_jty);
205
206 void Expand_SPutFast(llvm::Value* static_storage_addr,
207 llvm::Value* field_offset_value,
208 llvm::Value* is_volatile_value,
209 llvm::Value* new_value,
210 JType field_jty);
211
212 llvm::Value* Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr);
213
214 llvm::Value* Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value);
215
216 llvm::Value*
217 Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value);
218
219 llvm::Value*
220 Expand_GetVirtualCalleeMethodObjAddrFast(llvm::Value* vtable_idx_value,
221 llvm::Value* this_addr);
222
223 llvm::Value* Expand_Invoke(llvm::CallInst& call_inst);
224
TDYa1274ec8ccd2012-08-11 07:04:57 -0700225 llvm::Value* Expand_DivRem(llvm::CallInst& call_inst, bool is_div, JType op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700226
TDYa127ce4cc0d2012-11-18 16:59:53 -0800227 void Expand_AllocaShadowFrame(llvm::Value* num_vregs_value);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700228
TDYa1278e950c12012-11-02 09:58:19 -0700229 void Expand_SetVReg(llvm::Value* entry_idx, llvm::Value* obj);
230
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700231 void Expand_PopShadowFrame();
232
233 void Expand_UpdateDexPC(llvm::Value* dex_pc_value);
234
TDYa127a1b21852012-07-23 03:20:39 -0700235 //----------------------------------------------------------------------------
236 // Quick
237 //----------------------------------------------------------------------------
238
239 llvm::Value* Expand_FPCompare(llvm::Value* src1_value,
240 llvm::Value* src2_value,
241 bool gt_bias);
242
243 llvm::Value* Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value);
244
245 llvm::Value* EmitCompareResultSelection(llvm::Value* cmp_eq,
246 llvm::Value* cmp_lt);
247
TDYa127f71bf5a2012-07-29 20:09:52 -0700248 llvm::Value* EmitLoadConstantClass(uint32_t dex_pc, uint32_t type_idx);
TDYa1275a26d442012-07-26 18:58:38 -0700249 llvm::Value* EmitLoadStaticStorage(uint32_t dex_pc, uint32_t type_idx);
250
TDYa1275e869b62012-07-25 00:45:39 -0700251 llvm::Value* Expand_HLIGet(llvm::CallInst& call_inst, JType field_jty);
252 void Expand_HLIPut(llvm::CallInst& call_inst, JType field_jty);
253
TDYa1275a26d442012-07-26 18:58:38 -0700254 llvm::Value* Expand_HLSget(llvm::CallInst& call_inst, JType field_jty);
255 void Expand_HLSput(llvm::CallInst& call_inst, JType field_jty);
256
257 llvm::Value* Expand_HLArrayGet(llvm::CallInst& call_inst, JType field_jty);
258 void Expand_HLArrayPut(llvm::CallInst& call_inst, JType field_jty);
259
TDYa127f71bf5a2012-07-29 20:09:52 -0700260 llvm::Value* Expand_ConstString(llvm::CallInst& call_inst);
261 llvm::Value* Expand_ConstClass(llvm::CallInst& call_inst);
262
263 void Expand_MonitorEnter(llvm::CallInst& call_inst);
264 void Expand_MonitorExit(llvm::CallInst& call_inst);
265
266 void Expand_HLCheckCast(llvm::CallInst& call_inst);
267 llvm::Value* Expand_InstanceOf(llvm::CallInst& call_inst);
268
269 llvm::Value* Expand_NewInstance(llvm::CallInst& call_inst);
270
271 llvm::Value* Expand_HLInvoke(llvm::CallInst& call_inst);
272
273 llvm::Value* Expand_OptArrayLength(llvm::CallInst& call_inst);
274 llvm::Value* Expand_NewArray(llvm::CallInst& call_inst);
275 llvm::Value* Expand_HLFilledNewArray(llvm::CallInst& call_inst);
276 void Expand_HLFillArrayData(llvm::CallInst& call_inst);
277
278 llvm::Value* EmitAllocNewArray(uint32_t dex_pc,
279 llvm::Value* array_length_value,
280 uint32_t type_idx,
281 bool is_filled_new_array);
282
283 llvm::Value* EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -0700284 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -0700285 llvm::Value* this_addr,
286 uint32_t dex_pc,
287 bool is_fast_path);
288
TDYa1275e869b62012-07-25 00:45:39 -0700289 void EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr);
290
291 void EmitUpdateDexPC(uint32_t dex_pc);
292
293 void EmitGuard_DivZeroException(uint32_t dex_pc,
294 llvm::Value* denominator,
295 JType op_jty);
296
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +0100297 void EmitGuard_NullPointerException(uint32_t dex_pc, llvm::Value* object,
298 int opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -0700299
300 void EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
301 llvm::Value* array,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +0100302 llvm::Value* index,
303 int opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -0700304
TDYa1275e869b62012-07-25 00:45:39 -0700305 llvm::FunctionType* GetFunctionType(uint32_t method_idx, bool is_static);
306
307 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
308
309 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
310 const char* postfix);
311
312 int32_t GetTryItemOffset(uint32_t dex_pc);
313
314 llvm::BasicBlock* GetLandingPadBasicBlock(uint32_t dex_pc);
315
316 llvm::BasicBlock* GetUnwindBasicBlock();
317
318 void EmitGuard_ExceptionLandingPad(uint32_t dex_pc);
319
320 void EmitBranchExceptionLandingPad(uint32_t dex_pc);
321
Logan Chien75e4b602012-07-23 14:24:12 -0700322 //----------------------------------------------------------------------------
323 // Expand Arithmetic Helper Intrinsics
324 //----------------------------------------------------------------------------
325
326 llvm::Value* Expand_IntegerShift(llvm::Value* src1_value,
327 llvm::Value* src2_value,
328 IntegerShiftKind kind,
329 JType op_jty);
330
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700331 public:
332 static char ID;
333
334 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb)
335 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
Logan Chiene5b8f8b2012-08-13 11:45:05 +0800336 context_(irb.getContext()), rtb_(irb.Runtime()),
TDYa1278e950c12012-11-02 09:58:19 -0700337 shadow_frame_(NULL), old_shadow_frame_(NULL),
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700338 compiler_(NULL), dex_file_(NULL), code_item_(NULL),
Logan Chien67645d82012-08-17 09:10:54 +0800339 oat_compilation_unit_(NULL), method_idx_(-1u), func_(NULL),
340 changed_(false)
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700341 { }
342
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700343 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
TDYa127920be7c2012-09-10 17:13:22 -0700344 art::Compiler* compiler, art::OatCompilationUnit* oat_compilation_unit)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700345 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
346 context_(irb.getContext()), rtb_(irb.Runtime()),
TDYa1278e950c12012-11-02 09:58:19 -0700347 shadow_frame_(NULL), old_shadow_frame_(NULL),
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700348 compiler_(compiler),
349 dex_file_(oat_compilation_unit->GetDexFile()),
350 code_item_(oat_compilation_unit->GetCodeItem()),
351 oat_compilation_unit_(oat_compilation_unit),
352 method_idx_(oat_compilation_unit->GetDexMethodIndex()),
353 func_(NULL), changed_(false)
354 { }
355
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700356 bool runOnFunction(llvm::Function& func);
357
358 private:
Logan Chien67645d82012-08-17 09:10:54 +0800359 void InsertStackOverflowCheck(llvm::Function& func);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700360
361 llvm::Value* ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
362 llvm::CallInst& call_inst);
363
364};
365
366char GBCExpanderPass::ID = 0;
367
368bool GBCExpanderPass::runOnFunction(llvm::Function& func) {
TDYa127b672d1e2012-06-28 21:21:45 -0700369 // Runtime support or stub
370 if (func.getName().startswith("art_") || func.getName().startswith("Art")) {
371 return false;
372 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700373
Logan Chien67645d82012-08-17 09:10:54 +0800374 // Setup rewrite context
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700375 shadow_frame_ = NULL;
376 old_shadow_frame_ = NULL;
TDYa1275e869b62012-07-25 00:45:39 -0700377 func_ = &func;
Logan Chien67645d82012-08-17 09:10:54 +0800378 changed_ = false; // Assume unchanged
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700379
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700380 basic_blocks_.resize(code_item_->insns_size_in_code_units_);
381 basic_block_landing_pads_.resize(code_item_->tries_size_, NULL);
382 basic_block_unwind_ = NULL;
383 for (llvm::Function::iterator bb_iter = func_->begin(), bb_end = func_->end();
384 bb_iter != bb_end;
385 ++bb_iter) {
386 if (bb_iter->begin()->getMetadata("DexOff") == NULL) {
387 continue;
388 }
389 uint32_t dex_pc = LV2UInt(bb_iter->begin()->getMetadata("DexOff")->getOperand(0));
390 basic_blocks_[dex_pc] = bb_iter;
391 }
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700392
Logan Chien67645d82012-08-17 09:10:54 +0800393 // Insert stack overflow check
394 InsertStackOverflowCheck(func); // TODO: Use intrinsic.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700395
Logan Chien67645d82012-08-17 09:10:54 +0800396 // Rewrite the intrinsics
397 RewriteFunction();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700398
399 VERIFY_LLVM_FUNCTION(func);
400
Logan Chien67645d82012-08-17 09:10:54 +0800401 return changed_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700402}
403
Logan Chien67645d82012-08-17 09:10:54 +0800404void GBCExpanderPass::RewriteBasicBlock(llvm::BasicBlock* original_block) {
405 llvm::BasicBlock* curr_basic_block = original_block;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700406
Logan Chien67645d82012-08-17 09:10:54 +0800407 llvm::BasicBlock::iterator inst_iter = original_block->begin();
408 llvm::BasicBlock::iterator inst_end = original_block->end();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700409
Logan Chien67645d82012-08-17 09:10:54 +0800410 while (inst_iter != inst_end) {
411 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst_iter);
412 IntrinsicHelper::IntrinsicId intr_id = IntrinsicHelper::UnknownId;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700413
Logan Chien67645d82012-08-17 09:10:54 +0800414 if (call_inst) {
415 llvm::Function* callee_func = call_inst->getCalledFunction();
416 intr_id = intrinsic_helper_.GetIntrinsicId(callee_func);
417 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700418
Logan Chien67645d82012-08-17 09:10:54 +0800419 if (intr_id == IntrinsicHelper::UnknownId) {
420 // This is not intrinsic call. Skip this instruction.
421 ++inst_iter;
422 continue;
423 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700424
Logan Chien67645d82012-08-17 09:10:54 +0800425 // Rewrite the intrinsic and change the function
426 changed_ = true;
427 irb_.SetInsertPoint(inst_iter);
428
429 // Expand the intrinsic
430 if (llvm::Value* new_value = ExpandIntrinsic(intr_id, *call_inst)) {
431 inst_iter->replaceAllUsesWith(new_value);
432 }
433
434 // Remove the old intrinsic call instruction
435 llvm::BasicBlock::iterator old_inst = inst_iter++;
436 old_inst->eraseFromParent();
437
438 // Splice the instruction to the new basic block
439 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
440 if (next_basic_block != curr_basic_block) {
441 next_basic_block->getInstList().splice(
442 irb_.GetInsertPoint(), curr_basic_block->getInstList(),
443 inst_iter, inst_end);
444 curr_basic_block = next_basic_block;
445 inst_end = curr_basic_block->end();
446 }
447 }
448}
449
450
451void GBCExpanderPass::RewriteFunction() {
452 size_t num_basic_blocks = func_->getBasicBlockList().size();
453 // NOTE: We are not using (bb_iter != bb_end) as the for-loop condition,
454 // because we will create new basic block while expanding the intrinsics.
455 // We only want to iterate through the input basic blocks.
456
TDYa127aa558872012-08-16 05:11:07 -0700457 landing_pad_phi_mapping_.clear();
458
Logan Chien67645d82012-08-17 09:10:54 +0800459 for (llvm::Function::iterator bb_iter = func_->begin();
460 num_basic_blocks > 0; ++bb_iter, --num_basic_blocks) {
Shih-wei Liao627d8c42012-08-24 08:31:18 -0700461 // Set insert point to current basic block.
462 irb_.SetInsertPoint(bb_iter);
Logan Chien67645d82012-08-17 09:10:54 +0800463
TDYa12755e5e6c2012-09-11 15:14:42 -0700464 current_bb_ = bb_iter;
TDYa127aa558872012-08-16 05:11:07 -0700465
Logan Chien67645d82012-08-17 09:10:54 +0800466 // Rewrite the basic block
467 RewriteBasicBlock(bb_iter);
468
469 // Update the phi-instructions in the successor basic block
470 llvm::BasicBlock* last_block = irb_.GetInsertBlock();
471 if (last_block != bb_iter) {
472 UpdatePhiInstruction(bb_iter, last_block);
473 }
474 }
TDYa127aa558872012-08-16 05:11:07 -0700475
476 typedef std::map<llvm::PHINode*, llvm::PHINode*> HandlerPHIMap;
477 HandlerPHIMap handler_phi;
478 // Iterate every used landing pad basic block
479 for (size_t i = 0, ei = basic_block_landing_pads_.size(); i != ei; ++i) {
480 llvm::BasicBlock* lbb = basic_block_landing_pads_[i];
481 if (lbb == NULL) {
482 continue;
483 }
484
485 llvm::TerminatorInst* term_inst = lbb->getTerminator();
486 std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> >& rewrite_pair
487 = landing_pad_phi_mapping_[lbb];
488 irb_.SetInsertPoint(lbb->begin());
489
490 // Iterate every succeeding basic block (catch block)
491 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
492 succ_iter != succ_end; ++succ_iter) {
493 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
494
495 // Iterate every phi instructions in the succeeding basic block
496 for (llvm::BasicBlock::iterator
497 inst_iter = succ_basic_block->begin(),
498 inst_end = succ_basic_block->end();
499 inst_iter != inst_end; ++inst_iter) {
500 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
501
502 if (!phi) {
503 break; // Meet non-phi instruction. Done.
504 }
505
506 if (handler_phi[phi] == NULL) {
507 handler_phi[phi] = llvm::PHINode::Create(phi->getType(), 1);
508 }
509
510 // Create new_phi in landing pad
511 llvm::PHINode* new_phi = irb_.CreatePHI(phi->getType(), rewrite_pair.size());
512 // Insert all incoming value into new_phi by rewrite_pair
513 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
514 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
515 llvm::BasicBlock* new_bb = rewrite_pair[j].second;
516 new_phi->addIncoming(phi->getIncomingValueForBlock(old_bb), new_bb);
517 }
518 // Delete all incoming value from phi by rewrite_pair
519 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
520 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
521 int old_bb_idx = phi->getBasicBlockIndex(old_bb);
522 if (old_bb_idx >= 0) {
523 phi->removeIncomingValue(old_bb_idx, false);
524 }
525 }
526 // Insert new_phi into new handler phi
527 handler_phi[phi]->addIncoming(new_phi, lbb);
528 }
529 }
530 }
531
532 // Replace all handler phi
533 // We can't just use the old handler phi, because some exception edges will disappear after we
534 // compute fast-path.
535 for (HandlerPHIMap::iterator it = handler_phi.begin(); it != handler_phi.end(); ++it) {
536 llvm::PHINode* old_phi = it->first;
537 llvm::PHINode* new_phi = it->second;
538 new_phi->insertBefore(old_phi);
539 old_phi->replaceAllUsesWith(new_phi);
540 old_phi->eraseFromParent();
541 }
Logan Chien67645d82012-08-17 09:10:54 +0800542}
543
544void GBCExpanderPass::UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
545 llvm::BasicBlock* new_basic_block) {
546 llvm::TerminatorInst* term_inst = new_basic_block->getTerminator();
547
548 if (!term_inst) {
549 return; // No terminating instruction in new_basic_block. Nothing to do.
550 }
551
552 // Iterate every succeeding basic block
553 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
554 succ_iter != succ_end; ++succ_iter) {
555 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
556
557 // Iterate every phi instructions in the succeeding basic block
558 for (llvm::BasicBlock::iterator
559 inst_iter = succ_basic_block->begin(),
560 inst_end = succ_basic_block->end();
561 inst_iter != inst_end; ++inst_iter) {
562 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
563
564 if (!phi) {
565 break; // Meet non-phi instruction. Done.
566 }
567
568 // Update the incoming block of this phi instruction
569 for (llvm::PHINode::block_iterator
570 ibb_iter = phi->block_begin(), ibb_end = phi->block_end();
571 ibb_iter != ibb_end; ++ibb_iter) {
572 if (*ibb_iter == old_basic_block) {
573 *ibb_iter = new_basic_block;
574 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700575 }
576 }
577 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700578}
579
580llvm::Value* GBCExpanderPass::ExpandToRuntime(runtime_support::RuntimeId rt,
581 llvm::CallInst& inst) {
582 // Some GBC intrinsic can directly replace with IBC runtime. "Directly" means
583 // the arguments passed to the GBC intrinsic are as the same as IBC runtime
584 // function, therefore only called function is needed to change.
585 unsigned num_args = inst.getNumArgOperands();
586
587 if (num_args <= 0) {
588 return irb_.CreateCall(irb_.GetRuntime(rt));
589 } else {
590 std::vector<llvm::Value*> args;
591 for (unsigned i = 0; i < num_args; i++) {
592 args.push_back(inst.getArgOperand(i));
593 }
594
595 return irb_.CreateCall(irb_.GetRuntime(rt), args);
596 }
597}
598
Logan Chien67645d82012-08-17 09:10:54 +0800599void
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700600GBCExpanderPass::EmitStackOverflowCheck(llvm::Instruction* first_non_alloca) {
601 llvm::Function* func = first_non_alloca->getParent()->getParent();
602 llvm::Module* module = func->getParent();
603
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700604 // Call llvm intrinsic function to get frame address.
605 llvm::Function* frameaddress =
606 llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
607
608 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
609 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
610
611 // Cast i8* to int
612 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
613
614 // Get thread.stack_end_
615 llvm::Value* stack_end =
TDYa127920be7c2012-09-10 17:13:22 -0700616 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::StackEndOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700617 irb_.getPtrEquivIntTy(),
618 kTBAARuntimeInfo);
619
620 // Check the frame address < thread.stack_end_ ?
621 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
622
623 llvm::BasicBlock* block_exception =
624 llvm::BasicBlock::Create(context_, "stack_overflow", func);
625
626 llvm::BasicBlock* block_continue =
627 llvm::BasicBlock::Create(context_, "stack_overflow_cont", func);
628
629 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
630
631 // If stack overflow, throw exception.
632 irb_.SetInsertPoint(block_exception);
633 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowStackOverflowException));
634
635 // Unwind.
636 llvm::Type* ret_type = func->getReturnType();
637 if (ret_type->isVoidTy()) {
638 irb_.CreateRetVoid();
639 } else {
640 // The return value is ignored when there's an exception. MethodCompiler
641 // returns zero value under the the corresponding return type in this case.
642 // GBCExpander returns LLVM undef value here for brevity
643 irb_.CreateRet(llvm::UndefValue::get(ret_type));
644 }
645
646 irb_.SetInsertPoint(block_continue);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700647}
648
TDYa127920be7c2012-09-10 17:13:22 -0700649llvm::Value* GBCExpanderPass::EmitLoadDexCacheAddr(art::MemberOffset offset) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700650 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
651
652 return irb_.LoadFromObjectOffset(method_object_addr,
653 offset.Int32Value(),
654 irb_.getJObjectTy(),
655 kTBAAConstJObject);
656}
657
658llvm::Value*
659GBCExpanderPass::EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
660 llvm::Value* static_storage_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800661 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheInitializedStaticStorageOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700662
663 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
664
665 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
666}
667
668llvm::Value*
669GBCExpanderPass::EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
670 llvm::Value* resolved_type_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800671 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedTypesOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700672
673 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
674
675 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
676}
677
678llvm::Value* GBCExpanderPass::
679EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
680 llvm::Value* resolved_method_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800681 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedMethodsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700682
683 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
684
685 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
686}
687
688llvm::Value* GBCExpanderPass::
689EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
690 llvm::Value* string_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800691 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheStringsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700692
693 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
694
695 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
696}
697
698llvm::Value* GBCExpanderPass::EmitLoadMethodObjectAddr() {
699 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
700 return parent_func->arg_begin();
701}
702
703llvm::Value* GBCExpanderPass::EmitLoadArrayLength(llvm::Value* array) {
704 // Load array length
705 return irb_.LoadFromObjectOffset(array,
Ian Rogers98573f92013-01-30 17:26:32 -0800706 art::mirror::Array::LengthOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700707 irb_.getJIntTy(),
708 kTBAAConstJObject);
709
710}
711
712llvm::Value*
713GBCExpanderPass::EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
714 llvm::Value* callee_method_object_field_addr =
715 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
716
TDYa127ce4cc0d2012-11-18 16:59:53 -0800717 return irb_.CreateLoad(callee_method_object_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700718}
719
720llvm::Value* GBCExpanderPass::
721EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, llvm::Value* this_addr) {
722 // Load class object of *this* pointer
723 llvm::Value* class_object_addr =
724 irb_.LoadFromObjectOffset(this_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800725 art::mirror::Object::ClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700726 irb_.getJObjectTy(),
727 kTBAAConstJObject);
728
729 // Load vtable address
730 llvm::Value* vtable_addr =
731 irb_.LoadFromObjectOffset(class_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800732 art::mirror::Class::VTableOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700733 irb_.getJObjectTy(),
734 kTBAAConstJObject);
735
736 // Load callee method object
737 llvm::Value* vtable_idx_value =
738 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
739
740 llvm::Value* method_field_addr =
741 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
742
743 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
744}
745
746// Emit Array GetElementPtr
747llvm::Value* GBCExpanderPass::EmitArrayGEP(llvm::Value* array_addr,
748 llvm::Value* index_value,
749 JType elem_jty) {
750
751 int data_offset;
752 if (elem_jty == kLong || elem_jty == kDouble ||
Ian Rogers98573f92013-01-30 17:26:32 -0800753 (elem_jty == kObject && sizeof(uint64_t) == sizeof(art::mirror::Object*))) {
754 data_offset = art::mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700755 } else {
Ian Rogers98573f92013-01-30 17:26:32 -0800756 data_offset = art::mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700757 }
758
759 llvm::Constant* data_offset_value =
760 irb_.getPtrEquivInt(data_offset);
761
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800762 llvm::Type* elem_type = irb_.getJType(elem_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700763
764 llvm::Value* array_data_addr =
765 irb_.CreatePtrDisp(array_addr, data_offset_value,
766 elem_type->getPointerTo());
767
768 return irb_.CreateGEP(array_data_addr, index_value);
769}
770
771void GBCExpanderPass::Expand_TestSuspend(llvm::CallInst& call_inst) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800772 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
773
774 llvm::Value* suspend_count =
775 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::ThreadFlagsOffset().Int32Value(),
776 irb_.getInt16Ty(),
777 kTBAARuntimeInfo);
778 llvm::Value* is_suspend = irb_.CreateICmpNE(suspend_count, irb_.getInt16(0));
779
780 llvm::BasicBlock* basic_block_suspend = CreateBasicBlockWithDexPC(dex_pc, "suspend");
781 llvm::BasicBlock* basic_block_cont = CreateBasicBlockWithDexPC(dex_pc, "suspend_cont");
782
783 irb_.CreateCondBr(is_suspend, basic_block_suspend, basic_block_cont, kUnlikely);
784
785 irb_.SetInsertPoint(basic_block_suspend);
786 if (dex_pc != art::DexFile::kDexNoIndex) {
787 EmitUpdateDexPC(dex_pc);
788 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700789 irb_.Runtime().EmitTestSuspend();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800790
791 llvm::BasicBlock* basic_block_exception = CreateBasicBlockWithDexPC(dex_pc, "exception");
792 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
793 irb_.CreateCondBr(exception_pending, basic_block_exception, basic_block_cont, kUnlikely);
794
795 irb_.SetInsertPoint(basic_block_exception);
796 llvm::Type* ret_type = call_inst.getParent()->getParent()->getReturnType();
797 if (ret_type->isVoidTy()) {
798 irb_.CreateRetVoid();
799 } else {
800 // The return value is ignored when there's an exception.
801 irb_.CreateRet(llvm::UndefValue::get(ret_type));
802 }
TDYa127ce4cc0d2012-11-18 16:59:53 -0800803
804 irb_.SetInsertPoint(basic_block_cont);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700805 return;
806}
807
TDYa1279a129452012-07-19 03:10:08 -0700808void GBCExpanderPass::Expand_MarkGCCard(llvm::CallInst& call_inst) {
TDYa1279a129452012-07-19 03:10:08 -0700809 irb_.Runtime().EmitMarkGCCard(call_inst.getArgOperand(0), call_inst.getArgOperand(1));
TDYa1279a129452012-07-19 03:10:08 -0700810 return;
811}
812
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700813llvm::Value*
814GBCExpanderPass::Expand_LoadStringFromDexCache(llvm::Value* string_idx_value) {
815 uint32_t string_idx =
816 llvm::cast<llvm::ConstantInt>(string_idx_value)->getZExtValue();
817
818 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
819
TDYa127ce4cc0d2012-11-18 16:59:53 -0800820 return irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700821}
822
823llvm::Value*
824GBCExpanderPass::Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value) {
825 uint32_t type_idx =
826 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
827
828 llvm::Value* type_field_addr =
829 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
830
TDYa127ce4cc0d2012-11-18 16:59:53 -0800831 return irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700832}
833
834void GBCExpanderPass::Expand_LockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700835 rtb_.EmitLockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700836 return;
837}
838
839void GBCExpanderPass::Expand_UnlockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700840 rtb_.EmitUnlockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700841 return;
842}
843
844llvm::Value* GBCExpanderPass::Expand_ArrayGet(llvm::Value* array_addr,
845 llvm::Value* index_value,
846 JType elem_jty) {
847 llvm::Value* array_elem_addr =
848 EmitArrayGEP(array_addr, index_value, elem_jty);
849
850 return irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
851}
852
853void GBCExpanderPass::Expand_ArrayPut(llvm::Value* new_value,
854 llvm::Value* array_addr,
855 llvm::Value* index_value,
856 JType elem_jty) {
857 llvm::Value* array_elem_addr =
858 EmitArrayGEP(array_addr, index_value, elem_jty);
859
860 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
861
862 return;
863}
864
865void GBCExpanderPass::Expand_FilledNewArray(llvm::CallInst& call_inst) {
866 // Most of the codes refer to MethodCompiler::EmitInsn_FilledNewArray
867 llvm::Value* array = call_inst.getArgOperand(0);
868
869 uint32_t element_jty =
870 llvm::cast<llvm::ConstantInt>(call_inst.getArgOperand(1))->getZExtValue();
871
872 DCHECK(call_inst.getNumArgOperands() > 2);
873 unsigned num_elements = (call_inst.getNumArgOperands() - 2);
874
875 bool is_elem_int_ty = (static_cast<JType>(element_jty) == kInt);
876
877 uint32_t alignment;
878 llvm::Constant* elem_size;
879 llvm::PointerType* field_type;
880
881 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
882 // as the element, thus we are only checking 2 cases: primitive int and
883 // non-primitive type.
884 if (is_elem_int_ty) {
885 alignment = sizeof(int32_t);
886 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
887 field_type = irb_.getJIntTy()->getPointerTo();
888 } else {
889 alignment = irb_.getSizeOfPtrEquivInt();
890 elem_size = irb_.getSizeOfPtrEquivIntValue();
891 field_type = irb_.getJObjectTy()->getPointerTo();
892 }
893
894 llvm::Value* data_field_offset =
Ian Rogers98573f92013-01-30 17:26:32 -0800895 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700896
897 llvm::Value* data_field_addr =
898 irb_.CreatePtrDisp(array, data_field_offset, field_type);
899
900 for (unsigned i = 0; i < num_elements; ++i) {
901 // Values to fill the array begin at the 3rd argument
902 llvm::Value* reg_value = call_inst.getArgOperand(2 + i);
903
904 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
905
906 data_field_addr =
907 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
908 }
909
910 return;
911}
912
913llvm::Value* GBCExpanderPass::Expand_IGetFast(llvm::Value* field_offset_value,
914 llvm::Value* /*is_volatile_value*/,
915 llvm::Value* object_addr,
916 JType field_jty) {
917 int field_offset =
918 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
919
920 DCHECK_GE(field_offset, 0);
921
922 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800923 irb_.getJType(field_jty)->getPointerTo();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700924
925 field_offset_value = irb_.getPtrEquivInt(field_offset);
926
927 llvm::Value* field_addr =
928 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
929
930 // TODO: Check is_volatile. We need to generate atomic load instruction
931 // when is_volatile is true.
932 return irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
933}
934
935void GBCExpanderPass::Expand_IPutFast(llvm::Value* field_offset_value,
936 llvm::Value* /* is_volatile_value */,
937 llvm::Value* object_addr,
938 llvm::Value* new_value,
939 JType field_jty) {
940 int field_offset =
941 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
942
943 DCHECK_GE(field_offset, 0);
944
945 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800946 irb_.getJType(field_jty)->getPointerTo();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700947
948 field_offset_value = irb_.getPtrEquivInt(field_offset);
949
950 llvm::Value* field_addr =
951 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
952
953 // TODO: Check is_volatile. We need to generate atomic store instruction
954 // when is_volatile is true.
955 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
956
957 return;
958}
959
960llvm::Value* GBCExpanderPass::Expand_SGetFast(llvm::Value* static_storage_addr,
961 llvm::Value* field_offset_value,
962 llvm::Value* /*is_volatile_value*/,
963 JType field_jty) {
964 int field_offset =
965 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
966
967 DCHECK_GE(field_offset, 0);
968
969 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
970
971 llvm::Value* static_field_addr =
972 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800973 irb_.getJType(field_jty)->getPointerTo());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700974
975 // TODO: Check is_volatile. We need to generate atomic store instruction
976 // when is_volatile is true.
977 return irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
978}
979
980void GBCExpanderPass::Expand_SPutFast(llvm::Value* static_storage_addr,
981 llvm::Value* field_offset_value,
982 llvm::Value* /* is_volatile_value */,
983 llvm::Value* new_value,
984 JType field_jty) {
985 int field_offset =
986 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
987
988 DCHECK_GE(field_offset, 0);
989
990 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
991
992 llvm::Value* static_field_addr =
993 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800994 irb_.getJType(field_jty)->getPointerTo());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700995
996 // TODO: Check is_volatile. We need to generate atomic store instruction
997 // when is_volatile is true.
998 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
999
1000 return;
1001}
1002
1003llvm::Value*
1004GBCExpanderPass::Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr) {
1005 return irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001006 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001007 irb_.getJObjectTy(),
1008 kTBAAConstJObject);
1009}
1010
1011llvm::Value*
1012GBCExpanderPass::Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value) {
1013 uint32_t type_idx =
1014 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
1015
1016 llvm::Value* storage_field_addr =
1017 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1018
TDYa127ce4cc0d2012-11-18 16:59:53 -08001019 return irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001020}
1021
1022llvm::Value*
1023GBCExpanderPass::Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value) {
1024 uint32_t callee_method_idx =
1025 llvm::cast<llvm::ConstantInt>(callee_method_idx_value)->getZExtValue();
1026
1027 return EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
1028}
1029
1030llvm::Value* GBCExpanderPass::Expand_GetVirtualCalleeMethodObjAddrFast(
1031 llvm::Value* vtable_idx_value,
1032 llvm::Value* this_addr) {
1033 int vtable_idx =
1034 llvm::cast<llvm::ConstantInt>(vtable_idx_value)->getSExtValue();
1035
1036 return EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
1037}
1038
1039llvm::Value* GBCExpanderPass::Expand_Invoke(llvm::CallInst& call_inst) {
1040 // Most of the codes refer to MethodCompiler::EmitInsn_Invoke
1041 llvm::Value* callee_method_object_addr = call_inst.getArgOperand(0);
1042 unsigned num_args = call_inst.getNumArgOperands();
1043 llvm::Type* ret_type = call_inst.getType();
1044
1045 // Determine the function type of the callee method
1046 std::vector<llvm::Type*> args_type;
1047 std::vector<llvm::Value*> args;
1048 for (unsigned i = 0; i < num_args; i++) {
1049 args.push_back(call_inst.getArgOperand(i));
1050 args_type.push_back(args[i]->getType());
1051 }
1052
1053 llvm::FunctionType* callee_method_type =
1054 llvm::FunctionType::get(ret_type, args_type, false);
1055
1056 llvm::Value* code_addr =
1057 irb_.LoadFromObjectOffset(callee_method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001058 art::mirror::AbstractMethod::GetCodeOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001059 callee_method_type->getPointerTo(),
TDYa127ce4cc0d2012-11-18 16:59:53 -08001060 kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001061
1062 // Invoke callee
1063 llvm::Value* retval = irb_.CreateCall(code_addr, args);
1064
1065 return retval;
1066}
1067
TDYa1274ec8ccd2012-08-11 07:04:57 -07001068llvm::Value* GBCExpanderPass::Expand_DivRem(llvm::CallInst& call_inst,
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001069 bool is_div, JType op_jty) {
TDYa1274ec8ccd2012-08-11 07:04:57 -07001070 llvm::Value* dividend = call_inst.getArgOperand(0);
1071 llvm::Value* divisor = call_inst.getArgOperand(1);
TDYa1274ec8ccd2012-08-11 07:04:57 -07001072 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1073 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001074 // Most of the codes refer to MethodCompiler::EmitIntDivRemResultComputation
1075
1076 // Check the special case: MININT / -1 = MININT
1077 // That case will cause overflow, which is undefined behavior in llvm.
1078 // So we check the divisor is -1 or not, if the divisor is -1, we do
1079 // the special path to avoid undefined behavior.
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001080 llvm::Type* op_type = irb_.getJType(op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001081 llvm::Value* zero = irb_.getJZero(op_jty);
1082 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
1083
TDYa1275e869b62012-07-25 00:45:39 -07001084 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001085 llvm::BasicBlock* eq_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1086 llvm::BasicBlock* ne_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1087 llvm::BasicBlock* neg_one_cont =
1088 llvm::BasicBlock::Create(context_, "", parent);
1089
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001090 llvm::Value* is_equal_neg_one = irb_.CreateICmpEQ(divisor, neg_one);
1091 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
1092
1093 // If divisor == -1
1094 irb_.SetInsertPoint(eq_neg_one);
1095 llvm::Value* eq_result;
1096 if (is_div) {
1097 // We can just change from "dividend div -1" to "neg dividend". The sub
1098 // don't care the sign/unsigned because of two's complement representation.
1099 // And the behavior is what we want:
1100 // -(2^n) (2^n)-1
1101 // MININT < k <= MAXINT -> mul k -1 = -k
1102 // MININT == k -> mul k -1 = k
1103 //
1104 // LLVM use sub to represent 'neg'
1105 eq_result = irb_.CreateSub(zero, dividend);
1106 } else {
1107 // Everything modulo -1 will be 0.
1108 eq_result = zero;
1109 }
1110 irb_.CreateBr(neg_one_cont);
1111
1112 // If divisor != -1, just do the division.
1113 irb_.SetInsertPoint(ne_neg_one);
1114 llvm::Value* ne_result;
1115 if (is_div) {
1116 ne_result = irb_.CreateSDiv(dividend, divisor);
1117 } else {
1118 ne_result = irb_.CreateSRem(dividend, divisor);
1119 }
1120 irb_.CreateBr(neg_one_cont);
1121
1122 irb_.SetInsertPoint(neg_one_cont);
1123 llvm::PHINode* result = irb_.CreatePHI(op_type, 2);
1124 result->addIncoming(eq_result, eq_neg_one);
1125 result->addIncoming(ne_result, ne_neg_one);
1126
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001127 return result;
1128}
1129
TDYa127ce4cc0d2012-11-18 16:59:53 -08001130void GBCExpanderPass::Expand_AllocaShadowFrame(llvm::Value* num_vregs_value) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001131 // Most of the codes refer to MethodCompiler::EmitPrologueAllocShadowFrame and
1132 // MethodCompiler::EmitPushShadowFrame
TDYa1278e950c12012-11-02 09:58:19 -07001133 uint16_t num_vregs =
1134 llvm::cast<llvm::ConstantInt>(num_vregs_value)->getZExtValue();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001135
1136 llvm::StructType* shadow_frame_type =
TDYa127ce4cc0d2012-11-18 16:59:53 -08001137 irb_.getShadowFrameTy(num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001138
Sebastien Hertz77209702013-02-28 16:34:13 +01001139 // Create allocas at the start of entry block.
1140 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1141 llvm::BasicBlock* entry_block = &func_->front();
1142 irb_.SetInsertPoint(&entry_block->front());
1143
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001144 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
1145
1146 // Alloca a pointer to old shadow frame
1147 old_shadow_frame_ =
1148 irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
1149
Sebastien Hertz77209702013-02-28 16:34:13 +01001150 irb_.restoreIP(irb_ip_original);
1151
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001152 // Push the shadow frame
1153 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1154
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001155 llvm::Value* shadow_frame_upcast =
1156 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
1157
1158 llvm::Value* result = rtb_.EmitPushShadowFrame(shadow_frame_upcast,
1159 method_object_addr,
TDYa1278e950c12012-11-02 09:58:19 -07001160 num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001161
1162 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
1163
1164 return;
1165}
1166
TDYa1278e950c12012-11-02 09:58:19 -07001167void GBCExpanderPass::Expand_SetVReg(llvm::Value* entry_idx,
1168 llvm::Value* value) {
1169 DCHECK(shadow_frame_ != NULL);
1170
1171 llvm::Value* gep_index[] = {
1172 irb_.getInt32(0), // No pointer displacement
TDYa127ce4cc0d2012-11-18 16:59:53 -08001173 irb_.getInt32(1), // VRegs
TDYa1278e950c12012-11-02 09:58:19 -07001174 entry_idx // Pointer field
1175 };
1176
1177 llvm::Value* vreg_addr = irb_.CreateGEP(shadow_frame_, gep_index);
1178
1179 irb_.CreateStore(value,
1180 irb_.CreateBitCast(vreg_addr, value->getType()->getPointerTo()),
1181 kTBAAShadowFrame);
1182 return;
1183}
1184
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001185void GBCExpanderPass::Expand_PopShadowFrame() {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001186 if (old_shadow_frame_ == NULL) {
1187 return;
1188 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001189 rtb_.EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
1190 return;
1191}
1192
1193void GBCExpanderPass::Expand_UpdateDexPC(llvm::Value* dex_pc_value) {
1194 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07001195 art::ShadowFrame::DexPCOffset(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001196 dex_pc_value,
1197 kTBAAShadowFrame);
1198 return;
1199}
1200
Logan Chien67645d82012-08-17 09:10:54 +08001201void GBCExpanderPass::InsertStackOverflowCheck(llvm::Function& func) {
jeffhao40283122013-01-15 13:15:24 -08001202 // All alloca instructions are generated in the first basic block of the
1203 // function, and there are no alloca instructions after the first non-alloca
1204 // instruction.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001205
Logan Chien67645d82012-08-17 09:10:54 +08001206 llvm::BasicBlock* first_basic_block = &func.front();
1207
1208 // Look for first non-alloca instruction
1209 llvm::BasicBlock::iterator first_non_alloca = first_basic_block->begin();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001210 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1211 ++first_non_alloca;
1212 }
1213
Logan Chien67645d82012-08-17 09:10:54 +08001214 irb_.SetInsertPoint(first_non_alloca);
1215
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001216 // Insert stack overflow check codes before first_non_alloca (i.e., after all
1217 // alloca instructions)
Logan Chien67645d82012-08-17 09:10:54 +08001218 EmitStackOverflowCheck(&*first_non_alloca);
1219
TDYa127890ea892012-08-22 10:49:42 -07001220 irb_.Runtime().EmitTestSuspend();
TDYa127890ea892012-08-22 10:49:42 -07001221
Logan Chien67645d82012-08-17 09:10:54 +08001222 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
1223 if (next_basic_block != first_basic_block) {
1224 // Splice the rest of the instruction to the continuing basic block
1225 next_basic_block->getInstList().splice(
1226 irb_.GetInsertPoint(), first_basic_block->getInstList(),
1227 first_non_alloca, first_basic_block->end());
1228
1229 // Rewrite the basic block
1230 RewriteBasicBlock(next_basic_block);
1231
1232 // Update the phi-instructions in the successor basic block
1233 UpdatePhiInstruction(first_basic_block, irb_.GetInsertBlock());
1234 }
1235
1236 // We have changed the basic block
1237 changed_ = true;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001238}
1239
TDYa1275e869b62012-07-25 00:45:39 -07001240// ==== High-level intrinsic expander ==========================================
1241
TDYa127a1b21852012-07-23 03:20:39 -07001242llvm::Value* GBCExpanderPass::Expand_FPCompare(llvm::Value* src1_value,
1243 llvm::Value* src2_value,
1244 bool gt_bias) {
1245 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1246 llvm::Value* cmp_lt;
1247
1248 if (gt_bias) {
1249 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1250 } else {
1251 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1252 }
1253
1254 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1255}
1256
1257llvm::Value* GBCExpanderPass::Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value) {
1258 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1259 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1260
1261 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1262}
1263
1264llvm::Value* GBCExpanderPass::EmitCompareResultSelection(llvm::Value* cmp_eq,
1265 llvm::Value* cmp_lt) {
1266
1267 llvm::Constant* zero = irb_.getJInt(0);
1268 llvm::Constant* pos1 = irb_.getJInt(1);
1269 llvm::Constant* neg1 = irb_.getJInt(-1);
1270
1271 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1272 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1273
1274 return result_eq;
1275}
1276
Logan Chien75e4b602012-07-23 14:24:12 -07001277llvm::Value* GBCExpanderPass::Expand_IntegerShift(llvm::Value* src1_value,
1278 llvm::Value* src2_value,
1279 IntegerShiftKind kind,
1280 JType op_jty) {
1281 DCHECK(op_jty == kInt || op_jty == kLong);
1282
1283 // Mask and zero-extend RHS properly
1284 if (op_jty == kInt) {
1285 src2_value = irb_.CreateAnd(src2_value, 0x1f);
1286 } else {
1287 llvm::Value* masked_src2_value = irb_.CreateAnd(src2_value, 0x3f);
1288 src2_value = irb_.CreateZExt(masked_src2_value, irb_.getJLongTy());
1289 }
1290
1291 // Create integer shift llvm instruction
1292 switch (kind) {
1293 case kIntegerSHL:
1294 return irb_.CreateShl(src1_value, src2_value);
1295
1296 case kIntegerSHR:
1297 return irb_.CreateAShr(src1_value, src2_value);
1298
1299 case kIntegerUSHR:
1300 return irb_.CreateLShr(src1_value, src2_value);
1301
1302 default:
1303 LOG(FATAL) << "Unknown integer shift kind: " << kind;
1304 return NULL;
1305 }
1306}
1307
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001308llvm::Value* GBCExpanderPass::SignOrZeroExtendCat1Types(llvm::Value* value, JType jty) {
1309 switch (jty) {
1310 case kBoolean:
1311 case kChar:
1312 return irb_.CreateZExt(value, irb_.getJType(kInt));
1313 case kByte:
1314 case kShort:
1315 return irb_.CreateSExt(value, irb_.getJType(kInt));
1316 case kVoid:
1317 case kInt:
1318 case kLong:
1319 case kFloat:
1320 case kDouble:
1321 case kObject:
1322 return value; // Nothing to do.
1323 default:
1324 LOG(FATAL) << "Unknown java type: " << jty;
1325 return NULL;
1326 }
1327}
1328
1329llvm::Value* GBCExpanderPass::TruncateCat1Types(llvm::Value* value, JType jty) {
1330 switch (jty) {
1331 case kBoolean:
1332 case kChar:
1333 case kByte:
1334 case kShort:
1335 return irb_.CreateTrunc(value, irb_.getJType(jty));
1336 case kVoid:
1337 case kInt:
1338 case kLong:
1339 case kFloat:
1340 case kDouble:
1341 case kObject:
1342 return value; // Nothing to do.
1343 default:
1344 LOG(FATAL) << "Unknown java type: " << jty;
1345 return NULL;
1346 }
1347}
1348
TDYa1275a26d442012-07-26 18:58:38 -07001349llvm::Value* GBCExpanderPass::Expand_HLArrayGet(llvm::CallInst& call_inst,
1350 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001351 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1352 llvm::Value* array_addr = call_inst.getArgOperand(1);
1353 llvm::Value* index_value = call_inst.getArgOperand(2);
TDYa127920be7c2012-09-10 17:13:22 -07001354 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001355
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001356 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1357 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1358 opt_flags);
TDYa1275a26d442012-07-26 18:58:38 -07001359
1360 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1361
1362 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
1363
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001364 return SignOrZeroExtendCat1Types(array_elem_value, elem_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001365}
1366
1367
1368void GBCExpanderPass::Expand_HLArrayPut(llvm::CallInst& call_inst,
1369 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001370 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1371 llvm::Value* new_value = call_inst.getArgOperand(1);
1372 llvm::Value* array_addr = call_inst.getArgOperand(2);
1373 llvm::Value* index_value = call_inst.getArgOperand(3);
TDYa127920be7c2012-09-10 17:13:22 -07001374 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001375
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001376 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1377 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1378 opt_flags);
TDYa1275a26d442012-07-26 18:58:38 -07001379
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001380 new_value = TruncateCat1Types(new_value, elem_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001381
1382 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1383
1384 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
1385 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::CheckPutArrayElement);
1386
1387 irb_.CreateCall2(runtime_func, new_value, array_addr);
1388
1389 EmitGuard_ExceptionLandingPad(dex_pc);
1390
1391 EmitMarkGCCard(new_value, array_addr);
1392 }
1393
1394 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1395
1396 return;
1397}
1398
TDYa1275e869b62012-07-25 00:45:39 -07001399llvm::Value* GBCExpanderPass::Expand_HLIGet(llvm::CallInst& call_inst,
1400 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001401 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1402 llvm::Value* object_addr = call_inst.getArgOperand(1);
1403 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(2));
TDYa127920be7c2012-09-10 17:13:22 -07001404 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001405
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001406 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -07001407
1408 llvm::Value* field_value;
1409
1410 int field_offset;
1411 bool is_volatile;
1412 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
1413 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
1414
1415 if (!is_fast_path) {
1416 llvm::Function* runtime_func;
1417
1418 if (field_jty == kObject) {
1419 runtime_func = irb_.GetRuntime(runtime_support::GetObjectInstance);
1420 } else if (field_jty == kLong || field_jty == kDouble) {
1421 runtime_func = irb_.GetRuntime(runtime_support::Get64Instance);
1422 } else {
1423 runtime_func = irb_.GetRuntime(runtime_support::Get32Instance);
1424 }
1425
1426 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
1427
1428 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1429
1430 EmitUpdateDexPC(dex_pc);
1431
1432 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
1433 method_object_addr, object_addr);
1434
1435 EmitGuard_ExceptionLandingPad(dex_pc);
1436
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001437 if (field_jty == kFloat || field_jty == kDouble) {
1438 field_value = irb_.CreateBitCast(field_value, irb_.getJType(field_jty));
1439 }
TDYa1275e869b62012-07-25 00:45:39 -07001440 } else {
1441 DCHECK_GE(field_offset, 0);
1442
1443 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001444 irb_.getJType(field_jty)->getPointerTo();
TDYa1275e869b62012-07-25 00:45:39 -07001445
1446 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
1447
1448 llvm::Value* field_addr =
1449 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1450
1451 // TODO: Check is_volatile. We need to generate atomic load instruction
1452 // when is_volatile is true.
1453 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001454 field_value = SignOrZeroExtendCat1Types(field_value, field_jty);
TDYa1275e869b62012-07-25 00:45:39 -07001455 }
1456
TDYa1275e869b62012-07-25 00:45:39 -07001457 return field_value;
1458}
1459
1460void GBCExpanderPass::Expand_HLIPut(llvm::CallInst& call_inst,
1461 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001462 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001463 llvm::Value* new_value = call_inst.getArgOperand(1);
1464 llvm::Value* object_addr = call_inst.getArgOperand(2);
TDYa1275e869b62012-07-25 00:45:39 -07001465 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(3));
TDYa127920be7c2012-09-10 17:13:22 -07001466 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001467
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001468 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -07001469
1470 int field_offset;
1471 bool is_volatile;
1472 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
1473 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
1474
1475 if (!is_fast_path) {
1476 llvm::Function* runtime_func;
1477
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001478 if (field_jty == kFloat) {
1479 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1480 } else if (field_jty == kDouble) {
1481 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1482 }
1483
TDYa1275e869b62012-07-25 00:45:39 -07001484 if (field_jty == kObject) {
1485 runtime_func = irb_.GetRuntime(runtime_support::SetObjectInstance);
1486 } else if (field_jty == kLong || field_jty == kDouble) {
1487 runtime_func = irb_.GetRuntime(runtime_support::Set64Instance);
1488 } else {
1489 runtime_func = irb_.GetRuntime(runtime_support::Set32Instance);
1490 }
1491
1492 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1493
1494 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1495
1496 EmitUpdateDexPC(dex_pc);
1497
1498 irb_.CreateCall4(runtime_func, field_idx_value,
1499 method_object_addr, object_addr, new_value);
1500
1501 EmitGuard_ExceptionLandingPad(dex_pc);
1502
1503 } else {
1504 DCHECK_GE(field_offset, 0);
1505
1506 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001507 irb_.getJType(field_jty)->getPointerTo();
TDYa1275e869b62012-07-25 00:45:39 -07001508
1509 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
1510
1511 llvm::Value* field_addr =
1512 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1513
1514 // TODO: Check is_volatile. We need to generate atomic store instruction
1515 // when is_volatile is true.
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001516 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275e869b62012-07-25 00:45:39 -07001517 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1518
1519 if (field_jty == kObject) { // If put an object, mark the GC card table.
1520 EmitMarkGCCard(new_value, object_addr);
1521 }
1522 }
1523
1524 return;
1525}
1526
TDYa127f71bf5a2012-07-29 20:09:52 -07001527llvm::Value* GBCExpanderPass::EmitLoadConstantClass(uint32_t dex_pc,
1528 uint32_t type_idx) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001529 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, *dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001530 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1531
1532 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1533
1534 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1535
1536 llvm::Function* runtime_func =
1537 irb_.GetRuntime(runtime_support::InitializeTypeAndVerifyAccess);
1538
1539 EmitUpdateDexPC(dex_pc);
1540
1541 llvm::Value* type_object_addr =
1542 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1543
1544 EmitGuard_ExceptionLandingPad(dex_pc);
1545
1546 return type_object_addr;
1547
1548 } else {
1549 // Try to load the class (type) object from the test cache.
1550 llvm::Value* type_field_addr =
1551 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1552
TDYa127ce4cc0d2012-11-18 16:59:53 -08001553 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07001554
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001555 if (compiler_->CanAssumeTypeIsPresentInDexCache(*dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001556 return type_object_addr;
1557 }
1558
1559 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1560
1561 // Test whether class (type) object is in the dex cache or not
1562 llvm::Value* equal_null =
1563 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1564
1565 llvm::BasicBlock* block_cont =
1566 CreateBasicBlockWithDexPC(dex_pc, "cont");
1567
1568 llvm::BasicBlock* block_load_class =
1569 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1570
1571 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
1572
1573 // Failback routine to load the class object
1574 irb_.SetInsertPoint(block_load_class);
1575
1576 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeType);
1577
1578 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1579
1580 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1581
1582 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1583
1584 EmitUpdateDexPC(dex_pc);
1585
1586 llvm::Value* loaded_type_object_addr =
1587 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1588
1589 EmitGuard_ExceptionLandingPad(dex_pc);
1590
1591 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1592
1593 irb_.CreateBr(block_cont);
1594
1595 // Now the class object must be loaded
1596 irb_.SetInsertPoint(block_cont);
1597
1598 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1599
1600 phi->addIncoming(type_object_addr, block_original);
1601 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1602
1603 return phi;
1604 }
1605}
1606
TDYa1275a26d442012-07-26 18:58:38 -07001607llvm::Value* GBCExpanderPass::EmitLoadStaticStorage(uint32_t dex_pc,
1608 uint32_t type_idx) {
1609 llvm::BasicBlock* block_load_static =
1610 CreateBasicBlockWithDexPC(dex_pc, "load_static");
1611
1612 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1613
1614 // Load static storage from dex cache
1615 llvm::Value* storage_field_addr =
1616 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1617
TDYa127ce4cc0d2012-11-18 16:59:53 -08001618 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
TDYa1275a26d442012-07-26 18:58:38 -07001619
1620 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1621
1622 // Test: Is the static storage of this class initialized?
1623 llvm::Value* equal_null =
1624 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
1625
1626 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
1627
1628 // Failback routine to load the class object
1629 irb_.SetInsertPoint(block_load_static);
1630
1631 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeStaticStorage);
1632
1633 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1634
1635 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1636
1637 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1638
1639 EmitUpdateDexPC(dex_pc);
1640
1641 llvm::Value* loaded_storage_object_addr =
1642 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1643
1644 EmitGuard_ExceptionLandingPad(dex_pc);
1645
1646 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
1647
1648 irb_.CreateBr(block_cont);
1649
1650 // Now the class object must be loaded
1651 irb_.SetInsertPoint(block_cont);
1652
1653 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1654
1655 phi->addIncoming(storage_object_addr, block_original);
1656 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
1657
1658 return phi;
1659}
1660
1661llvm::Value* GBCExpanderPass::Expand_HLSget(llvm::CallInst& call_inst,
1662 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001663 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1664 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1665
1666 int field_offset;
1667 int ssb_index;
1668 bool is_referrers_class;
1669 bool is_volatile;
1670
1671 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
1672 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1673 is_referrers_class, is_volatile, false);
1674
1675 llvm::Value* static_field_value;
1676
1677 if (!is_fast_path) {
1678 llvm::Function* runtime_func;
1679
1680 if (field_jty == kObject) {
1681 runtime_func = irb_.GetRuntime(runtime_support::GetObjectStatic);
1682 } else if (field_jty == kLong || field_jty == kDouble) {
1683 runtime_func = irb_.GetRuntime(runtime_support::Get64Static);
1684 } else {
1685 runtime_func = irb_.GetRuntime(runtime_support::Get32Static);
1686 }
1687
1688 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1689
1690 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1691
1692 EmitUpdateDexPC(dex_pc);
1693
1694 static_field_value =
1695 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
1696
1697 EmitGuard_ExceptionLandingPad(dex_pc);
1698
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001699 if (field_jty == kFloat || field_jty == kDouble) {
1700 static_field_value = irb_.CreateBitCast(static_field_value, irb_.getJType(field_jty));
1701 }
TDYa1275a26d442012-07-26 18:58:38 -07001702 } 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,
Ian Rogers98573f92013-01-30 17:26:32 -08001713 art::mirror::AbstractMethod::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,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001727 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001728
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);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001732 static_field_value = SignOrZeroExtendCat1Types(static_field_value, field_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001733 }
1734
TDYa1275a26d442012-07-26 18:58:38 -07001735 return static_field_value;
1736}
1737
1738void GBCExpanderPass::Expand_HLSput(llvm::CallInst& call_inst,
1739 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001740 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1741 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1742 llvm::Value* new_value = call_inst.getArgOperand(1);
1743
1744 if (field_jty == kFloat || field_jty == kDouble) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001745 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty));
TDYa1275a26d442012-07-26 18:58:38 -07001746 }
1747
1748 int field_offset;
1749 int ssb_index;
1750 bool is_referrers_class;
1751 bool is_volatile;
1752
1753 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
1754 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1755 is_referrers_class, is_volatile, true);
1756
1757 if (!is_fast_path) {
1758 llvm::Function* runtime_func;
1759
1760 if (field_jty == kObject) {
1761 runtime_func = irb_.GetRuntime(runtime_support::SetObjectStatic);
1762 } else if (field_jty == kLong || field_jty == kDouble) {
1763 runtime_func = irb_.GetRuntime(runtime_support::Set64Static);
1764 } else {
1765 runtime_func = irb_.GetRuntime(runtime_support::Set32Static);
1766 }
1767
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001768 if (field_jty == kFloat) {
1769 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1770 } else if (field_jty == kDouble) {
1771 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1772 }
1773
TDYa1275a26d442012-07-26 18:58:38 -07001774 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1775
1776 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1777
1778 EmitUpdateDexPC(dex_pc);
1779
1780 irb_.CreateCall3(runtime_func, field_idx_value,
1781 method_object_addr, new_value);
1782
1783 EmitGuard_ExceptionLandingPad(dex_pc);
1784
1785 } else {
1786 DCHECK_GE(field_offset, 0);
1787
1788 llvm::Value* static_storage_addr = NULL;
1789
1790 if (is_referrers_class) {
1791 // Fast path, static storage base is this method's class
1792 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1793
1794 static_storage_addr =
1795 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001796 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001797 irb_.getJObjectTy(),
1798 kTBAAConstJObject);
1799 } else {
1800 // Medium path, static storage base in a different class which
1801 // requires checks that the other class is initialized
1802 DCHECK_GE(ssb_index, 0);
1803 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1804 }
1805
1806 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1807
1808 llvm::Value* static_field_addr =
1809 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001810 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001811
1812 // TODO: Check is_volatile. We need to generate atomic store instruction
1813 // when is_volatile is true.
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001814 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001815 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1816
1817 if (field_jty == kObject) { // If put an object, mark the GC card table.
1818 EmitMarkGCCard(new_value, static_storage_addr);
1819 }
1820 }
1821
1822 return;
1823}
1824
TDYa127f71bf5a2012-07-29 20:09:52 -07001825llvm::Value* GBCExpanderPass::Expand_ConstString(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001826 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1827 uint32_t string_idx = LV2UInt(call_inst.getArgOperand(0));
1828
1829 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1830
TDYa127ce4cc0d2012-11-18 16:59:53 -08001831 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07001832
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001833 if (!compiler_->CanAssumeStringIsPresentInDexCache(*dex_file_, string_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001834 llvm::BasicBlock* block_str_exist =
1835 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1836
1837 llvm::BasicBlock* block_str_resolve =
1838 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1839
1840 llvm::BasicBlock* block_cont =
1841 CreateBasicBlockWithDexPC(dex_pc, "str_cont");
1842
1843 // Test: Is the string resolved and in the dex cache?
1844 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1845
1846 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
1847
1848 // String is resolved, go to next basic block.
1849 irb_.SetInsertPoint(block_str_exist);
1850 irb_.CreateBr(block_cont);
1851
1852 // String is not resolved yet, resolve it now.
1853 irb_.SetInsertPoint(block_str_resolve);
1854
1855 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::ResolveString);
1856
1857 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1858
1859 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1860
1861 EmitUpdateDexPC(dex_pc);
1862
1863 llvm::Value* result = irb_.CreateCall2(runtime_func, method_object_addr,
1864 string_idx_value);
1865
1866 EmitGuard_ExceptionLandingPad(dex_pc);
1867
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001868 irb_.CreateBr(block_cont);
1869
1870
TDYa127f71bf5a2012-07-29 20:09:52 -07001871 llvm::BasicBlock* block_pre_cont = irb_.GetInsertBlock();
1872
1873 irb_.SetInsertPoint(block_cont);
1874
1875 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1876
1877 phi->addIncoming(string_addr, block_str_exist);
1878 phi->addIncoming(result, block_pre_cont);
1879
1880 string_addr = phi;
1881 }
1882
1883 return string_addr;
1884}
1885
1886llvm::Value* GBCExpanderPass::Expand_ConstClass(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001887 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1888 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1889
1890 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
1891
1892 return type_object_addr;
1893}
1894
1895void GBCExpanderPass::Expand_MonitorEnter(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001896 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1897 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07001898 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07001899
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001900 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07001901
TDYa127ce4cc0d2012-11-18 16:59:53 -08001902 EmitUpdateDexPC(dex_pc);
1903
TDYa127f71bf5a2012-07-29 20:09:52 -07001904 irb_.Runtime().EmitLockObject(object_addr);
1905
1906 return;
1907}
1908
1909void GBCExpanderPass::Expand_MonitorExit(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001910 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1911 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07001912 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07001913
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001914 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07001915
1916 EmitUpdateDexPC(dex_pc);
1917
1918 irb_.Runtime().EmitUnlockObject(object_addr);
1919
1920 EmitGuard_ExceptionLandingPad(dex_pc);
1921
1922 return;
1923}
1924
1925void GBCExpanderPass::Expand_HLCheckCast(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001926 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1927 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1928 llvm::Value* object_addr = call_inst.getArgOperand(1);
1929
1930 llvm::BasicBlock* block_test_class =
1931 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1932
1933 llvm::BasicBlock* block_test_sub_class =
1934 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1935
1936 llvm::BasicBlock* block_cont =
1937 CreateBasicBlockWithDexPC(dex_pc, "checkcast_cont");
1938
1939 // Test: Is the reference equal to null? Act as no-op when it is null.
1940 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1941
1942 irb_.CreateCondBr(equal_null,
1943 block_cont,
1944 block_test_class);
1945
1946 // Test: Is the object instantiated from the given class?
1947 irb_.SetInsertPoint(block_test_class);
1948 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08001949 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07001950
1951 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1952
1953 llvm::Value* object_type_field_addr =
1954 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1955
1956 llvm::Value* object_type_object_addr =
1957 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
1958
1959 llvm::Value* equal_class =
1960 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1961
1962 irb_.CreateCondBr(equal_class,
1963 block_cont,
1964 block_test_sub_class);
1965
1966 // Test: Is the object instantiated from the subclass of the given class?
1967 irb_.SetInsertPoint(block_test_sub_class);
1968
1969 EmitUpdateDexPC(dex_pc);
1970
1971 irb_.CreateCall2(irb_.GetRuntime(runtime_support::CheckCast),
1972 type_object_addr, object_type_object_addr);
1973
1974 EmitGuard_ExceptionLandingPad(dex_pc);
1975
1976 irb_.CreateBr(block_cont);
1977
1978 irb_.SetInsertPoint(block_cont);
1979
1980 return;
1981}
1982
1983llvm::Value* GBCExpanderPass::Expand_InstanceOf(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001984 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1985 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1986 llvm::Value* object_addr = call_inst.getArgOperand(1);
1987
1988 llvm::BasicBlock* block_nullp =
1989 CreateBasicBlockWithDexPC(dex_pc, "nullp");
1990
1991 llvm::BasicBlock* block_test_class =
1992 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1993
1994 llvm::BasicBlock* block_class_equals =
1995 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1996
1997 llvm::BasicBlock* block_test_sub_class =
1998 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1999
2000 llvm::BasicBlock* block_cont =
2001 CreateBasicBlockWithDexPC(dex_pc, "instance_of_cont");
2002
2003 // Overview of the following code :
2004 // We check for null, if so, then false, otherwise check for class == . If so
2005 // then true, otherwise do callout slowpath.
2006 //
2007 // Test: Is the reference equal to null? Set 0 when it is null.
2008 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2009
2010 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
2011
2012 irb_.SetInsertPoint(block_nullp);
2013 irb_.CreateBr(block_cont);
2014
2015 // Test: Is the object instantiated from the given class?
2016 irb_.SetInsertPoint(block_test_class);
2017 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08002018 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002019
2020 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2021
2022 llvm::Value* object_type_field_addr =
2023 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2024
2025 llvm::Value* object_type_object_addr =
2026 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2027
2028 llvm::Value* equal_class =
2029 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2030
2031 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
2032
2033 irb_.SetInsertPoint(block_class_equals);
2034 irb_.CreateBr(block_cont);
2035
2036 // Test: Is the object instantiated from the subclass of the given class?
2037 irb_.SetInsertPoint(block_test_sub_class);
2038 llvm::Value* result =
2039 irb_.CreateCall2(irb_.GetRuntime(runtime_support::IsAssignable),
2040 type_object_addr, object_type_object_addr);
2041 irb_.CreateBr(block_cont);
2042
2043 irb_.SetInsertPoint(block_cont);
2044
2045 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJIntTy(), 3);
2046
2047 phi->addIncoming(irb_.getJInt(0), block_nullp);
2048 phi->addIncoming(irb_.getJInt(1), block_class_equals);
2049 phi->addIncoming(result, block_test_sub_class);
2050
2051 return phi;
2052}
2053
2054llvm::Value* GBCExpanderPass::Expand_NewInstance(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002055 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2056 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2057
2058 llvm::Function* runtime_func;
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002059 if (compiler_->CanAccessInstantiableTypeWithoutChecks(method_idx_, *dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002060 runtime_func = irb_.GetRuntime(runtime_support::AllocObject);
2061 } else {
2062 runtime_func = irb_.GetRuntime(runtime_support::AllocObjectWithAccessCheck);
2063 }
2064
2065 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2066
2067 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2068
2069 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2070
2071 EmitUpdateDexPC(dex_pc);
2072
2073 llvm::Value* object_addr =
2074 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
2075
2076 EmitGuard_ExceptionLandingPad(dex_pc);
2077
2078 return object_addr;
2079}
2080
2081llvm::Value* GBCExpanderPass::Expand_HLInvoke(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002082 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
TDYa127920be7c2012-09-10 17:13:22 -07002083 art::InvokeType invoke_type = static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
2084 bool is_static = (invoke_type == art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002085 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
TDYa127920be7c2012-09-10 17:13:22 -07002086 int opt_flags = LV2UInt(call_inst.getArgOperand(2));
TDYa127f71bf5a2012-07-29 20:09:52 -07002087
2088 // Compute invoke related information for compiler decision
2089 int vtable_idx = -1;
2090 uintptr_t direct_code = 0;
2091 uintptr_t direct_method = 0;
2092 bool is_fast_path = compiler_->
2093 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
2094 invoke_type, vtable_idx, direct_code, direct_method);
2095
2096 // Load *this* actual parameter
2097 llvm::Value* this_addr = NULL;
2098
2099 if (!is_static) {
2100 // Test: Is *this* parameter equal to null?
2101 this_addr = call_inst.getArgOperand(3);
2102 }
2103
2104 // Load the method object
2105 llvm::Value* callee_method_object_addr = NULL;
2106
2107 if (!is_fast_path) {
2108 callee_method_object_addr =
2109 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2110 this_addr, dex_pc, is_fast_path);
2111
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002112 if (!is_static) {
2113 EmitGuard_NullPointerException(dex_pc, this_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002114 }
2115 } else {
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002116 if (!is_static) {
2117 EmitGuard_NullPointerException(dex_pc, this_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002118 }
2119
2120 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002121 case art::kStatic:
2122 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002123 if (direct_method != 0u &&
2124 direct_method != static_cast<uintptr_t>(-1)) {
2125 callee_method_object_addr =
2126 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2127 irb_.getJObjectTy());
2128 } else {
2129 callee_method_object_addr =
2130 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2131 }
2132 break;
2133
TDYa127920be7c2012-09-10 17:13:22 -07002134 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002135 DCHECK(vtable_idx != -1);
2136 callee_method_object_addr =
2137 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2138 break;
2139
TDYa127920be7c2012-09-10 17:13:22 -07002140 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002141 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2142 "the fast path.";
2143 break;
2144
TDYa127920be7c2012-09-10 17:13:22 -07002145 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002146 callee_method_object_addr =
2147 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2148 invoke_type, this_addr,
2149 dex_pc, is_fast_path);
2150 break;
2151 }
2152 }
2153
2154 // Load the actual parameter
2155 std::vector<llvm::Value*> args;
2156
2157 args.push_back(callee_method_object_addr); // method object for callee
2158
2159 for (uint32_t i = 3; i < call_inst.getNumArgOperands(); ++i) {
2160 args.push_back(call_inst.getArgOperand(i));
2161 }
2162
2163 llvm::Value* code_addr;
2164 if (direct_code != 0u &&
2165 direct_code != static_cast<uintptr_t>(-1)) {
2166 code_addr =
2167 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_code),
2168 GetFunctionType(callee_method_idx, is_static)->getPointerTo());
2169 } else {
2170 code_addr =
2171 irb_.LoadFromObjectOffset(callee_method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08002172 art::mirror::AbstractMethod::GetCodeOffset().Int32Value(),
TDYa127f71bf5a2012-07-29 20:09:52 -07002173 GetFunctionType(callee_method_idx, is_static)->getPointerTo(),
TDYa127ce4cc0d2012-11-18 16:59:53 -08002174 kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07002175 }
2176
2177 // Invoke callee
2178 EmitUpdateDexPC(dex_pc);
2179 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2180 EmitGuard_ExceptionLandingPad(dex_pc);
2181
2182 return retval;
2183}
2184
2185llvm::Value* GBCExpanderPass::Expand_OptArrayLength(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002186 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2187 // Get the array object address
2188 llvm::Value* array_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002189 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002190
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002191 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002192
2193 // Get the array length and store it to the register
2194 return EmitLoadArrayLength(array_addr);
2195}
2196
2197llvm::Value* GBCExpanderPass::Expand_NewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002198 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2199 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2200 llvm::Value* length = call_inst.getArgOperand(1);
2201
2202 return EmitAllocNewArray(dex_pc, length, type_idx, false);
2203}
2204
2205llvm::Value* GBCExpanderPass::Expand_HLFilledNewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002206 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2207 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(1));
2208 uint32_t length = call_inst.getNumArgOperands() - 3;
2209
2210 llvm::Value* object_addr =
2211 EmitAllocNewArray(dex_pc, irb_.getInt32(length), type_idx, true);
2212
2213 if (length > 0) {
2214 // Check for the element type
2215 uint32_t type_desc_len = 0;
2216 const char* type_desc =
2217 dex_file_->StringByTypeIdx(type_idx, &type_desc_len);
2218
2219 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
2220 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
2221 bool is_elem_int_ty = (type_desc[1] == 'I');
2222
2223 uint32_t alignment;
2224 llvm::Constant* elem_size;
2225 llvm::PointerType* field_type;
2226
2227 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
2228 // as the element, thus we are only checking 2 cases: primitive int and
2229 // non-primitive type.
2230 if (is_elem_int_ty) {
2231 alignment = sizeof(int32_t);
2232 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
2233 field_type = irb_.getJIntTy()->getPointerTo();
2234 } else {
2235 alignment = irb_.getSizeOfPtrEquivInt();
2236 elem_size = irb_.getSizeOfPtrEquivIntValue();
2237 field_type = irb_.getJObjectTy()->getPointerTo();
2238 }
2239
2240 llvm::Value* data_field_offset =
Ian Rogers98573f92013-01-30 17:26:32 -08002241 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
TDYa127f71bf5a2012-07-29 20:09:52 -07002242
2243 llvm::Value* data_field_addr =
2244 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
2245
2246 // TODO: Tune this code. Currently we are generating one instruction for
2247 // one element which may be very space consuming. Maybe changing to use
2248 // memcpy may help; however, since we can't guarantee that the alloca of
2249 // dalvik register are continuous, we can't perform such optimization yet.
2250 for (uint32_t i = 0; i < length; ++i) {
2251 llvm::Value* reg_value = call_inst.getArgOperand(i+3);
2252
2253 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
2254
2255 data_field_addr =
2256 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
2257 }
2258 }
2259
2260 return object_addr;
2261}
2262
2263void GBCExpanderPass::Expand_HLFillArrayData(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002264 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2265 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
2266 LV2SInt(call_inst.getArgOperand(0));
2267 llvm::Value* array_addr = call_inst.getArgOperand(1);
2268
TDYa127920be7c2012-09-10 17:13:22 -07002269 const art::Instruction::ArrayDataPayload* payload =
2270 reinterpret_cast<const art::Instruction::ArrayDataPayload*>(
TDYa127f71bf5a2012-07-29 20:09:52 -07002271 code_item_->insns_ + payload_offset);
2272
2273 if (payload->element_count == 0) {
2274 // When the number of the elements in the payload is zero, we don't have
2275 // to copy any numbers. However, we should check whether the array object
2276 // address is equal to null or not.
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002277 EmitGuard_NullPointerException(dex_pc, array_addr, 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002278 } else {
2279 // To save the code size, we are going to call the runtime function to
2280 // copy the content from DexFile.
2281
2282 // NOTE: We will check for the NullPointerException in the runtime.
2283
2284 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::FillArrayData);
2285
2286 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2287
2288 EmitUpdateDexPC(dex_pc);
2289
2290 irb_.CreateCall4(runtime_func,
2291 method_object_addr, irb_.getInt32(dex_pc),
2292 array_addr, irb_.getInt32(payload_offset));
2293
2294 EmitGuard_ExceptionLandingPad(dex_pc);
2295 }
2296
2297 return;
2298}
2299
2300llvm::Value* GBCExpanderPass::EmitAllocNewArray(uint32_t dex_pc,
2301 llvm::Value* array_length_value,
2302 uint32_t type_idx,
2303 bool is_filled_new_array) {
2304 llvm::Function* runtime_func;
2305
2306 bool skip_access_check =
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002307 compiler_->CanAccessTypeWithoutChecks(method_idx_, *dex_file_, type_idx);
TDYa127f71bf5a2012-07-29 20:09:52 -07002308
2309
2310 if (is_filled_new_array) {
2311 runtime_func = skip_access_check ?
2312 irb_.GetRuntime(runtime_support::CheckAndAllocArray) :
2313 irb_.GetRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck);
2314 } else {
2315 runtime_func = skip_access_check ?
2316 irb_.GetRuntime(runtime_support::AllocArray) :
2317 irb_.GetRuntime(runtime_support::AllocArrayWithAccessCheck);
2318 }
2319
2320 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2321
2322 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2323
2324 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2325
2326 EmitUpdateDexPC(dex_pc);
2327
2328 llvm::Value* object_addr =
2329 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
2330 array_length_value, thread_object_addr);
2331
2332 EmitGuard_ExceptionLandingPad(dex_pc);
2333
2334 return object_addr;
2335}
2336
2337llvm::Value* GBCExpanderPass::
2338EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -07002339 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -07002340 llvm::Value* this_addr,
2341 uint32_t dex_pc,
2342 bool is_fast_path) {
2343
2344 llvm::Function* runtime_func = NULL;
2345
2346 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002347 case art::kStatic:
TDYa127f71bf5a2012-07-29 20:09:52 -07002348 runtime_func = irb_.GetRuntime(runtime_support::FindStaticMethodWithAccessCheck);
2349 break;
2350
TDYa127920be7c2012-09-10 17:13:22 -07002351 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002352 runtime_func = irb_.GetRuntime(runtime_support::FindDirectMethodWithAccessCheck);
2353 break;
2354
TDYa127920be7c2012-09-10 17:13:22 -07002355 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002356 runtime_func = irb_.GetRuntime(runtime_support::FindVirtualMethodWithAccessCheck);
2357 break;
2358
TDYa127920be7c2012-09-10 17:13:22 -07002359 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002360 runtime_func = irb_.GetRuntime(runtime_support::FindSuperMethodWithAccessCheck);
2361 break;
2362
TDYa127920be7c2012-09-10 17:13:22 -07002363 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002364 if (is_fast_path) {
2365 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethod);
2366 } else {
2367 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethodWithAccessCheck);
2368 }
2369 break;
2370 }
2371
2372 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2373
2374 if (this_addr == NULL) {
TDYa127920be7c2012-09-10 17:13:22 -07002375 DCHECK_EQ(invoke_type, art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002376 this_addr = irb_.getJNull();
2377 }
2378
2379 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2380
2381 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2382
2383 EmitUpdateDexPC(dex_pc);
2384
2385 llvm::Value* callee_method_object_addr =
2386 irb_.CreateCall4(runtime_func,
2387 callee_method_idx_value,
2388 this_addr,
2389 caller_method_object_addr,
2390 thread_object_addr);
2391
2392 EmitGuard_ExceptionLandingPad(dex_pc);
2393
2394 return callee_method_object_addr;
2395}
2396
TDYa1275e869b62012-07-25 00:45:39 -07002397void GBCExpanderPass::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2398 // Using runtime support, let the target can override by InlineAssembly.
2399 irb_.Runtime().EmitMarkGCCard(value, target_addr);
2400}
2401
2402void GBCExpanderPass::EmitUpdateDexPC(uint32_t dex_pc) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002403 if (shadow_frame_ == NULL) {
2404 return;
2405 }
TDYa1275e869b62012-07-25 00:45:39 -07002406 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07002407 art::ShadowFrame::DexPCOffset(),
TDYa1275e869b62012-07-25 00:45:39 -07002408 irb_.getInt32(dex_pc),
2409 kTBAAShadowFrame);
2410}
2411
2412void GBCExpanderPass::EmitGuard_DivZeroException(uint32_t dex_pc,
2413 llvm::Value* denominator,
2414 JType op_jty) {
2415 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2416
2417 llvm::Constant* zero = irb_.getJZero(op_jty);
2418
2419 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2420
2421 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2422
2423 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2424
2425 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
2426
2427 irb_.SetInsertPoint(block_exception);
2428 EmitUpdateDexPC(dex_pc);
2429 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowDivZeroException));
2430 EmitBranchExceptionLandingPad(dex_pc);
2431
2432 irb_.SetInsertPoint(block_continue);
2433}
2434
2435void GBCExpanderPass::EmitGuard_NullPointerException(uint32_t dex_pc,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002436 llvm::Value* object,
2437 int opt_flags) {
2438 bool ignore_null_check = ((opt_flags & MIR_IGNORE_NULL_CHECK) != 0);
2439 if (ignore_null_check) {
2440 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2441 if (lpad) {
2442 // There is at least one catch: create a "fake" conditional branch to
2443 // keep the exception edge to the catch block.
2444 landing_pad_phi_mapping_[lpad].push_back(
2445 std::make_pair(current_bb_->getUniquePredecessor(),
2446 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002447
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002448 llvm::BasicBlock* block_continue =
2449 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002450
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002451 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue);
TDYa1275e869b62012-07-25 00:45:39 -07002452
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002453 irb_.SetInsertPoint(block_continue);
2454 }
2455 } else {
2456 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
TDYa1275e869b62012-07-25 00:45:39 -07002457
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002458 llvm::BasicBlock* block_exception =
2459 CreateBasicBlockWithDexPC(dex_pc, "nullp");
TDYa1275e869b62012-07-25 00:45:39 -07002460
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002461 llvm::BasicBlock* block_continue =
2462 CreateBasicBlockWithDexPC(dex_pc, "cont");
2463
2464 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
2465
2466 irb_.SetInsertPoint(block_exception);
2467 EmitUpdateDexPC(dex_pc);
2468 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowNullPointerException),
2469 irb_.getInt32(dex_pc));
2470 EmitBranchExceptionLandingPad(dex_pc);
2471
2472 irb_.SetInsertPoint(block_continue);
2473 }
TDYa1275e869b62012-07-25 00:45:39 -07002474}
2475
2476void
2477GBCExpanderPass::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2478 llvm::Value* array,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002479 llvm::Value* index,
2480 int opt_flags) {
2481 bool ignore_range_check = ((opt_flags & MIR_IGNORE_RANGE_CHECK) != 0);
2482 if (ignore_range_check) {
2483 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2484 if (lpad) {
2485 // There is at least one catch: create a "fake" conditional branch to
2486 // keep the exception edge to the catch block.
2487 landing_pad_phi_mapping_[lpad].push_back(
2488 std::make_pair(current_bb_->getUniquePredecessor(),
2489 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002490
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002491 llvm::BasicBlock* block_continue =
2492 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002493
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002494 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue);
TDYa1275e869b62012-07-25 00:45:39 -07002495
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002496 irb_.SetInsertPoint(block_continue);
2497 }
2498 } else {
2499 llvm::Value* array_len = EmitLoadArrayLength(array);
TDYa1275e869b62012-07-25 00:45:39 -07002500
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002501 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
TDYa1275e869b62012-07-25 00:45:39 -07002502
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002503 llvm::BasicBlock* block_exception =
2504 CreateBasicBlockWithDexPC(dex_pc, "overflow");
TDYa1275e869b62012-07-25 00:45:39 -07002505
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002506 llvm::BasicBlock* block_continue =
2507 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002508
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002509 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
2510
2511 irb_.SetInsertPoint(block_exception);
2512
2513 EmitUpdateDexPC(dex_pc);
2514 irb_.CreateCall2(irb_.GetRuntime(runtime_support::ThrowIndexOutOfBounds), index, array_len);
2515 EmitBranchExceptionLandingPad(dex_pc);
2516
2517 irb_.SetInsertPoint(block_continue);
2518 }
TDYa1275e869b62012-07-25 00:45:39 -07002519}
2520
TDYa1275e869b62012-07-25 00:45:39 -07002521llvm::FunctionType* GBCExpanderPass::GetFunctionType(uint32_t method_idx,
2522 bool is_static) {
2523 // Get method signature
TDYa127920be7c2012-09-10 17:13:22 -07002524 art::DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
TDYa1275e869b62012-07-25 00:45:39 -07002525
2526 uint32_t shorty_size;
2527 const char* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
2528 CHECK_GE(shorty_size, 1u);
2529
2530 // Get return type
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002531
2532 char ret_shorty = shorty[0];
buzbee26f10ee2012-12-21 11:16:29 -08002533 ret_shorty = art::RemapShorty(ret_shorty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002534 llvm::Type* ret_type = irb_.getJType(ret_shorty);
TDYa1275e869b62012-07-25 00:45:39 -07002535
2536 // Get argument type
2537 std::vector<llvm::Type*> args_type;
2538
2539 args_type.push_back(irb_.getJObjectTy()); // method object pointer
2540
2541 if (!is_static) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002542 args_type.push_back(irb_.getJType('L')); // "this" object pointer
TDYa1275e869b62012-07-25 00:45:39 -07002543 }
2544
2545 for (uint32_t i = 1; i < shorty_size; ++i) {
buzbee26f10ee2012-12-21 11:16:29 -08002546 char shorty_type = art::RemapShorty(shorty[i]);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002547 args_type.push_back(irb_.getJType(shorty_type));
TDYa1275e869b62012-07-25 00:45:39 -07002548 }
2549
2550 return llvm::FunctionType::get(ret_type, args_type, false);
2551}
2552
2553
2554llvm::BasicBlock* GBCExpanderPass::
2555CreateBasicBlockWithDexPC(uint32_t dex_pc, const char* postfix) {
2556 std::string name;
2557
2558#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002559 art::StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
TDYa1275e869b62012-07-25 00:45:39 -07002560#endif
2561
2562 return llvm::BasicBlock::Create(context_, name, func_);
2563}
2564
2565llvm::BasicBlock* GBCExpanderPass::GetBasicBlock(uint32_t dex_pc) {
2566 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002567 CHECK(basic_blocks_[dex_pc] != NULL);
TDYa1275e869b62012-07-25 00:45:39 -07002568 return basic_blocks_[dex_pc];
2569}
2570
2571int32_t GBCExpanderPass::GetTryItemOffset(uint32_t dex_pc) {
2572 int32_t min = 0;
2573 int32_t max = code_item_->tries_size_ - 1;
2574
2575 while (min <= max) {
2576 int32_t mid = min + (max - min) / 2;
2577
TDYa127920be7c2012-09-10 17:13:22 -07002578 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*code_item_, mid);
TDYa1275e869b62012-07-25 00:45:39 -07002579 uint32_t start = ti->start_addr_;
2580 uint32_t end = start + ti->insn_count_;
2581
2582 if (dex_pc < start) {
2583 max = mid - 1;
2584 } else if (dex_pc >= end) {
2585 min = mid + 1;
2586 } else {
2587 return mid; // found
2588 }
2589 }
2590
2591 return -1; // not found
2592}
2593
2594llvm::BasicBlock* GBCExpanderPass::GetLandingPadBasicBlock(uint32_t dex_pc) {
2595 // Find the try item for this address in this method
2596 int32_t ti_offset = GetTryItemOffset(dex_pc);
2597
2598 if (ti_offset == -1) {
2599 return NULL; // No landing pad is available for this address.
2600 }
2601
2602 // Check for the existing landing pad basic block
2603 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2604 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2605
2606 if (block_lpad) {
2607 // We have generated landing pad for this try item already. Return the
2608 // same basic block.
2609 return block_lpad;
2610 }
2611
2612 // Get try item from code item
TDYa127920be7c2012-09-10 17:13:22 -07002613 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*code_item_, ti_offset);
TDYa1275e869b62012-07-25 00:45:39 -07002614
2615 std::string lpadname;
2616
2617#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002618 art::StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
TDYa1275e869b62012-07-25 00:45:39 -07002619#endif
2620
2621 // Create landing pad basic block
2622 block_lpad = llvm::BasicBlock::Create(context_, lpadname, func_);
2623
2624 // Change IRBuilder insert point
2625 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2626 irb_.SetInsertPoint(block_lpad);
2627
2628 // Find catch block with matching type
2629 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2630
2631 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
2632
2633 llvm::Value* catch_handler_index_value =
2634 irb_.CreateCall2(irb_.GetRuntime(runtime_support::FindCatchBlock),
2635 method_object_addr, ti_offset_value);
2636
2637 // Switch instruction (Go to unwind basic block by default)
2638 llvm::SwitchInst* sw =
2639 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2640
2641 // Cases with matched catch block
TDYa127920be7c2012-09-10 17:13:22 -07002642 art::CatchHandlerIterator iter(*code_item_, ti->start_addr_);
TDYa1275e869b62012-07-25 00:45:39 -07002643
2644 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2645 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2646 }
2647
2648 // Restore the orignal insert point for IRBuilder
2649 irb_.restoreIP(irb_ip_original);
2650
2651 // Cache this landing pad
2652 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2653 basic_block_landing_pads_[ti_offset] = block_lpad;
2654
2655 return block_lpad;
2656}
2657
2658llvm::BasicBlock* GBCExpanderPass::GetUnwindBasicBlock() {
2659 // Check the existing unwinding baisc block block
2660 if (basic_block_unwind_ != NULL) {
2661 return basic_block_unwind_;
2662 }
2663
2664 // Create new basic block for unwinding
2665 basic_block_unwind_ =
2666 llvm::BasicBlock::Create(context_, "exception_unwind", func_);
2667
2668 // Change IRBuilder insert point
2669 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2670 irb_.SetInsertPoint(basic_block_unwind_);
2671
2672 // Pop the shadow frame
2673 Expand_PopShadowFrame();
2674
2675 // Emit the code to return default value (zero) for the given return type.
2676 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
buzbee26f10ee2012-12-21 11:16:29 -08002677 ret_shorty = art::RemapShorty(ret_shorty);
TDYa1275e869b62012-07-25 00:45:39 -07002678 if (ret_shorty == 'V') {
2679 irb_.CreateRetVoid();
2680 } else {
2681 irb_.CreateRet(irb_.getJZero(ret_shorty));
2682 }
2683
2684 // Restore the orignal insert point for IRBuilder
2685 irb_.restoreIP(irb_ip_original);
2686
2687 return basic_block_unwind_;
2688}
2689
2690void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2691 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002692 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002693 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002694 irb_.CreateBr(lpad);
2695 } else {
2696 irb_.CreateBr(GetUnwindBasicBlock());
2697 }
2698}
2699
2700void GBCExpanderPass::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
Jeff Hao9a142652013-01-17 23:10:19 +00002701 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
2702
TDYa1275e869b62012-07-25 00:45:39 -07002703 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2704
2705 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002706 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002707 irb_.GetInsertBlock()));
Jeff Hao9a142652013-01-17 23:10:19 +00002708 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002709 } else {
Jeff Hao9a142652013-01-17 23:10:19 +00002710 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002711 }
2712
2713 irb_.SetInsertPoint(block_cont);
2714}
2715
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002716llvm::Value*
2717GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
2718 llvm::CallInst& call_inst) {
2719 switch (intr_id) {
2720 //==- Thread -----------------------------------------------------------==//
2721 case IntrinsicHelper::GetCurrentThread: {
TDYa127b672d1e2012-06-28 21:21:45 -07002722 return irb_.Runtime().EmitGetCurrentThread();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002723 }
Logan Chien75e4b602012-07-23 14:24:12 -07002724 case IntrinsicHelper::CheckSuspend: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08002725 Expand_TestSuspend(call_inst);
TDYa127890ea892012-08-22 10:49:42 -07002726 return NULL;
2727 }
2728 case IntrinsicHelper::TestSuspend: {
Logan Chiend54a23d2012-07-24 11:19:23 -07002729 Expand_TestSuspend(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002730 return NULL;
2731 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002732 case IntrinsicHelper::MarkGCCard: {
TDYa1279a129452012-07-19 03:10:08 -07002733 Expand_MarkGCCard(call_inst);
2734 return NULL;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002735 }
Logan Chien75e4b602012-07-23 14:24:12 -07002736
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002737 //==- Exception --------------------------------------------------------==//
2738 case IntrinsicHelper::ThrowException: {
2739 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
2740 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002741 case IntrinsicHelper::HLThrowException: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002742 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2743
2744 EmitUpdateDexPC(dex_pc);
2745
2746 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowException),
2747 call_inst.getArgOperand(0));
2748
2749 EmitGuard_ExceptionLandingPad(dex_pc);
2750 return NULL;
2751 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002752 case IntrinsicHelper::GetException: {
TDYa127823433d2012-09-26 16:03:51 -07002753 return irb_.Runtime().EmitGetAndClearException();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002754 }
2755 case IntrinsicHelper::IsExceptionPending: {
2756 return irb_.Runtime().EmitIsExceptionPending();
2757 }
2758 case IntrinsicHelper::FindCatchBlock: {
2759 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
2760 }
2761 case IntrinsicHelper::ThrowDivZeroException: {
2762 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
2763 }
2764 case IntrinsicHelper::ThrowNullPointerException: {
2765 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
2766 }
2767 case IntrinsicHelper::ThrowIndexOutOfBounds: {
2768 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
2769 }
Logan Chien75e4b602012-07-23 14:24:12 -07002770
2771 //==- Const String -----------------------------------------------------==//
2772 case IntrinsicHelper::ConstString: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002773 return Expand_ConstString(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002774 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002775 case IntrinsicHelper::LoadStringFromDexCache: {
2776 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
2777 }
2778 case IntrinsicHelper::ResolveString: {
2779 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
2780 }
Logan Chien75e4b602012-07-23 14:24:12 -07002781
2782 //==- Const Class ------------------------------------------------------==//
2783 case IntrinsicHelper::ConstClass: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002784 return Expand_ConstClass(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002785 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002786 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
2787 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
2788 }
2789 case IntrinsicHelper::LoadTypeFromDexCache: {
2790 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
2791 }
2792 case IntrinsicHelper::InitializeType: {
2793 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
2794 }
Logan Chien75e4b602012-07-23 14:24:12 -07002795
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002796 //==- Lock -------------------------------------------------------------==//
2797 case IntrinsicHelper::LockObject: {
2798 Expand_LockObject(call_inst.getArgOperand(0));
2799 return NULL;
2800 }
2801 case IntrinsicHelper::UnlockObject: {
2802 Expand_UnlockObject(call_inst.getArgOperand(0));
2803 return NULL;
2804 }
Logan Chien75e4b602012-07-23 14:24:12 -07002805
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002806 //==- Cast -------------------------------------------------------------==//
2807 case IntrinsicHelper::CheckCast: {
2808 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
2809 }
Logan Chien75e4b602012-07-23 14:24:12 -07002810 case IntrinsicHelper::HLCheckCast: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002811 Expand_HLCheckCast(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002812 return NULL;
2813 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002814 case IntrinsicHelper::IsAssignable: {
2815 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
2816 }
Logan Chien75e4b602012-07-23 14:24:12 -07002817
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002818 //==- Alloc ------------------------------------------------------------==//
2819 case IntrinsicHelper::AllocObject: {
2820 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
2821 }
2822 case IntrinsicHelper::AllocObjectWithAccessCheck: {
2823 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
2824 }
Logan Chien75e4b602012-07-23 14:24:12 -07002825
2826 //==- Instance ---------------------------------------------------------==//
2827 case IntrinsicHelper::NewInstance: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002828 return Expand_NewInstance(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002829 }
2830 case IntrinsicHelper::InstanceOf: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002831 return Expand_InstanceOf(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002832 }
2833
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002834 //==- Array ------------------------------------------------------------==//
Logan Chien75e4b602012-07-23 14:24:12 -07002835 case IntrinsicHelper::NewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002836 return Expand_NewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002837 }
2838 case IntrinsicHelper::OptArrayLength: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002839 return Expand_OptArrayLength(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002840 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002841 case IntrinsicHelper::ArrayLength: {
2842 return EmitLoadArrayLength(call_inst.getArgOperand(0));
2843 }
2844 case IntrinsicHelper::AllocArray: {
2845 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
2846 }
2847 case IntrinsicHelper::AllocArrayWithAccessCheck: {
2848 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
2849 call_inst);
2850 }
2851 case IntrinsicHelper::CheckAndAllocArray: {
2852 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
2853 }
2854 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
2855 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
2856 call_inst);
2857 }
2858 case IntrinsicHelper::ArrayGet: {
2859 return Expand_ArrayGet(call_inst.getArgOperand(0),
2860 call_inst.getArgOperand(1),
2861 kInt);
2862 }
2863 case IntrinsicHelper::ArrayGetWide: {
2864 return Expand_ArrayGet(call_inst.getArgOperand(0),
2865 call_inst.getArgOperand(1),
2866 kLong);
2867 }
2868 case IntrinsicHelper::ArrayGetObject: {
2869 return Expand_ArrayGet(call_inst.getArgOperand(0),
2870 call_inst.getArgOperand(1),
2871 kObject);
2872 }
2873 case IntrinsicHelper::ArrayGetBoolean: {
2874 return Expand_ArrayGet(call_inst.getArgOperand(0),
2875 call_inst.getArgOperand(1),
2876 kBoolean);
2877 }
2878 case IntrinsicHelper::ArrayGetByte: {
2879 return Expand_ArrayGet(call_inst.getArgOperand(0),
2880 call_inst.getArgOperand(1),
2881 kByte);
2882 }
2883 case IntrinsicHelper::ArrayGetChar: {
2884 return Expand_ArrayGet(call_inst.getArgOperand(0),
2885 call_inst.getArgOperand(1),
2886 kChar);
2887 }
2888 case IntrinsicHelper::ArrayGetShort: {
2889 return Expand_ArrayGet(call_inst.getArgOperand(0),
2890 call_inst.getArgOperand(1),
2891 kShort);
2892 }
2893 case IntrinsicHelper::ArrayPut: {
2894 Expand_ArrayPut(call_inst.getArgOperand(0),
2895 call_inst.getArgOperand(1),
2896 call_inst.getArgOperand(2),
2897 kInt);
2898 return NULL;
2899 }
2900 case IntrinsicHelper::ArrayPutWide: {
2901 Expand_ArrayPut(call_inst.getArgOperand(0),
2902 call_inst.getArgOperand(1),
2903 call_inst.getArgOperand(2),
2904 kLong);
2905 return NULL;
2906 }
2907 case IntrinsicHelper::ArrayPutObject: {
2908 Expand_ArrayPut(call_inst.getArgOperand(0),
2909 call_inst.getArgOperand(1),
2910 call_inst.getArgOperand(2),
2911 kObject);
2912 return NULL;
2913 }
2914 case IntrinsicHelper::ArrayPutBoolean: {
2915 Expand_ArrayPut(call_inst.getArgOperand(0),
2916 call_inst.getArgOperand(1),
2917 call_inst.getArgOperand(2),
2918 kBoolean);
2919 return NULL;
2920 }
2921 case IntrinsicHelper::ArrayPutByte: {
2922 Expand_ArrayPut(call_inst.getArgOperand(0),
2923 call_inst.getArgOperand(1),
2924 call_inst.getArgOperand(2),
2925 kByte);
2926 return NULL;
2927 }
2928 case IntrinsicHelper::ArrayPutChar: {
2929 Expand_ArrayPut(call_inst.getArgOperand(0),
2930 call_inst.getArgOperand(1),
2931 call_inst.getArgOperand(2),
2932 kChar);
2933 return NULL;
2934 }
2935 case IntrinsicHelper::ArrayPutShort: {
2936 Expand_ArrayPut(call_inst.getArgOperand(0),
2937 call_inst.getArgOperand(1),
2938 call_inst.getArgOperand(2),
2939 kShort);
2940 return NULL;
2941 }
2942 case IntrinsicHelper::CheckPutArrayElement: {
2943 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
2944 }
2945 case IntrinsicHelper::FilledNewArray: {
2946 Expand_FilledNewArray(call_inst);
2947 return NULL;
2948 }
2949 case IntrinsicHelper::FillArrayData: {
2950 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
2951 }
Logan Chien75e4b602012-07-23 14:24:12 -07002952 case IntrinsicHelper::HLFillArrayData: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002953 Expand_HLFillArrayData(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002954 return NULL;
2955 }
2956 case IntrinsicHelper::HLFilledNewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002957 return Expand_HLFilledNewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002958 }
2959
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002960 //==- Instance Field ---------------------------------------------------==//
2961 case IntrinsicHelper::InstanceFieldGet:
2962 case IntrinsicHelper::InstanceFieldGetBoolean:
2963 case IntrinsicHelper::InstanceFieldGetByte:
2964 case IntrinsicHelper::InstanceFieldGetChar:
2965 case IntrinsicHelper::InstanceFieldGetShort: {
2966 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
2967 }
2968 case IntrinsicHelper::InstanceFieldGetWide: {
2969 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
2970 }
2971 case IntrinsicHelper::InstanceFieldGetObject: {
2972 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
2973 }
2974 case IntrinsicHelper::InstanceFieldGetFast: {
2975 return Expand_IGetFast(call_inst.getArgOperand(0),
2976 call_inst.getArgOperand(1),
2977 call_inst.getArgOperand(2),
2978 kInt);
2979 }
2980 case IntrinsicHelper::InstanceFieldGetWideFast: {
2981 return Expand_IGetFast(call_inst.getArgOperand(0),
2982 call_inst.getArgOperand(1),
2983 call_inst.getArgOperand(2),
2984 kLong);
2985 }
2986 case IntrinsicHelper::InstanceFieldGetObjectFast: {
2987 return Expand_IGetFast(call_inst.getArgOperand(0),
2988 call_inst.getArgOperand(1),
2989 call_inst.getArgOperand(2),
2990 kObject);
2991 }
2992 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
2993 return Expand_IGetFast(call_inst.getArgOperand(0),
2994 call_inst.getArgOperand(1),
2995 call_inst.getArgOperand(2),
2996 kBoolean);
2997 }
2998 case IntrinsicHelper::InstanceFieldGetByteFast: {
2999 return Expand_IGetFast(call_inst.getArgOperand(0),
3000 call_inst.getArgOperand(1),
3001 call_inst.getArgOperand(2),
3002 kByte);
3003 }
3004 case IntrinsicHelper::InstanceFieldGetCharFast: {
3005 return Expand_IGetFast(call_inst.getArgOperand(0),
3006 call_inst.getArgOperand(1),
3007 call_inst.getArgOperand(2),
3008 kChar);
3009 }
3010 case IntrinsicHelper::InstanceFieldGetShortFast: {
3011 return Expand_IGetFast(call_inst.getArgOperand(0),
3012 call_inst.getArgOperand(1),
3013 call_inst.getArgOperand(2),
3014 kShort);
3015 }
3016 case IntrinsicHelper::InstanceFieldPut:
3017 case IntrinsicHelper::InstanceFieldPutBoolean:
3018 case IntrinsicHelper::InstanceFieldPutByte:
3019 case IntrinsicHelper::InstanceFieldPutChar:
3020 case IntrinsicHelper::InstanceFieldPutShort: {
3021 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
3022 }
3023 case IntrinsicHelper::InstanceFieldPutWide: {
3024 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
3025 }
3026 case IntrinsicHelper::InstanceFieldPutObject: {
3027 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
3028 }
3029 case IntrinsicHelper::InstanceFieldPutFast: {
3030 Expand_IPutFast(call_inst.getArgOperand(0),
3031 call_inst.getArgOperand(1),
3032 call_inst.getArgOperand(2),
3033 call_inst.getArgOperand(3),
3034 kInt);
3035 return NULL;
3036 }
3037 case IntrinsicHelper::InstanceFieldPutWideFast: {
3038 Expand_IPutFast(call_inst.getArgOperand(0),
3039 call_inst.getArgOperand(1),
3040 call_inst.getArgOperand(2),
3041 call_inst.getArgOperand(3),
3042 kLong);
3043 return NULL;
3044 }
3045 case IntrinsicHelper::InstanceFieldPutObjectFast: {
3046 Expand_IPutFast(call_inst.getArgOperand(0),
3047 call_inst.getArgOperand(1),
3048 call_inst.getArgOperand(2),
3049 call_inst.getArgOperand(3),
3050 kObject);
3051 return NULL;
3052 }
3053 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
3054 Expand_IPutFast(call_inst.getArgOperand(0),
3055 call_inst.getArgOperand(1),
3056 call_inst.getArgOperand(2),
3057 call_inst.getArgOperand(3),
3058 kBoolean);
3059 return NULL;
3060 }
3061 case IntrinsicHelper::InstanceFieldPutByteFast: {
3062 Expand_IPutFast(call_inst.getArgOperand(0),
3063 call_inst.getArgOperand(1),
3064 call_inst.getArgOperand(2),
3065 call_inst.getArgOperand(3),
3066 kByte);
3067 return NULL;
3068 }
3069 case IntrinsicHelper::InstanceFieldPutCharFast: {
3070 Expand_IPutFast(call_inst.getArgOperand(0),
3071 call_inst.getArgOperand(1),
3072 call_inst.getArgOperand(2),
3073 call_inst.getArgOperand(3),
3074 kChar);
3075 return NULL;
3076 }
3077 case IntrinsicHelper::InstanceFieldPutShortFast: {
3078 Expand_IPutFast(call_inst.getArgOperand(0),
3079 call_inst.getArgOperand(1),
3080 call_inst.getArgOperand(2),
3081 call_inst.getArgOperand(3),
3082 kShort);
3083 return NULL;
3084 }
Logan Chien75e4b602012-07-23 14:24:12 -07003085
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003086 //==- Static Field -----------------------------------------------------==//
3087 case IntrinsicHelper::StaticFieldGet:
3088 case IntrinsicHelper::StaticFieldGetBoolean:
3089 case IntrinsicHelper::StaticFieldGetByte:
3090 case IntrinsicHelper::StaticFieldGetChar:
3091 case IntrinsicHelper::StaticFieldGetShort: {
3092 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
3093 }
3094 case IntrinsicHelper::StaticFieldGetWide: {
3095 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
3096 }
3097 case IntrinsicHelper::StaticFieldGetObject: {
3098 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
3099 }
3100 case IntrinsicHelper::StaticFieldGetFast: {
3101 return Expand_SGetFast(call_inst.getArgOperand(0),
3102 call_inst.getArgOperand(1),
3103 call_inst.getArgOperand(2),
3104 kInt);
3105 }
3106 case IntrinsicHelper::StaticFieldGetWideFast: {
3107 return Expand_SGetFast(call_inst.getArgOperand(0),
3108 call_inst.getArgOperand(1),
3109 call_inst.getArgOperand(2),
3110 kLong);
3111 }
3112 case IntrinsicHelper::StaticFieldGetObjectFast: {
3113 return Expand_SGetFast(call_inst.getArgOperand(0),
3114 call_inst.getArgOperand(1),
3115 call_inst.getArgOperand(2),
3116 kObject);
3117 }
3118 case IntrinsicHelper::StaticFieldGetBooleanFast: {
3119 return Expand_SGetFast(call_inst.getArgOperand(0),
3120 call_inst.getArgOperand(1),
3121 call_inst.getArgOperand(2),
3122 kBoolean);
3123 }
3124 case IntrinsicHelper::StaticFieldGetByteFast: {
3125 return Expand_SGetFast(call_inst.getArgOperand(0),
3126 call_inst.getArgOperand(1),
3127 call_inst.getArgOperand(2),
3128 kByte);
3129 }
3130 case IntrinsicHelper::StaticFieldGetCharFast: {
3131 return Expand_SGetFast(call_inst.getArgOperand(0),
3132 call_inst.getArgOperand(1),
3133 call_inst.getArgOperand(2),
3134 kChar);
3135 }
3136 case IntrinsicHelper::StaticFieldGetShortFast: {
3137 return Expand_SGetFast(call_inst.getArgOperand(0),
3138 call_inst.getArgOperand(1),
3139 call_inst.getArgOperand(2),
3140 kShort);
3141 }
3142 case IntrinsicHelper::StaticFieldPut:
3143 case IntrinsicHelper::StaticFieldPutBoolean:
3144 case IntrinsicHelper::StaticFieldPutByte:
3145 case IntrinsicHelper::StaticFieldPutChar:
3146 case IntrinsicHelper::StaticFieldPutShort: {
3147 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
3148 }
3149 case IntrinsicHelper::StaticFieldPutWide: {
3150 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
3151 }
3152 case IntrinsicHelper::StaticFieldPutObject: {
3153 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
3154 }
3155 case IntrinsicHelper::StaticFieldPutFast: {
3156 Expand_SPutFast(call_inst.getArgOperand(0),
3157 call_inst.getArgOperand(1),
3158 call_inst.getArgOperand(2),
3159 call_inst.getArgOperand(3),
3160 kInt);
3161 return NULL;
3162 }
3163 case IntrinsicHelper::StaticFieldPutWideFast: {
3164 Expand_SPutFast(call_inst.getArgOperand(0),
3165 call_inst.getArgOperand(1),
3166 call_inst.getArgOperand(2),
3167 call_inst.getArgOperand(3),
3168 kLong);
3169 return NULL;
3170 }
3171 case IntrinsicHelper::StaticFieldPutObjectFast: {
3172 Expand_SPutFast(call_inst.getArgOperand(0),
3173 call_inst.getArgOperand(1),
3174 call_inst.getArgOperand(2),
3175 call_inst.getArgOperand(3),
3176 kObject);
3177 return NULL;
3178 }
3179 case IntrinsicHelper::StaticFieldPutBooleanFast: {
3180 Expand_SPutFast(call_inst.getArgOperand(0),
3181 call_inst.getArgOperand(1),
3182 call_inst.getArgOperand(2),
3183 call_inst.getArgOperand(3),
3184 kBoolean);
3185 return NULL;
3186 }
3187 case IntrinsicHelper::StaticFieldPutByteFast: {
3188 Expand_SPutFast(call_inst.getArgOperand(0),
3189 call_inst.getArgOperand(1),
3190 call_inst.getArgOperand(2),
3191 call_inst.getArgOperand(3),
3192 kByte);
3193 return NULL;
3194 }
3195 case IntrinsicHelper::StaticFieldPutCharFast: {
3196 Expand_SPutFast(call_inst.getArgOperand(0),
3197 call_inst.getArgOperand(1),
3198 call_inst.getArgOperand(2),
3199 call_inst.getArgOperand(3),
3200 kChar);
3201 return NULL;
3202 }
3203 case IntrinsicHelper::StaticFieldPutShortFast: {
3204 Expand_SPutFast(call_inst.getArgOperand(0),
3205 call_inst.getArgOperand(1),
3206 call_inst.getArgOperand(2),
3207 call_inst.getArgOperand(3),
3208 kShort);
3209 return NULL;
3210 }
3211 case IntrinsicHelper::LoadDeclaringClassSSB: {
3212 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
3213 }
3214 case IntrinsicHelper::LoadClassSSBFromDexCache: {
3215 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
3216 }
3217 case IntrinsicHelper::InitializeAndLoadClassSSB: {
3218 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
3219 }
Logan Chien75e4b602012-07-23 14:24:12 -07003220
3221 //==- High-level Array -------------------------------------------------==//
3222 case IntrinsicHelper::HLArrayGet: {
TDYa1275a26d442012-07-26 18:58:38 -07003223 return Expand_HLArrayGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003224 }
3225 case IntrinsicHelper::HLArrayGetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003226 return Expand_HLArrayGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003227 }
3228 case IntrinsicHelper::HLArrayGetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003229 return Expand_HLArrayGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003230 }
3231 case IntrinsicHelper::HLArrayGetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003232 return Expand_HLArrayGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003233 }
3234 case IntrinsicHelper::HLArrayGetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003235 return Expand_HLArrayGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003236 }
3237 case IntrinsicHelper::HLArrayGetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003238 return Expand_HLArrayGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003239 }
3240 case IntrinsicHelper::HLArrayGetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003241 return Expand_HLArrayGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003242 }
3243 case IntrinsicHelper::HLArrayGetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003244 return Expand_HLArrayGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003245 }
3246 case IntrinsicHelper::HLArrayGetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003247 return Expand_HLArrayGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003248 }
3249 case IntrinsicHelper::HLArrayPut: {
TDYa1275a26d442012-07-26 18:58:38 -07003250 Expand_HLArrayPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003251 return NULL;
3252 }
3253 case IntrinsicHelper::HLArrayPutBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003254 Expand_HLArrayPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003255 return NULL;
3256 }
3257 case IntrinsicHelper::HLArrayPutByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003258 Expand_HLArrayPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003259 return NULL;
3260 }
3261 case IntrinsicHelper::HLArrayPutChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003262 Expand_HLArrayPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003263 return NULL;
3264 }
3265 case IntrinsicHelper::HLArrayPutShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003266 Expand_HLArrayPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003267 return NULL;
3268 }
3269 case IntrinsicHelper::HLArrayPutFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003270 Expand_HLArrayPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003271 return NULL;
3272 }
3273 case IntrinsicHelper::HLArrayPutWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003274 Expand_HLArrayPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003275 return NULL;
3276 }
3277 case IntrinsicHelper::HLArrayPutDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003278 Expand_HLArrayPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003279 return NULL;
3280 }
3281 case IntrinsicHelper::HLArrayPutObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003282 Expand_HLArrayPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003283 return NULL;
3284 }
3285
3286 //==- High-level Instance ----------------------------------------------==//
3287 case IntrinsicHelper::HLIGet: {
TDYa1275e869b62012-07-25 00:45:39 -07003288 return Expand_HLIGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003289 }
3290 case IntrinsicHelper::HLIGetBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003291 return Expand_HLIGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003292 }
3293 case IntrinsicHelper::HLIGetByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003294 return Expand_HLIGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003295 }
3296 case IntrinsicHelper::HLIGetChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003297 return Expand_HLIGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003298 }
3299 case IntrinsicHelper::HLIGetShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003300 return Expand_HLIGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003301 }
3302 case IntrinsicHelper::HLIGetFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003303 return Expand_HLIGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003304 }
3305 case IntrinsicHelper::HLIGetWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003306 return Expand_HLIGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003307 }
3308 case IntrinsicHelper::HLIGetDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003309 return Expand_HLIGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003310 }
3311 case IntrinsicHelper::HLIGetObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003312 return Expand_HLIGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003313 }
3314 case IntrinsicHelper::HLIPut: {
TDYa1275e869b62012-07-25 00:45:39 -07003315 Expand_HLIPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003316 return NULL;
3317 }
3318 case IntrinsicHelper::HLIPutBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003319 Expand_HLIPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003320 return NULL;
3321 }
3322 case IntrinsicHelper::HLIPutByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003323 Expand_HLIPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003324 return NULL;
3325 }
3326 case IntrinsicHelper::HLIPutChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003327 Expand_HLIPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003328 return NULL;
3329 }
3330 case IntrinsicHelper::HLIPutShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003331 Expand_HLIPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003332 return NULL;
3333 }
3334 case IntrinsicHelper::HLIPutFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003335 Expand_HLIPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003336 return NULL;
3337 }
3338 case IntrinsicHelper::HLIPutWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003339 Expand_HLIPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003340 return NULL;
3341 }
3342 case IntrinsicHelper::HLIPutDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003343 Expand_HLIPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003344 return NULL;
3345 }
3346 case IntrinsicHelper::HLIPutObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003347 Expand_HLIPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003348 return NULL;
3349 }
3350
3351 //==- High-level Invoke ------------------------------------------------==//
TDYa127f71bf5a2012-07-29 20:09:52 -07003352 case IntrinsicHelper::HLInvokeVoid:
3353 case IntrinsicHelper::HLInvokeObj:
3354 case IntrinsicHelper::HLInvokeInt:
3355 case IntrinsicHelper::HLInvokeFloat:
3356 case IntrinsicHelper::HLInvokeLong:
Logan Chien75e4b602012-07-23 14:24:12 -07003357 case IntrinsicHelper::HLInvokeDouble: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003358 return Expand_HLInvoke(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003359 }
3360
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003361 //==- Invoke -----------------------------------------------------------==//
3362 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
3363 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
3364 }
3365 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
3366 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
3367 }
3368 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
3369 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
3370 }
3371 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
3372 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
3373 }
3374 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
3375 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
3376 }
3377 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
3378 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
3379 }
3380 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
3381 return Expand_GetVirtualCalleeMethodObjAddrFast(
3382 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
3383 }
3384 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
3385 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
3386 }
3387 case IntrinsicHelper::InvokeRetVoid:
3388 case IntrinsicHelper::InvokeRetBoolean:
3389 case IntrinsicHelper::InvokeRetByte:
3390 case IntrinsicHelper::InvokeRetChar:
3391 case IntrinsicHelper::InvokeRetShort:
3392 case IntrinsicHelper::InvokeRetInt:
3393 case IntrinsicHelper::InvokeRetLong:
3394 case IntrinsicHelper::InvokeRetFloat:
3395 case IntrinsicHelper::InvokeRetDouble:
3396 case IntrinsicHelper::InvokeRetObject: {
3397 return Expand_Invoke(call_inst);
3398 }
Logan Chien75e4b602012-07-23 14:24:12 -07003399
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003400 //==- Math -------------------------------------------------------------==//
3401 case IntrinsicHelper::DivInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003402 return Expand_DivRem(call_inst, /* is_div */true, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003403 }
3404 case IntrinsicHelper::RemInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003405 return Expand_DivRem(call_inst, /* is_div */false, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003406 }
3407 case IntrinsicHelper::DivLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003408 return Expand_DivRem(call_inst, /* is_div */true, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003409 }
3410 case IntrinsicHelper::RemLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003411 return Expand_DivRem(call_inst, /* is_div */false, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003412 }
3413 case IntrinsicHelper::D2L: {
3414 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
3415 }
3416 case IntrinsicHelper::D2I: {
3417 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
3418 }
3419 case IntrinsicHelper::F2L: {
3420 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
3421 }
3422 case IntrinsicHelper::F2I: {
3423 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
3424 }
Logan Chien75e4b602012-07-23 14:24:12 -07003425
3426 //==- High-level Static ------------------------------------------------==//
3427 case IntrinsicHelper::HLSget: {
TDYa1275a26d442012-07-26 18:58:38 -07003428 return Expand_HLSget(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003429 }
3430 case IntrinsicHelper::HLSgetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003431 return Expand_HLSget(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003432 }
3433 case IntrinsicHelper::HLSgetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003434 return Expand_HLSget(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003435 }
3436 case IntrinsicHelper::HLSgetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003437 return Expand_HLSget(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003438 }
3439 case IntrinsicHelper::HLSgetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003440 return Expand_HLSget(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003441 }
3442 case IntrinsicHelper::HLSgetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003443 return Expand_HLSget(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003444 }
3445 case IntrinsicHelper::HLSgetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003446 return Expand_HLSget(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003447 }
3448 case IntrinsicHelper::HLSgetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003449 return Expand_HLSget(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003450 }
3451 case IntrinsicHelper::HLSgetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003452 return Expand_HLSget(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003453 }
3454 case IntrinsicHelper::HLSput: {
TDYa1275a26d442012-07-26 18:58:38 -07003455 Expand_HLSput(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003456 return NULL;
3457 }
3458 case IntrinsicHelper::HLSputBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003459 Expand_HLSput(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003460 return NULL;
3461 }
3462 case IntrinsicHelper::HLSputByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003463 Expand_HLSput(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003464 return NULL;
3465 }
3466 case IntrinsicHelper::HLSputChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003467 Expand_HLSput(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003468 return NULL;
3469 }
3470 case IntrinsicHelper::HLSputShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003471 Expand_HLSput(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003472 return NULL;
3473 }
3474 case IntrinsicHelper::HLSputFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003475 Expand_HLSput(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003476 return NULL;
3477 }
3478 case IntrinsicHelper::HLSputWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003479 Expand_HLSput(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003480 return NULL;
3481 }
3482 case IntrinsicHelper::HLSputDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003483 Expand_HLSput(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003484 return NULL;
3485 }
3486 case IntrinsicHelper::HLSputObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003487 Expand_HLSput(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003488 return NULL;
3489 }
3490
3491 //==- High-level Monitor -----------------------------------------------==//
3492 case IntrinsicHelper::MonitorEnter: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003493 Expand_MonitorEnter(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003494 return NULL;
3495 }
3496 case IntrinsicHelper::MonitorExit: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003497 Expand_MonitorExit(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003498 return NULL;
3499 }
3500
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003501 //==- Shadow Frame -----------------------------------------------------==//
3502 case IntrinsicHelper::AllocaShadowFrame: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08003503 Expand_AllocaShadowFrame(call_inst.getArgOperand(0));
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003504 return NULL;
3505 }
TDYa1278e950c12012-11-02 09:58:19 -07003506 case IntrinsicHelper::SetVReg: {
3507 Expand_SetVReg(call_inst.getArgOperand(0),
3508 call_inst.getArgOperand(1));
3509 return NULL;
3510 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003511 case IntrinsicHelper::PopShadowFrame: {
3512 Expand_PopShadowFrame();
3513 return NULL;
3514 }
3515 case IntrinsicHelper::UpdateDexPC: {
3516 Expand_UpdateDexPC(call_inst.getArgOperand(0));
3517 return NULL;
3518 }
TDYa127a1b21852012-07-23 03:20:39 -07003519
Logan Chien75e4b602012-07-23 14:24:12 -07003520 //==- Comparison -------------------------------------------------------==//
3521 case IntrinsicHelper::CmplFloat:
3522 case IntrinsicHelper::CmplDouble: {
3523 return Expand_FPCompare(call_inst.getArgOperand(0),
3524 call_inst.getArgOperand(1),
3525 false);
3526 }
3527 case IntrinsicHelper::CmpgFloat:
3528 case IntrinsicHelper::CmpgDouble: {
3529 return Expand_FPCompare(call_inst.getArgOperand(0),
3530 call_inst.getArgOperand(1),
3531 true);
3532 }
3533 case IntrinsicHelper::CmpLong: {
3534 return Expand_LongCompare(call_inst.getArgOperand(0),
3535 call_inst.getArgOperand(1));
3536 }
TDYa127a1b21852012-07-23 03:20:39 -07003537
Logan Chien75e4b602012-07-23 14:24:12 -07003538 //==- Const ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003539 case IntrinsicHelper::ConstInt:
3540 case IntrinsicHelper::ConstLong: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003541 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003542 }
TDYa127920be7c2012-09-10 17:13:22 -07003543 case IntrinsicHelper::ConstFloat: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003544 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3545 irb_.getJFloatTy());
Logan Chien75e4b602012-07-23 14:24:12 -07003546 }
TDYa127920be7c2012-09-10 17:13:22 -07003547 case IntrinsicHelper::ConstDouble: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003548 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3549 irb_.getJDoubleTy());
3550 }
TDYa127920be7c2012-09-10 17:13:22 -07003551 case IntrinsicHelper::ConstObj: {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003552 CHECK(LV2UInt(call_inst.getArgOperand(0)) == 0);
3553 return irb_.getJNull();
Logan Chien75e4b602012-07-23 14:24:12 -07003554 }
3555
3556 //==- Method Info ------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003557 case IntrinsicHelper::MethodInfo: {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003558 // Nothing to be done, because MethodInfo carries optional hints that are
3559 // not needed by the portable path.
Logan Chien75e4b602012-07-23 14:24:12 -07003560 return NULL;
3561 }
3562
3563 //==- Copy -------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003564 case IntrinsicHelper::CopyInt:
3565 case IntrinsicHelper::CopyFloat:
3566 case IntrinsicHelper::CopyLong:
3567 case IntrinsicHelper::CopyDouble:
3568 case IntrinsicHelper::CopyObj: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003569 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003570 }
3571
3572 //==- Shift ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003573 case IntrinsicHelper::SHLLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003574 return Expand_IntegerShift(call_inst.getArgOperand(0),
3575 call_inst.getArgOperand(1),
3576 kIntegerSHL, kLong);
3577 }
TDYa127920be7c2012-09-10 17:13:22 -07003578 case IntrinsicHelper::SHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003579 return Expand_IntegerShift(call_inst.getArgOperand(0),
3580 call_inst.getArgOperand(1),
3581 kIntegerSHR, kLong);
3582 }
TDYa127920be7c2012-09-10 17:13:22 -07003583 case IntrinsicHelper::USHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003584 return Expand_IntegerShift(call_inst.getArgOperand(0),
3585 call_inst.getArgOperand(1),
3586 kIntegerUSHR, kLong);
3587 }
TDYa127920be7c2012-09-10 17:13:22 -07003588 case IntrinsicHelper::SHLInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003589 return Expand_IntegerShift(call_inst.getArgOperand(0),
3590 call_inst.getArgOperand(1),
3591 kIntegerSHL, kInt);
3592 }
TDYa127920be7c2012-09-10 17:13:22 -07003593 case IntrinsicHelper::SHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003594 return Expand_IntegerShift(call_inst.getArgOperand(0),
3595 call_inst.getArgOperand(1),
3596 kIntegerSHR, kInt);
3597 }
TDYa127920be7c2012-09-10 17:13:22 -07003598 case IntrinsicHelper::USHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003599 return Expand_IntegerShift(call_inst.getArgOperand(0),
3600 call_inst.getArgOperand(1),
3601 kIntegerUSHR, kInt);
3602 }
3603
3604 //==- Conversion -------------------------------------------------------==//
TDYa127a1b21852012-07-23 03:20:39 -07003605 case IntrinsicHelper::IntToChar: {
3606 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
3607 irb_.getJIntTy());
3608 }
3609 case IntrinsicHelper::IntToShort: {
3610 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
3611 irb_.getJIntTy());
3612 }
3613 case IntrinsicHelper::IntToByte: {
3614 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
3615 irb_.getJIntTy());
3616 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003617
TDYa12787caa7e2012-08-25 23:23:27 -07003618 //==- Exception --------------------------------------------------------==//
3619 case IntrinsicHelper::CatchTargets: {
TDYa12755e5e6c2012-09-11 15:14:42 -07003620 UpdatePhiInstruction(current_bb_, irb_.GetInsertBlock());
TDYa12787caa7e2012-08-25 23:23:27 -07003621 llvm::SwitchInst* si = llvm::dyn_cast<llvm::SwitchInst>(call_inst.getNextNode());
3622 CHECK(si != NULL);
3623 irb_.CreateBr(si->getDefaultDest());
3624 si->eraseFromParent();
3625 return call_inst.getArgOperand(0);
3626 }
3627
Sebastien Hertz0d43d542013-02-27 19:02:16 +01003628 //==- Constructor barrier-----------------------------------------------==//
3629 case IntrinsicHelper::ConstructorBarrier: {
3630 irb_.CreateMemoryBarrier(art::kStoreStore);
3631 return NULL;
3632 }
3633
Logan Chien75e4b602012-07-23 14:24:12 -07003634 //==- Unknown Cases ----------------------------------------------------==//
3635 case IntrinsicHelper::MaxIntrinsicId:
3636 case IntrinsicHelper::UnknownId:
3637 //default:
3638 // NOTE: "default" is intentionally commented so that C/C++ compiler will
3639 // give some warning on unmatched cases.
3640 // NOTE: We should not implement these cases.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003641 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003642 }
Logan Chien75e4b602012-07-23 14:24:12 -07003643 UNIMPLEMENTED(FATAL) << "Unexpected GBC intrinsic: " << static_cast<int>(intr_id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003644 return NULL;
3645}
3646
3647} // anonymous namespace
3648
3649namespace art {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003650
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003651namespace compiler_llvm {
3652
3653llvm::FunctionPass*
3654CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb) {
3655 return new GBCExpanderPass(intrinsic_helper, irb);
3656}
3657
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003658llvm::FunctionPass*
3659CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
3660 Compiler* compiler, OatCompilationUnit* oat_compilation_unit) {
3661 if (compiler != NULL) {
3662 return new GBCExpanderPass(intrinsic_helper, irb,
3663 compiler, oat_compilation_unit);
3664 } else {
3665 return new GBCExpanderPass(intrinsic_helper, irb);
3666 }
3667}
3668
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003669} // namespace compiler_llvm
3670} // namespace art