blob: fd3fc13da82f3d7f4f187fe9ed01659a1bf2e345 [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 Rogers89756f22013-03-04 16:40:02 -080018#include "compiler/driver/dex_compilation_unit.h"
Ian Rogers76ae4fe2013-02-27 16:03:41 -080019#include "intrinsic_helper.h"
Ian Rogers8e696052013-03-04 09:00:40 -080020#include "ir_builder.h"
Ian Rogers98573f92013-01-30 17:26:32 -080021#include "mirror/abstract_method.h"
22#include "mirror/array.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
Ian Rogers4c1c2832013-03-04 18:30:13 -080043using namespace art::llvm;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070044
Ian Rogers4c1c2832013-03-04 18:30:13 -080045using art::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
Ian Rogers89756f22013-03-04 16:40:02 -080068 const art::DexCompilationUnit* const dex_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 Rogers89756f22013-03-04 16:40:02 -0800329 art::CompilerDriver* compiler, art::DexCompilationUnit* dex_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),
Ian Rogers89756f22013-03-04 16:40:02 -0800334 dex_compilation_unit_(dex_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 Rogers89756f22013-03-04 16:40:02 -0800363 basic_blocks_.resize(dex_compilation_unit_->GetCodeItem()->insns_size_in_code_units_);
364 basic_block_landing_pads_.resize(dex_compilation_unit_->GetCodeItem()->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(
Ian Rogers89756f22013-03-04 16:40:02 -08001396 field_idx, dex_compilation_unit_, field_offset, is_volatile, false);
TDYa1275e869b62012-07-25 00:45:39 -07001397
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
TDYa1275e869b62012-07-25 00:45:39 -07001434 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001435 field_value = SignOrZeroExtendCat1Types(field_value, field_jty);
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001436
1437 if (is_volatile) {
1438 irb_.CreateMemoryBarrier(art::kLoadLoad);
1439 }
TDYa1275e869b62012-07-25 00:45:39 -07001440 }
1441
TDYa1275e869b62012-07-25 00:45:39 -07001442 return field_value;
1443}
1444
1445void GBCExpanderPass::Expand_HLIPut(llvm::CallInst& call_inst,
1446 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001447 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001448 llvm::Value* new_value = call_inst.getArgOperand(1);
1449 llvm::Value* object_addr = call_inst.getArgOperand(2);
TDYa1275e869b62012-07-25 00:45:39 -07001450 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(3));
TDYa127920be7c2012-09-10 17:13:22 -07001451 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001452
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001453 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -07001454
1455 int field_offset;
1456 bool is_volatile;
Ian Rogers1212a022013-03-04 10:48:41 -08001457 bool is_fast_path = driver_->ComputeInstanceFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001458 field_idx, dex_compilation_unit_, field_offset, is_volatile, true);
TDYa1275e869b62012-07-25 00:45:39 -07001459
1460 if (!is_fast_path) {
1461 llvm::Function* runtime_func;
1462
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001463 if (field_jty == kFloat) {
1464 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1465 } else if (field_jty == kDouble) {
1466 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1467 }
1468
TDYa1275e869b62012-07-25 00:45:39 -07001469 if (field_jty == kObject) {
1470 runtime_func = irb_.GetRuntime(runtime_support::SetObjectInstance);
1471 } else if (field_jty == kLong || field_jty == kDouble) {
1472 runtime_func = irb_.GetRuntime(runtime_support::Set64Instance);
1473 } else {
1474 runtime_func = irb_.GetRuntime(runtime_support::Set32Instance);
1475 }
1476
1477 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1478
1479 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1480
1481 EmitUpdateDexPC(dex_pc);
1482
1483 irb_.CreateCall4(runtime_func, field_idx_value,
1484 method_object_addr, object_addr, new_value);
1485
1486 EmitGuard_ExceptionLandingPad(dex_pc);
1487
1488 } else {
1489 DCHECK_GE(field_offset, 0);
1490
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001491 if (is_volatile) {
1492 irb_.CreateMemoryBarrier(art::kStoreStore);
1493 }
1494
TDYa1275e869b62012-07-25 00:45:39 -07001495 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001496 irb_.getJType(field_jty)->getPointerTo();
TDYa1275e869b62012-07-25 00:45:39 -07001497
1498 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
1499
1500 llvm::Value* field_addr =
1501 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1502
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001503 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275e869b62012-07-25 00:45:39 -07001504 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1505
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001506 if (is_volatile) {
1507 irb_.CreateMemoryBarrier(art::kLoadLoad);
1508 }
1509
TDYa1275e869b62012-07-25 00:45:39 -07001510 if (field_jty == kObject) { // If put an object, mark the GC card table.
1511 EmitMarkGCCard(new_value, object_addr);
1512 }
1513 }
1514
1515 return;
1516}
1517
TDYa127f71bf5a2012-07-29 20:09:52 -07001518llvm::Value* GBCExpanderPass::EmitLoadConstantClass(uint32_t dex_pc,
1519 uint32_t type_idx) {
Ian Rogers89756f22013-03-04 16:40:02 -08001520 if (!driver_->CanAccessTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
1521 *dex_compilation_unit_->GetDexFile(), type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001522 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1523
1524 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1525
1526 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1527
1528 llvm::Function* runtime_func =
1529 irb_.GetRuntime(runtime_support::InitializeTypeAndVerifyAccess);
1530
1531 EmitUpdateDexPC(dex_pc);
1532
1533 llvm::Value* type_object_addr =
1534 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1535
1536 EmitGuard_ExceptionLandingPad(dex_pc);
1537
1538 return type_object_addr;
1539
1540 } else {
1541 // Try to load the class (type) object from the test cache.
1542 llvm::Value* type_field_addr =
1543 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1544
TDYa127ce4cc0d2012-11-18 16:59:53 -08001545 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07001546
Ian Rogers89756f22013-03-04 16:40:02 -08001547 if (driver_->CanAssumeTypeIsPresentInDexCache(*dex_compilation_unit_->GetDexFile(), type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001548 return type_object_addr;
1549 }
1550
1551 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1552
1553 // Test whether class (type) object is in the dex cache or not
1554 llvm::Value* equal_null =
1555 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1556
1557 llvm::BasicBlock* block_cont =
1558 CreateBasicBlockWithDexPC(dex_pc, "cont");
1559
1560 llvm::BasicBlock* block_load_class =
1561 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1562
1563 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
1564
1565 // Failback routine to load the class object
1566 irb_.SetInsertPoint(block_load_class);
1567
1568 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeType);
1569
1570 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1571
1572 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1573
1574 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1575
1576 EmitUpdateDexPC(dex_pc);
1577
1578 llvm::Value* loaded_type_object_addr =
1579 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1580
1581 EmitGuard_ExceptionLandingPad(dex_pc);
1582
1583 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1584
1585 irb_.CreateBr(block_cont);
1586
1587 // Now the class object must be loaded
1588 irb_.SetInsertPoint(block_cont);
1589
1590 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1591
1592 phi->addIncoming(type_object_addr, block_original);
1593 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1594
1595 return phi;
1596 }
1597}
1598
TDYa1275a26d442012-07-26 18:58:38 -07001599llvm::Value* GBCExpanderPass::EmitLoadStaticStorage(uint32_t dex_pc,
1600 uint32_t type_idx) {
1601 llvm::BasicBlock* block_load_static =
1602 CreateBasicBlockWithDexPC(dex_pc, "load_static");
1603
1604 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1605
1606 // Load static storage from dex cache
1607 llvm::Value* storage_field_addr =
1608 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1609
TDYa127ce4cc0d2012-11-18 16:59:53 -08001610 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
TDYa1275a26d442012-07-26 18:58:38 -07001611
1612 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1613
1614 // Test: Is the static storage of this class initialized?
1615 llvm::Value* equal_null =
1616 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
1617
1618 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
1619
1620 // Failback routine to load the class object
1621 irb_.SetInsertPoint(block_load_static);
1622
1623 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeStaticStorage);
1624
1625 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1626
1627 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1628
1629 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1630
1631 EmitUpdateDexPC(dex_pc);
1632
1633 llvm::Value* loaded_storage_object_addr =
1634 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1635
1636 EmitGuard_ExceptionLandingPad(dex_pc);
1637
1638 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
1639
1640 irb_.CreateBr(block_cont);
1641
1642 // Now the class object must be loaded
1643 irb_.SetInsertPoint(block_cont);
1644
1645 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1646
1647 phi->addIncoming(storage_object_addr, block_original);
1648 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
1649
1650 return phi;
1651}
1652
1653llvm::Value* GBCExpanderPass::Expand_HLSget(llvm::CallInst& call_inst,
1654 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001655 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1656 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1657
1658 int field_offset;
1659 int ssb_index;
1660 bool is_referrers_class;
1661 bool is_volatile;
1662
Ian Rogers1212a022013-03-04 10:48:41 -08001663 bool is_fast_path = driver_->ComputeStaticFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001664 field_idx, dex_compilation_unit_, field_offset, ssb_index,
TDYa1275a26d442012-07-26 18:58:38 -07001665 is_referrers_class, is_volatile, false);
1666
1667 llvm::Value* static_field_value;
1668
1669 if (!is_fast_path) {
1670 llvm::Function* runtime_func;
1671
1672 if (field_jty == kObject) {
1673 runtime_func = irb_.GetRuntime(runtime_support::GetObjectStatic);
1674 } else if (field_jty == kLong || field_jty == kDouble) {
1675 runtime_func = irb_.GetRuntime(runtime_support::Get64Static);
1676 } else {
1677 runtime_func = irb_.GetRuntime(runtime_support::Get32Static);
1678 }
1679
1680 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1681
1682 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1683
1684 EmitUpdateDexPC(dex_pc);
1685
1686 static_field_value =
1687 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
1688
1689 EmitGuard_ExceptionLandingPad(dex_pc);
1690
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001691 if (field_jty == kFloat || field_jty == kDouble) {
1692 static_field_value = irb_.CreateBitCast(static_field_value, irb_.getJType(field_jty));
1693 }
TDYa1275a26d442012-07-26 18:58:38 -07001694 } else {
1695 DCHECK_GE(field_offset, 0);
1696
1697 llvm::Value* static_storage_addr = NULL;
1698
1699 if (is_referrers_class) {
1700 // Fast path, static storage base is this method's class
1701 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1702
1703 static_storage_addr =
1704 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001705 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001706 irb_.getJObjectTy(),
1707 kTBAAConstJObject);
1708 } else {
1709 // Medium path, static storage base in a different class which
1710 // requires checks that the other class is initialized
1711 DCHECK_GE(ssb_index, 0);
1712 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1713 }
1714
1715 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1716
1717 llvm::Value* static_field_addr =
1718 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001719 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001720
TDYa1275a26d442012-07-26 18:58:38 -07001721 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001722 static_field_value = SignOrZeroExtendCat1Types(static_field_value, field_jty);
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001723
1724 if (is_volatile) {
1725 irb_.CreateMemoryBarrier(art::kLoadLoad);
1726 }
TDYa1275a26d442012-07-26 18:58:38 -07001727 }
1728
TDYa1275a26d442012-07-26 18:58:38 -07001729 return static_field_value;
1730}
1731
1732void GBCExpanderPass::Expand_HLSput(llvm::CallInst& call_inst,
1733 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001734 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1735 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1736 llvm::Value* new_value = call_inst.getArgOperand(1);
1737
1738 if (field_jty == kFloat || field_jty == kDouble) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001739 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty));
TDYa1275a26d442012-07-26 18:58:38 -07001740 }
1741
1742 int field_offset;
1743 int ssb_index;
1744 bool is_referrers_class;
1745 bool is_volatile;
1746
Ian Rogers1212a022013-03-04 10:48:41 -08001747 bool is_fast_path = driver_->ComputeStaticFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001748 field_idx, dex_compilation_unit_, field_offset, ssb_index,
TDYa1275a26d442012-07-26 18:58:38 -07001749 is_referrers_class, is_volatile, true);
1750
1751 if (!is_fast_path) {
1752 llvm::Function* runtime_func;
1753
1754 if (field_jty == kObject) {
1755 runtime_func = irb_.GetRuntime(runtime_support::SetObjectStatic);
1756 } else if (field_jty == kLong || field_jty == kDouble) {
1757 runtime_func = irb_.GetRuntime(runtime_support::Set64Static);
1758 } else {
1759 runtime_func = irb_.GetRuntime(runtime_support::Set32Static);
1760 }
1761
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001762 if (field_jty == kFloat) {
1763 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1764 } else if (field_jty == kDouble) {
1765 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1766 }
1767
TDYa1275a26d442012-07-26 18:58:38 -07001768 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1769
1770 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1771
1772 EmitUpdateDexPC(dex_pc);
1773
1774 irb_.CreateCall3(runtime_func, field_idx_value,
1775 method_object_addr, new_value);
1776
1777 EmitGuard_ExceptionLandingPad(dex_pc);
1778
1779 } else {
1780 DCHECK_GE(field_offset, 0);
1781
1782 llvm::Value* static_storage_addr = NULL;
1783
1784 if (is_referrers_class) {
1785 // Fast path, static storage base is this method's class
1786 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1787
1788 static_storage_addr =
1789 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001790 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001791 irb_.getJObjectTy(),
1792 kTBAAConstJObject);
1793 } else {
1794 // Medium path, static storage base in a different class which
1795 // requires checks that the other class is initialized
1796 DCHECK_GE(ssb_index, 0);
1797 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1798 }
1799
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001800 if (is_volatile) {
1801 irb_.CreateMemoryBarrier(art::kStoreStore);
1802 }
1803
TDYa1275a26d442012-07-26 18:58:38 -07001804 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1805
1806 llvm::Value* static_field_addr =
1807 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001808 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001809
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001810 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001811 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1812
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001813 if (is_volatile) {
1814 irb_.CreateMemoryBarrier(art::kStoreLoad);
1815 }
1816
TDYa1275a26d442012-07-26 18:58:38 -07001817 if (field_jty == kObject) { // If put an object, mark the GC card table.
1818 EmitMarkGCCard(new_value, static_storage_addr);
1819 }
1820 }
1821
1822 return;
1823}
1824
TDYa127f71bf5a2012-07-29 20:09:52 -07001825llvm::Value* GBCExpanderPass::Expand_ConstString(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001826 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1827 uint32_t string_idx = LV2UInt(call_inst.getArgOperand(0));
1828
1829 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1830
TDYa127ce4cc0d2012-11-18 16:59:53 -08001831 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07001832
Ian Rogers89756f22013-03-04 16:40:02 -08001833 if (!driver_->CanAssumeStringIsPresentInDexCache(*dex_compilation_unit_->GetDexFile(),
1834 string_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001835 llvm::BasicBlock* block_str_exist =
1836 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1837
1838 llvm::BasicBlock* block_str_resolve =
1839 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1840
1841 llvm::BasicBlock* block_cont =
1842 CreateBasicBlockWithDexPC(dex_pc, "str_cont");
1843
1844 // Test: Is the string resolved and in the dex cache?
1845 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1846
1847 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
1848
1849 // String is resolved, go to next basic block.
1850 irb_.SetInsertPoint(block_str_exist);
1851 irb_.CreateBr(block_cont);
1852
1853 // String is not resolved yet, resolve it now.
1854 irb_.SetInsertPoint(block_str_resolve);
1855
1856 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::ResolveString);
1857
1858 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1859
1860 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1861
1862 EmitUpdateDexPC(dex_pc);
1863
1864 llvm::Value* result = irb_.CreateCall2(runtime_func, method_object_addr,
1865 string_idx_value);
1866
1867 EmitGuard_ExceptionLandingPad(dex_pc);
1868
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001869 irb_.CreateBr(block_cont);
1870
1871
TDYa127f71bf5a2012-07-29 20:09:52 -07001872 llvm::BasicBlock* block_pre_cont = irb_.GetInsertBlock();
1873
1874 irb_.SetInsertPoint(block_cont);
1875
1876 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1877
1878 phi->addIncoming(string_addr, block_str_exist);
1879 phi->addIncoming(result, block_pre_cont);
1880
1881 string_addr = phi;
1882 }
1883
1884 return string_addr;
1885}
1886
1887llvm::Value* GBCExpanderPass::Expand_ConstClass(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001888 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1889 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1890
1891 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
1892
1893 return type_object_addr;
1894}
1895
1896void GBCExpanderPass::Expand_MonitorEnter(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001897 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1898 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07001899 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07001900
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001901 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07001902
TDYa127ce4cc0d2012-11-18 16:59:53 -08001903 EmitUpdateDexPC(dex_pc);
1904
TDYa127f71bf5a2012-07-29 20:09:52 -07001905 irb_.Runtime().EmitLockObject(object_addr);
1906
1907 return;
1908}
1909
1910void GBCExpanderPass::Expand_MonitorExit(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001911 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1912 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07001913 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07001914
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001915 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07001916
1917 EmitUpdateDexPC(dex_pc);
1918
1919 irb_.Runtime().EmitUnlockObject(object_addr);
1920
1921 EmitGuard_ExceptionLandingPad(dex_pc);
1922
1923 return;
1924}
1925
1926void GBCExpanderPass::Expand_HLCheckCast(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001927 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1928 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1929 llvm::Value* object_addr = call_inst.getArgOperand(1);
1930
1931 llvm::BasicBlock* block_test_class =
1932 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1933
1934 llvm::BasicBlock* block_test_sub_class =
1935 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1936
1937 llvm::BasicBlock* block_cont =
1938 CreateBasicBlockWithDexPC(dex_pc, "checkcast_cont");
1939
1940 // Test: Is the reference equal to null? Act as no-op when it is null.
1941 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1942
Ian Rogers8e696052013-03-04 09:00:40 -08001943 irb_.CreateCondBr(equal_null, block_cont, block_test_class, kUnlikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07001944
1945 // Test: Is the object instantiated from the given class?
1946 irb_.SetInsertPoint(block_test_class);
1947 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08001948 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07001949
1950 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1951
1952 llvm::Value* object_type_field_addr =
1953 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1954
1955 llvm::Value* object_type_object_addr =
1956 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
1957
1958 llvm::Value* equal_class =
1959 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1960
Ian Rogers8e696052013-03-04 09:00:40 -08001961 irb_.CreateCondBr(equal_class, block_cont, block_test_sub_class, kLikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07001962
1963 // Test: Is the object instantiated from the subclass of the given class?
1964 irb_.SetInsertPoint(block_test_sub_class);
1965
1966 EmitUpdateDexPC(dex_pc);
1967
1968 irb_.CreateCall2(irb_.GetRuntime(runtime_support::CheckCast),
1969 type_object_addr, object_type_object_addr);
1970
1971 EmitGuard_ExceptionLandingPad(dex_pc);
1972
1973 irb_.CreateBr(block_cont);
1974
1975 irb_.SetInsertPoint(block_cont);
1976
1977 return;
1978}
1979
1980llvm::Value* GBCExpanderPass::Expand_InstanceOf(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001981 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1982 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
1983 llvm::Value* object_addr = call_inst.getArgOperand(1);
1984
1985 llvm::BasicBlock* block_nullp =
1986 CreateBasicBlockWithDexPC(dex_pc, "nullp");
1987
1988 llvm::BasicBlock* block_test_class =
1989 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1990
1991 llvm::BasicBlock* block_class_equals =
1992 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1993
1994 llvm::BasicBlock* block_test_sub_class =
1995 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1996
1997 llvm::BasicBlock* block_cont =
1998 CreateBasicBlockWithDexPC(dex_pc, "instance_of_cont");
1999
2000 // Overview of the following code :
2001 // We check for null, if so, then false, otherwise check for class == . If so
2002 // then true, otherwise do callout slowpath.
2003 //
2004 // Test: Is the reference equal to null? Set 0 when it is null.
2005 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2006
Ian Rogers8e696052013-03-04 09:00:40 -08002007 irb_.CreateCondBr(equal_null, block_nullp, block_test_class, kUnlikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002008
2009 irb_.SetInsertPoint(block_nullp);
2010 irb_.CreateBr(block_cont);
2011
2012 // Test: Is the object instantiated from the given class?
2013 irb_.SetInsertPoint(block_test_class);
2014 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08002015 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002016
2017 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2018
2019 llvm::Value* object_type_field_addr =
2020 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2021
2022 llvm::Value* object_type_object_addr =
2023 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2024
2025 llvm::Value* equal_class =
2026 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2027
Ian Rogers8e696052013-03-04 09:00:40 -08002028 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class, kLikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002029
2030 irb_.SetInsertPoint(block_class_equals);
2031 irb_.CreateBr(block_cont);
2032
2033 // Test: Is the object instantiated from the subclass of the given class?
2034 irb_.SetInsertPoint(block_test_sub_class);
2035 llvm::Value* result =
2036 irb_.CreateCall2(irb_.GetRuntime(runtime_support::IsAssignable),
2037 type_object_addr, object_type_object_addr);
2038 irb_.CreateBr(block_cont);
2039
2040 irb_.SetInsertPoint(block_cont);
2041
2042 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJIntTy(), 3);
2043
2044 phi->addIncoming(irb_.getJInt(0), block_nullp);
2045 phi->addIncoming(irb_.getJInt(1), block_class_equals);
2046 phi->addIncoming(result, block_test_sub_class);
2047
2048 return phi;
2049}
2050
2051llvm::Value* GBCExpanderPass::Expand_NewInstance(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002052 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2053 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2054
2055 llvm::Function* runtime_func;
Ian Rogers89756f22013-03-04 16:40:02 -08002056 if (driver_->CanAccessInstantiableTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
2057 *dex_compilation_unit_->GetDexFile(),
2058 type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002059 runtime_func = irb_.GetRuntime(runtime_support::AllocObject);
2060 } else {
2061 runtime_func = irb_.GetRuntime(runtime_support::AllocObjectWithAccessCheck);
2062 }
2063
2064 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2065
2066 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2067
2068 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2069
2070 EmitUpdateDexPC(dex_pc);
2071
2072 llvm::Value* object_addr =
2073 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
2074
2075 EmitGuard_ExceptionLandingPad(dex_pc);
2076
2077 return object_addr;
2078}
2079
2080llvm::Value* GBCExpanderPass::Expand_HLInvoke(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002081 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
TDYa127920be7c2012-09-10 17:13:22 -07002082 art::InvokeType invoke_type = static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
2083 bool is_static = (invoke_type == art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002084 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
TDYa127920be7c2012-09-10 17:13:22 -07002085 int opt_flags = LV2UInt(call_inst.getArgOperand(2));
TDYa127f71bf5a2012-07-29 20:09:52 -07002086
2087 // Compute invoke related information for compiler decision
2088 int vtable_idx = -1;
2089 uintptr_t direct_code = 0;
2090 uintptr_t direct_method = 0;
Ian Rogers1212a022013-03-04 10:48:41 -08002091 bool is_fast_path = driver_->
Ian Rogers89756f22013-03-04 16:40:02 -08002092 ComputeInvokeInfo(callee_method_idx, dex_compilation_unit_,
TDYa127f71bf5a2012-07-29 20:09:52 -07002093 invoke_type, vtable_idx, direct_code, direct_method);
2094
2095 // Load *this* actual parameter
2096 llvm::Value* this_addr = NULL;
2097
2098 if (!is_static) {
2099 // Test: Is *this* parameter equal to null?
2100 this_addr = call_inst.getArgOperand(3);
2101 }
2102
2103 // Load the method object
2104 llvm::Value* callee_method_object_addr = NULL;
2105
2106 if (!is_fast_path) {
2107 callee_method_object_addr =
2108 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2109 this_addr, dex_pc, is_fast_path);
2110
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002111 if (!is_static) {
2112 EmitGuard_NullPointerException(dex_pc, this_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002113 }
2114 } else {
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002115 if (!is_static) {
2116 EmitGuard_NullPointerException(dex_pc, this_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002117 }
2118
2119 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002120 case art::kStatic:
2121 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002122 if (direct_method != 0u &&
2123 direct_method != static_cast<uintptr_t>(-1)) {
2124 callee_method_object_addr =
2125 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2126 irb_.getJObjectTy());
2127 } else {
2128 callee_method_object_addr =
2129 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2130 }
2131 break;
2132
TDYa127920be7c2012-09-10 17:13:22 -07002133 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002134 DCHECK(vtable_idx != -1);
2135 callee_method_object_addr =
2136 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2137 break;
2138
TDYa127920be7c2012-09-10 17:13:22 -07002139 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002140 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2141 "the fast path.";
2142 break;
2143
TDYa127920be7c2012-09-10 17:13:22 -07002144 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002145 callee_method_object_addr =
2146 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2147 invoke_type, this_addr,
2148 dex_pc, is_fast_path);
2149 break;
2150 }
2151 }
2152
2153 // Load the actual parameter
2154 std::vector<llvm::Value*> args;
2155
2156 args.push_back(callee_method_object_addr); // method object for callee
2157
2158 for (uint32_t i = 3; i < call_inst.getNumArgOperands(); ++i) {
2159 args.push_back(call_inst.getArgOperand(i));
2160 }
2161
Ian Rogers8e696052013-03-04 09:00:40 -08002162 // Generate the load of the Method*. We base the return type on that of the call as method's
2163 // returning a value are void calls if the return value is unused.
TDYa127f71bf5a2012-07-29 20:09:52 -07002164 llvm::Value* code_addr;
2165 if (direct_code != 0u &&
2166 direct_code != static_cast<uintptr_t>(-1)) {
2167 code_addr =
2168 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_code),
Ian Rogers8e696052013-03-04 09:00:40 -08002169 GetFunctionType(call_inst.getType(), callee_method_idx, is_static)->getPointerTo());
TDYa127f71bf5a2012-07-29 20:09:52 -07002170 } else {
2171 code_addr =
2172 irb_.LoadFromObjectOffset(callee_method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08002173 art::mirror::AbstractMethod::GetCodeOffset().Int32Value(),
Ian Rogers8e696052013-03-04 09:00:40 -08002174 GetFunctionType(call_inst.getType(), callee_method_idx, is_static)->getPointerTo(),
TDYa127ce4cc0d2012-11-18 16:59:53 -08002175 kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07002176 }
2177
2178 // Invoke callee
2179 EmitUpdateDexPC(dex_pc);
2180 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2181 EmitGuard_ExceptionLandingPad(dex_pc);
2182
2183 return retval;
2184}
2185
2186llvm::Value* GBCExpanderPass::Expand_OptArrayLength(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002187 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2188 // Get the array object address
2189 llvm::Value* array_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002190 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002191
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002192 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002193
2194 // Get the array length and store it to the register
2195 return EmitLoadArrayLength(array_addr);
2196}
2197
2198llvm::Value* GBCExpanderPass::Expand_NewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002199 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2200 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2201 llvm::Value* length = call_inst.getArgOperand(1);
2202
2203 return EmitAllocNewArray(dex_pc, length, type_idx, false);
2204}
2205
2206llvm::Value* GBCExpanderPass::Expand_HLFilledNewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002207 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2208 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(1));
2209 uint32_t length = call_inst.getNumArgOperands() - 3;
2210
2211 llvm::Value* object_addr =
2212 EmitAllocNewArray(dex_pc, irb_.getInt32(length), type_idx, true);
2213
2214 if (length > 0) {
2215 // Check for the element type
2216 uint32_t type_desc_len = 0;
2217 const char* type_desc =
Ian Rogers89756f22013-03-04 16:40:02 -08002218 dex_compilation_unit_->GetDexFile()->StringByTypeIdx(type_idx, &type_desc_len);
TDYa127f71bf5a2012-07-29 20:09:52 -07002219
2220 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
2221 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
2222 bool is_elem_int_ty = (type_desc[1] == 'I');
2223
2224 uint32_t alignment;
2225 llvm::Constant* elem_size;
2226 llvm::PointerType* field_type;
2227
2228 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
2229 // as the element, thus we are only checking 2 cases: primitive int and
2230 // non-primitive type.
2231 if (is_elem_int_ty) {
2232 alignment = sizeof(int32_t);
2233 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
2234 field_type = irb_.getJIntTy()->getPointerTo();
2235 } else {
2236 alignment = irb_.getSizeOfPtrEquivInt();
2237 elem_size = irb_.getSizeOfPtrEquivIntValue();
2238 field_type = irb_.getJObjectTy()->getPointerTo();
2239 }
2240
2241 llvm::Value* data_field_offset =
Ian Rogers98573f92013-01-30 17:26:32 -08002242 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
TDYa127f71bf5a2012-07-29 20:09:52 -07002243
2244 llvm::Value* data_field_addr =
2245 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
2246
2247 // TODO: Tune this code. Currently we are generating one instruction for
2248 // one element which may be very space consuming. Maybe changing to use
2249 // memcpy may help; however, since we can't guarantee that the alloca of
2250 // dalvik register are continuous, we can't perform such optimization yet.
2251 for (uint32_t i = 0; i < length; ++i) {
2252 llvm::Value* reg_value = call_inst.getArgOperand(i+3);
2253
2254 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
2255
2256 data_field_addr =
2257 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
2258 }
2259 }
2260
2261 return object_addr;
2262}
2263
2264void GBCExpanderPass::Expand_HLFillArrayData(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002265 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2266 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
2267 LV2SInt(call_inst.getArgOperand(0));
2268 llvm::Value* array_addr = call_inst.getArgOperand(1);
2269
TDYa127920be7c2012-09-10 17:13:22 -07002270 const art::Instruction::ArrayDataPayload* payload =
2271 reinterpret_cast<const art::Instruction::ArrayDataPayload*>(
Ian Rogers89756f22013-03-04 16:40:02 -08002272 dex_compilation_unit_->GetCodeItem()->insns_ + payload_offset);
TDYa127f71bf5a2012-07-29 20:09:52 -07002273
2274 if (payload->element_count == 0) {
2275 // When the number of the elements in the payload is zero, we don't have
2276 // to copy any numbers. However, we should check whether the array object
2277 // address is equal to null or not.
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002278 EmitGuard_NullPointerException(dex_pc, array_addr, 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002279 } else {
2280 // To save the code size, we are going to call the runtime function to
2281 // copy the content from DexFile.
2282
2283 // NOTE: We will check for the NullPointerException in the runtime.
2284
2285 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::FillArrayData);
2286
2287 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2288
2289 EmitUpdateDexPC(dex_pc);
2290
2291 irb_.CreateCall4(runtime_func,
2292 method_object_addr, irb_.getInt32(dex_pc),
2293 array_addr, irb_.getInt32(payload_offset));
2294
2295 EmitGuard_ExceptionLandingPad(dex_pc);
2296 }
2297
2298 return;
2299}
2300
2301llvm::Value* GBCExpanderPass::EmitAllocNewArray(uint32_t dex_pc,
2302 llvm::Value* array_length_value,
2303 uint32_t type_idx,
2304 bool is_filled_new_array) {
2305 llvm::Function* runtime_func;
2306
2307 bool skip_access_check =
Ian Rogers89756f22013-03-04 16:40:02 -08002308 driver_->CanAccessTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
2309 *dex_compilation_unit_->GetDexFile(), type_idx);
TDYa127f71bf5a2012-07-29 20:09:52 -07002310
2311
2312 if (is_filled_new_array) {
2313 runtime_func = skip_access_check ?
2314 irb_.GetRuntime(runtime_support::CheckAndAllocArray) :
2315 irb_.GetRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck);
2316 } else {
2317 runtime_func = skip_access_check ?
2318 irb_.GetRuntime(runtime_support::AllocArray) :
2319 irb_.GetRuntime(runtime_support::AllocArrayWithAccessCheck);
2320 }
2321
2322 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2323
2324 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2325
2326 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2327
2328 EmitUpdateDexPC(dex_pc);
2329
2330 llvm::Value* object_addr =
2331 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
2332 array_length_value, thread_object_addr);
2333
2334 EmitGuard_ExceptionLandingPad(dex_pc);
2335
2336 return object_addr;
2337}
2338
2339llvm::Value* GBCExpanderPass::
2340EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -07002341 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -07002342 llvm::Value* this_addr,
2343 uint32_t dex_pc,
2344 bool is_fast_path) {
2345
2346 llvm::Function* runtime_func = NULL;
2347
2348 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002349 case art::kStatic:
TDYa127f71bf5a2012-07-29 20:09:52 -07002350 runtime_func = irb_.GetRuntime(runtime_support::FindStaticMethodWithAccessCheck);
2351 break;
2352
TDYa127920be7c2012-09-10 17:13:22 -07002353 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002354 runtime_func = irb_.GetRuntime(runtime_support::FindDirectMethodWithAccessCheck);
2355 break;
2356
TDYa127920be7c2012-09-10 17:13:22 -07002357 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002358 runtime_func = irb_.GetRuntime(runtime_support::FindVirtualMethodWithAccessCheck);
2359 break;
2360
TDYa127920be7c2012-09-10 17:13:22 -07002361 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002362 runtime_func = irb_.GetRuntime(runtime_support::FindSuperMethodWithAccessCheck);
2363 break;
2364
TDYa127920be7c2012-09-10 17:13:22 -07002365 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002366 if (is_fast_path) {
2367 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethod);
2368 } else {
2369 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethodWithAccessCheck);
2370 }
2371 break;
2372 }
2373
2374 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2375
2376 if (this_addr == NULL) {
TDYa127920be7c2012-09-10 17:13:22 -07002377 DCHECK_EQ(invoke_type, art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002378 this_addr = irb_.getJNull();
2379 }
2380
2381 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2382
2383 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2384
2385 EmitUpdateDexPC(dex_pc);
2386
2387 llvm::Value* callee_method_object_addr =
2388 irb_.CreateCall4(runtime_func,
2389 callee_method_idx_value,
2390 this_addr,
2391 caller_method_object_addr,
2392 thread_object_addr);
2393
2394 EmitGuard_ExceptionLandingPad(dex_pc);
2395
2396 return callee_method_object_addr;
2397}
2398
TDYa1275e869b62012-07-25 00:45:39 -07002399void GBCExpanderPass::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2400 // Using runtime support, let the target can override by InlineAssembly.
2401 irb_.Runtime().EmitMarkGCCard(value, target_addr);
2402}
2403
2404void GBCExpanderPass::EmitUpdateDexPC(uint32_t dex_pc) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002405 if (shadow_frame_ == NULL) {
2406 return;
2407 }
TDYa1275e869b62012-07-25 00:45:39 -07002408 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07002409 art::ShadowFrame::DexPCOffset(),
TDYa1275e869b62012-07-25 00:45:39 -07002410 irb_.getInt32(dex_pc),
2411 kTBAAShadowFrame);
2412}
2413
2414void GBCExpanderPass::EmitGuard_DivZeroException(uint32_t dex_pc,
2415 llvm::Value* denominator,
2416 JType op_jty) {
2417 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2418
2419 llvm::Constant* zero = irb_.getJZero(op_jty);
2420
2421 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2422
2423 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2424
2425 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2426
2427 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
2428
2429 irb_.SetInsertPoint(block_exception);
2430 EmitUpdateDexPC(dex_pc);
2431 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowDivZeroException));
2432 EmitBranchExceptionLandingPad(dex_pc);
2433
2434 irb_.SetInsertPoint(block_continue);
2435}
2436
2437void GBCExpanderPass::EmitGuard_NullPointerException(uint32_t dex_pc,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002438 llvm::Value* object,
2439 int opt_flags) {
2440 bool ignore_null_check = ((opt_flags & MIR_IGNORE_NULL_CHECK) != 0);
2441 if (ignore_null_check) {
2442 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2443 if (lpad) {
2444 // There is at least one catch: create a "fake" conditional branch to
2445 // keep the exception edge to the catch block.
2446 landing_pad_phi_mapping_[lpad].push_back(
2447 std::make_pair(current_bb_->getUniquePredecessor(),
2448 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002449
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002450 llvm::BasicBlock* block_continue =
2451 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002452
Ian Rogers8e696052013-03-04 09:00:40 -08002453 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002454
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002455 irb_.SetInsertPoint(block_continue);
2456 }
2457 } else {
2458 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
TDYa1275e869b62012-07-25 00:45:39 -07002459
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002460 llvm::BasicBlock* block_exception =
2461 CreateBasicBlockWithDexPC(dex_pc, "nullp");
TDYa1275e869b62012-07-25 00:45:39 -07002462
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002463 llvm::BasicBlock* block_continue =
2464 CreateBasicBlockWithDexPC(dex_pc, "cont");
2465
2466 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
2467
2468 irb_.SetInsertPoint(block_exception);
2469 EmitUpdateDexPC(dex_pc);
2470 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowNullPointerException),
2471 irb_.getInt32(dex_pc));
2472 EmitBranchExceptionLandingPad(dex_pc);
2473
2474 irb_.SetInsertPoint(block_continue);
2475 }
TDYa1275e869b62012-07-25 00:45:39 -07002476}
2477
2478void
2479GBCExpanderPass::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2480 llvm::Value* array,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002481 llvm::Value* index,
2482 int opt_flags) {
2483 bool ignore_range_check = ((opt_flags & MIR_IGNORE_RANGE_CHECK) != 0);
2484 if (ignore_range_check) {
2485 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2486 if (lpad) {
2487 // There is at least one catch: create a "fake" conditional branch to
2488 // keep the exception edge to the catch block.
2489 landing_pad_phi_mapping_[lpad].push_back(
2490 std::make_pair(current_bb_->getUniquePredecessor(),
2491 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002492
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002493 llvm::BasicBlock* block_continue =
2494 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002495
Ian Rogers8e696052013-03-04 09:00:40 -08002496 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002497
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002498 irb_.SetInsertPoint(block_continue);
2499 }
2500 } else {
2501 llvm::Value* array_len = EmitLoadArrayLength(array);
TDYa1275e869b62012-07-25 00:45:39 -07002502
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002503 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
TDYa1275e869b62012-07-25 00:45:39 -07002504
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002505 llvm::BasicBlock* block_exception =
2506 CreateBasicBlockWithDexPC(dex_pc, "overflow");
TDYa1275e869b62012-07-25 00:45:39 -07002507
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002508 llvm::BasicBlock* block_continue =
2509 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002510
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002511 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
2512
2513 irb_.SetInsertPoint(block_exception);
2514
2515 EmitUpdateDexPC(dex_pc);
2516 irb_.CreateCall2(irb_.GetRuntime(runtime_support::ThrowIndexOutOfBounds), index, array_len);
2517 EmitBranchExceptionLandingPad(dex_pc);
2518
2519 irb_.SetInsertPoint(block_continue);
2520 }
TDYa1275e869b62012-07-25 00:45:39 -07002521}
2522
Ian Rogers8e696052013-03-04 09:00:40 -08002523llvm::FunctionType* GBCExpanderPass::GetFunctionType(llvm::Type* ret_type, uint32_t method_idx,
TDYa1275e869b62012-07-25 00:45:39 -07002524 bool is_static) {
2525 // Get method signature
Ian Rogers8e696052013-03-04 09:00:40 -08002526 art::DexFile::MethodId const& method_id =
Ian Rogers89756f22013-03-04 16:40:02 -08002527 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx);
TDYa1275e869b62012-07-25 00:45:39 -07002528
2529 uint32_t shorty_size;
Ian Rogers89756f22013-03-04 16:40:02 -08002530 const char* shorty = dex_compilation_unit_->GetDexFile()->GetMethodShorty(method_id, &shorty_size);
TDYa1275e869b62012-07-25 00:45:39 -07002531 CHECK_GE(shorty_size, 1u);
2532
TDYa1275e869b62012-07-25 00:45:39 -07002533 // Get argument type
2534 std::vector<llvm::Type*> args_type;
2535
2536 args_type.push_back(irb_.getJObjectTy()); // method object pointer
2537
2538 if (!is_static) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002539 args_type.push_back(irb_.getJType('L')); // "this" object pointer
TDYa1275e869b62012-07-25 00:45:39 -07002540 }
2541
2542 for (uint32_t i = 1; i < shorty_size; ++i) {
buzbee26f10ee2012-12-21 11:16:29 -08002543 char shorty_type = art::RemapShorty(shorty[i]);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002544 args_type.push_back(irb_.getJType(shorty_type));
TDYa1275e869b62012-07-25 00:45:39 -07002545 }
2546
2547 return llvm::FunctionType::get(ret_type, args_type, false);
2548}
2549
2550
2551llvm::BasicBlock* GBCExpanderPass::
2552CreateBasicBlockWithDexPC(uint32_t dex_pc, const char* postfix) {
2553 std::string name;
2554
2555#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002556 art::StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
TDYa1275e869b62012-07-25 00:45:39 -07002557#endif
2558
2559 return llvm::BasicBlock::Create(context_, name, func_);
2560}
2561
2562llvm::BasicBlock* GBCExpanderPass::GetBasicBlock(uint32_t dex_pc) {
Ian Rogers89756f22013-03-04 16:40:02 -08002563 DCHECK(dex_pc < dex_compilation_unit_->GetCodeItem()->insns_size_in_code_units_);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002564 CHECK(basic_blocks_[dex_pc] != NULL);
TDYa1275e869b62012-07-25 00:45:39 -07002565 return basic_blocks_[dex_pc];
2566}
2567
2568int32_t GBCExpanderPass::GetTryItemOffset(uint32_t dex_pc) {
2569 int32_t min = 0;
Ian Rogers89756f22013-03-04 16:40:02 -08002570 int32_t max = dex_compilation_unit_->GetCodeItem()->tries_size_ - 1;
TDYa1275e869b62012-07-25 00:45:39 -07002571
2572 while (min <= max) {
2573 int32_t mid = min + (max - min) / 2;
2574
Ian Rogers89756f22013-03-04 16:40:02 -08002575 const art::DexFile::TryItem* ti =
2576 art::DexFile::GetTryItems(*dex_compilation_unit_->GetCodeItem(), mid);
TDYa1275e869b62012-07-25 00:45:39 -07002577 uint32_t start = ti->start_addr_;
2578 uint32_t end = start + ti->insn_count_;
2579
2580 if (dex_pc < start) {
2581 max = mid - 1;
2582 } else if (dex_pc >= end) {
2583 min = mid + 1;
2584 } else {
2585 return mid; // found
2586 }
2587 }
2588
2589 return -1; // not found
2590}
2591
2592llvm::BasicBlock* GBCExpanderPass::GetLandingPadBasicBlock(uint32_t dex_pc) {
2593 // Find the try item for this address in this method
2594 int32_t ti_offset = GetTryItemOffset(dex_pc);
2595
2596 if (ti_offset == -1) {
2597 return NULL; // No landing pad is available for this address.
2598 }
2599
2600 // Check for the existing landing pad basic block
2601 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2602 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2603
2604 if (block_lpad) {
2605 // We have generated landing pad for this try item already. Return the
2606 // same basic block.
2607 return block_lpad;
2608 }
2609
2610 // Get try item from code item
Ian Rogers89756f22013-03-04 16:40:02 -08002611 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*dex_compilation_unit_->GetCodeItem(),
Ian Rogers8e696052013-03-04 09:00:40 -08002612 ti_offset);
TDYa1275e869b62012-07-25 00:45:39 -07002613
2614 std::string lpadname;
2615
2616#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002617 art::StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
TDYa1275e869b62012-07-25 00:45:39 -07002618#endif
2619
2620 // Create landing pad basic block
2621 block_lpad = llvm::BasicBlock::Create(context_, lpadname, func_);
2622
2623 // Change IRBuilder insert point
2624 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2625 irb_.SetInsertPoint(block_lpad);
2626
2627 // Find catch block with matching type
2628 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2629
2630 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
2631
2632 llvm::Value* catch_handler_index_value =
2633 irb_.CreateCall2(irb_.GetRuntime(runtime_support::FindCatchBlock),
2634 method_object_addr, ti_offset_value);
2635
2636 // Switch instruction (Go to unwind basic block by default)
2637 llvm::SwitchInst* sw =
2638 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2639
2640 // Cases with matched catch block
Ian Rogers89756f22013-03-04 16:40:02 -08002641 art::CatchHandlerIterator iter(*dex_compilation_unit_->GetCodeItem(), ti->start_addr_);
TDYa1275e869b62012-07-25 00:45:39 -07002642
2643 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2644 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2645 }
2646
2647 // Restore the orignal insert point for IRBuilder
2648 irb_.restoreIP(irb_ip_original);
2649
2650 // Cache this landing pad
2651 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2652 basic_block_landing_pads_[ti_offset] = block_lpad;
2653
2654 return block_lpad;
2655}
2656
2657llvm::BasicBlock* GBCExpanderPass::GetUnwindBasicBlock() {
2658 // Check the existing unwinding baisc block block
2659 if (basic_block_unwind_ != NULL) {
2660 return basic_block_unwind_;
2661 }
2662
2663 // Create new basic block for unwinding
2664 basic_block_unwind_ =
2665 llvm::BasicBlock::Create(context_, "exception_unwind", func_);
2666
2667 // Change IRBuilder insert point
2668 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2669 irb_.SetInsertPoint(basic_block_unwind_);
2670
2671 // Pop the shadow frame
2672 Expand_PopShadowFrame();
2673
2674 // Emit the code to return default value (zero) for the given return type.
Ian Rogers89756f22013-03-04 16:40:02 -08002675 char ret_shorty = dex_compilation_unit_->GetShorty()[0];
buzbee26f10ee2012-12-21 11:16:29 -08002676 ret_shorty = art::RemapShorty(ret_shorty);
TDYa1275e869b62012-07-25 00:45:39 -07002677 if (ret_shorty == 'V') {
2678 irb_.CreateRetVoid();
2679 } else {
2680 irb_.CreateRet(irb_.getJZero(ret_shorty));
2681 }
2682
2683 // Restore the orignal insert point for IRBuilder
2684 irb_.restoreIP(irb_ip_original);
2685
2686 return basic_block_unwind_;
2687}
2688
2689void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2690 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002691 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002692 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002693 irb_.CreateBr(lpad);
2694 } else {
2695 irb_.CreateBr(GetUnwindBasicBlock());
2696 }
2697}
2698
2699void GBCExpanderPass::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
Jeff Hao9a142652013-01-17 23:10:19 +00002700 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
2701
TDYa1275e869b62012-07-25 00:45:39 -07002702 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2703
2704 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002705 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002706 irb_.GetInsertBlock()));
Jeff Hao9a142652013-01-17 23:10:19 +00002707 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002708 } else {
Jeff Hao9a142652013-01-17 23:10:19 +00002709 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002710 }
2711
2712 irb_.SetInsertPoint(block_cont);
2713}
2714
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002715llvm::Value*
2716GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
2717 llvm::CallInst& call_inst) {
2718 switch (intr_id) {
2719 //==- Thread -----------------------------------------------------------==//
2720 case IntrinsicHelper::GetCurrentThread: {
TDYa127b672d1e2012-06-28 21:21:45 -07002721 return irb_.Runtime().EmitGetCurrentThread();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002722 }
Logan Chien75e4b602012-07-23 14:24:12 -07002723 case IntrinsicHelper::CheckSuspend: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08002724 Expand_TestSuspend(call_inst);
TDYa127890ea892012-08-22 10:49:42 -07002725 return NULL;
2726 }
2727 case IntrinsicHelper::TestSuspend: {
Logan Chiend54a23d2012-07-24 11:19:23 -07002728 Expand_TestSuspend(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002729 return NULL;
2730 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002731 case IntrinsicHelper::MarkGCCard: {
TDYa1279a129452012-07-19 03:10:08 -07002732 Expand_MarkGCCard(call_inst);
2733 return NULL;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002734 }
Logan Chien75e4b602012-07-23 14:24:12 -07002735
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002736 //==- Exception --------------------------------------------------------==//
2737 case IntrinsicHelper::ThrowException: {
2738 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
2739 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002740 case IntrinsicHelper::HLThrowException: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002741 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2742
2743 EmitUpdateDexPC(dex_pc);
2744
2745 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowException),
2746 call_inst.getArgOperand(0));
2747
2748 EmitGuard_ExceptionLandingPad(dex_pc);
2749 return NULL;
2750 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002751 case IntrinsicHelper::GetException: {
TDYa127823433d2012-09-26 16:03:51 -07002752 return irb_.Runtime().EmitGetAndClearException();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002753 }
2754 case IntrinsicHelper::IsExceptionPending: {
2755 return irb_.Runtime().EmitIsExceptionPending();
2756 }
2757 case IntrinsicHelper::FindCatchBlock: {
2758 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
2759 }
2760 case IntrinsicHelper::ThrowDivZeroException: {
2761 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
2762 }
2763 case IntrinsicHelper::ThrowNullPointerException: {
2764 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
2765 }
2766 case IntrinsicHelper::ThrowIndexOutOfBounds: {
2767 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
2768 }
Logan Chien75e4b602012-07-23 14:24:12 -07002769
2770 //==- Const String -----------------------------------------------------==//
2771 case IntrinsicHelper::ConstString: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002772 return Expand_ConstString(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002773 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002774 case IntrinsicHelper::LoadStringFromDexCache: {
2775 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
2776 }
2777 case IntrinsicHelper::ResolveString: {
2778 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
2779 }
Logan Chien75e4b602012-07-23 14:24:12 -07002780
2781 //==- Const Class ------------------------------------------------------==//
2782 case IntrinsicHelper::ConstClass: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002783 return Expand_ConstClass(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002784 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002785 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
2786 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
2787 }
2788 case IntrinsicHelper::LoadTypeFromDexCache: {
2789 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
2790 }
2791 case IntrinsicHelper::InitializeType: {
2792 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
2793 }
Logan Chien75e4b602012-07-23 14:24:12 -07002794
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002795 //==- Lock -------------------------------------------------------------==//
2796 case IntrinsicHelper::LockObject: {
2797 Expand_LockObject(call_inst.getArgOperand(0));
2798 return NULL;
2799 }
2800 case IntrinsicHelper::UnlockObject: {
2801 Expand_UnlockObject(call_inst.getArgOperand(0));
2802 return NULL;
2803 }
Logan Chien75e4b602012-07-23 14:24:12 -07002804
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002805 //==- Cast -------------------------------------------------------------==//
2806 case IntrinsicHelper::CheckCast: {
2807 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
2808 }
Logan Chien75e4b602012-07-23 14:24:12 -07002809 case IntrinsicHelper::HLCheckCast: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002810 Expand_HLCheckCast(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002811 return NULL;
2812 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002813 case IntrinsicHelper::IsAssignable: {
2814 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
2815 }
Logan Chien75e4b602012-07-23 14:24:12 -07002816
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002817 //==- Alloc ------------------------------------------------------------==//
2818 case IntrinsicHelper::AllocObject: {
2819 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
2820 }
2821 case IntrinsicHelper::AllocObjectWithAccessCheck: {
2822 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
2823 }
Logan Chien75e4b602012-07-23 14:24:12 -07002824
2825 //==- Instance ---------------------------------------------------------==//
2826 case IntrinsicHelper::NewInstance: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002827 return Expand_NewInstance(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002828 }
2829 case IntrinsicHelper::InstanceOf: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002830 return Expand_InstanceOf(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002831 }
2832
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002833 //==- Array ------------------------------------------------------------==//
Logan Chien75e4b602012-07-23 14:24:12 -07002834 case IntrinsicHelper::NewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002835 return Expand_NewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002836 }
2837 case IntrinsicHelper::OptArrayLength: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002838 return Expand_OptArrayLength(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002839 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002840 case IntrinsicHelper::ArrayLength: {
2841 return EmitLoadArrayLength(call_inst.getArgOperand(0));
2842 }
2843 case IntrinsicHelper::AllocArray: {
2844 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
2845 }
2846 case IntrinsicHelper::AllocArrayWithAccessCheck: {
2847 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
2848 call_inst);
2849 }
2850 case IntrinsicHelper::CheckAndAllocArray: {
2851 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
2852 }
2853 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
2854 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
2855 call_inst);
2856 }
2857 case IntrinsicHelper::ArrayGet: {
2858 return Expand_ArrayGet(call_inst.getArgOperand(0),
2859 call_inst.getArgOperand(1),
2860 kInt);
2861 }
2862 case IntrinsicHelper::ArrayGetWide: {
2863 return Expand_ArrayGet(call_inst.getArgOperand(0),
2864 call_inst.getArgOperand(1),
2865 kLong);
2866 }
2867 case IntrinsicHelper::ArrayGetObject: {
2868 return Expand_ArrayGet(call_inst.getArgOperand(0),
2869 call_inst.getArgOperand(1),
2870 kObject);
2871 }
2872 case IntrinsicHelper::ArrayGetBoolean: {
2873 return Expand_ArrayGet(call_inst.getArgOperand(0),
2874 call_inst.getArgOperand(1),
2875 kBoolean);
2876 }
2877 case IntrinsicHelper::ArrayGetByte: {
2878 return Expand_ArrayGet(call_inst.getArgOperand(0),
2879 call_inst.getArgOperand(1),
2880 kByte);
2881 }
2882 case IntrinsicHelper::ArrayGetChar: {
2883 return Expand_ArrayGet(call_inst.getArgOperand(0),
2884 call_inst.getArgOperand(1),
2885 kChar);
2886 }
2887 case IntrinsicHelper::ArrayGetShort: {
2888 return Expand_ArrayGet(call_inst.getArgOperand(0),
2889 call_inst.getArgOperand(1),
2890 kShort);
2891 }
2892 case IntrinsicHelper::ArrayPut: {
2893 Expand_ArrayPut(call_inst.getArgOperand(0),
2894 call_inst.getArgOperand(1),
2895 call_inst.getArgOperand(2),
2896 kInt);
2897 return NULL;
2898 }
2899 case IntrinsicHelper::ArrayPutWide: {
2900 Expand_ArrayPut(call_inst.getArgOperand(0),
2901 call_inst.getArgOperand(1),
2902 call_inst.getArgOperand(2),
2903 kLong);
2904 return NULL;
2905 }
2906 case IntrinsicHelper::ArrayPutObject: {
2907 Expand_ArrayPut(call_inst.getArgOperand(0),
2908 call_inst.getArgOperand(1),
2909 call_inst.getArgOperand(2),
2910 kObject);
2911 return NULL;
2912 }
2913 case IntrinsicHelper::ArrayPutBoolean: {
2914 Expand_ArrayPut(call_inst.getArgOperand(0),
2915 call_inst.getArgOperand(1),
2916 call_inst.getArgOperand(2),
2917 kBoolean);
2918 return NULL;
2919 }
2920 case IntrinsicHelper::ArrayPutByte: {
2921 Expand_ArrayPut(call_inst.getArgOperand(0),
2922 call_inst.getArgOperand(1),
2923 call_inst.getArgOperand(2),
2924 kByte);
2925 return NULL;
2926 }
2927 case IntrinsicHelper::ArrayPutChar: {
2928 Expand_ArrayPut(call_inst.getArgOperand(0),
2929 call_inst.getArgOperand(1),
2930 call_inst.getArgOperand(2),
2931 kChar);
2932 return NULL;
2933 }
2934 case IntrinsicHelper::ArrayPutShort: {
2935 Expand_ArrayPut(call_inst.getArgOperand(0),
2936 call_inst.getArgOperand(1),
2937 call_inst.getArgOperand(2),
2938 kShort);
2939 return NULL;
2940 }
2941 case IntrinsicHelper::CheckPutArrayElement: {
2942 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
2943 }
2944 case IntrinsicHelper::FilledNewArray: {
2945 Expand_FilledNewArray(call_inst);
2946 return NULL;
2947 }
2948 case IntrinsicHelper::FillArrayData: {
2949 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
2950 }
Logan Chien75e4b602012-07-23 14:24:12 -07002951 case IntrinsicHelper::HLFillArrayData: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002952 Expand_HLFillArrayData(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002953 return NULL;
2954 }
2955 case IntrinsicHelper::HLFilledNewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002956 return Expand_HLFilledNewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002957 }
2958
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002959 //==- Instance Field ---------------------------------------------------==//
2960 case IntrinsicHelper::InstanceFieldGet:
2961 case IntrinsicHelper::InstanceFieldGetBoolean:
2962 case IntrinsicHelper::InstanceFieldGetByte:
2963 case IntrinsicHelper::InstanceFieldGetChar:
2964 case IntrinsicHelper::InstanceFieldGetShort: {
2965 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
2966 }
2967 case IntrinsicHelper::InstanceFieldGetWide: {
2968 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
2969 }
2970 case IntrinsicHelper::InstanceFieldGetObject: {
2971 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
2972 }
2973 case IntrinsicHelper::InstanceFieldGetFast: {
2974 return Expand_IGetFast(call_inst.getArgOperand(0),
2975 call_inst.getArgOperand(1),
2976 call_inst.getArgOperand(2),
2977 kInt);
2978 }
2979 case IntrinsicHelper::InstanceFieldGetWideFast: {
2980 return Expand_IGetFast(call_inst.getArgOperand(0),
2981 call_inst.getArgOperand(1),
2982 call_inst.getArgOperand(2),
2983 kLong);
2984 }
2985 case IntrinsicHelper::InstanceFieldGetObjectFast: {
2986 return Expand_IGetFast(call_inst.getArgOperand(0),
2987 call_inst.getArgOperand(1),
2988 call_inst.getArgOperand(2),
2989 kObject);
2990 }
2991 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
2992 return Expand_IGetFast(call_inst.getArgOperand(0),
2993 call_inst.getArgOperand(1),
2994 call_inst.getArgOperand(2),
2995 kBoolean);
2996 }
2997 case IntrinsicHelper::InstanceFieldGetByteFast: {
2998 return Expand_IGetFast(call_inst.getArgOperand(0),
2999 call_inst.getArgOperand(1),
3000 call_inst.getArgOperand(2),
3001 kByte);
3002 }
3003 case IntrinsicHelper::InstanceFieldGetCharFast: {
3004 return Expand_IGetFast(call_inst.getArgOperand(0),
3005 call_inst.getArgOperand(1),
3006 call_inst.getArgOperand(2),
3007 kChar);
3008 }
3009 case IntrinsicHelper::InstanceFieldGetShortFast: {
3010 return Expand_IGetFast(call_inst.getArgOperand(0),
3011 call_inst.getArgOperand(1),
3012 call_inst.getArgOperand(2),
3013 kShort);
3014 }
3015 case IntrinsicHelper::InstanceFieldPut:
3016 case IntrinsicHelper::InstanceFieldPutBoolean:
3017 case IntrinsicHelper::InstanceFieldPutByte:
3018 case IntrinsicHelper::InstanceFieldPutChar:
3019 case IntrinsicHelper::InstanceFieldPutShort: {
3020 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
3021 }
3022 case IntrinsicHelper::InstanceFieldPutWide: {
3023 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
3024 }
3025 case IntrinsicHelper::InstanceFieldPutObject: {
3026 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
3027 }
3028 case IntrinsicHelper::InstanceFieldPutFast: {
3029 Expand_IPutFast(call_inst.getArgOperand(0),
3030 call_inst.getArgOperand(1),
3031 call_inst.getArgOperand(2),
3032 call_inst.getArgOperand(3),
3033 kInt);
3034 return NULL;
3035 }
3036 case IntrinsicHelper::InstanceFieldPutWideFast: {
3037 Expand_IPutFast(call_inst.getArgOperand(0),
3038 call_inst.getArgOperand(1),
3039 call_inst.getArgOperand(2),
3040 call_inst.getArgOperand(3),
3041 kLong);
3042 return NULL;
3043 }
3044 case IntrinsicHelper::InstanceFieldPutObjectFast: {
3045 Expand_IPutFast(call_inst.getArgOperand(0),
3046 call_inst.getArgOperand(1),
3047 call_inst.getArgOperand(2),
3048 call_inst.getArgOperand(3),
3049 kObject);
3050 return NULL;
3051 }
3052 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
3053 Expand_IPutFast(call_inst.getArgOperand(0),
3054 call_inst.getArgOperand(1),
3055 call_inst.getArgOperand(2),
3056 call_inst.getArgOperand(3),
3057 kBoolean);
3058 return NULL;
3059 }
3060 case IntrinsicHelper::InstanceFieldPutByteFast: {
3061 Expand_IPutFast(call_inst.getArgOperand(0),
3062 call_inst.getArgOperand(1),
3063 call_inst.getArgOperand(2),
3064 call_inst.getArgOperand(3),
3065 kByte);
3066 return NULL;
3067 }
3068 case IntrinsicHelper::InstanceFieldPutCharFast: {
3069 Expand_IPutFast(call_inst.getArgOperand(0),
3070 call_inst.getArgOperand(1),
3071 call_inst.getArgOperand(2),
3072 call_inst.getArgOperand(3),
3073 kChar);
3074 return NULL;
3075 }
3076 case IntrinsicHelper::InstanceFieldPutShortFast: {
3077 Expand_IPutFast(call_inst.getArgOperand(0),
3078 call_inst.getArgOperand(1),
3079 call_inst.getArgOperand(2),
3080 call_inst.getArgOperand(3),
3081 kShort);
3082 return NULL;
3083 }
Logan Chien75e4b602012-07-23 14:24:12 -07003084
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003085 //==- Static Field -----------------------------------------------------==//
3086 case IntrinsicHelper::StaticFieldGet:
3087 case IntrinsicHelper::StaticFieldGetBoolean:
3088 case IntrinsicHelper::StaticFieldGetByte:
3089 case IntrinsicHelper::StaticFieldGetChar:
3090 case IntrinsicHelper::StaticFieldGetShort: {
3091 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
3092 }
3093 case IntrinsicHelper::StaticFieldGetWide: {
3094 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
3095 }
3096 case IntrinsicHelper::StaticFieldGetObject: {
3097 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
3098 }
3099 case IntrinsicHelper::StaticFieldGetFast: {
3100 return Expand_SGetFast(call_inst.getArgOperand(0),
3101 call_inst.getArgOperand(1),
3102 call_inst.getArgOperand(2),
3103 kInt);
3104 }
3105 case IntrinsicHelper::StaticFieldGetWideFast: {
3106 return Expand_SGetFast(call_inst.getArgOperand(0),
3107 call_inst.getArgOperand(1),
3108 call_inst.getArgOperand(2),
3109 kLong);
3110 }
3111 case IntrinsicHelper::StaticFieldGetObjectFast: {
3112 return Expand_SGetFast(call_inst.getArgOperand(0),
3113 call_inst.getArgOperand(1),
3114 call_inst.getArgOperand(2),
3115 kObject);
3116 }
3117 case IntrinsicHelper::StaticFieldGetBooleanFast: {
3118 return Expand_SGetFast(call_inst.getArgOperand(0),
3119 call_inst.getArgOperand(1),
3120 call_inst.getArgOperand(2),
3121 kBoolean);
3122 }
3123 case IntrinsicHelper::StaticFieldGetByteFast: {
3124 return Expand_SGetFast(call_inst.getArgOperand(0),
3125 call_inst.getArgOperand(1),
3126 call_inst.getArgOperand(2),
3127 kByte);
3128 }
3129 case IntrinsicHelper::StaticFieldGetCharFast: {
3130 return Expand_SGetFast(call_inst.getArgOperand(0),
3131 call_inst.getArgOperand(1),
3132 call_inst.getArgOperand(2),
3133 kChar);
3134 }
3135 case IntrinsicHelper::StaticFieldGetShortFast: {
3136 return Expand_SGetFast(call_inst.getArgOperand(0),
3137 call_inst.getArgOperand(1),
3138 call_inst.getArgOperand(2),
3139 kShort);
3140 }
3141 case IntrinsicHelper::StaticFieldPut:
3142 case IntrinsicHelper::StaticFieldPutBoolean:
3143 case IntrinsicHelper::StaticFieldPutByte:
3144 case IntrinsicHelper::StaticFieldPutChar:
3145 case IntrinsicHelper::StaticFieldPutShort: {
3146 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
3147 }
3148 case IntrinsicHelper::StaticFieldPutWide: {
3149 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
3150 }
3151 case IntrinsicHelper::StaticFieldPutObject: {
3152 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
3153 }
3154 case IntrinsicHelper::StaticFieldPutFast: {
3155 Expand_SPutFast(call_inst.getArgOperand(0),
3156 call_inst.getArgOperand(1),
3157 call_inst.getArgOperand(2),
3158 call_inst.getArgOperand(3),
3159 kInt);
3160 return NULL;
3161 }
3162 case IntrinsicHelper::StaticFieldPutWideFast: {
3163 Expand_SPutFast(call_inst.getArgOperand(0),
3164 call_inst.getArgOperand(1),
3165 call_inst.getArgOperand(2),
3166 call_inst.getArgOperand(3),
3167 kLong);
3168 return NULL;
3169 }
3170 case IntrinsicHelper::StaticFieldPutObjectFast: {
3171 Expand_SPutFast(call_inst.getArgOperand(0),
3172 call_inst.getArgOperand(1),
3173 call_inst.getArgOperand(2),
3174 call_inst.getArgOperand(3),
3175 kObject);
3176 return NULL;
3177 }
3178 case IntrinsicHelper::StaticFieldPutBooleanFast: {
3179 Expand_SPutFast(call_inst.getArgOperand(0),
3180 call_inst.getArgOperand(1),
3181 call_inst.getArgOperand(2),
3182 call_inst.getArgOperand(3),
3183 kBoolean);
3184 return NULL;
3185 }
3186 case IntrinsicHelper::StaticFieldPutByteFast: {
3187 Expand_SPutFast(call_inst.getArgOperand(0),
3188 call_inst.getArgOperand(1),
3189 call_inst.getArgOperand(2),
3190 call_inst.getArgOperand(3),
3191 kByte);
3192 return NULL;
3193 }
3194 case IntrinsicHelper::StaticFieldPutCharFast: {
3195 Expand_SPutFast(call_inst.getArgOperand(0),
3196 call_inst.getArgOperand(1),
3197 call_inst.getArgOperand(2),
3198 call_inst.getArgOperand(3),
3199 kChar);
3200 return NULL;
3201 }
3202 case IntrinsicHelper::StaticFieldPutShortFast: {
3203 Expand_SPutFast(call_inst.getArgOperand(0),
3204 call_inst.getArgOperand(1),
3205 call_inst.getArgOperand(2),
3206 call_inst.getArgOperand(3),
3207 kShort);
3208 return NULL;
3209 }
3210 case IntrinsicHelper::LoadDeclaringClassSSB: {
3211 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
3212 }
3213 case IntrinsicHelper::LoadClassSSBFromDexCache: {
3214 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
3215 }
3216 case IntrinsicHelper::InitializeAndLoadClassSSB: {
3217 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
3218 }
Logan Chien75e4b602012-07-23 14:24:12 -07003219
3220 //==- High-level Array -------------------------------------------------==//
3221 case IntrinsicHelper::HLArrayGet: {
TDYa1275a26d442012-07-26 18:58:38 -07003222 return Expand_HLArrayGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003223 }
3224 case IntrinsicHelper::HLArrayGetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003225 return Expand_HLArrayGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003226 }
3227 case IntrinsicHelper::HLArrayGetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003228 return Expand_HLArrayGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003229 }
3230 case IntrinsicHelper::HLArrayGetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003231 return Expand_HLArrayGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003232 }
3233 case IntrinsicHelper::HLArrayGetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003234 return Expand_HLArrayGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003235 }
3236 case IntrinsicHelper::HLArrayGetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003237 return Expand_HLArrayGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003238 }
3239 case IntrinsicHelper::HLArrayGetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003240 return Expand_HLArrayGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003241 }
3242 case IntrinsicHelper::HLArrayGetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003243 return Expand_HLArrayGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003244 }
3245 case IntrinsicHelper::HLArrayGetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003246 return Expand_HLArrayGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003247 }
3248 case IntrinsicHelper::HLArrayPut: {
TDYa1275a26d442012-07-26 18:58:38 -07003249 Expand_HLArrayPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003250 return NULL;
3251 }
3252 case IntrinsicHelper::HLArrayPutBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003253 Expand_HLArrayPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003254 return NULL;
3255 }
3256 case IntrinsicHelper::HLArrayPutByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003257 Expand_HLArrayPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003258 return NULL;
3259 }
3260 case IntrinsicHelper::HLArrayPutChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003261 Expand_HLArrayPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003262 return NULL;
3263 }
3264 case IntrinsicHelper::HLArrayPutShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003265 Expand_HLArrayPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003266 return NULL;
3267 }
3268 case IntrinsicHelper::HLArrayPutFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003269 Expand_HLArrayPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003270 return NULL;
3271 }
3272 case IntrinsicHelper::HLArrayPutWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003273 Expand_HLArrayPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003274 return NULL;
3275 }
3276 case IntrinsicHelper::HLArrayPutDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003277 Expand_HLArrayPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003278 return NULL;
3279 }
3280 case IntrinsicHelper::HLArrayPutObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003281 Expand_HLArrayPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003282 return NULL;
3283 }
3284
3285 //==- High-level Instance ----------------------------------------------==//
3286 case IntrinsicHelper::HLIGet: {
TDYa1275e869b62012-07-25 00:45:39 -07003287 return Expand_HLIGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003288 }
3289 case IntrinsicHelper::HLIGetBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003290 return Expand_HLIGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003291 }
3292 case IntrinsicHelper::HLIGetByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003293 return Expand_HLIGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003294 }
3295 case IntrinsicHelper::HLIGetChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003296 return Expand_HLIGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003297 }
3298 case IntrinsicHelper::HLIGetShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003299 return Expand_HLIGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003300 }
3301 case IntrinsicHelper::HLIGetFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003302 return Expand_HLIGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003303 }
3304 case IntrinsicHelper::HLIGetWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003305 return Expand_HLIGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003306 }
3307 case IntrinsicHelper::HLIGetDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003308 return Expand_HLIGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003309 }
3310 case IntrinsicHelper::HLIGetObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003311 return Expand_HLIGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003312 }
3313 case IntrinsicHelper::HLIPut: {
TDYa1275e869b62012-07-25 00:45:39 -07003314 Expand_HLIPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003315 return NULL;
3316 }
3317 case IntrinsicHelper::HLIPutBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003318 Expand_HLIPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003319 return NULL;
3320 }
3321 case IntrinsicHelper::HLIPutByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003322 Expand_HLIPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003323 return NULL;
3324 }
3325 case IntrinsicHelper::HLIPutChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003326 Expand_HLIPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003327 return NULL;
3328 }
3329 case IntrinsicHelper::HLIPutShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003330 Expand_HLIPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003331 return NULL;
3332 }
3333 case IntrinsicHelper::HLIPutFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003334 Expand_HLIPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003335 return NULL;
3336 }
3337 case IntrinsicHelper::HLIPutWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003338 Expand_HLIPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003339 return NULL;
3340 }
3341 case IntrinsicHelper::HLIPutDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003342 Expand_HLIPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003343 return NULL;
3344 }
3345 case IntrinsicHelper::HLIPutObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003346 Expand_HLIPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003347 return NULL;
3348 }
3349
3350 //==- High-level Invoke ------------------------------------------------==//
TDYa127f71bf5a2012-07-29 20:09:52 -07003351 case IntrinsicHelper::HLInvokeVoid:
3352 case IntrinsicHelper::HLInvokeObj:
3353 case IntrinsicHelper::HLInvokeInt:
3354 case IntrinsicHelper::HLInvokeFloat:
3355 case IntrinsicHelper::HLInvokeLong:
Logan Chien75e4b602012-07-23 14:24:12 -07003356 case IntrinsicHelper::HLInvokeDouble: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003357 return Expand_HLInvoke(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003358 }
3359
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003360 //==- Invoke -----------------------------------------------------------==//
3361 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
3362 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
3363 }
3364 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
3365 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
3366 }
3367 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
3368 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
3369 }
3370 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
3371 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
3372 }
3373 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
3374 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
3375 }
3376 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
3377 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
3378 }
3379 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
3380 return Expand_GetVirtualCalleeMethodObjAddrFast(
3381 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
3382 }
3383 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
3384 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
3385 }
3386 case IntrinsicHelper::InvokeRetVoid:
3387 case IntrinsicHelper::InvokeRetBoolean:
3388 case IntrinsicHelper::InvokeRetByte:
3389 case IntrinsicHelper::InvokeRetChar:
3390 case IntrinsicHelper::InvokeRetShort:
3391 case IntrinsicHelper::InvokeRetInt:
3392 case IntrinsicHelper::InvokeRetLong:
3393 case IntrinsicHelper::InvokeRetFloat:
3394 case IntrinsicHelper::InvokeRetDouble:
3395 case IntrinsicHelper::InvokeRetObject: {
3396 return Expand_Invoke(call_inst);
3397 }
Logan Chien75e4b602012-07-23 14:24:12 -07003398
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003399 //==- Math -------------------------------------------------------------==//
3400 case IntrinsicHelper::DivInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003401 return Expand_DivRem(call_inst, /* is_div */true, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003402 }
3403 case IntrinsicHelper::RemInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003404 return Expand_DivRem(call_inst, /* is_div */false, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003405 }
3406 case IntrinsicHelper::DivLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003407 return Expand_DivRem(call_inst, /* is_div */true, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003408 }
3409 case IntrinsicHelper::RemLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003410 return Expand_DivRem(call_inst, /* is_div */false, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003411 }
3412 case IntrinsicHelper::D2L: {
3413 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
3414 }
3415 case IntrinsicHelper::D2I: {
3416 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
3417 }
3418 case IntrinsicHelper::F2L: {
3419 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
3420 }
3421 case IntrinsicHelper::F2I: {
3422 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
3423 }
Logan Chien75e4b602012-07-23 14:24:12 -07003424
3425 //==- High-level Static ------------------------------------------------==//
3426 case IntrinsicHelper::HLSget: {
TDYa1275a26d442012-07-26 18:58:38 -07003427 return Expand_HLSget(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003428 }
3429 case IntrinsicHelper::HLSgetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003430 return Expand_HLSget(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003431 }
3432 case IntrinsicHelper::HLSgetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003433 return Expand_HLSget(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003434 }
3435 case IntrinsicHelper::HLSgetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003436 return Expand_HLSget(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003437 }
3438 case IntrinsicHelper::HLSgetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003439 return Expand_HLSget(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003440 }
3441 case IntrinsicHelper::HLSgetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003442 return Expand_HLSget(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003443 }
3444 case IntrinsicHelper::HLSgetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003445 return Expand_HLSget(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003446 }
3447 case IntrinsicHelper::HLSgetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003448 return Expand_HLSget(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003449 }
3450 case IntrinsicHelper::HLSgetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003451 return Expand_HLSget(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003452 }
3453 case IntrinsicHelper::HLSput: {
TDYa1275a26d442012-07-26 18:58:38 -07003454 Expand_HLSput(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003455 return NULL;
3456 }
3457 case IntrinsicHelper::HLSputBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003458 Expand_HLSput(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003459 return NULL;
3460 }
3461 case IntrinsicHelper::HLSputByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003462 Expand_HLSput(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003463 return NULL;
3464 }
3465 case IntrinsicHelper::HLSputChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003466 Expand_HLSput(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003467 return NULL;
3468 }
3469 case IntrinsicHelper::HLSputShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003470 Expand_HLSput(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003471 return NULL;
3472 }
3473 case IntrinsicHelper::HLSputFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003474 Expand_HLSput(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003475 return NULL;
3476 }
3477 case IntrinsicHelper::HLSputWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003478 Expand_HLSput(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003479 return NULL;
3480 }
3481 case IntrinsicHelper::HLSputDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003482 Expand_HLSput(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003483 return NULL;
3484 }
3485 case IntrinsicHelper::HLSputObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003486 Expand_HLSput(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003487 return NULL;
3488 }
3489
3490 //==- High-level Monitor -----------------------------------------------==//
3491 case IntrinsicHelper::MonitorEnter: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003492 Expand_MonitorEnter(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003493 return NULL;
3494 }
3495 case IntrinsicHelper::MonitorExit: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003496 Expand_MonitorExit(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003497 return NULL;
3498 }
3499
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003500 //==- Shadow Frame -----------------------------------------------------==//
3501 case IntrinsicHelper::AllocaShadowFrame: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08003502 Expand_AllocaShadowFrame(call_inst.getArgOperand(0));
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003503 return NULL;
3504 }
TDYa1278e950c12012-11-02 09:58:19 -07003505 case IntrinsicHelper::SetVReg: {
3506 Expand_SetVReg(call_inst.getArgOperand(0),
3507 call_inst.getArgOperand(1));
3508 return NULL;
3509 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003510 case IntrinsicHelper::PopShadowFrame: {
3511 Expand_PopShadowFrame();
3512 return NULL;
3513 }
3514 case IntrinsicHelper::UpdateDexPC: {
3515 Expand_UpdateDexPC(call_inst.getArgOperand(0));
3516 return NULL;
3517 }
TDYa127a1b21852012-07-23 03:20:39 -07003518
Logan Chien75e4b602012-07-23 14:24:12 -07003519 //==- Comparison -------------------------------------------------------==//
3520 case IntrinsicHelper::CmplFloat:
3521 case IntrinsicHelper::CmplDouble: {
3522 return Expand_FPCompare(call_inst.getArgOperand(0),
3523 call_inst.getArgOperand(1),
3524 false);
3525 }
3526 case IntrinsicHelper::CmpgFloat:
3527 case IntrinsicHelper::CmpgDouble: {
3528 return Expand_FPCompare(call_inst.getArgOperand(0),
3529 call_inst.getArgOperand(1),
3530 true);
3531 }
3532 case IntrinsicHelper::CmpLong: {
3533 return Expand_LongCompare(call_inst.getArgOperand(0),
3534 call_inst.getArgOperand(1));
3535 }
TDYa127a1b21852012-07-23 03:20:39 -07003536
Logan Chien75e4b602012-07-23 14:24:12 -07003537 //==- Const ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003538 case IntrinsicHelper::ConstInt:
3539 case IntrinsicHelper::ConstLong: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003540 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003541 }
TDYa127920be7c2012-09-10 17:13:22 -07003542 case IntrinsicHelper::ConstFloat: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003543 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3544 irb_.getJFloatTy());
Logan Chien75e4b602012-07-23 14:24:12 -07003545 }
TDYa127920be7c2012-09-10 17:13:22 -07003546 case IntrinsicHelper::ConstDouble: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003547 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3548 irb_.getJDoubleTy());
3549 }
TDYa127920be7c2012-09-10 17:13:22 -07003550 case IntrinsicHelper::ConstObj: {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003551 CHECK(LV2UInt(call_inst.getArgOperand(0)) == 0);
3552 return irb_.getJNull();
Logan Chien75e4b602012-07-23 14:24:12 -07003553 }
3554
3555 //==- Method Info ------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003556 case IntrinsicHelper::MethodInfo: {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003557 // Nothing to be done, because MethodInfo carries optional hints that are
3558 // not needed by the portable path.
Logan Chien75e4b602012-07-23 14:24:12 -07003559 return NULL;
3560 }
3561
3562 //==- Copy -------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003563 case IntrinsicHelper::CopyInt:
3564 case IntrinsicHelper::CopyFloat:
3565 case IntrinsicHelper::CopyLong:
3566 case IntrinsicHelper::CopyDouble:
3567 case IntrinsicHelper::CopyObj: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003568 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003569 }
3570
3571 //==- Shift ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003572 case IntrinsicHelper::SHLLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003573 return Expand_IntegerShift(call_inst.getArgOperand(0),
3574 call_inst.getArgOperand(1),
3575 kIntegerSHL, kLong);
3576 }
TDYa127920be7c2012-09-10 17:13:22 -07003577 case IntrinsicHelper::SHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003578 return Expand_IntegerShift(call_inst.getArgOperand(0),
3579 call_inst.getArgOperand(1),
3580 kIntegerSHR, kLong);
3581 }
TDYa127920be7c2012-09-10 17:13:22 -07003582 case IntrinsicHelper::USHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003583 return Expand_IntegerShift(call_inst.getArgOperand(0),
3584 call_inst.getArgOperand(1),
3585 kIntegerUSHR, kLong);
3586 }
TDYa127920be7c2012-09-10 17:13:22 -07003587 case IntrinsicHelper::SHLInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003588 return Expand_IntegerShift(call_inst.getArgOperand(0),
3589 call_inst.getArgOperand(1),
3590 kIntegerSHL, kInt);
3591 }
TDYa127920be7c2012-09-10 17:13:22 -07003592 case IntrinsicHelper::SHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003593 return Expand_IntegerShift(call_inst.getArgOperand(0),
3594 call_inst.getArgOperand(1),
3595 kIntegerSHR, kInt);
3596 }
TDYa127920be7c2012-09-10 17:13:22 -07003597 case IntrinsicHelper::USHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003598 return Expand_IntegerShift(call_inst.getArgOperand(0),
3599 call_inst.getArgOperand(1),
3600 kIntegerUSHR, kInt);
3601 }
3602
3603 //==- Conversion -------------------------------------------------------==//
TDYa127a1b21852012-07-23 03:20:39 -07003604 case IntrinsicHelper::IntToChar: {
3605 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
3606 irb_.getJIntTy());
3607 }
3608 case IntrinsicHelper::IntToShort: {
3609 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
3610 irb_.getJIntTy());
3611 }
3612 case IntrinsicHelper::IntToByte: {
3613 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
3614 irb_.getJIntTy());
3615 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003616
TDYa12787caa7e2012-08-25 23:23:27 -07003617 //==- Exception --------------------------------------------------------==//
3618 case IntrinsicHelper::CatchTargets: {
TDYa12755e5e6c2012-09-11 15:14:42 -07003619 UpdatePhiInstruction(current_bb_, irb_.GetInsertBlock());
TDYa12787caa7e2012-08-25 23:23:27 -07003620 llvm::SwitchInst* si = llvm::dyn_cast<llvm::SwitchInst>(call_inst.getNextNode());
3621 CHECK(si != NULL);
3622 irb_.CreateBr(si->getDefaultDest());
3623 si->eraseFromParent();
3624 return call_inst.getArgOperand(0);
3625 }
3626
Sebastien Hertz0d43d542013-02-27 19:02:16 +01003627 //==- Constructor barrier-----------------------------------------------==//
3628 case IntrinsicHelper::ConstructorBarrier: {
3629 irb_.CreateMemoryBarrier(art::kStoreStore);
3630 return NULL;
3631 }
3632
Logan Chien75e4b602012-07-23 14:24:12 -07003633 //==- Unknown Cases ----------------------------------------------------==//
3634 case IntrinsicHelper::MaxIntrinsicId:
3635 case IntrinsicHelper::UnknownId:
3636 //default:
3637 // NOTE: "default" is intentionally commented so that C/C++ compiler will
3638 // give some warning on unmatched cases.
3639 // NOTE: We should not implement these cases.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003640 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003641 }
Logan Chien75e4b602012-07-23 14:24:12 -07003642 UNIMPLEMENTED(FATAL) << "Unexpected GBC intrinsic: " << static_cast<int>(intr_id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003643 return NULL;
3644}
3645
3646} // anonymous namespace
3647
3648namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -08003649namespace llvm {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003650
Ian Rogers4c1c2832013-03-04 18:30:13 -08003651::llvm::FunctionPass*
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003652CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Ian Rogers89756f22013-03-04 16:40:02 -08003653 CompilerDriver* driver, DexCompilationUnit* dex_compilation_unit) {
3654 return new GBCExpanderPass(intrinsic_helper, irb, driver, dex_compilation_unit);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003655}
3656
Ian Rogers4c1c2832013-03-04 18:30:13 -08003657} // namespace llvm
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003658} // namespace art