blob: c1e35a69a7718729ba49f1ff870a79d9aaeb6c83 [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"
Ian Rogers98573f92013-01-30 17:26:32 -080022#include "mirror/abstract_method.h"
23#include "mirror/array.h"
Sebastien Hertz901d5ba2013-03-06 15:19:34 +010024#include "mirror/string.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070025#include "thread.h"
Ian Rogers8e696052013-03-04 09:00:40 -080026#include "utils_llvm.h"
TDYa1275e869b62012-07-25 00:45:39 -070027#include "verifier/method_verifier.h"
Shih-wei Liao21d28f52012-06-12 05:55:00 -070028
buzbee311ca162013-02-28 15:56:43 -080029#include "compiler/dex/mir_graph.h"
buzbee395116c2013-02-27 14:30:25 -080030#include "compiler/dex/compiler_ir.h"
buzbee1fd33462013-03-25 13:40:45 -070031#include "compiler/dex/quick/mir_to_lir.h"
TDYa127920be7c2012-09-10 17:13:22 -070032using art::kMIRIgnoreNullCheck;
33using art::kMIRIgnoreRangeCheck;
34
Shih-wei Liao21d28f52012-06-12 05:55:00 -070035#include <llvm/ADT/STLExtras.h>
Brian Carlstrom37d48792013-03-22 14:14:45 -070036#include <llvm/IR/Intrinsics.h>
37#include <llvm/IR/Metadata.h>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070038#include <llvm/Pass.h>
39#include <llvm/Support/CFG.h>
40#include <llvm/Support/InstIterator.h>
41
42#include <vector>
TDYa127aa558872012-08-16 05:11:07 -070043#include <map>
44#include <utility>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070045
Ian Rogers4c1c2832013-03-04 18:30:13 -080046using namespace art::llvm;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070047
Ian Rogers4c1c2832013-03-04 18:30:13 -080048using art::llvm::IntrinsicHelper;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070049
Shih-wei Liaob2596522012-09-14 16:36:11 -070050namespace art {
buzbee26f10ee2012-12-21 11:16:29 -080051extern char RemapShorty(char shortyType);
Shih-wei Liaob2596522012-09-14 16:36:11 -070052};
53
Shih-wei Liao21d28f52012-06-12 05:55:00 -070054namespace {
55
56class GBCExpanderPass : public llvm::FunctionPass {
57 private:
58 const IntrinsicHelper& intrinsic_helper_;
59 IRBuilder& irb_;
60
61 llvm::LLVMContext& context_;
62 RuntimeSupportBuilder& rtb_;
63
64 private:
65 llvm::AllocaInst* shadow_frame_;
66 llvm::Value* old_shadow_frame_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070067
68 private:
Ian Rogers1212a022013-03-04 10:48:41 -080069 art::CompilerDriver* const driver_;
TDYa1275e869b62012-07-25 00:45:39 -070070
Ian Rogers89756f22013-03-04 16:40:02 -080071 const art::DexCompilationUnit* const dex_compilation_unit_;
TDYa1275e869b62012-07-25 00:45:39 -070072
TDYa1275e869b62012-07-25 00:45:39 -070073 llvm::Function* func_;
74
75 std::vector<llvm::BasicBlock*> basic_blocks_;
76
77 std::vector<llvm::BasicBlock*> basic_block_landing_pads_;
TDYa12755e5e6c2012-09-11 15:14:42 -070078 llvm::BasicBlock* current_bb_;
TDYa127aa558872012-08-16 05:11:07 -070079 std::map<llvm::BasicBlock*, std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> > >
80 landing_pad_phi_mapping_;
TDYa1275e869b62012-07-25 00:45:39 -070081 llvm::BasicBlock* basic_block_unwind_;
82
Sebastien Hertzf11afa02013-03-19 17:39:29 +010083 // Maps each vreg to its shadow frame address.
84 std::vector<llvm::Value*> shadow_frame_vreg_addresses_;
85
Logan Chien67645d82012-08-17 09:10:54 +080086 bool changed_;
87
TDYa1275e869b62012-07-25 00:45:39 -070088 private:
Shih-wei Liao21d28f52012-06-12 05:55:00 -070089 //----------------------------------------------------------------------------
Logan Chien75e4b602012-07-23 14:24:12 -070090 // Constant for GBC expansion
91 //----------------------------------------------------------------------------
92 enum IntegerShiftKind {
93 kIntegerSHL,
94 kIntegerSHR,
95 kIntegerUSHR,
96 };
97
98 private:
99 //----------------------------------------------------------------------------
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700100 // Helper function for GBC expansion
101 //----------------------------------------------------------------------------
102
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700103 llvm::Value* ExpandToRuntime(runtime_support::RuntimeId rt,
104 llvm::CallInst& inst);
105
TDYa1275e869b62012-07-25 00:45:39 -0700106 uint64_t LV2UInt(llvm::Value* lv) {
107 return llvm::cast<llvm::ConstantInt>(lv)->getZExtValue();
108 }
109
110 int64_t LV2SInt(llvm::Value* lv) {
111 return llvm::cast<llvm::ConstantInt>(lv)->getSExtValue();
112 }
113
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700114 private:
115 // TODO: Almost all Emit* are directly copy-n-paste from MethodCompiler.
116 // Refactor these utility functions from MethodCompiler to avoid forking.
117
Logan Chien67645d82012-08-17 09:10:54 +0800118 void EmitStackOverflowCheck(llvm::Instruction* first_non_alloca);
119
120 void RewriteFunction();
121
122 void RewriteBasicBlock(llvm::BasicBlock* original_block);
123
124 void UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
125 llvm::BasicBlock* new_basic_block);
126
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700127
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800128 // Sign or zero extend category 1 types < 32bits in size to 32bits.
129 llvm::Value* SignOrZeroExtendCat1Types(llvm::Value* value, JType jty);
130
131 // Truncate category 1 types from 32bits to the given JType size.
132 llvm::Value* TruncateCat1Types(llvm::Value* value, JType jty);
133
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700134 //----------------------------------------------------------------------------
135 // Dex cache code generation helper function
136 //----------------------------------------------------------------------------
TDYa127920be7c2012-09-10 17:13:22 -0700137 llvm::Value* EmitLoadDexCacheAddr(art::MemberOffset dex_cache_offset);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700138
139 llvm::Value* EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx);
140
141 llvm::Value* EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx);
142
143 llvm::Value* EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx);
144
145 llvm::Value* EmitLoadDexCacheStringFieldAddr(uint32_t string_idx);
146
147 //----------------------------------------------------------------------------
148 // Code generation helper function
149 //----------------------------------------------------------------------------
150 llvm::Value* EmitLoadMethodObjectAddr();
151
152 llvm::Value* EmitLoadArrayLength(llvm::Value* array);
153
154 llvm::Value* EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx);
155
156 llvm::Value* EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
157 llvm::Value* this_addr);
158
159 llvm::Value* EmitArrayGEP(llvm::Value* array_addr,
160 llvm::Value* index_value,
161 JType elem_jty);
162
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100163 //----------------------------------------------------------------------------
164 // Invoke helper function
165 //----------------------------------------------------------------------------
166 llvm::Value* EmitInvoke(llvm::CallInst& call_inst);
167
168 //----------------------------------------------------------------------------
169 // Inlining helper functions
170 //----------------------------------------------------------------------------
171 bool EmitIntrinsic(llvm::CallInst& call_inst, llvm::Value** result);
172
173 bool EmitIntrinsicStringLengthOrIsEmpty(llvm::CallInst& call_inst,
174 llvm::Value** result, bool is_empty);
175
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700176 private:
177 //----------------------------------------------------------------------------
178 // Expand Greenland intrinsics
179 //----------------------------------------------------------------------------
180 void Expand_TestSuspend(llvm::CallInst& call_inst);
181
TDYa1279a129452012-07-19 03:10:08 -0700182 void Expand_MarkGCCard(llvm::CallInst& call_inst);
183
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700184 llvm::Value* Expand_LoadStringFromDexCache(llvm::Value* string_idx_value);
185
186 llvm::Value* Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value);
187
188 void Expand_LockObject(llvm::Value* obj);
189
190 void Expand_UnlockObject(llvm::Value* obj);
191
192 llvm::Value* Expand_ArrayGet(llvm::Value* array_addr,
193 llvm::Value* index_value,
194 JType elem_jty);
195
196 void Expand_ArrayPut(llvm::Value* new_value,
197 llvm::Value* array_addr,
198 llvm::Value* index_value,
199 JType elem_jty);
200
201 void Expand_FilledNewArray(llvm::CallInst& call_inst);
202
203 llvm::Value* Expand_IGetFast(llvm::Value* field_offset_value,
204 llvm::Value* is_volatile_value,
205 llvm::Value* object_addr,
206 JType field_jty);
207
208 void Expand_IPutFast(llvm::Value* field_offset_value,
209 llvm::Value* is_volatile_value,
210 llvm::Value* object_addr,
211 llvm::Value* new_value,
212 JType field_jty);
213
214 llvm::Value* Expand_SGetFast(llvm::Value* static_storage_addr,
215 llvm::Value* field_offset_value,
216 llvm::Value* is_volatile_value,
217 JType field_jty);
218
219 void Expand_SPutFast(llvm::Value* static_storage_addr,
220 llvm::Value* field_offset_value,
221 llvm::Value* is_volatile_value,
222 llvm::Value* new_value,
223 JType field_jty);
224
225 llvm::Value* Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr);
226
227 llvm::Value* Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value);
228
229 llvm::Value*
230 Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value);
231
232 llvm::Value*
233 Expand_GetVirtualCalleeMethodObjAddrFast(llvm::Value* vtable_idx_value,
234 llvm::Value* this_addr);
235
236 llvm::Value* Expand_Invoke(llvm::CallInst& call_inst);
237
TDYa1274ec8ccd2012-08-11 07:04:57 -0700238 llvm::Value* Expand_DivRem(llvm::CallInst& call_inst, bool is_div, JType op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700239
TDYa127ce4cc0d2012-11-18 16:59:53 -0800240 void Expand_AllocaShadowFrame(llvm::Value* num_vregs_value);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700241
TDYa1278e950c12012-11-02 09:58:19 -0700242 void Expand_SetVReg(llvm::Value* entry_idx, llvm::Value* obj);
243
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700244 void Expand_PopShadowFrame();
245
246 void Expand_UpdateDexPC(llvm::Value* dex_pc_value);
247
TDYa127a1b21852012-07-23 03:20:39 -0700248 //----------------------------------------------------------------------------
249 // Quick
250 //----------------------------------------------------------------------------
251
252 llvm::Value* Expand_FPCompare(llvm::Value* src1_value,
253 llvm::Value* src2_value,
254 bool gt_bias);
255
256 llvm::Value* Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value);
257
258 llvm::Value* EmitCompareResultSelection(llvm::Value* cmp_eq,
259 llvm::Value* cmp_lt);
260
TDYa127f71bf5a2012-07-29 20:09:52 -0700261 llvm::Value* EmitLoadConstantClass(uint32_t dex_pc, uint32_t type_idx);
TDYa1275a26d442012-07-26 18:58:38 -0700262 llvm::Value* EmitLoadStaticStorage(uint32_t dex_pc, uint32_t type_idx);
263
TDYa1275e869b62012-07-25 00:45:39 -0700264 llvm::Value* Expand_HLIGet(llvm::CallInst& call_inst, JType field_jty);
265 void Expand_HLIPut(llvm::CallInst& call_inst, JType field_jty);
266
TDYa1275a26d442012-07-26 18:58:38 -0700267 llvm::Value* Expand_HLSget(llvm::CallInst& call_inst, JType field_jty);
268 void Expand_HLSput(llvm::CallInst& call_inst, JType field_jty);
269
270 llvm::Value* Expand_HLArrayGet(llvm::CallInst& call_inst, JType field_jty);
271 void Expand_HLArrayPut(llvm::CallInst& call_inst, JType field_jty);
272
TDYa127f71bf5a2012-07-29 20:09:52 -0700273 llvm::Value* Expand_ConstString(llvm::CallInst& call_inst);
274 llvm::Value* Expand_ConstClass(llvm::CallInst& call_inst);
275
276 void Expand_MonitorEnter(llvm::CallInst& call_inst);
277 void Expand_MonitorExit(llvm::CallInst& call_inst);
278
279 void Expand_HLCheckCast(llvm::CallInst& call_inst);
280 llvm::Value* Expand_InstanceOf(llvm::CallInst& call_inst);
281
282 llvm::Value* Expand_NewInstance(llvm::CallInst& call_inst);
283
284 llvm::Value* Expand_HLInvoke(llvm::CallInst& call_inst);
285
286 llvm::Value* Expand_OptArrayLength(llvm::CallInst& call_inst);
287 llvm::Value* Expand_NewArray(llvm::CallInst& call_inst);
288 llvm::Value* Expand_HLFilledNewArray(llvm::CallInst& call_inst);
289 void Expand_HLFillArrayData(llvm::CallInst& call_inst);
290
291 llvm::Value* EmitAllocNewArray(uint32_t dex_pc,
292 llvm::Value* array_length_value,
293 uint32_t type_idx,
294 bool is_filled_new_array);
295
296 llvm::Value* EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -0700297 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -0700298 llvm::Value* this_addr,
299 uint32_t dex_pc,
300 bool is_fast_path);
301
TDYa1275e869b62012-07-25 00:45:39 -0700302 void EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr);
303
304 void EmitUpdateDexPC(uint32_t dex_pc);
305
306 void EmitGuard_DivZeroException(uint32_t dex_pc,
307 llvm::Value* denominator,
308 JType op_jty);
309
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +0100310 void EmitGuard_NullPointerException(uint32_t dex_pc, llvm::Value* object,
311 int opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -0700312
313 void EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
314 llvm::Value* array,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +0100315 llvm::Value* index,
316 int opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -0700317
Ian Rogers8e696052013-03-04 09:00:40 -0800318 llvm::FunctionType* GetFunctionType(llvm::Type* ret_type, uint32_t method_idx, bool is_static);
TDYa1275e869b62012-07-25 00:45:39 -0700319
320 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
321
322 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
323 const char* postfix);
324
325 int32_t GetTryItemOffset(uint32_t dex_pc);
326
327 llvm::BasicBlock* GetLandingPadBasicBlock(uint32_t dex_pc);
328
329 llvm::BasicBlock* GetUnwindBasicBlock();
330
331 void EmitGuard_ExceptionLandingPad(uint32_t dex_pc);
332
333 void EmitBranchExceptionLandingPad(uint32_t dex_pc);
334
Logan Chien75e4b602012-07-23 14:24:12 -0700335 //----------------------------------------------------------------------------
336 // Expand Arithmetic Helper Intrinsics
337 //----------------------------------------------------------------------------
338
339 llvm::Value* Expand_IntegerShift(llvm::Value* src1_value,
340 llvm::Value* src2_value,
341 IntegerShiftKind kind,
342 JType op_jty);
343
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700344 public:
345 static char ID;
346
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700347 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Ian Rogers0d94eb62013-03-06 15:20:50 -0800348 art::CompilerDriver* driver, const art::DexCompilationUnit* dex_compilation_unit)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700349 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
350 context_(irb.getContext()), rtb_(irb.Runtime()),
TDYa1278e950c12012-11-02 09:58:19 -0700351 shadow_frame_(NULL), old_shadow_frame_(NULL),
Ian Rogers0d94eb62013-03-06 15:20:50 -0800352 driver_(driver),
Ian Rogers89756f22013-03-04 16:40:02 -0800353 dex_compilation_unit_(dex_compilation_unit),
Ian Rogers8e696052013-03-04 09:00:40 -0800354 func_(NULL), current_bb_(NULL), basic_block_unwind_(NULL), changed_(false) {}
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700355
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700356 bool runOnFunction(llvm::Function& func);
357
358 private:
Logan Chien67645d82012-08-17 09:10:54 +0800359 void InsertStackOverflowCheck(llvm::Function& func);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700360
361 llvm::Value* ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
362 llvm::CallInst& call_inst);
363
364};
365
366char GBCExpanderPass::ID = 0;
367
368bool GBCExpanderPass::runOnFunction(llvm::Function& func) {
Ian Rogers8e696052013-03-04 09:00:40 -0800369 VLOG(compiler) << "GBC expansion on " << func.getName().str();
370
TDYa127b672d1e2012-06-28 21:21:45 -0700371 // Runtime support or stub
Brian Carlstrom265091e2013-01-30 14:08:26 -0800372 if (dex_compilation_unit_ == NULL) {
TDYa127b672d1e2012-06-28 21:21:45 -0700373 return false;
374 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700375
Logan Chien67645d82012-08-17 09:10:54 +0800376 // Setup rewrite context
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700377 shadow_frame_ = NULL;
378 old_shadow_frame_ = NULL;
TDYa1275e869b62012-07-25 00:45:39 -0700379 func_ = &func;
Logan Chien67645d82012-08-17 09:10:54 +0800380 changed_ = false; // Assume unchanged
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700381
Sebastien Hertzf11afa02013-03-19 17:39:29 +0100382 shadow_frame_vreg_addresses_.resize(dex_compilation_unit_->GetCodeItem()->registers_size_, NULL);
Ian Rogers89756f22013-03-04 16:40:02 -0800383 basic_blocks_.resize(dex_compilation_unit_->GetCodeItem()->insns_size_in_code_units_);
384 basic_block_landing_pads_.resize(dex_compilation_unit_->GetCodeItem()->tries_size_, NULL);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700385 basic_block_unwind_ = NULL;
386 for (llvm::Function::iterator bb_iter = func_->begin(), bb_end = func_->end();
387 bb_iter != bb_end;
388 ++bb_iter) {
389 if (bb_iter->begin()->getMetadata("DexOff") == NULL) {
390 continue;
391 }
392 uint32_t dex_pc = LV2UInt(bb_iter->begin()->getMetadata("DexOff")->getOperand(0));
393 basic_blocks_[dex_pc] = bb_iter;
394 }
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700395
Logan Chien67645d82012-08-17 09:10:54 +0800396 // Insert stack overflow check
397 InsertStackOverflowCheck(func); // TODO: Use intrinsic.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700398
Logan Chien67645d82012-08-17 09:10:54 +0800399 // Rewrite the intrinsics
400 RewriteFunction();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700401
402 VERIFY_LLVM_FUNCTION(func);
403
Logan Chien67645d82012-08-17 09:10:54 +0800404 return changed_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700405}
406
Logan Chien67645d82012-08-17 09:10:54 +0800407void GBCExpanderPass::RewriteBasicBlock(llvm::BasicBlock* original_block) {
408 llvm::BasicBlock* curr_basic_block = original_block;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700409
Logan Chien67645d82012-08-17 09:10:54 +0800410 llvm::BasicBlock::iterator inst_iter = original_block->begin();
411 llvm::BasicBlock::iterator inst_end = original_block->end();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700412
Logan Chien67645d82012-08-17 09:10:54 +0800413 while (inst_iter != inst_end) {
414 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst_iter);
415 IntrinsicHelper::IntrinsicId intr_id = IntrinsicHelper::UnknownId;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700416
Logan Chien67645d82012-08-17 09:10:54 +0800417 if (call_inst) {
418 llvm::Function* callee_func = call_inst->getCalledFunction();
419 intr_id = intrinsic_helper_.GetIntrinsicId(callee_func);
420 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700421
Logan Chien67645d82012-08-17 09:10:54 +0800422 if (intr_id == IntrinsicHelper::UnknownId) {
423 // This is not intrinsic call. Skip this instruction.
424 ++inst_iter;
425 continue;
426 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700427
Logan Chien67645d82012-08-17 09:10:54 +0800428 // Rewrite the intrinsic and change the function
429 changed_ = true;
430 irb_.SetInsertPoint(inst_iter);
431
432 // Expand the intrinsic
433 if (llvm::Value* new_value = ExpandIntrinsic(intr_id, *call_inst)) {
434 inst_iter->replaceAllUsesWith(new_value);
435 }
436
437 // Remove the old intrinsic call instruction
438 llvm::BasicBlock::iterator old_inst = inst_iter++;
439 old_inst->eraseFromParent();
440
441 // Splice the instruction to the new basic block
442 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
443 if (next_basic_block != curr_basic_block) {
444 next_basic_block->getInstList().splice(
445 irb_.GetInsertPoint(), curr_basic_block->getInstList(),
446 inst_iter, inst_end);
447 curr_basic_block = next_basic_block;
448 inst_end = curr_basic_block->end();
449 }
450 }
451}
452
453
454void GBCExpanderPass::RewriteFunction() {
455 size_t num_basic_blocks = func_->getBasicBlockList().size();
456 // NOTE: We are not using (bb_iter != bb_end) as the for-loop condition,
457 // because we will create new basic block while expanding the intrinsics.
458 // We only want to iterate through the input basic blocks.
459
TDYa127aa558872012-08-16 05:11:07 -0700460 landing_pad_phi_mapping_.clear();
461
Logan Chien67645d82012-08-17 09:10:54 +0800462 for (llvm::Function::iterator bb_iter = func_->begin();
463 num_basic_blocks > 0; ++bb_iter, --num_basic_blocks) {
Shih-wei Liao627d8c42012-08-24 08:31:18 -0700464 // Set insert point to current basic block.
465 irb_.SetInsertPoint(bb_iter);
Logan Chien67645d82012-08-17 09:10:54 +0800466
TDYa12755e5e6c2012-09-11 15:14:42 -0700467 current_bb_ = bb_iter;
TDYa127aa558872012-08-16 05:11:07 -0700468
Logan Chien67645d82012-08-17 09:10:54 +0800469 // Rewrite the basic block
470 RewriteBasicBlock(bb_iter);
471
472 // Update the phi-instructions in the successor basic block
473 llvm::BasicBlock* last_block = irb_.GetInsertBlock();
474 if (last_block != bb_iter) {
475 UpdatePhiInstruction(bb_iter, last_block);
476 }
477 }
TDYa127aa558872012-08-16 05:11:07 -0700478
479 typedef std::map<llvm::PHINode*, llvm::PHINode*> HandlerPHIMap;
480 HandlerPHIMap handler_phi;
481 // Iterate every used landing pad basic block
482 for (size_t i = 0, ei = basic_block_landing_pads_.size(); i != ei; ++i) {
483 llvm::BasicBlock* lbb = basic_block_landing_pads_[i];
484 if (lbb == NULL) {
485 continue;
486 }
487
488 llvm::TerminatorInst* term_inst = lbb->getTerminator();
489 std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> >& rewrite_pair
490 = landing_pad_phi_mapping_[lbb];
491 irb_.SetInsertPoint(lbb->begin());
492
493 // Iterate every succeeding basic block (catch block)
494 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
495 succ_iter != succ_end; ++succ_iter) {
496 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
497
498 // Iterate every phi instructions in the succeeding basic block
499 for (llvm::BasicBlock::iterator
500 inst_iter = succ_basic_block->begin(),
501 inst_end = succ_basic_block->end();
502 inst_iter != inst_end; ++inst_iter) {
503 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
504
505 if (!phi) {
506 break; // Meet non-phi instruction. Done.
507 }
508
509 if (handler_phi[phi] == NULL) {
510 handler_phi[phi] = llvm::PHINode::Create(phi->getType(), 1);
511 }
512
513 // Create new_phi in landing pad
514 llvm::PHINode* new_phi = irb_.CreatePHI(phi->getType(), rewrite_pair.size());
515 // Insert all incoming value into new_phi by rewrite_pair
516 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
517 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
518 llvm::BasicBlock* new_bb = rewrite_pair[j].second;
519 new_phi->addIncoming(phi->getIncomingValueForBlock(old_bb), new_bb);
520 }
521 // Delete all incoming value from phi by rewrite_pair
522 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
523 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
524 int old_bb_idx = phi->getBasicBlockIndex(old_bb);
525 if (old_bb_idx >= 0) {
526 phi->removeIncomingValue(old_bb_idx, false);
527 }
528 }
529 // Insert new_phi into new handler phi
530 handler_phi[phi]->addIncoming(new_phi, lbb);
531 }
532 }
533 }
534
535 // Replace all handler phi
536 // We can't just use the old handler phi, because some exception edges will disappear after we
537 // compute fast-path.
538 for (HandlerPHIMap::iterator it = handler_phi.begin(); it != handler_phi.end(); ++it) {
539 llvm::PHINode* old_phi = it->first;
540 llvm::PHINode* new_phi = it->second;
541 new_phi->insertBefore(old_phi);
542 old_phi->replaceAllUsesWith(new_phi);
543 old_phi->eraseFromParent();
544 }
Logan Chien67645d82012-08-17 09:10:54 +0800545}
546
547void GBCExpanderPass::UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
548 llvm::BasicBlock* new_basic_block) {
549 llvm::TerminatorInst* term_inst = new_basic_block->getTerminator();
550
551 if (!term_inst) {
552 return; // No terminating instruction in new_basic_block. Nothing to do.
553 }
554
555 // Iterate every succeeding basic block
556 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
557 succ_iter != succ_end; ++succ_iter) {
558 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
559
560 // Iterate every phi instructions in the succeeding basic block
561 for (llvm::BasicBlock::iterator
562 inst_iter = succ_basic_block->begin(),
563 inst_end = succ_basic_block->end();
564 inst_iter != inst_end; ++inst_iter) {
565 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
566
567 if (!phi) {
568 break; // Meet non-phi instruction. Done.
569 }
570
571 // Update the incoming block of this phi instruction
572 for (llvm::PHINode::block_iterator
573 ibb_iter = phi->block_begin(), ibb_end = phi->block_end();
574 ibb_iter != ibb_end; ++ibb_iter) {
575 if (*ibb_iter == old_basic_block) {
576 *ibb_iter = new_basic_block;
577 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700578 }
579 }
580 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700581}
582
583llvm::Value* GBCExpanderPass::ExpandToRuntime(runtime_support::RuntimeId rt,
584 llvm::CallInst& inst) {
585 // Some GBC intrinsic can directly replace with IBC runtime. "Directly" means
586 // the arguments passed to the GBC intrinsic are as the same as IBC runtime
587 // function, therefore only called function is needed to change.
588 unsigned num_args = inst.getNumArgOperands();
589
590 if (num_args <= 0) {
591 return irb_.CreateCall(irb_.GetRuntime(rt));
592 } else {
593 std::vector<llvm::Value*> args;
594 for (unsigned i = 0; i < num_args; i++) {
595 args.push_back(inst.getArgOperand(i));
596 }
597
598 return irb_.CreateCall(irb_.GetRuntime(rt), args);
599 }
600}
601
Logan Chien67645d82012-08-17 09:10:54 +0800602void
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700603GBCExpanderPass::EmitStackOverflowCheck(llvm::Instruction* first_non_alloca) {
604 llvm::Function* func = first_non_alloca->getParent()->getParent();
605 llvm::Module* module = func->getParent();
606
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700607 // Call llvm intrinsic function to get frame address.
608 llvm::Function* frameaddress =
609 llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
610
611 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
612 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
613
614 // Cast i8* to int
615 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
616
617 // Get thread.stack_end_
618 llvm::Value* stack_end =
TDYa127920be7c2012-09-10 17:13:22 -0700619 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::StackEndOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700620 irb_.getPtrEquivIntTy(),
621 kTBAARuntimeInfo);
622
623 // Check the frame address < thread.stack_end_ ?
624 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
625
626 llvm::BasicBlock* block_exception =
627 llvm::BasicBlock::Create(context_, "stack_overflow", func);
628
629 llvm::BasicBlock* block_continue =
630 llvm::BasicBlock::Create(context_, "stack_overflow_cont", func);
631
632 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
633
634 // If stack overflow, throw exception.
635 irb_.SetInsertPoint(block_exception);
636 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowStackOverflowException));
637
638 // Unwind.
639 llvm::Type* ret_type = func->getReturnType();
640 if (ret_type->isVoidTy()) {
641 irb_.CreateRetVoid();
642 } else {
643 // The return value is ignored when there's an exception. MethodCompiler
644 // returns zero value under the the corresponding return type in this case.
645 // GBCExpander returns LLVM undef value here for brevity
646 irb_.CreateRet(llvm::UndefValue::get(ret_type));
647 }
648
649 irb_.SetInsertPoint(block_continue);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700650}
651
TDYa127920be7c2012-09-10 17:13:22 -0700652llvm::Value* GBCExpanderPass::EmitLoadDexCacheAddr(art::MemberOffset offset) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700653 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
654
655 return irb_.LoadFromObjectOffset(method_object_addr,
656 offset.Int32Value(),
657 irb_.getJObjectTy(),
658 kTBAAConstJObject);
659}
660
661llvm::Value*
662GBCExpanderPass::EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
663 llvm::Value* static_storage_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800664 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheInitializedStaticStorageOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700665
666 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
667
668 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
669}
670
671llvm::Value*
672GBCExpanderPass::EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
673 llvm::Value* resolved_type_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800674 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedTypesOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700675
676 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
677
678 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
679}
680
681llvm::Value* GBCExpanderPass::
682EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
683 llvm::Value* resolved_method_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800684 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedMethodsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700685
686 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
687
688 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
689}
690
691llvm::Value* GBCExpanderPass::
692EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
693 llvm::Value* string_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800694 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheStringsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700695
696 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
697
698 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
699}
700
701llvm::Value* GBCExpanderPass::EmitLoadMethodObjectAddr() {
702 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
703 return parent_func->arg_begin();
704}
705
706llvm::Value* GBCExpanderPass::EmitLoadArrayLength(llvm::Value* array) {
707 // Load array length
708 return irb_.LoadFromObjectOffset(array,
Ian Rogers98573f92013-01-30 17:26:32 -0800709 art::mirror::Array::LengthOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700710 irb_.getJIntTy(),
711 kTBAAConstJObject);
712
713}
714
715llvm::Value*
716GBCExpanderPass::EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
717 llvm::Value* callee_method_object_field_addr =
718 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
719
TDYa127ce4cc0d2012-11-18 16:59:53 -0800720 return irb_.CreateLoad(callee_method_object_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700721}
722
723llvm::Value* GBCExpanderPass::
724EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, llvm::Value* this_addr) {
725 // Load class object of *this* pointer
726 llvm::Value* class_object_addr =
727 irb_.LoadFromObjectOffset(this_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800728 art::mirror::Object::ClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700729 irb_.getJObjectTy(),
730 kTBAAConstJObject);
731
732 // Load vtable address
733 llvm::Value* vtable_addr =
734 irb_.LoadFromObjectOffset(class_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800735 art::mirror::Class::VTableOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700736 irb_.getJObjectTy(),
737 kTBAAConstJObject);
738
739 // Load callee method object
740 llvm::Value* vtable_idx_value =
741 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
742
743 llvm::Value* method_field_addr =
744 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
745
746 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
747}
748
749// Emit Array GetElementPtr
750llvm::Value* GBCExpanderPass::EmitArrayGEP(llvm::Value* array_addr,
751 llvm::Value* index_value,
752 JType elem_jty) {
753
754 int data_offset;
755 if (elem_jty == kLong || elem_jty == kDouble ||
Ian Rogers98573f92013-01-30 17:26:32 -0800756 (elem_jty == kObject && sizeof(uint64_t) == sizeof(art::mirror::Object*))) {
757 data_offset = art::mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700758 } else {
Ian Rogers98573f92013-01-30 17:26:32 -0800759 data_offset = art::mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700760 }
761
762 llvm::Constant* data_offset_value =
763 irb_.getPtrEquivInt(data_offset);
764
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800765 llvm::Type* elem_type = irb_.getJType(elem_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700766
767 llvm::Value* array_data_addr =
768 irb_.CreatePtrDisp(array_addr, data_offset_value,
769 elem_type->getPointerTo());
770
771 return irb_.CreateGEP(array_data_addr, index_value);
772}
773
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100774llvm::Value* GBCExpanderPass::EmitInvoke(llvm::CallInst& call_inst) {
775 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
776 art::InvokeType invoke_type =
777 static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
778 bool is_static = (invoke_type == art::kStatic);
779 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
780
781 // Load *this* actual parameter
782 llvm::Value* this_addr = (!is_static) ? call_inst.getArgOperand(3) : NULL;
783
784 // Compute invoke related information for compiler decision
785 int vtable_idx = -1;
786 uintptr_t direct_code = 0;
787 uintptr_t direct_method = 0;
Sameer Abu Asalb0682d52013-04-05 19:13:07 -0700788 // TODO: pass actual value of dex PC (instead of kDexPCNotready) needed by verifier based
789 // sharpening after LLVM re-factoring is finished.
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100790 bool is_fast_path = driver_->
Sameer Abu Asalb0682d52013-04-05 19:13:07 -0700791 ComputeInvokeInfo(callee_method_idx, art::kDexPCNotReady, dex_compilation_unit_,
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100792 invoke_type, vtable_idx, direct_code, direct_method);
793
794 // Load the method object
795 llvm::Value* callee_method_object_addr = NULL;
796
797 if (!is_fast_path) {
798 callee_method_object_addr =
799 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
800 this_addr, dex_pc, is_fast_path);
801 } else {
802 switch (invoke_type) {
803 case art::kStatic:
804 case art::kDirect:
805 if (direct_method != 0u &&
806 direct_method != static_cast<uintptr_t>(-1)) {
807 callee_method_object_addr =
808 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
809 irb_.getJObjectTy());
810 } else {
811 callee_method_object_addr =
812 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
813 }
814 break;
815
816 case art::kVirtual:
817 DCHECK(vtable_idx != -1);
818 callee_method_object_addr =
819 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
820 break;
821
822 case art::kSuper:
823 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
824 "the fast path.";
825 break;
826
827 case art::kInterface:
828 callee_method_object_addr =
829 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
830 invoke_type, this_addr,
831 dex_pc, is_fast_path);
832 break;
833 }
834 }
835
836 // Load the actual parameter
837 std::vector<llvm::Value*> args;
838
839 args.push_back(callee_method_object_addr); // method object for callee
840
841 for (uint32_t i = 3; i < call_inst.getNumArgOperands(); ++i) {
842 args.push_back(call_inst.getArgOperand(i));
843 }
844
845 llvm::Value* code_addr;
846 llvm::Type* func_type = GetFunctionType(call_inst.getType(),
847 callee_method_idx, is_static);
848 if (direct_code != 0u && direct_code != static_cast<uintptr_t>(-1)) {
849 code_addr =
850 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_code),
851 func_type->getPointerTo());
852 } else {
853 code_addr =
854 irb_.LoadFromObjectOffset(callee_method_object_addr,
855 art::mirror::AbstractMethod::GetCodeOffset().Int32Value(),
856 func_type->getPointerTo(), kTBAARuntimeInfo);
857 }
858
859 // Invoke callee
860 EmitUpdateDexPC(dex_pc);
861 llvm::Value* retval = irb_.CreateCall(code_addr, args);
862 EmitGuard_ExceptionLandingPad(dex_pc);
863
864 return retval;
865}
866
867bool GBCExpanderPass::EmitIntrinsic(llvm::CallInst& call_inst,
868 llvm::Value** result) {
869 DCHECK(result != NULL);
870
871 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
872 std::string callee_method_name(
873 PrettyMethod(callee_method_idx, *dex_compilation_unit_->GetDexFile()));
874
875 if (callee_method_name == "int java.lang.String.length()") {
876 return EmitIntrinsicStringLengthOrIsEmpty(call_inst, result,
877 false /* is_empty */);
878 }
879 if (callee_method_name == "boolean java.lang.String.isEmpty()") {
880 return EmitIntrinsicStringLengthOrIsEmpty(call_inst, result,
881 true /* is_empty */);
882 }
883
884 *result = NULL;
885 return false;
886}
887
888bool GBCExpanderPass::EmitIntrinsicStringLengthOrIsEmpty(llvm::CallInst& call_inst,
889 llvm::Value** result,
890 bool is_empty) {
891 art::InvokeType invoke_type =
892 static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
893 DCHECK_NE(invoke_type, art::kStatic);
894 DCHECK_EQ(call_inst.getNumArgOperands(), 4U);
895
896 llvm::Value* this_object = call_inst.getArgOperand(3);
897 llvm::Value* string_count =
898 irb_.LoadFromObjectOffset(this_object,
899 art::mirror::String::CountOffset().Int32Value(),
900 irb_.getJIntTy(),
901 kTBAAConstJObject);
902 if (is_empty) {
903 llvm::Value* count_equals_zero = irb_.CreateICmpEQ(string_count,
904 irb_.getJInt(0));
905 llvm::Value* is_empty = irb_.CreateSelect(count_equals_zero,
906 irb_.getJBoolean(true),
907 irb_.getJBoolean(false));
908 is_empty = SignOrZeroExtendCat1Types(is_empty, kBoolean);
909 *result = is_empty;
910 } else {
911 *result = string_count;
912 }
913 return true;
914}
915
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700916void GBCExpanderPass::Expand_TestSuspend(llvm::CallInst& call_inst) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800917 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
918
919 llvm::Value* suspend_count =
920 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::ThreadFlagsOffset().Int32Value(),
921 irb_.getInt16Ty(),
922 kTBAARuntimeInfo);
923 llvm::Value* is_suspend = irb_.CreateICmpNE(suspend_count, irb_.getInt16(0));
924
925 llvm::BasicBlock* basic_block_suspend = CreateBasicBlockWithDexPC(dex_pc, "suspend");
926 llvm::BasicBlock* basic_block_cont = CreateBasicBlockWithDexPC(dex_pc, "suspend_cont");
927
928 irb_.CreateCondBr(is_suspend, basic_block_suspend, basic_block_cont, kUnlikely);
929
930 irb_.SetInsertPoint(basic_block_suspend);
931 if (dex_pc != art::DexFile::kDexNoIndex) {
932 EmitUpdateDexPC(dex_pc);
933 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700934 irb_.Runtime().EmitTestSuspend();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800935
936 llvm::BasicBlock* basic_block_exception = CreateBasicBlockWithDexPC(dex_pc, "exception");
937 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
938 irb_.CreateCondBr(exception_pending, basic_block_exception, basic_block_cont, kUnlikely);
939
940 irb_.SetInsertPoint(basic_block_exception);
941 llvm::Type* ret_type = call_inst.getParent()->getParent()->getReturnType();
942 if (ret_type->isVoidTy()) {
943 irb_.CreateRetVoid();
944 } else {
945 // The return value is ignored when there's an exception.
946 irb_.CreateRet(llvm::UndefValue::get(ret_type));
947 }
TDYa127ce4cc0d2012-11-18 16:59:53 -0800948
949 irb_.SetInsertPoint(basic_block_cont);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700950 return;
951}
952
TDYa1279a129452012-07-19 03:10:08 -0700953void GBCExpanderPass::Expand_MarkGCCard(llvm::CallInst& call_inst) {
TDYa1279a129452012-07-19 03:10:08 -0700954 irb_.Runtime().EmitMarkGCCard(call_inst.getArgOperand(0), call_inst.getArgOperand(1));
TDYa1279a129452012-07-19 03:10:08 -0700955 return;
956}
957
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700958llvm::Value*
959GBCExpanderPass::Expand_LoadStringFromDexCache(llvm::Value* string_idx_value) {
960 uint32_t string_idx =
961 llvm::cast<llvm::ConstantInt>(string_idx_value)->getZExtValue();
962
963 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
964
TDYa127ce4cc0d2012-11-18 16:59:53 -0800965 return irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700966}
967
968llvm::Value*
969GBCExpanderPass::Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value) {
970 uint32_t type_idx =
971 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
972
973 llvm::Value* type_field_addr =
974 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
975
TDYa127ce4cc0d2012-11-18 16:59:53 -0800976 return irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700977}
978
979void GBCExpanderPass::Expand_LockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700980 rtb_.EmitLockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700981 return;
982}
983
984void GBCExpanderPass::Expand_UnlockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700985 rtb_.EmitUnlockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700986 return;
987}
988
989llvm::Value* GBCExpanderPass::Expand_ArrayGet(llvm::Value* array_addr,
990 llvm::Value* index_value,
991 JType elem_jty) {
992 llvm::Value* array_elem_addr =
993 EmitArrayGEP(array_addr, index_value, elem_jty);
994
995 return irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
996}
997
998void GBCExpanderPass::Expand_ArrayPut(llvm::Value* new_value,
999 llvm::Value* array_addr,
1000 llvm::Value* index_value,
1001 JType elem_jty) {
1002 llvm::Value* array_elem_addr =
1003 EmitArrayGEP(array_addr, index_value, elem_jty);
1004
1005 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1006
1007 return;
1008}
1009
1010void GBCExpanderPass::Expand_FilledNewArray(llvm::CallInst& call_inst) {
1011 // Most of the codes refer to MethodCompiler::EmitInsn_FilledNewArray
1012 llvm::Value* array = call_inst.getArgOperand(0);
1013
1014 uint32_t element_jty =
1015 llvm::cast<llvm::ConstantInt>(call_inst.getArgOperand(1))->getZExtValue();
1016
1017 DCHECK(call_inst.getNumArgOperands() > 2);
1018 unsigned num_elements = (call_inst.getNumArgOperands() - 2);
1019
1020 bool is_elem_int_ty = (static_cast<JType>(element_jty) == kInt);
1021
1022 uint32_t alignment;
1023 llvm::Constant* elem_size;
1024 llvm::PointerType* field_type;
1025
1026 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1027 // as the element, thus we are only checking 2 cases: primitive int and
1028 // non-primitive type.
1029 if (is_elem_int_ty) {
1030 alignment = sizeof(int32_t);
1031 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
1032 field_type = irb_.getJIntTy()->getPointerTo();
1033 } else {
1034 alignment = irb_.getSizeOfPtrEquivInt();
1035 elem_size = irb_.getSizeOfPtrEquivIntValue();
1036 field_type = irb_.getJObjectTy()->getPointerTo();
1037 }
1038
1039 llvm::Value* data_field_offset =
Ian Rogers98573f92013-01-30 17:26:32 -08001040 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001041
1042 llvm::Value* data_field_addr =
1043 irb_.CreatePtrDisp(array, data_field_offset, field_type);
1044
1045 for (unsigned i = 0; i < num_elements; ++i) {
1046 // Values to fill the array begin at the 3rd argument
1047 llvm::Value* reg_value = call_inst.getArgOperand(2 + i);
1048
1049 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
1050
1051 data_field_addr =
1052 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
1053 }
1054
1055 return;
1056}
1057
1058llvm::Value* GBCExpanderPass::Expand_IGetFast(llvm::Value* field_offset_value,
1059 llvm::Value* /*is_volatile_value*/,
1060 llvm::Value* object_addr,
1061 JType field_jty) {
1062 int field_offset =
1063 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1064
1065 DCHECK_GE(field_offset, 0);
1066
1067 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001068 irb_.getJType(field_jty)->getPointerTo();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001069
1070 field_offset_value = irb_.getPtrEquivInt(field_offset);
1071
1072 llvm::Value* field_addr =
1073 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1074
1075 // TODO: Check is_volatile. We need to generate atomic load instruction
1076 // when is_volatile is true.
1077 return irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
1078}
1079
1080void GBCExpanderPass::Expand_IPutFast(llvm::Value* field_offset_value,
1081 llvm::Value* /* is_volatile_value */,
1082 llvm::Value* object_addr,
1083 llvm::Value* new_value,
1084 JType field_jty) {
1085 int field_offset =
1086 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1087
1088 DCHECK_GE(field_offset, 0);
1089
1090 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001091 irb_.getJType(field_jty)->getPointerTo();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001092
1093 field_offset_value = irb_.getPtrEquivInt(field_offset);
1094
1095 llvm::Value* field_addr =
1096 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1097
1098 // TODO: Check is_volatile. We need to generate atomic store instruction
1099 // when is_volatile is true.
1100 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1101
1102 return;
1103}
1104
1105llvm::Value* GBCExpanderPass::Expand_SGetFast(llvm::Value* static_storage_addr,
1106 llvm::Value* field_offset_value,
1107 llvm::Value* /*is_volatile_value*/,
1108 JType field_jty) {
1109 int field_offset =
1110 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1111
1112 DCHECK_GE(field_offset, 0);
1113
1114 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1115
1116 llvm::Value* static_field_addr =
1117 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001118 irb_.getJType(field_jty)->getPointerTo());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001119
1120 // TODO: Check is_volatile. We need to generate atomic store instruction
1121 // when is_volatile is true.
1122 return irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
1123}
1124
1125void GBCExpanderPass::Expand_SPutFast(llvm::Value* static_storage_addr,
1126 llvm::Value* field_offset_value,
1127 llvm::Value* /* is_volatile_value */,
1128 llvm::Value* new_value,
1129 JType field_jty) {
1130 int field_offset =
1131 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1132
1133 DCHECK_GE(field_offset, 0);
1134
1135 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1136
1137 llvm::Value* static_field_addr =
1138 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001139 irb_.getJType(field_jty)->getPointerTo());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001140
1141 // TODO: Check is_volatile. We need to generate atomic store instruction
1142 // when is_volatile is true.
1143 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1144
1145 return;
1146}
1147
1148llvm::Value*
1149GBCExpanderPass::Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr) {
1150 return irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001151 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001152 irb_.getJObjectTy(),
1153 kTBAAConstJObject);
1154}
1155
1156llvm::Value*
1157GBCExpanderPass::Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value) {
1158 uint32_t type_idx =
1159 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
1160
1161 llvm::Value* storage_field_addr =
1162 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1163
TDYa127ce4cc0d2012-11-18 16:59:53 -08001164 return irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001165}
1166
1167llvm::Value*
1168GBCExpanderPass::Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value) {
1169 uint32_t callee_method_idx =
1170 llvm::cast<llvm::ConstantInt>(callee_method_idx_value)->getZExtValue();
1171
1172 return EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
1173}
1174
1175llvm::Value* GBCExpanderPass::Expand_GetVirtualCalleeMethodObjAddrFast(
1176 llvm::Value* vtable_idx_value,
1177 llvm::Value* this_addr) {
1178 int vtable_idx =
1179 llvm::cast<llvm::ConstantInt>(vtable_idx_value)->getSExtValue();
1180
1181 return EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
1182}
1183
1184llvm::Value* GBCExpanderPass::Expand_Invoke(llvm::CallInst& call_inst) {
1185 // Most of the codes refer to MethodCompiler::EmitInsn_Invoke
1186 llvm::Value* callee_method_object_addr = call_inst.getArgOperand(0);
1187 unsigned num_args = call_inst.getNumArgOperands();
1188 llvm::Type* ret_type = call_inst.getType();
1189
1190 // Determine the function type of the callee method
1191 std::vector<llvm::Type*> args_type;
1192 std::vector<llvm::Value*> args;
1193 for (unsigned i = 0; i < num_args; i++) {
1194 args.push_back(call_inst.getArgOperand(i));
1195 args_type.push_back(args[i]->getType());
1196 }
1197
1198 llvm::FunctionType* callee_method_type =
1199 llvm::FunctionType::get(ret_type, args_type, false);
1200
1201 llvm::Value* code_addr =
1202 irb_.LoadFromObjectOffset(callee_method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001203 art::mirror::AbstractMethod::GetCodeOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001204 callee_method_type->getPointerTo(),
TDYa127ce4cc0d2012-11-18 16:59:53 -08001205 kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001206
1207 // Invoke callee
1208 llvm::Value* retval = irb_.CreateCall(code_addr, args);
1209
1210 return retval;
1211}
1212
TDYa1274ec8ccd2012-08-11 07:04:57 -07001213llvm::Value* GBCExpanderPass::Expand_DivRem(llvm::CallInst& call_inst,
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001214 bool is_div, JType op_jty) {
TDYa1274ec8ccd2012-08-11 07:04:57 -07001215 llvm::Value* dividend = call_inst.getArgOperand(0);
1216 llvm::Value* divisor = call_inst.getArgOperand(1);
TDYa1274ec8ccd2012-08-11 07:04:57 -07001217 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1218 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001219 // Most of the codes refer to MethodCompiler::EmitIntDivRemResultComputation
1220
1221 // Check the special case: MININT / -1 = MININT
1222 // That case will cause overflow, which is undefined behavior in llvm.
1223 // So we check the divisor is -1 or not, if the divisor is -1, we do
1224 // the special path to avoid undefined behavior.
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001225 llvm::Type* op_type = irb_.getJType(op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001226 llvm::Value* zero = irb_.getJZero(op_jty);
1227 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
1228
TDYa1275e869b62012-07-25 00:45:39 -07001229 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001230 llvm::BasicBlock* eq_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1231 llvm::BasicBlock* ne_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1232 llvm::BasicBlock* neg_one_cont =
1233 llvm::BasicBlock::Create(context_, "", parent);
1234
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001235 llvm::Value* is_equal_neg_one = irb_.CreateICmpEQ(divisor, neg_one);
1236 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
1237
1238 // If divisor == -1
1239 irb_.SetInsertPoint(eq_neg_one);
1240 llvm::Value* eq_result;
1241 if (is_div) {
1242 // We can just change from "dividend div -1" to "neg dividend". The sub
1243 // don't care the sign/unsigned because of two's complement representation.
1244 // And the behavior is what we want:
1245 // -(2^n) (2^n)-1
1246 // MININT < k <= MAXINT -> mul k -1 = -k
1247 // MININT == k -> mul k -1 = k
1248 //
1249 // LLVM use sub to represent 'neg'
1250 eq_result = irb_.CreateSub(zero, dividend);
1251 } else {
1252 // Everything modulo -1 will be 0.
1253 eq_result = zero;
1254 }
1255 irb_.CreateBr(neg_one_cont);
1256
1257 // If divisor != -1, just do the division.
1258 irb_.SetInsertPoint(ne_neg_one);
1259 llvm::Value* ne_result;
1260 if (is_div) {
1261 ne_result = irb_.CreateSDiv(dividend, divisor);
1262 } else {
1263 ne_result = irb_.CreateSRem(dividend, divisor);
1264 }
1265 irb_.CreateBr(neg_one_cont);
1266
1267 irb_.SetInsertPoint(neg_one_cont);
1268 llvm::PHINode* result = irb_.CreatePHI(op_type, 2);
1269 result->addIncoming(eq_result, eq_neg_one);
1270 result->addIncoming(ne_result, ne_neg_one);
1271
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001272 return result;
1273}
1274
TDYa127ce4cc0d2012-11-18 16:59:53 -08001275void GBCExpanderPass::Expand_AllocaShadowFrame(llvm::Value* num_vregs_value) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001276 // Most of the codes refer to MethodCompiler::EmitPrologueAllocShadowFrame and
1277 // MethodCompiler::EmitPushShadowFrame
TDYa1278e950c12012-11-02 09:58:19 -07001278 uint16_t num_vregs =
1279 llvm::cast<llvm::ConstantInt>(num_vregs_value)->getZExtValue();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001280
1281 llvm::StructType* shadow_frame_type =
TDYa127ce4cc0d2012-11-18 16:59:53 -08001282 irb_.getShadowFrameTy(num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001283
Sebastien Hertz77209702013-02-28 16:34:13 +01001284 // Create allocas at the start of entry block.
1285 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1286 llvm::BasicBlock* entry_block = &func_->front();
1287 irb_.SetInsertPoint(&entry_block->front());
1288
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001289 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
1290
1291 // Alloca a pointer to old shadow frame
1292 old_shadow_frame_ =
1293 irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
1294
Sebastien Hertz77209702013-02-28 16:34:13 +01001295 irb_.restoreIP(irb_ip_original);
1296
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001297 // Push the shadow frame
1298 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1299
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001300 llvm::Value* shadow_frame_upcast =
1301 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
1302
1303 llvm::Value* result = rtb_.EmitPushShadowFrame(shadow_frame_upcast,
1304 method_object_addr,
TDYa1278e950c12012-11-02 09:58:19 -07001305 num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001306
1307 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
1308
1309 return;
1310}
1311
TDYa1278e950c12012-11-02 09:58:19 -07001312void GBCExpanderPass::Expand_SetVReg(llvm::Value* entry_idx,
1313 llvm::Value* value) {
Sebastien Hertzf11afa02013-03-19 17:39:29 +01001314 unsigned vreg_idx = LV2UInt(entry_idx);
1315 DCHECK_LT(vreg_idx, dex_compilation_unit_->GetCodeItem()->registers_size_);
TDYa1278e950c12012-11-02 09:58:19 -07001316
Sebastien Hertzf11afa02013-03-19 17:39:29 +01001317 llvm::Value* vreg_addr = shadow_frame_vreg_addresses_[vreg_idx];
1318 if (UNLIKELY(vreg_addr == NULL)) {
1319 DCHECK(shadow_frame_ != NULL);
TDYa1278e950c12012-11-02 09:58:19 -07001320
Sebastien Hertzf11afa02013-03-19 17:39:29 +01001321 llvm::Value* gep_index[] = {
1322 irb_.getInt32(0), // No pointer displacement
1323 irb_.getInt32(1), // VRegs
1324 entry_idx // Pointer field
1325 };
1326
1327 // A shadow frame address must dominate every use in the function so we
1328 // place it in the entry block right after the allocas.
1329 llvm::BasicBlock::iterator first_non_alloca = func_->getEntryBlock().begin();
1330 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1331 ++first_non_alloca;
1332 }
1333
1334 llvm::IRBuilderBase::InsertPoint ip = irb_.saveIP();
1335 irb_.SetInsertPoint(static_cast<llvm::Instruction*>(first_non_alloca));
1336 vreg_addr = irb_.CreateGEP(shadow_frame_, gep_index);
1337 shadow_frame_vreg_addresses_[vreg_idx] = vreg_addr;
1338 irb_.restoreIP(ip);
1339 }
TDYa1278e950c12012-11-02 09:58:19 -07001340
1341 irb_.CreateStore(value,
1342 irb_.CreateBitCast(vreg_addr, value->getType()->getPointerTo()),
1343 kTBAAShadowFrame);
1344 return;
1345}
1346
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001347void GBCExpanderPass::Expand_PopShadowFrame() {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001348 if (old_shadow_frame_ == NULL) {
1349 return;
1350 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001351 rtb_.EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
1352 return;
1353}
1354
1355void GBCExpanderPass::Expand_UpdateDexPC(llvm::Value* dex_pc_value) {
1356 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07001357 art::ShadowFrame::DexPCOffset(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001358 dex_pc_value,
1359 kTBAAShadowFrame);
1360 return;
1361}
1362
Logan Chien67645d82012-08-17 09:10:54 +08001363void GBCExpanderPass::InsertStackOverflowCheck(llvm::Function& func) {
jeffhao40283122013-01-15 13:15:24 -08001364 // All alloca instructions are generated in the first basic block of the
1365 // function, and there are no alloca instructions after the first non-alloca
1366 // instruction.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001367
Logan Chien67645d82012-08-17 09:10:54 +08001368 llvm::BasicBlock* first_basic_block = &func.front();
1369
1370 // Look for first non-alloca instruction
1371 llvm::BasicBlock::iterator first_non_alloca = first_basic_block->begin();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001372 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1373 ++first_non_alloca;
1374 }
1375
Logan Chien67645d82012-08-17 09:10:54 +08001376 irb_.SetInsertPoint(first_non_alloca);
1377
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001378 // Insert stack overflow check codes before first_non_alloca (i.e., after all
1379 // alloca instructions)
Logan Chien67645d82012-08-17 09:10:54 +08001380 EmitStackOverflowCheck(&*first_non_alloca);
1381
TDYa127890ea892012-08-22 10:49:42 -07001382 irb_.Runtime().EmitTestSuspend();
TDYa127890ea892012-08-22 10:49:42 -07001383
Logan Chien67645d82012-08-17 09:10:54 +08001384 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
1385 if (next_basic_block != first_basic_block) {
1386 // Splice the rest of the instruction to the continuing basic block
1387 next_basic_block->getInstList().splice(
1388 irb_.GetInsertPoint(), first_basic_block->getInstList(),
1389 first_non_alloca, first_basic_block->end());
1390
1391 // Rewrite the basic block
1392 RewriteBasicBlock(next_basic_block);
1393
1394 // Update the phi-instructions in the successor basic block
1395 UpdatePhiInstruction(first_basic_block, irb_.GetInsertBlock());
1396 }
1397
1398 // We have changed the basic block
1399 changed_ = true;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001400}
1401
TDYa1275e869b62012-07-25 00:45:39 -07001402// ==== High-level intrinsic expander ==========================================
1403
TDYa127a1b21852012-07-23 03:20:39 -07001404llvm::Value* GBCExpanderPass::Expand_FPCompare(llvm::Value* src1_value,
1405 llvm::Value* src2_value,
1406 bool gt_bias) {
1407 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1408 llvm::Value* cmp_lt;
1409
1410 if (gt_bias) {
1411 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1412 } else {
1413 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1414 }
1415
1416 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1417}
1418
1419llvm::Value* GBCExpanderPass::Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value) {
1420 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1421 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1422
1423 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1424}
1425
1426llvm::Value* GBCExpanderPass::EmitCompareResultSelection(llvm::Value* cmp_eq,
1427 llvm::Value* cmp_lt) {
1428
1429 llvm::Constant* zero = irb_.getJInt(0);
1430 llvm::Constant* pos1 = irb_.getJInt(1);
1431 llvm::Constant* neg1 = irb_.getJInt(-1);
1432
1433 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1434 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1435
1436 return result_eq;
1437}
1438
Logan Chien75e4b602012-07-23 14:24:12 -07001439llvm::Value* GBCExpanderPass::Expand_IntegerShift(llvm::Value* src1_value,
1440 llvm::Value* src2_value,
1441 IntegerShiftKind kind,
1442 JType op_jty) {
1443 DCHECK(op_jty == kInt || op_jty == kLong);
1444
1445 // Mask and zero-extend RHS properly
1446 if (op_jty == kInt) {
1447 src2_value = irb_.CreateAnd(src2_value, 0x1f);
1448 } else {
1449 llvm::Value* masked_src2_value = irb_.CreateAnd(src2_value, 0x3f);
1450 src2_value = irb_.CreateZExt(masked_src2_value, irb_.getJLongTy());
1451 }
1452
1453 // Create integer shift llvm instruction
1454 switch (kind) {
1455 case kIntegerSHL:
1456 return irb_.CreateShl(src1_value, src2_value);
1457
1458 case kIntegerSHR:
1459 return irb_.CreateAShr(src1_value, src2_value);
1460
1461 case kIntegerUSHR:
1462 return irb_.CreateLShr(src1_value, src2_value);
1463
1464 default:
1465 LOG(FATAL) << "Unknown integer shift kind: " << kind;
1466 return NULL;
1467 }
1468}
1469
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001470llvm::Value* GBCExpanderPass::SignOrZeroExtendCat1Types(llvm::Value* value, JType jty) {
1471 switch (jty) {
1472 case kBoolean:
1473 case kChar:
1474 return irb_.CreateZExt(value, irb_.getJType(kInt));
1475 case kByte:
1476 case kShort:
1477 return irb_.CreateSExt(value, irb_.getJType(kInt));
1478 case kVoid:
1479 case kInt:
1480 case kLong:
1481 case kFloat:
1482 case kDouble:
1483 case kObject:
1484 return value; // Nothing to do.
1485 default:
1486 LOG(FATAL) << "Unknown java type: " << jty;
1487 return NULL;
1488 }
1489}
1490
1491llvm::Value* GBCExpanderPass::TruncateCat1Types(llvm::Value* value, JType jty) {
1492 switch (jty) {
1493 case kBoolean:
1494 case kChar:
1495 case kByte:
1496 case kShort:
1497 return irb_.CreateTrunc(value, irb_.getJType(jty));
1498 case kVoid:
1499 case kInt:
1500 case kLong:
1501 case kFloat:
1502 case kDouble:
1503 case kObject:
1504 return value; // Nothing to do.
1505 default:
1506 LOG(FATAL) << "Unknown java type: " << jty;
1507 return NULL;
1508 }
1509}
1510
TDYa1275a26d442012-07-26 18:58:38 -07001511llvm::Value* GBCExpanderPass::Expand_HLArrayGet(llvm::CallInst& call_inst,
1512 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001513 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1514 llvm::Value* array_addr = call_inst.getArgOperand(1);
1515 llvm::Value* index_value = call_inst.getArgOperand(2);
TDYa127920be7c2012-09-10 17:13:22 -07001516 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001517
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001518 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1519 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1520 opt_flags);
TDYa1275a26d442012-07-26 18:58:38 -07001521
1522 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1523
1524 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
1525
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001526 return SignOrZeroExtendCat1Types(array_elem_value, elem_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001527}
1528
1529
1530void GBCExpanderPass::Expand_HLArrayPut(llvm::CallInst& call_inst,
1531 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001532 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1533 llvm::Value* new_value = call_inst.getArgOperand(1);
1534 llvm::Value* array_addr = call_inst.getArgOperand(2);
1535 llvm::Value* index_value = call_inst.getArgOperand(3);
TDYa127920be7c2012-09-10 17:13:22 -07001536 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001537
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001538 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1539 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1540 opt_flags);
TDYa1275a26d442012-07-26 18:58:38 -07001541
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001542 new_value = TruncateCat1Types(new_value, elem_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001543
1544 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1545
1546 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
1547 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::CheckPutArrayElement);
1548
1549 irb_.CreateCall2(runtime_func, new_value, array_addr);
1550
1551 EmitGuard_ExceptionLandingPad(dex_pc);
1552
1553 EmitMarkGCCard(new_value, array_addr);
1554 }
1555
1556 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1557
1558 return;
1559}
1560
TDYa1275e869b62012-07-25 00:45:39 -07001561llvm::Value* GBCExpanderPass::Expand_HLIGet(llvm::CallInst& call_inst,
1562 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001563 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1564 llvm::Value* object_addr = call_inst.getArgOperand(1);
1565 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(2));
TDYa127920be7c2012-09-10 17:13:22 -07001566 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001567
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001568 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -07001569
1570 llvm::Value* field_value;
1571
1572 int field_offset;
1573 bool is_volatile;
Ian Rogers1212a022013-03-04 10:48:41 -08001574 bool is_fast_path = driver_->ComputeInstanceFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001575 field_idx, dex_compilation_unit_, field_offset, is_volatile, false);
TDYa1275e869b62012-07-25 00:45:39 -07001576
1577 if (!is_fast_path) {
1578 llvm::Function* runtime_func;
1579
1580 if (field_jty == kObject) {
1581 runtime_func = irb_.GetRuntime(runtime_support::GetObjectInstance);
1582 } else if (field_jty == kLong || field_jty == kDouble) {
1583 runtime_func = irb_.GetRuntime(runtime_support::Get64Instance);
1584 } else {
1585 runtime_func = irb_.GetRuntime(runtime_support::Get32Instance);
1586 }
1587
1588 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
1589
1590 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1591
1592 EmitUpdateDexPC(dex_pc);
1593
1594 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
1595 method_object_addr, object_addr);
1596
1597 EmitGuard_ExceptionLandingPad(dex_pc);
1598
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001599 if (field_jty == kFloat || field_jty == kDouble) {
1600 field_value = irb_.CreateBitCast(field_value, irb_.getJType(field_jty));
1601 }
TDYa1275e869b62012-07-25 00:45:39 -07001602 } else {
1603 DCHECK_GE(field_offset, 0);
1604
1605 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001606 irb_.getJType(field_jty)->getPointerTo();
TDYa1275e869b62012-07-25 00:45:39 -07001607
1608 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
1609
1610 llvm::Value* field_addr =
1611 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1612
TDYa1275e869b62012-07-25 00:45:39 -07001613 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001614 field_value = SignOrZeroExtendCat1Types(field_value, field_jty);
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001615
1616 if (is_volatile) {
1617 irb_.CreateMemoryBarrier(art::kLoadLoad);
1618 }
TDYa1275e869b62012-07-25 00:45:39 -07001619 }
1620
TDYa1275e869b62012-07-25 00:45:39 -07001621 return field_value;
1622}
1623
1624void GBCExpanderPass::Expand_HLIPut(llvm::CallInst& call_inst,
1625 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001626 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001627 llvm::Value* new_value = call_inst.getArgOperand(1);
1628 llvm::Value* object_addr = call_inst.getArgOperand(2);
TDYa1275e869b62012-07-25 00:45:39 -07001629 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(3));
TDYa127920be7c2012-09-10 17:13:22 -07001630 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001631
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001632 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -07001633
1634 int field_offset;
1635 bool is_volatile;
Ian Rogers1212a022013-03-04 10:48:41 -08001636 bool is_fast_path = driver_->ComputeInstanceFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001637 field_idx, dex_compilation_unit_, field_offset, is_volatile, true);
TDYa1275e869b62012-07-25 00:45:39 -07001638
1639 if (!is_fast_path) {
1640 llvm::Function* runtime_func;
1641
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001642 if (field_jty == kFloat) {
1643 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1644 } else if (field_jty == kDouble) {
1645 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1646 }
1647
TDYa1275e869b62012-07-25 00:45:39 -07001648 if (field_jty == kObject) {
1649 runtime_func = irb_.GetRuntime(runtime_support::SetObjectInstance);
1650 } else if (field_jty == kLong || field_jty == kDouble) {
1651 runtime_func = irb_.GetRuntime(runtime_support::Set64Instance);
1652 } else {
1653 runtime_func = irb_.GetRuntime(runtime_support::Set32Instance);
1654 }
1655
1656 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1657
1658 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1659
1660 EmitUpdateDexPC(dex_pc);
1661
1662 irb_.CreateCall4(runtime_func, field_idx_value,
1663 method_object_addr, object_addr, new_value);
1664
1665 EmitGuard_ExceptionLandingPad(dex_pc);
1666
1667 } else {
1668 DCHECK_GE(field_offset, 0);
1669
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001670 if (is_volatile) {
1671 irb_.CreateMemoryBarrier(art::kStoreStore);
1672 }
1673
TDYa1275e869b62012-07-25 00:45:39 -07001674 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001675 irb_.getJType(field_jty)->getPointerTo();
TDYa1275e869b62012-07-25 00:45:39 -07001676
1677 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
1678
1679 llvm::Value* field_addr =
1680 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1681
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001682 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275e869b62012-07-25 00:45:39 -07001683 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1684
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001685 if (is_volatile) {
1686 irb_.CreateMemoryBarrier(art::kLoadLoad);
1687 }
1688
TDYa1275e869b62012-07-25 00:45:39 -07001689 if (field_jty == kObject) { // If put an object, mark the GC card table.
1690 EmitMarkGCCard(new_value, object_addr);
1691 }
1692 }
1693
1694 return;
1695}
1696
TDYa127f71bf5a2012-07-29 20:09:52 -07001697llvm::Value* GBCExpanderPass::EmitLoadConstantClass(uint32_t dex_pc,
1698 uint32_t type_idx) {
Ian Rogers89756f22013-03-04 16:40:02 -08001699 if (!driver_->CanAccessTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
1700 *dex_compilation_unit_->GetDexFile(), type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001701 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1702
1703 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1704
1705 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1706
1707 llvm::Function* runtime_func =
1708 irb_.GetRuntime(runtime_support::InitializeTypeAndVerifyAccess);
1709
1710 EmitUpdateDexPC(dex_pc);
1711
1712 llvm::Value* type_object_addr =
1713 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1714
1715 EmitGuard_ExceptionLandingPad(dex_pc);
1716
1717 return type_object_addr;
1718
1719 } else {
1720 // Try to load the class (type) object from the test cache.
1721 llvm::Value* type_field_addr =
1722 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1723
TDYa127ce4cc0d2012-11-18 16:59:53 -08001724 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07001725
Ian Rogers89756f22013-03-04 16:40:02 -08001726 if (driver_->CanAssumeTypeIsPresentInDexCache(*dex_compilation_unit_->GetDexFile(), type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001727 return type_object_addr;
1728 }
1729
1730 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1731
1732 // Test whether class (type) object is in the dex cache or not
1733 llvm::Value* equal_null =
1734 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1735
1736 llvm::BasicBlock* block_cont =
1737 CreateBasicBlockWithDexPC(dex_pc, "cont");
1738
1739 llvm::BasicBlock* block_load_class =
1740 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1741
1742 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
1743
1744 // Failback routine to load the class object
1745 irb_.SetInsertPoint(block_load_class);
1746
1747 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeType);
1748
1749 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1750
1751 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1752
1753 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1754
1755 EmitUpdateDexPC(dex_pc);
1756
1757 llvm::Value* loaded_type_object_addr =
1758 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1759
1760 EmitGuard_ExceptionLandingPad(dex_pc);
1761
1762 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1763
1764 irb_.CreateBr(block_cont);
1765
1766 // Now the class object must be loaded
1767 irb_.SetInsertPoint(block_cont);
1768
1769 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1770
1771 phi->addIncoming(type_object_addr, block_original);
1772 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1773
1774 return phi;
1775 }
1776}
1777
TDYa1275a26d442012-07-26 18:58:38 -07001778llvm::Value* GBCExpanderPass::EmitLoadStaticStorage(uint32_t dex_pc,
1779 uint32_t type_idx) {
1780 llvm::BasicBlock* block_load_static =
1781 CreateBasicBlockWithDexPC(dex_pc, "load_static");
1782
1783 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1784
1785 // Load static storage from dex cache
1786 llvm::Value* storage_field_addr =
1787 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1788
TDYa127ce4cc0d2012-11-18 16:59:53 -08001789 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
TDYa1275a26d442012-07-26 18:58:38 -07001790
1791 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1792
1793 // Test: Is the static storage of this class initialized?
1794 llvm::Value* equal_null =
1795 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
1796
1797 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
1798
1799 // Failback routine to load the class object
1800 irb_.SetInsertPoint(block_load_static);
1801
1802 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeStaticStorage);
1803
1804 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1805
1806 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1807
1808 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1809
1810 EmitUpdateDexPC(dex_pc);
1811
1812 llvm::Value* loaded_storage_object_addr =
1813 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1814
1815 EmitGuard_ExceptionLandingPad(dex_pc);
1816
1817 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
1818
1819 irb_.CreateBr(block_cont);
1820
1821 // Now the class object must be loaded
1822 irb_.SetInsertPoint(block_cont);
1823
1824 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1825
1826 phi->addIncoming(storage_object_addr, block_original);
1827 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
1828
1829 return phi;
1830}
1831
1832llvm::Value* GBCExpanderPass::Expand_HLSget(llvm::CallInst& call_inst,
1833 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001834 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1835 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1836
1837 int field_offset;
1838 int ssb_index;
1839 bool is_referrers_class;
1840 bool is_volatile;
1841
Ian Rogers1212a022013-03-04 10:48:41 -08001842 bool is_fast_path = driver_->ComputeStaticFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001843 field_idx, dex_compilation_unit_, field_offset, ssb_index,
TDYa1275a26d442012-07-26 18:58:38 -07001844 is_referrers_class, is_volatile, false);
1845
1846 llvm::Value* static_field_value;
1847
1848 if (!is_fast_path) {
1849 llvm::Function* runtime_func;
1850
1851 if (field_jty == kObject) {
1852 runtime_func = irb_.GetRuntime(runtime_support::GetObjectStatic);
1853 } else if (field_jty == kLong || field_jty == kDouble) {
1854 runtime_func = irb_.GetRuntime(runtime_support::Get64Static);
1855 } else {
1856 runtime_func = irb_.GetRuntime(runtime_support::Get32Static);
1857 }
1858
1859 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1860
1861 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1862
1863 EmitUpdateDexPC(dex_pc);
1864
1865 static_field_value =
1866 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
1867
1868 EmitGuard_ExceptionLandingPad(dex_pc);
1869
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001870 if (field_jty == kFloat || field_jty == kDouble) {
1871 static_field_value = irb_.CreateBitCast(static_field_value, irb_.getJType(field_jty));
1872 }
TDYa1275a26d442012-07-26 18:58:38 -07001873 } else {
1874 DCHECK_GE(field_offset, 0);
1875
1876 llvm::Value* static_storage_addr = NULL;
1877
1878 if (is_referrers_class) {
1879 // Fast path, static storage base is this method's class
1880 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1881
1882 static_storage_addr =
1883 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001884 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001885 irb_.getJObjectTy(),
1886 kTBAAConstJObject);
1887 } else {
1888 // Medium path, static storage base in a different class which
1889 // requires checks that the other class is initialized
1890 DCHECK_GE(ssb_index, 0);
1891 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1892 }
1893
1894 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1895
1896 llvm::Value* static_field_addr =
1897 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001898 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001899
TDYa1275a26d442012-07-26 18:58:38 -07001900 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001901 static_field_value = SignOrZeroExtendCat1Types(static_field_value, field_jty);
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001902
1903 if (is_volatile) {
1904 irb_.CreateMemoryBarrier(art::kLoadLoad);
1905 }
TDYa1275a26d442012-07-26 18:58:38 -07001906 }
1907
TDYa1275a26d442012-07-26 18:58:38 -07001908 return static_field_value;
1909}
1910
1911void GBCExpanderPass::Expand_HLSput(llvm::CallInst& call_inst,
1912 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001913 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1914 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1915 llvm::Value* new_value = call_inst.getArgOperand(1);
1916
1917 if (field_jty == kFloat || field_jty == kDouble) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001918 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty));
TDYa1275a26d442012-07-26 18:58:38 -07001919 }
1920
1921 int field_offset;
1922 int ssb_index;
1923 bool is_referrers_class;
1924 bool is_volatile;
1925
Ian Rogers1212a022013-03-04 10:48:41 -08001926 bool is_fast_path = driver_->ComputeStaticFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001927 field_idx, dex_compilation_unit_, field_offset, ssb_index,
TDYa1275a26d442012-07-26 18:58:38 -07001928 is_referrers_class, is_volatile, true);
1929
1930 if (!is_fast_path) {
1931 llvm::Function* runtime_func;
1932
1933 if (field_jty == kObject) {
1934 runtime_func = irb_.GetRuntime(runtime_support::SetObjectStatic);
1935 } else if (field_jty == kLong || field_jty == kDouble) {
1936 runtime_func = irb_.GetRuntime(runtime_support::Set64Static);
1937 } else {
1938 runtime_func = irb_.GetRuntime(runtime_support::Set32Static);
1939 }
1940
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001941 if (field_jty == kFloat) {
1942 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1943 } else if (field_jty == kDouble) {
1944 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1945 }
1946
TDYa1275a26d442012-07-26 18:58:38 -07001947 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1948
1949 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1950
1951 EmitUpdateDexPC(dex_pc);
1952
1953 irb_.CreateCall3(runtime_func, field_idx_value,
1954 method_object_addr, new_value);
1955
1956 EmitGuard_ExceptionLandingPad(dex_pc);
1957
1958 } else {
1959 DCHECK_GE(field_offset, 0);
1960
1961 llvm::Value* static_storage_addr = NULL;
1962
1963 if (is_referrers_class) {
1964 // Fast path, static storage base is this method's class
1965 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1966
1967 static_storage_addr =
1968 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001969 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001970 irb_.getJObjectTy(),
1971 kTBAAConstJObject);
1972 } else {
1973 // Medium path, static storage base in a different class which
1974 // requires checks that the other class is initialized
1975 DCHECK_GE(ssb_index, 0);
1976 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1977 }
1978
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001979 if (is_volatile) {
1980 irb_.CreateMemoryBarrier(art::kStoreStore);
1981 }
1982
TDYa1275a26d442012-07-26 18:58:38 -07001983 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1984
1985 llvm::Value* static_field_addr =
1986 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001987 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001988
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001989 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001990 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1991
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001992 if (is_volatile) {
1993 irb_.CreateMemoryBarrier(art::kStoreLoad);
1994 }
1995
TDYa1275a26d442012-07-26 18:58:38 -07001996 if (field_jty == kObject) { // If put an object, mark the GC card table.
1997 EmitMarkGCCard(new_value, static_storage_addr);
1998 }
1999 }
2000
2001 return;
2002}
2003
TDYa127f71bf5a2012-07-29 20:09:52 -07002004llvm::Value* GBCExpanderPass::Expand_ConstString(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002005 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2006 uint32_t string_idx = LV2UInt(call_inst.getArgOperand(0));
2007
2008 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
2009
TDYa127ce4cc0d2012-11-18 16:59:53 -08002010 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07002011
Ian Rogers89756f22013-03-04 16:40:02 -08002012 if (!driver_->CanAssumeStringIsPresentInDexCache(*dex_compilation_unit_->GetDexFile(),
2013 string_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002014 llvm::BasicBlock* block_str_exist =
2015 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
2016
2017 llvm::BasicBlock* block_str_resolve =
2018 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
2019
2020 llvm::BasicBlock* block_cont =
2021 CreateBasicBlockWithDexPC(dex_pc, "str_cont");
2022
2023 // Test: Is the string resolved and in the dex cache?
2024 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
2025
2026 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
2027
2028 // String is resolved, go to next basic block.
2029 irb_.SetInsertPoint(block_str_exist);
2030 irb_.CreateBr(block_cont);
2031
2032 // String is not resolved yet, resolve it now.
2033 irb_.SetInsertPoint(block_str_resolve);
2034
2035 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::ResolveString);
2036
2037 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2038
2039 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
2040
2041 EmitUpdateDexPC(dex_pc);
2042
2043 llvm::Value* result = irb_.CreateCall2(runtime_func, method_object_addr,
2044 string_idx_value);
2045
2046 EmitGuard_ExceptionLandingPad(dex_pc);
2047
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002048 irb_.CreateBr(block_cont);
2049
2050
TDYa127f71bf5a2012-07-29 20:09:52 -07002051 llvm::BasicBlock* block_pre_cont = irb_.GetInsertBlock();
2052
2053 irb_.SetInsertPoint(block_cont);
2054
2055 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2056
2057 phi->addIncoming(string_addr, block_str_exist);
2058 phi->addIncoming(result, block_pre_cont);
2059
2060 string_addr = phi;
2061 }
2062
2063 return string_addr;
2064}
2065
2066llvm::Value* GBCExpanderPass::Expand_ConstClass(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002067 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2068 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2069
2070 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
2071
2072 return type_object_addr;
2073}
2074
2075void GBCExpanderPass::Expand_MonitorEnter(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002076 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2077 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002078 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002079
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002080 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002081
TDYa127ce4cc0d2012-11-18 16:59:53 -08002082 EmitUpdateDexPC(dex_pc);
2083
TDYa127f71bf5a2012-07-29 20:09:52 -07002084 irb_.Runtime().EmitLockObject(object_addr);
2085
2086 return;
2087}
2088
2089void GBCExpanderPass::Expand_MonitorExit(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002090 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2091 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002092 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002093
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002094 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002095
2096 EmitUpdateDexPC(dex_pc);
2097
2098 irb_.Runtime().EmitUnlockObject(object_addr);
2099
2100 EmitGuard_ExceptionLandingPad(dex_pc);
2101
2102 return;
2103}
2104
2105void GBCExpanderPass::Expand_HLCheckCast(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002106 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2107 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2108 llvm::Value* object_addr = call_inst.getArgOperand(1);
2109
2110 llvm::BasicBlock* block_test_class =
2111 CreateBasicBlockWithDexPC(dex_pc, "test_class");
2112
2113 llvm::BasicBlock* block_test_sub_class =
2114 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
2115
2116 llvm::BasicBlock* block_cont =
2117 CreateBasicBlockWithDexPC(dex_pc, "checkcast_cont");
2118
2119 // Test: Is the reference equal to null? Act as no-op when it is null.
2120 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2121
Ian Rogers8e696052013-03-04 09:00:40 -08002122 irb_.CreateCondBr(equal_null, block_cont, block_test_class, kUnlikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002123
2124 // Test: Is the object instantiated from the given class?
2125 irb_.SetInsertPoint(block_test_class);
2126 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08002127 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002128
2129 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2130
2131 llvm::Value* object_type_field_addr =
2132 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2133
2134 llvm::Value* object_type_object_addr =
2135 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2136
2137 llvm::Value* equal_class =
2138 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2139
Ian Rogers8e696052013-03-04 09:00:40 -08002140 irb_.CreateCondBr(equal_class, block_cont, block_test_sub_class, kLikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002141
2142 // Test: Is the object instantiated from the subclass of the given class?
2143 irb_.SetInsertPoint(block_test_sub_class);
2144
2145 EmitUpdateDexPC(dex_pc);
2146
2147 irb_.CreateCall2(irb_.GetRuntime(runtime_support::CheckCast),
2148 type_object_addr, object_type_object_addr);
2149
2150 EmitGuard_ExceptionLandingPad(dex_pc);
2151
2152 irb_.CreateBr(block_cont);
2153
2154 irb_.SetInsertPoint(block_cont);
2155
2156 return;
2157}
2158
2159llvm::Value* GBCExpanderPass::Expand_InstanceOf(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002160 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2161 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2162 llvm::Value* object_addr = call_inst.getArgOperand(1);
2163
2164 llvm::BasicBlock* block_nullp =
2165 CreateBasicBlockWithDexPC(dex_pc, "nullp");
2166
2167 llvm::BasicBlock* block_test_class =
2168 CreateBasicBlockWithDexPC(dex_pc, "test_class");
2169
2170 llvm::BasicBlock* block_class_equals =
2171 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
2172
2173 llvm::BasicBlock* block_test_sub_class =
2174 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
2175
2176 llvm::BasicBlock* block_cont =
2177 CreateBasicBlockWithDexPC(dex_pc, "instance_of_cont");
2178
2179 // Overview of the following code :
2180 // We check for null, if so, then false, otherwise check for class == . If so
2181 // then true, otherwise do callout slowpath.
2182 //
2183 // Test: Is the reference equal to null? Set 0 when it is null.
2184 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2185
Ian Rogers8e696052013-03-04 09:00:40 -08002186 irb_.CreateCondBr(equal_null, block_nullp, block_test_class, kUnlikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002187
2188 irb_.SetInsertPoint(block_nullp);
2189 irb_.CreateBr(block_cont);
2190
2191 // Test: Is the object instantiated from the given class?
2192 irb_.SetInsertPoint(block_test_class);
2193 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08002194 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002195
2196 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2197
2198 llvm::Value* object_type_field_addr =
2199 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2200
2201 llvm::Value* object_type_object_addr =
2202 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2203
2204 llvm::Value* equal_class =
2205 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2206
Ian Rogers8e696052013-03-04 09:00:40 -08002207 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class, kLikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002208
2209 irb_.SetInsertPoint(block_class_equals);
2210 irb_.CreateBr(block_cont);
2211
2212 // Test: Is the object instantiated from the subclass of the given class?
2213 irb_.SetInsertPoint(block_test_sub_class);
2214 llvm::Value* result =
2215 irb_.CreateCall2(irb_.GetRuntime(runtime_support::IsAssignable),
2216 type_object_addr, object_type_object_addr);
2217 irb_.CreateBr(block_cont);
2218
2219 irb_.SetInsertPoint(block_cont);
2220
2221 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJIntTy(), 3);
2222
2223 phi->addIncoming(irb_.getJInt(0), block_nullp);
2224 phi->addIncoming(irb_.getJInt(1), block_class_equals);
2225 phi->addIncoming(result, block_test_sub_class);
2226
2227 return phi;
2228}
2229
2230llvm::Value* GBCExpanderPass::Expand_NewInstance(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002231 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2232 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2233
2234 llvm::Function* runtime_func;
Ian Rogers89756f22013-03-04 16:40:02 -08002235 if (driver_->CanAccessInstantiableTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
2236 *dex_compilation_unit_->GetDexFile(),
2237 type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002238 runtime_func = irb_.GetRuntime(runtime_support::AllocObject);
2239 } else {
2240 runtime_func = irb_.GetRuntime(runtime_support::AllocObjectWithAccessCheck);
2241 }
2242
2243 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2244
2245 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2246
2247 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2248
2249 EmitUpdateDexPC(dex_pc);
2250
2251 llvm::Value* object_addr =
2252 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
2253
2254 EmitGuard_ExceptionLandingPad(dex_pc);
2255
2256 return object_addr;
2257}
2258
2259llvm::Value* GBCExpanderPass::Expand_HLInvoke(llvm::CallInst& call_inst) {
TDYa127920be7c2012-09-10 17:13:22 -07002260 art::InvokeType invoke_type = static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
2261 bool is_static = (invoke_type == art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002262
2263 if (!is_static) {
2264 // Test: Is *this* parameter equal to null?
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002265 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2266 llvm::Value* this_addr = call_inst.getArgOperand(3);
2267 int opt_flags = LV2UInt(call_inst.getArgOperand(2));
2268
2269 EmitGuard_NullPointerException(dex_pc, this_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002270 }
2271
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002272 llvm::Value* result = NULL;
2273 if (EmitIntrinsic(call_inst, &result)) {
2274 return result;
TDYa127f71bf5a2012-07-29 20:09:52 -07002275 }
2276
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002277 return EmitInvoke(call_inst);
TDYa127f71bf5a2012-07-29 20:09:52 -07002278}
2279
2280llvm::Value* GBCExpanderPass::Expand_OptArrayLength(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002281 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2282 // Get the array object address
2283 llvm::Value* array_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002284 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002285
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002286 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002287
2288 // Get the array length and store it to the register
2289 return EmitLoadArrayLength(array_addr);
2290}
2291
2292llvm::Value* GBCExpanderPass::Expand_NewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002293 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2294 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2295 llvm::Value* length = call_inst.getArgOperand(1);
2296
2297 return EmitAllocNewArray(dex_pc, length, type_idx, false);
2298}
2299
2300llvm::Value* GBCExpanderPass::Expand_HLFilledNewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002301 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2302 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(1));
2303 uint32_t length = call_inst.getNumArgOperands() - 3;
2304
2305 llvm::Value* object_addr =
2306 EmitAllocNewArray(dex_pc, irb_.getInt32(length), type_idx, true);
2307
2308 if (length > 0) {
2309 // Check for the element type
2310 uint32_t type_desc_len = 0;
2311 const char* type_desc =
Ian Rogers89756f22013-03-04 16:40:02 -08002312 dex_compilation_unit_->GetDexFile()->StringByTypeIdx(type_idx, &type_desc_len);
TDYa127f71bf5a2012-07-29 20:09:52 -07002313
2314 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
2315 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
2316 bool is_elem_int_ty = (type_desc[1] == 'I');
2317
2318 uint32_t alignment;
2319 llvm::Constant* elem_size;
2320 llvm::PointerType* field_type;
2321
2322 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
2323 // as the element, thus we are only checking 2 cases: primitive int and
2324 // non-primitive type.
2325 if (is_elem_int_ty) {
2326 alignment = sizeof(int32_t);
2327 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
2328 field_type = irb_.getJIntTy()->getPointerTo();
2329 } else {
2330 alignment = irb_.getSizeOfPtrEquivInt();
2331 elem_size = irb_.getSizeOfPtrEquivIntValue();
2332 field_type = irb_.getJObjectTy()->getPointerTo();
2333 }
2334
2335 llvm::Value* data_field_offset =
Ian Rogers98573f92013-01-30 17:26:32 -08002336 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
TDYa127f71bf5a2012-07-29 20:09:52 -07002337
2338 llvm::Value* data_field_addr =
2339 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
2340
2341 // TODO: Tune this code. Currently we are generating one instruction for
2342 // one element which may be very space consuming. Maybe changing to use
2343 // memcpy may help; however, since we can't guarantee that the alloca of
2344 // dalvik register are continuous, we can't perform such optimization yet.
2345 for (uint32_t i = 0; i < length; ++i) {
2346 llvm::Value* reg_value = call_inst.getArgOperand(i+3);
2347
2348 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
2349
2350 data_field_addr =
2351 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
2352 }
2353 }
2354
2355 return object_addr;
2356}
2357
2358void GBCExpanderPass::Expand_HLFillArrayData(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002359 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2360 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
2361 LV2SInt(call_inst.getArgOperand(0));
2362 llvm::Value* array_addr = call_inst.getArgOperand(1);
2363
TDYa127920be7c2012-09-10 17:13:22 -07002364 const art::Instruction::ArrayDataPayload* payload =
2365 reinterpret_cast<const art::Instruction::ArrayDataPayload*>(
Ian Rogers89756f22013-03-04 16:40:02 -08002366 dex_compilation_unit_->GetCodeItem()->insns_ + payload_offset);
TDYa127f71bf5a2012-07-29 20:09:52 -07002367
2368 if (payload->element_count == 0) {
2369 // When the number of the elements in the payload is zero, we don't have
2370 // to copy any numbers. However, we should check whether the array object
2371 // address is equal to null or not.
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002372 EmitGuard_NullPointerException(dex_pc, array_addr, 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002373 } else {
2374 // To save the code size, we are going to call the runtime function to
2375 // copy the content from DexFile.
2376
2377 // NOTE: We will check for the NullPointerException in the runtime.
2378
2379 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::FillArrayData);
2380
2381 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2382
2383 EmitUpdateDexPC(dex_pc);
2384
2385 irb_.CreateCall4(runtime_func,
2386 method_object_addr, irb_.getInt32(dex_pc),
2387 array_addr, irb_.getInt32(payload_offset));
2388
2389 EmitGuard_ExceptionLandingPad(dex_pc);
2390 }
2391
2392 return;
2393}
2394
2395llvm::Value* GBCExpanderPass::EmitAllocNewArray(uint32_t dex_pc,
2396 llvm::Value* array_length_value,
2397 uint32_t type_idx,
2398 bool is_filled_new_array) {
2399 llvm::Function* runtime_func;
2400
2401 bool skip_access_check =
Ian Rogers89756f22013-03-04 16:40:02 -08002402 driver_->CanAccessTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
2403 *dex_compilation_unit_->GetDexFile(), type_idx);
TDYa127f71bf5a2012-07-29 20:09:52 -07002404
2405
2406 if (is_filled_new_array) {
2407 runtime_func = skip_access_check ?
2408 irb_.GetRuntime(runtime_support::CheckAndAllocArray) :
2409 irb_.GetRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck);
2410 } else {
2411 runtime_func = skip_access_check ?
2412 irb_.GetRuntime(runtime_support::AllocArray) :
2413 irb_.GetRuntime(runtime_support::AllocArrayWithAccessCheck);
2414 }
2415
2416 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2417
2418 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2419
2420 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2421
2422 EmitUpdateDexPC(dex_pc);
2423
2424 llvm::Value* object_addr =
2425 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
2426 array_length_value, thread_object_addr);
2427
2428 EmitGuard_ExceptionLandingPad(dex_pc);
2429
2430 return object_addr;
2431}
2432
2433llvm::Value* GBCExpanderPass::
2434EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -07002435 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -07002436 llvm::Value* this_addr,
2437 uint32_t dex_pc,
2438 bool is_fast_path) {
2439
2440 llvm::Function* runtime_func = NULL;
2441
2442 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002443 case art::kStatic:
TDYa127f71bf5a2012-07-29 20:09:52 -07002444 runtime_func = irb_.GetRuntime(runtime_support::FindStaticMethodWithAccessCheck);
2445 break;
2446
TDYa127920be7c2012-09-10 17:13:22 -07002447 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002448 runtime_func = irb_.GetRuntime(runtime_support::FindDirectMethodWithAccessCheck);
2449 break;
2450
TDYa127920be7c2012-09-10 17:13:22 -07002451 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002452 runtime_func = irb_.GetRuntime(runtime_support::FindVirtualMethodWithAccessCheck);
2453 break;
2454
TDYa127920be7c2012-09-10 17:13:22 -07002455 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002456 runtime_func = irb_.GetRuntime(runtime_support::FindSuperMethodWithAccessCheck);
2457 break;
2458
TDYa127920be7c2012-09-10 17:13:22 -07002459 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002460 if (is_fast_path) {
2461 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethod);
2462 } else {
2463 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethodWithAccessCheck);
2464 }
2465 break;
2466 }
2467
2468 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2469
2470 if (this_addr == NULL) {
TDYa127920be7c2012-09-10 17:13:22 -07002471 DCHECK_EQ(invoke_type, art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002472 this_addr = irb_.getJNull();
2473 }
2474
2475 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2476
2477 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2478
2479 EmitUpdateDexPC(dex_pc);
2480
2481 llvm::Value* callee_method_object_addr =
2482 irb_.CreateCall4(runtime_func,
2483 callee_method_idx_value,
2484 this_addr,
2485 caller_method_object_addr,
2486 thread_object_addr);
2487
2488 EmitGuard_ExceptionLandingPad(dex_pc);
2489
2490 return callee_method_object_addr;
2491}
2492
TDYa1275e869b62012-07-25 00:45:39 -07002493void GBCExpanderPass::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2494 // Using runtime support, let the target can override by InlineAssembly.
2495 irb_.Runtime().EmitMarkGCCard(value, target_addr);
2496}
2497
2498void GBCExpanderPass::EmitUpdateDexPC(uint32_t dex_pc) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002499 if (shadow_frame_ == NULL) {
2500 return;
2501 }
TDYa1275e869b62012-07-25 00:45:39 -07002502 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07002503 art::ShadowFrame::DexPCOffset(),
TDYa1275e869b62012-07-25 00:45:39 -07002504 irb_.getInt32(dex_pc),
2505 kTBAAShadowFrame);
2506}
2507
2508void GBCExpanderPass::EmitGuard_DivZeroException(uint32_t dex_pc,
2509 llvm::Value* denominator,
2510 JType op_jty) {
2511 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2512
2513 llvm::Constant* zero = irb_.getJZero(op_jty);
2514
2515 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2516
2517 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2518
2519 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2520
2521 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
2522
2523 irb_.SetInsertPoint(block_exception);
2524 EmitUpdateDexPC(dex_pc);
2525 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowDivZeroException));
2526 EmitBranchExceptionLandingPad(dex_pc);
2527
2528 irb_.SetInsertPoint(block_continue);
2529}
2530
2531void GBCExpanderPass::EmitGuard_NullPointerException(uint32_t dex_pc,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002532 llvm::Value* object,
2533 int opt_flags) {
2534 bool ignore_null_check = ((opt_flags & MIR_IGNORE_NULL_CHECK) != 0);
2535 if (ignore_null_check) {
2536 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2537 if (lpad) {
2538 // There is at least one catch: create a "fake" conditional branch to
2539 // keep the exception edge to the catch block.
2540 landing_pad_phi_mapping_[lpad].push_back(
2541 std::make_pair(current_bb_->getUniquePredecessor(),
2542 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002543
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002544 llvm::BasicBlock* block_continue =
2545 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002546
Ian Rogers8e696052013-03-04 09:00:40 -08002547 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002548
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002549 irb_.SetInsertPoint(block_continue);
2550 }
2551 } else {
2552 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
TDYa1275e869b62012-07-25 00:45:39 -07002553
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002554 llvm::BasicBlock* block_exception =
2555 CreateBasicBlockWithDexPC(dex_pc, "nullp");
TDYa1275e869b62012-07-25 00:45:39 -07002556
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002557 llvm::BasicBlock* block_continue =
2558 CreateBasicBlockWithDexPC(dex_pc, "cont");
2559
2560 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
2561
2562 irb_.SetInsertPoint(block_exception);
2563 EmitUpdateDexPC(dex_pc);
2564 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowNullPointerException),
2565 irb_.getInt32(dex_pc));
2566 EmitBranchExceptionLandingPad(dex_pc);
2567
2568 irb_.SetInsertPoint(block_continue);
2569 }
TDYa1275e869b62012-07-25 00:45:39 -07002570}
2571
2572void
2573GBCExpanderPass::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2574 llvm::Value* array,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002575 llvm::Value* index,
2576 int opt_flags) {
2577 bool ignore_range_check = ((opt_flags & MIR_IGNORE_RANGE_CHECK) != 0);
2578 if (ignore_range_check) {
2579 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2580 if (lpad) {
2581 // There is at least one catch: create a "fake" conditional branch to
2582 // keep the exception edge to the catch block.
2583 landing_pad_phi_mapping_[lpad].push_back(
2584 std::make_pair(current_bb_->getUniquePredecessor(),
2585 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002586
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002587 llvm::BasicBlock* block_continue =
2588 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002589
Ian Rogers8e696052013-03-04 09:00:40 -08002590 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002591
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002592 irb_.SetInsertPoint(block_continue);
2593 }
2594 } else {
2595 llvm::Value* array_len = EmitLoadArrayLength(array);
TDYa1275e869b62012-07-25 00:45:39 -07002596
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002597 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
TDYa1275e869b62012-07-25 00:45:39 -07002598
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002599 llvm::BasicBlock* block_exception =
2600 CreateBasicBlockWithDexPC(dex_pc, "overflow");
TDYa1275e869b62012-07-25 00:45:39 -07002601
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002602 llvm::BasicBlock* block_continue =
2603 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002604
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002605 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
2606
2607 irb_.SetInsertPoint(block_exception);
2608
2609 EmitUpdateDexPC(dex_pc);
2610 irb_.CreateCall2(irb_.GetRuntime(runtime_support::ThrowIndexOutOfBounds), index, array_len);
2611 EmitBranchExceptionLandingPad(dex_pc);
2612
2613 irb_.SetInsertPoint(block_continue);
2614 }
TDYa1275e869b62012-07-25 00:45:39 -07002615}
2616
Ian Rogers8e696052013-03-04 09:00:40 -08002617llvm::FunctionType* GBCExpanderPass::GetFunctionType(llvm::Type* ret_type, uint32_t method_idx,
TDYa1275e869b62012-07-25 00:45:39 -07002618 bool is_static) {
2619 // Get method signature
Ian Rogers8e696052013-03-04 09:00:40 -08002620 art::DexFile::MethodId const& method_id =
Ian Rogers89756f22013-03-04 16:40:02 -08002621 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx);
TDYa1275e869b62012-07-25 00:45:39 -07002622
2623 uint32_t shorty_size;
Ian Rogers89756f22013-03-04 16:40:02 -08002624 const char* shorty = dex_compilation_unit_->GetDexFile()->GetMethodShorty(method_id, &shorty_size);
TDYa1275e869b62012-07-25 00:45:39 -07002625 CHECK_GE(shorty_size, 1u);
2626
TDYa1275e869b62012-07-25 00:45:39 -07002627 // Get argument type
2628 std::vector<llvm::Type*> args_type;
2629
2630 args_type.push_back(irb_.getJObjectTy()); // method object pointer
2631
2632 if (!is_static) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002633 args_type.push_back(irb_.getJType('L')); // "this" object pointer
TDYa1275e869b62012-07-25 00:45:39 -07002634 }
2635
2636 for (uint32_t i = 1; i < shorty_size; ++i) {
buzbee26f10ee2012-12-21 11:16:29 -08002637 char shorty_type = art::RemapShorty(shorty[i]);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002638 args_type.push_back(irb_.getJType(shorty_type));
TDYa1275e869b62012-07-25 00:45:39 -07002639 }
2640
2641 return llvm::FunctionType::get(ret_type, args_type, false);
2642}
2643
2644
2645llvm::BasicBlock* GBCExpanderPass::
2646CreateBasicBlockWithDexPC(uint32_t dex_pc, const char* postfix) {
2647 std::string name;
2648
2649#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002650 art::StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
TDYa1275e869b62012-07-25 00:45:39 -07002651#endif
2652
2653 return llvm::BasicBlock::Create(context_, name, func_);
2654}
2655
2656llvm::BasicBlock* GBCExpanderPass::GetBasicBlock(uint32_t dex_pc) {
Ian Rogers89756f22013-03-04 16:40:02 -08002657 DCHECK(dex_pc < dex_compilation_unit_->GetCodeItem()->insns_size_in_code_units_);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002658 CHECK(basic_blocks_[dex_pc] != NULL);
TDYa1275e869b62012-07-25 00:45:39 -07002659 return basic_blocks_[dex_pc];
2660}
2661
2662int32_t GBCExpanderPass::GetTryItemOffset(uint32_t dex_pc) {
2663 int32_t min = 0;
Ian Rogers89756f22013-03-04 16:40:02 -08002664 int32_t max = dex_compilation_unit_->GetCodeItem()->tries_size_ - 1;
TDYa1275e869b62012-07-25 00:45:39 -07002665
2666 while (min <= max) {
2667 int32_t mid = min + (max - min) / 2;
2668
Ian Rogers89756f22013-03-04 16:40:02 -08002669 const art::DexFile::TryItem* ti =
2670 art::DexFile::GetTryItems(*dex_compilation_unit_->GetCodeItem(), mid);
TDYa1275e869b62012-07-25 00:45:39 -07002671 uint32_t start = ti->start_addr_;
2672 uint32_t end = start + ti->insn_count_;
2673
2674 if (dex_pc < start) {
2675 max = mid - 1;
2676 } else if (dex_pc >= end) {
2677 min = mid + 1;
2678 } else {
2679 return mid; // found
2680 }
2681 }
2682
2683 return -1; // not found
2684}
2685
2686llvm::BasicBlock* GBCExpanderPass::GetLandingPadBasicBlock(uint32_t dex_pc) {
2687 // Find the try item for this address in this method
2688 int32_t ti_offset = GetTryItemOffset(dex_pc);
2689
2690 if (ti_offset == -1) {
2691 return NULL; // No landing pad is available for this address.
2692 }
2693
2694 // Check for the existing landing pad basic block
2695 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2696 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2697
2698 if (block_lpad) {
2699 // We have generated landing pad for this try item already. Return the
2700 // same basic block.
2701 return block_lpad;
2702 }
2703
2704 // Get try item from code item
Ian Rogers89756f22013-03-04 16:40:02 -08002705 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*dex_compilation_unit_->GetCodeItem(),
Ian Rogers8e696052013-03-04 09:00:40 -08002706 ti_offset);
TDYa1275e869b62012-07-25 00:45:39 -07002707
2708 std::string lpadname;
2709
2710#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002711 art::StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
TDYa1275e869b62012-07-25 00:45:39 -07002712#endif
2713
2714 // Create landing pad basic block
2715 block_lpad = llvm::BasicBlock::Create(context_, lpadname, func_);
2716
2717 // Change IRBuilder insert point
2718 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2719 irb_.SetInsertPoint(block_lpad);
2720
2721 // Find catch block with matching type
2722 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2723
2724 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
2725
2726 llvm::Value* catch_handler_index_value =
2727 irb_.CreateCall2(irb_.GetRuntime(runtime_support::FindCatchBlock),
2728 method_object_addr, ti_offset_value);
2729
2730 // Switch instruction (Go to unwind basic block by default)
2731 llvm::SwitchInst* sw =
2732 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2733
2734 // Cases with matched catch block
Ian Rogers89756f22013-03-04 16:40:02 -08002735 art::CatchHandlerIterator iter(*dex_compilation_unit_->GetCodeItem(), ti->start_addr_);
TDYa1275e869b62012-07-25 00:45:39 -07002736
2737 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2738 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2739 }
2740
2741 // Restore the orignal insert point for IRBuilder
2742 irb_.restoreIP(irb_ip_original);
2743
2744 // Cache this landing pad
2745 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2746 basic_block_landing_pads_[ti_offset] = block_lpad;
2747
2748 return block_lpad;
2749}
2750
2751llvm::BasicBlock* GBCExpanderPass::GetUnwindBasicBlock() {
2752 // Check the existing unwinding baisc block block
2753 if (basic_block_unwind_ != NULL) {
2754 return basic_block_unwind_;
2755 }
2756
2757 // Create new basic block for unwinding
2758 basic_block_unwind_ =
2759 llvm::BasicBlock::Create(context_, "exception_unwind", func_);
2760
2761 // Change IRBuilder insert point
2762 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2763 irb_.SetInsertPoint(basic_block_unwind_);
2764
2765 // Pop the shadow frame
2766 Expand_PopShadowFrame();
2767
2768 // Emit the code to return default value (zero) for the given return type.
Ian Rogers89756f22013-03-04 16:40:02 -08002769 char ret_shorty = dex_compilation_unit_->GetShorty()[0];
buzbee26f10ee2012-12-21 11:16:29 -08002770 ret_shorty = art::RemapShorty(ret_shorty);
TDYa1275e869b62012-07-25 00:45:39 -07002771 if (ret_shorty == 'V') {
2772 irb_.CreateRetVoid();
2773 } else {
2774 irb_.CreateRet(irb_.getJZero(ret_shorty));
2775 }
2776
2777 // Restore the orignal insert point for IRBuilder
2778 irb_.restoreIP(irb_ip_original);
2779
2780 return basic_block_unwind_;
2781}
2782
2783void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2784 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002785 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002786 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002787 irb_.CreateBr(lpad);
2788 } else {
2789 irb_.CreateBr(GetUnwindBasicBlock());
2790 }
2791}
2792
2793void GBCExpanderPass::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
Jeff Hao9a142652013-01-17 23:10:19 +00002794 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
2795
TDYa1275e869b62012-07-25 00:45:39 -07002796 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2797
2798 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002799 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002800 irb_.GetInsertBlock()));
Jeff Hao9a142652013-01-17 23:10:19 +00002801 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002802 } else {
Jeff Hao9a142652013-01-17 23:10:19 +00002803 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002804 }
2805
2806 irb_.SetInsertPoint(block_cont);
2807}
2808
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002809llvm::Value*
2810GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
2811 llvm::CallInst& call_inst) {
2812 switch (intr_id) {
2813 //==- Thread -----------------------------------------------------------==//
2814 case IntrinsicHelper::GetCurrentThread: {
TDYa127b672d1e2012-06-28 21:21:45 -07002815 return irb_.Runtime().EmitGetCurrentThread();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002816 }
Logan Chien75e4b602012-07-23 14:24:12 -07002817 case IntrinsicHelper::CheckSuspend: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08002818 Expand_TestSuspend(call_inst);
TDYa127890ea892012-08-22 10:49:42 -07002819 return NULL;
2820 }
2821 case IntrinsicHelper::TestSuspend: {
Logan Chiend54a23d2012-07-24 11:19:23 -07002822 Expand_TestSuspend(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002823 return NULL;
2824 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002825 case IntrinsicHelper::MarkGCCard: {
TDYa1279a129452012-07-19 03:10:08 -07002826 Expand_MarkGCCard(call_inst);
2827 return NULL;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002828 }
Logan Chien75e4b602012-07-23 14:24:12 -07002829
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002830 //==- Exception --------------------------------------------------------==//
2831 case IntrinsicHelper::ThrowException: {
2832 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
2833 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002834 case IntrinsicHelper::HLThrowException: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002835 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2836
2837 EmitUpdateDexPC(dex_pc);
2838
2839 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowException),
2840 call_inst.getArgOperand(0));
2841
2842 EmitGuard_ExceptionLandingPad(dex_pc);
2843 return NULL;
2844 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002845 case IntrinsicHelper::GetException: {
TDYa127823433d2012-09-26 16:03:51 -07002846 return irb_.Runtime().EmitGetAndClearException();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002847 }
2848 case IntrinsicHelper::IsExceptionPending: {
2849 return irb_.Runtime().EmitIsExceptionPending();
2850 }
2851 case IntrinsicHelper::FindCatchBlock: {
2852 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
2853 }
2854 case IntrinsicHelper::ThrowDivZeroException: {
2855 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
2856 }
2857 case IntrinsicHelper::ThrowNullPointerException: {
2858 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
2859 }
2860 case IntrinsicHelper::ThrowIndexOutOfBounds: {
2861 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
2862 }
Logan Chien75e4b602012-07-23 14:24:12 -07002863
2864 //==- Const String -----------------------------------------------------==//
2865 case IntrinsicHelper::ConstString: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002866 return Expand_ConstString(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002867 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002868 case IntrinsicHelper::LoadStringFromDexCache: {
2869 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
2870 }
2871 case IntrinsicHelper::ResolveString: {
2872 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
2873 }
Logan Chien75e4b602012-07-23 14:24:12 -07002874
2875 //==- Const Class ------------------------------------------------------==//
2876 case IntrinsicHelper::ConstClass: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002877 return Expand_ConstClass(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002878 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002879 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
2880 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
2881 }
2882 case IntrinsicHelper::LoadTypeFromDexCache: {
2883 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
2884 }
2885 case IntrinsicHelper::InitializeType: {
2886 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
2887 }
Logan Chien75e4b602012-07-23 14:24:12 -07002888
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002889 //==- Lock -------------------------------------------------------------==//
2890 case IntrinsicHelper::LockObject: {
2891 Expand_LockObject(call_inst.getArgOperand(0));
2892 return NULL;
2893 }
2894 case IntrinsicHelper::UnlockObject: {
2895 Expand_UnlockObject(call_inst.getArgOperand(0));
2896 return NULL;
2897 }
Logan Chien75e4b602012-07-23 14:24:12 -07002898
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002899 //==- Cast -------------------------------------------------------------==//
2900 case IntrinsicHelper::CheckCast: {
2901 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
2902 }
Logan Chien75e4b602012-07-23 14:24:12 -07002903 case IntrinsicHelper::HLCheckCast: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002904 Expand_HLCheckCast(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002905 return NULL;
2906 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002907 case IntrinsicHelper::IsAssignable: {
2908 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
2909 }
Logan Chien75e4b602012-07-23 14:24:12 -07002910
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002911 //==- Alloc ------------------------------------------------------------==//
2912 case IntrinsicHelper::AllocObject: {
2913 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
2914 }
2915 case IntrinsicHelper::AllocObjectWithAccessCheck: {
2916 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
2917 }
Logan Chien75e4b602012-07-23 14:24:12 -07002918
2919 //==- Instance ---------------------------------------------------------==//
2920 case IntrinsicHelper::NewInstance: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002921 return Expand_NewInstance(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002922 }
2923 case IntrinsicHelper::InstanceOf: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002924 return Expand_InstanceOf(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002925 }
2926
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002927 //==- Array ------------------------------------------------------------==//
Logan Chien75e4b602012-07-23 14:24:12 -07002928 case IntrinsicHelper::NewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002929 return Expand_NewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002930 }
2931 case IntrinsicHelper::OptArrayLength: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002932 return Expand_OptArrayLength(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002933 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002934 case IntrinsicHelper::ArrayLength: {
2935 return EmitLoadArrayLength(call_inst.getArgOperand(0));
2936 }
2937 case IntrinsicHelper::AllocArray: {
2938 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
2939 }
2940 case IntrinsicHelper::AllocArrayWithAccessCheck: {
2941 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
2942 call_inst);
2943 }
2944 case IntrinsicHelper::CheckAndAllocArray: {
2945 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
2946 }
2947 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
2948 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
2949 call_inst);
2950 }
2951 case IntrinsicHelper::ArrayGet: {
2952 return Expand_ArrayGet(call_inst.getArgOperand(0),
2953 call_inst.getArgOperand(1),
2954 kInt);
2955 }
2956 case IntrinsicHelper::ArrayGetWide: {
2957 return Expand_ArrayGet(call_inst.getArgOperand(0),
2958 call_inst.getArgOperand(1),
2959 kLong);
2960 }
2961 case IntrinsicHelper::ArrayGetObject: {
2962 return Expand_ArrayGet(call_inst.getArgOperand(0),
2963 call_inst.getArgOperand(1),
2964 kObject);
2965 }
2966 case IntrinsicHelper::ArrayGetBoolean: {
2967 return Expand_ArrayGet(call_inst.getArgOperand(0),
2968 call_inst.getArgOperand(1),
2969 kBoolean);
2970 }
2971 case IntrinsicHelper::ArrayGetByte: {
2972 return Expand_ArrayGet(call_inst.getArgOperand(0),
2973 call_inst.getArgOperand(1),
2974 kByte);
2975 }
2976 case IntrinsicHelper::ArrayGetChar: {
2977 return Expand_ArrayGet(call_inst.getArgOperand(0),
2978 call_inst.getArgOperand(1),
2979 kChar);
2980 }
2981 case IntrinsicHelper::ArrayGetShort: {
2982 return Expand_ArrayGet(call_inst.getArgOperand(0),
2983 call_inst.getArgOperand(1),
2984 kShort);
2985 }
2986 case IntrinsicHelper::ArrayPut: {
2987 Expand_ArrayPut(call_inst.getArgOperand(0),
2988 call_inst.getArgOperand(1),
2989 call_inst.getArgOperand(2),
2990 kInt);
2991 return NULL;
2992 }
2993 case IntrinsicHelper::ArrayPutWide: {
2994 Expand_ArrayPut(call_inst.getArgOperand(0),
2995 call_inst.getArgOperand(1),
2996 call_inst.getArgOperand(2),
2997 kLong);
2998 return NULL;
2999 }
3000 case IntrinsicHelper::ArrayPutObject: {
3001 Expand_ArrayPut(call_inst.getArgOperand(0),
3002 call_inst.getArgOperand(1),
3003 call_inst.getArgOperand(2),
3004 kObject);
3005 return NULL;
3006 }
3007 case IntrinsicHelper::ArrayPutBoolean: {
3008 Expand_ArrayPut(call_inst.getArgOperand(0),
3009 call_inst.getArgOperand(1),
3010 call_inst.getArgOperand(2),
3011 kBoolean);
3012 return NULL;
3013 }
3014 case IntrinsicHelper::ArrayPutByte: {
3015 Expand_ArrayPut(call_inst.getArgOperand(0),
3016 call_inst.getArgOperand(1),
3017 call_inst.getArgOperand(2),
3018 kByte);
3019 return NULL;
3020 }
3021 case IntrinsicHelper::ArrayPutChar: {
3022 Expand_ArrayPut(call_inst.getArgOperand(0),
3023 call_inst.getArgOperand(1),
3024 call_inst.getArgOperand(2),
3025 kChar);
3026 return NULL;
3027 }
3028 case IntrinsicHelper::ArrayPutShort: {
3029 Expand_ArrayPut(call_inst.getArgOperand(0),
3030 call_inst.getArgOperand(1),
3031 call_inst.getArgOperand(2),
3032 kShort);
3033 return NULL;
3034 }
3035 case IntrinsicHelper::CheckPutArrayElement: {
3036 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
3037 }
3038 case IntrinsicHelper::FilledNewArray: {
3039 Expand_FilledNewArray(call_inst);
3040 return NULL;
3041 }
3042 case IntrinsicHelper::FillArrayData: {
3043 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
3044 }
Logan Chien75e4b602012-07-23 14:24:12 -07003045 case IntrinsicHelper::HLFillArrayData: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003046 Expand_HLFillArrayData(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003047 return NULL;
3048 }
3049 case IntrinsicHelper::HLFilledNewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003050 return Expand_HLFilledNewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003051 }
3052
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003053 //==- Instance Field ---------------------------------------------------==//
3054 case IntrinsicHelper::InstanceFieldGet:
3055 case IntrinsicHelper::InstanceFieldGetBoolean:
3056 case IntrinsicHelper::InstanceFieldGetByte:
3057 case IntrinsicHelper::InstanceFieldGetChar:
3058 case IntrinsicHelper::InstanceFieldGetShort: {
3059 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
3060 }
3061 case IntrinsicHelper::InstanceFieldGetWide: {
3062 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
3063 }
3064 case IntrinsicHelper::InstanceFieldGetObject: {
3065 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
3066 }
3067 case IntrinsicHelper::InstanceFieldGetFast: {
3068 return Expand_IGetFast(call_inst.getArgOperand(0),
3069 call_inst.getArgOperand(1),
3070 call_inst.getArgOperand(2),
3071 kInt);
3072 }
3073 case IntrinsicHelper::InstanceFieldGetWideFast: {
3074 return Expand_IGetFast(call_inst.getArgOperand(0),
3075 call_inst.getArgOperand(1),
3076 call_inst.getArgOperand(2),
3077 kLong);
3078 }
3079 case IntrinsicHelper::InstanceFieldGetObjectFast: {
3080 return Expand_IGetFast(call_inst.getArgOperand(0),
3081 call_inst.getArgOperand(1),
3082 call_inst.getArgOperand(2),
3083 kObject);
3084 }
3085 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
3086 return Expand_IGetFast(call_inst.getArgOperand(0),
3087 call_inst.getArgOperand(1),
3088 call_inst.getArgOperand(2),
3089 kBoolean);
3090 }
3091 case IntrinsicHelper::InstanceFieldGetByteFast: {
3092 return Expand_IGetFast(call_inst.getArgOperand(0),
3093 call_inst.getArgOperand(1),
3094 call_inst.getArgOperand(2),
3095 kByte);
3096 }
3097 case IntrinsicHelper::InstanceFieldGetCharFast: {
3098 return Expand_IGetFast(call_inst.getArgOperand(0),
3099 call_inst.getArgOperand(1),
3100 call_inst.getArgOperand(2),
3101 kChar);
3102 }
3103 case IntrinsicHelper::InstanceFieldGetShortFast: {
3104 return Expand_IGetFast(call_inst.getArgOperand(0),
3105 call_inst.getArgOperand(1),
3106 call_inst.getArgOperand(2),
3107 kShort);
3108 }
3109 case IntrinsicHelper::InstanceFieldPut:
3110 case IntrinsicHelper::InstanceFieldPutBoolean:
3111 case IntrinsicHelper::InstanceFieldPutByte:
3112 case IntrinsicHelper::InstanceFieldPutChar:
3113 case IntrinsicHelper::InstanceFieldPutShort: {
3114 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
3115 }
3116 case IntrinsicHelper::InstanceFieldPutWide: {
3117 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
3118 }
3119 case IntrinsicHelper::InstanceFieldPutObject: {
3120 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
3121 }
3122 case IntrinsicHelper::InstanceFieldPutFast: {
3123 Expand_IPutFast(call_inst.getArgOperand(0),
3124 call_inst.getArgOperand(1),
3125 call_inst.getArgOperand(2),
3126 call_inst.getArgOperand(3),
3127 kInt);
3128 return NULL;
3129 }
3130 case IntrinsicHelper::InstanceFieldPutWideFast: {
3131 Expand_IPutFast(call_inst.getArgOperand(0),
3132 call_inst.getArgOperand(1),
3133 call_inst.getArgOperand(2),
3134 call_inst.getArgOperand(3),
3135 kLong);
3136 return NULL;
3137 }
3138 case IntrinsicHelper::InstanceFieldPutObjectFast: {
3139 Expand_IPutFast(call_inst.getArgOperand(0),
3140 call_inst.getArgOperand(1),
3141 call_inst.getArgOperand(2),
3142 call_inst.getArgOperand(3),
3143 kObject);
3144 return NULL;
3145 }
3146 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
3147 Expand_IPutFast(call_inst.getArgOperand(0),
3148 call_inst.getArgOperand(1),
3149 call_inst.getArgOperand(2),
3150 call_inst.getArgOperand(3),
3151 kBoolean);
3152 return NULL;
3153 }
3154 case IntrinsicHelper::InstanceFieldPutByteFast: {
3155 Expand_IPutFast(call_inst.getArgOperand(0),
3156 call_inst.getArgOperand(1),
3157 call_inst.getArgOperand(2),
3158 call_inst.getArgOperand(3),
3159 kByte);
3160 return NULL;
3161 }
3162 case IntrinsicHelper::InstanceFieldPutCharFast: {
3163 Expand_IPutFast(call_inst.getArgOperand(0),
3164 call_inst.getArgOperand(1),
3165 call_inst.getArgOperand(2),
3166 call_inst.getArgOperand(3),
3167 kChar);
3168 return NULL;
3169 }
3170 case IntrinsicHelper::InstanceFieldPutShortFast: {
3171 Expand_IPutFast(call_inst.getArgOperand(0),
3172 call_inst.getArgOperand(1),
3173 call_inst.getArgOperand(2),
3174 call_inst.getArgOperand(3),
3175 kShort);
3176 return NULL;
3177 }
Logan Chien75e4b602012-07-23 14:24:12 -07003178
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003179 //==- Static Field -----------------------------------------------------==//
3180 case IntrinsicHelper::StaticFieldGet:
3181 case IntrinsicHelper::StaticFieldGetBoolean:
3182 case IntrinsicHelper::StaticFieldGetByte:
3183 case IntrinsicHelper::StaticFieldGetChar:
3184 case IntrinsicHelper::StaticFieldGetShort: {
3185 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
3186 }
3187 case IntrinsicHelper::StaticFieldGetWide: {
3188 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
3189 }
3190 case IntrinsicHelper::StaticFieldGetObject: {
3191 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
3192 }
3193 case IntrinsicHelper::StaticFieldGetFast: {
3194 return Expand_SGetFast(call_inst.getArgOperand(0),
3195 call_inst.getArgOperand(1),
3196 call_inst.getArgOperand(2),
3197 kInt);
3198 }
3199 case IntrinsicHelper::StaticFieldGetWideFast: {
3200 return Expand_SGetFast(call_inst.getArgOperand(0),
3201 call_inst.getArgOperand(1),
3202 call_inst.getArgOperand(2),
3203 kLong);
3204 }
3205 case IntrinsicHelper::StaticFieldGetObjectFast: {
3206 return Expand_SGetFast(call_inst.getArgOperand(0),
3207 call_inst.getArgOperand(1),
3208 call_inst.getArgOperand(2),
3209 kObject);
3210 }
3211 case IntrinsicHelper::StaticFieldGetBooleanFast: {
3212 return Expand_SGetFast(call_inst.getArgOperand(0),
3213 call_inst.getArgOperand(1),
3214 call_inst.getArgOperand(2),
3215 kBoolean);
3216 }
3217 case IntrinsicHelper::StaticFieldGetByteFast: {
3218 return Expand_SGetFast(call_inst.getArgOperand(0),
3219 call_inst.getArgOperand(1),
3220 call_inst.getArgOperand(2),
3221 kByte);
3222 }
3223 case IntrinsicHelper::StaticFieldGetCharFast: {
3224 return Expand_SGetFast(call_inst.getArgOperand(0),
3225 call_inst.getArgOperand(1),
3226 call_inst.getArgOperand(2),
3227 kChar);
3228 }
3229 case IntrinsicHelper::StaticFieldGetShortFast: {
3230 return Expand_SGetFast(call_inst.getArgOperand(0),
3231 call_inst.getArgOperand(1),
3232 call_inst.getArgOperand(2),
3233 kShort);
3234 }
3235 case IntrinsicHelper::StaticFieldPut:
3236 case IntrinsicHelper::StaticFieldPutBoolean:
3237 case IntrinsicHelper::StaticFieldPutByte:
3238 case IntrinsicHelper::StaticFieldPutChar:
3239 case IntrinsicHelper::StaticFieldPutShort: {
3240 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
3241 }
3242 case IntrinsicHelper::StaticFieldPutWide: {
3243 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
3244 }
3245 case IntrinsicHelper::StaticFieldPutObject: {
3246 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
3247 }
3248 case IntrinsicHelper::StaticFieldPutFast: {
3249 Expand_SPutFast(call_inst.getArgOperand(0),
3250 call_inst.getArgOperand(1),
3251 call_inst.getArgOperand(2),
3252 call_inst.getArgOperand(3),
3253 kInt);
3254 return NULL;
3255 }
3256 case IntrinsicHelper::StaticFieldPutWideFast: {
3257 Expand_SPutFast(call_inst.getArgOperand(0),
3258 call_inst.getArgOperand(1),
3259 call_inst.getArgOperand(2),
3260 call_inst.getArgOperand(3),
3261 kLong);
3262 return NULL;
3263 }
3264 case IntrinsicHelper::StaticFieldPutObjectFast: {
3265 Expand_SPutFast(call_inst.getArgOperand(0),
3266 call_inst.getArgOperand(1),
3267 call_inst.getArgOperand(2),
3268 call_inst.getArgOperand(3),
3269 kObject);
3270 return NULL;
3271 }
3272 case IntrinsicHelper::StaticFieldPutBooleanFast: {
3273 Expand_SPutFast(call_inst.getArgOperand(0),
3274 call_inst.getArgOperand(1),
3275 call_inst.getArgOperand(2),
3276 call_inst.getArgOperand(3),
3277 kBoolean);
3278 return NULL;
3279 }
3280 case IntrinsicHelper::StaticFieldPutByteFast: {
3281 Expand_SPutFast(call_inst.getArgOperand(0),
3282 call_inst.getArgOperand(1),
3283 call_inst.getArgOperand(2),
3284 call_inst.getArgOperand(3),
3285 kByte);
3286 return NULL;
3287 }
3288 case IntrinsicHelper::StaticFieldPutCharFast: {
3289 Expand_SPutFast(call_inst.getArgOperand(0),
3290 call_inst.getArgOperand(1),
3291 call_inst.getArgOperand(2),
3292 call_inst.getArgOperand(3),
3293 kChar);
3294 return NULL;
3295 }
3296 case IntrinsicHelper::StaticFieldPutShortFast: {
3297 Expand_SPutFast(call_inst.getArgOperand(0),
3298 call_inst.getArgOperand(1),
3299 call_inst.getArgOperand(2),
3300 call_inst.getArgOperand(3),
3301 kShort);
3302 return NULL;
3303 }
3304 case IntrinsicHelper::LoadDeclaringClassSSB: {
3305 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
3306 }
3307 case IntrinsicHelper::LoadClassSSBFromDexCache: {
3308 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
3309 }
3310 case IntrinsicHelper::InitializeAndLoadClassSSB: {
3311 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
3312 }
Logan Chien75e4b602012-07-23 14:24:12 -07003313
3314 //==- High-level Array -------------------------------------------------==//
3315 case IntrinsicHelper::HLArrayGet: {
TDYa1275a26d442012-07-26 18:58:38 -07003316 return Expand_HLArrayGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003317 }
3318 case IntrinsicHelper::HLArrayGetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003319 return Expand_HLArrayGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003320 }
3321 case IntrinsicHelper::HLArrayGetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003322 return Expand_HLArrayGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003323 }
3324 case IntrinsicHelper::HLArrayGetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003325 return Expand_HLArrayGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003326 }
3327 case IntrinsicHelper::HLArrayGetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003328 return Expand_HLArrayGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003329 }
3330 case IntrinsicHelper::HLArrayGetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003331 return Expand_HLArrayGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003332 }
3333 case IntrinsicHelper::HLArrayGetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003334 return Expand_HLArrayGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003335 }
3336 case IntrinsicHelper::HLArrayGetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003337 return Expand_HLArrayGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003338 }
3339 case IntrinsicHelper::HLArrayGetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003340 return Expand_HLArrayGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003341 }
3342 case IntrinsicHelper::HLArrayPut: {
TDYa1275a26d442012-07-26 18:58:38 -07003343 Expand_HLArrayPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003344 return NULL;
3345 }
3346 case IntrinsicHelper::HLArrayPutBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003347 Expand_HLArrayPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003348 return NULL;
3349 }
3350 case IntrinsicHelper::HLArrayPutByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003351 Expand_HLArrayPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003352 return NULL;
3353 }
3354 case IntrinsicHelper::HLArrayPutChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003355 Expand_HLArrayPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003356 return NULL;
3357 }
3358 case IntrinsicHelper::HLArrayPutShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003359 Expand_HLArrayPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003360 return NULL;
3361 }
3362 case IntrinsicHelper::HLArrayPutFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003363 Expand_HLArrayPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003364 return NULL;
3365 }
3366 case IntrinsicHelper::HLArrayPutWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003367 Expand_HLArrayPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003368 return NULL;
3369 }
3370 case IntrinsicHelper::HLArrayPutDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003371 Expand_HLArrayPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003372 return NULL;
3373 }
3374 case IntrinsicHelper::HLArrayPutObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003375 Expand_HLArrayPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003376 return NULL;
3377 }
3378
3379 //==- High-level Instance ----------------------------------------------==//
3380 case IntrinsicHelper::HLIGet: {
TDYa1275e869b62012-07-25 00:45:39 -07003381 return Expand_HLIGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003382 }
3383 case IntrinsicHelper::HLIGetBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003384 return Expand_HLIGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003385 }
3386 case IntrinsicHelper::HLIGetByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003387 return Expand_HLIGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003388 }
3389 case IntrinsicHelper::HLIGetChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003390 return Expand_HLIGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003391 }
3392 case IntrinsicHelper::HLIGetShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003393 return Expand_HLIGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003394 }
3395 case IntrinsicHelper::HLIGetFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003396 return Expand_HLIGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003397 }
3398 case IntrinsicHelper::HLIGetWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003399 return Expand_HLIGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003400 }
3401 case IntrinsicHelper::HLIGetDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003402 return Expand_HLIGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003403 }
3404 case IntrinsicHelper::HLIGetObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003405 return Expand_HLIGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003406 }
3407 case IntrinsicHelper::HLIPut: {
TDYa1275e869b62012-07-25 00:45:39 -07003408 Expand_HLIPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003409 return NULL;
3410 }
3411 case IntrinsicHelper::HLIPutBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003412 Expand_HLIPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003413 return NULL;
3414 }
3415 case IntrinsicHelper::HLIPutByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003416 Expand_HLIPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003417 return NULL;
3418 }
3419 case IntrinsicHelper::HLIPutChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003420 Expand_HLIPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003421 return NULL;
3422 }
3423 case IntrinsicHelper::HLIPutShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003424 Expand_HLIPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003425 return NULL;
3426 }
3427 case IntrinsicHelper::HLIPutFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003428 Expand_HLIPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003429 return NULL;
3430 }
3431 case IntrinsicHelper::HLIPutWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003432 Expand_HLIPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003433 return NULL;
3434 }
3435 case IntrinsicHelper::HLIPutDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003436 Expand_HLIPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003437 return NULL;
3438 }
3439 case IntrinsicHelper::HLIPutObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003440 Expand_HLIPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003441 return NULL;
3442 }
3443
3444 //==- High-level Invoke ------------------------------------------------==//
TDYa127f71bf5a2012-07-29 20:09:52 -07003445 case IntrinsicHelper::HLInvokeVoid:
3446 case IntrinsicHelper::HLInvokeObj:
3447 case IntrinsicHelper::HLInvokeInt:
3448 case IntrinsicHelper::HLInvokeFloat:
3449 case IntrinsicHelper::HLInvokeLong:
Logan Chien75e4b602012-07-23 14:24:12 -07003450 case IntrinsicHelper::HLInvokeDouble: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003451 return Expand_HLInvoke(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003452 }
3453
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003454 //==- Invoke -----------------------------------------------------------==//
3455 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
3456 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
3457 }
3458 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
3459 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
3460 }
3461 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
3462 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
3463 }
3464 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
3465 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
3466 }
3467 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
3468 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
3469 }
3470 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
3471 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
3472 }
3473 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
3474 return Expand_GetVirtualCalleeMethodObjAddrFast(
3475 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
3476 }
3477 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
3478 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
3479 }
3480 case IntrinsicHelper::InvokeRetVoid:
3481 case IntrinsicHelper::InvokeRetBoolean:
3482 case IntrinsicHelper::InvokeRetByte:
3483 case IntrinsicHelper::InvokeRetChar:
3484 case IntrinsicHelper::InvokeRetShort:
3485 case IntrinsicHelper::InvokeRetInt:
3486 case IntrinsicHelper::InvokeRetLong:
3487 case IntrinsicHelper::InvokeRetFloat:
3488 case IntrinsicHelper::InvokeRetDouble:
3489 case IntrinsicHelper::InvokeRetObject: {
3490 return Expand_Invoke(call_inst);
3491 }
Logan Chien75e4b602012-07-23 14:24:12 -07003492
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003493 //==- Math -------------------------------------------------------------==//
3494 case IntrinsicHelper::DivInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003495 return Expand_DivRem(call_inst, /* is_div */true, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003496 }
3497 case IntrinsicHelper::RemInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003498 return Expand_DivRem(call_inst, /* is_div */false, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003499 }
3500 case IntrinsicHelper::DivLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003501 return Expand_DivRem(call_inst, /* is_div */true, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003502 }
3503 case IntrinsicHelper::RemLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003504 return Expand_DivRem(call_inst, /* is_div */false, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003505 }
3506 case IntrinsicHelper::D2L: {
3507 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
3508 }
3509 case IntrinsicHelper::D2I: {
3510 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
3511 }
3512 case IntrinsicHelper::F2L: {
3513 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
3514 }
3515 case IntrinsicHelper::F2I: {
3516 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
3517 }
Logan Chien75e4b602012-07-23 14:24:12 -07003518
3519 //==- High-level Static ------------------------------------------------==//
3520 case IntrinsicHelper::HLSget: {
TDYa1275a26d442012-07-26 18:58:38 -07003521 return Expand_HLSget(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003522 }
3523 case IntrinsicHelper::HLSgetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003524 return Expand_HLSget(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003525 }
3526 case IntrinsicHelper::HLSgetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003527 return Expand_HLSget(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003528 }
3529 case IntrinsicHelper::HLSgetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003530 return Expand_HLSget(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003531 }
3532 case IntrinsicHelper::HLSgetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003533 return Expand_HLSget(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003534 }
3535 case IntrinsicHelper::HLSgetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003536 return Expand_HLSget(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003537 }
3538 case IntrinsicHelper::HLSgetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003539 return Expand_HLSget(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003540 }
3541 case IntrinsicHelper::HLSgetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003542 return Expand_HLSget(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003543 }
3544 case IntrinsicHelper::HLSgetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003545 return Expand_HLSget(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003546 }
3547 case IntrinsicHelper::HLSput: {
TDYa1275a26d442012-07-26 18:58:38 -07003548 Expand_HLSput(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003549 return NULL;
3550 }
3551 case IntrinsicHelper::HLSputBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003552 Expand_HLSput(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003553 return NULL;
3554 }
3555 case IntrinsicHelper::HLSputByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003556 Expand_HLSput(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003557 return NULL;
3558 }
3559 case IntrinsicHelper::HLSputChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003560 Expand_HLSput(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003561 return NULL;
3562 }
3563 case IntrinsicHelper::HLSputShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003564 Expand_HLSput(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003565 return NULL;
3566 }
3567 case IntrinsicHelper::HLSputFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003568 Expand_HLSput(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003569 return NULL;
3570 }
3571 case IntrinsicHelper::HLSputWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003572 Expand_HLSput(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003573 return NULL;
3574 }
3575 case IntrinsicHelper::HLSputDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003576 Expand_HLSput(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003577 return NULL;
3578 }
3579 case IntrinsicHelper::HLSputObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003580 Expand_HLSput(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003581 return NULL;
3582 }
3583
3584 //==- High-level Monitor -----------------------------------------------==//
3585 case IntrinsicHelper::MonitorEnter: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003586 Expand_MonitorEnter(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003587 return NULL;
3588 }
3589 case IntrinsicHelper::MonitorExit: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003590 Expand_MonitorExit(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003591 return NULL;
3592 }
3593
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003594 //==- Shadow Frame -----------------------------------------------------==//
3595 case IntrinsicHelper::AllocaShadowFrame: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08003596 Expand_AllocaShadowFrame(call_inst.getArgOperand(0));
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003597 return NULL;
3598 }
TDYa1278e950c12012-11-02 09:58:19 -07003599 case IntrinsicHelper::SetVReg: {
3600 Expand_SetVReg(call_inst.getArgOperand(0),
3601 call_inst.getArgOperand(1));
3602 return NULL;
3603 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003604 case IntrinsicHelper::PopShadowFrame: {
3605 Expand_PopShadowFrame();
3606 return NULL;
3607 }
3608 case IntrinsicHelper::UpdateDexPC: {
3609 Expand_UpdateDexPC(call_inst.getArgOperand(0));
3610 return NULL;
3611 }
TDYa127a1b21852012-07-23 03:20:39 -07003612
Logan Chien75e4b602012-07-23 14:24:12 -07003613 //==- Comparison -------------------------------------------------------==//
3614 case IntrinsicHelper::CmplFloat:
3615 case IntrinsicHelper::CmplDouble: {
3616 return Expand_FPCompare(call_inst.getArgOperand(0),
3617 call_inst.getArgOperand(1),
3618 false);
3619 }
3620 case IntrinsicHelper::CmpgFloat:
3621 case IntrinsicHelper::CmpgDouble: {
3622 return Expand_FPCompare(call_inst.getArgOperand(0),
3623 call_inst.getArgOperand(1),
3624 true);
3625 }
3626 case IntrinsicHelper::CmpLong: {
3627 return Expand_LongCompare(call_inst.getArgOperand(0),
3628 call_inst.getArgOperand(1));
3629 }
TDYa127a1b21852012-07-23 03:20:39 -07003630
Logan Chien75e4b602012-07-23 14:24:12 -07003631 //==- Const ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003632 case IntrinsicHelper::ConstInt:
3633 case IntrinsicHelper::ConstLong: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003634 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003635 }
TDYa127920be7c2012-09-10 17:13:22 -07003636 case IntrinsicHelper::ConstFloat: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003637 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3638 irb_.getJFloatTy());
Logan Chien75e4b602012-07-23 14:24:12 -07003639 }
TDYa127920be7c2012-09-10 17:13:22 -07003640 case IntrinsicHelper::ConstDouble: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003641 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3642 irb_.getJDoubleTy());
3643 }
TDYa127920be7c2012-09-10 17:13:22 -07003644 case IntrinsicHelper::ConstObj: {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003645 CHECK(LV2UInt(call_inst.getArgOperand(0)) == 0);
3646 return irb_.getJNull();
Logan Chien75e4b602012-07-23 14:24:12 -07003647 }
3648
3649 //==- Method Info ------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003650 case IntrinsicHelper::MethodInfo: {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003651 // Nothing to be done, because MethodInfo carries optional hints that are
3652 // not needed by the portable path.
Logan Chien75e4b602012-07-23 14:24:12 -07003653 return NULL;
3654 }
3655
3656 //==- Copy -------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003657 case IntrinsicHelper::CopyInt:
3658 case IntrinsicHelper::CopyFloat:
3659 case IntrinsicHelper::CopyLong:
3660 case IntrinsicHelper::CopyDouble:
3661 case IntrinsicHelper::CopyObj: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003662 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003663 }
3664
3665 //==- Shift ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003666 case IntrinsicHelper::SHLLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003667 return Expand_IntegerShift(call_inst.getArgOperand(0),
3668 call_inst.getArgOperand(1),
3669 kIntegerSHL, kLong);
3670 }
TDYa127920be7c2012-09-10 17:13:22 -07003671 case IntrinsicHelper::SHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003672 return Expand_IntegerShift(call_inst.getArgOperand(0),
3673 call_inst.getArgOperand(1),
3674 kIntegerSHR, kLong);
3675 }
TDYa127920be7c2012-09-10 17:13:22 -07003676 case IntrinsicHelper::USHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003677 return Expand_IntegerShift(call_inst.getArgOperand(0),
3678 call_inst.getArgOperand(1),
3679 kIntegerUSHR, kLong);
3680 }
TDYa127920be7c2012-09-10 17:13:22 -07003681 case IntrinsicHelper::SHLInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003682 return Expand_IntegerShift(call_inst.getArgOperand(0),
3683 call_inst.getArgOperand(1),
3684 kIntegerSHL, kInt);
3685 }
TDYa127920be7c2012-09-10 17:13:22 -07003686 case IntrinsicHelper::SHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003687 return Expand_IntegerShift(call_inst.getArgOperand(0),
3688 call_inst.getArgOperand(1),
3689 kIntegerSHR, kInt);
3690 }
TDYa127920be7c2012-09-10 17:13:22 -07003691 case IntrinsicHelper::USHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003692 return Expand_IntegerShift(call_inst.getArgOperand(0),
3693 call_inst.getArgOperand(1),
3694 kIntegerUSHR, kInt);
3695 }
3696
3697 //==- Conversion -------------------------------------------------------==//
TDYa127a1b21852012-07-23 03:20:39 -07003698 case IntrinsicHelper::IntToChar: {
3699 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
3700 irb_.getJIntTy());
3701 }
3702 case IntrinsicHelper::IntToShort: {
3703 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
3704 irb_.getJIntTy());
3705 }
3706 case IntrinsicHelper::IntToByte: {
3707 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
3708 irb_.getJIntTy());
3709 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003710
TDYa12787caa7e2012-08-25 23:23:27 -07003711 //==- Exception --------------------------------------------------------==//
3712 case IntrinsicHelper::CatchTargets: {
TDYa12755e5e6c2012-09-11 15:14:42 -07003713 UpdatePhiInstruction(current_bb_, irb_.GetInsertBlock());
TDYa12787caa7e2012-08-25 23:23:27 -07003714 llvm::SwitchInst* si = llvm::dyn_cast<llvm::SwitchInst>(call_inst.getNextNode());
3715 CHECK(si != NULL);
3716 irb_.CreateBr(si->getDefaultDest());
3717 si->eraseFromParent();
3718 return call_inst.getArgOperand(0);
3719 }
3720
Sebastien Hertz0d43d542013-02-27 19:02:16 +01003721 //==- Constructor barrier-----------------------------------------------==//
3722 case IntrinsicHelper::ConstructorBarrier: {
3723 irb_.CreateMemoryBarrier(art::kStoreStore);
3724 return NULL;
3725 }
3726
Logan Chien75e4b602012-07-23 14:24:12 -07003727 //==- Unknown Cases ----------------------------------------------------==//
3728 case IntrinsicHelper::MaxIntrinsicId:
3729 case IntrinsicHelper::UnknownId:
3730 //default:
3731 // NOTE: "default" is intentionally commented so that C/C++ compiler will
3732 // give some warning on unmatched cases.
3733 // NOTE: We should not implement these cases.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003734 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003735 }
Logan Chien75e4b602012-07-23 14:24:12 -07003736 UNIMPLEMENTED(FATAL) << "Unexpected GBC intrinsic: " << static_cast<int>(intr_id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003737 return NULL;
3738}
3739
3740} // anonymous namespace
3741
3742namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -08003743namespace llvm {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003744
Ian Rogers4c1c2832013-03-04 18:30:13 -08003745::llvm::FunctionPass*
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003746CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Brian Carlstrom265091e2013-01-30 14:08:26 -08003747 CompilerDriver* driver, const DexCompilationUnit* dex_compilation_unit) {
Ian Rogers89756f22013-03-04 16:40:02 -08003748 return new GBCExpanderPass(intrinsic_helper, irb, driver, dex_compilation_unit);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003749}
3750
Ian Rogers4c1c2832013-03-04 18:30:13 -08003751} // namespace llvm
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003752} // namespace art