blob: f0d38306c8b701c6fe27a49bc13037d7e7eb0ffa [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
Ian Rogers1212a022013-03-04 10:48:41 -080017#include "compiler/driver/compiler_driver.h"
Ian Rogers76ae4fe2013-02-27 16:03:41 -080018#include "intrinsic_helper.h"
Ian Rogers8e696052013-03-04 09:00:40 -080019#include "ir_builder.h"
Ian Rogers98573f92013-01-30 17:26:32 -080020#include "mirror/abstract_method.h"
21#include "mirror/array.h"
TDYa1275e869b62012-07-25 00:45:39 -070022#include "oat_compilation_unit.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070023#include "thread.h"
Ian Rogers8e696052013-03-04 09:00:40 -080024#include "utils_llvm.h"
TDYa1275e869b62012-07-25 00:45:39 -070025#include "verifier/method_verifier.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070026
buzbee395116c2013-02-27 14:30:25 -080027#include "compiler/dex/compiler_ir.h"
28#include "compiler/dex/quick/codegen.h"
TDYa127920be7c2012-09-10 17:13:22 -070029using art::kMIRIgnoreNullCheck;
30using art::kMIRIgnoreRangeCheck;
31
Shih-wei Liao21d28f52012-06-12 05:55:00 -070032#include <llvm/ADT/STLExtras.h>
33#include <llvm/Intrinsics.h>
Logan Chiend36a2ac2012-08-04 00:37:30 +080034#include <llvm/Metadata.h>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070035#include <llvm/Pass.h>
36#include <llvm/Support/CFG.h>
37#include <llvm/Support/InstIterator.h>
38
39#include <vector>
TDYa127aa558872012-08-16 05:11:07 -070040#include <map>
41#include <utility>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070042
TDYa127920be7c2012-09-10 17:13:22 -070043using namespace art::compiler_llvm;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070044
Ian Rogers76ae4fe2013-02-27 16:03:41 -080045using art::compiler_llvm::IntrinsicHelper;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070046
Shih-wei Liaob2596522012-09-14 16:36:11 -070047namespace art {
buzbee26f10ee2012-12-21 11:16:29 -080048extern char RemapShorty(char shortyType);
Shih-wei Liaob2596522012-09-14 16:36:11 -070049};
50
Shih-wei Liao21d28f52012-06-12 05:55:00 -070051namespace {
52
53class GBCExpanderPass : public llvm::FunctionPass {
54 private:
55 const IntrinsicHelper& intrinsic_helper_;
56 IRBuilder& irb_;
57
58 llvm::LLVMContext& context_;
59 RuntimeSupportBuilder& rtb_;
60
61 private:
62 llvm::AllocaInst* shadow_frame_;
63 llvm::Value* old_shadow_frame_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070064
65 private:
Ian Rogers1212a022013-03-04 10:48:41 -080066 art::CompilerDriver* const driver_;
TDYa1275e869b62012-07-25 00:45:39 -070067
TDYa127920be7c2012-09-10 17:13:22 -070068 art::OatCompilationUnit* oat_compilation_unit_;
TDYa1275e869b62012-07-25 00:45:39 -070069
TDYa1275e869b62012-07-25 00:45:39 -070070 llvm::Function* func_;
71
72 std::vector<llvm::BasicBlock*> basic_blocks_;
73
74 std::vector<llvm::BasicBlock*> basic_block_landing_pads_;
TDYa12755e5e6c2012-09-11 15:14:42 -070075 llvm::BasicBlock* current_bb_;
TDYa127aa558872012-08-16 05:11:07 -070076 std::map<llvm::BasicBlock*, std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> > >
77 landing_pad_phi_mapping_;
TDYa1275e869b62012-07-25 00:45:39 -070078 llvm::BasicBlock* basic_block_unwind_;
79
Logan Chien67645d82012-08-17 09:10:54 +080080 bool changed_;
81
TDYa1275e869b62012-07-25 00:45:39 -070082 private:
Shih-wei Liao21d28f52012-06-12 05:55:00 -070083 //----------------------------------------------------------------------------
Logan Chien75e4b602012-07-23 14:24:12 -070084 // Constant for GBC expansion
85 //----------------------------------------------------------------------------
86 enum IntegerShiftKind {
87 kIntegerSHL,
88 kIntegerSHR,
89 kIntegerUSHR,
90 };
91
92 private:
93 //----------------------------------------------------------------------------
Shih-wei Liao21d28f52012-06-12 05:55:00 -070094 // Helper function for GBC expansion
95 //----------------------------------------------------------------------------
96
Shih-wei Liao21d28f52012-06-12 05:55:00 -070097 llvm::Value* ExpandToRuntime(runtime_support::RuntimeId rt,
98 llvm::CallInst& inst);
99
TDYa1275e869b62012-07-25 00:45:39 -0700100 uint64_t LV2UInt(llvm::Value* lv) {
101 return llvm::cast<llvm::ConstantInt>(lv)->getZExtValue();
102 }
103
104 int64_t LV2SInt(llvm::Value* lv) {
105 return llvm::cast<llvm::ConstantInt>(lv)->getSExtValue();
106 }
107
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700108 private:
109 // TODO: Almost all Emit* are directly copy-n-paste from MethodCompiler.
110 // Refactor these utility functions from MethodCompiler to avoid forking.
111
Logan Chien67645d82012-08-17 09:10:54 +0800112 void EmitStackOverflowCheck(llvm::Instruction* first_non_alloca);
113
114 void RewriteFunction();
115
116 void RewriteBasicBlock(llvm::BasicBlock* original_block);
117
118 void UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
119 llvm::BasicBlock* new_basic_block);
120
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700121
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800122 // Sign or zero extend category 1 types < 32bits in size to 32bits.
123 llvm::Value* SignOrZeroExtendCat1Types(llvm::Value* value, JType jty);
124
125 // Truncate category 1 types from 32bits to the given JType size.
126 llvm::Value* TruncateCat1Types(llvm::Value* value, JType jty);
127
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700128 //----------------------------------------------------------------------------
129 // Dex cache code generation helper function
130 //----------------------------------------------------------------------------
TDYa127920be7c2012-09-10 17:13:22 -0700131 llvm::Value* EmitLoadDexCacheAddr(art::MemberOffset dex_cache_offset);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700132
133 llvm::Value* EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx);
134
135 llvm::Value* EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx);
136
137 llvm::Value* EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx);
138
139 llvm::Value* EmitLoadDexCacheStringFieldAddr(uint32_t string_idx);
140
141 //----------------------------------------------------------------------------
142 // Code generation helper function
143 //----------------------------------------------------------------------------
144 llvm::Value* EmitLoadMethodObjectAddr();
145
146 llvm::Value* EmitLoadArrayLength(llvm::Value* array);
147
148 llvm::Value* EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx);
149
150 llvm::Value* EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
151 llvm::Value* this_addr);
152
153 llvm::Value* EmitArrayGEP(llvm::Value* array_addr,
154 llvm::Value* index_value,
155 JType elem_jty);
156
157 private:
158 //----------------------------------------------------------------------------
159 // Expand Greenland intrinsics
160 //----------------------------------------------------------------------------
161 void Expand_TestSuspend(llvm::CallInst& call_inst);
162
TDYa1279a129452012-07-19 03:10:08 -0700163 void Expand_MarkGCCard(llvm::CallInst& call_inst);
164
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700165 llvm::Value* Expand_LoadStringFromDexCache(llvm::Value* string_idx_value);
166
167 llvm::Value* Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value);
168
169 void Expand_LockObject(llvm::Value* obj);
170
171 void Expand_UnlockObject(llvm::Value* obj);
172
173 llvm::Value* Expand_ArrayGet(llvm::Value* array_addr,
174 llvm::Value* index_value,
175 JType elem_jty);
176
177 void Expand_ArrayPut(llvm::Value* new_value,
178 llvm::Value* array_addr,
179 llvm::Value* index_value,
180 JType elem_jty);
181
182 void Expand_FilledNewArray(llvm::CallInst& call_inst);
183
184 llvm::Value* Expand_IGetFast(llvm::Value* field_offset_value,
185 llvm::Value* is_volatile_value,
186 llvm::Value* object_addr,
187 JType field_jty);
188
189 void Expand_IPutFast(llvm::Value* field_offset_value,
190 llvm::Value* is_volatile_value,
191 llvm::Value* object_addr,
192 llvm::Value* new_value,
193 JType field_jty);
194
195 llvm::Value* Expand_SGetFast(llvm::Value* static_storage_addr,
196 llvm::Value* field_offset_value,
197 llvm::Value* is_volatile_value,
198 JType field_jty);
199
200 void Expand_SPutFast(llvm::Value* static_storage_addr,
201 llvm::Value* field_offset_value,
202 llvm::Value* is_volatile_value,
203 llvm::Value* new_value,
204 JType field_jty);
205
206 llvm::Value* Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr);
207
208 llvm::Value* Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value);
209
210 llvm::Value*
211 Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value);
212
213 llvm::Value*
214 Expand_GetVirtualCalleeMethodObjAddrFast(llvm::Value* vtable_idx_value,
215 llvm::Value* this_addr);
216
217 llvm::Value* Expand_Invoke(llvm::CallInst& call_inst);
218
TDYa1274ec8ccd2012-08-11 07:04:57 -0700219 llvm::Value* Expand_DivRem(llvm::CallInst& call_inst, bool is_div, JType op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700220
TDYa127ce4cc0d2012-11-18 16:59:53 -0800221 void Expand_AllocaShadowFrame(llvm::Value* num_vregs_value);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700222
TDYa1278e950c12012-11-02 09:58:19 -0700223 void Expand_SetVReg(llvm::Value* entry_idx, llvm::Value* obj);
224
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700225 void Expand_PopShadowFrame();
226
227 void Expand_UpdateDexPC(llvm::Value* dex_pc_value);
228
TDYa127a1b21852012-07-23 03:20:39 -0700229 //----------------------------------------------------------------------------
230 // Quick
231 //----------------------------------------------------------------------------
232
233 llvm::Value* Expand_FPCompare(llvm::Value* src1_value,
234 llvm::Value* src2_value,
235 bool gt_bias);
236
237 llvm::Value* Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value);
238
239 llvm::Value* EmitCompareResultSelection(llvm::Value* cmp_eq,
240 llvm::Value* cmp_lt);
241
TDYa127f71bf5a2012-07-29 20:09:52 -0700242 llvm::Value* EmitLoadConstantClass(uint32_t dex_pc, uint32_t type_idx);
TDYa1275a26d442012-07-26 18:58:38 -0700243 llvm::Value* EmitLoadStaticStorage(uint32_t dex_pc, uint32_t type_idx);
244
TDYa1275e869b62012-07-25 00:45:39 -0700245 llvm::Value* Expand_HLIGet(llvm::CallInst& call_inst, JType field_jty);
246 void Expand_HLIPut(llvm::CallInst& call_inst, JType field_jty);
247
TDYa1275a26d442012-07-26 18:58:38 -0700248 llvm::Value* Expand_HLSget(llvm::CallInst& call_inst, JType field_jty);
249 void Expand_HLSput(llvm::CallInst& call_inst, JType field_jty);
250
251 llvm::Value* Expand_HLArrayGet(llvm::CallInst& call_inst, JType field_jty);
252 void Expand_HLArrayPut(llvm::CallInst& call_inst, JType field_jty);
253
TDYa127f71bf5a2012-07-29 20:09:52 -0700254 llvm::Value* Expand_ConstString(llvm::CallInst& call_inst);
255 llvm::Value* Expand_ConstClass(llvm::CallInst& call_inst);
256
257 void Expand_MonitorEnter(llvm::CallInst& call_inst);
258 void Expand_MonitorExit(llvm::CallInst& call_inst);
259
260 void Expand_HLCheckCast(llvm::CallInst& call_inst);
261 llvm::Value* Expand_InstanceOf(llvm::CallInst& call_inst);
262
263 llvm::Value* Expand_NewInstance(llvm::CallInst& call_inst);
264
265 llvm::Value* Expand_HLInvoke(llvm::CallInst& call_inst);
266
267 llvm::Value* Expand_OptArrayLength(llvm::CallInst& call_inst);
268 llvm::Value* Expand_NewArray(llvm::CallInst& call_inst);
269 llvm::Value* Expand_HLFilledNewArray(llvm::CallInst& call_inst);
270 void Expand_HLFillArrayData(llvm::CallInst& call_inst);
271
272 llvm::Value* EmitAllocNewArray(uint32_t dex_pc,
273 llvm::Value* array_length_value,
274 uint32_t type_idx,
275 bool is_filled_new_array);
276
277 llvm::Value* EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -0700278 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -0700279 llvm::Value* this_addr,
280 uint32_t dex_pc,
281 bool is_fast_path);
282
TDYa1275e869b62012-07-25 00:45:39 -0700283 void EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr);
284
285 void EmitUpdateDexPC(uint32_t dex_pc);
286
287 void EmitGuard_DivZeroException(uint32_t dex_pc,
288 llvm::Value* denominator,
289 JType op_jty);
290
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +0100291 void EmitGuard_NullPointerException(uint32_t dex_pc, llvm::Value* object,
292 int opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -0700293
294 void EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
295 llvm::Value* array,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +0100296 llvm::Value* index,
297 int opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -0700298
Ian Rogers8e696052013-03-04 09:00:40 -0800299 llvm::FunctionType* GetFunctionType(llvm::Type* ret_type, uint32_t method_idx, bool is_static);
TDYa1275e869b62012-07-25 00:45:39 -0700300
301 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
302
303 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
304 const char* postfix);
305
306 int32_t GetTryItemOffset(uint32_t dex_pc);
307
308 llvm::BasicBlock* GetLandingPadBasicBlock(uint32_t dex_pc);
309
310 llvm::BasicBlock* GetUnwindBasicBlock();
311
312 void EmitGuard_ExceptionLandingPad(uint32_t dex_pc);
313
314 void EmitBranchExceptionLandingPad(uint32_t dex_pc);
315
Logan Chien75e4b602012-07-23 14:24:12 -0700316 //----------------------------------------------------------------------------
317 // Expand Arithmetic Helper Intrinsics
318 //----------------------------------------------------------------------------
319
320 llvm::Value* Expand_IntegerShift(llvm::Value* src1_value,
321 llvm::Value* src2_value,
322 IntegerShiftKind kind,
323 JType op_jty);
324
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700325 public:
326 static char ID;
327
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700328 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Ian Rogers1212a022013-03-04 10:48:41 -0800329 art::CompilerDriver* compiler, art::OatCompilationUnit* oat_compilation_unit)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700330 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
331 context_(irb.getContext()), rtb_(irb.Runtime()),
TDYa1278e950c12012-11-02 09:58:19 -0700332 shadow_frame_(NULL), old_shadow_frame_(NULL),
Ian Rogers1212a022013-03-04 10:48:41 -0800333 driver_(compiler),
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700334 oat_compilation_unit_(oat_compilation_unit),
Ian Rogers8e696052013-03-04 09:00:40 -0800335 func_(NULL), current_bb_(NULL), basic_block_unwind_(NULL), changed_(false) {}
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700336
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700337 bool runOnFunction(llvm::Function& func);
338
339 private:
Logan Chien67645d82012-08-17 09:10:54 +0800340 void InsertStackOverflowCheck(llvm::Function& func);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700341
342 llvm::Value* ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
343 llvm::CallInst& call_inst);
344
345};
346
347char GBCExpanderPass::ID = 0;
348
349bool GBCExpanderPass::runOnFunction(llvm::Function& func) {
Ian Rogers8e696052013-03-04 09:00:40 -0800350 VLOG(compiler) << "GBC expansion on " << func.getName().str();
351
TDYa127b672d1e2012-06-28 21:21:45 -0700352 // Runtime support or stub
353 if (func.getName().startswith("art_") || func.getName().startswith("Art")) {
354 return false;
355 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700356
Logan Chien67645d82012-08-17 09:10:54 +0800357 // Setup rewrite context
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700358 shadow_frame_ = NULL;
359 old_shadow_frame_ = NULL;
TDYa1275e869b62012-07-25 00:45:39 -0700360 func_ = &func;
Logan Chien67645d82012-08-17 09:10:54 +0800361 changed_ = false; // Assume unchanged
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700362
Ian Rogers8e696052013-03-04 09:00:40 -0800363 basic_blocks_.resize(oat_compilation_unit_->code_item_->insns_size_in_code_units_);
364 basic_block_landing_pads_.resize(oat_compilation_unit_->code_item_->tries_size_, NULL);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700365 basic_block_unwind_ = NULL;
366 for (llvm::Function::iterator bb_iter = func_->begin(), bb_end = func_->end();
367 bb_iter != bb_end;
368 ++bb_iter) {
369 if (bb_iter->begin()->getMetadata("DexOff") == NULL) {
370 continue;
371 }
372 uint32_t dex_pc = LV2UInt(bb_iter->begin()->getMetadata("DexOff")->getOperand(0));
373 basic_blocks_[dex_pc] = bb_iter;
374 }
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700375
Logan Chien67645d82012-08-17 09:10:54 +0800376 // Insert stack overflow check
377 InsertStackOverflowCheck(func); // TODO: Use intrinsic.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700378
Logan Chien67645d82012-08-17 09:10:54 +0800379 // Rewrite the intrinsics
380 RewriteFunction();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700381
382 VERIFY_LLVM_FUNCTION(func);
383
Logan Chien67645d82012-08-17 09:10:54 +0800384 return changed_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700385}
386
Logan Chien67645d82012-08-17 09:10:54 +0800387void GBCExpanderPass::RewriteBasicBlock(llvm::BasicBlock* original_block) {
388 llvm::BasicBlock* curr_basic_block = original_block;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700389
Logan Chien67645d82012-08-17 09:10:54 +0800390 llvm::BasicBlock::iterator inst_iter = original_block->begin();
391 llvm::BasicBlock::iterator inst_end = original_block->end();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700392
Logan Chien67645d82012-08-17 09:10:54 +0800393 while (inst_iter != inst_end) {
394 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst_iter);
395 IntrinsicHelper::IntrinsicId intr_id = IntrinsicHelper::UnknownId;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700396
Logan Chien67645d82012-08-17 09:10:54 +0800397 if (call_inst) {
398 llvm::Function* callee_func = call_inst->getCalledFunction();
399 intr_id = intrinsic_helper_.GetIntrinsicId(callee_func);
400 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700401
Logan Chien67645d82012-08-17 09:10:54 +0800402 if (intr_id == IntrinsicHelper::UnknownId) {
403 // This is not intrinsic call. Skip this instruction.
404 ++inst_iter;
405 continue;
406 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700407
Logan Chien67645d82012-08-17 09:10:54 +0800408 // Rewrite the intrinsic and change the function
409 changed_ = true;
410 irb_.SetInsertPoint(inst_iter);
411
412 // Expand the intrinsic
413 if (llvm::Value* new_value = ExpandIntrinsic(intr_id, *call_inst)) {
414 inst_iter->replaceAllUsesWith(new_value);
415 }
416
417 // Remove the old intrinsic call instruction
418 llvm::BasicBlock::iterator old_inst = inst_iter++;
419 old_inst->eraseFromParent();
420
421 // Splice the instruction to the new basic block
422 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
423 if (next_basic_block != curr_basic_block) {
424 next_basic_block->getInstList().splice(
425 irb_.GetInsertPoint(), curr_basic_block->getInstList(),
426 inst_iter, inst_end);
427 curr_basic_block = next_basic_block;
428 inst_end = curr_basic_block->end();
429 }
430 }
431}
432
433
434void GBCExpanderPass::RewriteFunction() {
435 size_t num_basic_blocks = func_->getBasicBlockList().size();
436 // NOTE: We are not using (bb_iter != bb_end) as the for-loop condition,
437 // because we will create new basic block while expanding the intrinsics.
438 // We only want to iterate through the input basic blocks.
439
TDYa127aa558872012-08-16 05:11:07 -0700440 landing_pad_phi_mapping_.clear();
441
Logan Chien67645d82012-08-17 09:10:54 +0800442 for (llvm::Function::iterator bb_iter = func_->begin();
443 num_basic_blocks > 0; ++bb_iter, --num_basic_blocks) {
Shih-wei Liao627d8c42012-08-24 08:31:18 -0700444 // Set insert point to current basic block.
445 irb_.SetInsertPoint(bb_iter);
Logan Chien67645d82012-08-17 09:10:54 +0800446
TDYa12755e5e6c2012-09-11 15:14:42 -0700447 current_bb_ = bb_iter;
TDYa127aa558872012-08-16 05:11:07 -0700448
Logan Chien67645d82012-08-17 09:10:54 +0800449 // Rewrite the basic block
450 RewriteBasicBlock(bb_iter);
451
452 // Update the phi-instructions in the successor basic block
453 llvm::BasicBlock* last_block = irb_.GetInsertBlock();
454 if (last_block != bb_iter) {
455 UpdatePhiInstruction(bb_iter, last_block);
456 }
457 }
TDYa127aa558872012-08-16 05:11:07 -0700458
459 typedef std::map<llvm::PHINode*, llvm::PHINode*> HandlerPHIMap;
460 HandlerPHIMap handler_phi;
461 // Iterate every used landing pad basic block
462 for (size_t i = 0, ei = basic_block_landing_pads_.size(); i != ei; ++i) {
463 llvm::BasicBlock* lbb = basic_block_landing_pads_[i];
464 if (lbb == NULL) {
465 continue;
466 }
467
468 llvm::TerminatorInst* term_inst = lbb->getTerminator();
469 std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> >& rewrite_pair
470 = landing_pad_phi_mapping_[lbb];
471 irb_.SetInsertPoint(lbb->begin());
472
473 // Iterate every succeeding basic block (catch block)
474 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
475 succ_iter != succ_end; ++succ_iter) {
476 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
477
478 // Iterate every phi instructions in the succeeding basic block
479 for (llvm::BasicBlock::iterator
480 inst_iter = succ_basic_block->begin(),
481 inst_end = succ_basic_block->end();
482 inst_iter != inst_end; ++inst_iter) {
483 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
484
485 if (!phi) {
486 break; // Meet non-phi instruction. Done.
487 }
488
489 if (handler_phi[phi] == NULL) {
490 handler_phi[phi] = llvm::PHINode::Create(phi->getType(), 1);
491 }
492
493 // Create new_phi in landing pad
494 llvm::PHINode* new_phi = irb_.CreatePHI(phi->getType(), rewrite_pair.size());
495 // Insert all incoming value into new_phi by rewrite_pair
496 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
497 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
498 llvm::BasicBlock* new_bb = rewrite_pair[j].second;
499 new_phi->addIncoming(phi->getIncomingValueForBlock(old_bb), new_bb);
500 }
501 // Delete all incoming value from phi by rewrite_pair
502 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
503 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
504 int old_bb_idx = phi->getBasicBlockIndex(old_bb);
505 if (old_bb_idx >= 0) {
506 phi->removeIncomingValue(old_bb_idx, false);
507 }
508 }
509 // Insert new_phi into new handler phi
510 handler_phi[phi]->addIncoming(new_phi, lbb);
511 }
512 }
513 }
514
515 // Replace all handler phi
516 // We can't just use the old handler phi, because some exception edges will disappear after we
517 // compute fast-path.
518 for (HandlerPHIMap::iterator it = handler_phi.begin(); it != handler_phi.end(); ++it) {
519 llvm::PHINode* old_phi = it->first;
520 llvm::PHINode* new_phi = it->second;
521 new_phi->insertBefore(old_phi);
522 old_phi->replaceAllUsesWith(new_phi);
523 old_phi->eraseFromParent();
524 }
Logan Chien67645d82012-08-17 09:10:54 +0800525}
526
527void GBCExpanderPass::UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
528 llvm::BasicBlock* new_basic_block) {
529 llvm::TerminatorInst* term_inst = new_basic_block->getTerminator();
530
531 if (!term_inst) {
532 return; // No terminating instruction in new_basic_block. Nothing to do.
533 }
534
535 // Iterate every succeeding basic block
536 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
537 succ_iter != succ_end; ++succ_iter) {
538 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
539
540 // Iterate every phi instructions in the succeeding basic block
541 for (llvm::BasicBlock::iterator
542 inst_iter = succ_basic_block->begin(),
543 inst_end = succ_basic_block->end();
544 inst_iter != inst_end; ++inst_iter) {
545 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
546
547 if (!phi) {
548 break; // Meet non-phi instruction. Done.
549 }
550
551 // Update the incoming block of this phi instruction
552 for (llvm::PHINode::block_iterator
553 ibb_iter = phi->block_begin(), ibb_end = phi->block_end();
554 ibb_iter != ibb_end; ++ibb_iter) {
555 if (*ibb_iter == old_basic_block) {
556 *ibb_iter = new_basic_block;
557 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700558 }
559 }
560 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700561}
562
563llvm::Value* GBCExpanderPass::ExpandToRuntime(runtime_support::RuntimeId rt,
564 llvm::CallInst& inst) {
565 // Some GBC intrinsic can directly replace with IBC runtime. "Directly" means
566 // the arguments passed to the GBC intrinsic are as the same as IBC runtime
567 // function, therefore only called function is needed to change.
568 unsigned num_args = inst.getNumArgOperands();
569
570 if (num_args <= 0) {
571 return irb_.CreateCall(irb_.GetRuntime(rt));
572 } else {
573 std::vector<llvm::Value*> args;
574 for (unsigned i = 0; i < num_args; i++) {
575 args.push_back(inst.getArgOperand(i));
576 }
577
578 return irb_.CreateCall(irb_.GetRuntime(rt), args);
579 }
580}
581
Logan Chien67645d82012-08-17 09:10:54 +0800582void
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700583GBCExpanderPass::EmitStackOverflowCheck(llvm::Instruction* first_non_alloca) {
584 llvm::Function* func = first_non_alloca->getParent()->getParent();
585 llvm::Module* module = func->getParent();
586
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700587 // Call llvm intrinsic function to get frame address.
588 llvm::Function* frameaddress =
589 llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
590
591 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
592 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
593
594 // Cast i8* to int
595 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
596
597 // Get thread.stack_end_
598 llvm::Value* stack_end =
TDYa127920be7c2012-09-10 17:13:22 -0700599 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::StackEndOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700600 irb_.getPtrEquivIntTy(),
601 kTBAARuntimeInfo);
602
603 // Check the frame address < thread.stack_end_ ?
604 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
605
606 llvm::BasicBlock* block_exception =
607 llvm::BasicBlock::Create(context_, "stack_overflow", func);
608
609 llvm::BasicBlock* block_continue =
610 llvm::BasicBlock::Create(context_, "stack_overflow_cont", func);
611
612 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
613
614 // If stack overflow, throw exception.
615 irb_.SetInsertPoint(block_exception);
616 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowStackOverflowException));
617
618 // Unwind.
619 llvm::Type* ret_type = func->getReturnType();
620 if (ret_type->isVoidTy()) {
621 irb_.CreateRetVoid();
622 } else {
623 // The return value is ignored when there's an exception. MethodCompiler
624 // returns zero value under the the corresponding return type in this case.
625 // GBCExpander returns LLVM undef value here for brevity
626 irb_.CreateRet(llvm::UndefValue::get(ret_type));
627 }
628
629 irb_.SetInsertPoint(block_continue);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700630}
631
TDYa127920be7c2012-09-10 17:13:22 -0700632llvm::Value* GBCExpanderPass::EmitLoadDexCacheAddr(art::MemberOffset offset) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700633 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
634
635 return irb_.LoadFromObjectOffset(method_object_addr,
636 offset.Int32Value(),
637 irb_.getJObjectTy(),
638 kTBAAConstJObject);
639}
640
641llvm::Value*
642GBCExpanderPass::EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
643 llvm::Value* static_storage_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800644 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheInitializedStaticStorageOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700645
646 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
647
648 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
649}
650
651llvm::Value*
652GBCExpanderPass::EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
653 llvm::Value* resolved_type_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800654 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedTypesOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700655
656 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
657
658 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
659}
660
661llvm::Value* GBCExpanderPass::
662EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
663 llvm::Value* resolved_method_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800664 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedMethodsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700665
666 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
667
668 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
669}
670
671llvm::Value* GBCExpanderPass::
672EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
673 llvm::Value* string_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800674 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheStringsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700675
676 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
677
678 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
679}
680
681llvm::Value* GBCExpanderPass::EmitLoadMethodObjectAddr() {
682 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
683 return parent_func->arg_begin();
684}
685
686llvm::Value* GBCExpanderPass::EmitLoadArrayLength(llvm::Value* array) {
687 // Load array length
688 return irb_.LoadFromObjectOffset(array,
Ian Rogers98573f92013-01-30 17:26:32 -0800689 art::mirror::Array::LengthOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700690 irb_.getJIntTy(),
691 kTBAAConstJObject);
692
693}
694
695llvm::Value*
696GBCExpanderPass::EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
697 llvm::Value* callee_method_object_field_addr =
698 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
699
TDYa127ce4cc0d2012-11-18 16:59:53 -0800700 return irb_.CreateLoad(callee_method_object_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700701}
702
703llvm::Value* GBCExpanderPass::
704EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, llvm::Value* this_addr) {
705 // Load class object of *this* pointer
706 llvm::Value* class_object_addr =
707 irb_.LoadFromObjectOffset(this_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800708 art::mirror::Object::ClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700709 irb_.getJObjectTy(),
710 kTBAAConstJObject);
711
712 // Load vtable address
713 llvm::Value* vtable_addr =
714 irb_.LoadFromObjectOffset(class_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800715 art::mirror::Class::VTableOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700716 irb_.getJObjectTy(),
717 kTBAAConstJObject);
718
719 // Load callee method object
720 llvm::Value* vtable_idx_value =
721 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
722
723 llvm::Value* method_field_addr =
724 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
725
726 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
727}
728
729// Emit Array GetElementPtr
730llvm::Value* GBCExpanderPass::EmitArrayGEP(llvm::Value* array_addr,
731 llvm::Value* index_value,
732 JType elem_jty) {
733
734 int data_offset;
735 if (elem_jty == kLong || elem_jty == kDouble ||
Ian Rogers98573f92013-01-30 17:26:32 -0800736 (elem_jty == kObject && sizeof(uint64_t) == sizeof(art::mirror::Object*))) {
737 data_offset = art::mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700738 } else {
Ian Rogers98573f92013-01-30 17:26:32 -0800739 data_offset = art::mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700740 }
741
742 llvm::Constant* data_offset_value =
743 irb_.getPtrEquivInt(data_offset);
744
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800745 llvm::Type* elem_type = irb_.getJType(elem_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700746
747 llvm::Value* array_data_addr =
748 irb_.CreatePtrDisp(array_addr, data_offset_value,
749 elem_type->getPointerTo());
750
751 return irb_.CreateGEP(array_data_addr, index_value);
752}
753
754void GBCExpanderPass::Expand_TestSuspend(llvm::CallInst& call_inst) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800755 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
756
757 llvm::Value* suspend_count =
758 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::ThreadFlagsOffset().Int32Value(),
759 irb_.getInt16Ty(),
760 kTBAARuntimeInfo);
761 llvm::Value* is_suspend = irb_.CreateICmpNE(suspend_count, irb_.getInt16(0));
762
763 llvm::BasicBlock* basic_block_suspend = CreateBasicBlockWithDexPC(dex_pc, "suspend");
764 llvm::BasicBlock* basic_block_cont = CreateBasicBlockWithDexPC(dex_pc, "suspend_cont");
765
766 irb_.CreateCondBr(is_suspend, basic_block_suspend, basic_block_cont, kUnlikely);
767
768 irb_.SetInsertPoint(basic_block_suspend);
769 if (dex_pc != art::DexFile::kDexNoIndex) {
770 EmitUpdateDexPC(dex_pc);
771 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700772 irb_.Runtime().EmitTestSuspend();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800773
774 llvm::BasicBlock* basic_block_exception = CreateBasicBlockWithDexPC(dex_pc, "exception");
775 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
776 irb_.CreateCondBr(exception_pending, basic_block_exception, basic_block_cont, kUnlikely);
777
778 irb_.SetInsertPoint(basic_block_exception);
779 llvm::Type* ret_type = call_inst.getParent()->getParent()->getReturnType();
780 if (ret_type->isVoidTy()) {
781 irb_.CreateRetVoid();
782 } else {
783 // The return value is ignored when there's an exception.
784 irb_.CreateRet(llvm::UndefValue::get(ret_type));
785 }
TDYa127ce4cc0d2012-11-18 16:59:53 -0800786
787 irb_.SetInsertPoint(basic_block_cont);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700788 return;
789}
790
TDYa1279a129452012-07-19 03:10:08 -0700791void GBCExpanderPass::Expand_MarkGCCard(llvm::CallInst& call_inst) {
TDYa1279a129452012-07-19 03:10:08 -0700792 irb_.Runtime().EmitMarkGCCard(call_inst.getArgOperand(0), call_inst.getArgOperand(1));
TDYa1279a129452012-07-19 03:10:08 -0700793 return;
794}
795
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700796llvm::Value*
797GBCExpanderPass::Expand_LoadStringFromDexCache(llvm::Value* string_idx_value) {
798 uint32_t string_idx =
799 llvm::cast<llvm::ConstantInt>(string_idx_value)->getZExtValue();
800
801 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
802
TDYa127ce4cc0d2012-11-18 16:59:53 -0800803 return irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700804}
805
806llvm::Value*
807GBCExpanderPass::Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value) {
808 uint32_t type_idx =
809 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
810
811 llvm::Value* type_field_addr =
812 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
813
TDYa127ce4cc0d2012-11-18 16:59:53 -0800814 return irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700815}
816
817void GBCExpanderPass::Expand_LockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700818 rtb_.EmitLockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700819 return;
820}
821
822void GBCExpanderPass::Expand_UnlockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700823 rtb_.EmitUnlockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700824 return;
825}
826
827llvm::Value* GBCExpanderPass::Expand_ArrayGet(llvm::Value* array_addr,
828 llvm::Value* index_value,
829 JType elem_jty) {
830 llvm::Value* array_elem_addr =
831 EmitArrayGEP(array_addr, index_value, elem_jty);
832
833 return irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
834}
835
836void GBCExpanderPass::Expand_ArrayPut(llvm::Value* new_value,
837 llvm::Value* array_addr,
838 llvm::Value* index_value,
839 JType elem_jty) {
840 llvm::Value* array_elem_addr =
841 EmitArrayGEP(array_addr, index_value, elem_jty);
842
843 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
844
845 return;
846}
847
848void GBCExpanderPass::Expand_FilledNewArray(llvm::CallInst& call_inst) {
849 // Most of the codes refer to MethodCompiler::EmitInsn_FilledNewArray
850 llvm::Value* array = call_inst.getArgOperand(0);
851
852 uint32_t element_jty =
853 llvm::cast<llvm::ConstantInt>(call_inst.getArgOperand(1))->getZExtValue();
854
855 DCHECK(call_inst.getNumArgOperands() > 2);
856 unsigned num_elements = (call_inst.getNumArgOperands() - 2);
857
858 bool is_elem_int_ty = (static_cast<JType>(element_jty) == kInt);
859
860 uint32_t alignment;
861 llvm::Constant* elem_size;
862 llvm::PointerType* field_type;
863
864 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
865 // as the element, thus we are only checking 2 cases: primitive int and
866 // non-primitive type.
867 if (is_elem_int_ty) {
868 alignment = sizeof(int32_t);
869 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
870 field_type = irb_.getJIntTy()->getPointerTo();
871 } else {
872 alignment = irb_.getSizeOfPtrEquivInt();
873 elem_size = irb_.getSizeOfPtrEquivIntValue();
874 field_type = irb_.getJObjectTy()->getPointerTo();
875 }
876
877 llvm::Value* data_field_offset =
Ian Rogers98573f92013-01-30 17:26:32 -0800878 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700879
880 llvm::Value* data_field_addr =
881 irb_.CreatePtrDisp(array, data_field_offset, field_type);
882
883 for (unsigned i = 0; i < num_elements; ++i) {
884 // Values to fill the array begin at the 3rd argument
885 llvm::Value* reg_value = call_inst.getArgOperand(2 + i);
886
887 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
888
889 data_field_addr =
890 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
891 }
892
893 return;
894}
895
896llvm::Value* GBCExpanderPass::Expand_IGetFast(llvm::Value* field_offset_value,
897 llvm::Value* /*is_volatile_value*/,
898 llvm::Value* object_addr,
899 JType field_jty) {
900 int field_offset =
901 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
902
903 DCHECK_GE(field_offset, 0);
904
905 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800906 irb_.getJType(field_jty)->getPointerTo();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700907
908 field_offset_value = irb_.getPtrEquivInt(field_offset);
909
910 llvm::Value* field_addr =
911 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
912
913 // TODO: Check is_volatile. We need to generate atomic load instruction
914 // when is_volatile is true.
915 return irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
916}
917
918void GBCExpanderPass::Expand_IPutFast(llvm::Value* field_offset_value,
919 llvm::Value* /* is_volatile_value */,
920 llvm::Value* object_addr,
921 llvm::Value* new_value,
922 JType field_jty) {
923 int field_offset =
924 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
925
926 DCHECK_GE(field_offset, 0);
927
928 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800929 irb_.getJType(field_jty)->getPointerTo();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700930
931 field_offset_value = irb_.getPtrEquivInt(field_offset);
932
933 llvm::Value* field_addr =
934 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
935
936 // TODO: Check is_volatile. We need to generate atomic store instruction
937 // when is_volatile is true.
938 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
939
940 return;
941}
942
943llvm::Value* GBCExpanderPass::Expand_SGetFast(llvm::Value* static_storage_addr,
944 llvm::Value* field_offset_value,
945 llvm::Value* /*is_volatile_value*/,
946 JType field_jty) {
947 int field_offset =
948 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
949
950 DCHECK_GE(field_offset, 0);
951
952 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
953
954 llvm::Value* static_field_addr =
955 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800956 irb_.getJType(field_jty)->getPointerTo());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700957
958 // TODO: Check is_volatile. We need to generate atomic store instruction
959 // when is_volatile is true.
960 return irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
961}
962
963void GBCExpanderPass::Expand_SPutFast(llvm::Value* static_storage_addr,
964 llvm::Value* field_offset_value,
965 llvm::Value* /* is_volatile_value */,
966 llvm::Value* new_value,
967 JType field_jty) {
968 int field_offset =
969 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
970
971 DCHECK_GE(field_offset, 0);
972
973 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
974
975 llvm::Value* static_field_addr =
976 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800977 irb_.getJType(field_jty)->getPointerTo());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700978
979 // TODO: Check is_volatile. We need to generate atomic store instruction
980 // when is_volatile is true.
981 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
982
983 return;
984}
985
986llvm::Value*
987GBCExpanderPass::Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr) {
988 return irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800989 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700990 irb_.getJObjectTy(),
991 kTBAAConstJObject);
992}
993
994llvm::Value*
995GBCExpanderPass::Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value) {
996 uint32_t type_idx =
997 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
998
999 llvm::Value* storage_field_addr =
1000 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1001
TDYa127ce4cc0d2012-11-18 16:59:53 -08001002 return irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001003}
1004
1005llvm::Value*
1006GBCExpanderPass::Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value) {
1007 uint32_t callee_method_idx =
1008 llvm::cast<llvm::ConstantInt>(callee_method_idx_value)->getZExtValue();
1009
1010 return EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
1011}
1012
1013llvm::Value* GBCExpanderPass::Expand_GetVirtualCalleeMethodObjAddrFast(
1014 llvm::Value* vtable_idx_value,
1015 llvm::Value* this_addr) {
1016 int vtable_idx =
1017 llvm::cast<llvm::ConstantInt>(vtable_idx_value)->getSExtValue();
1018
1019 return EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
1020}
1021
1022llvm::Value* GBCExpanderPass::Expand_Invoke(llvm::CallInst& call_inst) {
1023 // Most of the codes refer to MethodCompiler::EmitInsn_Invoke
1024 llvm::Value* callee_method_object_addr = call_inst.getArgOperand(0);
1025 unsigned num_args = call_inst.getNumArgOperands();
1026 llvm::Type* ret_type = call_inst.getType();
1027
1028 // Determine the function type of the callee method
1029 std::vector<llvm::Type*> args_type;
1030 std::vector<llvm::Value*> args;
1031 for (unsigned i = 0; i < num_args; i++) {
1032 args.push_back(call_inst.getArgOperand(i));
1033 args_type.push_back(args[i]->getType());
1034 }
1035
1036 llvm::FunctionType* callee_method_type =
1037 llvm::FunctionType::get(ret_type, args_type, false);
1038
1039 llvm::Value* code_addr =
1040 irb_.LoadFromObjectOffset(callee_method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001041 art::mirror::AbstractMethod::GetCodeOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001042 callee_method_type->getPointerTo(),
TDYa127ce4cc0d2012-11-18 16:59:53 -08001043 kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001044
1045 // Invoke callee
1046 llvm::Value* retval = irb_.CreateCall(code_addr, args);
1047
1048 return retval;
1049}
1050
TDYa1274ec8ccd2012-08-11 07:04:57 -07001051llvm::Value* GBCExpanderPass::Expand_DivRem(llvm::CallInst& call_inst,
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001052 bool is_div, JType op_jty) {
TDYa1274ec8ccd2012-08-11 07:04:57 -07001053 llvm::Value* dividend = call_inst.getArgOperand(0);
1054 llvm::Value* divisor = call_inst.getArgOperand(1);
TDYa1274ec8ccd2012-08-11 07:04:57 -07001055 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1056 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001057 // Most of the codes refer to MethodCompiler::EmitIntDivRemResultComputation
1058
1059 // Check the special case: MININT / -1 = MININT
1060 // That case will cause overflow, which is undefined behavior in llvm.
1061 // So we check the divisor is -1 or not, if the divisor is -1, we do
1062 // the special path to avoid undefined behavior.
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001063 llvm::Type* op_type = irb_.getJType(op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001064 llvm::Value* zero = irb_.getJZero(op_jty);
1065 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
1066
TDYa1275e869b62012-07-25 00:45:39 -07001067 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001068 llvm::BasicBlock* eq_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1069 llvm::BasicBlock* ne_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1070 llvm::BasicBlock* neg_one_cont =
1071 llvm::BasicBlock::Create(context_, "", parent);
1072
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001073 llvm::Value* is_equal_neg_one = irb_.CreateICmpEQ(divisor, neg_one);
1074 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
1075
1076 // If divisor == -1
1077 irb_.SetInsertPoint(eq_neg_one);
1078 llvm::Value* eq_result;
1079 if (is_div) {
1080 // We can just change from "dividend div -1" to "neg dividend". The sub
1081 // don't care the sign/unsigned because of two's complement representation.
1082 // And the behavior is what we want:
1083 // -(2^n) (2^n)-1
1084 // MININT < k <= MAXINT -> mul k -1 = -k
1085 // MININT == k -> mul k -1 = k
1086 //
1087 // LLVM use sub to represent 'neg'
1088 eq_result = irb_.CreateSub(zero, dividend);
1089 } else {
1090 // Everything modulo -1 will be 0.
1091 eq_result = zero;
1092 }
1093 irb_.CreateBr(neg_one_cont);
1094
1095 // If divisor != -1, just do the division.
1096 irb_.SetInsertPoint(ne_neg_one);
1097 llvm::Value* ne_result;
1098 if (is_div) {
1099 ne_result = irb_.CreateSDiv(dividend, divisor);
1100 } else {
1101 ne_result = irb_.CreateSRem(dividend, divisor);
1102 }
1103 irb_.CreateBr(neg_one_cont);
1104
1105 irb_.SetInsertPoint(neg_one_cont);
1106 llvm::PHINode* result = irb_.CreatePHI(op_type, 2);
1107 result->addIncoming(eq_result, eq_neg_one);
1108 result->addIncoming(ne_result, ne_neg_one);
1109
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001110 return result;
1111}
1112
TDYa127ce4cc0d2012-11-18 16:59:53 -08001113void GBCExpanderPass::Expand_AllocaShadowFrame(llvm::Value* num_vregs_value) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001114 // Most of the codes refer to MethodCompiler::EmitPrologueAllocShadowFrame and
1115 // MethodCompiler::EmitPushShadowFrame
TDYa1278e950c12012-11-02 09:58:19 -07001116 uint16_t num_vregs =
1117 llvm::cast<llvm::ConstantInt>(num_vregs_value)->getZExtValue();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001118
1119 llvm::StructType* shadow_frame_type =
TDYa127ce4cc0d2012-11-18 16:59:53 -08001120 irb_.getShadowFrameTy(num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001121
Sebastien Hertz77209702013-02-28 16:34:13 +01001122 // Create allocas at the start of entry block.
1123 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1124 llvm::BasicBlock* entry_block = &func_->front();
1125 irb_.SetInsertPoint(&entry_block->front());
1126
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001127 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
1128
1129 // Alloca a pointer to old shadow frame
1130 old_shadow_frame_ =
1131 irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
1132
Sebastien Hertz77209702013-02-28 16:34:13 +01001133 irb_.restoreIP(irb_ip_original);
1134
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001135 // Push the shadow frame
1136 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1137
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001138 llvm::Value* shadow_frame_upcast =
1139 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
1140
1141 llvm::Value* result = rtb_.EmitPushShadowFrame(shadow_frame_upcast,
1142 method_object_addr,
TDYa1278e950c12012-11-02 09:58:19 -07001143 num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001144
1145 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
1146
1147 return;
1148}
1149
TDYa1278e950c12012-11-02 09:58:19 -07001150void GBCExpanderPass::Expand_SetVReg(llvm::Value* entry_idx,
1151 llvm::Value* value) {
1152 DCHECK(shadow_frame_ != NULL);
1153
1154 llvm::Value* gep_index[] = {
1155 irb_.getInt32(0), // No pointer displacement
TDYa127ce4cc0d2012-11-18 16:59:53 -08001156 irb_.getInt32(1), // VRegs
TDYa1278e950c12012-11-02 09:58:19 -07001157 entry_idx // Pointer field
1158 };
1159
1160 llvm::Value* vreg_addr = irb_.CreateGEP(shadow_frame_, gep_index);
1161
1162 irb_.CreateStore(value,
1163 irb_.CreateBitCast(vreg_addr, value->getType()->getPointerTo()),
1164 kTBAAShadowFrame);
1165 return;
1166}
1167
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001168void GBCExpanderPass::Expand_PopShadowFrame() {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001169 if (old_shadow_frame_ == NULL) {
1170 return;
1171 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001172 rtb_.EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
1173 return;
1174}
1175
1176void GBCExpanderPass::Expand_UpdateDexPC(llvm::Value* dex_pc_value) {
1177 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07001178 art::ShadowFrame::DexPCOffset(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001179 dex_pc_value,
1180 kTBAAShadowFrame);
1181 return;
1182}
1183
Logan Chien67645d82012-08-17 09:10:54 +08001184void GBCExpanderPass::InsertStackOverflowCheck(llvm::Function& func) {
jeffhao40283122013-01-15 13:15:24 -08001185 // All alloca instructions are generated in the first basic block of the
1186 // function, and there are no alloca instructions after the first non-alloca
1187 // instruction.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001188
Logan Chien67645d82012-08-17 09:10:54 +08001189 llvm::BasicBlock* first_basic_block = &func.front();
1190
1191 // Look for first non-alloca instruction
1192 llvm::BasicBlock::iterator first_non_alloca = first_basic_block->begin();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001193 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1194 ++first_non_alloca;
1195 }
1196
Logan Chien67645d82012-08-17 09:10:54 +08001197 irb_.SetInsertPoint(first_non_alloca);
1198
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001199 // Insert stack overflow check codes before first_non_alloca (i.e., after all
1200 // alloca instructions)
Logan Chien67645d82012-08-17 09:10:54 +08001201 EmitStackOverflowCheck(&*first_non_alloca);
1202
TDYa127890ea892012-08-22 10:49:42 -07001203 irb_.Runtime().EmitTestSuspend();
TDYa127890ea892012-08-22 10:49:42 -07001204
Logan Chien67645d82012-08-17 09:10:54 +08001205 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
1206 if (next_basic_block != first_basic_block) {
1207 // Splice the rest of the instruction to the continuing basic block
1208 next_basic_block->getInstList().splice(
1209 irb_.GetInsertPoint(), first_basic_block->getInstList(),
1210 first_non_alloca, first_basic_block->end());
1211
1212 // Rewrite the basic block
1213 RewriteBasicBlock(next_basic_block);
1214
1215 // Update the phi-instructions in the successor basic block
1216 UpdatePhiInstruction(first_basic_block, irb_.GetInsertBlock());
1217 }
1218
1219 // We have changed the basic block
1220 changed_ = true;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001221}
1222
TDYa1275e869b62012-07-25 00:45:39 -07001223// ==== High-level intrinsic expander ==========================================
1224
TDYa127a1b21852012-07-23 03:20:39 -07001225llvm::Value* GBCExpanderPass::Expand_FPCompare(llvm::Value* src1_value,
1226 llvm::Value* src2_value,
1227 bool gt_bias) {
1228 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1229 llvm::Value* cmp_lt;
1230
1231 if (gt_bias) {
1232 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1233 } else {
1234 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1235 }
1236
1237 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1238}
1239
1240llvm::Value* GBCExpanderPass::Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value) {
1241 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1242 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1243
1244 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1245}
1246
1247llvm::Value* GBCExpanderPass::EmitCompareResultSelection(llvm::Value* cmp_eq,
1248 llvm::Value* cmp_lt) {
1249
1250 llvm::Constant* zero = irb_.getJInt(0);
1251 llvm::Constant* pos1 = irb_.getJInt(1);
1252 llvm::Constant* neg1 = irb_.getJInt(-1);
1253
1254 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1255 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1256
1257 return result_eq;
1258}
1259
Logan Chien75e4b602012-07-23 14:24:12 -07001260llvm::Value* GBCExpanderPass::Expand_IntegerShift(llvm::Value* src1_value,
1261 llvm::Value* src2_value,
1262 IntegerShiftKind kind,
1263 JType op_jty) {
1264 DCHECK(op_jty == kInt || op_jty == kLong);
1265
1266 // Mask and zero-extend RHS properly
1267 if (op_jty == kInt) {
1268 src2_value = irb_.CreateAnd(src2_value, 0x1f);
1269 } else {
1270 llvm::Value* masked_src2_value = irb_.CreateAnd(src2_value, 0x3f);
1271 src2_value = irb_.CreateZExt(masked_src2_value, irb_.getJLongTy());
1272 }
1273
1274 // Create integer shift llvm instruction
1275 switch (kind) {
1276 case kIntegerSHL:
1277 return irb_.CreateShl(src1_value, src2_value);
1278
1279 case kIntegerSHR:
1280 return irb_.CreateAShr(src1_value, src2_value);
1281
1282 case kIntegerUSHR:
1283 return irb_.CreateLShr(src1_value, src2_value);
1284
1285 default:
1286 LOG(FATAL) << "Unknown integer shift kind: " << kind;
1287 return NULL;
1288 }
1289}
1290
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001291llvm::Value* GBCExpanderPass::SignOrZeroExtendCat1Types(llvm::Value* value, JType jty) {
1292 switch (jty) {
1293 case kBoolean:
1294 case kChar:
1295 return irb_.CreateZExt(value, irb_.getJType(kInt));
1296 case kByte:
1297 case kShort:
1298 return irb_.CreateSExt(value, irb_.getJType(kInt));
1299 case kVoid:
1300 case kInt:
1301 case kLong:
1302 case kFloat:
1303 case kDouble:
1304 case kObject:
1305 return value; // Nothing to do.
1306 default:
1307 LOG(FATAL) << "Unknown java type: " << jty;
1308 return NULL;
1309 }
1310}
1311
1312llvm::Value* GBCExpanderPass::TruncateCat1Types(llvm::Value* value, JType jty) {
1313 switch (jty) {
1314 case kBoolean:
1315 case kChar:
1316 case kByte:
1317 case kShort:
1318 return irb_.CreateTrunc(value, irb_.getJType(jty));
1319 case kVoid:
1320 case kInt:
1321 case kLong:
1322 case kFloat:
1323 case kDouble:
1324 case kObject:
1325 return value; // Nothing to do.
1326 default:
1327 LOG(FATAL) << "Unknown java type: " << jty;
1328 return NULL;
1329 }
1330}
1331
TDYa1275a26d442012-07-26 18:58:38 -07001332llvm::Value* GBCExpanderPass::Expand_HLArrayGet(llvm::CallInst& call_inst,
1333 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001334 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1335 llvm::Value* array_addr = call_inst.getArgOperand(1);
1336 llvm::Value* index_value = call_inst.getArgOperand(2);
TDYa127920be7c2012-09-10 17:13:22 -07001337 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001338
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001339 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1340 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1341 opt_flags);
TDYa1275a26d442012-07-26 18:58:38 -07001342
1343 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1344
1345 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
1346
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001347 return SignOrZeroExtendCat1Types(array_elem_value, elem_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001348}
1349
1350
1351void GBCExpanderPass::Expand_HLArrayPut(llvm::CallInst& call_inst,
1352 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001353 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1354 llvm::Value* new_value = call_inst.getArgOperand(1);
1355 llvm::Value* array_addr = call_inst.getArgOperand(2);
1356 llvm::Value* index_value = call_inst.getArgOperand(3);
TDYa127920be7c2012-09-10 17:13:22 -07001357 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001358
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001359 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1360 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1361 opt_flags);
TDYa1275a26d442012-07-26 18:58:38 -07001362
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001363 new_value = TruncateCat1Types(new_value, elem_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001364
1365 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1366
1367 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
1368 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::CheckPutArrayElement);
1369
1370 irb_.CreateCall2(runtime_func, new_value, array_addr);
1371
1372 EmitGuard_ExceptionLandingPad(dex_pc);
1373
1374 EmitMarkGCCard(new_value, array_addr);
1375 }
1376
1377 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1378
1379 return;
1380}
1381
TDYa1275e869b62012-07-25 00:45:39 -07001382llvm::Value* GBCExpanderPass::Expand_HLIGet(llvm::CallInst& call_inst,
1383 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001384 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1385 llvm::Value* object_addr = call_inst.getArgOperand(1);
1386 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(2));
TDYa127920be7c2012-09-10 17:13:22 -07001387 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001388
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001389 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -07001390
1391 llvm::Value* field_value;
1392
1393 int field_offset;
1394 bool is_volatile;
Ian Rogers1212a022013-03-04 10:48:41 -08001395 bool is_fast_path = driver_->ComputeInstanceFieldInfo(
TDYa1275e869b62012-07-25 00:45:39 -07001396 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
1397
1398 if (!is_fast_path) {
1399 llvm::Function* runtime_func;
1400
1401 if (field_jty == kObject) {
1402 runtime_func = irb_.GetRuntime(runtime_support::GetObjectInstance);
1403 } else if (field_jty == kLong || field_jty == kDouble) {
1404 runtime_func = irb_.GetRuntime(runtime_support::Get64Instance);
1405 } else {
1406 runtime_func = irb_.GetRuntime(runtime_support::Get32Instance);
1407 }
1408
1409 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
1410
1411 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1412
1413 EmitUpdateDexPC(dex_pc);
1414
1415 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
1416 method_object_addr, object_addr);
1417
1418 EmitGuard_ExceptionLandingPad(dex_pc);
1419
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001420 if (field_jty == kFloat || field_jty == kDouble) {
1421 field_value = irb_.CreateBitCast(field_value, irb_.getJType(field_jty));
1422 }
TDYa1275e869b62012-07-25 00:45:39 -07001423 } else {
1424 DCHECK_GE(field_offset, 0);
1425
1426 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001427 irb_.getJType(field_jty)->getPointerTo();
TDYa1275e869b62012-07-25 00:45:39 -07001428
1429 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
1430
1431 llvm::Value* field_addr =
1432 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1433
1434 // TODO: Check is_volatile. We need to generate atomic load instruction
1435 // when is_volatile is true.
1436 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001437 field_value = SignOrZeroExtendCat1Types(field_value, field_jty);
TDYa1275e869b62012-07-25 00:45:39 -07001438 }
1439
TDYa1275e869b62012-07-25 00:45:39 -07001440 return field_value;
1441}
1442
1443void GBCExpanderPass::Expand_HLIPut(llvm::CallInst& call_inst,
1444 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001445 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001446 llvm::Value* new_value = call_inst.getArgOperand(1);
1447 llvm::Value* object_addr = call_inst.getArgOperand(2);
TDYa1275e869b62012-07-25 00:45:39 -07001448 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(3));
TDYa127920be7c2012-09-10 17:13:22 -07001449 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001450
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001451 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -07001452
1453 int field_offset;
1454 bool is_volatile;
Ian Rogers1212a022013-03-04 10:48:41 -08001455 bool is_fast_path = driver_->ComputeInstanceFieldInfo(
TDYa1275e869b62012-07-25 00:45:39 -07001456 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
1457
1458 if (!is_fast_path) {
1459 llvm::Function* runtime_func;
1460
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001461 if (field_jty == kFloat) {
1462 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1463 } else if (field_jty == kDouble) {
1464 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1465 }
1466
TDYa1275e869b62012-07-25 00:45:39 -07001467 if (field_jty == kObject) {
1468 runtime_func = irb_.GetRuntime(runtime_support::SetObjectInstance);
1469 } else if (field_jty == kLong || field_jty == kDouble) {
1470 runtime_func = irb_.GetRuntime(runtime_support::Set64Instance);
1471 } else {
1472 runtime_func = irb_.GetRuntime(runtime_support::Set32Instance);
1473 }
1474
1475 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1476
1477 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1478
1479 EmitUpdateDexPC(dex_pc);
1480
1481 irb_.CreateCall4(runtime_func, field_idx_value,
1482 method_object_addr, object_addr, new_value);
1483
1484 EmitGuard_ExceptionLandingPad(dex_pc);
1485
1486 } else {
1487 DCHECK_GE(field_offset, 0);
1488
1489 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001490 irb_.getJType(field_jty)->getPointerTo();
TDYa1275e869b62012-07-25 00:45:39 -07001491
1492 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
1493
1494 llvm::Value* field_addr =
1495 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1496
1497 // TODO: Check is_volatile. We need to generate atomic store instruction
1498 // when is_volatile is true.
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001499 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275e869b62012-07-25 00:45:39 -07001500 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1501
1502 if (field_jty == kObject) { // If put an object, mark the GC card table.
1503 EmitMarkGCCard(new_value, object_addr);
1504 }
1505 }
1506
1507 return;
1508}
1509
TDYa127f71bf5a2012-07-29 20:09:52 -07001510llvm::Value* GBCExpanderPass::EmitLoadConstantClass(uint32_t dex_pc,
1511 uint32_t type_idx) {
Ian Rogers1212a022013-03-04 10:48:41 -08001512 if (!driver_->CanAccessTypeWithoutChecks(oat_compilation_unit_->method_idx_,
Ian Rogers8e696052013-03-04 09:00:40 -08001513 *oat_compilation_unit_->dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001514 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1515
1516 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1517
1518 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1519
1520 llvm::Function* runtime_func =
1521 irb_.GetRuntime(runtime_support::InitializeTypeAndVerifyAccess);
1522
1523 EmitUpdateDexPC(dex_pc);
1524
1525 llvm::Value* type_object_addr =
1526 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1527
1528 EmitGuard_ExceptionLandingPad(dex_pc);
1529
1530 return type_object_addr;
1531
1532 } else {
1533 // Try to load the class (type) object from the test cache.
1534 llvm::Value* type_field_addr =
1535 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1536
TDYa127ce4cc0d2012-11-18 16:59:53 -08001537 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07001538
Ian Rogers1212a022013-03-04 10:48:41 -08001539 if (driver_->CanAssumeTypeIsPresentInDexCache(*oat_compilation_unit_->dex_file_, type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001540 return type_object_addr;
1541 }
1542
1543 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1544
1545 // Test whether class (type) object is in the dex cache or not
1546 llvm::Value* equal_null =
1547 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1548
1549 llvm::BasicBlock* block_cont =
1550 CreateBasicBlockWithDexPC(dex_pc, "cont");
1551
1552 llvm::BasicBlock* block_load_class =
1553 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1554
1555 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
1556
1557 // Failback routine to load the class object
1558 irb_.SetInsertPoint(block_load_class);
1559
1560 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeType);
1561
1562 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1563
1564 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1565
1566 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1567
1568 EmitUpdateDexPC(dex_pc);
1569
1570 llvm::Value* loaded_type_object_addr =
1571 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1572
1573 EmitGuard_ExceptionLandingPad(dex_pc);
1574
1575 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1576
1577 irb_.CreateBr(block_cont);
1578
1579 // Now the class object must be loaded
1580 irb_.SetInsertPoint(block_cont);
1581
1582 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1583
1584 phi->addIncoming(type_object_addr, block_original);
1585 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1586
1587 return phi;
1588 }
1589}
1590
TDYa1275a26d442012-07-26 18:58:38 -07001591llvm::Value* GBCExpanderPass::EmitLoadStaticStorage(uint32_t dex_pc,
1592 uint32_t type_idx) {
1593 llvm::BasicBlock* block_load_static =
1594 CreateBasicBlockWithDexPC(dex_pc, "load_static");
1595
1596 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1597
1598 // Load static storage from dex cache
1599 llvm::Value* storage_field_addr =
1600 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1601
TDYa127ce4cc0d2012-11-18 16:59:53 -08001602 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
TDYa1275a26d442012-07-26 18:58:38 -07001603
1604 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1605
1606 // Test: Is the static storage of this class initialized?
1607 llvm::Value* equal_null =
1608 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
1609
1610 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
1611
1612 // Failback routine to load the class object
1613 irb_.SetInsertPoint(block_load_static);
1614
1615 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeStaticStorage);
1616
1617 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1618
1619 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1620
1621 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1622
1623 EmitUpdateDexPC(dex_pc);
1624
1625 llvm::Value* loaded_storage_object_addr =
1626 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1627
1628 EmitGuard_ExceptionLandingPad(dex_pc);
1629
1630 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
1631
1632 irb_.CreateBr(block_cont);
1633
1634 // Now the class object must be loaded
1635 irb_.SetInsertPoint(block_cont);
1636
1637 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1638
1639 phi->addIncoming(storage_object_addr, block_original);
1640 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
1641
1642 return phi;
1643}
1644
1645llvm::Value* GBCExpanderPass::Expand_HLSget(llvm::CallInst& call_inst,
1646 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001647 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1648 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1649
1650 int field_offset;
1651 int ssb_index;
1652 bool is_referrers_class;
1653 bool is_volatile;
1654
Ian Rogers1212a022013-03-04 10:48:41 -08001655 bool is_fast_path = driver_->ComputeStaticFieldInfo(
TDYa1275a26d442012-07-26 18:58:38 -07001656 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1657 is_referrers_class, is_volatile, false);
1658
1659 llvm::Value* static_field_value;
1660
1661 if (!is_fast_path) {
1662 llvm::Function* runtime_func;
1663
1664 if (field_jty == kObject) {
1665 runtime_func = irb_.GetRuntime(runtime_support::GetObjectStatic);
1666 } else if (field_jty == kLong || field_jty == kDouble) {
1667 runtime_func = irb_.GetRuntime(runtime_support::Get64Static);
1668 } else {
1669 runtime_func = irb_.GetRuntime(runtime_support::Get32Static);
1670 }
1671
1672 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1673
1674 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1675
1676 EmitUpdateDexPC(dex_pc);
1677
1678 static_field_value =
1679 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
1680
1681 EmitGuard_ExceptionLandingPad(dex_pc);
1682
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001683 if (field_jty == kFloat || field_jty == kDouble) {
1684 static_field_value = irb_.CreateBitCast(static_field_value, irb_.getJType(field_jty));
1685 }
TDYa1275a26d442012-07-26 18:58:38 -07001686 } else {
1687 DCHECK_GE(field_offset, 0);
1688
1689 llvm::Value* static_storage_addr = NULL;
1690
1691 if (is_referrers_class) {
1692 // Fast path, static storage base is this method's class
1693 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1694
1695 static_storage_addr =
1696 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001697 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001698 irb_.getJObjectTy(),
1699 kTBAAConstJObject);
1700 } else {
1701 // Medium path, static storage base in a different class which
1702 // requires checks that the other class is initialized
1703 DCHECK_GE(ssb_index, 0);
1704 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1705 }
1706
1707 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1708
1709 llvm::Value* static_field_addr =
1710 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001711 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001712
1713 // TODO: Check is_volatile. We need to generate atomic load instruction
1714 // when is_volatile is true.
1715 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001716 static_field_value = SignOrZeroExtendCat1Types(static_field_value, field_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001717 }
1718
TDYa1275a26d442012-07-26 18:58:38 -07001719 return static_field_value;
1720}
1721
1722void GBCExpanderPass::Expand_HLSput(llvm::CallInst& call_inst,
1723 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001724 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1725 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1726 llvm::Value* new_value = call_inst.getArgOperand(1);
1727
1728 if (field_jty == kFloat || field_jty == kDouble) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001729 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty));
TDYa1275a26d442012-07-26 18:58:38 -07001730 }
1731
1732 int field_offset;
1733 int ssb_index;
1734 bool is_referrers_class;
1735 bool is_volatile;
1736
Ian Rogers1212a022013-03-04 10:48:41 -08001737 bool is_fast_path = driver_->ComputeStaticFieldInfo(
TDYa1275a26d442012-07-26 18:58:38 -07001738 field_idx, oat_compilation_unit_, field_offset, ssb_index,
1739 is_referrers_class, is_volatile, true);
1740
1741 if (!is_fast_path) {
1742 llvm::Function* runtime_func;
1743
1744 if (field_jty == kObject) {
1745 runtime_func = irb_.GetRuntime(runtime_support::SetObjectStatic);
1746 } else if (field_jty == kLong || field_jty == kDouble) {
1747 runtime_func = irb_.GetRuntime(runtime_support::Set64Static);
1748 } else {
1749 runtime_func = irb_.GetRuntime(runtime_support::Set32Static);
1750 }
1751
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001752 if (field_jty == kFloat) {
1753 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1754 } else if (field_jty == kDouble) {
1755 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1756 }
1757
TDYa1275a26d442012-07-26 18:58:38 -07001758 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1759
1760 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1761
1762 EmitUpdateDexPC(dex_pc);
1763
1764 irb_.CreateCall3(runtime_func, field_idx_value,
1765 method_object_addr, new_value);
1766
1767 EmitGuard_ExceptionLandingPad(dex_pc);
1768
1769 } else {
1770 DCHECK_GE(field_offset, 0);
1771
1772 llvm::Value* static_storage_addr = NULL;
1773
1774 if (is_referrers_class) {
1775 // Fast path, static storage base is this method's class
1776 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1777
1778 static_storage_addr =
1779 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001780 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001781 irb_.getJObjectTy(),
1782 kTBAAConstJObject);
1783 } else {
1784 // Medium path, static storage base in a different class which
1785 // requires checks that the other class is initialized
1786 DCHECK_GE(ssb_index, 0);
1787 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1788 }
1789
1790 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1791
1792 llvm::Value* static_field_addr =
1793 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001794 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001795
1796 // TODO: Check is_volatile. We need to generate atomic store instruction
1797 // when is_volatile is true.
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001798 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001799 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1800
1801 if (field_jty == kObject) { // If put an object, mark the GC card table.
1802 EmitMarkGCCard(new_value, static_storage_addr);
1803 }
1804 }
1805
1806 return;
1807}
1808
TDYa127f71bf5a2012-07-29 20:09:52 -07001809llvm::Value* GBCExpanderPass::Expand_ConstString(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001810 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1811 uint32_t string_idx = LV2UInt(call_inst.getArgOperand(0));
1812
1813 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1814
TDYa127ce4cc0d2012-11-18 16:59:53 -08001815 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07001816
Ian Rogers1212a022013-03-04 10:48:41 -08001817 if (!driver_->CanAssumeStringIsPresentInDexCache(*oat_compilation_unit_->dex_file_,
Ian Rogers8e696052013-03-04 09:00:40 -08001818 string_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001819 llvm::BasicBlock* block_str_exist =
1820 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1821
1822 llvm::BasicBlock* block_str_resolve =
1823 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1824
1825 llvm::BasicBlock* block_cont =
1826 CreateBasicBlockWithDexPC(dex_pc, "str_cont");
1827
1828 // Test: Is the string resolved and in the dex cache?
1829 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1830
1831 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
1832
1833 // String is resolved, go to next basic block.
1834 irb_.SetInsertPoint(block_str_exist);
1835 irb_.CreateBr(block_cont);
1836
1837 // String is not resolved yet, resolve it now.
1838 irb_.SetInsertPoint(block_str_resolve);
1839
1840 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::ResolveString);
1841
1842 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1843
1844 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1845
1846 EmitUpdateDexPC(dex_pc);
1847
1848 llvm::Value* result = irb_.CreateCall2(runtime_func, method_object_addr,
1849 string_idx_value);
1850
1851 EmitGuard_ExceptionLandingPad(dex_pc);
1852
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001853 irb_.CreateBr(block_cont);
1854
1855
TDYa127f71bf5a2012-07-29 20:09:52 -07001856 llvm::BasicBlock* block_pre_cont = irb_.GetInsertBlock();
1857
1858 irb_.SetInsertPoint(block_cont);
1859
1860 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1861
1862 phi->addIncoming(string_addr, block_str_exist);
1863 phi->addIncoming(result, block_pre_cont);
1864
1865 string_addr = phi;
1866 }
1867
1868 return string_addr;
1869}
1870
1871llvm::Value* GBCExpanderPass::Expand_ConstClass(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001872 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1873 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1874
1875 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
1876
1877 return type_object_addr;
1878}
1879
1880void GBCExpanderPass::Expand_MonitorEnter(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001881 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1882 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07001883 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07001884
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001885 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07001886
TDYa127ce4cc0d2012-11-18 16:59:53 -08001887 EmitUpdateDexPC(dex_pc);
1888
TDYa127f71bf5a2012-07-29 20:09:52 -07001889 irb_.Runtime().EmitLockObject(object_addr);
1890
1891 return;
1892}
1893
1894void GBCExpanderPass::Expand_MonitorExit(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001895 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1896 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07001897 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07001898
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001899 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07001900
1901 EmitUpdateDexPC(dex_pc);
1902
1903 irb_.Runtime().EmitUnlockObject(object_addr);
1904
1905 EmitGuard_ExceptionLandingPad(dex_pc);
1906
1907 return;
1908}
1909
1910void GBCExpanderPass::Expand_HLCheckCast(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001911 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1912 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1913 llvm::Value* object_addr = call_inst.getArgOperand(1);
1914
1915 llvm::BasicBlock* block_test_class =
1916 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1917
1918 llvm::BasicBlock* block_test_sub_class =
1919 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1920
1921 llvm::BasicBlock* block_cont =
1922 CreateBasicBlockWithDexPC(dex_pc, "checkcast_cont");
1923
1924 // Test: Is the reference equal to null? Act as no-op when it is null.
1925 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1926
Ian Rogers8e696052013-03-04 09:00:40 -08001927 irb_.CreateCondBr(equal_null, block_cont, block_test_class, kUnlikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07001928
1929 // Test: Is the object instantiated from the given class?
1930 irb_.SetInsertPoint(block_test_class);
1931 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08001932 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07001933
1934 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1935
1936 llvm::Value* object_type_field_addr =
1937 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1938
1939 llvm::Value* object_type_object_addr =
1940 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
1941
1942 llvm::Value* equal_class =
1943 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1944
Ian Rogers8e696052013-03-04 09:00:40 -08001945 irb_.CreateCondBr(equal_class, block_cont, block_test_sub_class, kLikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07001946
1947 // Test: Is the object instantiated from the subclass of the given class?
1948 irb_.SetInsertPoint(block_test_sub_class);
1949
1950 EmitUpdateDexPC(dex_pc);
1951
1952 irb_.CreateCall2(irb_.GetRuntime(runtime_support::CheckCast),
1953 type_object_addr, object_type_object_addr);
1954
1955 EmitGuard_ExceptionLandingPad(dex_pc);
1956
1957 irb_.CreateBr(block_cont);
1958
1959 irb_.SetInsertPoint(block_cont);
1960
1961 return;
1962}
1963
1964llvm::Value* GBCExpanderPass::Expand_InstanceOf(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001965 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1966 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1967 llvm::Value* object_addr = call_inst.getArgOperand(1);
1968
1969 llvm::BasicBlock* block_nullp =
1970 CreateBasicBlockWithDexPC(dex_pc, "nullp");
1971
1972 llvm::BasicBlock* block_test_class =
1973 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1974
1975 llvm::BasicBlock* block_class_equals =
1976 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1977
1978 llvm::BasicBlock* block_test_sub_class =
1979 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1980
1981 llvm::BasicBlock* block_cont =
1982 CreateBasicBlockWithDexPC(dex_pc, "instance_of_cont");
1983
1984 // Overview of the following code :
1985 // We check for null, if so, then false, otherwise check for class == . If so
1986 // then true, otherwise do callout slowpath.
1987 //
1988 // Test: Is the reference equal to null? Set 0 when it is null.
1989 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1990
Ian Rogers8e696052013-03-04 09:00:40 -08001991 irb_.CreateCondBr(equal_null, block_nullp, block_test_class, kUnlikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07001992
1993 irb_.SetInsertPoint(block_nullp);
1994 irb_.CreateBr(block_cont);
1995
1996 // Test: Is the object instantiated from the given class?
1997 irb_.SetInsertPoint(block_test_class);
1998 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08001999 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002000
2001 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2002
2003 llvm::Value* object_type_field_addr =
2004 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2005
2006 llvm::Value* object_type_object_addr =
2007 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2008
2009 llvm::Value* equal_class =
2010 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2011
Ian Rogers8e696052013-03-04 09:00:40 -08002012 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class, kLikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002013
2014 irb_.SetInsertPoint(block_class_equals);
2015 irb_.CreateBr(block_cont);
2016
2017 // Test: Is the object instantiated from the subclass of the given class?
2018 irb_.SetInsertPoint(block_test_sub_class);
2019 llvm::Value* result =
2020 irb_.CreateCall2(irb_.GetRuntime(runtime_support::IsAssignable),
2021 type_object_addr, object_type_object_addr);
2022 irb_.CreateBr(block_cont);
2023
2024 irb_.SetInsertPoint(block_cont);
2025
2026 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJIntTy(), 3);
2027
2028 phi->addIncoming(irb_.getJInt(0), block_nullp);
2029 phi->addIncoming(irb_.getJInt(1), block_class_equals);
2030 phi->addIncoming(result, block_test_sub_class);
2031
2032 return phi;
2033}
2034
2035llvm::Value* GBCExpanderPass::Expand_NewInstance(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002036 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2037 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2038
2039 llvm::Function* runtime_func;
Ian Rogers1212a022013-03-04 10:48:41 -08002040 if (driver_->CanAccessInstantiableTypeWithoutChecks(oat_compilation_unit_->method_idx_,
Ian Rogers8e696052013-03-04 09:00:40 -08002041 *oat_compilation_unit_->dex_file_,
2042 type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002043 runtime_func = irb_.GetRuntime(runtime_support::AllocObject);
2044 } else {
2045 runtime_func = irb_.GetRuntime(runtime_support::AllocObjectWithAccessCheck);
2046 }
2047
2048 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2049
2050 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2051
2052 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2053
2054 EmitUpdateDexPC(dex_pc);
2055
2056 llvm::Value* object_addr =
2057 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
2058
2059 EmitGuard_ExceptionLandingPad(dex_pc);
2060
2061 return object_addr;
2062}
2063
2064llvm::Value* GBCExpanderPass::Expand_HLInvoke(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002065 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
TDYa127920be7c2012-09-10 17:13:22 -07002066 art::InvokeType invoke_type = static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
2067 bool is_static = (invoke_type == art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002068 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
TDYa127920be7c2012-09-10 17:13:22 -07002069 int opt_flags = LV2UInt(call_inst.getArgOperand(2));
TDYa127f71bf5a2012-07-29 20:09:52 -07002070
2071 // Compute invoke related information for compiler decision
2072 int vtable_idx = -1;
2073 uintptr_t direct_code = 0;
2074 uintptr_t direct_method = 0;
Ian Rogers1212a022013-03-04 10:48:41 -08002075 bool is_fast_path = driver_->
TDYa127f71bf5a2012-07-29 20:09:52 -07002076 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
2077 invoke_type, vtable_idx, direct_code, direct_method);
2078
2079 // Load *this* actual parameter
2080 llvm::Value* this_addr = NULL;
2081
2082 if (!is_static) {
2083 // Test: Is *this* parameter equal to null?
2084 this_addr = call_inst.getArgOperand(3);
2085 }
2086
2087 // Load the method object
2088 llvm::Value* callee_method_object_addr = NULL;
2089
2090 if (!is_fast_path) {
2091 callee_method_object_addr =
2092 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2093 this_addr, dex_pc, is_fast_path);
2094
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002095 if (!is_static) {
2096 EmitGuard_NullPointerException(dex_pc, this_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002097 }
2098 } else {
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002099 if (!is_static) {
2100 EmitGuard_NullPointerException(dex_pc, this_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002101 }
2102
2103 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002104 case art::kStatic:
2105 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002106 if (direct_method != 0u &&
2107 direct_method != static_cast<uintptr_t>(-1)) {
2108 callee_method_object_addr =
2109 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2110 irb_.getJObjectTy());
2111 } else {
2112 callee_method_object_addr =
2113 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2114 }
2115 break;
2116
TDYa127920be7c2012-09-10 17:13:22 -07002117 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002118 DCHECK(vtable_idx != -1);
2119 callee_method_object_addr =
2120 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2121 break;
2122
TDYa127920be7c2012-09-10 17:13:22 -07002123 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002124 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2125 "the fast path.";
2126 break;
2127
TDYa127920be7c2012-09-10 17:13:22 -07002128 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002129 callee_method_object_addr =
2130 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2131 invoke_type, this_addr,
2132 dex_pc, is_fast_path);
2133 break;
2134 }
2135 }
2136
2137 // Load the actual parameter
2138 std::vector<llvm::Value*> args;
2139
2140 args.push_back(callee_method_object_addr); // method object for callee
2141
2142 for (uint32_t i = 3; i < call_inst.getNumArgOperands(); ++i) {
2143 args.push_back(call_inst.getArgOperand(i));
2144 }
2145
Ian Rogers8e696052013-03-04 09:00:40 -08002146 // Generate the load of the Method*. We base the return type on that of the call as method's
2147 // returning a value are void calls if the return value is unused.
TDYa127f71bf5a2012-07-29 20:09:52 -07002148 llvm::Value* code_addr;
2149 if (direct_code != 0u &&
2150 direct_code != static_cast<uintptr_t>(-1)) {
2151 code_addr =
2152 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_code),
Ian Rogers8e696052013-03-04 09:00:40 -08002153 GetFunctionType(call_inst.getType(), callee_method_idx, is_static)->getPointerTo());
TDYa127f71bf5a2012-07-29 20:09:52 -07002154 } else {
2155 code_addr =
2156 irb_.LoadFromObjectOffset(callee_method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08002157 art::mirror::AbstractMethod::GetCodeOffset().Int32Value(),
Ian Rogers8e696052013-03-04 09:00:40 -08002158 GetFunctionType(call_inst.getType(), callee_method_idx, is_static)->getPointerTo(),
TDYa127ce4cc0d2012-11-18 16:59:53 -08002159 kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07002160 }
2161
2162 // Invoke callee
2163 EmitUpdateDexPC(dex_pc);
2164 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2165 EmitGuard_ExceptionLandingPad(dex_pc);
2166
2167 return retval;
2168}
2169
2170llvm::Value* GBCExpanderPass::Expand_OptArrayLength(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002171 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2172 // Get the array object address
2173 llvm::Value* array_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002174 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002175
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002176 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002177
2178 // Get the array length and store it to the register
2179 return EmitLoadArrayLength(array_addr);
2180}
2181
2182llvm::Value* GBCExpanderPass::Expand_NewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002183 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2184 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2185 llvm::Value* length = call_inst.getArgOperand(1);
2186
2187 return EmitAllocNewArray(dex_pc, length, type_idx, false);
2188}
2189
2190llvm::Value* GBCExpanderPass::Expand_HLFilledNewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002191 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2192 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(1));
2193 uint32_t length = call_inst.getNumArgOperands() - 3;
2194
2195 llvm::Value* object_addr =
2196 EmitAllocNewArray(dex_pc, irb_.getInt32(length), type_idx, true);
2197
2198 if (length > 0) {
2199 // Check for the element type
2200 uint32_t type_desc_len = 0;
2201 const char* type_desc =
Ian Rogers8e696052013-03-04 09:00:40 -08002202 oat_compilation_unit_->dex_file_->StringByTypeIdx(type_idx, &type_desc_len);
TDYa127f71bf5a2012-07-29 20:09:52 -07002203
2204 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
2205 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
2206 bool is_elem_int_ty = (type_desc[1] == 'I');
2207
2208 uint32_t alignment;
2209 llvm::Constant* elem_size;
2210 llvm::PointerType* field_type;
2211
2212 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
2213 // as the element, thus we are only checking 2 cases: primitive int and
2214 // non-primitive type.
2215 if (is_elem_int_ty) {
2216 alignment = sizeof(int32_t);
2217 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
2218 field_type = irb_.getJIntTy()->getPointerTo();
2219 } else {
2220 alignment = irb_.getSizeOfPtrEquivInt();
2221 elem_size = irb_.getSizeOfPtrEquivIntValue();
2222 field_type = irb_.getJObjectTy()->getPointerTo();
2223 }
2224
2225 llvm::Value* data_field_offset =
Ian Rogers98573f92013-01-30 17:26:32 -08002226 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
TDYa127f71bf5a2012-07-29 20:09:52 -07002227
2228 llvm::Value* data_field_addr =
2229 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
2230
2231 // TODO: Tune this code. Currently we are generating one instruction for
2232 // one element which may be very space consuming. Maybe changing to use
2233 // memcpy may help; however, since we can't guarantee that the alloca of
2234 // dalvik register are continuous, we can't perform such optimization yet.
2235 for (uint32_t i = 0; i < length; ++i) {
2236 llvm::Value* reg_value = call_inst.getArgOperand(i+3);
2237
2238 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
2239
2240 data_field_addr =
2241 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
2242 }
2243 }
2244
2245 return object_addr;
2246}
2247
2248void GBCExpanderPass::Expand_HLFillArrayData(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002249 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2250 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
2251 LV2SInt(call_inst.getArgOperand(0));
2252 llvm::Value* array_addr = call_inst.getArgOperand(1);
2253
TDYa127920be7c2012-09-10 17:13:22 -07002254 const art::Instruction::ArrayDataPayload* payload =
2255 reinterpret_cast<const art::Instruction::ArrayDataPayload*>(
Ian Rogers8e696052013-03-04 09:00:40 -08002256 oat_compilation_unit_->code_item_->insns_ + payload_offset);
TDYa127f71bf5a2012-07-29 20:09:52 -07002257
2258 if (payload->element_count == 0) {
2259 // When the number of the elements in the payload is zero, we don't have
2260 // to copy any numbers. However, we should check whether the array object
2261 // address is equal to null or not.
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002262 EmitGuard_NullPointerException(dex_pc, array_addr, 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002263 } else {
2264 // To save the code size, we are going to call the runtime function to
2265 // copy the content from DexFile.
2266
2267 // NOTE: We will check for the NullPointerException in the runtime.
2268
2269 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::FillArrayData);
2270
2271 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2272
2273 EmitUpdateDexPC(dex_pc);
2274
2275 irb_.CreateCall4(runtime_func,
2276 method_object_addr, irb_.getInt32(dex_pc),
2277 array_addr, irb_.getInt32(payload_offset));
2278
2279 EmitGuard_ExceptionLandingPad(dex_pc);
2280 }
2281
2282 return;
2283}
2284
2285llvm::Value* GBCExpanderPass::EmitAllocNewArray(uint32_t dex_pc,
2286 llvm::Value* array_length_value,
2287 uint32_t type_idx,
2288 bool is_filled_new_array) {
2289 llvm::Function* runtime_func;
2290
2291 bool skip_access_check =
Ian Rogers1212a022013-03-04 10:48:41 -08002292 driver_->CanAccessTypeWithoutChecks(oat_compilation_unit_->method_idx_,
Ian Rogers8e696052013-03-04 09:00:40 -08002293 *oat_compilation_unit_->dex_file_, type_idx);
TDYa127f71bf5a2012-07-29 20:09:52 -07002294
2295
2296 if (is_filled_new_array) {
2297 runtime_func = skip_access_check ?
2298 irb_.GetRuntime(runtime_support::CheckAndAllocArray) :
2299 irb_.GetRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck);
2300 } else {
2301 runtime_func = skip_access_check ?
2302 irb_.GetRuntime(runtime_support::AllocArray) :
2303 irb_.GetRuntime(runtime_support::AllocArrayWithAccessCheck);
2304 }
2305
2306 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2307
2308 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2309
2310 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2311
2312 EmitUpdateDexPC(dex_pc);
2313
2314 llvm::Value* object_addr =
2315 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
2316 array_length_value, thread_object_addr);
2317
2318 EmitGuard_ExceptionLandingPad(dex_pc);
2319
2320 return object_addr;
2321}
2322
2323llvm::Value* GBCExpanderPass::
2324EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -07002325 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -07002326 llvm::Value* this_addr,
2327 uint32_t dex_pc,
2328 bool is_fast_path) {
2329
2330 llvm::Function* runtime_func = NULL;
2331
2332 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002333 case art::kStatic:
TDYa127f71bf5a2012-07-29 20:09:52 -07002334 runtime_func = irb_.GetRuntime(runtime_support::FindStaticMethodWithAccessCheck);
2335 break;
2336
TDYa127920be7c2012-09-10 17:13:22 -07002337 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002338 runtime_func = irb_.GetRuntime(runtime_support::FindDirectMethodWithAccessCheck);
2339 break;
2340
TDYa127920be7c2012-09-10 17:13:22 -07002341 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002342 runtime_func = irb_.GetRuntime(runtime_support::FindVirtualMethodWithAccessCheck);
2343 break;
2344
TDYa127920be7c2012-09-10 17:13:22 -07002345 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002346 runtime_func = irb_.GetRuntime(runtime_support::FindSuperMethodWithAccessCheck);
2347 break;
2348
TDYa127920be7c2012-09-10 17:13:22 -07002349 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002350 if (is_fast_path) {
2351 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethod);
2352 } else {
2353 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethodWithAccessCheck);
2354 }
2355 break;
2356 }
2357
2358 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2359
2360 if (this_addr == NULL) {
TDYa127920be7c2012-09-10 17:13:22 -07002361 DCHECK_EQ(invoke_type, art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002362 this_addr = irb_.getJNull();
2363 }
2364
2365 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2366
2367 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2368
2369 EmitUpdateDexPC(dex_pc);
2370
2371 llvm::Value* callee_method_object_addr =
2372 irb_.CreateCall4(runtime_func,
2373 callee_method_idx_value,
2374 this_addr,
2375 caller_method_object_addr,
2376 thread_object_addr);
2377
2378 EmitGuard_ExceptionLandingPad(dex_pc);
2379
2380 return callee_method_object_addr;
2381}
2382
TDYa1275e869b62012-07-25 00:45:39 -07002383void GBCExpanderPass::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2384 // Using runtime support, let the target can override by InlineAssembly.
2385 irb_.Runtime().EmitMarkGCCard(value, target_addr);
2386}
2387
2388void GBCExpanderPass::EmitUpdateDexPC(uint32_t dex_pc) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002389 if (shadow_frame_ == NULL) {
2390 return;
2391 }
TDYa1275e869b62012-07-25 00:45:39 -07002392 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07002393 art::ShadowFrame::DexPCOffset(),
TDYa1275e869b62012-07-25 00:45:39 -07002394 irb_.getInt32(dex_pc),
2395 kTBAAShadowFrame);
2396}
2397
2398void GBCExpanderPass::EmitGuard_DivZeroException(uint32_t dex_pc,
2399 llvm::Value* denominator,
2400 JType op_jty) {
2401 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2402
2403 llvm::Constant* zero = irb_.getJZero(op_jty);
2404
2405 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2406
2407 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2408
2409 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2410
2411 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
2412
2413 irb_.SetInsertPoint(block_exception);
2414 EmitUpdateDexPC(dex_pc);
2415 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowDivZeroException));
2416 EmitBranchExceptionLandingPad(dex_pc);
2417
2418 irb_.SetInsertPoint(block_continue);
2419}
2420
2421void GBCExpanderPass::EmitGuard_NullPointerException(uint32_t dex_pc,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002422 llvm::Value* object,
2423 int opt_flags) {
2424 bool ignore_null_check = ((opt_flags & MIR_IGNORE_NULL_CHECK) != 0);
2425 if (ignore_null_check) {
2426 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2427 if (lpad) {
2428 // There is at least one catch: create a "fake" conditional branch to
2429 // keep the exception edge to the catch block.
2430 landing_pad_phi_mapping_[lpad].push_back(
2431 std::make_pair(current_bb_->getUniquePredecessor(),
2432 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002433
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002434 llvm::BasicBlock* block_continue =
2435 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002436
Ian Rogers8e696052013-03-04 09:00:40 -08002437 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002438
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002439 irb_.SetInsertPoint(block_continue);
2440 }
2441 } else {
2442 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
TDYa1275e869b62012-07-25 00:45:39 -07002443
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002444 llvm::BasicBlock* block_exception =
2445 CreateBasicBlockWithDexPC(dex_pc, "nullp");
TDYa1275e869b62012-07-25 00:45:39 -07002446
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002447 llvm::BasicBlock* block_continue =
2448 CreateBasicBlockWithDexPC(dex_pc, "cont");
2449
2450 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
2451
2452 irb_.SetInsertPoint(block_exception);
2453 EmitUpdateDexPC(dex_pc);
2454 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowNullPointerException),
2455 irb_.getInt32(dex_pc));
2456 EmitBranchExceptionLandingPad(dex_pc);
2457
2458 irb_.SetInsertPoint(block_continue);
2459 }
TDYa1275e869b62012-07-25 00:45:39 -07002460}
2461
2462void
2463GBCExpanderPass::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2464 llvm::Value* array,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002465 llvm::Value* index,
2466 int opt_flags) {
2467 bool ignore_range_check = ((opt_flags & MIR_IGNORE_RANGE_CHECK) != 0);
2468 if (ignore_range_check) {
2469 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2470 if (lpad) {
2471 // There is at least one catch: create a "fake" conditional branch to
2472 // keep the exception edge to the catch block.
2473 landing_pad_phi_mapping_[lpad].push_back(
2474 std::make_pair(current_bb_->getUniquePredecessor(),
2475 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002476
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002477 llvm::BasicBlock* block_continue =
2478 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002479
Ian Rogers8e696052013-03-04 09:00:40 -08002480 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002481
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002482 irb_.SetInsertPoint(block_continue);
2483 }
2484 } else {
2485 llvm::Value* array_len = EmitLoadArrayLength(array);
TDYa1275e869b62012-07-25 00:45:39 -07002486
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002487 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
TDYa1275e869b62012-07-25 00:45:39 -07002488
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002489 llvm::BasicBlock* block_exception =
2490 CreateBasicBlockWithDexPC(dex_pc, "overflow");
TDYa1275e869b62012-07-25 00:45:39 -07002491
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002492 llvm::BasicBlock* block_continue =
2493 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002494
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002495 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
2496
2497 irb_.SetInsertPoint(block_exception);
2498
2499 EmitUpdateDexPC(dex_pc);
2500 irb_.CreateCall2(irb_.GetRuntime(runtime_support::ThrowIndexOutOfBounds), index, array_len);
2501 EmitBranchExceptionLandingPad(dex_pc);
2502
2503 irb_.SetInsertPoint(block_continue);
2504 }
TDYa1275e869b62012-07-25 00:45:39 -07002505}
2506
Ian Rogers8e696052013-03-04 09:00:40 -08002507llvm::FunctionType* GBCExpanderPass::GetFunctionType(llvm::Type* ret_type, uint32_t method_idx,
TDYa1275e869b62012-07-25 00:45:39 -07002508 bool is_static) {
2509 // Get method signature
Ian Rogers8e696052013-03-04 09:00:40 -08002510 art::DexFile::MethodId const& method_id =
2511 oat_compilation_unit_->dex_file_->GetMethodId(method_idx);
TDYa1275e869b62012-07-25 00:45:39 -07002512
2513 uint32_t shorty_size;
Ian Rogers8e696052013-03-04 09:00:40 -08002514 const char* shorty = oat_compilation_unit_->dex_file_->GetMethodShorty(method_id, &shorty_size);
TDYa1275e869b62012-07-25 00:45:39 -07002515 CHECK_GE(shorty_size, 1u);
2516
TDYa1275e869b62012-07-25 00:45:39 -07002517 // Get argument type
2518 std::vector<llvm::Type*> args_type;
2519
2520 args_type.push_back(irb_.getJObjectTy()); // method object pointer
2521
2522 if (!is_static) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002523 args_type.push_back(irb_.getJType('L')); // "this" object pointer
TDYa1275e869b62012-07-25 00:45:39 -07002524 }
2525
2526 for (uint32_t i = 1; i < shorty_size; ++i) {
buzbee26f10ee2012-12-21 11:16:29 -08002527 char shorty_type = art::RemapShorty(shorty[i]);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002528 args_type.push_back(irb_.getJType(shorty_type));
TDYa1275e869b62012-07-25 00:45:39 -07002529 }
2530
2531 return llvm::FunctionType::get(ret_type, args_type, false);
2532}
2533
2534
2535llvm::BasicBlock* GBCExpanderPass::
2536CreateBasicBlockWithDexPC(uint32_t dex_pc, const char* postfix) {
2537 std::string name;
2538
2539#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002540 art::StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
TDYa1275e869b62012-07-25 00:45:39 -07002541#endif
2542
2543 return llvm::BasicBlock::Create(context_, name, func_);
2544}
2545
2546llvm::BasicBlock* GBCExpanderPass::GetBasicBlock(uint32_t dex_pc) {
Ian Rogers8e696052013-03-04 09:00:40 -08002547 DCHECK(dex_pc < oat_compilation_unit_->code_item_->insns_size_in_code_units_);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002548 CHECK(basic_blocks_[dex_pc] != NULL);
TDYa1275e869b62012-07-25 00:45:39 -07002549 return basic_blocks_[dex_pc];
2550}
2551
2552int32_t GBCExpanderPass::GetTryItemOffset(uint32_t dex_pc) {
2553 int32_t min = 0;
Ian Rogers8e696052013-03-04 09:00:40 -08002554 int32_t max = oat_compilation_unit_->code_item_->tries_size_ - 1;
TDYa1275e869b62012-07-25 00:45:39 -07002555
2556 while (min <= max) {
2557 int32_t mid = min + (max - min) / 2;
2558
Ian Rogers8e696052013-03-04 09:00:40 -08002559 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*oat_compilation_unit_->code_item_,
2560 mid);
TDYa1275e869b62012-07-25 00:45:39 -07002561 uint32_t start = ti->start_addr_;
2562 uint32_t end = start + ti->insn_count_;
2563
2564 if (dex_pc < start) {
2565 max = mid - 1;
2566 } else if (dex_pc >= end) {
2567 min = mid + 1;
2568 } else {
2569 return mid; // found
2570 }
2571 }
2572
2573 return -1; // not found
2574}
2575
2576llvm::BasicBlock* GBCExpanderPass::GetLandingPadBasicBlock(uint32_t dex_pc) {
2577 // Find the try item for this address in this method
2578 int32_t ti_offset = GetTryItemOffset(dex_pc);
2579
2580 if (ti_offset == -1) {
2581 return NULL; // No landing pad is available for this address.
2582 }
2583
2584 // Check for the existing landing pad basic block
2585 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2586 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2587
2588 if (block_lpad) {
2589 // We have generated landing pad for this try item already. Return the
2590 // same basic block.
2591 return block_lpad;
2592 }
2593
2594 // Get try item from code item
Ian Rogers8e696052013-03-04 09:00:40 -08002595 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*oat_compilation_unit_->code_item_,
2596 ti_offset);
TDYa1275e869b62012-07-25 00:45:39 -07002597
2598 std::string lpadname;
2599
2600#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002601 art::StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
TDYa1275e869b62012-07-25 00:45:39 -07002602#endif
2603
2604 // Create landing pad basic block
2605 block_lpad = llvm::BasicBlock::Create(context_, lpadname, func_);
2606
2607 // Change IRBuilder insert point
2608 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2609 irb_.SetInsertPoint(block_lpad);
2610
2611 // Find catch block with matching type
2612 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2613
2614 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
2615
2616 llvm::Value* catch_handler_index_value =
2617 irb_.CreateCall2(irb_.GetRuntime(runtime_support::FindCatchBlock),
2618 method_object_addr, ti_offset_value);
2619
2620 // Switch instruction (Go to unwind basic block by default)
2621 llvm::SwitchInst* sw =
2622 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2623
2624 // Cases with matched catch block
Ian Rogers8e696052013-03-04 09:00:40 -08002625 art::CatchHandlerIterator iter(*oat_compilation_unit_->code_item_, ti->start_addr_);
TDYa1275e869b62012-07-25 00:45:39 -07002626
2627 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2628 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2629 }
2630
2631 // Restore the orignal insert point for IRBuilder
2632 irb_.restoreIP(irb_ip_original);
2633
2634 // Cache this landing pad
2635 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2636 basic_block_landing_pads_[ti_offset] = block_lpad;
2637
2638 return block_lpad;
2639}
2640
2641llvm::BasicBlock* GBCExpanderPass::GetUnwindBasicBlock() {
2642 // Check the existing unwinding baisc block block
2643 if (basic_block_unwind_ != NULL) {
2644 return basic_block_unwind_;
2645 }
2646
2647 // Create new basic block for unwinding
2648 basic_block_unwind_ =
2649 llvm::BasicBlock::Create(context_, "exception_unwind", func_);
2650
2651 // Change IRBuilder insert point
2652 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2653 irb_.SetInsertPoint(basic_block_unwind_);
2654
2655 // Pop the shadow frame
2656 Expand_PopShadowFrame();
2657
2658 // Emit the code to return default value (zero) for the given return type.
2659 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
buzbee26f10ee2012-12-21 11:16:29 -08002660 ret_shorty = art::RemapShorty(ret_shorty);
TDYa1275e869b62012-07-25 00:45:39 -07002661 if (ret_shorty == 'V') {
2662 irb_.CreateRetVoid();
2663 } else {
2664 irb_.CreateRet(irb_.getJZero(ret_shorty));
2665 }
2666
2667 // Restore the orignal insert point for IRBuilder
2668 irb_.restoreIP(irb_ip_original);
2669
2670 return basic_block_unwind_;
2671}
2672
2673void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2674 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002675 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002676 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002677 irb_.CreateBr(lpad);
2678 } else {
2679 irb_.CreateBr(GetUnwindBasicBlock());
2680 }
2681}
2682
2683void GBCExpanderPass::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
Jeff Hao9a142652013-01-17 23:10:19 +00002684 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
2685
TDYa1275e869b62012-07-25 00:45:39 -07002686 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2687
2688 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002689 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002690 irb_.GetInsertBlock()));
Jeff Hao9a142652013-01-17 23:10:19 +00002691 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002692 } else {
Jeff Hao9a142652013-01-17 23:10:19 +00002693 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002694 }
2695
2696 irb_.SetInsertPoint(block_cont);
2697}
2698
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002699llvm::Value*
2700GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
2701 llvm::CallInst& call_inst) {
2702 switch (intr_id) {
2703 //==- Thread -----------------------------------------------------------==//
2704 case IntrinsicHelper::GetCurrentThread: {
TDYa127b672d1e2012-06-28 21:21:45 -07002705 return irb_.Runtime().EmitGetCurrentThread();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002706 }
Logan Chien75e4b602012-07-23 14:24:12 -07002707 case IntrinsicHelper::CheckSuspend: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08002708 Expand_TestSuspend(call_inst);
TDYa127890ea892012-08-22 10:49:42 -07002709 return NULL;
2710 }
2711 case IntrinsicHelper::TestSuspend: {
Logan Chiend54a23d2012-07-24 11:19:23 -07002712 Expand_TestSuspend(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002713 return NULL;
2714 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002715 case IntrinsicHelper::MarkGCCard: {
TDYa1279a129452012-07-19 03:10:08 -07002716 Expand_MarkGCCard(call_inst);
2717 return NULL;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002718 }
Logan Chien75e4b602012-07-23 14:24:12 -07002719
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002720 //==- Exception --------------------------------------------------------==//
2721 case IntrinsicHelper::ThrowException: {
2722 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
2723 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002724 case IntrinsicHelper::HLThrowException: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002725 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2726
2727 EmitUpdateDexPC(dex_pc);
2728
2729 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowException),
2730 call_inst.getArgOperand(0));
2731
2732 EmitGuard_ExceptionLandingPad(dex_pc);
2733 return NULL;
2734 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002735 case IntrinsicHelper::GetException: {
TDYa127823433d2012-09-26 16:03:51 -07002736 return irb_.Runtime().EmitGetAndClearException();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002737 }
2738 case IntrinsicHelper::IsExceptionPending: {
2739 return irb_.Runtime().EmitIsExceptionPending();
2740 }
2741 case IntrinsicHelper::FindCatchBlock: {
2742 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
2743 }
2744 case IntrinsicHelper::ThrowDivZeroException: {
2745 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
2746 }
2747 case IntrinsicHelper::ThrowNullPointerException: {
2748 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
2749 }
2750 case IntrinsicHelper::ThrowIndexOutOfBounds: {
2751 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
2752 }
Logan Chien75e4b602012-07-23 14:24:12 -07002753
2754 //==- Const String -----------------------------------------------------==//
2755 case IntrinsicHelper::ConstString: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002756 return Expand_ConstString(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002757 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002758 case IntrinsicHelper::LoadStringFromDexCache: {
2759 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
2760 }
2761 case IntrinsicHelper::ResolveString: {
2762 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
2763 }
Logan Chien75e4b602012-07-23 14:24:12 -07002764
2765 //==- Const Class ------------------------------------------------------==//
2766 case IntrinsicHelper::ConstClass: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002767 return Expand_ConstClass(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002768 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002769 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
2770 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
2771 }
2772 case IntrinsicHelper::LoadTypeFromDexCache: {
2773 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
2774 }
2775 case IntrinsicHelper::InitializeType: {
2776 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
2777 }
Logan Chien75e4b602012-07-23 14:24:12 -07002778
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002779 //==- Lock -------------------------------------------------------------==//
2780 case IntrinsicHelper::LockObject: {
2781 Expand_LockObject(call_inst.getArgOperand(0));
2782 return NULL;
2783 }
2784 case IntrinsicHelper::UnlockObject: {
2785 Expand_UnlockObject(call_inst.getArgOperand(0));
2786 return NULL;
2787 }
Logan Chien75e4b602012-07-23 14:24:12 -07002788
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002789 //==- Cast -------------------------------------------------------------==//
2790 case IntrinsicHelper::CheckCast: {
2791 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
2792 }
Logan Chien75e4b602012-07-23 14:24:12 -07002793 case IntrinsicHelper::HLCheckCast: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002794 Expand_HLCheckCast(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002795 return NULL;
2796 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002797 case IntrinsicHelper::IsAssignable: {
2798 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
2799 }
Logan Chien75e4b602012-07-23 14:24:12 -07002800
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002801 //==- Alloc ------------------------------------------------------------==//
2802 case IntrinsicHelper::AllocObject: {
2803 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
2804 }
2805 case IntrinsicHelper::AllocObjectWithAccessCheck: {
2806 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
2807 }
Logan Chien75e4b602012-07-23 14:24:12 -07002808
2809 //==- Instance ---------------------------------------------------------==//
2810 case IntrinsicHelper::NewInstance: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002811 return Expand_NewInstance(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002812 }
2813 case IntrinsicHelper::InstanceOf: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002814 return Expand_InstanceOf(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002815 }
2816
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002817 //==- Array ------------------------------------------------------------==//
Logan Chien75e4b602012-07-23 14:24:12 -07002818 case IntrinsicHelper::NewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002819 return Expand_NewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002820 }
2821 case IntrinsicHelper::OptArrayLength: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002822 return Expand_OptArrayLength(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002823 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002824 case IntrinsicHelper::ArrayLength: {
2825 return EmitLoadArrayLength(call_inst.getArgOperand(0));
2826 }
2827 case IntrinsicHelper::AllocArray: {
2828 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
2829 }
2830 case IntrinsicHelper::AllocArrayWithAccessCheck: {
2831 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
2832 call_inst);
2833 }
2834 case IntrinsicHelper::CheckAndAllocArray: {
2835 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
2836 }
2837 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
2838 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
2839 call_inst);
2840 }
2841 case IntrinsicHelper::ArrayGet: {
2842 return Expand_ArrayGet(call_inst.getArgOperand(0),
2843 call_inst.getArgOperand(1),
2844 kInt);
2845 }
2846 case IntrinsicHelper::ArrayGetWide: {
2847 return Expand_ArrayGet(call_inst.getArgOperand(0),
2848 call_inst.getArgOperand(1),
2849 kLong);
2850 }
2851 case IntrinsicHelper::ArrayGetObject: {
2852 return Expand_ArrayGet(call_inst.getArgOperand(0),
2853 call_inst.getArgOperand(1),
2854 kObject);
2855 }
2856 case IntrinsicHelper::ArrayGetBoolean: {
2857 return Expand_ArrayGet(call_inst.getArgOperand(0),
2858 call_inst.getArgOperand(1),
2859 kBoolean);
2860 }
2861 case IntrinsicHelper::ArrayGetByte: {
2862 return Expand_ArrayGet(call_inst.getArgOperand(0),
2863 call_inst.getArgOperand(1),
2864 kByte);
2865 }
2866 case IntrinsicHelper::ArrayGetChar: {
2867 return Expand_ArrayGet(call_inst.getArgOperand(0),
2868 call_inst.getArgOperand(1),
2869 kChar);
2870 }
2871 case IntrinsicHelper::ArrayGetShort: {
2872 return Expand_ArrayGet(call_inst.getArgOperand(0),
2873 call_inst.getArgOperand(1),
2874 kShort);
2875 }
2876 case IntrinsicHelper::ArrayPut: {
2877 Expand_ArrayPut(call_inst.getArgOperand(0),
2878 call_inst.getArgOperand(1),
2879 call_inst.getArgOperand(2),
2880 kInt);
2881 return NULL;
2882 }
2883 case IntrinsicHelper::ArrayPutWide: {
2884 Expand_ArrayPut(call_inst.getArgOperand(0),
2885 call_inst.getArgOperand(1),
2886 call_inst.getArgOperand(2),
2887 kLong);
2888 return NULL;
2889 }
2890 case IntrinsicHelper::ArrayPutObject: {
2891 Expand_ArrayPut(call_inst.getArgOperand(0),
2892 call_inst.getArgOperand(1),
2893 call_inst.getArgOperand(2),
2894 kObject);
2895 return NULL;
2896 }
2897 case IntrinsicHelper::ArrayPutBoolean: {
2898 Expand_ArrayPut(call_inst.getArgOperand(0),
2899 call_inst.getArgOperand(1),
2900 call_inst.getArgOperand(2),
2901 kBoolean);
2902 return NULL;
2903 }
2904 case IntrinsicHelper::ArrayPutByte: {
2905 Expand_ArrayPut(call_inst.getArgOperand(0),
2906 call_inst.getArgOperand(1),
2907 call_inst.getArgOperand(2),
2908 kByte);
2909 return NULL;
2910 }
2911 case IntrinsicHelper::ArrayPutChar: {
2912 Expand_ArrayPut(call_inst.getArgOperand(0),
2913 call_inst.getArgOperand(1),
2914 call_inst.getArgOperand(2),
2915 kChar);
2916 return NULL;
2917 }
2918 case IntrinsicHelper::ArrayPutShort: {
2919 Expand_ArrayPut(call_inst.getArgOperand(0),
2920 call_inst.getArgOperand(1),
2921 call_inst.getArgOperand(2),
2922 kShort);
2923 return NULL;
2924 }
2925 case IntrinsicHelper::CheckPutArrayElement: {
2926 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
2927 }
2928 case IntrinsicHelper::FilledNewArray: {
2929 Expand_FilledNewArray(call_inst);
2930 return NULL;
2931 }
2932 case IntrinsicHelper::FillArrayData: {
2933 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
2934 }
Logan Chien75e4b602012-07-23 14:24:12 -07002935 case IntrinsicHelper::HLFillArrayData: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002936 Expand_HLFillArrayData(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002937 return NULL;
2938 }
2939 case IntrinsicHelper::HLFilledNewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002940 return Expand_HLFilledNewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002941 }
2942
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002943 //==- Instance Field ---------------------------------------------------==//
2944 case IntrinsicHelper::InstanceFieldGet:
2945 case IntrinsicHelper::InstanceFieldGetBoolean:
2946 case IntrinsicHelper::InstanceFieldGetByte:
2947 case IntrinsicHelper::InstanceFieldGetChar:
2948 case IntrinsicHelper::InstanceFieldGetShort: {
2949 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
2950 }
2951 case IntrinsicHelper::InstanceFieldGetWide: {
2952 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
2953 }
2954 case IntrinsicHelper::InstanceFieldGetObject: {
2955 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
2956 }
2957 case IntrinsicHelper::InstanceFieldGetFast: {
2958 return Expand_IGetFast(call_inst.getArgOperand(0),
2959 call_inst.getArgOperand(1),
2960 call_inst.getArgOperand(2),
2961 kInt);
2962 }
2963 case IntrinsicHelper::InstanceFieldGetWideFast: {
2964 return Expand_IGetFast(call_inst.getArgOperand(0),
2965 call_inst.getArgOperand(1),
2966 call_inst.getArgOperand(2),
2967 kLong);
2968 }
2969 case IntrinsicHelper::InstanceFieldGetObjectFast: {
2970 return Expand_IGetFast(call_inst.getArgOperand(0),
2971 call_inst.getArgOperand(1),
2972 call_inst.getArgOperand(2),
2973 kObject);
2974 }
2975 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
2976 return Expand_IGetFast(call_inst.getArgOperand(0),
2977 call_inst.getArgOperand(1),
2978 call_inst.getArgOperand(2),
2979 kBoolean);
2980 }
2981 case IntrinsicHelper::InstanceFieldGetByteFast: {
2982 return Expand_IGetFast(call_inst.getArgOperand(0),
2983 call_inst.getArgOperand(1),
2984 call_inst.getArgOperand(2),
2985 kByte);
2986 }
2987 case IntrinsicHelper::InstanceFieldGetCharFast: {
2988 return Expand_IGetFast(call_inst.getArgOperand(0),
2989 call_inst.getArgOperand(1),
2990 call_inst.getArgOperand(2),
2991 kChar);
2992 }
2993 case IntrinsicHelper::InstanceFieldGetShortFast: {
2994 return Expand_IGetFast(call_inst.getArgOperand(0),
2995 call_inst.getArgOperand(1),
2996 call_inst.getArgOperand(2),
2997 kShort);
2998 }
2999 case IntrinsicHelper::InstanceFieldPut:
3000 case IntrinsicHelper::InstanceFieldPutBoolean:
3001 case IntrinsicHelper::InstanceFieldPutByte:
3002 case IntrinsicHelper::InstanceFieldPutChar:
3003 case IntrinsicHelper::InstanceFieldPutShort: {
3004 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
3005 }
3006 case IntrinsicHelper::InstanceFieldPutWide: {
3007 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
3008 }
3009 case IntrinsicHelper::InstanceFieldPutObject: {
3010 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
3011 }
3012 case IntrinsicHelper::InstanceFieldPutFast: {
3013 Expand_IPutFast(call_inst.getArgOperand(0),
3014 call_inst.getArgOperand(1),
3015 call_inst.getArgOperand(2),
3016 call_inst.getArgOperand(3),
3017 kInt);
3018 return NULL;
3019 }
3020 case IntrinsicHelper::InstanceFieldPutWideFast: {
3021 Expand_IPutFast(call_inst.getArgOperand(0),
3022 call_inst.getArgOperand(1),
3023 call_inst.getArgOperand(2),
3024 call_inst.getArgOperand(3),
3025 kLong);
3026 return NULL;
3027 }
3028 case IntrinsicHelper::InstanceFieldPutObjectFast: {
3029 Expand_IPutFast(call_inst.getArgOperand(0),
3030 call_inst.getArgOperand(1),
3031 call_inst.getArgOperand(2),
3032 call_inst.getArgOperand(3),
3033 kObject);
3034 return NULL;
3035 }
3036 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
3037 Expand_IPutFast(call_inst.getArgOperand(0),
3038 call_inst.getArgOperand(1),
3039 call_inst.getArgOperand(2),
3040 call_inst.getArgOperand(3),
3041 kBoolean);
3042 return NULL;
3043 }
3044 case IntrinsicHelper::InstanceFieldPutByteFast: {
3045 Expand_IPutFast(call_inst.getArgOperand(0),
3046 call_inst.getArgOperand(1),
3047 call_inst.getArgOperand(2),
3048 call_inst.getArgOperand(3),
3049 kByte);
3050 return NULL;
3051 }
3052 case IntrinsicHelper::InstanceFieldPutCharFast: {
3053 Expand_IPutFast(call_inst.getArgOperand(0),
3054 call_inst.getArgOperand(1),
3055 call_inst.getArgOperand(2),
3056 call_inst.getArgOperand(3),
3057 kChar);
3058 return NULL;
3059 }
3060 case IntrinsicHelper::InstanceFieldPutShortFast: {
3061 Expand_IPutFast(call_inst.getArgOperand(0),
3062 call_inst.getArgOperand(1),
3063 call_inst.getArgOperand(2),
3064 call_inst.getArgOperand(3),
3065 kShort);
3066 return NULL;
3067 }
Logan Chien75e4b602012-07-23 14:24:12 -07003068
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003069 //==- Static Field -----------------------------------------------------==//
3070 case IntrinsicHelper::StaticFieldGet:
3071 case IntrinsicHelper::StaticFieldGetBoolean:
3072 case IntrinsicHelper::StaticFieldGetByte:
3073 case IntrinsicHelper::StaticFieldGetChar:
3074 case IntrinsicHelper::StaticFieldGetShort: {
3075 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
3076 }
3077 case IntrinsicHelper::StaticFieldGetWide: {
3078 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
3079 }
3080 case IntrinsicHelper::StaticFieldGetObject: {
3081 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
3082 }
3083 case IntrinsicHelper::StaticFieldGetFast: {
3084 return Expand_SGetFast(call_inst.getArgOperand(0),
3085 call_inst.getArgOperand(1),
3086 call_inst.getArgOperand(2),
3087 kInt);
3088 }
3089 case IntrinsicHelper::StaticFieldGetWideFast: {
3090 return Expand_SGetFast(call_inst.getArgOperand(0),
3091 call_inst.getArgOperand(1),
3092 call_inst.getArgOperand(2),
3093 kLong);
3094 }
3095 case IntrinsicHelper::StaticFieldGetObjectFast: {
3096 return Expand_SGetFast(call_inst.getArgOperand(0),
3097 call_inst.getArgOperand(1),
3098 call_inst.getArgOperand(2),
3099 kObject);
3100 }
3101 case IntrinsicHelper::StaticFieldGetBooleanFast: {
3102 return Expand_SGetFast(call_inst.getArgOperand(0),
3103 call_inst.getArgOperand(1),
3104 call_inst.getArgOperand(2),
3105 kBoolean);
3106 }
3107 case IntrinsicHelper::StaticFieldGetByteFast: {
3108 return Expand_SGetFast(call_inst.getArgOperand(0),
3109 call_inst.getArgOperand(1),
3110 call_inst.getArgOperand(2),
3111 kByte);
3112 }
3113 case IntrinsicHelper::StaticFieldGetCharFast: {
3114 return Expand_SGetFast(call_inst.getArgOperand(0),
3115 call_inst.getArgOperand(1),
3116 call_inst.getArgOperand(2),
3117 kChar);
3118 }
3119 case IntrinsicHelper::StaticFieldGetShortFast: {
3120 return Expand_SGetFast(call_inst.getArgOperand(0),
3121 call_inst.getArgOperand(1),
3122 call_inst.getArgOperand(2),
3123 kShort);
3124 }
3125 case IntrinsicHelper::StaticFieldPut:
3126 case IntrinsicHelper::StaticFieldPutBoolean:
3127 case IntrinsicHelper::StaticFieldPutByte:
3128 case IntrinsicHelper::StaticFieldPutChar:
3129 case IntrinsicHelper::StaticFieldPutShort: {
3130 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
3131 }
3132 case IntrinsicHelper::StaticFieldPutWide: {
3133 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
3134 }
3135 case IntrinsicHelper::StaticFieldPutObject: {
3136 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
3137 }
3138 case IntrinsicHelper::StaticFieldPutFast: {
3139 Expand_SPutFast(call_inst.getArgOperand(0),
3140 call_inst.getArgOperand(1),
3141 call_inst.getArgOperand(2),
3142 call_inst.getArgOperand(3),
3143 kInt);
3144 return NULL;
3145 }
3146 case IntrinsicHelper::StaticFieldPutWideFast: {
3147 Expand_SPutFast(call_inst.getArgOperand(0),
3148 call_inst.getArgOperand(1),
3149 call_inst.getArgOperand(2),
3150 call_inst.getArgOperand(3),
3151 kLong);
3152 return NULL;
3153 }
3154 case IntrinsicHelper::StaticFieldPutObjectFast: {
3155 Expand_SPutFast(call_inst.getArgOperand(0),
3156 call_inst.getArgOperand(1),
3157 call_inst.getArgOperand(2),
3158 call_inst.getArgOperand(3),
3159 kObject);
3160 return NULL;
3161 }
3162 case IntrinsicHelper::StaticFieldPutBooleanFast: {
3163 Expand_SPutFast(call_inst.getArgOperand(0),
3164 call_inst.getArgOperand(1),
3165 call_inst.getArgOperand(2),
3166 call_inst.getArgOperand(3),
3167 kBoolean);
3168 return NULL;
3169 }
3170 case IntrinsicHelper::StaticFieldPutByteFast: {
3171 Expand_SPutFast(call_inst.getArgOperand(0),
3172 call_inst.getArgOperand(1),
3173 call_inst.getArgOperand(2),
3174 call_inst.getArgOperand(3),
3175 kByte);
3176 return NULL;
3177 }
3178 case IntrinsicHelper::StaticFieldPutCharFast: {
3179 Expand_SPutFast(call_inst.getArgOperand(0),
3180 call_inst.getArgOperand(1),
3181 call_inst.getArgOperand(2),
3182 call_inst.getArgOperand(3),
3183 kChar);
3184 return NULL;
3185 }
3186 case IntrinsicHelper::StaticFieldPutShortFast: {
3187 Expand_SPutFast(call_inst.getArgOperand(0),
3188 call_inst.getArgOperand(1),
3189 call_inst.getArgOperand(2),
3190 call_inst.getArgOperand(3),
3191 kShort);
3192 return NULL;
3193 }
3194 case IntrinsicHelper::LoadDeclaringClassSSB: {
3195 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
3196 }
3197 case IntrinsicHelper::LoadClassSSBFromDexCache: {
3198 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
3199 }
3200 case IntrinsicHelper::InitializeAndLoadClassSSB: {
3201 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
3202 }
Logan Chien75e4b602012-07-23 14:24:12 -07003203
3204 //==- High-level Array -------------------------------------------------==//
3205 case IntrinsicHelper::HLArrayGet: {
TDYa1275a26d442012-07-26 18:58:38 -07003206 return Expand_HLArrayGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003207 }
3208 case IntrinsicHelper::HLArrayGetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003209 return Expand_HLArrayGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003210 }
3211 case IntrinsicHelper::HLArrayGetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003212 return Expand_HLArrayGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003213 }
3214 case IntrinsicHelper::HLArrayGetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003215 return Expand_HLArrayGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003216 }
3217 case IntrinsicHelper::HLArrayGetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003218 return Expand_HLArrayGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003219 }
3220 case IntrinsicHelper::HLArrayGetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003221 return Expand_HLArrayGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003222 }
3223 case IntrinsicHelper::HLArrayGetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003224 return Expand_HLArrayGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003225 }
3226 case IntrinsicHelper::HLArrayGetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003227 return Expand_HLArrayGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003228 }
3229 case IntrinsicHelper::HLArrayGetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003230 return Expand_HLArrayGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003231 }
3232 case IntrinsicHelper::HLArrayPut: {
TDYa1275a26d442012-07-26 18:58:38 -07003233 Expand_HLArrayPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003234 return NULL;
3235 }
3236 case IntrinsicHelper::HLArrayPutBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003237 Expand_HLArrayPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003238 return NULL;
3239 }
3240 case IntrinsicHelper::HLArrayPutByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003241 Expand_HLArrayPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003242 return NULL;
3243 }
3244 case IntrinsicHelper::HLArrayPutChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003245 Expand_HLArrayPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003246 return NULL;
3247 }
3248 case IntrinsicHelper::HLArrayPutShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003249 Expand_HLArrayPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003250 return NULL;
3251 }
3252 case IntrinsicHelper::HLArrayPutFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003253 Expand_HLArrayPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003254 return NULL;
3255 }
3256 case IntrinsicHelper::HLArrayPutWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003257 Expand_HLArrayPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003258 return NULL;
3259 }
3260 case IntrinsicHelper::HLArrayPutDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003261 Expand_HLArrayPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003262 return NULL;
3263 }
3264 case IntrinsicHelper::HLArrayPutObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003265 Expand_HLArrayPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003266 return NULL;
3267 }
3268
3269 //==- High-level Instance ----------------------------------------------==//
3270 case IntrinsicHelper::HLIGet: {
TDYa1275e869b62012-07-25 00:45:39 -07003271 return Expand_HLIGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003272 }
3273 case IntrinsicHelper::HLIGetBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003274 return Expand_HLIGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003275 }
3276 case IntrinsicHelper::HLIGetByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003277 return Expand_HLIGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003278 }
3279 case IntrinsicHelper::HLIGetChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003280 return Expand_HLIGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003281 }
3282 case IntrinsicHelper::HLIGetShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003283 return Expand_HLIGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003284 }
3285 case IntrinsicHelper::HLIGetFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003286 return Expand_HLIGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003287 }
3288 case IntrinsicHelper::HLIGetWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003289 return Expand_HLIGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003290 }
3291 case IntrinsicHelper::HLIGetDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003292 return Expand_HLIGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003293 }
3294 case IntrinsicHelper::HLIGetObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003295 return Expand_HLIGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003296 }
3297 case IntrinsicHelper::HLIPut: {
TDYa1275e869b62012-07-25 00:45:39 -07003298 Expand_HLIPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003299 return NULL;
3300 }
3301 case IntrinsicHelper::HLIPutBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003302 Expand_HLIPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003303 return NULL;
3304 }
3305 case IntrinsicHelper::HLIPutByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003306 Expand_HLIPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003307 return NULL;
3308 }
3309 case IntrinsicHelper::HLIPutChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003310 Expand_HLIPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003311 return NULL;
3312 }
3313 case IntrinsicHelper::HLIPutShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003314 Expand_HLIPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003315 return NULL;
3316 }
3317 case IntrinsicHelper::HLIPutFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003318 Expand_HLIPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003319 return NULL;
3320 }
3321 case IntrinsicHelper::HLIPutWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003322 Expand_HLIPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003323 return NULL;
3324 }
3325 case IntrinsicHelper::HLIPutDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003326 Expand_HLIPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003327 return NULL;
3328 }
3329 case IntrinsicHelper::HLIPutObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003330 Expand_HLIPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003331 return NULL;
3332 }
3333
3334 //==- High-level Invoke ------------------------------------------------==//
TDYa127f71bf5a2012-07-29 20:09:52 -07003335 case IntrinsicHelper::HLInvokeVoid:
3336 case IntrinsicHelper::HLInvokeObj:
3337 case IntrinsicHelper::HLInvokeInt:
3338 case IntrinsicHelper::HLInvokeFloat:
3339 case IntrinsicHelper::HLInvokeLong:
Logan Chien75e4b602012-07-23 14:24:12 -07003340 case IntrinsicHelper::HLInvokeDouble: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003341 return Expand_HLInvoke(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003342 }
3343
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003344 //==- Invoke -----------------------------------------------------------==//
3345 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
3346 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
3347 }
3348 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
3349 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
3350 }
3351 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
3352 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
3353 }
3354 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
3355 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
3356 }
3357 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
3358 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
3359 }
3360 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
3361 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
3362 }
3363 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
3364 return Expand_GetVirtualCalleeMethodObjAddrFast(
3365 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
3366 }
3367 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
3368 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
3369 }
3370 case IntrinsicHelper::InvokeRetVoid:
3371 case IntrinsicHelper::InvokeRetBoolean:
3372 case IntrinsicHelper::InvokeRetByte:
3373 case IntrinsicHelper::InvokeRetChar:
3374 case IntrinsicHelper::InvokeRetShort:
3375 case IntrinsicHelper::InvokeRetInt:
3376 case IntrinsicHelper::InvokeRetLong:
3377 case IntrinsicHelper::InvokeRetFloat:
3378 case IntrinsicHelper::InvokeRetDouble:
3379 case IntrinsicHelper::InvokeRetObject: {
3380 return Expand_Invoke(call_inst);
3381 }
Logan Chien75e4b602012-07-23 14:24:12 -07003382
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003383 //==- Math -------------------------------------------------------------==//
3384 case IntrinsicHelper::DivInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003385 return Expand_DivRem(call_inst, /* is_div */true, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003386 }
3387 case IntrinsicHelper::RemInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003388 return Expand_DivRem(call_inst, /* is_div */false, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003389 }
3390 case IntrinsicHelper::DivLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003391 return Expand_DivRem(call_inst, /* is_div */true, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003392 }
3393 case IntrinsicHelper::RemLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003394 return Expand_DivRem(call_inst, /* is_div */false, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003395 }
3396 case IntrinsicHelper::D2L: {
3397 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
3398 }
3399 case IntrinsicHelper::D2I: {
3400 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
3401 }
3402 case IntrinsicHelper::F2L: {
3403 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
3404 }
3405 case IntrinsicHelper::F2I: {
3406 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
3407 }
Logan Chien75e4b602012-07-23 14:24:12 -07003408
3409 //==- High-level Static ------------------------------------------------==//
3410 case IntrinsicHelper::HLSget: {
TDYa1275a26d442012-07-26 18:58:38 -07003411 return Expand_HLSget(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003412 }
3413 case IntrinsicHelper::HLSgetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003414 return Expand_HLSget(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003415 }
3416 case IntrinsicHelper::HLSgetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003417 return Expand_HLSget(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003418 }
3419 case IntrinsicHelper::HLSgetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003420 return Expand_HLSget(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003421 }
3422 case IntrinsicHelper::HLSgetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003423 return Expand_HLSget(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003424 }
3425 case IntrinsicHelper::HLSgetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003426 return Expand_HLSget(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003427 }
3428 case IntrinsicHelper::HLSgetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003429 return Expand_HLSget(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003430 }
3431 case IntrinsicHelper::HLSgetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003432 return Expand_HLSget(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003433 }
3434 case IntrinsicHelper::HLSgetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003435 return Expand_HLSget(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003436 }
3437 case IntrinsicHelper::HLSput: {
TDYa1275a26d442012-07-26 18:58:38 -07003438 Expand_HLSput(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003439 return NULL;
3440 }
3441 case IntrinsicHelper::HLSputBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003442 Expand_HLSput(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003443 return NULL;
3444 }
3445 case IntrinsicHelper::HLSputByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003446 Expand_HLSput(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003447 return NULL;
3448 }
3449 case IntrinsicHelper::HLSputChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003450 Expand_HLSput(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003451 return NULL;
3452 }
3453 case IntrinsicHelper::HLSputShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003454 Expand_HLSput(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003455 return NULL;
3456 }
3457 case IntrinsicHelper::HLSputFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003458 Expand_HLSput(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003459 return NULL;
3460 }
3461 case IntrinsicHelper::HLSputWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003462 Expand_HLSput(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003463 return NULL;
3464 }
3465 case IntrinsicHelper::HLSputDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003466 Expand_HLSput(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003467 return NULL;
3468 }
3469 case IntrinsicHelper::HLSputObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003470 Expand_HLSput(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003471 return NULL;
3472 }
3473
3474 //==- High-level Monitor -----------------------------------------------==//
3475 case IntrinsicHelper::MonitorEnter: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003476 Expand_MonitorEnter(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003477 return NULL;
3478 }
3479 case IntrinsicHelper::MonitorExit: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003480 Expand_MonitorExit(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003481 return NULL;
3482 }
3483
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003484 //==- Shadow Frame -----------------------------------------------------==//
3485 case IntrinsicHelper::AllocaShadowFrame: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08003486 Expand_AllocaShadowFrame(call_inst.getArgOperand(0));
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003487 return NULL;
3488 }
TDYa1278e950c12012-11-02 09:58:19 -07003489 case IntrinsicHelper::SetVReg: {
3490 Expand_SetVReg(call_inst.getArgOperand(0),
3491 call_inst.getArgOperand(1));
3492 return NULL;
3493 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003494 case IntrinsicHelper::PopShadowFrame: {
3495 Expand_PopShadowFrame();
3496 return NULL;
3497 }
3498 case IntrinsicHelper::UpdateDexPC: {
3499 Expand_UpdateDexPC(call_inst.getArgOperand(0));
3500 return NULL;
3501 }
TDYa127a1b21852012-07-23 03:20:39 -07003502
Logan Chien75e4b602012-07-23 14:24:12 -07003503 //==- Comparison -------------------------------------------------------==//
3504 case IntrinsicHelper::CmplFloat:
3505 case IntrinsicHelper::CmplDouble: {
3506 return Expand_FPCompare(call_inst.getArgOperand(0),
3507 call_inst.getArgOperand(1),
3508 false);
3509 }
3510 case IntrinsicHelper::CmpgFloat:
3511 case IntrinsicHelper::CmpgDouble: {
3512 return Expand_FPCompare(call_inst.getArgOperand(0),
3513 call_inst.getArgOperand(1),
3514 true);
3515 }
3516 case IntrinsicHelper::CmpLong: {
3517 return Expand_LongCompare(call_inst.getArgOperand(0),
3518 call_inst.getArgOperand(1));
3519 }
TDYa127a1b21852012-07-23 03:20:39 -07003520
Logan Chien75e4b602012-07-23 14:24:12 -07003521 //==- Const ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003522 case IntrinsicHelper::ConstInt:
3523 case IntrinsicHelper::ConstLong: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003524 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003525 }
TDYa127920be7c2012-09-10 17:13:22 -07003526 case IntrinsicHelper::ConstFloat: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003527 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3528 irb_.getJFloatTy());
Logan Chien75e4b602012-07-23 14:24:12 -07003529 }
TDYa127920be7c2012-09-10 17:13:22 -07003530 case IntrinsicHelper::ConstDouble: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003531 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3532 irb_.getJDoubleTy());
3533 }
TDYa127920be7c2012-09-10 17:13:22 -07003534 case IntrinsicHelper::ConstObj: {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003535 CHECK(LV2UInt(call_inst.getArgOperand(0)) == 0);
3536 return irb_.getJNull();
Logan Chien75e4b602012-07-23 14:24:12 -07003537 }
3538
3539 //==- Method Info ------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003540 case IntrinsicHelper::MethodInfo: {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003541 // Nothing to be done, because MethodInfo carries optional hints that are
3542 // not needed by the portable path.
Logan Chien75e4b602012-07-23 14:24:12 -07003543 return NULL;
3544 }
3545
3546 //==- Copy -------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003547 case IntrinsicHelper::CopyInt:
3548 case IntrinsicHelper::CopyFloat:
3549 case IntrinsicHelper::CopyLong:
3550 case IntrinsicHelper::CopyDouble:
3551 case IntrinsicHelper::CopyObj: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003552 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003553 }
3554
3555 //==- Shift ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003556 case IntrinsicHelper::SHLLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003557 return Expand_IntegerShift(call_inst.getArgOperand(0),
3558 call_inst.getArgOperand(1),
3559 kIntegerSHL, kLong);
3560 }
TDYa127920be7c2012-09-10 17:13:22 -07003561 case IntrinsicHelper::SHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003562 return Expand_IntegerShift(call_inst.getArgOperand(0),
3563 call_inst.getArgOperand(1),
3564 kIntegerSHR, kLong);
3565 }
TDYa127920be7c2012-09-10 17:13:22 -07003566 case IntrinsicHelper::USHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003567 return Expand_IntegerShift(call_inst.getArgOperand(0),
3568 call_inst.getArgOperand(1),
3569 kIntegerUSHR, kLong);
3570 }
TDYa127920be7c2012-09-10 17:13:22 -07003571 case IntrinsicHelper::SHLInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003572 return Expand_IntegerShift(call_inst.getArgOperand(0),
3573 call_inst.getArgOperand(1),
3574 kIntegerSHL, kInt);
3575 }
TDYa127920be7c2012-09-10 17:13:22 -07003576 case IntrinsicHelper::SHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003577 return Expand_IntegerShift(call_inst.getArgOperand(0),
3578 call_inst.getArgOperand(1),
3579 kIntegerSHR, kInt);
3580 }
TDYa127920be7c2012-09-10 17:13:22 -07003581 case IntrinsicHelper::USHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003582 return Expand_IntegerShift(call_inst.getArgOperand(0),
3583 call_inst.getArgOperand(1),
3584 kIntegerUSHR, kInt);
3585 }
3586
3587 //==- Conversion -------------------------------------------------------==//
TDYa127a1b21852012-07-23 03:20:39 -07003588 case IntrinsicHelper::IntToChar: {
3589 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
3590 irb_.getJIntTy());
3591 }
3592 case IntrinsicHelper::IntToShort: {
3593 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
3594 irb_.getJIntTy());
3595 }
3596 case IntrinsicHelper::IntToByte: {
3597 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
3598 irb_.getJIntTy());
3599 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003600
TDYa12787caa7e2012-08-25 23:23:27 -07003601 //==- Exception --------------------------------------------------------==//
3602 case IntrinsicHelper::CatchTargets: {
TDYa12755e5e6c2012-09-11 15:14:42 -07003603 UpdatePhiInstruction(current_bb_, irb_.GetInsertBlock());
TDYa12787caa7e2012-08-25 23:23:27 -07003604 llvm::SwitchInst* si = llvm::dyn_cast<llvm::SwitchInst>(call_inst.getNextNode());
3605 CHECK(si != NULL);
3606 irb_.CreateBr(si->getDefaultDest());
3607 si->eraseFromParent();
3608 return call_inst.getArgOperand(0);
3609 }
3610
Sebastien Hertz0d43d542013-02-27 19:02:16 +01003611 //==- Constructor barrier-----------------------------------------------==//
3612 case IntrinsicHelper::ConstructorBarrier: {
3613 irb_.CreateMemoryBarrier(art::kStoreStore);
3614 return NULL;
3615 }
3616
Logan Chien75e4b602012-07-23 14:24:12 -07003617 //==- Unknown Cases ----------------------------------------------------==//
3618 case IntrinsicHelper::MaxIntrinsicId:
3619 case IntrinsicHelper::UnknownId:
3620 //default:
3621 // NOTE: "default" is intentionally commented so that C/C++ compiler will
3622 // give some warning on unmatched cases.
3623 // NOTE: We should not implement these cases.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003624 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003625 }
Logan Chien75e4b602012-07-23 14:24:12 -07003626 UNIMPLEMENTED(FATAL) << "Unexpected GBC intrinsic: " << static_cast<int>(intr_id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003627 return NULL;
3628}
3629
3630} // anonymous namespace
3631
3632namespace art {
3633namespace compiler_llvm {
3634
3635llvm::FunctionPass*
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003636CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Ian Rogers1212a022013-03-04 10:48:41 -08003637 CompilerDriver* driver, OatCompilationUnit* oat_compilation_unit) {
3638 return new GBCExpanderPass(intrinsic_helper, irb, driver, oat_compilation_unit);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003639}
3640
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003641} // namespace compiler_llvm
3642} // namespace art