blob: 4702b37de17bbd2da34ba2bbe47220c5f0930b22 [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 Rogers4f6ad8a2013-03-18 15:27:28 -070019#include "dex_file-inl.h"
Ian Rogers76ae4fe2013-02-27 16:03:41 -080020#include "intrinsic_helper.h"
Ian Rogers8e696052013-03-04 09:00:40 -080021#include "ir_builder.h"
Brian Carlstrom51c24672013-07-11 16:00:56 -070022#include "method_reference.h"
Ian Rogers98573f92013-01-30 17:26:32 -080023#include "mirror/abstract_method.h"
24#include "mirror/array.h"
Sebastien Hertz901d5ba2013-03-06 15:19:34 +010025#include "mirror/string.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070026#include "thread.h"
Ian Rogers8e696052013-03-04 09:00:40 -080027#include "utils_llvm.h"
TDYa1275e869b62012-07-25 00:45:39 -070028#include "verifier/method_verifier.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070029
buzbee311ca162013-02-28 15:56:43 -080030#include "compiler/dex/mir_graph.h"
buzbee395116c2013-02-27 14:30:25 -080031#include "compiler/dex/compiler_ir.h"
buzbee1fd33462013-03-25 13:40:45 -070032#include "compiler/dex/quick/mir_to_lir.h"
TDYa127920be7c2012-09-10 17:13:22 -070033using art::kMIRIgnoreNullCheck;
34using art::kMIRIgnoreRangeCheck;
35
Shih-wei Liao21d28f52012-06-12 05:55:00 -070036#include <llvm/ADT/STLExtras.h>
Brian Carlstrom37d48792013-03-22 14:14:45 -070037#include <llvm/IR/Intrinsics.h>
38#include <llvm/IR/Metadata.h>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070039#include <llvm/Pass.h>
40#include <llvm/Support/CFG.h>
41#include <llvm/Support/InstIterator.h>
42
43#include <vector>
TDYa127aa558872012-08-16 05:11:07 -070044#include <map>
45#include <utility>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070046
Ian Rogers4c1c2832013-03-04 18:30:13 -080047using namespace art::llvm;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070048
Ian Rogers4c1c2832013-03-04 18:30:13 -080049using art::llvm::IntrinsicHelper;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070050
Shih-wei Liaob2596522012-09-14 16:36:11 -070051namespace art {
buzbee26f10ee2012-12-21 11:16:29 -080052extern char RemapShorty(char shortyType);
Shih-wei Liaob2596522012-09-14 16:36:11 -070053};
54
Shih-wei Liao21d28f52012-06-12 05:55:00 -070055namespace {
56
57class GBCExpanderPass : public llvm::FunctionPass {
58 private:
59 const IntrinsicHelper& intrinsic_helper_;
60 IRBuilder& irb_;
61
62 llvm::LLVMContext& context_;
63 RuntimeSupportBuilder& rtb_;
64
65 private:
66 llvm::AllocaInst* shadow_frame_;
67 llvm::Value* old_shadow_frame_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070068
69 private:
Ian Rogers1212a022013-03-04 10:48:41 -080070 art::CompilerDriver* const driver_;
TDYa1275e869b62012-07-25 00:45:39 -070071
Ian Rogers89756f22013-03-04 16:40:02 -080072 const art::DexCompilationUnit* const dex_compilation_unit_;
TDYa1275e869b62012-07-25 00:45:39 -070073
TDYa1275e869b62012-07-25 00:45:39 -070074 llvm::Function* func_;
75
76 std::vector<llvm::BasicBlock*> basic_blocks_;
77
78 std::vector<llvm::BasicBlock*> basic_block_landing_pads_;
TDYa12755e5e6c2012-09-11 15:14:42 -070079 llvm::BasicBlock* current_bb_;
TDYa127aa558872012-08-16 05:11:07 -070080 std::map<llvm::BasicBlock*, std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> > >
81 landing_pad_phi_mapping_;
TDYa1275e869b62012-07-25 00:45:39 -070082 llvm::BasicBlock* basic_block_unwind_;
83
Sebastien Hertzf11afa02013-03-19 17:39:29 +010084 // Maps each vreg to its shadow frame address.
85 std::vector<llvm::Value*> shadow_frame_vreg_addresses_;
86
Logan Chien67645d82012-08-17 09:10:54 +080087 bool changed_;
88
TDYa1275e869b62012-07-25 00:45:39 -070089 private:
Shih-wei Liao21d28f52012-06-12 05:55:00 -070090 //----------------------------------------------------------------------------
Logan Chien75e4b602012-07-23 14:24:12 -070091 // Constant for GBC expansion
92 //----------------------------------------------------------------------------
93 enum IntegerShiftKind {
94 kIntegerSHL,
95 kIntegerSHR,
96 kIntegerUSHR,
97 };
98
99 private:
100 //----------------------------------------------------------------------------
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700101 // Helper function for GBC expansion
102 //----------------------------------------------------------------------------
103
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700104 llvm::Value* ExpandToRuntime(runtime_support::RuntimeId rt,
105 llvm::CallInst& inst);
106
TDYa1275e869b62012-07-25 00:45:39 -0700107 uint64_t LV2UInt(llvm::Value* lv) {
108 return llvm::cast<llvm::ConstantInt>(lv)->getZExtValue();
109 }
110
111 int64_t LV2SInt(llvm::Value* lv) {
112 return llvm::cast<llvm::ConstantInt>(lv)->getSExtValue();
113 }
114
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700115 private:
116 // TODO: Almost all Emit* are directly copy-n-paste from MethodCompiler.
117 // Refactor these utility functions from MethodCompiler to avoid forking.
118
Logan Chien67645d82012-08-17 09:10:54 +0800119 void EmitStackOverflowCheck(llvm::Instruction* first_non_alloca);
120
121 void RewriteFunction();
122
123 void RewriteBasicBlock(llvm::BasicBlock* original_block);
124
125 void UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
126 llvm::BasicBlock* new_basic_block);
127
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700128
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800129 // Sign or zero extend category 1 types < 32bits in size to 32bits.
130 llvm::Value* SignOrZeroExtendCat1Types(llvm::Value* value, JType jty);
131
132 // Truncate category 1 types from 32bits to the given JType size.
133 llvm::Value* TruncateCat1Types(llvm::Value* value, JType jty);
134
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700135 //----------------------------------------------------------------------------
136 // Dex cache code generation helper function
137 //----------------------------------------------------------------------------
TDYa127920be7c2012-09-10 17:13:22 -0700138 llvm::Value* EmitLoadDexCacheAddr(art::MemberOffset dex_cache_offset);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700139
140 llvm::Value* EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx);
141
142 llvm::Value* EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx);
143
144 llvm::Value* EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx);
145
146 llvm::Value* EmitLoadDexCacheStringFieldAddr(uint32_t string_idx);
147
148 //----------------------------------------------------------------------------
149 // Code generation helper function
150 //----------------------------------------------------------------------------
151 llvm::Value* EmitLoadMethodObjectAddr();
152
153 llvm::Value* EmitLoadArrayLength(llvm::Value* array);
154
155 llvm::Value* EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx);
156
157 llvm::Value* EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
158 llvm::Value* this_addr);
159
160 llvm::Value* EmitArrayGEP(llvm::Value* array_addr,
161 llvm::Value* index_value,
162 JType elem_jty);
163
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100164 //----------------------------------------------------------------------------
165 // Invoke helper function
166 //----------------------------------------------------------------------------
167 llvm::Value* EmitInvoke(llvm::CallInst& call_inst);
168
169 //----------------------------------------------------------------------------
170 // Inlining helper functions
171 //----------------------------------------------------------------------------
172 bool EmitIntrinsic(llvm::CallInst& call_inst, llvm::Value** result);
173
174 bool EmitIntrinsicStringLengthOrIsEmpty(llvm::CallInst& call_inst,
175 llvm::Value** result, bool is_empty);
176
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700177 private:
178 //----------------------------------------------------------------------------
179 // Expand Greenland intrinsics
180 //----------------------------------------------------------------------------
181 void Expand_TestSuspend(llvm::CallInst& call_inst);
182
TDYa1279a129452012-07-19 03:10:08 -0700183 void Expand_MarkGCCard(llvm::CallInst& call_inst);
184
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700185 llvm::Value* Expand_LoadStringFromDexCache(llvm::Value* string_idx_value);
186
187 llvm::Value* Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value);
188
189 void Expand_LockObject(llvm::Value* obj);
190
191 void Expand_UnlockObject(llvm::Value* obj);
192
193 llvm::Value* Expand_ArrayGet(llvm::Value* array_addr,
194 llvm::Value* index_value,
195 JType elem_jty);
196
197 void Expand_ArrayPut(llvm::Value* new_value,
198 llvm::Value* array_addr,
199 llvm::Value* index_value,
200 JType elem_jty);
201
202 void Expand_FilledNewArray(llvm::CallInst& call_inst);
203
204 llvm::Value* Expand_IGetFast(llvm::Value* field_offset_value,
205 llvm::Value* is_volatile_value,
206 llvm::Value* object_addr,
207 JType field_jty);
208
209 void Expand_IPutFast(llvm::Value* field_offset_value,
210 llvm::Value* is_volatile_value,
211 llvm::Value* object_addr,
212 llvm::Value* new_value,
213 JType field_jty);
214
215 llvm::Value* Expand_SGetFast(llvm::Value* static_storage_addr,
216 llvm::Value* field_offset_value,
217 llvm::Value* is_volatile_value,
218 JType field_jty);
219
220 void Expand_SPutFast(llvm::Value* static_storage_addr,
221 llvm::Value* field_offset_value,
222 llvm::Value* is_volatile_value,
223 llvm::Value* new_value,
224 JType field_jty);
225
226 llvm::Value* Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr);
227
228 llvm::Value* Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value);
229
230 llvm::Value*
231 Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value);
232
233 llvm::Value*
234 Expand_GetVirtualCalleeMethodObjAddrFast(llvm::Value* vtable_idx_value,
235 llvm::Value* this_addr);
236
237 llvm::Value* Expand_Invoke(llvm::CallInst& call_inst);
238
TDYa1274ec8ccd2012-08-11 07:04:57 -0700239 llvm::Value* Expand_DivRem(llvm::CallInst& call_inst, bool is_div, JType op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700240
TDYa127ce4cc0d2012-11-18 16:59:53 -0800241 void Expand_AllocaShadowFrame(llvm::Value* num_vregs_value);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700242
TDYa1278e950c12012-11-02 09:58:19 -0700243 void Expand_SetVReg(llvm::Value* entry_idx, llvm::Value* obj);
244
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700245 void Expand_PopShadowFrame();
246
247 void Expand_UpdateDexPC(llvm::Value* dex_pc_value);
248
TDYa127a1b21852012-07-23 03:20:39 -0700249 //----------------------------------------------------------------------------
250 // Quick
251 //----------------------------------------------------------------------------
252
253 llvm::Value* Expand_FPCompare(llvm::Value* src1_value,
254 llvm::Value* src2_value,
255 bool gt_bias);
256
257 llvm::Value* Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value);
258
259 llvm::Value* EmitCompareResultSelection(llvm::Value* cmp_eq,
260 llvm::Value* cmp_lt);
261
TDYa127f71bf5a2012-07-29 20:09:52 -0700262 llvm::Value* EmitLoadConstantClass(uint32_t dex_pc, uint32_t type_idx);
TDYa1275a26d442012-07-26 18:58:38 -0700263 llvm::Value* EmitLoadStaticStorage(uint32_t dex_pc, uint32_t type_idx);
264
TDYa1275e869b62012-07-25 00:45:39 -0700265 llvm::Value* Expand_HLIGet(llvm::CallInst& call_inst, JType field_jty);
266 void Expand_HLIPut(llvm::CallInst& call_inst, JType field_jty);
267
TDYa1275a26d442012-07-26 18:58:38 -0700268 llvm::Value* Expand_HLSget(llvm::CallInst& call_inst, JType field_jty);
269 void Expand_HLSput(llvm::CallInst& call_inst, JType field_jty);
270
271 llvm::Value* Expand_HLArrayGet(llvm::CallInst& call_inst, JType field_jty);
272 void Expand_HLArrayPut(llvm::CallInst& call_inst, JType field_jty);
273
TDYa127f71bf5a2012-07-29 20:09:52 -0700274 llvm::Value* Expand_ConstString(llvm::CallInst& call_inst);
275 llvm::Value* Expand_ConstClass(llvm::CallInst& call_inst);
276
277 void Expand_MonitorEnter(llvm::CallInst& call_inst);
278 void Expand_MonitorExit(llvm::CallInst& call_inst);
279
280 void Expand_HLCheckCast(llvm::CallInst& call_inst);
281 llvm::Value* Expand_InstanceOf(llvm::CallInst& call_inst);
282
283 llvm::Value* Expand_NewInstance(llvm::CallInst& call_inst);
284
285 llvm::Value* Expand_HLInvoke(llvm::CallInst& call_inst);
286
287 llvm::Value* Expand_OptArrayLength(llvm::CallInst& call_inst);
288 llvm::Value* Expand_NewArray(llvm::CallInst& call_inst);
289 llvm::Value* Expand_HLFilledNewArray(llvm::CallInst& call_inst);
290 void Expand_HLFillArrayData(llvm::CallInst& call_inst);
291
292 llvm::Value* EmitAllocNewArray(uint32_t dex_pc,
293 llvm::Value* array_length_value,
294 uint32_t type_idx,
295 bool is_filled_new_array);
296
297 llvm::Value* EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -0700298 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -0700299 llvm::Value* this_addr,
300 uint32_t dex_pc,
301 bool is_fast_path);
302
TDYa1275e869b62012-07-25 00:45:39 -0700303 void EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr);
304
305 void EmitUpdateDexPC(uint32_t dex_pc);
306
307 void EmitGuard_DivZeroException(uint32_t dex_pc,
308 llvm::Value* denominator,
309 JType op_jty);
310
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +0100311 void EmitGuard_NullPointerException(uint32_t dex_pc, llvm::Value* object,
312 int opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -0700313
314 void EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
315 llvm::Value* array,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +0100316 llvm::Value* index,
317 int opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -0700318
Ian Rogers8e696052013-03-04 09:00:40 -0800319 llvm::FunctionType* GetFunctionType(llvm::Type* ret_type, uint32_t method_idx, bool is_static);
TDYa1275e869b62012-07-25 00:45:39 -0700320
321 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
322
323 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
324 const char* postfix);
325
326 int32_t GetTryItemOffset(uint32_t dex_pc);
327
328 llvm::BasicBlock* GetLandingPadBasicBlock(uint32_t dex_pc);
329
330 llvm::BasicBlock* GetUnwindBasicBlock();
331
332 void EmitGuard_ExceptionLandingPad(uint32_t dex_pc);
333
334 void EmitBranchExceptionLandingPad(uint32_t dex_pc);
335
Logan Chien75e4b602012-07-23 14:24:12 -0700336 //----------------------------------------------------------------------------
337 // Expand Arithmetic Helper Intrinsics
338 //----------------------------------------------------------------------------
339
340 llvm::Value* Expand_IntegerShift(llvm::Value* src1_value,
341 llvm::Value* src2_value,
342 IntegerShiftKind kind,
343 JType op_jty);
344
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700345 public:
346 static char ID;
347
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700348 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Ian Rogers0d94eb62013-03-06 15:20:50 -0800349 art::CompilerDriver* driver, const art::DexCompilationUnit* dex_compilation_unit)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700350 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
351 context_(irb.getContext()), rtb_(irb.Runtime()),
TDYa1278e950c12012-11-02 09:58:19 -0700352 shadow_frame_(NULL), old_shadow_frame_(NULL),
Ian Rogers0d94eb62013-03-06 15:20:50 -0800353 driver_(driver),
Ian Rogers89756f22013-03-04 16:40:02 -0800354 dex_compilation_unit_(dex_compilation_unit),
Ian Rogers8e696052013-03-04 09:00:40 -0800355 func_(NULL), current_bb_(NULL), basic_block_unwind_(NULL), changed_(false) {}
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700356
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700357 bool runOnFunction(llvm::Function& func);
358
359 private:
Logan Chien67645d82012-08-17 09:10:54 +0800360 void InsertStackOverflowCheck(llvm::Function& func);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700361
362 llvm::Value* ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
363 llvm::CallInst& call_inst);
364
365};
366
367char GBCExpanderPass::ID = 0;
368
369bool GBCExpanderPass::runOnFunction(llvm::Function& func) {
Ian Rogers8e696052013-03-04 09:00:40 -0800370 VLOG(compiler) << "GBC expansion on " << func.getName().str();
371
TDYa127b672d1e2012-06-28 21:21:45 -0700372 // Runtime support or stub
Brian Carlstrom265091e2013-01-30 14:08:26 -0800373 if (dex_compilation_unit_ == NULL) {
TDYa127b672d1e2012-06-28 21:21:45 -0700374 return false;
375 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700376
Logan Chien67645d82012-08-17 09:10:54 +0800377 // Setup rewrite context
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700378 shadow_frame_ = NULL;
379 old_shadow_frame_ = NULL;
TDYa1275e869b62012-07-25 00:45:39 -0700380 func_ = &func;
Logan Chien67645d82012-08-17 09:10:54 +0800381 changed_ = false; // Assume unchanged
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700382
Sebastien Hertzf11afa02013-03-19 17:39:29 +0100383 shadow_frame_vreg_addresses_.resize(dex_compilation_unit_->GetCodeItem()->registers_size_, NULL);
Ian Rogers89756f22013-03-04 16:40:02 -0800384 basic_blocks_.resize(dex_compilation_unit_->GetCodeItem()->insns_size_in_code_units_);
385 basic_block_landing_pads_.resize(dex_compilation_unit_->GetCodeItem()->tries_size_, NULL);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700386 basic_block_unwind_ = NULL;
387 for (llvm::Function::iterator bb_iter = func_->begin(), bb_end = func_->end();
388 bb_iter != bb_end;
389 ++bb_iter) {
390 if (bb_iter->begin()->getMetadata("DexOff") == NULL) {
391 continue;
392 }
393 uint32_t dex_pc = LV2UInt(bb_iter->begin()->getMetadata("DexOff")->getOperand(0));
394 basic_blocks_[dex_pc] = bb_iter;
395 }
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700396
Logan Chien67645d82012-08-17 09:10:54 +0800397 // Insert stack overflow check
398 InsertStackOverflowCheck(func); // TODO: Use intrinsic.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700399
Logan Chien67645d82012-08-17 09:10:54 +0800400 // Rewrite the intrinsics
401 RewriteFunction();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700402
403 VERIFY_LLVM_FUNCTION(func);
404
Logan Chien67645d82012-08-17 09:10:54 +0800405 return changed_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700406}
407
Logan Chien67645d82012-08-17 09:10:54 +0800408void GBCExpanderPass::RewriteBasicBlock(llvm::BasicBlock* original_block) {
409 llvm::BasicBlock* curr_basic_block = original_block;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700410
Logan Chien67645d82012-08-17 09:10:54 +0800411 llvm::BasicBlock::iterator inst_iter = original_block->begin();
412 llvm::BasicBlock::iterator inst_end = original_block->end();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700413
Logan Chien67645d82012-08-17 09:10:54 +0800414 while (inst_iter != inst_end) {
415 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst_iter);
416 IntrinsicHelper::IntrinsicId intr_id = IntrinsicHelper::UnknownId;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700417
Logan Chien67645d82012-08-17 09:10:54 +0800418 if (call_inst) {
419 llvm::Function* callee_func = call_inst->getCalledFunction();
420 intr_id = intrinsic_helper_.GetIntrinsicId(callee_func);
421 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700422
Logan Chien67645d82012-08-17 09:10:54 +0800423 if (intr_id == IntrinsicHelper::UnknownId) {
424 // This is not intrinsic call. Skip this instruction.
425 ++inst_iter;
426 continue;
427 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700428
Logan Chien67645d82012-08-17 09:10:54 +0800429 // Rewrite the intrinsic and change the function
430 changed_ = true;
431 irb_.SetInsertPoint(inst_iter);
432
433 // Expand the intrinsic
434 if (llvm::Value* new_value = ExpandIntrinsic(intr_id, *call_inst)) {
435 inst_iter->replaceAllUsesWith(new_value);
436 }
437
438 // Remove the old intrinsic call instruction
439 llvm::BasicBlock::iterator old_inst = inst_iter++;
440 old_inst->eraseFromParent();
441
442 // Splice the instruction to the new basic block
443 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
444 if (next_basic_block != curr_basic_block) {
445 next_basic_block->getInstList().splice(
446 irb_.GetInsertPoint(), curr_basic_block->getInstList(),
447 inst_iter, inst_end);
448 curr_basic_block = next_basic_block;
449 inst_end = curr_basic_block->end();
450 }
451 }
452}
453
454
455void GBCExpanderPass::RewriteFunction() {
456 size_t num_basic_blocks = func_->getBasicBlockList().size();
457 // NOTE: We are not using (bb_iter != bb_end) as the for-loop condition,
458 // because we will create new basic block while expanding the intrinsics.
459 // We only want to iterate through the input basic blocks.
460
TDYa127aa558872012-08-16 05:11:07 -0700461 landing_pad_phi_mapping_.clear();
462
Logan Chien67645d82012-08-17 09:10:54 +0800463 for (llvm::Function::iterator bb_iter = func_->begin();
464 num_basic_blocks > 0; ++bb_iter, --num_basic_blocks) {
Shih-wei Liao627d8c42012-08-24 08:31:18 -0700465 // Set insert point to current basic block.
466 irb_.SetInsertPoint(bb_iter);
Logan Chien67645d82012-08-17 09:10:54 +0800467
TDYa12755e5e6c2012-09-11 15:14:42 -0700468 current_bb_ = bb_iter;
TDYa127aa558872012-08-16 05:11:07 -0700469
Logan Chien67645d82012-08-17 09:10:54 +0800470 // Rewrite the basic block
471 RewriteBasicBlock(bb_iter);
472
473 // Update the phi-instructions in the successor basic block
474 llvm::BasicBlock* last_block = irb_.GetInsertBlock();
475 if (last_block != bb_iter) {
476 UpdatePhiInstruction(bb_iter, last_block);
477 }
478 }
TDYa127aa558872012-08-16 05:11:07 -0700479
480 typedef std::map<llvm::PHINode*, llvm::PHINode*> HandlerPHIMap;
481 HandlerPHIMap handler_phi;
482 // Iterate every used landing pad basic block
483 for (size_t i = 0, ei = basic_block_landing_pads_.size(); i != ei; ++i) {
484 llvm::BasicBlock* lbb = basic_block_landing_pads_[i];
485 if (lbb == NULL) {
486 continue;
487 }
488
489 llvm::TerminatorInst* term_inst = lbb->getTerminator();
490 std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> >& rewrite_pair
491 = landing_pad_phi_mapping_[lbb];
492 irb_.SetInsertPoint(lbb->begin());
493
494 // Iterate every succeeding basic block (catch block)
495 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
496 succ_iter != succ_end; ++succ_iter) {
497 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
498
499 // Iterate every phi instructions in the succeeding basic block
500 for (llvm::BasicBlock::iterator
501 inst_iter = succ_basic_block->begin(),
502 inst_end = succ_basic_block->end();
503 inst_iter != inst_end; ++inst_iter) {
504 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
505
506 if (!phi) {
507 break; // Meet non-phi instruction. Done.
508 }
509
510 if (handler_phi[phi] == NULL) {
511 handler_phi[phi] = llvm::PHINode::Create(phi->getType(), 1);
512 }
513
514 // Create new_phi in landing pad
515 llvm::PHINode* new_phi = irb_.CreatePHI(phi->getType(), rewrite_pair.size());
516 // Insert all incoming value into new_phi by rewrite_pair
517 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
518 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
519 llvm::BasicBlock* new_bb = rewrite_pair[j].second;
520 new_phi->addIncoming(phi->getIncomingValueForBlock(old_bb), new_bb);
521 }
522 // Delete all incoming value from phi by rewrite_pair
523 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
524 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
525 int old_bb_idx = phi->getBasicBlockIndex(old_bb);
526 if (old_bb_idx >= 0) {
527 phi->removeIncomingValue(old_bb_idx, false);
528 }
529 }
530 // Insert new_phi into new handler phi
531 handler_phi[phi]->addIncoming(new_phi, lbb);
532 }
533 }
534 }
535
536 // Replace all handler phi
537 // We can't just use the old handler phi, because some exception edges will disappear after we
538 // compute fast-path.
539 for (HandlerPHIMap::iterator it = handler_phi.begin(); it != handler_phi.end(); ++it) {
540 llvm::PHINode* old_phi = it->first;
541 llvm::PHINode* new_phi = it->second;
542 new_phi->insertBefore(old_phi);
543 old_phi->replaceAllUsesWith(new_phi);
544 old_phi->eraseFromParent();
545 }
Logan Chien67645d82012-08-17 09:10:54 +0800546}
547
548void GBCExpanderPass::UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
549 llvm::BasicBlock* new_basic_block) {
550 llvm::TerminatorInst* term_inst = new_basic_block->getTerminator();
551
552 if (!term_inst) {
553 return; // No terminating instruction in new_basic_block. Nothing to do.
554 }
555
556 // Iterate every succeeding basic block
557 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
558 succ_iter != succ_end; ++succ_iter) {
559 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
560
561 // Iterate every phi instructions in the succeeding basic block
562 for (llvm::BasicBlock::iterator
563 inst_iter = succ_basic_block->begin(),
564 inst_end = succ_basic_block->end();
565 inst_iter != inst_end; ++inst_iter) {
566 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
567
568 if (!phi) {
569 break; // Meet non-phi instruction. Done.
570 }
571
572 // Update the incoming block of this phi instruction
573 for (llvm::PHINode::block_iterator
574 ibb_iter = phi->block_begin(), ibb_end = phi->block_end();
575 ibb_iter != ibb_end; ++ibb_iter) {
576 if (*ibb_iter == old_basic_block) {
577 *ibb_iter = new_basic_block;
578 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700579 }
580 }
581 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700582}
583
584llvm::Value* GBCExpanderPass::ExpandToRuntime(runtime_support::RuntimeId rt,
585 llvm::CallInst& inst) {
586 // Some GBC intrinsic can directly replace with IBC runtime. "Directly" means
587 // the arguments passed to the GBC intrinsic are as the same as IBC runtime
588 // function, therefore only called function is needed to change.
589 unsigned num_args = inst.getNumArgOperands();
590
591 if (num_args <= 0) {
592 return irb_.CreateCall(irb_.GetRuntime(rt));
593 } else {
594 std::vector<llvm::Value*> args;
595 for (unsigned i = 0; i < num_args; i++) {
596 args.push_back(inst.getArgOperand(i));
597 }
598
599 return irb_.CreateCall(irb_.GetRuntime(rt), args);
600 }
601}
602
Logan Chien67645d82012-08-17 09:10:54 +0800603void
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700604GBCExpanderPass::EmitStackOverflowCheck(llvm::Instruction* first_non_alloca) {
605 llvm::Function* func = first_non_alloca->getParent()->getParent();
606 llvm::Module* module = func->getParent();
607
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700608 // Call llvm intrinsic function to get frame address.
609 llvm::Function* frameaddress =
610 llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
611
612 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
613 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
614
615 // Cast i8* to int
616 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
617
618 // Get thread.stack_end_
619 llvm::Value* stack_end =
TDYa127920be7c2012-09-10 17:13:22 -0700620 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::StackEndOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700621 irb_.getPtrEquivIntTy(),
622 kTBAARuntimeInfo);
623
624 // Check the frame address < thread.stack_end_ ?
625 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
626
627 llvm::BasicBlock* block_exception =
628 llvm::BasicBlock::Create(context_, "stack_overflow", func);
629
630 llvm::BasicBlock* block_continue =
631 llvm::BasicBlock::Create(context_, "stack_overflow_cont", func);
632
633 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
634
635 // If stack overflow, throw exception.
636 irb_.SetInsertPoint(block_exception);
637 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowStackOverflowException));
638
639 // Unwind.
640 llvm::Type* ret_type = func->getReturnType();
641 if (ret_type->isVoidTy()) {
642 irb_.CreateRetVoid();
643 } else {
644 // The return value is ignored when there's an exception. MethodCompiler
645 // returns zero value under the the corresponding return type in this case.
646 // GBCExpander returns LLVM undef value here for brevity
647 irb_.CreateRet(llvm::UndefValue::get(ret_type));
648 }
649
650 irb_.SetInsertPoint(block_continue);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700651}
652
TDYa127920be7c2012-09-10 17:13:22 -0700653llvm::Value* GBCExpanderPass::EmitLoadDexCacheAddr(art::MemberOffset offset) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700654 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
655
656 return irb_.LoadFromObjectOffset(method_object_addr,
657 offset.Int32Value(),
658 irb_.getJObjectTy(),
659 kTBAAConstJObject);
660}
661
662llvm::Value*
663GBCExpanderPass::EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
664 llvm::Value* static_storage_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800665 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheInitializedStaticStorageOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700666
667 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
668
669 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
670}
671
672llvm::Value*
673GBCExpanderPass::EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
674 llvm::Value* resolved_type_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800675 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedTypesOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700676
677 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
678
679 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
680}
681
682llvm::Value* GBCExpanderPass::
683EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
684 llvm::Value* resolved_method_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800685 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedMethodsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700686
687 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
688
689 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
690}
691
692llvm::Value* GBCExpanderPass::
693EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
694 llvm::Value* string_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800695 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheStringsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700696
697 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
698
699 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
700}
701
702llvm::Value* GBCExpanderPass::EmitLoadMethodObjectAddr() {
703 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
704 return parent_func->arg_begin();
705}
706
707llvm::Value* GBCExpanderPass::EmitLoadArrayLength(llvm::Value* array) {
708 // Load array length
709 return irb_.LoadFromObjectOffset(array,
Ian Rogers98573f92013-01-30 17:26:32 -0800710 art::mirror::Array::LengthOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700711 irb_.getJIntTy(),
712 kTBAAConstJObject);
713
714}
715
716llvm::Value*
717GBCExpanderPass::EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
718 llvm::Value* callee_method_object_field_addr =
719 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
720
TDYa127ce4cc0d2012-11-18 16:59:53 -0800721 return irb_.CreateLoad(callee_method_object_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700722}
723
724llvm::Value* GBCExpanderPass::
725EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, llvm::Value* this_addr) {
726 // Load class object of *this* pointer
727 llvm::Value* class_object_addr =
728 irb_.LoadFromObjectOffset(this_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800729 art::mirror::Object::ClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700730 irb_.getJObjectTy(),
731 kTBAAConstJObject);
732
733 // Load vtable address
734 llvm::Value* vtable_addr =
735 irb_.LoadFromObjectOffset(class_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800736 art::mirror::Class::VTableOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700737 irb_.getJObjectTy(),
738 kTBAAConstJObject);
739
740 // Load callee method object
741 llvm::Value* vtable_idx_value =
742 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
743
744 llvm::Value* method_field_addr =
745 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
746
747 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
748}
749
750// Emit Array GetElementPtr
751llvm::Value* GBCExpanderPass::EmitArrayGEP(llvm::Value* array_addr,
752 llvm::Value* index_value,
753 JType elem_jty) {
754
755 int data_offset;
756 if (elem_jty == kLong || elem_jty == kDouble ||
Ian Rogers98573f92013-01-30 17:26:32 -0800757 (elem_jty == kObject && sizeof(uint64_t) == sizeof(art::mirror::Object*))) {
758 data_offset = art::mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700759 } else {
Ian Rogers98573f92013-01-30 17:26:32 -0800760 data_offset = art::mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700761 }
762
763 llvm::Constant* data_offset_value =
764 irb_.getPtrEquivInt(data_offset);
765
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800766 llvm::Type* elem_type = irb_.getJType(elem_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700767
768 llvm::Value* array_data_addr =
769 irb_.CreatePtrDisp(array_addr, data_offset_value,
770 elem_type->getPointerTo());
771
772 return irb_.CreateGEP(array_data_addr, index_value);
773}
774
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100775llvm::Value* GBCExpanderPass::EmitInvoke(llvm::CallInst& call_inst) {
776 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
777 art::InvokeType invoke_type =
778 static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
779 bool is_static = (invoke_type == art::kStatic);
Brian Carlstrom51c24672013-07-11 16:00:56 -0700780 art::MethodReference target_method(dex_compilation_unit_->GetDexFile(),
781 LV2UInt(call_inst.getArgOperand(1)));
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100782
783 // Load *this* actual parameter
784 llvm::Value* this_addr = (!is_static) ? call_inst.getArgOperand(3) : NULL;
785
786 // Compute invoke related information for compiler decision
787 int vtable_idx = -1;
788 uintptr_t direct_code = 0;
789 uintptr_t direct_method = 0;
Ian Rogerse3cd2f02013-05-24 15:32:56 -0700790 bool is_fast_path = driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc,
791 invoke_type, target_method,
792 vtable_idx,
793 direct_code, direct_method,
794 true);
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100795 // Load the method object
796 llvm::Value* callee_method_object_addr = NULL;
797
798 if (!is_fast_path) {
799 callee_method_object_addr =
Ian Rogerse3cd2f02013-05-24 15:32:56 -0700800 EmitCallRuntimeForCalleeMethodObjectAddr(target_method.dex_method_index, invoke_type,
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100801 this_addr, dex_pc, is_fast_path);
802 } else {
803 switch (invoke_type) {
804 case art::kStatic:
805 case art::kDirect:
806 if (direct_method != 0u &&
807 direct_method != static_cast<uintptr_t>(-1)) {
808 callee_method_object_addr =
809 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
810 irb_.getJObjectTy());
811 } else {
812 callee_method_object_addr =
Ian Rogerse3cd2f02013-05-24 15:32:56 -0700813 EmitLoadSDCalleeMethodObjectAddr(target_method.dex_method_index);
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100814 }
815 break;
816
817 case art::kVirtual:
818 DCHECK(vtable_idx != -1);
819 callee_method_object_addr =
820 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
821 break;
822
823 case art::kSuper:
824 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
825 "the fast path.";
826 break;
827
828 case art::kInterface:
829 callee_method_object_addr =
Ian Rogerse3cd2f02013-05-24 15:32:56 -0700830 EmitCallRuntimeForCalleeMethodObjectAddr(target_method.dex_method_index,
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100831 invoke_type, this_addr,
832 dex_pc, is_fast_path);
833 break;
834 }
835 }
836
837 // Load the actual parameter
838 std::vector<llvm::Value*> args;
839
840 args.push_back(callee_method_object_addr); // method object for callee
841
842 for (uint32_t i = 3; i < call_inst.getNumArgOperands(); ++i) {
843 args.push_back(call_inst.getArgOperand(i));
844 }
845
846 llvm::Value* code_addr;
847 llvm::Type* func_type = GetFunctionType(call_inst.getType(),
Ian Rogerse3cd2f02013-05-24 15:32:56 -0700848 target_method.dex_method_index, is_static);
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100849 if (direct_code != 0u && direct_code != static_cast<uintptr_t>(-1)) {
850 code_addr =
851 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_code),
852 func_type->getPointerTo());
853 } else {
854 code_addr =
855 irb_.LoadFromObjectOffset(callee_method_object_addr,
Jeff Haoaa4a7932013-05-13 11:28:27 -0700856 art::mirror::AbstractMethod::GetEntryPointFromCompiledCodeOffset().Int32Value(),
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100857 func_type->getPointerTo(), kTBAARuntimeInfo);
858 }
859
860 // Invoke callee
861 EmitUpdateDexPC(dex_pc);
862 llvm::Value* retval = irb_.CreateCall(code_addr, args);
863 EmitGuard_ExceptionLandingPad(dex_pc);
864
865 return retval;
866}
867
868bool GBCExpanderPass::EmitIntrinsic(llvm::CallInst& call_inst,
869 llvm::Value** result) {
870 DCHECK(result != NULL);
871
872 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
873 std::string callee_method_name(
874 PrettyMethod(callee_method_idx, *dex_compilation_unit_->GetDexFile()));
875
876 if (callee_method_name == "int java.lang.String.length()") {
877 return EmitIntrinsicStringLengthOrIsEmpty(call_inst, result,
878 false /* is_empty */);
879 }
880 if (callee_method_name == "boolean java.lang.String.isEmpty()") {
881 return EmitIntrinsicStringLengthOrIsEmpty(call_inst, result,
882 true /* is_empty */);
883 }
884
885 *result = NULL;
886 return false;
887}
888
889bool GBCExpanderPass::EmitIntrinsicStringLengthOrIsEmpty(llvm::CallInst& call_inst,
890 llvm::Value** result,
891 bool is_empty) {
892 art::InvokeType invoke_type =
893 static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
894 DCHECK_NE(invoke_type, art::kStatic);
895 DCHECK_EQ(call_inst.getNumArgOperands(), 4U);
896
897 llvm::Value* this_object = call_inst.getArgOperand(3);
898 llvm::Value* string_count =
899 irb_.LoadFromObjectOffset(this_object,
900 art::mirror::String::CountOffset().Int32Value(),
901 irb_.getJIntTy(),
902 kTBAAConstJObject);
903 if (is_empty) {
904 llvm::Value* count_equals_zero = irb_.CreateICmpEQ(string_count,
905 irb_.getJInt(0));
906 llvm::Value* is_empty = irb_.CreateSelect(count_equals_zero,
907 irb_.getJBoolean(true),
908 irb_.getJBoolean(false));
909 is_empty = SignOrZeroExtendCat1Types(is_empty, kBoolean);
910 *result = is_empty;
911 } else {
912 *result = string_count;
913 }
914 return true;
915}
916
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700917void GBCExpanderPass::Expand_TestSuspend(llvm::CallInst& call_inst) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800918 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
919
920 llvm::Value* suspend_count =
921 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::ThreadFlagsOffset().Int32Value(),
922 irb_.getInt16Ty(),
923 kTBAARuntimeInfo);
924 llvm::Value* is_suspend = irb_.CreateICmpNE(suspend_count, irb_.getInt16(0));
925
926 llvm::BasicBlock* basic_block_suspend = CreateBasicBlockWithDexPC(dex_pc, "suspend");
927 llvm::BasicBlock* basic_block_cont = CreateBasicBlockWithDexPC(dex_pc, "suspend_cont");
928
929 irb_.CreateCondBr(is_suspend, basic_block_suspend, basic_block_cont, kUnlikely);
930
931 irb_.SetInsertPoint(basic_block_suspend);
932 if (dex_pc != art::DexFile::kDexNoIndex) {
933 EmitUpdateDexPC(dex_pc);
934 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700935 irb_.Runtime().EmitTestSuspend();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800936
937 llvm::BasicBlock* basic_block_exception = CreateBasicBlockWithDexPC(dex_pc, "exception");
938 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
939 irb_.CreateCondBr(exception_pending, basic_block_exception, basic_block_cont, kUnlikely);
940
941 irb_.SetInsertPoint(basic_block_exception);
942 llvm::Type* ret_type = call_inst.getParent()->getParent()->getReturnType();
943 if (ret_type->isVoidTy()) {
944 irb_.CreateRetVoid();
945 } else {
946 // The return value is ignored when there's an exception.
947 irb_.CreateRet(llvm::UndefValue::get(ret_type));
948 }
TDYa127ce4cc0d2012-11-18 16:59:53 -0800949
950 irb_.SetInsertPoint(basic_block_cont);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700951 return;
952}
953
TDYa1279a129452012-07-19 03:10:08 -0700954void GBCExpanderPass::Expand_MarkGCCard(llvm::CallInst& call_inst) {
TDYa1279a129452012-07-19 03:10:08 -0700955 irb_.Runtime().EmitMarkGCCard(call_inst.getArgOperand(0), call_inst.getArgOperand(1));
TDYa1279a129452012-07-19 03:10:08 -0700956 return;
957}
958
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700959llvm::Value*
960GBCExpanderPass::Expand_LoadStringFromDexCache(llvm::Value* string_idx_value) {
961 uint32_t string_idx =
962 llvm::cast<llvm::ConstantInt>(string_idx_value)->getZExtValue();
963
964 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
965
TDYa127ce4cc0d2012-11-18 16:59:53 -0800966 return irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700967}
968
969llvm::Value*
970GBCExpanderPass::Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value) {
971 uint32_t type_idx =
972 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
973
974 llvm::Value* type_field_addr =
975 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
976
TDYa127ce4cc0d2012-11-18 16:59:53 -0800977 return irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700978}
979
980void GBCExpanderPass::Expand_LockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700981 rtb_.EmitLockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700982 return;
983}
984
985void GBCExpanderPass::Expand_UnlockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700986 rtb_.EmitUnlockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700987 return;
988}
989
990llvm::Value* GBCExpanderPass::Expand_ArrayGet(llvm::Value* array_addr,
991 llvm::Value* index_value,
992 JType elem_jty) {
993 llvm::Value* array_elem_addr =
994 EmitArrayGEP(array_addr, index_value, elem_jty);
995
996 return irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
997}
998
999void GBCExpanderPass::Expand_ArrayPut(llvm::Value* new_value,
1000 llvm::Value* array_addr,
1001 llvm::Value* index_value,
1002 JType elem_jty) {
1003 llvm::Value* array_elem_addr =
1004 EmitArrayGEP(array_addr, index_value, elem_jty);
1005
1006 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1007
1008 return;
1009}
1010
1011void GBCExpanderPass::Expand_FilledNewArray(llvm::CallInst& call_inst) {
1012 // Most of the codes refer to MethodCompiler::EmitInsn_FilledNewArray
1013 llvm::Value* array = call_inst.getArgOperand(0);
1014
1015 uint32_t element_jty =
1016 llvm::cast<llvm::ConstantInt>(call_inst.getArgOperand(1))->getZExtValue();
1017
1018 DCHECK(call_inst.getNumArgOperands() > 2);
1019 unsigned num_elements = (call_inst.getNumArgOperands() - 2);
1020
1021 bool is_elem_int_ty = (static_cast<JType>(element_jty) == kInt);
1022
1023 uint32_t alignment;
1024 llvm::Constant* elem_size;
1025 llvm::PointerType* field_type;
1026
1027 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1028 // as the element, thus we are only checking 2 cases: primitive int and
1029 // non-primitive type.
1030 if (is_elem_int_ty) {
1031 alignment = sizeof(int32_t);
1032 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
1033 field_type = irb_.getJIntTy()->getPointerTo();
1034 } else {
1035 alignment = irb_.getSizeOfPtrEquivInt();
1036 elem_size = irb_.getSizeOfPtrEquivIntValue();
1037 field_type = irb_.getJObjectTy()->getPointerTo();
1038 }
1039
1040 llvm::Value* data_field_offset =
Ian Rogers98573f92013-01-30 17:26:32 -08001041 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001042
1043 llvm::Value* data_field_addr =
1044 irb_.CreatePtrDisp(array, data_field_offset, field_type);
1045
1046 for (unsigned i = 0; i < num_elements; ++i) {
1047 // Values to fill the array begin at the 3rd argument
1048 llvm::Value* reg_value = call_inst.getArgOperand(2 + i);
1049
1050 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
1051
1052 data_field_addr =
1053 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
1054 }
1055
1056 return;
1057}
1058
1059llvm::Value* GBCExpanderPass::Expand_IGetFast(llvm::Value* field_offset_value,
1060 llvm::Value* /*is_volatile_value*/,
1061 llvm::Value* object_addr,
1062 JType field_jty) {
1063 int field_offset =
1064 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1065
1066 DCHECK_GE(field_offset, 0);
1067
1068 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001069 irb_.getJType(field_jty)->getPointerTo();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001070
1071 field_offset_value = irb_.getPtrEquivInt(field_offset);
1072
1073 llvm::Value* field_addr =
1074 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1075
1076 // TODO: Check is_volatile. We need to generate atomic load instruction
1077 // when is_volatile is true.
1078 return irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
1079}
1080
1081void GBCExpanderPass::Expand_IPutFast(llvm::Value* field_offset_value,
1082 llvm::Value* /* is_volatile_value */,
1083 llvm::Value* object_addr,
1084 llvm::Value* new_value,
1085 JType field_jty) {
1086 int field_offset =
1087 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1088
1089 DCHECK_GE(field_offset, 0);
1090
1091 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001092 irb_.getJType(field_jty)->getPointerTo();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001093
1094 field_offset_value = irb_.getPtrEquivInt(field_offset);
1095
1096 llvm::Value* field_addr =
1097 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1098
1099 // TODO: Check is_volatile. We need to generate atomic store instruction
1100 // when is_volatile is true.
1101 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1102
1103 return;
1104}
1105
1106llvm::Value* GBCExpanderPass::Expand_SGetFast(llvm::Value* static_storage_addr,
1107 llvm::Value* field_offset_value,
1108 llvm::Value* /*is_volatile_value*/,
1109 JType field_jty) {
1110 int field_offset =
1111 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1112
1113 DCHECK_GE(field_offset, 0);
1114
1115 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1116
1117 llvm::Value* static_field_addr =
1118 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001119 irb_.getJType(field_jty)->getPointerTo());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001120
1121 // TODO: Check is_volatile. We need to generate atomic store instruction
1122 // when is_volatile is true.
1123 return irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
1124}
1125
1126void GBCExpanderPass::Expand_SPutFast(llvm::Value* static_storage_addr,
1127 llvm::Value* field_offset_value,
1128 llvm::Value* /* is_volatile_value */,
1129 llvm::Value* new_value,
1130 JType field_jty) {
1131 int field_offset =
1132 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1133
1134 DCHECK_GE(field_offset, 0);
1135
1136 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1137
1138 llvm::Value* static_field_addr =
1139 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001140 irb_.getJType(field_jty)->getPointerTo());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001141
1142 // TODO: Check is_volatile. We need to generate atomic store instruction
1143 // when is_volatile is true.
1144 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1145
1146 return;
1147}
1148
1149llvm::Value*
1150GBCExpanderPass::Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr) {
1151 return irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001152 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001153 irb_.getJObjectTy(),
1154 kTBAAConstJObject);
1155}
1156
1157llvm::Value*
1158GBCExpanderPass::Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value) {
1159 uint32_t type_idx =
1160 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
1161
1162 llvm::Value* storage_field_addr =
1163 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1164
TDYa127ce4cc0d2012-11-18 16:59:53 -08001165 return irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001166}
1167
1168llvm::Value*
1169GBCExpanderPass::Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value) {
1170 uint32_t callee_method_idx =
1171 llvm::cast<llvm::ConstantInt>(callee_method_idx_value)->getZExtValue();
1172
1173 return EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
1174}
1175
1176llvm::Value* GBCExpanderPass::Expand_GetVirtualCalleeMethodObjAddrFast(
1177 llvm::Value* vtable_idx_value,
1178 llvm::Value* this_addr) {
1179 int vtable_idx =
1180 llvm::cast<llvm::ConstantInt>(vtable_idx_value)->getSExtValue();
1181
1182 return EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
1183}
1184
1185llvm::Value* GBCExpanderPass::Expand_Invoke(llvm::CallInst& call_inst) {
1186 // Most of the codes refer to MethodCompiler::EmitInsn_Invoke
1187 llvm::Value* callee_method_object_addr = call_inst.getArgOperand(0);
1188 unsigned num_args = call_inst.getNumArgOperands();
1189 llvm::Type* ret_type = call_inst.getType();
1190
1191 // Determine the function type of the callee method
1192 std::vector<llvm::Type*> args_type;
1193 std::vector<llvm::Value*> args;
1194 for (unsigned i = 0; i < num_args; i++) {
1195 args.push_back(call_inst.getArgOperand(i));
1196 args_type.push_back(args[i]->getType());
1197 }
1198
1199 llvm::FunctionType* callee_method_type =
1200 llvm::FunctionType::get(ret_type, args_type, false);
1201
1202 llvm::Value* code_addr =
1203 irb_.LoadFromObjectOffset(callee_method_object_addr,
Jeff Haoaa4a7932013-05-13 11:28:27 -07001204 art::mirror::AbstractMethod::GetEntryPointFromCompiledCodeOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001205 callee_method_type->getPointerTo(),
TDYa127ce4cc0d2012-11-18 16:59:53 -08001206 kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001207
1208 // Invoke callee
1209 llvm::Value* retval = irb_.CreateCall(code_addr, args);
1210
1211 return retval;
1212}
1213
TDYa1274ec8ccd2012-08-11 07:04:57 -07001214llvm::Value* GBCExpanderPass::Expand_DivRem(llvm::CallInst& call_inst,
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001215 bool is_div, JType op_jty) {
TDYa1274ec8ccd2012-08-11 07:04:57 -07001216 llvm::Value* dividend = call_inst.getArgOperand(0);
1217 llvm::Value* divisor = call_inst.getArgOperand(1);
TDYa1274ec8ccd2012-08-11 07:04:57 -07001218 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1219 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001220 // Most of the codes refer to MethodCompiler::EmitIntDivRemResultComputation
1221
1222 // Check the special case: MININT / -1 = MININT
1223 // That case will cause overflow, which is undefined behavior in llvm.
1224 // So we check the divisor is -1 or not, if the divisor is -1, we do
1225 // the special path to avoid undefined behavior.
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001226 llvm::Type* op_type = irb_.getJType(op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001227 llvm::Value* zero = irb_.getJZero(op_jty);
1228 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
1229
TDYa1275e869b62012-07-25 00:45:39 -07001230 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001231 llvm::BasicBlock* eq_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1232 llvm::BasicBlock* ne_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1233 llvm::BasicBlock* neg_one_cont =
1234 llvm::BasicBlock::Create(context_, "", parent);
1235
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001236 llvm::Value* is_equal_neg_one = irb_.CreateICmpEQ(divisor, neg_one);
1237 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
1238
1239 // If divisor == -1
1240 irb_.SetInsertPoint(eq_neg_one);
1241 llvm::Value* eq_result;
1242 if (is_div) {
1243 // We can just change from "dividend div -1" to "neg dividend". The sub
1244 // don't care the sign/unsigned because of two's complement representation.
1245 // And the behavior is what we want:
1246 // -(2^n) (2^n)-1
1247 // MININT < k <= MAXINT -> mul k -1 = -k
1248 // MININT == k -> mul k -1 = k
1249 //
1250 // LLVM use sub to represent 'neg'
1251 eq_result = irb_.CreateSub(zero, dividend);
1252 } else {
1253 // Everything modulo -1 will be 0.
1254 eq_result = zero;
1255 }
1256 irb_.CreateBr(neg_one_cont);
1257
1258 // If divisor != -1, just do the division.
1259 irb_.SetInsertPoint(ne_neg_one);
1260 llvm::Value* ne_result;
1261 if (is_div) {
1262 ne_result = irb_.CreateSDiv(dividend, divisor);
1263 } else {
1264 ne_result = irb_.CreateSRem(dividend, divisor);
1265 }
1266 irb_.CreateBr(neg_one_cont);
1267
1268 irb_.SetInsertPoint(neg_one_cont);
1269 llvm::PHINode* result = irb_.CreatePHI(op_type, 2);
1270 result->addIncoming(eq_result, eq_neg_one);
1271 result->addIncoming(ne_result, ne_neg_one);
1272
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001273 return result;
1274}
1275
TDYa127ce4cc0d2012-11-18 16:59:53 -08001276void GBCExpanderPass::Expand_AllocaShadowFrame(llvm::Value* num_vregs_value) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001277 // Most of the codes refer to MethodCompiler::EmitPrologueAllocShadowFrame and
1278 // MethodCompiler::EmitPushShadowFrame
TDYa1278e950c12012-11-02 09:58:19 -07001279 uint16_t num_vregs =
1280 llvm::cast<llvm::ConstantInt>(num_vregs_value)->getZExtValue();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001281
1282 llvm::StructType* shadow_frame_type =
TDYa127ce4cc0d2012-11-18 16:59:53 -08001283 irb_.getShadowFrameTy(num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001284
Sebastien Hertz77209702013-02-28 16:34:13 +01001285 // Create allocas at the start of entry block.
1286 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1287 llvm::BasicBlock* entry_block = &func_->front();
1288 irb_.SetInsertPoint(&entry_block->front());
1289
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001290 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
1291
1292 // Alloca a pointer to old shadow frame
1293 old_shadow_frame_ =
1294 irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
1295
Sebastien Hertz77209702013-02-28 16:34:13 +01001296 irb_.restoreIP(irb_ip_original);
1297
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001298 // Push the shadow frame
1299 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1300
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001301 llvm::Value* shadow_frame_upcast =
1302 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
1303
1304 llvm::Value* result = rtb_.EmitPushShadowFrame(shadow_frame_upcast,
1305 method_object_addr,
TDYa1278e950c12012-11-02 09:58:19 -07001306 num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001307
1308 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
1309
1310 return;
1311}
1312
TDYa1278e950c12012-11-02 09:58:19 -07001313void GBCExpanderPass::Expand_SetVReg(llvm::Value* entry_idx,
1314 llvm::Value* value) {
Sebastien Hertzf11afa02013-03-19 17:39:29 +01001315 unsigned vreg_idx = LV2UInt(entry_idx);
1316 DCHECK_LT(vreg_idx, dex_compilation_unit_->GetCodeItem()->registers_size_);
TDYa1278e950c12012-11-02 09:58:19 -07001317
Sebastien Hertzf11afa02013-03-19 17:39:29 +01001318 llvm::Value* vreg_addr = shadow_frame_vreg_addresses_[vreg_idx];
1319 if (UNLIKELY(vreg_addr == NULL)) {
1320 DCHECK(shadow_frame_ != NULL);
TDYa1278e950c12012-11-02 09:58:19 -07001321
Sebastien Hertzf11afa02013-03-19 17:39:29 +01001322 llvm::Value* gep_index[] = {
1323 irb_.getInt32(0), // No pointer displacement
1324 irb_.getInt32(1), // VRegs
1325 entry_idx // Pointer field
1326 };
1327
1328 // A shadow frame address must dominate every use in the function so we
1329 // place it in the entry block right after the allocas.
1330 llvm::BasicBlock::iterator first_non_alloca = func_->getEntryBlock().begin();
1331 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1332 ++first_non_alloca;
1333 }
1334
1335 llvm::IRBuilderBase::InsertPoint ip = irb_.saveIP();
1336 irb_.SetInsertPoint(static_cast<llvm::Instruction*>(first_non_alloca));
1337 vreg_addr = irb_.CreateGEP(shadow_frame_, gep_index);
1338 shadow_frame_vreg_addresses_[vreg_idx] = vreg_addr;
1339 irb_.restoreIP(ip);
1340 }
TDYa1278e950c12012-11-02 09:58:19 -07001341
1342 irb_.CreateStore(value,
1343 irb_.CreateBitCast(vreg_addr, value->getType()->getPointerTo()),
1344 kTBAAShadowFrame);
1345 return;
1346}
1347
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001348void GBCExpanderPass::Expand_PopShadowFrame() {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001349 if (old_shadow_frame_ == NULL) {
1350 return;
1351 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001352 rtb_.EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
1353 return;
1354}
1355
1356void GBCExpanderPass::Expand_UpdateDexPC(llvm::Value* dex_pc_value) {
1357 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07001358 art::ShadowFrame::DexPCOffset(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001359 dex_pc_value,
1360 kTBAAShadowFrame);
1361 return;
1362}
1363
Logan Chien67645d82012-08-17 09:10:54 +08001364void GBCExpanderPass::InsertStackOverflowCheck(llvm::Function& func) {
jeffhao40283122013-01-15 13:15:24 -08001365 // All alloca instructions are generated in the first basic block of the
1366 // function, and there are no alloca instructions after the first non-alloca
1367 // instruction.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001368
Logan Chien67645d82012-08-17 09:10:54 +08001369 llvm::BasicBlock* first_basic_block = &func.front();
1370
1371 // Look for first non-alloca instruction
1372 llvm::BasicBlock::iterator first_non_alloca = first_basic_block->begin();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001373 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1374 ++first_non_alloca;
1375 }
1376
Logan Chien67645d82012-08-17 09:10:54 +08001377 irb_.SetInsertPoint(first_non_alloca);
1378
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001379 // Insert stack overflow check codes before first_non_alloca (i.e., after all
1380 // alloca instructions)
Logan Chien67645d82012-08-17 09:10:54 +08001381 EmitStackOverflowCheck(&*first_non_alloca);
1382
TDYa127890ea892012-08-22 10:49:42 -07001383 irb_.Runtime().EmitTestSuspend();
TDYa127890ea892012-08-22 10:49:42 -07001384
Logan Chien67645d82012-08-17 09:10:54 +08001385 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
1386 if (next_basic_block != first_basic_block) {
1387 // Splice the rest of the instruction to the continuing basic block
1388 next_basic_block->getInstList().splice(
1389 irb_.GetInsertPoint(), first_basic_block->getInstList(),
1390 first_non_alloca, first_basic_block->end());
1391
1392 // Rewrite the basic block
1393 RewriteBasicBlock(next_basic_block);
1394
1395 // Update the phi-instructions in the successor basic block
1396 UpdatePhiInstruction(first_basic_block, irb_.GetInsertBlock());
1397 }
1398
1399 // We have changed the basic block
1400 changed_ = true;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001401}
1402
TDYa1275e869b62012-07-25 00:45:39 -07001403// ==== High-level intrinsic expander ==========================================
1404
TDYa127a1b21852012-07-23 03:20:39 -07001405llvm::Value* GBCExpanderPass::Expand_FPCompare(llvm::Value* src1_value,
1406 llvm::Value* src2_value,
1407 bool gt_bias) {
1408 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1409 llvm::Value* cmp_lt;
1410
1411 if (gt_bias) {
1412 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1413 } else {
1414 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1415 }
1416
1417 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1418}
1419
1420llvm::Value* GBCExpanderPass::Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value) {
1421 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1422 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1423
1424 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1425}
1426
1427llvm::Value* GBCExpanderPass::EmitCompareResultSelection(llvm::Value* cmp_eq,
1428 llvm::Value* cmp_lt) {
1429
1430 llvm::Constant* zero = irb_.getJInt(0);
1431 llvm::Constant* pos1 = irb_.getJInt(1);
1432 llvm::Constant* neg1 = irb_.getJInt(-1);
1433
1434 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1435 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1436
1437 return result_eq;
1438}
1439
Logan Chien75e4b602012-07-23 14:24:12 -07001440llvm::Value* GBCExpanderPass::Expand_IntegerShift(llvm::Value* src1_value,
1441 llvm::Value* src2_value,
1442 IntegerShiftKind kind,
1443 JType op_jty) {
1444 DCHECK(op_jty == kInt || op_jty == kLong);
1445
1446 // Mask and zero-extend RHS properly
1447 if (op_jty == kInt) {
1448 src2_value = irb_.CreateAnd(src2_value, 0x1f);
1449 } else {
1450 llvm::Value* masked_src2_value = irb_.CreateAnd(src2_value, 0x3f);
1451 src2_value = irb_.CreateZExt(masked_src2_value, irb_.getJLongTy());
1452 }
1453
1454 // Create integer shift llvm instruction
1455 switch (kind) {
1456 case kIntegerSHL:
1457 return irb_.CreateShl(src1_value, src2_value);
1458
1459 case kIntegerSHR:
1460 return irb_.CreateAShr(src1_value, src2_value);
1461
1462 case kIntegerUSHR:
1463 return irb_.CreateLShr(src1_value, src2_value);
1464
1465 default:
1466 LOG(FATAL) << "Unknown integer shift kind: " << kind;
1467 return NULL;
1468 }
1469}
1470
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001471llvm::Value* GBCExpanderPass::SignOrZeroExtendCat1Types(llvm::Value* value, JType jty) {
1472 switch (jty) {
1473 case kBoolean:
1474 case kChar:
1475 return irb_.CreateZExt(value, irb_.getJType(kInt));
1476 case kByte:
1477 case kShort:
1478 return irb_.CreateSExt(value, irb_.getJType(kInt));
1479 case kVoid:
1480 case kInt:
1481 case kLong:
1482 case kFloat:
1483 case kDouble:
1484 case kObject:
1485 return value; // Nothing to do.
1486 default:
1487 LOG(FATAL) << "Unknown java type: " << jty;
1488 return NULL;
1489 }
1490}
1491
1492llvm::Value* GBCExpanderPass::TruncateCat1Types(llvm::Value* value, JType jty) {
1493 switch (jty) {
1494 case kBoolean:
1495 case kChar:
1496 case kByte:
1497 case kShort:
1498 return irb_.CreateTrunc(value, irb_.getJType(jty));
1499 case kVoid:
1500 case kInt:
1501 case kLong:
1502 case kFloat:
1503 case kDouble:
1504 case kObject:
1505 return value; // Nothing to do.
1506 default:
1507 LOG(FATAL) << "Unknown java type: " << jty;
1508 return NULL;
1509 }
1510}
1511
TDYa1275a26d442012-07-26 18:58:38 -07001512llvm::Value* GBCExpanderPass::Expand_HLArrayGet(llvm::CallInst& call_inst,
1513 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001514 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1515 llvm::Value* array_addr = call_inst.getArgOperand(1);
1516 llvm::Value* index_value = call_inst.getArgOperand(2);
TDYa127920be7c2012-09-10 17:13:22 -07001517 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001518
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001519 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1520 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1521 opt_flags);
TDYa1275a26d442012-07-26 18:58:38 -07001522
1523 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1524
1525 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
1526
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001527 return SignOrZeroExtendCat1Types(array_elem_value, elem_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001528}
1529
1530
1531void GBCExpanderPass::Expand_HLArrayPut(llvm::CallInst& call_inst,
1532 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001533 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1534 llvm::Value* new_value = call_inst.getArgOperand(1);
1535 llvm::Value* array_addr = call_inst.getArgOperand(2);
1536 llvm::Value* index_value = call_inst.getArgOperand(3);
TDYa127920be7c2012-09-10 17:13:22 -07001537 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001538
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001539 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1540 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1541 opt_flags);
TDYa1275a26d442012-07-26 18:58:38 -07001542
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001543 new_value = TruncateCat1Types(new_value, elem_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001544
1545 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1546
1547 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
1548 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::CheckPutArrayElement);
1549
1550 irb_.CreateCall2(runtime_func, new_value, array_addr);
1551
1552 EmitGuard_ExceptionLandingPad(dex_pc);
1553
1554 EmitMarkGCCard(new_value, array_addr);
1555 }
1556
1557 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1558
1559 return;
1560}
1561
TDYa1275e869b62012-07-25 00:45:39 -07001562llvm::Value* GBCExpanderPass::Expand_HLIGet(llvm::CallInst& call_inst,
1563 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001564 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1565 llvm::Value* object_addr = call_inst.getArgOperand(1);
1566 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(2));
TDYa127920be7c2012-09-10 17:13:22 -07001567 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001568
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001569 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -07001570
1571 llvm::Value* field_value;
1572
1573 int field_offset;
1574 bool is_volatile;
Ian Rogers1212a022013-03-04 10:48:41 -08001575 bool is_fast_path = driver_->ComputeInstanceFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001576 field_idx, dex_compilation_unit_, field_offset, is_volatile, false);
TDYa1275e869b62012-07-25 00:45:39 -07001577
1578 if (!is_fast_path) {
1579 llvm::Function* runtime_func;
1580
1581 if (field_jty == kObject) {
1582 runtime_func = irb_.GetRuntime(runtime_support::GetObjectInstance);
1583 } else if (field_jty == kLong || field_jty == kDouble) {
1584 runtime_func = irb_.GetRuntime(runtime_support::Get64Instance);
1585 } else {
1586 runtime_func = irb_.GetRuntime(runtime_support::Get32Instance);
1587 }
1588
1589 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
1590
1591 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1592
1593 EmitUpdateDexPC(dex_pc);
1594
1595 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
1596 method_object_addr, object_addr);
1597
1598 EmitGuard_ExceptionLandingPad(dex_pc);
1599
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001600 if (field_jty == kFloat || field_jty == kDouble) {
1601 field_value = irb_.CreateBitCast(field_value, irb_.getJType(field_jty));
1602 }
TDYa1275e869b62012-07-25 00:45:39 -07001603 } else {
1604 DCHECK_GE(field_offset, 0);
1605
1606 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001607 irb_.getJType(field_jty)->getPointerTo();
TDYa1275e869b62012-07-25 00:45:39 -07001608
1609 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
1610
1611 llvm::Value* field_addr =
1612 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1613
TDYa1275e869b62012-07-25 00:45:39 -07001614 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001615 field_value = SignOrZeroExtendCat1Types(field_value, field_jty);
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001616
1617 if (is_volatile) {
1618 irb_.CreateMemoryBarrier(art::kLoadLoad);
1619 }
TDYa1275e869b62012-07-25 00:45:39 -07001620 }
1621
TDYa1275e869b62012-07-25 00:45:39 -07001622 return field_value;
1623}
1624
1625void GBCExpanderPass::Expand_HLIPut(llvm::CallInst& call_inst,
1626 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001627 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001628 llvm::Value* new_value = call_inst.getArgOperand(1);
1629 llvm::Value* object_addr = call_inst.getArgOperand(2);
TDYa1275e869b62012-07-25 00:45:39 -07001630 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(3));
TDYa127920be7c2012-09-10 17:13:22 -07001631 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001632
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001633 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -07001634
1635 int field_offset;
1636 bool is_volatile;
Ian Rogers1212a022013-03-04 10:48:41 -08001637 bool is_fast_path = driver_->ComputeInstanceFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001638 field_idx, dex_compilation_unit_, field_offset, is_volatile, true);
TDYa1275e869b62012-07-25 00:45:39 -07001639
1640 if (!is_fast_path) {
1641 llvm::Function* runtime_func;
1642
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001643 if (field_jty == kFloat) {
1644 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1645 } else if (field_jty == kDouble) {
1646 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1647 }
1648
TDYa1275e869b62012-07-25 00:45:39 -07001649 if (field_jty == kObject) {
1650 runtime_func = irb_.GetRuntime(runtime_support::SetObjectInstance);
1651 } else if (field_jty == kLong || field_jty == kDouble) {
1652 runtime_func = irb_.GetRuntime(runtime_support::Set64Instance);
1653 } else {
1654 runtime_func = irb_.GetRuntime(runtime_support::Set32Instance);
1655 }
1656
1657 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1658
1659 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1660
1661 EmitUpdateDexPC(dex_pc);
1662
1663 irb_.CreateCall4(runtime_func, field_idx_value,
1664 method_object_addr, object_addr, new_value);
1665
1666 EmitGuard_ExceptionLandingPad(dex_pc);
1667
1668 } else {
1669 DCHECK_GE(field_offset, 0);
1670
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001671 if (is_volatile) {
1672 irb_.CreateMemoryBarrier(art::kStoreStore);
1673 }
1674
TDYa1275e869b62012-07-25 00:45:39 -07001675 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001676 irb_.getJType(field_jty)->getPointerTo();
TDYa1275e869b62012-07-25 00:45:39 -07001677
1678 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
1679
1680 llvm::Value* field_addr =
1681 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1682
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001683 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275e869b62012-07-25 00:45:39 -07001684 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1685
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001686 if (is_volatile) {
1687 irb_.CreateMemoryBarrier(art::kLoadLoad);
1688 }
1689
TDYa1275e869b62012-07-25 00:45:39 -07001690 if (field_jty == kObject) { // If put an object, mark the GC card table.
1691 EmitMarkGCCard(new_value, object_addr);
1692 }
1693 }
1694
1695 return;
1696}
1697
TDYa127f71bf5a2012-07-29 20:09:52 -07001698llvm::Value* GBCExpanderPass::EmitLoadConstantClass(uint32_t dex_pc,
1699 uint32_t type_idx) {
Ian Rogers89756f22013-03-04 16:40:02 -08001700 if (!driver_->CanAccessTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
1701 *dex_compilation_unit_->GetDexFile(), type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001702 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1703
1704 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1705
1706 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1707
1708 llvm::Function* runtime_func =
1709 irb_.GetRuntime(runtime_support::InitializeTypeAndVerifyAccess);
1710
1711 EmitUpdateDexPC(dex_pc);
1712
1713 llvm::Value* type_object_addr =
1714 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1715
1716 EmitGuard_ExceptionLandingPad(dex_pc);
1717
1718 return type_object_addr;
1719
1720 } else {
1721 // Try to load the class (type) object from the test cache.
1722 llvm::Value* type_field_addr =
1723 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1724
TDYa127ce4cc0d2012-11-18 16:59:53 -08001725 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07001726
Ian Rogers89756f22013-03-04 16:40:02 -08001727 if (driver_->CanAssumeTypeIsPresentInDexCache(*dex_compilation_unit_->GetDexFile(), type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001728 return type_object_addr;
1729 }
1730
1731 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1732
1733 // Test whether class (type) object is in the dex cache or not
1734 llvm::Value* equal_null =
1735 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1736
1737 llvm::BasicBlock* block_cont =
1738 CreateBasicBlockWithDexPC(dex_pc, "cont");
1739
1740 llvm::BasicBlock* block_load_class =
1741 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1742
1743 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
1744
1745 // Failback routine to load the class object
1746 irb_.SetInsertPoint(block_load_class);
1747
1748 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeType);
1749
1750 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1751
1752 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1753
1754 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1755
1756 EmitUpdateDexPC(dex_pc);
1757
1758 llvm::Value* loaded_type_object_addr =
1759 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1760
1761 EmitGuard_ExceptionLandingPad(dex_pc);
1762
1763 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1764
1765 irb_.CreateBr(block_cont);
1766
1767 // Now the class object must be loaded
1768 irb_.SetInsertPoint(block_cont);
1769
1770 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1771
1772 phi->addIncoming(type_object_addr, block_original);
1773 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1774
1775 return phi;
1776 }
1777}
1778
TDYa1275a26d442012-07-26 18:58:38 -07001779llvm::Value* GBCExpanderPass::EmitLoadStaticStorage(uint32_t dex_pc,
1780 uint32_t type_idx) {
1781 llvm::BasicBlock* block_load_static =
1782 CreateBasicBlockWithDexPC(dex_pc, "load_static");
1783
1784 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1785
1786 // Load static storage from dex cache
1787 llvm::Value* storage_field_addr =
1788 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1789
TDYa127ce4cc0d2012-11-18 16:59:53 -08001790 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
TDYa1275a26d442012-07-26 18:58:38 -07001791
1792 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1793
1794 // Test: Is the static storage of this class initialized?
1795 llvm::Value* equal_null =
1796 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
1797
1798 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
1799
1800 // Failback routine to load the class object
1801 irb_.SetInsertPoint(block_load_static);
1802
1803 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeStaticStorage);
1804
1805 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1806
1807 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1808
1809 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1810
1811 EmitUpdateDexPC(dex_pc);
1812
1813 llvm::Value* loaded_storage_object_addr =
1814 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1815
1816 EmitGuard_ExceptionLandingPad(dex_pc);
1817
1818 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
1819
1820 irb_.CreateBr(block_cont);
1821
1822 // Now the class object must be loaded
1823 irb_.SetInsertPoint(block_cont);
1824
1825 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1826
1827 phi->addIncoming(storage_object_addr, block_original);
1828 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
1829
1830 return phi;
1831}
1832
1833llvm::Value* GBCExpanderPass::Expand_HLSget(llvm::CallInst& call_inst,
1834 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001835 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1836 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1837
1838 int field_offset;
1839 int ssb_index;
1840 bool is_referrers_class;
1841 bool is_volatile;
1842
Ian Rogers1212a022013-03-04 10:48:41 -08001843 bool is_fast_path = driver_->ComputeStaticFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001844 field_idx, dex_compilation_unit_, field_offset, ssb_index,
TDYa1275a26d442012-07-26 18:58:38 -07001845 is_referrers_class, is_volatile, false);
1846
1847 llvm::Value* static_field_value;
1848
1849 if (!is_fast_path) {
1850 llvm::Function* runtime_func;
1851
1852 if (field_jty == kObject) {
1853 runtime_func = irb_.GetRuntime(runtime_support::GetObjectStatic);
1854 } else if (field_jty == kLong || field_jty == kDouble) {
1855 runtime_func = irb_.GetRuntime(runtime_support::Get64Static);
1856 } else {
1857 runtime_func = irb_.GetRuntime(runtime_support::Get32Static);
1858 }
1859
1860 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1861
1862 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1863
1864 EmitUpdateDexPC(dex_pc);
1865
1866 static_field_value =
1867 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
1868
1869 EmitGuard_ExceptionLandingPad(dex_pc);
1870
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001871 if (field_jty == kFloat || field_jty == kDouble) {
1872 static_field_value = irb_.CreateBitCast(static_field_value, irb_.getJType(field_jty));
1873 }
TDYa1275a26d442012-07-26 18:58:38 -07001874 } else {
1875 DCHECK_GE(field_offset, 0);
1876
1877 llvm::Value* static_storage_addr = NULL;
1878
1879 if (is_referrers_class) {
1880 // Fast path, static storage base is this method's class
1881 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1882
1883 static_storage_addr =
1884 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001885 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001886 irb_.getJObjectTy(),
1887 kTBAAConstJObject);
1888 } else {
1889 // Medium path, static storage base in a different class which
1890 // requires checks that the other class is initialized
1891 DCHECK_GE(ssb_index, 0);
1892 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1893 }
1894
1895 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1896
1897 llvm::Value* static_field_addr =
1898 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001899 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001900
TDYa1275a26d442012-07-26 18:58:38 -07001901 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001902 static_field_value = SignOrZeroExtendCat1Types(static_field_value, field_jty);
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001903
1904 if (is_volatile) {
1905 irb_.CreateMemoryBarrier(art::kLoadLoad);
1906 }
TDYa1275a26d442012-07-26 18:58:38 -07001907 }
1908
TDYa1275a26d442012-07-26 18:58:38 -07001909 return static_field_value;
1910}
1911
1912void GBCExpanderPass::Expand_HLSput(llvm::CallInst& call_inst,
1913 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001914 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1915 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1916 llvm::Value* new_value = call_inst.getArgOperand(1);
1917
1918 if (field_jty == kFloat || field_jty == kDouble) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001919 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty));
TDYa1275a26d442012-07-26 18:58:38 -07001920 }
1921
1922 int field_offset;
1923 int ssb_index;
1924 bool is_referrers_class;
1925 bool is_volatile;
1926
Ian Rogers1212a022013-03-04 10:48:41 -08001927 bool is_fast_path = driver_->ComputeStaticFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001928 field_idx, dex_compilation_unit_, field_offset, ssb_index,
TDYa1275a26d442012-07-26 18:58:38 -07001929 is_referrers_class, is_volatile, true);
1930
1931 if (!is_fast_path) {
1932 llvm::Function* runtime_func;
1933
1934 if (field_jty == kObject) {
1935 runtime_func = irb_.GetRuntime(runtime_support::SetObjectStatic);
1936 } else if (field_jty == kLong || field_jty == kDouble) {
1937 runtime_func = irb_.GetRuntime(runtime_support::Set64Static);
1938 } else {
1939 runtime_func = irb_.GetRuntime(runtime_support::Set32Static);
1940 }
1941
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001942 if (field_jty == kFloat) {
1943 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1944 } else if (field_jty == kDouble) {
1945 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1946 }
1947
TDYa1275a26d442012-07-26 18:58:38 -07001948 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1949
1950 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1951
1952 EmitUpdateDexPC(dex_pc);
1953
1954 irb_.CreateCall3(runtime_func, field_idx_value,
1955 method_object_addr, new_value);
1956
1957 EmitGuard_ExceptionLandingPad(dex_pc);
1958
1959 } else {
1960 DCHECK_GE(field_offset, 0);
1961
1962 llvm::Value* static_storage_addr = NULL;
1963
1964 if (is_referrers_class) {
1965 // Fast path, static storage base is this method's class
1966 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1967
1968 static_storage_addr =
1969 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001970 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001971 irb_.getJObjectTy(),
1972 kTBAAConstJObject);
1973 } else {
1974 // Medium path, static storage base in a different class which
1975 // requires checks that the other class is initialized
1976 DCHECK_GE(ssb_index, 0);
1977 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1978 }
1979
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001980 if (is_volatile) {
1981 irb_.CreateMemoryBarrier(art::kStoreStore);
1982 }
1983
TDYa1275a26d442012-07-26 18:58:38 -07001984 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1985
1986 llvm::Value* static_field_addr =
1987 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001988 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001989
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001990 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001991 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1992
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001993 if (is_volatile) {
1994 irb_.CreateMemoryBarrier(art::kStoreLoad);
1995 }
1996
TDYa1275a26d442012-07-26 18:58:38 -07001997 if (field_jty == kObject) { // If put an object, mark the GC card table.
1998 EmitMarkGCCard(new_value, static_storage_addr);
1999 }
2000 }
2001
2002 return;
2003}
2004
TDYa127f71bf5a2012-07-29 20:09:52 -07002005llvm::Value* GBCExpanderPass::Expand_ConstString(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002006 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2007 uint32_t string_idx = LV2UInt(call_inst.getArgOperand(0));
2008
2009 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
2010
TDYa127ce4cc0d2012-11-18 16:59:53 -08002011 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07002012
Ian Rogers89756f22013-03-04 16:40:02 -08002013 if (!driver_->CanAssumeStringIsPresentInDexCache(*dex_compilation_unit_->GetDexFile(),
2014 string_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002015 llvm::BasicBlock* block_str_exist =
2016 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
2017
2018 llvm::BasicBlock* block_str_resolve =
2019 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
2020
2021 llvm::BasicBlock* block_cont =
2022 CreateBasicBlockWithDexPC(dex_pc, "str_cont");
2023
2024 // Test: Is the string resolved and in the dex cache?
2025 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
2026
2027 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
2028
2029 // String is resolved, go to next basic block.
2030 irb_.SetInsertPoint(block_str_exist);
2031 irb_.CreateBr(block_cont);
2032
2033 // String is not resolved yet, resolve it now.
2034 irb_.SetInsertPoint(block_str_resolve);
2035
2036 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::ResolveString);
2037
2038 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2039
2040 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
2041
2042 EmitUpdateDexPC(dex_pc);
2043
2044 llvm::Value* result = irb_.CreateCall2(runtime_func, method_object_addr,
2045 string_idx_value);
2046
2047 EmitGuard_ExceptionLandingPad(dex_pc);
2048
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002049 irb_.CreateBr(block_cont);
2050
2051
TDYa127f71bf5a2012-07-29 20:09:52 -07002052 llvm::BasicBlock* block_pre_cont = irb_.GetInsertBlock();
2053
2054 irb_.SetInsertPoint(block_cont);
2055
2056 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2057
2058 phi->addIncoming(string_addr, block_str_exist);
2059 phi->addIncoming(result, block_pre_cont);
2060
2061 string_addr = phi;
2062 }
2063
2064 return string_addr;
2065}
2066
2067llvm::Value* GBCExpanderPass::Expand_ConstClass(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002068 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2069 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2070
2071 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
2072
2073 return type_object_addr;
2074}
2075
2076void GBCExpanderPass::Expand_MonitorEnter(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002077 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2078 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002079 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002080
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002081 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002082
TDYa127ce4cc0d2012-11-18 16:59:53 -08002083 EmitUpdateDexPC(dex_pc);
2084
TDYa127f71bf5a2012-07-29 20:09:52 -07002085 irb_.Runtime().EmitLockObject(object_addr);
2086
2087 return;
2088}
2089
2090void GBCExpanderPass::Expand_MonitorExit(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002091 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2092 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002093 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002094
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002095 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002096
2097 EmitUpdateDexPC(dex_pc);
2098
2099 irb_.Runtime().EmitUnlockObject(object_addr);
2100
2101 EmitGuard_ExceptionLandingPad(dex_pc);
2102
2103 return;
2104}
2105
2106void GBCExpanderPass::Expand_HLCheckCast(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002107 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2108 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2109 llvm::Value* object_addr = call_inst.getArgOperand(1);
2110
2111 llvm::BasicBlock* block_test_class =
2112 CreateBasicBlockWithDexPC(dex_pc, "test_class");
2113
2114 llvm::BasicBlock* block_test_sub_class =
2115 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
2116
2117 llvm::BasicBlock* block_cont =
2118 CreateBasicBlockWithDexPC(dex_pc, "checkcast_cont");
2119
2120 // Test: Is the reference equal to null? Act as no-op when it is null.
2121 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2122
Ian Rogers8e696052013-03-04 09:00:40 -08002123 irb_.CreateCondBr(equal_null, block_cont, block_test_class, kUnlikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002124
2125 // Test: Is the object instantiated from the given class?
2126 irb_.SetInsertPoint(block_test_class);
2127 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08002128 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002129
2130 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2131
2132 llvm::Value* object_type_field_addr =
2133 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2134
2135 llvm::Value* object_type_object_addr =
2136 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2137
2138 llvm::Value* equal_class =
2139 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2140
Ian Rogers8e696052013-03-04 09:00:40 -08002141 irb_.CreateCondBr(equal_class, block_cont, block_test_sub_class, kLikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002142
2143 // Test: Is the object instantiated from the subclass of the given class?
2144 irb_.SetInsertPoint(block_test_sub_class);
2145
2146 EmitUpdateDexPC(dex_pc);
2147
2148 irb_.CreateCall2(irb_.GetRuntime(runtime_support::CheckCast),
2149 type_object_addr, object_type_object_addr);
2150
2151 EmitGuard_ExceptionLandingPad(dex_pc);
2152
2153 irb_.CreateBr(block_cont);
2154
2155 irb_.SetInsertPoint(block_cont);
2156
2157 return;
2158}
2159
2160llvm::Value* GBCExpanderPass::Expand_InstanceOf(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002161 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2162 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2163 llvm::Value* object_addr = call_inst.getArgOperand(1);
2164
2165 llvm::BasicBlock* block_nullp =
2166 CreateBasicBlockWithDexPC(dex_pc, "nullp");
2167
2168 llvm::BasicBlock* block_test_class =
2169 CreateBasicBlockWithDexPC(dex_pc, "test_class");
2170
2171 llvm::BasicBlock* block_class_equals =
2172 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
2173
2174 llvm::BasicBlock* block_test_sub_class =
2175 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
2176
2177 llvm::BasicBlock* block_cont =
2178 CreateBasicBlockWithDexPC(dex_pc, "instance_of_cont");
2179
2180 // Overview of the following code :
2181 // We check for null, if so, then false, otherwise check for class == . If so
2182 // then true, otherwise do callout slowpath.
2183 //
2184 // Test: Is the reference equal to null? Set 0 when it is null.
2185 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2186
Ian Rogers8e696052013-03-04 09:00:40 -08002187 irb_.CreateCondBr(equal_null, block_nullp, block_test_class, kUnlikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002188
2189 irb_.SetInsertPoint(block_nullp);
2190 irb_.CreateBr(block_cont);
2191
2192 // Test: Is the object instantiated from the given class?
2193 irb_.SetInsertPoint(block_test_class);
2194 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08002195 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002196
2197 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2198
2199 llvm::Value* object_type_field_addr =
2200 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2201
2202 llvm::Value* object_type_object_addr =
2203 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2204
2205 llvm::Value* equal_class =
2206 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2207
Ian Rogers8e696052013-03-04 09:00:40 -08002208 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class, kLikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002209
2210 irb_.SetInsertPoint(block_class_equals);
2211 irb_.CreateBr(block_cont);
2212
2213 // Test: Is the object instantiated from the subclass of the given class?
2214 irb_.SetInsertPoint(block_test_sub_class);
2215 llvm::Value* result =
2216 irb_.CreateCall2(irb_.GetRuntime(runtime_support::IsAssignable),
2217 type_object_addr, object_type_object_addr);
2218 irb_.CreateBr(block_cont);
2219
2220 irb_.SetInsertPoint(block_cont);
2221
2222 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJIntTy(), 3);
2223
2224 phi->addIncoming(irb_.getJInt(0), block_nullp);
2225 phi->addIncoming(irb_.getJInt(1), block_class_equals);
2226 phi->addIncoming(result, block_test_sub_class);
2227
2228 return phi;
2229}
2230
2231llvm::Value* GBCExpanderPass::Expand_NewInstance(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002232 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2233 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2234
2235 llvm::Function* runtime_func;
Ian Rogers89756f22013-03-04 16:40:02 -08002236 if (driver_->CanAccessInstantiableTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
2237 *dex_compilation_unit_->GetDexFile(),
2238 type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002239 runtime_func = irb_.GetRuntime(runtime_support::AllocObject);
2240 } else {
2241 runtime_func = irb_.GetRuntime(runtime_support::AllocObjectWithAccessCheck);
2242 }
2243
2244 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2245
2246 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2247
2248 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2249
2250 EmitUpdateDexPC(dex_pc);
2251
2252 llvm::Value* object_addr =
2253 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
2254
2255 EmitGuard_ExceptionLandingPad(dex_pc);
2256
2257 return object_addr;
2258}
2259
2260llvm::Value* GBCExpanderPass::Expand_HLInvoke(llvm::CallInst& call_inst) {
TDYa127920be7c2012-09-10 17:13:22 -07002261 art::InvokeType invoke_type = static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
2262 bool is_static = (invoke_type == art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002263
2264 if (!is_static) {
2265 // Test: Is *this* parameter equal to null?
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002266 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2267 llvm::Value* this_addr = call_inst.getArgOperand(3);
2268 int opt_flags = LV2UInt(call_inst.getArgOperand(2));
2269
2270 EmitGuard_NullPointerException(dex_pc, this_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002271 }
2272
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002273 llvm::Value* result = NULL;
2274 if (EmitIntrinsic(call_inst, &result)) {
2275 return result;
TDYa127f71bf5a2012-07-29 20:09:52 -07002276 }
2277
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002278 return EmitInvoke(call_inst);
TDYa127f71bf5a2012-07-29 20:09:52 -07002279}
2280
2281llvm::Value* GBCExpanderPass::Expand_OptArrayLength(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002282 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2283 // Get the array object address
2284 llvm::Value* array_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002285 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002286
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002287 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002288
2289 // Get the array length and store it to the register
2290 return EmitLoadArrayLength(array_addr);
2291}
2292
2293llvm::Value* GBCExpanderPass::Expand_NewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002294 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2295 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2296 llvm::Value* length = call_inst.getArgOperand(1);
2297
2298 return EmitAllocNewArray(dex_pc, length, type_idx, false);
2299}
2300
2301llvm::Value* GBCExpanderPass::Expand_HLFilledNewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002302 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2303 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(1));
2304 uint32_t length = call_inst.getNumArgOperands() - 3;
2305
2306 llvm::Value* object_addr =
2307 EmitAllocNewArray(dex_pc, irb_.getInt32(length), type_idx, true);
2308
2309 if (length > 0) {
2310 // Check for the element type
2311 uint32_t type_desc_len = 0;
2312 const char* type_desc =
Ian Rogers89756f22013-03-04 16:40:02 -08002313 dex_compilation_unit_->GetDexFile()->StringByTypeIdx(type_idx, &type_desc_len);
TDYa127f71bf5a2012-07-29 20:09:52 -07002314
2315 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
2316 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
2317 bool is_elem_int_ty = (type_desc[1] == 'I');
2318
2319 uint32_t alignment;
2320 llvm::Constant* elem_size;
2321 llvm::PointerType* field_type;
2322
2323 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
2324 // as the element, thus we are only checking 2 cases: primitive int and
2325 // non-primitive type.
2326 if (is_elem_int_ty) {
2327 alignment = sizeof(int32_t);
2328 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
2329 field_type = irb_.getJIntTy()->getPointerTo();
2330 } else {
2331 alignment = irb_.getSizeOfPtrEquivInt();
2332 elem_size = irb_.getSizeOfPtrEquivIntValue();
2333 field_type = irb_.getJObjectTy()->getPointerTo();
2334 }
2335
2336 llvm::Value* data_field_offset =
Ian Rogers98573f92013-01-30 17:26:32 -08002337 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
TDYa127f71bf5a2012-07-29 20:09:52 -07002338
2339 llvm::Value* data_field_addr =
2340 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
2341
2342 // TODO: Tune this code. Currently we are generating one instruction for
2343 // one element which may be very space consuming. Maybe changing to use
2344 // memcpy may help; however, since we can't guarantee that the alloca of
2345 // dalvik register are continuous, we can't perform such optimization yet.
2346 for (uint32_t i = 0; i < length; ++i) {
2347 llvm::Value* reg_value = call_inst.getArgOperand(i+3);
2348
2349 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
2350
2351 data_field_addr =
2352 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
2353 }
2354 }
2355
2356 return object_addr;
2357}
2358
2359void GBCExpanderPass::Expand_HLFillArrayData(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002360 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2361 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
2362 LV2SInt(call_inst.getArgOperand(0));
2363 llvm::Value* array_addr = call_inst.getArgOperand(1);
2364
TDYa127920be7c2012-09-10 17:13:22 -07002365 const art::Instruction::ArrayDataPayload* payload =
2366 reinterpret_cast<const art::Instruction::ArrayDataPayload*>(
Ian Rogers89756f22013-03-04 16:40:02 -08002367 dex_compilation_unit_->GetCodeItem()->insns_ + payload_offset);
TDYa127f71bf5a2012-07-29 20:09:52 -07002368
2369 if (payload->element_count == 0) {
2370 // When the number of the elements in the payload is zero, we don't have
2371 // to copy any numbers. However, we should check whether the array object
2372 // address is equal to null or not.
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002373 EmitGuard_NullPointerException(dex_pc, array_addr, 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002374 } else {
2375 // To save the code size, we are going to call the runtime function to
2376 // copy the content from DexFile.
2377
2378 // NOTE: We will check for the NullPointerException in the runtime.
2379
2380 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::FillArrayData);
2381
2382 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2383
2384 EmitUpdateDexPC(dex_pc);
2385
2386 irb_.CreateCall4(runtime_func,
2387 method_object_addr, irb_.getInt32(dex_pc),
2388 array_addr, irb_.getInt32(payload_offset));
2389
2390 EmitGuard_ExceptionLandingPad(dex_pc);
2391 }
2392
2393 return;
2394}
2395
2396llvm::Value* GBCExpanderPass::EmitAllocNewArray(uint32_t dex_pc,
2397 llvm::Value* array_length_value,
2398 uint32_t type_idx,
2399 bool is_filled_new_array) {
2400 llvm::Function* runtime_func;
2401
2402 bool skip_access_check =
Ian Rogers89756f22013-03-04 16:40:02 -08002403 driver_->CanAccessTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
2404 *dex_compilation_unit_->GetDexFile(), type_idx);
TDYa127f71bf5a2012-07-29 20:09:52 -07002405
2406
2407 if (is_filled_new_array) {
2408 runtime_func = skip_access_check ?
2409 irb_.GetRuntime(runtime_support::CheckAndAllocArray) :
2410 irb_.GetRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck);
2411 } else {
2412 runtime_func = skip_access_check ?
2413 irb_.GetRuntime(runtime_support::AllocArray) :
2414 irb_.GetRuntime(runtime_support::AllocArrayWithAccessCheck);
2415 }
2416
2417 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2418
2419 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2420
2421 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2422
2423 EmitUpdateDexPC(dex_pc);
2424
2425 llvm::Value* object_addr =
2426 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
2427 array_length_value, thread_object_addr);
2428
2429 EmitGuard_ExceptionLandingPad(dex_pc);
2430
2431 return object_addr;
2432}
2433
2434llvm::Value* GBCExpanderPass::
2435EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -07002436 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -07002437 llvm::Value* this_addr,
2438 uint32_t dex_pc,
2439 bool is_fast_path) {
2440
2441 llvm::Function* runtime_func = NULL;
2442
2443 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002444 case art::kStatic:
TDYa127f71bf5a2012-07-29 20:09:52 -07002445 runtime_func = irb_.GetRuntime(runtime_support::FindStaticMethodWithAccessCheck);
2446 break;
2447
TDYa127920be7c2012-09-10 17:13:22 -07002448 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002449 runtime_func = irb_.GetRuntime(runtime_support::FindDirectMethodWithAccessCheck);
2450 break;
2451
TDYa127920be7c2012-09-10 17:13:22 -07002452 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002453 runtime_func = irb_.GetRuntime(runtime_support::FindVirtualMethodWithAccessCheck);
2454 break;
2455
TDYa127920be7c2012-09-10 17:13:22 -07002456 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002457 runtime_func = irb_.GetRuntime(runtime_support::FindSuperMethodWithAccessCheck);
2458 break;
2459
TDYa127920be7c2012-09-10 17:13:22 -07002460 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002461 if (is_fast_path) {
2462 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethod);
2463 } else {
2464 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethodWithAccessCheck);
2465 }
2466 break;
2467 }
2468
2469 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2470
2471 if (this_addr == NULL) {
TDYa127920be7c2012-09-10 17:13:22 -07002472 DCHECK_EQ(invoke_type, art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002473 this_addr = irb_.getJNull();
2474 }
2475
2476 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2477
2478 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2479
2480 EmitUpdateDexPC(dex_pc);
2481
2482 llvm::Value* callee_method_object_addr =
2483 irb_.CreateCall4(runtime_func,
2484 callee_method_idx_value,
2485 this_addr,
2486 caller_method_object_addr,
2487 thread_object_addr);
2488
2489 EmitGuard_ExceptionLandingPad(dex_pc);
2490
2491 return callee_method_object_addr;
2492}
2493
TDYa1275e869b62012-07-25 00:45:39 -07002494void GBCExpanderPass::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2495 // Using runtime support, let the target can override by InlineAssembly.
2496 irb_.Runtime().EmitMarkGCCard(value, target_addr);
2497}
2498
2499void GBCExpanderPass::EmitUpdateDexPC(uint32_t dex_pc) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002500 if (shadow_frame_ == NULL) {
2501 return;
2502 }
TDYa1275e869b62012-07-25 00:45:39 -07002503 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07002504 art::ShadowFrame::DexPCOffset(),
TDYa1275e869b62012-07-25 00:45:39 -07002505 irb_.getInt32(dex_pc),
2506 kTBAAShadowFrame);
2507}
2508
2509void GBCExpanderPass::EmitGuard_DivZeroException(uint32_t dex_pc,
2510 llvm::Value* denominator,
2511 JType op_jty) {
2512 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2513
2514 llvm::Constant* zero = irb_.getJZero(op_jty);
2515
2516 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2517
2518 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2519
2520 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2521
2522 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
2523
2524 irb_.SetInsertPoint(block_exception);
2525 EmitUpdateDexPC(dex_pc);
2526 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowDivZeroException));
2527 EmitBranchExceptionLandingPad(dex_pc);
2528
2529 irb_.SetInsertPoint(block_continue);
2530}
2531
2532void GBCExpanderPass::EmitGuard_NullPointerException(uint32_t dex_pc,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002533 llvm::Value* object,
2534 int opt_flags) {
2535 bool ignore_null_check = ((opt_flags & MIR_IGNORE_NULL_CHECK) != 0);
2536 if (ignore_null_check) {
2537 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2538 if (lpad) {
2539 // There is at least one catch: create a "fake" conditional branch to
2540 // keep the exception edge to the catch block.
2541 landing_pad_phi_mapping_[lpad].push_back(
2542 std::make_pair(current_bb_->getUniquePredecessor(),
2543 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002544
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002545 llvm::BasicBlock* block_continue =
2546 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002547
Ian Rogers8e696052013-03-04 09:00:40 -08002548 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002549
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002550 irb_.SetInsertPoint(block_continue);
2551 }
2552 } else {
2553 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
TDYa1275e869b62012-07-25 00:45:39 -07002554
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002555 llvm::BasicBlock* block_exception =
2556 CreateBasicBlockWithDexPC(dex_pc, "nullp");
TDYa1275e869b62012-07-25 00:45:39 -07002557
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002558 llvm::BasicBlock* block_continue =
2559 CreateBasicBlockWithDexPC(dex_pc, "cont");
2560
2561 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
2562
2563 irb_.SetInsertPoint(block_exception);
2564 EmitUpdateDexPC(dex_pc);
2565 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowNullPointerException),
2566 irb_.getInt32(dex_pc));
2567 EmitBranchExceptionLandingPad(dex_pc);
2568
2569 irb_.SetInsertPoint(block_continue);
2570 }
TDYa1275e869b62012-07-25 00:45:39 -07002571}
2572
2573void
2574GBCExpanderPass::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2575 llvm::Value* array,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002576 llvm::Value* index,
2577 int opt_flags) {
2578 bool ignore_range_check = ((opt_flags & MIR_IGNORE_RANGE_CHECK) != 0);
2579 if (ignore_range_check) {
2580 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2581 if (lpad) {
2582 // There is at least one catch: create a "fake" conditional branch to
2583 // keep the exception edge to the catch block.
2584 landing_pad_phi_mapping_[lpad].push_back(
2585 std::make_pair(current_bb_->getUniquePredecessor(),
2586 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002587
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002588 llvm::BasicBlock* block_continue =
2589 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002590
Ian Rogers8e696052013-03-04 09:00:40 -08002591 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002592
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002593 irb_.SetInsertPoint(block_continue);
2594 }
2595 } else {
2596 llvm::Value* array_len = EmitLoadArrayLength(array);
TDYa1275e869b62012-07-25 00:45:39 -07002597
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002598 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
TDYa1275e869b62012-07-25 00:45:39 -07002599
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002600 llvm::BasicBlock* block_exception =
2601 CreateBasicBlockWithDexPC(dex_pc, "overflow");
TDYa1275e869b62012-07-25 00:45:39 -07002602
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002603 llvm::BasicBlock* block_continue =
2604 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002605
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002606 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
2607
2608 irb_.SetInsertPoint(block_exception);
2609
2610 EmitUpdateDexPC(dex_pc);
2611 irb_.CreateCall2(irb_.GetRuntime(runtime_support::ThrowIndexOutOfBounds), index, array_len);
2612 EmitBranchExceptionLandingPad(dex_pc);
2613
2614 irb_.SetInsertPoint(block_continue);
2615 }
TDYa1275e869b62012-07-25 00:45:39 -07002616}
2617
Ian Rogers8e696052013-03-04 09:00:40 -08002618llvm::FunctionType* GBCExpanderPass::GetFunctionType(llvm::Type* ret_type, uint32_t method_idx,
TDYa1275e869b62012-07-25 00:45:39 -07002619 bool is_static) {
2620 // Get method signature
Ian Rogers8e696052013-03-04 09:00:40 -08002621 art::DexFile::MethodId const& method_id =
Ian Rogers89756f22013-03-04 16:40:02 -08002622 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx);
TDYa1275e869b62012-07-25 00:45:39 -07002623
2624 uint32_t shorty_size;
Ian Rogers89756f22013-03-04 16:40:02 -08002625 const char* shorty = dex_compilation_unit_->GetDexFile()->GetMethodShorty(method_id, &shorty_size);
TDYa1275e869b62012-07-25 00:45:39 -07002626 CHECK_GE(shorty_size, 1u);
2627
TDYa1275e869b62012-07-25 00:45:39 -07002628 // Get argument type
2629 std::vector<llvm::Type*> args_type;
2630
2631 args_type.push_back(irb_.getJObjectTy()); // method object pointer
2632
2633 if (!is_static) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002634 args_type.push_back(irb_.getJType('L')); // "this" object pointer
TDYa1275e869b62012-07-25 00:45:39 -07002635 }
2636
2637 for (uint32_t i = 1; i < shorty_size; ++i) {
buzbee26f10ee2012-12-21 11:16:29 -08002638 char shorty_type = art::RemapShorty(shorty[i]);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002639 args_type.push_back(irb_.getJType(shorty_type));
TDYa1275e869b62012-07-25 00:45:39 -07002640 }
2641
2642 return llvm::FunctionType::get(ret_type, args_type, false);
2643}
2644
2645
2646llvm::BasicBlock* GBCExpanderPass::
2647CreateBasicBlockWithDexPC(uint32_t dex_pc, const char* postfix) {
2648 std::string name;
2649
2650#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002651 art::StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
TDYa1275e869b62012-07-25 00:45:39 -07002652#endif
2653
2654 return llvm::BasicBlock::Create(context_, name, func_);
2655}
2656
2657llvm::BasicBlock* GBCExpanderPass::GetBasicBlock(uint32_t dex_pc) {
Ian Rogers89756f22013-03-04 16:40:02 -08002658 DCHECK(dex_pc < dex_compilation_unit_->GetCodeItem()->insns_size_in_code_units_);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002659 CHECK(basic_blocks_[dex_pc] != NULL);
TDYa1275e869b62012-07-25 00:45:39 -07002660 return basic_blocks_[dex_pc];
2661}
2662
2663int32_t GBCExpanderPass::GetTryItemOffset(uint32_t dex_pc) {
2664 int32_t min = 0;
Ian Rogers89756f22013-03-04 16:40:02 -08002665 int32_t max = dex_compilation_unit_->GetCodeItem()->tries_size_ - 1;
TDYa1275e869b62012-07-25 00:45:39 -07002666
2667 while (min <= max) {
2668 int32_t mid = min + (max - min) / 2;
2669
Ian Rogers89756f22013-03-04 16:40:02 -08002670 const art::DexFile::TryItem* ti =
2671 art::DexFile::GetTryItems(*dex_compilation_unit_->GetCodeItem(), mid);
TDYa1275e869b62012-07-25 00:45:39 -07002672 uint32_t start = ti->start_addr_;
2673 uint32_t end = start + ti->insn_count_;
2674
2675 if (dex_pc < start) {
2676 max = mid - 1;
2677 } else if (dex_pc >= end) {
2678 min = mid + 1;
2679 } else {
2680 return mid; // found
2681 }
2682 }
2683
2684 return -1; // not found
2685}
2686
2687llvm::BasicBlock* GBCExpanderPass::GetLandingPadBasicBlock(uint32_t dex_pc) {
2688 // Find the try item for this address in this method
2689 int32_t ti_offset = GetTryItemOffset(dex_pc);
2690
2691 if (ti_offset == -1) {
2692 return NULL; // No landing pad is available for this address.
2693 }
2694
2695 // Check for the existing landing pad basic block
2696 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2697 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2698
2699 if (block_lpad) {
2700 // We have generated landing pad for this try item already. Return the
2701 // same basic block.
2702 return block_lpad;
2703 }
2704
2705 // Get try item from code item
Ian Rogers89756f22013-03-04 16:40:02 -08002706 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*dex_compilation_unit_->GetCodeItem(),
Ian Rogers8e696052013-03-04 09:00:40 -08002707 ti_offset);
TDYa1275e869b62012-07-25 00:45:39 -07002708
2709 std::string lpadname;
2710
2711#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002712 art::StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
TDYa1275e869b62012-07-25 00:45:39 -07002713#endif
2714
2715 // Create landing pad basic block
2716 block_lpad = llvm::BasicBlock::Create(context_, lpadname, func_);
2717
2718 // Change IRBuilder insert point
2719 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2720 irb_.SetInsertPoint(block_lpad);
2721
2722 // Find catch block with matching type
2723 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2724
2725 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
2726
2727 llvm::Value* catch_handler_index_value =
2728 irb_.CreateCall2(irb_.GetRuntime(runtime_support::FindCatchBlock),
2729 method_object_addr, ti_offset_value);
2730
2731 // Switch instruction (Go to unwind basic block by default)
2732 llvm::SwitchInst* sw =
2733 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2734
2735 // Cases with matched catch block
Ian Rogers89756f22013-03-04 16:40:02 -08002736 art::CatchHandlerIterator iter(*dex_compilation_unit_->GetCodeItem(), ti->start_addr_);
TDYa1275e869b62012-07-25 00:45:39 -07002737
2738 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2739 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2740 }
2741
2742 // Restore the orignal insert point for IRBuilder
2743 irb_.restoreIP(irb_ip_original);
2744
2745 // Cache this landing pad
2746 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2747 basic_block_landing_pads_[ti_offset] = block_lpad;
2748
2749 return block_lpad;
2750}
2751
2752llvm::BasicBlock* GBCExpanderPass::GetUnwindBasicBlock() {
2753 // Check the existing unwinding baisc block block
2754 if (basic_block_unwind_ != NULL) {
2755 return basic_block_unwind_;
2756 }
2757
2758 // Create new basic block for unwinding
2759 basic_block_unwind_ =
2760 llvm::BasicBlock::Create(context_, "exception_unwind", func_);
2761
2762 // Change IRBuilder insert point
2763 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2764 irb_.SetInsertPoint(basic_block_unwind_);
2765
2766 // Pop the shadow frame
2767 Expand_PopShadowFrame();
2768
2769 // Emit the code to return default value (zero) for the given return type.
Ian Rogers89756f22013-03-04 16:40:02 -08002770 char ret_shorty = dex_compilation_unit_->GetShorty()[0];
buzbee26f10ee2012-12-21 11:16:29 -08002771 ret_shorty = art::RemapShorty(ret_shorty);
TDYa1275e869b62012-07-25 00:45:39 -07002772 if (ret_shorty == 'V') {
2773 irb_.CreateRetVoid();
2774 } else {
2775 irb_.CreateRet(irb_.getJZero(ret_shorty));
2776 }
2777
2778 // Restore the orignal insert point for IRBuilder
2779 irb_.restoreIP(irb_ip_original);
2780
2781 return basic_block_unwind_;
2782}
2783
2784void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2785 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002786 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002787 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002788 irb_.CreateBr(lpad);
2789 } else {
2790 irb_.CreateBr(GetUnwindBasicBlock());
2791 }
2792}
2793
2794void GBCExpanderPass::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
Jeff Hao9a142652013-01-17 23:10:19 +00002795 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
2796
TDYa1275e869b62012-07-25 00:45:39 -07002797 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2798
2799 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002800 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002801 irb_.GetInsertBlock()));
Jeff Hao9a142652013-01-17 23:10:19 +00002802 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002803 } else {
Jeff Hao9a142652013-01-17 23:10:19 +00002804 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002805 }
2806
2807 irb_.SetInsertPoint(block_cont);
2808}
2809
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002810llvm::Value*
2811GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
2812 llvm::CallInst& call_inst) {
2813 switch (intr_id) {
2814 //==- Thread -----------------------------------------------------------==//
2815 case IntrinsicHelper::GetCurrentThread: {
TDYa127b672d1e2012-06-28 21:21:45 -07002816 return irb_.Runtime().EmitGetCurrentThread();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002817 }
Logan Chien75e4b602012-07-23 14:24:12 -07002818 case IntrinsicHelper::CheckSuspend: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08002819 Expand_TestSuspend(call_inst);
TDYa127890ea892012-08-22 10:49:42 -07002820 return NULL;
2821 }
2822 case IntrinsicHelper::TestSuspend: {
Logan Chiend54a23d2012-07-24 11:19:23 -07002823 Expand_TestSuspend(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002824 return NULL;
2825 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002826 case IntrinsicHelper::MarkGCCard: {
TDYa1279a129452012-07-19 03:10:08 -07002827 Expand_MarkGCCard(call_inst);
2828 return NULL;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002829 }
Logan Chien75e4b602012-07-23 14:24:12 -07002830
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002831 //==- Exception --------------------------------------------------------==//
2832 case IntrinsicHelper::ThrowException: {
2833 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
2834 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002835 case IntrinsicHelper::HLThrowException: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002836 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2837
2838 EmitUpdateDexPC(dex_pc);
2839
2840 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowException),
2841 call_inst.getArgOperand(0));
2842
2843 EmitGuard_ExceptionLandingPad(dex_pc);
2844 return NULL;
2845 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002846 case IntrinsicHelper::GetException: {
TDYa127823433d2012-09-26 16:03:51 -07002847 return irb_.Runtime().EmitGetAndClearException();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002848 }
2849 case IntrinsicHelper::IsExceptionPending: {
2850 return irb_.Runtime().EmitIsExceptionPending();
2851 }
2852 case IntrinsicHelper::FindCatchBlock: {
2853 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
2854 }
2855 case IntrinsicHelper::ThrowDivZeroException: {
2856 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
2857 }
2858 case IntrinsicHelper::ThrowNullPointerException: {
2859 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
2860 }
2861 case IntrinsicHelper::ThrowIndexOutOfBounds: {
2862 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
2863 }
Logan Chien75e4b602012-07-23 14:24:12 -07002864
2865 //==- Const String -----------------------------------------------------==//
2866 case IntrinsicHelper::ConstString: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002867 return Expand_ConstString(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002868 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002869 case IntrinsicHelper::LoadStringFromDexCache: {
2870 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
2871 }
2872 case IntrinsicHelper::ResolveString: {
2873 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
2874 }
Logan Chien75e4b602012-07-23 14:24:12 -07002875
2876 //==- Const Class ------------------------------------------------------==//
2877 case IntrinsicHelper::ConstClass: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002878 return Expand_ConstClass(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002879 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002880 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
2881 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
2882 }
2883 case IntrinsicHelper::LoadTypeFromDexCache: {
2884 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
2885 }
2886 case IntrinsicHelper::InitializeType: {
2887 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
2888 }
Logan Chien75e4b602012-07-23 14:24:12 -07002889
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002890 //==- Lock -------------------------------------------------------------==//
2891 case IntrinsicHelper::LockObject: {
2892 Expand_LockObject(call_inst.getArgOperand(0));
2893 return NULL;
2894 }
2895 case IntrinsicHelper::UnlockObject: {
2896 Expand_UnlockObject(call_inst.getArgOperand(0));
2897 return NULL;
2898 }
Logan Chien75e4b602012-07-23 14:24:12 -07002899
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002900 //==- Cast -------------------------------------------------------------==//
2901 case IntrinsicHelper::CheckCast: {
2902 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
2903 }
Logan Chien75e4b602012-07-23 14:24:12 -07002904 case IntrinsicHelper::HLCheckCast: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002905 Expand_HLCheckCast(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002906 return NULL;
2907 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002908 case IntrinsicHelper::IsAssignable: {
2909 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
2910 }
Logan Chien75e4b602012-07-23 14:24:12 -07002911
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002912 //==- Alloc ------------------------------------------------------------==//
2913 case IntrinsicHelper::AllocObject: {
2914 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
2915 }
2916 case IntrinsicHelper::AllocObjectWithAccessCheck: {
2917 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
2918 }
Logan Chien75e4b602012-07-23 14:24:12 -07002919
2920 //==- Instance ---------------------------------------------------------==//
2921 case IntrinsicHelper::NewInstance: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002922 return Expand_NewInstance(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002923 }
2924 case IntrinsicHelper::InstanceOf: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002925 return Expand_InstanceOf(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002926 }
2927
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002928 //==- Array ------------------------------------------------------------==//
Logan Chien75e4b602012-07-23 14:24:12 -07002929 case IntrinsicHelper::NewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002930 return Expand_NewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002931 }
2932 case IntrinsicHelper::OptArrayLength: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002933 return Expand_OptArrayLength(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002934 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002935 case IntrinsicHelper::ArrayLength: {
2936 return EmitLoadArrayLength(call_inst.getArgOperand(0));
2937 }
2938 case IntrinsicHelper::AllocArray: {
2939 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
2940 }
2941 case IntrinsicHelper::AllocArrayWithAccessCheck: {
2942 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
2943 call_inst);
2944 }
2945 case IntrinsicHelper::CheckAndAllocArray: {
2946 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
2947 }
2948 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
2949 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
2950 call_inst);
2951 }
2952 case IntrinsicHelper::ArrayGet: {
2953 return Expand_ArrayGet(call_inst.getArgOperand(0),
2954 call_inst.getArgOperand(1),
2955 kInt);
2956 }
2957 case IntrinsicHelper::ArrayGetWide: {
2958 return Expand_ArrayGet(call_inst.getArgOperand(0),
2959 call_inst.getArgOperand(1),
2960 kLong);
2961 }
2962 case IntrinsicHelper::ArrayGetObject: {
2963 return Expand_ArrayGet(call_inst.getArgOperand(0),
2964 call_inst.getArgOperand(1),
2965 kObject);
2966 }
2967 case IntrinsicHelper::ArrayGetBoolean: {
2968 return Expand_ArrayGet(call_inst.getArgOperand(0),
2969 call_inst.getArgOperand(1),
2970 kBoolean);
2971 }
2972 case IntrinsicHelper::ArrayGetByte: {
2973 return Expand_ArrayGet(call_inst.getArgOperand(0),
2974 call_inst.getArgOperand(1),
2975 kByte);
2976 }
2977 case IntrinsicHelper::ArrayGetChar: {
2978 return Expand_ArrayGet(call_inst.getArgOperand(0),
2979 call_inst.getArgOperand(1),
2980 kChar);
2981 }
2982 case IntrinsicHelper::ArrayGetShort: {
2983 return Expand_ArrayGet(call_inst.getArgOperand(0),
2984 call_inst.getArgOperand(1),
2985 kShort);
2986 }
2987 case IntrinsicHelper::ArrayPut: {
2988 Expand_ArrayPut(call_inst.getArgOperand(0),
2989 call_inst.getArgOperand(1),
2990 call_inst.getArgOperand(2),
2991 kInt);
2992 return NULL;
2993 }
2994 case IntrinsicHelper::ArrayPutWide: {
2995 Expand_ArrayPut(call_inst.getArgOperand(0),
2996 call_inst.getArgOperand(1),
2997 call_inst.getArgOperand(2),
2998 kLong);
2999 return NULL;
3000 }
3001 case IntrinsicHelper::ArrayPutObject: {
3002 Expand_ArrayPut(call_inst.getArgOperand(0),
3003 call_inst.getArgOperand(1),
3004 call_inst.getArgOperand(2),
3005 kObject);
3006 return NULL;
3007 }
3008 case IntrinsicHelper::ArrayPutBoolean: {
3009 Expand_ArrayPut(call_inst.getArgOperand(0),
3010 call_inst.getArgOperand(1),
3011 call_inst.getArgOperand(2),
3012 kBoolean);
3013 return NULL;
3014 }
3015 case IntrinsicHelper::ArrayPutByte: {
3016 Expand_ArrayPut(call_inst.getArgOperand(0),
3017 call_inst.getArgOperand(1),
3018 call_inst.getArgOperand(2),
3019 kByte);
3020 return NULL;
3021 }
3022 case IntrinsicHelper::ArrayPutChar: {
3023 Expand_ArrayPut(call_inst.getArgOperand(0),
3024 call_inst.getArgOperand(1),
3025 call_inst.getArgOperand(2),
3026 kChar);
3027 return NULL;
3028 }
3029 case IntrinsicHelper::ArrayPutShort: {
3030 Expand_ArrayPut(call_inst.getArgOperand(0),
3031 call_inst.getArgOperand(1),
3032 call_inst.getArgOperand(2),
3033 kShort);
3034 return NULL;
3035 }
3036 case IntrinsicHelper::CheckPutArrayElement: {
3037 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
3038 }
3039 case IntrinsicHelper::FilledNewArray: {
3040 Expand_FilledNewArray(call_inst);
3041 return NULL;
3042 }
3043 case IntrinsicHelper::FillArrayData: {
3044 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
3045 }
Logan Chien75e4b602012-07-23 14:24:12 -07003046 case IntrinsicHelper::HLFillArrayData: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003047 Expand_HLFillArrayData(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003048 return NULL;
3049 }
3050 case IntrinsicHelper::HLFilledNewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003051 return Expand_HLFilledNewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003052 }
3053
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003054 //==- Instance Field ---------------------------------------------------==//
3055 case IntrinsicHelper::InstanceFieldGet:
3056 case IntrinsicHelper::InstanceFieldGetBoolean:
3057 case IntrinsicHelper::InstanceFieldGetByte:
3058 case IntrinsicHelper::InstanceFieldGetChar:
3059 case IntrinsicHelper::InstanceFieldGetShort: {
3060 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
3061 }
3062 case IntrinsicHelper::InstanceFieldGetWide: {
3063 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
3064 }
3065 case IntrinsicHelper::InstanceFieldGetObject: {
3066 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
3067 }
3068 case IntrinsicHelper::InstanceFieldGetFast: {
3069 return Expand_IGetFast(call_inst.getArgOperand(0),
3070 call_inst.getArgOperand(1),
3071 call_inst.getArgOperand(2),
3072 kInt);
3073 }
3074 case IntrinsicHelper::InstanceFieldGetWideFast: {
3075 return Expand_IGetFast(call_inst.getArgOperand(0),
3076 call_inst.getArgOperand(1),
3077 call_inst.getArgOperand(2),
3078 kLong);
3079 }
3080 case IntrinsicHelper::InstanceFieldGetObjectFast: {
3081 return Expand_IGetFast(call_inst.getArgOperand(0),
3082 call_inst.getArgOperand(1),
3083 call_inst.getArgOperand(2),
3084 kObject);
3085 }
3086 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
3087 return Expand_IGetFast(call_inst.getArgOperand(0),
3088 call_inst.getArgOperand(1),
3089 call_inst.getArgOperand(2),
3090 kBoolean);
3091 }
3092 case IntrinsicHelper::InstanceFieldGetByteFast: {
3093 return Expand_IGetFast(call_inst.getArgOperand(0),
3094 call_inst.getArgOperand(1),
3095 call_inst.getArgOperand(2),
3096 kByte);
3097 }
3098 case IntrinsicHelper::InstanceFieldGetCharFast: {
3099 return Expand_IGetFast(call_inst.getArgOperand(0),
3100 call_inst.getArgOperand(1),
3101 call_inst.getArgOperand(2),
3102 kChar);
3103 }
3104 case IntrinsicHelper::InstanceFieldGetShortFast: {
3105 return Expand_IGetFast(call_inst.getArgOperand(0),
3106 call_inst.getArgOperand(1),
3107 call_inst.getArgOperand(2),
3108 kShort);
3109 }
3110 case IntrinsicHelper::InstanceFieldPut:
3111 case IntrinsicHelper::InstanceFieldPutBoolean:
3112 case IntrinsicHelper::InstanceFieldPutByte:
3113 case IntrinsicHelper::InstanceFieldPutChar:
3114 case IntrinsicHelper::InstanceFieldPutShort: {
3115 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
3116 }
3117 case IntrinsicHelper::InstanceFieldPutWide: {
3118 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
3119 }
3120 case IntrinsicHelper::InstanceFieldPutObject: {
3121 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
3122 }
3123 case IntrinsicHelper::InstanceFieldPutFast: {
3124 Expand_IPutFast(call_inst.getArgOperand(0),
3125 call_inst.getArgOperand(1),
3126 call_inst.getArgOperand(2),
3127 call_inst.getArgOperand(3),
3128 kInt);
3129 return NULL;
3130 }
3131 case IntrinsicHelper::InstanceFieldPutWideFast: {
3132 Expand_IPutFast(call_inst.getArgOperand(0),
3133 call_inst.getArgOperand(1),
3134 call_inst.getArgOperand(2),
3135 call_inst.getArgOperand(3),
3136 kLong);
3137 return NULL;
3138 }
3139 case IntrinsicHelper::InstanceFieldPutObjectFast: {
3140 Expand_IPutFast(call_inst.getArgOperand(0),
3141 call_inst.getArgOperand(1),
3142 call_inst.getArgOperand(2),
3143 call_inst.getArgOperand(3),
3144 kObject);
3145 return NULL;
3146 }
3147 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
3148 Expand_IPutFast(call_inst.getArgOperand(0),
3149 call_inst.getArgOperand(1),
3150 call_inst.getArgOperand(2),
3151 call_inst.getArgOperand(3),
3152 kBoolean);
3153 return NULL;
3154 }
3155 case IntrinsicHelper::InstanceFieldPutByteFast: {
3156 Expand_IPutFast(call_inst.getArgOperand(0),
3157 call_inst.getArgOperand(1),
3158 call_inst.getArgOperand(2),
3159 call_inst.getArgOperand(3),
3160 kByte);
3161 return NULL;
3162 }
3163 case IntrinsicHelper::InstanceFieldPutCharFast: {
3164 Expand_IPutFast(call_inst.getArgOperand(0),
3165 call_inst.getArgOperand(1),
3166 call_inst.getArgOperand(2),
3167 call_inst.getArgOperand(3),
3168 kChar);
3169 return NULL;
3170 }
3171 case IntrinsicHelper::InstanceFieldPutShortFast: {
3172 Expand_IPutFast(call_inst.getArgOperand(0),
3173 call_inst.getArgOperand(1),
3174 call_inst.getArgOperand(2),
3175 call_inst.getArgOperand(3),
3176 kShort);
3177 return NULL;
3178 }
Logan Chien75e4b602012-07-23 14:24:12 -07003179
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003180 //==- Static Field -----------------------------------------------------==//
3181 case IntrinsicHelper::StaticFieldGet:
3182 case IntrinsicHelper::StaticFieldGetBoolean:
3183 case IntrinsicHelper::StaticFieldGetByte:
3184 case IntrinsicHelper::StaticFieldGetChar:
3185 case IntrinsicHelper::StaticFieldGetShort: {
3186 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
3187 }
3188 case IntrinsicHelper::StaticFieldGetWide: {
3189 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
3190 }
3191 case IntrinsicHelper::StaticFieldGetObject: {
3192 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
3193 }
3194 case IntrinsicHelper::StaticFieldGetFast: {
3195 return Expand_SGetFast(call_inst.getArgOperand(0),
3196 call_inst.getArgOperand(1),
3197 call_inst.getArgOperand(2),
3198 kInt);
3199 }
3200 case IntrinsicHelper::StaticFieldGetWideFast: {
3201 return Expand_SGetFast(call_inst.getArgOperand(0),
3202 call_inst.getArgOperand(1),
3203 call_inst.getArgOperand(2),
3204 kLong);
3205 }
3206 case IntrinsicHelper::StaticFieldGetObjectFast: {
3207 return Expand_SGetFast(call_inst.getArgOperand(0),
3208 call_inst.getArgOperand(1),
3209 call_inst.getArgOperand(2),
3210 kObject);
3211 }
3212 case IntrinsicHelper::StaticFieldGetBooleanFast: {
3213 return Expand_SGetFast(call_inst.getArgOperand(0),
3214 call_inst.getArgOperand(1),
3215 call_inst.getArgOperand(2),
3216 kBoolean);
3217 }
3218 case IntrinsicHelper::StaticFieldGetByteFast: {
3219 return Expand_SGetFast(call_inst.getArgOperand(0),
3220 call_inst.getArgOperand(1),
3221 call_inst.getArgOperand(2),
3222 kByte);
3223 }
3224 case IntrinsicHelper::StaticFieldGetCharFast: {
3225 return Expand_SGetFast(call_inst.getArgOperand(0),
3226 call_inst.getArgOperand(1),
3227 call_inst.getArgOperand(2),
3228 kChar);
3229 }
3230 case IntrinsicHelper::StaticFieldGetShortFast: {
3231 return Expand_SGetFast(call_inst.getArgOperand(0),
3232 call_inst.getArgOperand(1),
3233 call_inst.getArgOperand(2),
3234 kShort);
3235 }
3236 case IntrinsicHelper::StaticFieldPut:
3237 case IntrinsicHelper::StaticFieldPutBoolean:
3238 case IntrinsicHelper::StaticFieldPutByte:
3239 case IntrinsicHelper::StaticFieldPutChar:
3240 case IntrinsicHelper::StaticFieldPutShort: {
3241 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
3242 }
3243 case IntrinsicHelper::StaticFieldPutWide: {
3244 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
3245 }
3246 case IntrinsicHelper::StaticFieldPutObject: {
3247 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
3248 }
3249 case IntrinsicHelper::StaticFieldPutFast: {
3250 Expand_SPutFast(call_inst.getArgOperand(0),
3251 call_inst.getArgOperand(1),
3252 call_inst.getArgOperand(2),
3253 call_inst.getArgOperand(3),
3254 kInt);
3255 return NULL;
3256 }
3257 case IntrinsicHelper::StaticFieldPutWideFast: {
3258 Expand_SPutFast(call_inst.getArgOperand(0),
3259 call_inst.getArgOperand(1),
3260 call_inst.getArgOperand(2),
3261 call_inst.getArgOperand(3),
3262 kLong);
3263 return NULL;
3264 }
3265 case IntrinsicHelper::StaticFieldPutObjectFast: {
3266 Expand_SPutFast(call_inst.getArgOperand(0),
3267 call_inst.getArgOperand(1),
3268 call_inst.getArgOperand(2),
3269 call_inst.getArgOperand(3),
3270 kObject);
3271 return NULL;
3272 }
3273 case IntrinsicHelper::StaticFieldPutBooleanFast: {
3274 Expand_SPutFast(call_inst.getArgOperand(0),
3275 call_inst.getArgOperand(1),
3276 call_inst.getArgOperand(2),
3277 call_inst.getArgOperand(3),
3278 kBoolean);
3279 return NULL;
3280 }
3281 case IntrinsicHelper::StaticFieldPutByteFast: {
3282 Expand_SPutFast(call_inst.getArgOperand(0),
3283 call_inst.getArgOperand(1),
3284 call_inst.getArgOperand(2),
3285 call_inst.getArgOperand(3),
3286 kByte);
3287 return NULL;
3288 }
3289 case IntrinsicHelper::StaticFieldPutCharFast: {
3290 Expand_SPutFast(call_inst.getArgOperand(0),
3291 call_inst.getArgOperand(1),
3292 call_inst.getArgOperand(2),
3293 call_inst.getArgOperand(3),
3294 kChar);
3295 return NULL;
3296 }
3297 case IntrinsicHelper::StaticFieldPutShortFast: {
3298 Expand_SPutFast(call_inst.getArgOperand(0),
3299 call_inst.getArgOperand(1),
3300 call_inst.getArgOperand(2),
3301 call_inst.getArgOperand(3),
3302 kShort);
3303 return NULL;
3304 }
3305 case IntrinsicHelper::LoadDeclaringClassSSB: {
3306 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
3307 }
3308 case IntrinsicHelper::LoadClassSSBFromDexCache: {
3309 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
3310 }
3311 case IntrinsicHelper::InitializeAndLoadClassSSB: {
3312 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
3313 }
Logan Chien75e4b602012-07-23 14:24:12 -07003314
3315 //==- High-level Array -------------------------------------------------==//
3316 case IntrinsicHelper::HLArrayGet: {
TDYa1275a26d442012-07-26 18:58:38 -07003317 return Expand_HLArrayGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003318 }
3319 case IntrinsicHelper::HLArrayGetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003320 return Expand_HLArrayGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003321 }
3322 case IntrinsicHelper::HLArrayGetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003323 return Expand_HLArrayGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003324 }
3325 case IntrinsicHelper::HLArrayGetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003326 return Expand_HLArrayGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003327 }
3328 case IntrinsicHelper::HLArrayGetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003329 return Expand_HLArrayGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003330 }
3331 case IntrinsicHelper::HLArrayGetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003332 return Expand_HLArrayGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003333 }
3334 case IntrinsicHelper::HLArrayGetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003335 return Expand_HLArrayGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003336 }
3337 case IntrinsicHelper::HLArrayGetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003338 return Expand_HLArrayGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003339 }
3340 case IntrinsicHelper::HLArrayGetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003341 return Expand_HLArrayGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003342 }
3343 case IntrinsicHelper::HLArrayPut: {
TDYa1275a26d442012-07-26 18:58:38 -07003344 Expand_HLArrayPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003345 return NULL;
3346 }
3347 case IntrinsicHelper::HLArrayPutBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003348 Expand_HLArrayPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003349 return NULL;
3350 }
3351 case IntrinsicHelper::HLArrayPutByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003352 Expand_HLArrayPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003353 return NULL;
3354 }
3355 case IntrinsicHelper::HLArrayPutChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003356 Expand_HLArrayPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003357 return NULL;
3358 }
3359 case IntrinsicHelper::HLArrayPutShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003360 Expand_HLArrayPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003361 return NULL;
3362 }
3363 case IntrinsicHelper::HLArrayPutFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003364 Expand_HLArrayPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003365 return NULL;
3366 }
3367 case IntrinsicHelper::HLArrayPutWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003368 Expand_HLArrayPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003369 return NULL;
3370 }
3371 case IntrinsicHelper::HLArrayPutDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003372 Expand_HLArrayPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003373 return NULL;
3374 }
3375 case IntrinsicHelper::HLArrayPutObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003376 Expand_HLArrayPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003377 return NULL;
3378 }
3379
3380 //==- High-level Instance ----------------------------------------------==//
3381 case IntrinsicHelper::HLIGet: {
TDYa1275e869b62012-07-25 00:45:39 -07003382 return Expand_HLIGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003383 }
3384 case IntrinsicHelper::HLIGetBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003385 return Expand_HLIGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003386 }
3387 case IntrinsicHelper::HLIGetByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003388 return Expand_HLIGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003389 }
3390 case IntrinsicHelper::HLIGetChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003391 return Expand_HLIGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003392 }
3393 case IntrinsicHelper::HLIGetShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003394 return Expand_HLIGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003395 }
3396 case IntrinsicHelper::HLIGetFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003397 return Expand_HLIGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003398 }
3399 case IntrinsicHelper::HLIGetWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003400 return Expand_HLIGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003401 }
3402 case IntrinsicHelper::HLIGetDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003403 return Expand_HLIGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003404 }
3405 case IntrinsicHelper::HLIGetObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003406 return Expand_HLIGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003407 }
3408 case IntrinsicHelper::HLIPut: {
TDYa1275e869b62012-07-25 00:45:39 -07003409 Expand_HLIPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003410 return NULL;
3411 }
3412 case IntrinsicHelper::HLIPutBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003413 Expand_HLIPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003414 return NULL;
3415 }
3416 case IntrinsicHelper::HLIPutByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003417 Expand_HLIPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003418 return NULL;
3419 }
3420 case IntrinsicHelper::HLIPutChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003421 Expand_HLIPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003422 return NULL;
3423 }
3424 case IntrinsicHelper::HLIPutShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003425 Expand_HLIPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003426 return NULL;
3427 }
3428 case IntrinsicHelper::HLIPutFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003429 Expand_HLIPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003430 return NULL;
3431 }
3432 case IntrinsicHelper::HLIPutWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003433 Expand_HLIPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003434 return NULL;
3435 }
3436 case IntrinsicHelper::HLIPutDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003437 Expand_HLIPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003438 return NULL;
3439 }
3440 case IntrinsicHelper::HLIPutObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003441 Expand_HLIPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003442 return NULL;
3443 }
3444
3445 //==- High-level Invoke ------------------------------------------------==//
TDYa127f71bf5a2012-07-29 20:09:52 -07003446 case IntrinsicHelper::HLInvokeVoid:
3447 case IntrinsicHelper::HLInvokeObj:
3448 case IntrinsicHelper::HLInvokeInt:
3449 case IntrinsicHelper::HLInvokeFloat:
3450 case IntrinsicHelper::HLInvokeLong:
Logan Chien75e4b602012-07-23 14:24:12 -07003451 case IntrinsicHelper::HLInvokeDouble: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003452 return Expand_HLInvoke(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003453 }
3454
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003455 //==- Invoke -----------------------------------------------------------==//
3456 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
3457 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
3458 }
3459 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
3460 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
3461 }
3462 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
3463 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
3464 }
3465 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
3466 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
3467 }
3468 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
3469 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
3470 }
3471 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
3472 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
3473 }
3474 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
3475 return Expand_GetVirtualCalleeMethodObjAddrFast(
3476 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
3477 }
3478 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
3479 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
3480 }
3481 case IntrinsicHelper::InvokeRetVoid:
3482 case IntrinsicHelper::InvokeRetBoolean:
3483 case IntrinsicHelper::InvokeRetByte:
3484 case IntrinsicHelper::InvokeRetChar:
3485 case IntrinsicHelper::InvokeRetShort:
3486 case IntrinsicHelper::InvokeRetInt:
3487 case IntrinsicHelper::InvokeRetLong:
3488 case IntrinsicHelper::InvokeRetFloat:
3489 case IntrinsicHelper::InvokeRetDouble:
3490 case IntrinsicHelper::InvokeRetObject: {
3491 return Expand_Invoke(call_inst);
3492 }
Logan Chien75e4b602012-07-23 14:24:12 -07003493
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003494 //==- Math -------------------------------------------------------------==//
3495 case IntrinsicHelper::DivInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003496 return Expand_DivRem(call_inst, /* is_div */true, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003497 }
3498 case IntrinsicHelper::RemInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003499 return Expand_DivRem(call_inst, /* is_div */false, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003500 }
3501 case IntrinsicHelper::DivLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003502 return Expand_DivRem(call_inst, /* is_div */true, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003503 }
3504 case IntrinsicHelper::RemLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003505 return Expand_DivRem(call_inst, /* is_div */false, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003506 }
3507 case IntrinsicHelper::D2L: {
3508 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
3509 }
3510 case IntrinsicHelper::D2I: {
3511 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
3512 }
3513 case IntrinsicHelper::F2L: {
3514 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
3515 }
3516 case IntrinsicHelper::F2I: {
3517 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
3518 }
Logan Chien75e4b602012-07-23 14:24:12 -07003519
3520 //==- High-level Static ------------------------------------------------==//
3521 case IntrinsicHelper::HLSget: {
TDYa1275a26d442012-07-26 18:58:38 -07003522 return Expand_HLSget(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003523 }
3524 case IntrinsicHelper::HLSgetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003525 return Expand_HLSget(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003526 }
3527 case IntrinsicHelper::HLSgetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003528 return Expand_HLSget(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003529 }
3530 case IntrinsicHelper::HLSgetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003531 return Expand_HLSget(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003532 }
3533 case IntrinsicHelper::HLSgetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003534 return Expand_HLSget(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003535 }
3536 case IntrinsicHelper::HLSgetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003537 return Expand_HLSget(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003538 }
3539 case IntrinsicHelper::HLSgetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003540 return Expand_HLSget(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003541 }
3542 case IntrinsicHelper::HLSgetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003543 return Expand_HLSget(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003544 }
3545 case IntrinsicHelper::HLSgetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003546 return Expand_HLSget(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003547 }
3548 case IntrinsicHelper::HLSput: {
TDYa1275a26d442012-07-26 18:58:38 -07003549 Expand_HLSput(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003550 return NULL;
3551 }
3552 case IntrinsicHelper::HLSputBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003553 Expand_HLSput(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003554 return NULL;
3555 }
3556 case IntrinsicHelper::HLSputByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003557 Expand_HLSput(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003558 return NULL;
3559 }
3560 case IntrinsicHelper::HLSputChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003561 Expand_HLSput(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003562 return NULL;
3563 }
3564 case IntrinsicHelper::HLSputShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003565 Expand_HLSput(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003566 return NULL;
3567 }
3568 case IntrinsicHelper::HLSputFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003569 Expand_HLSput(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003570 return NULL;
3571 }
3572 case IntrinsicHelper::HLSputWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003573 Expand_HLSput(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003574 return NULL;
3575 }
3576 case IntrinsicHelper::HLSputDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003577 Expand_HLSput(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003578 return NULL;
3579 }
3580 case IntrinsicHelper::HLSputObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003581 Expand_HLSput(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003582 return NULL;
3583 }
3584
3585 //==- High-level Monitor -----------------------------------------------==//
3586 case IntrinsicHelper::MonitorEnter: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003587 Expand_MonitorEnter(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003588 return NULL;
3589 }
3590 case IntrinsicHelper::MonitorExit: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003591 Expand_MonitorExit(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003592 return NULL;
3593 }
3594
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003595 //==- Shadow Frame -----------------------------------------------------==//
3596 case IntrinsicHelper::AllocaShadowFrame: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08003597 Expand_AllocaShadowFrame(call_inst.getArgOperand(0));
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003598 return NULL;
3599 }
TDYa1278e950c12012-11-02 09:58:19 -07003600 case IntrinsicHelper::SetVReg: {
3601 Expand_SetVReg(call_inst.getArgOperand(0),
3602 call_inst.getArgOperand(1));
3603 return NULL;
3604 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003605 case IntrinsicHelper::PopShadowFrame: {
3606 Expand_PopShadowFrame();
3607 return NULL;
3608 }
3609 case IntrinsicHelper::UpdateDexPC: {
3610 Expand_UpdateDexPC(call_inst.getArgOperand(0));
3611 return NULL;
3612 }
TDYa127a1b21852012-07-23 03:20:39 -07003613
Logan Chien75e4b602012-07-23 14:24:12 -07003614 //==- Comparison -------------------------------------------------------==//
3615 case IntrinsicHelper::CmplFloat:
3616 case IntrinsicHelper::CmplDouble: {
3617 return Expand_FPCompare(call_inst.getArgOperand(0),
3618 call_inst.getArgOperand(1),
3619 false);
3620 }
3621 case IntrinsicHelper::CmpgFloat:
3622 case IntrinsicHelper::CmpgDouble: {
3623 return Expand_FPCompare(call_inst.getArgOperand(0),
3624 call_inst.getArgOperand(1),
3625 true);
3626 }
3627 case IntrinsicHelper::CmpLong: {
3628 return Expand_LongCompare(call_inst.getArgOperand(0),
3629 call_inst.getArgOperand(1));
3630 }
TDYa127a1b21852012-07-23 03:20:39 -07003631
Logan Chien75e4b602012-07-23 14:24:12 -07003632 //==- Const ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003633 case IntrinsicHelper::ConstInt:
3634 case IntrinsicHelper::ConstLong: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003635 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003636 }
TDYa127920be7c2012-09-10 17:13:22 -07003637 case IntrinsicHelper::ConstFloat: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003638 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3639 irb_.getJFloatTy());
Logan Chien75e4b602012-07-23 14:24:12 -07003640 }
TDYa127920be7c2012-09-10 17:13:22 -07003641 case IntrinsicHelper::ConstDouble: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003642 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3643 irb_.getJDoubleTy());
3644 }
TDYa127920be7c2012-09-10 17:13:22 -07003645 case IntrinsicHelper::ConstObj: {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003646 CHECK(LV2UInt(call_inst.getArgOperand(0)) == 0);
3647 return irb_.getJNull();
Logan Chien75e4b602012-07-23 14:24:12 -07003648 }
3649
3650 //==- Method Info ------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003651 case IntrinsicHelper::MethodInfo: {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003652 // Nothing to be done, because MethodInfo carries optional hints that are
3653 // not needed by the portable path.
Logan Chien75e4b602012-07-23 14:24:12 -07003654 return NULL;
3655 }
3656
3657 //==- Copy -------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003658 case IntrinsicHelper::CopyInt:
3659 case IntrinsicHelper::CopyFloat:
3660 case IntrinsicHelper::CopyLong:
3661 case IntrinsicHelper::CopyDouble:
3662 case IntrinsicHelper::CopyObj: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003663 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003664 }
3665
3666 //==- Shift ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003667 case IntrinsicHelper::SHLLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003668 return Expand_IntegerShift(call_inst.getArgOperand(0),
3669 call_inst.getArgOperand(1),
3670 kIntegerSHL, kLong);
3671 }
TDYa127920be7c2012-09-10 17:13:22 -07003672 case IntrinsicHelper::SHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003673 return Expand_IntegerShift(call_inst.getArgOperand(0),
3674 call_inst.getArgOperand(1),
3675 kIntegerSHR, kLong);
3676 }
TDYa127920be7c2012-09-10 17:13:22 -07003677 case IntrinsicHelper::USHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003678 return Expand_IntegerShift(call_inst.getArgOperand(0),
3679 call_inst.getArgOperand(1),
3680 kIntegerUSHR, kLong);
3681 }
TDYa127920be7c2012-09-10 17:13:22 -07003682 case IntrinsicHelper::SHLInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003683 return Expand_IntegerShift(call_inst.getArgOperand(0),
3684 call_inst.getArgOperand(1),
3685 kIntegerSHL, kInt);
3686 }
TDYa127920be7c2012-09-10 17:13:22 -07003687 case IntrinsicHelper::SHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003688 return Expand_IntegerShift(call_inst.getArgOperand(0),
3689 call_inst.getArgOperand(1),
3690 kIntegerSHR, kInt);
3691 }
TDYa127920be7c2012-09-10 17:13:22 -07003692 case IntrinsicHelper::USHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003693 return Expand_IntegerShift(call_inst.getArgOperand(0),
3694 call_inst.getArgOperand(1),
3695 kIntegerUSHR, kInt);
3696 }
3697
3698 //==- Conversion -------------------------------------------------------==//
TDYa127a1b21852012-07-23 03:20:39 -07003699 case IntrinsicHelper::IntToChar: {
3700 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
3701 irb_.getJIntTy());
3702 }
3703 case IntrinsicHelper::IntToShort: {
3704 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
3705 irb_.getJIntTy());
3706 }
3707 case IntrinsicHelper::IntToByte: {
3708 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
3709 irb_.getJIntTy());
3710 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003711
TDYa12787caa7e2012-08-25 23:23:27 -07003712 //==- Exception --------------------------------------------------------==//
3713 case IntrinsicHelper::CatchTargets: {
TDYa12755e5e6c2012-09-11 15:14:42 -07003714 UpdatePhiInstruction(current_bb_, irb_.GetInsertBlock());
TDYa12787caa7e2012-08-25 23:23:27 -07003715 llvm::SwitchInst* si = llvm::dyn_cast<llvm::SwitchInst>(call_inst.getNextNode());
3716 CHECK(si != NULL);
3717 irb_.CreateBr(si->getDefaultDest());
3718 si->eraseFromParent();
3719 return call_inst.getArgOperand(0);
3720 }
3721
Sebastien Hertz0d43d542013-02-27 19:02:16 +01003722 //==- Constructor barrier-----------------------------------------------==//
3723 case IntrinsicHelper::ConstructorBarrier: {
3724 irb_.CreateMemoryBarrier(art::kStoreStore);
3725 return NULL;
3726 }
3727
Logan Chien75e4b602012-07-23 14:24:12 -07003728 //==- Unknown Cases ----------------------------------------------------==//
3729 case IntrinsicHelper::MaxIntrinsicId:
3730 case IntrinsicHelper::UnknownId:
3731 //default:
3732 // NOTE: "default" is intentionally commented so that C/C++ compiler will
3733 // give some warning on unmatched cases.
3734 // NOTE: We should not implement these cases.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003735 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003736 }
Logan Chien75e4b602012-07-23 14:24:12 -07003737 UNIMPLEMENTED(FATAL) << "Unexpected GBC intrinsic: " << static_cast<int>(intr_id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003738 return NULL;
3739}
3740
3741} // anonymous namespace
3742
3743namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -08003744namespace llvm {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003745
Ian Rogers4c1c2832013-03-04 18:30:13 -08003746::llvm::FunctionPass*
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003747CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Brian Carlstrom265091e2013-01-30 14:08:26 -08003748 CompilerDriver* driver, const DexCompilationUnit* dex_compilation_unit) {
Ian Rogers89756f22013-03-04 16:40:02 -08003749 return new GBCExpanderPass(intrinsic_helper, irb, driver, dex_compilation_unit);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003750}
3751
Ian Rogers4c1c2832013-03-04 18:30:13 -08003752} // namespace llvm
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003753} // namespace art